1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/webrick/httpproxy.rb (WEBrick::HTTPProxyServer#proxy_service):

should delete trailing LF from the result of pack("m*").

* lib/webrick/httpproxy.rb (WEBrick::HTTPProxyServer#proxy_connect):
  - should delete trailing LF from the result of pack("m*").
  - clear Request-Line not to send the sesponse by HTTPServer#run.

* lib/webrick/httputils (WEBrick::HTTPUtils.parse_qvalues):
  refine regexp (and change the name of a local variable).

* lib/webrick/server.rb (WEBrick::Daemon.start): prepared stdio
  don't allow changing its mode.

* test/webrick/*, sample/webrick/httpproxy.rb: add new files.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7743 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
gotoyuzo 2005-01-07 11:05:22 +00:00
parent fe1019c665
commit 36c839f233
14 changed files with 895 additions and 34 deletions

View file

@ -1,3 +1,20 @@
Fri Jan 7 20:01:31 2005 GOTOU Yuuzou <gotoyuzo@notwork.org>
* lib/webrick/httpproxy.rb (WEBrick::HTTPProxyServer#proxy_service):
should delete trailing LF from the result of pack("m*").
* lib/webrick/httpproxy.rb (WEBrick::HTTPProxyServer#proxy_connect):
- should delete trailing LF from the result of pack("m*").
- clear Request-Line not to send the sesponse by HTTPServer#run.
* lib/webrick/httputils (WEBrick::HTTPUtils.parse_qvalues):
refine regexp (and change the name of a local variable).
* lib/webrick/server.rb (WEBrick::Daemon.start): prepared stdio
don't allow changing its mode.
* test/webrick/*, sample/webrick/httpproxy.rb: add new files.
Fri Jan 7 18:03:35 2005 Tanaka Akira <akr@m17n.org>
* gc.c (mark_locations_array): avoid core dump with -O3.

View file

@ -15,6 +15,14 @@ require "net/http"
Net::HTTP::version_1_2 if RUBY_VERSION < "1.7"
module WEBrick
NullReader = Object.new
class << NullReader
def read(*args)
nil
end
alias gets read
end
class HTTPProxyServer < HTTPServer
def initialize(config)
super
@ -111,6 +119,7 @@ module WEBrick
proxy_port = proxy.port
if proxy.userinfo
credentials = "Basic " + [proxy.userinfo].pack("m*")
credentials.chomp!
header['proxy-authorization'] = credentials
end
end
@ -171,6 +180,7 @@ module WEBrick
proxy_request_line = "CONNECT #{host}:#{port} HTTP/1.0"
if proxy.userinfo
credentials = "Basic " + [proxy.userinfo].pack("m*")
credentials.chomp!
end
host, port = proxy.host, proxy.port
end
@ -211,6 +221,10 @@ module WEBrick
end
res.send_response(ua)
access_log(@config, req, res)
# Should clear request-line not to send the sesponse twice.
# see: HTTPServer#run
req.parse(NullReader) rescue nil
end
begin

View file

@ -179,14 +179,14 @@ module WEBrick
if value
parts = value.split(/,\s*/)
parts.each {|part|
if m = %r{^([^\s,]+?)(?:;\s*q=([\d]+(?:\.[\d]+)))?$}.match(part)
lang = m[1]
if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
val = m[1]
q = (m[2] or 1).to_f
tmp.push([lang, q])
tmp.push([val, q])
end
}
tmp = tmp.sort_by{|lang, q| -q}
tmp.collect!{|lang, q| lang}
tmp = tmp.sort_by{|val, q| -q}
tmp.collect!{|val, q| val}
end
return tmp
end

View file

@ -31,9 +31,9 @@ module WEBrick
exit!(0) if fork
Dir::chdir("/")
File::umask(0)
[ STDIN, STDOUT, STDERR ].each{|io|
io.reopen("/dev/null", "r+")
}
STDIN.reopen("/dev/null")
STDOUT.reopen("/dev/null", "w")
STDERR.reopen("/dev/null", "w")
yield if block_given?
end
end

View file

@ -0,0 +1,26 @@
require "webrick"
require "webrick/httpproxy"
# :ProxyContentHandler will be invoked before sending
# response to User-Agenge. You can inspect the pair of
# request and response messages (or can edit the response
# message if necessary).
pch = Proc.new{|req, res|
p [ req.request_line, res.status_line ]
}
def upstream_proxy
if prx = ENV["http_proxy"]
return URI.parse(prx)
end
return nil
end
httpd = WEBrick::HTTPProxyServer.new(
:Port => 10080,
:ProxyContentHandler => pch,
:ProxyURI => upstream_proxy
)
Signal.trap(:INT){ httpd.shutdown }
httpd.start

View file

@ -0,0 +1,56 @@
require "test/unit"
require "webrick/cookie"
class TestWEBrickCookie < Test::Unit::TestCase
def test_new
cookie = WEBrick::Cookie.new("foo","bar")
assert_equal("foo", cookie.name)
assert_equal("bar", cookie.value)
assert_equal("foo=bar", cookie.to_s)
end
def test_time
cookie = WEBrick::Cookie.new("foo","bar")
t = 1000000000
cookie.max_age = t
assert_match(t.to_s, cookie.to_s)
cookie = WEBrick::Cookie.new("foo","bar")
t = Time.at(1000000000)
cookie.expires = t
assert_equal(Time, cookie.expires.class)
assert_equal(t, cookie.expires)
ts = t.httpdate
cookie.expires = ts
assert_equal(Time, cookie.expires.class)
assert_equal(t, cookie.expires)
assert_match(ts, cookie.to_s)
end
def test_parse
data = ""
data << '$Version="1"; '
data << 'Customer="WILE_E_COYOTE"; $Path="/acme"; '
data << 'Part_Number="Rocket_Launcher_0001"; $Path="/acme"; '
data << 'Shipping="FedEx"; $Path="/acme"'
cookies = WEBrick::Cookie.parse(data)
assert_equal(1, cookies[0].version)
assert_equal("Customer", cookies[0].name)
assert_equal("WILE_E_COYOTE", cookies[0].value)
assert_equal("/acme", cookies[0].path)
assert_equal(1, cookies[1].version)
assert_equal("Part_Number", cookies[1].name)
assert_equal("Rocket_Launcher_0001", cookies[1].value)
assert_equal(1, cookies[2].version)
assert_equal("Shipping", cookies[2].name)
assert_equal("FedEx", cookies[2].value)
data = "hoge=moge; __div__session=9865ecfd514be7f7"
cookies = WEBrick::Cookie.parse(data)
assert_equal(0, cookies[0].version)
assert_equal("hoge", cookies[0].name)
assert_equal("moge", cookies[0].value)
assert_equal("__div__session", cookies[1].name)
assert_equal("9865ecfd514be7f7", cookies[1].value)
end
end

View file

@ -0,0 +1,65 @@
require "test/unit"
require "webrick"
require "stringio"
class WEBrick::TestFileHandler < Test::Unit::TestCase
def default_file_handler(filename)
klass = WEBrick::HTTPServlet::DefaultFileHandler
klass.new(WEBrick::Config::HTTP, filename)
end
def get_res_body(res)
return res.body.read rescue res.body
end
def make_range_request(range_spec)
msg = <<-_end_of_request_
GET / HTTP/1.0
Range: #{range_spec}
_end_of_request_
return StringIO.new(msg.gsub(/^ {6}/, ""))
end
def make_range_response(file, range_spec)
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(make_range_request(range_spec))
res = WEBrick::HTTPResponse.new(WEBrick::Config::HTTP)
size = File.size(file)
handler = default_file_handler(file)
handler.make_partial_content(req, res, file, size)
return res
end
def test_make_partial_content
filename = __FILE__
filesize = File.size(filename)
res = make_range_response(filename, "bytes=#{filesize-100}-")
assert_match(%r{^text/plain}, res["content-type"])
assert_equal(get_res_body(res).size, 100)
res = make_range_response(filename, "bytes=-100")
assert_match(%r{^text/plain}, res["content-type"])
assert_equal(get_res_body(res).size, 100)
res = make_range_response(filename, "bytes=0-99")
assert_match(%r{^text/plain}, res["content-type"])
assert_equal(get_res_body(res).size, 100)
res = make_range_response(filename, "bytes=100-199")
assert_match(%r{^text/plain}, res["content-type"])
assert_equal(get_res_body(res).size, 100)
res = make_range_response(filename, "bytes=0-0")
assert_match(%r{^text/plain}, res["content-type"])
assert_equal(get_res_body(res).size, 1)
res = make_range_response(filename, "bytes=-1")
assert_match(%r{^text/plain}, res["content-type"])
assert_equal(get_res_body(res).size, 1)
res = make_range_response(filename, "bytes=0-0, -2")
assert_match(%r{^multipart/byteranges}, res["content-type"])
end
end

View file

@ -3,34 +3,11 @@ require "net/http"
require "tempfile"
require "webrick"
require "webrick/httpauth/basicauth"
require File.join(File.dirname(__FILE__), "utils.rb")
class TestWEBrickHTTPAuth < Test::Unit::TestCase
class NullWriter
def NullWriter.<<(msg)
puts msg if $DEBUG
return self
end
end
def start_httpserver
server = WEBrick::HTTPServer.new(
:BindAddress => "0.0.0.0", :Port => 0,
:Logger => WEBrick::Log.new(NullWriter),
:AccessLog => [[NullWriter, ""]]
)
thread = nil
begin
thread = Thread.start{ server.start }
addr = server.listeners[0].addr
yield([server, addr[3], addr[1]])
ensure
server.stop
thread.join
end
end
def test_basic_auth
start_httpserver{|server, addr, port|
TestWEBrick.start_httpserver{|server, addr, port|
realm = "WEBrick's realm"
path = "/basic_auth"
@ -50,7 +27,7 @@ class TestWEBrickHTTPAuth < Test::Unit::TestCase
end
def test_basic_auth2
start_httpserver{|server, addr, port|
TestWEBrick.start_httpserver{|server, addr, port|
realm = "WEBrick's realm"
path = "/basic_auth2"

View file

@ -0,0 +1,214 @@
require "webrick"
require "stringio"
require "test/unit"
class TestWEBrickHTTPRequest < Test::Unit::TestCase
def test_parse_09
msg = <<-_end_of_message_
GET /
foobar # HTTP/0.9 request don't have header nor entity body.
_end_of_message_
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
assert_equal("GET", req.request_method)
assert_equal("/", req.unparsed_uri)
assert_equal(WEBrick::HTTPVersion.new("0.9"), req.http_version)
assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
assert_equal(80, req.port)
assert_equal(false, req.keep_alive?)
assert_equal(nil, req.body)
assert(req.query.empty?)
end
def test_parse_10
msg = <<-_end_of_message_
GET / HTTP/1.0
_end_of_message_
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
assert_equal("GET", req.request_method)
assert_equal("/", req.unparsed_uri)
assert_equal(WEBrick::HTTPVersion.new("1.0"), req.http_version)
assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
assert_equal(80, req.port)
assert_equal(false, req.keep_alive?)
assert_equal(nil, req.body)
assert(req.query.empty?)
end
def test_parse_11
msg = <<-_end_of_message_
GET /path HTTP/1.1
_end_of_message_
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
assert_equal("GET", req.request_method)
assert_equal("/path", req.unparsed_uri)
assert_equal("", req.script_name)
assert_equal("/path", req.path_info)
assert_equal(WEBrick::HTTPVersion.new("1.1"), req.http_version)
assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
assert_equal(80, req.port)
assert_equal(true, req.keep_alive?)
assert_equal(nil, req.body)
assert(req.query.empty?)
end
def test_parse_headers
msg = <<-_end_of_message_
GET /path HTTP/1.1
Host: test.ruby-lang.org:8080
Connection: close
Accept: text/*;q=0.3, text/html;q=0.7, text/html;level=1,
text/html;level=2;q=0.4, */*;q=0.5
Accept-Encoding: compress;q=0.5
Accept-Encoding: gzip;q=1.0, identity; q=0.4, *;q=0
Accept-Language: en;q=0.5, *; q=0
Accept-Language: ja
Content-Type: text/plain
Content-Length: 7
foobar
_end_of_message_
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
assert_equal(
URI.parse("http://test.ruby-lang.org:8080/path"), req.request_uri)
assert_equal("test.ruby-lang.org", req.host)
assert_equal(8080, req.port)
assert_equal(false, req.keep_alive?)
assert_equal(
%w(text/html;level=1 text/html */* text/html;level=2 text/*),
req.accept)
assert_equal(%w(gzip compress identity *), req.accept_encoding)
assert_equal(%w(ja en *), req.accept_language)
assert_equal(7, req.content_length)
assert_equal("text/plain", req.content_type)
assert_equal("foobar\n", req.body)
assert(req.query.empty?)
end
def test_parse_header2()
msg = <<-_end_of_message_
POST /foo/bar/../baz?q=a HTTP/1.0
Content-Length: 9
User-Agent:
FOO BAR
BAZ
hogehoge
_end_of_message_
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
assert_equal("POST", req.request_method)
assert_equal("/foo/baz", req.path)
assert_equal("", req.script_name)
assert_equal("/foo/baz", req.path_info)
assert_equal("9", req['content-length'])
assert_equal("FOO BAR BAZ", req['user-agent'])
assert_equal("hogehoge\n", req.body)
end
def test_parse_get_params
param = "foo=1;foo=2;foo=3;bar=x"
msg = <<-_end_of_message_
GET /path?#{param} HTTP/1.1
Host: test.ruby-lang.org:8080
_end_of_message_
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
query = req.query
assert_equal("1", query["foo"])
assert_equal(["1", "2", "3"], query["foo"].to_ary)
assert_equal(["1", "2", "3"], query["foo"].list)
assert_equal("x", query["bar"])
assert_equal(["x"], query["bar"].list)
end
def test_parse_post_params
param = "foo=1;foo=2;foo=3;bar=x"
msg = <<-_end_of_message_
POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
Host: test.ruby-lang.org:8080
Content-Length: #{param.size}
Content-Type: application/x-www-form-urlencoded
#{param}
_end_of_message_
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
query = req.query
assert_equal("1", query["foo"])
assert_equal(["1", "2", "3"], query["foo"].to_ary)
assert_equal(["1", "2", "3"], query["foo"].list)
assert_equal("x", query["bar"])
assert_equal(["x"], query["bar"].list)
end
def test_chunked
crlf = "\x0d\x0a"
msg = <<-_end_of_message_
POST /path HTTP/1.1
Host: test.ruby-lang.org:8080
Transfer-Encoding: chunked
_end_of_message_
msg.gsub!(/^ {6}/, "")
open(__FILE__){|io|
while chunk = io.read(100)
msg << chunk.size.to_s(16) << crlf
msg << chunk << crlf
end
}
msg << "0" << crlf
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg))
assert_equal(File.read(__FILE__), req.body)
end
def test_bad_messages
param = "foo=1;foo=2;foo=3;bar=x"
msg = <<-_end_of_message_
POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
Host: test.ruby-lang.org:8080
Content-Type: application/x-www-form-urlencoded
#{param}
_end_of_message_
assert_raises(WEBrick::HTTPStatus::LengthRequired){
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
req.body
}
msg = <<-_end_of_message_
POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
Host: test.ruby-lang.org:8080
Content-Length: 100000
body is too short.
_end_of_message_
assert_raises(WEBrick::HTTPStatus::BadRequest){
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
req.body
}
msg = <<-_end_of_message_
POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
Host: test.ruby-lang.org:8080
Transfer-Encoding: foobar
body is too short.
_end_of_message_
assert_raises(WEBrick::HTTPStatus::NotImplemented){
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
req.body
}
end
end

View file

@ -0,0 +1,260 @@
require "test/unit"
require "net/http"
require "webrick"
require File.join(File.dirname(__FILE__), "utils.rb")
class TestWEBrickHTTPServer < Test::Unit::TestCase
def test_mount
httpd = WEBrick::HTTPServer.new(
:Logger => WEBrick::Log.new(TestWEBrick::NullWriter),
:DoNotListen=>true
)
httpd.mount("/", :Root)
httpd.mount("/foo", :Foo)
httpd.mount("/foo/bar", :Bar, :bar1)
httpd.mount("/foo/bar/baz", :Baz, :baz1, :baz2)
serv, opts, script_name, path_info = httpd.search_servlet("/")
assert_equal(:Root, serv)
assert_equal([], opts)
assert_equal(script_name, "")
assert_equal(path_info, "/")
serv, opts, script_name, path_info = httpd.search_servlet("/sub")
assert_equal(:Root, serv)
assert_equal([], opts)
assert_equal(script_name, "")
assert_equal(path_info, "/sub")
serv, opts, script_name, path_info = httpd.search_servlet("/sub/")
assert_equal(:Root, serv)
assert_equal([], opts)
assert_equal(script_name, "")
assert_equal(path_info, "/sub/")
serv, opts, script_name, path_info = httpd.search_servlet("/foo")
assert_equal(:Foo, serv)
assert_equal([], opts)
assert_equal(script_name, "/foo")
assert_equal(path_info, "")
serv, opts, script_name, path_info = httpd.search_servlet("/foo/")
assert_equal(:Foo, serv)
assert_equal([], opts)
assert_equal(script_name, "/foo")
assert_equal(path_info, "/")
serv, opts, script_name, path_info = httpd.search_servlet("/foo/sub")
assert_equal(:Foo, serv)
assert_equal([], opts)
assert_equal(script_name, "/foo")
assert_equal(path_info, "/sub")
serv, opts, script_name, path_info = httpd.search_servlet("/foo/bar")
assert_equal(:Bar, serv)
assert_equal([:bar1], opts)
assert_equal(script_name, "/foo/bar")
assert_equal(path_info, "")
serv, opts, script_name, path_info = httpd.search_servlet("/foo/bar/baz")
assert_equal(:Baz, serv)
assert_equal([:baz1, :baz2], opts)
assert_equal(script_name, "/foo/bar/baz")
assert_equal(path_info, "")
end
class Req
attr_reader :port, :host
def initialize(addr, port, host)
@addr, @port, @host = addr, port, host
end
def addr
[0,0,0,@addr]
end
end
def httpd(addr, port, host, ali)
config ={
:Logger => WEBrick::Log.new(TestWEBrick::NullWriter),
:DoNotListen => true,
:BindAddress => addr,
:Port => port,
:ServerName => host,
:ServerAlias => ali,
}
return WEBrick::HTTPServer.new(config)
end
def assert_eql?(v1, v2)
assert_equal(v1.object_id, v2.object_id)
end
def test_lookup_server
addr1 = "192.168.100.1"
addr2 = "192.168.100.2"
addrz = "192.168.100.254"
local = "127.0.0.1"
port1 = 80
port2 = 8080
port3 = 10080
portz = 32767
name1 = "www.example.com"
name2 = "www2.example.com"
name3 = "www3.example.com"
namea = "www.example.co.jp"
nameb = "www.example.jp"
namec = "www2.example.co.jp"
named = "www2.example.jp"
namez = "foobar.example.com"
alias1 = [namea, nameb]
alias2 = [namec, named]
host1 = httpd(nil, port1, name1, nil)
hosts = [
host2 = httpd(addr1, port1, name1, nil),
host3 = httpd(addr1, port1, name2, alias1),
host4 = httpd(addr1, port2, name1, nil),
host5 = httpd(addr1, port2, name2, alias1),
host6 = httpd(addr1, port2, name3, alias2),
host7 = httpd(addr2, nil, name1, nil),
host8 = httpd(addr2, nil, name2, alias1),
host9 = httpd(addr2, nil, name3, alias2),
host10 = httpd(local, nil, nil, nil),
host11 = httpd(nil, port3, nil, nil),
].sort_by{ rand }
hosts.each{|h| host1.virtual_host(h) }
# connect to addr1
assert_eql?(host2, host1.lookup_server(Req.new(addr1, port1, name1)))
assert_eql?(host3, host1.lookup_server(Req.new(addr1, port1, name2)))
assert_eql?(host3, host1.lookup_server(Req.new(addr1, port1, namea)))
assert_eql?(host3, host1.lookup_server(Req.new(addr1, port1, nameb)))
assert_eql?(nil, host1.lookup_server(Req.new(addr1, port1, namez)))
assert_eql?(host4, host1.lookup_server(Req.new(addr1, port2, name1)))
assert_eql?(host5, host1.lookup_server(Req.new(addr1, port2, name2)))
assert_eql?(host5, host1.lookup_server(Req.new(addr1, port2, namea)))
assert_eql?(host5, host1.lookup_server(Req.new(addr1, port2, nameb)))
assert_eql?(nil, host1.lookup_server(Req.new(addr1, port2, namez)))
assert_eql?(host11, host1.lookup_server(Req.new(addr1, port3, name1)))
assert_eql?(host11, host1.lookup_server(Req.new(addr1, port3, name2)))
assert_eql?(host11, host1.lookup_server(Req.new(addr1, port3, namea)))
assert_eql?(host11, host1.lookup_server(Req.new(addr1, port3, nameb)))
assert_eql?(host11, host1.lookup_server(Req.new(addr1, port3, namez)))
assert_eql?(nil, host1.lookup_server(Req.new(addr1, portz, name1)))
assert_eql?(nil, host1.lookup_server(Req.new(addr1, portz, name2)))
assert_eql?(nil, host1.lookup_server(Req.new(addr1, portz, namea)))
assert_eql?(nil, host1.lookup_server(Req.new(addr1, portz, nameb)))
assert_eql?(nil, host1.lookup_server(Req.new(addr1, portz, namez)))
# connect to addr2
assert_eql?(host7, host1.lookup_server(Req.new(addr2, port1, name1)))
assert_eql?(host8, host1.lookup_server(Req.new(addr2, port1, name2)))
assert_eql?(host8, host1.lookup_server(Req.new(addr2, port1, namea)))
assert_eql?(host8, host1.lookup_server(Req.new(addr2, port1, nameb)))
assert_eql?(nil, host1.lookup_server(Req.new(addr2, port1, namez)))
assert_eql?(host7, host1.lookup_server(Req.new(addr2, port2, name1)))
assert_eql?(host8, host1.lookup_server(Req.new(addr2, port2, name2)))
assert_eql?(host8, host1.lookup_server(Req.new(addr2, port2, namea)))
assert_eql?(host8, host1.lookup_server(Req.new(addr2, port2, nameb)))
assert_eql?(nil, host1.lookup_server(Req.new(addr2, port2, namez)))
assert_eql?(host7, host1.lookup_server(Req.new(addr2, port3, name1)))
assert_eql?(host8, host1.lookup_server(Req.new(addr2, port3, name2)))
assert_eql?(host8, host1.lookup_server(Req.new(addr2, port3, namea)))
assert_eql?(host8, host1.lookup_server(Req.new(addr2, port3, nameb)))
assert_eql?(host11, host1.lookup_server(Req.new(addr2, port3, namez)))
assert_eql?(host7, host1.lookup_server(Req.new(addr2, portz, name1)))
assert_eql?(host8, host1.lookup_server(Req.new(addr2, portz, name2)))
assert_eql?(host8, host1.lookup_server(Req.new(addr2, portz, namea)))
assert_eql?(host8, host1.lookup_server(Req.new(addr2, portz, nameb)))
assert_eql?(nil, host1.lookup_server(Req.new(addr2, portz, namez)))
# connect to addrz
assert_eql?(nil, host1.lookup_server(Req.new(addrz, port1, name1)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, port1, name2)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, port1, namea)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, port1, nameb)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, port1, namez)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, port2, name1)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, port2, name2)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, port2, namea)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, port2, nameb)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, port2, namez)))
assert_eql?(host11, host1.lookup_server(Req.new(addrz, port3, name1)))
assert_eql?(host11, host1.lookup_server(Req.new(addrz, port3, name2)))
assert_eql?(host11, host1.lookup_server(Req.new(addrz, port3, namea)))
assert_eql?(host11, host1.lookup_server(Req.new(addrz, port3, nameb)))
assert_eql?(host11, host1.lookup_server(Req.new(addrz, port3, namez)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, portz, name1)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, portz, name2)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, portz, namea)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, portz, nameb)))
assert_eql?(nil, host1.lookup_server(Req.new(addrz, portz, namez)))
# connect to localhost
assert_eql?(host10, host1.lookup_server(Req.new(local, port1, name1)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port1, name2)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port1, namea)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port1, nameb)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port1, namez)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port2, name1)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port2, name2)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port2, namea)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port2, nameb)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port2, namez)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port3, name1)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port3, name2)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port3, namea)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port3, nameb)))
assert_eql?(host10, host1.lookup_server(Req.new(local, port3, namez)))
assert_eql?(host10, host1.lookup_server(Req.new(local, portz, name1)))
assert_eql?(host10, host1.lookup_server(Req.new(local, portz, name2)))
assert_eql?(host10, host1.lookup_server(Req.new(local, portz, namea)))
assert_eql?(host10, host1.lookup_server(Req.new(local, portz, nameb)))
assert_eql?(host10, host1.lookup_server(Req.new(local, portz, namez)))
end
def test_callbacks
accepted = started = stopped = 0
requested0 = requested1 = 0
config = {
:ServerName => "localhost",
:AcceptCallback => Proc.new{ accepted += 1 },
:StartCallback => Proc.new{ started += 1 },
:StopCallback => Proc.new{ stopped += 1 },
:RequestCallback => Proc.new{|req, res| requested0 += 1 },
}
TestWEBrick.start_httpserver(config){|server, addr, port|
vhost_config = {
:ServerName => "myhostname",
:BindAddress => addr,
:Port => port,
:DoNotListen => true,
:Logger => WEBrick::Log.new(TestWEBrick::NullWriter),
:AccessLog => [],
:RequestCallback => Proc.new{|req, res| requested1 += 1 },
}
server.virtual_host(WEBrick::HTTPServer.new(vhost_config))
true while server.status != :Running
assert_equal(started, 1)
assert_equal(stopped, 0)
assert_equal(accepted, 0)
http = Net::HTTP.new(addr, port)
req = Net::HTTP::Get.new("/")
req["Host"] = "myhostname:#{port}"
http.request(req){|res| assert_equal("404", res.code)}
http.request(req){|res| assert_equal("404", res.code)}
http.request(req){|res| assert_equal("404", res.code)}
req["Host"] = "localhost:#{port}"
http.request(req){|res| assert_equal("404", res.code)}
http.request(req){|res| assert_equal("404", res.code)}
http.request(req){|res| assert_equal("404", res.code)}
assert_equal(6, accepted)
assert_equal(3, requested0)
assert_equal(3, requested1)
}
assert_equal(started, 1)
assert_equal(stopped, 1)
end
end

View file

@ -0,0 +1,90 @@
require "test/unit"
require "webrick/httputils"
class TestWEBrickHTTPUtils < Test::Unit::TestCase
include WEBrick::HTTPUtils
def test_normilize_path
assert_equal("/foo", normalize_path("/foo"))
assert_equal("/foo/bar/", normalize_path("/foo/bar/"))
assert_equal("/", normalize_path("/foo/../"))
assert_equal("/", normalize_path("/foo/.."))
assert_equal("/", normalize_path("/foo/bar/../../"))
assert_equal("/", normalize_path("/foo/bar/../.."))
assert_equal("/", normalize_path("/foo/bar/../.."))
assert_equal("/baz", normalize_path("/foo/bar/../../baz"))
assert_equal("/baz", normalize_path("/foo/../bar/../baz"))
assert_equal("/baz/", normalize_path("/foo/../bar/../baz/"))
assert_equal("/...", normalize_path("/bar/../..."))
assert_equal("/.../", normalize_path("/bar/../.../"))
assert_equal("/foo/", normalize_path("/foo/./"))
assert_equal("/foo/", normalize_path("/foo/."))
assert_equal("/foo/", normalize_path("/foo/././"))
assert_equal("/foo/", normalize_path("/foo/./."))
assert_equal("/foo/bar", normalize_path("/foo/./bar"))
assert_equal("/foo/bar/", normalize_path("/foo/./bar/."))
assert_equal("/foo/bar/", normalize_path("/./././foo/./bar/."))
assert_equal("/foo/bar/", normalize_path("//foo///.//bar/.///.//"))
assert_equal("/", normalize_path("//foo///..///bar/.///..//.//"))
assert_raises(RuntimeError){ normalize_path("foo/bar") }
assert_raises(RuntimeError){ normalize_path("..") }
assert_raises(RuntimeError){ normalize_path("/..") }
assert_raises(RuntimeError){ normalize_path("/./..") }
assert_raises(RuntimeError){ normalize_path("/./../") }
assert_raises(RuntimeError){ normalize_path("/./../..") }
assert_raises(RuntimeError){ normalize_path("/./../../") }
assert_raises(RuntimeError){ normalize_path("/./../") }
assert_raises(RuntimeError){ normalize_path("/../..") }
assert_raises(RuntimeError){ normalize_path("/../../") }
assert_raises(RuntimeError){ normalize_path("/../../..") }
assert_raises(RuntimeError){ normalize_path("/../../../") }
assert_raises(RuntimeError){ normalize_path("/../foo/../") }
assert_raises(RuntimeError){ normalize_path("/../foo/../../") }
assert_raises(RuntimeError){ normalize_path("/foo/bar/../../../../") }
assert_raises(RuntimeError){ normalize_path("/foo/../bar/../../") }
assert_raises(RuntimeError){ normalize_path("/./../bar/") }
assert_raises(RuntimeError){ normalize_path("/./../") }
end
def test_split_header_value
assert_equal(['foo', 'bar'], split_header_value('foo, bar'))
assert_equal(['"foo"', 'bar'], split_header_value('"foo", bar'))
assert_equal(['foo', '"bar"'], split_header_value('foo, "bar"'))
assert_equal(['*'], split_header_value('*'))
assert_equal(['W/"xyzzy"', 'W/"r2d2xxxx"', 'W/"c3piozzzz"'],
split_header_value('W/"xyzzy", W/"r2d2xxxx", W/"c3piozzzz"'))
end
def test_escape
assert_equal("/foo/bar", escape("/foo/bar"))
assert_equal("/~foo/bar", escape("/~foo/bar"))
assert_equal("/~foo%20bar", escape("/~foo bar"))
assert_equal("/~foo%20bar", escape("/~foo bar"))
assert_equal("/~foo%09bar", escape("/~foo\tbar"))
assert_equal("/~foo+bar", escape("/~foo+bar"))
end
def test_escape_form
assert_equal("%2Ffoo%2Fbar", escape_form("/foo/bar"))
assert_equal("%2F~foo%2Fbar", escape_form("/~foo/bar"))
assert_equal("%2F~foo+bar", escape_form("/~foo bar"))
assert_equal("%2F~foo+%2B+bar", escape_form("/~foo + bar"))
end
def test_unescape
assert_equal("/foo/bar", unescape("%2ffoo%2fbar"))
assert_equal("/~foo/bar", unescape("/%7efoo/bar"))
assert_equal("/~foo/bar", unescape("%2f%7efoo%2fbar"))
assert_equal("/~foo+bar", unescape("/%7efoo+bar"))
end
def test_unescape_form
assert_equal("//foo/bar", unescape_form("/%2Ffoo/bar"))
assert_equal("//foo/bar baz", unescape_form("/%2Ffoo/bar+baz"))
assert_equal("/~foo/bar baz", unescape_form("/%7Efoo/bar+baz"))
end
end

View file

@ -0,0 +1,40 @@
require "test/unit"
require "webrick/httpversion"
class TestWEBrickHTTPVersion < Test::Unit::TestCase
def setup
@v09 = WEBrick::HTTPVersion.new("0.9")
@v10 = WEBrick::HTTPVersion.new("1.0")
@v11 = WEBrick::HTTPVersion.new("1.001")
end
def test_to_s()
assert_equal("0.9", @v09.to_s)
assert_equal("1.0", @v10.to_s)
assert_equal("1.1", @v11.to_s)
end
def test_major()
assert_equal(0, @v09.major)
assert_equal(1, @v10.major)
assert_equal(1, @v11.major)
end
def test_minor()
assert_equal(9, @v09.minor)
assert_equal(0, @v10.minor)
assert_equal(1, @v11.minor)
end
def test_compar()
assert_equal(0, @v09 <=> "0.9")
assert_equal(0, @v09 <=> "0.09")
assert_equal(-1, @v09 <=> @v10)
assert_equal(-1, @v09 <=> "1.00")
assert_equal(1, @v11 <=> @v09)
assert_equal(1, @v11 <=> "1.0")
assert_equal(1, @v11 <=> "0.9")
end
end

View file

@ -0,0 +1,66 @@
require "test/unit"
require "tempfile"
require "webrick"
require File.join(File.dirname(__FILE__), "utils.rb")
class TestWEBrickServer < Test::Unit::TestCase
class Echo < WEBrick::GenericServer
def run(sock)
while line = sock.gets
sock << line
end
end
end
def test_server
TestWEBrick.start_server(Echo){|server, addr, port|
TCPSocket.open(addr, port){|sock|
sock.puts("foo"); assert_equal("foo\n", sock.gets)
sock.puts("bar"); assert_equal("bar\n", sock.gets)
sock.puts("baz"); assert_equal("baz\n", sock.gets)
sock.puts("qux"); assert_equal("qux\n", sock.gets)
}
}
end
def test_callbacks
accepted = started = stopped = 0
config = {
:AcceptCallback => Proc.new{ accepted += 1 },
:StartCallback => Proc.new{ started += 1 },
:StopCallback => Proc.new{ stopped += 1 },
}
TestWEBrick.start_server(Echo, config){|server, addr, port|
true while server.status != :Running
assert_equal(started, 1)
assert_equal(stopped, 0)
assert_equal(accepted, 0)
TCPSocket.open(addr, port){|sock| (sock << "foo\n").gets }
TCPSocket.open(addr, port){|sock| (sock << "foo\n").gets }
TCPSocket.open(addr, port){|sock| (sock << "foo\n").gets }
assert_equal(accepted, 3)
}
assert_equal(started, 1)
assert_equal(stopped, 1)
end
def test_daemon
begin
r, w = IO.pipe
Process.fork{
r.close
WEBrick::Daemon.start
w.puts(Process.ppid)
w.puts(Process.pid)
sleep
}
assert_equal(1, r.gets.to_i)
assert(Process.kill(:KILL, r.gets.to_i))
rescue NotImplementedError
# snip this test
ensure
r.close
w.close
end
end
end

36
test/webrick/utils.rb Normal file
View file

@ -0,0 +1,36 @@
require "webrick"
require "webrick/httpproxy"
module TestWEBrick
NullWriter = Object.new
def NullWriter.<<(msg)
puts msg if $DEBUG
return self
end
module_function
def start_server(klass, config={}, &block)
server = klass.new({
:BindAddress => "127.0.0.1", :Port => 0,
:Logger => WEBrick::Log.new(NullWriter),
:AccessLog => [[NullWriter, ""]]
}.update(config))
begin
thread = Thread.start{ server.start }
addr = server.listeners[0].addr
block.call([server, addr[3], addr[1]])
ensure
server.stop
thread.join
end
end
def start_httpserver(config={}, &block)
start_server(WEBrick::HTTPServer, config, &block)
end
def start_httpproxy(config={}, &block)
start_server(WEBrick::HTTPProxyServer, config, &block)
end
end