Changed interface of ngx_http_validate_host().

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.
This commit is contained in:
Sergey Kandaurov 2025-11-05 16:15:12 +04:00 committed by Roman Arutyunyan
parent 511abb19e1
commit 6446f99107
5 changed files with 31 additions and 57 deletions

View 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);

View File

@ -383,21 +383,18 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
case sw_host_end:
r->host_end = p;
if (r->method == NGX_HTTP_CONNECT) {
if (ch == ':') {
state = sw_port;
break;
}
r->host_end = p;
if (r->method == NGX_HTTP_CONNECT) {
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;

View 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;
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;
}

View 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;
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;
}

View 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,