Skip to content

Commit c8b859e

Browse files
committed
Set SERVER_PORT appropriately
The Perl, PHP, Python, Ruby & Java language modules all hard code SERVER_PORT to "80". Adjust them to bring them in line with the wasm language module which uses r->local_port (I.e. the port unit accepted the connection on). Closes: #761 Signed-off-by: Andrew Clayton <[email protected]>
1 parent a9071e1 commit c8b859e

File tree

6 files changed

+16
-35
lines changed

6 files changed

+16
-35
lines changed

src/java/nxt_jni_Request.c

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -624,36 +624,14 @@ nxt_java_Request_getServerName(JNIEnv *env, jclass cls, jlong req_ptr)
624624
static jint JNICALL
625625
nxt_java_Request_getServerPort(JNIEnv *env, jclass cls, jlong req_ptr)
626626
{
627-
jint res;
628-
char *host, *colon, tmp;
629-
nxt_unit_field_t *f;
627+
char *p;
630628
nxt_unit_request_t *r;
631629

632630
r = nxt_jlong2ptr(req_ptr);
633631

634-
f = nxt_java_findHeader(r->fields, r->fields + r->fields_count,
635-
"Host", 4);
636-
if (f != NULL) {
637-
host = nxt_unit_sptr_get(&f->value);
638-
639-
colon = memchr(host, ':', f->value_length);
640-
641-
if (colon == NULL) {
642-
return 80;
643-
}
644-
645-
tmp = host[f->value_length];
646-
647-
host[f->value_length] = '\0';
648-
649-
res = strtol(colon + 1, NULL, 10);
650-
651-
host[f->value_length] = tmp;
652-
653-
return res;
654-
}
632+
p = nxt_unit_sptr_get(&r->local_port);
655633

656-
return nxt_java_Request_getLocalPort(env, cls, req_ptr);
634+
return strtol(p, NULL, 10);
657635
}
658636

659637

src/nxt_php_sapi.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,8 @@ nxt_php_register_variables(zval *track_vars_array TSRMLS_DC)
15001500

15011501
nxt_php_set_sptr(req, "SERVER_NAME", &r->server_name, r->server_name_length,
15021502
track_vars_array TSRMLS_CC);
1503-
nxt_php_set_cstr(req, "SERVER_PORT", "80", 2, track_vars_array TSRMLS_CC);
1503+
nxt_php_set_sptr(req, "SERVER_PORT", &r->local_port, r->local_port_length,
1504+
track_vars_array TSRMLS_CC);
15041505

15051506
if (r->tls) {
15061507
nxt_php_set_cstr(req, "HTTPS", "on", 2, track_vars_array TSRMLS_CC);

src/perl/nxt_perl_psgi.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,8 @@ nxt_perl_psgi_env_create(PerlInterpreter *my_perl,
673673

674674
RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("SERVER_NAME"),
675675
&r->server_name, r->server_name_length));
676-
RC(nxt_perl_psgi_add_str(my_perl, hash_env, NL("SERVER_PORT"), "80", 2));
676+
RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("SERVER_PORT"),
677+
&r->local_port, r->local_port_length));
677678

678679
for (i = 0; i < r->fields_count; i++) {
679680
f = r->fields + i;

src/python/nxt_python_asgi.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req,
644644
PyObject *scope, *v, *type, *scheme;
645645
PyObject *headers, *header;
646646
nxt_str_t prefix;
647+
unsigned long port;
647648
nxt_unit_field_t *f;
648649
nxt_unit_request_t *r;
649650

@@ -752,7 +753,10 @@ nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req,
752753
SET_ITEM(scope, client, v)
753754
Py_DECREF(v);
754755

755-
v = nxt_py_asgi_create_address(&r->local_addr, r->local_addr_length, 80);
756+
p = nxt_unit_sptr_get(&r->local_port);
757+
port = strtoul(p, NULL, 10);
758+
759+
v = nxt_py_asgi_create_address(&r->local_addr, r->local_addr_length, port);
756760
if (nxt_slow_path(v == NULL)) {
757761
nxt_unit_req_alert(req, "Python failed to create 'server' pair");
758762
goto fail;

src/python/nxt_python_wsgi.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ static PyTypeObject nxt_py_input_type = {
131131

132132
static PyObject *nxt_py_environ_ptyp;
133133

134-
static PyObject *nxt_py_80_str;
135134
static PyObject *nxt_py_close_str;
136135
static PyObject *nxt_py_content_length_str;
137136
static PyObject *nxt_py_content_type_str;
@@ -151,7 +150,6 @@ static PyObject *nxt_py_wsgi_input_str;
151150
static PyObject *nxt_py_wsgi_uri_scheme_str;
152151

153152
static nxt_python_string_t nxt_python_strings[] = {
154-
{ nxt_string("80"), &nxt_py_80_str },
155153
{ nxt_string("close"), &nxt_py_close_str },
156154
{ nxt_string("CONTENT_LENGTH"), &nxt_py_content_length_str },
157155
{ nxt_string("CONTENT_TYPE"), &nxt_py_content_type_str },
@@ -638,6 +636,8 @@ nxt_python_get_environ(nxt_python_ctx_t *pctx,
638636
r->remote_length));
639637
RC(nxt_python_add_sptr(pctx, nxt_py_server_addr_str, &r->local_addr,
640638
r->local_addr_length));
639+
RC(nxt_python_add_sptr(pctx, nxt_py_server_port_str, &r->local_port,
640+
r->local_port_length));
641641

642642
if (r->tls) {
643643
RC(nxt_python_add_obj(pctx, nxt_py_wsgi_uri_scheme_str,
@@ -652,7 +652,6 @@ nxt_python_get_environ(nxt_python_ctx_t *pctx,
652652

653653
RC(nxt_python_add_sptr(pctx, nxt_py_server_name_str, &r->server_name,
654654
r->server_name_length));
655-
RC(nxt_python_add_obj(pctx, nxt_py_server_port_str, nxt_py_80_str));
656655

657656
nxt_unit_request_group_dup_fields(pctx->req);
658657

src/ruby/nxt_ruby.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ typedef struct {
106106
VALUE *v;
107107
} nxt_ruby_string_t;
108108

109-
static VALUE nxt_rb_80_str;
110109
static VALUE nxt_rb_content_length_str;
111110
static VALUE nxt_rb_content_type_str;
112111
static VALUE nxt_rb_http_str;
@@ -127,7 +126,6 @@ static VALUE nxt_rb_on_thread_boot;
127126
static VALUE nxt_rb_on_thread_shutdown;
128127

129128
static nxt_ruby_string_t nxt_rb_strings[] = {
130-
{ nxt_string("80"), &nxt_rb_80_str },
131129
{ nxt_string("CONTENT_LENGTH"), &nxt_rb_content_length_str },
132130
{ nxt_string("CONTENT_TYPE"), &nxt_rb_content_type_str },
133131
{ nxt_string("http"), &nxt_rb_http_str },
@@ -754,11 +752,11 @@ nxt_ruby_read_request(nxt_unit_request_info_t *req, VALUE hash_env)
754752
r->remote_length);
755753
nxt_ruby_add_sptr(hash_env, nxt_rb_server_addr_str, &r->local_addr,
756754
r->local_addr_length);
755+
nxt_ruby_add_sptr(hash_env, nxt_rb_server_port_str, &r->local_port,
756+
r->local_port_length);
757757
nxt_ruby_add_sptr(hash_env, nxt_rb_server_name_str, &r->server_name,
758758
r->server_name_length);
759759

760-
rb_hash_aset(hash_env, nxt_rb_server_port_str, nxt_rb_80_str);
761-
762760
rb_hash_aset(hash_env, nxt_rb_rack_url_scheme_str,
763761
r->tls ? nxt_rb_https_str : nxt_rb_http_str);
764762

0 commit comments

Comments
 (0)