Blame SOURCES/httpd-2.4.34-CVE-2021-33193.patch

879b17
diff --git a/include/http_core.h b/include/http_core.h
879b17
index 8e10988..3ba8069 100644
879b17
--- a/include/http_core.h
879b17
+++ b/include/http_core.h
879b17
@@ -741,6 +741,7 @@ typedef struct {
879b17
 #define AP_HTTP_METHODS_REGISTERED    2
879b17
     char http_methods;
879b17
     unsigned int merge_slashes;
879b17
+    unsigned int strict_host_check;
879b17
 } core_server_config;
879b17
 
879b17
 /* for AddOutputFiltersByType in core.c */
879b17
@@ -769,6 +770,11 @@ AP_DECLARE(void) ap_set_server_protocol(server_rec* s, const char* proto);
879b17
 typedef struct core_output_filter_ctx core_output_filter_ctx_t;
879b17
 typedef struct core_filter_ctx        core_ctx_t;
879b17
 
879b17
+struct core_filter_ctx {
879b17
+    apr_bucket_brigade *b;
879b17
+    apr_bucket_brigade *tmpbb;
879b17
+};
879b17
+
879b17
 typedef struct core_net_rec {
879b17
     /** Connection to the client */
879b17
     apr_socket_t *client_socket;
879b17
diff --git a/include/http_protocol.h b/include/http_protocol.h
879b17
index 11c7b2d..e7abdd9 100644
879b17
--- a/include/http_protocol.h
879b17
+++ b/include/http_protocol.h
879b17
@@ -53,6 +53,13 @@ AP_DECLARE_DATA extern ap_filter_rec_t *ap_old_write_func;
879b17
  * or control the ones that eventually do.
879b17
  */
879b17
 
879b17
+/**
879b17
+ * Read an empty request and set reasonable defaults.
879b17
+ * @param c The current connection
879b17
+ * @return The new request_rec
879b17
+ */
879b17
+AP_DECLARE(request_rec *) ap_create_request(conn_rec *c);
879b17
+
879b17
 /**
879b17
  * Read a request and fill in the fields.
879b17
  * @param c The current connection
879b17
@@ -60,6 +67,20 @@ AP_DECLARE_DATA extern ap_filter_rec_t *ap_old_write_func;
879b17
  */
879b17
 request_rec *ap_read_request(conn_rec *c);
879b17
 
879b17
+/**
879b17
+ * Parse and validate the request line.
879b17
+ * @param r The current request
879b17
+ * @return 1 on success, 0 on failure
879b17
+ */
879b17
+AP_DECLARE(int) ap_parse_request_line(request_rec *r);
879b17
+
879b17
+/**
879b17
+ * Validate the request header and select vhost.
879b17
+ * @param r The current request
879b17
+ * @return 1 on success, 0 on failure
879b17
+ */
879b17
+AP_DECLARE(int) ap_check_request_header(request_rec *r);
879b17
+
879b17
 /**
879b17
  * Read the mime-encoded headers.
879b17
  * @param r The current request
879b17
diff --git a/include/http_vhost.h b/include/http_vhost.h
879b17
index 473c9c7..d2d9c97 100644
879b17
--- a/include/http_vhost.h
879b17
+++ b/include/http_vhost.h
879b17
@@ -99,6 +99,19 @@ AP_DECLARE(void) ap_update_vhost_given_ip(conn_rec *conn);
879b17
  */
879b17
 AP_DECLARE(void) ap_update_vhost_from_headers(request_rec *r);
879b17
 
879b17
+/**
879b17
+ * Updates r->server with the best name-based virtual host match, within
879b17
+ * the chain of matching virtual hosts selected by ap_update_vhost_given_ip.
879b17
+ * @param r The current request
879b17
+ * @param require_match 1 to return an HTTP error if the requested hostname is
879b17
+ * not explicitly matched to a VirtualHost. 
879b17
+ * @return return HTTP_OK unless require_match was specified and the requested
879b17
+ * hostname did not match any ServerName, ServerAlias, or VirtualHost 
879b17
+ * address-spec.
879b17
+ */
879b17
+AP_DECLARE(int) ap_update_vhost_from_headers_ex(request_rec *r, int require_match);
879b17
+
879b17
+
879b17
 /**
879b17
  * Match the host in the header with the hostname of the server for this
879b17
  * request.
879b17
diff --git a/server/core.c b/server/core.c
879b17
index 6dfd4d5..456336c 100644
879b17
--- a/server/core.c
879b17
+++ b/server/core.c
879b17
@@ -498,6 +498,8 @@ static void *create_core_server_config(apr_pool_t *a, server_rec *s)
879b17
     conf->protocols = apr_array_make(a, 5, sizeof(const char *));
879b17
     conf->protocols_honor_order = -1;
879b17
     
879b17
+    conf->strict_host_check= AP_CORE_CONFIG_UNSET; 
879b17
+
879b17
     return (void *)conf;
879b17
 }
879b17
 
879b17
@@ -565,6 +567,12 @@ static void *merge_core_server_configs(apr_pool_t *p, void *basev, void *virtv)
879b17
 
879b17
     AP_CORE_MERGE_FLAG(merge_slashes, conf, base, virt);
879b17
 
879b17
+    conf->strict_host_check = (virt->strict_host_check != AP_CORE_CONFIG_UNSET)
879b17
+                              ? virt->strict_host_check 
879b17
+                              : base->strict_host_check;
879b17
+
879b17
+    AP_CORE_MERGE_FLAG(strict_host_check, conf, base, virt);
879b17
+
879b17
     return conf;
879b17
 }
879b17
 
879b17
@@ -4543,7 +4551,10 @@ AP_INIT_TAKE2("CGIVar", set_cgi_var, NULL, OR_FILEINFO,
879b17
 AP_INIT_FLAG("QualifyRedirectURL", set_qualify_redirect_url, NULL, OR_FILEINFO,
879b17
              "Controls whether HTTP authorization headers, normally hidden, will "
879b17
              "be passed to scripts"),
879b17
-
879b17
+AP_INIT_FLAG("StrictHostCheck", set_core_server_flag,
879b17
+             (void *)APR_OFFSETOF(core_server_config, strict_host_check),
879b17
+             RSRC_CONF,
879b17
+             "Controls whether a hostname match is required"),
879b17
 AP_INIT_TAKE1("ForceType", ap_set_string_slot_lower,
879b17
        (void *)APR_OFFSETOF(core_dir_config, mime_type), OR_FILEINFO,
879b17
      "a mime type that overrides other configured type"),
879b17
@@ -5578,4 +5589,3 @@ AP_DECLARE_MODULE(core) = {
879b17
     core_cmds,                    /* command apr_table_t */
879b17
     register_hooks                /* register hooks */
879b17
 };
879b17
-
879b17
diff --git a/server/core_filters.c b/server/core_filters.c
879b17
index ddc2ff7..326996d 100644
879b17
--- a/server/core_filters.c
879b17
+++ b/server/core_filters.c
879b17
@@ -84,11 +84,6 @@ struct core_output_filter_ctx {
879b17
     apr_size_t bytes_written;
879b17
 };
879b17
 
879b17
-struct core_filter_ctx {
879b17
-    apr_bucket_brigade *b;
879b17
-    apr_bucket_brigade *tmpbb;
879b17
-};
879b17
-
879b17
 
879b17
 apr_status_t ap_core_input_filter(ap_filter_t *f, apr_bucket_brigade *b,
879b17
                                   ap_input_mode_t mode, apr_read_type_e block,
879b17
diff --git a/server/protocol.c b/server/protocol.c
879b17
index c77da24..867dc8a 100644
879b17
--- a/server/protocol.c
879b17
+++ b/server/protocol.c
879b17
@@ -609,8 +609,15 @@ AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri)
879b17
         }
879b17
 
879b17
         r->args = r->parsed_uri.query;
879b17
-        r->uri = r->parsed_uri.path ? r->parsed_uri.path
879b17
-                 : apr_pstrdup(r->pool, "/");
879b17
+        if (r->parsed_uri.path) {
879b17
+            r->uri = r->parsed_uri.path;
879b17
+        }
879b17
+        else if (r->method_number == M_OPTIONS) {
879b17
+            r->uri = apr_pstrdup(r->pool, "*");
879b17
+        }
879b17
+        else {
879b17
+            r->uri = apr_pstrdup(r->pool, "/");
879b17
+        }
879b17
 
879b17
 #if defined(OS2) || defined(WIN32)
879b17
         /* Handle path translations for OS/2 and plug security hole.
879b17
@@ -645,13 +652,6 @@ static int field_name_len(const char *field)
879b17
 
879b17
 static int read_request_line(request_rec *r, apr_bucket_brigade *bb)
879b17
 {
879b17
-    enum {
879b17
-        rrl_none, rrl_badmethod, rrl_badwhitespace, rrl_excesswhitespace,
879b17
-        rrl_missinguri, rrl_baduri, rrl_badprotocol, rrl_trailingtext,
879b17
-        rrl_badmethod09, rrl_reject09
879b17
-    } deferred_error = rrl_none;
879b17
-    char *ll;
879b17
-    char *uri;
879b17
     apr_size_t len;
879b17
     int num_blank_lines = DEFAULT_LIMIT_BLANK_LINES;
879b17
     core_server_config *conf = ap_get_core_module_config(r->server->module_config);
879b17
@@ -711,6 +711,20 @@ static int read_request_line(request_rec *r, apr_bucket_brigade *bb)
879b17
     }
879b17
 
879b17
     r->request_time = apr_time_now();
879b17
+    return 1;
879b17
+}
879b17
+
879b17
+AP_DECLARE(int) ap_parse_request_line(request_rec *r)
879b17
+{
879b17
+    core_server_config *conf = ap_get_core_module_config(r->server->module_config);
879b17
+    int strict = (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE);
879b17
+    enum {
879b17
+        rrl_none, rrl_badmethod, rrl_badwhitespace, rrl_excesswhitespace,
879b17
+        rrl_missinguri, rrl_baduri, rrl_badprotocol, rrl_trailingtext,
879b17
+        rrl_badmethod09, rrl_reject09
879b17
+    } deferred_error = rrl_none;
879b17
+    apr_size_t len = 0;
879b17
+    char *uri, *ll;
879b17
 
879b17
     r->method = r->the_request;
879b17
 
879b17
@@ -742,7 +756,6 @@ static int read_request_line(request_rec *r, apr_bucket_brigade *bb)
879b17
         if (deferred_error == rrl_none)
879b17
             deferred_error = rrl_missinguri;
879b17
         r->protocol = uri = "";
879b17
-        len = 0;
879b17
         goto rrl_done;
879b17
     }
879b17
     else if (strict && ll[0] && apr_isspace(ll[1])
879b17
@@ -773,7 +786,6 @@ static int read_request_line(request_rec *r, apr_bucket_brigade *bb)
879b17
     /* Verify URI terminated with a single SP, or mark as specific error */
879b17
     if (!ll) {
879b17
         r->protocol = "";
879b17
-        len = 0;
879b17
         goto rrl_done;
879b17
     }
879b17
     else if (strict && ll[0] && apr_isspace(ll[1])
879b17
@@ -866,6 +878,14 @@ rrl_done:
879b17
         r->header_only = 1;
879b17
 
879b17
     ap_parse_uri(r, uri);
879b17
+    if (r->status == HTTP_OK
879b17
+            && (r->parsed_uri.path != NULL)
879b17
+            && (r->parsed_uri.path[0] != '/')
879b17
+            && (r->method_number != M_OPTIONS
879b17
+                || strcmp(r->parsed_uri.path, "*") != 0)) {
879b17
+        /* Invalid request-target per RFC 7230 section 5.3 */
879b17
+        r->status = HTTP_BAD_REQUEST;
879b17
+    }
879b17
 
879b17
     /* With the request understood, we can consider HTTP/0.9 specific errors */
879b17
     if (r->proto_num == HTTP_VERSION(0, 9) && deferred_error == rrl_none) {
879b17
@@ -973,6 +993,79 @@ rrl_failed:
879b17
     return 0;
879b17
 }
879b17
 
879b17
+AP_DECLARE(int) ap_check_request_header(request_rec *r)
879b17
+{
879b17
+    core_server_config *conf;
879b17
+    int strict_host_check;
879b17
+    const char *expect;
879b17
+    int access_status;
879b17
+
879b17
+    conf = ap_get_core_module_config(r->server->module_config);
879b17
+
879b17
+    /* update what we think the virtual host is based on the headers we've
879b17
+     * now read. may update status.
879b17
+     */
879b17
+    strict_host_check = (conf->strict_host_check == AP_CORE_CONFIG_ON);
879b17
+    access_status = ap_update_vhost_from_headers_ex(r, strict_host_check);
879b17
+    if (strict_host_check && access_status != HTTP_OK) { 
879b17
+        if (r->server == ap_server_conf) { 
879b17
+            ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(10156)
879b17
+                          "Requested hostname '%s' did not match any ServerName/ServerAlias "
879b17
+                          "in the global server configuration ", r->hostname);
879b17
+        }
879b17
+        else { 
879b17
+            ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(10157)
879b17
+                          "Requested hostname '%s' did not match any ServerName/ServerAlias "
879b17
+                          "in the matching virtual host (default vhost for "
879b17
+                          "current connection is %s:%u)", 
879b17
+                          r->hostname, r->server->defn_name, r->server->defn_line_number);
879b17
+        }
879b17
+        r->status = access_status;
879b17
+    }
879b17
+    if (r->status != HTTP_OK) { 
879b17
+        return 0;
879b17
+    }
879b17
+
879b17
+    if ((!r->hostname && (r->proto_num >= HTTP_VERSION(1, 1)))
879b17
+        || ((r->proto_num == HTTP_VERSION(1, 1))
879b17
+            && !apr_table_get(r->headers_in, "Host"))) {
879b17
+        /*
879b17
+         * Client sent us an HTTP/1.1 or later request without telling us the
879b17
+         * hostname, either with a full URL or a Host: header. We therefore
879b17
+         * need to (as per the 1.1 spec) send an error.  As a special case,
879b17
+         * HTTP/1.1 mentions twice (S9, S14.23) that a request MUST contain
879b17
+         * a Host: header, and the server MUST respond with 400 if it doesn't.
879b17
+         */
879b17
+        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00569)
879b17
+                      "client sent HTTP/1.1 request without hostname "
879b17
+                      "(see RFC2616 section 14.23): %s", r->uri);
879b17
+        r->status = HTTP_BAD_REQUEST;
879b17
+        return 0;
879b17
+    }
879b17
+
879b17
+    if (((expect = apr_table_get(r->headers_in, "Expect")) != NULL)
879b17
+        && (expect[0] != '\0')) {
879b17
+        /*
879b17
+         * The Expect header field was added to HTTP/1.1 after RFC 2068
879b17
+         * as a means to signal when a 100 response is desired and,
879b17
+         * unfortunately, to signal a poor man's mandatory extension that
879b17
+         * the server must understand or return 417 Expectation Failed.
879b17
+         */
879b17
+        if (ap_cstr_casecmp(expect, "100-continue") == 0) {
879b17
+            r->expecting_100 = 1;
879b17
+        }
879b17
+        else {
879b17
+            ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00570)
879b17
+                          "client sent an unrecognized expectation value "
879b17
+                          "of Expect: %s", expect);
879b17
+            r->status = HTTP_EXPECTATION_FAILED;
879b17
+            return 0;
879b17
+        }
879b17
+    }
879b17
+
879b17
+    return 1;
879b17
+}
879b17
+
879b17
 static int table_do_fn_check_lengths(void *r_, const char *key,
879b17
                                      const char *value)
879b17
 {
879b17
@@ -1256,16 +1349,10 @@ AP_DECLARE(void) ap_get_mime_headers(request_rec *r)
879b17
     apr_brigade_destroy(tmp_bb);
879b17
 }
879b17
 
879b17
-request_rec *ap_read_request(conn_rec *conn)
879b17
+AP_DECLARE(request_rec *) ap_create_request(conn_rec *conn)
879b17
 {
879b17
     request_rec *r;
879b17
     apr_pool_t *p;
879b17
-    const char *expect;
879b17
-    int access_status;
879b17
-    apr_bucket_brigade *tmp_bb;
879b17
-    apr_socket_t *csd;
879b17
-    apr_interval_time_t cur_timeout;
879b17
-
879b17
 
879b17
     apr_pool_create(&p, conn->pool);
879b17
     apr_pool_tag(p, "request");
879b17
@@ -1304,6 +1391,7 @@ request_rec *ap_read_request(conn_rec *conn)
879b17
     r->read_body       = REQUEST_NO_BODY;
879b17
 
879b17
     r->status          = HTTP_OK;  /* Until further notice */
879b17
+    r->header_only     = 0;
879b17
     r->the_request     = NULL;
879b17
 
879b17
     /* Begin by presuming any module can make its own path_info assumptions,
879b17
@@ -1314,12 +1402,33 @@ request_rec *ap_read_request(conn_rec *conn)
879b17
     r->useragent_addr = conn->client_addr;
879b17
     r->useragent_ip = conn->client_ip;
879b17
 
879b17
+    return r;
879b17
+}
879b17
+
879b17
+/* Apply the server's timeout/config to the connection/request. */
879b17
+static void apply_server_config(request_rec *r)
879b17
+{
879b17
+    apr_socket_t *csd;
879b17
+
879b17
+    csd = ap_get_conn_socket(r->connection);
879b17
+    apr_socket_timeout_set(csd, r->server->timeout);
879b17
+
879b17
+    r->per_dir_config = r->server->lookup_defaults;
879b17
+}
879b17
+
879b17
+request_rec *ap_read_request(conn_rec *conn)
879b17
+{
879b17
+    int access_status;
879b17
+    apr_bucket_brigade *tmp_bb;
879b17
+
879b17
+    request_rec *r = ap_create_request(conn);
879b17
     tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
879b17
 
879b17
     ap_run_pre_read_request(r, conn);
879b17
 
879b17
     /* Get the request... */
879b17
-    if (!read_request_line(r, tmp_bb)) {
879b17
+    if (!read_request_line(r, tmp_bb) || !ap_parse_request_line(r)) {
879b17
+        apr_brigade_cleanup(tmp_bb);
879b17
         switch (r->status) {
879b17
         case HTTP_REQUEST_URI_TOO_LARGE:
879b17
         case HTTP_BAD_REQUEST:
879b17
@@ -1335,49 +1444,38 @@ request_rec *ap_read_request(conn_rec *conn)
879b17
                               "request failed: malformed request line");
879b17
             }
879b17
             access_status = r->status;
879b17
-            r->status = HTTP_OK;
879b17
-            ap_die(access_status, r);
879b17
-            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
879b17
-            ap_run_log_transaction(r);
879b17
-            r = NULL;
879b17
-            apr_brigade_destroy(tmp_bb);
879b17
-            goto traceout;
879b17
+            goto die_unusable_input;
879b17
+
879b17
         case HTTP_REQUEST_TIME_OUT:
879b17
+            /* Just log, no further action on this connection. */
879b17
             ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, NULL);
879b17
             if (!r->connection->keepalives)
879b17
                 ap_run_log_transaction(r);
879b17
-            apr_brigade_destroy(tmp_bb);
879b17
-            goto traceout;
879b17
-        default:
879b17
-            apr_brigade_destroy(tmp_bb);
879b17
-            r = NULL;
879b17
-            goto traceout;
879b17
+            break;
879b17
         }
879b17
+        /* Not worth dying with. */
879b17
+        conn->keepalive = AP_CONN_CLOSE;
879b17
+        apr_pool_destroy(r->pool);
879b17
+        goto ignore;
879b17
     }
879b17
+    apr_brigade_cleanup(tmp_bb);
879b17
 
879b17
     /* We may have been in keep_alive_timeout mode, so toggle back
879b17
      * to the normal timeout mode as we fetch the header lines,
879b17
      * as necessary.
879b17
      */
879b17
-    csd = ap_get_conn_socket(conn);
879b17
-    apr_socket_timeout_get(csd, &cur_timeout);
879b17
-    if (cur_timeout != conn->base_server->timeout) {
879b17
-        apr_socket_timeout_set(csd, conn->base_server->timeout);
879b17
-        cur_timeout = conn->base_server->timeout;
879b17
-    }
879b17
+    apply_server_config(r);
879b17
 
879b17
     if (!r->assbackwards) {
879b17
         const char *tenc;
879b17
 
879b17
         ap_get_mime_headers_core(r, tmp_bb);
879b17
+        apr_brigade_cleanup(tmp_bb);
879b17
         if (r->status != HTTP_OK) {
879b17
             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00567)
879b17
                           "request failed: error reading the headers");
879b17
-            ap_send_error_response(r, 0);
879b17
-            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
879b17
-            ap_run_log_transaction(r);
879b17
-            apr_brigade_destroy(tmp_bb);
879b17
-            goto traceout;
879b17
+            access_status = r->status;
879b17
+            goto die_unusable_input;
879b17
         }
879b17
 
879b17
         tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
879b17
@@ -1393,13 +1491,8 @@ request_rec *ap_read_request(conn_rec *conn)
879b17
                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02539)
879b17
                               "client sent unknown Transfer-Encoding "
879b17
                               "(%s): %s", tenc, r->uri);
879b17
-                r->status = HTTP_BAD_REQUEST;
879b17
-                conn->keepalive = AP_CONN_CLOSE;
879b17
-                ap_send_error_response(r, 0);
879b17
-                ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
879b17
-                ap_run_log_transaction(r);
879b17
-                apr_brigade_destroy(tmp_bb);
879b17
-                goto traceout;
879b17
+                access_status = HTTP_BAD_REQUEST;
879b17
+                goto die_unusable_input;
879b17
             }
879b17
 
879b17
             /* http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-23
879b17
@@ -1412,88 +1505,81 @@ request_rec *ap_read_request(conn_rec *conn)
879b17
         }
879b17
     }
879b17
 
879b17
-    apr_brigade_destroy(tmp_bb);
879b17
-
879b17
-    /* update what we think the virtual host is based on the headers we've
879b17
-     * now read. may update status.
879b17
-     */
879b17
-    ap_update_vhost_from_headers(r);
879b17
-    access_status = r->status;
879b17
-
879b17
-    /* Toggle to the Host:-based vhost's timeout mode to fetch the
879b17
-     * request body and send the response body, if needed.
879b17
-     */
879b17
-    if (cur_timeout != r->server->timeout) {
879b17
-        apr_socket_timeout_set(csd, r->server->timeout);
879b17
-        cur_timeout = r->server->timeout;
879b17
-    }
879b17
-
879b17
-    /* we may have switched to another server */
879b17
-    r->per_dir_config = r->server->lookup_defaults;
879b17
-
879b17
-    if ((!r->hostname && (r->proto_num >= HTTP_VERSION(1, 1)))
879b17
-        || ((r->proto_num == HTTP_VERSION(1, 1))
879b17
-            && !apr_table_get(r->headers_in, "Host"))) {
879b17
-        /*
879b17
-         * Client sent us an HTTP/1.1 or later request without telling us the
879b17
-         * hostname, either with a full URL or a Host: header. We therefore
879b17
-         * need to (as per the 1.1 spec) send an error.  As a special case,
879b17
-         * HTTP/1.1 mentions twice (S9, S14.23) that a request MUST contain
879b17
-         * a Host: header, and the server MUST respond with 400 if it doesn't.
879b17
-         */
879b17
-        access_status = HTTP_BAD_REQUEST;
879b17
-        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00569)
879b17
-                      "client sent HTTP/1.1 request without hostname "
879b17
-                      "(see RFC2616 section 14.23): %s", r->uri);
879b17
-    }
879b17
-
879b17
     /*
879b17
      * Add the HTTP_IN filter here to ensure that ap_discard_request_body
879b17
      * called by ap_die and by ap_send_error_response works correctly on
879b17
      * status codes that do not cause the connection to be dropped and
879b17
      * in situations where the connection should be kept alive.
879b17
      */
879b17
-
879b17
     ap_add_input_filter_handle(ap_http_input_filter_handle,
879b17
                                NULL, r, r->connection);
879b17
 
879b17
-    if (access_status != HTTP_OK
879b17
-        || (access_status = ap_run_post_read_request(r))) {
879b17
-        ap_die(access_status, r);
879b17
-        ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
879b17
-        ap_run_log_transaction(r);
879b17
-        r = NULL;
879b17
-        goto traceout;
879b17
+    /* Validate Host/Expect headers and select vhost. */
879b17
+    if (!ap_check_request_header(r)) {
879b17
+        /* we may have switched to another server still */
879b17
+        apply_server_config(r);
879b17
+        access_status = r->status;
879b17
+        goto die_before_hooks;
879b17
     }
879b17
 
879b17
-    if (((expect = apr_table_get(r->headers_in, "Expect")) != NULL)
879b17
-        && (expect[0] != '\0')) {
879b17
-        /*
879b17
-         * The Expect header field was added to HTTP/1.1 after RFC 2068
879b17
-         * as a means to signal when a 100 response is desired and,
879b17
-         * unfortunately, to signal a poor man's mandatory extension that
879b17
-         * the server must understand or return 417 Expectation Failed.
879b17
-         */
879b17
-        if (strcasecmp(expect, "100-continue") == 0) {
879b17
-            r->expecting_100 = 1;
879b17
-        }
879b17
-        else {
879b17
-            r->status = HTTP_EXPECTATION_FAILED;
879b17
-            ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00570)
879b17
-                          "client sent an unrecognized expectation value of "
879b17
-                          "Expect: %s", expect);
879b17
-            ap_send_error_response(r, 0);
879b17
-            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
879b17
-            ap_run_log_transaction(r);
879b17
-            goto traceout;
879b17
-        }
879b17
+    /* we may have switched to another server */
879b17
+    apply_server_config(r);
879b17
+
879b17
+    if ((access_status = ap_run_post_read_request(r))) {
879b17
+        goto die;
879b17
     }
879b17
 
879b17
-    AP_READ_REQUEST_SUCCESS((uintptr_t)r, (char *)r->method, (char *)r->uri, (char *)r->server->defn_name, r->status);
879b17
+    AP_READ_REQUEST_SUCCESS((uintptr_t)r, (char *)r->method,
879b17
+                            (char *)r->uri, (char *)r->server->defn_name,
879b17
+                            r->status);
879b17
+
879b17
     return r;
879b17
-    traceout:
879b17
+
879b17
+    /* Everything falls through on failure */
879b17
+
879b17
+die_unusable_input:
879b17
+    /* Input filters are in an undeterminate state, cleanup (including
879b17
+     * CORE_IN's socket) such that any further attempt to read is EOF.
879b17
+     */
879b17
+    {
879b17
+        ap_filter_t *f = conn->input_filters;
879b17
+        while (f) {
879b17
+            if (f->frec == ap_core_input_filter_handle) {
879b17
+                core_net_rec *net = f->ctx;
879b17
+                apr_brigade_cleanup(net->in_ctx->b);
879b17
+                break;
879b17
+            }
879b17
+            ap_remove_input_filter(f);
879b17
+            f = f->next;
879b17
+        }
879b17
+        conn->input_filters = r->input_filters = f;
879b17
+        conn->keepalive = AP_CONN_CLOSE;
879b17
+    }
879b17
+
879b17
+die_before_hooks:
879b17
+    /* First call to ap_die() (non recursive) */
879b17
+    r->status = HTTP_OK;
879b17
+
879b17
+die:
879b17
+    ap_die(access_status, r);
879b17
+
879b17
+    /* ap_die() sent the response through the output filters, we must now
879b17
+     * end the request with an EOR bucket for stream/pipeline accounting.
879b17
+     */
879b17
+    {
879b17
+        apr_bucket_brigade *eor_bb;
879b17
+        eor_bb = apr_brigade_create(conn->pool, conn->bucket_alloc);
879b17
+        APR_BRIGADE_INSERT_TAIL(eor_bb,
879b17
+                                ap_bucket_eor_create(conn->bucket_alloc, r));
879b17
+        ap_pass_brigade(conn->output_filters, eor_bb);
879b17
+        apr_brigade_cleanup(eor_bb);
879b17
+    }
879b17
+
879b17
+ignore:
879b17
+    r = NULL;
879b17
+
879b17
     AP_READ_REQUEST_FAILURE((uintptr_t)r);
879b17
-    return r;
879b17
+    return NULL;
879b17
 }
879b17
 
879b17
 /* if a request with a body creates a subrequest, remove original request's
879b17
diff --git a/server/vhost.c b/server/vhost.c
879b17
index b23b2dd..6e233b5 100644
879b17
--- a/server/vhost.c
879b17
+++ b/server/vhost.c
879b17
@@ -34,6 +34,7 @@
879b17
 #include "http_vhost.h"
879b17
 #include "http_protocol.h"
879b17
 #include "http_core.h"
879b17
+#include "http_main.h"
879b17
 
879b17
 #if APR_HAVE_ARPA_INET_H
879b17
 #include <arpa/inet.h>
879b17
@@ -973,7 +974,13 @@ AP_DECLARE(int) ap_matches_request_vhost(request_rec *r, const char *host,
879b17
 }
879b17
 
879b17
 
879b17
-static void check_hostalias(request_rec *r)
879b17
+/*
879b17
+ * Updates r->server from ServerName/ServerAlias. Per the interaction
879b17
+ * of ip and name-based vhosts, it only looks in the best match from the
879b17
+ * connection-level ip-based matching.
879b17
+ * Returns HTTP_BAD_REQUEST if there was no match.
879b17
+ */
879b17
+static int update_server_from_aliases(request_rec *r)
879b17
 {
879b17
     /*
879b17
      * Even if the request has a Host: header containing a port we ignore
879b17
@@ -1050,11 +1057,18 @@ static void check_hostalias(request_rec *r)
879b17
         goto found;
879b17
     }
879b17
 
879b17
-    return;
879b17
+    if (!r->connection->vhost_lookup_data) { 
879b17
+        if (matches_aliases(r->server, host)) {
879b17
+            s = r->server;
879b17
+            goto found;
879b17
+        }
879b17
+    }
879b17
+    return HTTP_BAD_REQUEST;
879b17
 
879b17
 found:
879b17
     /* s is the first matching server, we're done */
879b17
     r->server = s;
879b17
+    return HTTP_OK;
879b17
 }
879b17
 
879b17
 
879b17
@@ -1071,7 +1085,7 @@ static void check_serverpath(request_rec *r)
879b17
      * This is in conjunction with the ServerPath code in http_core, so we
879b17
      * get the right host attached to a non- Host-sending request.
879b17
      *
879b17
-     * See the comment in check_hostalias about how each vhost can be
879b17
+     * See the comment in update_server_from_aliases about how each vhost can be
879b17
      * listed multiple times.
879b17
      */
879b17
 
879b17
@@ -1134,11 +1148,17 @@ static APR_INLINE const char *construct_host_header(request_rec *r,
879b17
 }
879b17
 
879b17
 AP_DECLARE(void) ap_update_vhost_from_headers(request_rec *r)
879b17
+{
879b17
+    ap_update_vhost_from_headers_ex(r, 0);
879b17
+}
879b17
+
879b17
+AP_DECLARE(int) ap_update_vhost_from_headers_ex(request_rec *r, int require_match)
879b17
 {
879b17
     core_server_config *conf = ap_get_core_module_config(r->server->module_config);
879b17
     const char *host_header = apr_table_get(r->headers_in, "Host");
879b17
     int is_v6literal = 0;
879b17
     int have_hostname_from_url = 0;
879b17
+    int rc = HTTP_OK;
879b17
 
879b17
     if (r->hostname) {
879b17
         /*
879b17
@@ -1151,8 +1171,8 @@ AP_DECLARE(void) ap_update_vhost_from_headers(request_rec *r)
879b17
     else if (host_header != NULL) {
879b17
         is_v6literal = fix_hostname(r, host_header, conf->http_conformance);
879b17
     }
879b17
-    if (r->status != HTTP_OK)
879b17
-        return;
879b17
+    if (!require_match && r->status != HTTP_OK)
879b17
+        return HTTP_OK;
879b17
 
879b17
     if (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE) {
879b17
         /*
879b17
@@ -1173,10 +1193,16 @@ AP_DECLARE(void) ap_update_vhost_from_headers(request_rec *r)
879b17
     /* check if we tucked away a name_chain */
879b17
     if (r->connection->vhost_lookup_data) {
879b17
         if (r->hostname)
879b17
-            check_hostalias(r);
879b17
+            rc = update_server_from_aliases(r);
879b17
         else
879b17
             check_serverpath(r);
879b17
     }
879b17
+    else if (require_match && r->hostname) { 
879b17
+        /* check the base server config */
879b17
+        rc = update_server_from_aliases(r);
879b17
+    }
879b17
+    
879b17
+    return rc;
879b17
 }
879b17
 
879b17
 /**