xs_httpd.h 6.6 KB

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