em-http-request 1.0.0 → 1.0.1

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.

Potentially problematic release.


This version of em-http-request might be problematic. Click here for more details.

@@ -1,24 +1,24 @@
1
- require 'helper'
2
-
3
- requires_connection do
4
- requires_port(8080) do
5
- describe EventMachine::HttpRequest do
6
-
7
- # ssh -D 8080 igvita
8
- let(:proxy) { {:proxy => { :host => '127.0.0.1', :port => 8080, :type => :socks5 }} }
9
-
10
- it "should use SOCKS5 proxy" do
11
- EventMachine.run {
12
- http = EventMachine::HttpRequest.new('http://jsonip.com/', proxy).get
13
-
14
- http.errback { failed(http) }
15
- http.callback {
16
- http.response_header.status.should == 200
17
- http.response.should match('72.52.131')
18
- EventMachine.stop
19
- }
20
- }
21
- end
22
- end
23
- end
24
- end
1
+ require 'helper'
2
+
3
+ requires_connection do
4
+ requires_port(8080) do
5
+ describe EventMachine::HttpRequest do
6
+
7
+ # ssh -D 8080 igvita
8
+ let(:proxy) { {:proxy => { :host => '127.0.0.1', :port => 8080, :type => :socks5 }} }
9
+
10
+ it "should use SOCKS5 proxy" do
11
+ EventMachine.run {
12
+ http = EventMachine::HttpRequest.new('http://jsonip.com/', proxy).get
13
+
14
+ http.errback { failed(http) }
15
+ http.callback {
16
+ http.response_header.status.should == 200
17
+ http.response.should match('173.230.151.99')
18
+ EventMachine.stop
19
+ }
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,270 +1,273 @@
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.head? && stable.request.path_info == '/'
92
- stable.response.status = 200
93
-
94
- elsif stable.request.delete?
95
- stable.response.status = 200
96
-
97
- elsif stable.request.put?
98
- stable.response.write stable.request.body.read
99
-
100
- elsif stable.request.post?
101
- if stable.request.path_info == '/echo_content_type'
102
- stable.response["Content-Type"] = stable.request.env["CONTENT_TYPE"] || 'text/html'
103
- stable.response.write stable.request.env["CONTENT_TYPE"]
104
- else
105
- stable.response.write stable.request.body.read
106
- end
107
-
108
- elsif stable.request.path_info == '/set_cookie'
109
- stable.response["Set-Cookie"] = "id=1; expires=Tue, 09-Aug-2011 17:53:39 GMT; path=/;"
110
- stable.response.write "cookie set"
111
-
112
- elsif stable.request.path_info == '/set_multiple_cookies'
113
- stable.response["Set-Cookie"] = [
114
- "id=1; expires=Tue, 09-Aug-2011 17:53:39 GMT; path=/;",
115
- "id=2;"
116
- ]
117
- stable.response.write "cookies set"
118
-
119
- elsif stable.request.path_info == '/echo_cookie'
120
- stable.response.write stable.request.env["HTTP_COOKIE"]
121
-
122
- elsif stable.request.path_info == '/timeout'
123
- sleep(10)
124
- stable.response.write 'timeout'
125
-
126
- elsif stable.request.path_info == '/cookie_parrot'
127
- stable.response.status = 200
128
- stable.response["Set-Cookie"] = stable.request.env['HTTP_COOKIE']
129
-
130
- elsif stable.request.path_info == '/redirect'
131
- stable.response.status = 301
132
- stable.response["Location"] = "/gzip"
133
- stable.response.write 'redirect'
134
-
135
- elsif stable.request.path_info == '/redirect/multiple-with-cookie'
136
- stable.response.status = 301
137
- stable.response["Set-Cookie"] = "another_id=1; expires=Tue, 09-Aug-2011 17:53:39 GMT; path=/;"
138
- stable.response["Location"] = "/redirect"
139
- stable.response.write 'redirect'
140
-
141
- elsif stable.request.path_info == '/redirect/bad'
142
- stable.response.status = 301
143
- stable.response["Location"] = "http://127.0.0.1:8090"
144
-
145
- elsif stable.request.path_info == '/redirect/head'
146
- stable.response.status = 301
147
- stable.response["Location"] = "/"
148
-
149
- elsif stable.request.path_info == '/redirect/middleware_redirects_1'
150
- stable.response.status = 301
151
- stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
152
- stable.response["Location"] = "/redirect/middleware_redirects_2"
153
-
154
- elsif stable.request.path_info == '/redirect/middleware_redirects_2'
155
- stable.response.status = 301
156
- stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
157
- stable.response["Location"] = "/redirect/middleware_redirects_3"
158
-
159
- elsif stable.request.path_info == '/redirect/middleware_redirects_3'
160
- stable.response.status = 200
161
- stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
162
-
163
- elsif stable.request.path_info == '/redirect/nohost'
164
- stable.response.status = 301
165
- stable.response["Location"] = "http:/"
166
-
167
- elsif stable.request.path_info == '/redirect/badhost'
168
- stable.response.status = 301
169
- stable.response["Location"] = "http://$$$@$!%&^"
170
-
171
- elsif stable.request.path_info == '/gzip'
172
- io = StringIO.new
173
- gzip = Zlib::GzipWriter.new(io)
174
- gzip << "compressed"
175
- gzip.close
176
-
177
- stable.response.write io.string
178
- stable.response["Content-Encoding"] = "gzip"
179
-
180
- elsif stable.request.path_info == '/deflate'
181
- deflater = Zlib::Deflate.new(
182
- Zlib::DEFAULT_COMPRESSION,
183
- -Zlib::MAX_WBITS, # drop the zlib header which causes both Safari and IE to choke
184
- Zlib::DEF_MEM_LEVEL,
185
- Zlib::DEFAULT_STRATEGY
186
- )
187
- deflater.deflate("compressed")
188
- stable.response.write deflater.finish
189
- stable.response["Content-Encoding"] = "deflate"
190
-
191
- elsif stable.request.env["HTTP_IF_NONE_MATCH"]
192
- stable.response.status = 304
193
-
194
- elsif stable.request.env["HTTP_AUTHORIZATION"]
195
- if stable.request.path_info == '/auth'
196
- stable.response.status = 200
197
- stable.response.write stable.request.env["HTTP_AUTHORIZATION"]
198
- else
199
- auth = "Basic %s" % Base64.encode64(['user', 'pass'].join(':')).split.join
200
-
201
- if auth == stable.request.env["HTTP_AUTHORIZATION"]
202
- stable.response.status = 200
203
- stable.response.write 'success'
204
- else
205
- stable.response.status = 401
206
- end
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)
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
+ if stable.request.path_info == '/auth'
199
+ stable.response.status = 200
200
+ stable.response.write stable.request.env["HTTP_AUTHORIZATION"]
201
+ else
202
+ auth = "Basic %s" % Base64.encode64(['user', 'pass'].join(':')).split.join
203
+
204
+ if auth == stable.request.env["HTTP_AUTHORIZATION"]
205
+ stable.response.status = 200
206
+ stable.response.write 'success'
207
+ else
208
+ stable.response.status = 401
209
+ end
210
+ end
211
+ elsif stable.request.path_info == '/relative-location'
212
+ stable.response.status = 301
213
+ stable.response["Location"] = '/forwarded'
214
+
215
+ elsif
216
+ stable.response.write 'Hello, World!'
217
+ end
218
+
219
+ end
220
+ end
221
+
222
+ Thread.new do
223
+ begin
224
+ Stallion.run :Host => '127.0.0.1', :Port => 8090
225
+ rescue Exception => e
226
+ print e
227
+ end
228
+ end
229
+
230
+ #
231
+ # Simple HTTP Proxy server
232
+ #
233
+ Thread.new do
234
+ server = TCPServer.new('127.0.0.1', 8083)
235
+ loop do
236
+ session = server.accept
237
+ request = ""
238
+ while (data = session.gets) != "\r\n"
239
+ request << data
240
+ end
241
+ parts = request.split("\r\n")
242
+ method, destination, http_version = parts.first.split(' ')
243
+ if destination =~ /^http:/
244
+ uri = Addressable::URI.parse(destination)
245
+ absolute_path = uri.path + (uri.query ? "?#{uri.query}" : "")
246
+ client = TCPSocket.open(uri.host, uri.port || 80)
247
+
248
+ client.write "#{method} #{absolute_path} #{http_version}\r\n"
249
+ parts[1..-1].each do |part|
250
+ client.write "#{part}\r\n"
251
+ end
252
+
253
+ client.write "\r\n"
254
+ client.flush
255
+ client.close_write
256
+
257
+ # Take the initial line from the upstream response
258
+ session.write client.gets
259
+
260
+ # What (absolute) uri was requested? Send it back in a header
261
+ session.write "X-The-Requested-URI: #{destination}\r\n"
262
+
263
+ while data = client.gets
264
+ session.write data
265
+ end
266
+ session.flush
267
+ client.close
268
+ end
269
+ session.close
270
+ end
271
+ end
272
+
273
+ sleep(1)