xs_httpd.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. char *body = NULL;
  71. *p_size = 0;
  72. for (;;) {
  73. xs *line = xs_strip_i(xs_readline(f));
  74. /* parse chunk size (in hex) */
  75. int chunk_size = strtol(line, NULL, 16);
  76. if (chunk_size <= 0)
  77. break;
  78. /* read chunk data */
  79. xs *chunk = xs_read(f, &chunk_size);
  80. if (chunk == NULL || chunk_size <= 0)
  81. break;
  82. body = xs_realloc(body, *p_size + chunk_size);
  83. memcpy(body + *p_size, chunk, chunk_size);
  84. *p_size += chunk_size;
  85. /* read trailing \r\n after chunk data */
  86. xs *dummy = xs_readline(f);
  87. }
  88. *payload = body;
  89. }
  90. v = xs_dict_get(req, "content-type");
  91. if (*payload && v && strcmp(v, "application/x-www-form-urlencoded") == 0) {
  92. p_vars = xs_url_vars(*payload);
  93. }
  94. else
  95. if (*payload && v && xs_startswith(v, "multipart/form-data")) {
  96. p_vars = xs_multipart_form_data(*payload, *p_size, v);
  97. }
  98. else
  99. p_vars = xs_dict_new();
  100. req = xs_dict_append(req, "q_vars", q_vars);
  101. req = xs_dict_append(req, "p_vars", p_vars);
  102. if (errno)
  103. req = xs_free(req);
  104. return req;
  105. }
  106. void xs_httpd_response(FILE *f, int status, const char *status_text,
  107. const xs_dict *headers, const xs_val *body, int b_size)
  108. /* sends an httpd response */
  109. {
  110. fprintf(f, "HTTP/1.1 %d %s\r\n", status, status_text ? status_text : "");
  111. const xs_str *k;
  112. const xs_val *v;
  113. xs_dict_foreach(headers, k, v) {
  114. fprintf(f, "%s: %s\r\n", k, v);
  115. }
  116. if (b_size != 0)
  117. fprintf(f, "content-length: %d\r\n", b_size);
  118. fprintf(f, "\r\n");
  119. if (body != NULL && b_size != 0)
  120. fwrite(body, b_size, 1, f);
  121. }
  122. #endif /* XS_IMPLEMENTATION */
  123. #endif /* XS_HTTPD_H */