engineyard-mongrel 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. data/CHANGELOG +16 -0
  2. data/CONTRIBUTORS +17 -0
  3. data/COPYING +55 -0
  4. data/LICENSE +55 -0
  5. data/Manifest +71 -0
  6. data/README +74 -0
  7. data/bin/mongrel_rails +285 -0
  8. data/examples/builder.rb +29 -0
  9. data/examples/camping/README +3 -0
  10. data/examples/camping/blog.rb +294 -0
  11. data/examples/camping/tepee.rb +149 -0
  12. data/examples/httpd.conf +474 -0
  13. data/examples/mime.yaml +3 -0
  14. data/examples/mongrel.conf +9 -0
  15. data/examples/mongrel_simple_ctrl.rb +92 -0
  16. data/examples/mongrel_simple_service.rb +116 -0
  17. data/examples/monitrc +57 -0
  18. data/examples/random_thrash.rb +19 -0
  19. data/examples/simpletest.rb +52 -0
  20. data/examples/webrick_compare.rb +20 -0
  21. data/ext/http11/ext_help.h +15 -0
  22. data/ext/http11/extconf.rb +6 -0
  23. data/ext/http11/http11.c +527 -0
  24. data/ext/http11/http11_parser.c +1243 -0
  25. data/ext/http11/http11_parser.h +49 -0
  26. data/ext/http11/http11_parser.java.rl +171 -0
  27. data/ext/http11/http11_parser.rl +152 -0
  28. data/ext/http11/http11_parser_common.rl +54 -0
  29. data/ext/http11_java/Http11Service.java +13 -0
  30. data/ext/http11_java/org/jruby/mongrel/Http11.java +266 -0
  31. data/ext/http11_java/org/jruby/mongrel/Http11Parser.java +474 -0
  32. data/lib/mongrel.rb +360 -0
  33. data/lib/mongrel/camping.rb +107 -0
  34. data/lib/mongrel/cgi.rb +181 -0
  35. data/lib/mongrel/command.rb +222 -0
  36. data/lib/mongrel/configurator.rb +389 -0
  37. data/lib/mongrel/const.rb +110 -0
  38. data/lib/mongrel/debug.rb +203 -0
  39. data/lib/mongrel/gems.rb +22 -0
  40. data/lib/mongrel/handlers.rb +468 -0
  41. data/lib/mongrel/header_out.rb +28 -0
  42. data/lib/mongrel/http_request.rb +155 -0
  43. data/lib/mongrel/http_response.rb +166 -0
  44. data/lib/mongrel/init.rb +10 -0
  45. data/lib/mongrel/mime_types.yml +616 -0
  46. data/lib/mongrel/rails.rb +214 -0
  47. data/lib/mongrel/stats.rb +89 -0
  48. data/lib/mongrel/tcphack.rb +18 -0
  49. data/lib/mongrel/uri_classifier.rb +76 -0
  50. data/mongrel-public_cert.pem +20 -0
  51. data/setup.rb +1585 -0
  52. data/test/benchmark/previous.rb +11 -0
  53. data/test/benchmark/simple.rb +11 -0
  54. data/test/benchmark/utils.rb +82 -0
  55. data/test/mime.yaml +3 -0
  56. data/test/mongrel.conf +1 -0
  57. data/test/test_helper.rb +79 -0
  58. data/test/tools/trickletest.rb +45 -0
  59. data/test/unit/test_cgi_wrapper.rb +26 -0
  60. data/test/unit/test_command.rb +86 -0
  61. data/test/unit/test_conditional.rb +107 -0
  62. data/test/unit/test_configurator.rb +88 -0
  63. data/test/unit/test_debug.rb +25 -0
  64. data/test/unit/test_handlers.rb +136 -0
  65. data/test/unit/test_http_parser.rb +156 -0
  66. data/test/unit/test_redirect_handler.rb +45 -0
  67. data/test/unit/test_request_progress.rb +100 -0
  68. data/test/unit/test_response.rb +127 -0
  69. data/test/unit/test_stats.rb +35 -0
  70. data/test/unit/test_uriclassifier.rb +261 -0
  71. data/test/unit/test_ws.rb +116 -0
  72. metadata +158 -0
@@ -0,0 +1,100 @@
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/test_helper'
8
+
9
+ class UploadBeginHandler < Mongrel::HttpHandler
10
+ attr_reader :request_began, :request_progressed, :request_processed
11
+
12
+ def initialize
13
+ @request_notify = true
14
+ end
15
+
16
+ def reset
17
+ @request_began = false
18
+ @request_progressed = false
19
+ @request_processed = false
20
+ end
21
+
22
+ def request_begins(params)
23
+ @request_began = true
24
+ end
25
+
26
+ def request_progress(params,len,total)
27
+ @request_progressed = true
28
+ end
29
+
30
+ def process(request, response)
31
+ @request_processed = true
32
+ response.start do |head,body|
33
+ body.write("test")
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ class RequestProgressTest < Test::Unit::TestCase
40
+ def setup
41
+ @port = process_based_port
42
+ redirect_test_io do
43
+ @server = Mongrel::HttpServer.new("127.0.0.1", @port)
44
+ end
45
+ @handler = UploadBeginHandler.new
46
+ @server.register("/upload", @handler)
47
+ @server.run
48
+ end
49
+
50
+ def teardown
51
+ @server.stop(true)
52
+ end
53
+
54
+ def test_begin_end_progress
55
+ Net::HTTP.get("localhost", "/upload", @port)
56
+ assert @handler.request_began
57
+ assert @handler.request_progressed
58
+ assert @handler.request_processed
59
+ end
60
+
61
+ def call_and_assert_handlers_in_turn(handlers)
62
+ # reset all handlers
63
+ handlers.each { |h| h.reset }
64
+
65
+ # make the call
66
+ Net::HTTP.get("localhost", "/upload", @port)
67
+
68
+ # assert that each one was fired
69
+ handlers.each { |h|
70
+ assert h.request_began && h.request_progressed && h.request_processed,
71
+ "Callbacks NOT fired for #{h}"
72
+ }
73
+ end
74
+
75
+ def test_more_than_one_begin_end_progress
76
+ handlers = [@handler]
77
+
78
+ second = UploadBeginHandler.new
79
+ @server.register("/upload", second)
80
+ handlers << second
81
+ call_and_assert_handlers_in_turn(handlers)
82
+
83
+ # check three handlers
84
+ third = UploadBeginHandler.new
85
+ @server.register("/upload", third)
86
+ handlers << third
87
+ call_and_assert_handlers_in_turn(handlers)
88
+
89
+ # remove handlers to make sure they've all gone away
90
+ @server.unregister("/upload")
91
+ handlers.each { |h| h.reset }
92
+ Net::HTTP.get("localhost", "/upload", @port)
93
+ handlers.each { |h|
94
+ assert !h.request_began && !h.request_progressed && !h.request_processed
95
+ }
96
+
97
+ # re-register upload to the state before this test
98
+ @server.register("/upload", @handler)
99
+ end
100
+ end
@@ -0,0 +1,127 @@
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/test_helper'
8
+
9
+ include Mongrel
10
+
11
+ class ResponseTest < Test::Unit::TestCase
12
+
13
+ def test_response_headers
14
+ out = StringIO.new
15
+ resp = HttpResponse.new(out)
16
+ resp.status = 200
17
+ resp.header["Accept"] = "text/plain"
18
+ resp.header["X-Whatever"] = "stuff"
19
+ resp.body.write("test")
20
+ resp.finished
21
+
22
+ assert out.length > 0, "output didn't have data"
23
+ end
24
+
25
+ def test_response_200
26
+ io = StringIO.new
27
+ resp = HttpResponse.new(io)
28
+ resp.start do |head,out|
29
+ head["Accept"] = "text/plain"
30
+ out.write("tested")
31
+ out.write("hello!")
32
+ end
33
+
34
+ resp.finished
35
+ assert io.length > 0, "output didn't have data"
36
+ end
37
+
38
+ def test_response_duplicate_header_squash
39
+ io = StringIO.new
40
+ resp = HttpResponse.new(io)
41
+ resp.start do |head,out|
42
+ head["Content-Length"] = 30
43
+ head["Content-Length"] = 0
44
+ end
45
+
46
+ resp.finished
47
+
48
+ assert_equal io.length, 95, "too much output"
49
+ end
50
+
51
+
52
+ def test_response_some_duplicates_allowed
53
+ allowed_duplicates = ["Set-Cookie", "Set-Cookie2", "Warning", "WWW-Authenticate"]
54
+ io = StringIO.new
55
+ resp = HttpResponse.new(io)
56
+ resp.start do |head,out|
57
+ allowed_duplicates.each do |dup|
58
+ 10.times do |i|
59
+ head[dup] = i
60
+ end
61
+ end
62
+ end
63
+
64
+ resp.finished
65
+
66
+ assert_equal io.length, 734, "wrong amount of output"
67
+ end
68
+
69
+ def test_response_404
70
+ io = StringIO.new
71
+
72
+ resp = HttpResponse.new(io)
73
+ resp.start(404) do |head,out|
74
+ head['Accept'] = "text/plain"
75
+ out.write("NOT FOUND")
76
+ end
77
+
78
+ resp.finished
79
+ assert io.length > 0, "output didn't have data"
80
+ end
81
+
82
+ def test_response_file
83
+ contents = "PLAIN TEXT\r\nCONTENTS\r\n"
84
+ require 'tempfile'
85
+ tmpf = Tempfile.new("test_response_file")
86
+ tmpf.binmode
87
+ tmpf.write(contents)
88
+ tmpf.rewind
89
+
90
+ io = StringIO.new
91
+ resp = HttpResponse.new(io)
92
+ resp.start(200) do |head,out|
93
+ head['Content-Type'] = 'text/plain'
94
+ resp.send_header
95
+ resp.send_file(tmpf.path)
96
+ end
97
+ io.rewind
98
+ tmpf.close
99
+
100
+ assert io.length > 0, "output didn't have data"
101
+ assert io.read[-contents.length..-1] == contents, "output doesn't end with file payload"
102
+ end
103
+
104
+ def test_response_with_custom_reason
105
+ reason = "You made a bad request"
106
+ io = StringIO.new
107
+ resp = HttpResponse.new(io)
108
+ resp.start(400, false, reason) { |head,out| }
109
+ resp.finished
110
+
111
+ io.rewind
112
+ assert_match(/.* #{reason}$/, io.readline.chomp, "wrong custom reason phrase")
113
+ end
114
+
115
+ def test_response_with_default_reason
116
+ code = 400
117
+ io = StringIO.new
118
+ resp = HttpResponse.new(io)
119
+ resp.start(code) { |head,out| }
120
+ resp.finished
121
+
122
+ io.rewind
123
+ assert_match(/.* #{HTTP_STATUS_CODES[code]}$/, io.readline.chomp, "wrong default reason phrase")
124
+ end
125
+
126
+ end
127
+
@@ -0,0 +1,35 @@
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/test_helper'
8
+
9
+ class StatsTest < Test::Unit::TestCase
10
+
11
+ def test_sampling_speed
12
+ out = StringIO.new
13
+
14
+ s = Mongrel::Stats.new("test")
15
+ t = Mongrel::Stats.new("time")
16
+
17
+ 100.times { s.sample(rand(20)); t.tick }
18
+
19
+ s.dump("FIRST", out)
20
+ t.dump("FIRST", out)
21
+
22
+ old_mean = s.mean
23
+ old_sd = s.sd
24
+
25
+ s.reset
26
+ t.reset
27
+ 100.times { s.sample(rand(30)); t.tick }
28
+
29
+ s.dump("SECOND", out)
30
+ t.dump("SECOND", out)
31
+ assert_not_equal old_mean, s.mean
32
+ assert_not_equal old_mean, s.sd
33
+ end
34
+
35
+ end
@@ -0,0 +1,261 @@
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/test_helper'
8
+
9
+ include Mongrel
10
+
11
+ class URIClassifierTest < Test::Unit::TestCase
12
+
13
+ def test_uri_finding
14
+ uri_classifier = URIClassifier.new
15
+ uri_classifier.register("/test", 1)
16
+
17
+ script_name, path_info, value = uri_classifier.resolve("/test")
18
+ assert_equal 1, value
19
+ assert_equal "/test", script_name
20
+ end
21
+
22
+ def test_root_handler_only
23
+ uri_classifier = URIClassifier.new
24
+ uri_classifier.register("/", 1)
25
+
26
+ script_name, path_info, value = uri_classifier.resolve("/test")
27
+ assert_equal 1, value
28
+ assert_equal "/", script_name
29
+ assert_equal "/test", path_info
30
+ end
31
+
32
+ def test_uri_prefix_ops
33
+ test = "/pre/fix/test"
34
+ prefix = "/pre"
35
+
36
+ uri_classifier = URIClassifier.new
37
+ uri_classifier.register(prefix,1)
38
+
39
+ script_name, path_info, value = uri_classifier.resolve(prefix)
40
+ script_name, path_info, value = uri_classifier.resolve(test)
41
+ assert_equal 1, value
42
+ assert_equal prefix, script_name
43
+ assert_equal test[script_name.length .. -1], path_info
44
+
45
+ assert uri_classifier.inspect
46
+ assert_equal prefix, uri_classifier.uris[0]
47
+ end
48
+
49
+ def test_not_finding
50
+ test = "/cant/find/me"
51
+ uri_classifier = URIClassifier.new
52
+ uri_classifier.register(test, 1)
53
+
54
+ script_name, path_info, value = uri_classifier.resolve("/nope/not/here")
55
+ assert_nil script_name
56
+ assert_nil path_info
57
+ assert_nil value
58
+ end
59
+
60
+ def test_exceptions
61
+ uri_classifier = URIClassifier.new
62
+
63
+ uri_classifier.register("/test", 1)
64
+
65
+ failed = false
66
+ begin
67
+ uri_classifier.register("/test", 1)
68
+ rescue => e
69
+ failed = true
70
+ end
71
+
72
+ assert failed
73
+
74
+ failed = false
75
+ begin
76
+ uri_classifier.register("", 1)
77
+ rescue => e
78
+ failed = true
79
+ end
80
+
81
+ assert failed
82
+ end
83
+
84
+
85
+ def test_register_unregister
86
+ uri_classifier = URIClassifier.new
87
+
88
+ 100.times do
89
+ uri_classifier.register("/stuff", 1)
90
+ value = uri_classifier.unregister("/stuff")
91
+ assert_equal 1, value
92
+ end
93
+
94
+ uri_classifier.register("/things",1)
95
+ script_name, path_info, value = uri_classifier.resolve("/things")
96
+ assert_equal 1, value
97
+
98
+ uri_classifier.unregister("/things")
99
+ script_name, path_info, value = uri_classifier.resolve("/things")
100
+ assert_nil value
101
+
102
+ end
103
+
104
+
105
+ def test_uri_branching
106
+ uri_classifier = URIClassifier.new
107
+ uri_classifier.register("/test", 1)
108
+ uri_classifier.register("/test/this",2)
109
+
110
+ script_name, path_info, handler = uri_classifier.resolve("/test")
111
+ script_name, path_info, handler = uri_classifier.resolve("/test/that")
112
+ assert_equal "/test", script_name, "failed to properly find script off branch portion of uri"
113
+ assert_equal "/that", path_info
114
+ assert_equal 1, handler, "wrong result for branching uri"
115
+ end
116
+
117
+ def test_all_prefixing
118
+ tests = ["/test","/test/that","/test/this"]
119
+ uri = "/test/this/that"
120
+ uri_classifier = URIClassifier.new
121
+
122
+ current = ""
123
+ uri.each_byte do |c|
124
+ current << c.chr
125
+ uri_classifier.register(current, c)
126
+ end
127
+
128
+
129
+ # Try to resolve everything with no asserts as a fuzzing
130
+ tests.each do |prefix|
131
+ current = ""
132
+ prefix.each_byte do |c|
133
+ current << c.chr
134
+ script_name, path_info, handler = uri_classifier.resolve(current)
135
+ assert script_name
136
+ assert path_info
137
+ assert handler
138
+ end
139
+ end
140
+
141
+ # Assert that we find stuff
142
+ tests.each do |t|
143
+ script_name, path_info, handler = uri_classifier.resolve(t)
144
+ assert handler
145
+ end
146
+
147
+ # Assert we don't find stuff
148
+ script_name, path_info, handler = uri_classifier.resolve("chicken")
149
+ assert_nil handler
150
+ assert_nil script_name
151
+ assert_nil path_info
152
+ end
153
+
154
+
155
+ # Verifies that a root mounted ("/") handler resolves
156
+ # such that path info matches the original URI.
157
+ # This is needed to accommodate real usage of handlers.
158
+ def test_root_mounted
159
+ uri_classifier = URIClassifier.new
160
+ root = "/"
161
+ path = "/this/is/a/test"
162
+
163
+ uri_classifier.register(root, 1)
164
+
165
+ script_name, path_info, handler = uri_classifier.resolve(root)
166
+ assert_equal 1, handler
167
+ assert_equal root, path_info
168
+ assert_equal root, script_name
169
+
170
+ script_name, path_info, handler = uri_classifier.resolve(path)
171
+ assert_equal path, path_info
172
+ assert_equal root, script_name
173
+ assert_equal 1, handler
174
+ end
175
+
176
+ # Verifies that a root mounted ("/") handler
177
+ # is the default point, doesn't matter the order we use
178
+ # to register the URIs
179
+ def test_classifier_order
180
+ tests = ["/before", "/way_past"]
181
+ root = "/"
182
+ path = "/path"
183
+
184
+ uri_classifier = URIClassifier.new
185
+ uri_classifier.register(path, 1)
186
+ uri_classifier.register(root, 2)
187
+
188
+ tests.each do |uri|
189
+ script_name, path_info, handler = uri_classifier.resolve(uri)
190
+ assert_equal root, script_name, "#{uri} did not resolve to #{root}"
191
+ assert_equal uri, path_info
192
+ assert_equal 2, handler
193
+ end
194
+ end
195
+
196
+ if ENV['BENCHMARK']
197
+ # Eventually we will have a suite of benchmarks instead of lamely installing a test
198
+
199
+ def test_benchmark
200
+
201
+ # This URI set should favor a TST. Both versions increase linearly until you hit 14
202
+ # URIs, then the TST flattens out.
203
+ @uris = %w(
204
+ /
205
+ /dag /dig /digbark /dog /dogbark /dog/bark /dug /dugbarking /puppy
206
+ /c /cat /cat/tree /cat/tree/mulberry /cats /cot /cot/tree/mulberry /kitty /kittycat
207
+ # /eag /eig /eigbark /eog /eogbark /eog/bark /eug /eugbarking /iuppy
208
+ # /f /fat /fat/tree /fat/tree/mulberry /fats /fot /fot/tree/mulberry /jitty /jittyfat
209
+ # /gag /gig /gigbark /gog /gogbark /gog/bark /gug /gugbarking /kuppy
210
+ # /h /hat /hat/tree /hat/tree/mulberry /hats /hot /hot/tree/mulberry /litty /littyhat
211
+ # /ceag /ceig /ceigbark /ceog /ceogbark /ceog/cbark /ceug /ceugbarking /ciuppy
212
+ # /cf /cfat /cfat/ctree /cfat/ctree/cmulberry /cfats /cfot /cfot/ctree/cmulberry /cjitty /cjittyfat
213
+ # /cgag /cgig /cgigbark /cgog /cgogbark /cgog/cbark /cgug /cgugbarking /ckuppy
214
+ # /ch /chat /chat/ctree /chat/ctree/cmulberry /chats /chot /chot/ctree/cmulberry /citty /cittyhat
215
+ )
216
+
217
+ @requests = %w(
218
+ /
219
+ /dig
220
+ /digging
221
+ /dogging
222
+ /dogbarking/
223
+ /puppy/barking
224
+ /c
225
+ /cat
226
+ /cat/shrub
227
+ /cat/tree
228
+ /cat/tree/maple
229
+ /cat/tree/mulberry/tree
230
+ /cat/tree/oak
231
+ /cats/
232
+ /cats/tree
233
+ /cod
234
+ /zebra
235
+ )
236
+
237
+ @classifier = URIClassifier.new
238
+ @uris.each do |uri|
239
+ @classifier.register(uri, 1)
240
+ end
241
+
242
+ puts "#{@uris.size} URIs / #{@requests.size * 10000} requests"
243
+
244
+ Benchmark.bm do |x|
245
+ x.report do
246
+ # require 'ruby-prof'
247
+ # profile = RubyProf.profile do
248
+ 10000.times do
249
+ @requests.each do |request|
250
+ @classifier.resolve(request)
251
+ end
252
+ end
253
+ # end
254
+ # File.open("profile.html", 'w') { |file| RubyProf::GraphHtmlPrinter.new(profile).print(file, 0) }
255
+ end
256
+ end
257
+ end
258
+ end
259
+
260
+ end
261
+