commit 3ffd8c7df60dc102afed2681bd3f5fb7a8ec887a
parent ee8dbd256badedc7a4392accbaa7c48d20d189a7
Author: William Casarin <jb55@jb55.com>
Date: Thu, 14 Jan 2021 15:13:55 -0800
ap_json: cleanup a bit
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
3 files changed, 78 insertions(+), 66 deletions(-)
diff --git a/src/ap_json.c b/src/ap_json.c
@@ -17,22 +17,55 @@ static int handle_ap_string(struct json_handlers *h, struct json_strtok *str)
return handle_string(&apjson->compact_handler, str);
}
-static int handle_wssig(struct ap_json *a)
+static int handle_key(struct ap_json *a, char *key)
{
- struct json_strtok sig;
- init_json_strtok(&sig);
+ struct json_strtok str;
unsigned int *nop;
+ init_json_strtok(&str);
- sig.text = (unsigned char*)"@wssig";
- sig.size = 6;
- sig.is_key = 1;
- if (!handle_string(&a->compact_handler, &sig)) {
- note_error(&a->errs, "push @wssig key oob");
+ str.text = (unsigned char *)key;
+ str.size = strlen(key);
+ str.is_key = 1;
+ if (!handle_string(&a->compact_handler, &str)) {
+ note_error(&a->errs, "push @wsheaders oob");
return 0;
}
if (!handle_token(&a->compact_handler, ':', &nop)) {
- note_error(&a->errs, "push @wssig : tok oob");
+ return 0;
+ }
+
+ return 1;
+}
+
+static int handle_kv(struct ap_json *a, char *key, char *val, int escape_val)
+{
+ struct json_strtok str;
+ init_json_strtok(&str);
+
+ if (!handle_key(a, key)) {
+ return 0;
+ }
+
+ str.text = (unsigned char *)val;
+ str.size = strlen(val);
+ str.is_key = 0;
+ str.needs_escape = escape_val;
+ if (!handle_string(&a->compact_handler, &str)) {
+ note_error(&a->errs, "push @wsheaders oob");
+ return 0;
+ }
+
+ return 1;
+}
+
+static int handle_wssig(struct ap_json *a)
+{
+ struct json_strtok sig;
+ init_json_strtok(&sig);
+ unsigned int *nop;
+
+ if (!handle_key(a, "@wssig")) {
return 0;
}
@@ -58,16 +91,7 @@ static int handle_wsstamp(struct ap_json *a)
struct json_strtok str;
init_json_strtok(&str);
- str.text = (unsigned char *)"@wsstamp";
- str.size = 8;
- str.is_key = 1;
- if (!handle_string(&a->compact_handler, &str)) {
- note_error(&a->errs, "push @wsstamp : tok oob");
- return 0;
- }
-
- if (!handle_token(&a->compact_handler, ':', &nop)) {
- note_error(&a->errs, "push @wsstamp : tok oob");
+ if (!handle_key(a, "@wsstamp")) {
return 0;
}
@@ -90,61 +114,53 @@ static int handle_wskeyid(struct ap_json *a)
struct json_strtok str;
init_json_strtok(&str);
- str.text = (unsigned char *)"@wskeyid";
- str.size = 8;
- str.is_key = 1;
- if (!handle_string(&a->compact_handler, &str)) {
- note_error(&a->errs, "push @wskeyid : tok oob");
+ if (!handle_kv(a, "@wskeyid", (char*)a->sig->key_id, 0)) {
return 0;
}
- if (!handle_token(&a->compact_handler, ':', &nop)) {
- note_error(&a->errs, "push @wskeyid : tok oob");
+ if (!handle_token(&a->compact_handler, ',', &nop)) {
+ note_error(&a->errs, "push @wskeyid , tok oob");
return 0;
}
- str.text = (unsigned char*)a->sig->key_id;
- str.size = strlen(a->sig->key_id);
- str.is_key = 0;
- if (!handle_string(&a->compact_handler, &str)) {
- note_error(&a->errs, "push @wskeyid oob number");
+ return 1;
+}
+
+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 json_pusher *p = (struct json_pusher *)a->compact_handler.data;
+ struct cursor *c = &p->cur;
+
+ if (!handle_key(a, "(request-target)")) {
return 0;
}
- if (!handle_token(&a->compact_handler, ',', &nop)) {
- note_error(&a->errs, "push @wskeyid , tok oob");
+ if (!push_byte(c, '"')) {
return 0;
}
- return 1;
-}
+ if (!push_str(c, a->req->method)) {
+ return 0;
+ }
-static int handle_header(struct ap_json *a, struct http_header *header)
-{
- struct json_strtok str;
- unsigned int *nop;
- init_json_strtok(&str);
+ if (!push_byte(c, ' ')) {
+ return 0;
+ }
- str.text = (unsigned char*)header->name;
- str.size = strlen(header->name);
- str.is_key = 1;
- if (!handle_string(&a->compact_handler, &str)) {
- note_error(&a->errs, "push '%s' header key oob", header->name);
+ if (!push_str(c, a->req->path)) {
return 0;
}
- if (!handle_token(&a->compact_handler, ':', &nop)) {
- note_error(&a->errs, "push header : tok oob");
+ if (!push_byte(c, '"')) {
return 0;
}
- str.text = (unsigned char*)header->value;
- str.size = strlen(header->value);
- str.is_key = 1;
- str.needs_escape = 1;
- if (!handle_string(&a->compact_handler, &str)) {
- note_error(&a->errs, "push '%s' header value '%s' oob",
- header->name, header->value);
+ if (!push_byte(c, ',')) {
return 0;
}
@@ -158,25 +174,21 @@ static int handle_wsheaders(struct ap_json *a)
struct json_strtok str;
init_json_strtok(&str);
- str.text = (unsigned char *)"@wsheaders";
- str.size = 10;
- str.is_key = 1;
- if (!handle_string(&a->compact_handler, &str)) {
- note_error(&a->errs, "push @wsheaders oob");
+ if (!handle_key(a, "@wsheaders")) {
return 0;
}
- if (!handle_token(&a->compact_handler, ':', &nop)) {
- note_error(&a->errs, "push @wsheaders : tok oob");
+ 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 (!handle_request_target(a)) {
+ note_error(&a->errs, "push @wsheaders request target oob");
return 0;
}
- for (header = a->headers; header; header = header->next) {
+ for (header = a->req->headers; header; header = header->next) {
if (!handle_header(a, header)) {
note_error(&a->errs, "header");
return 0;
diff --git a/src/ap_json.h b/src/ap_json.h
@@ -12,7 +12,7 @@
struct ap_json {
struct json_handlers compact_handler;
struct errors errs;
- struct http_header *headers;
+ struct http_req *req;
struct sig_header *sig;
int flags;
};
diff --git a/src/wolfsocks.c b/src/wolfsocks.c
@@ -175,7 +175,7 @@ static int handle_inbox_request(struct http_req *req, struct cursor *arena)
}
apjson.sig = &sig;
- apjson.headers = req->headers;
+ apjson.req = req;
// TODO: defer this check
if (!verify_signature_header(&sig)) {