summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorValentin Bartenev <vbart@nginx.com>2019-02-27 17:25:07 +0300
committerValentin Bartenev <vbart@nginx.com>2019-02-27 17:25:07 +0300
commite929d08201aae949db25ae8c1051a91e96ba3011 (patch)
tree76af15b49cb67547dbd1942466a6bb360459fc6b
parenta881c31abdb9fda2ba0d2e05c45c2ce890a6cfab (diff)
downloadunit-e929d08201aae949db25ae8c1051a91e96ba3011.tar.gz
unit-e929d08201aae949db25ae8c1051a91e96ba3011.tar.bz2
Fixed processing of SERVER_NAME after 77aad2c142a0.
Previously, the nxt_router_prepare_msg() function expected server host among other headers unmodified. It's not true anymore since normalization of the Host header has been introduced in 77aad2c142a0. The nxt_unit_split_host() function was removed. It didn't work correctly with IPv6 literals. Anyway, after 77aad2c142a0 the port splitting is done in router while Host header processing.
-rw-r--r--src/go/unit/nxt_cgo_lib.c7
-rw-r--r--src/nxt_application.h2
-rw-r--r--src/nxt_http_request.c9
-rw-r--r--src/nxt_php_sapi.c31
-rw-r--r--src/nxt_python_wsgi.c29
-rw-r--r--src/nxt_router.c24
-rw-r--r--src/nxt_unit.c57
-rw-r--r--src/nxt_unit_field.h1
-rw-r--r--src/nxt_unit_request.h3
-rw-r--r--src/perl/nxt_perl_psgi.c30
-rw-r--r--src/ruby/nxt_ruby.c28
11 files changed, 54 insertions, 167 deletions
diff --git a/src/go/unit/nxt_cgo_lib.c b/src/go/unit/nxt_cgo_lib.c
index 172bef88..98a23482 100644
--- a/src/go/unit/nxt_cgo_lib.c
+++ b/src/go/unit/nxt_cgo_lib.c
@@ -75,14 +75,11 @@ nxt_cgo_request_handler(nxt_unit_request_info_t *req)
nxt_go_request_add_header(go_req,
nxt_cgo_str_init(&name, &f->name, f->name_length),
nxt_cgo_str_init(&value, &f->value, f->value_length));
-
- if (f->hash == NXT_UNIT_HASH_HOST) {
- host = value;
- }
}
nxt_go_request_set_content_length(go_req, r->content_length);
- nxt_go_request_set_host(go_req, &host);
+ nxt_go_request_set_host(go_req,
+ nxt_cgo_str_init(&host, &r->server_name, r->server_name_length));
nxt_go_request_set_remote_addr(go_req,
nxt_cgo_str_init(&remote_addr, &r->remote, r->remote_length));
diff --git a/src/nxt_application.h b/src/nxt_application.h
index 10f5a922..fe8113c6 100644
--- a/src/nxt_application.h
+++ b/src/nxt_application.h
@@ -95,13 +95,13 @@ typedef struct {
nxt_str_t version;
nxt_str_t path;
nxt_str_t query;
+ nxt_str_t server_name;
nxt_list_t *fields;
nxt_str_t cookie;
nxt_str_t content_length;
nxt_str_t content_type;
- nxt_str_t host;
off_t parsed_content_length;
nxt_bool_t done;
diff --git a/src/nxt_http_request.c b/src/nxt_http_request.c
index f6c14df9..724b0808 100644
--- a/src/nxt_http_request.c
+++ b/src/nxt_http_request.c
@@ -367,6 +367,13 @@ nxt_http_request_application(nxt_task_t *task, nxt_http_request_t *r,
ar->r.header.method = *r->method;
}
+ if (r->host.length != 0) {
+ ar->r.header.server_name = r->host;
+
+ } else {
+ nxt_str_set(&ar->r.header.server_name, "localhost");
+ }
+
ar->r.header.target = r->target;
if (r->path != NULL) {
@@ -377,8 +384,6 @@ nxt_http_request_application(nxt_task_t *task, nxt_http_request_t *r,
ar->r.header.query = *r->args;
}
- ar->r.header.host = r->host;
-
if (r->content_type != NULL) {
ar->r.header.content_type.length = r->content_type->value_length;
ar->r.header.content_type.start = r->content_type->value;
diff --git a/src/nxt_php_sapi.c b/src/nxt_php_sapi.c
index 8c25f82a..559a3e37 100644
--- a/src/nxt_php_sapi.c
+++ b/src/nxt_php_sapi.c
@@ -62,7 +62,7 @@ static void nxt_php_set_sptr(nxt_unit_request_info_t *req, const char *name,
nxt_inline void nxt_php_set_str(nxt_unit_request_info_t *req, const char *name,
nxt_str_t *s, zval *track_vars_array TSRMLS_DC);
static void nxt_php_set_cstr(nxt_unit_request_info_t *req, const char *name,
- char *str, uint32_t len, zval *track_vars_array TSRMLS_DC);
+ const char *str, uint32_t len, zval *track_vars_array TSRMLS_DC);
static void nxt_php_register_variables(zval *track_vars_array TSRMLS_DC);
#ifdef NXT_HAVE_PHP_LOG_MESSAGE_WITH_SYSLOG_TYPE
static void nxt_php_log_message(char *message, int syslog_type_int);
@@ -846,8 +846,6 @@ nxt_php_read_cookies(TSRMLS_D)
static void
nxt_php_register_variables(zval *track_vars_array TSRMLS_DC)
{
- char *host_start, *port_start;
- uint32_t host_length, port_length;
const char *name;
nxt_unit_field_t *f, *f_end;
nxt_php_run_ctx_t *ctx;
@@ -928,6 +926,10 @@ nxt_php_register_variables(zval *track_vars_array TSRMLS_DC)
nxt_php_set_sptr(req, "SERVER_ADDR", &r->local, r->local_length,
track_vars_array TSRMLS_CC);
+ nxt_php_set_sptr(req, "SERVER_NAME", &r->server_name, r->server_name_length,
+ track_vars_array TSRMLS_CC);
+ nxt_php_set_cstr(req, "SERVER_PORT", "80", 2, track_vars_array TSRMLS_CC);
+
f_end = r->fields + r->fields_count;
for (f = r->fields; f < f_end; f++) {
name = nxt_unit_sptr_get(&f->name);
@@ -949,25 +951,6 @@ nxt_php_register_variables(zval *track_vars_array TSRMLS_DC)
nxt_php_set_sptr(req, "CONTENT_TYPE", &f->value, f->value_length,
track_vars_array TSRMLS_CC);
}
-
- if (r->host_field != NXT_UNIT_NONE_FIELD) {
- f = r->fields + r->host_field;
-
- host_start = nxt_unit_sptr_get(&f->value);
- host_length = f->value_length;
-
- } else {
- host_start = NULL;
- host_length = 0;
- }
-
- nxt_unit_split_host(host_start, host_length, &host_start, &host_length,
- &port_start, &port_length);
-
- nxt_php_set_cstr(req, "SERVER_NAME", host_start, host_length,
- track_vars_array TSRMLS_CC);
- nxt_php_set_cstr(req, "SERVER_PORT", port_start, port_length,
- track_vars_array TSRMLS_CC);
}
@@ -997,7 +980,7 @@ nxt_php_set_str(nxt_unit_request_info_t *req, const char *name,
static void
nxt_php_set_cstr(nxt_unit_request_info_t *req, const char *name,
- char *cstr, uint32_t len, zval *track_vars_array TSRMLS_DC)
+ const char *cstr, uint32_t len, zval *track_vars_array TSRMLS_DC)
{
if (nxt_slow_path(cstr == NULL)) {
return;
@@ -1005,7 +988,7 @@ nxt_php_set_cstr(nxt_unit_request_info_t *req, const char *name,
nxt_unit_req_debug(req, "php: register %s='%.*s'", name, (int) len, cstr);
- php_register_variable_safe((char *) name, cstr, len,
+ php_register_variable_safe((char *) name, (char *) cstr, len,
track_vars_array TSRMLS_CC);
}
diff --git a/src/nxt_python_wsgi.c b/src/nxt_python_wsgi.c
index bd3a2cb2..871c8e91 100644
--- a/src/nxt_python_wsgi.c
+++ b/src/nxt_python_wsgi.c
@@ -72,7 +72,7 @@ static PyObject *nxt_python_get_environ(nxt_python_run_ctx_t *ctx);
static int nxt_python_add_sptr(nxt_python_run_ctx_t *ctx, const char *name,
nxt_unit_sptr_t *sptr, uint32_t size);
static int nxt_python_add_str(nxt_python_run_ctx_t *ctx, const char *name,
- char *str, uint32_t size);
+ const char *str, uint32_t size);
static PyObject *nxt_py_start_resp(PyObject *self, PyObject *args);
static int nxt_python_response_add_field(nxt_python_run_ctx_t *ctx,
@@ -690,8 +690,8 @@ static PyObject *
nxt_python_get_environ(nxt_python_run_ctx_t *ctx)
{
int rc;
- char *name, *host_start, *port_start;
- uint32_t i, host_length, port_length;
+ char *name;
+ uint32_t i;
PyObject *environ;
nxt_unit_field_t *f;
nxt_unit_request_t *r;
@@ -732,6 +732,10 @@ nxt_python_get_environ(nxt_python_run_ctx_t *ctx)
RC(nxt_python_add_sptr(ctx, "SERVER_PROTOCOL", &r->version,
r->version_length));
+ RC(nxt_python_add_sptr(ctx, "SERVER_NAME", &r->server_name,
+ r->server_name_length));
+ RC(nxt_python_add_str(ctx, "SERVER_PORT", "80", 2));
+
for (i = 0; i < r->fields_count; i++) {
f = r->fields + i;
name = nxt_unit_sptr_get(&f->name);
@@ -753,23 +757,6 @@ nxt_python_get_environ(nxt_python_run_ctx_t *ctx)
f->value_length));
}
- if (r->host_field != NXT_UNIT_NONE_FIELD) {
- f = r->fields + r->host_field;
-
- host_start = nxt_unit_sptr_get(&f->value);
- host_length = f->value_length;
-
- } else {
- host_start = NULL;
- host_length = 0;
- }
-
- nxt_unit_split_host(host_start, host_length, &host_start, &host_length,
- &port_start, &port_length);
-
- RC(nxt_python_add_str(ctx, "SERVER_NAME", host_start, host_length));
- RC(nxt_python_add_str(ctx, "SERVER_PORT", port_start, port_length));
-
#undef RC
return environ;
@@ -818,7 +805,7 @@ nxt_python_add_sptr(nxt_python_run_ctx_t *ctx, const char *name,
static int
nxt_python_add_str(nxt_python_run_ctx_t *ctx, const char *name,
- char *str, uint32_t size)
+ const char *str, uint32_t size)
{
PyObject *value;
diff --git a/src/nxt_router.c b/src/nxt_router.c
index e81a2f96..7cd28416 100644
--- a/src/nxt_router.c
+++ b/src/nxt_router.c
@@ -4662,12 +4662,13 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
h = &r->header;
req_size = sizeof(nxt_unit_request_t)
- + h->method.length + 1
- + h->version.length + 1
- + r->remote.length + 1
- + r->local.length + 1
- + h->target.length + 1
- + (h->path.start != h->target.start ? h->path.length + 1 : 0);
+ + h->method.length + 1
+ + h->version.length + 1
+ + r->remote.length + 1
+ + r->local.length + 1
+ + h->server_name.length + 1
+ + h->target.length + 1
+ + (h->path.start != h->target.start ? h->path.length + 1 : 0);
fields_count = 0;
@@ -4722,6 +4723,11 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
p = nxt_cpymem(p, r->local.start, r->local.length);
*p++ = '\0';
+ req->server_name_length = h->server_name.length;
+ nxt_unit_sptr_set(&req->server_name, p);
+ p = nxt_cpymem(p, h->server_name.start, h->server_name.length);
+ *p++ = '\0';
+
target_pos = p;
req->target_length = h->target.length;
nxt_unit_sptr_set(&req->target, p);
@@ -4749,7 +4755,6 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
req->query.offset = 0;
}
- req->host_field = NXT_UNIT_NONE_FIELD;
req->content_length_field = NXT_UNIT_NONE_FIELD;
req->content_type_field = NXT_UNIT_NONE_FIELD;
req->cookie_field = NXT_UNIT_NONE_FIELD;
@@ -4769,10 +4774,7 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
dst_field->name_length = field->name_length + prefix->length;
dst_field->value_length = field->value_length;
- if (field->value == h->host.start) {
- req->host_field = dst_field - req->fields;
-
- } else if (field->value == h->content_length.start) {
+ if (field->value == h->content_length.start) {
req->content_length_field = dst_field - req->fields;
} else if (field->value == h->content_type.start) {
diff --git a/src/nxt_unit.c b/src/nxt_unit.c
index fa1fb1d5..ec8ba641 100644
--- a/src/nxt_unit.c
+++ b/src/nxt_unit.c
@@ -890,59 +890,6 @@ nxt_unit_field_hash(const char *name, size_t name_length)
void
-nxt_unit_split_host(char *host, uint32_t host_length,
- char **name, uint32_t *name_length, char **port, uint32_t *port_length)
-{
- char *cpos;
-
- static char default_host[] = "localhost";
- static char default_port[] = "80";
-
- if (nxt_slow_path(host == NULL || host_length == 0)) {
- *name = default_host;
- *name_length = nxt_length(default_host);
-
- *port = default_port;
- *port_length = nxt_length(default_port);
-
- return;
- }
-
- cpos = memchr(host, ':', host_length);
-
- if (nxt_slow_path(cpos == NULL)) {
- *name = host;
- *name_length = host_length;
-
- *port = default_port;
- *port_length = nxt_length(default_port);
-
- return;
- }
-
- if (nxt_slow_path(cpos == host)) {
- *name = default_host;
- *name_length = nxt_length(default_host);
-
- } else {
- *name = host;
- *name_length = cpos - host;
- }
-
- cpos++;
-
- if (nxt_slow_path(host + host_length == cpos)) {
- *port = default_port;
- *port_length = nxt_length(default_port);
-
- } else {
- *port = cpos;
- *port_length = host_length - (cpos - host);
- }
-}
-
-
-void
nxt_unit_request_group_dup_fields(nxt_unit_request_info_t *req)
{
uint32_t i, j;
@@ -957,10 +904,6 @@ nxt_unit_request_group_dup_fields(nxt_unit_request_info_t *req)
for (i = 0; i < r->fields_count; i++) {
switch (fields[i].hash) {
- case NXT_UNIT_HASH_HOST:
- r->host_field = i;
- break;
-
case NXT_UNIT_HASH_CONTENT_LENGTH:
r->content_length_field = i;
break;
diff --git a/src/nxt_unit_field.h b/src/nxt_unit_field.h
index 0d490f31..d19db0f0 100644
--- a/src/nxt_unit_field.h
+++ b/src/nxt_unit_field.h
@@ -12,7 +12,6 @@
#include "nxt_unit_sptr.h"
enum {
- NXT_UNIT_HASH_HOST = 0xE6EB,
NXT_UNIT_HASH_CONTENT_LENGTH = 0x1EA0,
NXT_UNIT_HASH_CONTENT_TYPE = 0x5F7D,
NXT_UNIT_HASH_COOKIE = 0x23F2,
diff --git a/src/nxt_unit_request.h b/src/nxt_unit_request.h
index af5c29a1..88d569a6 100644
--- a/src/nxt_unit_request.h
+++ b/src/nxt_unit_request.h
@@ -19,12 +19,12 @@ struct nxt_unit_request_s {
uint8_t version_length;
uint8_t remote_length;
uint8_t local_length;
+ uint32_t server_name_length;
uint32_t target_length;
uint32_t path_length;
uint32_t query_length;
uint32_t fields_count;
- uint32_t host_field;
uint32_t content_length_field;
uint32_t content_type_field;
uint32_t cookie_field;
@@ -35,6 +35,7 @@ struct nxt_unit_request_s {
nxt_unit_sptr_t version;
nxt_unit_sptr_t remote;
nxt_unit_sptr_t local;
+ nxt_unit_sptr_t server_name;
nxt_unit_sptr_t target;
nxt_unit_sptr_t path;
nxt_unit_sptr_t query;
diff --git a/src/perl/nxt_perl_psgi.c b/src/perl/nxt_perl_psgi.c
index da4a3864..a86fd0de 100644
--- a/src/perl/nxt_perl_psgi.c
+++ b/src/perl/nxt_perl_psgi.c
@@ -68,7 +68,7 @@ static SV *nxt_perl_psgi_env_create(PerlInterpreter *my_perl,
nxt_inline int nxt_perl_psgi_add_sptr(PerlInterpreter *my_perl, HV *hash_env,
const char *name, uint32_t name_len, nxt_unit_sptr_t *sptr, uint32_t len);
nxt_inline int nxt_perl_psgi_add_str(PerlInterpreter *my_perl, HV *hash_env,
- const char *name, uint32_t name_len, char *str, uint32_t len);
+ const char *name, uint32_t name_len, const char *str, uint32_t len);
nxt_inline int nxt_perl_psgi_add_value(PerlInterpreter *my_perl, HV *hash_env,
const char *name, uint32_t name_len, void *value);
@@ -450,8 +450,7 @@ nxt_perl_psgi_env_create(PerlInterpreter *my_perl,
{
HV *hash_env;
AV *array_version;
- char *host_start, *port_start;
- uint32_t i, host_length, port_length;
+ uint32_t i;
nxt_unit_field_t *f;
nxt_unit_request_t *r;
@@ -519,6 +518,10 @@ nxt_perl_psgi_env_create(PerlInterpreter *my_perl,
RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("SERVER_ADDR"),
&r->local, r->local_length));
+ RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("SERVER_NAME"),
+ &r->server_name, r->server_name_length));
+ RC(nxt_perl_psgi_add_str(my_perl, hash_env, NL("SERVER_PORT"), "80", 2));
+
for (i = 0; i < r->fields_count; i++) {
f = r->fields + i;
@@ -541,25 +544,6 @@ nxt_perl_psgi_env_create(PerlInterpreter *my_perl,
&f->value, f->value_length));
}
- if (r->host_field != NXT_UNIT_NONE_FIELD) {
- f = r->fields + r->host_field;
-
- host_start = nxt_unit_sptr_get(&f->value);
- host_length = f->value_length;
-
- } else {
- host_start = NULL;
- host_length = 0;
- }
-
- nxt_unit_split_host(host_start, host_length, &host_start, &host_length,
- &port_start, &port_length);
-
- RC(nxt_perl_psgi_add_str(my_perl, hash_env, NL("SERVER_NAME"),
- host_start, host_length));
- RC(nxt_perl_psgi_add_str(my_perl, hash_env, NL("SERVER_PORT"),
- port_start, port_length));
-
#undef NL
#undef RC
@@ -584,7 +568,7 @@ nxt_perl_psgi_add_sptr(PerlInterpreter *my_perl, HV *hash_env,
nxt_inline int
nxt_perl_psgi_add_str(PerlInterpreter *my_perl, HV *hash_env,
- const char *name, uint32_t name_len, char *str, uint32_t len)
+ const char *name, uint32_t name_len, const char *str, uint32_t len)
{
SV **ha;
diff --git a/src/ruby/nxt_ruby.c b/src/ruby/nxt_ruby.c
index a08b8189..d099a338 100644
--- a/src/ruby/nxt_ruby.c
+++ b/src/ruby/nxt_ruby.c
@@ -45,7 +45,7 @@ static int nxt_ruby_read_request(VALUE hash_env);
nxt_inline void nxt_ruby_add_sptr(VALUE hash_env,
const char *name, uint32_t name_len, nxt_unit_sptr_t *sptr, uint32_t len);
nxt_inline void nxt_ruby_add_str(VALUE hash_env,
- const char *name, uint32_t name_len, char *str, uint32_t len);
+ const char *name, uint32_t name_len, const char *str, uint32_t len);
static nxt_int_t nxt_ruby_rack_result_status(VALUE result);
static int nxt_ruby_rack_result_headers(VALUE result, nxt_int_t status);
static int nxt_ruby_hash_info(VALUE r_key, VALUE r_value, VALUE arg);
@@ -428,8 +428,7 @@ fail:
static int
nxt_ruby_read_request(VALUE hash_env)
{
- char *host_start, *port_start;
- uint32_t i, host_length, port_length;
+ uint32_t i;
nxt_unit_field_t *f;
nxt_unit_request_t *r;
@@ -452,6 +451,10 @@ nxt_ruby_read_request(VALUE hash_env)
r->remote_length);
nxt_ruby_add_sptr(hash_env, NL("SERVER_ADDR"), &r->local, r->local_length);
+ nxt_ruby_add_sptr(hash_env, NL("SERVER_NAME"), &r->server_name,
+ r->server_name_length);
+ nxt_ruby_add_str(hash_env, NL("SERVER_PORT"), "80", 2);
+
for (i = 0; i < r->fields_count; i++) {
f = r->fields + i;
@@ -473,23 +476,6 @@ nxt_ruby_read_request(VALUE hash_env)
&f->value, f->value_length);
}
- if (r->host_field != NXT_UNIT_NONE_FIELD) {
- f = r->fields + r->host_field;
-
- host_start = nxt_unit_sptr_get(&f->value);
- host_length = f->value_length;
-
- } else {
- host_start = NULL;
- host_length = 0;
- }
-
- nxt_unit_split_host(host_start, host_length, &host_start, &host_length,
- &port_start, &port_length);
-
- nxt_ruby_add_str(hash_env, NL("SERVER_NAME"), host_start, host_length);
- nxt_ruby_add_str(hash_env, NL("SERVER_PORT"), port_start, port_length);
-
#undef NL
return NXT_UNIT_OK;
@@ -510,7 +496,7 @@ nxt_ruby_add_sptr(VALUE hash_env,
nxt_inline void
nxt_ruby_add_str(VALUE hash_env,
- const char *name, uint32_t name_len, char *str, uint32_t len)
+ const char *name, uint32_t name_len, const char *str, uint32_t len)
{
rb_hash_aset(hash_env, rb_str_new(name, name_len), rb_str_new(str, len));
}