xs_url.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
  2. #ifndef _XS_URL_H
  3. #define _XS_URL_H
  4. xs_str *xs_url_dec(const char *str);
  5. xs_str *xs_url_enc(const char *str);
  6. xs_dict *xs_url_vars(const char *str);
  7. xs_dict *xs_multipart_form_data(const char *payload, int p_size, const char *header);
  8. #ifdef XS_IMPLEMENTATION
  9. xs_str *xs_url_dec(const char *str)
  10. /* decodes an URL */
  11. {
  12. xs_str *s = xs_str_new(NULL);
  13. while (*str) {
  14. if (*str == '%') {
  15. unsigned int i;
  16. if (sscanf(str + 1, "%02x", &i) == 1) {
  17. unsigned char uc = i;
  18. s = xs_append_m(s, (char *)&uc, 1);
  19. str += 2;
  20. }
  21. }
  22. else
  23. if (*str == '+')
  24. s = xs_append_m(s, " ", 1);
  25. else
  26. s = xs_append_m(s, str, 1);
  27. str++;
  28. }
  29. return s;
  30. }
  31. xs_str *xs_url_enc(const char *str)
  32. /* URL-encodes a string (RFC 3986) */
  33. {
  34. xs_str *s = xs_str_new(NULL);
  35. while (*str) {
  36. if (isalnum(*str) || strchr("-._~", *str)) {
  37. s = xs_append_m(s, str, 1);
  38. }
  39. else {
  40. char tmp[8];
  41. snprintf(tmp, sizeof(tmp), "%%%02X", (unsigned char)*str);
  42. s = xs_append_m(s, tmp, 3);
  43. }
  44. str++;
  45. }
  46. return s;
  47. }
  48. xs_dict *xs_url_vars(const char *str)
  49. /* parse url variables */
  50. {
  51. xs_dict *vars;
  52. vars = xs_dict_new();
  53. if (xs_is_string(str)) {
  54. /* split by arguments */
  55. xs *args = xs_split(str, "&");
  56. const xs_val *v;
  57. xs_list_foreach(args, v) {
  58. xs *dv = xs_url_dec(v);
  59. xs *kv = xs_split_n(dv, "=", 1);
  60. if (xs_list_len(kv) == 2) {
  61. const char *key = xs_list_get(kv, 0);
  62. const char *pv = xs_dict_get(vars, key);
  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. 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. return vars;
  89. }
  90. xs_dict *xs_multipart_form_data(const char *payload, int p_size, const char *header)
  91. /* parses a multipart/form-data payload */
  92. {
  93. xs *boundary = NULL;
  94. int offset = 0;
  95. int bsz;
  96. char *p;
  97. /* build the boundary string */
  98. {
  99. xs *l1 = xs_split(header, "=");
  100. if (xs_list_len(l1) != 2)
  101. return NULL;
  102. xs *t_boundary = xs_dup(xs_list_get(l1, 1));
  103. /* Tokodon sends the boundary header with double quotes surrounded */
  104. if (xs_between("\"", t_boundary, "\"") != 0)
  105. t_boundary = xs_strip_chars_i(t_boundary, "\"");
  106. boundary = xs_fmt("--%s", t_boundary);
  107. }
  108. bsz = strlen(boundary);
  109. xs_dict *p_vars = xs_dict_new();
  110. /* iterate searching the boundaries */
  111. while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) {
  112. xs *s1 = NULL;
  113. xs *l1 = NULL;
  114. const char *vn = NULL;
  115. const char *fn = NULL;
  116. const char *ct = NULL;
  117. char *q;
  118. int po, ps;
  119. /* final boundary? */
  120. p += bsz;
  121. if (p[0] == '-' && p[1] == '-')
  122. break;
  123. /* skip the \r\n */
  124. p += 2;
  125. /* Tokodon sends also a Content-Type headers,
  126. let's use it to determine the file type */
  127. do {
  128. if (p[0] == 13 && p[1] == 10)
  129. break;
  130. q = strchr(p, '\r');
  131. /* unexpected formatting, fail immediately */
  132. if (q == NULL)
  133. return p_vars;
  134. s1 = xs_realloc(NULL, q - p + 1);
  135. memcpy(s1, p, q - p);
  136. s1[q - p] = '\0';
  137. if (xs_startswith(s1, "Content-Disposition") || xs_startswith(s1, "content-disposition")) {
  138. /* split by " like a primitive man */
  139. l1 = xs_split(s1, "\"");
  140. /* get the variable name */
  141. vn = xs_list_get(l1, 1);
  142. /* is it an attached file? */
  143. if (xs_list_len(l1) >= 4 && strcmp(xs_list_get(l1, 2), "; filename=") == 0) {
  144. /* get the file name */
  145. fn = xs_list_get(l1, 3);
  146. }
  147. }
  148. else
  149. if (xs_startswith(s1, "Content-Type") || xs_startswith(s1, "content-type")) {
  150. l1 = xs_split(s1, ":");
  151. if (xs_list_len(l1) >= 2) {
  152. ct = xs_lstrip_chars_i(xs_dup(xs_list_get(l1, 1)), " ");
  153. }
  154. }
  155. p += (q - p);
  156. p += 2; // Skip /r/n
  157. } while (1);
  158. /* find the start of the part content */
  159. if ((p = xs_memmem(p, p_size - (p - payload), "\r\n", 2)) == NULL)
  160. break;
  161. p += 2; // Skip empty line
  162. /* find the next boundary */
  163. if ((q = xs_memmem(p, p_size - (p - payload), boundary, bsz)) == NULL)
  164. break;
  165. po = p - payload;
  166. ps = q - p - 2; /* - 2 because the final \r\n */
  167. /* is it a filename? */
  168. if (fn != NULL) {
  169. /* p_var value is a list */
  170. /* if filename has no extension and content-type is image, attach extension to the filename */
  171. if (strchr(fn, '.') == NULL && ct && xs_startswith(ct, "image/")) {
  172. char *ext = strchr(ct, '/');
  173. ext++;
  174. fn = xs_str_cat(xs_str_new(""), fn, ".", ext);
  175. }
  176. xs *l1 = xs_list_new();
  177. xs *vpo = xs_number_new(po);
  178. xs *vps = xs_number_new(ps);
  179. l1 = xs_list_append(l1, fn);
  180. l1 = xs_list_append(l1, vpo);
  181. l1 = xs_list_append(l1, vps);
  182. p_vars = xs_dict_append(p_vars, vn, l1);
  183. }
  184. else {
  185. /* regular variable; just copy */
  186. xs *vc = xs_realloc(NULL, ps + 1);
  187. memcpy(vc, payload + po, ps);
  188. vc[ps] = '\0';
  189. p_vars = xs_dict_append(p_vars, vn, vc);
  190. }
  191. /* move on */
  192. offset = q - payload;
  193. }
  194. return p_vars;
  195. }
  196. #endif /* XS_IMPLEMENTATION */
  197. #endif /* XS_URL_H */