snac2/xs_httpd.h

126 lines
3.0 KiB
C
Raw Normal View History

2024-01-04 11:22:03 +03:00
/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
2022-09-19 21:41:11 +03:00
#ifndef _XS_HTTPD_H
#define _XS_HTTPD_H
2023-01-28 19:49:02 +03:00
xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size);
void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *headers, xs_str *body, int b_size);
2022-09-19 21:41:11 +03:00
#ifdef XS_IMPLEMENTATION
2023-01-28 19:49:02 +03:00
xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size)
2022-09-19 21:41:11 +03:00
/* processes an httpd connection */
{
2023-01-28 19:49:02 +03:00
xs *q_vars = NULL;
xs *p_vars = NULL;
2022-09-19 21:41:11 +03:00
xs *l1, *l2;
2024-05-21 15:12:15 +03:00
const char *v;
2022-09-19 21:41:11 +03:00
xs_socket_timeout(fileno(f), 2.0, 0.0);
/* read the first line and split it */
2023-01-12 11:28:02 +03:00
l1 = xs_strip_i(xs_readline(f));
2022-09-19 21:41:11 +03:00
l2 = xs_split(l1, " ");
if (xs_list_len(l2) != 3) {
/* error or timeout */
return NULL;
}
2023-01-28 19:49:02 +03:00
xs_dict *req = xs_dict_new();
2022-09-19 21:41:11 +03:00
req = xs_dict_append(req, "method", xs_list_get(l2, 0));
req = xs_dict_append(req, "proto", xs_list_get(l2, 2));
2022-09-19 21:41:11 +03:00
{
/* split the path with its optional variables */
xs *udp = xs_url_dec(xs_list_get(l2, 1));
2022-09-20 00:08:59 +03:00
xs *pnv = xs_split_n(udp, "?", 1);
2022-09-19 21:41:11 +03:00
/* store the path */
req = xs_dict_append(req, "path", xs_list_get(pnv, 0));
2022-09-19 21:41:11 +03:00
/* get the variables */
q_vars = xs_url_vars(xs_list_get(pnv, 1));
}
/* read the headers */
for (;;) {
xs *l, *p = NULL;
2023-01-12 11:28:02 +03:00
l = xs_strip_i(xs_readline(f));
2022-09-19 21:41:11 +03:00
/* done with the header? */
if (strcmp(l, "") == 0)
break;
/* split header and content */
2022-09-20 00:08:59 +03:00
p = xs_split_n(l, ": ", 1);
2022-09-19 21:41:11 +03:00
if (xs_list_len(p) == 2)
2024-05-21 15:12:15 +03:00
req = xs_dict_append(req, xs_tolower_i(
(xs_str *)xs_list_get(p, 0)), xs_list_get(p, 1));
2022-09-19 21:41:11 +03:00
}
xs_socket_timeout(fileno(f), 5.0, 0.0);
if ((v = xs_dict_get(req, "content-length")) != NULL) {
/* if it has a payload, load it */
*p_size = atoi(v);
2022-09-28 10:29:09 +03:00
*payload = xs_read(f, p_size);
}
v = xs_dict_get(req, "content-type");
2022-09-19 21:41:11 +03:00
2023-04-20 13:46:59 +03:00
if (*payload && v && strcmp(v, "application/x-www-form-urlencoded") == 0) {
xs *upl = xs_url_dec(*payload);
p_vars = xs_url_vars(upl);
2022-09-19 21:41:11 +03:00
}
else
2023-04-20 13:46:59 +03:00
if (*payload && v && xs_startswith(v, "multipart/form-data")) {
2023-09-25 19:25:09 +03:00
p_vars = xs_multipart_form_data(*payload, *p_size, v);
}
2022-09-19 21:41:11 +03:00
else
p_vars = xs_dict_new();
req = xs_dict_append(req, "q_vars", q_vars);
req = xs_dict_append(req, "p_vars", p_vars);
2022-10-25 10:32:41 +03:00
if (errno)
req = xs_free(req);
2022-09-19 21:41:11 +03:00
return req;
}
void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *headers, xs_str *body, int b_size)
2022-09-19 21:41:11 +03:00
/* sends an httpd response */
{
xs *proto;
2024-05-23 11:01:37 +03:00
const xs_str *k;
const xs_val *v;
2022-09-19 21:41:11 +03:00
proto = xs_fmt("HTTP/1.1 %d %s", status, status_text);
2022-09-19 21:41:11 +03:00
fprintf(f, "%s\r\n", proto);
2024-03-09 10:32:20 +03:00
int c = 0;
while (xs_dict_next(headers, &k, &v, &c)) {
2022-09-19 21:41:11 +03:00
fprintf(f, "%s: %s\r\n", k, v);
}
if (b_size != 0)
fprintf(f, "content-length: %d\r\n", b_size);
fprintf(f, "\r\n");
if (body != NULL && b_size != 0)
fwrite(body, b_size, 1, f);
}
#endif /* XS_IMPLEMENTATION */
#endif /* XS_HTTPD_H */