]> git.kaiwu.me - nginx.git/commitdiff
Changed interface of ngx_http_validate_host().
authorSergey Kandaurov <pluknet@nginx.com>
Wed, 5 Nov 2025 12:15:12 +0000 (16:15 +0400)
committerRoman Arutyunyan <arutyunyan.roman@gmail.com>
Wed, 26 Nov 2025 15:51:40 +0000 (19:51 +0400)
This allows to process a port subcomponent and save it in r->port
in a unified way, similar to r->headers_in.server.  For HTTP/1.x
request line in the absolute form, r->host_end now includes a port
subcomponent, which is also consistent with HTTP/2 and HTTP/3.

src/http/ngx_http.h
src/http/ngx_http_parse.c
src/http/ngx_http_request.c
src/http/v2/ngx_http_v2.c
src/http/v3/ngx_http_v3_request.c

index 922d3f4f9ec538eba865da6024dc60e34db32485..4fb6b17ea4c204afc71c2756c6bd39678ec7407b 100644 (file)
@@ -130,8 +130,8 @@ ngx_int_t ngx_http_post_request(ngx_http_request_t *r,
     ngx_http_posted_request_t *pr);
 ngx_int_t ngx_http_set_virtual_server(ngx_http_request_t *r,
     ngx_str_t *host);
-ngx_int_t ngx_http_validate_host(ngx_str_t *host, ngx_pool_t *pool,
-    ngx_uint_t alloc);
+ngx_int_t ngx_http_validate_host(ngx_str_t *host, in_port_t *port,
+    ngx_pool_t *pool, ngx_uint_t alloc);
 void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t rc);
 void ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc);
 void ngx_http_free_request(ngx_http_request_t *r, ngx_int_t rc);
index 4dfeb4bcfc46855b4b056eccffe43c95e6b975dc..059edae44a8ea9a348c6d2d5431eed693b3b1c38 100644 (file)
@@ -383,21 +383,18 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
 
         case sw_host_end:
 
+            if (ch == ':') {
+                state = sw_port;
+                break;
+            }
+
             r->host_end = p;
 
             if (r->method == NGX_HTTP_CONNECT) {
-                if (ch == ':') {
-                    state = sw_port;
-                    break;
-                }
-
                 return NGX_HTTP_PARSE_INVALID_REQUEST;
             }
 
             switch (ch) {
-            case ':':
-                state = sw_port;
-                break;
             case '/':
                 r->uri_start = p;
                 state = sw_after_slash_in_uri;
@@ -465,14 +462,11 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
 
         case sw_port:
             if (ch >= '0' && ch <= '9') {
-                if (r->port >= 6553 && (r->port > 6553 || (ch - '0') > 5)) {
-                    return NGX_HTTP_PARSE_INVALID_REQUEST;
-                }
-
-                r->port = r->port * 10 + (ch - '0');
                 break;
             }
 
+            r->host_end = p;
+
             if (r->method == NGX_HTTP_CONNECT) {
                 if (ch == ' ') {
                     state = sw_http_09;
index 557de66968d7752ba6a818e12876175068bcb708..41c1cda041a5f10bd01cae98822a38e32e13ea72 100644 (file)
@@ -931,7 +931,7 @@ ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg)
         goto done;
     }
 
-    rc = ngx_http_validate_host(&host, c->pool, 1);
+    rc = ngx_http_validate_host(&host, NULL, c->pool, 1);
 
     if (rc == NGX_ERROR) {
         goto error;
@@ -1107,6 +1107,7 @@ ngx_http_process_request_line(ngx_event_t *rev)
     ssize_t              n;
     ngx_int_t            rc, rv;
     ngx_str_t            host;
+    in_port_t            port;
     ngx_connection_t    *c;
     ngx_http_request_t  *r;
 
@@ -1169,7 +1170,7 @@ ngx_http_process_request_line(ngx_event_t *rev)
                 host.len = r->host_end - r->host_start;
                 host.data = r->host_start;
 
-                rc = ngx_http_validate_host(&host, r->pool, 0);
+                rc = ngx_http_validate_host(&host, &port, r->pool, 0);
 
                 if (rc == NGX_DECLINED) {
                     ngx_log_error(NGX_LOG_INFO, c->log, 0,
@@ -1188,6 +1189,7 @@ ngx_http_process_request_line(ngx_event_t *rev)
                 }
 
                 r->headers_in.server = host;
+                r->port = port;
             }
 
             if (r->http_version < NGX_HTTP_VERSION_10) {
@@ -1846,9 +1848,9 @@ static ngx_int_t
 ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h,
     ngx_uint_t offset)
 {
-    u_char     *p;
-    ngx_int_t   rc;
-    ngx_str_t   host;
+    ngx_int_t  rc;
+    ngx_str_t  host;
+    in_port_t  port;
 
     if (r->headers_in.host) {
         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
@@ -1865,7 +1867,7 @@ ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h,
 
     host = h->value;
 
-    rc = ngx_http_validate_host(&host, r->pool, 0);
+    rc = ngx_http_validate_host(&host, &port, r->pool, 0);
 
     if (rc == NGX_DECLINED) {
         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
@@ -1888,17 +1890,7 @@ ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h,
     }
 
     r->headers_in.server = host;
-
-    p = ngx_strlchr(h->value.data + host.len,
-                    h->value.data + h->value.len, ':');
-
-    if (p) {
-        rc = ngx_atoi(p + 1, h->value.data + h->value.len - p - 1);
-
-        if (rc > 0 && rc < 65536) {
-            r->port = rc;
-        }
-    }
+    r->port = port;
 
     return NGX_OK;
 }
@@ -2182,7 +2174,8 @@ ngx_http_process_request(ngx_http_request_t *r)
 
 
 ngx_int_t
-ngx_http_validate_host(ngx_str_t *host, ngx_pool_t *pool, ngx_uint_t alloc)
+ngx_http_validate_host(ngx_str_t *host, in_port_t *portp, ngx_pool_t *pool,
+    ngx_uint_t alloc)
 {
     u_char     *h, ch;
     size_t      i, dot_pos, host_len;
@@ -2370,6 +2363,10 @@ ngx_http_validate_host(ngx_str_t *host, ngx_pool_t *pool, ngx_uint_t alloc)
 
     host->len = host_len;
 
+    if (portp) {
+        *portp = port;
+    }
+
     return NGX_OK;
 }
 
index 4bfee589ad49699df866cb03f8c3af619020cba1..49ea25edefb63178e5d22102c654539f709dafa9 100644 (file)
@@ -3518,8 +3518,8 @@ ngx_http_v2_parse_scheme(ngx_http_request_t *r, ngx_str_t *value)
 static ngx_int_t
 ngx_http_v2_parse_authority(ngx_http_request_t *r, ngx_str_t *value)
 {
-    u_char     *p;
-    ngx_int_t   rc;
+    ngx_int_t  rc;
+    in_port_t  port;
 
     if (r->host_start) {
         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
@@ -3530,7 +3530,7 @@ ngx_http_v2_parse_authority(ngx_http_request_t *r, ngx_str_t *value)
     r->host_start = value->data;
     r->host_end = value->data + value->len;
 
-    rc = ngx_http_validate_host(value, r->pool, 0);
+    rc = ngx_http_validate_host(value, &port, r->pool, 0);
 
     if (rc == NGX_DECLINED) {
         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
@@ -3552,16 +3552,7 @@ ngx_http_v2_parse_authority(ngx_http_request_t *r, ngx_str_t *value)
     }
 
     r->headers_in.server = *value;
-
-    p = ngx_strlchr(r->host_start + value->len, r->host_end, ':');
-
-    if (p) {
-        rc = ngx_atoi(p + 1, r->host_end - p - 1);
-
-        if (rc > 0 && rc < 65536) {
-            r->port = rc;
-        }
-    }
+    r->port = port;
 
     return NGX_OK;
 }
index 77c55bfeec95ec8ff0fa19f4bcb6c0687e6b8c40..6865e14664da72875c3c29a588e420d37f71eb55 100644 (file)
@@ -904,6 +904,7 @@ ngx_http_v3_init_pseudo_headers(ngx_http_request_t *r)
     u_char     *p;
     ngx_int_t   rc;
     ngx_str_t   host;
+    in_port_t   port;
 
     if (r->request_line.len) {
         return NGX_OK;
@@ -961,7 +962,7 @@ ngx_http_v3_init_pseudo_headers(ngx_http_request_t *r)
         host.len = r->host_end - r->host_start;
         host.data = r->host_start;
 
-        rc = ngx_http_validate_host(&host, r->pool, 0);
+        rc = ngx_http_validate_host(&host, &port, r->pool, 0);
 
         if (rc == NGX_DECLINED) {
             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
@@ -979,16 +980,7 @@ ngx_http_v3_init_pseudo_headers(ngx_http_request_t *r)
         }
 
         r->headers_in.server = host;
-
-        p = ngx_strlchr(r->host_start + host.len, r->host_end, ':');
-
-        if (p) {
-            rc = ngx_atoi(p + 1, r->host_end - p - 1);
-
-            if (rc > 0 && rc < 65536) {
-                r->port = rc;
-            }
-        }
+        r->port = port;
     }
 
     if (ngx_list_init(&r->headers_in.headers, r->pool, 20,