summaryrefslogtreecommitdiff
path: root/xs_httpd.h
diff options
context:
space:
mode:
authorGravatar default2023-01-28 17:49:02 +0100
committerGravatar default2023-01-28 17:49:02 +0100
commit876bebd9ac904ca930117237edaf8c3dcae7a922 (patch)
tree7e91e26c49e18fd80c7de93ff275ffce83fb14df /xs_httpd.h
parentBumped version. (diff)
downloadpenes-snac2-876bebd9ac904ca930117237edaf8c3dcae7a922.tar.gz
penes-snac2-876bebd9ac904ca930117237edaf8c3dcae7a922.tar.xz
penes-snac2-876bebd9ac904ca930117237edaf8c3dcae7a922.zip
Backport from xs.
Diffstat (limited to '')
-rw-r--r--xs_httpd.h43
1 files changed, 22 insertions, 21 deletions
diff --git a/xs_httpd.h b/xs_httpd.h
index 47901e8..829411c 100644
--- a/xs_httpd.h
+++ b/xs_httpd.h
@@ -4,18 +4,18 @@
4 4
5#define _XS_HTTPD_H 5#define _XS_HTTPD_H
6 6
7d_char *xs_url_dec(char *str); 7xs_str *xs_url_dec(char *str);
8d_char *xs_url_vars(char *str); 8xs_dict *xs_url_vars(char *str);
9d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size); 9xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size);
10void xs_httpd_response(FILE *f, int status, d_char *headers, char *body, int b_size); 10void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size);
11 11
12 12
13#ifdef XS_IMPLEMENTATION 13#ifdef XS_IMPLEMENTATION
14 14
15d_char *xs_url_dec(char *str) 15xs_str *xs_url_dec(char *str)
16/* decodes an URL */ 16/* decodes an URL */
17{ 17{
18 d_char *s = xs_str_new(NULL); 18 xs_str *s = xs_str_new(NULL);
19 19
20 while (*str) { 20 while (*str) {
21 if (*str == '%') { 21 if (*str == '%') {
@@ -41,19 +41,19 @@ d_char *xs_url_dec(char *str)
41} 41}
42 42
43 43
44d_char *xs_url_vars(char *str) 44xs_dict *xs_url_vars(char *str)
45/* parse url variables */ 45/* parse url variables */
46{ 46{
47 d_char *vars; 47 xs_dict *vars;
48 48
49 vars = xs_dict_new(); 49 vars = xs_dict_new();
50 50
51 if (str != NULL) { 51 if (str != NULL) {
52 char *v, *l;
53 xs *args;
54
55 /* split by arguments */ 52 /* split by arguments */
56 args = xs_split(str, "&"); 53 xs *args = xs_split(str, "&");
54
55 xs_list *l;
56 xs_val *v;
57 57
58 l = args; 58 l = args;
59 while (xs_list_iter(&l, &v)) { 59 while (xs_list_iter(&l, &v)) {
@@ -69,7 +69,7 @@ d_char *xs_url_vars(char *str)
69} 69}
70 70
71 71
72d_char *_xs_multipart_form_data(char *payload, int p_size, char *header) 72xs_dict *_xs_multipart_form_data(xs_str *payload, int p_size, char *header)
73/* parses a multipart/form-data payload */ 73/* parses a multipart/form-data payload */
74{ 74{
75 xs *boundary = NULL; 75 xs *boundary = NULL;
@@ -89,7 +89,7 @@ d_char *_xs_multipart_form_data(char *payload, int p_size, char *header)
89 89
90 bsz = strlen(boundary); 90 bsz = strlen(boundary);
91 91
92 d_char *p_vars = xs_dict_new(); 92 xs_dict *p_vars = xs_dict_new();
93 93
94 /* iterate searching the boundaries */ 94 /* iterate searching the boundaries */
95 while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) { 95 while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) {
@@ -173,12 +173,11 @@ d_char *_xs_multipart_form_data(char *payload, int p_size, char *header)
173} 173}
174 174
175 175
176d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) 176xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size)
177/* processes an httpd connection */ 177/* processes an httpd connection */
178{ 178{
179 d_char *req = NULL; 179 xs *q_vars = NULL;
180 xs *q_vars = NULL; 180 xs *p_vars = NULL;
181 xs *p_vars = NULL;
182 xs *l1, *l2; 181 xs *l1, *l2;
183 char *v; 182 char *v;
184 183
@@ -193,7 +192,7 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
193 return NULL; 192 return NULL;
194 } 193 }
195 194
196 req = xs_dict_new(); 195 xs_dict *req = xs_dict_new();
197 196
198 req = xs_dict_append(req, "method", xs_list_get(l2, 0)); 197 req = xs_dict_append(req, "method", xs_list_get(l2, 0));
199 req = xs_dict_append(req, "proto", xs_list_get(l2, 2)); 198 req = xs_dict_append(req, "proto", xs_list_get(l2, 2));
@@ -258,11 +257,13 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
258} 257}
259 258
260 259
261void xs_httpd_response(FILE *f, int status, d_char *headers, char *body, int b_size) 260void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size)
262/* sends an httpd response */ 261/* sends an httpd response */
263{ 262{
264 xs *proto; 263 xs *proto;
265 char *p, *k, *v; 264 xs_dict *p;
265 xs_str *k;
266 xs_val *v;
266 267
267 proto = xs_fmt("HTTP/1.1 %d ", status); 268 proto = xs_fmt("HTTP/1.1 %d ", status);
268 fprintf(f, "%s\r\n", proto); 269 fprintf(f, "%s\r\n", proto);