summaryrefslogtreecommitdiff
path: root/xs_httpd.h
diff options
context:
space:
mode:
authorGravatar default2022-09-25 07:42:57 +0200
committerGravatar default2022-09-25 07:42:57 +0200
commitb070d2d8f88866bf83103c34c4d86352c6b74e8d (patch)
treef3d48f9a370b92560e25cc0d96dac252ee851b96 /xs_httpd.h
parentNew function srv_archive(). (diff)
downloadpenes-snac2-b070d2d8f88866bf83103c34c4d86352c6b74e8d.tar.gz
penes-snac2-b070d2d8f88866bf83103c34c4d86352c6b74e8d.tar.xz
penes-snac2-b070d2d8f88866bf83103c34c4d86352c6b74e8d.zip
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)
72d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) 72d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
73/* processes an httpd connection */ 73/* processes an httpd connection */
74{ 74{
75 xs *headers = NULL; 75 d_char *req = NULL;
76 xs *q_vars = NULL; 76 xs *q_vars = NULL;
77 xs *p_vars = NULL; 77 xs *p_vars = NULL;
78 d_char *req = NULL;
79 xs *l1, *l2; 78 xs *l1, *l2;
80 char *v; 79 char *v;
81 80
@@ -90,10 +89,10 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
90 return NULL; 89 return NULL;
91 } 90 }
92 91
93 headers = xs_dict_new(); 92 req = xs_dict_new();
94 93
95 headers = xs_dict_append(headers, "method", xs_list_get(l2, 0)); 94 req = xs_dict_append(req, "method", xs_list_get(l2, 0));
96 headers = xs_dict_append(headers, "proto", xs_list_get(l2, 2)); 95 req = xs_dict_append(req, "proto", xs_list_get(l2, 2));
97 96
98 { 97 {
99 /* split the path with its optional variables */ 98 /* split the path with its optional variables */
@@ -101,7 +100,7 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
101 xs *pnv = xs_split_n(udp, "?", 1); 100 xs *pnv = xs_split_n(udp, "?", 1);
102 101
103 /* store the path */ 102 /* store the path */
104 headers = xs_dict_append(headers, "path", xs_list_get(pnv, 0)); 103 req = xs_dict_append(req, "path", xs_list_get(pnv, 0));
105 104
106 /* get the variables */ 105 /* get the variables */
107 q_vars = xs_url_vars(xs_list_get(pnv, 1)); 106 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)
121 p = xs_split_n(l, ": ", 1); 120 p = xs_split_n(l, ": ", 1);
122 121
123 if (xs_list_len(p) == 2) 122 if (xs_list_len(p) == 2)
124 headers = xs_dict_append(headers, 123 req = xs_dict_append(req, xs_tolower(xs_list_get(p, 0)), xs_list_get(p, 1));
125 xs_tolower(xs_list_get(p, 0)), xs_list_get(p, 1));
126 } 124 }
127 125
128 xs_socket_timeout(fileno(f), 5.0, 0.0); 126 xs_socket_timeout(fileno(f), 5.0, 0.0);
129 127
130 if ((v = xs_dict_get(headers, "content-length")) != NULL) { 128 if ((v = xs_dict_get(req, "content-length")) != NULL) {
131 /* if it has a payload, load it */ 129 /* if it has a payload, load it */
132 *p_size = atoi(v); 130 *p_size = atoi(v);
133 *payload = xs_read(f, *p_size); 131 *payload = xs_read(f, *p_size);
134 } 132 }
135 133
136 /* does it have a payload with form urlencoded variables? */ 134 /* is the payload form urlencoded variables? */
137 v = xs_dict_get(headers, "content-type"); 135 v = xs_dict_get(req, "content-type");
138 136
139 if (v && strcmp(v, "application/x-www-form-urlencoded") == 0) { 137 if (v && strcmp(v, "application/x-www-form-urlencoded") == 0) {
140 xs *upl = xs_url_dec(*payload); 138 xs *upl = xs_url_dec(*payload);
@@ -143,11 +141,12 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
143 else 141 else
144 p_vars = xs_dict_new(); 142 p_vars = xs_dict_new();
145 143
146 if (errno == 0) { 144 req = xs_dict_append(req, "q_vars", q_vars);
147 req = xs_dict_new(); 145 req = xs_dict_append(req, "p_vars", p_vars);
148 req = xs_dict_append(req, "headers", headers); 146
149 req = xs_dict_append(req, "q_vars", q_vars); 147 if (errno) {
150 req = xs_dict_append(req, "p_vars", p_vars); 148 free(req);
149 req = NULL;
151 } 150 }
152 151
153 return req; 152 return req;