chibipub

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 6b26836b3665a7a0733dc8f35129cfef66464084
parent 4a275d08525df830c96135a5aa3de4f1778532aa
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 16 Jan 2021 17:08:36 -0800

ap_json: prepare sigbuf for the sigchecker

Let's build the sigbuf before saving to activities.json

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Msrc/ap_json.c | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
Msrc/wolfsocks.c | 2+-
2 files changed, 78 insertions(+), 34 deletions(-)

diff --git a/src/ap_json.c b/src/ap_json.c @@ -4,6 +4,7 @@ #include "base64.h" #include <time.h> +#include <strings.h> static int handle_ap_string(struct json_handlers *h, struct json_strtok *str) @@ -133,61 +134,104 @@ static inline int handle_header(struct ap_json *a, struct http_header *header) return handle_kv(a, header->name, header->value, 1); } -static int handle_request_target(struct ap_json *a) +struct sized_str { + unsigned char *text; + int size; +}; + +static int split_headers(unsigned char *headers, int header_len, + struct sized_str *strs, int max_strs, int *out) { - struct json_pusher *p = (struct json_pusher *)a->compact_handler.data; - struct cursor *c = &p->cur; + int n_strs = 0; + struct sized_str *str; + unsigned char *p, *pstart; + + pstart = p = headers; + for (;; p++) { + if (n_strs == max_strs) + return 0; + + if (*p == ' ' || *p == 0) { + str = &strs[n_strs++]; + str->text = pstart; + str->size = p - pstart; + pstart = p+1; + } + + if (*p == 0) + break; + } - return handle_key(a, "(request-target)") - && push_byte(c, '"') - && push_str(c, a->req->method) - && push_byte(c, ' ') - && push_str(c, a->req->path) - && push_str(c, "\","); + *out = n_strs; + return 1; } -static int handle_wsheaders(struct ap_json *a) +static inline struct http_header *find_header(struct http_header *headers, + unsigned char *name, int name_len) { - unsigned int *nop; struct http_header *header; - struct json_strtok str; - init_json_strtok(&str); - if (!handle_key(a, "@wsheaders")) { + for (header = headers; header; header = header->next) { + if (name_len == strlen(header->name) && + !strncasecmp((const char *)name, header->name, name_len)) { + return header; + } + } + + return NULL; +} + +static int handle_wssigbuf(struct ap_json *a) +{ + struct sized_str strs[16] = {0}; + int n_headers = 0, i; + struct http_header *header; + struct cursor *c = &((struct json_pusher *)a->compact_handler.data)->cur; + struct sized_str *str; + + if (!handle_key(a, "@wssigbuf")) { return 0; } - if (!handle_token(&a->compact_handler, '{', &nop)) { - note_error(&a->errs, "push @wsheaders { tok oob"); + if (!push_byte(c, '"')) { return 0; } - if (!handle_request_target(a)) { - note_error(&a->errs, "push @wsheaders request target oob"); + if (!split_headers((unsigned char*)a->sig->headers, + strlen(a->sig->headers), strs, + sizeof(strs)/sizeof(strs[0]), &n_headers)) { + note_error(&a->errs, "split_headers overflow"); return 0; } - for (header = a->req->headers; header; header = header->next) { - if (!handle_header(a, header)) { - note_error(&a->errs, "header"); + static const char request_target[] = "(request-target)"; + + for (i = 0; i < n_headers; i++) { + str = &strs[i]; + if (!push_data(c, str->text, str->size)) { + return 0; + } + if (!push_str(c, ": ")) { return 0; } - if (header->next) { - if (!handle_token(&a->compact_handler, ',', &nop)) { - note_error(&a->errs, "push header , tok oob"); + if (str->size == (sizeof(request_target)-1) && + !memcmp(request_target, str->text, str->size)) { + if (!push_str(c, "post /inbox")) { + return 0; + } + } else if ((header = find_header(a->req->headers, str->text, str->size))) { + if (!push_str(c, header->value)) { return 0; } + } else { + note_error(&a->errs, "could not find header %.*s", + str->size, str->text); + return 0; } } - if (!handle_token(&a->compact_handler, '}', &nop)) { - note_error(&a->errs, "push @wsheaders } tok oob"); - return 0; - } - - if (!handle_token(&a->compact_handler, ',', &nop)) { - note_error(&a->errs, "push @wsheaders , tok oob"); + if (!push_str(c, "\",")) { return 0; } @@ -265,8 +309,8 @@ static int handle_ap_token(struct json_handlers *h, char token, return 0; } - if (!handle_wsheaders(a)) { - note_error(&a->errs, "push wsheaders"); + if (!handle_wssigbuf(a)) { + note_error(&a->errs, "push wssigbuf"); return 0; } diff --git a/src/wolfsocks.c b/src/wolfsocks.c @@ -482,7 +482,7 @@ static int checksigs() int main(int argc, char *argv[]) { - if (argc >= 1 && !strcmp(argv[1], "checksigs")) { + if (argc >= 2 && !strcmp(argv[1], "checksigs")) { return checksigs(); }