about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-04-21 11:14:49 -0700
committerEric Wong <normalperson@yhbt.net>2009-04-21 11:16:33 -0700
commitba916e542a46686185cb701e87a98a617c0f4078 (patch)
treefd2930b9f9649298146c07a9f9e08ce905f8b6e4
parentb5ff27899a3caf8a66be1d024e727ac166dd3b3f (diff)
downloadunicorn-ba916e542a46686185cb701e87a98a617c0f4078.tar.gz
This means "Host: foo-bar:" (trailing colon) will assume
server_port is 80, not a blank string.
-rw-r--r--ext/unicorn/http11/http11.c6
-rw-r--r--test/unit/test_http_parser.rb33
2 files changed, 37 insertions, 2 deletions
diff --git a/ext/unicorn/http11/http11.c b/ext/unicorn/http11/http11.c
index f8ee151..36e0e54 100644
--- a/ext/unicorn/http11/http11.c
+++ b/ext/unicorn/http11/http11.c
@@ -288,9 +288,11 @@ static void header_done(void *data, const char *at, size_t length)
   if ((temp = rb_hash_aref(req, global_http_host)) != Qnil) {
     char *colon = memchr(RSTRING_PTR(temp), ':', RSTRING_LEN(temp));
     if (colon) {
+      long port_start = colon - RSTRING_PTR(temp) + 1;
+
       server_name = rb_str_substr(temp, 0, colon - RSTRING_PTR(temp));
-      server_port = rb_str_substr(temp, colon - RSTRING_PTR(temp)+1,
-                                  RSTRING_LEN(temp));
+      if ((RSTRING_LEN(temp) - port_start) > 0)
+        server_port = rb_str_substr(temp, port_start, RSTRING_LEN(temp));
     } else {
       server_name = temp;
     }
diff --git a/test/unit/test_http_parser.rb b/test/unit/test_http_parser.rb
index 1f3c9d2..50dfea7 100644
--- a/test/unit/test_http_parser.rb
+++ b/test/unit/test_http_parser.rb
@@ -43,6 +43,39 @@ class HttpParserTest < Test::Unit::TestCase
     assert_equal '', req['QUERY_STRING']
   end
 
+  def test_parse_server_host_default_port
+    parser = HttpParser.new
+    req = {}
+    assert parser.execute(req, "GET / HTTP/1.1\r\nHost: foo\r\n\r\n")
+    assert_equal 'foo', req['SERVER_NAME']
+    assert_equal '80', req['SERVER_PORT']
+  end
+
+  def test_parse_server_host_alt_port
+    parser = HttpParser.new
+    req = {}
+    assert parser.execute(req, "GET / HTTP/1.1\r\nHost: foo:999\r\n\r\n")
+    assert_equal 'foo', req['SERVER_NAME']
+    assert_equal '999', req['SERVER_PORT']
+  end
+
+  def test_parse_server_host_empty_port
+    parser = HttpParser.new
+    req = {}
+    assert parser.execute(req, "GET / HTTP/1.1\r\nHost: foo:\r\n\r\n")
+    assert_equal 'foo', req['SERVER_NAME']
+    assert_equal '80', req['SERVER_PORT']
+  end
+
+  def test_parse_server_host_xfp_https
+    parser = HttpParser.new
+    req = {}
+    assert parser.execute(req, "GET / HTTP/1.1\r\nHost: foo:\r\n" \
+                          "X-Forwarded-Proto: https\r\n\r\n")
+    assert_equal 'foo', req['SERVER_NAME']
+    assert_equal '443', req['SERVER_PORT']
+  end
+
   def test_parse_strange_headers
     parser = HttpParser.new
     req = {}