4 #include "stat_cache.h"
7 #include "connections.h"
10 #include "http_chunk.h"
11 #include "network_backends.h"
15 #include <sys/types.h>
19 # include <winsock2.h>
21 # include <sys/socket.h>
22 # include <sys/wait.h>
23 # include <netinet/in.h>
24 # include <arpa/inet.h>
39 enum {EOL_UNSET
, EOL_N
, EOL_RN
};
56 unsigned short execute_x_only
;
57 unsigned short xsendfile_allow
;
58 array
*xsendfile_docroot
;
66 buffer
*parse_response
;
68 plugin_config
**config_storage
;
77 int fde_ndx
; /* index into the fd-event buffer */
78 int fde_ndx_tocgi
; /* index into the fd-event buffer */
80 connection
*remote_conn
; /* dumb pointer */
81 plugin_data
*plugin_data
; /* dumb pointer */
84 buffer
*response_header
;
87 static handler_ctx
* cgi_handler_ctx_init(void) {
88 handler_ctx
*hctx
= calloc(1, sizeof(*hctx
));
92 hctx
->response
= buffer_init();
93 hctx
->response_header
= buffer_init();
100 static void cgi_handler_ctx_free(handler_ctx
*hctx
) {
101 buffer_free(hctx
->response
);
102 buffer_free(hctx
->response_header
);
107 enum {FDEVENT_HANDLED_UNSET
, FDEVENT_HANDLED_FINISHED
, FDEVENT_HANDLED_NOT_FINISHED
, FDEVENT_HANDLED_ERROR
};
109 INIT_FUNC(mod_cgi_init
) {
112 p
= calloc(1, sizeof(*p
));
116 p
->tmp_buf
= buffer_init();
117 p
->parse_response
= buffer_init();
123 FREE_FUNC(mod_cgi_free
) {
124 plugin_data
*p
= p_d
;
125 buffer_pid_t
*r
= &(p
->cgi_pid
);
129 if (p
->config_storage
) {
131 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
132 plugin_config
*s
= p
->config_storage
[i
];
134 if (NULL
== s
) continue;
137 array_free(s
->xsendfile_docroot
);
141 free(p
->config_storage
);
145 if (r
->ptr
) free(r
->ptr
);
147 buffer_free(p
->tmp_buf
);
148 buffer_free(p
->parse_response
);
152 return HANDLER_GO_ON
;
155 SETDEFAULTS_FUNC(mod_fastcgi_set_defaults
) {
156 plugin_data
*p
= p_d
;
159 config_values_t cv
[] = {
160 { "cgi.assign", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
161 { "cgi.execute-x-only", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
162 { "cgi.x-sendfile", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
163 { "cgi.x-sendfile-docroot", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
164 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
167 if (!p
) return HANDLER_ERROR
;
169 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
170 force_assert(p
->config_storage
);
172 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
173 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
176 s
= calloc(1, sizeof(plugin_config
));
179 s
->cgi
= array_init();
180 s
->execute_x_only
= 0;
181 s
->xsendfile_allow
= 0;
182 s
->xsendfile_docroot
= array_init();
184 cv
[0].destination
= s
->cgi
;
185 cv
[1].destination
= &(s
->execute_x_only
);
186 cv
[2].destination
= &(s
->xsendfile_allow
);
187 cv
[3].destination
= s
->xsendfile_docroot
;
189 p
->config_storage
[i
] = s
;
191 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
192 return HANDLER_ERROR
;
195 if (s
->xsendfile_docroot
->used
) {
197 for (j
= 0; j
< s
->xsendfile_docroot
->used
; ++j
) {
198 data_string
*ds
= (data_string
*)s
->xsendfile_docroot
->data
[j
];
199 if (ds
->type
!= TYPE_STRING
) {
200 log_error_write(srv
, __FILE__
, __LINE__
, "s",
201 "unexpected type for key cgi.x-sendfile-docroot; expected: cgi.x-sendfile-docroot = ( \"/allowed/path\", ... )");
202 return HANDLER_ERROR
;
204 if (ds
->value
->ptr
[0] != '/') {
205 log_error_write(srv
, __FILE__
, __LINE__
, "SBs",
206 "cgi.x-sendfile-docroot paths must begin with '/'; invalid: \"", ds
->value
, "\"");
207 return HANDLER_ERROR
;
209 buffer_path_simplify(ds
->value
, ds
->value
);
210 buffer_append_slash(ds
->value
);
215 return HANDLER_GO_ON
;
219 static int cgi_pid_add(server
*srv
, plugin_data
*p
, pid_t pid
) {
222 buffer_pid_t
*r
= &(p
->cgi_pid
);
226 for (i
= 0; i
< r
->used
; i
++) {
227 if (r
->ptr
[i
] > m
) m
= r
->ptr
[i
];
232 r
->ptr
= malloc(sizeof(*r
->ptr
) * r
->size
);
233 force_assert(r
->ptr
);
234 } else if (r
->used
== r
->size
) {
236 r
->ptr
= realloc(r
->ptr
, sizeof(*r
->ptr
) * r
->size
);
237 force_assert(r
->ptr
);
240 r
->ptr
[r
->used
++] = pid
;
245 static int cgi_pid_del(server
*srv
, plugin_data
*p
, pid_t pid
) {
247 buffer_pid_t
*r
= &(p
->cgi_pid
);
251 for (i
= 0; i
< r
->used
; i
++) {
252 if (r
->ptr
[i
] == pid
) break;
258 if (i
!= r
->used
- 1) {
259 r
->ptr
[i
] = r
->ptr
[r
->used
- 1];
267 static int cgi_response_parse(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*in
) {
274 buffer_copy_buffer(p
->parse_response
, in
);
276 for (s
= p
->parse_response
->ptr
;
277 NULL
!= (ns
= strchr(s
, '\n'));
278 s
= ns
+ 1, line
++) {
279 const char *key
, *value
;
286 if (ns
> s
&& ns
[-1] == '\r') ns
[-1] = '\0';
289 0 == strncmp(s
, "HTTP/1.", 7)) {
290 /* non-parsed header ... we parse them anyway */
296 /* after the space should be a status code for us */
298 status
= strtol(s
+9, NULL
, 10);
302 /* we expected 3 digits and didn't got them */
303 con
->parsed_response
|= HTTP_STATUS
;
304 con
->http_status
= status
;
308 /* parse the headers */
310 if (NULL
== (value
= strchr(s
, ':'))) {
311 /* we expect: "<key>: <value>\r\n" */
315 key_len
= value
- key
;
319 while (*value
== ' ' || *value
== '\t') value
++;
321 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->response
.headers
, TYPE_STRING
))) {
322 ds
= data_response_init();
324 buffer_copy_string_len(ds
->key
, key
, key_len
);
325 buffer_copy_string(ds
->value
, value
);
327 array_insert_unique(con
->response
.headers
, (data_unset
*)ds
);
331 if (0 == strncasecmp(key
, "Date", key_len
)) {
332 con
->parsed_response
|= HTTP_DATE
;
336 if (0 == strncasecmp(key
, "Status", key_len
)) {
337 int status
= strtol(value
, NULL
, 10);
338 if (status
>= 100 && status
< 1000) {
339 con
->http_status
= status
;
340 con
->parsed_response
|= HTTP_STATUS
;
342 con
->http_status
= 502;
347 if (0 == strncasecmp(key
, "Location", key_len
)) {
348 con
->parsed_response
|= HTTP_LOCATION
;
352 if (0 == strncasecmp(key
, "Connection", key_len
)) {
353 con
->response
.keep_alive
= (0 == strcasecmp(value
, "Keep-Alive")) ? 1 : 0;
354 con
->parsed_response
|= HTTP_CONNECTION
;
358 if (0 == strncasecmp(key
, "Content-Length", key_len
)) {
359 con
->response
.content_length
= strtoul(value
, NULL
, 10);
360 con
->parsed_response
|= HTTP_CONTENT_LENGTH
;
369 /* CGI/1.1 rev 03 - 7.2.1.2 */
370 if ((con
->parsed_response
& HTTP_LOCATION
) &&
371 !(con
->parsed_response
& HTTP_STATUS
)) {
372 con
->http_status
= 302;
379 static int cgi_demux_response(server
*srv
, handler_ctx
*hctx
) {
380 plugin_data
*p
= hctx
->plugin_data
;
381 connection
*con
= hctx
->remote_conn
;
388 buffer_string_prepare_copy(hctx
->response
, 4 * 1024);
390 if (ioctl(con
->fd
, FIONREAD
, &toread
) || toread
== 0 || toread
<= 4*1024) {
391 buffer_string_prepare_copy(hctx
->response
, 4 * 1024);
393 if (toread
> MAX_READ_LIMIT
) toread
= MAX_READ_LIMIT
;
394 buffer_string_prepare_copy(hctx
->response
, toread
);
398 if (-1 == (n
= read(hctx
->fd
, hctx
->response
->ptr
, hctx
->response
->size
- 1))) {
399 if (errno
== EAGAIN
|| errno
== EINTR
) {
400 /* would block, wait for signal */
401 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
402 return FDEVENT_HANDLED_NOT_FINISHED
;
405 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", strerror(errno
), con
->fd
, hctx
->fd
);
406 return FDEVENT_HANDLED_ERROR
;
411 return FDEVENT_HANDLED_FINISHED
;
414 buffer_commit(hctx
->response
, n
);
416 /* split header from body */
418 if (con
->file_started
== 0) {
420 int is_header_end
= 0;
422 size_t i
, header_len
;
424 buffer_append_string_buffer(hctx
->response_header
, hctx
->response
);
427 * we have to handle a few cases:
440 * and different mixes of \n and \r\n combinations
442 * Some users also forget about CGI and just send a response and hope
443 * we handle it. No headers, no header-content seperator
447 /* nph (non-parsed headers) */
448 if (0 == strncmp(hctx
->response_header
->ptr
, "HTTP/1.", 7)) is_header
= 1;
450 header_len
= buffer_string_length(hctx
->response_header
);
451 for (i
= 0; !is_header_end
&& i
< header_len
; i
++) {
452 char c
= hctx
->response_header
->ptr
[i
];
458 * looks like we have a normal header
464 if (is_header
== 0) {
465 /* we got a EOL but we don't seem to got a HTTP header */
473 * check if we saw a \n(\r)?\n sequence
476 ((i
- last_eol
== 1) ||
477 (i
- last_eol
== 2 && hctx
->response_header
->ptr
[i
- 1] == '\r'))) {
490 /* no header, but a body */
491 if (0 != http_chunk_append_buffer(srv
, con
, hctx
->response_header
)) {
492 return FDEVENT_HANDLED_ERROR
;
498 /* the body starts after the EOL */
499 bstart
= hctx
->response_header
->ptr
+ i
;
500 blen
= header_len
- i
;
503 * i still points to the char after the terminating EOL EOL
505 * put it on the last \n again
509 /* string the last \r?\n */
510 if (i
> 0 && (hctx
->response_header
->ptr
[i
- 1] == '\r')) {
514 buffer_string_set_length(hctx
->response_header
, i
);
516 /* parse the response header */
517 cgi_response_parse(srv
, con
, p
, hctx
->response_header
);
519 if (p
->conf
.xsendfile_allow
) {
521 if (NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "X-Sendfile"))) {
522 http_response_xsendfile(srv
, con
, ds
->value
, p
->conf
.xsendfile_docroot
);
523 return FDEVENT_HANDLED_FINISHED
;
528 if (0 != http_chunk_append_mem(srv
, con
, bstart
, blen
)) {
529 return FDEVENT_HANDLED_ERROR
;
534 con
->file_started
= 1;
536 /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
537 if (header_len
> MAX_HTTP_REQUEST_HEADER
) {
538 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "response headers too large for", con
->uri
.path
);
539 con
->http_status
= 502; /* Bad Gateway */
541 return FDEVENT_HANDLED_FINISHED
;
545 if (0 != http_chunk_append_buffer(srv
, con
, hctx
->response
)) {
546 return FDEVENT_HANDLED_ERROR
;
548 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
549 && chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
550 if (!con
->is_writable
) {
551 /*(defer removal of FDEVENT_IN interest since
552 * connection_state_machine() might be able to send data
553 * immediately, unless !con->is_writable, where
554 * connection_state_machine() might not loop back to call
555 * mod_cgi_handle_subrequest())*/
556 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
563 log_error_write(srv
, __FILE__
, __LINE__
, "ddss", con
->fd
, hctx
->fd
, connection_get_state(con
->state
), b
->ptr
);
567 return FDEVENT_HANDLED_NOT_FINISHED
;
570 static void cgi_connection_close_fdtocgi(server
*srv
, handler_ctx
*hctx
) {
571 /*(closes only hctx->fdtocgi)*/
572 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx_tocgi
), hctx
->fdtocgi
);
573 fdevent_unregister(srv
->ev
, hctx
->fdtocgi
);
575 if (close(hctx
->fdtocgi
)) {
576 log_error_write(srv
, __FILE__
, __LINE__
, "sds", "cgi stdin close failed ", hctx
->fdtocgi
, strerror(errno
));
581 static void cgi_connection_close(server
*srv
, handler_ctx
*hctx
) {
584 plugin_data
*p
= hctx
->plugin_data
;
585 connection
*con
= hctx
->remote_conn
;
589 /* the connection to the browser went away, but we still have a connection
592 * close cgi-connection
595 if (hctx
->fd
!= -1) {
596 /* close connection to the cgi-script */
597 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
);
598 fdevent_unregister(srv
->ev
, hctx
->fd
);
600 if (close(hctx
->fd
)) {
601 log_error_write(srv
, __FILE__
, __LINE__
, "sds", "cgi close failed ", hctx
->fd
, strerror(errno
));
605 if (hctx
->fdtocgi
!= -1) {
606 cgi_connection_close_fdtocgi(srv
, hctx
); /*(closes only hctx->fdtocgi)*/
611 con
->plugin_ctx
[p
->id
] = NULL
;
613 cgi_handler_ctx_free(hctx
);
615 /* if waitpid hasn't been called by response.c yet, do it here */
617 /* check if the CGI-script is already gone */
618 switch(waitpid(pid
, &status
, WNOHANG
)) {
620 /* not finished yet */
622 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "(debug) child isn't done yet, pid:", pid
);
627 if (errno
== EINTR
) break;
630 * errno == ECHILD happens if _subrequest catches the process-status before
631 * we have read the response of the cgi process
636 * -> we get here with waitpid == ECHILD
639 if (errno
!= ECHILD
) {
640 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "waitpid failed: ", strerror(errno
));
642 /* anyway: don't wait for it anymore */
646 if (WIFEXITED(status
)) {
648 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "(debug) cgi exited fine, pid:", pid
);
651 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "cgi died, pid:", pid
);
661 /* cgi-script is still alive, queue the PID for removal */
662 cgi_pid_add(srv
, p
, pid
);
667 /* finish response (if not already con->file_started, con->file_finished) */
668 if (con
->mode
== p
->id
) {
669 http_response_backend_done(srv
, con
);
673 static handler_t
cgi_connection_close_callback(server
*srv
, connection
*con
, void *p_d
) {
674 plugin_data
*p
= p_d
;
675 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
676 if (hctx
) cgi_connection_close(srv
, hctx
);
678 return HANDLER_GO_ON
;
682 static int cgi_write_request(server
*srv
, handler_ctx
*hctx
, int fd
);
685 static handler_t
cgi_handle_fdevent_send (server
*srv
, void *ctx
, int revents
) {
686 handler_ctx
*hctx
= ctx
;
687 connection
*con
= hctx
->remote_conn
;
689 /*(joblist only actually necessary here in mod_cgi fdevent send if returning HANDLER_ERROR)*/
690 joblist_append(srv
, con
);
692 if (revents
& FDEVENT_OUT
) {
693 if (0 != cgi_write_request(srv
, hctx
, hctx
->fdtocgi
)) {
694 cgi_connection_close(srv
, hctx
);
695 return HANDLER_ERROR
;
697 /* more request body to be sent to CGI */
700 if (revents
& FDEVENT_HUP
) {
701 /* skip sending remaining data to CGI */
702 if (con
->request
.content_length
) {
703 chunkqueue
*cq
= con
->request_content_queue
;
704 chunkqueue_mark_written(cq
, chunkqueue_length(cq
));
705 if (cq
->bytes_in
!= (off_t
)con
->request
.content_length
) {
710 cgi_connection_close_fdtocgi(srv
, hctx
); /*(closes only hctx->fdtocgi)*/
711 } else if (revents
& FDEVENT_ERR
) {
712 /* kill all connections to the cgi process */
714 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cgi-FDEVENT_ERR");
716 cgi_connection_close(srv
, hctx
);
717 return HANDLER_ERROR
;
720 return HANDLER_FINISHED
;
724 static int cgi_recv_response(server
*srv
, handler_ctx
*hctx
) {
725 switch (cgi_demux_response(srv
, hctx
)) {
726 case FDEVENT_HANDLED_NOT_FINISHED
:
728 case FDEVENT_HANDLED_FINISHED
:
732 log_error_write(srv
, __FILE__
, __LINE__
, "ddss", con
->fd
, hctx
->fd
, connection_get_state(con
->state
), "finished");
734 cgi_connection_close(srv
, hctx
);
736 /* if we get a IN|HUP and have read everything don't exec the close twice */
737 return HANDLER_FINISHED
;
738 case FDEVENT_HANDLED_ERROR
:
739 log_error_write(srv
, __FILE__
, __LINE__
, "s", "demuxer failed: ");
741 cgi_connection_close(srv
, hctx
);
742 return HANDLER_FINISHED
;
745 return HANDLER_GO_ON
;
749 static handler_t
cgi_handle_fdevent(server
*srv
, void *ctx
, int revents
) {
750 handler_ctx
*hctx
= ctx
;
751 connection
*con
= hctx
->remote_conn
;
753 joblist_append(srv
, con
);
755 if (revents
& FDEVENT_IN
) {
756 handler_t rc
= cgi_recv_response(srv
, hctx
);/*(might invalidate hctx)*/
757 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
760 /* perhaps this issue is already handled */
761 if (revents
& FDEVENT_HUP
) {
762 if (con
->file_started
) {
763 /* drain any remaining data from kernel pipe buffers
764 * even if (con->conf.stream_response_body
765 * & FDEVENT_STREAM_RESPONSE_BUFMIN)
766 * since event loop will spin on fd FDEVENT_HUP event
767 * until unregistered. */
770 rc
= cgi_recv_response(srv
,hctx
);/*(might invalidate hctx)*/
771 } while (rc
== HANDLER_GO_ON
); /*(unless HANDLER_GO_ON)*/
772 return rc
; /* HANDLER_FINISHED or HANDLER_ERROR */
773 } else if (!buffer_string_is_empty(hctx
->response_header
)) {
774 /* unfinished header package which is a body in reality */
775 con
->file_started
= 1;
776 if (0 != http_chunk_append_buffer(srv
, con
, hctx
->response_header
)) {
777 cgi_connection_close(srv
, hctx
);
778 return HANDLER_ERROR
;
782 log_error_write(srv
, __FILE__
, __LINE__
, "sddd", "got HUP from cgi", con
->fd
, hctx
->fd
, revents
);
785 cgi_connection_close(srv
, hctx
);
786 } else if (revents
& FDEVENT_ERR
) {
787 /* kill all connections to the cgi process */
788 cgi_connection_close(srv
, hctx
);
790 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cgi-FDEVENT_ERR");
792 return HANDLER_ERROR
;
795 return HANDLER_FINISHED
;
799 static int cgi_env_add(char_array
*env
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
802 if (!key
|| !val
) return -1;
804 dst
= malloc(key_len
+ val_len
+ 2);
806 memcpy(dst
, key
, key_len
);
808 memcpy(dst
+ key_len
+ 1, val
, val_len
);
809 dst
[key_len
+ 1 + val_len
] = '\0';
811 if (env
->size
== 0) {
813 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
814 force_assert(env
->ptr
);
815 } else if (env
->size
== env
->used
) {
817 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
818 force_assert(env
->ptr
);
821 env
->ptr
[env
->used
++] = dst
;
826 /* returns: 0: continue, -1: fatal error, -2: connection reset */
827 /* similar to network_write_file_chunk_mmap, but doesn't use send on windows (because we're on pipes),
828 * also mmaps and sends complete chunk instead of only small parts - the files
829 * are supposed to be temp files with reasonable chunk sizes.
831 * Also always use mmap; the files are "trusted", as we created them.
833 static ssize_t
cgi_write_file_chunk_mmap(server
*srv
, connection
*con
, int fd
, chunkqueue
*cq
) {
834 chunk
* const c
= cq
->first
;
835 off_t offset
, toSend
, file_end
;
837 size_t mmap_offset
, mmap_avail
;
840 force_assert(NULL
!= c
);
841 force_assert(FILE_CHUNK
== c
->type
);
842 force_assert(c
->offset
>= 0 && c
->offset
<= c
->file
.length
);
844 offset
= c
->file
.start
+ c
->offset
;
845 toSend
= c
->file
.length
- c
->offset
;
846 file_end
= c
->file
.start
+ c
->file
.length
; /* offset to file end in this chunk */
849 chunkqueue_remove_finished_chunks(cq
);
853 if (0 != network_open_file_chunk(srv
, con
, cq
)) return -1;
855 /* (re)mmap the buffer if range is not covered completely */
856 if (MAP_FAILED
== c
->file
.mmap
.start
857 || offset
< c
->file
.mmap
.offset
858 || file_end
> (off_t
)(c
->file
.mmap
.offset
+ c
->file
.mmap
.length
)) {
860 if (MAP_FAILED
!= c
->file
.mmap
.start
) {
861 munmap(c
->file
.mmap
.start
, c
->file
.mmap
.length
);
862 c
->file
.mmap
.start
= MAP_FAILED
;
865 c
->file
.mmap
.offset
= mmap_align_offset(offset
);
866 c
->file
.mmap
.length
= file_end
- c
->file
.mmap
.offset
;
868 if (MAP_FAILED
== (c
->file
.mmap
.start
= mmap(NULL
, c
->file
.mmap
.length
, PROT_READ
, MAP_PRIVATE
, c
->file
.fd
, c
->file
.mmap
.offset
))) {
869 if (toSend
> 65536) toSend
= 65536;
870 data
= malloc(toSend
);
872 if (-1 == lseek(c
->file
.fd
, offset
, SEEK_SET
)
873 || 0 >= (toSend
= read(c
->file
.fd
, data
, toSend
))) {
875 log_error_write(srv
, __FILE__
, __LINE__
, "ssbdo", "lseek/read failed:",
876 strerror(errno
), c
->file
.name
, c
->file
.fd
, offset
);
877 } else { /*(0 == toSend)*/
878 log_error_write(srv
, __FILE__
, __LINE__
, "sbdo", "unexpected EOF (input truncated?):",
879 c
->file
.name
, c
->file
.fd
, offset
);
887 if (MAP_FAILED
!= c
->file
.mmap
.start
) {
888 force_assert(offset
>= c
->file
.mmap
.offset
);
889 mmap_offset
= offset
- c
->file
.mmap
.offset
;
890 force_assert(c
->file
.mmap
.length
> mmap_offset
);
891 mmap_avail
= c
->file
.mmap
.length
- mmap_offset
;
892 force_assert(toSend
<= (off_t
) mmap_avail
);
894 data
= c
->file
.mmap
.start
+ mmap_offset
;
897 r
= write(fd
, data
, toSend
);
899 if (MAP_FAILED
== c
->file
.mmap
.start
) free(data
);
910 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
911 "write failed:", strerror(errno
), fd
);
917 chunkqueue_mark_written(cq
, r
);
923 static int cgi_write_request(server
*srv
, handler_ctx
*hctx
, int fd
) {
924 connection
*con
= hctx
->remote_conn
;
925 chunkqueue
*cq
= con
->request_content_queue
;
928 /* old comment: windows doesn't support select() on pipes - wouldn't be easy to fix for all platforms.
929 * solution: if this is still a problem on windows, then substitute
930 * socketpair() for pipe() and closesocket() for close() on windows.
933 for (c
= cq
->first
; c
; c
= cq
->first
) {
938 r
= cgi_write_file_chunk_mmap(srv
, con
, fd
, cq
);
942 if ((r
= write(fd
, c
->mem
->ptr
+ c
->offset
, buffer_string_length(c
->mem
) - c
->offset
)) < 0) {
946 /* ignore and try again */
951 /* connection closed */
956 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "write failed due to: ", strerror(errno
));
961 chunkqueue_mark_written(cq
, r
);
966 if (0 == r
) break; /*(might block)*/
973 /* connection reset */
974 log_error_write(srv
, __FILE__
, __LINE__
, "s", "failed to send post data to cgi, connection closed by CGI");
975 /* skip all remaining data */
976 chunkqueue_mark_written(cq
, chunkqueue_length(cq
));
983 if (cq
->bytes_out
== (off_t
)con
->request
.content_length
) {
984 /* sent all request body input */
985 /* close connection to the cgi-script */
986 if (-1 == hctx
->fdtocgi
) { /*(received request body sent in initial send to pipe buffer)*/
988 log_error_write(srv
, __FILE__
, __LINE__
, "sds", "cgi stdin close failed ", fd
, strerror(errno
));
991 cgi_connection_close_fdtocgi(srv
, hctx
); /*(closes only hctx->fdtocgi)*/
994 off_t cqlen
= cq
->bytes_in
- cq
->bytes_out
;
995 if (cq
->bytes_in
< (off_t
)con
->request
.content_length
&& cqlen
< 65536 - 16384) {
996 /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
997 if (!(con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_POLLIN
)) {
998 con
->conf
.stream_request_body
|= FDEVENT_STREAM_REQUEST_POLLIN
;
999 con
->is_readable
= 1; /* trigger optimistic read from client */
1002 if (-1 == hctx
->fdtocgi
) { /*(not registered yet)*/
1004 hctx
->fde_ndx_tocgi
= -1;
1005 fdevent_register(srv
->ev
, hctx
->fdtocgi
, cgi_handle_fdevent_send
, hctx
);
1007 if (0 == cqlen
) { /*(chunkqueue_is_empty(cq))*/
1008 if ((fdevent_event_get_interest(srv
->ev
, hctx
->fdtocgi
) & FDEVENT_OUT
)) {
1009 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx_tocgi
), hctx
->fdtocgi
, 0);
1012 /* more request body remains to be sent to CGI so register for fdevents */
1013 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx_tocgi
), hctx
->fdtocgi
, FDEVENT_OUT
);
1020 static int cgi_create_env(server
*srv
, connection
*con
, plugin_data
*p
, handler_ctx
*hctx
, buffer
*cgi_handler
) {
1024 char b2
[INET6_ADDRSTRLEN
+ 1];
1028 int from_cgi_fds
[2];
1033 if (!buffer_string_is_empty(cgi_handler
)) {
1034 /* stat the exec file */
1035 if (-1 == (stat(cgi_handler
->ptr
, &st
))) {
1036 log_error_write(srv
, __FILE__
, __LINE__
, "sbss",
1037 "stat for cgi-handler", cgi_handler
,
1038 "failed:", strerror(errno
));
1043 if (pipe(to_cgi_fds
)) {
1044 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "pipe failed:", strerror(errno
));
1048 if (pipe(from_cgi_fds
)) {
1049 close(to_cgi_fds
[0]);
1050 close(to_cgi_fds
[1]);
1051 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "pipe failed:", strerror(errno
));
1056 switch (pid
= fork()) {
1062 char buf
[LI_ITOSTRING_LENGTH
];
1067 server_socket
*srv_sock
= con
->srv_socket
;
1069 /* move stdout to from_cgi_fd[1] */
1070 close(STDOUT_FILENO
);
1071 dup2(from_cgi_fds
[1], STDOUT_FILENO
);
1072 close(from_cgi_fds
[1]);
1074 close(from_cgi_fds
[0]);
1076 /* move the stdin to to_cgi_fd[0] */
1077 close(STDIN_FILENO
);
1078 dup2(to_cgi_fds
[0], STDIN_FILENO
);
1079 close(to_cgi_fds
[0]);
1081 close(to_cgi_fds
[1]);
1083 /* create environment */
1088 cgi_env_add(&env
, CONST_STR_LEN("SERVER_SOFTWARE"), CONST_BUF_LEN(con
->conf
.server_tag
));
1090 if (!buffer_string_is_empty(con
->server_name
)) {
1091 size_t len
= buffer_string_length(con
->server_name
);
1093 if (con
->server_name
->ptr
[0] == '[') {
1094 const char *colon
= strstr(con
->server_name
->ptr
, "]:");
1095 if (colon
) len
= (colon
+ 1) - con
->server_name
->ptr
;
1097 const char *colon
= strchr(con
->server_name
->ptr
, ':');
1098 if (colon
) len
= colon
- con
->server_name
->ptr
;
1101 cgi_env_add(&env
, CONST_STR_LEN("SERVER_NAME"), con
->server_name
->ptr
, len
);
1105 srv_sock
->addr
.plain
.sa_family
,
1106 srv_sock
->addr
.plain
.sa_family
== AF_INET6
?
1107 (const void *) &(srv_sock
->addr
.ipv6
.sin6_addr
) :
1108 (const void *) &(srv_sock
->addr
.ipv4
.sin_addr
),
1111 s
= inet_ntoa(srv_sock
->addr
.ipv4
.sin_addr
);
1114 cgi_env_add(&env
, CONST_STR_LEN("SERVER_NAME"), s
, strlen(s
));
1116 cgi_env_add(&env
, CONST_STR_LEN("GATEWAY_INTERFACE"), CONST_STR_LEN("CGI/1.1"));
1118 s
= get_http_version_name(con
->request
.http_version
);
1120 cgi_env_add(&env
, CONST_STR_LEN("SERVER_PROTOCOL"), s
, strlen(s
));
1122 li_utostrn(buf
, sizeof(buf
),
1124 ntohs(srv_sock
->addr
.plain
.sa_family
== AF_INET6
? srv_sock
->addr
.ipv6
.sin6_port
: srv_sock
->addr
.ipv4
.sin_port
)
1126 ntohs(srv_sock
->addr
.ipv4
.sin_port
)
1129 cgi_env_add(&env
, CONST_STR_LEN("SERVER_PORT"), buf
, strlen(buf
));
1131 switch (srv_sock
->addr
.plain
.sa_family
) {
1135 srv_sock
->addr
.plain
.sa_family
,
1136 (const void *) &(srv_sock
->addr
.ipv6
.sin6_addr
),
1141 srv_sock
->addr
.plain
.sa_family
,
1142 (const void *) &(srv_sock
->addr
.ipv4
.sin_addr
),
1147 s
= inet_ntoa(srv_sock
->addr
.ipv4
.sin_addr
);
1155 cgi_env_add(&env
, CONST_STR_LEN("SERVER_ADDR"), s
, strlen(s
));
1157 s
= get_http_method_name(con
->request
.http_method
);
1159 cgi_env_add(&env
, CONST_STR_LEN("REQUEST_METHOD"), s
, strlen(s
));
1161 if (!buffer_string_is_empty(con
->request
.pathinfo
)) {
1162 cgi_env_add(&env
, CONST_STR_LEN("PATH_INFO"), CONST_BUF_LEN(con
->request
.pathinfo
));
1164 if (!buffer_string_is_empty(con
->uri
.query
)) {
1165 cgi_env_add(&env
, CONST_STR_LEN("QUERY_STRING"), CONST_BUF_LEN(con
->uri
.query
));
1167 cgi_env_add(&env
, CONST_STR_LEN("QUERY_STRING"), CONST_STR_LEN(""));
1169 if (con
->error_handler_saved_status
>= 0) {
1170 cgi_env_add(&env
, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(con
->request
.uri
));
1172 cgi_env_add(&env
, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(con
->request
.orig_uri
));
1174 /* set REDIRECT_STATUS for php compiled with --force-redirect
1175 * (if REDIRECT_STATUS has not already been set by error handler) */
1176 if (0 == con
->error_handler_saved_status
) {
1177 cgi_env_add(&env
, CONST_STR_LEN("REDIRECT_STATUS"), CONST_STR_LEN("200"));
1181 switch (con
->dst_addr
.plain
.sa_family
) {
1185 con
->dst_addr
.plain
.sa_family
,
1186 (const void *) &(con
->dst_addr
.ipv6
.sin6_addr
),
1191 con
->dst_addr
.plain
.sa_family
,
1192 (const void *) &(con
->dst_addr
.ipv4
.sin_addr
),
1197 s
= inet_ntoa(con
->dst_addr
.ipv4
.sin_addr
);
1205 cgi_env_add(&env
, CONST_STR_LEN("REMOTE_ADDR"), s
, strlen(s
));
1207 li_utostrn(buf
, sizeof(buf
),
1209 ntohs(con
->dst_addr
.plain
.sa_family
== AF_INET6
? con
->dst_addr
.ipv6
.sin6_port
: con
->dst_addr
.ipv4
.sin_port
)
1211 ntohs(con
->dst_addr
.ipv4
.sin_port
)
1214 cgi_env_add(&env
, CONST_STR_LEN("REMOTE_PORT"), buf
, strlen(buf
));
1216 if (buffer_is_equal_caseless_string(con
->uri
.scheme
, CONST_STR_LEN("https"))) {
1217 cgi_env_add(&env
, CONST_STR_LEN("HTTPS"), CONST_STR_LEN("on"));
1220 li_itostrn(buf
, sizeof(buf
), con
->request
.content_length
);
1221 cgi_env_add(&env
, CONST_STR_LEN("CONTENT_LENGTH"), buf
, strlen(buf
));
1222 cgi_env_add(&env
, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(con
->physical
.path
));
1223 cgi_env_add(&env
, CONST_STR_LEN("SCRIPT_NAME"), CONST_BUF_LEN(con
->uri
.path
));
1224 cgi_env_add(&env
, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(con
->physical
.basedir
));
1227 if (NULL
!= (s
= getenv("LD_PRELOAD"))) {
1228 cgi_env_add(&env
, CONST_STR_LEN("LD_PRELOAD"), s
, strlen(s
));
1231 if (NULL
!= (s
= getenv("LD_LIBRARY_PATH"))) {
1232 cgi_env_add(&env
, CONST_STR_LEN("LD_LIBRARY_PATH"), s
, strlen(s
));
1235 /* CYGWIN needs SYSTEMROOT */
1236 if (NULL
!= (s
= getenv("SYSTEMROOT"))) {
1237 cgi_env_add(&env
, CONST_STR_LEN("SYSTEMROOT"), s
, strlen(s
));
1241 for (n
= 0; n
< con
->request
.headers
->used
; n
++) {
1244 ds
= (data_string
*)con
->request
.headers
->data
[n
];
1246 if (!buffer_is_empty(ds
->value
) && !buffer_is_empty(ds
->key
)) {
1247 buffer_copy_string_encoded_cgi_varnames(p
->tmp_buf
, CONST_BUF_LEN(ds
->key
), 1);
1249 cgi_env_add(&env
, CONST_BUF_LEN(p
->tmp_buf
), CONST_BUF_LEN(ds
->value
));
1253 for (n
= 0; n
< con
->environment
->used
; n
++) {
1256 ds
= (data_string
*)con
->environment
->data
[n
];
1258 if (!buffer_is_empty(ds
->value
) && !buffer_is_empty(ds
->key
)) {
1259 buffer_copy_string_encoded_cgi_varnames(p
->tmp_buf
, CONST_BUF_LEN(ds
->key
), 0);
1261 cgi_env_add(&env
, CONST_BUF_LEN(p
->tmp_buf
), CONST_BUF_LEN(ds
->value
));
1265 if (env
.size
== env
.used
) {
1267 env
.ptr
= realloc(env
.ptr
, env
.size
* sizeof(*env
.ptr
));
1270 env
.ptr
[env
.used
] = NULL
;
1274 args
= malloc(sizeof(*args
) * argc
);
1278 if (!buffer_string_is_empty(cgi_handler
)) {
1279 args
[i
++] = cgi_handler
->ptr
;
1281 args
[i
++] = con
->physical
.path
->ptr
;
1284 /* search for the last / */
1285 if (NULL
!= (c
= strrchr(con
->physical
.path
->ptr
, '/'))) {
1286 /* handle special case of file in root directory */
1287 const char* physdir
= (c
== con
->physical
.path
->ptr
) ? "/" : con
->physical
.path
->ptr
;
1289 /* temporarily shorten con->physical.path to directory without terminating '/' */
1291 /* change to the physical directory */
1292 if (-1 == chdir(physdir
)) {
1293 log_error_write(srv
, __FILE__
, __LINE__
, "ssb", "chdir failed:", strerror(errno
), con
->physical
.path
);
1298 /* we don't need the client socket */
1299 for (i
= 3; i
< 256; i
++) {
1300 if (i
!= srv
->errorlog_fd
) close(i
);
1304 execve(args
[0], args
, env
.ptr
);
1306 /* most log files may have been closed/redirected by this point,
1307 * though stderr might still point to lighttpd.breakage.log */
1313 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fork failed:", strerror(errno
));
1314 close(from_cgi_fds
[0]);
1315 close(from_cgi_fds
[1]);
1316 close(to_cgi_fds
[0]);
1317 close(to_cgi_fds
[1]);
1320 /* parent process */
1322 close(from_cgi_fds
[1]);
1323 close(to_cgi_fds
[0]);
1325 /* register PID and wait for them asynchronously */
1328 hctx
->fd
= from_cgi_fds
[0];
1331 if (0 == con
->request
.content_length
) {
1332 close(to_cgi_fds
[1]);
1334 /* there is content to send */
1335 if (-1 == fdevent_fcntl_set(srv
->ev
, to_cgi_fds
[1])) {
1336 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fcntl failed: ", strerror(errno
));
1337 close(to_cgi_fds
[1]);
1338 cgi_connection_close(srv
, hctx
);
1342 if (0 != cgi_write_request(srv
, hctx
, to_cgi_fds
[1])) {
1343 close(to_cgi_fds
[1]);
1344 cgi_connection_close(srv
, hctx
);
1349 fdevent_register(srv
->ev
, hctx
->fd
, cgi_handle_fdevent
, hctx
);
1350 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
1352 if (-1 == fdevent_fcntl_set(srv
->ev
, hctx
->fd
)) {
1353 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fcntl failed: ", strerror(errno
));
1354 cgi_connection_close(srv
, hctx
);
1368 static buffer
* cgi_get_handler(array
*a
, buffer
*fn
) {
1369 size_t k
, s_len
= buffer_string_length(fn
);
1370 for (k
= 0; k
< a
->used
; ++k
) {
1371 data_string
*ds
= (data_string
*)a
->data
[k
];
1372 size_t ct_len
= buffer_string_length(ds
->key
);
1374 if (buffer_is_empty(ds
->key
)) continue;
1375 if (s_len
< ct_len
) continue;
1377 if (0 == strncmp(fn
->ptr
+ s_len
- ct_len
, ds
->key
->ptr
, ct_len
)) {
1387 static int mod_cgi_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
1389 plugin_config
*s
= p
->config_storage
[0];
1392 PATCH(execute_x_only
);
1393 PATCH(xsendfile_allow
);
1394 PATCH(xsendfile_docroot
);
1396 /* skip the first, the global context */
1397 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
1398 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
1399 s
= p
->config_storage
[i
];
1401 /* condition didn't match */
1402 if (!config_check_cond(srv
, con
, dc
)) continue;
1405 for (j
= 0; j
< dc
->value
->used
; j
++) {
1406 data_unset
*du
= dc
->value
->data
[j
];
1408 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cgi.assign"))) {
1410 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cgi.execute-x-only"))) {
1411 PATCH(execute_x_only
);
1412 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cgi.x-sendfile"))) {
1413 PATCH(xsendfile_allow
);
1414 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cgi.x-sendfile-docroot"))) {
1415 PATCH(xsendfile_docroot
);
1424 URIHANDLER_FUNC(cgi_is_handled
) {
1425 plugin_data
*p
= p_d
;
1426 buffer
*fn
= con
->physical
.path
;
1427 stat_cache_entry
*sce
= NULL
;
1429 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
1431 if (buffer_is_empty(fn
)) return HANDLER_GO_ON
;
1433 mod_cgi_patch_connection(srv
, con
, p
);
1435 if (HANDLER_ERROR
== stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) return HANDLER_GO_ON
;
1436 if (!S_ISREG(sce
->st
.st_mode
)) return HANDLER_GO_ON
;
1437 if (p
->conf
.execute_x_only
== 1 && (sce
->st
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0) return HANDLER_GO_ON
;
1439 if (NULL
!= cgi_get_handler(p
->conf
.cgi
, fn
)) {
1440 handler_ctx
*hctx
= cgi_handler_ctx_init();
1441 hctx
->remote_conn
= con
;
1442 hctx
->plugin_data
= p
;
1443 con
->plugin_ctx
[p
->id
] = hctx
;
1447 return HANDLER_GO_ON
;
1450 TRIGGER_FUNC(cgi_trigger
) {
1451 plugin_data
*p
= p_d
;
1453 /* the trigger handle only cares about lonely PID which we have to wait for */
1456 for (ndx
= 0; ndx
< p
->cgi_pid
.used
; ndx
++) {
1459 switch(waitpid(p
->cgi_pid
.ptr
[ndx
], &status
, WNOHANG
)) {
1461 /* not finished yet */
1463 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "(debug) child isn't done yet, pid:", p
->cgi_pid
.ptr
[ndx
]);
1467 if (errno
== ECHILD
) {
1468 /* someone else called waitpid... remove the pid to stop looping the error each time */
1469 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cgi child vanished, probably someone else called waitpid");
1471 cgi_pid_del(srv
, p
, p
->cgi_pid
.ptr
[ndx
]);
1476 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "waitpid failed: ", strerror(errno
));
1478 return HANDLER_ERROR
;
1481 if (WIFEXITED(status
)) {
1483 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "(debug) cgi exited fine, pid:", p
->cgi_pid
.ptr
[ndx
]);
1485 } else if (WIFSIGNALED(status
)) {
1486 /* FIXME: what if we killed the CGI script with a kill(..., SIGTERM) ?
1488 if (WTERMSIG(status
) != SIGTERM
) {
1489 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "cleaning up CGI: process died with signal", WTERMSIG(status
));
1492 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cleaning up CGI: ended unexpectedly");
1495 cgi_pid_del(srv
, p
, p
->cgi_pid
.ptr
[ndx
]);
1496 /* del modified the buffer structure
1497 * and copies the last entry to the current one
1498 * -> recheck the current index
1504 return HANDLER_GO_ON
;
1508 * - HANDLER_GO_ON : not our job
1509 * - HANDLER_FINISHED: got response
1510 * - HANDLER_WAIT_FOR_EVENT: waiting for response
1512 SUBREQUEST_FUNC(mod_cgi_handle_subrequest
) {
1513 plugin_data
*p
= p_d
;
1514 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
1515 chunkqueue
*cq
= con
->request_content_queue
;
1517 if (con
->mode
!= p
->id
) return HANDLER_GO_ON
;
1518 if (NULL
== hctx
) return HANDLER_GO_ON
;
1520 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
1521 && con
->file_started
) {
1522 if (chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
1523 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
1524 } else if (!(fdevent_event_get_interest(srv
->ev
, hctx
->fd
) & FDEVENT_IN
)) {
1525 /* optimistic read from backend, which might re-enable FDEVENT_IN */
1526 handler_t rc
= cgi_recv_response(srv
, hctx
); /*(might invalidate hctx)*/
1527 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
1531 if (cq
->bytes_in
!= (off_t
)con
->request
.content_length
) {
1532 /*(64k - 4k to attempt to avoid temporary files
1533 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
1534 if (cq
->bytes_in
- cq
->bytes_out
> 65536 - 4096
1535 && (con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_BUFMIN
)){
1536 con
->conf
.stream_request_body
&= ~FDEVENT_STREAM_REQUEST_POLLIN
;
1537 if (-1 != hctx
->fd
) return HANDLER_WAIT_FOR_EVENT
;
1539 handler_t r
= connection_handle_read_post_state(srv
, con
);
1540 if (!chunkqueue_is_empty(cq
)) {
1541 if (fdevent_event_get_interest(srv
->ev
, hctx
->fdtocgi
) & FDEVENT_OUT
) {
1542 return (r
== HANDLER_GO_ON
) ? HANDLER_WAIT_FOR_EVENT
: r
;
1545 if (r
!= HANDLER_GO_ON
) return r
;
1549 if (-1 == hctx
->fd
) {
1550 buffer
*handler
= cgi_get_handler(p
->conf
.cgi
, con
->physical
.path
);
1551 if (!handler
) return HANDLER_GO_ON
; /*(should not happen; checked in cgi_is_handled())*/
1552 if (cgi_create_env(srv
, con
, p
, hctx
, handler
)) {
1553 con
->http_status
= 500;
1556 return HANDLER_FINISHED
;
1559 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", "subrequest, pid =", hctx
, hctx
->pid
);
1561 } else if (!chunkqueue_is_empty(con
->request_content_queue
)) {
1562 if (0 != cgi_write_request(srv
, hctx
, hctx
->fdtocgi
)) {
1563 cgi_connection_close(srv
, hctx
);
1564 return HANDLER_ERROR
;
1568 /* if not done, wait for CGI to close stdout, so we read EOF on pipe */
1569 return HANDLER_WAIT_FOR_EVENT
;
1573 int mod_cgi_plugin_init(plugin
*p
);
1574 int mod_cgi_plugin_init(plugin
*p
) {
1575 p
->version
= LIGHTTPD_VERSION_ID
;
1576 p
->name
= buffer_init_string("cgi");
1578 p
->connection_reset
= cgi_connection_close_callback
;
1579 p
->handle_subrequest_start
= cgi_is_handled
;
1580 p
->handle_subrequest
= mod_cgi_handle_subrequest
;
1581 p
->handle_trigger
= cgi_trigger
;
1582 p
->init
= mod_cgi_init
;
1583 p
->cleanup
= mod_cgi_free
;
1584 p
->set_defaults
= mod_fastcgi_set_defaults
;