From 1b31c40997ff8b932a457275e9a2f219de1d32c8 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 23 May 2011 21:04:56 +0000 Subject: strip trailing and leading linear whitespace in headers RFC 2616, section 4.2: > The field-content does not include any leading or trailing LWS: > linear white space occurring before the first non-whitespace > character of the field-value or after the last non-whitespace > character of the field-value. Such leading or trailing LWS MAY be > removed without changing the semantics of the field value. Any LWS > that occurs between field-content MAY be replaced with a single SP > before interpreting the field value or forwarding the message > downstream. --- test/unit/test_http_parser.rb | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'test') diff --git a/test/unit/test_http_parser.rb b/test/unit/test_http_parser.rb index dc1aab7..70bb789 100644 --- a/test/unit/test_http_parser.rb +++ b/test/unit/test_http_parser.rb @@ -258,6 +258,20 @@ class HttpParserTest < Test::Unit::TestCase assert_equal 'hi y x ASDF', req['HTTP_X_ASDF'] end + def test_continuation_eats_trailing_spaces + parser = HttpParser.new + header = "GET / HTTP/1.1\r\n" \ + "X-ASDF: \r\n" \ + "\t\r\n" \ + " b \r\n" \ + " ASDF\r\n\r\n" + parser.buf << header + req = parser.env + assert_equal req, parser.parse + assert_equal '', parser.buf + assert_equal 'b ASDF', req['HTTP_X_ASDF'] + end + def test_continuation_with_absolute_uri_and_ignored_host_header parser = HttpParser.new header = "GET http://example.com/ HTTP/1.1\r\n" \ @@ -764,6 +778,48 @@ class HttpParserTest < Test::Unit::TestCase end + def test_leading_tab + parser = HttpParser.new + get = "GET / HTTP/1.1\r\nHost:\texample.com\r\n\r\n" + assert parser.add_parse(get) + assert_equal 'example.com', parser.env['HTTP_HOST'] + end + + def test_trailing_whitespace + parser = HttpParser.new + get = "GET / HTTP/1.1\r\nHost: example.com \r\n\r\n" + assert parser.add_parse(get) + assert_equal 'example.com', parser.env['HTTP_HOST'] + end + + def test_trailing_tab + parser = HttpParser.new + get = "GET / HTTP/1.1\r\nHost: example.com\t\r\n\r\n" + assert parser.add_parse(get) + assert_equal 'example.com', parser.env['HTTP_HOST'] + end + + def test_trailing_multiple_linear_whitespace + parser = HttpParser.new + get = "GET / HTTP/1.1\r\nHost: example.com\t \t \t\r\n\r\n" + assert parser.add_parse(get) + assert_equal 'example.com', parser.env['HTTP_HOST'] + end + + def test_embedded_linear_whitespace_ok + parser = HttpParser.new + get = "GET / HTTP/1.1\r\nX-Space: hello\t world\t \r\n\r\n" + assert parser.add_parse(get) + assert_equal "hello\t world", parser.env["HTTP_X_SPACE"] + end + + def test_empty_header + parser = HttpParser.new + get = "GET / HTTP/1.1\r\nHost: \r\n\r\n" + assert parser.add_parse(get) + assert_equal '', parser.env['HTTP_HOST'] + end + # so we don't care about the portability of this test # if it doesn't leak on Linux, it won't leak anywhere else # unless your C compiler or platform is otherwise broken -- cgit v1.2.3-24-ge0c7