summaryrefslogtreecommitdiff
path: root/src/fconn_fastcgi.c
blob: 1d21603eca64ed8489213498dc4fc0f5184d64bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* Copyright (c) 2015-2016 Yoran Heling

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "fcgy.h"

/* Comparison for use with the c->reqs vector */
#define req_cmp(c, val) (((int)(c)->reqs.a[i]->fconn_id) - ((int)val))


static void handle_begin_request(fconn_fastcgi *c, uint16_t id, size_t len, const char *buf) {
    if(id == 0 || len < 8 || *buf != 0 || buf[1] < 1 || buf[1] > 3) {
        fprintf(stderr, "Invalid BEGIN_REQUEST record\n");
        fconn_fastcgi_destroy(c);
        return;
    }

    size_t n;
    vec_search_insert(c->reqs, n, req_cmp(c, id));
    if(n < c->reqs.n && c->reqs.a[n]->fconn_id == id) {
        fprintf(stderr, "BEGIN_REQUEST record for existing request ID\n");
        fconn_fastcgi_destroy(c);
        return;
    }
    *vec_insert_orderp(c->reqs, n) = req_create(c, id);
}


static void handle_params(fconn_fastcgi *c, fcgy_req *req, size_t len, const char *buf) {
    if(!len) /* End of param list. Should signal this to fcgy_req? */
        return;

    ssize_t n;
    while(len > 0) {
        n = fastcgi_param_parse(c->pp, buf, len);
        if(n < 0) {
            fprintf(stderr, "Invalid parameter\n");
            fconn_fastcgi_destroy(c);
            return;
        }
        if(n == 0)
            break;

        req_set_param(req, c->pp->namelen, c->pp->valuelen, c->pp->buf);
        c->pp->buf = NULL; /* To avoid a double-free in fconn_fastcgi_destroy() */
        len -= n;
        buf += n;
    }
}


static fcgy_req *get_request(fconn_fastcgi *c, uint16_t id) {
    vec_search(c->reqs, req_cmp(c, id), return c->reqs.a[i]);
    return NULL;
}


static void read_cb(fastcgi_reader *r, ssize_t len, fastcgi_header h, const char *buf) {
    fconn_fastcgi *c = r->data;

    if(len < 0) {
        fprintf(stderr, "Read error: %s\n", strerror(errno));
        fconn_fastcgi_destroy(c);
        return;
    }

    fprintf(stderr, "Got FastCGI record: len = %4u, version = %u, type = %2u, requestId = %2u, contentLength = %4u\n",
        (unsigned)len, (unsigned)h.version, (unsigned)h.type, (unsigned)h.requestId, (unsigned)h.contentLength);

    /* TODO: Handle management records here */

    if(h.type == FCGI_BEGIN_REQUEST) {
        handle_begin_request(c, h.requestId, h.contentLength, buf);
        return;
    }

    fcgy_req *req = get_request(c, h.requestId);
    if(!req) {
        fprintf(stderr, "Received record for unknown request id (%u)\n", (unsigned)h.requestId);
        fconn_fastcgi_destroy(c);
        return;
    }

    if(h.type == FCGI_PARAMS)
        handle_params(c, req, h.contentLength, buf);

    /* TODO: Handle other request records here */
}


void fconn_fastcgi_create(fcgy_app *app, int rfd, int wfd) {
    fconn_fastcgi *c = calloc(1, sizeof(fconn_fastcgi));
    c->app = app;
    c->rfd = rfd;
    c->wfd = wfd;

    c->rd = fastcgi_reader_create(c->rfd, read_cb, c);
    fastcgi_reader_start(c->rd);

    hlist_prepend(app->fconns, c);
}


void fconn_fastcgi_destroy(fconn_fastcgi *c) {
    fastcgi_reader_destroy(c->rd);
    hlist_remove(c->app->fconns, c);

    free(c->pp->buf);

    size_t i;
    for(i=0; i<c->reqs.n; i++)
        req_unset_front(c->reqs.a[i]);
    free(c->reqs.a);

    close(c->wfd);
    if(c->wfd != c->rfd)
        close(c->rfd);

    free(c);
}