snac2/xs_httpd.h

126 lines
3 KiB
C
Raw Normal View History

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