xs_url.h 6.3 KB

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