def call(env)
super
request = create_request(env)
http_method = env[:method].to_s.downcase.to_sym
if env[:parallel_manager]
env[:parallel_manager].add(request, http_method, request_config(env)) do |resp|
save_response(env, resp.response_header.status, resp.response) do |resp_headers|
resp.response_header.each do |name, value|
resp_headers[name.to_sym] = value
end
end
env[:response].finish(env)
end
else
client = nil
block = lambda { request.send(http_method, request_config(env)) }
if !EM.reactor_running?
EM.run do
Fiber.new {
client = block.call
EM.stop
}.resume
end
else
client = block.call
end
raise client.error if client.error
status = client.response_header.status
reason = client.response_header.http_reason
save_response(env, status, client.response, nil, reason) do |resp_headers|
client.response_header.each do |name, value|
resp_headers[name.to_sym] = value
end
end
end
@app.call env
rescue Errno::ECONNREFUSED
raise Error::ConnectionFailed, $!
rescue EventMachine::Connectify::CONNECTError => err
if err.message.include?("Proxy Authentication Required")
raise Error::ConnectionFailed, %Q{407 "Proxy Authentication Required "}
else
raise Error::ConnectionFailed, err
end
rescue Errno::ETIMEDOUT => err
raise Error::TimeoutError, err
rescue RuntimeError => err
if err.message == "connection closed by server"
raise Error::ConnectionFailed, err
else
raise
end
rescue => err
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
raise Faraday::SSLError, err
else
raise
end
end