parent
aad7a1f82f
commit
9e6e2cf556
@ -0,0 +1,107 @@
|
||||
|
||||
#include "http/client/client.h"
|
||||
#include "json/layout.h"
|
||||
#include "format.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
struct link
|
||||
{
|
||||
char* href;
|
||||
char* rel;
|
||||
char* type;
|
||||
};
|
||||
#define OBJ_TYPE struct link
|
||||
static struct json_object_field link_layout[] = {
|
||||
JSON_FIELD_STRING( href, true ),
|
||||
JSON_FIELD_STRING( rel, true ),
|
||||
JSON_FIELD_STRING( type, true ),
|
||||
JSON_FIELD_END,
|
||||
};
|
||||
#undef OBJ_TYPE
|
||||
void link_free( struct link* l )
|
||||
{
|
||||
if( !l ) { return; }
|
||||
|
||||
free(l->href);
|
||||
free(l->type);
|
||||
free(l->rel);
|
||||
}
|
||||
JSON_FIELD_TYPE_OBJECT_LAYOUT_WITH_DEFAULTS( link );
|
||||
struct result
|
||||
{
|
||||
struct {
|
||||
struct link** items;
|
||||
int count;
|
||||
} links;
|
||||
};
|
||||
|
||||
#define OBJ_TYPE struct result
|
||||
static struct json_object_field result_layout[] = {
|
||||
JSON_FIELD_ARRAY_OF_TYPE( links, true, link_type ),
|
||||
JSON_FIELD_END,
|
||||
};
|
||||
#undef OBJ_TYPE
|
||||
|
||||
char* webfinger_query( const char* handle, const char* rel, const char* type )
|
||||
{
|
||||
char handle2[512];
|
||||
strncpy( handle2, handle, 512 );
|
||||
char* server = NULL;
|
||||
char* username = strtok_r( handle2, "@", &server );
|
||||
|
||||
char url[512];
|
||||
mkdir( "data/webfinger/", 0755 );
|
||||
const char* uri = format( url, sizeof(url), "https://%s/.well-known/webfinger?resource=acct:%s", server, handle );
|
||||
|
||||
char filename[512];
|
||||
snprintf( filename, sizeof(filename), "data/webfinger/%s.json", handle );
|
||||
|
||||
printf( "* Fetching %s\n", uri );
|
||||
char tmp_filename[512];
|
||||
FILE* f = fopen(format(tmp_filename,512,"%s.tmp",filename),"w");
|
||||
|
||||
long status_code = -1;
|
||||
const void* request[] = {
|
||||
HTTP_REQ_URL, uri,
|
||||
HTTP_REQ_OUTFILE, f,
|
||||
HTTP_REQ_RESULT_STATUS, &status_code,
|
||||
NULL,
|
||||
};
|
||||
if( !http_client_do( request ) ) {
|
||||
printf( "! Unable to fetch %s\n", uri );
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
printf( "status_code = %d\n", status_code );
|
||||
if( status_code != 200 ) {
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
fclose(f);
|
||||
rename(tmp_filename,filename);
|
||||
|
||||
// Get the href value requested
|
||||
struct result r;
|
||||
memset(&r,0,sizeof(r));
|
||||
|
||||
json_read_object_layout_from_file( filename, result_layout, &r );
|
||||
char* value = NULL;
|
||||
|
||||
for( int i = 0; i < r.links.count; ++i ) {
|
||||
if( 0 == strcmp( rel, r.links.items[i]->rel ) ) {
|
||||
if( 0 == strcmp( type, r.links.items[i]->type ) ) {
|
||||
free(value);
|
||||
value = strdup(r.links.items[i]->href);
|
||||
}
|
||||
}
|
||||
link_free(r.links.items[i]);
|
||||
}
|
||||
free(r.links.items);
|
||||
|
||||
return value;
|
||||
}
|
Loading…
Reference in new issue