em-http-promise 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/em-http-promise.gemspec +30 -0
- data/lib/em-http-promise.rb +5 -0
- data/lib/em-http-promise/promising_http_request.rb +23 -0
- data/lib/em-http-promise/version.rb +3 -0
- data/spec/helper.rb +29 -0
- data/spec/promising_client_spec.rb +85 -0
- data/spec/stallion.rb +298 -0
- data/spec/stub_server.rb +42 -0
- metadata +173 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 48f941a9eae44f79e0507c41bd078f952a0877eb
|
4
|
+
data.tar.gz: a0e113c6977fb68c2c2350507c1028bf692da07e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f61aefb1a52f67b57c699f6dc8a4ab5e6b3e65cf33af94ad4d165b53a1545d659989bbcc71c3f4c7225d47525dc3f01e345113aae668482cebac5a8016e0dbfc
|
7
|
+
data.tar.gz: 90f3bbaf7fe8c56075065ac500f05fa8f2843118e09821407d165e3729e23bcdaf56da91845772bb42cf42ec8036b8186bdca62df7d4c501373b7bf502191613
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Joel Jackson
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Em::Http::Promise
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'em-http-promise'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install em-http-promise
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require 'em-http-promise/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "em-http-promise"
|
7
|
+
spec.version = EMHttpPromise::VERSION
|
8
|
+
spec.authors = ["Joel Jackson"]
|
9
|
+
spec.email = ["jackson.joel@gmail.com"]
|
10
|
+
spec.description = %q{A gem to wrap EM-HTTP-Request in the loving embrace of promises.}
|
11
|
+
spec.summary = spec.description
|
12
|
+
spec.homepage = "https://github.com/joeljackson/em-http-promise"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split("\n")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "em-http-request"
|
21
|
+
spec.add_dependency "em-promise", "~>1.1.1"
|
22
|
+
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rack"
|
26
|
+
spec.add_development_dependency "mongrel", "~> 1.2.0.pre2"
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
29
|
+
spec.add_development_dependency "rake"
|
30
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module PromisingHttpRequest
|
2
|
+
def then(success_back = nil, error_back = nil, &blk)
|
3
|
+
deferred = EM::Q.defer
|
4
|
+
|
5
|
+
self.callback {
|
6
|
+
if self.response_header.status == 200
|
7
|
+
deferred.resolve(self)
|
8
|
+
else
|
9
|
+
deferred.reject(self)
|
10
|
+
end
|
11
|
+
}
|
12
|
+
self.errback {
|
13
|
+
deferred.reject("Connection failure")
|
14
|
+
}
|
15
|
+
deferred.promise.then(success_back, error_back, &blk)
|
16
|
+
end
|
17
|
+
|
18
|
+
def is_a?(klass)
|
19
|
+
return true if klass == EventMachine::Q::Promise && ! self.finished?
|
20
|
+
super(klass)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
EM::HttpClient.send(:include, PromisingHttpRequest)
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#Note, grabbed this straight from em-http-request
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler/setup'
|
4
|
+
|
5
|
+
require 'em-http-request'
|
6
|
+
|
7
|
+
require 'stallion'
|
8
|
+
require 'stub_server'
|
9
|
+
|
10
|
+
def failed(http = nil)
|
11
|
+
EventMachine.stop
|
12
|
+
http ? fail(http.error) : fail
|
13
|
+
end
|
14
|
+
|
15
|
+
def requires_connection(&blk)
|
16
|
+
blk.call if system('ping -t1 -c1 google.com 2>&1 > /dev/null')
|
17
|
+
end
|
18
|
+
|
19
|
+
def requires_port(port, &blk)
|
20
|
+
port_open = true
|
21
|
+
begin
|
22
|
+
s = TCPSocket.new('localhost', port)
|
23
|
+
s.close()
|
24
|
+
rescue
|
25
|
+
port_open = false
|
26
|
+
end
|
27
|
+
|
28
|
+
blk.call if port_open
|
29
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'em-http-promise'
|
3
|
+
|
4
|
+
describe EventMachine::HttpRequest do
|
5
|
+
def failed(http=nil)
|
6
|
+
EventMachine.stop
|
7
|
+
http ? fail(http.error) : fail
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should respond to then" do
|
11
|
+
EventMachine.run {
|
12
|
+
EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get.should respond_to(:then)
|
13
|
+
EventMachine.stop
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should call the success callback with a block" do
|
18
|
+
stub = double("stub")
|
19
|
+
stub.stub(:called_back)
|
20
|
+
stub.should_receive(:called_back)
|
21
|
+
EventMachine.run {
|
22
|
+
EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get.then { |response|
|
23
|
+
response.response_header.status.should == 200
|
24
|
+
stub.called_back
|
25
|
+
EventMachine.stop
|
26
|
+
}
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should call the success callback with a lambda" do
|
31
|
+
stub = double("stub")
|
32
|
+
stub.stub(:called_back)
|
33
|
+
stub.should_receive(:called_back)
|
34
|
+
EventMachine.run {
|
35
|
+
EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get.then(
|
36
|
+
->(response) {
|
37
|
+
response.response_header.status.should == 200
|
38
|
+
stub.called_back
|
39
|
+
EventMachine.stop
|
40
|
+
}
|
41
|
+
)
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should pass the promise to the next promise handler" do
|
46
|
+
stub = double("stub")
|
47
|
+
stub.stub(:called_back)
|
48
|
+
stub.should_receive(:called_back).twice
|
49
|
+
EventMachine.run {
|
50
|
+
EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get.then(
|
51
|
+
->(response) {
|
52
|
+
response.response_header.status.should == 200
|
53
|
+
stub.called_back
|
54
|
+
EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get
|
55
|
+
}
|
56
|
+
).then(
|
57
|
+
->(response) {
|
58
|
+
response.response_header.status.should == 200
|
59
|
+
stub.called_back
|
60
|
+
EventMachine.stop
|
61
|
+
}
|
62
|
+
)
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should pass failures through to the final error handler" do
|
67
|
+
stub = double("stub")
|
68
|
+
stub.stub(:called_back)
|
69
|
+
stub.should_receive(:called_back)
|
70
|
+
EventMachine.run {
|
71
|
+
EventMachine::HttpRequest.new('http://127.0.0.1:8090/fail').get.then {
|
72
|
+
EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get
|
73
|
+
}.then(
|
74
|
+
->(response) {
|
75
|
+
EventMachine.stop
|
76
|
+
},
|
77
|
+
->(response) {
|
78
|
+
stub.called_back
|
79
|
+
EventMachine.stop
|
80
|
+
}
|
81
|
+
)
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
data/spec/stallion.rb
ADDED
@@ -0,0 +1,298 @@
|
|
1
|
+
# #--
|
2
|
+
# Includes portion originally Copyright (C)2008 Michael Fellinger
|
3
|
+
# license See file LICENSE for details
|
4
|
+
# #--
|
5
|
+
#Straight from em-http-request
|
6
|
+
|
7
|
+
require 'rack'
|
8
|
+
|
9
|
+
module Stallion
|
10
|
+
class Mount
|
11
|
+
def initialize(name, *methods, &block)
|
12
|
+
@name, @methods, @block = name, methods, block
|
13
|
+
end
|
14
|
+
|
15
|
+
def ride
|
16
|
+
@block.call
|
17
|
+
end
|
18
|
+
|
19
|
+
def match?(request)
|
20
|
+
method = request['REQUEST_METHOD']
|
21
|
+
right_method = @methods.empty? or @methods.include?(method)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Stable
|
26
|
+
attr_reader :request, :response
|
27
|
+
|
28
|
+
def initialize
|
29
|
+
@boxes = {}
|
30
|
+
end
|
31
|
+
|
32
|
+
def in(path, *methods, &block)
|
33
|
+
mount = Mount.new(path, *methods, &block)
|
34
|
+
@boxes[[path, methods]] = mount
|
35
|
+
mount
|
36
|
+
end
|
37
|
+
|
38
|
+
def call(request, response)
|
39
|
+
@request, @response = request, response
|
40
|
+
@boxes.each do |(path, methods), mount|
|
41
|
+
if mount.match?(request)
|
42
|
+
mount.ride
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
STABLES = {}
|
49
|
+
|
50
|
+
def self.saddle(name = nil)
|
51
|
+
STABLES[name] = stable = Stable.new
|
52
|
+
yield stable
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.run(options = {})
|
56
|
+
options = {:Host => "127.0.0.1", :Port => 8090}.merge(options)
|
57
|
+
Rack::Handler::Mongrel.run(Rack::Lint.new(self), options)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.call(env)
|
61
|
+
request = Rack::Request.new(env)
|
62
|
+
response = Rack::Response.new
|
63
|
+
|
64
|
+
STABLES.each do |name, stable|
|
65
|
+
stable.call(request, response)
|
66
|
+
end
|
67
|
+
|
68
|
+
response.finish
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
Stallion.saddle :spec do |stable|
|
73
|
+
stable.in '/' do
|
74
|
+
|
75
|
+
if stable.request.path_info == '/fail'
|
76
|
+
stable.response.status = 404
|
77
|
+
|
78
|
+
elsif stable.request.path_info == '/fail_with_nonstandard_response'
|
79
|
+
stable.response.status = 420
|
80
|
+
|
81
|
+
elsif stable.request.query_string == 'q=test'
|
82
|
+
stable.response.write 'test'
|
83
|
+
|
84
|
+
elsif stable.request.path_info == '/echo_query'
|
85
|
+
stable.response["ETag"] = "abcdefg"
|
86
|
+
stable.response["Last-Modified"] = "Fri, 13 Aug 2010 17:31:21 GMT"
|
87
|
+
stable.response.write stable.request.query_string
|
88
|
+
|
89
|
+
elsif stable.request.path_info == '/echo_headers'
|
90
|
+
stable.response["Set-Cookie"] = "test=yes"
|
91
|
+
stable.response["X-Forward-Host"] = "proxy.local"
|
92
|
+
stable.response.write stable.request.query_string
|
93
|
+
|
94
|
+
elsif stable.request.path_info == '/echo_content_length'
|
95
|
+
stable.response.write stable.request.content_length
|
96
|
+
|
97
|
+
elsif stable.request.path_info == '/echo_content_length_from_header'
|
98
|
+
stable.response.write "content-length:#{stable.request.env["CONTENT_LENGTH"]}"
|
99
|
+
|
100
|
+
elsif stable.request.head? && stable.request.path_info == '/'
|
101
|
+
stable.response.status = 200
|
102
|
+
|
103
|
+
elsif stable.request.delete?
|
104
|
+
stable.response.status = 200
|
105
|
+
|
106
|
+
elsif stable.request.put?
|
107
|
+
stable.response.write stable.request.body.read
|
108
|
+
|
109
|
+
elsif stable.request.post? || stable.request.patch?
|
110
|
+
if stable.request.path_info == '/echo_content_type'
|
111
|
+
stable.response["Content-Type"] = stable.request.env["CONTENT_TYPE"] || 'text/html'
|
112
|
+
stable.response.write stable.request.env["CONTENT_TYPE"]
|
113
|
+
else
|
114
|
+
stable.response.write stable.request.body.read
|
115
|
+
end
|
116
|
+
|
117
|
+
elsif stable.request.path_info == '/set_cookie'
|
118
|
+
stable.response["Set-Cookie"] = "id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;"
|
119
|
+
stable.response.write "cookie set"
|
120
|
+
|
121
|
+
elsif stable.request.path_info == '/set_multiple_cookies'
|
122
|
+
stable.response["Set-Cookie"] = [
|
123
|
+
"id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;",
|
124
|
+
"id=2;"
|
125
|
+
]
|
126
|
+
stable.response.write "cookies set"
|
127
|
+
|
128
|
+
elsif stable.request.path_info == '/echo_cookie'
|
129
|
+
stable.response.write stable.request.env["HTTP_COOKIE"]
|
130
|
+
|
131
|
+
elsif stable.request.path_info == '/timeout'
|
132
|
+
sleep(10)
|
133
|
+
stable.response.write 'timeout'
|
134
|
+
|
135
|
+
elsif stable.request.path_info == '/cookie_parrot'
|
136
|
+
stable.response.status = 200
|
137
|
+
stable.response["Set-Cookie"] = stable.request.env['HTTP_COOKIE']
|
138
|
+
|
139
|
+
elsif stable.request.path_info == '/redirect'
|
140
|
+
stable.response.status = 301
|
141
|
+
stable.response["Location"] = "/gzip"
|
142
|
+
stable.response.write 'redirect'
|
143
|
+
|
144
|
+
elsif stable.request.path_info == '/redirect/created'
|
145
|
+
stable.response.status = 201
|
146
|
+
stable.response["Location"] = "/"
|
147
|
+
stable.response.write 'Hello, World!'
|
148
|
+
|
149
|
+
elsif stable.request.path_info == '/redirect/multiple-with-cookie'
|
150
|
+
stable.response.status = 301
|
151
|
+
stable.response["Set-Cookie"] = "another_id=1; expires=Sat, 09 Aug 2031 17:53:39 GMT; path=/;"
|
152
|
+
stable.response["Location"] = "/redirect"
|
153
|
+
stable.response.write 'redirect'
|
154
|
+
|
155
|
+
elsif stable.request.path_info == '/redirect/bad'
|
156
|
+
stable.response.status = 301
|
157
|
+
stable.response["Location"] = "http://127.0.0.1:8090"
|
158
|
+
|
159
|
+
elsif stable.request.path_info == '/redirect/timeout'
|
160
|
+
stable.response.status = 301
|
161
|
+
stable.response["Location"] = "http://127.0.0.1:8090/timeout"
|
162
|
+
|
163
|
+
elsif stable.request.path_info == '/redirect/head'
|
164
|
+
stable.response.status = 301
|
165
|
+
stable.response["Location"] = "/"
|
166
|
+
|
167
|
+
elsif stable.request.path_info == '/redirect/middleware_redirects_1'
|
168
|
+
stable.response.status = 301
|
169
|
+
stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
|
170
|
+
stable.response["Location"] = "/redirect/middleware_redirects_2"
|
171
|
+
|
172
|
+
elsif stable.request.path_info == '/redirect/middleware_redirects_2'
|
173
|
+
stable.response.status = 301
|
174
|
+
stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
|
175
|
+
stable.response["Location"] = "/redirect/middleware_redirects_3"
|
176
|
+
|
177
|
+
elsif stable.request.path_info == '/redirect/middleware_redirects_3'
|
178
|
+
stable.response.status = 200
|
179
|
+
stable.response["EM-Middleware"] = stable.request.env["HTTP_EM_MIDDLEWARE"]
|
180
|
+
|
181
|
+
elsif stable.request.path_info == '/redirect/nohost'
|
182
|
+
stable.response.status = 301
|
183
|
+
stable.response["Location"] = "http:/"
|
184
|
+
|
185
|
+
elsif stable.request.path_info == '/redirect/badhost'
|
186
|
+
stable.response.status = 301
|
187
|
+
stable.response["Location"] = "http://$$$@$!%&^"
|
188
|
+
|
189
|
+
elsif stable.request.path_info == '/gzip'
|
190
|
+
io = StringIO.new
|
191
|
+
gzip = Zlib::GzipWriter.new(io)
|
192
|
+
gzip << "compressed"
|
193
|
+
gzip.close
|
194
|
+
|
195
|
+
stable.response.write io.string
|
196
|
+
stable.response["Content-Encoding"] = "gzip"
|
197
|
+
|
198
|
+
elsif stable.request.path_info == '/gzip-large'
|
199
|
+
contents = File.open(File.dirname(__FILE__) + "/fixtures/gzip-sample.gz", 'r') { |f| f.read }
|
200
|
+
|
201
|
+
stable.response.write contents
|
202
|
+
stable.response["Content-Encoding"] = "gzip"
|
203
|
+
|
204
|
+
elsif stable.request.path_info == '/deflate'
|
205
|
+
deflater = Zlib::Deflate.new(
|
206
|
+
Zlib::DEFAULT_COMPRESSION,
|
207
|
+
-Zlib::MAX_WBITS, # drop the zlib header which causes both Safari and IE to choke
|
208
|
+
Zlib::DEF_MEM_LEVEL,
|
209
|
+
Zlib::DEFAULT_STRATEGY
|
210
|
+
)
|
211
|
+
deflater.deflate("compressed")
|
212
|
+
stable.response.write deflater.finish
|
213
|
+
stable.response["Content-Encoding"] = "deflate"
|
214
|
+
|
215
|
+
elsif stable.request.env["HTTP_IF_NONE_MATCH"]
|
216
|
+
stable.response.status = 304
|
217
|
+
|
218
|
+
elsif stable.request.path_info == '/auth' && stable.request.env["HTTP_AUTHORIZATION"]
|
219
|
+
stable.response.status = 200
|
220
|
+
stable.response.write stable.request.env["HTTP_AUTHORIZATION"]
|
221
|
+
elsif stable.request.path_info == '/authtest'
|
222
|
+
auth = "Basic %s" % Base64.encode64(['user', 'pass'].join(':')).split.join
|
223
|
+
if auth == stable.request.env["HTTP_AUTHORIZATION"]
|
224
|
+
stable.response.status = 200
|
225
|
+
stable.response.write 'success'
|
226
|
+
else
|
227
|
+
stable.response.status = 401
|
228
|
+
end
|
229
|
+
elsif stable.request.path_info == '/relative-location'
|
230
|
+
stable.response.status = 301
|
231
|
+
stable.response["Location"] = '/forwarded'
|
232
|
+
elsif stable.request.path_info == '/echo-user-agent'
|
233
|
+
stable.response.write stable.request.env["HTTP_USER_AGENT"].inspect
|
234
|
+
|
235
|
+
elsif
|
236
|
+
stable.response.write 'Hello, World!'
|
237
|
+
end
|
238
|
+
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
Thread.new do
|
243
|
+
begin
|
244
|
+
Stallion.run :Host => '127.0.0.1', :Port => 8090
|
245
|
+
rescue Exception => e
|
246
|
+
print e
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
#
|
251
|
+
# Simple HTTP Proxy server
|
252
|
+
#
|
253
|
+
Thread.new do
|
254
|
+
server = TCPServer.new('127.0.0.1', 8083)
|
255
|
+
loop do
|
256
|
+
session = server.accept
|
257
|
+
request = ""
|
258
|
+
while (data = session.gets) != "\r\n"
|
259
|
+
request << data
|
260
|
+
end
|
261
|
+
parts = request.split("\r\n")
|
262
|
+
method, destination, http_version = parts.first.split(' ')
|
263
|
+
proxy = parts.find { |part| part =~ /Proxy-Authorization/ }
|
264
|
+
if destination =~ /^http:/
|
265
|
+
uri = Addressable::URI.parse(destination)
|
266
|
+
absolute_path = uri.path + (uri.query ? "?#{uri.query}" : "")
|
267
|
+
client = TCPSocket.open(uri.host, uri.port || 80)
|
268
|
+
|
269
|
+
client.write "#{method} #{absolute_path} #{http_version}\r\n"
|
270
|
+
parts[1..-1].each do |part|
|
271
|
+
client.write "#{part}\r\n"
|
272
|
+
end
|
273
|
+
|
274
|
+
client.write "\r\n"
|
275
|
+
client.flush
|
276
|
+
client.close_write
|
277
|
+
|
278
|
+
# Take the initial line from the upstream response
|
279
|
+
session.write client.gets
|
280
|
+
|
281
|
+
if proxy
|
282
|
+
session.write "X-Proxy-Auth: #{proxy}\r\n"
|
283
|
+
end
|
284
|
+
|
285
|
+
# What (absolute) uri was requested? Send it back in a header
|
286
|
+
session.write "X-The-Requested-URI: #{destination}\r\n"
|
287
|
+
|
288
|
+
while data = client.gets
|
289
|
+
session.write data
|
290
|
+
end
|
291
|
+
session.flush
|
292
|
+
client.close
|
293
|
+
end
|
294
|
+
session.close
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
sleep(1)
|
data/spec/stub_server.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
class StubServer
|
2
|
+
module Server
|
3
|
+
def receive_data(data)
|
4
|
+
if echo?
|
5
|
+
send_data("HTTP/1.0 200 OK\r\nContent-Length: #{data.bytesize}\r\nContent-Type: text/plain\r\n\r\n")
|
6
|
+
send_data(data)
|
7
|
+
else
|
8
|
+
send_data @response
|
9
|
+
end
|
10
|
+
|
11
|
+
close_connection_after_writing
|
12
|
+
end
|
13
|
+
|
14
|
+
def echo= flag
|
15
|
+
@echo = flag
|
16
|
+
end
|
17
|
+
|
18
|
+
def echo?
|
19
|
+
!!@echo
|
20
|
+
end
|
21
|
+
|
22
|
+
def response=(response)
|
23
|
+
@response = response
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize options = {}
|
28
|
+
options = {:response => options} if options.kind_of?(String)
|
29
|
+
options = {:port => 8081, :host => '127.0.0.1'}.merge(options)
|
30
|
+
|
31
|
+
host = options[:host]
|
32
|
+
port = options[:port]
|
33
|
+
@sig = EventMachine::start_server(host, port, Server) do |server|
|
34
|
+
server.response = options[:response]
|
35
|
+
server.echo = options[:echo]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def stop
|
40
|
+
EventMachine.stop_server @sig
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em-http-promise
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joel Jackson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: em-http-request
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: em-promise
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.1.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: mongrel
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.2.0.pre2
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.2.0.pre2
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: bundler
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ~>
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.3'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ~>
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.3'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rake
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: A gem to wrap EM-HTTP-Request in the loving embrace of promises.
|
126
|
+
email:
|
127
|
+
- jackson.joel@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- .gitignore
|
133
|
+
- Gemfile
|
134
|
+
- LICENSE.txt
|
135
|
+
- README.md
|
136
|
+
- Rakefile
|
137
|
+
- em-http-promise.gemspec
|
138
|
+
- lib/em-http-promise.rb
|
139
|
+
- lib/em-http-promise/promising_http_request.rb
|
140
|
+
- lib/em-http-promise/version.rb
|
141
|
+
- spec/helper.rb
|
142
|
+
- spec/promising_client_spec.rb
|
143
|
+
- spec/stallion.rb
|
144
|
+
- spec/stub_server.rb
|
145
|
+
homepage: https://github.com/joeljackson/em-http-promise
|
146
|
+
licenses:
|
147
|
+
- MIT
|
148
|
+
metadata: {}
|
149
|
+
post_install_message:
|
150
|
+
rdoc_options: []
|
151
|
+
require_paths:
|
152
|
+
- lib
|
153
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - '>='
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - '>='
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
requirements: []
|
164
|
+
rubyforge_project:
|
165
|
+
rubygems_version: 2.0.3
|
166
|
+
signing_key:
|
167
|
+
specification_version: 4
|
168
|
+
summary: A gem to wrap EM-HTTP-Request in the loving embrace of promises.
|
169
|
+
test_files:
|
170
|
+
- spec/helper.rb
|
171
|
+
- spec/promising_client_spec.rb
|
172
|
+
- spec/stallion.rb
|
173
|
+
- spec/stub_server.rb
|