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