2.67
xs_url.h
1/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
2
3#ifndef _XS_URL_H
4
5#define _XS_URL_H
6
7xs_str *xs_url_dec(const char *str);
8xs_dict *xs_url_vars(const char *str);
9xs_dict *xs_multipart_form_data(const char *payload, int p_size, const char *header);
10
11#ifdef XS_IMPLEMENTATION
12
13xs_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 unsigned 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
42xs_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 const xs_val *v;
54
55 xs_list_foreach(args, v) {
56 xs *dv = xs_url_dec(v);
57 xs *kv = xs_split_n(dv, "=", 1);
58
59 if (xs_list_len(kv) == 2) {
60 const char *key = xs_list_get(kv, 0);
61 const char *pv = xs_dict_get(vars, key);
62
63 if (!xs_is_null(pv)) {
64 /* there is a previous value: convert to a list and append */
65 xs *vlist = NULL;
66 if (xs_type(pv) == XSTYPE_LIST)
67 vlist = xs_dup(pv);
68 else {
69 vlist = xs_list_new();
70 vlist = xs_list_append(vlist, pv);
71 }
72
73 vlist = xs_list_append(vlist, xs_list_get(kv, 1));
74 vars = xs_dict_set(vars, key, vlist);
75 }
76 else {
77 /* ends with []? force to always be a list */
78 if (xs_endswith(key, "[]")) {
79 xs *vlist = xs_list_new();
80 vlist = xs_list_append(vlist, xs_list_get(kv, 1));
81 vars = xs_dict_append(vars, key, vlist);
82 }
83 else
84 vars = xs_dict_append(vars, key, xs_list_get(kv, 1));
85 }
86 }
87 }
88 }
89
90 return vars;
91}
92
93
94xs_dict *xs_multipart_form_data(const char *payload, int p_size, const char *header)
95/* parses a multipart/form-data payload */
96{
97 xs *boundary = NULL;
98 int offset = 0;
99 int bsz;
100 char *p;
101
102 /* build the boundary string */
103 {
104 xs *l1 = xs_split(header, "=");
105
106 if (xs_list_len(l1) != 2)
107 return NULL;
108
109 xs *t_boundary = xs_dup(xs_list_get(l1, 1));
110
111 /* Tokodon sends the boundary header with double quotes surrounded */
112 if (xs_between("\"", t_boundary, "\"") != 0)
113 t_boundary = xs_strip_chars_i(t_boundary, "\"");
114
115 boundary = xs_fmt("--%s", t_boundary);
116 }
117
118 bsz = strlen(boundary);
119
120 xs_dict *p_vars = xs_dict_new();
121
122 /* iterate searching the boundaries */
123 while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) {
124 xs *s1 = NULL;
125 xs *l1 = NULL;
126 const char *vn = NULL;
127 const char *fn = NULL;
128 const char *ct = NULL;
129 char *q;
130 int po, ps;
131
132 /* final boundary? */
133 p += bsz;
134
135 if (p[0] == '-' && p[1] == '-')
136 break;
137
138 /* skip the \r\n */
139 p += 2;
140
141 /* Tokodon sends also a Content-Type headers,
142 let's use it to determine the file type */
143 do {
144 if (p[0] == 13 && p[1] == 10)
145 break;
146 q = strchr(p, '\r');
147
148 /* unexpected formatting, fail immediately */
149 if (q == NULL)
150 return p_vars;
151
152 s1 = xs_realloc(NULL, q - p + 1);
153 memcpy(s1, p, q - p);
154 s1[q - p] = '\0';
155
156 if (xs_startswith(s1, "Content-Disposition") || xs_startswith(s1, "content-disposition")) {
157 /* split by " like a primitive man */
158 l1 = xs_split(s1, "\"");
159
160 /* get the variable name */
161 vn = xs_list_get(l1, 1);
162
163 /* is it an attached file? */
164 if (xs_list_len(l1) >= 4 && strcmp(xs_list_get(l1, 2), "; filename=") == 0) {
165 /* get the file name */
166 fn = xs_list_get(l1, 3);
167 }
168 }
169 else
170 if (xs_startswith(s1, "Content-Type") || xs_startswith(s1, "content-type")) {
171 l1 = xs_split(s1, ":");
172
173 if (xs_list_len(l1) >= 2) {
174 ct = xs_lstrip_chars_i(xs_dup(xs_list_get(l1, 1)), " ");
175 }
176 }
177
178 p += (q - p);
179 p += 2; // Skip /r/n
180 } while (1);
181
182 /* find the start of the part content */
183 if ((p = xs_memmem(p, p_size - (p - payload), "\r\n", 2)) == NULL)
184 break;
185
186 p += 2; // Skip empty line
187
188 /* find the next boundary */
189 if ((q = xs_memmem(p, p_size - (p - payload), boundary, bsz)) == NULL)
190 break;
191
192 po = p - payload;
193 ps = q - p - 2; /* - 2 because the final \r\n */
194
195 /* is it a filename? */
196 if (fn != NULL) {
197 /* p_var value is a list */
198 /* if filename has no extension and content-type is image, attach extension to the filename */
199 if (strchr(fn, '.') == NULL && ct && xs_startswith(ct, "image/")) {
200 char *ext = strchr(ct, '/');
201 ext++;
202 fn = xs_str_cat(xs_str_new(""), fn, ".", ext);
203 }
204
205 xs *l1 = xs_list_new();
206 xs *vpo = xs_number_new(po);
207 xs *vps = xs_number_new(ps);
208
209 l1 = xs_list_append(l1, fn);
210 l1 = xs_list_append(l1, vpo);
211 l1 = xs_list_append(l1, vps);
212
213 p_vars = xs_dict_append(p_vars, vn, l1);
214 }
215 else {
216 /* regular variable; just copy */
217 xs *vc = xs_realloc(NULL, ps + 1);
218 memcpy(vc, payload + po, ps);
219 vc[ps] = '\0';
220
221 p_vars = xs_dict_append(p_vars, vn, vc);
222 }
223
224 /* move on */
225 offset = q - payload;
226 }
227
228 return p_vars;
229}
230
231
232#endif /* XS_IMPLEMENTATION */
233
234#endif /* XS_URL_H */