parent
38856cb9ad
commit
d52b2643b9
@ -0,0 +1,81 @@
|
||||
#include "inbox.h"
|
||||
|
||||
#include "http_server/http_request.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
static void write_json_escaped( FILE* f, const char* str )
|
||||
{
|
||||
while( *str ) {
|
||||
switch( *str ) {
|
||||
case '\"':
|
||||
fprintf( f, "\\\"" );
|
||||
break;
|
||||
case '\n':
|
||||
fprintf( f, "\\\n" );
|
||||
break;
|
||||
case '\t':
|
||||
fprintf( f, "\\\t" );
|
||||
break;
|
||||
default:
|
||||
fputc( *str, f );
|
||||
}
|
||||
++str;
|
||||
}
|
||||
}
|
||||
|
||||
static void io_copy( FILE* in, FILE* out )
|
||||
{
|
||||
char buffer[512];
|
||||
for(;;) {
|
||||
int count = fread( buffer, 1, 512, in );
|
||||
if( count == 0 ) { return; }
|
||||
fwrite( buffer, 1, count, out );
|
||||
}
|
||||
}
|
||||
|
||||
bool route_inbox( struct http_request* req )
|
||||
{
|
||||
// No subroutes
|
||||
if( !http_request_route_term( req, "" ) ) { return false; }
|
||||
if( !http_request_route_method( req, "POST" ) ) { return false; }
|
||||
|
||||
printf( "Queue inbox items for later processing\n" );
|
||||
|
||||
const char* signature = http_request_get_header(req,"Signature");
|
||||
const char* date = http_request_get_header(req,"Date");
|
||||
|
||||
printf( "Signature: %s\nDate: %s", signature, date );
|
||||
|
||||
FILE* body = http_request_get_request_data(req);
|
||||
|
||||
uint64_t time_ns;
|
||||
struct timespec ts;
|
||||
clock_gettime( CLOCK_REALTIME, &ts );
|
||||
time_ns = (uint64_t)ts.tv_sec * 1000000000 + (uint64_t)ts.tv_nsec;
|
||||
|
||||
char filename[512];
|
||||
snprintf( filename, 512, "data/inbox/%llu.json", time_ns );
|
||||
char tmp_filename[512+32];
|
||||
snprintf( tmp_filename, 512+32, "%s.tmp-%d", filename, rand() );
|
||||
|
||||
FILE* f = fopen( tmp_filename, "w" );
|
||||
if( !f ) {
|
||||
printf( "Failed to open %s\n", tmp_filename );
|
||||
return false;
|
||||
}
|
||||
#define RENDER
|
||||
#include "src/view/inbox_envelope.json.inc"
|
||||
#undef RENDER
|
||||
fclose(f);
|
||||
|
||||
rename( tmp_filename, filename );
|
||||
|
||||
http_request_send_headers( req, 200, "text/plain", true );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
struct http_request;
|
||||
|
||||
bool route_inbox( struct http_request* req );
|
||||
|
@ -0,0 +1,5 @@
|
||||
{
|
||||
"signature": "%( write_json_escaped( f, signature ); )",
|
||||
"date": "%( write_json_escaped( f, signature ); )",
|
||||
"body": %( io_copy( body, f ); )
|
||||
}
|
Loading…
Reference in new issue