2.64 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, "raw_path", xs_list_get(l2, 1));
 36    req = xs_dict_append(req, "proto",  xs_list_get(l2, 2));
 37
 38    {
 39        /* split the path with its optional variables */
 40        const xs_val *udp = xs_list_get(l2, 1);
 41        xs *pnv = xs_split_n(udp, "?", 1);
 42
 43        /* store the path */
 44        req = xs_dict_append(req, "path", xs_list_get(pnv, 0));
 45
 46        /* get the variables */
 47        q_vars = xs_url_vars(xs_list_get(pnv, 1));
 48    }
 49
 50    /* read the headers */
 51    for (;;) {
 52        xs *l, *p = NULL;
 53
 54        l = xs_strip_i(xs_readline(f));
 55
 56        /* done with the header? */
 57        if (strcmp(l, "") == 0)
 58            break;
 59
 60        /* split header and content */
 61        p = xs_split_n(l, ": ", 1);
 62
 63        if (xs_list_len(p) == 2)
 64            req = xs_dict_append(req, xs_tolower_i(
 65                    (xs_str *)xs_list_get(p, 0)), xs_list_get(p, 1));
 66    }
 67
 68    xs_socket_timeout(fileno(f), 5.0, 0.0);
 69
 70    if ((v = xs_dict_get(req, "content-length")) != NULL) {
 71        /* if it has a payload, load it */
 72        *p_size  = atoi(v);
 73        *payload = xs_read(f, p_size);
 74    }
 75
 76    v = xs_dict_get(req, "content-type");
 77
 78    if (*payload && v && strcmp(v, "application/x-www-form-urlencoded") == 0) {
 79        p_vars  = xs_url_vars(*payload);
 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 */