You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.7 KiB
C
73 lines
1.7 KiB
C
#if __has_include("http/server/request.h")
|
|
|
|
#include "object.h"
|
|
|
|
// Submodules
|
|
#include "http/server/request.h"
|
|
|
|
// Standard Library
|
|
#include <string.h>
|
|
|
|
bool ap_object_handle_http_request_should_provide( struct http_request* req )
|
|
{
|
|
const char* accept_header = http_request_get_header( req, "Accept" );
|
|
const char* user_agent = http_request_get_header( req, "User-Agent" );
|
|
if( accept_header ) {
|
|
printf( "Accept: %s\n", accept_header );
|
|
if( strstr( accept_header, "application/ld+json" ) ) {
|
|
return true;
|
|
}
|
|
if( strstr( accept_header, "application/json" ) ) {
|
|
return true;
|
|
}
|
|
if( strstr( accept_header, "application/activity+json" ) ) {
|
|
return true;
|
|
}
|
|
}
|
|
if( user_agent ) {
|
|
printf( "User-Agent: %s\n", user_agent );
|
|
if( strstr( user_agent, "Pleroma" ) ) {
|
|
return true;
|
|
}
|
|
if( strstr( user_agent, "Mastodon" ) ) {
|
|
return true;
|
|
}
|
|
if( strstr( user_agent, "Akkoma" ) ) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool ap_object_handle_http_request( struct http_request* req, struct ap_object* obj, bool release, bool default_jsonld )
|
|
{
|
|
bool result = false;
|
|
if( ap_object_handle_http_request_should_provide( req ) ) {
|
|
goto json_ld;
|
|
} else if( default_jsonld ) {
|
|
goto json_ld;
|
|
} else {
|
|
goto failed;
|
|
}
|
|
json_ld:
|
|
FILE* f = http_request_get_response_body( req );
|
|
http_request_add_header( req, "Access-Control-Allow-Origin", "*" );
|
|
http_request_send_headers( req, 200, "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", true );
|
|
|
|
ap_object_write_to_FILE( obj, f );
|
|
goto success;
|
|
cleanup:
|
|
if( release ) { ap_object_free(obj); }
|
|
return result;
|
|
success:
|
|
result = true;
|
|
goto cleanup;
|
|
failed:
|
|
printf( "Not treating this request as a JSON-LD request\n" );
|
|
result = false;
|
|
goto cleanup;
|
|
}
|
|
|
|
#endif
|