From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-3.8 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 2934F211BB for ; Sat, 1 Dec 2018 13:31:28 +0000 (UTC) From: Eric Wong To: kcar-public@bogomips.org Subject: [PATCH 08/11] do not assume SERVER_PORT Date: Sat, 1 Dec 2018 13:31:22 +0000 Message-Id: <20181201133125.5524-9-e@80x24.org> In-Reply-To: <20181201133125.5524-1-e@80x24.org> References: <20181201133125.5524-1-e@80x24.org> List-Id: Without X-Forwarded-{Proto,Ssl} or Forwarded header parsing, it is impossible for us to know to know which default port was specified for non-absolute-URI requests. We will only set SERVER_PORT for the rare absolute-URI requests and when we can parse the Host header. --- ext/kcar/kcar.rl | 5 +++-- test/test_request_parser.rb | 11 +++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ext/kcar/kcar.rl b/ext/kcar/kcar.rl index 6330910..e1445aa 100644 --- a/ext/kcar/kcar.rl +++ b/ext/kcar/kcar.rl @@ -160,7 +160,7 @@ static void set_server_vars(struct http_parser *hp, VALUE env, VALUE host) long host_len = RSTRING_LEN(host); char *colon; VALUE server_name = host; - VALUE server_port = hp->is_https ? g_443 : g_80; + VALUE server_port = hp->has_scheme ? (hp->is_https ? g_443 : g_80) : Qfalse; if (*host_ptr == '[') { /* ipv6 address format */ char *rbracket = memchr(host_ptr + 1, ']', host_len - 1); @@ -185,7 +185,8 @@ static void set_server_vars(struct http_parser *hp, VALUE env, VALUE host) } } rb_hash_aset(env, g_SERVER_NAME, server_name); - rb_hash_aset(env, g_SERVER_PORT, server_port); + if (server_port != Qfalse) + rb_hash_aset(env, g_SERVER_PORT, server_port); } static void http_09_request(VALUE env) diff --git a/test/test_request_parser.rb b/test/test_request_parser.rb index 7654e62..c4fefc8 100644 --- a/test/test_request_parser.rb +++ b/test/test_request_parser.rb @@ -96,7 +96,7 @@ class TestRequestParser < Test::Unit::TestCase buf = "GET / HTTP/1.1\r\nHost: foo\r\n\r\n" assert_same @env, @hp.request(@env, buf) assert_equal 'foo', @env['SERVER_NAME'] - assert_equal '80', @env['SERVER_PORT'] + assert_nil @env['SERVER_PORT'] assert_equal '', buf assert @hp.keepalive? end @@ -113,14 +113,14 @@ class TestRequestParser < Test::Unit::TestCase def test_parse_server_host_empty_port @hp.request(@env, "GET / HTTP/1.1\r\nHost: foo:\r\n\r\n") assert_equal 'foo', @env['SERVER_NAME'] - assert_equal '80', @env['SERVER_PORT'] + assert_nil @env['SERVER_PORT'] assert @hp.keepalive? end def test_parse_host_cont @hp.request(@env, "GET / HTTP/1.1\r\nHost:\r\n foo\r\n\r\n") assert_equal 'foo', @env['SERVER_NAME'] - assert_equal '80', @env['SERVER_PORT'] + assert_nil @env['SERVER_PORT'] assert @hp.keepalive? end @@ -541,7 +541,7 @@ class TestRequestParser < Test::Unit::TestCase req = @hp.request(@env, buf) assert_equal "[::1]", req['HTTP_HOST'] assert_equal "[::1]", req['SERVER_NAME'] - assert_equal '80', req['SERVER_PORT'] + assert_nil req['SERVER_PORT'] end def test_ipv6_host_header_with_port @@ -555,7 +555,7 @@ class TestRequestParser < Test::Unit::TestCase def test_ipv6_host_header_with_empty_port req = @hp.request(@env, "GET / HTTP/1.1\r\nHost: [::1]:\r\n\r\n") assert_equal "[::1]", req['SERVER_NAME'] - assert_equal '80', req['SERVER_PORT'] + assert_nil req['SERVER_PORT'] assert_equal "[::1]:", req['HTTP_HOST'] end @@ -836,7 +836,6 @@ class TestRequestParser < Test::Unit::TestCase expect = { 'HTTP_HOST' => 'example.com', 'SERVER_NAME' => 'example.com', - 'SERVER_PORT' => '80', 'REQUEST_PATH' => '/', 'SERVER_PROTOCOL' => 'HTTP/1.1', 'PATH_INFO' => '/',