xs_httpd.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  2. #ifndef _XS_HTTPD_H
  3. #define _XS_HTTPD_H
  4. xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size);
  5. void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *headers, xs_str *body, int b_size);
  6. #ifdef XS_IMPLEMENTATION
  7. xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size)
  8. /* processes an httpd connection */
  9. {
  10. xs *q_vars = NULL;
  11. xs *p_vars = NULL;
  12. xs *l1, *l2;
  13. const char *v;
  14. xs_socket_timeout(fileno(f), 2.0, 0.0);
  15. /* read the first line and split it */
  16. l1 = xs_strip_i(xs_readline(f));
  17. l2 = xs_split(l1, " ");
  18. if (xs_list_len(l2) != 3) {
  19. /* error or timeout */
  20. return NULL;
  21. }
  22. xs_dict *req = xs_dict_new();
  23. req = xs_dict_append(req, "method", xs_list_get(l2, 0));
  24. req = xs_dict_append(req, "raw_path", xs_list_get(l2, 1));
  25. req = xs_dict_append(req, "proto", xs_list_get(l2, 2));
  26. {
  27. /* split the path with its optional variables */
  28. const xs_val *udp = xs_list_get(l2, 1);
  29. xs *pnv = xs_split_n(udp, "?", 1);
  30. /* store the path */
  31. req = xs_dict_append(req, "path", xs_list_get(pnv, 0));
  32. /* get the variables */
  33. q_vars = xs_url_vars(xs_list_get(pnv, 1));
  34. }
  35. /* read the headers */
  36. for (;;) {
  37. xs *l, *p = NULL;
  38. l = xs_strip_i(xs_readline(f));
  39. /* done with the header? */
  40. if (strcmp(l, "") == 0)
  41. break;
  42. /* split header and content */
  43. p = xs_split_n(l, ": ", 1);
  44. if (xs_list_len(p) == 2)
  45. req = xs_dict_append(req, xs_tolower_i(
  46. (xs_str *)xs_list_get(p, 0)), xs_list_get(p, 1));
  47. }
  48. xs_socket_timeout(fileno(f), 5.0, 0.0);
  49. if ((v = xs_dict_get(req, "content-length")) != NULL) {
  50. /* if it has a payload, load it */
  51. *p_size = atoi(v);
  52. *payload = xs_read(f, p_size);
  53. }
  54. v = xs_dict_get(req, "content-type");
  55. if (*payload && v && strcmp(v, "application/x-www-form-urlencoded") == 0) {
  56. p_vars = xs_url_vars(*payload);
  57. }
  58. else
  59. if (*payload && v && xs_startswith(v, "multipart/form-data")) {
  60. p_vars = xs_multipart_form_data(*payload, *p_size, v);
  61. }
  62. else
  63. p_vars = xs_dict_new();
  64. req = xs_dict_append(req, "q_vars", q_vars);
  65. req = xs_dict_append(req, "p_vars", p_vars);
  66. if (errno)
  67. req = xs_free(req);
  68. return req;
  69. }
  70. void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *headers, xs_str *body, int b_size)
  71. /* sends an httpd response */
  72. {
  73. xs *proto;
  74. const xs_str *k;
  75. const xs_val *v;
  76. proto = xs_fmt("HTTP/1.1 %d %s", status, status_text);
  77. fprintf(f, "%s\r\n", proto);
  78. xs_dict_foreach(headers, k, v) {
  79. fprintf(f, "%s: %s\r\n", k, v);
  80. }
  81. if (b_size != 0)
  82. fprintf(f, "content-length: %d\r\n", b_size);
  83. fprintf(f, "\r\n");
  84. if (body != NULL && b_size != 0)
  85. fwrite(body, b_size, 1, f);
  86. }
  87. #endif /* XS_IMPLEMENTATION */
  88. #endif /* XS_HTTPD_H */