2.70
xs_url.h
1/* copyright (c) 2022 - 2025 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_str *xs_url_enc(const char *str);
9xs_dict *xs_url_vars(const char *str);
10xs_dict *xs_multipart_form_data(const char *payload, int p_size, const char *header);
11
12#ifdef XS_IMPLEMENTATION
13
14xs_str *xs_url_dec(const char *str)
15/* decodes an URL */
16{
17 xs_str *s = xs_str_new(NULL);
18
19 while (*str) {
20 if (*str == '%') {
21 unsigned int i;
22
23 if (sscanf(str + 1, "%02x", &i) == 1) {
24 unsigned char uc = i;
25
26 s = xs_append_m(s, (char *)&uc, 1);
27 str += 2;
28 }
29 }
30 else
31 if (*str == '+')
32 s = xs_append_m(s, " ", 1);
33 else
34 s = xs_append_m(s, str, 1);
35
36 str++;
37 }
38
39 return s;
40}
41
42
43xs_str *xs_url_enc(const char *str)
44/* URL-encodes a string (RFC 3986) */
45{
46 xs_str *s = xs_str_new(NULL);
47
48 while (*str) {
49 if (isalnum(*str) || strchr("-._~", *str)) {
50 s = xs_append_m(s, str, 1);
51 }
52 else {
53 char tmp[8];
54 snprintf(tmp, sizeof(tmp), "%%%02X", (unsigned char)*str);
55 s = xs_append_m(s, tmp, 3);
56 }
57
58 str++;
59 }
60
61 return s;
62}
63
64
65xs_dict *xs_url_vars(const char *str)
66/* parse url variables */
67{
68 xs_dict *vars;
69
70 vars = xs_dict_new();
71
72 if (str != NULL) {
73 /* split by arguments */
74 xs *args = xs_split(str, "&");
75
76 const xs_val *v;
77
78 xs_list_foreach(args, v) {
79 xs *dv = xs_url_dec(v);
80 xs *kv = xs_split_n(dv, "=", 1);
81
82 if (xs_list_len(kv) == 2) {
83 const char *key = xs_list_get(kv, 0);
84 const char *pv = xs_dict_get(vars, key);
85
86 if (!xs_is_null(pv)) {
87 /* there is a previous value: convert to a list and append */
88 xs *vlist = NULL;
89 if (xs_type(pv) == XSTYPE_LIST)
90 vlist = xs_dup(pv);
91 else {
92 vlist = xs_list_new();
93 vlist = xs_list_append(vlist, pv);
94 }
95
96 vlist = xs_list_append(vlist, xs_list_get(kv, 1));
97 vars = xs_dict_set(vars, key, vlist);
98 }
99 else {
100 /* ends with []? force to always be a list */
101 if (xs_endswith(key, "[]")) {
102 xs *vlist = xs_list_new();
103 vlist = xs_list_append(vlist, xs_list_get(kv, 1));
104 vars = xs_dict_append(vars, key, vlist);
105 }
106 else
107 vars = xs_dict_append(vars, key, xs_list_get(kv, 1));
108 }
109 }
110 }
111 }
112
113 return vars;
114}
115
116
117xs_dict *xs_multipart_form_data(const char *payload, int p_size, const char *header)
118/* parses a multipart/form-data payload */
119{
120 xs *boundary = NULL;
121 int offset = 0;
122 int bsz;
123 char *p;
124
125 /* build the boundary string */
126 {
127 xs *l1 = xs_split(header, "=");
128
129 if (xs_list_len(l1) != 2)
130 return NULL;
131
132 xs *t_boundary = xs_dup(xs_list_get(l1, 1));
133
134 /* Tokodon sends the boundary header with double quotes surrounded */
135 if (xs_between("\"", t_boundary, "\"") != 0)
136 t_boundary = xs_strip_chars_i(t_boundary, "\"");
137
138 boundary = xs_fmt("--%s", t_boundary);
139 }
140
141 bsz = strlen(boundary);
142
143 xs_dict *p_vars = xs_dict_new();
144
145 /* iterate searching the boundaries */
146 while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) {
147 xs *s1 = NULL;
148 xs *l1 = NULL;
149 const char *vn = NULL;
150 const char *fn = NULL;
151 const char *ct = NULL;
152 char *q;
153 int po, ps;
154
155 /* final boundary? */
156 p += bsz;
157
158 if (p[0] == '-' && p[1] == '-')
159 break;
160
161 /* skip the \r\n */
162 p += 2;
163
164 /* Tokodon sends also a Content-Type headers,
165 let's use it to determine the file type */
166 do {
167 if (p[0] == 13 && p[1] == 10)
168 break;
169 q = strchr(p, '\r');
170
171 /* unexpected formatting, fail immediately */
172 if (q == NULL)
173 return p_vars;
174
175 s1 = xs_realloc(NULL, q - p + 1);
176 memcpy(s1, p, q - p);
177 s1[q - p] = '\0';
178
179 if (xs_startswith(s1, "Content-Disposition") || xs_startswith(s1, "content-disposition")) {
180 /* split by " like a primitive man */
181 l1 = xs_split(s1, "\"");
182
183 /* get the variable name */
184 vn = xs_list_get(l1, 1);
185
186 /* is it an attached file? */
187 if (xs_list_len(l1) >= 4 && strcmp(xs_list_get(l1, 2), "; filename=") == 0) {
188 /* get the file name */
189 fn = xs_list_get(l1, 3);
190 }
191 }
192 else
193 if (xs_startswith(s1, "Content-Type") || xs_startswith(s1, "content-type")) {
194 l1 = xs_split(s1, ":");
195
196 if (xs_list_len(l1) >= 2) {
197 ct = xs_lstrip_chars_i(xs_dup(xs_list_get(l1, 1)), " ");
198 }
199 }
200
201 p += (q - p);
202 p += 2; // Skip /r/n
203 } while (1);
204
205 /* find the start of the part content */
206 if ((p = xs_memmem(p, p_size - (p - payload), "\r\n", 2)) == NULL)
207 break;
208
209 p += 2; // Skip empty line
210
211 /* find the next boundary */
212 if ((q = xs_memmem(p, p_size - (p - payload), boundary, bsz)) == NULL)
213 break;
214
215 po = p - payload;
216 ps = q - p - 2; /* - 2 because the final \r\n */
217
218 /* is it a filename? */
219 if (fn != NULL) {
220 /* p_var value is a list */
221 /* if filename has no extension and content-type is image, attach extension to the filename */
222 if (strchr(fn, '.') == NULL && ct && xs_startswith(ct, "image/")) {
223 char *ext = strchr(ct, '/');
224 ext++;
225 fn = xs_str_cat(xs_str_new(""), fn, ".", ext);
226 }
227
228 xs *l1 = xs_list_new();
229 xs *vpo = xs_number_new(po);
230 xs *vps = xs_number_new(ps);
231
232 l1 = xs_list_append(l1, fn);
233 l1 = xs_list_append(l1, vpo);
234 l1 = xs_list_append(l1, vps);
235
236 p_vars = xs_dict_append(p_vars, vn, l1);
237 }
238 else {
239 /* regular variable; just copy */
240 xs *vc = xs_realloc(NULL, ps + 1);
241 memcpy(vc, payload + po, ps);
242 vc[ps] = '\0';
243
244 p_vars = xs_dict_append(p_vars, vn, vc);
245 }
246
247 /* move on */
248 offset = q - payload;
249 }
250
251 return p_vars;
252}
253
254
255#endif /* XS_IMPLEMENTATION */
256
257#endif /* XS_URL_H */