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_COMEBACK
, 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
<= 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 (con
->http_status
>= 300 && con
->http_status
< 400) {
520 /*(con->parsed_response & HTTP_LOCATION)*/
522 if (NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "Location"))
523 && ds
->value
->ptr
[0] == '/') {
524 if (++con
->loops_per_request
> 5) {
525 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "too many internal loops while processing request:", con
->request
.orig_uri
);
526 con
->http_status
= 500; /* Internal Server Error */
528 return FDEVENT_HANDLED_FINISHED
;
531 if (!buffer_is_equal(con
->request
.uri
, con
->request
.orig_uri
)
532 && !array_get_element(con
->environment
, "REDIRECT_URI")) {
533 array_set_key_value(con
->environment
,
534 CONST_STR_LEN("REDIRECT_URI"),
535 CONST_BUF_LEN(con
->request
.orig_uri
));
538 buffer_copy_buffer(con
->request
.uri
, ds
->value
);
540 if (con
->request
.content_length
) {
541 if ((off_t
)con
->request
.content_length
!= chunkqueue_length(con
->request_content_queue
)) {
544 con
->request
.content_length
= 0;
545 chunkqueue_reset(con
->request_content_queue
);
548 if (con
->http_status
!= 307 && con
->http_status
!= 308) {
549 /* Note: request body (if any) sent to initial dynamic handler
550 * and is not available to the internal redirect */
551 con
->request
.http_method
= HTTP_METHOD_GET
;
554 connection_response_reset(srv
, con
); /*(includes con->http_status = 0)*/
557 return FDEVENT_HANDLED_COMEBACK
;
561 if (p
->conf
.xsendfile_allow
) {
563 if (NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "X-Sendfile"))) {
564 http_response_xsendfile(srv
, con
, ds
->value
, p
->conf
.xsendfile_docroot
);
565 return FDEVENT_HANDLED_FINISHED
;
570 if (0 != http_chunk_append_mem(srv
, con
, bstart
, blen
)) {
571 return FDEVENT_HANDLED_ERROR
;
576 con
->file_started
= 1;
578 /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
579 if (header_len
> MAX_HTTP_REQUEST_HEADER
) {
580 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "response headers too large for", con
->uri
.path
);
581 con
->http_status
= 502; /* Bad Gateway */
583 return FDEVENT_HANDLED_FINISHED
;
587 if (0 != http_chunk_append_buffer(srv
, con
, hctx
->response
)) {
588 return FDEVENT_HANDLED_ERROR
;
590 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
591 && chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
592 if (!con
->is_writable
) {
593 /*(defer removal of FDEVENT_IN interest since
594 * connection_state_machine() might be able to send data
595 * immediately, unless !con->is_writable, where
596 * connection_state_machine() might not loop back to call
597 * mod_cgi_handle_subrequest())*/
598 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
605 log_error_write(srv
, __FILE__
, __LINE__
, "ddss", con
->fd
, hctx
->fd
, connection_get_state(con
->state
), b
->ptr
);
609 return FDEVENT_HANDLED_NOT_FINISHED
;
612 static void cgi_connection_close_fdtocgi(server
*srv
, handler_ctx
*hctx
) {
613 /*(closes only hctx->fdtocgi)*/
614 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx_tocgi
), hctx
->fdtocgi
);
615 fdevent_unregister(srv
->ev
, hctx
->fdtocgi
);
617 if (close(hctx
->fdtocgi
)) {
618 log_error_write(srv
, __FILE__
, __LINE__
, "sds", "cgi stdin close failed ", hctx
->fdtocgi
, strerror(errno
));
623 static void cgi_connection_close(server
*srv
, handler_ctx
*hctx
) {
626 plugin_data
*p
= hctx
->plugin_data
;
627 connection
*con
= hctx
->remote_conn
;
631 /* the connection to the browser went away, but we still have a connection
634 * close cgi-connection
637 if (hctx
->fd
!= -1) {
638 /* close connection to the cgi-script */
639 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
);
640 fdevent_unregister(srv
->ev
, hctx
->fd
);
642 if (close(hctx
->fd
)) {
643 log_error_write(srv
, __FILE__
, __LINE__
, "sds", "cgi close failed ", hctx
->fd
, strerror(errno
));
647 if (hctx
->fdtocgi
!= -1) {
648 cgi_connection_close_fdtocgi(srv
, hctx
); /*(closes only hctx->fdtocgi)*/
653 con
->plugin_ctx
[p
->id
] = NULL
;
655 cgi_handler_ctx_free(hctx
);
657 /* if waitpid hasn't been called by response.c yet, do it here */
659 /* check if the CGI-script is already gone */
660 switch(waitpid(pid
, &status
, WNOHANG
)) {
662 /* not finished yet */
664 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "(debug) child isn't done yet, pid:", pid
);
669 if (errno
== EINTR
) break;
672 * errno == ECHILD happens if _subrequest catches the process-status before
673 * we have read the response of the cgi process
678 * -> we get here with waitpid == ECHILD
681 if (errno
!= ECHILD
) {
682 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "waitpid failed: ", strerror(errno
));
684 /* anyway: don't wait for it anymore */
688 if (WIFEXITED(status
)) {
690 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "(debug) cgi exited fine, pid:", pid
);
693 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "cgi died, pid:", pid
);
703 /* cgi-script is still alive, queue the PID for removal */
704 cgi_pid_add(srv
, p
, pid
);
709 /* finish response (if not already con->file_started, con->file_finished) */
710 if (con
->mode
== p
->id
) {
711 http_response_backend_done(srv
, con
);
715 static handler_t
cgi_connection_close_callback(server
*srv
, connection
*con
, void *p_d
) {
716 plugin_data
*p
= p_d
;
717 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
718 if (hctx
) cgi_connection_close(srv
, hctx
);
720 return HANDLER_GO_ON
;
724 static int cgi_write_request(server
*srv
, handler_ctx
*hctx
, int fd
);
727 static handler_t
cgi_handle_fdevent_send (server
*srv
, void *ctx
, int revents
) {
728 handler_ctx
*hctx
= ctx
;
729 connection
*con
= hctx
->remote_conn
;
731 /*(joblist only actually necessary here in mod_cgi fdevent send if returning HANDLER_ERROR)*/
732 joblist_append(srv
, con
);
734 if (revents
& FDEVENT_OUT
) {
735 if (0 != cgi_write_request(srv
, hctx
, hctx
->fdtocgi
)) {
736 cgi_connection_close(srv
, hctx
);
737 return HANDLER_ERROR
;
739 /* more request body to be sent to CGI */
742 if (revents
& FDEVENT_HUP
) {
743 /* skip sending remaining data to CGI */
744 if (con
->request
.content_length
) {
745 chunkqueue
*cq
= con
->request_content_queue
;
746 chunkqueue_mark_written(cq
, chunkqueue_length(cq
));
747 if (cq
->bytes_in
!= (off_t
)con
->request
.content_length
) {
752 cgi_connection_close_fdtocgi(srv
, hctx
); /*(closes only hctx->fdtocgi)*/
753 } else if (revents
& FDEVENT_ERR
) {
754 /* kill all connections to the cgi process */
756 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cgi-FDEVENT_ERR");
758 cgi_connection_close(srv
, hctx
);
759 return HANDLER_ERROR
;
762 return HANDLER_FINISHED
;
766 static int cgi_recv_response(server
*srv
, handler_ctx
*hctx
) {
767 switch (cgi_demux_response(srv
, hctx
)) {
768 case FDEVENT_HANDLED_NOT_FINISHED
:
770 case FDEVENT_HANDLED_FINISHED
:
774 log_error_write(srv
, __FILE__
, __LINE__
, "ddss", con
->fd
, hctx
->fd
, connection_get_state(con
->state
), "finished");
776 cgi_connection_close(srv
, hctx
);
778 /* if we get a IN|HUP and have read everything don't exec the close twice */
779 return HANDLER_FINISHED
;
780 case FDEVENT_HANDLED_COMEBACK
:
781 cgi_connection_close(srv
, hctx
);
782 return HANDLER_COMEBACK
;
783 case FDEVENT_HANDLED_ERROR
:
784 log_error_write(srv
, __FILE__
, __LINE__
, "s", "demuxer failed: ");
786 cgi_connection_close(srv
, hctx
);
787 return HANDLER_FINISHED
;
790 return HANDLER_GO_ON
;
794 static handler_t
cgi_handle_fdevent(server
*srv
, void *ctx
, int revents
) {
795 handler_ctx
*hctx
= ctx
;
796 connection
*con
= hctx
->remote_conn
;
798 joblist_append(srv
, con
);
800 if (revents
& FDEVENT_IN
) {
801 handler_t rc
= cgi_recv_response(srv
, hctx
);/*(might invalidate hctx)*/
802 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
805 /* perhaps this issue is already handled */
806 if (revents
& FDEVENT_HUP
) {
807 if (con
->file_started
) {
808 /* drain any remaining data from kernel pipe buffers
809 * even if (con->conf.stream_response_body
810 * & FDEVENT_STREAM_RESPONSE_BUFMIN)
811 * since event loop will spin on fd FDEVENT_HUP event
812 * until unregistered. */
815 rc
= cgi_recv_response(srv
,hctx
);/*(might invalidate hctx)*/
816 } while (rc
== HANDLER_GO_ON
); /*(unless HANDLER_GO_ON)*/
817 return rc
; /* HANDLER_FINISHED or HANDLER_COMEBACK or HANDLER_ERROR */
818 } else if (!buffer_string_is_empty(hctx
->response_header
)) {
819 /* unfinished header package which is a body in reality */
820 con
->file_started
= 1;
821 if (0 != http_chunk_append_buffer(srv
, con
, hctx
->response_header
)) {
822 cgi_connection_close(srv
, hctx
);
823 return HANDLER_ERROR
;
827 log_error_write(srv
, __FILE__
, __LINE__
, "sddd", "got HUP from cgi", con
->fd
, hctx
->fd
, revents
);
830 cgi_connection_close(srv
, hctx
);
831 } else if (revents
& FDEVENT_ERR
) {
832 /* kill all connections to the cgi process */
833 cgi_connection_close(srv
, hctx
);
835 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cgi-FDEVENT_ERR");
837 return HANDLER_ERROR
;
840 return HANDLER_FINISHED
;
844 static int cgi_env_add(char_array
*env
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
847 if (!key
|| !val
) return -1;
849 dst
= malloc(key_len
+ val_len
+ 2);
851 memcpy(dst
, key
, key_len
);
853 memcpy(dst
+ key_len
+ 1, val
, val_len
);
854 dst
[key_len
+ 1 + val_len
] = '\0';
856 if (env
->size
== 0) {
858 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
859 force_assert(env
->ptr
);
860 } else if (env
->size
== env
->used
) {
862 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
863 force_assert(env
->ptr
);
866 env
->ptr
[env
->used
++] = dst
;
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
;
882 size_t mmap_offset
, mmap_avail
;
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 */
894 chunkqueue_remove_finished_chunks(cq
);
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
);
917 if (-1 == lseek(c
->file
.fd
, offset
, SEEK_SET
)
918 || 0 >= (toSend
= read(c
->file
.fd
, data
, 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
);
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
);
955 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
956 "write failed:", strerror(errno
), fd
);
962 chunkqueue_mark_written(cq
, 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
;
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
) {
983 r
= cgi_write_file_chunk_mmap(srv
, con
, fd
, cq
);
987 if ((r
= write(fd
, c
->mem
->ptr
+ c
->offset
, buffer_string_length(c
->mem
) - c
->offset
)) < 0) {
991 /* ignore and try again */
996 /* connection closed */
1001 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "write failed due to: ", strerror(errno
));
1006 chunkqueue_mark_written(cq
, r
);
1011 if (0 == r
) break; /*(might block)*/
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
));
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)*/
1033 log_error_write(srv
, __FILE__
, __LINE__
, "sds", "cgi stdin close failed ", fd
, strerror(errno
));
1036 cgi_connection_close_fdtocgi(srv
, hctx
); /*(closes only hctx->fdtocgi)*/
1039 off_t cqlen
= cq
->bytes_in
- cq
->bytes_out
;
1040 if (cq
->bytes_in
< (off_t
)con
->request
.content_length
&& cqlen
< 65536 - 16384) {
1041 /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
1042 if (!(con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_POLLIN
)) {
1043 con
->conf
.stream_request_body
|= FDEVENT_STREAM_REQUEST_POLLIN
;
1044 con
->is_readable
= 1; /* trigger optimistic read from client */
1047 if (-1 == hctx
->fdtocgi
) { /*(not registered yet)*/
1049 hctx
->fde_ndx_tocgi
= -1;
1050 fdevent_register(srv
->ev
, hctx
->fdtocgi
, cgi_handle_fdevent_send
, hctx
);
1052 if (0 == cqlen
) { /*(chunkqueue_is_empty(cq))*/
1053 if ((fdevent_event_get_interest(srv
->ev
, hctx
->fdtocgi
) & FDEVENT_OUT
)) {
1054 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx_tocgi
), hctx
->fdtocgi
, 0);
1057 /* more request body remains to be sent to CGI so register for fdevents */
1058 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx_tocgi
), hctx
->fdtocgi
, FDEVENT_OUT
);
1065 static int cgi_create_env(server
*srv
, connection
*con
, plugin_data
*p
, handler_ctx
*hctx
, buffer
*cgi_handler
) {
1069 char b2
[INET6_ADDRSTRLEN
+ 1];
1073 int from_cgi_fds
[2];
1078 if (!buffer_string_is_empty(cgi_handler
)) {
1079 /* stat the exec file */
1080 if (-1 == (stat(cgi_handler
->ptr
, &st
))) {
1081 log_error_write(srv
, __FILE__
, __LINE__
, "sbss",
1082 "stat for cgi-handler", cgi_handler
,
1083 "failed:", strerror(errno
));
1088 if (pipe(to_cgi_fds
)) {
1089 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "pipe failed:", strerror(errno
));
1093 if (pipe(from_cgi_fds
)) {
1094 close(to_cgi_fds
[0]);
1095 close(to_cgi_fds
[1]);
1096 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "pipe failed:", strerror(errno
));
1101 switch (pid
= fork()) {
1107 char buf
[LI_ITOSTRING_LENGTH
];
1112 server_socket
*srv_sock
= con
->srv_socket
;
1114 /* move stdout to from_cgi_fd[1] */
1115 close(STDOUT_FILENO
);
1116 dup2(from_cgi_fds
[1], STDOUT_FILENO
);
1117 close(from_cgi_fds
[1]);
1119 close(from_cgi_fds
[0]);
1121 /* move the stdin to to_cgi_fd[0] */
1122 close(STDIN_FILENO
);
1123 dup2(to_cgi_fds
[0], STDIN_FILENO
);
1124 close(to_cgi_fds
[0]);
1126 close(to_cgi_fds
[1]);
1128 /* create environment */
1133 cgi_env_add(&env
, CONST_STR_LEN("SERVER_SOFTWARE"), CONST_BUF_LEN(con
->conf
.server_tag
));
1135 if (!buffer_string_is_empty(con
->server_name
)) {
1136 size_t len
= buffer_string_length(con
->server_name
);
1138 if (con
->server_name
->ptr
[0] == '[') {
1139 const char *colon
= strstr(con
->server_name
->ptr
, "]:");
1140 if (colon
) len
= (colon
+ 1) - con
->server_name
->ptr
;
1142 const char *colon
= strchr(con
->server_name
->ptr
, ':');
1143 if (colon
) len
= colon
- con
->server_name
->ptr
;
1146 cgi_env_add(&env
, CONST_STR_LEN("SERVER_NAME"), con
->server_name
->ptr
, len
);
1150 srv_sock
->addr
.plain
.sa_family
,
1151 srv_sock
->addr
.plain
.sa_family
== AF_INET6
?
1152 (const void *) &(srv_sock
->addr
.ipv6
.sin6_addr
) :
1153 (const void *) &(srv_sock
->addr
.ipv4
.sin_addr
),
1156 s
= inet_ntoa(srv_sock
->addr
.ipv4
.sin_addr
);
1159 cgi_env_add(&env
, CONST_STR_LEN("SERVER_NAME"), s
, strlen(s
));
1161 cgi_env_add(&env
, CONST_STR_LEN("GATEWAY_INTERFACE"), CONST_STR_LEN("CGI/1.1"));
1163 s
= get_http_version_name(con
->request
.http_version
);
1165 cgi_env_add(&env
, CONST_STR_LEN("SERVER_PROTOCOL"), s
, strlen(s
));
1167 li_utostrn(buf
, sizeof(buf
),
1169 ntohs(srv_sock
->addr
.plain
.sa_family
== AF_INET6
? srv_sock
->addr
.ipv6
.sin6_port
: srv_sock
->addr
.ipv4
.sin_port
)
1171 ntohs(srv_sock
->addr
.ipv4
.sin_port
)
1174 cgi_env_add(&env
, CONST_STR_LEN("SERVER_PORT"), buf
, strlen(buf
));
1176 switch (srv_sock
->addr
.plain
.sa_family
) {
1180 srv_sock
->addr
.plain
.sa_family
,
1181 (const void *) &(srv_sock
->addr
.ipv6
.sin6_addr
),
1186 srv_sock
->addr
.plain
.sa_family
,
1187 (const void *) &(srv_sock
->addr
.ipv4
.sin_addr
),
1192 s
= inet_ntoa(srv_sock
->addr
.ipv4
.sin_addr
);
1200 cgi_env_add(&env
, CONST_STR_LEN("SERVER_ADDR"), s
, strlen(s
));
1202 s
= get_http_method_name(con
->request
.http_method
);
1204 cgi_env_add(&env
, CONST_STR_LEN("REQUEST_METHOD"), s
, strlen(s
));
1206 if (!buffer_string_is_empty(con
->request
.pathinfo
)) {
1207 cgi_env_add(&env
, CONST_STR_LEN("PATH_INFO"), CONST_BUF_LEN(con
->request
.pathinfo
));
1209 if (!buffer_string_is_empty(con
->uri
.query
)) {
1210 cgi_env_add(&env
, CONST_STR_LEN("QUERY_STRING"), CONST_BUF_LEN(con
->uri
.query
));
1212 cgi_env_add(&env
, CONST_STR_LEN("QUERY_STRING"), CONST_STR_LEN(""));
1214 if (con
->error_handler_saved_status
>= 0) {
1215 cgi_env_add(&env
, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(con
->request
.uri
));
1217 cgi_env_add(&env
, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(con
->request
.orig_uri
));
1219 /* set REDIRECT_STATUS for php compiled with --force-redirect
1220 * (if REDIRECT_STATUS has not already been set by error handler) */
1221 if (0 == con
->error_handler_saved_status
) {
1222 cgi_env_add(&env
, CONST_STR_LEN("REDIRECT_STATUS"), CONST_STR_LEN("200"));
1226 switch (con
->dst_addr
.plain
.sa_family
) {
1230 con
->dst_addr
.plain
.sa_family
,
1231 (const void *) &(con
->dst_addr
.ipv6
.sin6_addr
),
1236 con
->dst_addr
.plain
.sa_family
,
1237 (const void *) &(con
->dst_addr
.ipv4
.sin_addr
),
1242 s
= inet_ntoa(con
->dst_addr
.ipv4
.sin_addr
);
1250 cgi_env_add(&env
, CONST_STR_LEN("REMOTE_ADDR"), s
, strlen(s
));
1252 li_utostrn(buf
, sizeof(buf
),
1254 ntohs(con
->dst_addr
.plain
.sa_family
== AF_INET6
? con
->dst_addr
.ipv6
.sin6_port
: con
->dst_addr
.ipv4
.sin_port
)
1256 ntohs(con
->dst_addr
.ipv4
.sin_port
)
1259 cgi_env_add(&env
, CONST_STR_LEN("REMOTE_PORT"), buf
, strlen(buf
));
1261 if (buffer_is_equal_caseless_string(con
->uri
.scheme
, CONST_STR_LEN("https"))) {
1262 cgi_env_add(&env
, CONST_STR_LEN("HTTPS"), CONST_STR_LEN("on"));
1265 li_itostrn(buf
, sizeof(buf
), con
->request
.content_length
);
1266 cgi_env_add(&env
, CONST_STR_LEN("CONTENT_LENGTH"), buf
, strlen(buf
));
1267 cgi_env_add(&env
, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(con
->physical
.path
));
1268 cgi_env_add(&env
, CONST_STR_LEN("SCRIPT_NAME"), CONST_BUF_LEN(con
->uri
.path
));
1269 cgi_env_add(&env
, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(con
->physical
.basedir
));
1272 if (NULL
!= (s
= getenv("LD_PRELOAD"))) {
1273 cgi_env_add(&env
, CONST_STR_LEN("LD_PRELOAD"), s
, strlen(s
));
1276 if (NULL
!= (s
= getenv("LD_LIBRARY_PATH"))) {
1277 cgi_env_add(&env
, CONST_STR_LEN("LD_LIBRARY_PATH"), s
, strlen(s
));
1280 /* CYGWIN needs SYSTEMROOT */
1281 if (NULL
!= (s
= getenv("SYSTEMROOT"))) {
1282 cgi_env_add(&env
, CONST_STR_LEN("SYSTEMROOT"), s
, strlen(s
));
1286 for (n
= 0; n
< con
->request
.headers
->used
; n
++) {
1289 ds
= (data_string
*)con
->request
.headers
->data
[n
];
1291 if (!buffer_is_empty(ds
->value
) && !buffer_is_empty(ds
->key
)) {
1292 /* Do not emit HTTP_PROXY in environment.
1293 * Some executables use HTTP_PROXY to configure
1294 * outgoing proxy. See also https://httpoxy.org/ */
1295 if (buffer_is_equal_caseless_string(ds
->key
, CONST_STR_LEN("Proxy"))) {
1299 buffer_copy_string_encoded_cgi_varnames(p
->tmp_buf
, CONST_BUF_LEN(ds
->key
), 1);
1301 cgi_env_add(&env
, CONST_BUF_LEN(p
->tmp_buf
), CONST_BUF_LEN(ds
->value
));
1305 for (n
= 0; n
< con
->environment
->used
; n
++) {
1308 ds
= (data_string
*)con
->environment
->data
[n
];
1310 if (!buffer_is_empty(ds
->value
) && !buffer_is_empty(ds
->key
)) {
1311 buffer_copy_string_encoded_cgi_varnames(p
->tmp_buf
, CONST_BUF_LEN(ds
->key
), 0);
1313 cgi_env_add(&env
, CONST_BUF_LEN(p
->tmp_buf
), CONST_BUF_LEN(ds
->value
));
1317 if (env
.size
== env
.used
) {
1319 env
.ptr
= realloc(env
.ptr
, env
.size
* sizeof(*env
.ptr
));
1322 env
.ptr
[env
.used
] = NULL
;
1326 args
= malloc(sizeof(*args
) * argc
);
1330 if (!buffer_string_is_empty(cgi_handler
)) {
1331 args
[i
++] = cgi_handler
->ptr
;
1333 args
[i
++] = con
->physical
.path
->ptr
;
1336 /* search for the last / */
1337 if (NULL
!= (c
= strrchr(con
->physical
.path
->ptr
, '/'))) {
1338 /* handle special case of file in root directory */
1339 const char* physdir
= (c
== con
->physical
.path
->ptr
) ? "/" : con
->physical
.path
->ptr
;
1341 /* temporarily shorten con->physical.path to directory without terminating '/' */
1343 /* change to the physical directory */
1344 if (-1 == chdir(physdir
)) {
1345 log_error_write(srv
, __FILE__
, __LINE__
, "ssb", "chdir failed:", strerror(errno
), con
->physical
.path
);
1350 /* we don't need the client socket */
1351 for (i
= 3; i
< 256; i
++) {
1352 if (i
!= srv
->errorlog_fd
) close(i
);
1356 execve(args
[0], args
, env
.ptr
);
1358 /* most log files may have been closed/redirected by this point,
1359 * though stderr might still point to lighttpd.breakage.log */
1365 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fork failed:", strerror(errno
));
1366 close(from_cgi_fds
[0]);
1367 close(from_cgi_fds
[1]);
1368 close(to_cgi_fds
[0]);
1369 close(to_cgi_fds
[1]);
1372 /* parent process */
1374 close(from_cgi_fds
[1]);
1375 close(to_cgi_fds
[0]);
1377 /* register PID and wait for them asynchronously */
1380 hctx
->fd
= from_cgi_fds
[0];
1383 if (0 == con
->request
.content_length
) {
1384 close(to_cgi_fds
[1]);
1386 /* there is content to send */
1387 if (-1 == fdevent_fcntl_set(srv
->ev
, to_cgi_fds
[1])) {
1388 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fcntl failed: ", strerror(errno
));
1389 close(to_cgi_fds
[1]);
1390 cgi_connection_close(srv
, hctx
);
1394 if (0 != cgi_write_request(srv
, hctx
, to_cgi_fds
[1])) {
1395 close(to_cgi_fds
[1]);
1396 cgi_connection_close(srv
, hctx
);
1401 fdevent_register(srv
->ev
, hctx
->fd
, cgi_handle_fdevent
, hctx
);
1402 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
1404 if (-1 == fdevent_fcntl_set(srv
->ev
, hctx
->fd
)) {
1405 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fcntl failed: ", strerror(errno
));
1406 cgi_connection_close(srv
, hctx
);
1420 static buffer
* cgi_get_handler(array
*a
, buffer
*fn
) {
1421 size_t k
, s_len
= buffer_string_length(fn
);
1422 for (k
= 0; k
< a
->used
; ++k
) {
1423 data_string
*ds
= (data_string
*)a
->data
[k
];
1424 size_t ct_len
= buffer_string_length(ds
->key
);
1426 if (buffer_is_empty(ds
->key
)) continue;
1427 if (s_len
< ct_len
) continue;
1429 if (0 == strncmp(fn
->ptr
+ s_len
- ct_len
, ds
->key
->ptr
, ct_len
)) {
1439 static int mod_cgi_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
1441 plugin_config
*s
= p
->config_storage
[0];
1444 PATCH(execute_x_only
);
1445 PATCH(xsendfile_allow
);
1446 PATCH(xsendfile_docroot
);
1448 /* skip the first, the global context */
1449 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
1450 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
1451 s
= p
->config_storage
[i
];
1453 /* condition didn't match */
1454 if (!config_check_cond(srv
, con
, dc
)) continue;
1457 for (j
= 0; j
< dc
->value
->used
; j
++) {
1458 data_unset
*du
= dc
->value
->data
[j
];
1460 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cgi.assign"))) {
1462 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cgi.execute-x-only"))) {
1463 PATCH(execute_x_only
);
1464 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cgi.x-sendfile"))) {
1465 PATCH(xsendfile_allow
);
1466 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("cgi.x-sendfile-docroot"))) {
1467 PATCH(xsendfile_docroot
);
1476 URIHANDLER_FUNC(cgi_is_handled
) {
1477 plugin_data
*p
= p_d
;
1478 buffer
*fn
= con
->physical
.path
;
1479 stat_cache_entry
*sce
= NULL
;
1481 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
1483 if (buffer_is_empty(fn
)) return HANDLER_GO_ON
;
1485 mod_cgi_patch_connection(srv
, con
, p
);
1487 if (HANDLER_ERROR
== stat_cache_get_entry(srv
, con
, con
->physical
.path
, &sce
)) return HANDLER_GO_ON
;
1488 if (!S_ISREG(sce
->st
.st_mode
)) return HANDLER_GO_ON
;
1489 if (p
->conf
.execute_x_only
== 1 && (sce
->st
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0) return HANDLER_GO_ON
;
1491 if (NULL
!= cgi_get_handler(p
->conf
.cgi
, fn
)) {
1492 handler_ctx
*hctx
= cgi_handler_ctx_init();
1493 hctx
->remote_conn
= con
;
1494 hctx
->plugin_data
= p
;
1495 con
->plugin_ctx
[p
->id
] = hctx
;
1499 return HANDLER_GO_ON
;
1502 TRIGGER_FUNC(cgi_trigger
) {
1503 plugin_data
*p
= p_d
;
1505 /* the trigger handle only cares about lonely PID which we have to wait for */
1508 for (ndx
= 0; ndx
< p
->cgi_pid
.used
; ndx
++) {
1511 switch(waitpid(p
->cgi_pid
.ptr
[ndx
], &status
, WNOHANG
)) {
1513 /* not finished yet */
1515 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "(debug) child isn't done yet, pid:", p
->cgi_pid
.ptr
[ndx
]);
1519 if (errno
== ECHILD
) {
1520 /* someone else called waitpid... remove the pid to stop looping the error each time */
1521 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cgi child vanished, probably someone else called waitpid");
1523 cgi_pid_del(srv
, p
, p
->cgi_pid
.ptr
[ndx
]);
1528 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "waitpid failed: ", strerror(errno
));
1530 return HANDLER_ERROR
;
1533 if (WIFEXITED(status
)) {
1535 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "(debug) cgi exited fine, pid:", p
->cgi_pid
.ptr
[ndx
]);
1537 } else if (WIFSIGNALED(status
)) {
1538 /* FIXME: what if we killed the CGI script with a kill(..., SIGTERM) ?
1540 if (WTERMSIG(status
) != SIGTERM
) {
1541 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "cleaning up CGI: process died with signal", WTERMSIG(status
));
1544 log_error_write(srv
, __FILE__
, __LINE__
, "s", "cleaning up CGI: ended unexpectedly");
1547 cgi_pid_del(srv
, p
, p
->cgi_pid
.ptr
[ndx
]);
1548 /* del modified the buffer structure
1549 * and copies the last entry to the current one
1550 * -> recheck the current index
1556 return HANDLER_GO_ON
;
1560 * - HANDLER_GO_ON : not our job
1561 * - HANDLER_FINISHED: got response
1562 * - HANDLER_WAIT_FOR_EVENT: waiting for response
1564 SUBREQUEST_FUNC(mod_cgi_handle_subrequest
) {
1565 plugin_data
*p
= p_d
;
1566 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
1567 chunkqueue
*cq
= con
->request_content_queue
;
1569 if (con
->mode
!= p
->id
) return HANDLER_GO_ON
;
1570 if (NULL
== hctx
) return HANDLER_GO_ON
;
1572 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
1573 && con
->file_started
) {
1574 if (chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
1575 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
1576 } else if (!(fdevent_event_get_interest(srv
->ev
, hctx
->fd
) & FDEVENT_IN
)) {
1577 /* optimistic read from backend, which might re-enable FDEVENT_IN */
1578 handler_t rc
= cgi_recv_response(srv
, hctx
); /*(might invalidate hctx)*/
1579 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
1583 if (cq
->bytes_in
!= (off_t
)con
->request
.content_length
) {
1584 /*(64k - 4k to attempt to avoid temporary files
1585 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
1586 if (cq
->bytes_in
- cq
->bytes_out
> 65536 - 4096
1587 && (con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_BUFMIN
)){
1588 con
->conf
.stream_request_body
&= ~FDEVENT_STREAM_REQUEST_POLLIN
;
1589 if (-1 != hctx
->fd
) return HANDLER_WAIT_FOR_EVENT
;
1591 handler_t r
= connection_handle_read_post_state(srv
, con
);
1592 if (!chunkqueue_is_empty(cq
)) {
1593 if (fdevent_event_get_interest(srv
->ev
, hctx
->fdtocgi
) & FDEVENT_OUT
) {
1594 return (r
== HANDLER_GO_ON
) ? HANDLER_WAIT_FOR_EVENT
: r
;
1597 if (r
!= HANDLER_GO_ON
) return r
;
1601 if (-1 == hctx
->fd
) {
1602 buffer
*handler
= cgi_get_handler(p
->conf
.cgi
, con
->physical
.path
);
1603 if (!handler
) return HANDLER_GO_ON
; /*(should not happen; checked in cgi_is_handled())*/
1604 if (cgi_create_env(srv
, con
, p
, hctx
, handler
)) {
1605 con
->http_status
= 500;
1608 return HANDLER_FINISHED
;
1611 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", "subrequest, pid =", hctx
, hctx
->pid
);
1613 } else if (!chunkqueue_is_empty(con
->request_content_queue
)) {
1614 if (0 != cgi_write_request(srv
, hctx
, hctx
->fdtocgi
)) {
1615 cgi_connection_close(srv
, hctx
);
1616 return HANDLER_ERROR
;
1620 /* if not done, wait for CGI to close stdout, so we read EOF on pipe */
1621 return HANDLER_WAIT_FOR_EVENT
;
1625 int mod_cgi_plugin_init(plugin
*p
);
1626 int mod_cgi_plugin_init(plugin
*p
) {
1627 p
->version
= LIGHTTPD_VERSION_ID
;
1628 p
->name
= buffer_init_string("cgi");
1630 p
->connection_reset
= cgi_connection_close_callback
;
1631 p
->handle_subrequest_start
= cgi_is_handled
;
1632 p
->handle_subrequest
= mod_cgi_handle_subrequest
;
1633 p
->handle_trigger
= cgi_trigger
;
1634 p
->init
= mod_cgi_init
;
1635 p
->cleanup
= mod_cgi_free
;
1636 p
->set_defaults
= mod_fastcgi_set_defaults
;