xs_httpd.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* copyright (c) 2022 - 2025 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;
  13. const char *v;
  14. char *saveptr;
  15. xs_socket_timeout(fileno(f), 2.0, 0.0);
  16. /* read the first line and split it */
  17. l1 = xs_strip_i(xs_readline(f));
  18. char *raw_path;
  19. const char *mtd;
  20. const char *proto;
  21. if (!(mtd = strtok_r(l1, " ", &saveptr)) ||
  22. !(raw_path = strtok_r(NULL, " ", &saveptr)) ||
  23. !(proto = strtok_r(NULL, " ", &saveptr)) ||
  24. strtok_r(NULL, " ", &saveptr))
  25. return NULL;
  26. if (!xs_is_string(mtd) || !xs_is_string(raw_path) || !xs_is_string(proto))
  27. return NULL;
  28. xs_dict *req = xs_dict_new();
  29. req = xs_dict_append(req, "method", mtd);
  30. req = xs_dict_append(req, "raw_path", raw_path);
  31. req = xs_dict_append(req, "proto", proto);
  32. {
  33. char *q = strchr(raw_path, '?');
  34. /* get the variables */
  35. if (q) {
  36. *q++ = '\0';
  37. q_vars = xs_url_vars(q);
  38. }
  39. /* store the path */
  40. req = xs_dict_append(req, "path", raw_path);
  41. }
  42. /* read the headers */
  43. for (;;) {
  44. xs *l;
  45. l = xs_strip_i(xs_readline(f));
  46. /* done with the header? */
  47. if (strcmp(l, "") == 0)
  48. break;
  49. /* split header and content */
  50. char *cnt = strchr(l, ':');
  51. if (!cnt)
  52. continue;
  53. *cnt++ = '\0';
  54. cnt += strspn(cnt, " \r\n\t\v\f");
  55. xs_rstrip_chars_i(l, " \r\n\t\v\f");
  56. if (!xs_is_string(cnt))
  57. continue;
  58. req = xs_dict_append(req, xs_tolower_i(l), cnt);
  59. }
  60. xs_socket_timeout(fileno(f), 5.0, 0.0);
  61. if ((v = xs_dict_get(req, "content-length")) != NULL) {
  62. /* if it has a payload, load it */
  63. *p_size = atoi(v);
  64. *payload = xs_read(f, p_size);
  65. }
  66. v = xs_dict_get(req, "content-type");
  67. if (*payload && v && strcmp(v, "application/x-www-form-urlencoded") == 0) {
  68. p_vars = xs_url_vars(*payload);
  69. }
  70. else
  71. if (*payload && v && xs_startswith(v, "multipart/form-data")) {
  72. p_vars = xs_multipart_form_data(*payload, *p_size, v);
  73. }
  74. else
  75. p_vars = xs_dict_new();
  76. req = xs_dict_append(req, "q_vars", q_vars);
  77. req = xs_dict_append(req, "p_vars", p_vars);
  78. if (errno)
  79. req = xs_free(req);
  80. return req;
  81. }
  82. void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *headers, xs_str *body, int b_size)
  83. /* sends an httpd response */
  84. {
  85. xs *proto;
  86. const xs_str *k;
  87. const xs_val *v;
  88. proto = xs_fmt("HTTP/1.1 %d %s", status, status_text);
  89. fprintf(f, "%s\r\n", proto);
  90. xs_dict_foreach(headers, k, v) {
  91. fprintf(f, "%s: %s\r\n", k, v);
  92. }
  93. if (b_size != 0)
  94. fprintf(f, "content-length: %d\r\n", b_size);
  95. fprintf(f, "\r\n");
  96. if (body != NULL && b_size != 0)
  97. fwrite(body, b_size, 1, f);
  98. }
  99. #endif /* XS_IMPLEMENTATION */
  100. #endif /* XS_HTTPD_H */