em-http-request 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of em-http-request might be problematic. Click here for more details.
- data/examples/fibered-http.rb +51 -51
- data/lib/em-http/client.rb +309 -307
- data/lib/em-http/http_client_options.rb +3 -9
- data/lib/em-http/http_connection.rb +205 -199
- data/lib/em-http/version.rb +1 -1
- data/spec/client_fiber_spec.rb +10 -8
- data/spec/client_spec.rb +763 -734
- data/spec/http_proxy_spec.rb +22 -1
- data/spec/redirect_spec.rb +4 -4
- data/spec/stallion.rb +270 -273
- metadata +22 -22
data/spec/http_proxy_spec.rb
CHANGED
@@ -48,6 +48,27 @@ describe EventMachine::HttpRequest do
|
|
48
48
|
}
|
49
49
|
}
|
50
50
|
end
|
51
|
+
|
52
|
+
it "should use HTTP proxy while redirecting" do
|
53
|
+
EventMachine.run {
|
54
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect', proxy).get :redirects => 1
|
55
|
+
|
56
|
+
http.errback { failed(http) }
|
57
|
+
http.callback {
|
58
|
+
http.response_header.status.should == 200
|
59
|
+
|
60
|
+
http.response_header['X_THE_REQUESTED_URI'].should == 'http://127.0.0.1:8090/gzip'
|
61
|
+
http.response_header['X_THE_REQUESTED_URI'].should_not == '/redirect'
|
62
|
+
|
63
|
+
http.response_header["CONTENT_ENCODING"].should == "gzip"
|
64
|
+
http.response.should == "compressed"
|
65
|
+
http.last_effective_url.to_s.should == 'http://127.0.0.1:8090/gzip'
|
66
|
+
http.redirects.should == 1
|
67
|
+
|
68
|
+
EventMachine.stop
|
69
|
+
}
|
70
|
+
}
|
71
|
+
end
|
51
72
|
end
|
52
73
|
|
53
|
-
end
|
74
|
+
end
|
data/spec/redirect_spec.rb
CHANGED
@@ -42,7 +42,7 @@ describe EventMachine::HttpRequest do
|
|
42
42
|
|
43
43
|
it "should not forward cookies across domains with http redirect" do
|
44
44
|
|
45
|
-
expires = (Date.today +
|
45
|
+
expires = (Date.today + 2).strftime('%a, %d %b %Y %T GMT')
|
46
46
|
response =<<-HTTP.gsub(/^ +/, '')
|
47
47
|
HTTP/1.1 301 MOVED PERMANENTLY
|
48
48
|
Location: http://localhost:8081/
|
@@ -68,7 +68,7 @@ describe EventMachine::HttpRequest do
|
|
68
68
|
|
69
69
|
it "should forward valid cookies across domains with http redirect" do
|
70
70
|
|
71
|
-
expires = (Date.today +
|
71
|
+
expires = (Date.today + 2).strftime('%a, %d %b %Y %T GMT')
|
72
72
|
response =<<-HTTP.gsub(/^ +/, '')
|
73
73
|
HTTP/1.1 301 MOVED PERMANENTLY
|
74
74
|
Location: http://127.0.0.1:8081/
|
@@ -77,8 +77,8 @@ describe EventMachine::HttpRequest do
|
|
77
77
|
HTTP
|
78
78
|
|
79
79
|
EventMachine.run do
|
80
|
-
@stub = StubServer.new(:port => 8080, :response => response)
|
81
|
-
@echo = StubServer.new(:port => 8081, :echo => true)
|
80
|
+
@stub = StubServer.new(:host => '127.0.0.1', :port => 8080, :response => response)
|
81
|
+
@echo = StubServer.new(:host => '127.0.0.1', :port => 8081, :echo => true)
|
82
82
|
|
83
83
|
http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get :redirects => 1
|
84
84
|
|
data/spec/stallion.rb
CHANGED
@@ -1,273 +1,270 @@
|
|
1
|
-
# #--
|
2
|
-
# Includes portion originally Copyright (C)2008 Michael Fellinger
|
3
|
-
# license See file LICENSE for details
|
4
|
-
# #--
|
5
|
-
|
6
|
-
require 'rack'
|
7
|
-
|
8
|
-
module Stallion
|
9
|
-
class Mount
|
10
|
-
def initialize(name, *methods, &block)
|
11
|
-
@name, @methods, @block = name, methods, block
|
12
|
-
end
|
13
|
-
|
14
|
-
def ride
|
15
|
-
@block.call
|
16
|
-
end
|
17
|
-
|
18
|
-
def match?(request)
|
19
|
-
method = request['REQUEST_METHOD']
|
20
|
-
right_method = @methods.empty? or @methods.include?(method)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
class Stable
|
25
|
-
attr_reader :request, :response
|
26
|
-
|
27
|
-
def initialize
|
28
|
-
@boxes = {}
|
29
|
-
end
|
30
|
-
|
31
|
-
def in(path, *methods, &block)
|
32
|
-
mount = Mount.new(path, *methods, &block)
|
33
|
-
@boxes[[path, methods]] = mount
|
34
|
-
mount
|
35
|
-
end
|
36
|
-
|
37
|
-
def call(request, response)
|
38
|
-
@request, @response = request, response
|
39
|
-
@boxes.each do |(path, methods), mount|
|
40
|
-
if mount.match?(request)
|
41
|
-
mount.ride
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
STABLES = {}
|
48
|
-
|
49
|
-
def self.saddle(name = nil)
|
50
|
-
STABLES[name] = stable = Stable.new
|
51
|
-
yield stable
|
52
|
-
end
|
53
|
-
|
54
|
-
def self.run(options = {})
|
55
|
-
options = {:Host => "127.0.0.1", :Port => 8090}.merge(options)
|
56
|
-
Rack::Handler::Mongrel.run(Rack::Lint.new(self), options)
|
57
|
-
end
|
58
|
-
|
59
|
-
def self.call(env)
|
60
|
-
request = Rack::Request.new(env)
|
61
|
-
response = Rack::Response.new
|
62
|
-
|
63
|
-
STABLES.each do |name, stable|
|
64
|
-
stable.call(request, response)
|
65
|
-
end
|
66
|
-
|
67
|
-
response.finish
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
Stallion.saddle :spec do |stable|
|
72
|
-
stable.in '/' do
|
73
|
-
|
74
|
-
if stable.request.path_info == '/fail'
|
75
|
-
stable.response.status = 404
|
76
|
-
|
77
|
-
elsif stable.request.path_info == '/fail_with_nonstandard_response'
|
78
|
-
stable.response.status = 420
|
79
|
-
|
80
|
-
elsif stable.request.query_string == 'q=test'
|
81
|
-
stable.response.write 'test'
|
82
|
-
|
83
|
-
elsif stable.request.path_info == '/echo_query'
|
84
|
-
stable.response["ETag"] = "abcdefg"
|
85
|
-
stable.response["Last-Modified"] = "Fri, 13 Aug 2010 17:31:21 GMT"
|
86
|
-
stable.response.write stable.request.query_string
|
87
|
-
|
88
|
-
elsif stable.request.path_info == '/echo_content_length'
|
89
|
-
stable.response.write stable.request.content_length
|
90
|
-
|
91
|
-
elsif stable.request.path_info == '/echo_content_length_from_header'
|
92
|
-
stable.response.write "content-length:#{stable.request.env["CONTENT_LENGTH"]}"
|
93
|
-
|
94
|
-
elsif stable.request.head? && stable.request.path_info == '/'
|
95
|
-
stable.response.status = 200
|
96
|
-
|
97
|
-
elsif stable.request.delete?
|
98
|
-
stable.response.status = 200
|
99
|
-
|
100
|
-
elsif stable.request.put?
|
101
|
-
stable.response.write stable.request.body.read
|
102
|
-
|
103
|
-
elsif stable.request.post?
|
104
|
-
if stable.request.path_info == '/echo_content_type'
|
105
|
-
stable.response["Content-Type"] = stable.request.env["CONTENT_TYPE"] || 'text/html'
|
106
|
-
stable.response.write stable.request.env["CONTENT_TYPE"]
|
107
|
-
else
|
108
|
-
stable.response.write stable.request.body.read
|
109
|
-
end
|
110
|
-
|
111
|
-
elsif stable.request.path_info == '/set_cookie'
|
112
|
-
stable.response["Set-Cookie"] = "id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;"
|
113
|
-
stable.response.write "cookie set"
|
114
|
-
|
115
|
-
elsif stable.request.path_info == '/set_multiple_cookies'
|
116
|
-
stable.response["Set-Cookie"] = [
|
117
|
-
"id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;",
|
118
|
-
"id=2;"
|
119
|
-
]
|
120
|
-
stable.response.write "cookies set"
|
121
|
-
|
122
|
-
elsif stable.request.path_info == '/echo_cookie'
|
123
|
-
stable.response.write stable.request.env["HTTP_COOKIE"]
|
124
|
-
|
125
|
-
elsif stable.request.path_info == '/timeout'
|
126
|
-
sleep(10)
|
127
|
-
stable.response.write 'timeout'
|
128
|
-
|
129
|
-
elsif stable.request.path_info == '/cookie_parrot'
|
130
|
-
stable.response.status = 200
|
131
|
-
stable.response["Set-Cookie"] = stable.request.env['HTTP_COOKIE']
|
132
|
-
|
133
|
-
elsif stable.request.path_info == '/redirect'
|
134
|
-
stable.response.status = 301
|
135
|
-
stable.response["Location"] = "/gzip"
|
136
|
-
stable.response.write 'redirect'
|
137
|
-
|
138
|
-
elsif stable.request.path_info == '/redirect/multiple-with-cookie'
|
139
|
-
stable.response.status = 301
|
140
|
-
stable.response["Set-Cookie"] = "another_id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;"
|
141
|
-
stable.response["Location"] = "/redirect"
|
142
|
-
stable.response.write 'redirect'
|
143
|
-
|
144
|
-
elsif stable.request.path_info == '/redirect/bad'
|
145
|
-
stable.response.status = 301
|
146
|
-
stable.response["Location"] = "http://127.0.0.1:8090"
|
147
|
-
|
148
|
-
elsif stable.request.path_info == '/redirect/head'
|
149
|
-
stable.response.status = 301
|
150
|
-
stable.response["Location"] = "/"
|
151
|
-
|
152
|
-
elsif stable.request.path_info == '/redirect/middleware_redirects_1'
|
153
|
-
stable.response.status = 301
|
154
|
-
stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
|
155
|
-
stable.response["Location"] = "/redirect/middleware_redirects_2"
|
156
|
-
|
157
|
-
elsif stable.request.path_info == '/redirect/middleware_redirects_2'
|
158
|
-
stable.response.status = 301
|
159
|
-
stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
|
160
|
-
stable.response["Location"] = "/redirect/middleware_redirects_3"
|
161
|
-
|
162
|
-
elsif stable.request.path_info == '/redirect/middleware_redirects_3'
|
163
|
-
stable.response.status = 200
|
164
|
-
stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
|
165
|
-
|
166
|
-
elsif stable.request.path_info == '/redirect/nohost'
|
167
|
-
stable.response.status = 301
|
168
|
-
stable.response["Location"] = "http:/"
|
169
|
-
|
170
|
-
elsif stable.request.path_info == '/redirect/badhost'
|
171
|
-
stable.response.status = 301
|
172
|
-
stable.response["Location"] = "http://$$$@$!%&^"
|
173
|
-
|
174
|
-
elsif stable.request.path_info == '/gzip'
|
175
|
-
io = StringIO.new
|
176
|
-
gzip = Zlib::GzipWriter.new(io)
|
177
|
-
gzip << "compressed"
|
178
|
-
gzip.close
|
179
|
-
|
180
|
-
stable.response.write io.string
|
181
|
-
stable.response["Content-Encoding"] = "gzip"
|
182
|
-
|
183
|
-
elsif stable.request.path_info == '/deflate'
|
184
|
-
deflater = Zlib::Deflate.new(
|
185
|
-
Zlib::DEFAULT_COMPRESSION,
|
186
|
-
-Zlib::MAX_WBITS, # drop the zlib header which causes both Safari and IE to choke
|
187
|
-
Zlib::DEF_MEM_LEVEL,
|
188
|
-
Zlib::DEFAULT_STRATEGY
|
189
|
-
)
|
190
|
-
deflater.deflate("compressed")
|
191
|
-
stable.response.write deflater.finish
|
192
|
-
stable.response["Content-Encoding"] = "deflate"
|
193
|
-
|
194
|
-
elsif stable.request.env["HTTP_IF_NONE_MATCH"]
|
195
|
-
stable.response.status = 304
|
196
|
-
|
197
|
-
elsif stable.request.env["HTTP_AUTHORIZATION"]
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
stable.response
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
client.
|
256
|
-
|
257
|
-
#
|
258
|
-
session.write
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
end
|
272
|
-
|
273
|
-
sleep(1)
|
1
|
+
# #--
|
2
|
+
# Includes portion originally Copyright (C)2008 Michael Fellinger
|
3
|
+
# license See file LICENSE for details
|
4
|
+
# #--
|
5
|
+
|
6
|
+
require 'rack'
|
7
|
+
|
8
|
+
module Stallion
|
9
|
+
class Mount
|
10
|
+
def initialize(name, *methods, &block)
|
11
|
+
@name, @methods, @block = name, methods, block
|
12
|
+
end
|
13
|
+
|
14
|
+
def ride
|
15
|
+
@block.call
|
16
|
+
end
|
17
|
+
|
18
|
+
def match?(request)
|
19
|
+
method = request['REQUEST_METHOD']
|
20
|
+
right_method = @methods.empty? or @methods.include?(method)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Stable
|
25
|
+
attr_reader :request, :response
|
26
|
+
|
27
|
+
def initialize
|
28
|
+
@boxes = {}
|
29
|
+
end
|
30
|
+
|
31
|
+
def in(path, *methods, &block)
|
32
|
+
mount = Mount.new(path, *methods, &block)
|
33
|
+
@boxes[[path, methods]] = mount
|
34
|
+
mount
|
35
|
+
end
|
36
|
+
|
37
|
+
def call(request, response)
|
38
|
+
@request, @response = request, response
|
39
|
+
@boxes.each do |(path, methods), mount|
|
40
|
+
if mount.match?(request)
|
41
|
+
mount.ride
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
STABLES = {}
|
48
|
+
|
49
|
+
def self.saddle(name = nil)
|
50
|
+
STABLES[name] = stable = Stable.new
|
51
|
+
yield stable
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.run(options = {})
|
55
|
+
options = {:Host => "127.0.0.1", :Port => 8090}.merge(options)
|
56
|
+
Rack::Handler::Mongrel.run(Rack::Lint.new(self), options)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.call(env)
|
60
|
+
request = Rack::Request.new(env)
|
61
|
+
response = Rack::Response.new
|
62
|
+
|
63
|
+
STABLES.each do |name, stable|
|
64
|
+
stable.call(request, response)
|
65
|
+
end
|
66
|
+
|
67
|
+
response.finish
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
Stallion.saddle :spec do |stable|
|
72
|
+
stable.in '/' do
|
73
|
+
|
74
|
+
if stable.request.path_info == '/fail'
|
75
|
+
stable.response.status = 404
|
76
|
+
|
77
|
+
elsif stable.request.path_info == '/fail_with_nonstandard_response'
|
78
|
+
stable.response.status = 420
|
79
|
+
|
80
|
+
elsif stable.request.query_string == 'q=test'
|
81
|
+
stable.response.write 'test'
|
82
|
+
|
83
|
+
elsif stable.request.path_info == '/echo_query'
|
84
|
+
stable.response["ETag"] = "abcdefg"
|
85
|
+
stable.response["Last-Modified"] = "Fri, 13 Aug 2010 17:31:21 GMT"
|
86
|
+
stable.response.write stable.request.query_string
|
87
|
+
|
88
|
+
elsif stable.request.path_info == '/echo_content_length'
|
89
|
+
stable.response.write stable.request.content_length
|
90
|
+
|
91
|
+
elsif stable.request.path_info == '/echo_content_length_from_header'
|
92
|
+
stable.response.write "content-length:#{stable.request.env["CONTENT_LENGTH"]}"
|
93
|
+
|
94
|
+
elsif stable.request.head? && stable.request.path_info == '/'
|
95
|
+
stable.response.status = 200
|
96
|
+
|
97
|
+
elsif stable.request.delete?
|
98
|
+
stable.response.status = 200
|
99
|
+
|
100
|
+
elsif stable.request.put?
|
101
|
+
stable.response.write stable.request.body.read
|
102
|
+
|
103
|
+
elsif stable.request.post?
|
104
|
+
if stable.request.path_info == '/echo_content_type'
|
105
|
+
stable.response["Content-Type"] = stable.request.env["CONTENT_TYPE"] || 'text/html'
|
106
|
+
stable.response.write stable.request.env["CONTENT_TYPE"]
|
107
|
+
else
|
108
|
+
stable.response.write stable.request.body.read
|
109
|
+
end
|
110
|
+
|
111
|
+
elsif stable.request.path_info == '/set_cookie'
|
112
|
+
stable.response["Set-Cookie"] = "id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;"
|
113
|
+
stable.response.write "cookie set"
|
114
|
+
|
115
|
+
elsif stable.request.path_info == '/set_multiple_cookies'
|
116
|
+
stable.response["Set-Cookie"] = [
|
117
|
+
"id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;",
|
118
|
+
"id=2;"
|
119
|
+
]
|
120
|
+
stable.response.write "cookies set"
|
121
|
+
|
122
|
+
elsif stable.request.path_info == '/echo_cookie'
|
123
|
+
stable.response.write stable.request.env["HTTP_COOKIE"]
|
124
|
+
|
125
|
+
elsif stable.request.path_info == '/timeout'
|
126
|
+
sleep(10)
|
127
|
+
stable.response.write 'timeout'
|
128
|
+
|
129
|
+
elsif stable.request.path_info == '/cookie_parrot'
|
130
|
+
stable.response.status = 200
|
131
|
+
stable.response["Set-Cookie"] = stable.request.env['HTTP_COOKIE']
|
132
|
+
|
133
|
+
elsif stable.request.path_info == '/redirect'
|
134
|
+
stable.response.status = 301
|
135
|
+
stable.response["Location"] = "/gzip"
|
136
|
+
stable.response.write 'redirect'
|
137
|
+
|
138
|
+
elsif stable.request.path_info == '/redirect/multiple-with-cookie'
|
139
|
+
stable.response.status = 301
|
140
|
+
stable.response["Set-Cookie"] = "another_id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;"
|
141
|
+
stable.response["Location"] = "/redirect"
|
142
|
+
stable.response.write 'redirect'
|
143
|
+
|
144
|
+
elsif stable.request.path_info == '/redirect/bad'
|
145
|
+
stable.response.status = 301
|
146
|
+
stable.response["Location"] = "http://127.0.0.1:8090"
|
147
|
+
|
148
|
+
elsif stable.request.path_info == '/redirect/head'
|
149
|
+
stable.response.status = 301
|
150
|
+
stable.response["Location"] = "/"
|
151
|
+
|
152
|
+
elsif stable.request.path_info == '/redirect/middleware_redirects_1'
|
153
|
+
stable.response.status = 301
|
154
|
+
stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
|
155
|
+
stable.response["Location"] = "/redirect/middleware_redirects_2"
|
156
|
+
|
157
|
+
elsif stable.request.path_info == '/redirect/middleware_redirects_2'
|
158
|
+
stable.response.status = 301
|
159
|
+
stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
|
160
|
+
stable.response["Location"] = "/redirect/middleware_redirects_3"
|
161
|
+
|
162
|
+
elsif stable.request.path_info == '/redirect/middleware_redirects_3'
|
163
|
+
stable.response.status = 200
|
164
|
+
stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
|
165
|
+
|
166
|
+
elsif stable.request.path_info == '/redirect/nohost'
|
167
|
+
stable.response.status = 301
|
168
|
+
stable.response["Location"] = "http:/"
|
169
|
+
|
170
|
+
elsif stable.request.path_info == '/redirect/badhost'
|
171
|
+
stable.response.status = 301
|
172
|
+
stable.response["Location"] = "http://$$$@$!%&^"
|
173
|
+
|
174
|
+
elsif stable.request.path_info == '/gzip'
|
175
|
+
io = StringIO.new
|
176
|
+
gzip = Zlib::GzipWriter.new(io)
|
177
|
+
gzip << "compressed"
|
178
|
+
gzip.close
|
179
|
+
|
180
|
+
stable.response.write io.string
|
181
|
+
stable.response["Content-Encoding"] = "gzip"
|
182
|
+
|
183
|
+
elsif stable.request.path_info == '/deflate'
|
184
|
+
deflater = Zlib::Deflate.new(
|
185
|
+
Zlib::DEFAULT_COMPRESSION,
|
186
|
+
-Zlib::MAX_WBITS, # drop the zlib header which causes both Safari and IE to choke
|
187
|
+
Zlib::DEF_MEM_LEVEL,
|
188
|
+
Zlib::DEFAULT_STRATEGY
|
189
|
+
)
|
190
|
+
deflater.deflate("compressed")
|
191
|
+
stable.response.write deflater.finish
|
192
|
+
stable.response["Content-Encoding"] = "deflate"
|
193
|
+
|
194
|
+
elsif stable.request.env["HTTP_IF_NONE_MATCH"]
|
195
|
+
stable.response.status = 304
|
196
|
+
|
197
|
+
elsif stable.request.path_info == '/auth' && stable.request.env["HTTP_AUTHORIZATION"]
|
198
|
+
stable.response.status = 200
|
199
|
+
stable.response.write stable.request.env["HTTP_AUTHORIZATION"]
|
200
|
+
elsif stable.request.path_info == '/authtest'
|
201
|
+
auth = "Basic %s" % Base64.encode64(['user', 'pass'].join(':')).split.join
|
202
|
+
if auth == stable.request.env["HTTP_AUTHORIZATION"]
|
203
|
+
stable.response.status = 200
|
204
|
+
stable.response.write 'success'
|
205
|
+
else
|
206
|
+
stable.response.status = 401
|
207
|
+
end
|
208
|
+
elsif stable.request.path_info == '/relative-location'
|
209
|
+
stable.response.status = 301
|
210
|
+
stable.response["Location"] = '/forwarded'
|
211
|
+
|
212
|
+
elsif
|
213
|
+
stable.response.write 'Hello, World!'
|
214
|
+
end
|
215
|
+
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
Thread.new do
|
220
|
+
begin
|
221
|
+
Stallion.run :Host => '127.0.0.1', :Port => 8090
|
222
|
+
rescue Exception => e
|
223
|
+
print e
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
#
|
228
|
+
# Simple HTTP Proxy server
|
229
|
+
#
|
230
|
+
Thread.new do
|
231
|
+
server = TCPServer.new('127.0.0.1', 8083)
|
232
|
+
loop do
|
233
|
+
session = server.accept
|
234
|
+
request = ""
|
235
|
+
while (data = session.gets) != "\r\n"
|
236
|
+
request << data
|
237
|
+
end
|
238
|
+
parts = request.split("\r\n")
|
239
|
+
method, destination, http_version = parts.first.split(' ')
|
240
|
+
if destination =~ /^http:/
|
241
|
+
uri = Addressable::URI.parse(destination)
|
242
|
+
absolute_path = uri.path + (uri.query ? "?#{uri.query}" : "")
|
243
|
+
client = TCPSocket.open(uri.host, uri.port || 80)
|
244
|
+
|
245
|
+
client.write "#{method} #{absolute_path} #{http_version}\r\n"
|
246
|
+
parts[1..-1].each do |part|
|
247
|
+
client.write "#{part}\r\n"
|
248
|
+
end
|
249
|
+
|
250
|
+
client.write "\r\n"
|
251
|
+
client.flush
|
252
|
+
client.close_write
|
253
|
+
|
254
|
+
# Take the initial line from the upstream response
|
255
|
+
session.write client.gets
|
256
|
+
|
257
|
+
# What (absolute) uri was requested? Send it back in a header
|
258
|
+
session.write "X-The-Requested-URI: #{destination}\r\n"
|
259
|
+
|
260
|
+
while data = client.gets
|
261
|
+
session.write data
|
262
|
+
end
|
263
|
+
session.flush
|
264
|
+
client.close
|
265
|
+
end
|
266
|
+
session.close
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
sleep(1)
|