]> granicus.if.org Git - apache/blobdiff - modules/http/http_core.c
Remove MPM-private stuff from conn_state_t
[apache] / modules / http / http_core.c
index 26e50935b65447c642930c5d8f44a0fa5955a408..8421f42b973419eff82b164791dfc3002fb742a8 100644 (file)
@@ -1,59 +1,17 @@
-/* ====================================================================
- * The Apache Software License, Version 1.1
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
  *
- * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
- * reserved.
+ *     http://www.apache.org/licenses/LICENSE-2.0
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * 3. The end-user documentation included with the redistribution,
- *    if any, must include the following acknowledgment:
- *       "This product includes software developed by the
- *        Apache Software Foundation (http://www.apache.org/)."
- *    Alternately, this acknowledgment may appear in the software itself,
- *    if and wherever such third-party acknowledgments normally appear.
- *
- * 4. The names "Apache" and "Apache Software Foundation" must
- *    not be used to endorse or promote products derived from this
- *    software without prior written permission. For written
- *    permission, please contact apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache",
- *    nor may "Apache" appear in their name, without prior written
- *    permission of the Apache Software Foundation.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- * Portions of this software are based upon public domain software
- * originally written at the National Center for Supercomputing Applications,
- * University of Illinois, Urbana-Champaign.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
 #include "apr_strings.h"
 #define APR_WANT_STRFUNC
 #include "apr_want.h"
 
-#define CORE_PRIVATE
 #include "httpd.h"
 #include "http_config.h"
 #include "http_connection.h"
 #include "http_core.h"
-#include "http_protocol.h"     /* For index_of_response().  Grump. */
+#include "http_protocol.h"   /* For index_of_response().  Grump. */
 #include "http_request.h"
 
 #include "util_filter.h"
 #include "mod_core.h"
 
 /* Handles for core filters */
-ap_filter_rec_t *ap_http_input_filter_handle;
-ap_filter_rec_t *ap_http_header_filter_handle;
-ap_filter_rec_t *ap_chunk_filter_handle;
-ap_filter_rec_t *ap_byterange_filter_handle;
+AP_DECLARE_DATA ap_filter_rec_t *ap_http_input_filter_handle;
+AP_DECLARE_DATA ap_filter_rec_t *ap_http_header_filter_handle;
+AP_DECLARE_DATA ap_filter_rec_t *ap_chunk_filter_handle;
+AP_DECLARE_DATA ap_filter_rec_t *ap_http_outerror_filter_handle;
+AP_DECLARE_DATA ap_filter_rec_t *ap_byterange_filter_handle;
+
+AP_DECLARE_DATA const char *ap_multipart_boundary;
+
+/* If we are using an MPM That Supports Async Connections,
+ * use a different processing function
+ */
+static int async_mpm = 0;
 
 static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
-                                         const char *arg)
+                                          const char *arg)
 {
-    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
+    apr_interval_time_t timeout;
+    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
     if (err != NULL) {
         return err;
     }
 
-    cmd->server->keep_alive_timeout = atoi(arg);
+    /* Stolen from mod_proxy.c */
+    if (ap_timeout_parameter_parse(arg, &timeout, "s") != APR_SUCCESS)
+        return "KeepAliveTimeout has wrong format";
+    cmd->server->keep_alive_timeout = timeout;
     return NULL;
 }
 
 static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
-                                 const char *arg) 
+                                  int arg)
 {
-    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
+    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
     if (err != NULL) {
         return err;
     }
 
-    /* We've changed it to On/Off, but used to use numbers
-     * so we accept anything but "Off" or "0" as "On"
-     */
-    if (!strcasecmp(arg, "off") || !strcmp(arg, "0")) {
-       cmd->server->keep_alive = 0;
-    }
-    else {
-       cmd->server->keep_alive = 1;
-    }
+    cmd->server->keep_alive = arg;
     return NULL;
 }
 
 static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
-                                     const char *arg)
+                                      const char *arg)
 {
-    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
+    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
     if (err != NULL) {
         return err;
     }
@@ -131,177 +92,145 @@ static const command_rec http_cmds[] = {
     AP_INIT_TAKE1("KeepAliveTimeout", set_keep_alive_timeout, NULL, RSRC_CONF,
                   "Keep-Alive timeout duration (sec)"),
     AP_INIT_TAKE1("MaxKeepAliveRequests", set_keep_alive_max, NULL, RSRC_CONF,
-     "Maximum number of Keep-Alive requests per connection, or 0 for infinite"),
-    AP_INIT_TAKE1("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
+                  "Maximum number of Keep-Alive requests per connection, "
+                  "or 0 for infinite"),
+    AP_INIT_FLAG("KeepAlive", set_keep_alive, NULL, RSRC_CONF,
                   "Whether persistent connections should be On or Off"),
     { NULL }
 };
 
-/*
- * HTTP/1.1 chunked transfer encoding filter.
- */
-static apr_status_t chunk_filter(ap_filter_t *f, apr_bucket_brigade *b)
+static const char *http_scheme(const request_rec *r)
 {
-#define ASCII_CRLF  "\015\012"
-#define ASCII_ZERO  "\060"
-    apr_bucket_brigade *more;
-    apr_bucket *e;
-    apr_status_t rv;
-
-    for (more = NULL; b; b = more, more = NULL) {
-        apr_off_t bytes = 0;
-        apr_bucket *eos = NULL;
-        apr_bucket *flush = NULL;
-        /* XXX: chunk_hdr must remain at this scope since it is used in a 
-         *      transient bucket.
-         */
-        char chunk_hdr[20]; /* enough space for the snprintf below */
-
-        APR_BRIGADE_FOREACH(e, b) {
-            if (APR_BUCKET_IS_EOS(e)) {
-                /* there shouldn't be anything after the eos */
-                eos = e;
-                break;
-            }
-            if (APR_BUCKET_IS_FLUSH(e)) {
-                flush = e;
-            }
-            else if (e->length == -1) {
-                /* unknown amount of data (e.g. a pipe) */
-                const char *data;
-                apr_size_t len;
-
-                rv = apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
-                if (rv != APR_SUCCESS) {
-                    return rv;
-                }
-                if (len > 0) {
-                    /*
-                     * There may be a new next bucket representing the
-                     * rest of the data stream on which a read() may
-                     * block so we pass down what we have so far.
-                     */
-                    bytes += len;
-                    more = apr_brigade_split(b, APR_BUCKET_NEXT(e));
-                    break;
-                }
-                else {
-                    /* If there was nothing in this bucket then we can
-                     * safely move on to the next one without pausing
-                     * to pass down what we have counted up so far.
-                     */
-                    continue;
-                }
-            }
-            else {
-                bytes += e->length;
-            }
-        }
+    /*
+     * The http module shouldn't return anything other than
+     * "http" (the default) or "https".
+     */
+    if (r->server->server_scheme &&
+        (strcmp(r->server->server_scheme, "https") == 0))
+        return "https";
 
-        /*
-         * XXX: if there aren't very many bytes at this point it may
-         * be a good idea to set them aside and return for more,
-         * unless we haven't finished counting this brigade yet.
-         */
-        /* if there are content bytes, then wrap them in a chunk */
-        if (bytes > 0) {
-            apr_size_t hdr_len;
-            /*
-             * Insert the chunk header, specifying the number of bytes in
-             * the chunk.
-             */
-            /* XXX might be nice to have APR_OFF_T_FMT_HEX */
-            hdr_len = apr_snprintf(chunk_hdr, sizeof(chunk_hdr),
-                                   "%qx" CRLF, (apr_uint64_t)bytes);
-            ap_xlate_proto_to_ascii(chunk_hdr, hdr_len);
-            e = apr_bucket_transient_create(chunk_hdr, hdr_len);
-            APR_BRIGADE_INSERT_HEAD(b, e);
-
-            /*
-             * Insert the end-of-chunk CRLF before an EOS or
-             * FLUSH bucket, or appended to the brigade
-             */
-            e = apr_bucket_immortal_create(ASCII_CRLF, 2);
-            if (eos != NULL) {
-                APR_BUCKET_INSERT_BEFORE(eos, e);
-            }
-            else if (flush != NULL) {
-                APR_BUCKET_INSERT_BEFORE(flush, e);
-            }
-            else {
-                APR_BRIGADE_INSERT_TAIL(b, e);
+    return "http";
+}
+
+static apr_port_t http_port(const request_rec *r)
+{
+    if (r->server->server_scheme &&
+        (strcmp(r->server->server_scheme, "https") == 0))
+        return DEFAULT_HTTPS_PORT;
+
+    return DEFAULT_HTTP_PORT;
+}
+
+static int ap_process_http_async_connection(conn_rec *c)
+{
+    request_rec *r;
+    conn_state_t *cs = c->cs;
+
+    AP_DEBUG_ASSERT(cs != NULL);
+    AP_DEBUG_ASSERT(cs->state == CONN_STATE_READ_REQUEST_LINE);
+
+    while (cs->state == CONN_STATE_READ_REQUEST_LINE) {
+        ap_update_child_status_from_conn(c->sbh, SERVER_BUSY_READ, c);
+
+        if ((r = ap_read_request(c))) {
+
+            c->keepalive = AP_CONN_UNKNOWN;
+            /* process the request if it was read without error */
+
+            ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
+            if (r->status == HTTP_OK) {
+                cs->state = CONN_STATE_HANDLER;
+                ap_process_async_request(r);
+                /* After the call to ap_process_request, the
+                 * request pool may have been deleted.  We set
+                 * r=NULL here to ensure that any dereference
+                 * of r that might be added later in this function
+                 * will result in a segfault immediately instead
+                 * of nondeterministic failures later.
+                 */
+                r = NULL;
             }
-        }
 
-        /* RFC 2616, Section 3.6.1
-         *
-         * If there is an EOS bucket, then prefix it with:
-         *   1) the last-chunk marker ("0" CRLF)
-         *   2) the trailer
-         *   3) the end-of-chunked body CRLF
-         *
-         * If there is no EOS bucket, then do nothing.
-         *
-         * XXX: it would be nice to combine this with the end-of-chunk
-         * marker above, but this is a bit more straight-forward for
-         * now.
-         */
-        if (eos != NULL) {
-            /* XXX: (2) trailers ... does not yet exist */
-            e = apr_bucket_immortal_create(ASCII_ZERO ASCII_CRLF /* <trailers> */ ASCII_CRLF, 5);
-            APR_BUCKET_INSERT_BEFORE(eos, e);
+            if (cs->state != CONN_STATE_WRITE_COMPLETION &&
+                cs->state != CONN_STATE_SUSPENDED) {
+                /* Something went wrong; close the connection */
+                cs->state = CONN_STATE_LINGER;
+            }
         }
-
-        /* pass the brigade to the next filter. */
-        rv = ap_pass_brigade(f->next, b);
-        if (rv != APR_SUCCESS || eos != NULL) {
-            return rv;
+        else {   /* ap_read_request failed - client may have closed */
+            cs->state = CONN_STATE_LINGER;
         }
     }
-    return APR_SUCCESS;
-}
 
-static const char *http_method(const request_rec *r)
-    { return "http"; }
-
-static apr_port_t http_port(const request_rec *r)
-    { return DEFAULT_HTTP_PORT; }
+    return OK;
+}
 
-static int ap_process_http_connection(conn_rec *c)
+static int ap_process_http_sync_connection(conn_rec *c)
 {
     request_rec *r;
+    conn_state_t *cs = c->cs;
+    apr_socket_t *csd = NULL;
+    int mpm_state = 0;
+
     /*
      * Read and process each request found on our connection
      * until no requests are left or we decide to close.
      */
-    ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
+
+    ap_update_child_status_from_conn(c->sbh, SERVER_BUSY_READ, c);
     while ((r = ap_read_request(c)) != NULL) {
-        c->keepalive = 0;
+
+        c->keepalive = AP_CONN_UNKNOWN;
         /* process the request if it was read without error */
+
         ap_update_child_status(c->sbh, SERVER_BUSY_WRITE, r);
-        if (r->status == HTTP_OK)
+        if (r->status == HTTP_OK) {
+            cs->state = CONN_STATE_HANDLER;
             ap_process_request(r);
-        if (ap_extended_status)
-            ap_increment_counts(c->sbh, r);
-        if (!c->keepalive || c->aborted)
+            /* After the call to ap_process_request, the
+             * request pool will have been deleted.  We set
+             * r=NULL here to ensure that any dereference
+             * of r that might be added later in this function
+             * will result in a segfault immediately instead
+             * of nondeterministic failures later.
+             */
+            r = NULL;
+        }
+
+        if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
             break;
-        ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, r);
-        apr_pool_destroy(r->pool);
-        if (ap_graceful_stop_signalled())
+
+        ap_update_child_status(c->sbh, SERVER_BUSY_KEEPALIVE, NULL);
+
+        if (ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state)) {
             break;
+        }
+
+        if (mpm_state == AP_MPMQ_STOPPING) {
+          break;
+        }
+
+        if (!csd) {
+            csd = ap_get_conn_socket(c);
+        }
+        apr_socket_opt_set(csd, APR_INCOMPLETE_READ, 1);
+        apr_socket_timeout_set(csd, c->base_server->keep_alive_timeout);
+        /* Go straight to select() to wait for the next request */
     }
+
     return OK;
 }
 
+static int ap_process_http_connection(conn_rec *c)
+{
+    if (async_mpm && !c->clogging_input_filters) {
+        return ap_process_http_async_connection(c);
+    }
+    else {
+        return ap_process_http_sync_connection(c);
+    }
+}
+
 static int http_create_request(request_rec *r)
 {
     if (!r->main && !r->prev) {
@@ -311,38 +240,68 @@ static int http_create_request(request_rec *r)
                                     NULL, r, r->connection);
         ap_add_output_filter_handle(ap_http_header_filter_handle,
                                     NULL, r, r->connection);
+        ap_add_output_filter_handle(ap_http_outerror_filter_handle,
+                                    NULL, r, r->connection);
+    }
+
+    return OK;
+}
+
+static int http_send_options(request_rec *r)
+{
+    if ((r->method_number == M_OPTIONS) && r->uri && (r->uri[0] == '*') &&
+         (r->uri[1] == '\0')) {
+        return DONE;           /* Send HTTP pong, without Allow header */
+    }
+    return DECLINED;
+}
+
+static int http_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
+{
+    apr_uint64_t val;
+    if (ap_mpm_query(AP_MPMQ_IS_ASYNC, &async_mpm) != APR_SUCCESS) {
+        async_mpm = 0;
     }
+    ap_random_insecure_bytes(&val, sizeof(val));
+    ap_multipart_boundary = apr_psprintf(p, "%0" APR_UINT64_T_HEX_FMT, val);
 
     return OK;
 }
 
 static void register_hooks(apr_pool_t *p)
 {
-    ap_hook_process_connection(ap_process_http_connection,NULL,NULL,
-                              APR_HOOK_REALLY_LAST);
+    ap_hook_post_config(http_post_config, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_process_connection(ap_process_http_connection, NULL, NULL,
+                               APR_HOOK_REALLY_LAST);
     ap_hook_map_to_storage(ap_send_http_trace,NULL,NULL,APR_HOOK_MIDDLE);
-    ap_hook_http_method(http_method,NULL,NULL,APR_HOOK_REALLY_LAST);
+    ap_hook_map_to_storage(http_send_options,NULL,NULL,APR_HOOK_MIDDLE);
+    ap_hook_http_scheme(http_scheme,NULL,NULL,APR_HOOK_REALLY_LAST);
     ap_hook_default_port(http_port,NULL,NULL,APR_HOOK_REALLY_LAST);
     ap_hook_create_request(http_create_request, NULL, NULL, APR_HOOK_REALLY_LAST);
     ap_http_input_filter_handle =
         ap_register_input_filter("HTTP_IN", ap_http_filter,
-                                 AP_FTYPE_HTTP_HEADER);
+                                 NULL, AP_FTYPE_PROTOCOL);
     ap_http_header_filter_handle =
-        ap_register_output_filter("HTTP_HEADER", ap_http_header_filter, 
-                                  AP_FTYPE_HTTP_HEADER);
+        ap_register_output_filter("HTTP_HEADER", ap_http_header_filter,
+                                  NULL, AP_FTYPE_PROTOCOL);
     ap_chunk_filter_handle =
-        ap_register_output_filter("CHUNK", chunk_filter, AP_FTYPE_TRANSCODE);
+        ap_register_output_filter("CHUNK", ap_http_chunk_filter,
+                                  NULL, AP_FTYPE_TRANSCODE);
+    ap_http_outerror_filter_handle =
+        ap_register_output_filter("HTTP_OUTERROR", ap_http_outerror_filter,
+                                  NULL, AP_FTYPE_PROTOCOL);
     ap_byterange_filter_handle =
         ap_register_output_filter("BYTERANGE", ap_byterange_filter,
-                                  AP_FTYPE_HTTP_HEADER);
+                                  NULL, AP_FTYPE_PROTOCOL);
+    ap_method_registry_init(p);
 }
 
-module AP_MODULE_DECLARE_DATA http_module = {
+AP_DECLARE_MODULE(http) = {
     STANDARD20_MODULE_STUFF,
-    NULL,                      /* create per-directory config structure */
-    NULL,                      /* merge per-directory config structures */
-    NULL,                      /* create per-server config structure */
-    NULL,                      /* merge per-server config structures */
-    http_cmds,                 /* command apr_table_t */
-    register_hooks             /* register hooks */
+    NULL,              /* create per-directory config structure */
+    NULL,              /* merge per-directory config structures */
+    NULL,              /* create per-server config structure */
+    NULL,              /* merge per-server config structures */
+    http_cmds,         /* command apr_table_t */
+    register_hooks     /* register hooks */
 };