webrick 1.3.1 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of webrick might be problematic. Click here for more details.

Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +63 -0
  5. data/Rakefile +10 -0
  6. data/bin/console +14 -0
  7. data/bin/setup +8 -0
  8. data/lib/webrick/accesslog.rb +9 -1
  9. data/lib/webrick/cgi.rb +58 -5
  10. data/lib/webrick/compat.rb +2 -1
  11. data/lib/webrick/config.rb +47 -10
  12. data/lib/webrick/cookie.rb +69 -7
  13. data/lib/webrick/htmlutils.rb +4 -2
  14. data/lib/webrick/httpauth/authenticator.rb +13 -8
  15. data/lib/webrick/httpauth/basicauth.rb +16 -8
  16. data/lib/webrick/httpauth/digestauth.rb +35 -32
  17. data/lib/webrick/httpauth/htdigest.rb +12 -8
  18. data/lib/webrick/httpauth/htgroup.rb +10 -6
  19. data/lib/webrick/httpauth/htpasswd.rb +46 -9
  20. data/lib/webrick/httpauth/userdb.rb +1 -0
  21. data/lib/webrick/httpauth.rb +6 -5
  22. data/lib/webrick/httpproxy.rb +93 -48
  23. data/lib/webrick/httprequest.rb +192 -27
  24. data/lib/webrick/httpresponse.rb +221 -70
  25. data/lib/webrick/https.rb +90 -2
  26. data/lib/webrick/httpserver.rb +45 -15
  27. data/lib/webrick/httpservlet/abstract.rb +5 -6
  28. data/lib/webrick/httpservlet/cgi_runner.rb +3 -2
  29. data/lib/webrick/httpservlet/cgihandler.rb +22 -10
  30. data/lib/webrick/httpservlet/erbhandler.rb +4 -3
  31. data/lib/webrick/httpservlet/filehandler.rb +136 -65
  32. data/lib/webrick/httpservlet/prochandler.rb +15 -1
  33. data/lib/webrick/httpservlet.rb +6 -5
  34. data/lib/webrick/httpstatus.rb +24 -14
  35. data/lib/webrick/httputils.rb +133 -13
  36. data/lib/webrick/httpversion.rb +28 -1
  37. data/lib/webrick/log.rb +25 -5
  38. data/lib/webrick/server.rb +234 -74
  39. data/lib/webrick/ssl.rb +100 -12
  40. data/lib/webrick/utils.rb +98 -69
  41. data/lib/webrick/version.rb +6 -1
  42. data/lib/webrick.rb +7 -7
  43. data/webrick.gemspec +76 -0
  44. metadata +70 -69
  45. data/README.txt +0 -21
  46. data/sample/webrick/demo-app.rb +0 -66
  47. data/sample/webrick/demo-multipart.cgi +0 -12
  48. data/sample/webrick/demo-servlet.rb +0 -6
  49. data/sample/webrick/demo-urlencoded.cgi +0 -12
  50. data/sample/webrick/hello.cgi +0 -11
  51. data/sample/webrick/hello.rb +0 -8
  52. data/sample/webrick/httpd.rb +0 -23
  53. data/sample/webrick/httpproxy.rb +0 -25
  54. data/sample/webrick/httpsd.rb +0 -33
  55. data/test/openssl/utils.rb +0 -313
  56. data/test/ruby/envutil.rb +0 -208
  57. data/test/webrick/test_cgi.rb +0 -134
  58. data/test/webrick/test_cookie.rb +0 -131
  59. data/test/webrick/test_filehandler.rb +0 -285
  60. data/test/webrick/test_httpauth.rb +0 -167
  61. data/test/webrick/test_httpproxy.rb +0 -282
  62. data/test/webrick/test_httprequest.rb +0 -411
  63. data/test/webrick/test_httpresponse.rb +0 -49
  64. data/test/webrick/test_httpserver.rb +0 -305
  65. data/test/webrick/test_httputils.rb +0 -96
  66. data/test/webrick/test_httpversion.rb +0 -40
  67. data/test/webrick/test_server.rb +0 -67
  68. data/test/webrick/test_utils.rb +0 -64
  69. data/test/webrick/utils.rb +0 -58
  70. data/test/webrick/webrick.cgi +0 -36
  71. data/test/webrick/webrick_long_filename.cgi +0 -36
@@ -1,411 +0,0 @@
1
- require "webrick"
2
- require "stringio"
3
- require "test/unit"
4
-
5
- class TestWEBrickHTTPRequest < Test::Unit::TestCase
6
- def test_simple_request
7
- msg = <<-_end_of_message_
8
- GET /
9
- _end_of_message_
10
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
11
- req.parse(StringIO.new(msg))
12
- assert(req.meta_vars) # fails if @header was not initialized and iteration is attempted on the nil reference
13
- end
14
-
15
- def test_parse_09
16
- msg = <<-_end_of_message_
17
- GET /
18
- foobar # HTTP/0.9 request don't have header nor entity body.
19
- _end_of_message_
20
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
21
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
22
- assert_equal("GET", req.request_method)
23
- assert_equal("/", req.unparsed_uri)
24
- assert_equal(WEBrick::HTTPVersion.new("0.9"), req.http_version)
25
- assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
26
- assert_equal(80, req.port)
27
- assert_equal(false, req.keep_alive?)
28
- assert_equal(nil, req.body)
29
- assert(req.query.empty?)
30
- end
31
-
32
- def test_parse_10
33
- msg = <<-_end_of_message_
34
- GET / HTTP/1.0
35
-
36
- _end_of_message_
37
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
38
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
39
- assert_equal("GET", req.request_method)
40
- assert_equal("/", req.unparsed_uri)
41
- assert_equal(WEBrick::HTTPVersion.new("1.0"), req.http_version)
42
- assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
43
- assert_equal(80, req.port)
44
- assert_equal(false, req.keep_alive?)
45
- assert_equal(nil, req.body)
46
- assert(req.query.empty?)
47
- end
48
-
49
- def test_parse_11
50
- msg = <<-_end_of_message_
51
- GET /path HTTP/1.1
52
-
53
- _end_of_message_
54
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
55
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
56
- assert_equal("GET", req.request_method)
57
- assert_equal("/path", req.unparsed_uri)
58
- assert_equal("", req.script_name)
59
- assert_equal("/path", req.path_info)
60
- assert_equal(WEBrick::HTTPVersion.new("1.1"), req.http_version)
61
- assert_equal(WEBrick::Config::HTTP[:ServerName], req.host)
62
- assert_equal(80, req.port)
63
- assert_equal(true, req.keep_alive?)
64
- assert_equal(nil, req.body)
65
- assert(req.query.empty?)
66
- end
67
-
68
- def test_request_uri_too_large
69
- msg = <<-_end_of_message_
70
- GET /#{"a"*2084} HTTP/1.1
71
- _end_of_message_
72
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
73
- assert_raise(WEBrick::HTTPStatus::RequestURITooLarge){
74
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
75
- }
76
- end
77
-
78
- def test_parse_headers
79
- msg = <<-_end_of_message_
80
- GET /path HTTP/1.1
81
- Host: test.ruby-lang.org:8080
82
- Connection: close
83
- Accept: text/*;q=0.3, text/html;q=0.7, text/html;level=1,
84
- text/html;level=2;q=0.4, */*;q=0.5
85
- Accept-Encoding: compress;q=0.5
86
- Accept-Encoding: gzip;q=1.0, identity; q=0.4, *;q=0
87
- Accept-Language: en;q=0.5, *; q=0
88
- Accept-Language: ja
89
- Content-Type: text/plain
90
- Content-Length: 7
91
- X-Empty-Header:
92
-
93
- foobar
94
- _end_of_message_
95
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
96
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
97
- assert_equal(
98
- URI.parse("http://test.ruby-lang.org:8080/path"), req.request_uri)
99
- assert_equal("test.ruby-lang.org", req.host)
100
- assert_equal(8080, req.port)
101
- assert_equal(false, req.keep_alive?)
102
- assert_equal(
103
- %w(text/html;level=1 text/html */* text/html;level=2 text/*),
104
- req.accept)
105
- assert_equal(%w(gzip compress identity *), req.accept_encoding)
106
- assert_equal(%w(ja en *), req.accept_language)
107
- assert_equal(7, req.content_length)
108
- assert_equal("text/plain", req.content_type)
109
- assert_equal("foobar\n", req.body)
110
- assert_equal("", req["x-empty-header"])
111
- assert_equal(nil, req["x-no-header"])
112
- assert(req.query.empty?)
113
- end
114
-
115
- def test_parse_header2()
116
- msg = <<-_end_of_message_
117
- POST /foo/bar/../baz?q=a HTTP/1.0
118
- Content-Length: 9
119
- User-Agent:
120
- FOO BAR
121
- BAZ
122
-
123
- hogehoge
124
- _end_of_message_
125
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
126
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
127
- assert_equal("POST", req.request_method)
128
- assert_equal("/foo/baz", req.path)
129
- assert_equal("", req.script_name)
130
- assert_equal("/foo/baz", req.path_info)
131
- assert_equal("9", req['content-length'])
132
- assert_equal("FOO BAR BAZ", req['user-agent'])
133
- assert_equal("hogehoge\n", req.body)
134
- end
135
-
136
- def test_parse_headers3
137
- msg = <<-_end_of_message_
138
- GET /path HTTP/1.1
139
- Host: test.ruby-lang.org
140
-
141
- _end_of_message_
142
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
143
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
144
- assert_equal(URI.parse("http://test.ruby-lang.org/path"), req.request_uri)
145
- assert_equal("test.ruby-lang.org", req.host)
146
- assert_equal(80, req.port)
147
-
148
- msg = <<-_end_of_message_
149
- GET /path HTTP/1.1
150
- Host: 192.168.1.1
151
-
152
- _end_of_message_
153
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
154
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
155
- assert_equal(URI.parse("http://192.168.1.1/path"), req.request_uri)
156
- assert_equal("192.168.1.1", req.host)
157
- assert_equal(80, req.port)
158
-
159
- msg = <<-_end_of_message_
160
- GET /path HTTP/1.1
161
- Host: [fe80::208:dff:feef:98c7]
162
-
163
- _end_of_message_
164
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
165
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
166
- assert_equal(URI.parse("http://[fe80::208:dff:feef:98c7]/path"),
167
- req.request_uri)
168
- assert_equal("[fe80::208:dff:feef:98c7]", req.host)
169
- assert_equal(80, req.port)
170
-
171
- msg = <<-_end_of_message_
172
- GET /path HTTP/1.1
173
- Host: 192.168.1.1:8080
174
-
175
- _end_of_message_
176
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
177
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
178
- assert_equal(URI.parse("http://192.168.1.1:8080/path"), req.request_uri)
179
- assert_equal("192.168.1.1", req.host)
180
- assert_equal(8080, req.port)
181
-
182
- msg = <<-_end_of_message_
183
- GET /path HTTP/1.1
184
- Host: [fe80::208:dff:feef:98c7]:8080
185
-
186
- _end_of_message_
187
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
188
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
189
- assert_equal(URI.parse("http://[fe80::208:dff:feef:98c7]:8080/path"),
190
- req.request_uri)
191
- assert_equal("[fe80::208:dff:feef:98c7]", req.host)
192
- assert_equal(8080, req.port)
193
- end
194
-
195
- def test_parse_get_params
196
- param = "foo=1;foo=2;foo=3;bar=x"
197
- msg = <<-_end_of_message_
198
- GET /path?#{param} HTTP/1.1
199
- Host: test.ruby-lang.org:8080
200
-
201
- _end_of_message_
202
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
203
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
204
- query = req.query
205
- assert_equal("1", query["foo"])
206
- assert_equal(["1", "2", "3"], query["foo"].to_ary)
207
- assert_equal(["1", "2", "3"], query["foo"].list)
208
- assert_equal("x", query["bar"])
209
- assert_equal(["x"], query["bar"].list)
210
- end
211
-
212
- def test_parse_post_params
213
- param = "foo=1;foo=2;foo=3;bar=x"
214
- msg = <<-_end_of_message_
215
- POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
216
- Host: test.ruby-lang.org:8080
217
- Content-Length: #{param.size}
218
- Content-Type: application/x-www-form-urlencoded
219
-
220
- #{param}
221
- _end_of_message_
222
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
223
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
224
- query = req.query
225
- assert_equal("1", query["foo"])
226
- assert_equal(["1", "2", "3"], query["foo"].to_ary)
227
- assert_equal(["1", "2", "3"], query["foo"].list)
228
- assert_equal("x", query["bar"])
229
- assert_equal(["x"], query["bar"].list)
230
- end
231
-
232
- def test_chunked
233
- crlf = "\x0d\x0a"
234
- msg = <<-_end_of_message_
235
- POST /path HTTP/1.1
236
- Host: test.ruby-lang.org:8080
237
- Transfer-Encoding: chunked
238
-
239
- _end_of_message_
240
- msg.gsub!(/^ {6}/, "")
241
- open(__FILE__){|io|
242
- while chunk = io.read(100)
243
- msg << chunk.size.to_s(16) << crlf
244
- msg << chunk << crlf
245
- end
246
- }
247
- msg << "0" << crlf
248
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
249
- req.parse(StringIO.new(msg))
250
- assert_equal(File.read(__FILE__), req.body)
251
- end
252
-
253
- def test_forwarded
254
- msg = <<-_end_of_message_
255
- GET /foo HTTP/1.1
256
- Host: localhost:10080
257
- User-Agent: w3m/0.5.2
258
- X-Forwarded-For: 123.123.123.123
259
- X-Forwarded-Host: forward.example.com
260
- X-Forwarded-Server: server.example.com
261
- Connection: Keep-Alive
262
-
263
- _end_of_message_
264
- msg.gsub!(/^ {6}/, "")
265
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
266
- req.parse(StringIO.new(msg))
267
- assert_equal("server.example.com", req.server_name)
268
- assert_equal("http://forward.example.com/foo", req.request_uri.to_s)
269
- assert_equal("forward.example.com", req.host)
270
- assert_equal(80, req.port)
271
- assert_equal("123.123.123.123", req.remote_ip)
272
- assert(!req.ssl?)
273
-
274
- msg = <<-_end_of_message_
275
- GET /foo HTTP/1.1
276
- Host: localhost:10080
277
- User-Agent: w3m/0.5.2
278
- X-Forwarded-For: 192.168.1.10, 172.16.1.1, 123.123.123.123
279
- X-Forwarded-Host: forward.example.com:8080
280
- X-Forwarded-Server: server.example.com
281
- Connection: Keep-Alive
282
-
283
- _end_of_message_
284
- msg.gsub!(/^ {6}/, "")
285
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
286
- req.parse(StringIO.new(msg))
287
- assert_equal("server.example.com", req.server_name)
288
- assert_equal("http://forward.example.com:8080/foo", req.request_uri.to_s)
289
- assert_equal("forward.example.com", req.host)
290
- assert_equal(8080, req.port)
291
- assert_equal("123.123.123.123", req.remote_ip)
292
- assert(!req.ssl?)
293
-
294
- msg = <<-_end_of_message_
295
- GET /foo HTTP/1.1
296
- Host: localhost:10080
297
- Client-IP: 234.234.234.234
298
- X-Forwarded-Proto: https
299
- X-Forwarded-For: 192.168.1.10, 10.0.0.1, 123.123.123.123
300
- X-Forwarded-Host: forward.example.com
301
- X-Forwarded-Server: server.example.com
302
- X-Requested-With: XMLHttpRequest
303
- Connection: Keep-Alive
304
-
305
- _end_of_message_
306
- msg.gsub!(/^ {6}/, "")
307
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
308
- req.parse(StringIO.new(msg))
309
- assert_equal("server.example.com", req.server_name)
310
- assert_equal("https://forward.example.com/foo", req.request_uri.to_s)
311
- assert_equal("forward.example.com", req.host)
312
- assert_equal(443, req.port)
313
- assert_equal("234.234.234.234", req.remote_ip)
314
- assert(req.ssl?)
315
-
316
- msg = <<-_end_of_message_
317
- GET /foo HTTP/1.1
318
- Host: localhost:10080
319
- Client-IP: 234.234.234.234
320
- X-Forwarded-Proto: https
321
- X-Forwarded-For: 192.168.1.10
322
- X-Forwarded-Host: forward1.example.com:1234, forward2.example.com:5678
323
- X-Forwarded-Server: server1.example.com, server2.example.com
324
- X-Requested-With: XMLHttpRequest
325
- Connection: Keep-Alive
326
-
327
- _end_of_message_
328
- msg.gsub!(/^ {6}/, "")
329
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
330
- req.parse(StringIO.new(msg))
331
- assert_equal("server1.example.com", req.server_name)
332
- assert_equal("https://forward1.example.com:1234/foo", req.request_uri.to_s)
333
- assert_equal("forward1.example.com", req.host)
334
- assert_equal(1234, req.port)
335
- assert_equal("234.234.234.234", req.remote_ip)
336
- assert(req.ssl?)
337
- end
338
-
339
- def test_continue_sent
340
- msg = <<-_end_of_message_
341
- POST /path HTTP/1.1
342
- Expect: 100-continue
343
-
344
- _end_of_message_
345
- msg.gsub!(/^ {6}/, "")
346
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
347
- req.parse(StringIO.new(msg))
348
- assert req['expect']
349
- l = msg.size
350
- req.continue
351
- assert_not_equal l, msg.size
352
- assert_match /HTTP\/1.1 100 continue\r\n\r\n\z/, msg
353
- assert !req['expect']
354
- end
355
-
356
- def test_continue_not_sent
357
- msg = <<-_end_of_message_
358
- POST /path HTTP/1.1
359
-
360
- _end_of_message_
361
- msg.gsub!(/^ {6}/, "")
362
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
363
- req.parse(StringIO.new(msg))
364
- assert !req['expect']
365
- l = msg.size
366
- req.continue
367
- assert_equal l, msg.size
368
- end
369
-
370
- def test_bad_messages
371
- param = "foo=1;foo=2;foo=3;bar=x"
372
- msg = <<-_end_of_message_
373
- POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
374
- Host: test.ruby-lang.org:8080
375
- Content-Type: application/x-www-form-urlencoded
376
-
377
- #{param}
378
- _end_of_message_
379
- assert_raise(WEBrick::HTTPStatus::LengthRequired){
380
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
381
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
382
- req.body
383
- }
384
-
385
- msg = <<-_end_of_message_
386
- POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
387
- Host: test.ruby-lang.org:8080
388
- Content-Length: 100000
389
-
390
- body is too short.
391
- _end_of_message_
392
- assert_raise(WEBrick::HTTPStatus::BadRequest){
393
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
394
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
395
- req.body
396
- }
397
-
398
- msg = <<-_end_of_message_
399
- POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1
400
- Host: test.ruby-lang.org:8080
401
- Transfer-Encoding: foobar
402
-
403
- body is too short.
404
- _end_of_message_
405
- assert_raise(WEBrick::HTTPStatus::NotImplemented){
406
- req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
407
- req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
408
- req.body
409
- }
410
- end
411
- end
@@ -1,49 +0,0 @@
1
- require "webrick"
2
- require "minitest/autorun"
3
-
4
- module WEBrick
5
- class TestHTTPResponse < MiniTest::Unit::TestCase
6
- class FakeLogger
7
- attr_reader :messages
8
-
9
- def initialize
10
- @messages = []
11
- end
12
-
13
- def warn msg
14
- @messages << msg
15
- end
16
- end
17
-
18
- attr_reader :config, :logger, :res
19
-
20
- def setup
21
- super
22
- @logger = FakeLogger.new
23
- @config = Config::HTTP
24
- @config[:Logger] = logger
25
- @res = HTTPResponse.new config
26
- @res.keep_alive = true
27
- end
28
-
29
- def test_304_does_not_log_warning
30
- res.status = 304
31
- res.setup_header
32
- assert_equal 0, logger.messages.length
33
- end
34
-
35
- def test_204_does_not_log_warning
36
- res.status = 204
37
- res.setup_header
38
-
39
- assert_equal 0, logger.messages.length
40
- end
41
-
42
- def test_1xx_does_not_log_warnings
43
- res.status = 105
44
- res.setup_header
45
-
46
- assert_equal 0, logger.messages.length
47
- end
48
- end
49
- end