remove an unused variable, add cast when printing pid_t to prevent a warning
[mod_fastcgi.git] / mod_fastcgi.c
blobcb43576034ff7be9d779b2f4b31251de0b4618ae
1 /*
2 * mod_fastcgi.c --
4 * Apache server module for FastCGI.
6 * $Id: mod_fastcgi.c,v 1.119 2001/11/20 01:55:05 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)) return;
209 SetEvent(fcgi_event_handles[MBOX_EVENT]);
210 #else
211 ap_assert(buflen <= FCGI_MAX_MSG_LEN);
213 if (write(fcgi_pm_pipe[1], (const void *)buf, buflen) != buflen) {
214 ap_log_error(FCGI_LOG_WARN, fcgi_apache_main_server,
215 "FastCGI: write() to PM failed");
217 #endif
221 *----------------------------------------------------------------------
223 * init_module
225 * An Apache module initializer, called by the Apache core
226 * after reading the server config.
228 * Start the process manager no matter what, since there may be a
229 * request for dynamic FastCGI applications without any being
230 * configured as static applications. Also, check for the existence
231 * and create if necessary a subdirectory into which all dynamic
232 * sockets will go.
234 *----------------------------------------------------------------------
236 static void init_module(server_rec *s, pool *p)
238 const char *err;
240 /* Register to reset to default values when the config pool is cleaned */
241 ap_block_alarms();
242 ap_register_cleanup(p, NULL, fcgi_config_reset_globals, ap_null_cleanup);
243 ap_unblock_alarms();
245 ap_add_version_component("mod_fastcgi/" MOD_FASTCGI_VERSION);
247 fcgi_config_set_fcgi_uid_n_gid(1);
249 /* keep these handy */
250 fcgi_config_pool = p;
251 fcgi_apache_main_server = s;
253 #ifndef WIN32
254 /* Create Unix/Domain socket directory */
255 if ((err = fcgi_config_make_dir(p, fcgi_socket_dir)))
256 ap_log_error(FCGI_LOG_ERR, s, "FastCGI: %s", err);
257 #endif
259 /* Create Dynamic directory */
260 if ((err = fcgi_config_make_dynamic_dir(p, 1)))
261 ap_log_error(FCGI_LOG_ERR, s, "FastCGI: %s", err);
263 #ifndef WIN32
264 /* Spawn the PM only once. Under Unix, Apache calls init() routines
265 * twice, once before detach() and once after. Win32 doesn't detach.
266 * Under DSO, DSO modules are unloaded between the two init() calls.
267 * Under Unix, the -X switch causes two calls to init() but no detach
268 * (but all subprocesses are wacked so the PM is toasted anyway)! */
270 if (ap_standalone && ap_restart_time == 0)
271 return;
273 /* Create the pipe for comm with the PM */
274 if (pipe(fcgi_pm_pipe) < 0) {
275 ap_log_error(FCGI_LOG_ERR, s, "FastCGI: pipe() failed");
278 /* Start the Process Manager */
279 fcgi_pm_pid = ap_spawn_child(p, fcgi_pm_main, NULL, kill_only_once, NULL, NULL, NULL);
280 if (fcgi_pm_pid <= 0) {
281 ap_log_error(FCGI_LOG_ALERT, s,
282 "FastCGI: can't start the process manager, spawn_child() failed");
285 close(fcgi_pm_pipe[0]);
286 #endif
289 static void fcgi_child_init(server_rec *dc0, pool *dc1)
291 #ifdef WIN32
292 /* Create the MBOX, TERM, and WAKE event handlers */
293 fcgi_event_handles[0] = CreateEvent(NULL, FALSE, FALSE, NULL);
294 if (fcgi_event_handles[0] == NULL) {
295 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
296 "FastCGI: CreateEvent() failed");
298 fcgi_event_handles[1] = CreateEvent(NULL, FALSE, FALSE, NULL);
299 if (fcgi_event_handles[1] == NULL) {
300 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
301 "FastCGI: CreateEvent() failed");
303 fcgi_event_handles[2] = CreateEvent(NULL, FALSE, FALSE, NULL);
304 if (fcgi_event_handles[2] == NULL) {
305 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
306 "FastCGI: CreateEvent() failed");
309 /* Create the mbox mutex (PM - request threads) */
310 fcgi_dynamic_mbox_mutex = CreateMutex(NULL, FALSE, NULL);
311 if (fcgi_dynamic_mbox_mutex == NULL) {
312 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
313 "FastCGI: CreateMutex() failed");
316 /* Spawn of the process manager thread */
317 fcgi_pm_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)fcgi_pm_main, NULL, 0, NULL);
318 if (fcgi_pm_thread == NULL) {
319 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
320 "CreateThread() failed to spawn the process manager");
322 #endif
324 return;
327 static void fcgi_child_exit(server_rec *dc0, pool *dc1)
329 #ifdef WIN32
330 /* Signal the PM thread to exit*/
331 SetEvent(fcgi_event_handles[TERM_EVENT]);
333 /* Waiting on pm thread to exit */
334 WaitForSingleObject(fcgi_pm_thread, INFINITE);
335 #endif
337 return;
341 *----------------------------------------------------------------------
343 * get_header_line --
345 * Terminate a line: scan to the next newline, scan back to the
346 * first non-space character and store a terminating zero. Return
347 * the next character past the end of the newline.
349 * If the end of the string is reached, ASSERT!
351 * If the FIRST character(s) in the line are '\n' or "\r\n", the
352 * first character is replaced with a NULL and next character
353 * past the newline is returned. NOTE: this condition supercedes
354 * the processing of RFC-822 continuation lines.
356 * If continuation is set to 'TRUE', then it parses a (possible)
357 * sequence of RFC-822 continuation lines.
359 * Results:
360 * As above.
362 * Side effects:
363 * Termination byte stored in string.
365 *----------------------------------------------------------------------
367 static char *get_header_line(char *start, int continuation)
369 char *p = start;
370 char *end = start;
372 if(p[0] == '\r' && p[1] == '\n') { /* If EOL in 1st 2 chars */
373 p++; /* point to \n and stop */
374 } else if(*p != '\n') {
375 if(continuation) {
376 while(*p != '\0') {
377 if(*p == '\n' && p[1] != ' ' && p[1] != '\t')
378 break;
379 p++;
381 } else {
382 while(*p != '\0' && *p != '\n') {
383 p++;
388 ap_assert(*p != '\0');
389 end = p;
390 end++;
393 * Trim any trailing whitespace.
395 while(isspace((unsigned char)p[-1]) && p > start) {
396 p--;
399 *p = '\0';
400 return end;
404 *----------------------------------------------------------------------
406 * process_headers --
408 * Call with r->parseHeader == SCAN_CGI_READING_HEADERS
409 * and initial script output in fr->header.
411 * If the initial script output does not include the header
412 * terminator ("\r\n\r\n") process_headers returns with no side
413 * effects, to be called again when more script output
414 * has been appended to fr->header.
416 * If the initial script output includes the header terminator,
417 * process_headers parses the headers and determines whether or
418 * not the remaining script output will be sent to the client.
419 * If so, process_headers sends the HTTP response headers to the
420 * client and copies any non-header script output to the output
421 * buffer reqOutbuf.
423 * Results:
424 * none.
426 * Side effects:
427 * May set r->parseHeader to:
428 * SCAN_CGI_FINISHED -- headers parsed, returning script response
429 * SCAN_CGI_BAD_HEADER -- malformed header from script
430 * SCAN_CGI_INT_REDIRECT -- handler should perform internal redirect
431 * SCAN_CGI_SRV_REDIRECT -- handler should return REDIRECT
433 *----------------------------------------------------------------------
436 static const char *process_headers(request_rec *r, fcgi_request *fr)
438 char *p, *next, *name, *value;
439 int len, flag;
440 int hasContentType, hasStatus, hasLocation;
442 ap_assert(fr->parseHeader == SCAN_CGI_READING_HEADERS);
444 if (fr->header == NULL)
445 return NULL;
448 * Do we have the entire header? Scan for the blank line that
449 * terminates the header.
451 p = (char *)fr->header->elts;
452 len = fr->header->nelts;
453 flag = 0;
454 while(len-- && flag < 2) {
455 switch(*p) {
456 case '\r':
457 break;
458 case '\n':
459 flag++;
460 break;
461 case '\0':
462 case '\v':
463 case '\f':
464 name = "Invalid Character";
465 goto BadHeader;
466 break;
467 default:
468 flag = 0;
469 break;
471 p++;
474 /* Return (to be called later when we have more data)
475 * if we don't have an entire header. */
476 if (flag < 2)
477 return NULL;
480 * Parse all the headers.
482 fr->parseHeader = SCAN_CGI_FINISHED;
483 hasContentType = hasStatus = hasLocation = FALSE;
484 next = (char *)fr->header->elts;
485 for(;;) {
486 next = get_header_line(name = next, TRUE);
487 if (*name == '\0') {
488 break;
490 if ((p = strchr(name, ':')) == NULL) {
491 goto BadHeader;
493 value = p + 1;
494 while (p != name && isspace((unsigned char)*(p - 1))) {
495 p--;
497 if (p == name) {
498 goto BadHeader;
500 *p = '\0';
501 if (strpbrk(name, " \t") != NULL) {
502 *p = ' ';
503 goto BadHeader;
505 while (isspace((unsigned char)*value)) {
506 value++;
509 if (strcasecmp(name, "Status") == 0) {
510 int statusValue = strtol(value, NULL, 10);
512 if (hasStatus) {
513 goto DuplicateNotAllowed;
515 if (statusValue < 0) {
516 fr->parseHeader = SCAN_CGI_BAD_HEADER;
517 return ap_psprintf(r->pool, "invalid Status '%s'", value);
519 hasStatus = TRUE;
520 r->status = statusValue;
521 r->status_line = ap_pstrdup(r->pool, value);
522 continue;
525 if (fr->role == FCGI_RESPONDER) {
526 if (strcasecmp(name, "Content-type") == 0) {
527 if (hasContentType) {
528 goto DuplicateNotAllowed;
530 hasContentType = TRUE;
531 r->content_type = ap_pstrdup(r->pool, value);
532 continue;
535 if (strcasecmp(name, "Location") == 0) {
536 if (hasLocation) {
537 goto DuplicateNotAllowed;
539 hasLocation = TRUE;
540 ap_table_set(r->headers_out, "Location", value);
541 continue;
544 /* If the script wants them merged, it can do it */
545 ap_table_add(r->err_headers_out, name, value);
546 continue;
548 else {
549 ap_table_add(fr->authHeaders, name, value);
553 if (fr->role != FCGI_RESPONDER)
554 return NULL;
557 * Who responds, this handler or Apache?
559 if (hasLocation) {
560 const char *location = ap_table_get(r->headers_out, "Location");
562 * Based on internal redirect handling in mod_cgi.c...
564 * If a script wants to produce its own Redirect
565 * body, it now has to explicitly *say* "Status: 302"
567 if (r->status == 200) {
568 if(location[0] == '/') {
570 * Location is an relative path. This handler will
571 * consume all script output, then have Apache perform an
572 * internal redirect.
574 fr->parseHeader = SCAN_CGI_INT_REDIRECT;
575 return NULL;
576 } else {
578 * Location is an absolute URL. If the script didn't
579 * produce a Content-type header, this handler will
580 * consume all script output and then have Apache generate
581 * its standard redirect response. Otherwise this handler
582 * will transmit the script's response.
584 fr->parseHeader = SCAN_CGI_SRV_REDIRECT;
585 return NULL;
590 * We're responding. Send headers, buffer excess script output.
592 ap_send_http_header(r);
594 /* We need to reinstate our timeout, send_http_header() kill()s it */
595 ap_hard_timeout("FastCGI request processing", r);
597 if (r->header_only)
598 return NULL;
600 len = fr->header->nelts - (next - fr->header->elts);
601 ap_assert(len >= 0);
602 ap_assert(BufferLength(fr->clientOutputBuffer) == 0);
603 if (BufferFree(fr->clientOutputBuffer) < len) {
604 fr->clientOutputBuffer = fcgi_buf_new(r->pool, len);
606 ap_assert(BufferFree(fr->clientOutputBuffer) >= len);
607 if (len > 0) {
608 int sent = fcgi_buf_add_block(fr->clientOutputBuffer, next, len);
609 ap_assert(sent == len);
611 return NULL;
613 BadHeader:
614 /* Log first line of a multi-line header */
615 if ((p = strpbrk(name, "\r\n")) != NULL)
616 *p = '\0';
617 fr->parseHeader = SCAN_CGI_BAD_HEADER;
618 return ap_psprintf(r->pool, "malformed header '%s'", name);
620 DuplicateNotAllowed:
621 fr->parseHeader = SCAN_CGI_BAD_HEADER;
622 return ap_psprintf(r->pool, "duplicate header '%s'", name);
626 * Read from the client filling both the FastCGI server buffer and the
627 * client buffer with the hopes of buffering the client data before
628 * making the connect() to the FastCGI server. This prevents slow
629 * clients from keeping the FastCGI server in processing longer than is
630 * necessary.
632 static int read_from_client_n_queue(fcgi_request *fr)
634 char *end;
635 int count;
636 long int countRead;
638 while (BufferFree(fr->clientInputBuffer) > 0 || BufferFree(fr->serverOutputBuffer) > 0) {
639 fcgi_protocol_queue_client_buffer(fr);
641 if (fr->expectingClientContent <= 0)
642 return OK;
644 fcgi_buf_get_free_block_info(fr->clientInputBuffer, &end, &count);
645 if (count == 0)
646 return OK;
648 if ((countRead = ap_get_client_block(fr->r, end, count)) < 0)
649 return -1;
651 if (countRead == 0) {
652 fr->expectingClientContent = 0;
654 else {
655 fcgi_buf_add_update(fr->clientInputBuffer, countRead);
656 ap_reset_timeout(fr->r);
659 return OK;
662 static int write_to_client(fcgi_request *fr)
664 char *begin;
665 int count;
667 fcgi_buf_get_block_info(fr->clientOutputBuffer, &begin, &count);
668 if (count == 0)
669 return OK;
671 /* If fewer than count bytes are written, an error occured.
672 * ap_bwrite() typically forces a flushed write to the client, this
673 * effectively results in a block (and short packets) - it should
674 * be fixed, but I didn't win much support for the idea on new-httpd.
675 * So, without patching Apache, the best way to deal with this is
676 * to size the fcgi_bufs to hold all of the script output (within
677 * reason) so the script can be released from having to wait around
678 * for the transmission to the client to complete. */
679 #ifdef RUSSIAN_APACHE
680 if (ap_rwrite(begin, count, fr->r) != count) {
681 ap_log_rerror(FCGI_LOG_INFO_NOERRNO, fr->r,
682 "FastCGI: client stopped connection before send body completed");
683 return -1;
685 #else
686 if (ap_bwrite(fr->r->connection->client, begin, count) != count) {
687 ap_log_rerror(FCGI_LOG_INFO_NOERRNO, fr->r,
688 "FastCGI: client stopped connection before send body completed");
689 return -1;
691 #endif
693 ap_reset_timeout(fr->r);
695 /* Don't bother with a wrapped buffer, limiting exposure to slow
696 * clients. The BUFF routines don't allow a writev from above,
697 * and don't always memcpy to minimize small write()s, this should
698 * be fixed, but I didn't win much support for the idea on
699 * new-httpd - I'll have to _prove_ its a problem first.. */
701 /* The default behaviour used to be to flush with every write, but this
702 * can tie up the FastCGI server longer than is necessary so its an option now */
703 if (fr->fs && fr->fs->flush) {
704 #ifdef RUSSIAN_APACHE
705 if (ap_rflush(fr->r)) {
706 ap_log_rerror(FCGI_LOG_INFO_NOERRNO, fr->r,
707 "FastCGI: client stopped connection before send body completed");
708 return -1;
710 #else
711 if (ap_bflush(fr->r->connection->client)) {
712 ap_log_rerror(FCGI_LOG_INFO_NOERRNO, fr->r,
713 "FastCGI: client stopped connection before send body completed");
714 return -1;
716 #endif
717 ap_reset_timeout(fr->r);
720 fcgi_buf_toss(fr->clientOutputBuffer, count);
721 return OK;
724 /*******************************************************************************
725 * Determine the user and group the wrapper should be called with.
726 * Based on code in Apache's create_argv_cmd() (util_script.c).
728 static void set_uid_n_gid(request_rec *r, const char **user, const char **group)
730 if (fcgi_wrapper == NULL) {
731 *user = "-";
732 *group = "-";
733 return;
736 if (strncmp("/~", r->uri, 2) == 0) {
737 /* its a user dir uri, just send the ~user, and leave it to the PM */
738 char *end = strchr(r->uri + 2, '/');
740 if (end)
741 *user = memcpy(ap_pcalloc(r->pool, end - r->uri), r->uri + 1, end - r->uri - 1);
742 else
743 *user = ap_pstrdup(r->pool, r->uri + 1);
744 *group = "-";
746 else {
747 *user = ap_psprintf(r->pool, "%ld", (long)r->server->server_uid);
748 *group = ap_psprintf(r->pool, "%ld", (long)r->server->server_gid);
752 static void send_request_complete(fcgi_request *fr)
754 if (fr->completeTime.tv_sec)
756 struct timeval qtime, rtime;
758 timersub(&fr->queueTime, &fr->startTime, &qtime);
759 timersub(&fr->completeTime, &fr->queueTime, &rtime);
761 send_to_pm(FCGI_REQUEST_COMPLETE_JOB, fr->fs_path,
762 fr->user, fr->group,
763 qtime.tv_sec * 1000000 + qtime.tv_usec,
764 rtime.tv_sec * 1000000 + rtime.tv_usec);
768 #ifdef WIN32
770 static int set_nonblocking(const fcgi_request * fr, int nonblocking)
772 if (fr->using_npipe_io)
774 if (nonblocking)
776 DWORD mode = PIPE_NOWAIT | PIPE_READMODE_BYTE;
777 if (SetNamedPipeHandleState((HANDLE) fr->fd, &mode, NULL, NULL) == 0)
779 ap_log_rerror(FCGI_LOG_ERR, fr->r,
780 "FastCGI: SetNamedPipeHandleState() failed");
781 return -1;
785 else
787 unsigned long ioctl_arg = (nonblocking) ? 1 : 0;
788 if (ioctlsocket(fr->fd, FIONBIO, &ioctl_arg) != 0)
790 errno = WSAGetLastError();
791 ap_log_rerror(FCGI_LOG_ERR_ERRNO, fr->r,
792 "FastCGI: ioctlsocket() failed");
793 return -1;
797 return 0;
800 #else
802 static int set_nonblocking(const fcgi_request * fr, int nonblocking)
804 int nb_flag = 0;
805 int fd_flags = fcntl(fr->fd, F_GETFL, 0);
807 if (fd_flags < 0) return -1;
809 #if defined(O_NONBLOCK)
810 nb_flag = O_NONBLOCK;
811 #elif defined(O_NDELAY)
812 nb_flag = O_NDELAY;
813 #elif defined(FNDELAY)
814 nb_flag = FNDELAY;
815 #else
816 #error "TODO - don't read from app until all data from client is posted."
817 #endif
819 fd_flags = (nonblocking) ? (fd_flags | nb_flag) : (fd_flags & ~nb_flag);
821 return fcntl(fr->fd, F_SETFL, fd_flags);
824 #endif
826 /*******************************************************************************
827 * Close the connection to the FastCGI server. This is normally called by
828 * do_work(), but may also be called as in request pool cleanup.
830 static void close_connection_to_fs(fcgi_request *fr)
832 #ifdef WIN32
834 if (fr->fd != INVALID_SOCKET)
836 set_nonblocking(fr, FALSE);
838 if (fr->using_npipe_io)
840 CloseHandle((HANDLE) fr->fd);
842 else
844 closesocket(fr->fd);
847 fr->fd = INVALID_SOCKET;
850 #else /* ! WIN32 */
852 if (fr->fd >= 0)
854 set_nonblocking(fr, FALSE);
855 closesocket(fr->fd);
856 fr->fd = -1;
859 #endif /* ! WIN32 */
861 if (fr->dynamic && fr->keepReadingFromFcgiApp == FALSE)
863 /* XXX FCGI_REQUEST_COMPLETE_JOB is only sent for requests which complete
864 * normally WRT the fcgi app. There is no data sent for
865 * connect() timeouts or requests which complete abnormally.
866 * KillDynamicProcs() and RemoveRecords() need to be looked at
867 * to be sure they can reasonably handle these cases before
868 * sending these sort of stats - theres some funk in there.
870 if (fcgi_util_ticks(&fr->completeTime) < 0)
872 /* there's no point to aborting the request, just log it */
873 ap_log_error(FCGI_LOG_ERR, fr->r->server, "FastCGI: can't get time of day");
878 /*******************************************************************************
879 * Connect to the FastCGI server.
881 static int open_connection_to_fs(fcgi_request *fr)
883 struct timeval tval;
884 fd_set write_fds, read_fds;
885 int status;
886 request_rec * const r = fr->r;
887 pool * const rp = r->pool;
888 const char *socket_path = NULL;
889 struct sockaddr *socket_addr = NULL;
890 int socket_addr_len = 0;
891 #ifndef WIN32
892 const char *err = NULL;
893 #endif
895 /* Create the connection point */
896 if (fr->dynamic)
898 socket_path = fcgi_util_socket_hash_filename(rp, fr->fs_path, fr->user, fr->group);
899 socket_path = fcgi_util_socket_make_path_absolute(rp, socket_path, 1);
901 #ifndef WIN32
902 err = fcgi_util_socket_make_domain_addr(rp, (struct sockaddr_un **)&socket_addr,
903 &socket_addr_len, socket_path);
904 if (err) {
905 ap_log_rerror(FCGI_LOG_ERR, r,
906 "FastCGI: failed to connect to server \"%s\": "
907 "%s", fr->fs_path, err);
908 return FCGI_FAILED;
910 #endif
912 else
914 #ifdef WIN32
915 if (fr->fs->dest_addr != NULL) {
916 socket_addr = fr->fs->dest_addr;
918 else if (fr->fs->socket_addr) {
919 socket_addr = fr->fs->socket_addr;
921 else {
922 socket_path = fr->fs->socket_path;
924 #else
925 socket_addr = fr->fs->socket_addr;
926 #endif
927 socket_addr_len = fr->fs->socket_addr_len;
930 if (fr->dynamic)
932 #ifdef WIN32
933 if (fr->fs && fr->fs->restartTime)
934 #else
935 struct stat sock_stat;
937 if (stat(socket_path, &sock_stat) == 0)
938 #endif
940 // It exists
941 if (dynamicAutoUpdate)
943 struct stat app_stat;
945 /* TODO: follow sym links */
947 if (stat(fr->fs_path, &app_stat) == 0)
949 #ifdef WIN32
950 if (fr->fs->restartTime < app_stat.st_mtime)
951 #else
952 if (sock_stat.st_mtime < app_stat.st_mtime)
953 #endif
955 #ifndef WIN32
956 struct timeval tv = {1, 0};
957 #endif
959 * There's a newer one, request a restart.
961 send_to_pm(FCGI_SERVER_RESTART_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
963 #ifdef WIN32
964 Sleep(1000);
965 #else
966 /* Avoid sleep/alarm interactions */
967 ap_select(0, NULL, NULL, NULL, &tv);
968 #endif
973 else
975 send_to_pm(FCGI_SERVER_START_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
977 /* Wait until it looks like its running */
979 for (;;)
981 #ifdef WIN32
982 Sleep(1000);
984 fr->fs = fcgi_util_fs_get_by_id(fr->fs_path, 0, 0);
986 if (fr->fs && fr->fs->restartTime)
987 #else
988 struct timeval tv = {1, 0};
990 /* Avoid sleep/alarm interactions */
991 ap_select(0, NULL, NULL, NULL, &tv);
993 if (stat(socket_path, &sock_stat) == 0)
994 #endif
996 break;
1002 #ifdef WIN32
1003 if (socket_path)
1005 BOOL ready;
1006 int connect_time;
1008 DWORD interval;
1009 int max_connect_time = FCGI_NAMED_PIPE_CONNECT_TIMEOUT;
1011 fr->using_npipe_io = TRUE;
1013 if (fr->dynamic)
1015 interval = dynamicPleaseStartDelay * 1000;
1017 if (dynamicAppConnectTimeout) {
1018 max_connect_time = dynamicAppConnectTimeout;
1021 else
1023 interval = FCGI_NAMED_PIPE_CONNECT_TIMEOUT * 1000;
1025 if (fr->fs->appConnectTimeout) {
1026 max_connect_time = fr->fs->appConnectTimeout;
1030 if (fcgi_util_ticks(&fr->startTime) < 0) {
1031 ap_log_rerror(FCGI_LOG_ERR, r,
1032 "FastCGI: failed to connect to server \"%s\": "
1033 "can't get time of day", fr->fs_path);
1034 return FCGI_FAILED;
1039 fr->fd = (SOCKET) CreateFile(socket_path,
1040 GENERIC_READ | GENERIC_WRITE,
1041 FILE_SHARE_READ | FILE_SHARE_WRITE,
1042 NULL, // no security attributes
1043 OPEN_EXISTING, // opens existing pipe
1044 FILE_ATTRIBUTE_NORMAL, // default attributes
1045 NULL); // no template file
1047 if (fr->fd != (SOCKET) INVALID_HANDLE_VALUE) {
1048 break;
1051 if (GetLastError() != ERROR_PIPE_BUSY
1052 && GetLastError() != ERROR_FILE_NOT_FOUND)
1054 ap_log_rerror(FCGI_LOG_ERR, r,
1055 "FastCGI: failed to connect to server \"%s\": "
1056 "CreateFile() failed", fr->fs_path);
1057 return FCGI_FAILED;
1060 // All pipe instances are busy, so wait
1061 ready = WaitNamedPipe(socket_path, interval);
1063 if (fr->dynamic && !ready) {
1064 send_to_pm(FCGI_REQUEST_TIMEOUT_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
1067 if (fcgi_util_ticks(&fr->queueTime) < 0) {
1068 ap_log_rerror(FCGI_LOG_ERR, r,
1069 "FastCGI: failed to connect to server \"%s\": "
1070 "can't get time of day", fr->fs_path);
1071 return FCGI_FAILED;
1074 connect_time = fr->queueTime.tv_sec - fr->startTime.tv_sec;
1076 FCGIDBG5("interval=%d, max_connect_time=%d, connect_time=%d, ready=%d", interval, max_connect_time, connect_time, ready);
1078 } while (connect_time < max_connect_time);
1080 if (fr->fd == (SOCKET) INVALID_HANDLE_VALUE) {
1081 ap_log_rerror(FCGI_LOG_ERR, r,
1082 "FastCGI: failed to connect to server \"%s\": "
1083 "CreateFile()/WaitNamedPipe() timed out", fr->fs_path);
1084 fr->fd = INVALID_SOCKET;
1085 return FCGI_FAILED;
1088 FCGIDBG2("got_named_pipe_connect: %s", fr->fs_path);
1090 return FCGI_OK;
1092 #endif
1094 /* Create the socket */
1095 fr->fd = socket(socket_addr->sa_family, SOCK_STREAM, 0);
1097 #ifdef WIN32
1098 if (fr->fd == INVALID_SOCKET) {
1099 errno = WSAGetLastError(); // Not sure this is going to work as expected
1100 #else
1101 if (fr->fd < 0) {
1102 #endif
1103 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1104 "FastCGI: failed to connect to server \"%s\": "
1105 "socket() failed", fr->fs_path);
1106 return FCGI_FAILED;
1109 #ifndef WIN32
1110 if (fr->fd >= FD_SETSIZE) {
1111 ap_log_rerror(FCGI_LOG_ERR, r,
1112 "FastCGI: failed to connect to server \"%s\": "
1113 "socket file descriptor (%u) is larger than "
1114 "FD_SETSIZE (%u), you probably need to rebuild Apache with a "
1115 "larger FD_SETSIZE", fr->fs_path, fr->fd, FD_SETSIZE);
1116 return FCGI_FAILED;
1118 #endif
1120 /* If appConnectTimeout is non-zero, setup do a non-blocking connect */
1121 if ((fr->dynamic && dynamicAppConnectTimeout) || (!fr->dynamic && fr->fs->appConnectTimeout)) {
1122 set_nonblocking(fr, TRUE);
1125 if (fr->dynamic && fcgi_util_ticks(&fr->startTime) < 0) {
1126 ap_log_rerror(FCGI_LOG_ERR, r,
1127 "FastCGI: failed to connect to server \"%s\": "
1128 "can't get time of day", fr->fs_path);
1129 return FCGI_FAILED;
1132 /* Connect */
1133 if (connect(fr->fd, (struct sockaddr *)socket_addr, socket_addr_len) == 0)
1134 goto ConnectionComplete;
1136 #ifdef WIN32
1138 errno = WSAGetLastError();
1139 if (errno != WSAEWOULDBLOCK) {
1140 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1141 "FastCGI: failed to connect to server \"%s\": "
1142 "connect() failed", fr->fs_path);
1143 return FCGI_FAILED;
1146 #else
1148 /* ECONNREFUSED means the listen queue is full (or there isn't one).
1149 * With dynamic I can at least make sure the PM knows this is occuring */
1150 if (fr->dynamic && errno == ECONNREFUSED) {
1151 /* @@@ This might be better as some other "kind" of message */
1152 send_to_pm(FCGI_REQUEST_TIMEOUT_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
1154 errno = ECONNREFUSED;
1157 if (errno != EINPROGRESS) {
1158 ap_log_rerror(FCGI_LOG_ERR, r,
1159 "FastCGI: failed to connect to server \"%s\": "
1160 "connect() failed", fr->fs_path);
1161 return FCGI_FAILED;
1164 #endif
1166 /* The connect() is non-blocking */
1168 errno = 0;
1170 if (fr->dynamic) {
1171 do {
1172 FD_ZERO(&write_fds);
1173 FD_SET(fr->fd, &write_fds);
1174 read_fds = write_fds;
1175 tval.tv_sec = dynamicPleaseStartDelay;
1176 tval.tv_usec = 0;
1178 status = ap_select((fr->fd+1), &read_fds, &write_fds, NULL, &tval);
1179 if (status < 0)
1180 break;
1182 if (fcgi_util_ticks(&fr->queueTime) < 0) {
1183 ap_log_rerror(FCGI_LOG_ERR, r,
1184 "FastCGI: failed to connect to server \"%s\": "
1185 "can't get time of day", fr->fs_path);
1186 return FCGI_FAILED;
1189 if (status > 0)
1190 break;
1192 /* select() timed out */
1193 send_to_pm(FCGI_REQUEST_TIMEOUT_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
1194 } while ((fr->queueTime.tv_sec - fr->startTime.tv_sec) < (int)dynamicAppConnectTimeout);
1196 /* XXX These can be moved down when dynamic vars live is a struct */
1197 if (status == 0) {
1198 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1199 "FastCGI: failed to connect to server \"%s\": "
1200 "connect() timed out (appConnTimeout=%dsec)",
1201 fr->fs_path, dynamicAppConnectTimeout);
1202 return FCGI_FAILED;
1204 } /* dynamic */
1205 else {
1206 tval.tv_sec = fr->fs->appConnectTimeout;
1207 tval.tv_usec = 0;
1208 FD_ZERO(&write_fds);
1209 FD_SET(fr->fd, &write_fds);
1210 read_fds = write_fds;
1212 status = ap_select((fr->fd+1), &read_fds, &write_fds, NULL, &tval);
1214 if (status == 0) {
1215 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1216 "FastCGI: failed to connect to server \"%s\": "
1217 "connect() timed out (appConnTimeout=%dsec)",
1218 fr->fs_path, dynamicAppConnectTimeout);
1219 return FCGI_FAILED;
1221 } /* !dynamic */
1223 if (status < 0) {
1224 #ifdef WIN32
1225 errno = WSAGetLastError();
1226 #endif
1227 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1228 "FastCGI: failed to connect to server \"%s\": "
1229 "select() failed", fr->fs_path);
1230 return FCGI_FAILED;
1233 if (FD_ISSET(fr->fd, &write_fds) || FD_ISSET(fr->fd, &read_fds)) {
1234 int error = 0;
1235 NET_SIZE_T len = sizeof(error);
1237 if (getsockopt(fr->fd, SOL_SOCKET, SO_ERROR, (char *)&error, &len) < 0) {
1238 /* Solaris pending error */
1239 #ifdef WIN32
1240 errno = WSAGetLastError();
1241 #endif
1242 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1243 "FastCGI: failed to connect to server \"%s\": "
1244 "select() failed (Solaris pending error)", fr->fs_path);
1245 return FCGI_FAILED;
1248 if (error != 0) {
1249 /* Berkeley-derived pending error */
1250 errno = error;
1251 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1252 "FastCGI: failed to connect to server \"%s\": "
1253 "select() failed (pending error)", fr->fs_path);
1254 return FCGI_FAILED;
1257 else {
1258 #ifdef WIN32
1259 errno = WSAGetLastError();
1260 #endif
1261 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1262 "FastCGI: failed to connect to server \"%s\": "
1263 "select() error - THIS CAN'T HAPPEN!", fr->fs_path);
1264 return FCGI_FAILED;
1267 ConnectionComplete:
1268 /* Return to blocking mode if it was set up */
1269 if ((fr->dynamic && dynamicAppConnectTimeout) || (!fr->dynamic && fr->fs->appConnectTimeout)) {
1270 set_nonblocking(fr, FALSE);
1273 #ifdef TCP_NODELAY
1274 if (socket_addr->sa_family == AF_INET) {
1275 /* We shouldn't be sending small packets and there's no application
1276 * level ack of the data we send, so disable Nagle */
1277 int set = 1;
1278 setsockopt(fr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&set, sizeof(set));
1280 #endif
1282 return FCGI_OK;
1285 static int server_error(fcgi_request *fr)
1287 #if defined(SIGPIPE) && MODULE_MAGIC_NUMBER < 19990320
1288 /* Make sure we leave with Apache's sigpipe_handler in place */
1289 if (fr->apache_sigpipe_handler != NULL)
1290 signal(SIGPIPE, fr->apache_sigpipe_handler);
1291 #endif
1292 close_connection_to_fs(fr);
1293 ap_kill_timeout(fr->r);
1294 return SERVER_ERROR;
1297 static void cleanup(void *data)
1299 fcgi_request * const fr = (fcgi_request *) data;
1301 if (fr == NULL) return;
1303 /* its more than likely already run, but... */
1304 close_connection_to_fs(fr);
1306 send_request_complete(fr);
1308 if (fr->fs_stderr_len) {
1309 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, fr->r,
1310 "FastCGI: server \"%s\" stderr: %s", fr->fs_path, fr->fs_stderr);
1314 /*----------------------------------------------------------------------
1315 * This is the core routine for moving data between the FastCGI
1316 * application and the Web server's client.
1318 static int do_work(request_rec *r, fcgi_request *fr)
1320 struct timeval timeOut, dynamic_last_activity_time = {0, 0};
1321 fd_set read_set, write_set;
1322 int status = 0, idle_timeout;
1323 int numFDs, dynamic_first_read = fr->dynamic ? 1 : 0;
1324 int doClientWrite;
1325 int envSent = FALSE; /* has the complete ENV been buffered? */
1326 env_status env;
1327 pool *rp = r->pool;
1328 const char *err = NULL;
1330 FD_ZERO(&read_set);
1331 FD_ZERO(&write_set);
1333 fcgi_protocol_queue_begin_request(fr);
1335 /* Buffer as much of the environment as we can fit */
1336 env.envp = NULL;
1337 envSent = fcgi_protocol_queue_env(r, fr, &env);
1339 /* Start the Apache dropdead timer. See comments at top of file. */
1340 ap_hard_timeout("buffering of FastCGI client data", r);
1342 /* Read as much as possible from the client. */
1343 if (fr->role == FCGI_RESPONDER) {
1344 status = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR);
1345 if (status != OK) {
1346 ap_kill_timeout(r);
1347 return status;
1349 fr->expectingClientContent = (ap_should_client_block(r) != 0);
1351 if (read_from_client_n_queue(fr) != OK)
1352 return server_error(fr);
1355 ap_block_alarms();
1356 ap_register_cleanup(rp, (void *)fr, cleanup, ap_null_cleanup);
1357 ap_unblock_alarms();
1359 /* Connect to the FastCGI Application */
1360 ap_hard_timeout("connect() to FastCGI server", r);
1361 if (open_connection_to_fs(fr) != FCGI_OK) {
1362 return server_error(fr);
1365 numFDs = fr->fd + 1;
1366 idle_timeout = fr->dynamic ? dynamic_idle_timeout : fr->fs->idle_timeout;
1368 if (dynamic_first_read) {
1369 dynamic_last_activity_time = fr->startTime;
1371 if (dynamicAppConnectTimeout) {
1372 struct timeval qwait;
1373 timersub(&fr->queueTime, &fr->startTime, &qwait);
1374 dynamic_first_read = qwait.tv_sec / dynamicPleaseStartDelay + 1;
1378 /* @@@ We never reset the timer in this loop, most folks don't mess w/
1379 * Timeout directive which means we've got the 5 min default which is way
1380 * to long to tie up a fs. We need a better/configurable solution that
1381 * uses the select */
1382 ap_hard_timeout("FastCGI request processing", r);
1384 /* Before we do any writing, set the connection non-blocking */
1385 set_nonblocking(fr, TRUE);
1387 /* The socket is writeable, so get the first write out of the way */
1388 if (fcgi_buf_get_to_fd(fr->serverOutputBuffer, fr->fd) < 0) {
1389 #ifdef WIN32
1390 if (! fr->using_npipe_io)
1391 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1392 "FastCGI: comm with server \"%s\" aborted: write failed", fr->fs_path);
1393 else
1394 #endif
1395 ap_log_rerror(FCGI_LOG_ERR, r,
1396 "FastCGI: comm with server \"%s\" aborted: write failed", fr->fs_path);
1397 return server_error(fr);
1400 while (fr->keepReadingFromFcgiApp
1401 || BufferLength(fr->serverInputBuffer) > 0
1402 || BufferLength(fr->clientOutputBuffer) > 0) {
1404 /* If we didn't buffer all of the environment yet, buffer some more */
1405 if (!envSent)
1406 envSent = fcgi_protocol_queue_env(r, fr, &env);
1408 /* Read as much as possible from the client. */
1409 if (fr->role == FCGI_RESPONDER && !fr->eofSent && envSent) {
1411 /* ap_get_client_block() (called in read_from_client_n_queue()
1412 * can't handle a non-blocking fd, its a bummer. We might be
1413 * able to completely bypass the Apache BUFF routines in still
1414 * do it, but thats a major hassle. Apache 2.X will handle it,
1415 * and then so will we. */
1417 if (read_from_client_n_queue(fr) != OK)
1418 return server_error(fr);
1421 /* To avoid deadlock, don't do a blocking select to write to
1422 * the FastCGI application without selecting to read from the
1423 * FastCGI application.
1425 doClientWrite = FALSE;
1426 if (fr->keepReadingFromFcgiApp && BufferFree(fr->serverInputBuffer) > 0) {
1428 #ifdef WIN32
1429 DWORD bytesavail = 0;
1431 if (!fr->using_npipe_io) {
1432 #endif
1433 FD_SET(fr->fd, &read_set);
1435 /* Is data buffered for output to the FastCGI server? */
1436 if (BufferLength(fr->serverOutputBuffer) > 0) {
1437 FD_SET(fr->fd, &write_set);
1438 } else {
1439 FD_CLR(fr->fd, &write_set);
1441 #ifdef WIN32
1443 #endif
1445 * If there's data buffered to send to the client, don't
1446 * wait indefinitely for the FastCGI app; the app might
1447 * be doing server push.
1449 if (BufferLength(fr->clientOutputBuffer) > 0) {
1450 timeOut.tv_sec = 0;
1451 timeOut.tv_usec = 100000; /* 0.1 sec */
1453 else if (dynamic_first_read) {
1454 int delay;
1455 struct timeval qwait;
1457 if (fcgi_util_ticks(&fr->queueTime) < 0) {
1458 ap_log_rerror(FCGI_LOG_ERR, r, "FastCGI: can't get time of day");
1459 return server_error(fr);
1462 /* Check for idle_timeout */
1463 if (status) {
1464 dynamic_last_activity_time = fr->queueTime;
1466 else {
1467 struct timeval idle_time;
1468 timersub(&fr->queueTime, &dynamic_last_activity_time, &idle_time);
1469 if (idle_time.tv_sec > idle_timeout) {
1470 send_to_pm(FCGI_REQUEST_TIMEOUT_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
1471 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1472 "FastCGI: comm with (dynamic) server \"%s\" aborted: (first read) idle timeout (%d sec)",
1473 fr->fs_path, idle_timeout);
1474 return server_error(fr);
1478 timersub(&fr->queueTime, &fr->startTime, &qwait);
1480 delay = dynamic_first_read * dynamicPleaseStartDelay;
1481 if (qwait.tv_sec < delay) {
1482 timeOut.tv_sec = delay;
1483 timeOut.tv_usec = 100000; /* fudge for select() slop */
1484 timersub(&timeOut, &qwait, &timeOut);
1486 else {
1487 /* Killed time somewhere.. client read? */
1488 send_to_pm(FCGI_REQUEST_TIMEOUT_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
1489 dynamic_first_read = qwait.tv_sec / dynamicPleaseStartDelay + 1;
1490 timeOut.tv_sec = dynamic_first_read * dynamicPleaseStartDelay;
1491 timeOut.tv_usec = 100000; /* fudge for select() slop */
1492 timersub(&timeOut, &qwait, &timeOut);
1495 else {
1496 timeOut.tv_sec = idle_timeout;
1497 timeOut.tv_usec = 0;
1500 #ifdef WIN32
1501 if (!fr->using_npipe_io) {
1502 #endif
1503 if ((status = ap_select(numFDs, &read_set, &write_set, NULL, &timeOut)) < 0) {
1504 #ifdef WIN32
1505 errno = WSAGetLastError();
1506 #endif
1507 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1508 "FastCGI: comm with server \"%s\" aborted: select() failed", fr->fs_path);
1509 return server_error(fr);
1511 #ifdef WIN32
1513 else {
1514 int stopTime = time(NULL) + timeOut.tv_sec;
1516 if (BufferLength(fr->serverOutputBuffer) == 0)
1518 status = 0;
1520 while ((timeOut.tv_sec != 0) && (time(NULL) <= stopTime))
1522 BOOL ok = PeekNamedPipe((HANDLE) fr->fd,NULL, 0, NULL, &bytesavail, NULL);
1523 if (! ok)
1525 ap_log_rerror(FCGI_LOG_ERR, r,
1526 "FastCGI: comm with sever \"%s\" aborted: PeekNamedPipe() failed",
1527 fr->fs_path);
1528 return server_error(fr);
1530 if (bytesavail > 0)
1532 status =1;
1533 break;
1535 Sleep(100);
1538 else {
1539 status = 1;
1542 #endif
1544 if (status == 0) {
1545 if (BufferLength(fr->clientOutputBuffer) > 0) {
1546 doClientWrite = TRUE;
1548 else if (dynamic_first_read) {
1549 struct timeval qwait;
1551 if (fcgi_util_ticks(&fr->queueTime) < 0) {
1552 ap_log_rerror(FCGI_LOG_ERR, r, "FastCGI: can't get time of day");
1553 return server_error(fr);
1556 timersub(&fr->queueTime, &fr->startTime, &qwait);
1558 send_to_pm(FCGI_REQUEST_TIMEOUT_JOB, fr->fs_path, fr->user, fr->group, 0, 0);
1560 dynamic_first_read = qwait.tv_sec / dynamicPleaseStartDelay + 1;
1562 else {
1563 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1564 "FastCGI: comm with server \"%s\" aborted: idle timeout (%d sec)",
1565 fr->fs_path, idle_timeout);
1566 return server_error(fr);
1570 #if defined(SIGPIPE) && MODULE_MAGIC_NUMBER < 19990320
1571 /* Disable Apache's SIGPIPE handler */
1572 fr->apache_sigpipe_handler = signal(SIGPIPE, SIG_IGN);
1573 #endif
1575 /* Read from the FastCGI server */
1576 #ifdef WIN32
1577 if ((fr->using_npipe_io
1578 && (BufferFree(fr->serverInputBuffer) > 0)
1579 && PeekNamedPipe((HANDLE) fr->fd, NULL, 0, NULL, &bytesavail, NULL)
1580 && (bytesavail > 0))
1581 || FD_ISSET(fr->fd, &read_set)) {
1582 #else
1583 if (FD_ISSET(fr->fd, &read_set)) {
1584 #endif
1585 if (dynamic_first_read) {
1586 dynamic_first_read = 0;
1587 if (fcgi_util_ticks(&fr->queueTime) < 0) {
1588 ap_log_rerror(FCGI_LOG_ERR, r, "FastCGI: can't get time of day");
1589 return server_error(fr);
1593 if ((status = fcgi_buf_add_fd(fr->serverInputBuffer, fr->fd)) < 0) {
1594 #ifdef WIN32
1595 if (! fr->using_npipe_io)
1596 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1597 "FastCGI: comm with server \"%s\" aborted: read failed", fr->fs_path);
1598 else
1599 #endif
1600 ap_log_rerror(FCGI_LOG_ERR, r,
1601 "FastCGI: comm with server \"%s\" aborted: read failed", fr->fs_path);
1602 return server_error(fr);
1605 if (status == 0) {
1606 fr->keepReadingFromFcgiApp = FALSE;
1607 close_connection_to_fs(fr);
1611 /* Write to the FastCGI server */
1612 #ifdef WIN32
1613 /* XXX this is broke because it will cause a spin if the app doesn't read */
1614 if ((fr->using_npipe_io && (BufferLength(fr->serverOutputBuffer) > 0))
1615 || FD_ISSET(fr->fd, &write_set)) {
1616 #else
1617 if (FD_ISSET(fr->fd, &write_set)) {
1618 #endif
1620 if (fcgi_buf_get_to_fd(fr->serverOutputBuffer, fr->fd) < 0) {
1621 /* XXX this could fail even if the app finished sending a response */
1622 #ifdef WIN32
1623 if (! fr->using_npipe_io)
1624 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1625 "FastCGI: comm with server \"%s\" aborted: write failed", fr->fs_path);
1626 else
1627 #endif
1628 ap_log_rerror(FCGI_LOG_ERR, r,
1629 "FastCGI: comm with server \"%s\" aborted: write failed", fr->fs_path);
1630 return server_error(fr);
1634 #if defined(SIGPIPE) && MODULE_MAGIC_NUMBER < 19990320
1635 /* Reinstall Apache's SIGPIPE handler */
1636 signal(SIGPIPE, fr->apache_sigpipe_handler);
1637 #endif
1639 } else {
1640 doClientWrite = TRUE;
1643 if (fr->role == FCGI_RESPONDER && doClientWrite) {
1645 if (write_to_client(fr) != OK) {
1646 #if defined(SIGPIPE) && MODULE_MAGIC_NUMBER < 19990320
1647 /* Make sure we leave with Apache's sigpipe_handler in place */
1648 if (fr->apache_sigpipe_handler != NULL)
1649 signal(SIGPIPE, fr->apache_sigpipe_handler);
1650 #endif
1651 close_connection_to_fs(fr);
1652 ap_kill_timeout(fr->r);
1653 return OK;
1657 if (fcgi_protocol_dequeue(rp, fr) != OK)
1658 return server_error(fr);
1660 if (fr->keepReadingFromFcgiApp && fr->exitStatusSet) {
1661 /* we're done talking to the fcgi app */
1662 fr->keepReadingFromFcgiApp = FALSE;
1663 close_connection_to_fs(fr);
1666 if (fr->parseHeader == SCAN_CGI_READING_HEADERS) {
1667 if ((err = process_headers(r, fr))) {
1668 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1669 "FastCGI: comm with server \"%s\" aborted: error parsing headers: %s", fr->fs_path, err);
1670 return server_error(fr);
1674 } /* while */
1676 switch (fr->parseHeader) {
1678 case SCAN_CGI_FINISHED:
1679 if (fr->role == FCGI_RESPONDER) {
1680 #ifdef RUSSIAN_APACHE
1681 ap_rflush(r);
1682 #else
1683 ap_bflush(r->connection->client);
1684 #endif
1685 ap_bgetopt(r->connection->client, BO_BYTECT, &r->bytes_sent);
1687 break;
1689 case SCAN_CGI_READING_HEADERS:
1690 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1691 "FastCGI: incomplete headers (%d bytes) received from server \"%s\"",
1692 fr->header->nelts, fr->fs_path);
1693 return server_error(fr);
1695 case SCAN_CGI_BAD_HEADER:
1696 return server_error(fr);
1698 case SCAN_CGI_INT_REDIRECT:
1699 case SCAN_CGI_SRV_REDIRECT:
1701 * XXX We really should be soaking all client input
1702 * and all script output. See mod_cgi.c.
1703 * There's other differences we need to pick up here as well!
1704 * This has to be revisited.
1706 break;
1708 default:
1709 ap_assert(FALSE);
1712 ap_kill_timeout(r);
1713 return OK;
1716 static fcgi_request *create_fcgi_request(request_rec * const r, const char *fs_path)
1718 struct stat *my_finfo;
1719 pool * const p = r->pool;
1720 fcgi_server *fs;
1721 fcgi_request * const fr = (fcgi_request *)ap_pcalloc(p, sizeof(fcgi_request));
1723 if (fs_path) {
1724 my_finfo = (struct stat *)ap_palloc(p, sizeof(struct stat));
1725 if (stat(fs_path, my_finfo) < 0) {
1726 ap_log_rerror(FCGI_LOG_ERR_ERRNO, r,
1727 "FastCGI: stat() of \"%s\" failed", fs_path);
1728 return NULL;
1731 else {
1732 my_finfo = &r->finfo;
1733 fs_path = r->filename;
1736 fs = fcgi_util_fs_get_by_id(fs_path, r->server->server_uid, r->server->server_gid);
1737 if (fs == NULL) {
1738 /* Its a request for a dynamic FastCGI application */
1739 const char * const err =
1740 fcgi_util_fs_is_path_ok(p, fs_path, my_finfo);
1742 if (err) {
1743 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r, "FastCGI: invalid (dynamic) server \"%s\": %s", fs_path, err);
1744 return NULL;
1748 fr->serverInputBuffer = fcgi_buf_new(p, SERVER_BUFSIZE);
1749 fr->serverOutputBuffer = fcgi_buf_new(p, SERVER_BUFSIZE);
1750 fr->clientInputBuffer = fcgi_buf_new(p, SERVER_BUFSIZE);
1751 fr->clientOutputBuffer = fcgi_buf_new(p, SERVER_BUFSIZE);
1752 fr->erBufPtr = fcgi_buf_new(p, sizeof(FCGI_EndRequestBody) + 1);
1753 fr->gotHeader = FALSE;
1754 fr->parseHeader = SCAN_CGI_READING_HEADERS;
1755 fr->header = ap_make_array(p, 1, 1);
1756 fr->fs_stderr = NULL;
1757 fr->r = r;
1758 fr->readingEndRequestBody = FALSE;
1759 fr->exitStatus = 0;
1760 fr->exitStatusSet = FALSE;
1761 fr->requestId = 1; /* anything but zero is OK here */
1762 fr->eofSent = FALSE;
1763 fr->role = FCGI_RESPONDER;
1764 fr->expectingClientContent = FALSE;
1765 fr->keepReadingFromFcgiApp = TRUE;
1766 fr->fs = fs;
1767 fr->fs_path = fs_path;
1768 fr->authHeaders = ap_make_table(p, 10);
1769 #ifdef WIN32
1770 fr->fd = INVALID_SOCKET;
1771 fr->dynamic = ((fs == NULL) || (fs->directive == APP_CLASS_DYNAMIC)) ? TRUE : FALSE;
1772 fr->using_npipe_io = FALSE;
1773 #else
1774 fr->dynamic = (fs == NULL) ? TRUE : FALSE;
1775 fr->fd = -1;
1776 #endif
1778 set_uid_n_gid(r, &fr->user, &fr->group);
1780 return fr;
1784 *----------------------------------------------------------------------
1786 * handler --
1788 * This routine gets called for a request that corresponds to
1789 * a FastCGI connection. It performs the request synchronously.
1791 * Results:
1792 * Final status of request: OK or NOT_FOUND or SERVER_ERROR.
1794 * Side effects:
1795 * Request performed.
1797 *----------------------------------------------------------------------
1800 /* Stolen from mod_cgi.c..
1801 * KLUDGE --- for back-combatibility, we don't have to check ExecCGI
1802 * in ScriptAliased directories, which means we need to know if this
1803 * request came through ScriptAlias or not... so the Alias module
1804 * leaves a note for us.
1806 static int apache_is_scriptaliased(request_rec *r)
1808 const char *t = ap_table_get(r->notes, "alias-forced-type");
1809 return t && (!strcasecmp(t, "cgi-script"));
1812 /* If a script wants to produce its own Redirect body, it now
1813 * has to explicitly *say* "Status: 302". If it wants to use
1814 * Apache redirects say "Status: 200". See process_headers().
1816 static int post_process_for_redirects(request_rec * const r,
1817 const fcgi_request * const fr)
1819 switch(fr->parseHeader) {
1820 case SCAN_CGI_INT_REDIRECT:
1822 /* @@@ There are still differences between the handling in
1823 * mod_cgi and mod_fastcgi. This needs to be revisited.
1825 /* We already read the message body (if any), so don't allow
1826 * the redirected request to think it has one. We can ignore
1827 * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
1829 r->method = "GET";
1830 r->method_number = M_GET;
1831 ap_table_unset(r->headers_in, "Content-length");
1833 ap_internal_redirect_handler(ap_table_get(r->headers_out, "Location"), r);
1834 return OK;
1836 case SCAN_CGI_SRV_REDIRECT:
1837 return REDIRECT;
1839 default:
1840 return OK;
1844 /******************************************************************************
1845 * Process fastcgi-script requests. Based on mod_cgi::cgi_handler().
1847 static int content_handler(request_rec *r)
1849 fcgi_request *fr = NULL;
1850 int ret;
1852 FCGIDBG1("->content_handler()");
1854 /* Setup a new FastCGI request */
1855 if ((fr = create_fcgi_request(r, NULL)) == NULL)
1856 return SERVER_ERROR;
1858 /* If its a dynamic invocation, make sure scripts are OK here */
1859 if (fr->dynamic && !(ap_allow_options(r) & OPT_EXECCGI) && !apache_is_scriptaliased(r)) {
1860 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1861 "FastCGI: \"ExecCGI Option\" is off in this directory: %s", r->uri);
1862 return SERVER_ERROR;
1865 /* Process the fastcgi-script request */
1866 if ((ret = do_work(r, fr)) != OK)
1867 return ret;
1869 /* Special case redirects */
1870 ret = post_process_for_redirects(r, fr);
1872 FCGIDBG1("<-content_handler()");
1874 return ret;
1878 static int post_process_auth_passed_header(table *t, const char *key, const char * const val)
1880 if (strncasecmp(key, "Variable-", 9) == 0)
1881 key += 9;
1883 ap_table_setn(t, key, val);
1884 return 1;
1887 static int post_process_auth_passed_compat_header(table *t, const char *key, const char * const val)
1889 if (strncasecmp(key, "Variable-", 9) == 0)
1890 ap_table_setn(t, key + 9, val);
1892 return 1;
1895 static int post_process_auth_failed_header(table * const t, const char * const key, const char * const val)
1897 ap_table_setn(t, key, val);
1898 return 1;
1901 static void post_process_auth(fcgi_request * const fr, const int passed)
1903 request_rec * const r = fr->r;
1905 /* Restore the saved subprocess_env because we muddied ours up */
1906 r->subprocess_env = fr->saved_subprocess_env;
1908 if (passed) {
1909 if (fr->auth_compat) {
1910 ap_table_do((int (*)(void *, const char *, const char *))post_process_auth_passed_compat_header,
1911 (void *)r->subprocess_env, fr->authHeaders, NULL);
1913 else {
1914 ap_table_do((int (*)(void *, const char *, const char *))post_process_auth_passed_header,
1915 (void *)r->subprocess_env, fr->authHeaders, NULL);
1918 else {
1919 ap_table_do((int (*)(void *, const char *, const char *))post_process_auth_failed_header,
1920 (void *)r->err_headers_out, fr->authHeaders, NULL);
1923 /* @@@ Restore these.. its a hack until I rewrite the header handling */
1924 r->status = HTTP_OK;
1925 r->status_line = NULL;
1928 static int check_user_authentication(request_rec *r)
1930 int res, authenticated = 0;
1931 const char *password;
1932 fcgi_request *fr;
1933 const fcgi_dir_config * const dir_config =
1934 (const fcgi_dir_config *)ap_get_module_config(r->per_dir_config, &fastcgi_module);
1936 if (dir_config->authenticator == NULL)
1937 return DECLINED;
1939 /* Get the user password */
1940 if ((res = ap_get_basic_auth_pw(r, &password)) != OK)
1941 return res;
1943 if ((fr = create_fcgi_request(r, dir_config->authenticator)) == NULL)
1944 return SERVER_ERROR;
1946 /* Save the existing subprocess_env, because we're gonna muddy it up */
1947 fr->saved_subprocess_env = ap_copy_table(r->pool, r->subprocess_env);
1949 ap_table_setn(r->subprocess_env, "REMOTE_PASSWD", password);
1950 ap_table_setn(r->subprocess_env, "FCGI_APACHE_ROLE", "AUTHENTICATOR");
1952 /* The FastCGI Protocol doesn't differentiate authentication */
1953 fr->role = FCGI_AUTHORIZER;
1955 /* Do we need compatibility mode? */
1956 fr->auth_compat = (dir_config->authenticator_options & FCGI_COMPAT);
1958 if ((res = do_work(r, fr)) != OK)
1959 goto AuthenticationFailed;
1961 authenticated = (r->status == 200);
1962 post_process_auth(fr, authenticated);
1964 /* A redirect shouldn't be allowed during the authentication phase */
1965 if (ap_table_get(r->headers_out, "Location") != NULL) {
1966 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1967 "FastCGI: FastCgiAuthenticator \"%s\" redirected (not allowed)",
1968 dir_config->authenticator);
1969 goto AuthenticationFailed;
1972 if (authenticated)
1973 return OK;
1975 AuthenticationFailed:
1976 if (!(dir_config->authenticator_options & FCGI_AUTHORITATIVE))
1977 return DECLINED;
1979 /* @@@ Probably should support custom_responses */
1980 ap_note_basic_auth_failure(r);
1981 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
1982 "FastCGI: authentication failed for user \"%s\": %s", r->connection->user, r->uri);
1983 return (res == OK) ? AUTH_REQUIRED : res;
1986 static int check_user_authorization(request_rec *r)
1988 int res, authorized = 0;
1989 fcgi_request *fr;
1990 const fcgi_dir_config * const dir_config =
1991 (const fcgi_dir_config *)ap_get_module_config(r->per_dir_config, &fastcgi_module);
1993 if (dir_config->authorizer == NULL)
1994 return DECLINED;
1996 /* @@@ We should probably honor the existing parameters to the require directive
1997 * as well as allow the definition of new ones (or use the basename of the
1998 * FastCGI server and pass the rest of the directive line), but for now keep
1999 * it simple. */
2001 if ((fr = create_fcgi_request(r, dir_config->authorizer)) == NULL)
2002 return SERVER_ERROR;
2004 /* Save the existing subprocess_env, because we're gonna muddy it up */
2005 fr->saved_subprocess_env = ap_copy_table(r->pool, r->subprocess_env);
2007 ap_table_setn(r->subprocess_env, "FCGI_APACHE_ROLE", "AUTHORIZER");
2009 fr->role = FCGI_AUTHORIZER;
2011 /* Do we need compatibility mode? */
2012 fr->auth_compat = (dir_config->authenticator_options & FCGI_COMPAT);
2014 if ((res = do_work(r, fr)) != OK)
2015 goto AuthorizationFailed;
2017 authorized = (r->status == 200);
2018 post_process_auth(fr, authorized);
2020 /* A redirect shouldn't be allowed during the authorization phase */
2021 if (ap_table_get(r->headers_out, "Location") != NULL) {
2022 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
2023 "FastCGI: FastCgiAuthorizer \"%s\" redirected (not allowed)",
2024 dir_config->authorizer);
2025 goto AuthorizationFailed;
2028 if (authorized)
2029 return OK;
2031 AuthorizationFailed:
2032 if (!(dir_config->authorizer_options & FCGI_AUTHORITATIVE))
2033 return DECLINED;
2035 /* @@@ Probably should support custom_responses */
2036 ap_note_basic_auth_failure(r);
2037 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
2038 "FastCGI: authorization failed for user \"%s\": %s", r->connection->user, r->uri);
2039 return (res == OK) ? AUTH_REQUIRED : res;
2042 static int check_access(request_rec *r)
2044 int res, access_allowed = 0;
2045 fcgi_request *fr;
2046 const fcgi_dir_config * const dir_config =
2047 (fcgi_dir_config *)ap_get_module_config(r->per_dir_config, &fastcgi_module);
2049 if (dir_config == NULL || dir_config->access_checker == NULL)
2050 return DECLINED;
2052 if ((fr = create_fcgi_request(r, dir_config->access_checker)) == NULL)
2053 return SERVER_ERROR;
2055 /* Save the existing subprocess_env, because we're gonna muddy it up */
2056 fr->saved_subprocess_env = ap_copy_table(r->pool, r->subprocess_env);
2058 ap_table_setn(r->subprocess_env, "FCGI_APACHE_ROLE", "ACCESS_CHECKER");
2060 /* The FastCGI Protocol doesn't differentiate access control */
2061 fr->role = FCGI_AUTHORIZER;
2063 /* Do we need compatibility mode? */
2064 fr->auth_compat = (dir_config->authenticator_options & FCGI_COMPAT);
2066 if ((res = do_work(r, fr)) != OK)
2067 goto AccessFailed;
2069 access_allowed = (r->status == 200);
2070 post_process_auth(fr, access_allowed);
2072 /* A redirect shouldn't be allowed during the access check phase */
2073 if (ap_table_get(r->headers_out, "Location") != NULL) {
2074 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r,
2075 "FastCGI: FastCgiAccessChecker \"%s\" redirected (not allowed)",
2076 dir_config->access_checker);
2077 goto AccessFailed;
2080 if (access_allowed)
2081 return OK;
2083 AccessFailed:
2084 if (!(dir_config->access_checker_options & FCGI_AUTHORITATIVE))
2085 return DECLINED;
2087 /* @@@ Probably should support custom_responses */
2088 ap_log_rerror(FCGI_LOG_ERR_NOERRNO, r, "FastCGI: access denied: %s", r->uri);
2089 return (res == OK) ? FORBIDDEN : res;
2092 command_rec fastcgi_cmds[] = {
2093 { "AppClass", fcgi_config_new_static_server, NULL, RSRC_CONF, RAW_ARGS, NULL },
2094 { "FastCgiServer", fcgi_config_new_static_server, NULL, RSRC_CONF, RAW_ARGS, NULL },
2096 { "ExternalAppClass", fcgi_config_new_external_server, NULL, RSRC_CONF, RAW_ARGS, NULL },
2097 { "FastCgiExternalServer", fcgi_config_new_external_server, NULL, RSRC_CONF, RAW_ARGS, NULL },
2099 { "FastCgiIpcDir", fcgi_config_set_socket_dir, NULL, RSRC_CONF, TAKE1, NULL },
2101 { "FastCgiSuexec", fcgi_config_set_wrapper, NULL, RSRC_CONF, TAKE1, NULL },
2102 { "FastCgiWrapper", fcgi_config_set_wrapper, NULL, RSRC_CONF, TAKE1, NULL },
2104 { "FCGIConfig", fcgi_config_set_config, NULL, RSRC_CONF, RAW_ARGS, NULL },
2105 { "FastCgiConfig", fcgi_config_set_config, NULL, RSRC_CONF, RAW_ARGS, NULL },
2107 { "FastCgiAuthenticator", fcgi_config_new_auth_server,
2108 (void *)FCGI_AUTH_TYPE_AUTHENTICATOR, ACCESS_CONF, TAKE12,
2109 "a fastcgi-script path (absolute or relative to ServerRoot) followed by an optional -compat" },
2110 { "FastCgiAuthenticatorAuthoritative", fcgi_config_set_authoritative_slot,
2111 (void *)XtOffsetOf(fcgi_dir_config, authenticator_options), ACCESS_CONF, FLAG,
2112 "Set to 'off' to allow authentication to be passed along to lower modules upon failure" },
2114 { "FastCgiAuthorizer", fcgi_config_new_auth_server,
2115 (void *)FCGI_AUTH_TYPE_AUTHORIZER, ACCESS_CONF, TAKE12,
2116 "a fastcgi-script path (absolute or relative to ServerRoot) followed by an optional -compat" },
2117 { "FastCgiAuthorizerAuthoritative", fcgi_config_set_authoritative_slot,
2118 (void *)XtOffsetOf(fcgi_dir_config, authorizer_options), ACCESS_CONF, FLAG,
2119 "Set to 'off' to allow authorization to be passed along to lower modules upon failure" },
2121 { "FastCgiAccessChecker", fcgi_config_new_auth_server,
2122 (void *)FCGI_AUTH_TYPE_ACCESS_CHECKER, ACCESS_CONF, TAKE12,
2123 "a fastcgi-script path (absolute or relative to ServerRoot) followed by an optional -compat" },
2124 { "FastCgiAccessCheckerAuthoritative", fcgi_config_set_authoritative_slot,
2125 (void *)XtOffsetOf(fcgi_dir_config, access_checker_options), ACCESS_CONF, FLAG,
2126 "Set to 'off' to allow access control to be passed along to lower modules upon failure" },
2127 { NULL }
2131 handler_rec fastcgi_handlers[] = {
2132 { FCGI_MAGIC_TYPE, content_handler },
2133 { "fastcgi-script", content_handler },
2134 { NULL }
2138 module MODULE_VAR_EXPORT fastcgi_module = {
2139 STANDARD_MODULE_STUFF,
2140 init_module, /* initializer */
2141 fcgi_config_create_dir_config, /* per-dir config creator */
2142 NULL, /* per-dir config merger (default: override) */
2143 NULL, /* per-server config creator */
2144 NULL, /* per-server config merger (default: override) */
2145 fastcgi_cmds, /* command table */
2146 fastcgi_handlers, /* [9] content handlers */
2147 NULL, /* [2] URI-to-filename translation */
2148 check_user_authentication, /* [5] authenticate user_id */
2149 check_user_authorization, /* [6] authorize user_id */
2150 check_access, /* [4] check access (based on src & http headers) */
2151 NULL, /* [7] check/set MIME type */
2152 NULL, /* [8] fixups */
2153 NULL, /* [10] logger */
2154 NULL, /* [3] header-parser */
2155 fcgi_child_init, /* process initialization */
2156 fcgi_child_exit, /* process exit/cleanup */
2157 NULL /* [1] post read-request handling */