2.56 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            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        int ct = 0;
 54        const xs_val *v;
 55
 56        while (xs_list_next(args, &v, &ct)) {
 57            xs *kv = xs_split_n(v, "=", 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        boundary = xs_dup(xs_list_get(l1, 1));
110
111        /* Tokodon sends the boundary header with double quotes surrounded */
112        if (xs_starts_and_ends("\"", boundary, "\"") != 0)
113            boundary = xs_strip_chars_i(boundary, "\"");
114
115        boundary = xs_fmt("--%s", 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            s1 = xs_realloc(NULL, q - p + 1);
148            memcpy(s1, p, q - p);
149            s1[q - p] = '\0';
150
151            if (xs_startswith(s1, "Content-Disposition")) {
152                /* split by " like a primitive man */
153                l1 = xs_split(s1, "\"");
154
155                /* get the variable name */
156                vn = xs_list_get(l1, 1);
157
158                /* is it an attached file? */
159                if (xs_list_len(l1) >= 4 && strcmp(xs_list_get(l1, 2), "; filename=") == 0) {
160                    /* get the file name */
161                    fn = xs_list_get(l1, 3);
162                }
163            }
164            else
165            if (xs_startswith(s1, "Content-Type")) {
166                l1 = xs_split(s1, ":");
167
168                if (xs_list_len(l1) >= 2) {
169                    ct = xs_lstrip_chars_i(xs_dup(xs_list_get(l1, 1)), " ");
170                }
171            }
172
173            p += (q - p);
174            p += 2; // Skip /r/n
175        } while (1);
176
177        /* find the start of the part content */
178        if ((p = xs_memmem(p, p_size - (p - payload), "\r\n", 2)) == NULL)
179            break;
180
181        p += 2; // Skip empty line
182
183        /* find the next boundary */
184        if ((q = xs_memmem(p, p_size - (p - payload), boundary, bsz)) == NULL)
185            break;
186
187        po = p - payload;
188        ps = q - p - 2;     /* - 2 because the final \r\n */
189
190        /* is it a filename? */
191        if (fn != NULL) {
192            /* p_var value is a list */
193            /* if filename has no extension and content-type is image, attach extension to the filename */
194            if (strchr(fn, '.') == NULL && xs_startswith(ct, "image/")) {
195                char *ext = strchr(ct, '/');
196                ext++;
197                fn = xs_str_cat(xs_str_new(""), fn, ".", ext);
198            }
199
200            xs *l1 = xs_list_new();
201            xs *vpo = xs_number_new(po);
202            xs *vps = xs_number_new(ps);
203
204            l1 = xs_list_append(l1, fn);
205            l1 = xs_list_append(l1, vpo);
206            l1 = xs_list_append(l1, vps);
207
208            p_vars = xs_dict_append(p_vars, vn, l1);
209        }
210        else {
211            /* regular variable; just copy */
212            xs *vc = xs_realloc(NULL, ps + 1);
213            memcpy(vc, payload + po, ps);
214            vc[ps] = '\0';
215
216            p_vars = xs_dict_append(p_vars, vn, vc);
217        }
218
219        /* move on */
220        offset = q - payload;
221    }
222
223    return p_vars;
224}
225
226
227#endif /* XS_IMPLEMENTATION */
228
229#endif /* XS_URL_H */