commit c314e99
default
·
2023-09-25 16:25:09 +0000 UTC
parent 5f5e4b5
Backport from xs.
5 files changed,
+7,
-194
M
Makefile
+3,
-3
1@@ -46,10 +46,10 @@ httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_httpd.h xs_mime.h \
2 xs_time.h xs_openssl.h snac.h
3 main.o: main.c xs.h xs_io.h xs_json.h snac.h
4 mastoapi.o: mastoapi.c xs.h xs_openssl.h xs_json.h xs_io.h xs_time.h \
5- xs_glob.h xs_set.h xs_random.h xs_httpd.h xs_mime.h snac.h
6+ xs_glob.h xs_set.h xs_random.h xs_url.h xs_mime.h snac.h
7 snac.o: snac.c xs.h xs_io.h xs_unicode.h xs_json.h xs_curl.h xs_openssl.h \
8- xs_socket.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h \
9- xs_random.h xs_match.h snac.h
10+ xs_socket.h xs_url.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h \
11+ xs_glob.h xs_random.h xs_match.h snac.h
12 upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h
13 utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \
14 xs_random.h snac.h
+1,
-1
1@@ -11,7 +11,7 @@
2 #include "xs_glob.h"
3 #include "xs_set.h"
4 #include "xs_random.h"
5-#include "xs_httpd.h"
6+#include "xs_url.h"
7 #include "xs_mime.h"
8
9 #include "snac.h"
M
snac.c
+1,
-0
1@@ -10,6 +10,7 @@
2 #include "xs_curl.h"
3 #include "xs_openssl.h"
4 #include "xs_socket.h"
5+#include "xs_url.h"
6 #include "xs_httpd.h"
7 #include "xs_mime.h"
8 #include "xs_regex.h"
+1,
-189
1@@ -4,200 +4,12 @@
2
3 #define _XS_HTTPD_H
4
5-xs_str *xs_url_dec(const char *str);
6-xs_dict *xs_url_vars(const char *str);
7 xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size);
8 void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size);
9
10
11 #ifdef XS_IMPLEMENTATION
12
13-xs_str *xs_url_dec(const char *str)
14-/* decodes an URL */
15-{
16- xs_str *s = xs_str_new(NULL);
17-
18- while (*str) {
19- if (*str == '%') {
20- int i;
21-
22- if (sscanf(str + 1, "%02x", &i) == 1) {
23- unsigned char uc = i;
24-
25- s = xs_append_m(s, (char *)&uc, 1);
26- str += 2;
27- }
28- }
29- else
30- if (*str == '+')
31- s = xs_append_m(s, " ", 1);
32- else
33- s = xs_append_m(s, str, 1);
34-
35- str++;
36- }
37-
38- return s;
39-}
40-
41-
42-xs_dict *xs_url_vars(const char *str)
43-/* parse url variables */
44-{
45- xs_dict *vars;
46-
47- vars = xs_dict_new();
48-
49- if (str != NULL) {
50- /* split by arguments */
51- xs *args = xs_split(str, "&");
52-
53- xs_list *l;
54- xs_val *v;
55-
56- l = args;
57- while (xs_list_iter(&l, &v)) {
58- xs *kv = xs_split_n(v, "=", 2);
59-
60- if (xs_list_len(kv) == 2) {
61- const char *key = xs_list_get(kv, 0);
62- const char *pv = xs_dict_get(vars, key);
63-
64- if (!xs_is_null(pv)) {
65- /* there is a previous value: convert to a list and append */
66- xs *vlist = NULL;
67- if (xs_type(pv) == XSTYPE_LIST)
68- vlist = xs_dup(pv);
69- else {
70- vlist = xs_list_new();
71- vlist = xs_list_append(vlist, pv);
72- }
73-
74- vlist = xs_list_append(vlist, xs_list_get(kv, 1));
75- vars = xs_dict_set(vars, key, vlist);
76- }
77- else {
78- /* ends with []? force to always be a list */
79- if (xs_endswith(key, "[]")) {
80- xs *vlist = xs_list_new();
81- vlist = xs_list_append(vlist, xs_list_get(kv, 1));
82- vars = xs_dict_append(vars, key, vlist);
83- }
84- else
85- vars = xs_dict_append(vars, key, xs_list_get(kv, 1));
86- }
87- }
88- }
89- }
90-
91- return vars;
92-}
93-
94-
95-xs_dict *_xs_multipart_form_data(xs_str *payload, int p_size, char *header)
96-/* parses a multipart/form-data payload */
97-{
98- xs *boundary = NULL;
99- int offset = 0;
100- int bsz;
101- char *p;
102-
103- /* build the boundary string */
104- {
105- xs *l1 = xs_split(header, "=");
106-
107- if (xs_list_len(l1) != 2)
108- return NULL;
109-
110- boundary = xs_fmt("--%s", xs_list_get(l1, 1));
111- }
112-
113- bsz = strlen(boundary);
114-
115- xs_dict *p_vars = xs_dict_new();
116-
117- /* iterate searching the boundaries */
118- while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) {
119- xs *s1 = NULL;
120- xs *l1 = NULL;
121- char *vn = NULL;
122- char *fn = NULL;
123- char *q;
124- int po, ps;
125-
126- /* final boundary? */
127- p += bsz;
128-
129- if (p[0] == '-' && p[1] == '-')
130- break;
131-
132- /* skip the \r\n */
133- p += 2;
134-
135- /* now on a Content-Disposition... line; get it */
136- q = strchr(p, '\r');
137- s1 = xs_realloc(NULL, q - p + 1);
138- memcpy(s1, p, q - p);
139- s1[q - p] = '\0';
140-
141- /* move on (over a \r\n) */
142- p = q;
143-
144- /* split by " like a primitive man */
145- l1 = xs_split(s1, "\"");
146-
147- /* get the variable name */
148- vn = xs_list_get(l1, 1);
149-
150- /* is it an attached file? */
151- if (xs_list_len(l1) >= 4 && strcmp(xs_list_get(l1, 2), "; filename=") == 0) {
152- /* get the file name */
153- fn = xs_list_get(l1, 3);
154- }
155-
156- /* find the start of the part content */
157- if ((p = xs_memmem(p, p_size - (p - payload), "\r\n\r\n", 4)) == NULL)
158- break;
159-
160- p += 4;
161-
162- /* find the next boundary */
163- if ((q = xs_memmem(p, p_size - (p - payload), boundary, bsz)) == NULL)
164- break;
165-
166- po = p - payload;
167- ps = q - p - 2; /* - 2 because the final \r\n */
168-
169- /* is it a filename? */
170- if (fn != NULL) {
171- /* p_var value is a list */
172- xs *l1 = xs_list_new();
173- xs *vpo = xs_number_new(po);
174- xs *vps = xs_number_new(ps);
175-
176- l1 = xs_list_append(l1, fn);
177- l1 = xs_list_append(l1, vpo);
178- l1 = xs_list_append(l1, vps);
179-
180- p_vars = xs_dict_append(p_vars, vn, l1);
181- }
182- else {
183- /* regular variable; just copy */
184- xs *vc = xs_realloc(NULL, ps + 1);
185- memcpy(vc, payload + po, ps);
186- vc[ps] = '\0';
187-
188- p_vars = xs_dict_append(p_vars, vn, vc);
189- }
190-
191- /* move on */
192- offset = q - payload;
193- }
194-
195- return p_vars;
196-}
197-
198-
199 xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size)
200 /* processes an httpd connection */
201 {
202@@ -267,7 +79,7 @@ xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size)
203 }
204 else
205 if (*payload && v && xs_startswith(v, "multipart/form-data")) {
206- p_vars = _xs_multipart_form_data(*payload, *p_size, v);
207+ p_vars = xs_multipart_form_data(*payload, *p_size, v);
208 }
209 else
210 p_vars = xs_dict_new();
+1,
-1
1@@ -1 +1 @@
2-/* b5831045cfd18e95352e5108cd47386510bf060d */
3+/* 0b8d1591403ac07d83de975db6afa27e353f958c */