mirror of
https://github.com/lostisland/faraday.git
synced 2025-12-04 00:03:34 -05:00
Merge pull request #452 from Ben-M/net_http_with_connection
NetHttp uses with_net_http_connection (#446)
This commit is contained in:
commit
626c7f1cf7
@ -1,5 +1,9 @@
|
||||
# Faraday Changelog
|
||||
|
||||
## v0.9.1
|
||||
|
||||
* Refactor Net:HTTP adapter so that with_net_http_connection can be overridden to allow pooled connections. (@Ben-M)
|
||||
|
||||
## v0.9.0
|
||||
|
||||
* Add HTTPClient adapter (@hakanensari)
|
||||
|
||||
@ -28,7 +28,7 @@ We will accept adapters that:
|
||||
2. if they have features not present in included adapters.
|
||||
|
||||
We are pushing towards a 1.0 release, when we will have to follow [Semantic
|
||||
Versioning][semver]. If your patch includes changes to break compatiblitity,
|
||||
Versioning][semver]. If your patch includes changes to break compatibility,
|
||||
note that so we can add it to the [Changelog][].
|
||||
|
||||
[semver]: http://semver.org/
|
||||
|
||||
@ -14,7 +14,7 @@ require 'forwardable'
|
||||
# conn.get '/'
|
||||
#
|
||||
module Faraday
|
||||
VERSION = "0.9.0"
|
||||
VERSION = "0.9.1"
|
||||
|
||||
class << self
|
||||
# Public: Gets or sets the root path that Faraday is being loaded from.
|
||||
|
||||
@ -29,26 +29,27 @@ module Faraday
|
||||
|
||||
def call(env)
|
||||
super
|
||||
http = net_http_connection(env)
|
||||
configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
|
||||
with_net_http_connection(env) do |http|
|
||||
configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
|
||||
|
||||
req = env[:request]
|
||||
http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
|
||||
http.open_timeout = req[:open_timeout] if req[:open_timeout]
|
||||
req = env[:request]
|
||||
http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
|
||||
http.open_timeout = req[:open_timeout] if req[:open_timeout]
|
||||
|
||||
begin
|
||||
http_response = perform_request(http, env)
|
||||
rescue *NET_HTTP_EXCEPTIONS => err
|
||||
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
|
||||
raise Faraday::SSLError, err
|
||||
else
|
||||
raise Error::ConnectionFailed, err
|
||||
begin
|
||||
http_response = perform_request(http, env)
|
||||
rescue *NET_HTTP_EXCEPTIONS => err
|
||||
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
|
||||
raise Faraday::SSLError, err
|
||||
else
|
||||
raise Error::ConnectionFailed, err
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
save_response(env, http_response.code.to_i, http_response.body || '') do |response_headers|
|
||||
http_response.each_header do |key, value|
|
||||
response_headers[key] = value
|
||||
save_response(env, http_response.code.to_i, http_response.body || '') do |response_headers|
|
||||
http_response.each_header do |key, value|
|
||||
response_headers[key] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -82,6 +83,10 @@ module Faraday
|
||||
end
|
||||
end
|
||||
|
||||
def with_net_http_connection(env)
|
||||
yield net_http_connection(env)
|
||||
end
|
||||
|
||||
def net_http_connection(env)
|
||||
if proxy = env[:request][:proxy]
|
||||
Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password])
|
||||
|
||||
@ -8,7 +8,7 @@ module Faraday
|
||||
class NetHttpPersistent < NetHttp
|
||||
dependency 'net/http/persistent'
|
||||
|
||||
def net_http_connection(env)
|
||||
def with_net_http_connection(env)
|
||||
if proxy = env[:request][:proxy]
|
||||
proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
|
||||
proxy_uri.user = proxy_uri.password = nil
|
||||
@ -18,7 +18,8 @@ module Faraday
|
||||
define_method(:password) { proxy[:password] }
|
||||
end if proxy[:user]
|
||||
end
|
||||
Net::HTTP::Persistent.new 'Faraday', proxy_uri
|
||||
|
||||
yield Net::HTTP::Persistent.new 'Faraday', proxy_uri
|
||||
end
|
||||
|
||||
def perform_request(http, env)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user