114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/em-net-http.rb', line 114
def request(req, body = nil, &block)
return orig_net_http_request(req, body, &block) unless ::EM.reactor_running?
uri = Addressable::URI.parse("#{use_ssl? ? 'https://' : 'http://'}#{addr_port}#{req.path}")
body = body || req.body
opts = body.nil? ? {} : {:body => body}
if use_ssl?
sslopts = opts[:ssl] = {}
sslopts[:verify_peer] = verify_mode == OpenSSL::SSL::VERIFY_PEER
sslopts[:private_key_file] = key if key
sslopts[:cert_chain_file] = ca_file if ca_file
end
opts[:timeout] = self.read_timeout
opts[:decoding] = false
= opts[:head] = {}
req.each do |k, v|
[k] = v
end
['content-type'] ||= "application/x-www-form-urlencoded"
t0 = Time.now
http = EM::HttpRequest.new(uri)
request_method = (req.respond_to?(:method) ? req.method : req.class::METHOD).downcase.to_sym
request_method = :"a#{request_method}" if http.respond_to?(:"a#{request_method}")
httpreq = http.send(request_method, opts)
f=Fiber.current
convert_em_http_response = lambda do |res|
emres = EM::NetHTTP::Response.new(res.)
emres.set_body res.response
nhresclass = Net::HTTPResponse.response_class(emres.code)
nhres = nhresclass.new(emres.http_version, emres.code, emres.message)
emres.to_hash.each do |k, v|
if v.is_a?(Array)
v.each {|e| nhres.add_field(k, e)}
else
nhres.add_field(k, v)
end
end
nhres.body = emres.body if req.response_body_permitted? && nhresclass.body_permitted?
nhres.instance_variable_set '@read', true
f.resume nhres
end
if block_given?
httpreq. { ||
emres = EM::NetHTTP::Response.new()
nhresclass = Net::HTTPResponse.response_class(emres.code)
nhres = nhresclass.new(emres.http_version, emres.code, emres.message)
emres.to_hash.each do |k, v|
if v.is_a?(Array)
v.each {|e| nhres.add_field(k, e)}
else
nhres.add_field(k, v)
end
end
f.resume nhres
}
httpreq.errback {|err|f.resume(err)}
nhres = yield_with_error_check(t0)
nhres.instance_variable_set :@httpreq, httpreq
yield nhres
nhres
else
httpreq.callback &convert_em_http_response
httpreq.errback {|err|f.resume(err)}
yield_with_error_check(t0)
end
end
|