2006-06-30 16:42:12 -04:00
|
|
|
# Copyright (c) 2005 Zed A. Shaw
|
|
|
|
# You can redistribute it and/or modify it under the same terms as Ruby.
|
|
|
|
#
|
|
|
|
# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
|
|
|
|
# for more information.
|
2006-05-21 10:46:42 -04:00
|
|
|
|
2007-10-20 19:15:19 -04:00
|
|
|
require 'test/testhelp'
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2006-03-19 18:31:30 -05:00
|
|
|
include Mongrel
|
2006-01-28 14:03:53 -05:00
|
|
|
|
|
|
|
class HttpParserTest < Test::Unit::TestCase
|
|
|
|
|
2006-01-30 01:25:20 -05:00
|
|
|
def test_parse_simple
|
|
|
|
parser = HttpParser.new
|
|
|
|
req = {}
|
|
|
|
http = "GET / HTTP/1.1\r\n\r\n"
|
2006-05-14 19:42:19 -04:00
|
|
|
nread = parser.execute(req, http, 0)
|
2007-10-15 14:00:40 -04:00
|
|
|
|
2006-01-30 01:25:20 -05:00
|
|
|
assert nread == http.length, "Failed to parse the full HTTP request"
|
|
|
|
assert parser.finished?, "Parser didn't finish"
|
|
|
|
assert !parser.error?, "Parser had error"
|
|
|
|
assert nread == parser.nread, "Number read returned from execute does not match"
|
2007-10-15 14:00:40 -04:00
|
|
|
|
|
|
|
assert_equal 'HTTP/1.1', req['SERVER_PROTOCOL']
|
|
|
|
assert_equal '/', req['REQUEST_PATH']
|
|
|
|
assert_equal 'HTTP/1.1', req['HTTP_VERSION']
|
|
|
|
assert_equal '/', req['REQUEST_URI']
|
|
|
|
assert_equal 'CGI/1.2', req['GATEWAY_INTERFACE']
|
|
|
|
assert_equal 'GET', req['REQUEST_METHOD']
|
|
|
|
assert_nil req['FRAGMENT']
|
|
|
|
assert_nil req['QUERY_STRING']
|
|
|
|
|
2006-01-30 01:25:20 -05:00
|
|
|
parser.reset
|
|
|
|
assert parser.nread == 0, "Number read after reset should be 0"
|
|
|
|
end
|
2006-08-12 18:00:11 -04:00
|
|
|
|
|
|
|
def test_parse_dumbfuck_headers
|
|
|
|
parser = HttpParser.new
|
|
|
|
req = {}
|
|
|
|
should_be_good = "GET / HTTP/1.1\r\naaaaaaaaaaaaa:++++++++++\r\n\r\n"
|
|
|
|
nread = parser.execute(req, should_be_good, 0)
|
|
|
|
assert_equal should_be_good.length, nread
|
|
|
|
assert parser.finished?
|
|
|
|
assert !parser.error?
|
2006-10-24 18:52:21 -04:00
|
|
|
|
|
|
|
nasty_pound_header = "GET / HTTP/1.1\r\nX-SSL-Bullshit: -----BEGIN CERTIFICATE-----\r\n\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx\r\n\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT\r\n\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu\r\n\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV\r\n\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV\r\n\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB\r\n\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF\r\n\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR\r\n\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL\r\n\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP\r\n\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5j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eApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK\r\n\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu\r\n\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3\r\n\tRA==\r\n\t-----END CERTIFICATE-----\r\n\r\n"
|
|
|
|
parser = HttpParser.new
|
|
|
|
req = {}
|
|
|
|
#nread = parser.execute(req, nasty_pound_header, 0)
|
|
|
|
#assert_equal nasty_pound_header.length, nread
|
|
|
|
#assert parser.finished?
|
|
|
|
#assert !parser.error?
|
2006-08-12 18:00:11 -04:00
|
|
|
end
|
2006-01-30 01:25:20 -05:00
|
|
|
|
|
|
|
def test_parse_error
|
|
|
|
parser = HttpParser.new
|
|
|
|
req = {}
|
|
|
|
bad_http = "GET / SsUTF/1.1"
|
|
|
|
|
|
|
|
error = false
|
|
|
|
begin
|
2006-05-14 19:42:19 -04:00
|
|
|
nread = parser.execute(req, bad_http, 0)
|
2006-01-30 01:25:20 -05:00
|
|
|
rescue => details
|
|
|
|
error = true
|
2006-01-28 14:03:53 -05:00
|
|
|
end
|
2006-01-30 01:25:20 -05:00
|
|
|
|
|
|
|
assert error, "failed to throw exception"
|
|
|
|
assert !parser.finished?, "Parser shouldn't be finished"
|
|
|
|
assert parser.error?, "Parser SHOULD have error"
|
|
|
|
end
|
2006-03-19 18:31:30 -05:00
|
|
|
|
2007-10-15 14:00:40 -04:00
|
|
|
def test_fragment_in_uri
|
|
|
|
parser = HttpParser.new
|
|
|
|
req = {}
|
|
|
|
get = "GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1\r\n\r\n"
|
|
|
|
assert_nothing_raised do
|
|
|
|
parser.execute(req, get, 0)
|
|
|
|
end
|
|
|
|
assert parser.finished?
|
2007-10-18 16:45:16 -04:00
|
|
|
assert_equal '/forums/1/topics/2375?page=1', req['REQUEST_URI']
|
2007-10-15 14:00:40 -04:00
|
|
|
assert_equal 'posts-17408', req['FRAGMENT']
|
|
|
|
end
|
|
|
|
|
2006-04-02 22:27:59 -04:00
|
|
|
# lame random garbage maker
|
|
|
|
def rand_data(min, max, readable=true)
|
|
|
|
count = min + ((rand(max)+1) *10).to_i
|
|
|
|
res = count.to_s + "/"
|
|
|
|
|
|
|
|
if readable
|
2006-08-16 17:51:38 -04:00
|
|
|
res << Digest::SHA1.hexdigest(rand(count * 100).to_s) * (count / 40)
|
2006-04-02 22:27:59 -04:00
|
|
|
else
|
2006-08-16 17:51:38 -04:00
|
|
|
res << Digest::SHA1.digest(rand(count * 100).to_s) * (count / 20)
|
2006-04-02 22:27:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_horrible_queries
|
|
|
|
parser = HttpParser.new
|
|
|
|
|
|
|
|
# then that large header names are caught
|
2006-08-16 17:51:38 -04:00
|
|
|
10.times do |c|
|
2006-04-02 22:27:59 -04:00
|
|
|
get = "GET /#{rand_data(10,120)} HTTP/1.1\r\nX-#{rand_data(1024, 1024+(c*1024))}: Test\r\n\r\n"
|
|
|
|
assert_raises Mongrel::HttpParserError do
|
2006-05-14 19:42:19 -04:00
|
|
|
parser.execute({}, get, 0)
|
2006-04-02 22:27:59 -04:00
|
|
|
parser.reset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# then that large mangled field values are caught
|
2006-08-16 17:51:38 -04:00
|
|
|
10.times do |c|
|
2006-04-02 22:27:59 -04:00
|
|
|
get = "GET /#{rand_data(10,120)} HTTP/1.1\r\nX-Test: #{rand_data(1024, 1024+(c*1024), false)}\r\n\r\n"
|
|
|
|
assert_raises Mongrel::HttpParserError do
|
2006-05-14 19:42:19 -04:00
|
|
|
parser.execute({}, get, 0)
|
2006-04-02 22:27:59 -04:00
|
|
|
parser.reset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# then large headers are rejected too
|
|
|
|
get = "GET /#{rand_data(10,120)} HTTP/1.1\r\n"
|
|
|
|
get << "X-Test: test\r\n" * (80 * 1024)
|
|
|
|
assert_raises Mongrel::HttpParserError do
|
2006-05-14 19:42:19 -04:00
|
|
|
parser.execute({}, get, 0)
|
2006-04-02 22:27:59 -04:00
|
|
|
parser.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
# finally just that random garbage gets blocked all the time
|
|
|
|
10.times do |c|
|
|
|
|
get = "GET #{rand_data(1024, 1024+(c*1024), false)} #{rand_data(1024, 1024+(c*1024), false)}\r\n\r\n"
|
|
|
|
assert_raises Mongrel::HttpParserError do
|
2006-05-14 19:42:19 -04:00
|
|
|
parser.execute({}, get, 0)
|
2006-04-02 22:27:59 -04:00
|
|
|
parser.reset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-03-19 18:31:30 -05:00
|
|
|
def test_query_parse
|
2006-05-23 08:46:05 -04:00
|
|
|
res = HttpRequest.query_parse("zed=1&frank=#{HttpRequest.escape('&&& ')}")
|
2006-04-01 04:09:10 -05:00
|
|
|
assert res["zed"], "didn't get the request right"
|
|
|
|
assert res["frank"], "no frank"
|
|
|
|
assert_equal "1", res["zed"], "wrong result"
|
2006-05-23 08:46:05 -04:00
|
|
|
assert_equal "&&& ", HttpRequest.unescape(res["frank"]), "wrong result"
|
2006-04-01 04:09:10 -05:00
|
|
|
|
|
|
|
res = HttpRequest.query_parse("zed=1&zed=2&zed=3&frank=11;zed=45")
|
|
|
|
assert res["zed"], "didn't get the request right"
|
|
|
|
assert res["frank"], "no frank"
|
|
|
|
assert_equal 4,res["zed"].length, "wrong number for zed"
|
|
|
|
assert_equal "11",res["frank"], "wrong number for frank"
|
2006-03-19 18:31:30 -05:00
|
|
|
end
|
2006-04-02 22:27:59 -04:00
|
|
|
|
2006-01-28 14:03:53 -05:00
|
|
|
end
|
|
|
|
|