lib/em-net-http.rb in em-net-http-0.3.9 vs lib/em-net-http.rb in em-net-http-0.3.10
- old
+ new
@@ -124,31 +124,38 @@
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
headers = opts[:head] = {}
req.each do |k, v|
headers[k] = v
end
headers['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
- httpreq = EM::HttpRequest.new(uri).send(request_method, opts)
+ 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.response_header)
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|
- nhres.add_field(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
@@ -159,37 +166,38 @@
emres = EM::NetHTTP::Response.new(headers)
nhresclass = Net::HTTPResponse.response_class(emres.code)
nhres = nhresclass.new(emres.http_version, emres.code, emres.message)
emres.to_hash.each do |k, v|
- nhres.add_field(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(:error)}
+ 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(:error)}
+ httpreq.errback {|err|f.resume(err)}
yield_with_error_check(t0)
end
end
private
def yield_with_error_check(t0)
res = Fiber.yield
- if res == :error
- raise 'EM::HttpRequest error - request timed out' if Time.now - self.read_timeout > t0
- raise 'EM::HttpRequest error - unknown error'
- end
+ raise res.error if res.class == EM::HttpClient
res
end
end