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