summaryrefslogtreecommitdiff
path: root/xs_httpd.h
diff options
context:
space:
mode:
authordefault <nobody@localhost>2022-09-25 07:42:57 +0200
committerdefault <nobody@localhost>2022-09-25 07:42:57 +0200
commitb070d2d8f88866bf83103c34c4d86352c6b74e8d (patch)
treef3d48f9a370b92560e25cc0d96dac252ee851b96 /xs_httpd.h
parent58de0798f29d8ee29759bd4076e35702027f113d (diff)
The HTTP request headers are stored in a plain dict.
Diffstat (limited to 'xs_httpd.h')
-rw-r--r--xs_httpd.h31
1 files changed, 15 insertions, 16 deletions
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;