From b04f5e661f98474b4779cb627a956a0ec02b2e9b Mon Sep 17 00:00:00 2001 From: gotoyuzo Date: Wed, 9 Jan 2008 11:37:03 +0000 Subject: * lib/webrick/httprequest.rb: supprt X-Forwarded-* header fields. WEBrick::HTTPRequest#{host,port,request_uri} is derived having regards to X-Forwarded-Proto and X-Forwarded-Host. * lib/webrick/httprequest.rb (WEBrick::HTTPRequest#server_name?): new method. (WEBrick::HTTPRequest#remote_ip?): new method. (WEBrick::HTTPRequest#ssl?): new method. * string.c (rb_enc_cr_str_buf_cat): fix self appending. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14968 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/webrick/test_httprequest.rb | 67 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'test/webrick') diff --git a/test/webrick/test_httprequest.rb b/test/webrick/test_httprequest.rb index f2fd887873..f49bd59384 100644 --- a/test/webrick/test_httprequest.rb +++ b/test/webrick/test_httprequest.rb @@ -79,6 +79,7 @@ class TestWEBrickHTTPRequest < Test::Unit::TestCase Accept-Language: ja Content-Type: text/plain Content-Length: 7 + X-Empty-Header: foobar _end_of_message_ @@ -97,6 +98,8 @@ class TestWEBrickHTTPRequest < Test::Unit::TestCase assert_equal(7, req.content_length) assert_equal("text/plain", req.content_type) assert_equal("foobar\n", req.body) + assert_equal("", req["x-empty-header"]) + assert_equal(nil, req["x-no-header"]) assert(req.query.empty?) end @@ -238,6 +241,70 @@ class TestWEBrickHTTPRequest < Test::Unit::TestCase assert_equal(File.read(__FILE__), req.body) end + def test_forwarded + msg = <<-_end_of_message_ + GET /foo HTTP/1.1 + Host: localhost:10080 + User-Agent: w3m/0.5.2 + X-Forwarded-For: 123.123.123.123 + X-Forwarded-Host: forward.example.com + X-Forwarded-Server: server.example.com + Connection: Keep-Alive + + _end_of_message_ + msg.gsub!(/^ {6}/, "") + req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP) + req.parse(StringIO.new(msg)) + assert_equal("server.example.com", req.server_name) + assert_equal("http://forward.example.com/foo", req.request_uri.to_s) + assert_equal("forward.example.com", req.host) + assert_equal(80, req.port) + assert_equal("123.123.123.123", req.remote_ip) + assert(!req.ssl?) + + msg = <<-_end_of_message_ + GET /foo HTTP/1.1 + Host: localhost:10080 + User-Agent: w3m/0.5.2 + X-Forwarded-For: 192.168.1.10, 172.16.1.1, 123.123.123.123 + X-Forwarded-Host: forward.example.com:8080 + X-Forwarded-Server: server.example.com + Connection: Keep-Alive + + _end_of_message_ + msg.gsub!(/^ {6}/, "") + req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP) + req.parse(StringIO.new(msg)) + assert_equal("server.example.com", req.server_name) + assert_equal("http://forward.example.com:8080/foo", req.request_uri.to_s) + assert_equal("forward.example.com", req.host) + assert_equal(8080, req.port) + assert_equal("123.123.123.123", req.remote_ip) + assert(!req.ssl?) + + msg = <<-_end_of_message_ + GET /foo HTTP/1.1 + Host: localhost:10080 + Client-IP: 234.234.234.234 + X-Forwarded-Proto: https + X-Forwarded-For: 192.168.1.10, 10.0.0.1, 123.123.123.123 + X-Forwarded-Host: forward.example.com + X-Forwarded-Server: server.example.com + X-Requested-With: XMLHttpRequest + Connection: Keep-Alive + + _end_of_message_ + msg.gsub!(/^ {6}/, "") + req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP) + req.parse(StringIO.new(msg)) + assert_equal("server.example.com", req.server_name) + assert_equal("https://forward.example.com/foo", req.request_uri.to_s) + assert_equal("forward.example.com", req.host) + assert_equal(443, req.port) + assert_equal("234.234.234.234", req.remote_ip) + assert(req.ssl?) + end + def test_bad_messages param = "foo=1;foo=2;foo=3;bar=x" msg = <<-_end_of_message_ -- cgit v1.2.3