The HTTP request headers are stored in a plain dict.

This commit is contained in:
default 2022-09-25 07:42:57 +02:00
parent 58de0798f2
commit b070d2d8f8
4 changed files with 21 additions and 29 deletions

View file

@ -258,8 +258,7 @@ int activitypub_get_handler(d_char *req, char *q_path,
char **body, int *b_size, char **ctype) char **body, int *b_size, char **ctype)
{ {
int status = 200; int status = 200;
char *headers = xs_dict_get(req, "headers"); char *accept = xs_dict_get(req, "accept");
char *accept = xs_dict_get(headers, "accept");
snac snac; snac snac;
xs *msg = NULL; xs *msg = NULL;
@ -321,8 +320,7 @@ int activitypub_post_handler(d_char *req, char *q_path,
/* processes an input message */ /* processes an input message */
{ {
int status = 202; /* accepted */ int status = 202; /* accepted */
char *headers = xs_dict_get(req, "headers"); char *i_ctype = xs_dict_get(req, "content-type");
char *i_ctype = xs_dict_get(headers, "content-type");
snac snac; snac snac;
if (i_ctype == NULL) if (i_ctype == NULL)

10
httpd.c
View file

@ -24,8 +24,7 @@ int server_get_handler(d_char *req, char *q_path,
/* basic server services */ /* basic server services */
{ {
int status = 0; int status = 0;
char *req_hdrs = xs_dict_get(req, "headers"); char *acpt = xs_dict_get(req, "accept");
char *acpt = xs_dict_get(req_hdrs, "accept");
if (acpt == NULL) if (acpt == NULL)
return 400; return 400;
@ -90,7 +89,6 @@ void httpd_connection(int rs)
{ {
FILE *f; FILE *f;
xs *req; xs *req;
char *req_hdrs;
char *method; char *method;
int status = 0; int status = 0;
char *body = NULL; char *body = NULL;
@ -106,10 +104,8 @@ void httpd_connection(int rs)
req = xs_httpd_request(f, &payload, &p_size); req = xs_httpd_request(f, &payload, &p_size);
req_hdrs = xs_dict_get(req, "headers"); method = xs_dict_get(req, "method");
q_path = xs_dup(xs_dict_get(req, "path"));
method = xs_dict_get(req_hdrs, "method");
q_path = xs_dup(xs_dict_get(req_hdrs, "path"));
/* crop the q_path from leading / and the prefix */ /* crop the q_path from leading / and the prefix */
if (xs_endswith(q_path, "/")) if (xs_endswith(q_path, "/"))

3
snac.c
View file

@ -163,8 +163,7 @@ void srv_archive(char *direction, char *req, char *payload, int p_size,
if (p_size && payload) { if (p_size && payload) {
xs *payload_fn; xs *payload_fn;
char *h = xs_dict_get(req, "headers"); char *v = xs_dict_get(req, "content-type");
char *v = xs_dict_get(h, "content-type");
if (v && xs_str_in(v, "json") != -1) { if (v && xs_str_in(v, "json") != -1) {
payload_fn = xs_fmt("%s/payload.json", dir); payload_fn = xs_fmt("%s/payload.json", dir);

View file

@ -72,10 +72,9 @@ d_char *xs_url_vars(char *str)
d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
/* processes an httpd connection */ /* processes an httpd connection */
{ {
xs *headers = NULL; d_char *req = NULL;
xs *q_vars = NULL; xs *q_vars = NULL;
xs *p_vars = NULL; xs *p_vars = NULL;
d_char *req = NULL;
xs *l1, *l2; xs *l1, *l2;
char *v; char *v;
@ -90,10 +89,10 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
return NULL; return NULL;
} }
headers = xs_dict_new(); req = xs_dict_new();
headers = xs_dict_append(headers, "method", xs_list_get(l2, 0)); req = xs_dict_append(req, "method", xs_list_get(l2, 0));
headers = xs_dict_append(headers, "proto", xs_list_get(l2, 2)); req = xs_dict_append(req, "proto", xs_list_get(l2, 2));
{ {
/* split the path with its optional variables */ /* 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); xs *pnv = xs_split_n(udp, "?", 1);
/* store the path */ /* 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 */ /* get the variables */
q_vars = xs_url_vars(xs_list_get(pnv, 1)); 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); p = xs_split_n(l, ": ", 1);
if (xs_list_len(p) == 2) if (xs_list_len(p) == 2)
headers = xs_dict_append(headers, req = xs_dict_append(req, xs_tolower(xs_list_get(p, 0)), xs_list_get(p, 1));
xs_tolower(xs_list_get(p, 0)), xs_list_get(p, 1));
} }
xs_socket_timeout(fileno(f), 5.0, 0.0); 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 */ /* if it has a payload, load it */
*p_size = atoi(v); *p_size = atoi(v);
*payload = xs_read(f, *p_size); *payload = xs_read(f, *p_size);
} }
/* does it have a payload with form urlencoded variables? */ /* is the payload form urlencoded variables? */
v = xs_dict_get(headers, "content-type"); v = xs_dict_get(req, "content-type");
if (v && strcmp(v, "application/x-www-form-urlencoded") == 0) { if (v && strcmp(v, "application/x-www-form-urlencoded") == 0) {
xs *upl = xs_url_dec(*payload); xs *upl = xs_url_dec(*payload);
@ -143,11 +141,12 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
else else
p_vars = xs_dict_new(); p_vars = xs_dict_new();
if (errno == 0) { req = xs_dict_append(req, "q_vars", q_vars);
req = xs_dict_new(); req = xs_dict_append(req, "p_vars", p_vars);
req = xs_dict_append(req, "headers", headers);
req = xs_dict_append(req, "q_vars", q_vars); if (errno) {
req = xs_dict_append(req, "p_vars", p_vars); free(req);
req = NULL;
} }
return req; return req;