76f6dc7
diff --git a/modules/generators/cgi_common.h b/modules/generators/cgi_common.h
76f6dc7
new file mode 100644
76f6dc7
index 0000000..85c9685
76f6dc7
--- /dev/null
76f6dc7
+++ b/modules/generators/cgi_common.h
76f6dc7
@@ -0,0 +1,359 @@
76f6dc7
+/* Licensed to the Apache Software Foundation (ASF) under one or more
76f6dc7
+ * contributor license agreements.  See the NOTICE file distributed with
76f6dc7
+ * this work for additional information regarding copyright ownership.
76f6dc7
+ * The ASF licenses this file to You under the Apache License, Version 2.0
76f6dc7
+ * (the "License"); you may not use this file except in compliance with
76f6dc7
+ * the License.  You may obtain a copy of the License at
76f6dc7
+ *
76f6dc7
+ *     http://www.apache.org/licenses/LICENSE-2.0
76f6dc7
+ *
76f6dc7
+ * Unless required by applicable law or agreed to in writing, software
76f6dc7
+ * distributed under the License is distributed on an "AS IS" BASIS,
76f6dc7
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
76f6dc7
+ * See the License for the specific language governing permissions and
76f6dc7
+ * limitations under the License.
76f6dc7
+ */
76f6dc7
+
76f6dc7
+#include "apr.h"
76f6dc7
+#include "apr_strings.h"
76f6dc7
+#include "apr_buckets.h"
76f6dc7
+#include "apr_lib.h"
76f6dc7
+#include "apr_poll.h"
76f6dc7
+
76f6dc7
+#define APR_WANT_STRFUNC
76f6dc7
+#define APR_WANT_MEMFUNC
76f6dc7
+#include "apr_want.h"
76f6dc7
+
76f6dc7
+#include "httpd.h"
76f6dc7
+#include "util_filter.h"
76f6dc7
+
76f6dc7
+static void discard_script_output(apr_bucket_brigade *bb)
76f6dc7
+{
76f6dc7
+    apr_bucket *e;
76f6dc7
+    const char *buf;
76f6dc7
+    apr_size_t len;
76f6dc7
+
76f6dc7
+    for (e = APR_BRIGADE_FIRST(bb);
76f6dc7
+         e != APR_BRIGADE_SENTINEL(bb) && !APR_BUCKET_IS_EOS(e);
76f6dc7
+         e = APR_BRIGADE_FIRST(bb))
76f6dc7
+    {
76f6dc7
+        if (apr_bucket_read(e, &buf, &len, APR_BLOCK_READ)) {
76f6dc7
+            break;
76f6dc7
+        }
76f6dc7
+        apr_bucket_delete(e);
76f6dc7
+    }
76f6dc7
+}
76f6dc7
+
76f6dc7
+#ifdef WANT_CGI_BUCKET
76f6dc7
+/* A CGI bucket type is needed to catch any output to stderr from the
76f6dc7
+ * script; see PR 22030. */
76f6dc7
+static const apr_bucket_type_t bucket_type_cgi;
76f6dc7
+
76f6dc7
+struct cgi_bucket_data {
76f6dc7
+    apr_pollset_t *pollset;
76f6dc7
+    request_rec *r;
2993aea
+    apr_interval_time_t timeout;
76f6dc7
+};
2993aea
+
76f6dc7
+/* Create a CGI bucket using pipes from script stdout 'out'
76f6dc7
+ * and stderr 'err', for request 'r'. */
76f6dc7
+static apr_bucket *cgi_bucket_create(request_rec *r,
76f6dc7
+                                     apr_interval_time_t timeout,
76f6dc7
+                                     apr_file_t *out, apr_file_t *err,
76f6dc7
+                                     apr_bucket_alloc_t *list)
2993aea
+{
76f6dc7
+    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
76f6dc7
+    apr_status_t rv;
76f6dc7
+    apr_pollfd_t fd;
76f6dc7
+    struct cgi_bucket_data *data = apr_palloc(r->pool, sizeof *data);
76f6dc7
+
76f6dc7
+    /* Disable APR timeout handling since we'll use poll() entirely. */
76f6dc7
+    apr_file_pipe_timeout_set(out, 0);
76f6dc7
+    apr_file_pipe_timeout_set(err, 0);
76f6dc7
+    
76f6dc7
+    APR_BUCKET_INIT(b);
76f6dc7
+    b->free = apr_bucket_free;
76f6dc7
+    b->list = list;
76f6dc7
+    b->type = &bucket_type_cgi;
76f6dc7
+    b->length = (apr_size_t)(-1);
76f6dc7
+    b->start = -1;
76f6dc7
+
76f6dc7
+    /* Create the pollset */
76f6dc7
+    rv = apr_pollset_create(&data->pollset, 2, r->pool, 0);
76f6dc7
+    if (rv != APR_SUCCESS) {
76f6dc7
+        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01217)
76f6dc7
+                     "apr_pollset_create(); check system or user limits");
76f6dc7
+        return NULL;
76f6dc7
+    }
76f6dc7
+
76f6dc7
+    fd.desc_type = APR_POLL_FILE;
76f6dc7
+    fd.reqevents = APR_POLLIN;
76f6dc7
+    fd.p = r->pool;
76f6dc7
+    fd.desc.f = out; /* script's stdout */
76f6dc7
+    fd.client_data = (void *)1;
76f6dc7
+    rv = apr_pollset_add(data->pollset, &fd;;
76f6dc7
+    if (rv != APR_SUCCESS) {
76f6dc7
+        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01218)
76f6dc7
+                     "apr_pollset_add(); check system or user limits");
76f6dc7
+        return NULL;
76f6dc7
+    }
76f6dc7
+
76f6dc7
+    fd.desc.f = err; /* script's stderr */
76f6dc7
+    fd.client_data = (void *)2;
76f6dc7
+    rv = apr_pollset_add(data->pollset, &fd;;
76f6dc7
+    if (rv != APR_SUCCESS) {
76f6dc7
+        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01219)
76f6dc7
+                     "apr_pollset_add(); check system or user limits");
76f6dc7
+        return NULL;
76f6dc7
+    }
76f6dc7
+
76f6dc7
+    data->r = r;
76f6dc7
+    data->timeout = timeout;
76f6dc7
+    b->data = data;
76f6dc7
+    return b;
2993aea
+}
2993aea
+
76f6dc7
+/* Create a duplicate CGI bucket using given bucket data */
76f6dc7
+static apr_bucket *cgi_bucket_dup(struct cgi_bucket_data *data,
76f6dc7
+                                  apr_bucket_alloc_t *list)
2993aea
+{
76f6dc7
+    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
76f6dc7
+    APR_BUCKET_INIT(b);
76f6dc7
+    b->free = apr_bucket_free;
76f6dc7
+    b->list = list;
76f6dc7
+    b->type = &bucket_type_cgi;
76f6dc7
+    b->length = (apr_size_t)(-1);
76f6dc7
+    b->start = -1;
76f6dc7
+    b->data = data;
76f6dc7
+    return b;
76f6dc7
+}
2993aea
+
76f6dc7
+/* Handle stdout from CGI child.  Duplicate of logic from the _read
76f6dc7
+ * method of the real APR pipe bucket implementation. */
76f6dc7
+static apr_status_t cgi_read_stdout(apr_bucket *a, apr_file_t *out,
76f6dc7
+                                    const char **str, apr_size_t *len)
76f6dc7
+{
76f6dc7
+    char *buf;
76f6dc7
+    apr_status_t rv;
76f6dc7
+
76f6dc7
+    *str = NULL;
76f6dc7
+    *len = APR_BUCKET_BUFF_SIZE;
76f6dc7
+    buf = apr_bucket_alloc(*len, a->list); /* XXX: check for failure? */
76f6dc7
+
76f6dc7
+    rv = apr_file_read(out, buf, len);
76f6dc7
+
76f6dc7
+    if (rv != APR_SUCCESS && rv != APR_EOF) {
76f6dc7
+        apr_bucket_free(buf);
76f6dc7
+        return rv;
2993aea
+    }
2993aea
+
76f6dc7
+    if (*len > 0) {
76f6dc7
+        struct cgi_bucket_data *data = a->data;
76f6dc7
+        apr_bucket_heap *h;
76f6dc7
+
76f6dc7
+        /* Change the current bucket to refer to what we read */
76f6dc7
+        a = apr_bucket_heap_make(a, buf, *len, apr_bucket_free);
76f6dc7
+        h = a->data;
76f6dc7
+        h->alloc_len = APR_BUCKET_BUFF_SIZE; /* note the real buffer size */
76f6dc7
+        *str = buf;
76f6dc7
+        APR_BUCKET_INSERT_AFTER(a, cgi_bucket_dup(data, a->list));
76f6dc7
+    }
76f6dc7
+    else {
76f6dc7
+        apr_bucket_free(buf);
76f6dc7
+        a = apr_bucket_immortal_make(a, "", 0);
76f6dc7
+        *str = a->data;
76f6dc7
+    }
76f6dc7
+    return rv;
2993aea
+}
2993aea
+
76f6dc7
+/* Read method of CGI bucket: polls on stderr and stdout of the child,
76f6dc7
+ * sending any stderr output immediately away to the error log. */
76f6dc7
+static apr_status_t cgi_bucket_read(apr_bucket *b, const char **str,
76f6dc7
+                                    apr_size_t *len, apr_read_type_e block)
76f6dc7
+{
76f6dc7
+    struct cgi_bucket_data *data = b->data;
76f6dc7
+    apr_interval_time_t timeout = 0;
76f6dc7
+    apr_status_t rv;
76f6dc7
+    int gotdata = 0;
2993aea
+
76f6dc7
+    if (block != APR_NONBLOCK_READ) {
76f6dc7
+        timeout = data->timeout > 0 ? data->timeout : data->r->server->timeout;
5b6bedc
+    }
76f6dc7
+
2993aea
+    do {
76f6dc7
+        const apr_pollfd_t *results;
76f6dc7
+        apr_int32_t num;
2993aea
+
76f6dc7
+        rv = apr_pollset_poll(data->pollset, timeout, &num, &results);
76f6dc7
+        if (APR_STATUS_IS_TIMEUP(rv)) {
76f6dc7
+            if (timeout) {
76f6dc7
+                ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, data->r, APLOGNO(01220)
76f6dc7
+                              "Timeout waiting for output from CGI script %s",
76f6dc7
+                              data->r->filename);
76f6dc7
+                return rv;
76f6dc7
+            }
76f6dc7
+            else {
76f6dc7
+                return APR_EAGAIN;
76f6dc7
+            }
76f6dc7
+        }
76f6dc7
+        else if (APR_STATUS_IS_EINTR(rv)) {
76f6dc7
+            continue;
76f6dc7
+        }
76f6dc7
+        else if (rv != APR_SUCCESS) {
76f6dc7
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, data->r, APLOGNO(01221)
76f6dc7
+                          "poll failed waiting for CGI child");
76f6dc7
+            return rv;
76f6dc7
+        }
5b6bedc
+
76f6dc7
+        for (; num; num--, results++) {
76f6dc7
+            if (results[0].client_data == (void *)1) {
76f6dc7
+                /* stdout */
76f6dc7
+                rv = cgi_read_stdout(b, results[0].desc.f, str, len);
76f6dc7
+                if (APR_STATUS_IS_EOF(rv)) {
76f6dc7
+                    rv = APR_SUCCESS;
76f6dc7
+                }
76f6dc7
+                gotdata = 1;
76f6dc7
+            } else {
76f6dc7
+                /* stderr */
76f6dc7
+                apr_status_t rv2 = log_script_err(data->r, results[0].desc.f);
76f6dc7
+                if (APR_STATUS_IS_EOF(rv2)) {
76f6dc7
+                    apr_pollset_remove(data->pollset, &results[0]);
76f6dc7
+                }
76f6dc7
+            }
76f6dc7
+        }
76f6dc7
+
76f6dc7
+    } while (!gotdata);
76f6dc7
+
76f6dc7
+    return rv;
2993aea
+}
2993aea
+
76f6dc7
+static const apr_bucket_type_t bucket_type_cgi = {
76f6dc7
+    "CGI", 5, APR_BUCKET_DATA,
76f6dc7
+    apr_bucket_destroy_noop,
76f6dc7
+    cgi_bucket_read,
76f6dc7
+    apr_bucket_setaside_notimpl,
76f6dc7
+    apr_bucket_split_notimpl,
76f6dc7
+    apr_bucket_copy_notimpl
76f6dc7
+};
2993aea
+
76f6dc7
+#endif /* WANT_CGI_BUCKET */
76f6dc7
+
76f6dc7
+/* Handle the CGI response output, having set up the brigade with the
76f6dc7
+ * CGI or PIPE bucket as appropriate. */
76f6dc7
+static int cgi_handle_response(request_rec *r, int nph, apr_bucket_brigade *bb,
76f6dc7
+                               apr_interval_time_t timeout, cgi_server_conf *conf,
76f6dc7
+                               char *logdata, apr_file_t *script_err)
76f6dc7
+{
76f6dc7
+    apr_status_t rv;
76f6dc7
+    
76f6dc7
+    /* Handle script return... */
76f6dc7
+    if (!nph) {
76f6dc7
+        const char *location;
76f6dc7
+        char sbuf[MAX_STRING_LEN];
76f6dc7
+        int ret;
76f6dc7
+
76f6dc7
+        if ((ret = ap_scan_script_header_err_brigade_ex(r, bb, sbuf,
76f6dc7
+                                                        APLOG_MODULE_INDEX)))
76f6dc7
+        {
76f6dc7
+            ret = log_script(r, conf, ret, logdata, sbuf, bb, script_err);
76f6dc7
+
76f6dc7
+            /*
76f6dc7
+             * ret could be HTTP_NOT_MODIFIED in the case that the CGI script
76f6dc7
+             * does not set an explicit status and ap_meets_conditions, which
76f6dc7
+             * is called by ap_scan_script_header_err_brigade, detects that
76f6dc7
+             * the conditions of the requests are met and the response is
76f6dc7
+             * not modified.
76f6dc7
+             * In this case set r->status and return OK in order to prevent
76f6dc7
+             * running through the error processing stack as this would
76f6dc7
+             * break with mod_cache, if the conditions had been set by
76f6dc7
+             * mod_cache itself to validate a stale entity.
76f6dc7
+             * BTW: We circumvent the error processing stack anyway if the
76f6dc7
+             * CGI script set an explicit status code (whatever it is) and
76f6dc7
+             * the only possible values for ret here are:
76f6dc7
+             *
76f6dc7
+             * HTTP_NOT_MODIFIED          (set by ap_meets_conditions)
76f6dc7
+             * HTTP_PRECONDITION_FAILED   (set by ap_meets_conditions)
76f6dc7
+             * HTTP_INTERNAL_SERVER_ERROR (if something went wrong during the
76f6dc7
+             * processing of the response of the CGI script, e.g broken headers
76f6dc7
+             * or a crashed CGI process).
76f6dc7
+             */
76f6dc7
+            if (ret == HTTP_NOT_MODIFIED) {
76f6dc7
+                r->status = ret;
76f6dc7
+                return OK;
76f6dc7
+            }
76f6dc7
+
76f6dc7
+            return ret;
76f6dc7
+        }
76f6dc7
+
76f6dc7
+        location = apr_table_get(r->headers_out, "Location");
76f6dc7
+
76f6dc7
+        if (location && r->status == 200) {
76f6dc7
+            /* For a redirect whether internal or not, discard any
76f6dc7
+             * remaining stdout from the script, and log any remaining
76f6dc7
+             * stderr output, as normal. */
76f6dc7
+            discard_script_output(bb);
76f6dc7
+            apr_brigade_destroy(bb);
76f6dc7
+
76f6dc7
+            if (script_err) {
76f6dc7
+                apr_file_pipe_timeout_set(script_err, timeout);
76f6dc7
+                log_script_err(r, script_err);
76f6dc7
+            }
76f6dc7
+        }
76f6dc7
+
76f6dc7
+        if (location && location[0] == '/' && r->status == 200) {
76f6dc7
+            /* This redirect needs to be a GET no matter what the original
76f6dc7
+             * method was.
76f6dc7
+             */
76f6dc7
+            r->method = "GET";
76f6dc7
+            r->method_number = M_GET;
2993aea
+
76f6dc7
+            /* We already read the message body (if any), so don't allow
76f6dc7
+             * the redirected request to think it has one.  We can ignore
76f6dc7
+             * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
76f6dc7
+             */
76f6dc7
+            apr_table_unset(r->headers_in, "Content-Length");
2993aea
+
76f6dc7
+            ap_internal_redirect_handler(location, r);
76f6dc7
+            return OK;
76f6dc7
+        }
76f6dc7
+        else if (location && r->status == 200) {
76f6dc7
+            /* XXX: Note that if a script wants to produce its own Redirect
76f6dc7
+             * body, it now has to explicitly *say* "Status: 302"
76f6dc7
+             */
76f6dc7
+            discard_script_output(bb);
76f6dc7
+            apr_brigade_destroy(bb);
76f6dc7
+            return HTTP_MOVED_TEMPORARILY;
2993aea
+        }
2993aea
+
76f6dc7
+        rv = ap_pass_brigade(r->output_filters, bb);
2993aea
+    }
76f6dc7
+    else /* nph */ {
76f6dc7
+        struct ap_filter_t *cur;
76f6dc7
+
76f6dc7
+        /* get rid of all filters up through protocol...  since we
76f6dc7
+         * haven't parsed off the headers, there is no way they can
76f6dc7
+         * work
2993aea
+         */
76f6dc7
+
76f6dc7
+        cur = r->proto_output_filters;
76f6dc7
+        while (cur && cur->frec->ftype < AP_FTYPE_CONNECTION) {
76f6dc7
+            cur = cur->next;
76f6dc7
+        }
76f6dc7
+        r->output_filters = r->proto_output_filters = cur;
76f6dc7
+
76f6dc7
+        rv = ap_pass_brigade(r->output_filters, bb);
2993aea
+    }
76f6dc7
+
76f6dc7
+    /* don't soak up script output if errors occurred writing it
76f6dc7
+     * out...  otherwise, we prolong the life of the script when the
76f6dc7
+     * connection drops or we stopped sending output for some other
76f6dc7
+     * reason */
76f6dc7
+    if (script_err && rv == APR_SUCCESS && !r->connection->aborted) {
76f6dc7
+        apr_file_pipe_timeout_set(script_err, timeout);
76f6dc7
+        log_script_err(r, script_err);
2993aea
+    }
76f6dc7
+
76f6dc7
+    if (script_err) apr_file_close(script_err);
76f6dc7
+
76f6dc7
+    return OK;                      /* NOT r->status, even if it has changed. */
76f6dc7
+}
76f6dc7
diff --git a/modules/generators/config5.m4 b/modules/generators/config5.m4
76f6dc7
index bf29521..0863553 100644
76f6dc7
--- a/modules/generators/config5.m4
76f6dc7
+++ b/modules/generators/config5.m4
76f6dc7
@@ -78,4 +78,15 @@ fi
2993aea
 
76f6dc7
 APR_ADDTO(INCLUDES, [-I\$(top_srcdir)/$modpath_current])
2993aea
 
76f6dc7
+AC_ARG_ENABLE(cgid-fdpassing,
76f6dc7
+  [APACHE_HELP_STRING(--enable-cgid-fdpassing,Enable experimental mod_cgid support for fd passing)],
76f6dc7
+  [if test "$enableval" = "yes"; then
76f6dc7
+     AC_CHECK_DECL(CMSG_DATA,
76f6dc7
+       [AC_DEFINE([HAVE_CGID_FDPASSING], 1, [Enable FD passing support in mod_cgid])],
76f6dc7
+       [AC_MSG_ERROR([cannot support mod_cgid fd-passing on this system])], [
76f6dc7
+#include <sys/types.h>
76f6dc7
+#include <sys/socket.h>])
76f6dc7
+  fi
76f6dc7
+])
76f6dc7
+
76f6dc7
 APACHE_MODPATH_FINISH
76f6dc7
diff --git a/modules/generators/mod_cgi.c b/modules/generators/mod_cgi.c
76f6dc7
index 7e4b126..f438b35 100644
76f6dc7
--- a/modules/generators/mod_cgi.c
76f6dc7
+++ b/modules/generators/mod_cgi.c
76f6dc7
@@ -92,6 +92,10 @@ typedef struct {
76f6dc7
     apr_size_t  bufbytes;
76f6dc7
 } cgi_server_conf;
2993aea
 
76f6dc7
+typedef struct {
76f6dc7
+    apr_interval_time_t timeout;
76f6dc7
+} cgi_dirconf;
76f6dc7
+
76f6dc7
 static void *create_cgi_config(apr_pool_t *p, server_rec *s)
76f6dc7
 {
76f6dc7
     cgi_server_conf *c =
76f6dc7
@@ -112,6 +116,12 @@ static void *merge_cgi_config(apr_pool_t *p, void *basev, void *overridesv)
76f6dc7
     return overrides->logname ? overrides : base;
2993aea
 }
2993aea
 
76f6dc7
+static void *create_cgi_dirconf(apr_pool_t *p, char *dummy)
2993aea
+{
76f6dc7
+    cgi_dirconf *c = (cgi_dirconf *) apr_pcalloc(p, sizeof(cgi_dirconf));
76f6dc7
+    return c;
76f6dc7
+}
2993aea
+
76f6dc7
 static const char *set_scriptlog(cmd_parms *cmd, void *dummy, const char *arg)
76f6dc7
 {
76f6dc7
     server_rec *s = cmd->server;
76f6dc7
@@ -150,6 +160,17 @@ static const char *set_scriptlog_buffer(cmd_parms *cmd, void *dummy,
76f6dc7
     return NULL;
76f6dc7
 }
76f6dc7
 
76f6dc7
+static const char *set_script_timeout(cmd_parms *cmd, void *dummy, const char *arg)
76f6dc7
+{
76f6dc7
+    cgi_dirconf *dc = dummy;
2993aea
+
76f6dc7
+    if (ap_timeout_parameter_parse(arg, &dc->timeout, "s") != APR_SUCCESS) {
76f6dc7
+        return "CGIScriptTimeout has wrong format";
2993aea
+    }
2993aea
+
76f6dc7
+    return NULL;
2993aea
+}
2993aea
+
76f6dc7
 static const command_rec cgi_cmds[] =
76f6dc7
 {
76f6dc7
 AP_INIT_TAKE1("ScriptLog", set_scriptlog, NULL, RSRC_CONF,
76f6dc7
@@ -158,6 +179,9 @@ AP_INIT_TAKE1("ScriptLogLength", set_scriptlog_length, NULL, RSRC_CONF,
76f6dc7
      "the maximum length (in bytes) of the script debug log"),
76f6dc7
 AP_INIT_TAKE1("ScriptLogBuffer", set_scriptlog_buffer, NULL, RSRC_CONF,
76f6dc7
      "the maximum size (in bytes) to record of a POST request"),
76f6dc7
+AP_INIT_TAKE1("CGIScriptTimeout", set_script_timeout, NULL, RSRC_CONF | ACCESS_CONF,
76f6dc7
+     "The amount of time to wait between successful reads from "
76f6dc7
+     "the CGI script, in seconds."),
76f6dc7
     {NULL}
76f6dc7
 };
2993aea
 
76f6dc7
@@ -466,23 +490,26 @@ static apr_status_t run_cgi_child(apr_file_t **script_out,
76f6dc7
                           apr_filepath_name_get(r->filename));
76f6dc7
         }
76f6dc7
         else {
76f6dc7
+            cgi_dirconf *dc = ap_get_module_config(r->per_dir_config, &cgi_module);
76f6dc7
+            apr_interval_time_t timeout = dc->timeout > 0 ? dc->timeout : r->server->timeout;
2993aea
+
76f6dc7
             apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
76f6dc7
 
76f6dc7
             *script_in = procnew->out;
76f6dc7
             if (!*script_in)
76f6dc7
                 return APR_EBADF;
76f6dc7
-            apr_file_pipe_timeout_set(*script_in, r->server->timeout);
76f6dc7
+            apr_file_pipe_timeout_set(*script_in, timeout);
76f6dc7
 
76f6dc7
             if (e_info->prog_type == RUN_AS_CGI) {
76f6dc7
                 *script_out = procnew->in;
76f6dc7
                 if (!*script_out)
76f6dc7
                     return APR_EBADF;
76f6dc7
-                apr_file_pipe_timeout_set(*script_out, r->server->timeout);
76f6dc7
+                apr_file_pipe_timeout_set(*script_out, timeout);
76f6dc7
 
76f6dc7
                 *script_err = procnew->err;
76f6dc7
                 if (!*script_err)
76f6dc7
                     return APR_EBADF;
76f6dc7
-                apr_file_pipe_timeout_set(*script_err, r->server->timeout);
76f6dc7
+                apr_file_pipe_timeout_set(*script_err, timeout);
76f6dc7
             }
76f6dc7
         }
76f6dc7
     }
76f6dc7
@@ -536,209 +563,12 @@ static apr_status_t default_build_command(const char **cmd, const char ***argv,
76f6dc7
     return APR_SUCCESS;
aaba8b5
 }
aaba8b5
 
aaba8b5
-static void discard_script_output(apr_bucket_brigade *bb)
aaba8b5
-{
aaba8b5
-    apr_bucket *e;
aaba8b5
-    const char *buf;
aaba8b5
-    apr_size_t len;
aaba8b5
-
aaba8b5
-    for (e = APR_BRIGADE_FIRST(bb);
76f6dc7
-         e != APR_BRIGADE_SENTINEL(bb) && !APR_BUCKET_IS_EOS(e);
76f6dc7
-         e = APR_BRIGADE_FIRST(bb))
aaba8b5
-    {
76f6dc7
-        if (apr_bucket_read(e, &buf, &len, APR_BLOCK_READ)) {
aaba8b5
-            break;
aaba8b5
-        }
76f6dc7
-        apr_bucket_delete(e);
aaba8b5
-    }
aaba8b5
-}
aaba8b5
-
76f6dc7
 #if APR_FILES_AS_SOCKETS
76f6dc7
-
76f6dc7
-/* A CGI bucket type is needed to catch any output to stderr from the
76f6dc7
- * script; see PR 22030. */
76f6dc7
-static const apr_bucket_type_t bucket_type_cgi;
76f6dc7
-
76f6dc7
-struct cgi_bucket_data {
76f6dc7
-    apr_pollset_t *pollset;
76f6dc7
-    request_rec *r;
76f6dc7
-};
76f6dc7
-
76f6dc7
-/* Create a CGI bucket using pipes from script stdout 'out'
76f6dc7
- * and stderr 'err', for request 'r'. */
76f6dc7
-static apr_bucket *cgi_bucket_create(request_rec *r,
76f6dc7
-                                     apr_file_t *out, apr_file_t *err,
76f6dc7
-                                     apr_bucket_alloc_t *list)
76f6dc7
-{
76f6dc7
-    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
76f6dc7
-    apr_status_t rv;
76f6dc7
-    apr_pollfd_t fd;
76f6dc7
-    struct cgi_bucket_data *data = apr_palloc(r->pool, sizeof *data);
76f6dc7
-
76f6dc7
-    APR_BUCKET_INIT(b);
76f6dc7
-    b->free = apr_bucket_free;
76f6dc7
-    b->list = list;
76f6dc7
-    b->type = &bucket_type_cgi;
76f6dc7
-    b->length = (apr_size_t)(-1);
76f6dc7
-    b->start = -1;
76f6dc7
-
76f6dc7
-    /* Create the pollset */
76f6dc7
-    rv = apr_pollset_create(&data->pollset, 2, r->pool, 0);
76f6dc7
-    if (rv != APR_SUCCESS) {
76f6dc7
-        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01217)
76f6dc7
-                     "apr_pollset_create(); check system or user limits");
76f6dc7
-        return NULL;
76f6dc7
-    }
76f6dc7
-
76f6dc7
-    fd.desc_type = APR_POLL_FILE;
76f6dc7
-    fd.reqevents = APR_POLLIN;
76f6dc7
-    fd.p = r->pool;
76f6dc7
-    fd.desc.f = out; /* script's stdout */
76f6dc7
-    fd.client_data = (void *)1;
76f6dc7
-    rv = apr_pollset_add(data->pollset, &fd;;
76f6dc7
-    if (rv != APR_SUCCESS) {
76f6dc7
-        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01218)
76f6dc7
-                     "apr_pollset_add(); check system or user limits");
76f6dc7
-        return NULL;
76f6dc7
-    }
76f6dc7
-
76f6dc7
-    fd.desc.f = err; /* script's stderr */
76f6dc7
-    fd.client_data = (void *)2;
76f6dc7
-    rv = apr_pollset_add(data->pollset, &fd;;
76f6dc7
-    if (rv != APR_SUCCESS) {
76f6dc7
-        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01219)
76f6dc7
-                     "apr_pollset_add(); check system or user limits");
76f6dc7
-        return NULL;
76f6dc7
-    }
76f6dc7
-
76f6dc7
-    data->r = r;
76f6dc7
-    b->data = data;
76f6dc7
-    return b;
76f6dc7
-}
76f6dc7
-
76f6dc7
-/* Create a duplicate CGI bucket using given bucket data */
76f6dc7
-static apr_bucket *cgi_bucket_dup(struct cgi_bucket_data *data,
76f6dc7
-                                  apr_bucket_alloc_t *list)
76f6dc7
-{
76f6dc7
-    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
76f6dc7
-    APR_BUCKET_INIT(b);
76f6dc7
-    b->free = apr_bucket_free;
76f6dc7
-    b->list = list;
76f6dc7
-    b->type = &bucket_type_cgi;
76f6dc7
-    b->length = (apr_size_t)(-1);
76f6dc7
-    b->start = -1;
76f6dc7
-    b->data = data;
76f6dc7
-    return b;
76f6dc7
-}
76f6dc7
-
76f6dc7
-/* Handle stdout from CGI child.  Duplicate of logic from the _read
76f6dc7
- * method of the real APR pipe bucket implementation. */
76f6dc7
-static apr_status_t cgi_read_stdout(apr_bucket *a, apr_file_t *out,
76f6dc7
-                                    const char **str, apr_size_t *len)
76f6dc7
-{
76f6dc7
-    char *buf;
76f6dc7
-    apr_status_t rv;
76f6dc7
-
76f6dc7
-    *str = NULL;
76f6dc7
-    *len = APR_BUCKET_BUFF_SIZE;
76f6dc7
-    buf = apr_bucket_alloc(*len, a->list); /* XXX: check for failure? */
76f6dc7
-
76f6dc7
-    rv = apr_file_read(out, buf, len);
76f6dc7
-
76f6dc7
-    if (rv != APR_SUCCESS && rv != APR_EOF) {
76f6dc7
-        apr_bucket_free(buf);
76f6dc7
-        return rv;
76f6dc7
-    }
76f6dc7
-
76f6dc7
-    if (*len > 0) {
76f6dc7
-        struct cgi_bucket_data *data = a->data;
76f6dc7
-        apr_bucket_heap *h;
76f6dc7
-
76f6dc7
-        /* Change the current bucket to refer to what we read */
76f6dc7
-        a = apr_bucket_heap_make(a, buf, *len, apr_bucket_free);
76f6dc7
-        h = a->data;
76f6dc7
-        h->alloc_len = APR_BUCKET_BUFF_SIZE; /* note the real buffer size */
76f6dc7
-        *str = buf;
76f6dc7
-        APR_BUCKET_INSERT_AFTER(a, cgi_bucket_dup(data, a->list));
76f6dc7
-    }
76f6dc7
-    else {
76f6dc7
-        apr_bucket_free(buf);
76f6dc7
-        a = apr_bucket_immortal_make(a, "", 0);
76f6dc7
-        *str = a->data;
76f6dc7
-    }
76f6dc7
-    return rv;
76f6dc7
-}
76f6dc7
-
76f6dc7
-/* Read method of CGI bucket: polls on stderr and stdout of the child,
76f6dc7
- * sending any stderr output immediately away to the error log. */
76f6dc7
-static apr_status_t cgi_bucket_read(apr_bucket *b, const char **str,
76f6dc7
-                                    apr_size_t *len, apr_read_type_e block)
76f6dc7
-{
76f6dc7
-    struct cgi_bucket_data *data = b->data;
76f6dc7
-    apr_interval_time_t timeout;
76f6dc7
-    apr_status_t rv;
76f6dc7
-    int gotdata = 0;
76f6dc7
-
76f6dc7
-    timeout = block == APR_NONBLOCK_READ ? 0 : data->r->server->timeout;
76f6dc7
-
76f6dc7
-    do {
76f6dc7
-        const apr_pollfd_t *results;
76f6dc7
-        apr_int32_t num;
76f6dc7
-
76f6dc7
-        rv = apr_pollset_poll(data->pollset, timeout, &num, &results);
76f6dc7
-        if (APR_STATUS_IS_TIMEUP(rv)) {
76f6dc7
-            if (timeout) {
76f6dc7
-                ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, data->r, APLOGNO(01220)
76f6dc7
-                              "Timeout waiting for output from CGI script %s",
76f6dc7
-                              data->r->filename);
76f6dc7
-                return rv;
76f6dc7
-            }
76f6dc7
-            else {
76f6dc7
-                return APR_EAGAIN;
76f6dc7
-            }
76f6dc7
-        }
76f6dc7
-        else if (APR_STATUS_IS_EINTR(rv)) {
76f6dc7
-            continue;
76f6dc7
-        }
76f6dc7
-        else if (rv != APR_SUCCESS) {
76f6dc7
-            ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, data->r, APLOGNO(01221)
76f6dc7
-                          "poll failed waiting for CGI child");
76f6dc7
-            return rv;
76f6dc7
-        }
76f6dc7
-
76f6dc7
-        for (; num; num--, results++) {
76f6dc7
-            if (results[0].client_data == (void *)1) {
76f6dc7
-                /* stdout */
76f6dc7
-                rv = cgi_read_stdout(b, results[0].desc.f, str, len);
76f6dc7
-                if (APR_STATUS_IS_EOF(rv)) {
76f6dc7
-                    rv = APR_SUCCESS;
76f6dc7
-                }
76f6dc7
-                gotdata = 1;
76f6dc7
-            } else {
76f6dc7
-                /* stderr */
76f6dc7
-                apr_status_t rv2 = log_script_err(data->r, results[0].desc.f);
76f6dc7
-                if (APR_STATUS_IS_EOF(rv2)) {
76f6dc7
-                    apr_pollset_remove(data->pollset, &results[0]);
76f6dc7
-                }
76f6dc7
-            }
76f6dc7
-        }
76f6dc7
-
76f6dc7
-    } while (!gotdata);
76f6dc7
-
76f6dc7
-    return rv;
76f6dc7
-}
76f6dc7
-
76f6dc7
-static const apr_bucket_type_t bucket_type_cgi = {
76f6dc7
-    "CGI", 5, APR_BUCKET_DATA,
76f6dc7
-    apr_bucket_destroy_noop,
76f6dc7
-    cgi_bucket_read,
76f6dc7
-    apr_bucket_setaside_notimpl,
76f6dc7
-    apr_bucket_split_notimpl,
76f6dc7
-    apr_bucket_copy_notimpl
76f6dc7
-};
76f6dc7
-
76f6dc7
+#define WANT_CGI_BUCKET
76f6dc7
 #endif
2993aea
 
76f6dc7
+#include "cgi_common.h"
76f6dc7
+
76f6dc7
 static int cgi_handler(request_rec *r)
2993aea
 {
76f6dc7
     int nph;
76f6dc7
@@ -757,6 +587,8 @@ static int cgi_handler(request_rec *r)
2993aea
     apr_status_t rv;
76f6dc7
     cgi_exec_info_t e_info;
76f6dc7
     conn_rec *c;
76f6dc7
+    cgi_dirconf *dc = ap_get_module_config(r->per_dir_config, &cgi_module);
76f6dc7
+    apr_interval_time_t timeout = dc->timeout > 0 ? dc->timeout : r->server->timeout;
2993aea
 
2993aea
     if (strcmp(r->handler, CGI_MAGIC_TYPE) && strcmp(r->handler, "cgi-script")) {
2993aea
         return DECLINED;
76f6dc7
@@ -916,10 +748,7 @@ static int cgi_handler(request_rec *r)
76f6dc7
     AP_DEBUG_ASSERT(script_in != NULL);
2993aea
 
76f6dc7
 #if APR_FILES_AS_SOCKETS
76f6dc7
-    apr_file_pipe_timeout_set(script_in, 0);
76f6dc7
-    apr_file_pipe_timeout_set(script_err, 0);
76f6dc7
-
76f6dc7
-    b = cgi_bucket_create(r, script_in, script_err, c->bucket_alloc);
76f6dc7
+    b = cgi_bucket_create(r, dc->timeout, script_in, script_err, c->bucket_alloc);
76f6dc7
     if (b == NULL)
76f6dc7
         return HTTP_INTERNAL_SERVER_ERROR;
76f6dc7
 #else
76f6dc7
@@ -929,111 +758,7 @@ static int cgi_handler(request_rec *r)
76f6dc7
     b = apr_bucket_eos_create(c->bucket_alloc);
76f6dc7
     APR_BRIGADE_INSERT_TAIL(bb, b);
2993aea
 
aaba8b5
-    /* Handle script return... */
aaba8b5
-    if (!nph) {
aaba8b5
-        const char *location;
aaba8b5
-        char sbuf[MAX_STRING_LEN];
aaba8b5
-        int ret;
aaba8b5
-
aaba8b5
-        if ((ret = ap_scan_script_header_err_brigade_ex(r, bb, sbuf,
aaba8b5
-                                                        APLOG_MODULE_INDEX)))
aaba8b5
-        {
76f6dc7
-            ret = log_script(r, conf, ret, dbuf, sbuf, bb, script_err);
aaba8b5
-
aaba8b5
-            /*
aaba8b5
-             * ret could be HTTP_NOT_MODIFIED in the case that the CGI script
aaba8b5
-             * does not set an explicit status and ap_meets_conditions, which
aaba8b5
-             * is called by ap_scan_script_header_err_brigade, detects that
aaba8b5
-             * the conditions of the requests are met and the response is
aaba8b5
-             * not modified.
aaba8b5
-             * In this case set r->status and return OK in order to prevent
aaba8b5
-             * running through the error processing stack as this would
aaba8b5
-             * break with mod_cache, if the conditions had been set by
aaba8b5
-             * mod_cache itself to validate a stale entity.
aaba8b5
-             * BTW: We circumvent the error processing stack anyway if the
aaba8b5
-             * CGI script set an explicit status code (whatever it is) and
aaba8b5
-             * the only possible values for ret here are:
aaba8b5
-             *
aaba8b5
-             * HTTP_NOT_MODIFIED          (set by ap_meets_conditions)
aaba8b5
-             * HTTP_PRECONDITION_FAILED   (set by ap_meets_conditions)
aaba8b5
-             * HTTP_INTERNAL_SERVER_ERROR (if something went wrong during the
aaba8b5
-             * processing of the response of the CGI script, e.g broken headers
aaba8b5
-             * or a crashed CGI process).
aaba8b5
-             */
aaba8b5
-            if (ret == HTTP_NOT_MODIFIED) {
aaba8b5
-                r->status = ret;
aaba8b5
-                return OK;
aaba8b5
-            }
aaba8b5
-
aaba8b5
-            return ret;
aaba8b5
-        }
aaba8b5
-
aaba8b5
-        location = apr_table_get(r->headers_out, "Location");
aaba8b5
-
76f6dc7
-        if (location && r->status == 200) {
76f6dc7
-            /* For a redirect whether internal or not, discard any
76f6dc7
-             * remaining stdout from the script, and log any remaining
76f6dc7
-             * stderr output, as normal. */
aaba8b5
-            discard_script_output(bb);
aaba8b5
-            apr_brigade_destroy(bb);
76f6dc7
-            apr_file_pipe_timeout_set(script_err, r->server->timeout);
76f6dc7
-            log_script_err(r, script_err);
76f6dc7
-        }
76f6dc7
-
76f6dc7
-        if (location && location[0] == '/' && r->status == 200) {
aaba8b5
-            /* This redirect needs to be a GET no matter what the original
aaba8b5
-             * method was.
aaba8b5
-             */
aaba8b5
-            r->method = "GET";
aaba8b5
-            r->method_number = M_GET;
aaba8b5
-
aaba8b5
-            /* We already read the message body (if any), so don't allow
76f6dc7
-             * the redirected request to think it has one.  We can ignore
aaba8b5
-             * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
aaba8b5
-             */
aaba8b5
-            apr_table_unset(r->headers_in, "Content-Length");
aaba8b5
-
aaba8b5
-            ap_internal_redirect_handler(location, r);
aaba8b5
-            return OK;
aaba8b5
-        }
aaba8b5
-        else if (location && r->status == 200) {
aaba8b5
-            /* XXX: Note that if a script wants to produce its own Redirect
aaba8b5
-             * body, it now has to explicitly *say* "Status: 302"
aaba8b5
-             */
aaba8b5
-            return HTTP_MOVED_TEMPORARILY;
aaba8b5
-        }
aaba8b5
-
aaba8b5
-        rv = ap_pass_brigade(r->output_filters, bb);
aaba8b5
-    }
76f6dc7
-    else /* nph */ {
aaba8b5
-        struct ap_filter_t *cur;
aaba8b5
-
aaba8b5
-        /* get rid of all filters up through protocol...  since we
aaba8b5
-         * haven't parsed off the headers, there is no way they can
aaba8b5
-         * work
aaba8b5
-         */
aaba8b5
-
aaba8b5
-        cur = r->proto_output_filters;
aaba8b5
-        while (cur && cur->frec->ftype < AP_FTYPE_CONNECTION) {
aaba8b5
-            cur = cur->next;
aaba8b5
-        }
aaba8b5
-        r->output_filters = r->proto_output_filters = cur;
aaba8b5
-
76f6dc7
-        rv = ap_pass_brigade(r->output_filters, bb);
aaba8b5
-    }
76f6dc7
-
76f6dc7
-    /* don't soak up script output if errors occurred writing it
76f6dc7
-     * out...  otherwise, we prolong the life of the script when the
76f6dc7
-     * connection drops or we stopped sending output for some other
76f6dc7
-     * reason */
76f6dc7
-    if (rv == APR_SUCCESS && !r->connection->aborted) {
76f6dc7
-        apr_file_pipe_timeout_set(script_err, r->server->timeout);
76f6dc7
-        log_script_err(r, script_err);
76f6dc7
-    }
76f6dc7
-
76f6dc7
-    apr_file_close(script_err);
76f6dc7
-
76f6dc7
-    return OK;                      /* NOT r->status, even if it has changed. */
aaba8b5
+    return cgi_handle_response(r, nph, bb, timeout, conf, dbuf, script_err);
2993aea
 }
2993aea
 
76f6dc7
 /*============================================================================
76f6dc7
@@ -1268,7 +993,7 @@ static void register_hooks(apr_pool_t *p)
76f6dc7
 AP_DECLARE_MODULE(cgi) =
76f6dc7
 {
76f6dc7
     STANDARD20_MODULE_STUFF,
76f6dc7
-    NULL,                        /* dir config creater */
76f6dc7
+    create_cgi_dirconf,          /* dir config creater */
76f6dc7
     NULL,                        /* dir merger --- default is to override */
76f6dc7
     create_cgi_config,           /* server config */
76f6dc7
     merge_cgi_config,            /* merge server config */
76f6dc7
diff --git a/modules/generators/mod_cgid.c b/modules/generators/mod_cgid.c
76f6dc7
index 9f4282c..102d2b3 100644
76f6dc7
--- a/modules/generators/mod_cgid.c
76f6dc7
+++ b/modules/generators/mod_cgid.c
76f6dc7
@@ -342,15 +342,19 @@ static apr_status_t close_unix_socket(void *thefd)
76f6dc7
     return close(fd);
76f6dc7
 }
aaba8b5
 
76f6dc7
-/* deal with incomplete reads and signals
76f6dc7
- * assume you really have to read buf_size bytes
76f6dc7
- */
76f6dc7
-static apr_status_t sock_read(int fd, void *vbuf, size_t buf_size)
76f6dc7
+/* Read from the socket dealing with incomplete messages and signals.
76f6dc7
+ * Returns 0 on success or errno on failure.  Stderr fd passed as
76f6dc7
+ * auxiliary data from other end is written to *errfd, or else stderr
76f6dc7
+ * fileno if not present. */
76f6dc7
+static apr_status_t sock_readhdr(int fd, int *errfd, void *vbuf, size_t buf_size)
76f6dc7
 {
76f6dc7
-    char *buf = vbuf;
76f6dc7
     int rc;
76f6dc7
+#ifndef HAVE_CGID_FDPASSING
76f6dc7
+    char *buf = vbuf;
76f6dc7
     size_t bytes_read = 0;
76f6dc7
 
76f6dc7
+    if (errfd) *errfd = 0;
5b6bedc
+    
76f6dc7
     do {
76f6dc7
         do {
76f6dc7
             rc = read(fd, buf + bytes_read, buf_size - bytes_read);
76f6dc7
@@ -365,9 +369,60 @@ static apr_status_t sock_read(int fd, void *vbuf, size_t buf_size)
76f6dc7
         }
76f6dc7
     } while (bytes_read < buf_size);
76f6dc7
 
76f6dc7
+   
76f6dc7
+#else /* with FD passing */
76f6dc7
+    struct msghdr msg = {0};
76f6dc7
+    struct iovec vec = {vbuf, buf_size};
76f6dc7
+    struct cmsghdr *cmsg;
76f6dc7
+    union {  /* union to ensure alignment */
76f6dc7
+        struct cmsghdr cm;
76f6dc7
+        char buf[CMSG_SPACE(sizeof(int))];
76f6dc7
+    } u;
76f6dc7
+    
76f6dc7
+    msg.msg_iov = &vec;
76f6dc7
+    msg.msg_iovlen = 1;
2993aea
+
76f6dc7
+    if (errfd) {
76f6dc7
+        msg.msg_control = u.buf;
76f6dc7
+        msg.msg_controllen = sizeof(u.buf);
76f6dc7
+        *errfd = 0;
2993aea
+    }
76f6dc7
+    
76f6dc7
+    /* use MSG_WAITALL to skip loop on truncated reads */
76f6dc7
+    do {
76f6dc7
+        rc = recvmsg(fd, &msg, MSG_WAITALL);
76f6dc7
+    } while (rc < 0 && errno == EINTR);
2993aea
+
76f6dc7
+    if (rc == 0) {
76f6dc7
+        return ECONNRESET;
2993aea
+    }
76f6dc7
+    else if (rc < 0) {
76f6dc7
+        return errno;
76f6dc7
+    }
76f6dc7
+    else if (rc != buf_size) {
76f6dc7
+        /* MSG_WAITALL should ensure the recvmsg blocks until the
76f6dc7
+         * entire length is read, but let's be paranoid. */
76f6dc7
+        return APR_INCOMPLETE;
2993aea
+    }
2993aea
+
76f6dc7
+    if (errfd
76f6dc7
+        && (cmsg = CMSG_FIRSTHDR(&msg)) != NULL
76f6dc7
+        && cmsg->cmsg_len == CMSG_LEN(sizeof(*errfd))
76f6dc7
+        && cmsg->cmsg_level == SOL_SOCKET
76f6dc7
+        && cmsg->cmsg_type == SCM_RIGHTS) {
76f6dc7
+        *errfd = *((int *) CMSG_DATA(cmsg));
76f6dc7
+    }
76f6dc7
+#endif
76f6dc7
+    
76f6dc7
     return APR_SUCCESS;
76f6dc7
 }
76f6dc7
 
76f6dc7
+/* As sock_readhdr but without auxiliary fd passing. */
76f6dc7
+static apr_status_t sock_read(int fd, void *vbuf, size_t buf_size)
2993aea
+{
76f6dc7
+    return sock_readhdr(fd, NULL, vbuf, buf_size);
2993aea
+}
2993aea
+
76f6dc7
 /* deal with signals
76f6dc7
  */
76f6dc7
 static apr_status_t sock_write(int fd, const void *buf, size_t buf_size)
76f6dc7
@@ -384,7 +439,7 @@ static apr_status_t sock_write(int fd, const void *buf, size_t buf_size)
76f6dc7
     return APR_SUCCESS;
76f6dc7
 }
76f6dc7
 
76f6dc7
-static apr_status_t sock_writev(int fd, request_rec *r, int count, ...)
76f6dc7
+static apr_status_t sock_writev(int fd, int auxfd, request_rec *r, int count, ...)
76f6dc7
 {
76f6dc7
     va_list ap;
76f6dc7
     int rc;
76f6dc7
@@ -399,9 +454,39 @@ static apr_status_t sock_writev(int fd, request_rec *r, int count, ...)
76f6dc7
     }
76f6dc7
     va_end(ap);
76f6dc7
 
76f6dc7
+#ifndef HAVE_CGID_FDPASSING
76f6dc7
     do {
76f6dc7
         rc = writev(fd, vec, count);
76f6dc7
     } while (rc < 0 && errno == EINTR);
76f6dc7
+#else
76f6dc7
+    {
76f6dc7
+        struct msghdr msg = { 0 };
76f6dc7
+        struct cmsghdr *cmsg;
76f6dc7
+        union { /* union for alignment */
76f6dc7
+            char buf[CMSG_SPACE(sizeof(int))];
76f6dc7
+            struct cmsghdr align;
76f6dc7
+        } u;
2993aea
+
76f6dc7
+        msg.msg_iov = vec;
76f6dc7
+        msg.msg_iovlen = count;
2993aea
+
76f6dc7
+        if (auxfd) {
76f6dc7
+            msg.msg_control = u.buf;
76f6dc7
+            msg.msg_controllen = sizeof(u.buf);
2993aea
+
76f6dc7
+            cmsg = CMSG_FIRSTHDR(&msg;;
76f6dc7
+            cmsg->cmsg_level = SOL_SOCKET;
76f6dc7
+            cmsg->cmsg_type = SCM_RIGHTS;
76f6dc7
+            cmsg->cmsg_len = CMSG_LEN(sizeof(int));
76f6dc7
+            *((int *) CMSG_DATA(cmsg)) = auxfd;
76f6dc7
+        }
2993aea
+
76f6dc7
+        do {
76f6dc7
+            rc = sendmsg(fd, &msg, 0);
76f6dc7
+        } while (rc < 0 && errno == EINTR);
76f6dc7
+    }
76f6dc7
+#endif
76f6dc7
+    
76f6dc7
     if (rc < 0) {
76f6dc7
         return errno;
76f6dc7
     }
76f6dc7
@@ -410,7 +495,7 @@ static apr_status_t sock_writev(int fd, request_rec *r, int count, ...)
76f6dc7
 }
76f6dc7
 
76f6dc7
 static apr_status_t get_req(int fd, request_rec *r, char **argv0, char ***env,
76f6dc7
-                            cgid_req_t *req)
76f6dc7
+                            int *errfd, cgid_req_t *req)
76f6dc7
 {
76f6dc7
     int i;
76f6dc7
     char **environ;
76f6dc7
@@ -421,7 +506,7 @@ static apr_status_t get_req(int fd, request_rec *r, char **argv0, char ***env,
76f6dc7
     r->server = apr_pcalloc(r->pool, sizeof(server_rec));
76f6dc7
 
76f6dc7
     /* read the request header */
76f6dc7
-    stat = sock_read(fd, req, sizeof(*req));
76f6dc7
+    stat = sock_readhdr(fd, errfd, req, sizeof(*req));
76f6dc7
     if (stat != APR_SUCCESS) {
76f6dc7
         return stat;
76f6dc7
     }
76f6dc7
@@ -479,14 +564,15 @@ static apr_status_t get_req(int fd, request_rec *r, char **argv0, char ***env,
76f6dc7
     return APR_SUCCESS;
76f6dc7
 }
76f6dc7
 
76f6dc7
-static apr_status_t send_req(int fd, request_rec *r, char *argv0, char **env,
76f6dc7
-                             int req_type)
76f6dc7
+static apr_status_t send_req(int fd, apr_file_t *errpipe, request_rec *r,
76f6dc7
+                             char *argv0, char **env, int req_type)
76f6dc7
 {
76f6dc7
     int i;
76f6dc7
     cgid_req_t req = {0};
76f6dc7
     apr_status_t stat;
76f6dc7
     ap_unix_identity_t * ugid = ap_run_get_suexec_identity(r);
76f6dc7
     core_dir_config *core_conf = ap_get_core_module_config(r->per_dir_config);
76f6dc7
+    int errfd;
76f6dc7
 
76f6dc7
 
76f6dc7
     if (ugid == NULL) {
76f6dc7
@@ -507,16 +593,21 @@ static apr_status_t send_req(int fd, request_rec *r, char *argv0, char **env,
76f6dc7
     req.args_len = r->args ? strlen(r->args) : 0;
76f6dc7
     req.loglevel = r->server->log.level;
76f6dc7
 
76f6dc7
+    if (errpipe)
76f6dc7
+        apr_os_file_get(&errfd, errpipe);
76f6dc7
+    else
76f6dc7
+        errfd = 0;
76f6dc7
+    
76f6dc7
     /* Write the request header */
76f6dc7
     if (req.args_len) {
76f6dc7
-        stat = sock_writev(fd, r, 5,
76f6dc7
+        stat = sock_writev(fd, errfd, r, 5,
76f6dc7
                            &req, sizeof(req),
76f6dc7
                            r->filename, req.filename_len,
76f6dc7
                            argv0, req.argv0_len,
76f6dc7
                            r->uri, req.uri_len,
76f6dc7
                            r->args, req.args_len);
76f6dc7
     } else {
76f6dc7
-        stat = sock_writev(fd, r, 4,
76f6dc7
+        stat = sock_writev(fd, errfd, r, 4,
76f6dc7
                            &req, sizeof(req),
76f6dc7
                            r->filename, req.filename_len,
76f6dc7
                            argv0, req.argv0_len,
76f6dc7
@@ -531,7 +622,7 @@ static apr_status_t send_req(int fd, request_rec *r, char *argv0, char **env,
76f6dc7
     for (i = 0; i < req.env_count; i++) {
76f6dc7
         apr_size_t curlen = strlen(env[i]);
76f6dc7
 
76f6dc7
-        if ((stat = sock_writev(fd, r, 2, &curlen, sizeof(curlen),
76f6dc7
+        if ((stat = sock_writev(fd, 0, r, 2, &curlen, sizeof(curlen),
76f6dc7
                                 env[i], curlen)) != APR_SUCCESS) {
76f6dc7
             return stat;
76f6dc7
         }
76f6dc7
@@ -582,20 +673,34 @@ static void daemon_signal_handler(int sig)
76f6dc7
     }
76f6dc7
 }
76f6dc7
 
76f6dc7
+/* Callback executed in the forked child process if exec of the CGI
76f6dc7
+ * script fails.  For the fd-passing case, output to stderr goes to
76f6dc7
+ * the client (request handling thread) and is logged via
76f6dc7
+ * ap_log_rerror there.  For the non-fd-passing case, the "fake"
76f6dc7
+ * request_rec passed via userdata is used to log. */
76f6dc7
 static void cgid_child_errfn(apr_pool_t *pool, apr_status_t err,
76f6dc7
                              const char *description)
76f6dc7
 {
76f6dc7
-    request_rec *r;
76f6dc7
     void *vr;
76f6dc7
 
76f6dc7
     apr_pool_userdata_get(&vr, ERRFN_USERDATA_KEY, pool);
76f6dc7
-    r = vr;
76f6dc7
-
76f6dc7
-    /* sure we got r, but don't call ap_log_rerror() because we don't
76f6dc7
-     * have r->headers_in and possibly other storage referenced by
76f6dc7
-     * ap_log_rerror()
76f6dc7
-     */
76f6dc7
-    ap_log_error(APLOG_MARK, APLOG_ERR, err, r->server, APLOGNO(01241) "%s", description);
76f6dc7
+    if (vr) {
76f6dc7
+        request_rec *r = vr;
76f6dc7
+        
76f6dc7
+        /* sure we got r, but don't call ap_log_rerror() because we don't
76f6dc7
+         * have r->headers_in and possibly other storage referenced by
76f6dc7
+         * ap_log_rerror()
76f6dc7
+         */
76f6dc7
+        ap_log_error(APLOG_MARK, APLOG_ERR, err, r->server, APLOGNO(01241) "%s", description);
2993aea
+    }
2993aea
+    else {
76f6dc7
+        const char *logstr;
76f6dc7
+        
76f6dc7
+        logstr = apr_psprintf(pool, APLOGNO(01241) "error spawning CGI child: %s (%pm)\n",
76f6dc7
+                              description, &err;;
76f6dc7
+        fputs(logstr, stderr);
76f6dc7
+        fflush(stderr);
2993aea
+    }
76f6dc7
 }
76f6dc7
 
76f6dc7
 static int cgid_server(void *data)
76f6dc7
@@ -669,7 +774,7 @@ static int cgid_server(void *data)
76f6dc7
     }
76f6dc7
 
76f6dc7
     while (!daemon_should_exit) {
76f6dc7
-        int errfileno = STDERR_FILENO;
76f6dc7
+        int errfileno;
76f6dc7
         char *argv0 = NULL;
76f6dc7
         char **env = NULL;
76f6dc7
         const char * const *argv;
76f6dc7
@@ -709,7 +814,7 @@ static int cgid_server(void *data)
76f6dc7
         r = apr_pcalloc(ptrans, sizeof(request_rec));
76f6dc7
         procnew = apr_pcalloc(ptrans, sizeof(*procnew));
76f6dc7
         r->pool = ptrans;
76f6dc7
-        stat = get_req(sd2, r, &argv0, &env, &cgid_req);
76f6dc7
+        stat = get_req(sd2, r, &argv0, &env, &errfileno, &cgid_req);
76f6dc7
         if (stat != APR_SUCCESS) {
76f6dc7
             ap_log_error(APLOG_MARK, APLOG_ERR, stat,
76f6dc7
                          main_server, APLOGNO(01248)
76f6dc7
@@ -741,6 +846,16 @@ static int cgid_server(void *data)
76f6dc7
             continue;
76f6dc7
         }
76f6dc7
 
76f6dc7
+        if (errfileno == 0) {
76f6dc7
+            errfileno = STDERR_FILENO;
2993aea
+        }
76f6dc7
+        else {
76f6dc7
+            ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, main_server,
76f6dc7
+                          "using passed fd %d as stderr", errfileno);
76f6dc7
+            /* Limit the received fd lifetime to pool lifetime */
76f6dc7
+            apr_pool_cleanup_register(ptrans, (void *)((long)errfileno),
76f6dc7
+                                      close_unix_socket, close_unix_socket);
2993aea
+        }
76f6dc7
         apr_os_file_put(&r->server->error_log, &errfileno, 0, r->pool);
76f6dc7
         apr_os_file_put(&inout, &sd2, 0, r->pool);
76f6dc7
 
76f6dc7
@@ -800,7 +915,10 @@ static int cgid_server(void *data)
76f6dc7
             close(sd2);
76f6dc7
         }
76f6dc7
         else {
76f6dc7
-            apr_pool_userdata_set(r, ERRFN_USERDATA_KEY, apr_pool_cleanup_null, ptrans);
76f6dc7
+            if (errfileno == STDERR_FILENO) {
76f6dc7
+                /* Used by cgid_child_errfn without fd-passing. */
76f6dc7
+                apr_pool_userdata_set(r, ERRFN_USERDATA_KEY, apr_pool_cleanup_null, ptrans);
2993aea
+            }
76f6dc7
 
76f6dc7
             argv = (const char * const *)create_argv(r->pool, NULL, NULL, NULL, argv0, r->args);
76f6dc7
 
76f6dc7
@@ -1099,6 +1217,33 @@ static int log_scripterror(request_rec *r, cgid_server_conf * conf, int ret,
76f6dc7
     return ret;
76f6dc7
 }
76f6dc7
 
76f6dc7
+/* Soak up stderr from a script and redirect it to the error log.
76f6dc7
+ * TODO: log_scripterror() and this could move to cgi_common.h. */
76f6dc7
+static apr_status_t log_script_err(request_rec *r, apr_file_t *script_err)
aaba8b5
+{
76f6dc7
+    char argsbuffer[HUGE_STRING_LEN];
76f6dc7
+    char *newline;
aaba8b5
+    apr_status_t rv;
76f6dc7
+    cgid_server_conf *conf = ap_get_module_config(r->server->module_config, &cgid_module);
aaba8b5
+
76f6dc7
+    while ((rv = apr_file_gets(argsbuffer, HUGE_STRING_LEN,
76f6dc7
+                               script_err)) == APR_SUCCESS) {
aaba8b5
+
76f6dc7
+        newline = strchr(argsbuffer, '\n');
76f6dc7
+        if (newline) {
76f6dc7
+            char *prev = newline - 1;
76f6dc7
+            if (prev >= argsbuffer && *prev == '\r') {
76f6dc7
+                newline = prev;
aaba8b5
+            }
aaba8b5
+
76f6dc7
+            *newline = '\0';
aaba8b5
+        }
76f6dc7
+        log_scripterror(r, conf, r->status, 0, argsbuffer);
aaba8b5
+    }
aaba8b5
+
76f6dc7
+    return rv;
76f6dc7
+}
aaba8b5
+
76f6dc7
 static int log_script(request_rec *r, cgid_server_conf * conf, int ret,
76f6dc7
                       char *dbuf, const char *sbuf, apr_bucket_brigade *bb,
76f6dc7
                       apr_file_t *script_err)
76f6dc7
@@ -1204,6 +1349,13 @@ static int log_script(request_rec *r, cgid_server_conf * conf, int ret,
76f6dc7
     return ret;
76f6dc7
 }
76f6dc7
 
76f6dc7
+/* Pull in CGI bucket implementation. */
76f6dc7
+#define cgi_server_conf cgid_server_conf
76f6dc7
+#ifdef HAVE_CGID_FDPASSING
76f6dc7
+#define WANT_CGI_BUCKET
76f6dc7
+#endif
76f6dc7
+#include "cgi_common.h"
aaba8b5
+
76f6dc7
 static int connect_to_daemon(int *sdptr, request_rec *r,
76f6dc7
                              cgid_server_conf *conf)
76f6dc7
 {
76f6dc7
@@ -1270,23 +1422,6 @@ static int connect_to_daemon(int *sdptr, request_rec *r,
76f6dc7
     return OK;
76f6dc7
 }
76f6dc7
 
76f6dc7
-static void discard_script_output(apr_bucket_brigade *bb)
76f6dc7
-{
76f6dc7
-    apr_bucket *e;
76f6dc7
-    const char *buf;
76f6dc7
-    apr_size_t len;
76f6dc7
-
76f6dc7
-    for (e = APR_BRIGADE_FIRST(bb);
76f6dc7
-         e != APR_BRIGADE_SENTINEL(bb) && !APR_BUCKET_IS_EOS(e);
76f6dc7
-         e = APR_BRIGADE_FIRST(bb))
76f6dc7
-    {
76f6dc7
-        if (apr_bucket_read(e, &buf, &len, APR_BLOCK_READ)) {
76f6dc7
-            break;
76f6dc7
-        }
76f6dc7
-        apr_bucket_delete(e);
76f6dc7
-    }
76f6dc7
-}
76f6dc7
-
76f6dc7
 /****************************************************************
76f6dc7
  *
76f6dc7
  * Actual cgid handling...
76f6dc7
@@ -1391,6 +1526,7 @@ static apr_status_t cleanup_script(void *vptr)
76f6dc7
 
76f6dc7
 static int cgid_handler(request_rec *r)
76f6dc7
 {
76f6dc7
+    conn_rec *c = r->connection;
76f6dc7
     int retval, nph, dbpos;
76f6dc7
     char *argv0, *dbuf;
76f6dc7
     apr_bucket_brigade *bb;
76f6dc7
@@ -1400,10 +1536,11 @@ static int cgid_handler(request_rec *r)
76f6dc7
     int seen_eos, child_stopped_reading;
76f6dc7
     int sd;
76f6dc7
     char **env;
76f6dc7
-    apr_file_t *tempsock;
76f6dc7
+    apr_file_t *tempsock, *script_err, *errpipe_out;
76f6dc7
     struct cleanup_script_info *info;
76f6dc7
     apr_status_t rv;
76f6dc7
     cgid_dirconf *dc;
76f6dc7
+    apr_interval_time_t timeout;
76f6dc7
 
76f6dc7
     if (strcmp(r->handler, CGI_MAGIC_TYPE) && strcmp(r->handler, "cgi-script")) {
76f6dc7
         return DECLINED;
76f6dc7
@@ -1412,7 +1549,7 @@ static int cgid_handler(request_rec *r)
76f6dc7
     conf = ap_get_module_config(r->server->module_config, &cgid_module);
76f6dc7
     dc = ap_get_module_config(r->per_dir_config, &cgid_module);
76f6dc7
 
76f6dc7
-    
76f6dc7
+    timeout = dc->timeout > 0 ? dc->timeout : r->server->timeout;
76f6dc7
     is_included = !strcmp(r->protocol, "INCLUDED");
76f6dc7
 
76f6dc7
     if ((argv0 = strrchr(r->filename, '/')) != NULL) {
76f6dc7
@@ -1465,6 +1602,17 @@ static int cgid_handler(request_rec *r)
76f6dc7
     }
76f6dc7
     */
76f6dc7
 
76f6dc7
+#ifdef HAVE_CGID_FDPASSING
76f6dc7
+    rv = apr_file_pipe_create(&script_err, &errpipe_out, r->pool);
76f6dc7
+    if (rv) {
76f6dc7
+        return log_scripterror(r, conf, HTTP_SERVICE_UNAVAILABLE, rv, APLOGNO(10176)
76f6dc7
+                               "could not create pipe for stderr");
76f6dc7
+    }
76f6dc7
+#else
76f6dc7
+    script_err = NULL;
76f6dc7
+    errpipe_out = NULL;
76f6dc7
+#endif
76f6dc7
+    
76f6dc7
     /*
76f6dc7
      * httpd core function used to add common environment variables like
76f6dc7
      * DOCUMENT_ROOT. 
76f6dc7
@@ -1477,12 +1625,16 @@ static int cgid_handler(request_rec *r)
76f6dc7
         return retval;
76f6dc7
     }
76f6dc7
 
76f6dc7
-    rv = send_req(sd, r, argv0, env, CGI_REQ);
76f6dc7
+    rv = send_req(sd, errpipe_out, r, argv0, env, CGI_REQ);
76f6dc7
     if (rv != APR_SUCCESS) {
76f6dc7
         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01268)
76f6dc7
                      "write to cgi daemon process");
76f6dc7
     }
76f6dc7
 
76f6dc7
+    /* The write-end of the pipe is only used by the server, so close
76f6dc7
+     * it here. */
76f6dc7
+    if (errpipe_out) apr_file_close(errpipe_out);
76f6dc7
+    
76f6dc7
     info = apr_palloc(r->pool, sizeof(struct cleanup_script_info));
76f6dc7
     info->conf = conf;
76f6dc7
     info->r = r;
76f6dc7
@@ -1504,12 +1656,7 @@ static int cgid_handler(request_rec *r)
76f6dc7
      */
76f6dc7
 
76f6dc7
     apr_os_pipe_put_ex(&tempsock, &sd, 1, r->pool);
76f6dc7
-    if (dc->timeout > 0) { 
76f6dc7
-        apr_file_pipe_timeout_set(tempsock, dc->timeout);
76f6dc7
-    }
76f6dc7
-    else { 
76f6dc7
-        apr_file_pipe_timeout_set(tempsock, r->server->timeout);
76f6dc7
-    }
76f6dc7
+    apr_file_pipe_timeout_set(tempsock, timeout);
76f6dc7
     apr_pool_cleanup_kill(r->pool, (void *)((long)sd), close_unix_socket);
76f6dc7
 
76f6dc7
     /* Transfer any put/post args, CERN style...
76f6dc7
@@ -1601,114 +1748,19 @@ static int cgid_handler(request_rec *r)
76f6dc7
      */
76f6dc7
     shutdown(sd, 1);
76f6dc7
 
76f6dc7
-    /* Handle script return... */
76f6dc7
-    if (!nph) {
76f6dc7
-        conn_rec *c = r->connection;
76f6dc7
-        const char *location;
76f6dc7
-        char sbuf[MAX_STRING_LEN];
76f6dc7
-        int ret;
76f6dc7
-
76f6dc7
-        bb = apr_brigade_create(r->pool, c->bucket_alloc);
76f6dc7
-        b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
76f6dc7
-        APR_BRIGADE_INSERT_TAIL(bb, b);
76f6dc7
-        b = apr_bucket_eos_create(c->bucket_alloc);
76f6dc7
-        APR_BRIGADE_INSERT_TAIL(bb, b);
76f6dc7
-
76f6dc7
-        if ((ret = ap_scan_script_header_err_brigade_ex(r, bb, sbuf,
76f6dc7
-                                                        APLOG_MODULE_INDEX)))
76f6dc7
-        {
76f6dc7
-            ret = log_script(r, conf, ret, dbuf, sbuf, bb, NULL);
76f6dc7
-
76f6dc7
-            /*
76f6dc7
-             * ret could be HTTP_NOT_MODIFIED in the case that the CGI script
76f6dc7
-             * does not set an explicit status and ap_meets_conditions, which
76f6dc7
-             * is called by ap_scan_script_header_err_brigade, detects that
76f6dc7
-             * the conditions of the requests are met and the response is
76f6dc7
-             * not modified.
76f6dc7
-             * In this case set r->status and return OK in order to prevent
76f6dc7
-             * running through the error processing stack as this would
76f6dc7
-             * break with mod_cache, if the conditions had been set by
76f6dc7
-             * mod_cache itself to validate a stale entity.
76f6dc7
-             * BTW: We circumvent the error processing stack anyway if the
76f6dc7
-             * CGI script set an explicit status code (whatever it is) and
76f6dc7
-             * the only possible values for ret here are:
76f6dc7
-             *
76f6dc7
-             * HTTP_NOT_MODIFIED          (set by ap_meets_conditions)
76f6dc7
-             * HTTP_PRECONDITION_FAILED   (set by ap_meets_conditions)
76f6dc7
-             * HTTP_INTERNAL_SERVER_ERROR (if something went wrong during the
76f6dc7
-             * processing of the response of the CGI script, e.g broken headers
76f6dc7
-             * or a crashed CGI process).
76f6dc7
-             */
76f6dc7
-            if (ret == HTTP_NOT_MODIFIED) {
76f6dc7
-                r->status = ret;
76f6dc7
-                return OK;
76f6dc7
-            }
76f6dc7
-
76f6dc7
-            return ret;
76f6dc7
-        }
76f6dc7
-
76f6dc7
-        location = apr_table_get(r->headers_out, "Location");
76f6dc7
-
76f6dc7
-        if (location && location[0] == '/' && r->status == 200) {
76f6dc7
-
76f6dc7
-            /* Soak up all the script output */
76f6dc7
-            discard_script_output(bb);
76f6dc7
-            apr_brigade_destroy(bb);
76f6dc7
-            /* This redirect needs to be a GET no matter what the original
76f6dc7
-             * method was.
76f6dc7
-             */
76f6dc7
-            r->method = "GET";
76f6dc7
-            r->method_number = M_GET;
76f6dc7
-
76f6dc7
-            /* We already read the message body (if any), so don't allow
76f6dc7
-             * the redirected request to think it has one. We can ignore
76f6dc7
-             * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
76f6dc7
-             */
76f6dc7
-            apr_table_unset(r->headers_in, "Content-Length");
76f6dc7
-
76f6dc7
-            ap_internal_redirect_handler(location, r);
76f6dc7
-            return OK;
76f6dc7
-        }
76f6dc7
-        else if (location && r->status == 200) {
76f6dc7
-            /* XXX: Note that if a script wants to produce its own Redirect
76f6dc7
-             * body, it now has to explicitly *say* "Status: 302"
76f6dc7
-             */
76f6dc7
-            discard_script_output(bb);
76f6dc7
-            apr_brigade_destroy(bb);
76f6dc7
-            return HTTP_MOVED_TEMPORARILY;
76f6dc7
-        }
76f6dc7
-
76f6dc7
-        rv = ap_pass_brigade(r->output_filters, bb);
76f6dc7
-        if (rv != APR_SUCCESS) { 
76f6dc7
-            ap_log_rerror(APLOG_MARK, APLOG_TRACE1, rv, r,
76f6dc7
-                          "Failed to flush CGI output to client");
76f6dc7
-        }
76f6dc7
-    }
76f6dc7
-
76f6dc7
-    if (nph) {
76f6dc7
-        conn_rec *c = r->connection;
76f6dc7
-        struct ap_filter_t *cur;
76f6dc7
-
76f6dc7
-        /* get rid of all filters up through protocol...  since we
76f6dc7
-         * haven't parsed off the headers, there is no way they can
76f6dc7
-         * work
76f6dc7
-         */
76f6dc7
-
76f6dc7
-        cur = r->proto_output_filters;
76f6dc7
-        while (cur && cur->frec->ftype < AP_FTYPE_CONNECTION) {
76f6dc7
-            cur = cur->next;
76f6dc7
-        }
76f6dc7
-        r->output_filters = r->proto_output_filters = cur;
76f6dc7
-
76f6dc7
-        bb = apr_brigade_create(r->pool, c->bucket_alloc);
76f6dc7
-        b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
76f6dc7
-        APR_BRIGADE_INSERT_TAIL(bb, b);
76f6dc7
-        b = apr_bucket_eos_create(c->bucket_alloc);
76f6dc7
-        APR_BRIGADE_INSERT_TAIL(bb, b);
76f6dc7
-        ap_pass_brigade(r->output_filters, bb);
76f6dc7
-    }
76f6dc7
+    bb = apr_brigade_create(r->pool, c->bucket_alloc);
76f6dc7
+#ifdef HAVE_CGID_FDPASSING
76f6dc7
+    b = cgi_bucket_create(r, dc->timeout, tempsock, script_err, c->bucket_alloc);
76f6dc7
+    if (b == NULL)
76f6dc7
+        return HTTP_INTERNAL_SERVER_ERROR; /* should call log_scripterror() w/ _UNAVAILABLE? */
76f6dc7
+#else
76f6dc7
+    b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
76f6dc7
+#endif
76f6dc7
+    APR_BRIGADE_INSERT_TAIL(bb, b);
76f6dc7
+    b = apr_bucket_eos_create(c->bucket_alloc);
76f6dc7
+    APR_BRIGADE_INSERT_TAIL(bb, b);
76f6dc7
 
76f6dc7
-    return OK; /* NOT r->status, even if it has changed. */
76f6dc7
+    return cgi_handle_response(r, nph, bb, timeout, conf, dbuf, script_err);
76f6dc7
 }
76f6dc7
 
76f6dc7
 
76f6dc7
@@ -1825,7 +1877,7 @@ static int include_cmd(include_ctx_t *ctx, ap_filter_t *f,
76f6dc7
         return retval;
76f6dc7
     }
76f6dc7
 
76f6dc7
-    send_req(sd, r, command, env, SSI_REQ);
76f6dc7
+    send_req(sd, NULL, r, command, env, SSI_REQ);
76f6dc7
 
76f6dc7
     info = apr_palloc(r->pool, sizeof(struct cleanup_script_info));
76f6dc7
     info->conf = conf;