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