diff options
| author | 2023-09-25 18:25:53 +0200 | |
|---|---|---|
| committer | 2023-09-25 18:25:53 +0200 | |
| commit | eb8430d461d10741d1665da2249d87b85c40c84d (patch) | |
| tree | e22df0b92c5f91b08d21a112776b029d422f749b /xs_url.h | |
| parent | Backport from xs. (diff) | |
| download | penes-snac2-eb8430d461d10741d1665da2249d87b85c40c84d.tar.gz penes-snac2-eb8430d461d10741d1665da2249d87b85c40c84d.tar.xz penes-snac2-eb8430d461d10741d1665da2249d87b85c40c84d.zip | |
Added forgotten header.
Diffstat (limited to 'xs_url.h')
| -rw-r--r-- | xs_url.h | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/xs_url.h b/xs_url.h new file mode 100644 index 0000000..5fca5cb --- /dev/null +++ b/xs_url.h | |||
| @@ -0,0 +1,202 @@ | |||
| 1 | /* copyright (c) 2022 - 2023 grunfink et al. / MIT license */ | ||
| 2 | |||
| 3 | #ifndef _XS_URL_H | ||
| 4 | |||
| 5 | #define _XS_URL_H | ||
| 6 | |||
| 7 | xs_str *xs_url_dec(const char *str); | ||
| 8 | xs_dict *xs_url_vars(const char *str); | ||
| 9 | xs_dict *xs_multipart_form_data(const char *payload, int p_size, const char *header); | ||
| 10 | |||
| 11 | |||
| 12 | #ifdef XS_IMPLEMENTATION | ||
| 13 | |||
| 14 | xs_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 | 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 | |||
| 43 | xs_dict *xs_url_vars(const char *str) | ||
| 44 | /* parse url variables */ | ||
| 45 | { | ||
| 46 | xs_dict *vars; | ||
| 47 | |||
| 48 | vars = xs_dict_new(); | ||
| 49 | |||
| 50 | if (str != NULL) { | ||
| 51 | /* split by arguments */ | ||
| 52 | xs *args = xs_split(str, "&"); | ||
| 53 | |||
| 54 | xs_list *l; | ||
| 55 | xs_val *v; | ||
| 56 | |||
| 57 | l = args; | ||
| 58 | while (xs_list_iter(&l, &v)) { | ||
| 59 | xs *kv = xs_split_n(v, "=", 2); | ||
| 60 | |||
| 61 | if (xs_list_len(kv) == 2) { | ||
| 62 | const char *key = xs_list_get(kv, 0); | ||
| 63 | const char *pv = xs_dict_get(vars, key); | ||
| 64 | |||
| 65 | if (!xs_is_null(pv)) { | ||
| 66 | /* there is a previous value: convert to a list and append */ | ||
| 67 | xs *vlist = NULL; | ||
| 68 | if (xs_type(pv) == XSTYPE_LIST) | ||
| 69 | vlist = xs_dup(pv); | ||
| 70 | else { | ||
| 71 | vlist = xs_list_new(); | ||
| 72 | vlist = xs_list_append(vlist, pv); | ||
| 73 | } | ||
| 74 | |||
| 75 | vlist = xs_list_append(vlist, xs_list_get(kv, 1)); | ||
| 76 | vars = xs_dict_set(vars, key, vlist); | ||
| 77 | } | ||
| 78 | else { | ||
| 79 | /* ends with []? force to always be a list */ | ||
| 80 | if (xs_endswith(key, "[]")) { | ||
| 81 | xs *vlist = xs_list_new(); | ||
| 82 | vlist = xs_list_append(vlist, xs_list_get(kv, 1)); | ||
| 83 | vars = xs_dict_append(vars, key, vlist); | ||
| 84 | } | ||
| 85 | else | ||
| 86 | vars = xs_dict_append(vars, key, xs_list_get(kv, 1)); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | return vars; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | xs_dict *xs_multipart_form_data(const char *payload, int p_size, const char *header) | ||
| 97 | /* parses a multipart/form-data payload */ | ||
| 98 | { | ||
| 99 | xs *boundary = NULL; | ||
| 100 | int offset = 0; | ||
| 101 | int bsz; | ||
| 102 | char *p; | ||
| 103 | |||
| 104 | /* build the boundary string */ | ||
| 105 | { | ||
| 106 | xs *l1 = xs_split(header, "="); | ||
| 107 | |||
| 108 | if (xs_list_len(l1) != 2) | ||
| 109 | return NULL; | ||
| 110 | |||
| 111 | boundary = xs_fmt("--%s", xs_list_get(l1, 1)); | ||
| 112 | } | ||
| 113 | |||
| 114 | bsz = strlen(boundary); | ||
| 115 | |||
| 116 | xs_dict *p_vars = xs_dict_new(); | ||
| 117 | |||
| 118 | /* iterate searching the boundaries */ | ||
| 119 | while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) { | ||
| 120 | xs *s1 = NULL; | ||
| 121 | xs *l1 = NULL; | ||
| 122 | char *vn = NULL; | ||
| 123 | char *fn = NULL; | ||
| 124 | char *q; | ||
| 125 | int po, ps; | ||
| 126 | |||
| 127 | /* final boundary? */ | ||
| 128 | p += bsz; | ||
| 129 | |||
| 130 | if (p[0] == '-' && p[1] == '-') | ||
| 131 | break; | ||
| 132 | |||
| 133 | /* skip the \r\n */ | ||
| 134 | p += 2; | ||
| 135 | |||
| 136 | /* now on a Content-Disposition... line; get it */ | ||
| 137 | q = strchr(p, '\r'); | ||
| 138 | s1 = xs_realloc(NULL, q - p + 1); | ||
| 139 | memcpy(s1, p, q - p); | ||
| 140 | s1[q - p] = '\0'; | ||
| 141 | |||
| 142 | /* move on (over a \r\n) */ | ||
| 143 | p = q; | ||
| 144 | |||
| 145 | /* split by " like a primitive man */ | ||
| 146 | l1 = xs_split(s1, "\""); | ||
| 147 | |||
| 148 | /* get the variable name */ | ||
| 149 | vn = xs_list_get(l1, 1); | ||
| 150 | |||
| 151 | /* is it an attached file? */ | ||
| 152 | if (xs_list_len(l1) >= 4 && strcmp(xs_list_get(l1, 2), "; filename=") == 0) { | ||
| 153 | /* get the file name */ | ||
| 154 | fn = xs_list_get(l1, 3); | ||
| 155 | } | ||
| 156 | |||
| 157 | /* find the start of the part content */ | ||
| 158 | if ((p = xs_memmem(p, p_size - (p - payload), "\r\n\r\n", 4)) == NULL) | ||
| 159 | break; | ||
| 160 | |||
| 161 | p += 4; | ||
| 162 | |||
| 163 | /* find the next boundary */ | ||
| 164 | if ((q = xs_memmem(p, p_size - (p - payload), boundary, bsz)) == NULL) | ||
| 165 | break; | ||
| 166 | |||
| 167 | po = p - payload; | ||
| 168 | ps = q - p - 2; /* - 2 because the final \r\n */ | ||
| 169 | |||
| 170 | /* is it a filename? */ | ||
| 171 | if (fn != NULL) { | ||
| 172 | /* p_var value is a list */ | ||
| 173 | xs *l1 = xs_list_new(); | ||
| 174 | xs *vpo = xs_number_new(po); | ||
| 175 | xs *vps = xs_number_new(ps); | ||
| 176 | |||
| 177 | l1 = xs_list_append(l1, fn); | ||
| 178 | l1 = xs_list_append(l1, vpo); | ||
| 179 | l1 = xs_list_append(l1, vps); | ||
| 180 | |||
| 181 | p_vars = xs_dict_append(p_vars, vn, l1); | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | /* regular variable; just copy */ | ||
| 185 | xs *vc = xs_realloc(NULL, ps + 1); | ||
| 186 | memcpy(vc, payload + po, ps); | ||
| 187 | vc[ps] = '\0'; | ||
| 188 | |||
| 189 | p_vars = xs_dict_append(p_vars, vn, vc); | ||
| 190 | } | ||
| 191 | |||
| 192 | /* move on */ | ||
| 193 | offset = q - payload; | ||
| 194 | } | ||
| 195 | |||
| 196 | return p_vars; | ||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | #endif /* XS_IMPLEMENTATION */ | ||
| 201 | |||
| 202 | #endif /* XS_URL_H */ | ||