xs_httpd.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
  2. #ifndef _XS_HTTPD_H
  3. #define _XS_HTTPD_H
  4. xs_str *xs_url_dec(const char *str);
  5. xs_dict *xs_url_vars(const char *str);
  6. xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size);
  7. void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size);
  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. 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_dict *xs_url_vars(const char *str)
  32. /* parse url variables */
  33. {
  34. xs_dict *vars;
  35. vars = xs_dict_new();
  36. if (str != NULL) {
  37. /* split by arguments */
  38. xs *args = xs_split(str, "&");
  39. xs_list *l;
  40. xs_val *v;
  41. l = args;
  42. while (xs_list_iter(&l, &v)) {
  43. xs *kv = xs_split_n(v, "=", 2);
  44. if (xs_list_len(kv) == 2) {
  45. const char *key = xs_list_get(kv, 0);
  46. const char *pv = xs_dict_get(vars, key);
  47. if (!xs_is_null(pv)) {
  48. /* there is a previous value: convert to a list and append */
  49. xs *vlist = NULL;
  50. if (xs_type(pv) == XSTYPE_LIST)
  51. vlist = xs_dup(pv);
  52. else {
  53. vlist = xs_list_new();
  54. vlist = xs_list_append(vlist, pv);
  55. }
  56. vlist = xs_list_append(vlist, xs_list_get(kv, 1));
  57. vars = xs_dict_set(vars, key, vlist);
  58. }
  59. else {
  60. /* ends with []? force to always be a list */
  61. if (xs_endswith(key, "[]")) {
  62. xs *vlist = xs_list_new();
  63. vlist = xs_list_append(vlist, xs_list_get(kv, 1));
  64. vars = xs_dict_append(vars, key, vlist);
  65. }
  66. else
  67. vars = xs_dict_append(vars, key, xs_list_get(kv, 1));
  68. }
  69. }
  70. }
  71. }
  72. return vars;
  73. }
  74. xs_dict *_xs_multipart_form_data(xs_str *payload, int p_size, char *header)
  75. /* parses a multipart/form-data payload */
  76. {
  77. xs *boundary = NULL;
  78. int offset = 0;
  79. int bsz;
  80. char *p;
  81. /* build the boundary string */
  82. {
  83. xs *l1 = xs_split(header, "=");
  84. if (xs_list_len(l1) != 2)
  85. return NULL;
  86. boundary = xs_fmt("--%s", xs_list_get(l1, 1));
  87. }
  88. bsz = strlen(boundary);
  89. xs_dict *p_vars = xs_dict_new();
  90. /* iterate searching the boundaries */
  91. while ((p = xs_memmem(payload + offset, p_size - offset, boundary, bsz)) != NULL) {
  92. xs *s1 = NULL;
  93. xs *l1 = NULL;
  94. char *vn = NULL;
  95. char *fn = NULL;
  96. char *q;
  97. int po, ps;
  98. /* final boundary? */
  99. p += bsz;
  100. if (p[0] == '-' && p[1] == '-')
  101. break;
  102. /* skip the \r\n */
  103. p += 2;
  104. /* now on a Content-Disposition... line; get it */
  105. q = strchr(p, '\r');
  106. s1 = xs_realloc(NULL, q - p + 1);
  107. memcpy(s1, p, q - p);
  108. s1[q - p] = '\0';
  109. /* move on (over a \r\n) */
  110. p = q;
  111. /* split by " like a primitive man */
  112. l1 = xs_split(s1, "\"");
  113. /* get the variable name */
  114. vn = xs_list_get(l1, 1);
  115. /* is it an attached file? */
  116. if (xs_list_len(l1) >= 4 && strcmp(xs_list_get(l1, 2), "; filename=") == 0) {
  117. /* get the file name */
  118. fn = xs_list_get(l1, 3);
  119. }
  120. /* find the start of the part content */
  121. if ((p = xs_memmem(p, p_size - (p - payload), "\r\n\r\n", 4)) == NULL)
  122. break;
  123. p += 4;
  124. /* find the next boundary */
  125. if ((q = xs_memmem(p, p_size - (p - payload), boundary, bsz)) == NULL)
  126. break;
  127. po = p - payload;
  128. ps = q - p - 2; /* - 2 because the final \r\n */
  129. /* is it a filename? */
  130. if (fn != NULL) {
  131. /* p_var value is a list */
  132. xs *l1 = xs_list_new();
  133. xs *vpo = xs_number_new(po);
  134. xs *vps = xs_number_new(ps);
  135. l1 = xs_list_append(l1, fn);
  136. l1 = xs_list_append(l1, vpo);
  137. l1 = xs_list_append(l1, vps);
  138. p_vars = xs_dict_append(p_vars, vn, l1);
  139. }
  140. else {
  141. /* regular variable; just copy */
  142. xs *vc = xs_realloc(NULL, ps + 1);
  143. memcpy(vc, payload + po, ps);
  144. vc[ps] = '\0';
  145. p_vars = xs_dict_append(p_vars, vn, vc);
  146. }
  147. /* move on */
  148. offset = q - payload;
  149. }
  150. return p_vars;
  151. }
  152. xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size)
  153. /* processes an httpd connection */
  154. {
  155. xs *q_vars = NULL;
  156. xs *p_vars = NULL;
  157. xs *l1, *l2;
  158. char *v;
  159. xs_socket_timeout(fileno(f), 2.0, 0.0);
  160. /* read the first line and split it */
  161. l1 = xs_strip_i(xs_readline(f));
  162. l2 = xs_split(l1, " ");
  163. if (xs_list_len(l2) != 3) {
  164. /* error or timeout */
  165. return NULL;
  166. }
  167. xs_dict *req = xs_dict_new();
  168. req = xs_dict_append(req, "method", xs_list_get(l2, 0));
  169. req = xs_dict_append(req, "proto", xs_list_get(l2, 2));
  170. {
  171. /* split the path with its optional variables */
  172. xs *udp = xs_url_dec(xs_list_get(l2, 1));
  173. xs *pnv = xs_split_n(udp, "?", 1);
  174. /* store the path */
  175. req = xs_dict_append(req, "path", xs_list_get(pnv, 0));
  176. /* get the variables */
  177. q_vars = xs_url_vars(xs_list_get(pnv, 1));
  178. }
  179. /* read the headers */
  180. for (;;) {
  181. xs *l, *p = NULL;
  182. l = xs_strip_i(xs_readline(f));
  183. /* done with the header? */
  184. if (strcmp(l, "") == 0)
  185. break;
  186. /* split header and content */
  187. p = xs_split_n(l, ": ", 1);
  188. if (xs_list_len(p) == 2)
  189. req = xs_dict_append(req, xs_tolower_i(xs_list_get(p, 0)), xs_list_get(p, 1));
  190. }
  191. xs_socket_timeout(fileno(f), 5.0, 0.0);
  192. if ((v = xs_dict_get(req, "content-length")) != NULL) {
  193. /* if it has a payload, load it */
  194. *p_size = atoi(v);
  195. *payload = xs_read(f, p_size);
  196. }
  197. v = xs_dict_get(req, "content-type");
  198. if (*payload && v && strcmp(v, "application/x-www-form-urlencoded") == 0) {
  199. xs *upl = xs_url_dec(*payload);
  200. p_vars = xs_url_vars(upl);
  201. }
  202. else
  203. if (*payload && v && xs_startswith(v, "multipart/form-data")) {
  204. p_vars = _xs_multipart_form_data(*payload, *p_size, v);
  205. }
  206. else
  207. p_vars = xs_dict_new();
  208. req = xs_dict_append(req, "q_vars", q_vars);
  209. req = xs_dict_append(req, "p_vars", p_vars);
  210. if (errno)
  211. req = xs_free(req);
  212. return req;
  213. }
  214. void xs_httpd_response(FILE *f, int status, xs_dict *headers, xs_str *body, int b_size)
  215. /* sends an httpd response */
  216. {
  217. xs *proto;
  218. xs_dict *p;
  219. xs_str *k;
  220. xs_val *v;
  221. proto = xs_fmt("HTTP/1.1 %d %s", status, status / 100 == 2 ? "OK" : "ERROR");
  222. fprintf(f, "%s\r\n", proto);
  223. p = headers;
  224. while (xs_dict_iter(&p, &k, &v)) {
  225. fprintf(f, "%s: %s\r\n", k, v);
  226. }
  227. if (b_size != 0)
  228. fprintf(f, "content-length: %d\r\n", b_size);
  229. fprintf(f, "\r\n");
  230. if (body != NULL && b_size != 0)
  231. fwrite(body, b_size, 1, f);
  232. }
  233. #endif /* XS_IMPLEMENTATION */
  234. #endif /* XS_HTTPD_H */