2024-01-04 11:22:03 +03:00
|
|
|
/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
|
2023-09-25 19:25:53 +03:00
|
|
|
|
|
|
|
#ifndef _XS_URL_H
|
|
|
|
|
|
|
|
#define _XS_URL_H
|
|
|
|
|
|
|
|
xs_str *xs_url_dec(const char *str);
|
|
|
|
xs_dict *xs_url_vars(const char *str);
|
|
|
|
xs_dict *xs_multipart_form_data(const char *payload, int p_size, const char *header);
|
|
|
|
|
|
|
|
#ifdef XS_IMPLEMENTATION
|
|
|
|
|
|
|
|
xs_str *xs_url_dec(const char *str)
|
|
|
|
/* decodes an URL */
|
|
|
|
{
|
|
|
|
xs_str *s = xs_str_new(NULL);
|
|
|
|
|
|
|
|
while (*str) {
|
|
|
|
if (*str == '%') {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (sscanf(str + 1, "%02x", &i) == 1) {
|
|
|
|
unsigned char uc = i;
|
|
|
|
|
|
|
|
s = xs_append_m(s, (char *)&uc, 1);
|
|
|
|
str += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (*str == '+')
|
|
|
|
s = xs_append_m(s, " ", 1);
|
|
|
|
else
|
|
|
|
s = xs_append_m(s, str, 1);
|
|
|
|
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xs_dict *xs_url_vars(const char *str)
|
|
|
|
/* parse url variables */
|
|
|
|
{
|
|
|
|
xs_dict *vars;
|
|
|
|
|
|
|
|
vars = xs_dict_new();
|
|
|
|
|
|
|
|
if (str != NULL) {
|
|
|
|
/* split by arguments */
|
|
|
|
xs *args = xs_split(str, "&");
|
|
|
|
|
2024-05-23 11:01:37 +03:00
|
|
|
const xs_val *v;
|
2023-09-25 19:25:53 +03:00
|
|
|
|
2024-08-30 20:10:26 +03:00
|
|
|
xs_list_foreach(args, v) {
|
2024-04-14 20:26:49 +03:00
|
|
|
xs *kv = xs_split_n(v, "=", 1);
|
2023-09-25 19:25:53 +03:00
|
|
|
|
|
|
|
if (xs_list_len(kv) == 2) {
|
|
|
|
const char *key = xs_list_get(kv, 0);
|
|
|
|
const char *pv = xs_dict_get(vars, key);
|
|
|
|
|
|
|
|
if (!xs_is_null(pv)) {
|
|
|
|
/* there is a previous value: convert to a list and append */
|
|
|
|
xs *vlist = NULL;
|
|
|
|
if (xs_type(pv) == XSTYPE_LIST)
|
|
|
|
vlist = xs_dup(pv);
|
|
|
|
else {
|
|
|
|
vlist = xs_list_new();
|
|
|
|
vlist = xs_list_append(vlist, pv);
|
|
|
|
}
|
|
|
|
|
|
|
|
vlist = xs_list_append(vlist, xs_list_get(kv, 1));
|
|
|
|
vars = xs_dict_set(vars, key, vlist);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* ends with []? force to always be a list */
|
|
|
|
if (xs_endswith(key, "[]")) {
|
|
|
|
xs *vlist = xs_list_new();
|
|
|
|
vlist = xs_list_append(vlist, xs_list_get(kv, 1));
|
|
|
|
vars = xs_dict_append(vars, key, vlist);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
vars = xs_dict_append(vars, key, xs_list_get(kv, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vars;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xs_dict *xs_multipart_form_data(const char *payload, int p_size, const char *header)
|
|
|
|
/* parses a multipart/form-data payload */
|
|
|
|
{
|
|
|
|
xs *boundary = NULL;
|
|
|
|
int offset = 0;
|
|
|
|
int bsz;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/* build the boundary string */
|
|
|
|
{
|
|
|
|
xs *l1 = xs_split(header, "=");
|
|
|
|
|
|
|
|
if (xs_list_len(l1) != 2)
|
|
|
|
return NULL;
|
|
|
|
|
2024-05-28 03:15:53 +03:00
|
|
|
boundary = xs_dup(xs_list_get(l1, 1));
|
|
|
|
|
|
|
|
/* Tokodon sends the boundary header with double quotes surrounded */
|
2024-08-05 19:39:15 +03:00
|
|
|
if (xs_between("\"", boundary, "\"") != 0)
|
2024-05-28 03:15:53 +03:00
|
|
|
boundary = xs_strip_chars_i(boundary, "\"");
|
|
|
|
|
|
|
|
boundary = xs_fmt("--%s", boundary);
|
2023-09-25 19:25:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bsz = strlen(boundary);
|
|
|
|
|
|
|
|
xs_dict *p_vars = xs_dict_new();
|
|
|
|
|
|
|
|
/* iterate searching the boundaries */
|
|
|
|
while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) {
|
|
|
|
xs *s1 = NULL;
|
|
|
|
xs *l1 = NULL;
|
2024-05-21 15:12:15 +03:00
|
|
|
const char *vn = NULL;
|
|
|
|
const char *fn = NULL;
|
2024-05-29 12:53:34 +03:00
|
|
|
const char *ct = NULL;
|
2023-09-25 19:25:53 +03:00
|
|
|
char *q;
|
|
|
|
int po, ps;
|
|
|
|
|
|
|
|
/* final boundary? */
|
|
|
|
p += bsz;
|
|
|
|
|
|
|
|
if (p[0] == '-' && p[1] == '-')
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* skip the \r\n */
|
|
|
|
p += 2;
|
|
|
|
|
2024-05-29 12:53:34 +03:00
|
|
|
/* Tokodon sends also a Content-Type headers,
|
|
|
|
let's use it to determine the file type */
|
|
|
|
do {
|
|
|
|
if (p[0] == 13 && p[1] == 10)
|
|
|
|
break;
|
|
|
|
q = strchr(p, '\r');
|
2024-09-14 15:43:22 +03:00
|
|
|
|
|
|
|
/* unexpected formatting, fail immediately */
|
|
|
|
if (q == NULL)
|
|
|
|
return p_vars;
|
|
|
|
|
2024-05-29 12:53:34 +03:00
|
|
|
s1 = xs_realloc(NULL, q - p + 1);
|
|
|
|
memcpy(s1, p, q - p);
|
|
|
|
s1[q - p] = '\0';
|
|
|
|
|
|
|
|
if (xs_startswith(s1, "Content-Disposition")) {
|
|
|
|
/* split by " like a primitive man */
|
|
|
|
l1 = xs_split(s1, "\"");
|
|
|
|
|
|
|
|
/* get the variable name */
|
|
|
|
vn = xs_list_get(l1, 1);
|
|
|
|
|
|
|
|
/* is it an attached file? */
|
|
|
|
if (xs_list_len(l1) >= 4 && strcmp(xs_list_get(l1, 2), "; filename=") == 0) {
|
|
|
|
/* get the file name */
|
|
|
|
fn = xs_list_get(l1, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (xs_startswith(s1, "Content-Type")) {
|
|
|
|
l1 = xs_split(s1, ":");
|
2023-09-25 19:25:53 +03:00
|
|
|
|
2024-05-29 12:53:34 +03:00
|
|
|
if (xs_list_len(l1) >= 2) {
|
|
|
|
ct = xs_lstrip_chars_i(xs_dup(xs_list_get(l1, 1)), " ");
|
|
|
|
}
|
|
|
|
}
|
2023-09-25 19:25:53 +03:00
|
|
|
|
2024-05-29 12:53:34 +03:00
|
|
|
p += (q - p);
|
|
|
|
p += 2; // Skip /r/n
|
|
|
|
} while (1);
|
2023-09-25 19:25:53 +03:00
|
|
|
|
|
|
|
/* find the start of the part content */
|
2024-05-29 12:53:34 +03:00
|
|
|
if ((p = xs_memmem(p, p_size - (p - payload), "\r\n", 2)) == NULL)
|
2023-09-25 19:25:53 +03:00
|
|
|
break;
|
|
|
|
|
2024-05-29 12:53:34 +03:00
|
|
|
p += 2; // Skip empty line
|
2023-09-25 19:25:53 +03:00
|
|
|
|
|
|
|
/* find the next boundary */
|
|
|
|
if ((q = xs_memmem(p, p_size - (p - payload), boundary, bsz)) == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
po = p - payload;
|
|
|
|
ps = q - p - 2; /* - 2 because the final \r\n */
|
|
|
|
|
|
|
|
/* is it a filename? */
|
|
|
|
if (fn != NULL) {
|
|
|
|
/* p_var value is a list */
|
2024-05-29 12:53:34 +03:00
|
|
|
/* if filename has no extension and content-type is image, attach extension to the filename */
|
|
|
|
if (strchr(fn, '.') == NULL && xs_startswith(ct, "image/")) {
|
|
|
|
char *ext = strchr(ct, '/');
|
|
|
|
ext++;
|
|
|
|
fn = xs_str_cat(xs_str_new(""), fn, ".", ext);
|
|
|
|
}
|
|
|
|
|
2023-09-25 19:25:53 +03:00
|
|
|
xs *l1 = xs_list_new();
|
|
|
|
xs *vpo = xs_number_new(po);
|
|
|
|
xs *vps = xs_number_new(ps);
|
|
|
|
|
|
|
|
l1 = xs_list_append(l1, fn);
|
|
|
|
l1 = xs_list_append(l1, vpo);
|
|
|
|
l1 = xs_list_append(l1, vps);
|
|
|
|
|
|
|
|
p_vars = xs_dict_append(p_vars, vn, l1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* regular variable; just copy */
|
|
|
|
xs *vc = xs_realloc(NULL, ps + 1);
|
|
|
|
memcpy(vc, payload + po, ps);
|
|
|
|
vc[ps] = '\0';
|
|
|
|
|
|
|
|
p_vars = xs_dict_append(p_vars, vn, vc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* move on */
|
|
|
|
offset = q - payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p_vars;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* XS_IMPLEMENTATION */
|
|
|
|
|
|
|
|
#endif /* XS_URL_H */
|