glebtv-httpclient 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.rdoc +108 -0
- data/bin/httpclient +65 -0
- data/lib/glebtv-httpclient.rb +1 -0
- data/lib/hexdump.rb +50 -0
- data/lib/http-access2/cookie.rb +1 -0
- data/lib/http-access2/http.rb +1 -0
- data/lib/http-access2.rb +55 -0
- data/lib/httpclient/auth.rb +899 -0
- data/lib/httpclient/cacert.p7s +1912 -0
- data/lib/httpclient/connection.rb +88 -0
- data/lib/httpclient/cookie.rb +438 -0
- data/lib/httpclient/http.rb +1050 -0
- data/lib/httpclient/include_client.rb +83 -0
- data/lib/httpclient/session.rb +1031 -0
- data/lib/httpclient/ssl_config.rb +403 -0
- data/lib/httpclient/timeout.rb +140 -0
- data/lib/httpclient/util.rb +186 -0
- data/lib/httpclient/version.rb +3 -0
- data/lib/httpclient.rb +1157 -0
- data/lib/oauthclient.rb +110 -0
- data/sample/async.rb +8 -0
- data/sample/auth.rb +11 -0
- data/sample/cookie.rb +18 -0
- data/sample/dav.rb +103 -0
- data/sample/howto.rb +49 -0
- data/sample/oauth_buzz.rb +57 -0
- data/sample/oauth_friendfeed.rb +59 -0
- data/sample/oauth_twitter.rb +61 -0
- data/sample/ssl/0cert.pem +22 -0
- data/sample/ssl/0key.pem +30 -0
- data/sample/ssl/1000cert.pem +19 -0
- data/sample/ssl/1000key.pem +18 -0
- data/sample/ssl/htdocs/index.html +10 -0
- data/sample/ssl/ssl_client.rb +22 -0
- data/sample/ssl/webrick_httpsd.rb +29 -0
- data/sample/stream.rb +21 -0
- data/sample/thread.rb +27 -0
- data/sample/wcat.rb +21 -0
- data/test/ca-chain.cert +44 -0
- data/test/ca.cert +23 -0
- data/test/client.cert +19 -0
- data/test/client.key +15 -0
- data/test/helper.rb +129 -0
- data/test/htdigest +1 -0
- data/test/htpasswd +2 -0
- data/test/runner.rb +2 -0
- data/test/server.cert +19 -0
- data/test/server.key +15 -0
- data/test/sslsvr.rb +65 -0
- data/test/subca.cert +21 -0
- data/test/test_auth.rb +321 -0
- data/test/test_cookie.rb +412 -0
- data/test/test_hexdump.rb +14 -0
- data/test/test_http-access2.rb +507 -0
- data/test/test_httpclient.rb +1801 -0
- data/test/test_include_client.rb +52 -0
- data/test/test_ssl.rb +235 -0
- metadata +102 -0
@@ -0,0 +1,507 @@
|
|
1
|
+
require 'http-access2'
|
2
|
+
require File.expand_path('helper', File.dirname(__FILE__))
|
3
|
+
|
4
|
+
|
5
|
+
module HTTPAccess2
|
6
|
+
|
7
|
+
|
8
|
+
class TestClient < Test::Unit::TestCase
|
9
|
+
include Helper
|
10
|
+
include HTTPClient::Util
|
11
|
+
|
12
|
+
def setup
|
13
|
+
super
|
14
|
+
setup_server
|
15
|
+
setup_client
|
16
|
+
end
|
17
|
+
|
18
|
+
def teardown
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_initialize
|
23
|
+
setup_proxyserver
|
24
|
+
escape_noproxy do
|
25
|
+
@proxyio.string = ""
|
26
|
+
@client = HTTPAccess2::Client.new(proxyurl)
|
27
|
+
assert_equal(urify(proxyurl), @client.proxy)
|
28
|
+
assert_equal(200, @client.head(serverurl).status)
|
29
|
+
assert(!@proxyio.string.empty?)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_agent_name
|
34
|
+
@client = HTTPAccess2::Client.new(nil, "agent_name_foo")
|
35
|
+
str = ""
|
36
|
+
@client.debug_dev = str
|
37
|
+
@client.get(serverurl)
|
38
|
+
lines = str.split(/(?:\r?\n)+/)
|
39
|
+
assert_equal("= Request", lines[0])
|
40
|
+
assert_match(/^User-Agent: agent_name_foo/, lines[4])
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_from
|
44
|
+
@client = HTTPAccess2::Client.new(nil, nil, "from_bar")
|
45
|
+
str = ""
|
46
|
+
@client.debug_dev = str
|
47
|
+
@client.get(serverurl)
|
48
|
+
lines = str.split(/(?:\r?\n)+/)
|
49
|
+
assert_equal("= Request", lines[0])
|
50
|
+
assert_match(/^From: from_bar/, lines[5])
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_debug_dev
|
54
|
+
str = ""
|
55
|
+
@client.debug_dev = str
|
56
|
+
assert(str.empty?)
|
57
|
+
@client.get(serverurl)
|
58
|
+
assert(!str.empty?)
|
59
|
+
end
|
60
|
+
|
61
|
+
def _test_protocol_version_http09
|
62
|
+
@client.protocol_version = 'HTTP/0.9'
|
63
|
+
str = ""
|
64
|
+
@client.debug_dev = str
|
65
|
+
@client.get(serverurl + 'hello')
|
66
|
+
lines = str.split(/(?:\r?\n)+/)
|
67
|
+
assert_equal("= Request", lines[0])
|
68
|
+
assert_equal("! CONNECTION ESTABLISHED", lines[2])
|
69
|
+
assert_equal("GET /hello HTTP/0.9", lines[3])
|
70
|
+
assert_equal("Connection: close", lines[5])
|
71
|
+
assert_equal("= Response", lines[6])
|
72
|
+
assert_match(/^hello/, lines[7])
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_protocol_version_http10
|
76
|
+
@client.protocol_version = 'HTTP/1.0'
|
77
|
+
str = ""
|
78
|
+
@client.debug_dev = str
|
79
|
+
@client.get(serverurl + 'hello')
|
80
|
+
lines = str.split(/(?:\r?\n)+/)
|
81
|
+
assert_equal("= Request", lines[0])
|
82
|
+
assert_equal("! CONNECTION ESTABLISHED", lines[2])
|
83
|
+
assert_equal("GET /hello HTTP/1.0", lines[3])
|
84
|
+
assert_equal("Connection: close", lines[7])
|
85
|
+
assert_equal("= Response", lines[8])
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_protocol_version_http11
|
89
|
+
str = ""
|
90
|
+
@client.debug_dev = str
|
91
|
+
@client.get(serverurl)
|
92
|
+
lines = str.split(/(?:\r?\n)+/)
|
93
|
+
assert_equal("= Request", lines[0])
|
94
|
+
assert_equal("! CONNECTION ESTABLISHED", lines[2])
|
95
|
+
assert_equal("GET / HTTP/1.1", lines[3])
|
96
|
+
assert_equal("Host: localhost:#{serverport}", lines[7])
|
97
|
+
@client.protocol_version = 'HTTP/1.1'
|
98
|
+
str = ""
|
99
|
+
@client.debug_dev = str
|
100
|
+
@client.get(serverurl)
|
101
|
+
lines = str.split(/(?:\r?\n)+/)
|
102
|
+
assert_equal("= Request", lines[0])
|
103
|
+
assert_equal("! CONNECTION ESTABLISHED", lines[2])
|
104
|
+
assert_equal("GET / HTTP/1.1", lines[3])
|
105
|
+
@client.protocol_version = 'HTTP/1.0'
|
106
|
+
str = ""
|
107
|
+
@client.debug_dev = str
|
108
|
+
@client.get(serverurl)
|
109
|
+
lines = str.split(/(?:\r?\n)+/)
|
110
|
+
assert_equal("= Request", lines[0])
|
111
|
+
assert_equal("! CONNECTION ESTABLISHED", lines[2])
|
112
|
+
assert_equal("GET / HTTP/1.0", lines[3])
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_proxy
|
116
|
+
setup_proxyserver
|
117
|
+
escape_noproxy do
|
118
|
+
begin
|
119
|
+
@client.proxy = "http://"
|
120
|
+
rescue
|
121
|
+
assert_match(/InvalidURIError/, $!.class.to_s)
|
122
|
+
end
|
123
|
+
@client.proxy = ""
|
124
|
+
assert_nil(@client.proxy)
|
125
|
+
@client.proxy = "http://foo:1234"
|
126
|
+
assert_equal(urify("http://foo:1234"), @client.proxy)
|
127
|
+
uri = urify("http://bar:2345")
|
128
|
+
@client.proxy = uri
|
129
|
+
assert_equal(uri, @client.proxy)
|
130
|
+
#
|
131
|
+
@proxyio.string = ""
|
132
|
+
@client.proxy = nil
|
133
|
+
assert_equal(200, @client.head(serverurl).status)
|
134
|
+
assert(@proxyio.string.empty?)
|
135
|
+
#
|
136
|
+
@proxyio.string = ""
|
137
|
+
@client.proxy = proxyurl
|
138
|
+
assert_equal(200, @client.head(serverurl).status)
|
139
|
+
assert(!@proxyio.string.empty?)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_noproxy_for_localhost
|
144
|
+
@proxyio.string = ""
|
145
|
+
@client.proxy = proxyurl
|
146
|
+
assert_equal(200, @client.head(serverurl).status)
|
147
|
+
assert(@proxyio.string.empty?)
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_no_proxy
|
151
|
+
setup_proxyserver
|
152
|
+
escape_noproxy do
|
153
|
+
# proxy is not set.
|
154
|
+
@client.no_proxy = 'localhost'
|
155
|
+
@proxyio.string = ""
|
156
|
+
@client.proxy = nil
|
157
|
+
assert_equal(200, @client.head(serverurl).status)
|
158
|
+
assert(/accept/ !~ @proxyio.string)
|
159
|
+
#
|
160
|
+
@proxyio.string = ""
|
161
|
+
@client.proxy = proxyurl
|
162
|
+
assert_equal(200, @client.head(serverurl).status)
|
163
|
+
assert(/accept/ !~ @proxyio.string)
|
164
|
+
#
|
165
|
+
@client.no_proxy = 'foobar'
|
166
|
+
@proxyio.string = ""
|
167
|
+
@client.proxy = proxyurl
|
168
|
+
assert_equal(200, @client.head(serverurl).status)
|
169
|
+
assert(/accept/ =~ @proxyio.string)
|
170
|
+
#
|
171
|
+
@client.no_proxy = 'foobar,localhost:baz'
|
172
|
+
@proxyio.string = ""
|
173
|
+
@client.proxy = proxyurl
|
174
|
+
assert_equal(200, @client.head(serverurl).status)
|
175
|
+
assert(/accept/ !~ @proxyio.string)
|
176
|
+
#
|
177
|
+
@client.no_proxy = 'foobar,localhost:443'
|
178
|
+
@proxyio.string = ""
|
179
|
+
@client.proxy = proxyurl
|
180
|
+
assert_equal(200, @client.head(serverurl).status)
|
181
|
+
assert(/accept/ =~ @proxyio.string)
|
182
|
+
#
|
183
|
+
@client.no_proxy = "foobar,localhost:443:localhost:#{serverport},baz"
|
184
|
+
@proxyio.string = ""
|
185
|
+
@client.proxy = proxyurl
|
186
|
+
assert_equal(200, @client.head(serverurl).status)
|
187
|
+
assert(/accept/ !~ @proxyio.string)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_get_content
|
192
|
+
assert_equal('hello', @client.get_content(serverurl + 'hello'))
|
193
|
+
assert_equal('hello', @client.get_content(serverurl + 'redirect1'))
|
194
|
+
assert_equal('hello', @client.get_content(serverurl + 'redirect2'))
|
195
|
+
assert_raises(HTTPClient::Session::BadResponse) do
|
196
|
+
@client.get_content(serverurl + 'notfound')
|
197
|
+
end
|
198
|
+
assert_raises(HTTPClient::Session::BadResponse) do
|
199
|
+
@client.get_content(serverurl + 'redirect_self')
|
200
|
+
end
|
201
|
+
called = false
|
202
|
+
@client.redirect_uri_callback = lambda { |uri, res|
|
203
|
+
newuri = res.header['location'][0]
|
204
|
+
called = true
|
205
|
+
newuri
|
206
|
+
}
|
207
|
+
assert_equal('hello', @client.get_content(serverurl + 'relative_redirect'))
|
208
|
+
assert(called)
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_post_content
|
212
|
+
assert_equal('hello', @client.post_content(serverurl + 'hello'))
|
213
|
+
assert_equal('hello', @client.post_content(serverurl + 'redirect1'))
|
214
|
+
assert_equal('hello', @client.post_content(serverurl + 'redirect2'))
|
215
|
+
assert_raises(HTTPClient::Session::BadResponse) do
|
216
|
+
@client.post_content(serverurl + 'notfound')
|
217
|
+
end
|
218
|
+
assert_raises(HTTPClient::Session::BadResponse) do
|
219
|
+
@client.post_content(serverurl + 'redirect_self')
|
220
|
+
end
|
221
|
+
called = false
|
222
|
+
@client.redirect_uri_callback = lambda { |uri, res|
|
223
|
+
newuri = res.header['location'][0]
|
224
|
+
called = true
|
225
|
+
newuri
|
226
|
+
}
|
227
|
+
assert_equal('hello', @client.post_content(serverurl + 'relative_redirect'))
|
228
|
+
assert(called)
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_head
|
232
|
+
assert_equal("head", @client.head(serverurl + 'servlet').header["x-head"][0])
|
233
|
+
param = {'1'=>'2', '3'=>'4'}
|
234
|
+
res = @client.head(serverurl + 'servlet', param)
|
235
|
+
assert_equal(param, params(res.header["x-query"][0]))
|
236
|
+
end
|
237
|
+
|
238
|
+
def test_get
|
239
|
+
assert_equal("get", @client.get(serverurl + 'servlet').content)
|
240
|
+
param = {'1'=>'2', '3'=>'4'}
|
241
|
+
res = @client.get(serverurl + 'servlet', param)
|
242
|
+
assert_equal(param, params(res.header["x-query"][0]))
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_post
|
246
|
+
assert_equal("post", @client.post(serverurl + 'servlet').content)
|
247
|
+
param = {'1'=>'2', '3'=>'4'}
|
248
|
+
res = @client.get(serverurl + 'servlet', param)
|
249
|
+
assert_equal(param, params(res.header["x-query"][0]))
|
250
|
+
end
|
251
|
+
|
252
|
+
def test_put
|
253
|
+
assert_equal("put", @client.put(serverurl + 'servlet').content)
|
254
|
+
param = {'1'=>'2', '3'=>'4'}
|
255
|
+
res = @client.get(serverurl + 'servlet', param)
|
256
|
+
assert_equal(param, params(res.header["x-query"][0]))
|
257
|
+
end
|
258
|
+
|
259
|
+
def test_delete
|
260
|
+
assert_equal("delete", @client.delete(serverurl + 'servlet').content)
|
261
|
+
param = {'1'=>'2', '3'=>'4'}
|
262
|
+
res = @client.get(serverurl + 'servlet', param)
|
263
|
+
assert_equal(param, params(res.header["x-query"][0]))
|
264
|
+
end
|
265
|
+
|
266
|
+
def test_options
|
267
|
+
assert_equal("options", @client.options(serverurl + 'servlet').content)
|
268
|
+
param = {'1'=>'2', '3'=>'4'}
|
269
|
+
res = @client.get(serverurl + 'servlet', param)
|
270
|
+
assert_equal(param, params(res.header["x-query"][0]))
|
271
|
+
end
|
272
|
+
|
273
|
+
def test_trace
|
274
|
+
assert_equal("trace", @client.trace(serverurl + 'servlet').content)
|
275
|
+
param = {'1'=>'2', '3'=>'4'}
|
276
|
+
res = @client.get(serverurl + 'servlet', param)
|
277
|
+
assert_equal(param, params(res.header["x-query"][0]))
|
278
|
+
end
|
279
|
+
|
280
|
+
def test_get_query
|
281
|
+
assert_equal({'1'=>'2'}, check_query_get({1=>2}))
|
282
|
+
assert_equal({'a'=>'A', 'B'=>'b'}, check_query_get({"a"=>"A", "B"=>"b"}))
|
283
|
+
assert_equal({'&'=>'&'}, check_query_get({"&"=>"&"}))
|
284
|
+
assert_equal({'= '=>' =+'}, check_query_get({"= "=>" =+"}))
|
285
|
+
assert_equal(
|
286
|
+
['=', '&'].sort,
|
287
|
+
check_query_get([["=", "="], ["=", "&"]])['='].to_ary.sort
|
288
|
+
)
|
289
|
+
assert_equal({'123'=>'45'}, check_query_get('123=45'))
|
290
|
+
assert_equal({'12 3'=>'45', ' '=>' '}, check_query_get('12+3=45&+=+'))
|
291
|
+
assert_equal({}, check_query_get(''))
|
292
|
+
end
|
293
|
+
|
294
|
+
def test_post_body
|
295
|
+
assert_equal({'1'=>'2'}, check_query_post({1=>2}))
|
296
|
+
assert_equal({'a'=>'A', 'B'=>'b'}, check_query_post({"a"=>"A", "B"=>"b"}))
|
297
|
+
assert_equal({'&'=>'&'}, check_query_post({"&"=>"&"}))
|
298
|
+
assert_equal({'= '=>' =+'}, check_query_post({"= "=>" =+"}))
|
299
|
+
assert_equal(
|
300
|
+
['=', '&'].sort,
|
301
|
+
check_query_post([["=", "="], ["=", "&"]])['='].to_ary.sort
|
302
|
+
)
|
303
|
+
assert_equal({'123'=>'45'}, check_query_post('123=45'))
|
304
|
+
assert_equal({'12 3'=>'45', ' '=>' '}, check_query_post('12+3=45&+=+'))
|
305
|
+
assert_equal({}, check_query_post(''))
|
306
|
+
#
|
307
|
+
post_body = StringIO.new("foo=bar&foo=baz")
|
308
|
+
assert_equal(
|
309
|
+
["bar", "baz"],
|
310
|
+
check_query_post(post_body)["foo"].to_ary.sort
|
311
|
+
)
|
312
|
+
end
|
313
|
+
|
314
|
+
def test_extra_headers
|
315
|
+
str = ""
|
316
|
+
@client.debug_dev = str
|
317
|
+
@client.head(serverurl, nil, {"ABC" => "DEF"})
|
318
|
+
lines = str.split(/(?:\r?\n)+/)
|
319
|
+
assert_equal("= Request", lines[0])
|
320
|
+
assert_match("ABC: DEF", lines[4])
|
321
|
+
#
|
322
|
+
str = ""
|
323
|
+
@client.debug_dev = str
|
324
|
+
@client.get(serverurl, nil, [["ABC", "DEF"], ["ABC", "DEF"]])
|
325
|
+
lines = str.split(/(?:\r?\n)+/)
|
326
|
+
assert_equal("= Request", lines[0])
|
327
|
+
assert_match("ABC: DEF", lines[4])
|
328
|
+
assert_match("ABC: DEF", lines[5])
|
329
|
+
end
|
330
|
+
|
331
|
+
def test_timeout
|
332
|
+
assert_equal(60, @client.connect_timeout)
|
333
|
+
assert_equal(120, @client.send_timeout)
|
334
|
+
assert_equal(60, @client.receive_timeout)
|
335
|
+
end
|
336
|
+
|
337
|
+
def test_connect_timeout
|
338
|
+
# ToDo
|
339
|
+
end
|
340
|
+
|
341
|
+
def test_send_timeout
|
342
|
+
# ToDo
|
343
|
+
end
|
344
|
+
|
345
|
+
def test_receive_timeout
|
346
|
+
# this test takes 2 sec
|
347
|
+
assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=2'))
|
348
|
+
@client.receive_timeout = 1
|
349
|
+
assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=0'))
|
350
|
+
assert_raise(HTTPClient::ReceiveTimeoutError) do
|
351
|
+
@client.get_content(serverurl + 'sleep?sec=2')
|
352
|
+
end
|
353
|
+
@client.receive_timeout = 3
|
354
|
+
assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=2'))
|
355
|
+
end
|
356
|
+
|
357
|
+
def test_cookies
|
358
|
+
cookiefile = File.join(File.dirname(File.expand_path(__FILE__)),
|
359
|
+
'test_cookies_file')
|
360
|
+
# from [ruby-talk:164079]
|
361
|
+
File.open(cookiefile, "wb") do |f|
|
362
|
+
f << "http://rubyforge.org//account/login.php session_ser LjEwMy45Ni40Ni0q%2A-fa0537de8cc31 1131676286 .rubyforge.org / 13\n"
|
363
|
+
end
|
364
|
+
cm = WebAgent::CookieManager::new(cookiefile)
|
365
|
+
cm.load_cookies
|
366
|
+
cookie = cm.cookies.first
|
367
|
+
url = cookie.url
|
368
|
+
assert(cookie.domain_match(url.host, cookie.domain))
|
369
|
+
end
|
370
|
+
|
371
|
+
private
|
372
|
+
|
373
|
+
def check_query_get(query)
|
374
|
+
WEBrick::HTTPUtils.parse_query(
|
375
|
+
@client.get(serverurl + 'servlet', query).header["x-query"][0]
|
376
|
+
)
|
377
|
+
end
|
378
|
+
|
379
|
+
def check_query_post(query)
|
380
|
+
WEBrick::HTTPUtils.parse_query(
|
381
|
+
@client.post(serverurl + 'servlet', query).header["x-query"][0]
|
382
|
+
)
|
383
|
+
end
|
384
|
+
|
385
|
+
def setup_server
|
386
|
+
@server = WEBrick::HTTPServer.new(
|
387
|
+
:BindAddress => "localhost",
|
388
|
+
:Logger => @logger,
|
389
|
+
:Port => 0,
|
390
|
+
:AccessLog => [],
|
391
|
+
:DocumentRoot => File.dirname(File.expand_path(__FILE__))
|
392
|
+
)
|
393
|
+
@serverport = @server.config[:Port]
|
394
|
+
[:hello, :sleep, :redirect1, :redirect2, :redirect3, :redirect_self, :relative_redirect].each do |sym|
|
395
|
+
@server.mount(
|
396
|
+
"/#{sym}",
|
397
|
+
WEBrick::HTTPServlet::ProcHandler.new(method("do_#{sym}").to_proc)
|
398
|
+
)
|
399
|
+
end
|
400
|
+
@server.mount('/servlet', TestServlet.new(@server))
|
401
|
+
@server_thread = start_server_thread(@server)
|
402
|
+
end
|
403
|
+
|
404
|
+
def escape_noproxy
|
405
|
+
backup = HTTPAccess2::Client::NO_PROXY_HOSTS.dup
|
406
|
+
HTTPAccess2::Client::NO_PROXY_HOSTS.clear
|
407
|
+
yield
|
408
|
+
ensure
|
409
|
+
HTTPAccess2::Client::NO_PROXY_HOSTS.replace(backup)
|
410
|
+
end
|
411
|
+
|
412
|
+
def do_hello(req, res)
|
413
|
+
res['content-type'] = 'text/html'
|
414
|
+
res.body = "hello"
|
415
|
+
end
|
416
|
+
|
417
|
+
def do_sleep(req, res)
|
418
|
+
sec = req.query['sec'].to_i
|
419
|
+
sleep sec
|
420
|
+
res['content-type'] = 'text/html'
|
421
|
+
res.body = "hello"
|
422
|
+
end
|
423
|
+
|
424
|
+
def do_redirect1(req, res)
|
425
|
+
res.set_redirect(WEBrick::HTTPStatus::MovedPermanently, serverurl + "hello")
|
426
|
+
end
|
427
|
+
|
428
|
+
def do_redirect2(req, res)
|
429
|
+
res.set_redirect(WEBrick::HTTPStatus::TemporaryRedirect, serverurl + "redirect3")
|
430
|
+
end
|
431
|
+
|
432
|
+
def do_redirect3(req, res)
|
433
|
+
res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "hello")
|
434
|
+
end
|
435
|
+
|
436
|
+
def do_redirect_self(req, res)
|
437
|
+
res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "redirect_self")
|
438
|
+
end
|
439
|
+
|
440
|
+
def do_relative_redirect(req, res)
|
441
|
+
res.set_redirect(WEBrick::HTTPStatus::Found, "hello")
|
442
|
+
end
|
443
|
+
|
444
|
+
class TestServlet < WEBrick::HTTPServlet::AbstractServlet
|
445
|
+
def get_instance(*arg)
|
446
|
+
self
|
447
|
+
end
|
448
|
+
|
449
|
+
def do_HEAD(req, res)
|
450
|
+
res["x-head"] = 'head' # use this for test purpose only.
|
451
|
+
res["x-query"] = query_response(req)
|
452
|
+
end
|
453
|
+
|
454
|
+
def do_GET(req, res)
|
455
|
+
res.body = 'get'
|
456
|
+
res["x-query"] = query_response(req)
|
457
|
+
end
|
458
|
+
|
459
|
+
def do_POST(req, res)
|
460
|
+
res.body = 'post'
|
461
|
+
res["x-query"] = body_response(req)
|
462
|
+
end
|
463
|
+
|
464
|
+
def do_PUT(req, res)
|
465
|
+
res.body = 'put'
|
466
|
+
end
|
467
|
+
|
468
|
+
def do_DELETE(req, res)
|
469
|
+
res.body = 'delete'
|
470
|
+
end
|
471
|
+
|
472
|
+
def do_OPTIONS(req, res)
|
473
|
+
# check RFC for legal response.
|
474
|
+
res.body = 'options'
|
475
|
+
end
|
476
|
+
|
477
|
+
def do_TRACE(req, res)
|
478
|
+
# client SHOULD reflect the message received back to the client as the
|
479
|
+
# entity-body of a 200 (OK) response. [RFC2616]
|
480
|
+
res.body = 'trace'
|
481
|
+
res["x-query"] = query_response(req)
|
482
|
+
end
|
483
|
+
|
484
|
+
private
|
485
|
+
|
486
|
+
def query_response(req)
|
487
|
+
query_escape(WEBrick::HTTPUtils.parse_query(req.query_string))
|
488
|
+
end
|
489
|
+
|
490
|
+
def body_response(req)
|
491
|
+
query_escape(WEBrick::HTTPUtils.parse_query(req.body))
|
492
|
+
end
|
493
|
+
|
494
|
+
def query_escape(query)
|
495
|
+
escaped = []
|
496
|
+
query.collect do |k, v|
|
497
|
+
v.to_ary.each do |ve|
|
498
|
+
escaped << CGI.escape(k) + '=' + CGI.escape(ve)
|
499
|
+
end
|
500
|
+
end
|
501
|
+
escaped.join('&')
|
502
|
+
end
|
503
|
+
end
|
504
|
+
end
|
505
|
+
|
506
|
+
|
507
|
+
end
|