Update CLI handling, add CORS headers to ActivityPub endpoints to allow https://json-ld.org/playground/ to work, add route for /activity/%d{id}, add contexts to ap_object creation, fix single-digit dates in HTTP Signature Date header

master
teknomunk 1 year ago
parent 48f9a4d074
commit f662610622

@ -1 +1 @@
Subproject commit 60eb914d8bc43b388625cad8be69f2802594f264
Subproject commit eb512d0d70a3095eed12c4cb866ffc9215b95cfe

@ -2,6 +2,8 @@
#include "json/layout.h"
#include "controller/cli.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
@ -15,17 +17,26 @@ static struct json_object_field app_args_layout[] = {
};
#undef OBJ_TYPE
struct app_args* g_server = NULL;
struct app_args* app_args_new( int argc, char** argv )
{
struct app_args* args = (struct app_args*)malloc( sizeof(struct app_args) );
memset(args,0,sizeof(*args));
if( !args ) { return NULL; }
g_server = args;
args->port = 9053;
args->addr = strdup("0.0.0.0");
json_read_object_layout_from_file( "data/server.json", app_args_layout, args );
if( ( argc > 1 ) && ( 0 != strncmp(argv[1],"--",2) ) ) {
handle_command( argv, argc );
free(args);
return NULL;
}
for( int i = 1; i < argc; ++i ) {
const char* arg = argv[i];

@ -35,6 +35,7 @@ bool route_ap_note( struct http_request* req )
struct ap_object* act = activity_create_Note( s );
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
http_request_send_headers( req, 200, "application/ld+json", true );
FILE* f = http_request_get_response_body( req );
ap_object_write_to_FILE( act, f );
@ -47,6 +48,30 @@ bool route_ap_note( struct http_request* req )
return true;
}
// Route: /activity/
bool route_ap_activity( struct http_request* req )
{
char* id_str = http_request_route_get_dir_or_file(req);
if( !id_str || !*id_str ) { return false; }
// Route: /activity/%d{id}
int id = -1;
if( 1 != sscanf( id_str, "%d", &id ) ) { return false; }
free(id_str);
struct ap_object* act = activity_from_local_id( id );
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
http_request_send_headers( req, 200, "application/ld+json", true );
FILE* f = http_request_get_response_body( req );
ap_object_write_to_FILE( act, f );
fflush(f);
ap_object_free(act);
return true;
}
// Route: /outbox
bool route_ap_outbox( struct http_request* req )
{

@ -5,5 +5,6 @@ struct http_request;
#include <stdbool.h>
bool route_ap_note( struct http_request* req );
bool route_ap_activity( struct http_request* req );
bool route_ap_outbox( struct http_request* req );

@ -34,7 +34,7 @@ struct cli_request
static int cli_route_command( struct cli_request* req, const char* cmd, int count, const char* usage_args )
{
if( 0 != strcmp(req->argv[0], cmd ) ) {
//printf( "No match for %s==%s\n", req->argv[0], cmd );
printf( "No match for %s==%s\n", req->argv[0], cmd );
return 0;
}
req->argv += 1;

@ -60,6 +60,9 @@ static bool send_asset( struct http_request* req, const char* fs_path )
return false;
}
if( 0 == strcmp( mime_type, "application/ld+json" ) ) {
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
}
return http_request_send_file( req, fs_path, mime_type );
}
@ -166,6 +169,8 @@ bool route_request( struct http_request* req )
return route_ap_outbox( req );
} else if( http_request_route( req, "/note/" ) ) {
return route_ap_note( req );
} else if( http_request_route( req, "/activity/" ) ){
return route_ap_activity(req);
} else if( http_request_route( req, "/media/" ) ) {
return route_media( req );
} else if( http_request_route( req, "/emoji/" ) ) {

@ -18,6 +18,7 @@ static bool handle_featured( struct http_request* req )
{
struct account* owner_account = account_from_id(0);
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
http_request_send_headers( req, 200, "application/activity+json", true );
FILE* f = http_request_get_response_body(req);
#include "src/view/owner/featured.json.inc"
@ -34,6 +35,7 @@ static bool handle_followers( struct http_request* req )
if( http_request_route_term(req,"") ) {
struct ap_object* obj = account_ap_followers(owner_account);
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
http_request_send_headers( req, 200, "application/activity+json", true );
FILE* f = http_request_get_response_body(req);
ap_object_write_to_FILE( obj, f );
@ -49,6 +51,7 @@ static bool handle_followers( struct http_request* req )
struct ap_object* obj = account_ap_followers_page(owner_account,page);
if( !obj ) { goto failed; }
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
http_request_send_headers( req, 200, "application/activity+json", true );
FILE* f = http_request_get_response_body(req);
ap_object_write_to_FILE( obj, f );
@ -75,6 +78,7 @@ static bool handle_following( struct http_request* req )
if( http_request_route_term(req,"") ) {
struct ap_object* obj = account_ap_following(owner_account);
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
http_request_send_headers( req, 200, "application/activity+json", true );
FILE* f = http_request_get_response_body(req);
ap_object_write_to_FILE( obj, f );
@ -90,6 +94,7 @@ static bool handle_following( struct http_request* req )
struct ap_object* obj = account_ap_following_page(owner_account,page);
if( !obj ) { goto failed; }
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
http_request_send_headers( req, 200, "application/activity+json", true );
FILE* f = http_request_get_response_body(req);
ap_object_write_to_FILE( obj, f );
@ -113,6 +118,7 @@ static bool handle_owner_actor( struct http_request* req )
{
struct account* owner_account = account_from_id(0);
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
http_request_send_headers( req, 200, "application/activity+json", true );
FILE* f = http_request_get_response_body(req);

@ -16,11 +16,9 @@
#include "controller/outbox.h"
#include "controller/test.h"
#include "controller/indexer.h"
#include "controller/cli.h"
#include <curl/curl.h>
struct app_args* g_server = NULL;
bool terminate = false;
void handle_ctrl_c(int)
@ -96,14 +94,8 @@ int main( int argc, char* argv[] )
signal(SIGINT, handle_ctrl_c);
signal(SIGPIPE, SIG_IGN);
if( ( argc > 1 ) && ( 0 != strncmp(argv[1],"--",2) ) ) {
handle_command( argv, argc );
goto exit;
}
g_server = app_args_new( argc, argv );
app_args_new( argc, argv );
if( !g_server ) {
printf( "Error processing argument\n" );
return 1;
}

@ -31,6 +31,8 @@ struct ap_object* account_ap_actor( struct account* a )
act = malloc(sizeof(*act));
memset(act,0,sizeof(*act));
ap_object_add_context( act, "https://www.w3.org/ns/activitystreams");
ap_object_add_context( act, "https://apogee.polaris-1.work/schemas/litepub-0.1.jsonld");
act->published = time(NULL);
act->type = ap_Person;
act->id = aformat( "https://%s/owner/actor", g_server->domain );
@ -87,6 +89,8 @@ struct ap_object* account_ap_outbox( struct account* a )
outbox = malloc(sizeof(*outbox));
memset(outbox,0,sizeof(*outbox));
ap_object_add_context( outbox, "https://www.w3.org/ns/activitystreams");
ap_object_add_context( outbox, "https://apogee.polaris-1.work/schemas/litepub-0.1.jsonld");
outbox->type = ap_OrderedCollection;
outbox->published = time(NULL);
outbox->id = aformat( "https://%s/outbox", g_server->domain );
@ -109,6 +113,8 @@ struct ap_object* account_ap_outbox_page( struct account* a, int page )
struct ap_object* outbox;
outbox = malloc(sizeof(*outbox));
memset(outbox,0,sizeof(*outbox));
ap_object_add_context( outbox, "https://www.w3.org/ns/activitystreams");
ap_object_add_context( outbox, "https://apogee.polaris-1.work/schemas/litepub-0.1.jsonld");
struct {
char** items;
@ -190,6 +196,8 @@ static struct ap_object* account_list_page( int page, char* part_of, const char*
struct ap_object* o;
o = malloc(sizeof(*o));
memset(o,0,sizeof(*o));
ap_object_add_context( o, "https://www.w3.org/ns/activitystreams");
ap_object_add_context( o, "https://apogee.polaris-1.work/schemas/litepub-0.1.jsonld");
struct {
char** items;
@ -234,6 +242,8 @@ struct ap_object* account_ap_followers( struct account* a )
struct ap_object* o;
o = malloc(sizeof(*o));
memset(o,0,sizeof(*o));
ap_object_add_context( o, "https://www.w3.org/ns/activitystreams");
ap_object_add_context( o, "https://apogee.polaris-1.work/schemas/litepub-0.1.jsonld");
o->type = ap_OrderedCollection;
o->published = time(NULL);
@ -267,6 +277,8 @@ struct ap_object* account_ap_following( struct account* a )
struct ap_object* o;
o = malloc(sizeof(*o));
memset(o,0,sizeof(*o));
ap_object_add_context( o, "https://www.w3.org/ns/activitystreams");
ap_object_add_context( o, "https://apogee.polaris-1.work/schemas/litepub-0.1.jsonld");
o->type = ap_OrderedCollection;
o->published = time(NULL);

@ -36,7 +36,7 @@ bool http_signature_make( const char* inbox, struct crypto_keys* keys, struct ht
static const char* month_of_year[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
char date[32];
snprintf( date, sizeof(date),
"%s, %d %s %d %02d:%02d:%02d GMT",
"%s, %02d %s %d %02d:%02d:%02d GMT",
day_of_week[ gmtime_data.tm_wday ],
gmtime_data.tm_mday,
month_of_year[ gmtime_data.tm_mon ],

Loading…
Cancel
Save