/* 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; ireqs.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); }