18abd06a7SGlenn Strauss #include "first.h"
28abd06a7SGlenn Strauss
322e8b456SStefan Bühler #include <sys/types.h>
404d76e7aSGlenn Strauss #include <stdlib.h>
522e8b456SStefan Bühler #include <string.h>
645b970e6SGlenn Strauss
745b970e6SGlenn Strauss #include "gw_backend.h"
845b970e6SGlenn Strauss typedef gw_plugin_config plugin_config;
945b970e6SGlenn Strauss typedef gw_plugin_data plugin_data;
1045b970e6SGlenn Strauss typedef gw_handler_ctx handler_ctx;
1145b970e6SGlenn Strauss
1245b970e6SGlenn Strauss #include "buffer.h"
1345b970e6SGlenn Strauss #include "fdevent.h"
14c95f832fSGlenn Strauss #include "http_cgi.h"
1545b970e6SGlenn Strauss #include "http_chunk.h"
1645b970e6SGlenn Strauss #include "log.h"
1767c0b149SGlenn Strauss #include "request.h"
1822e8b456SStefan Bühler
1954922d61SGlenn Strauss #include "compat/fastcgi.h"
2022e8b456SStefan Bühler
2145b970e6SGlenn Strauss #if GW_RESPONDER != FCGI_RESPONDER
2245b970e6SGlenn Strauss #error "mismatched defines: (GW_RESPONDER != FCGI_RESPONDER)"
23bcdc6a3bSJan Kneschke #endif
2445b970e6SGlenn Strauss #if GW_AUTHORIZER != FCGI_AUTHORIZER
2545b970e6SGlenn Strauss #error "mismatched defines: (GW_AUTHORIZER != FCGI_AUTHORIZER)"
26bcdc6a3bSJan Kneschke #endif
2745b970e6SGlenn Strauss #if GW_FILTER != FCGI_FILTER
2845b970e6SGlenn Strauss #error "mismatched defines: (GW_FILTER != FCGI_FILTER)"
2945b970e6SGlenn Strauss #endif
3097556d99SGlenn Strauss
mod_fastcgi_merge_config_cpv(plugin_config * const pconf,const config_plugin_value_t * const cpv)314a6fe838SGlenn Strauss static void mod_fastcgi_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
324a6fe838SGlenn Strauss switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
334a6fe838SGlenn Strauss case 0: /* fastcgi.server */
344a6fe838SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) {
354a6fe838SGlenn Strauss gw_plugin_config * const gw = cpv->v.v;
364a6fe838SGlenn Strauss pconf->exts = gw->exts;
374a6fe838SGlenn Strauss pconf->exts_auth = gw->exts_auth;
384a6fe838SGlenn Strauss pconf->exts_resp = gw->exts_resp;
394a6fe838SGlenn Strauss }
404a6fe838SGlenn Strauss break;
414a6fe838SGlenn Strauss case 1: /* fastcgi.balance */
424a6fe838SGlenn Strauss /*if (cpv->vtype == T_CONFIG_LOCAL)*//*always true here for this param*/
434a6fe838SGlenn Strauss pconf->balance = (int)cpv->v.u;
444a6fe838SGlenn Strauss break;
454a6fe838SGlenn Strauss case 2: /* fastcgi.debug */
464a6fe838SGlenn Strauss pconf->debug = (int)cpv->v.u;
474a6fe838SGlenn Strauss break;
484a6fe838SGlenn Strauss case 3: /* fastcgi.map-extensions */
494a6fe838SGlenn Strauss pconf->ext_mapping = cpv->v.a;
504a6fe838SGlenn Strauss break;
514a6fe838SGlenn Strauss default:/* should not happen */
524a6fe838SGlenn Strauss return;
534a6fe838SGlenn Strauss }
544a6fe838SGlenn Strauss }
55bcdc6a3bSJan Kneschke
mod_fastcgi_merge_config(plugin_config * const pconf,const config_plugin_value_t * cpv)564a6fe838SGlenn Strauss static void mod_fastcgi_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
574a6fe838SGlenn Strauss do {
584a6fe838SGlenn Strauss mod_fastcgi_merge_config_cpv(pconf, cpv);
594a6fe838SGlenn Strauss } while ((++cpv)->k_id != -1);
604a6fe838SGlenn Strauss }
614a6fe838SGlenn Strauss
mod_fastcgi_patch_config(request_st * const r,plugin_data * const p)627c7f8c46SGlenn Strauss static void mod_fastcgi_patch_config(request_st * const r, plugin_data * const p) {
634a6fe838SGlenn Strauss memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
644a6fe838SGlenn Strauss for (int i = 1, used = p->nconfig; i < used; ++i) {
657c7f8c46SGlenn Strauss if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
664a6fe838SGlenn Strauss mod_fastcgi_merge_config(&p->conf,p->cvlist + p->cvlist[i].v.u2[0]);
674a6fe838SGlenn Strauss }
684a6fe838SGlenn Strauss }
694a6fe838SGlenn Strauss
SETDEFAULTS_FUNC(mod_fastcgi_set_defaults)704a6fe838SGlenn Strauss SETDEFAULTS_FUNC(mod_fastcgi_set_defaults) {
714a6fe838SGlenn Strauss static const config_plugin_keys_t cpk[] = {
724a6fe838SGlenn Strauss { CONST_STR_LEN("fastcgi.server"),
7303b4c993SGlenn Strauss T_CONFIG_ARRAY_KVARRAY,
744a6fe838SGlenn Strauss T_CONFIG_SCOPE_CONNECTION }
754a6fe838SGlenn Strauss ,{ CONST_STR_LEN("fastcgi.balance"),
764a6fe838SGlenn Strauss T_CONFIG_STRING,
774a6fe838SGlenn Strauss T_CONFIG_SCOPE_CONNECTION }
784a6fe838SGlenn Strauss ,{ CONST_STR_LEN("fastcgi.debug"),
794a6fe838SGlenn Strauss T_CONFIG_INT,
804a6fe838SGlenn Strauss T_CONFIG_SCOPE_CONNECTION }
814a6fe838SGlenn Strauss ,{ CONST_STR_LEN("fastcgi.map-extensions"),
8203b4c993SGlenn Strauss T_CONFIG_ARRAY_KVSTRING,
834a6fe838SGlenn Strauss T_CONFIG_SCOPE_CONNECTION }
844a6fe838SGlenn Strauss ,{ NULL, 0,
854a6fe838SGlenn Strauss T_CONFIG_UNSET,
864a6fe838SGlenn Strauss T_CONFIG_SCOPE_UNSET }
87bcdc6a3bSJan Kneschke };
88bcdc6a3bSJan Kneschke
894a6fe838SGlenn Strauss plugin_data * const p = p_d;
904a6fe838SGlenn Strauss if (!config_plugin_values_init(srv, p, cpk, "mod_fastcgi"))
914a6fe838SGlenn Strauss return HANDLER_ERROR;
92bcdc6a3bSJan Kneschke
934a6fe838SGlenn Strauss /* process and validate config directives
944a6fe838SGlenn Strauss * (init i to 0 if global context; to 1 to skip empty global context) */
954a6fe838SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
964a6fe838SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
974a6fe838SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) {
984a6fe838SGlenn Strauss switch (cpv->k_id) {
994a6fe838SGlenn Strauss case 0:{/* fastcgi.server */
100*5e14db43SGlenn Strauss gw_plugin_config *gw = ck_calloc(1, sizeof(gw_plugin_config));
1014a6fe838SGlenn Strauss if (!gw_set_defaults_backend(srv, p, cpv->v.a, gw, 0,
1024a6fe838SGlenn Strauss cpk[cpv->k_id].k)) {
1034a6fe838SGlenn Strauss gw_plugin_config_free(gw);
104fba7dd6fSStefan Bühler return HANDLER_ERROR;
105bcdc6a3bSJan Kneschke }
1064a6fe838SGlenn Strauss cpv->v.v = gw;
1074a6fe838SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL;
1084a6fe838SGlenn Strauss break;
1094a6fe838SGlenn Strauss }
1104a6fe838SGlenn Strauss case 1: /* fastcgi.balance */
1114a6fe838SGlenn Strauss cpv->v.u = (unsigned int)gw_get_defaults_balance(srv, cpv->v.b);
1124a6fe838SGlenn Strauss break;
1134a6fe838SGlenn Strauss case 2: /* fastcgi.debug */
1144a6fe838SGlenn Strauss case 3: /* fastcgi.map-extensions */
1154a6fe838SGlenn Strauss break;
1164a6fe838SGlenn Strauss default:/* should not happen */
1174a6fe838SGlenn Strauss break;
1182dcfe173SGlenn Strauss }
119bcdc6a3bSJan Kneschke }
1204a6fe838SGlenn Strauss }
1214a6fe838SGlenn Strauss
1224a6fe838SGlenn Strauss /* default is 0 */
1234a6fe838SGlenn Strauss /*p->defaults.balance = (unsigned int)gw_get_defaults_balance(srv, NULL);*/
1244a6fe838SGlenn Strauss
1254a6fe838SGlenn Strauss /* initialize p->defaults from global config context */
1264a6fe838SGlenn Strauss if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
1274a6fe838SGlenn Strauss const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
1284a6fe838SGlenn Strauss if (-1 != cpv->k_id)
1294a6fe838SGlenn Strauss mod_fastcgi_merge_config(&p->defaults, cpv);
1304a6fe838SGlenn Strauss }
131bcdc6a3bSJan Kneschke
132bcdc6a3bSJan Kneschke return HANDLER_GO_ON;
133bcdc6a3bSJan Kneschke }
134bcdc6a3bSJan Kneschke
1354a6fe838SGlenn Strauss
fcgi_env_add(void * venv,const char * key,size_t key_len,const char * val,size_t val_len)1367fa5bfc9SGlenn Strauss static int fcgi_env_add(void *venv, const char *key, size_t key_len, const char *val, size_t val_len) {
1377fa5bfc9SGlenn Strauss buffer *env = venv;
1384365bdbeSStefan Bühler char len_enc[8];
1394365bdbeSStefan Bühler size_t len_enc_len = 0;
140bcdc6a3bSJan Kneschke
141c0e2667bSGlenn Strauss if (!key || (!val && val_len)) return -1;
142bcdc6a3bSJan Kneschke
143eaed2f1eSJan Kneschke /**
144eaed2f1eSJan Kneschke * field length can be 31bit max
145eaed2f1eSJan Kneschke *
146eaed2f1eSJan Kneschke * HINT: this can't happen as FCGI_MAX_LENGTH is only 16bit
147eaed2f1eSJan Kneschke */
1484365bdbeSStefan Bühler force_assert(key_len < 0x7fffffffu);
1494365bdbeSStefan Bühler force_assert(val_len < 0x7fffffffu);
150eaed2f1eSJan Kneschke
151bcdc6a3bSJan Kneschke if (key_len > 127) {
15238c87358SGlenn Strauss len_enc[0] = ((key_len >> 24) & 0xff) | 0x80;
15338c87358SGlenn Strauss len_enc[1] = (key_len >> 16) & 0xff;
15438c87358SGlenn Strauss len_enc[2] = (key_len >> 8) & 0xff;
15538c87358SGlenn Strauss len_enc_len += 3;
156bcdc6a3bSJan Kneschke }
15738c87358SGlenn Strauss len_enc[len_enc_len++] = key_len & 0xff;
158bcdc6a3bSJan Kneschke
159bcdc6a3bSJan Kneschke if (val_len > 127) {
1604365bdbeSStefan Bühler len_enc[len_enc_len++] = ((val_len >> 24) & 0xff) | 0x80;
1614365bdbeSStefan Bühler len_enc[len_enc_len++] = (val_len >> 16) & 0xff;
1624365bdbeSStefan Bühler len_enc[len_enc_len++] = (val_len >> 8) & 0xff;
163bcdc6a3bSJan Kneschke }
16438c87358SGlenn Strauss len_enc[len_enc_len++] = val_len & 0xff;
165bcdc6a3bSJan Kneschke
16638c87358SGlenn Strauss const size_t len = len_enc_len + key_len + val_len;
16738c87358SGlenn Strauss const size_t fmax =
16838c87358SGlenn Strauss FCGI_MAX_LENGTH + sizeof(FCGI_BeginRequestRecord) + sizeof(FCGI_Header);
169af3df29aSGlenn Strauss if (len > fmax - buffer_clen(env))
17038c87358SGlenn Strauss return -1; /* we can't append more headers, ignore it */
17138c87358SGlenn Strauss
172dc01487eSGlenn Strauss buffer_append_str3(env, len_enc, len_enc_len, key, key_len, val, val_len);
173bcdc6a3bSJan Kneschke return 0;
174bcdc6a3bSJan Kneschke }
175bcdc6a3bSJan Kneschke
fcgi_header(FCGI_Header * header,unsigned char type,int request_id,int contentLength,unsigned char paddingLength)17645b970e6SGlenn Strauss static void fcgi_header(FCGI_Header * header, unsigned char type, int request_id, int contentLength, unsigned char paddingLength) {
17707dd0bd0SStefan Bühler force_assert(contentLength <= FCGI_MAX_LENGTH);
17834b3ee02SJan Kneschke
179bcdc6a3bSJan Kneschke header->version = FCGI_VERSION_1;
180bcdc6a3bSJan Kneschke header->type = type;
181bcdc6a3bSJan Kneschke header->requestIdB0 = request_id & 0xff;
182bcdc6a3bSJan Kneschke header->requestIdB1 = (request_id >> 8) & 0xff;
183bcdc6a3bSJan Kneschke header->contentLengthB0 = contentLength & 0xff;
184bcdc6a3bSJan Kneschke header->contentLengthB1 = (contentLength >> 8) & 0xff;
185bcdc6a3bSJan Kneschke header->paddingLength = paddingLength;
186bcdc6a3bSJan Kneschke header->reserved = 0;
187bcdc6a3bSJan Kneschke }
188bcdc6a3bSJan Kneschke
fcgi_stdin_append(handler_ctx * hctx)18950bdb55dSGlenn Strauss static handler_t fcgi_stdin_append(handler_ctx *hctx) {
190f69f209eSGlenn Strauss FCGI_Header header;
19181029b8bSGlenn Strauss chunkqueue * const req_cq = &hctx->r->reqbody_queue;
192f69f209eSGlenn Strauss off_t offset, weWant;
193f2b33e75SGlenn Strauss off_t req_cqlen = chunkqueue_length(req_cq);
19445b970e6SGlenn Strauss int request_id = hctx->request_id;
195f2b33e75SGlenn Strauss if (req_cqlen > MAX_WRITE_LIMIT) req_cqlen = MAX_WRITE_LIMIT;
196f69f209eSGlenn Strauss
197f69f209eSGlenn Strauss /* something to send ? */
198f69f209eSGlenn Strauss for (offset = 0; offset != req_cqlen; offset += weWant) {
199f69f209eSGlenn Strauss weWant = req_cqlen - offset > FCGI_MAX_LENGTH ? FCGI_MAX_LENGTH : req_cqlen - offset;
200f69f209eSGlenn Strauss
201316e959bSGlenn Strauss if (-1 != hctx->wb_reqlen) {
202316e959bSGlenn Strauss if (hctx->wb_reqlen >= 0) {
203f69f209eSGlenn Strauss hctx->wb_reqlen += sizeof(header);
204316e959bSGlenn Strauss } else {
205316e959bSGlenn Strauss hctx->wb_reqlen -= sizeof(header);
206316e959bSGlenn Strauss }
207316e959bSGlenn Strauss }
208f69f209eSGlenn Strauss
209a1b527e4SGlenn Strauss fcgi_header(&(header), FCGI_STDIN, request_id, weWant, 0);
21081029b8bSGlenn Strauss (chunkqueue_is_empty(&hctx->wb) || hctx->wb.first->type == MEM_CHUNK) /* else FILE_CHUNK for temp file */
21181029b8bSGlenn Strauss ? chunkqueue_append_mem(&hctx->wb, (const char *)&header, sizeof(header))
21281029b8bSGlenn Strauss : chunkqueue_append_mem_min(&hctx->wb, (const char *)&header, sizeof(header));
21381029b8bSGlenn Strauss chunkqueue_steal(&hctx->wb, req_cq, weWant);
214af5df352SGlenn Strauss /*(hctx->wb_reqlen already includes reqbody_length)*/
215f69f209eSGlenn Strauss }
216f69f209eSGlenn Strauss
21781029b8bSGlenn Strauss if (hctx->wb.bytes_in == hctx->wb_reqlen) {
218f69f209eSGlenn Strauss /* terminate STDIN */
219316e959bSGlenn Strauss /* (future: must defer ending FCGI_STDIN
220316e959bSGlenn Strauss * if might later upgrade protocols
221316e959bSGlenn Strauss * and then have more data to send) */
222f69f209eSGlenn Strauss fcgi_header(&(header), FCGI_STDIN, request_id, 0, 0);
22381029b8bSGlenn Strauss chunkqueue_append_mem(&hctx->wb, (const char *)&header, sizeof(header));
224f69f209eSGlenn Strauss hctx->wb_reqlen += (int)sizeof(header);
225f69f209eSGlenn Strauss }
22645b970e6SGlenn Strauss
22745b970e6SGlenn Strauss return HANDLER_GO_ON;
228f69f209eSGlenn Strauss }
229f69f209eSGlenn Strauss
fcgi_create_env(handler_ctx * hctx)23050bdb55dSGlenn Strauss static handler_t fcgi_create_env(handler_ctx *hctx) {
231bcdc6a3bSJan Kneschke FCGI_BeginRequestRecord beginRecord;
232bcdc6a3bSJan Kneschke FCGI_Header header;
233d26837d5SGlenn Strauss int request_id;
234bcdc6a3bSJan Kneschke
23545b970e6SGlenn Strauss gw_host *host = hctx->host;
2367c7f8c46SGlenn Strauss request_st * const r = hctx->r;
237bcdc6a3bSJan Kneschke
2387fa5bfc9SGlenn Strauss http_cgi_opts opts = {
23945b970e6SGlenn Strauss (hctx->gw_mode == FCGI_AUTHORIZER),
2407fa5bfc9SGlenn Strauss host->break_scriptfilename_for_php,
2417fa5bfc9SGlenn Strauss host->docroot,
2427fa5bfc9SGlenn Strauss host->strip_request_uri
2437fa5bfc9SGlenn Strauss };
244a39b5281SJan Kneschke
24581029b8bSGlenn Strauss size_t rsz = (size_t)(r->read_queue.bytes_out - hctx->wb.bytes_in);
24681029b8bSGlenn Strauss if (rsz >= 65536) rsz = r->rqst_header_len;
24781029b8bSGlenn Strauss buffer * const b = chunkqueue_prepend_buffer_open_sz(&hctx->wb, rsz);
2489f6a4673SGlenn Strauss
249bcdc6a3bSJan Kneschke /* send FCGI_BEGIN_REQUEST */
250bcdc6a3bSJan Kneschke
251d26837d5SGlenn Strauss if (hctx->request_id == 0) {
252d26837d5SGlenn Strauss hctx->request_id = 1; /* always use id 1 as we don't use multiplexing */
253d26837d5SGlenn Strauss } else {
2547c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
255010c2894SGlenn Strauss "fcgi-request is already in use: %d", hctx->request_id);
256d26837d5SGlenn Strauss }
257d26837d5SGlenn Strauss request_id = hctx->request_id;
258d26837d5SGlenn Strauss
259bcdc6a3bSJan Kneschke fcgi_header(&(beginRecord.header), FCGI_BEGIN_REQUEST, request_id, sizeof(beginRecord.body), 0);
26045b970e6SGlenn Strauss beginRecord.body.roleB0 = hctx->gw_mode;
261bcdc6a3bSJan Kneschke beginRecord.body.roleB1 = 0;
262bcdc6a3bSJan Kneschke beginRecord.body.flags = 0;
263bcdc6a3bSJan Kneschke memset(beginRecord.body.reserved, 0, sizeof(beginRecord.body.reserved));
264610d5c03SGlenn Strauss fcgi_header(&header, FCGI_PARAMS, request_id, 0, 0); /*(set aside space to fill in later)*/
265dc01487eSGlenn Strauss buffer_append_str2(b, (const char *)&beginRecord, sizeof(beginRecord),
266dc01487eSGlenn Strauss (const char *)&header, sizeof(header));
267bcdc6a3bSJan Kneschke
268610d5c03SGlenn Strauss /* send FCGI_PARAMS */
269610d5c03SGlenn Strauss
2707c7f8c46SGlenn Strauss if (0 != http_cgi_headers(r, &opts, fcgi_env_add, b)) {
2717c7f8c46SGlenn Strauss r->http_status = 400;
2727c7f8c46SGlenn Strauss r->handler_module = NULL;
273f69bd9cdSGlenn Strauss buffer_clear(b);
27481029b8bSGlenn Strauss chunkqueue_remove_finished_chunks(&hctx->wb);
27545b970e6SGlenn Strauss return HANDLER_FINISHED;
2765677f174SStefan Bühler } else {
277610d5c03SGlenn Strauss fcgi_header(&(header), FCGI_PARAMS, request_id,
278af3df29aSGlenn Strauss buffer_clen(b) - sizeof(FCGI_BeginRequestRecord) - sizeof(FCGI_Header), 0);
279610d5c03SGlenn Strauss memcpy(b->ptr+sizeof(FCGI_BeginRequestRecord), (const char *)&header, sizeof(header));
280bcdc6a3bSJan Kneschke
281bcdc6a3bSJan Kneschke fcgi_header(&(header), FCGI_PARAMS, request_id, 0, 0);
2826afad87dSStefan Bühler buffer_append_string_len(b, (const char *)&header, sizeof(header));
283bcdc6a3bSJan Kneschke
284af3df29aSGlenn Strauss hctx->wb_reqlen = buffer_clen(b);
28581029b8bSGlenn Strauss chunkqueue_prepend_buffer_commit(&hctx->wb);
2861be163b4SStefan Bühler }
2871c09f284SJan Kneschke
2887c7f8c46SGlenn Strauss if (r->reqbody_length) {
28981029b8bSGlenn Strauss /*chunkqueue_append_chunkqueue(&hctx->wb, &r->reqbody_queue);*/
2907c7f8c46SGlenn Strauss if (r->reqbody_length > 0)
2917c7f8c46SGlenn Strauss hctx->wb_reqlen += r->reqbody_length;/* (eventual) (minimal) total request size, not necessarily including all fcgi_headers around content length yet */
292316e959bSGlenn Strauss else /* as-yet-unknown total request size (Transfer-Encoding: chunked)*/
293316e959bSGlenn Strauss hctx->wb_reqlen = -hctx->wb_reqlen;
294316e959bSGlenn Strauss }
29550bdb55dSGlenn Strauss fcgi_stdin_append(hctx);
2961c09f284SJan Kneschke
2972a7d3a27SGlenn Strauss plugin_stats_inc("fastcgi.requests");
29845b970e6SGlenn Strauss return HANDLER_GO_ON;
299bcdc6a3bSJan Kneschke }
300bcdc6a3bSJan Kneschke
30138b8743aSJan Kneschke typedef struct {
302f69f209eSGlenn Strauss unsigned int len;
30338b8743aSJan Kneschke int type;
30438b8743aSJan Kneschke int padding;
305f69f209eSGlenn Strauss int request_id;
30638b8743aSJan Kneschke } fastcgi_response_packet;
30738b8743aSJan Kneschke
fastcgi_get_packet(handler_ctx * hctx,fastcgi_response_packet * packet)308010c2894SGlenn Strauss static int fastcgi_get_packet(handler_ctx *hctx, fastcgi_response_packet *packet) {
3092df8f9ebSGlenn Strauss FCGI_Header header;
3102df8f9ebSGlenn Strauss off_t rblen = chunkqueue_length(hctx->rb);
3112df8f9ebSGlenn Strauss if (rblen < (off_t)sizeof(FCGI_Header)) {
31238b8743aSJan Kneschke /* no header */
3132df8f9ebSGlenn Strauss if (hctx->conf.debug && 0 != rblen) {
3147c7f8c46SGlenn Strauss log_error(hctx->r->conf.errh, __FILE__, __LINE__,
315010c2894SGlenn Strauss "FastCGI: header too small: %lld bytes < %zu bytes, "
316010c2894SGlenn Strauss "waiting for more data", (long long)rblen, sizeof(FCGI_Header));
31778178466SStefan Bühler }
31838b8743aSJan Kneschke return -1;
31938b8743aSJan Kneschke }
320660d719aSGlenn Strauss char *ptr = (char *)&header;
321660d719aSGlenn Strauss uint32_t rd = sizeof(FCGI_Header);
322660d719aSGlenn Strauss if (chunkqueue_peek_data(hctx->rb, &ptr, &rd, hctx->r->conf.errh) < 0)
323660d719aSGlenn Strauss return -1;
324660d719aSGlenn Strauss if (rd != sizeof(FCGI_Header))
325660d719aSGlenn Strauss return -1;
326660d719aSGlenn Strauss if (ptr != (char *)&header) /* copy into aligned struct */
327660d719aSGlenn Strauss memcpy(&header, ptr, sizeof(FCGI_Header));
32838b8743aSJan Kneschke
3292df8f9ebSGlenn Strauss /* we have at least a header, now check how much we have to fetch */
3302df8f9ebSGlenn Strauss packet->len = (header.contentLengthB0 | (header.contentLengthB1 << 8)) + header.paddingLength;
3312df8f9ebSGlenn Strauss packet->request_id = (header.requestIdB0 | (header.requestIdB1 << 8));
3322df8f9ebSGlenn Strauss packet->type = header.type;
3332df8f9ebSGlenn Strauss packet->padding = header.paddingLength;
3342df8f9ebSGlenn Strauss
3352df8f9ebSGlenn Strauss if (packet->len > (unsigned int)rblen-sizeof(FCGI_Header)) {
3362df8f9ebSGlenn Strauss return -1; /* we didn't get the full packet */
33738b8743aSJan Kneschke }
33838b8743aSJan Kneschke
3392df8f9ebSGlenn Strauss chunkqueue_mark_written(hctx->rb, sizeof(FCGI_Header));
34038b8743aSJan Kneschke return 0;
34138b8743aSJan Kneschke }
342bcdc6a3bSJan Kneschke
fastcgi_get_packet_body(buffer * const b,handler_ctx * const hctx,const fastcgi_response_packet * const packet)343660d719aSGlenn Strauss static void fastcgi_get_packet_body(buffer * const b, handler_ctx * const hctx, const fastcgi_response_packet * const packet) {
3442df8f9ebSGlenn Strauss /* copy content; hctx->rb must contain at least packet->len content */
345660d719aSGlenn Strauss /* (read entire packet and then truncate padding, if present) */
346af3df29aSGlenn Strauss const uint32_t blen = buffer_clen(b);
347660d719aSGlenn Strauss if (chunkqueue_read_data(hctx->rb,
348660d719aSGlenn Strauss buffer_string_prepare_append(b, packet->len),
349660d719aSGlenn Strauss packet->len, hctx->r->conf.errh) < 0)
350660d719aSGlenn Strauss return; /*(should not happen; should all be in memory)*/
351af3df29aSGlenn Strauss buffer_truncate(b, blen + packet->len - packet->padding);
3522df8f9ebSGlenn Strauss }
3532df8f9ebSGlenn Strauss
354fe055165SGlenn Strauss __attribute_cold__
fcgi_recv_0(const request_st * const r,const handler_ctx * const hctx)355fe055165SGlenn Strauss static handler_t fcgi_recv_0(const request_st * const r, const handler_ctx * const hctx) {
356210b5770SGlenn Strauss if (-1 == hctx->request_id) return HANDLER_FINISHED; /*(flag request ended)*/
3579113011dSGlenn Strauss if (!(fdevent_fdnode_interest(hctx->fdn) & FDEVENT_IN)
3587c7f8c46SGlenn Strauss && !(r->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_POLLRDHUP))
359210b5770SGlenn Strauss return HANDLER_GO_ON;
3607c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
361010c2894SGlenn Strauss "unexpected end-of-file (perhaps the fastcgi process died):"
362010c2894SGlenn Strauss "pid: %d socket: %s",
363010c2894SGlenn Strauss hctx->proc->pid, hctx->proc->connection_name->ptr);
364bcdc6a3bSJan Kneschke
36577509ed0SGlenn Strauss return HANDLER_ERROR;
366bcdc6a3bSJan Kneschke }
367bcdc6a3bSJan Kneschke
fcgi_recv_parse_loop(request_st * const r,handler_ctx * const hctx)368fe055165SGlenn Strauss static handler_t fcgi_recv_parse_loop(request_st * const r, handler_ctx * const hctx) {
36938b8743aSJan Kneschke /*
37038b8743aSJan Kneschke * parse the fastcgi packets and forward the content to the write-queue
371bcdc6a3bSJan Kneschke *
372bcdc6a3bSJan Kneschke */
37338b8743aSJan Kneschke fastcgi_response_packet packet;
374fe055165SGlenn Strauss int fin = 0;
375fe055165SGlenn Strauss do {
37638b8743aSJan Kneschke /* check if we have at least one packet */
377010c2894SGlenn Strauss if (0 != fastcgi_get_packet(hctx, &packet)) {
37838b8743aSJan Kneschke /* no full packet */
379bcdc6a3bSJan Kneschke break;
380bcdc6a3bSJan Kneschke }
381bcdc6a3bSJan Kneschke
38238b8743aSJan Kneschke switch(packet.type) {
383bcdc6a3bSJan Kneschke case FCGI_STDOUT:
38438b8743aSJan Kneschke if (packet.len == 0) break;
385bcdc6a3bSJan Kneschke
38638b8743aSJan Kneschke /* is the header already finished */
3877c7f8c46SGlenn Strauss if (0 == r->resp_body_started) {
3880a635fc8SGlenn Strauss /* split header from body */
3892df8f9ebSGlenn Strauss buffer *hdrs = hctx->response;
3902df8f9ebSGlenn Strauss if (NULL == hdrs) {
3917c7f8c46SGlenn Strauss hdrs = r->tmp_buf;
392010c2894SGlenn Strauss buffer_clear(hdrs);
3932df8f9ebSGlenn Strauss }
3942df8f9ebSGlenn Strauss fastcgi_get_packet_body(hdrs, hctx, &packet);
3957c7f8c46SGlenn Strauss if (HANDLER_GO_ON != http_response_parse_headers(r, &hctx->opts, hdrs)) {
396bfac0285SGlenn Strauss hctx->send_content_body = 0;
397bfac0285SGlenn Strauss fin = 1;
3988c83976dSStefan Bühler break;
3998c83976dSStefan Bühler }
4007c7f8c46SGlenn Strauss if (0 == r->resp_body_started) {
40145b970e6SGlenn Strauss if (!hctx->response) {
4023d8d56d8SGlenn Strauss hctx->response = chunk_buffer_acquire();
4033d8d56d8SGlenn Strauss buffer_copy_buffer(hctx->response, hdrs);
4040a635fc8SGlenn Strauss }
4050a635fc8SGlenn Strauss }
40645b970e6SGlenn Strauss else if (hctx->gw_mode == GW_AUTHORIZER &&
4077c7f8c46SGlenn Strauss (r->http_status == 0 || r->http_status == 200)) {
4080a635fc8SGlenn Strauss /* authorizer approved request; ignore the content here */
409119c0da3SJan Kneschke hctx->send_content_body = 0;
410fe055165SGlenn Strauss hctx->opts.authorizer |= /*(save response streaming flags)*/
4116e45cff0SGlenn Strauss (r->conf.stream_response_body
4126e45cff0SGlenn Strauss & (FDEVENT_STREAM_RESPONSE
4136e45cff0SGlenn Strauss |FDEVENT_STREAM_RESPONSE_BUFMIN)) << 1;
4146e45cff0SGlenn Strauss r->conf.stream_response_body &=
4156e45cff0SGlenn Strauss ~(FDEVENT_STREAM_RESPONSE|FDEVENT_STREAM_RESPONSE_BUFMIN);
416119c0da3SJan Kneschke }
4178d13233bSGlenn Strauss #if 0
4188d13233bSGlenn Strauss else if ((r->conf.stream_response_body &
4198d13233bSGlenn Strauss (FDEVENT_STREAM_RESPONSE|FDEVENT_STREAM_RESPONSE_BUFMIN))
4208d13233bSGlenn Strauss && ( r->http_status == 204
4218d13233bSGlenn Strauss || r->http_status == 205
4228d13233bSGlenn Strauss || r->http_status == 304
4238d13233bSGlenn Strauss || r->http_method == HTTP_METHOD_HEAD)) {
4248d13233bSGlenn Strauss /* disable streaming to wait for backend protocol to signal
4258d13233bSGlenn Strauss * end of response (prevent http_response_write_prepare()
4268d13233bSGlenn Strauss * from short-circuiting and finishing responses without
4278d13233bSGlenn Strauss * response body) */
4288d13233bSGlenn Strauss r->conf.stream_response_body &=
4298d13233bSGlenn Strauss ~(FDEVENT_STREAM_RESPONSE|FDEVENT_STREAM_RESPONSE_BUFMIN);
4308d13233bSGlenn Strauss }
4318d13233bSGlenn Strauss #endif
4322df8f9ebSGlenn Strauss } else if (hctx->send_content_body) {
4331acf9db7SGlenn Strauss if (0 != http_response_transfer_cqlen(r, hctx->rb, (size_t)(packet.len - packet.padding))) {
4345a91fd4bSGlenn Strauss /* error writing to tempfile;
4355a91fd4bSGlenn Strauss * truncate response or send 500 if nothing sent yet */
4361acf9db7SGlenn Strauss hctx->send_content_body = 0;
4375a91fd4bSGlenn Strauss fin = 1;
43818a7b2beSGlenn Strauss }
43919b5fbddSGlenn Strauss if (packet.padding) chunkqueue_mark_written(hctx->rb, packet.padding);
4402df8f9ebSGlenn Strauss } else {
4412df8f9ebSGlenn Strauss chunkqueue_mark_written(hctx->rb, packet.len);
442bcdc6a3bSJan Kneschke }
443bcdc6a3bSJan Kneschke break;
444bcdc6a3bSJan Kneschke case FCGI_STDERR:
445010c2894SGlenn Strauss if (packet.len) {
4467c7f8c46SGlenn Strauss buffer * const tb = r->tmp_buf;
447010c2894SGlenn Strauss buffer_clear(tb);
448010c2894SGlenn Strauss fastcgi_get_packet_body(tb, hctx, &packet);
4497a21b385SGlenn Strauss log_error_multiline(r->conf.errh, __FILE__, __LINE__,
4507a21b385SGlenn Strauss BUF_PTR_LEN(tb), "FastCGI-stderr:");
451010c2894SGlenn Strauss }
452bcdc6a3bSJan Kneschke break;
453bcdc6a3bSJan Kneschke case FCGI_END_REQUEST:
454210b5770SGlenn Strauss hctx->request_id = -1; /*(flag request ended)*/
455bcdc6a3bSJan Kneschke fin = 1;
456bcdc6a3bSJan Kneschke break;
457bcdc6a3bSJan Kneschke default:
4587c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
459010c2894SGlenn Strauss "FastCGI: header.type not handled: %d", packet.type);
4602df8f9ebSGlenn Strauss chunkqueue_mark_written(hctx->rb, packet.len);
461bcdc6a3bSJan Kneschke break;
462bcdc6a3bSJan Kneschke }
463fe055165SGlenn Strauss } while (0 == fin);
464bcdc6a3bSJan Kneschke
46577509ed0SGlenn Strauss return 0 == fin ? HANDLER_GO_ON : HANDLER_FINISHED;
466bcdc6a3bSJan Kneschke }
467bcdc6a3bSJan Kneschke
fcgi_recv_parse(request_st * const r,struct http_response_opts_t * opts,buffer * b,size_t n)468fe055165SGlenn Strauss static handler_t fcgi_recv_parse(request_st * const r, struct http_response_opts_t *opts, buffer *b, size_t n) {
469fe055165SGlenn Strauss handler_ctx * const hctx = (handler_ctx *)opts->pdata;
470fe055165SGlenn Strauss if (0 == n) return fcgi_recv_0(r, hctx);
471fe055165SGlenn Strauss chunkqueue_append_buffer(hctx->rb, b);
472fe055165SGlenn Strauss return fcgi_recv_parse_loop(r, hctx);
473fe055165SGlenn Strauss }
474fe055165SGlenn Strauss
fcgi_check_extension(request_st * const r,void * p_d,int uri_path_handler)4757c7f8c46SGlenn Strauss static handler_t fcgi_check_extension(request_st * const r, void *p_d, int uri_path_handler) {
476bcdc6a3bSJan Kneschke plugin_data *p = p_d;
47745b970e6SGlenn Strauss handler_t rc;
478bcdc6a3bSJan Kneschke
4797c7f8c46SGlenn Strauss if (NULL != r->handler_module) return HANDLER_GO_ON;
480ad12e4c5SStefan Bühler
4817c7f8c46SGlenn Strauss mod_fastcgi_patch_config(r, p);
4827b7350eeSGlenn Strauss if (NULL == p->conf.exts) return HANDLER_GO_ON;
483bcdc6a3bSJan Kneschke
4847c7f8c46SGlenn Strauss rc = gw_check_extension(r, p, uri_path_handler, 0);
48545b970e6SGlenn Strauss if (HANDLER_GO_ON != rc) return rc;
4867b7350eeSGlenn Strauss
4877c7f8c46SGlenn Strauss if (r->handler_module == p->self) {
4887c7f8c46SGlenn Strauss handler_ctx *hctx = r->plugin_ctx[p->id];
4890a635fc8SGlenn Strauss hctx->opts.backend = BACKEND_FASTCGI;
49077509ed0SGlenn Strauss hctx->opts.parse = fcgi_recv_parse;
491f19f7162SGlenn Strauss hctx->opts.pdata = hctx; /*(skip +255 for potential padding)*/
492f19f7162SGlenn Strauss hctx->opts.max_per_read = sizeof(FCGI_Header)+FCGI_MAX_LENGTH+1;
49345b970e6SGlenn Strauss hctx->stdin_append = fcgi_stdin_append;
49445b970e6SGlenn Strauss hctx->create_env = fcgi_create_env;
495eb429c9cSGlenn Strauss if (!hctx->rb) {
49681029b8bSGlenn Strauss hctx->rb = chunkqueue_init(NULL);
49739b12c5fSJan Kneschke }
498eb429c9cSGlenn Strauss else {
499eb429c9cSGlenn Strauss chunkqueue_reset(hctx->rb);
500eb429c9cSGlenn Strauss }
501eb429c9cSGlenn Strauss }
502bcdc6a3bSJan Kneschke
503bcdc6a3bSJan Kneschke return HANDLER_GO_ON;
504bcdc6a3bSJan Kneschke }
505bcdc6a3bSJan Kneschke
506bcdc6a3bSJan Kneschke /* uri-path handler */
fcgi_check_extension_1(request_st * const r,void * p_d)5077c7f8c46SGlenn Strauss static handler_t fcgi_check_extension_1(request_st * const r, void *p_d) {
5087c7f8c46SGlenn Strauss return fcgi_check_extension(r, p_d, 1);
509bcdc6a3bSJan Kneschke }
510bcdc6a3bSJan Kneschke
511bcdc6a3bSJan Kneschke /* start request handler */
fcgi_check_extension_2(request_st * const r,void * p_d)5127c7f8c46SGlenn Strauss static handler_t fcgi_check_extension_2(request_st * const r, void *p_d) {
5137c7f8c46SGlenn Strauss return fcgi_check_extension(r, p_d, 0);
514bcdc6a3bSJan Kneschke }
515bcdc6a3bSJan Kneschke
516bcdc6a3bSJan Kneschke
517b82d7b8aSGlenn Strauss __attribute_cold__
51863f785a2SStefan Bühler int mod_fastcgi_plugin_init(plugin *p);
mod_fastcgi_plugin_init(plugin * p)519bcdc6a3bSJan Kneschke int mod_fastcgi_plugin_init(plugin *p) {
520bcdc6a3bSJan Kneschke p->version = LIGHTTPD_VERSION_ID;
521e2de4e58SGlenn Strauss p->name = "fastcgi";
522bcdc6a3bSJan Kneschke
52345b970e6SGlenn Strauss p->init = gw_init;
52445b970e6SGlenn Strauss p->cleanup = gw_free;
525bcdc6a3bSJan Kneschke p->set_defaults = mod_fastcgi_set_defaults;
52633c8cf41SGlenn Strauss p->handle_request_reset = gw_handle_request_reset;
527bcdc6a3bSJan Kneschke p->handle_uri_clean = fcgi_check_extension_1;
528bcdc6a3bSJan Kneschke p->handle_subrequest_start = fcgi_check_extension_2;
52945b970e6SGlenn Strauss p->handle_subrequest = gw_handle_subrequest;
53045b970e6SGlenn Strauss p->handle_trigger = gw_handle_trigger;
5319030cfaeSGlenn Strauss p->handle_waitpid = gw_handle_waitpid_cb;
532bcdc6a3bSJan Kneschke
533bcdc6a3bSJan Kneschke return 0;
534bcdc6a3bSJan Kneschke }
535