]> git.kaiwu.me - nginx.git/commitdiff
nginx-0.0.3-2004-05-14-20:51:47 import
authorIgor Sysoev <igor@sysoev.ru>
Fri, 14 May 2004 16:51:47 +0000 (16:51 +0000)
committerIgor Sysoev <igor@sysoev.ru>
Fri, 14 May 2004 16:51:47 +0000 (16:51 +0000)
src/http/modules/ngx_http_gzip_filter.c
src/http/modules/ngx_http_range_filter.c
src/http/ngx_http_core_module.c
src/http/ngx_http_core_module.h
src/http/ngx_http_request.c
src/http/ngx_http_request.h
src/http/ngx_http_special_response.c

index b7b5abf961d409f715809e62dfd17994cf95a5bc..76b07bfb572eccceb3ff1306546eb7072aa0c870 100644 (file)
@@ -18,7 +18,7 @@ typedef struct {
     int                  level;
     size_t               wbits;
     size_t               memlevel;
-    size_t               min_length;
+    ssize_t              min_length;
 } ngx_http_gzip_conf_t;
 
 
@@ -913,7 +913,7 @@ static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf,
     ngx_conf_merge_size_value(conf->wbits, prev->wbits, MAX_WBITS);
     ngx_conf_merge_size_value(conf->memlevel, prev->memlevel,
                               MAX_MEM_LEVEL - 1);
-    ngx_conf_merge_size_value(conf->min_length, prev->min_length, 0);
+    ngx_conf_merge_value(conf->min_length, prev->min_length, 0);
     ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0);
 
     return NGX_CONF_OK;
index d63f4c8d5b968060b49aa314a6bc92be72baf0d2..ecb42ad3d1332e64d5166e6abae416742ca1ac51 100644 (file)
@@ -114,17 +114,6 @@ static ngx_int_t ngx_http_range_header_filter(ngx_http_request_t *r)
         || r->headers_out.status != NGX_HTTP_OK
         || r->headers_out.content_length_n == -1
         || !(r->filter & NGX_HTTP_FILTER_ALLOW_RANGES))
-
-#if 0
-        /* STUB: we currently support ranges for file hunks only */
-        || !r->sendfile
-        || r->filter & NGX_HTTP_FILTER_NEED_IN_MEMORY
-#endif
-
-#if 0
-        || (r->headers_out.content_encoding
-            && r->headers_out.content_encoding->value.len))
-#endif
     {
         return ngx_http_next_header_filter(r);
     }
@@ -506,8 +495,7 @@ static ngx_int_t ngx_http_range_body_filter(ngx_http_request_t *r,
         return ngx_http_next_body_filter(r, out);
     }
 
-    /* TODO: several incoming hunks of proxied responses
-             and memory hunks on platforms that have no sendfile() */
+    /* TODO: alert */
 
     return ngx_http_next_body_filter(r, in);
 }
index b8e1a195d52284c79a6638c7a89abb482f203671..73a114ac5c3479e15e813d7114822ac4cfc79cf9 100644 (file)
@@ -37,6 +37,14 @@ static char *ngx_http_lowat_check(ngx_conf_t *cf, void *post, void *data);
 static ngx_conf_post_t  ngx_http_lowat_post = { ngx_http_lowat_check } ;
 
 
+static ngx_conf_enum_t  ngx_http_restrict_host_names[] = {
+    { ngx_string("off"), NGX_HTTP_RESTRICT_HOST_OFF },
+    { ngx_string("on"), NGX_HTTP_RESTRICT_HOST_ON },
+    { ngx_string("close"), NGX_HTTP_RESTRICT_HOST_CLOSE },
+    { ngx_null_string, 0 }
+};
+
+
 static ngx_command_t  ngx_http_core_commands[] = {
 
     { ngx_string("server"),
@@ -88,6 +96,13 @@ static ngx_command_t  ngx_http_core_commands[] = {
       offsetof(ngx_http_core_srv_conf_t, large_client_header),
       NULL },
 
+    { ngx_string("restrict_host_names"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_enum_slot,
+      NGX_HTTP_SRV_CONF_OFFSET,
+      offsetof(ngx_http_core_srv_conf_t, restrict_host_names),
+      &ngx_http_restrict_host_names },
+
     { ngx_string("location"),
       NGX_HTTP_SRV_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12,
       ngx_location_block,
@@ -1127,6 +1142,7 @@ static void *ngx_http_core_create_srv_conf(ngx_conf_t *cf)
     cscf->client_header_timeout = NGX_CONF_UNSET_MSEC;
     cscf->client_header_buffer_size = NGX_CONF_UNSET_SIZE;
     cscf->large_client_header = NGX_CONF_UNSET;
+    cscf->restrict_host_names = NGX_CONF_UNSET_UINT;
 
     return cscf;
 }
@@ -1182,6 +1198,8 @@ static char *ngx_http_core_merge_srv_conf(ngx_conf_t *cf,
                               prev->client_header_buffer_size, 1024);
     ngx_conf_merge_value(conf->large_client_header,
                          prev->large_client_header, 1);
+    ngx_conf_merge_unsigned_value(conf->restrict_host_names,
+                                  prev->restrict_host_names, 0);
 
     return NGX_CONF_OK;
 }
index 40cf40f0dd9ee5aba0971132f64e2de8689cf971..a8a6cf0ef2d51b4e20a40bacee37841314a02637 100644 (file)
@@ -58,6 +58,8 @@ typedef struct {
     ngx_msec_t   post_accept_timeout;
     ngx_msec_t   client_header_timeout;
 
+    ngx_uint_t   restrict_host_names;
+
     ngx_flag_t   large_client_header;
 } ngx_http_core_srv_conf_t;
 
index a8c4aeed26d647e2dbe1083e0e922484ff31f586..25cc295ee3b3489669fc5691770d12e3aa6d8c33 100644 (file)
@@ -39,7 +39,8 @@ static char *client_header_errors[] = {
     "client %s sent too long header line, URL: %s",
     "client %s sent HTTP/1.1 request without \"Host\" header, URL: %s",
     "client %s sent invalid \"Content-Length\" header, URL: %s",
-    "client %s sent POST method without \"Content-Length\" header, URL: %s"
+    "client %s sent POST method without \"Content-Length\" header, URL: %s",
+    "client %s sent invalid \"Host\" header \"%s\", URL: %s"
 };
 
 
@@ -847,6 +848,7 @@ static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r)
     size_t                     len;
     ngx_uint_t                 i;
     ngx_http_server_name_t    *name;
+    ngx_http_core_srv_conf_t  *cscf;
     ngx_http_core_loc_conf_t  *clcf;
 
     if (r->headers_in.host) {
@@ -881,6 +883,14 @@ static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r)
             }
         }
 
+        if (i == r->virtual_names->nelts) {
+            cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
+
+            if (cscf->restrict_host_names != NGX_HTTP_RESTRICT_HOST_OFF) {
+                return NGX_HTTP_PARSE_INVALID_HOST;
+            }
+        }
+
     } else {
         if (r->http_version > NGX_HTTP_VERSION_10) {
             return NGX_HTTP_PARSE_NO_HOST_HEADER;
@@ -1653,7 +1663,8 @@ void ngx_http_close_connection(ngx_connection_t *c)
 static void ngx_http_client_error(ngx_http_request_t *r,
                                   int client_error, int error)
 {
-    ngx_http_log_ctx_t  *ctx;
+    ngx_http_log_ctx_t        *ctx;
+    ngx_http_core_srv_conf_t  *cscf;
 
     ctx = r->connection->log->data;
 
@@ -1668,9 +1679,26 @@ static void ngx_http_client_error(ngx_http_request_t *r,
     r->connection->log->handler = NULL;
 
     if (ctx->url) {
-        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+        if (client_error == NGX_HTTP_PARSE_INVALID_HOST) {
+            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                    client_header_errors[client_error - NGX_HTTP_CLIENT_ERROR],
+                    ctx->client, r->headers_in.host->value.data, ctx->url);
+
+            error = NGX_HTTP_INVALID_HOST;
+
+            cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
+
+            if (cscf->restrict_host_names == NGX_HTTP_RESTRICT_HOST_CLOSE) {
+                ngx_http_close_request(r, error);
+                ngx_http_close_connection(r->connection);
+                return;
+            }
+
+        } else {
+            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                     client_header_errors[client_error - NGX_HTTP_CLIENT_ERROR],
                     ctx->client, ctx->url);
+        }
 
     } else {
         if (error == NGX_HTTP_REQUEST_URI_TOO_LARGE) {
index 716818fa48f57b96cf21bc70d67fc5cf6bba6f77..8013ef840f690ffaab375e6bd9597a0266087c3f 100644 (file)
@@ -31,6 +31,7 @@
 #define NGX_HTTP_PARSE_NO_HOST_HEADER      16
 #define NGX_HTTP_PARSE_INVALID_CL_HEADER   17
 #define NGX_HTTP_PARSE_POST_WO_CL_HEADER   18
+#define NGX_HTTP_PARSE_INVALID_HOST        19
 
 
 #define NGX_HTTP_OK                        200
 #define NGX_HTTP_REQUEST_URI_TOO_LARGE     414
 #define NGX_HTTP_RANGE_NOT_SATISFIABLE     416
 
+
+/* Our own HTTP codes */
+
+#define NGX_HTTP_NGX_CODES                 NGX_HTTP_INVALID_HOST
+
+/*
+ * We use the special code for the requests with invalid host name
+ * to distinguish it from 4XX in an error page redirection 
+ */
+#define NGX_HTTP_INVALID_HOST              498
+
 /*
  * HTTP does not define the code for the case when a client closed
  * the connection while we are processing its request so we introduce
@@ -58,6 +70,7 @@
  */
 #define NGX_HTTP_CLIENT_CLOSED_REQUEST     499
 
+
 #define NGX_HTTP_INTERNAL_SERVER_ERROR     500
 #define NGX_HTTP_NOT_IMPLEMENTED           501
 #define NGX_HTTP_BAD_GATEWAY               502
 #define NGX_HTTP_GATEWAY_TIME_OUT          504
 
 
+typedef enum {
+    NGX_HTTP_RESTRICT_HOST_OFF = 0,
+    NGX_HTTP_RESTRICT_HOST_ON,
+    NGX_HTTP_RESTRICT_HOST_CLOSE
+} ngx_http_restrict_host_e;
+
+
 typedef enum {
     NGX_HTTP_INITING_REQUEST_STATE = 0,
     NGX_HTTP_READING_REQUEST_STATE,
index 55f4bb3ac305c4d6225bf5c886cda7c2cb8a7a85..48963464ad90b2557c2113f144bbe13b276005c2 100644 (file)
@@ -166,6 +166,9 @@ static ngx_str_t error_pages[] = {
     ngx_null_string,             /* 415 */
     ngx_string(error_416_page),
 
+    ngx_string(error_404_page),  /* 498 */
+    ngx_null_string,             /* 499 */
+
     ngx_string(error_500_page),
     ngx_string(error_501_page),
     ngx_string(error_502_page),
@@ -229,13 +232,20 @@ int ngx_http_special_response_handler(ngx_http_request_t *r, int error)
         /* 3XX */
         err = error - NGX_HTTP_MOVED_PERMANENTLY;
 
-    } else if (error < NGX_HTTP_INTERNAL_SERVER_ERROR) {
+    } else if (error < NGX_HTTP_NGX_CODES) {
         /* 4XX */
         err = error - NGX_HTTP_BAD_REQUEST + 3;
 
     } else {
-        /* 5XX */
-        err = error - NGX_HTTP_INTERNAL_SERVER_ERROR + 3 + 17;
+        /* 49X, 5XX */
+        err = error - NGX_HTTP_NGX_CODES + 3 + 17;
+
+        switch (error) {
+            case NGX_HTTP_INVALID_HOST:
+                r->headers_out.status = NGX_HTTP_NOT_FOUND;
+                error = NGX_HTTP_NOT_FOUND;
+                break;
+        }
     }
 
     if (error_pages[err].len) {