fix race in dynamic handler configs (reentrancy) (fixes #2774)
[lighttpd.git] / src / mod_cgi.c
blob0b23dc7183543b0eadb13433e14b8edffbf257b0
1 #include "first.h"
3 #include "server.h"
4 #include "stat_cache.h"
5 #include "keyvalue.h"
6 #include "log.h"
7 #include "connections.h"
8 #include "joblist.h"
9 #include "response.h"
10 #include "http_chunk.h"
11 #include "network_backends.h"
13 #include "plugin.h"
15 #include <sys/types.h>
16 #include "sys-mmap.h"
18 #ifdef __WIN32
19 # include <winsock2.h>
20 #else
21 # include <sys/socket.h>
22 # include <sys/wait.h>
23 # include <netinet/in.h>
24 # include <arpa/inet.h>
25 #endif
27 #include <unistd.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <fdevent.h>
32 #include <signal.h>
33 #include <ctype.h>
34 #include <assert.h>
36 #include <stdio.h>
37 #include <fcntl.h>
39 #if defined(O_CLOEXEC) && (!defined(__FreeBSD__) || defined(SOCK_CLOEXEC)) \
40 && !(defined(__APPLE__) && defined(__MACH__))
41 #define pipe_cloexec(pipefd) pipe2((pipefd), O_CLOEXEC)
42 #elif defined FD_CLOEXEC
43 #define pipe_cloexec(pipefd) \
44 ( 0 == pipe(pipefd) \
45 && 0 == fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) \
46 && 0 == fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) \
47 ? 0 \
48 : -1)
49 #else
50 #define pipe_cloexec(pipefd) pipe(pipefd)
51 #endif
53 enum {EOL_UNSET, EOL_N, EOL_RN};
55 typedef struct {
56 char **ptr;
58 size_t size;
59 size_t used;
60 } char_array;
62 typedef struct {
63 pid_t *ptr;
64 size_t used;
65 size_t size;
66 } buffer_pid_t;
68 typedef struct {
69 array *cgi;
70 unsigned short execute_x_only;
71 unsigned short xsendfile_allow;
72 array *xsendfile_docroot;
73 } plugin_config;
75 typedef struct {
76 PLUGIN_DATA;
77 buffer_pid_t cgi_pid;
79 buffer *parse_response;
81 plugin_config **config_storage;
83 plugin_config conf;
84 } plugin_data;
86 typedef struct {
87 pid_t pid;
88 int fd;
89 int fdtocgi;
90 int fde_ndx; /* index into the fd-event buffer */
91 int fde_ndx_tocgi; /* index into the fd-event buffer */
93 connection *remote_conn; /* dumb pointer */
94 plugin_data *plugin_data; /* dumb pointer */
96 buffer *response;
97 buffer *response_header;
98 plugin_config conf;
99 } handler_ctx;
101 static handler_ctx * cgi_handler_ctx_init(void) {
102 handler_ctx *hctx = calloc(1, sizeof(*hctx));
104 force_assert(hctx);
106 hctx->response = buffer_init();
107 hctx->response_header = buffer_init();
108 hctx->fd = -1;
109 hctx->fdtocgi = -1;
111 return hctx;
114 static void cgi_handler_ctx_free(handler_ctx *hctx) {
115 buffer_free(hctx->response);
116 buffer_free(hctx->response_header);
118 free(hctx);
121 enum {FDEVENT_HANDLED_UNSET, FDEVENT_HANDLED_FINISHED, FDEVENT_HANDLED_NOT_FINISHED, FDEVENT_HANDLED_COMEBACK, FDEVENT_HANDLED_ERROR};
123 INIT_FUNC(mod_cgi_init) {
124 plugin_data *p;
126 p = calloc(1, sizeof(*p));
128 force_assert(p);
130 p->parse_response = buffer_init();
132 return p;
136 FREE_FUNC(mod_cgi_free) {
137 plugin_data *p = p_d;
138 buffer_pid_t *r = &(p->cgi_pid);
140 UNUSED(srv);
142 if (p->config_storage) {
143 size_t i;
144 for (i = 0; i < srv->config_context->used; i++) {
145 plugin_config *s = p->config_storage[i];
147 if (NULL == s) continue;
149 array_free(s->cgi);
150 array_free(s->xsendfile_docroot);
152 free(s);
154 free(p->config_storage);
158 if (r->ptr) free(r->ptr);
160 buffer_free(p->parse_response);
162 free(p);
164 return HANDLER_GO_ON;
167 SETDEFAULTS_FUNC(mod_fastcgi_set_defaults) {
168 plugin_data *p = p_d;
169 size_t i = 0;
171 config_values_t cv[] = {
172 { "cgi.assign", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
173 { "cgi.execute-x-only", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
174 { "cgi.x-sendfile", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
175 { "cgi.x-sendfile-docroot", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
176 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET}
179 if (!p) return HANDLER_ERROR;
181 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
182 force_assert(p->config_storage);
184 for (i = 0; i < srv->config_context->used; i++) {
185 data_config const* config = (data_config const*)srv->config_context->data[i];
186 plugin_config *s;
188 s = calloc(1, sizeof(plugin_config));
189 force_assert(s);
191 s->cgi = array_init();
192 s->execute_x_only = 0;
193 s->xsendfile_allow= 0;
194 s->xsendfile_docroot = array_init();
196 cv[0].destination = s->cgi;
197 cv[1].destination = &(s->execute_x_only);
198 cv[2].destination = &(s->xsendfile_allow);
199 cv[3].destination = s->xsendfile_docroot;
201 p->config_storage[i] = s;
203 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
204 return HANDLER_ERROR;
207 if (s->xsendfile_docroot->used) {
208 size_t j;
209 for (j = 0; j < s->xsendfile_docroot->used; ++j) {
210 data_string *ds = (data_string *)s->xsendfile_docroot->data[j];
211 if (ds->type != TYPE_STRING) {
212 log_error_write(srv, __FILE__, __LINE__, "s",
213 "unexpected type for key cgi.x-sendfile-docroot; expected: cgi.x-sendfile-docroot = ( \"/allowed/path\", ... )");
214 return HANDLER_ERROR;
216 if (ds->value->ptr[0] != '/') {
217 log_error_write(srv, __FILE__, __LINE__, "SBs",
218 "cgi.x-sendfile-docroot paths must begin with '/'; invalid: \"", ds->value, "\"");
219 return HANDLER_ERROR;
221 buffer_path_simplify(ds->value, ds->value);
222 buffer_append_slash(ds->value);
227 return HANDLER_GO_ON;
231 static int cgi_pid_add(server *srv, plugin_data *p, pid_t pid) {
232 int m = -1;
233 size_t i;
234 buffer_pid_t *r = &(p->cgi_pid);
236 UNUSED(srv);
238 for (i = 0; i < r->used; i++) {
239 if (r->ptr[i] > m) m = r->ptr[i];
242 if (r->size == 0) {
243 r->size = 16;
244 r->ptr = malloc(sizeof(*r->ptr) * r->size);
245 force_assert(r->ptr);
246 } else if (r->used == r->size) {
247 r->size += 16;
248 r->ptr = realloc(r->ptr, sizeof(*r->ptr) * r->size);
249 force_assert(r->ptr);
252 r->ptr[r->used++] = pid;
254 return m;
257 static int cgi_pid_del(server *srv, plugin_data *p, pid_t pid) {
258 size_t i;
259 buffer_pid_t *r = &(p->cgi_pid);
261 UNUSED(srv);
263 for (i = 0; i < r->used; i++) {
264 if (r->ptr[i] == pid) break;
267 if (i != r->used) {
268 /* found */
270 if (i != r->used - 1) {
271 r->ptr[i] = r->ptr[r->used - 1];
273 r->used--;
276 return 0;
279 static int cgi_response_parse(server *srv, connection *con, plugin_data *p, buffer *in) {
280 char *ns;
281 const char *s;
282 int line = 0;
284 UNUSED(srv);
286 buffer_copy_buffer(p->parse_response, in);
288 for (s = p->parse_response->ptr;
289 NULL != (ns = strchr(s, '\n'));
290 s = ns + 1, line++) {
291 const char *key, *value;
292 int key_len;
293 data_string *ds;
295 /* strip the \n */
296 ns[0] = '\0';
298 if (ns > s && ns[-1] == '\r') ns[-1] = '\0';
300 if (line == 0 &&
301 0 == strncmp(s, "HTTP/1.", 7)) {
302 /* non-parsed header ... we parse them anyway */
304 if ((s[7] == '1' ||
305 s[7] == '0') &&
306 s[8] == ' ') {
307 int status;
308 /* after the space should be a status code for us */
310 status = strtol(s+9, NULL, 10);
312 if (status >= 100 &&
313 status < 1000) {
314 /* we expected 3 digits and didn't got them */
315 con->parsed_response |= HTTP_STATUS;
316 con->http_status = status;
319 } else {
320 /* parse the headers */
321 key = s;
322 if (NULL == (value = strchr(s, ':'))) {
323 /* we expect: "<key>: <value>\r\n" */
324 continue;
327 key_len = value - key;
328 value += 1;
330 /* skip LWS */
331 while (*value == ' ' || *value == '\t') value++;
333 if (NULL == (ds = (data_string *)array_get_unused_element(con->response.headers, TYPE_STRING))) {
334 ds = data_response_init();
336 buffer_copy_string_len(ds->key, key, key_len);
337 buffer_copy_string(ds->value, value);
339 array_insert_unique(con->response.headers, (data_unset *)ds);
341 switch(key_len) {
342 case 4:
343 if (0 == strncasecmp(key, "Date", key_len)) {
344 con->parsed_response |= HTTP_DATE;
346 break;
347 case 6:
348 if (0 == strncasecmp(key, "Status", key_len)) {
349 int status = strtol(value, NULL, 10);
350 if (status >= 100 && status < 1000) {
351 con->http_status = status;
352 con->parsed_response |= HTTP_STATUS;
353 } else {
354 con->http_status = 502;
357 break;
358 case 8:
359 if (0 == strncasecmp(key, "Location", key_len)) {
360 con->parsed_response |= HTTP_LOCATION;
362 break;
363 case 10:
364 if (0 == strncasecmp(key, "Connection", key_len)) {
365 con->response.keep_alive = (0 == strcasecmp(value, "Keep-Alive")) ? 1 : 0;
366 con->parsed_response |= HTTP_CONNECTION;
368 break;
369 case 14:
370 if (0 == strncasecmp(key, "Content-Length", key_len)) {
371 con->response.content_length = strtoul(value, NULL, 10);
372 con->parsed_response |= HTTP_CONTENT_LENGTH;
374 break;
375 default:
376 break;
381 /* CGI/1.1 rev 03 - 7.2.1.2 */
382 if ((con->parsed_response & HTTP_LOCATION) &&
383 !(con->parsed_response & HTTP_STATUS)) {
384 con->http_status = 302;
387 return 0;
391 static int cgi_demux_response(server *srv, handler_ctx *hctx) {
392 plugin_data *p = hctx->plugin_data;
393 connection *con = hctx->remote_conn;
395 while(1) {
396 int n;
397 int toread;
399 #if defined(__WIN32)
400 buffer_string_prepare_copy(hctx->response, 4 * 1024);
401 #else
402 if (ioctl(con->fd, FIONREAD, &toread) || toread <= 4*1024) {
403 buffer_string_prepare_copy(hctx->response, 4 * 1024);
404 } else {
405 if (toread > MAX_READ_LIMIT) toread = MAX_READ_LIMIT;
406 buffer_string_prepare_copy(hctx->response, toread);
408 #endif
410 if (-1 == (n = read(hctx->fd, hctx->response->ptr, hctx->response->size - 1))) {
411 if (errno == EAGAIN || errno == EINTR) {
412 /* would block, wait for signal */
413 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
414 return FDEVENT_HANDLED_NOT_FINISHED;
416 /* error */
417 log_error_write(srv, __FILE__, __LINE__, "sdd", strerror(errno), con->fd, hctx->fd);
418 return FDEVENT_HANDLED_ERROR;
421 if (n == 0) {
422 /* read finished */
423 return FDEVENT_HANDLED_FINISHED;
426 buffer_commit(hctx->response, n);
428 /* split header from body */
430 if (con->file_started == 0) {
431 int is_header = 0;
432 int is_header_end = 0;
433 size_t last_eol = 0;
434 size_t i, header_len;
436 buffer_append_string_buffer(hctx->response_header, hctx->response);
439 * we have to handle a few cases:
441 * nph:
443 * HTTP/1.0 200 Ok\n
444 * Header: Value\n
445 * \n
447 * CGI:
448 * Header: Value\n
449 * Status: 200\n
450 * \n
452 * and different mixes of \n and \r\n combinations
454 * Some users also forget about CGI and just send a response and hope
455 * we handle it. No headers, no header-content seperator
459 /* nph (non-parsed headers) */
460 if (0 == strncmp(hctx->response_header->ptr, "HTTP/1.", 7)) is_header = 1;
462 header_len = buffer_string_length(hctx->response_header);
463 for (i = 0; !is_header_end && i < header_len; i++) {
464 char c = hctx->response_header->ptr[i];
466 switch (c) {
467 case ':':
468 /* we found a colon
470 * looks like we have a normal header
472 is_header = 1;
473 break;
474 case '\n':
475 /* EOL */
476 if (is_header == 0) {
477 /* we got a EOL but we don't seem to got a HTTP header */
479 is_header_end = 1;
481 break;
485 * check if we saw a \n(\r)?\n sequence
487 if (last_eol > 0 &&
488 ((i - last_eol == 1) ||
489 (i - last_eol == 2 && hctx->response_header->ptr[i - 1] == '\r'))) {
490 is_header_end = 1;
491 break;
494 last_eol = i;
496 break;
500 if (is_header_end) {
501 if (!is_header) {
502 /* no header, but a body */
503 if (0 != http_chunk_append_buffer(srv, con, hctx->response_header)) {
504 return FDEVENT_HANDLED_ERROR;
506 } else {
507 const char *bstart;
508 size_t blen;
510 /* the body starts after the EOL */
511 bstart = hctx->response_header->ptr + i;
512 blen = header_len - i;
515 * i still points to the char after the terminating EOL EOL
517 * put it on the last \n again
519 i--;
521 /* string the last \r?\n */
522 if (i > 0 && (hctx->response_header->ptr[i - 1] == '\r')) {
523 i--;
526 buffer_string_set_length(hctx->response_header, i);
528 /* parse the response header */
529 cgi_response_parse(srv, con, p, hctx->response_header);
531 if (con->http_status >= 300 && con->http_status < 400) {
532 /*(con->parsed_response & HTTP_LOCATION)*/
533 data_string *ds;
534 if (NULL != (ds = (data_string *) array_get_element(con->response.headers, "Location"))
535 && ds->value->ptr[0] == '/') {
536 if (++con->loops_per_request > 5) {
537 log_error_write(srv, __FILE__, __LINE__, "sb", "too many internal loops while processing request:", con->request.orig_uri);
538 con->http_status = 500; /* Internal Server Error */
539 con->mode = DIRECT;
540 return FDEVENT_HANDLED_FINISHED;
543 buffer_copy_buffer(con->request.uri, ds->value);
545 if (con->request.content_length) {
546 if ((off_t)con->request.content_length != chunkqueue_length(con->request_content_queue)) {
547 con->keep_alive = 0;
549 con->request.content_length = 0;
550 chunkqueue_reset(con->request_content_queue);
553 if (con->http_status != 307 && con->http_status != 308) {
554 /* Note: request body (if any) sent to initial dynamic handler
555 * and is not available to the internal redirect */
556 con->request.http_method = HTTP_METHOD_GET;
559 connection_response_reset(srv, con); /*(includes con->http_status = 0)*/
561 con->mode = DIRECT;
562 return FDEVENT_HANDLED_COMEBACK;
566 if (hctx->conf.xsendfile_allow) {
567 data_string *ds;
568 if (NULL != (ds = (data_string *) array_get_element(con->response.headers, "X-Sendfile"))) {
569 http_response_xsendfile(srv, con, ds->value, hctx->conf.xsendfile_docroot);
570 return FDEVENT_HANDLED_FINISHED;
574 if (blen > 0) {
575 if (0 != http_chunk_append_mem(srv, con, bstart, blen)) {
576 return FDEVENT_HANDLED_ERROR;
581 con->file_started = 1;
582 } else {
583 /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
584 if (header_len > MAX_HTTP_REQUEST_HEADER) {
585 log_error_write(srv, __FILE__, __LINE__, "sb", "response headers too large for", con->uri.path);
586 con->http_status = 502; /* Bad Gateway */
587 con->mode = DIRECT;
588 return FDEVENT_HANDLED_FINISHED;
591 } else {
592 if (0 != http_chunk_append_buffer(srv, con, hctx->response)) {
593 return FDEVENT_HANDLED_ERROR;
595 if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
596 && chunkqueue_length(con->write_queue) > 65536 - 4096) {
597 if (!con->is_writable) {
598 /*(defer removal of FDEVENT_IN interest since
599 * connection_state_machine() might be able to send data
600 * immediately, unless !con->is_writable, where
601 * connection_state_machine() might not loop back to call
602 * mod_cgi_handle_subrequest())*/
603 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
605 break;
609 #if 0
610 log_error_write(srv, __FILE__, __LINE__, "ddss", con->fd, hctx->fd, connection_get_state(con->state), b->ptr);
611 #endif
614 return FDEVENT_HANDLED_NOT_FINISHED;
617 static void cgi_connection_close_fdtocgi(server *srv, handler_ctx *hctx) {
618 /*(closes only hctx->fdtocgi)*/
619 fdevent_event_del(srv->ev, &(hctx->fde_ndx_tocgi), hctx->fdtocgi);
620 fdevent_unregister(srv->ev, hctx->fdtocgi);
621 fdevent_sched_close(srv->ev, hctx->fdtocgi, 0);
622 hctx->fdtocgi = -1;
625 static void cgi_connection_close(server *srv, handler_ctx *hctx) {
626 int status;
627 pid_t pid;
628 plugin_data *p = hctx->plugin_data;
629 connection *con = hctx->remote_conn;
631 #ifndef __WIN32
633 /* the connection to the browser went away, but we still have a connection
634 * to the CGI script
636 * close cgi-connection
639 if (hctx->fd != -1) {
640 /* close connection to the cgi-script */
641 fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
642 fdevent_unregister(srv->ev, hctx->fd);
643 fdevent_sched_close(srv->ev, hctx->fd, 0);
646 if (hctx->fdtocgi != -1) {
647 cgi_connection_close_fdtocgi(srv, hctx); /*(closes only hctx->fdtocgi)*/
650 pid = hctx->pid;
652 con->plugin_ctx[p->id] = NULL;
654 cgi_handler_ctx_free(hctx);
656 /* if waitpid hasn't been called by response.c yet, do it here */
657 if (pid) {
658 /* check if the CGI-script is already gone */
659 switch(waitpid(pid, &status, WNOHANG)) {
660 case 0:
661 /* not finished yet */
662 #if 0
663 log_error_write(srv, __FILE__, __LINE__, "sd", "(debug) child isn't done yet, pid:", pid);
664 #endif
665 break;
666 case -1:
667 /* */
668 if (errno == EINTR) break;
671 * errno == ECHILD happens if _subrequest catches the process-status before
672 * we have read the response of the cgi process
674 * -> catch status
675 * -> WAIT_FOR_EVENT
676 * -> read response
677 * -> we get here with waitpid == ECHILD
680 if (errno != ECHILD) {
681 log_error_write(srv, __FILE__, __LINE__, "ss", "waitpid failed: ", strerror(errno));
683 /* anyway: don't wait for it anymore */
684 pid = 0;
685 break;
686 default:
687 if (WIFEXITED(status)) {
688 #if 0
689 log_error_write(srv, __FILE__, __LINE__, "sd", "(debug) cgi exited fine, pid:", pid);
690 #endif
691 } else {
692 log_error_write(srv, __FILE__, __LINE__, "sd", "cgi died, pid:", pid);
695 pid = 0;
696 break;
699 if (pid) {
700 kill(pid, SIGTERM);
702 /* cgi-script is still alive, queue the PID for removal */
703 cgi_pid_add(srv, p, pid);
706 #endif
708 /* finish response (if not already con->file_started, con->file_finished) */
709 if (con->mode == p->id) {
710 http_response_backend_done(srv, con);
714 static handler_t cgi_connection_close_callback(server *srv, connection *con, void *p_d) {
715 plugin_data *p = p_d;
716 handler_ctx *hctx = con->plugin_ctx[p->id];
717 if (hctx) cgi_connection_close(srv, hctx);
719 return HANDLER_GO_ON;
723 static int cgi_write_request(server *srv, handler_ctx *hctx, int fd);
726 static handler_t cgi_handle_fdevent_send (server *srv, void *ctx, int revents) {
727 handler_ctx *hctx = ctx;
728 connection *con = hctx->remote_conn;
730 /*(joblist only actually necessary here in mod_cgi fdevent send if returning HANDLER_ERROR)*/
731 joblist_append(srv, con);
733 if (revents & FDEVENT_OUT) {
734 if (0 != cgi_write_request(srv, hctx, hctx->fdtocgi)) {
735 cgi_connection_close(srv, hctx);
736 return HANDLER_ERROR;
738 /* more request body to be sent to CGI */
741 if (revents & FDEVENT_HUP) {
742 /* skip sending remaining data to CGI */
743 if (con->request.content_length) {
744 chunkqueue *cq = con->request_content_queue;
745 chunkqueue_mark_written(cq, chunkqueue_length(cq));
746 if (cq->bytes_in != (off_t)con->request.content_length) {
747 con->keep_alive = 0;
751 cgi_connection_close_fdtocgi(srv, hctx); /*(closes only hctx->fdtocgi)*/
752 } else if (revents & FDEVENT_ERR) {
753 /* kill all connections to the cgi process */
754 #if 1
755 log_error_write(srv, __FILE__, __LINE__, "s", "cgi-FDEVENT_ERR");
756 #endif
757 cgi_connection_close(srv, hctx);
758 return HANDLER_ERROR;
761 return HANDLER_FINISHED;
765 static int cgi_recv_response(server *srv, handler_ctx *hctx) {
766 switch (cgi_demux_response(srv, hctx)) {
767 case FDEVENT_HANDLED_NOT_FINISHED:
768 break;
769 case FDEVENT_HANDLED_FINISHED:
770 /* we are done */
772 #if 0
773 log_error_write(srv, __FILE__, __LINE__, "ddss", con->fd, hctx->fd, connection_get_state(con->state), "finished");
774 #endif
775 cgi_connection_close(srv, hctx);
777 /* if we get a IN|HUP and have read everything don't exec the close twice */
778 return HANDLER_FINISHED;
779 case FDEVENT_HANDLED_COMEBACK:
780 cgi_connection_close(srv, hctx);
781 return HANDLER_COMEBACK;
782 case FDEVENT_HANDLED_ERROR:
783 log_error_write(srv, __FILE__, __LINE__, "s", "demuxer failed: ");
785 cgi_connection_close(srv, hctx);
786 return HANDLER_FINISHED;
789 return HANDLER_GO_ON;
793 static handler_t cgi_handle_fdevent(server *srv, void *ctx, int revents) {
794 handler_ctx *hctx = ctx;
795 connection *con = hctx->remote_conn;
797 joblist_append(srv, con);
799 if (revents & FDEVENT_IN) {
800 handler_t rc = cgi_recv_response(srv, hctx);/*(might invalidate hctx)*/
801 if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
804 /* perhaps this issue is already handled */
805 if (revents & FDEVENT_HUP) {
806 if (con->file_started) {
807 /* drain any remaining data from kernel pipe buffers
808 * even if (con->conf.stream_response_body
809 * & FDEVENT_STREAM_RESPONSE_BUFMIN)
810 * since event loop will spin on fd FDEVENT_HUP event
811 * until unregistered. */
812 handler_t rc;
813 do {
814 rc = cgi_recv_response(srv,hctx);/*(might invalidate hctx)*/
815 } while (rc == HANDLER_GO_ON); /*(unless HANDLER_GO_ON)*/
816 return rc; /* HANDLER_FINISHED or HANDLER_COMEBACK or HANDLER_ERROR */
817 } else if (!buffer_string_is_empty(hctx->response_header)) {
818 /* unfinished header package which is a body in reality */
819 con->file_started = 1;
820 if (0 != http_chunk_append_buffer(srv, con, hctx->response_header)) {
821 cgi_connection_close(srv, hctx);
822 return HANDLER_ERROR;
824 } else {
825 # if 0
826 log_error_write(srv, __FILE__, __LINE__, "sddd", "got HUP from cgi", con->fd, hctx->fd, revents);
827 # endif
829 cgi_connection_close(srv, hctx);
830 } else if (revents & FDEVENT_ERR) {
831 /* kill all connections to the cgi process */
832 cgi_connection_close(srv, hctx);
833 #if 1
834 log_error_write(srv, __FILE__, __LINE__, "s", "cgi-FDEVENT_ERR");
835 #endif
836 return HANDLER_ERROR;
839 return HANDLER_FINISHED;
843 static int cgi_env_add(void *venv, const char *key, size_t key_len, const char *val, size_t val_len) {
844 char_array *env = venv;
845 char *dst;
847 if (!key || !val) return -1;
849 dst = malloc(key_len + val_len + 2);
850 force_assert(dst);
851 memcpy(dst, key, key_len);
852 dst[key_len] = '=';
853 memcpy(dst + key_len + 1, val, val_len);
854 dst[key_len + 1 + val_len] = '\0';
856 if (env->size == 0) {
857 env->size = 16;
858 env->ptr = malloc(env->size * sizeof(*env->ptr));
859 force_assert(env->ptr);
860 } else if (env->size == env->used) {
861 env->size += 16;
862 env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
863 force_assert(env->ptr);
866 env->ptr[env->used++] = dst;
868 return 0;
871 /* returns: 0: continue, -1: fatal error, -2: connection reset */
872 /* similar to network_write_file_chunk_mmap, but doesn't use send on windows (because we're on pipes),
873 * also mmaps and sends complete chunk instead of only small parts - the files
874 * are supposed to be temp files with reasonable chunk sizes.
876 * Also always use mmap; the files are "trusted", as we created them.
878 static ssize_t cgi_write_file_chunk_mmap(server *srv, connection *con, int fd, chunkqueue *cq) {
879 chunk* const c = cq->first;
880 off_t offset, toSend, file_end;
881 ssize_t r;
882 size_t mmap_offset, mmap_avail;
883 char *data;
885 force_assert(NULL != c);
886 force_assert(FILE_CHUNK == c->type);
887 force_assert(c->offset >= 0 && c->offset <= c->file.length);
889 offset = c->file.start + c->offset;
890 toSend = c->file.length - c->offset;
891 file_end = c->file.start + c->file.length; /* offset to file end in this chunk */
893 if (0 == toSend) {
894 chunkqueue_remove_finished_chunks(cq);
895 return 0;
898 if (0 != network_open_file_chunk(srv, con, cq)) return -1;
900 /* (re)mmap the buffer if range is not covered completely */
901 if (MAP_FAILED == c->file.mmap.start
902 || offset < c->file.mmap.offset
903 || file_end > (off_t)(c->file.mmap.offset + c->file.mmap.length)) {
905 if (MAP_FAILED != c->file.mmap.start) {
906 munmap(c->file.mmap.start, c->file.mmap.length);
907 c->file.mmap.start = MAP_FAILED;
910 c->file.mmap.offset = mmap_align_offset(offset);
911 c->file.mmap.length = file_end - c->file.mmap.offset;
913 if (MAP_FAILED == (c->file.mmap.start = mmap(NULL, c->file.mmap.length, PROT_READ, MAP_PRIVATE, c->file.fd, c->file.mmap.offset))) {
914 if (toSend > 65536) toSend = 65536;
915 data = malloc(toSend);
916 force_assert(data);
917 if (-1 == lseek(c->file.fd, offset, SEEK_SET)
918 || 0 >= (toSend = read(c->file.fd, data, toSend))) {
919 if (-1 == toSend) {
920 log_error_write(srv, __FILE__, __LINE__, "ssbdo", "lseek/read failed:",
921 strerror(errno), c->file.name, c->file.fd, offset);
922 } else { /*(0 == toSend)*/
923 log_error_write(srv, __FILE__, __LINE__, "sbdo", "unexpected EOF (input truncated?):",
924 c->file.name, c->file.fd, offset);
926 free(data);
927 return -1;
932 if (MAP_FAILED != c->file.mmap.start) {
933 force_assert(offset >= c->file.mmap.offset);
934 mmap_offset = offset - c->file.mmap.offset;
935 force_assert(c->file.mmap.length > mmap_offset);
936 mmap_avail = c->file.mmap.length - mmap_offset;
937 force_assert(toSend <= (off_t) mmap_avail);
939 data = c->file.mmap.start + mmap_offset;
942 r = write(fd, data, toSend);
944 if (MAP_FAILED == c->file.mmap.start) free(data);
946 if (r < 0) {
947 switch (errno) {
948 case EAGAIN:
949 case EINTR:
950 return 0;
951 case EPIPE:
952 case ECONNRESET:
953 return -2;
954 default:
955 log_error_write(srv, __FILE__, __LINE__, "ssd",
956 "write failed:", strerror(errno), fd);
957 return -1;
961 if (r >= 0) {
962 chunkqueue_mark_written(cq, r);
965 return r;
968 static int cgi_write_request(server *srv, handler_ctx *hctx, int fd) {
969 connection *con = hctx->remote_conn;
970 chunkqueue *cq = con->request_content_queue;
971 chunk *c;
973 /* old comment: windows doesn't support select() on pipes - wouldn't be easy to fix for all platforms.
974 * solution: if this is still a problem on windows, then substitute
975 * socketpair() for pipe() and closesocket() for close() on windows.
978 for (c = cq->first; c; c = cq->first) {
979 ssize_t r = -1;
981 switch(c->type) {
982 case FILE_CHUNK:
983 r = cgi_write_file_chunk_mmap(srv, con, fd, cq);
984 break;
986 case MEM_CHUNK:
987 if ((r = write(fd, c->mem->ptr + c->offset, buffer_string_length(c->mem) - c->offset)) < 0) {
988 switch(errno) {
989 case EAGAIN:
990 case EINTR:
991 /* ignore and try again */
992 r = 0;
993 break;
994 case EPIPE:
995 case ECONNRESET:
996 /* connection closed */
997 r = -2;
998 break;
999 default:
1000 /* fatal error */
1001 log_error_write(srv, __FILE__, __LINE__, "ss", "write failed due to: ", strerror(errno));
1002 r = -1;
1003 break;
1005 } else if (r > 0) {
1006 chunkqueue_mark_written(cq, r);
1008 break;
1011 if (0 == r) break; /*(might block)*/
1013 switch (r) {
1014 case -1:
1015 /* fatal error */
1016 return -1;
1017 case -2:
1018 /* connection reset */
1019 log_error_write(srv, __FILE__, __LINE__, "s", "failed to send post data to cgi, connection closed by CGI");
1020 /* skip all remaining data */
1021 chunkqueue_mark_written(cq, chunkqueue_length(cq));
1022 break;
1023 default:
1024 break;
1028 if (cq->bytes_out == (off_t)con->request.content_length) {
1029 /* sent all request body input */
1030 /* close connection to the cgi-script */
1031 if (-1 == hctx->fdtocgi) { /*(received request body sent in initial send to pipe buffer)*/
1032 --srv->cur_fds;
1033 if (close(fd)) {
1034 log_error_write(srv, __FILE__, __LINE__, "sds", "cgi stdin close failed ", fd, strerror(errno));
1036 } else {
1037 cgi_connection_close_fdtocgi(srv, hctx); /*(closes only hctx->fdtocgi)*/
1039 } else {
1040 off_t cqlen = cq->bytes_in - cq->bytes_out;
1041 if (cq->bytes_in < (off_t)con->request.content_length && cqlen < 65536 - 16384) {
1042 /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
1043 if (!(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLIN)) {
1044 con->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
1045 con->is_readable = 1; /* trigger optimistic read from client */
1048 if (-1 == hctx->fdtocgi) { /*(not registered yet)*/
1049 hctx->fdtocgi = fd;
1050 hctx->fde_ndx_tocgi = -1;
1051 fdevent_register(srv->ev, hctx->fdtocgi, cgi_handle_fdevent_send, hctx);
1053 if (0 == cqlen) { /*(chunkqueue_is_empty(cq))*/
1054 if ((fdevent_event_get_interest(srv->ev, hctx->fdtocgi) & FDEVENT_OUT)) {
1055 fdevent_event_set(srv->ev, &(hctx->fde_ndx_tocgi), hctx->fdtocgi, 0);
1057 } else {
1058 /* more request body remains to be sent to CGI so register for fdevents */
1059 fdevent_event_set(srv->ev, &(hctx->fde_ndx_tocgi), hctx->fdtocgi, FDEVENT_OUT);
1063 return 0;
1066 static int cgi_create_env(server *srv, connection *con, plugin_data *p, handler_ctx *hctx, buffer *cgi_handler) {
1067 pid_t pid;
1069 int to_cgi_fds[2];
1070 int from_cgi_fds[2];
1071 struct stat st;
1072 UNUSED(p);
1074 #ifndef __WIN32
1076 if (!buffer_string_is_empty(cgi_handler)) {
1077 /* stat the exec file */
1078 if (-1 == (stat(cgi_handler->ptr, &st))) {
1079 log_error_write(srv, __FILE__, __LINE__, "sbss",
1080 "stat for cgi-handler", cgi_handler,
1081 "failed:", strerror(errno));
1082 return -1;
1086 if (pipe_cloexec(to_cgi_fds)) {
1087 log_error_write(srv, __FILE__, __LINE__, "ss", "pipe failed:", strerror(errno));
1088 return -1;
1091 if (pipe_cloexec(from_cgi_fds)) {
1092 close(to_cgi_fds[0]);
1093 close(to_cgi_fds[1]);
1094 log_error_write(srv, __FILE__, __LINE__, "ss", "pipe failed:", strerror(errno));
1095 return -1;
1098 /* fork, execve */
1099 switch (pid = fork()) {
1100 case 0: {
1101 /* child */
1102 char **args;
1103 int argc;
1104 int i = 0;
1105 char_array env;
1106 char *c;
1107 const char *s;
1108 http_cgi_opts opts = { 0, 0, NULL, NULL };
1110 /* move stdout to from_cgi_fd[1] */
1111 dup2(from_cgi_fds[1], STDOUT_FILENO);
1112 #ifndef FD_CLOEXEC
1113 close(from_cgi_fds[1]);
1114 /* not needed */
1115 close(from_cgi_fds[0]);
1116 #endif
1118 /* move the stdin to to_cgi_fd[0] */
1119 dup2(to_cgi_fds[0], STDIN_FILENO);
1120 #ifndef FD_CLOEXEC
1121 close(to_cgi_fds[0]);
1122 /* not needed */
1123 close(to_cgi_fds[1]);
1124 #endif
1126 /* create environment */
1127 env.ptr = NULL;
1128 env.size = 0;
1129 env.used = 0;
1131 http_cgi_headers(srv, con, &opts, cgi_env_add, &env);
1133 /* for valgrind */
1134 if (NULL != (s = getenv("LD_PRELOAD"))) {
1135 cgi_env_add(&env, CONST_STR_LEN("LD_PRELOAD"), s, strlen(s));
1138 if (NULL != (s = getenv("LD_LIBRARY_PATH"))) {
1139 cgi_env_add(&env, CONST_STR_LEN("LD_LIBRARY_PATH"), s, strlen(s));
1141 #ifdef __CYGWIN__
1142 /* CYGWIN needs SYSTEMROOT */
1143 if (NULL != (s = getenv("SYSTEMROOT"))) {
1144 cgi_env_add(&env, CONST_STR_LEN("SYSTEMROOT"), s, strlen(s));
1146 #endif
1148 if (env.size == env.used) {
1149 env.size += 16;
1150 env.ptr = realloc(env.ptr, env.size * sizeof(*env.ptr));
1153 env.ptr[env.used] = NULL;
1155 /* set up args */
1156 argc = 3;
1157 args = malloc(sizeof(*args) * argc);
1158 force_assert(args);
1159 i = 0;
1161 if (!buffer_string_is_empty(cgi_handler)) {
1162 args[i++] = cgi_handler->ptr;
1164 args[i++] = con->physical.path->ptr;
1165 args[i ] = NULL;
1167 /* search for the last / */
1168 if (NULL != (c = strrchr(con->physical.path->ptr, '/'))) {
1169 /* handle special case of file in root directory */
1170 const char* physdir = (c == con->physical.path->ptr) ? "/" : con->physical.path->ptr;
1172 /* temporarily shorten con->physical.path to directory without terminating '/' */
1173 *c = '\0';
1174 /* change to the physical directory */
1175 if (-1 == chdir(physdir)) {
1176 log_error_write(srv, __FILE__, __LINE__, "ssb", "chdir failed:", strerror(errno), con->physical.path);
1178 *c = '/';
1181 /* we don't need the client socket */
1182 for (i = 3; i < 256; i++) {
1183 if (i != srv->errorlog_fd) close(i);
1186 /* exec the cgi */
1187 execve(args[0], args, env.ptr);
1189 /* most log files may have been closed/redirected by this point,
1190 * though stderr might still point to lighttpd.breakage.log */
1191 perror(args[0]);
1192 _exit(1);
1194 case -1:
1195 /* error */
1196 log_error_write(srv, __FILE__, __LINE__, "ss", "fork failed:", strerror(errno));
1197 close(from_cgi_fds[0]);
1198 close(from_cgi_fds[1]);
1199 close(to_cgi_fds[0]);
1200 close(to_cgi_fds[1]);
1201 return -1;
1202 default: {
1203 /* parent process */
1205 close(from_cgi_fds[1]);
1206 close(to_cgi_fds[0]);
1208 /* register PID and wait for them asynchronously */
1210 hctx->pid = pid;
1211 hctx->fd = from_cgi_fds[0];
1212 hctx->fde_ndx = -1;
1214 ++srv->cur_fds;
1216 if (0 == con->request.content_length) {
1217 close(to_cgi_fds[1]);
1218 } else {
1219 /* there is content to send */
1220 if (-1 == fdevent_fcntl_set_nb(srv->ev, to_cgi_fds[1])) {
1221 log_error_write(srv, __FILE__, __LINE__, "ss", "fcntl failed: ", strerror(errno));
1222 close(to_cgi_fds[1]);
1223 cgi_connection_close(srv, hctx);
1224 return -1;
1227 if (0 != cgi_write_request(srv, hctx, to_cgi_fds[1])) {
1228 close(to_cgi_fds[1]);
1229 cgi_connection_close(srv, hctx);
1230 return -1;
1233 ++srv->cur_fds;
1236 fdevent_register(srv->ev, hctx->fd, cgi_handle_fdevent, hctx);
1237 if (-1 == fdevent_fcntl_set_nb(srv->ev, hctx->fd)) {
1238 log_error_write(srv, __FILE__, __LINE__, "ss", "fcntl failed: ", strerror(errno));
1239 cgi_connection_close(srv, hctx);
1240 return -1;
1242 fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
1244 break;
1248 return 0;
1249 #else
1250 return -1;
1251 #endif
1254 static buffer * cgi_get_handler(array *a, buffer *fn) {
1255 size_t k, s_len = buffer_string_length(fn);
1256 for (k = 0; k < a->used; ++k) {
1257 data_string *ds = (data_string *)a->data[k];
1258 size_t ct_len = buffer_string_length(ds->key);
1260 if (buffer_is_empty(ds->key)) continue;
1261 if (s_len < ct_len) continue;
1263 if (0 == strncmp(fn->ptr + s_len - ct_len, ds->key->ptr, ct_len)) {
1264 return ds->value;
1268 return NULL;
1271 #define PATCH(x) \
1272 p->conf.x = s->x;
1273 static int mod_cgi_patch_connection(server *srv, connection *con, plugin_data *p) {
1274 size_t i, j;
1275 plugin_config *s = p->config_storage[0];
1277 PATCH(cgi);
1278 PATCH(execute_x_only);
1279 PATCH(xsendfile_allow);
1280 PATCH(xsendfile_docroot);
1282 /* skip the first, the global context */
1283 for (i = 1; i < srv->config_context->used; i++) {
1284 data_config *dc = (data_config *)srv->config_context->data[i];
1285 s = p->config_storage[i];
1287 /* condition didn't match */
1288 if (!config_check_cond(srv, con, dc)) continue;
1290 /* merge config */
1291 for (j = 0; j < dc->value->used; j++) {
1292 data_unset *du = dc->value->data[j];
1294 if (buffer_is_equal_string(du->key, CONST_STR_LEN("cgi.assign"))) {
1295 PATCH(cgi);
1296 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("cgi.execute-x-only"))) {
1297 PATCH(execute_x_only);
1298 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("cgi.x-sendfile"))) {
1299 PATCH(xsendfile_allow);
1300 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("cgi.x-sendfile-docroot"))) {
1301 PATCH(xsendfile_docroot);
1306 return 0;
1308 #undef PATCH
1310 URIHANDLER_FUNC(cgi_is_handled) {
1311 plugin_data *p = p_d;
1312 buffer *fn = con->physical.path;
1313 stat_cache_entry *sce = NULL;
1314 struct stat stbuf;
1315 struct stat *st;
1317 if (con->mode != DIRECT) return HANDLER_GO_ON;
1319 if (buffer_is_empty(fn)) return HANDLER_GO_ON;
1321 mod_cgi_patch_connection(srv, con, p);
1323 if (HANDLER_ERROR != stat_cache_get_entry(srv, con, con->physical.path, &sce)) {
1324 st = &sce->st;
1325 } else {
1326 /* CGI might be executable even if it is not readable
1327 * (stat_cache_get_entry() currently checks file is readable)*/
1328 if (0 != stat(con->physical.path->ptr, &stbuf)) return HANDLER_GO_ON;
1329 st = &stbuf;
1332 if (!S_ISREG(st->st_mode)) return HANDLER_GO_ON;
1333 if (p->conf.execute_x_only == 1 && (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) return HANDLER_GO_ON;
1335 if (NULL != cgi_get_handler(p->conf.cgi, fn)) {
1336 handler_ctx *hctx = cgi_handler_ctx_init();
1337 hctx->remote_conn = con;
1338 hctx->plugin_data = p;
1339 memcpy(&hctx->conf, &p->conf, sizeof(plugin_config));
1340 con->plugin_ctx[p->id] = hctx;
1341 con->mode = p->id;
1344 return HANDLER_GO_ON;
1347 TRIGGER_FUNC(cgi_trigger) {
1348 plugin_data *p = p_d;
1349 size_t ndx;
1350 /* the trigger handle only cares about lonely PID which we have to wait for */
1351 #ifndef __WIN32
1353 for (ndx = 0; ndx < p->cgi_pid.used; ndx++) {
1354 int status;
1356 switch(waitpid(p->cgi_pid.ptr[ndx], &status, WNOHANG)) {
1357 case 0:
1358 /* not finished yet */
1359 #if 0
1360 log_error_write(srv, __FILE__, __LINE__, "sd", "(debug) child isn't done yet, pid:", p->cgi_pid.ptr[ndx]);
1361 #endif
1362 break;
1363 case -1:
1364 if (errno == ECHILD) {
1365 /* someone else called waitpid... remove the pid to stop looping the error each time */
1366 log_error_write(srv, __FILE__, __LINE__, "s", "cgi child vanished, probably someone else called waitpid");
1368 cgi_pid_del(srv, p, p->cgi_pid.ptr[ndx]);
1369 ndx--;
1370 continue;
1373 log_error_write(srv, __FILE__, __LINE__, "ss", "waitpid failed: ", strerror(errno));
1375 return HANDLER_ERROR;
1376 default:
1378 if (WIFEXITED(status)) {
1379 #if 0
1380 log_error_write(srv, __FILE__, __LINE__, "sd", "(debug) cgi exited fine, pid:", p->cgi_pid.ptr[ndx]);
1381 #endif
1382 } else if (WIFSIGNALED(status)) {
1383 /* FIXME: what if we killed the CGI script with a kill(..., SIGTERM) ?
1385 if (WTERMSIG(status) != SIGTERM) {
1386 log_error_write(srv, __FILE__, __LINE__, "sd", "cleaning up CGI: process died with signal", WTERMSIG(status));
1388 } else {
1389 log_error_write(srv, __FILE__, __LINE__, "s", "cleaning up CGI: ended unexpectedly");
1392 cgi_pid_del(srv, p, p->cgi_pid.ptr[ndx]);
1393 /* del modified the buffer structure
1394 * and copies the last entry to the current one
1395 * -> recheck the current index
1397 ndx--;
1400 #endif
1401 return HANDLER_GO_ON;
1405 * - HANDLER_GO_ON : not our job
1406 * - HANDLER_FINISHED: got response
1407 * - HANDLER_WAIT_FOR_EVENT: waiting for response
1409 SUBREQUEST_FUNC(mod_cgi_handle_subrequest) {
1410 plugin_data *p = p_d;
1411 handler_ctx *hctx = con->plugin_ctx[p->id];
1412 chunkqueue *cq = con->request_content_queue;
1414 if (con->mode != p->id) return HANDLER_GO_ON;
1415 if (NULL == hctx) return HANDLER_GO_ON;
1417 if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
1418 && con->file_started) {
1419 if (chunkqueue_length(con->write_queue) > 65536 - 4096) {
1420 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
1421 } else if (!(fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_IN)) {
1422 /* optimistic read from backend, which might re-enable FDEVENT_IN */
1423 handler_t rc = cgi_recv_response(srv, hctx); /*(might invalidate hctx)*/
1424 if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
1428 if (cq->bytes_in != (off_t)con->request.content_length) {
1429 /*(64k - 4k to attempt to avoid temporary files
1430 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
1431 if (cq->bytes_in - cq->bytes_out > 65536 - 4096
1432 && (con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN)){
1433 con->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
1434 if (-1 != hctx->fd) return HANDLER_WAIT_FOR_EVENT;
1435 } else {
1436 handler_t r = connection_handle_read_post_state(srv, con);
1437 if (!chunkqueue_is_empty(cq)) {
1438 if (fdevent_event_get_interest(srv->ev, hctx->fdtocgi) & FDEVENT_OUT) {
1439 return (r == HANDLER_GO_ON) ? HANDLER_WAIT_FOR_EVENT : r;
1442 if (r != HANDLER_GO_ON) return r;
1446 if (-1 == hctx->fd) {
1447 buffer *handler = cgi_get_handler(hctx->conf.cgi, con->physical.path);
1448 if (!handler) return HANDLER_GO_ON; /*(should not happen; checked in cgi_is_handled())*/
1449 if (cgi_create_env(srv, con, p, hctx, handler)) {
1450 con->http_status = 500;
1451 con->mode = DIRECT;
1453 return HANDLER_FINISHED;
1455 #if 0
1456 log_error_write(srv, __FILE__, __LINE__, "sdd", "subrequest, pid =", hctx, hctx->pid);
1457 #endif
1458 } else if (!chunkqueue_is_empty(con->request_content_queue)) {
1459 if (0 != cgi_write_request(srv, hctx, hctx->fdtocgi)) {
1460 cgi_connection_close(srv, hctx);
1461 return HANDLER_ERROR;
1465 /* if not done, wait for CGI to close stdout, so we read EOF on pipe */
1466 return HANDLER_WAIT_FOR_EVENT;
1470 int mod_cgi_plugin_init(plugin *p);
1471 int mod_cgi_plugin_init(plugin *p) {
1472 p->version = LIGHTTPD_VERSION_ID;
1473 p->name = buffer_init_string("cgi");
1475 p->connection_reset = cgi_connection_close_callback;
1476 p->handle_subrequest_start = cgi_is_handled;
1477 p->handle_subrequest = mod_cgi_handle_subrequest;
1478 p->handle_trigger = cgi_trigger;
1479 p->init = mod_cgi_init;
1480 p->cleanup = mod_cgi_free;
1481 p->set_defaults = mod_fastcgi_set_defaults;
1483 p->data = NULL;
1485 return 0;