commit 481be220476f4cb1a323771cef9073eeaadbfb1e
parent 207b9b30f42f03270001fbba4419b41093073e98
Author: William Casarin <jb55@jb55.com>
Date: Sun, 20 Dec 2020 12:48:08 -0800
write activities
Diffstat:
7 files changed, 56 insertions(+), 10 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -5,3 +5,4 @@
/test_out.ubjson
src/test_json
/corpus/math.json
+/activities.json
diff --git a/src/http.c b/src/http.c
@@ -103,7 +103,7 @@ static int parse_header(struct http_header **prev, struct parser *p)
}
-int parse_http_request(struct http_req *req, struct parser *p)
+int parse_http_request(struct http_req *req, struct parser *p, int size)
{
struct http_header **header;
struct cursor back;
@@ -122,7 +122,15 @@ int parse_http_request(struct http_req *req, struct parser *p)
header = &(*header)->next;
}
- return consume_crlf(&p->cur);
+ if (!consume_crlf(&p->cur)) {
+ note_error(&req->errs, "pre-body crlf");
+ return 0;
+ }
+
+ req->body = p->cur.p;
+ req->body_len = size - (p->cur.p - p->cur.start);
+
+ return 1;
}
void print_http_request(struct http_req *req)
diff --git a/src/http.h b/src/http.h
@@ -28,6 +28,8 @@ struct http_req {
char *method;
char *path;
char *ver;
+ unsigned char *body;
+ unsigned int body_len;
struct client client;
struct http_header *headers;
struct errors errs;
@@ -35,7 +37,7 @@ struct http_req {
void init_http_req(struct http_req *req);
int get_header(struct http_header *header, const char *match, const char **result);
-int parse_http_request(struct http_req *req, struct parser *p);
+int parse_http_request(struct http_req *req, struct parser *p, int size);
void print_http_request(struct http_req *req);
#endif /* WOLFSOCKS_HTTP */
diff --git a/src/inbox.c b/src/inbox.c
@@ -114,5 +114,5 @@ int verify_signature_header(struct sig_header *sig)
}
printf("\n");
- return 0;
+ return 1;
}
diff --git a/src/json.c b/src/json.c
@@ -6,6 +6,13 @@
#include <assert.h>
#include <ctype.h>
+void init_json_pusher_with(struct json_pusher *p, struct cursor *out)
+{
+ memset(p, 0, sizeof(*p));
+ init_errors(&p->errs);
+ copy_cursor(out, &p->cur);
+}
+
void init_json_pusher(struct json_pusher *p, unsigned char *buf, size_t size)
{
memset(p, 0, sizeof(*p));
diff --git a/src/json.h b/src/json.h
@@ -27,6 +27,7 @@ struct json_parser {
};
+void init_json_pusher_with(struct json_pusher *p, struct cursor *out);
void init_json_pusher(struct json_pusher *p, unsigned char *buf, size_t size);
void init_json_handlers(struct json_handlers *h);
void make_compact_handlers(struct json_handlers *h, struct json_pusher *out);
diff --git a/src/wolfsocks.c b/src/wolfsocks.c
@@ -14,6 +14,7 @@
#include "http.h"
#include "inbox.h"
+#include "json.h"
#define BUF_SIZE 4096
#define ARENA_SIZE 134217728 /* 128 MB virtual mem arena */
@@ -34,7 +35,7 @@ static void error(char *msg)
}
#define SCHEMA "https://"
-#define HOST "fe6d51cd2814.ngrok.io"
+#define HOST "6422dbd13e2f.ngrok.io"
static void init_test_webfinger(struct webfinger *finger)
{
@@ -109,8 +110,14 @@ static void http_log(struct http_req *req)
static int handle_inbox_request(struct http_req *req, struct cursor *arena)
{
const char *signature;
+ unsigned char *start;
+ FILE *out;
struct sig_header sig;
+ struct json_parser pull;
+ struct json_pusher push;
+ struct json_handlers compact;
+ make_compact_handlers(&compact, &push);
memset(&sig, 0, sizeof(sig));
if (!get_header(req->headers, "signature", &signature)) {
@@ -130,6 +137,25 @@ static int handle_inbox_request(struct http_req *req, struct cursor *arena)
return 0;
}
+ start = arena->p;
+ init_json_pusher_with(&push, arena);
+ if (!parse_json(req->body, req->body_len, &pull, &compact)) {
+ note_error(&req->errs, "json parse failed");
+ return 0;
+ }
+ copy_cursor(&push.cur, arena);
+
+ if (!(out = fopen("activities.json", "a"))) {
+ note_error(&req->errs, "could not open activities.json");
+ return 0;
+ }
+
+ fwrite(start, arena->p - start, 1, out);
+ fwrite("\n", 1, 1, out);
+ fclose(out);
+
+ write_resp_header(req, "200 OK");
+
return 1;
}
@@ -300,7 +326,7 @@ void run_http_server()
static unsigned char buffer[BUF_SIZE];
unsigned char *arena;
- ssize_t len;
+ ssize_t len, size;
int parent;
struct sockaddr_in server_addr;
@@ -345,18 +371,19 @@ void run_http_server()
http_accept_client(&req, parent);
- while(1) {
- len = read(req.client.socket, (void*)buffer, BUF_SIZE);
+ for(size = 0;1;) {
+ len = read(req.client.socket, (void*)(buffer+size), BUF_SIZE-size);
+ size += len;
if (len == 0) {
break;
}
- if (len != BUF_SIZE) {
+ if (len != (BUF_SIZE-size)) {
break;
}
}
- if (parse_http_request(&req, &parser)) {
+ if (parse_http_request(&req, &parser, size)) {
handle_request(&req, &parser.arena);
}