xs_url.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. 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. xs_list *l;
  39. xs_val *v;
  40. l = args;
  41. while (xs_list_iter(&l, &v)) {
  42. xs *kv = xs_split_n(v, "=", 1);
  43. if (xs_list_len(kv) == 2) {
  44. const char *key = xs_list_get(kv, 0);
  45. const char *pv = xs_dict_get(vars, key);
  46. if (!xs_is_null(pv)) {
  47. /* there is a previous value: convert to a list and append */
  48. xs *vlist = NULL;
  49. if (xs_type(pv) == XSTYPE_LIST)
  50. vlist = xs_dup(pv);
  51. else {
  52. vlist = xs_list_new();
  53. vlist = xs_list_append(vlist, pv);
  54. }
  55. vlist = xs_list_append(vlist, xs_list_get(kv, 1));
  56. vars = xs_dict_set(vars, key, vlist);
  57. }
  58. else {
  59. /* ends with []? force to always be a list */
  60. if (xs_endswith(key, "[]")) {
  61. xs *vlist = xs_list_new();
  62. vlist = xs_list_append(vlist, xs_list_get(kv, 1));
  63. vars = xs_dict_append(vars, key, vlist);
  64. }
  65. else
  66. vars = xs_dict_append(vars, key, xs_list_get(kv, 1));
  67. }
  68. }
  69. }
  70. }
  71. return vars;
  72. }
  73. xs_dict *xs_multipart_form_data(const char *payload, int p_size, const char *header)
  74. /* parses a multipart/form-data payload */
  75. {
  76. xs *boundary = NULL;
  77. int offset = 0;
  78. int bsz;
  79. char *p;
  80. /* build the boundary string */
  81. {
  82. xs *l1 = xs_split(header, "=");
  83. if (xs_list_len(l1) != 2)
  84. return NULL;
  85. boundary = xs_fmt("--%s", xs_list_get(l1, 1));
  86. }
  87. bsz = strlen(boundary);
  88. xs_dict *p_vars = xs_dict_new();
  89. /* iterate searching the boundaries */
  90. while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) {
  91. xs *s1 = NULL;
  92. xs *l1 = NULL;
  93. char *vn = NULL;
  94. char *fn = NULL;
  95. char *q;
  96. int po, ps;
  97. /* final boundary? */
  98. p += bsz;
  99. if (p[0] == '-' && p[1] == '-')
  100. break;
  101. /* skip the \r\n */
  102. p += 2;
  103. /* now on a Content-Disposition... line; get it */
  104. q = strchr(p, '\r');
  105. s1 = xs_realloc(NULL, q - p + 1);
  106. memcpy(s1, p, q - p);
  107. s1[q - p] = '\0';
  108. /* move on (over a \r\n) */
  109. p = q;
  110. /* split by " like a primitive man */
  111. l1 = xs_split(s1, "\"");
  112. /* get the variable name */
  113. vn = xs_list_get(l1, 1);
  114. /* is it an attached file? */
  115. if (xs_list_len(l1) >= 4 && strcmp(xs_list_get(l1, 2), "; filename=") == 0) {
  116. /* get the file name */
  117. fn = xs_list_get(l1, 3);
  118. }
  119. /* find the start of the part content */
  120. if ((p = xs_memmem(p, p_size - (p - payload), "\r\n\r\n", 4)) == NULL)
  121. break;
  122. p += 4;
  123. /* find the next boundary */
  124. if ((q = xs_memmem(p, p_size - (p - payload), boundary, bsz)) == NULL)
  125. break;
  126. po = p - payload;
  127. ps = q - p - 2; /* - 2 because the final \r\n */
  128. /* is it a filename? */
  129. if (fn != NULL) {
  130. /* p_var value is a list */
  131. xs *l1 = xs_list_new();
  132. xs *vpo = xs_number_new(po);
  133. xs *vps = xs_number_new(ps);
  134. l1 = xs_list_append(l1, fn);
  135. l1 = xs_list_append(l1, vpo);
  136. l1 = xs_list_append(l1, vps);
  137. p_vars = xs_dict_append(p_vars, vn, l1);
  138. }
  139. else {
  140. /* regular variable; just copy */
  141. xs *vc = xs_realloc(NULL, ps + 1);
  142. memcpy(vc, payload + po, ps);
  143. vc[ps] = '\0';
  144. p_vars = xs_dict_append(p_vars, vn, vc);
  145. }
  146. /* move on */
  147. offset = q - payload;
  148. }
  149. return p_vars;
  150. }
  151. #endif /* XS_IMPLEMENTATION */
  152. #endif /* XS_URL_H */