about summary refs log tree commit homepage
path: root/test
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-02-02 14:45:57 -0800
committerEric Wong <normalperson@yhbt.net>2011-02-02 14:45:57 -0800
commitd140e7b1ff44b06bc54c2b790d06e9c7325503fe (patch)
tree0985a4e7195bb8973a6d098cef3831ea63801db4 /test
parent24f8ef5f385e38954a5582fb2e8cd9d12fbf7d20 (diff)
downloadunicorn-d140e7b1ff44b06bc54c2b790d06e9c7325503fe.tar.gz
Just in case we have people that don't use DNS, we can support
folks who enter ugly IPv6 addresses...

IPv6 uses brackets around the address to avoid confusing
the colons used in the address with the colon used to denote
the TCP port number in URIs.
Diffstat (limited to 'test')
-rw-r--r--test/unit/test_http_parser.rb152
1 files changed, 152 insertions, 0 deletions
diff --git a/test/unit/test_http_parser.rb b/test/unit/test_http_parser.rb
index 9da43c9..e6d48e3 100644
--- a/test/unit/test_http_parser.rb
+++ b/test/unit/test_http_parser.rb
@@ -483,6 +483,158 @@ class HttpParserTest < Test::Unit::TestCase
     assert parser.keepalive? # TODO: read HTTP/1.2 when it's final
   end
 
+  def test_absolute_ipv6_uri
+    parser = HttpParser.new
+    req = {}
+    url = "http://[::1]/foo?q=bar"
+    http = "GET #{url} HTTP/1.1\r\n" \
+           "Host: bad.example.com\r\n\r\n"
+    assert_equal req, parser.headers(req, http)
+    assert_equal 'http', req['rack.url_scheme']
+    assert_equal '/foo?q=bar', req['REQUEST_URI']
+    assert_equal '/foo', req['REQUEST_PATH']
+    assert_equal 'q=bar', req['QUERY_STRING']
+
+    uri = URI.parse(url)
+    assert_equal "[::1]", uri.host,
+                 "URI.parse changed upstream for #{url}? host=#{uri.host}"
+    assert_equal "[::1]", req['HTTP_HOST']
+    assert_equal "[::1]", req['SERVER_NAME']
+    assert_equal '80', req['SERVER_PORT']
+    assert_equal "", http
+    assert parser.keepalive? # TODO: read HTTP/1.2 when it's final
+  end
+
+  def test_absolute_ipv6_uri_alpha
+    parser = HttpParser.new
+    req = {}
+    url = "http://[::a]/"
+    http = "GET #{url} HTTP/1.1\r\n" \
+           "Host: bad.example.com\r\n\r\n"
+    assert_equal req, parser.headers(req, http)
+    assert_equal 'http', req['rack.url_scheme']
+
+    uri = URI.parse(url)
+    assert_equal "[::a]", uri.host,
+                 "URI.parse changed upstream for #{url}? host=#{uri.host}"
+    assert_equal "[::a]", req['HTTP_HOST']
+    assert_equal "[::a]", req['SERVER_NAME']
+    assert_equal '80', req['SERVER_PORT']
+  end
+
+  def test_absolute_ipv6_uri_alpha_2
+    parser = HttpParser.new
+    req = {}
+    url = "http://[::B]/"
+    http = "GET #{url} HTTP/1.1\r\n" \
+           "Host: bad.example.com\r\n\r\n"
+    assert_equal req, parser.headers(req, http)
+    assert_equal 'http', req['rack.url_scheme']
+
+    uri = URI.parse(url)
+    assert_equal "[::B]", uri.host,
+                 "URI.parse changed upstream for #{url}? host=#{uri.host}"
+    assert_equal "[::B]", req['HTTP_HOST']
+    assert_equal "[::B]", req['SERVER_NAME']
+    assert_equal '80', req['SERVER_PORT']
+  end
+
+  def test_absolute_ipv6_uri_with_empty_port
+    parser = HttpParser.new
+    req = {}
+    url = "https://[::1]:/foo?q=bar"
+    http = "GET #{url} HTTP/1.1\r\n" \
+           "Host: bad.example.com\r\n\r\n"
+    assert_equal req, parser.headers(req, http)
+    assert_equal 'https', req['rack.url_scheme']
+    assert_equal '/foo?q=bar', req['REQUEST_URI']
+    assert_equal '/foo', req['REQUEST_PATH']
+    assert_equal 'q=bar', req['QUERY_STRING']
+
+    uri = URI.parse(url)
+    assert_equal "[::1]", uri.host,
+                 "URI.parse changed upstream for #{url}? host=#{uri.host}"
+    assert_equal "[::1]:", req['HTTP_HOST']
+    assert_equal "[::1]", req['SERVER_NAME']
+    assert_equal '443', req['SERVER_PORT']
+    assert_equal "", http
+    assert parser.keepalive? # TODO: read HTTP/1.2 when it's final
+  end
+
+  def test_absolute_ipv6_uri_with_port
+    parser = HttpParser.new
+    req = {}
+    url = "https://[::1]:666/foo?q=bar"
+    http = "GET #{url} HTTP/1.1\r\n" \
+           "Host: bad.example.com\r\n\r\n"
+    assert_equal req, parser.headers(req, http)
+    assert_equal 'https', req['rack.url_scheme']
+    assert_equal '/foo?q=bar', req['REQUEST_URI']
+    assert_equal '/foo', req['REQUEST_PATH']
+    assert_equal 'q=bar', req['QUERY_STRING']
+
+    uri = URI.parse(url)
+    assert_equal "[::1]", uri.host,
+                 "URI.parse changed upstream for #{url}? host=#{uri.host}"
+    assert_equal "[::1]:666", req['HTTP_HOST']
+    assert_equal "[::1]", req['SERVER_NAME']
+    assert_equal '666', req['SERVER_PORT']
+    assert_equal "", http
+    assert parser.keepalive? # TODO: read HTTP/1.2 when it's final
+  end
+
+  def test_ipv6_host_header
+    parser = HttpParser.new
+    req = {}
+    http = "GET / HTTP/1.1\r\n" \
+           "Host: [::1]\r\n\r\n"
+    assert_equal req, parser.headers(req, http)
+    assert_equal "[::1]", req['HTTP_HOST']
+    assert_equal "[::1]", req['SERVER_NAME']
+    assert_equal '80', req['SERVER_PORT']
+    assert_equal "", http
+    assert parser.keepalive? # TODO: read HTTP/1.2 when it's final
+  end
+
+  def test_ipv6_host_header_with_port
+    parser = HttpParser.new
+    req = {}
+    http = "GET / HTTP/1.1\r\n" \
+           "Host: [::1]:666\r\n\r\n"
+    assert_equal req, parser.headers(req, http)
+    assert_equal "[::1]", req['SERVER_NAME']
+    assert_equal '666', req['SERVER_PORT']
+    assert_equal "[::1]:666", req['HTTP_HOST']
+    assert_equal "", http
+    assert parser.keepalive? # TODO: read HTTP/1.2 when it's final
+  end
+
+  def test_ipv6_host_header_with_empty_port
+    parser = HttpParser.new
+    req = {}
+    http = "GET / HTTP/1.1\r\n" \
+           "Host: [::1]:\r\n\r\n"
+    assert_equal req, parser.headers(req, http)
+    assert_equal "[::1]", req['SERVER_NAME']
+    assert_equal '80', req['SERVER_PORT']
+    assert_equal "[::1]:", req['HTTP_HOST']
+    assert_equal "", http
+    assert parser.keepalive? # TODO: read HTTP/1.2 when it's final
+  end
+
+  # XXX Highly unlikely..., just make sure we don't segfault or assert on it
+  def test_broken_ipv6_host_header
+    parser = HttpParser.new
+    req = {}
+    http = "GET / HTTP/1.1\r\n" \
+           "Host: [::1:\r\n\r\n"
+    assert_equal req, parser.headers(req, http)
+    assert_equal "[", req['SERVER_NAME']
+    assert_equal ':1:', req['SERVER_PORT']
+    assert_equal "[::1:", req['HTTP_HOST']
+    assert_equal "", http
+  end
+
   def test_put_body_oneshot
     parser = HttpParser.new
     req = {}