Added support for the -flush argument to FastCgiConfig Eric Sit [esit@alum.mit.edu]
[mod_fastcgi.git] / mod_fastcgi.c
blob0f198b383130685d0f5e3796662712ddfb15824d
1 /*
2 * mod_fastcgi.c --
4 * Apache server module for FastCGI.
6 * $Id: mod_fastcgi.c,v 1.116 2001/05/29 15:22:13 robs Exp $
8 * Copyright (c) 1995-1996 Open Market, Inc.
10 * See the file "LICENSE.TERMS" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 * Patches for Apache-1.1 provided by
15 * Ralf S. Engelschall
16 * <rse@en.muc.de>
18 * Patches for Linux provided by
19 * Scott Langley
20 * <langles@vote-smart.org>
22 * Patches for suexec handling by
23 * Brian Grossman <brian@SoftHome.net> and
24 * Rob Saccoccio <robs@ipass.net>
28 * Module design notes.
30 * 1. Restart cleanup.
32 * mod_fastcgi spawns several processes: one process manager process
33 * and several application processes. None of these processes
34 * handle SIGHUP, so they just go away when the Web server performs
35 * a restart (as Apache does every time it starts.)
37 * In order to allow the process manager to properly cleanup the
38 * running fastcgi processes (without being disturbed by Apache),
39 * an intermediate process was introduced. The diagram is as follows;
41 * ApacheWS --> MiddleProc --> ProcMgr --> FCGI processes
43 * On a restart, ApacheWS sends a SIGKILL to MiddleProc and then
44 * collects it via waitpid(). The ProcMgr periodically checks for
45 * its parent (via getppid()) and if it does not have one, as in
46 * case when MiddleProc has terminated, ProcMgr issues a SIGTERM
47 * to all FCGI processes, waitpid()s on them and then exits, so it
48 * can be collected by init(1). Doing it any other way (short of
49 * changing Apache API), results either in inconsistent results or
50 * in generation of zombie processes.
52 * XXX: How does Apache 1.2 implement "gentle" restart
53 * that does not disrupt current connections? How does
54 * gentle restart interact with restart cleanup?
56 * 2. Request timeouts.
58 * Earlier versions of this module used ap_soft_timeout() rather than
59 * ap_hard_timeout() and ate FastCGI server output until it completed.
60 * This precluded the FastCGI server from having to implement a
61 * SIGPIPE handler, but meant hanging the application longer than
62 * necessary. SIGPIPE handler now must be installed in ALL FastCGI
63 * applications. The handler should abort further processing and go
64 * back into the accept() loop.
66 * Although using ap_soft_timeout() is better than ap_hard_timeout()
67 * we have to be more careful about SIGINT handling and subsequent
68 * processing, so, for now, make it hard.
72 #include "fcgi.h"
74 #ifndef timersub
75 #define timersub(a, b, result) \
76 do { \
77 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
78 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
79 if ((result)->tv_usec < 0) { \
80 --(result)->tv_sec; \
81 (result)->tv_usec += 1000000; \
82 } \
83 } while (0)
84 #endif
87 * Global variables
90 pool *fcgi_config_pool; /* the config pool */
91 server_rec *fcgi_apache_main_server;
93 const char *fcgi_wrapper = NULL; /* wrapper path */
94 uid_t fcgi_user_id; /* the run uid of Apache & PM */
95 gid_t fcgi_group_id; /* the run gid of Apache & PM */
97 fcgi_server *fcgi_servers = NULL; /* AppClasses */
99 char *fcgi_socket_dir = DEFAULT_SOCK_DIR; /* default FastCgiIpcDir */
101 char *fcgi_dynamic_dir = NULL; /* directory for the dynamic
102 * fastcgi apps' sockets */
104 #ifdef WIN32
106 #pragma warning( disable : 4706 4100 4127)
107 fcgi_pm_job *fcgi_dynamic_mbox = NULL;
108 HANDLE *fcgi_dynamic_mbox_mutex = NULL;
109 HANDLE fcgi_pm_thread = INVALID_HANDLE_VALUE;
111 #else
113 int fcgi_pm_pipe[2] = { -1, -1 };
114 pid_t fcgi_pm_pid = -1;
116 #endif
118 char *fcgi_empty_env = NULL;
120 u_int dynamicMaxProcs = FCGI_DEFAULT_MAX_PROCS;
121 int dynamicMinProcs = FCGI_DEFAULT_MIN_PROCS;
122 int dynamicMaxClassProcs = FCGI_DEFAULT_MAX_CLASS_PROCS;
123 u_int dynamicKillInterval = FCGI_DEFAULT_KILL_INTERVAL;
124 u_int dynamicUpdateInterval = FCGI_DEFAULT_UPDATE_INTERVAL;
125 float dynamicGain = FCGI_DEFAULT_GAIN;
126 int dynamicThreshold1 = FCGI_DEFAULT_THRESHOLD_1;
127 int dynamicThresholdN = FCGI_DEFAULT_THRESHOLD_N;
128 u_int dynamicPleaseStartDelay = FCGI_DEFAULT_START_PROCESS_DELAY;
129 u_int dynamicAppConnectTimeout = FCGI_DEFAULT_APP_CONN_TIMEOUT;
130 char **dynamicEnvp = &fcgi_empty_env;
131 u_int dynamicProcessSlack = FCGI_DEFAULT_PROCESS_SLACK;
132 int dynamicAutoRestart = FCGI_DEFAULT_RESTART_DYNAMIC;
133 int dynamicAutoUpdate = FCGI_DEFAULT_AUTOUPDATE;
134 int dynamicFlush = FCGI_FLUSH;
135 u_int dynamicListenQueueDepth = FCGI_DEFAULT_LISTEN_Q;
136 u_int dynamicInitStartDelay = DEFAULT_INIT_START_DELAY;
137 u_int dynamicRestartDelay = FCGI_DEFAULT_RESTART_DELAY;
138 array_header *dynamic_pass_headers = NULL;
139 u_int dynamic_idle_timeout = FCGI_DEFAULT_IDLE_TIMEOUT;
141 /*******************************************************************************
142 * Construct a message and write it to the pm_pipe.
144 static void send_to_pm(const char id, const char * const fs_path,
145 const char *user, const char * const group, const unsigned long q_usec,
146 const unsigned long req_usec)
148 #ifdef WIN32
149 fcgi_pm_job *job = NULL;
151 if (!(job = (fcgi_pm_job *) malloc(sizeof(fcgi_pm_job))))
152 return;
153 #else
154 int buflen = 0;
155 char buf[FCGI_MAX_MSG_LEN];
156 #endif
158 if (strlen(fs_path) > FCGI_MAXPATH) {
159 ap_log_error(FCGI_LOG_ERR_NOERRNO, fcgi_apache_main_server,
160 "FastCGI: the path \"%s\" is too long (>%d) for a dynamic server", fs_path, FCGI_MAXPATH);
161 return;
164 switch(id) {
166 case FCGI_SERVER_START_JOB:
167 #ifdef WIN32
168 job->id = id;
169 job->fs_path = strdup(fs_path);
170 job->user = strdup(user);
171 job->group = strdup(group);
172 job->qsec = 0L;
173 job->start_time = 0L;
174 #else
175 buflen = sprintf(buf, "%c %s %s %s*", id, fs_path, user, group);
176 #endif
177 break;
179 case FCGI_REQUEST_TIMEOUT_JOB:
180 #ifdef WIN32
181 job->id = id;
182 job->fs_path = strdup(fs_path);
183 job->user = strdup(user);
184 job->group = strdup(group);
185 job->qsec = 0L;
186 job->start_time = 0L;
187 #else
188 buflen = sprintf(buf, "%c %s %s %s*", id, fs_path, user, group);
189 #endif
190 break;
192 case FCGI_REQUEST_COMPLETE_JOB:
193 #ifdef WIN32
194 job->id = id;
195 job->fs_path = strdup(fs_path);
196 job->qsec = q_usec;
197 job->start_time = req_usec;
198 job->user = strdup(user);
199 job->group = strdup(group);
200 #else
201 buflen = sprintf(buf, "%c %s %s %s %lu %lu*", id, fs_path, user, group, q_usec, req_usec);
202 #endif
203 break;
206 #ifdef WIN32
207 if (fcgi_pm_add_job(job) == 0)
208 return;
210 SetEvent(fcgi_event_handles[MBOX_EVENT]);
211 #else
212 ap_assert(buflen <= FCGI_MAX_MSG_LEN);
214 if (write(fcgi_pm_pipe[1], (const void *)buf, buflen) != buflen) {
215 ap_log_error(FCGI_LOG_WARN, fcgi_apache_main_server,
216 "FastCGI: write() to PM failed");
218 #endif
222 *----------------------------------------------------------------------
224 * init_module
226 * An Apache module initializer, called by the Apache core
227 * after reading the server config.
229 * Start the process manager no matter what, since there may be a
230 * request for dynamic FastCGI applications without any being
231 * configured as static applications. Also, check for the existence
232 * and create if necessary a subdirectory into which all dynamic
233 * sockets will go.
235 *----------------------------------------------------------------------
237 static void init_module(server_rec *s, pool *p)
239 const char *err;
241 /* Register to reset to default values when the config pool is cleaned */
242 ap_block_alarms();
243 ap_register_cleanup(p, NULL, fcgi_config_reset_globals, ap_null_cleanup);
244 ap_unblock_alarms();
246 ap_add_version_component("mod_fastcgi/" MOD_FASTCGI_VERSION);
248 fcgi_config_set_fcgi_uid_n_gid(1);
250 /* keep these handy */
251 fcgi_config_pool = p;
252 fcgi_apache_main_server = s;
254 #ifndef WIN32
255 /* Create Unix/Domain socket directory */
256 if ((err = fcgi_config_make_dir(p, fcgi_socket_dir)))
257 ap_log_error(FCGI_LOG_ERR, s, "FastCGI: %s", err);
258 #endif
260 /* Create Dynamic directory */
261 if ((err = fcgi_config_make_dynamic_dir(p, 1)))
262 ap_log_error(FCGI_LOG_ERR, s, "FastCGI: %s", err);
264 #ifndef WIN32
265 /* Spawn the PM only once. Under Unix, Apache calls init() routines
266 * twice, once before detach() and once after. Win32 doesn't detach.
267 * Under DSO, DSO modules are unloaded between the two init() calls.
268 * Under Unix, the -X switch causes two calls to init() but no detach
269 * (but all subprocesses are wacked so the PM is toasted anyway)! */
271 if (ap_standalone && ap_restart_time == 0)
272 return;
274 /* Create the pipe for comm with the PM */
275 if (pipe(fcgi_pm_pipe) < 0) {
276 ap_log_error(FCGI_LOG_ERR, s, "FastCGI: pipe() failed");
279 /* Start the Process Manager */
280 fcgi_pm_pid = ap_spawn_child(p, fcgi_pm_main, NULL, kill_only_once, NULL, NULL, NULL);
281 if (fcgi_pm_pid <= 0) {
282 ap_log_error(FCGI_LOG_ALERT, s,
283 "FastCGI: can't start the process manager, spawn_child() failed");
286 close(fcgi_pm_pipe[0]);
287 #endif
290 static void fcgi_child_init(server_rec *dc0, pool *dc1)
292 #ifdef WIN32
293 /* Create the Event Handlers */
294 fcgi_event_handles[0] = CreateEvent(NULL, FALSE, FALSE, NULL);
295 fcgi_event_handles[1] = CreateEvent(NULL, FALSE, FALSE, NULL);
296 fcgi_event_handles[2] = CreateEvent(NULL, FALSE, FALSE, NULL);
297 fcgi_dynamic_mbox_mutex = ap_create_mutex("fcgi_dynamic_mbox_mutex");
299 /* Spawn of the process manager thread */
300 fcgi_pm_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)fcgi_pm_main, NULL, 0, NULL);
301 #endif
302 return;
305 static void fcgi_child_exit(server_rec *dc0, pool *dc1)
307 #ifdef WIN32
308 /* Signaling the PM thread tp exit*/
309 SetEvent(fcgi_event_handles[TERM_EVENT]);
311 /* Waiting on pm thread to exit */
312 WaitForSingleObject(fcgi_pm_thread, INFINITE);
313 #endif
315 return;
319 *----------------------------------------------------------------------
321 * get_header_line --
323 * Terminate a line: scan to the next newline, scan back to the
324 * first non-space character and store a terminating zero. Return
325 * the next character past the end of the newline.
327 * If the end of the string is reached, ASSERT!
329 * If the FIRST character(s) in the line are '\n' or "\r\n", the
330 * first character is replaced with a NULL and next character
331 * past the newline is returned. NOTE: this condition supercedes
332 * the processing of RFC-822 continuation lines.
334 * If continuation is set to 'TRUE', then it parses a (possible)
335 * sequence of RFC-822 continuation lines.
337 * Results:
338 * As above.
340 * Side effects:
341 * Termination byte stored in string.
343 *----------------------------------------------------------------------
345 static char *get_header_line(char *start, int continuation)
347 char *p = start;
348 char *end = start;
350 if(p[0] == '\r' && p[1] == '\n') { /* If EOL in 1st 2 chars */
351 p++; /* point to \n and stop */
352 } else if(*p != '\n') {
353 if(continuation) {
354 while(*p != '\0') {
355 if(*p == '\n' && p[1] != ' ' && p[1] != '\t')
356 break;
357 p++;
359 } else {
360 while(*p != '\0' && *p != '\n') {
361 p++;
366 ap_assert(*p != '\0');
367 end = p;
368 end++;
371 * Trim any trailing whitespace.
373 while(isspace((unsigned char)p[-1]) && p > start) {
374 p--;
377 *p = '\0';
378 return end;
382 *----------------------------------------------------------------------
384 * process_headers --
386 * Call with r->parseHeader == SCAN_CGI_READING_HEADERS
387 * and initial script output in fr->header.
389 * If the initial script output does not include the header
390 * terminator ("\r\n\r\n") process_headers returns with no side
391 * effects, to be called again when more script output
392 * has been appended to fr->header.
394 * If the initial script output includes the header terminator,
395 * process_headers parses the headers and determines whether or
396 * not the remaining script output will be sent to the client.
397 * If so, process_headers sends the HTTP response headers to the
398 * client and copies any non-header script output to the output
399 * buffer reqOutbuf.
401 * Results:
402 * none.
404 * Side effects:
405 * May set r->parseHeader to:
406 * SCAN_CGI_FINISHED -- headers parsed, returning script response
407 * SCAN_CGI_BAD_HEADER -- malformed header from script
408 * SCAN_CGI_INT_REDIRECT -- handler should perform internal redirect
409 * SCAN_CGI_SRV_REDIRECT -- handler should return REDIRECT
411 *----------------------------------------------------------------------
414 static const char *process_headers(request_rec *r, fcgi_request *fr)
416 char *p, *next, *name, *value;
417 int len, flag;
418 int hasContentType, hasStatus, hasLocation;
420 ap_assert(fr->parseHeader == SCAN_CGI_READING_HEADERS);
422 if (fr->header == NULL)
423 return NULL;
426 * Do we have the entire header? Scan for the blank line that
427 * terminates the header.
429 p = (char *)fr->header->elts;
430 len = fr->header->nelts;
431 flag = 0;
432 while(len-- && flag < 2) {
433 switch(*p) {
434 case '\r':
435 break;
436 case '\n':
437 flag++;
438 break;
439 case '\0':
440 case '\v':
441 case '\f':
442 name = "Invalid Character";
443 goto BadHeader;
444 break;
445 default:
446 flag = 0;
447 break;
449 p++;
452 /* Return (to be called later when we have more data)
453 * if we don't have an entire header. */
454 if (flag < 2)
455 return NULL;
458 * Parse all the headers.
460 fr->parseHeader = SCAN_CGI_FINISHED;
461 hasContentType = hasStatus = hasLocation = FALSE;
462 next = (char *)fr->header->elts;
463 for(;;) {
464 next = get_header_line(name = next, TRUE);
465 if (*name == '\0') {
466 break;
468 if ((p = strchr(name, ':')) == NULL) {
469 goto BadHeader;
471 value = p + 1;
472 while (p != name && isspace((unsigned char)*(p - 1))) {
473 p--;
475 if (p == name) {
476 goto BadHeader;
478 *p = '\0';
479 if (strpbrk(name, " \t") != NULL) {
480 *p = ' ';
481 goto BadHeader;
483 while (isspace((unsigned char)*value)) {
484 value++;
487 if (strcasecmp(name, "Status") == 0) {
488 int statusValue = strtol(value, NULL, 10);
490 if (hasStatus) {
491 goto DuplicateNotAllowed;
493 if (statusValue < 0) {
494 fr->parseHeader = SCAN_CGI_BAD_HEADER;
495 return ap_psprintf(r->pool, "invalid Status '%s'", value);
497 hasStatus = TRUE;
498 r->status = statusValue;
499 r->status_line = ap_pstrdup(r->pool, value);
500 continue;
503 if (fr->role == FCGI_RESPONDER) {
504 if (strcasecmp(name, "Content-type") == 0) {
505 if (hasContentType) {
506 goto DuplicateNotAllowed;
508 hasContentType = TRUE;
509 r->content_type = ap_pstrdup(r->pool, value);
510 continue;
513 if (strcasecmp(name, "Location") == 0) {
514 if (hasLocation) {
515 goto DuplicateNotAllowed;
517 hasLocation = TRUE;
518 ap_table_set(r->headers_out, "Location", value);
519 continue;
522 /* If the script wants them merged, it can do it */
523 ap_table_add(r->err_headers_out, name, value);
524 continue;
526 else {
527 ap_table_add(fr->authHeaders, name, value);
531 if (fr->role != FCGI_RESPONDER)
532 return NULL;
535 * Who responds, this handler or Apache?
537 if (hasLocation) {
538 const char *location = ap_table_get(r->headers_out, "Location");
540 * Based on internal redirect handling in mod_cgi.c...
542 * If a script wants to produce its own Redirect
543 * body, it now has to explicitly *say* "Status: 302"
545 if (r->status == 200) {
546 if(location[0] == '/') {
548 * Location is an relative path. This handler will
549 * consume all script output, then have Apache perform an
550 * internal redirect.
552 fr->parseHeader = SCAN_CGI_INT_REDIRECT;
553 return NULL;
554 } else {
556 * Location is an absolute URL. If the script didn't
557 * produce a Content-type header, this handler will
558 * consume all script output and then have Apache generate
559 * its standard redirect response. Otherwise this handler
560 * will transmit the script's response.
562 fr->parseHeader = SCAN_CGI_SRV_REDIRECT;
563 return NULL;
568 * We're responding. Send headers, buffer excess script output.
570 ap_send_http_header(r);
572 /* We need to reinstate our timeout, send_http_header() kill()s it */
573 ap_hard_timeout("FastCGI request processing", r);
575 if (r->header_only)
576 return NULL;
578 len = fr->header->nelts - (next - fr->header->elts);
579 ap_assert(len >= 0);
580 ap_assert(BufferLength(fr->clientOutputBuffer) == 0);
581 if (BufferFree(fr->clientOutputBuffer) < len) {
582 fr->clientOutputBuffer = fcgi_buf_new(r->pool, len);
584 ap_assert(BufferFree(fr->clientOutputBuffer) >= len);
585 if (len > 0) {
586 int sent = fcgi_buf_add_block(fr->clientOutputBuffer, next, len);
587 ap_assert(sent == len);
589 return NULL;
591 BadHeader:
592 /* Log first line of a multi-line header */
593 if ((p = strpbrk(name, "\r\n")) != NULL)
594 *p = '\0';
595 fr->parseHeader = SCAN_CGI_BAD_HEADER;
596 return ap_psprintf(r->pool, "malformed header '%s'", name);
598 DuplicateNotAllowed:
599 fr->parseHeader = SCAN_CGI_BAD_HEADER;
600 return ap_psprintf(r->pool, "duplicate header '%s'", name);
604 * Read from the client filling both the FastCGI server buffer and the
605 * client buffer with the hopes of buffering the client data before
606 * making the connect() to the FastCGI server. This prevents slow
607 * clients from keeping the FastCGI server in processing longer than is
608 * necessary.
610 static int read_from_client_n_queue(fcgi_request *fr)
612 char *end;
613 size_t count;
614 long int countRead;
616 while (BufferFree(fr->clientInputBuffer) > 0 || BufferFree(fr->serverOutputBuffer) > 0) {
617 fcgi_protocol_queue_client_buffer(fr);
619 if (fr->expectingClientContent <= 0)
620 return OK;
622 fcgi_buf_get_free_block_info(fr->clientInputBuffer, &end, &count);
623 if (count == 0)
624 return OK;
626 if ((countRead = ap_get_client_block(fr->r, end, count)) < 0)
627 return -1;
629 if (countRead == 0) {
630 fr->expectingClientContent = 0;
632 else {
633 fcgi_buf_add_update(fr->clientInputBuffer, countRead);
634 ap_reset_timeout(fr->r);
637 return OK;
640 static int write_to_client(fcgi_request *fr)
642 char *begin;
643 size_t count;
645 fcgi_buf_get_block_info(fr->clientOutputBuffer, &begin, &count);
646 if (count == 0)
647 return OK;
649 /* If fewer than count bytes are written, an error occured.
650 * ap_bwrite() typically forces a flushed write to the client, this
651 * effectively results in a block (and short packets) - it should
652 * be fixed, but I didn't win much support for the idea on new-httpd.
653 * So, without patching Apache, the best way to deal with this is
654 * to size the fcgi_bufs to hold all of the script output (within
655 * reason) so the script can be released from having to wait around
656 * for the transmission to the client to complete. */
657 #ifdef RUSSIAN_APACHE
658 if (ap_rwrite(begin, count, fr->r) != count) {
659 ap_log_rerror(FCGI_LOG_INFO_NOERRNO, fr->r,
660 "FastCGI: client stopped connection before send body completed");
661 return -1;
663 #else
664 if (ap_bwrite(fr->r->connection->client, begin, count) != (int) count) {
665 ap_log_rerror(FCGI_LOG_INFO_NOERRNO, fr->r,
666 "FastCGI: client stopped connection before send body completed");
667 return -1;
669 #endif
671 ap_reset_timeout(fr->r);
673 /* Don't bother with a wrapped buffer, limiting exposure to slow
674 * clients. The BUFF routines don't allow a writev from above,
675 * and don't always memcpy to minimize small write()s, this should
676 * be fixed, but I didn't win much support for the idea on
677 * new-httpd - I'll have to _prove_ its a problem first.. */
679 /* The default behaviour used to be to flush with every write, but this
680 * can tie up the FastCGI server longer than is necessary so its an option now */
681 if (fr->fs && fr->fs->flush) {
682 #ifdef RUSSIAN_APACHE
683 if (ap_rflush(fr->r)) {
684 ap_log_rerror(FCGI_LOG_INFO_NOERRNO, fr->r,
685 "FastCGI: client stopped connection before send body completed");
686 return -1;
688 #else
689 if (ap_bflush(fr->r->connection->client)) {
690 ap_log_rerror(FCGI_LOG_INFO_NOERRNO, fr->r,
691 "FastCGI: client stopped connection before send body completed");
692 return -1;
694 #endif
695 ap_reset_timeout(fr->r);
698 fcgi_buf_toss(fr->clientOutputBuffer, count);
699 return OK;
702 /*******************************************************************************
703 * Determine the user and group the wrapper should be called with.
704 * Based on code in Apache's create_argv_cmd() (util_script.c).
706 static void set_uid_n_gid(request_rec *r, const char **user, const char **group)
708 if (fcgi_wrapper == NULL) {
709 *user = "-";
710 *group = "-";
711 return;
714 if (strncmp("/~", r->uri, 2) == 0) {
715 /* its a user dir uri, just send the ~user, and leave it to the PM */
716 char *end = strchr(r->uri + 2, '/');
718 if (end)
719 *user = memcpy(ap_pcalloc(r->pool, end - r->uri), r->uri + 1, end - r->uri - 1);
720 else
721 *user = ap_pstrdup(r->pool, r->uri + 1);
722 *group = "-";
724 else {
725 *user = ap_psprintf(r->pool, "%ld", (long)r->server->server_uid);
726 *group = ap_psprintf(r->pool, "%ld", (long)r->server->server_gid);
730 /*******************************************************************************
731 * Close the connection to the FastCGI server. This is normally called by
732 * do_work(), but may also be called as in request pool cleanup.
734 static void close_connection_to_fs(fcgi_request *fr)
736 pool *rp = fr->r->pool;
738 if (fr->fd >= 0) {
739 ap_pclosesocket(rp, fr->fd);
742 if (fr->dynamic)
744 if (fr->keepReadingFromFcgiApp == FALSE) {
745 /* XXX FCGI_REQUEST_COMPLETE_JOB is only sent for requests which complete
746 * normally WRT the fcgi app. There is no data sent for
747 * connect() timeouts or requests which complete abnormally.
748 * KillDynamicProcs() and RemoveRecords() need to be looked at
749 * to be sure they can reasonably handle these cases before
750 * sending these sort of stats - theres some funk in there.
751 * XXX We should do something special when this a pool cleanup.
753 if (fcgi_util_gettimeofday(&fr->completeTime) < 0) {
754 /* there's no point to aborting the request, just log it */
755 ap_log_error(FCGI_LOG_ERR, fr->r->server, "FastCGI: can't get time of day");
756 } else {
757 struct timeval qtime, rtime;
759 timersub(&fr->queueTime, &fr->startTime, &qtime);
760 timersub(&fr->completeTime, &fr->queueTime, &rtime);
762 send_to_pm(FCGI_REQUEST_COMPLETE_JOB, fr->fs_path,
763 fr->user, fr->group,
764 qtime.tv_sec * 1000000 + qtime.tv_usec,
765 rtime.tv_sec * 1000000 + rtime.tv_usec);
771 #ifdef WIN32
773 static int set_nonblocking(SOCKET fd, int nonblocking)
775 unsigned long ioctl_arg = (nonblocking) ? 1 : 0;
776 return ioctlsocket(fd, FIONBIO, &ioctl_arg);
779 #else
781 static int set_nonblocking(int fd, int nonblocking)
783 int nb_flag = 0;
784 int fd_flags = fcntl(fd, F_GETFL, 0);
786 if (fd_flags < 0) return -1;
788 #if defined(O_NONBLOCK)
789 nb_flag = O_NONBLOCK;
790 #elif defined(O_NDELAY)
791 nb_flag = O_NDELAY;
792 #elif defined(FNDELAY)
793 nb_flag = FNDELAY;
794 #else
795 #error "TODO - don't read from app until all data from client is posted."
796 #endif
798 fd_flags = (nonblocking) ? (fd_flags | nb_flag) : (fd_flags & ~nb_flag);
800 return fcntl(fd, F_SETFL, fd_flags);
803 #endif
805 /*******************************************************************************
806 * Connect to the FastCGI server.
808 static int open_connection_to_fs(fcgi_request *fr)
810 struct timeval tval;
811 fd_set write_fds, read_fds;
812 int status;
813 request_rec * const r = fr->r;
814 pool * const rp = r->pool;
815 const char *socket_path = NULL;
816 struct sockaddr *socket_addr = NULL;
817 int socket_addr_len = 0;
818 #ifndef WIN32
819 const char *err = NULL;
820 #endif
822 /* Create the connection point */
823 if (fr->dynamic)
825 socket_path = fcgi_util_socket_hash_filename(rp, fr->fs_path, fr->user, fr->group);
826 socket_path = fcgi_util_socket_make_path_absolute(rp, socket_path, 1);
828 #ifndef WIN32
829 err = fcgi_util_socket_make_domain_addr(rp, (struct sockaddr_un **)&socket_addr,
830 &socket_addr_len, socket_path);
831 if (err) {
832 ap_log_rerror(FCGI_LOG_ERR, r,
833 "FastCGI: failed to connect to server \"%s\": "
834 "%s", fr->fs_path, err);
835 return FCGI_FAILED;
837 #endif
839 else
841 #ifdef WIN32
842 if (fr->fs->dest_addr != NULL) {
843 socket_addr = fr->fs->dest_addr;
845 else if (fr->fs->socket_addr) {
846 socket_addr = fr->fs->socket_addr;
848 else {
849 socket_path = fr->fs->socket_path;
851 #else
852 socket_addr = fr->fs->socket_addr;
853 #endif
854 socket_addr_len = fr->fs->socket_addr_len;
857 if (fr->dynamic)
859 #ifdef WIN32
860 if (fr->fs && fr->fs->restartTime)
861 #else
862 struct stat sock_stat;
864 if (stat(socket_path, &sock_stat) == 0)
865 #endif
867 // It exists
868 if (dynamicAutoUpdate)
870 struct stat app_stat;
872 /* TODO: follow sym links */
874 if (stat(fr->fs_path, &app_stat) == 0)
876 #ifdef WIN32
877 if (fr->fs->restartTime < app_stat.st_mtime)
878 #else
879 if (sock_stat.st_mtime < app_stat.st_mtime)
880 #endif
882 #ifndef WIN32
883 struct timeval tv = {1, 0};
884 #endif
886 * There's a newer one, request a restart.
888 send_to_pm(FCGI_SERVER_RESTART_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
890 #ifdef WIN32
891 Sleep(1000);
892 #else
893 /* Avoid sleep/alarm interactions */
894 ap_select(0, NULL, NULL, NULL, &tv);
895 #endif
900 else
902 send_to_pm(FCGI_SERVER_START_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
904 /* Wait until it looks like its running */
906 for (;;)
908 #ifdef WIN32
909 Sleep(1000);
911 fr->fs = fcgi_util_fs_get_by_id(fr->fs_path, 0, 0);
913 if (fr->fs && fr->fs->restartTime)
914 #else
915 struct timeval tv = {1, 0};
917 /* Avoid sleep/alarm interactions */
918 ap_select(0, NULL, NULL, NULL, &tv);
920 if (stat(socket_path, &sock_stat) == 0)
921 #endif
923 break;
929 #ifdef WIN32
930 if (socket_path)
932 BOOL ready;
933 int connect_time;
935 DWORD interval;
936 int max_connect_time = FCGI_NAMED_PIPE_CONNECT_TIMEOUT;
938 fr->using_npipe_io = TRUE;
940 if (fr->dynamic)
942 interval = dynamicPleaseStartDelay * 1000;
944 if (dynamicAppConnectTimeout) {
945 max_connect_time = dynamicAppConnectTimeout;
948 else
950 interval = FCGI_NAMED_PIPE_CONNECT_TIMEOUT * 1000;
952 if (fr->fs->appConnectTimeout) {
953 max_connect_time = fr->fs->appConnectTimeout;
957 if (fcgi_util_gettimeofday(&fr->startTime) < 0) {
958 ap_log_rerror(FCGI_LOG_ERR, r,
959 "FastCGI: failed to connect to server \"%s\": "
960 "can't get time of day", fr->fs_path);
961 return FCGI_FAILED;
966 fr->fd = (SOCKET) CreateFile(socket_path,
967 GENERIC_READ | GENERIC_WRITE,
968 FILE_SHARE_READ | FILE_SHARE_WRITE,
969 NULL, // no security attributes
970 OPEN_EXISTING, // opens existing pipe
971 FILE_ATTRIBUTE_NORMAL, // default attributes
972 NULL); // no template file
974 if (fr->fd != (SOCKET) INVALID_HANDLE_VALUE) {
975 break;
978 if (GetLastError() != ERROR_PIPE_BUSY
979 && GetLastError() != ERROR_FILE_NOT_FOUND)
981 ap_log_rerror(FCGI_LOG_ERR, r,
982 "FastCGI: failed to connect to server \"%s\": "
983 "CreateFile() failed", fr->fs_path);
984 return FCGI_FAILED;
987 // All pipe instances are busy, so wait
988 ready = WaitNamedPipe(socket_path, interval);
990 if (fr->dynamic && !ready) {
991 send_to_pm(FCGI_REQUEST_TIMEOUT_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
994 if (fcgi_util_gettimeofday(&fr->queueTime) < 0) {
995 ap_log_rerror(FCGI_LOG_ERR, r,
996 "FastCGI: failed to connect to server \"%s\": "
997 "can't get time of day", fr->fs_path);
998 return FCGI_FAILED;
1001 connect_time = fr->queueTime.tv_sec - fr->startTime.tv_sec;
1003 FCGIDBG5("interval=%d, max_connect_time=%d, connect_time=%d, ready=%d", interval, max_connect_time, connect_time, ready);
1005 } while (connect_time < max_connect_time);
1007 if (fr->fd == (SOCKET) INVALID_HANDLE_VALUE) {
1008 ap_log_rerror(FCGI_LOG_ERR, r,
1009 "FastCGI: failed to connect to server \"%s\": "
1010 "CreateFile()/WaitNamedPipe() timed out", fr->fs_path);
1011 return FCGI_FAILED;
1014 FCGIDBG2("got_named_pipe_connect: %s", fr->fs_path);
1016 ap_block_alarms();
1017 ap_note_cleanups_for_h(rp, (HANDLE) fr->fd);
1018 ap_unblock_alarms();
1020 return FCGI_OK;
1022 #endif
1024 /* Create the socket */
1025 fr->fd = ap_psocket(rp, socket_addr->sa_family, SOCK_STREAM, 0);
1027 if (fr->fd < 0) {
1028 #ifdef WIN32
1029 errno = WSAGetLastError(); // Not sure this is going to work as expected
1030 #endif
1031 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1032 "FastCGI: failed to connect to server \"%s\": "
1033 "ap_psocket() failed", fr->fs_path);
1034 return FCGI_FAILED;
1037 #ifndef WIN32
1038 if (fr->fd >= FD_SETSIZE) {
1039 ap_log_rerror(FCGI_LOG_ERR, r,
1040 "FastCGI: failed to connect to server \"%s\": "
1041 "socket file descriptor (%u) is larger than "
1042 "FD_SETSIZE (%u), you probably need to rebuild Apache with a "
1043 "larger FD_SETSIZE", fr->fs_path, fr->fd, FD_SETSIZE);
1044 return FCGI_FAILED;
1046 #endif
1048 /* If appConnectTimeout is non-zero, setup do a non-blocking connect */
1049 if ((fr->dynamic && dynamicAppConnectTimeout) || (!fr->dynamic && fr->fs->appConnectTimeout)) {
1050 set_nonblocking(fr->fd, TRUE);
1053 if (fr->dynamic && fcgi_util_gettimeofday(&fr->startTime) < 0) {
1054 ap_log_rerror(FCGI_LOG_ERR, r,
1055 "FastCGI: failed to connect to server \"%s\": "
1056 "can't get time of day", fr->fs_path);
1057 return FCGI_FAILED;
1060 /* Connect */
1061 if (connect(fr->fd, (struct sockaddr *)socket_addr, socket_addr_len) == 0)
1062 goto ConnectionComplete;
1064 #ifdef WIN32
1066 errno = WSAGetLastError();
1067 if (errno != WSAEWOULDBLOCK) {
1068 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1069 "FastCGI: failed to connect to server \"%s\": "
1070 "connect() failed", fr->fs_path);
1071 return FCGI_FAILED;
1074 #else
1076 /* ECONNREFUSED means the listen queue is full (or there isn't one).
1077 * With dynamic I can at least make sure the PM knows this is occuring */
1078 if (fr->dynamic && errno == ECONNREFUSED) {
1079 /* @@@ This might be better as some other "kind" of message */
1080 send_to_pm(FCGI_REQUEST_TIMEOUT_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
1082 errno = ECONNREFUSED;
1085 if (errno != EINPROGRESS) {
1086 ap_log_rerror(FCGI_LOG_ERR, r,
1087 "FastCGI: failed to connect to server \"%s\": "
1088 "connect() failed", fr->fs_path);
1089 return FCGI_FAILED;
1092 #endif
1094 /* The connect() is non-blocking */
1096 errno = 0;
1098 if (fr->dynamic) {
1099 do {
1100 FD_ZERO(&write_fds);
1101 FD_SET(fr->fd, &write_fds);
1102 read_fds = write_fds;
1103 tval.tv_sec = dynamicPleaseStartDelay;
1104 tval.tv_usec = 0;
1106 status = ap_select((fr->fd+1), &read_fds, &write_fds, NULL, &tval);
1107 if (status < 0)
1108 break;
1110 if (fcgi_util_gettimeofday(&fr->queueTime) < 0) {
1111 ap_log_rerror(FCGI_LOG_ERR, r,
1112 "FastCGI: failed to connect to server \"%s\": "
1113 "can't get time of day", fr->fs_path);
1114 return FCGI_FAILED;
1117 if (status > 0)
1118 break;
1120 /* select() timed out */
1121 send_to_pm(FCGI_REQUEST_TIMEOUT_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
1122 } while ((fr->queueTime.tv_sec - fr->startTime.tv_sec) < (int)dynamicAppConnectTimeout);
1124 /* XXX These can be moved down when dynamic vars live is a struct */
1125 if (status == 0) {
1126 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1127 "FastCGI: failed to connect to server \"%s\": "
1128 "connect() timed out (appConnTimeout=%dsec)",
1129 fr->fs_path, dynamicAppConnectTimeout);
1130 return FCGI_FAILED;
1132 } /* dynamic */
1133 else {
1134 tval.tv_sec = fr->fs->appConnectTimeout;
1135 tval.tv_usec = 0;
1136 FD_ZERO(&write_fds);
1137 FD_SET(fr->fd, &write_fds);
1138 read_fds = write_fds;
1140 status = ap_select((fr->fd+1), &read_fds, &write_fds, NULL, &tval);
1142 if (status == 0) {
1143 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1144 "FastCGI: failed to connect to server \"%s\": "
1145 "connect() timed out (appConnTimeout=%dsec)",
1146 fr->fs_path, dynamicAppConnectTimeout);
1147 return FCGI_FAILED;
1149 } /* !dynamic */
1151 if (status < 0) {
1152 #ifdef WIN32
1153 errno = WSAGetLastError();
1154 #endif
1155 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1156 "FastCGI: failed to connect to server \"%s\": "
1157 "select() failed", fr->fs_path);
1158 return FCGI_FAILED;
1161 if (FD_ISSET(fr->fd, &write_fds) || FD_ISSET(fr->fd, &read_fds)) {
1162 int error = 0;
1163 NET_SIZE_T len = sizeof(error);
1165 if (getsockopt(fr->fd, SOL_SOCKET, SO_ERROR, (char *)&error, &len) < 0) {
1166 /* Solaris pending error */
1167 #ifdef WIN32
1168 errno = WSAGetLastError();
1169 #endif
1170 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1171 "FastCGI: failed to connect to server \"%s\": "
1172 "select() failed (Solaris pending error)", fr->fs_path);
1173 return FCGI_FAILED;
1176 if (error != 0) {
1177 /* Berkeley-derived pending error */
1178 errno = error;
1179 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1180 "FastCGI: failed to connect to server \"%s\": "
1181 "select() failed (pending error)", fr->fs_path);
1182 return FCGI_FAILED;
1185 else {
1186 #ifdef WIN32
1187 errno = WSAGetLastError();
1188 #endif
1189 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1190 "FastCGI: failed to connect to server \"%s\": "
1191 "select() error - THIS CAN'T HAPPEN!", fr->fs_path);
1192 return FCGI_FAILED;
1195 ConnectionComplete:
1196 /* Return to blocking mode if it was set up */
1197 if ((fr->dynamic && dynamicAppConnectTimeout) || (!fr->dynamic && fr->fs->appConnectTimeout)) {
1198 set_nonblocking(fr->fd, FALSE);
1201 #ifdef TCP_NODELAY
1202 if (socket_addr->sa_family == AF_INET) {
1203 /* We shouldn't be sending small packets and there's no application
1204 * level ack of the data we send, so disable Nagle */
1205 int set = 1;
1206 setsockopt(fr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&set, sizeof(set));
1208 #endif
1210 return FCGI_OK;
1213 static int server_error(fcgi_request *fr)
1215 #if defined(SIGPIPE) && MODULE_MAGIC_NUMBER < 19990320
1216 /* Make sure we leave with Apache's sigpipe_handler in place */
1217 if (fr->apache_sigpipe_handler != NULL)
1218 signal(SIGPIPE, fr->apache_sigpipe_handler);
1219 #endif
1220 close_connection_to_fs(fr);
1221 ap_kill_timeout(fr->r);
1222 return SERVER_ERROR;
1225 static void cleanup(void *data)
1227 const fcgi_request * const fr = (fcgi_request *)data;
1229 if (fr == NULL)
1230 return ;
1232 if (fr->fd >= 0) {
1233 set_nonblocking(fr->fd, FALSE);
1236 if (fr->fs_stderr_len) {
1237 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, fr->r,
1238 "FastCGI: server \"%s\" stderr: %s", fr->fs_path, fr->fs_stderr);
1242 /*----------------------------------------------------------------------
1243 * This is the core routine for moving data between the FastCGI
1244 * application and the Web server's client.
1246 static int do_work(request_rec *r, fcgi_request *fr)
1248 struct timeval timeOut, dynamic_last_activity_time = {0, 0};
1249 fd_set read_set, write_set;
1250 int status = 0, idle_timeout;
1251 int numFDs, dynamic_first_read = fr->dynamic ? 1 : 0;
1252 int doClientWrite;
1253 int envSent = FALSE; /* has the complete ENV been buffered? */
1254 env_status env;
1255 pool *rp = r->pool;
1256 const char *err = NULL;
1258 FD_ZERO(&read_set);
1259 FD_ZERO(&write_set);
1261 fcgi_protocol_queue_begin_request(fr);
1263 /* Buffer as much of the environment as we can fit */
1264 env.envp = NULL;
1265 envSent = fcgi_protocol_queue_env(r, fr, &env);
1267 /* Start the Apache dropdead timer. See comments at top of file. */
1268 ap_hard_timeout("buffering of FastCGI client data", r);
1270 /* Read as much as possible from the client. */
1271 if (fr->role == FCGI_RESPONDER) {
1272 status = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR);
1273 if (status != OK) {
1274 ap_kill_timeout(r);
1275 return status;
1277 fr->expectingClientContent = (ap_should_client_block(r) != 0);
1279 if (read_from_client_n_queue(fr) != OK)
1280 return server_error(fr);
1283 /* Connect to the FastCGI Application */
1284 ap_hard_timeout("connect() to FastCGI server", r);
1285 if (open_connection_to_fs(fr) != FCGI_OK) {
1286 return server_error(fr);
1289 numFDs = fr->fd + 1;
1290 idle_timeout = fr->dynamic ? dynamic_idle_timeout : fr->fs->idle_timeout;
1292 if (dynamic_first_read) {
1293 dynamic_last_activity_time = fr->startTime;
1295 if (dynamicAppConnectTimeout) {
1296 struct timeval qwait;
1297 timersub(&fr->queueTime, &fr->startTime, &qwait);
1298 dynamic_first_read = qwait.tv_sec / dynamicPleaseStartDelay + 1;
1302 /* @@@ We never reset the timer in this loop, most folks don't mess w/
1303 * Timeout directive which means we've got the 5 min default which is way
1304 * to long to tie up a fs. We need a better/configurable solution that
1305 * uses the select */
1306 ap_hard_timeout("FastCGI request processing", r);
1308 /* Register to get the script's stderr logged at the end of the request */
1309 ap_block_alarms();
1310 ap_register_cleanup(rp, (void *)fr, cleanup, ap_null_cleanup);
1311 ap_unblock_alarms();
1313 /* Before we do any writing, set the connection non-blocking */
1314 #ifdef WIN32
1315 if (fr->using_npipe_io) {
1316 DWORD mode = PIPE_NOWAIT | PIPE_READMODE_BYTE;
1317 SetNamedPipeHandleState((HANDLE) fr->fd, &mode, NULL, NULL);
1319 else
1320 #endif
1322 set_nonblocking(fr->fd, TRUE);
1324 /* The socket is writeable, so get the first write out of the way */
1325 if (fcgi_buf_get_to_fd(fr->serverOutputBuffer, fr->fd) < 0) {
1326 #ifdef WIN32
1327 if (! fr->using_npipe_io)
1328 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1329 "FastCGI: comm with server \"%s\" aborted: write failed", fr->fs_path);
1330 else
1331 #endif
1332 ap_log_rerror(FCGI_LOG_ERR, r,
1333 "FastCGI: comm with server \"%s\" aborted: write failed", fr->fs_path);
1334 return server_error(fr);
1337 while (fr->keepReadingFromFcgiApp
1338 || BufferLength(fr->serverInputBuffer) > 0
1339 || BufferLength(fr->clientOutputBuffer) > 0) {
1341 /* If we didn't buffer all of the environment yet, buffer some more */
1342 if (!envSent)
1343 envSent = fcgi_protocol_queue_env(r, fr, &env);
1345 /* Read as much as possible from the client. */
1346 if (fr->role == FCGI_RESPONDER && !fr->eofSent && envSent) {
1348 /* ap_get_client_block() (called in read_from_client_n_queue()
1349 * can't handle a non-blocking fd, its a bummer. We might be
1350 * able to completely bypass the Apache BUFF routines in still
1351 * do it, but thats a major hassle. Apache 2.X will handle it,
1352 * and then so will we. */
1354 if (read_from_client_n_queue(fr) != OK)
1355 return server_error(fr);
1358 /* To avoid deadlock, don't do a blocking select to write to
1359 * the FastCGI application without selecting to read from the
1360 * FastCGI application.
1362 doClientWrite = FALSE;
1363 if (fr->keepReadingFromFcgiApp && BufferFree(fr->serverInputBuffer) > 0) {
1365 #ifdef WIN32
1366 DWORD bytesavail = 0;
1368 if (!fr->using_npipe_io) {
1369 #endif
1370 FD_SET(fr->fd, &read_set);
1372 /* Is data buffered for output to the FastCGI server? */
1373 if (BufferLength(fr->serverOutputBuffer) > 0) {
1374 FD_SET(fr->fd, &write_set);
1375 } else {
1376 FD_CLR(fr->fd, &write_set);
1378 #ifdef WIN32
1380 #endif
1382 * If there's data buffered to send to the client, don't
1383 * wait indefinitely for the FastCGI app; the app might
1384 * be doing server push.
1386 if (BufferLength(fr->clientOutputBuffer) > 0) {
1387 timeOut.tv_sec = 0;
1388 timeOut.tv_usec = 100000; /* 0.1 sec */
1390 else if (dynamic_first_read) {
1391 int delay;
1392 struct timeval qwait;
1394 if (fcgi_util_gettimeofday(&fr->queueTime) < 0) {
1395 ap_log_rerror(FCGI_LOG_ERR, r, "FastCGI: can't get time of day");
1396 return server_error(fr);
1399 /* Check for idle_timeout */
1400 if (status) {
1401 dynamic_last_activity_time = fr->queueTime;
1403 else {
1404 struct timeval idle_time;
1405 timersub(&fr->queueTime, &dynamic_last_activity_time, &idle_time);
1406 if (idle_time.tv_sec > idle_timeout) {
1407 send_to_pm(FCGI_REQUEST_TIMEOUT_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
1408 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1409 "FastCGI: comm with (dynamic) server \"%s\" aborted: (first read) idle timeout (%d sec)",
1410 fr->fs_path, idle_timeout);
1411 return server_error(fr);
1415 timersub(&fr->queueTime, &fr->startTime, &qwait);
1417 delay = dynamic_first_read * dynamicPleaseStartDelay;
1418 if (qwait.tv_sec < delay) {
1419 timeOut.tv_sec = delay;
1420 timeOut.tv_usec = 100000; /* fudge for select() slop */
1421 timersub(&timeOut, &qwait, &timeOut);
1423 else {
1424 /* Killed time somewhere.. client read? */
1425 send_to_pm(FCGI_REQUEST_TIMEOUT_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
1426 dynamic_first_read = qwait.tv_sec / dynamicPleaseStartDelay + 1;
1427 timeOut.tv_sec = dynamic_first_read * dynamicPleaseStartDelay;
1428 timeOut.tv_usec = 100000; /* fudge for select() slop */
1429 timersub(&timeOut, &qwait, &timeOut);
1432 else {
1433 timeOut.tv_sec = idle_timeout;
1434 timeOut.tv_usec = 0;
1437 #ifdef WIN32
1438 if (!fr->using_npipe_io) {
1439 #endif
1440 if ((status = ap_select(numFDs, &read_set, &write_set, NULL, &timeOut)) < 0) {
1441 #ifdef WIN32
1442 errno = WSAGetLastError();
1443 #endif
1444 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1445 "FastCGI: comm with server \"%s\" aborted: select() failed", fr->fs_path);
1446 return server_error(fr);
1448 #ifdef WIN32
1450 else {
1451 int stopTime = time(NULL) + timeOut.tv_sec;
1453 if (BufferLength(fr->serverOutputBuffer) == 0)
1455 status = 0;
1457 while ((timeOut.tv_sec != 0) && (time(NULL) <= stopTime))
1459 BOOL ok = PeekNamedPipe((HANDLE) fr->fd,NULL, 0, NULL, &bytesavail, NULL);
1460 if (! ok)
1462 ap_log_rerror(FCGI_LOG_ERR, r,
1463 "FastCGI: comm with sever \"%s\" aborted: PeekNamedPipe() failed",
1464 fr->fs_path);
1465 return server_error(fr);
1467 if (bytesavail > 0)
1469 status =1;
1470 break;
1472 Sleep(100);
1475 else {
1476 status = 1;
1479 #endif
1481 if (status == 0) {
1482 if (BufferLength(fr->clientOutputBuffer) > 0) {
1483 doClientWrite = TRUE;
1485 else if (dynamic_first_read) {
1486 struct timeval qwait;
1488 if (fcgi_util_gettimeofday(&fr->queueTime) < 0) {
1489 ap_log_rerror(FCGI_LOG_ERR, r, "FastCGI: can't get time of day");
1490 return server_error(fr);
1493 timersub(&fr->queueTime, &fr->startTime, &qwait);
1495 send_to_pm(FCGI_REQUEST_TIMEOUT_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
1497 dynamic_first_read = qwait.tv_sec / dynamicPleaseStartDelay + 1;
1499 else {
1500 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1501 "FastCGI: comm with server \"%s\" aborted: idle timeout (%d sec)",
1502 fr->fs_path, idle_timeout);
1503 return server_error(fr);
1507 #if defined(SIGPIPE) && MODULE_MAGIC_NUMBER < 19990320
1508 /* Disable Apache's SIGPIPE handler */
1509 fr->apache_sigpipe_handler = signal(SIGPIPE, SIG_IGN);
1510 #endif
1512 /* Read from the FastCGI server */
1513 #ifdef WIN32
1514 if ((fr->using_npipe_io
1515 && (BufferFree(fr->serverInputBuffer) > 0)
1516 && PeekNamedPipe((HANDLE) fr->fd, NULL, 0, NULL, &bytesavail, NULL)
1517 && (bytesavail > 0))
1518 || FD_ISSET(fr->fd, &read_set)) {
1519 #else
1520 if (FD_ISSET(fr->fd, &read_set)) {
1521 #endif
1522 if (dynamic_first_read) {
1523 dynamic_first_read = 0;
1524 if (fcgi_util_gettimeofday(&fr->queueTime) < 0) {
1525 ap_log_rerror(FCGI_LOG_ERR, r, "FastCGI: can't get time of day");
1526 return server_error(fr);
1530 if ((status = fcgi_buf_add_fd(fr->serverInputBuffer, fr->fd)) < 0) {
1531 #ifdef WIN32
1532 if (! fr->using_npipe_io)
1533 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1534 "FastCGI: comm with server \"%s\" aborted: read failed", fr->fs_path);
1535 else
1536 #endif
1537 ap_log_rerror(FCGI_LOG_ERR, r,
1538 "FastCGI: comm with server \"%s\" aborted: read failed", fr->fs_path);
1539 return server_error(fr);
1542 if (status == 0) {
1543 fr->keepReadingFromFcgiApp = FALSE;
1544 close_connection_to_fs(fr);
1548 /* Write to the FastCGI server */
1549 #ifdef WIN32
1550 if ((fr->using_npipe_io && (BufferLength(fr->serverOutputBuffer) > 0))
1551 || FD_ISSET(fr->fd, &write_set)) {
1552 #else
1553 if (FD_ISSET(fr->fd, &write_set)) {
1554 #endif
1556 if (fcgi_buf_get_to_fd(fr->serverOutputBuffer, fr->fd) < 0) {
1557 #ifdef WIN32
1558 if (! fr->using_npipe_io)
1559 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1560 "FastCGI: comm with server \"%s\" aborted: write failed", fr->fs_path);
1561 else
1562 #endif
1563 ap_log_rerror(FCGI_LOG_ERR, r,
1564 "FastCGI: comm with server \"%s\" aborted: write failed", fr->fs_path);
1565 return server_error(fr);
1569 #if defined(SIGPIPE) && MODULE_MAGIC_NUMBER < 19990320
1570 /* Reinstall Apache's SIGPIPE handler */
1571 signal(SIGPIPE, fr->apache_sigpipe_handler);
1572 #endif
1574 } else {
1575 doClientWrite = TRUE;
1578 if (fr->role == FCGI_RESPONDER && doClientWrite) {
1580 if (write_to_client(fr) != OK) {
1581 #if defined(SIGPIPE) && MODULE_MAGIC_NUMBER < 19990320
1582 /* Make sure we leave with Apache's sigpipe_handler in place */
1583 if (fr->apache_sigpipe_handler != NULL)
1584 signal(SIGPIPE, fr->apache_sigpipe_handler);
1585 #endif
1586 close_connection_to_fs(fr);
1587 ap_kill_timeout(fr->r);
1588 return OK;
1592 if (fcgi_protocol_dequeue(rp, fr) != OK)
1593 return server_error(fr);
1595 if (fr->keepReadingFromFcgiApp && fr->exitStatusSet) {
1596 /* we're done talking to the fcgi app */
1597 fr->keepReadingFromFcgiApp = FALSE;
1598 close_connection_to_fs(fr);
1601 if (fr->parseHeader == SCAN_CGI_READING_HEADERS) {
1602 if ((err = process_headers(r, fr))) {
1603 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1604 "FastCGI: comm with server \"%s\" aborted: error parsing headers: %s", fr->fs_path, err);
1605 return server_error(fr);
1609 } /* while */
1611 switch (fr->parseHeader) {
1613 case SCAN_CGI_FINISHED:
1614 if (fr->role == FCGI_RESPONDER) {
1615 #ifdef RUSSIAN_APACHE
1616 ap_rflush(r);
1617 #else
1618 ap_bflush(r->connection->client);
1619 #endif
1620 ap_bgetopt(r->connection->client, BO_BYTECT, &r->bytes_sent);
1622 break;
1624 case SCAN_CGI_READING_HEADERS:
1625 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1626 "FastCGI: incomplete headers (%d bytes) received from server \"%s\"",
1627 fr->header->nelts, fr->fs_path);
1628 return server_error(fr);
1630 case SCAN_CGI_BAD_HEADER:
1631 return server_error(fr);
1633 case SCAN_CGI_INT_REDIRECT:
1634 case SCAN_CGI_SRV_REDIRECT:
1636 * XXX We really should be soaking all client input
1637 * and all script output. See mod_cgi.c.
1638 * There's other differences we need to pick up here as well!
1639 * This has to be revisited.
1641 break;
1643 default:
1644 ap_assert(FALSE);
1647 ap_kill_timeout(r);
1648 return OK;
1651 static fcgi_request *create_fcgi_request(request_rec * const r, const char *fs_path)
1653 struct stat *my_finfo;
1654 pool * const p = r->pool;
1655 fcgi_server *fs;
1656 fcgi_request * const fr = (fcgi_request *)ap_pcalloc(p, sizeof(fcgi_request));
1658 if (fs_path) {
1659 my_finfo = (struct stat *)ap_palloc(p, sizeof(struct stat));
1660 if (stat(fs_path, my_finfo) < 0) {
1661 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1662 "FastCGI: stat() of \"%s\" failed", fs_path);
1663 return NULL;
1666 else {
1667 my_finfo = &r->finfo;
1668 fs_path = r->filename;
1671 fs = fcgi_util_fs_get_by_id(fs_path, r->server->server_uid, r->server->server_gid);
1672 if (fs == NULL) {
1673 /* Its a request for a dynamic FastCGI application */
1674 const char * const err =
1675 fcgi_util_fs_is_path_ok(p, fs_path, my_finfo);
1677 if (err) {
1678 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r, "FastCGI: invalid (dynamic) server \"%s\": %s", fs_path, err);
1679 return NULL;
1683 fr->serverInputBuffer = fcgi_buf_new(p, SERVER_BUFSIZE);
1684 fr->serverOutputBuffer = fcgi_buf_new(p, SERVER_BUFSIZE);
1685 fr->clientInputBuffer = fcgi_buf_new(p, SERVER_BUFSIZE);
1686 fr->clientOutputBuffer = fcgi_buf_new(p, SERVER_BUFSIZE);
1687 fr->erBufPtr = fcgi_buf_new(p, sizeof(FCGI_EndRequestBody) + 1);
1688 fr->gotHeader = FALSE;
1689 fr->parseHeader = SCAN_CGI_READING_HEADERS;
1690 fr->header = ap_make_array(p, 1, 1);
1691 fr->fs_stderr = NULL;
1692 fr->r = r;
1693 fr->readingEndRequestBody = FALSE;
1694 fr->exitStatus = 0;
1695 fr->exitStatusSet = FALSE;
1696 fr->requestId = 1; /* anything but zero is OK here */
1697 fr->eofSent = FALSE;
1698 fr->role = FCGI_RESPONDER;
1699 fr->expectingClientContent = FALSE;
1700 fr->keepReadingFromFcgiApp = TRUE;
1701 fr->fs = fs;
1702 fr->fs_path = fs_path;
1703 fr->authHeaders = ap_make_table(p, 10);
1704 #ifdef WIN32
1705 fr->fd = INVALID_SOCKET;
1706 fr->dynamic = ((fs == NULL) || (fs->directive == APP_CLASS_DYNAMIC)) ? TRUE : FALSE;
1707 fr->using_npipe_io = FALSE;
1708 #else
1709 fr->dynamic = (fs == NULL) ? TRUE : FALSE;
1710 fr->fd = -1;
1711 #endif
1713 set_uid_n_gid(r, &fr->user, &fr->group);
1715 return fr;
1719 *----------------------------------------------------------------------
1721 * handler --
1723 * This routine gets called for a request that corresponds to
1724 * a FastCGI connection. It performs the request synchronously.
1726 * Results:
1727 * Final status of request: OK or NOT_FOUND or SERVER_ERROR.
1729 * Side effects:
1730 * Request performed.
1732 *----------------------------------------------------------------------
1735 /* Stolen from mod_cgi.c..
1736 * KLUDGE --- for back-combatibility, we don't have to check ExecCGI
1737 * in ScriptAliased directories, which means we need to know if this
1738 * request came through ScriptAlias or not... so the Alias module
1739 * leaves a note for us.
1741 static int apache_is_scriptaliased(request_rec *r)
1743 const char *t = ap_table_get(r->notes, "alias-forced-type");
1744 return t && (!strcasecmp(t, "cgi-script"));
1747 /* If a script wants to produce its own Redirect body, it now
1748 * has to explicitly *say* "Status: 302". If it wants to use
1749 * Apache redirects say "Status: 200". See process_headers().
1751 static int post_process_for_redirects(request_rec * const r,
1752 const fcgi_request * const fr)
1754 switch(fr->parseHeader) {
1755 case SCAN_CGI_INT_REDIRECT:
1757 /* @@@ There are still differences between the handling in
1758 * mod_cgi and mod_fastcgi. This needs to be revisited.
1760 /* We already read the message body (if any), so don't allow
1761 * the redirected request to think it has one. We can ignore
1762 * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
1764 r->method = "GET";
1765 r->method_number = M_GET;
1766 ap_table_unset(r->headers_in, "Content-length");
1768 ap_internal_redirect_handler(ap_table_get(r->headers_out, "Location"), r);
1769 return OK;
1771 case SCAN_CGI_SRV_REDIRECT:
1772 return REDIRECT;
1774 default:
1775 return OK;
1779 /******************************************************************************
1780 * Process fastcgi-script requests. Based on mod_cgi::cgi_handler().
1782 static int content_handler(request_rec *r)
1784 fcgi_request *fr = NULL;
1785 int ret;
1787 /* Setup a new FastCGI request */
1788 if ((fr = create_fcgi_request(r, NULL)) == NULL)
1789 return SERVER_ERROR;
1791 /* If its a dynamic invocation, make sure scripts are OK here */
1792 if (fr->dynamic && !(ap_allow_options(r) & OPT_EXECCGI) && !apache_is_scriptaliased(r)) {
1793 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1794 "FastCGI: \"ExecCGI Option\" is off in this directory: %s", r->uri);
1795 return SERVER_ERROR;
1798 /* Process the fastcgi-script request */
1799 if ((ret = do_work(r, fr)) != OK)
1800 return ret;
1802 /* Special case redirects */
1803 return post_process_for_redirects(r, fr);
1807 static int post_process_auth_passed_header(table *t, const char *key, const char * const val)
1809 if (strncasecmp(key, "Variable-", 9) == 0)
1810 key += 9;
1812 ap_table_setn(t, key, val);
1813 return 1;
1816 static int post_process_auth_passed_compat_header(table *t, const char *key, const char * const val)
1818 if (strncasecmp(key, "Variable-", 9) == 0)
1819 ap_table_setn(t, key + 9, val);
1821 return 1;
1824 static int post_process_auth_failed_header(table * const t, const char * const key, const char * const val)
1826 ap_table_setn(t, key, val);
1827 return 1;
1830 static void post_process_auth(fcgi_request * const fr, const int passed)
1832 request_rec * const r = fr->r;
1834 /* Restore the saved subprocess_env because we muddied ours up */
1835 r->subprocess_env = fr->saved_subprocess_env;
1837 if (passed) {
1838 if (fr->auth_compat) {
1839 ap_table_do((int (*)(void *, const char *, const char *))post_process_auth_passed_compat_header,
1840 (void *)r->subprocess_env, fr->authHeaders, NULL);
1842 else {
1843 ap_table_do((int (*)(void *, const char *, const char *))post_process_auth_passed_header,
1844 (void *)r->subprocess_env, fr->authHeaders, NULL);
1847 else {
1848 ap_table_do((int (*)(void *, const char *, const char *))post_process_auth_failed_header,
1849 (void *)r->err_headers_out, fr->authHeaders, NULL);
1852 /* @@@ Restore these.. its a hack until I rewrite the header handling */
1853 r->status = HTTP_OK;
1854 r->status_line = NULL;
1857 static int check_user_authentication(request_rec *r)
1859 int res, authenticated = 0;
1860 const char *password;
1861 fcgi_request *fr;
1862 const fcgi_dir_config * const dir_config =
1863 (const fcgi_dir_config *)ap_get_module_config(r->per_dir_config, &fastcgi_module);
1865 if (dir_config->authenticator == NULL)
1866 return DECLINED;
1868 /* Get the user password */
1869 if ((res = ap_get_basic_auth_pw(r, &password)) != OK)
1870 return res;
1872 if ((fr = create_fcgi_request(r, dir_config->authenticator)) == NULL)
1873 return SERVER_ERROR;
1875 /* Save the existing subprocess_env, because we're gonna muddy it up */
1876 fr->saved_subprocess_env = ap_copy_table(r->pool, r->subprocess_env);
1878 ap_table_setn(r->subprocess_env, "REMOTE_PASSWD", password);
1879 ap_table_setn(r->subprocess_env, "FCGI_APACHE_ROLE", "AUTHENTICATOR");
1881 /* The FastCGI Protocol doesn't differentiate authentication */
1882 fr->role = FCGI_AUTHORIZER;
1884 /* Do we need compatibility mode? */
1885 fr->auth_compat = (dir_config->authenticator_options & FCGI_COMPAT);
1887 if ((res = do_work(r, fr)) != OK)
1888 goto AuthenticationFailed;
1890 authenticated = (r->status == 200);
1891 post_process_auth(fr, authenticated);
1893 /* A redirect shouldn't be allowed during the authentication phase */
1894 if (ap_table_get(r->headers_out, "Location") != NULL) {
1895 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1896 "FastCGI: FastCgiAuthenticator \"%s\" redirected (not allowed)",
1897 dir_config->authenticator);
1898 goto AuthenticationFailed;
1901 if (authenticated)
1902 return OK;
1904 AuthenticationFailed:
1905 if (!(dir_config->authenticator_options & FCGI_AUTHORITATIVE))
1906 return DECLINED;
1908 /* @@@ Probably should support custom_responses */
1909 ap_note_basic_auth_failure(r);
1910 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1911 "FastCGI: authentication failed for user \"%s\": %s", r->connection->user, r->uri);
1912 return (res == OK) ? AUTH_REQUIRED : res;
1915 static int check_user_authorization(request_rec *r)
1917 int res, authorized = 0;
1918 fcgi_request *fr;
1919 const fcgi_dir_config * const dir_config =
1920 (const fcgi_dir_config *)ap_get_module_config(r->per_dir_config, &fastcgi_module);
1922 if (dir_config->authorizer == NULL)
1923 return DECLINED;
1925 /* @@@ We should probably honor the existing parameters to the require directive
1926 * as well as allow the definition of new ones (or use the basename of the
1927 * FastCGI server and pass the rest of the directive line), but for now keep
1928 * it simple. */
1930 if ((fr = create_fcgi_request(r, dir_config->authorizer)) == NULL)
1931 return SERVER_ERROR;
1933 /* Save the existing subprocess_env, because we're gonna muddy it up */
1934 fr->saved_subprocess_env = ap_copy_table(r->pool, r->subprocess_env);
1936 ap_table_setn(r->subprocess_env, "FCGI_APACHE_ROLE", "AUTHORIZER");
1938 fr->role = FCGI_AUTHORIZER;
1940 /* Do we need compatibility mode? */
1941 fr->auth_compat = (dir_config->authenticator_options & FCGI_COMPAT);
1943 if ((res = do_work(r, fr)) != OK)
1944 goto AuthorizationFailed;
1946 authorized = (r->status == 200);
1947 post_process_auth(fr, authorized);
1949 /* A redirect shouldn't be allowed during the authorization phase */
1950 if (ap_table_get(r->headers_out, "Location") != NULL) {
1951 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1952 "FastCGI: FastCgiAuthorizer \"%s\" redirected (not allowed)",
1953 dir_config->authorizer);
1954 goto AuthorizationFailed;
1957 if (authorized)
1958 return OK;
1960 AuthorizationFailed:
1961 if (!(dir_config->authorizer_options & FCGI_AUTHORITATIVE))
1962 return DECLINED;
1964 /* @@@ Probably should support custom_responses */
1965 ap_note_basic_auth_failure(r);
1966 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1967 "FastCGI: authorization failed for user \"%s\": %s", r->connection->user, r->uri);
1968 return (res == OK) ? AUTH_REQUIRED : res;
1971 static int check_access(request_rec *r)
1973 int res, access_allowed = 0;
1974 fcgi_request *fr;
1975 const fcgi_dir_config * const dir_config =
1976 (fcgi_dir_config *)ap_get_module_config(r->per_dir_config, &fastcgi_module);
1978 if (dir_config == NULL || dir_config->access_checker == NULL)
1979 return DECLINED;
1981 if ((fr = create_fcgi_request(r, dir_config->access_checker)) == NULL)
1982 return SERVER_ERROR;
1984 /* Save the existing subprocess_env, because we're gonna muddy it up */
1985 fr->saved_subprocess_env = ap_copy_table(r->pool, r->subprocess_env);
1987 ap_table_setn(r->subprocess_env, "FCGI_APACHE_ROLE", "ACCESS_CHECKER");
1989 /* The FastCGI Protocol doesn't differentiate access control */
1990 fr->role = FCGI_AUTHORIZER;
1992 /* Do we need compatibility mode? */
1993 fr->auth_compat = (dir_config->authenticator_options & FCGI_COMPAT);
1995 if ((res = do_work(r, fr)) != OK)
1996 goto AccessFailed;
1998 access_allowed = (r->status == 200);
1999 post_process_auth(fr, access_allowed);
2001 /* A redirect shouldn't be allowed during the access check phase */
2002 if (ap_table_get(r->headers_out, "Location") != NULL) {
2003 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
2004 "FastCGI: FastCgiAccessChecker \"%s\" redirected (not allowed)",
2005 dir_config->access_checker);
2006 goto AccessFailed;
2009 if (access_allowed)
2010 return OK;
2012 AccessFailed:
2013 if (!(dir_config->access_checker_options & FCGI_AUTHORITATIVE))
2014 return DECLINED;
2016 /* @@@ Probably should support custom_responses */
2017 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r, "FastCGI: access denied: %s", r->uri);
2018 return (res == OK) ? FORBIDDEN : res;
2021 command_rec fastcgi_cmds[] = {
2022 { "AppClass", fcgi_config_new_static_server, NULL, RSRC_CONF, RAW_ARGS, NULL },
2023 { "FastCgiServer", fcgi_config_new_static_server, NULL, RSRC_CONF, RAW_ARGS, NULL },
2025 { "ExternalAppClass", fcgi_config_new_external_server, NULL, RSRC_CONF, RAW_ARGS, NULL },
2026 { "FastCgiExternalServer", fcgi_config_new_external_server, NULL, RSRC_CONF, RAW_ARGS, NULL },
2028 { "FastCgiIpcDir", fcgi_config_set_socket_dir, NULL, RSRC_CONF, TAKE1, NULL },
2030 { "FastCgiSuexec", fcgi_config_set_wrapper, NULL, RSRC_CONF, TAKE1, NULL },
2031 { "FastCgiWrapper", fcgi_config_set_wrapper, NULL, RSRC_CONF, TAKE1, NULL },
2033 { "FCGIConfig", fcgi_config_set_config, NULL, RSRC_CONF, RAW_ARGS, NULL },
2034 { "FastCgiConfig", fcgi_config_set_config, NULL, RSRC_CONF, RAW_ARGS, NULL },
2036 { "FastCgiAuthenticator", fcgi_config_new_auth_server,
2037 (void *)FCGI_AUTH_TYPE_AUTHENTICATOR, ACCESS_CONF, TAKE12,
2038 "a fastcgi-script path (absolute or relative to ServerRoot) followed by an optional -compat" },
2039 { "FastCgiAuthenticatorAuthoritative", fcgi_config_set_authoritative_slot,
2040 (void *)XtOffsetOf(fcgi_dir_config, authenticator_options), ACCESS_CONF, FLAG,
2041 "Set to 'off' to allow authentication to be passed along to lower modules upon failure" },
2043 { "FastCgiAuthorizer", fcgi_config_new_auth_server,
2044 (void *)FCGI_AUTH_TYPE_AUTHORIZER, ACCESS_CONF, TAKE12,
2045 "a fastcgi-script path (absolute or relative to ServerRoot) followed by an optional -compat" },
2046 { "FastCgiAuthorizerAuthoritative", fcgi_config_set_authoritative_slot,
2047 (void *)XtOffsetOf(fcgi_dir_config, authorizer_options), ACCESS_CONF, FLAG,
2048 "Set to 'off' to allow authorization to be passed along to lower modules upon failure" },
2050 { "FastCgiAccessChecker", fcgi_config_new_auth_server,
2051 (void *)FCGI_AUTH_TYPE_ACCESS_CHECKER, ACCESS_CONF, TAKE12,
2052 "a fastcgi-script path (absolute or relative to ServerRoot) followed by an optional -compat" },
2053 { "FastCgiAccessCheckerAuthoritative", fcgi_config_set_authoritative_slot,
2054 (void *)XtOffsetOf(fcgi_dir_config, access_checker_options), ACCESS_CONF, FLAG,
2055 "Set to 'off' to allow access control to be passed along to lower modules upon failure" },
2056 { NULL }
2060 handler_rec fastcgi_handlers[] = {
2061 { FCGI_MAGIC_TYPE, content_handler },
2062 { "fastcgi-script", content_handler },
2063 { NULL }
2067 module MODULE_VAR_EXPORT fastcgi_module = {
2068 STANDARD_MODULE_STUFF,
2069 init_module, /* initializer */
2070 fcgi_config_create_dir_config, /* per-dir config creator */
2071 NULL, /* per-dir config merger (default: override) */
2072 NULL, /* per-server config creator */
2073 NULL, /* per-server config merger (default: override) */
2074 fastcgi_cmds, /* command table */
2075 fastcgi_handlers, /* [9] content handlers */
2076 NULL, /* [2] URI-to-filename translation */
2077 check_user_authentication, /* [5] authenticate user_id */
2078 check_user_authorization, /* [6] authorize user_id */
2079 check_access, /* [4] check access (based on src & http headers) */
2080 NULL, /* [7] check/set MIME type */
2081 NULL, /* [8] fixups */
2082 NULL, /* [10] logger */
2083 NULL, /* [3] header-parser */
2084 fcgi_child_init, /* process initialization */
2085 fcgi_child_exit, /* process exit/cleanup */
2086 NULL /* [1] post read-request handling */