2.59 xs_httpd.h
  1/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  2
  3#ifndef _XS_HTTPD_H
  4
  5#define _XS_HTTPD_H
  6
  7xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size);
  8void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *headers, xs_str *body, int b_size);
  9
 10
 11#ifdef XS_IMPLEMENTATION
 12
 13xs_dict *xs_httpd_request(FILE *f, xs_str **payload, int *p_size)
 14/* processes an httpd connection */
 15{
 16    xs *q_vars = NULL;
 17    xs *p_vars = NULL;
 18    xs *l1, *l2;
 19    const char *v;
 20
 21    xs_socket_timeout(fileno(f), 2.0, 0.0);
 22
 23    /* read the first line and split it */
 24    l1 = xs_strip_i(xs_readline(f));
 25    l2 = xs_split(l1, " ");
 26
 27    if (xs_list_len(l2) != 3) {
 28        /* error or timeout */
 29        return NULL;
 30    }
 31
 32    xs_dict *req = xs_dict_new();
 33
 34    req = xs_dict_append(req, "method", xs_list_get(l2, 0));
 35    req = xs_dict_append(req, "proto",  xs_list_get(l2, 2));
 36
 37    {
 38        /* split the path with its optional variables */
 39        xs *udp = xs_url_dec(xs_list_get(l2, 1));
 40        xs *pnv = xs_split_n(udp, "?", 1);
 41
 42        /* store the path */
 43        req = xs_dict_append(req, "path", xs_list_get(pnv, 0));
 44
 45        /* get the variables */
 46        q_vars = xs_url_vars(xs_list_get(pnv, 1));
 47    }
 48
 49    /* read the headers */
 50    for (;;) {
 51        xs *l, *p = NULL;
 52
 53        l = xs_strip_i(xs_readline(f));
 54
 55        /* done with the header? */
 56        if (strcmp(l, "") == 0)
 57            break;
 58
 59        /* split header and content */
 60        p = xs_split_n(l, ": ", 1);
 61
 62        if (xs_list_len(p) == 2)
 63            req = xs_dict_append(req, xs_tolower_i(
 64                    (xs_str *)xs_list_get(p, 0)), xs_list_get(p, 1));
 65    }
 66
 67    xs_socket_timeout(fileno(f), 5.0, 0.0);
 68
 69    if ((v = xs_dict_get(req, "content-length")) != NULL) {
 70        /* if it has a payload, load it */
 71        *p_size  = atoi(v);
 72        *payload = xs_read(f, p_size);
 73    }
 74
 75    v = xs_dict_get(req, "content-type");
 76
 77    if (*payload && v && strcmp(v, "application/x-www-form-urlencoded") == 0) {
 78        xs *upl = xs_url_dec(*payload);
 79        p_vars  = xs_url_vars(upl);
 80    }
 81    else
 82    if (*payload && v && xs_startswith(v, "multipart/form-data")) {
 83        p_vars = xs_multipart_form_data(*payload, *p_size, v);
 84    }
 85    else
 86        p_vars = xs_dict_new();
 87
 88    req = xs_dict_append(req, "q_vars",  q_vars);
 89    req = xs_dict_append(req, "p_vars",  p_vars);
 90
 91    if (errno)
 92        req = xs_free(req);
 93
 94    return req;
 95}
 96
 97
 98void xs_httpd_response(FILE *f, int status, const char *status_text, xs_dict *headers, xs_str *body, int b_size)
 99/* sends an httpd response */
100{
101    xs *proto;
102    const xs_str *k;
103    const xs_val *v;
104
105    proto = xs_fmt("HTTP/1.1 %d %s", status, status_text);
106    fprintf(f, "%s\r\n", proto);
107
108    xs_dict_foreach(headers, k, v) {
109        fprintf(f, "%s: %s\r\n", k, v);
110    }
111
112    if (b_size != 0)
113        fprintf(f, "content-length: %d\r\n", b_size);
114
115    fprintf(f, "\r\n");
116
117    if (body != NULL && b_size != 0)
118        fwrite(body, b_size, 1, f);
119}
120
121
122#endif /* XS_IMPLEMENTATION */
123
124#endif /* XS_HTTPD_H */