1 # Copyright (c) 2005 Zed A. Shaw
2 # You can redistribute it and/or modify it under the same terms as Ruby.
4 # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
5 # for more information.
7 require 'test/test_helper'
11 class HttpParserTest < Test::Unit::TestCase
14 parser = HttpParser.new
16 http = "GET / HTTP/1.1\r\n\r\n"
17 assert parser.execute(req, http)
19 assert_equal 'HTTP/1.1', req['SERVER_PROTOCOL']
20 assert_equal '/', req['REQUEST_PATH']
21 assert_equal 'HTTP/1.1', req['HTTP_VERSION']
22 assert_equal '/', req['REQUEST_URI']
23 assert_equal 'GET', req['REQUEST_METHOD']
24 assert_nil req['FRAGMENT']
25 assert_nil req['QUERY_STRING']
30 assert ! parser.execute(req, "G")
33 # try parsing again to ensure we were reset correctly
34 http = "GET /hello-world HTTP/1.1\r\n\r\n"
35 assert parser.execute(req, http)
37 assert_equal 'HTTP/1.1', req['SERVER_PROTOCOL']
38 assert_equal '/hello-world', req['REQUEST_PATH']
39 assert_equal 'HTTP/1.1', req['HTTP_VERSION']
40 assert_equal '/hello-world', req['REQUEST_URI']
41 assert_equal 'GET', req['REQUEST_METHOD']
42 assert_nil req['FRAGMENT']
43 assert_nil req['QUERY_STRING']
46 def test_parse_strange_headers
47 parser = HttpParser.new
49 should_be_good = "GET / HTTP/1.1\r\naaaaaaaaaaaaa:++++++++++\r\n\r\n"
50 assert parser.execute(req, should_be_good)
52 # ref: http://thread.gmane.org/gmane.comp.lang.ruby.Unicorn.devel/37/focus=45
53 # (note we got 'pen' mixed up with 'pound' in that thread,
54 # but the gist of it is still relevant: these nasty headers are irrelevant
56 # nasty_pound_header = "GET / HTTP/1.1\r\nX-SSL-Bullshit: -----BEGIN CERTIFICATE-----\r\n\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx\r\n\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT\r\n\tAk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ZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE\r\n\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO\r\n\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1\r\n\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0\r\n\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD\r\n\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv\r\n\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3\r\n\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8\r\n\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk\r\n\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK\r\n\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu\r\n\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3\r\n\tRA==\r\n\t-----END CERTIFICATE-----\r\n\r\n"
57 # parser = HttpParser.new
59 # assert parser.execute(req, nasty_pound_header, 0)
62 def test_parse_ie6_urls
66 /we/love/you/ie6?q=<"">
70 parser = HttpParser.new
72 sorta_safe = %(GET #{path} HTTP/1.1\r\n\r\n)
73 assert parser.execute(req, sorta_safe)
78 parser = HttpParser.new
80 bad_http = "GET / SsUTF/1.1"
82 assert_raises(HttpParserError) { parser.execute(req, bad_http) }
84 assert(parser.execute({}, "GET / HTTP/1.0\r\n\r\n"))
88 parser = HttpParser.new
91 assert ! parser.execute(req, http)
92 assert_raises(HttpParserError) { parser.execute(req, http) }
93 assert ! parser.execute(req, http << " / HTTP/1.0")
94 assert_equal '/', req['REQUEST_PATH']
95 assert_equal '/', req['REQUEST_URI']
96 assert_equal 'GET', req['REQUEST_METHOD']
97 assert ! parser.execute(req, http << "\r\n")
98 assert_equal 'HTTP/1.0', req['HTTP_VERSION']
99 assert ! parser.execute(req, http << "\r")
100 assert parser.execute(req, http << "\n")
101 assert_equal 'HTTP/1.1', req['SERVER_PROTOCOL']
102 assert_nil req['FRAGMENT']
103 assert_nil req['QUERY_STRING']
106 def test_put_body_oneshot
107 parser = HttpParser.new
109 http = "PUT / HTTP/1.0\r\nContent-Length: 5\r\n\r\nabcde"
110 assert parser.execute(req, http)
111 assert_equal '/', req['REQUEST_PATH']
112 assert_equal '/', req['REQUEST_URI']
113 assert_equal 'PUT', req['REQUEST_METHOD']
114 assert_equal 'HTTP/1.0', req['HTTP_VERSION']
115 assert_equal 'HTTP/1.1', req['SERVER_PROTOCOL']
116 assert_equal "abcde", req[:http_body]
119 def test_put_body_later
120 parser = HttpParser.new
122 http = "PUT /l HTTP/1.0\r\nContent-Length: 5\r\n\r\n"
123 assert parser.execute(req, http)
124 assert_equal '/l', req['REQUEST_PATH']
125 assert_equal '/l', req['REQUEST_URI']
126 assert_equal 'PUT', req['REQUEST_METHOD']
127 assert_equal 'HTTP/1.0', req['HTTP_VERSION']
128 assert_equal 'HTTP/1.1', req['SERVER_PROTOCOL']
129 assert_equal "", req[:http_body]
132 def test_fragment_in_uri
133 parser = HttpParser.new
135 get = "GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1\r\n\r\n"
137 assert_nothing_raised do
138 ok = parser.execute(req, get)
141 assert_equal '/forums/1/topics/2375?page=1', req['REQUEST_URI']
142 assert_equal 'posts-17408', req['FRAGMENT']
143 assert_equal 'page=1', req['QUERY_STRING']
146 # lame random garbage maker
147 def rand_data(min, max, readable=true)
148 count = min + ((rand(max)+1) *10).to_i
149 res = count.to_s + "/"
152 res << Digest::SHA1.hexdigest(rand(count * 100).to_s) * (count / 40)
154 res << Digest::SHA1.digest(rand(count * 100).to_s) * (count / 20)
161 def test_horrible_queries
162 parser = HttpParser.new
164 # then that large header names are caught
166 get = "GET /#{rand_data(10,120)} HTTP/1.1\r\nX-#{rand_data(1024, 1024+(c*1024))}: Test\r\n\r\n"
167 assert_raises Unicorn::HttpParserError do
168 parser.execute({}, get)
173 # then that large mangled field values are caught
175 get = "GET /#{rand_data(10,120)} HTTP/1.1\r\nX-Test: #{rand_data(1024, 1024+(c*1024), false)}\r\n\r\n"
176 assert_raises Unicorn::HttpParserError do
177 parser.execute({}, get)
182 # then large headers are rejected too
183 get = "GET /#{rand_data(10,120)} HTTP/1.1\r\n"
184 get << "X-Test: test\r\n" * (80 * 1024)
185 assert_raises Unicorn::HttpParserError do
186 parser.execute({}, get)
190 # finally just that random garbage gets blocked all the time
192 get = "GET #{rand_data(1024, 1024+(c*1024), false)} #{rand_data(1024, 1024+(c*1024), false)}\r\n\r\n"
193 assert_raises Unicorn::HttpParserError do
194 parser.execute({}, get)