xs_httpd.h 4.1 KB

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