From b070d2d8f88866bf83103c34c4d86352c6b74e8d Mon Sep 17 00:00:00 2001 From: default Date: Sun, 25 Sep 2022 07:42:57 +0200 Subject: [PATCH] The HTTP request headers are stored in a plain dict. --- activitypub.c | 6 ++---- httpd.c | 10 +++------- snac.c | 3 +-- xs_httpd.h | 31 +++++++++++++++---------------- 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/activitypub.c b/activitypub.c index 976222a..ec1d820 100644 --- a/activitypub.c +++ b/activitypub.c @@ -258,8 +258,7 @@ int activitypub_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype) { int status = 200; - char *headers = xs_dict_get(req, "headers"); - char *accept = xs_dict_get(headers, "accept"); + char *accept = xs_dict_get(req, "accept"); snac snac; xs *msg = NULL; @@ -321,8 +320,7 @@ int activitypub_post_handler(d_char *req, char *q_path, /* processes an input message */ { int status = 202; /* accepted */ - char *headers = xs_dict_get(req, "headers"); - char *i_ctype = xs_dict_get(headers, "content-type"); + char *i_ctype = xs_dict_get(req, "content-type"); snac snac; if (i_ctype == NULL) diff --git a/httpd.c b/httpd.c index bf72d5a..7728d04 100644 --- a/httpd.c +++ b/httpd.c @@ -24,8 +24,7 @@ int server_get_handler(d_char *req, char *q_path, /* basic server services */ { int status = 0; - char *req_hdrs = xs_dict_get(req, "headers"); - char *acpt = xs_dict_get(req_hdrs, "accept"); + char *acpt = xs_dict_get(req, "accept"); if (acpt == NULL) return 400; @@ -90,7 +89,6 @@ void httpd_connection(int rs) { FILE *f; xs *req; - char *req_hdrs; char *method; int status = 0; char *body = NULL; @@ -106,10 +104,8 @@ void httpd_connection(int rs) req = xs_httpd_request(f, &payload, &p_size); - req_hdrs = xs_dict_get(req, "headers"); - - method = xs_dict_get(req_hdrs, "method"); - q_path = xs_dup(xs_dict_get(req_hdrs, "path")); + method = xs_dict_get(req, "method"); + q_path = xs_dup(xs_dict_get(req, "path")); /* crop the q_path from leading / and the prefix */ if (xs_endswith(q_path, "/")) diff --git a/snac.c b/snac.c index ba50a79..f5e23bc 100644 --- a/snac.c +++ b/snac.c @@ -163,8 +163,7 @@ void srv_archive(char *direction, char *req, char *payload, int p_size, if (p_size && payload) { xs *payload_fn; - char *h = xs_dict_get(req, "headers"); - char *v = xs_dict_get(h, "content-type"); + char *v = xs_dict_get(req, "content-type"); if (v && xs_str_in(v, "json") != -1) { payload_fn = xs_fmt("%s/payload.json", dir); diff --git a/xs_httpd.h b/xs_httpd.h index d56226f..0842e15 100644 --- a/xs_httpd.h +++ b/xs_httpd.h @@ -72,10 +72,9 @@ d_char *xs_url_vars(char *str) d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) /* processes an httpd connection */ { - xs *headers = NULL; + d_char *req = NULL; xs *q_vars = NULL; xs *p_vars = NULL; - d_char *req = NULL; xs *l1, *l2; char *v; @@ -90,10 +89,10 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) return NULL; } - headers = xs_dict_new(); + req = xs_dict_new(); - headers = xs_dict_append(headers, "method", xs_list_get(l2, 0)); - headers = xs_dict_append(headers, "proto", xs_list_get(l2, 2)); + req = xs_dict_append(req, "method", xs_list_get(l2, 0)); + req = xs_dict_append(req, "proto", xs_list_get(l2, 2)); { /* split the path with its optional variables */ @@ -101,7 +100,7 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) xs *pnv = xs_split_n(udp, "?", 1); /* store the path */ - headers = xs_dict_append(headers, "path", xs_list_get(pnv, 0)); + req = xs_dict_append(req, "path", xs_list_get(pnv, 0)); /* get the variables */ q_vars = xs_url_vars(xs_list_get(pnv, 1)); @@ -121,20 +120,19 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) p = xs_split_n(l, ": ", 1); if (xs_list_len(p) == 2) - headers = xs_dict_append(headers, - xs_tolower(xs_list_get(p, 0)), xs_list_get(p, 1)); + req = xs_dict_append(req, xs_tolower(xs_list_get(p, 0)), xs_list_get(p, 1)); } xs_socket_timeout(fileno(f), 5.0, 0.0); - if ((v = xs_dict_get(headers, "content-length")) != NULL) { + if ((v = xs_dict_get(req, "content-length")) != NULL) { /* if it has a payload, load it */ *p_size = atoi(v); *payload = xs_read(f, *p_size); } - /* does it have a payload with form urlencoded variables? */ - v = xs_dict_get(headers, "content-type"); + /* is the payload form urlencoded variables? */ + v = xs_dict_get(req, "content-type"); if (v && strcmp(v, "application/x-www-form-urlencoded") == 0) { xs *upl = xs_url_dec(*payload); @@ -143,11 +141,12 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) else p_vars = xs_dict_new(); - if (errno == 0) { - req = xs_dict_new(); - req = xs_dict_append(req, "headers", headers); - req = xs_dict_append(req, "q_vars", q_vars); - req = xs_dict_append(req, "p_vars", p_vars); + req = xs_dict_append(req, "q_vars", q_vars); + req = xs_dict_append(req, "p_vars", p_vars); + + if (errno) { + free(req); + req = NULL; } return req;