mongrel 1.2.0.pre2-x86-mswin32
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +55 -0
- data/History.txt +68 -0
- data/LICENSE +55 -0
- data/Manifest.txt +69 -0
- data/README.txt +80 -0
- data/Rakefile +8 -0
- data/TODO +5 -0
- data/bin/mongrel_rails +284 -0
- data/examples/builder.rb +29 -0
- data/examples/camping/README +3 -0
- data/examples/camping/blog.rb +294 -0
- data/examples/camping/tepee.rb +149 -0
- data/examples/httpd.conf +474 -0
- data/examples/mime.yaml +3 -0
- data/examples/mongrel.conf +9 -0
- data/examples/monitrc +57 -0
- data/examples/random_thrash.rb +19 -0
- data/examples/simpletest.rb +52 -0
- data/examples/webrick_compare.rb +20 -0
- data/ext/http11/Http11Service.java +13 -0
- data/ext/http11/ext_help.h +15 -0
- data/ext/http11/extconf.rb +6 -0
- data/ext/http11/http11.c +534 -0
- data/ext/http11/http11_parser.c +1243 -0
- data/ext/http11/http11_parser.h +49 -0
- data/ext/http11/http11_parser.java.rl +159 -0
- data/ext/http11/http11_parser.rl +153 -0
- data/ext/http11/http11_parser_common.rl +54 -0
- data/ext/http11/org/jruby/mongrel/Http11.java +241 -0
- data/ext/http11/org/jruby/mongrel/Http11Parser.java +486 -0
- data/lib/1.8/http11.so +0 -0
- data/lib/1.9/http11.so +0 -0
- data/lib/mongrel.rb +366 -0
- data/lib/mongrel/camping.rb +107 -0
- data/lib/mongrel/cgi.rb +181 -0
- data/lib/mongrel/command.rb +220 -0
- data/lib/mongrel/configurator.rb +388 -0
- data/lib/mongrel/const.rb +110 -0
- data/lib/mongrel/debug.rb +203 -0
- data/lib/mongrel/gems.rb +22 -0
- data/lib/mongrel/handlers.rb +468 -0
- data/lib/mongrel/header_out.rb +28 -0
- data/lib/mongrel/http_request.rb +155 -0
- data/lib/mongrel/http_response.rb +166 -0
- data/lib/mongrel/init.rb +10 -0
- data/lib/mongrel/mime_types.yml +616 -0
- data/lib/mongrel/rails.rb +185 -0
- data/lib/mongrel/stats.rb +89 -0
- data/lib/mongrel/tcphack.rb +18 -0
- data/lib/mongrel/uri_classifier.rb +76 -0
- data/setup.rb +1585 -0
- data/tasks/gem.rake +28 -0
- data/tasks/native.rake +24 -0
- data/tasks/ragel.rake +20 -0
- data/test/mime.yaml +3 -0
- data/test/mongrel.conf +1 -0
- data/test/test_cgi_wrapper.rb +26 -0
- data/test/test_command.rb +86 -0
- data/test/test_conditional.rb +107 -0
- data/test/test_configurator.rb +87 -0
- data/test/test_debug.rb +25 -0
- data/test/test_handlers.rb +135 -0
- data/test/test_http11.rb +156 -0
- data/test/test_redirect_handler.rb +44 -0
- data/test/test_request_progress.rb +99 -0
- data/test/test_response.rb +127 -0
- data/test/test_stats.rb +35 -0
- data/test/test_uriclassifier.rb +261 -0
- data/test/test_ws.rb +117 -0
- data/test/testhelp.rb +71 -0
- data/tools/trickletest.rb +45 -0
- metadata +197 -0
data/tasks/gem.rake
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'hoe'
|
2
|
+
|
3
|
+
HOE = Hoe.spec 'mongrel' do
|
4
|
+
self.rubyforge_name = 'mongrel'
|
5
|
+
developer 'Zed A. Shaw', 'mongrel-users@rubyforge.org'
|
6
|
+
|
7
|
+
spec_extras[:required_ruby_version] = Gem::Requirement.new('>= 1.8.6')
|
8
|
+
|
9
|
+
spec_extras[:extensions] = ["ext/http11/extconf.rb"]
|
10
|
+
spec_extras[:executables] = ['mongrel_rails']
|
11
|
+
|
12
|
+
extra_rdoc_files << 'LICENSE'
|
13
|
+
|
14
|
+
extra_deps << ['gem_plugin', '~> 0.2.3']
|
15
|
+
extra_deps << ['daemons', '~> 1.0.10']
|
16
|
+
|
17
|
+
extra_dev_deps << ['rake-compiler', "~> 0.7.0"]
|
18
|
+
|
19
|
+
clean_globs.push('test_*.log', 'log')
|
20
|
+
end
|
21
|
+
|
22
|
+
file "#{HOE.spec.name}.gemspec" => ['Rakefile', 'tasks/gem.rake'] do |t|
|
23
|
+
puts "Generating #{t.name}"
|
24
|
+
File.open(t.name, 'w') { |f| f.puts HOE.spec.to_yaml }
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "Generate or update the standalone gemspec file for the project"
|
28
|
+
task :gemspec => ["#{HOE.spec.name}.gemspec"]
|
data/tasks/native.rake
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# use rake-compiler for building the extension
|
2
|
+
require 'rake/extensiontask'
|
3
|
+
|
4
|
+
# build http11 C extension
|
5
|
+
Rake::ExtensionTask.new('http11', HOE.spec) do |ext|
|
6
|
+
# define target for extension (supporting fat binaries)
|
7
|
+
if RUBY_PLATFORM =~ /mingw|mswin/ then
|
8
|
+
RUBY_VERSION =~ /(\d+\.\d+)/
|
9
|
+
ext.lib_dir = "lib/#{$1}"
|
10
|
+
end
|
11
|
+
|
12
|
+
# define cross-compilation tasks when not on Windows.
|
13
|
+
unless RUBY_PLATFORM =~ /mingw|mswin/ then
|
14
|
+
ext.cross_compile = true
|
15
|
+
ext.cross_platform = ['i386-mswin32', 'i386-mingw32']
|
16
|
+
|
17
|
+
ext.cross_compiling do |gs|
|
18
|
+
gs.dependencies.delete gs.dependencies.find { |d| d.name == 'daemons' }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# ensure things are built prior testing
|
24
|
+
task :test => [:compile]
|
data/tasks/ragel.rake
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
# the following tasks ease the build of C file from Ragel one
|
3
|
+
|
4
|
+
file 'ext/http11/http11_parser.c' => ['ext/http11/http11_parser.rl'] do |t|
|
5
|
+
begin
|
6
|
+
sh "ragel #{t.prerequisites.last} -C -G2 -o #{t.name}"
|
7
|
+
rescue
|
8
|
+
fail "Could not build wrapper using Ragel (it failed or not installed?)"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
file 'ext/http11/org/jruby/mongrel/Http11Parser.java' => ['ext/http11/http11_parser.java.rl'] do |t|
|
13
|
+
begin
|
14
|
+
sh "ragel #{t.prerequisites.last} -J -G2 -o #{t.name}"
|
15
|
+
rescue
|
16
|
+
fail "Could not build wrapper using Ragel (it failed or not installed?)"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
task :ragel => (defined?(JRUBY_VERSION) ? 'ext/http11/org/jruby/mongrel/Http11Parser.java' : 'ext/http11/http11_parser.c')
|
data/test/mime.yaml
ADDED
data/test/mongrel.conf
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
uri "/fromconf", :handler => Mongrel::Error404Handler.new("test")
|
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
require 'test/testhelp'
|
3
|
+
|
4
|
+
class MockHttpRequest
|
5
|
+
attr_reader :body
|
6
|
+
|
7
|
+
def params
|
8
|
+
return { 'REQUEST_METHOD' => 'GET'}
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class CGIWrapperTest < Test::Unit::TestCase
|
13
|
+
|
14
|
+
def test_set_cookies_output_cookies
|
15
|
+
request = MockHttpRequest.new
|
16
|
+
response = nil # not needed for this test
|
17
|
+
output_headers = {}
|
18
|
+
|
19
|
+
cgi = Mongrel::CGIWrapper.new(request, response)
|
20
|
+
session = CGI::Session.new(cgi, 'database_manager' => CGI::Session::MemoryStore)
|
21
|
+
cgi.send_cookies(output_headers)
|
22
|
+
|
23
|
+
assert(output_headers.has_key?("Set-Cookie"))
|
24
|
+
assert_equal("_session_id="+session.session_id+"; path=", output_headers["Set-Cookie"])
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# Copyright (c) 2005 Zed A. Shaw
|
2
|
+
# You can redistribute it and/or modify it under the same terms as Ruby.
|
3
|
+
#
|
4
|
+
# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
|
5
|
+
# for more information.
|
6
|
+
|
7
|
+
require 'test/testhelp'
|
8
|
+
|
9
|
+
class TestCommand < GemPlugin::Plugin "/commands"
|
10
|
+
include Mongrel::Command::Base
|
11
|
+
|
12
|
+
def configure
|
13
|
+
options [
|
14
|
+
["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"],
|
15
|
+
['', '--user USER', "User to run as", :@user, nil],
|
16
|
+
["-d", "--daemonize", "Whether to run in the background or not", :@daemon, false],
|
17
|
+
["-x", "--test", "Used to let the test run failures", :@test, false],
|
18
|
+
]
|
19
|
+
end
|
20
|
+
|
21
|
+
def validate
|
22
|
+
valid_dir? ".", "Can't validate current directory."
|
23
|
+
valid_exists? "Rakefile", "Rakefile not there, test is invalid."
|
24
|
+
if @test
|
25
|
+
valid_exist? "BADFILE", "Yeah, badfile"
|
26
|
+
valid_file? "BADFILE", "Not even a file"
|
27
|
+
valid_dir? "BADDIR", "No dir here"
|
28
|
+
valid? false, "Total failure"
|
29
|
+
end
|
30
|
+
|
31
|
+
return @valid
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
def run
|
36
|
+
$test_command_ran = true
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class CommandTest < Test::Unit::TestCase
|
41
|
+
|
42
|
+
def setup
|
43
|
+
$test_command_ran = false
|
44
|
+
end
|
45
|
+
|
46
|
+
def teardown
|
47
|
+
end
|
48
|
+
|
49
|
+
def run_cmd(args)
|
50
|
+
Mongrel::Command::Registry.instance.run args
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_run_command
|
54
|
+
redirect_test_io do
|
55
|
+
run_cmd ["testcommand"]
|
56
|
+
assert $test_command_ran, "command didn't run"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_command_error
|
61
|
+
redirect_test_io do
|
62
|
+
run_cmd ["crapcommand"]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_command_listing
|
67
|
+
redirect_test_io do
|
68
|
+
run_cmd ["help"]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_options
|
73
|
+
redirect_test_io do
|
74
|
+
run_cmd ["testcommand","-h"]
|
75
|
+
run_cmd ["testcommand","--help"]
|
76
|
+
run_cmd ["testcommand","-e","test","-d","--user"]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_version
|
81
|
+
redirect_test_io do
|
82
|
+
run_cmd ["testcommand", "--version"]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# Copyright (c) 2005 Zed A. Shaw
|
2
|
+
# You can redistribute it and/or modify it under the same terms as Ruby.
|
3
|
+
#
|
4
|
+
# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
|
5
|
+
# for more information.
|
6
|
+
|
7
|
+
require 'test/testhelp'
|
8
|
+
|
9
|
+
include Mongrel
|
10
|
+
|
11
|
+
class ConditionalResponseTest < Test::Unit::TestCase
|
12
|
+
def setup
|
13
|
+
@server = HttpServer.new('127.0.0.1', 3501)
|
14
|
+
@server.register('/', Mongrel::DirHandler.new('.'))
|
15
|
+
@server.run
|
16
|
+
|
17
|
+
@http = Net::HTTP.new(@server.host, @server.port)
|
18
|
+
|
19
|
+
# get the ETag and Last-Modified headers
|
20
|
+
@path = '/README.txt'
|
21
|
+
res = @http.start { |http| http.get(@path) }
|
22
|
+
assert_not_nil @etag = res['ETag']
|
23
|
+
assert_not_nil @last_modified = res['Last-Modified']
|
24
|
+
assert_not_nil @content_length = res['Content-Length']
|
25
|
+
end
|
26
|
+
|
27
|
+
def teardown
|
28
|
+
@server.stop(true)
|
29
|
+
end
|
30
|
+
|
31
|
+
# status should be 304 Not Modified when If-None-Match is the matching ETag
|
32
|
+
def test_not_modified_via_if_none_match
|
33
|
+
assert_status_for_get_and_head Net::HTTPNotModified, 'If-None-Match' => @etag
|
34
|
+
end
|
35
|
+
|
36
|
+
# status should be 304 Not Modified when If-Modified-Since is the matching Last-Modified date
|
37
|
+
def test_not_modified_via_if_modified_since
|
38
|
+
assert_status_for_get_and_head Net::HTTPNotModified, 'If-Modified-Since' => @last_modified
|
39
|
+
end
|
40
|
+
|
41
|
+
# status should be 304 Not Modified when If-None-Match is the matching ETag
|
42
|
+
# and If-Modified-Since is the matching Last-Modified date
|
43
|
+
def test_not_modified_via_if_none_match_and_if_modified_since
|
44
|
+
assert_status_for_get_and_head Net::HTTPNotModified, 'If-None-Match' => @etag, 'If-Modified-Since' => @last_modified
|
45
|
+
end
|
46
|
+
|
47
|
+
# status should be 200 OK when If-None-Match is invalid
|
48
|
+
def test_invalid_if_none_match
|
49
|
+
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => 'invalid'
|
50
|
+
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => 'invalid', 'If-Modified-Since' => @last_modified
|
51
|
+
end
|
52
|
+
|
53
|
+
# status should be 200 OK when If-Modified-Since is invalid
|
54
|
+
def test_invalid_if_modified_since
|
55
|
+
assert_status_for_get_and_head Net::HTTPOK, 'If-Modified-Since' => 'invalid'
|
56
|
+
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => @etag, 'If-Modified-Since' => 'invalid'
|
57
|
+
end
|
58
|
+
|
59
|
+
# status should be 304 Not Modified when If-Modified-Since is greater than the Last-Modified header, but less than the system time
|
60
|
+
def test_if_modified_since_greater_than_last_modified
|
61
|
+
sleep 2
|
62
|
+
last_modified_plus_1 = (Time.httpdate(@last_modified) + 1).httpdate
|
63
|
+
assert_status_for_get_and_head Net::HTTPNotModified, 'If-Modified-Since' => last_modified_plus_1
|
64
|
+
assert_status_for_get_and_head Net::HTTPNotModified, 'If-None-Match' => @etag, 'If-Modified-Since' => last_modified_plus_1
|
65
|
+
end
|
66
|
+
|
67
|
+
# status should be 200 OK when If-Modified-Since is less than the Last-Modified header
|
68
|
+
def test_if_modified_since_less_than_last_modified
|
69
|
+
last_modified_minus_1 = (Time.httpdate(@last_modified) - 1).httpdate
|
70
|
+
assert_status_for_get_and_head Net::HTTPOK, 'If-Modified-Since' => last_modified_minus_1
|
71
|
+
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => @etag, 'If-Modified-Since' => last_modified_minus_1
|
72
|
+
end
|
73
|
+
|
74
|
+
# status should be 200 OK when If-Modified-Since is a date in the future
|
75
|
+
def test_future_if_modified_since
|
76
|
+
the_future = Time.at(2**31-1).httpdate
|
77
|
+
assert_status_for_get_and_head Net::HTTPOK, 'If-Modified-Since' => the_future
|
78
|
+
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => @etag, 'If-Modified-Since' => the_future
|
79
|
+
end
|
80
|
+
|
81
|
+
# status should be 200 OK when If-None-Match is a wildcard
|
82
|
+
def test_wildcard_match
|
83
|
+
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => '*'
|
84
|
+
assert_status_for_get_and_head Net::HTTPOK, 'If-None-Match' => '*', 'If-Modified-Since' => @last_modified
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
# assert the response status is correct for GET and HEAD
|
90
|
+
def assert_status_for_get_and_head(response_class, headers = {})
|
91
|
+
%w{ get head }.each do |method|
|
92
|
+
res = @http.send(method, @path, headers)
|
93
|
+
assert_kind_of response_class, res
|
94
|
+
assert_equal @etag, res['ETag']
|
95
|
+
case response_class.to_s
|
96
|
+
when 'Net::HTTPNotModified' then
|
97
|
+
assert_nil res['Last-Modified']
|
98
|
+
assert_nil res['Content-Length']
|
99
|
+
when 'Net::HTTPOK' then
|
100
|
+
assert_equal @last_modified, res['Last-Modified']
|
101
|
+
assert_equal @content_length, res['Content-Length']
|
102
|
+
else
|
103
|
+
fail "Incorrect response class: #{response_class}"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# Copyright (c) 2005 Zed A. Shaw
|
2
|
+
# You can redistribute it and/or modify it under the same terms as Ruby.
|
3
|
+
#
|
4
|
+
# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
|
5
|
+
# for more information.
|
6
|
+
|
7
|
+
require 'test/testhelp'
|
8
|
+
|
9
|
+
$test_plugin_fired = 0
|
10
|
+
|
11
|
+
class TestPlugin < GemPlugin::Plugin "/handlers"
|
12
|
+
include Mongrel::HttpHandlerPlugin
|
13
|
+
|
14
|
+
def process(request, response)
|
15
|
+
$test_plugin_fired += 1
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
class Sentinel < GemPlugin::Plugin "/handlers"
|
21
|
+
include Mongrel::HttpHandlerPlugin
|
22
|
+
|
23
|
+
def process(request, response)
|
24
|
+
raise "This Sentinel plugin shouldn't run."
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
class ConfiguratorTest < Test::Unit::TestCase
|
30
|
+
|
31
|
+
def test_base_handler_config
|
32
|
+
@config = nil
|
33
|
+
|
34
|
+
redirect_test_io do
|
35
|
+
@config = Mongrel::Configurator.new :host => "localhost" do
|
36
|
+
listener :port => 4501 do
|
37
|
+
# 2 in front should run, but the sentinel shouldn't since dirhandler processes the request
|
38
|
+
uri "/", :handler => plugin("/handlers/testplugin")
|
39
|
+
uri "/", :handler => plugin("/handlers/testplugin")
|
40
|
+
uri "/", :handler => Mongrel::DirHandler.new(".")
|
41
|
+
uri "/", :handler => plugin("/handlers/testplugin")
|
42
|
+
|
43
|
+
uri "/test", :handler => plugin("/handlers/testplugin")
|
44
|
+
uri "/test", :handler => plugin("/handlers/testplugin")
|
45
|
+
uri "/test", :handler => Mongrel::DirHandler.new(".")
|
46
|
+
uri "/test", :handler => plugin("/handlers/testplugin")
|
47
|
+
|
48
|
+
debug "/"
|
49
|
+
setup_signals
|
50
|
+
|
51
|
+
run_config(File.dirname(__FILE__) + "/../test/mongrel.conf")
|
52
|
+
load_mime_map(File.dirname(__FILE__) + "/../test/mime.yaml")
|
53
|
+
|
54
|
+
run
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# pp @config.listeners.values.first.classifier.routes
|
60
|
+
|
61
|
+
@config.listeners.each do |host,listener|
|
62
|
+
assert listener.classifier.uris.length == 3, "Wrong number of registered URIs"
|
63
|
+
assert listener.classifier.uris.include?("/"), "/ not registered"
|
64
|
+
assert listener.classifier.uris.include?("/test"), "/test not registered"
|
65
|
+
end
|
66
|
+
|
67
|
+
res = Net::HTTP.get(URI.parse('http://localhost:4501/test'))
|
68
|
+
assert res != nil, "Didn't get a response"
|
69
|
+
assert $test_plugin_fired == 3, "Test filter plugin didn't run 3 times."
|
70
|
+
|
71
|
+
redirect_test_io do
|
72
|
+
res = Net::HTTP.get(URI.parse('http://localhost:4501/'))
|
73
|
+
|
74
|
+
assert res != nil, "Didn't get a response"
|
75
|
+
assert $test_plugin_fired == 6, "Test filter plugin didn't run 6 times."
|
76
|
+
end
|
77
|
+
|
78
|
+
redirect_test_io do
|
79
|
+
@config.stop(false, true)
|
80
|
+
end
|
81
|
+
|
82
|
+
assert_raise Errno::EBADF, Errno::ECONNREFUSED do
|
83
|
+
res = Net::HTTP.get(URI.parse("http://localhost:4501/"))
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
data/test/test_debug.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Copyright (c) 2005 Zed A. Shaw
|
2
|
+
# You can redistribute it and/or modify it under the same terms as Ruby.
|
3
|
+
#
|
4
|
+
# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
|
5
|
+
# for more information.
|
6
|
+
|
7
|
+
require 'test/testhelp'
|
8
|
+
require 'mongrel/debug'
|
9
|
+
|
10
|
+
class MongrelDbgTest < Test::Unit::TestCase
|
11
|
+
|
12
|
+
def test_tracing_to_log
|
13
|
+
FileUtils.rm_rf "log/mongrel_debug"
|
14
|
+
|
15
|
+
MongrelDbg::configure
|
16
|
+
out = StringIO.new
|
17
|
+
|
18
|
+
MongrelDbg::begin_trace(:rails)
|
19
|
+
MongrelDbg::trace(:rails, "Good stuff")
|
20
|
+
MongrelDbg::end_trace(:rails)
|
21
|
+
|
22
|
+
assert File.exist?("log/mongrel_debug"), "Didn't make logging directory"
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
# Copyright (c) 2005 Zed A. Shaw
|
2
|
+
# You can redistribute it and/or modify it under the same terms as Ruby.
|
3
|
+
#
|
4
|
+
# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
|
5
|
+
# for more information.
|
6
|
+
|
7
|
+
require 'test/testhelp'
|
8
|
+
|
9
|
+
class SimpleHandler < Mongrel::HttpHandler
|
10
|
+
def process(request, response)
|
11
|
+
response.start do |head,out|
|
12
|
+
head["Content-Type"] = "text/html"
|
13
|
+
results = "<html><body>Your request:<br /><pre>#{request.params.to_yaml}</pre><a href=\"/files\">View the files.</a></body></html>"
|
14
|
+
out << results
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class DumbHandler < Mongrel::HttpHandler
|
20
|
+
def process(request, response)
|
21
|
+
response.start do |head,out|
|
22
|
+
head["Content-Type"] = "text/html"
|
23
|
+
out.write("test")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def check_status(results, expecting)
|
29
|
+
results.each do |res|
|
30
|
+
assert(res.kind_of?(expecting), "Didn't get #{expecting}, got: #{res.class}")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class HandlersTest < Test::Unit::TestCase
|
35
|
+
|
36
|
+
def setup
|
37
|
+
stats = Mongrel::StatisticsFilter.new(:sample_rate => 1)
|
38
|
+
|
39
|
+
@config = Mongrel::Configurator.new :host => '127.0.0.1', :port => 9998 do
|
40
|
+
listener do
|
41
|
+
uri "/", :handler => SimpleHandler.new
|
42
|
+
uri "/", :handler => stats
|
43
|
+
uri "/404", :handler => Mongrel::Error404Handler.new("Not found")
|
44
|
+
uri "/dumb", :handler => Mongrel::DeflateFilter.new
|
45
|
+
uri "/dumb", :handler => DumbHandler.new, :in_front => true
|
46
|
+
uri "/files", :handler => Mongrel::DirHandler.new("doc")
|
47
|
+
uri "/files_nodir", :handler => Mongrel::DirHandler.new("doc", listing_allowed=false, index_html="none")
|
48
|
+
uri "/status", :handler => Mongrel::StatusHandler.new(:stats_filter => stats)
|
49
|
+
uri "/relative", :handler => Mongrel::DirHandler.new(nil, listing_allowed=false, index_html="none")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
unless windows?
|
54
|
+
File.open("/tmp/testfile", 'w') { } # Do nothing
|
55
|
+
end
|
56
|
+
|
57
|
+
@config.run
|
58
|
+
end
|
59
|
+
|
60
|
+
def teardown
|
61
|
+
@config.stop(false, true)
|
62
|
+
File.delete "/tmp/testfile" unless windows?
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_registration_exception_is_not_lost
|
66
|
+
assert_raises(Mongrel::URIClassifier::RegistrationError) do
|
67
|
+
@config = Mongrel::Configurator.new do
|
68
|
+
listener do
|
69
|
+
uri "bogus", :handler => SimpleHandler.new
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_more_web_server
|
76
|
+
res = hit([ "http://localhost:9998/test",
|
77
|
+
"http://localhost:9998/dumb",
|
78
|
+
"http://localhost:9998/404",
|
79
|
+
"http://localhost:9998/files/rdoc/index.html",
|
80
|
+
"http://localhost:9998/files/rdoc/nothere.html",
|
81
|
+
"http://localhost:9998/files/rdoc/",
|
82
|
+
"http://localhost:9998/files_nodir/rdoc/",
|
83
|
+
"http://localhost:9998/status",
|
84
|
+
])
|
85
|
+
check_status res, String
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_nil_dirhandler
|
89
|
+
return if windows?
|
90
|
+
|
91
|
+
# Camping uses this internally
|
92
|
+
handler = Mongrel::DirHandler.new(nil, false)
|
93
|
+
assert handler.can_serve("/tmp/testfile")
|
94
|
+
# Not a bug! A nil @file parameter is the only circumstance under which
|
95
|
+
# we are allowed to serve any existing file
|
96
|
+
assert handler.can_serve("../../../../../../../../../../tmp/testfile")
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_non_nil_dirhandler_is_not_vulnerable_to_path_traversal
|
100
|
+
# The famous security bug of Mongrel 1.1.2
|
101
|
+
handler = Mongrel::DirHandler.new("/doc", false)
|
102
|
+
assert_nil handler.can_serve("/tmp/testfile")
|
103
|
+
assert_nil handler.can_serve("../../../../../../../../../../tmp/testfile")
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_deflate
|
107
|
+
Net::HTTP.start("localhost", 9998) do |h|
|
108
|
+
# Test that no accept-encoding returns a non-deflated response
|
109
|
+
req = h.get("/dumb")
|
110
|
+
assert(
|
111
|
+
!req['Content-Encoding'] ||
|
112
|
+
!req['Content-Encoding'].include?('deflate'))
|
113
|
+
assert_equal "test", req.body
|
114
|
+
|
115
|
+
req = h.get("/dumb", {"Accept-Encoding" => "deflate"})
|
116
|
+
# -MAX_WBITS stops zlib from looking for a zlib header
|
117
|
+
inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
|
118
|
+
assert req['Content-Encoding'].include?('deflate')
|
119
|
+
assert_equal "test", inflater.inflate(req.body)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# TODO: find out why this fails on win32 but nowhere else
|
124
|
+
#def test_posting_fails_dirhandler
|
125
|
+
# req = Net::HTTP::Post.new("http://localhost:9998/files/rdoc/")
|
126
|
+
# req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
|
127
|
+
# res = hit [["http://localhost:9998/files/rdoc/",req]]
|
128
|
+
# check_status res, Net::HTTPNotFound
|
129
|
+
#end
|
130
|
+
|
131
|
+
def test_unregister
|
132
|
+
@config.listeners["127.0.0.1:9998"].unregister("/")
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|