lib/httpx/adapters/faraday.rb in httpx-0.24.2 vs lib/httpx/adapters/faraday.rb in httpx-0.24.3

- old
+ new

@@ -38,12 +38,18 @@ def build_connection(env) return @connection if defined?(@connection) @connection = ::HTTPX.plugin(:compression).plugin(:persistent).plugin(ReasonPlugin) @connection = @connection.with(@connection_options) unless @connection_options.empty? - @connection = @connection.with(options_from_env(env)) + connection_opts = options_from_env(env) + if (bind = env.request.bind) + @bind = TCPSocket.new(bind[:host], bind[:port]) + connection_opts[:io] = @bind + end + @connection = @connection.with(connection_opts) + if (proxy = env.request.proxy) proxy_options = { uri: proxy.uri } proxy_options[:username] = proxy.user if proxy.user proxy_options[:password] = proxy.password if proxy.password @@ -53,11 +59,12 @@ @connection end def close - @session.close + @connection.close if @connection + @bind.close if @bind end private def connect(env, &blk) @@ -71,10 +78,12 @@ Errno::EINVAL, Errno::ENETUNREACH, Errno::EPIPE, ::HTTPX::ConnectionError => e raise CONNECTION_FAILED_ERROR, e + rescue ::HTTPX::TimeoutError => e + raise Faraday::TimeoutError, e end def build_request(env) meth = env[:method] @@ -85,25 +94,29 @@ [meth.to_s.upcase, env.url, request_options] end def options_from_env(env) timeout_options = {} - if (sec = request_timeout(:read, env)) + req_opts = env.request + if (sec = request_timeout(:read, req_opts)) timeout_options[:operation_timeout] = sec end - if (sec = request_timeout(:write, env)) + if (sec = request_timeout(:write, req_opts)) timeout_options[:operation_timeout] = sec end - if (sec = request_timeout(:open, env)) + if (sec = request_timeout(:open, req_opts)) timeout_options[:connect_timeout] = sec end ssl_options = {} - ssl_options[:verify_mode] = OpenSSL::SSL::VERIFY_PEER if env.ssl.verify + unless env.ssl.verify.nil? + ssl_options[:verify_mode] = env.ssl.verify ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE + end + ssl_options[:ca_file] = env.ssl.ca_file if env.ssl.ca_file ssl_options[:ca_path] = env.ssl.ca_path if env.ssl.ca_path ssl_options[:cert_store] = env.ssl.cert_store if env.ssl.cert_store ssl_options[:cert] = env.ssl.client_cert if env.ssl.client_cert ssl_options[:key] = env.ssl.client_key if env.ssl.client_key @@ -230,10 +243,12 @@ handler = @handlers[index] handler.on_response.call(response) handler.on_complete.call(handler.env) end end + rescue ::HTTPX::TimeoutError => e + raise Faraday::TimeoutError, e end # from Faraday::Adapter#connection def connection(env) conn = build_connection(env) @@ -297,9 +312,11 @@ response = session.request(request) # do not call #raise_for_status for HTTP 4xx or 5xx, as faraday has a middleware for that. response.raise_for_status unless response.is_a?(::HTTPX::Response) response end + rescue ::HTTPX::TimeoutError => e + raise Faraday::TimeoutError, e end def parallel?(env) env[:parallel_manager] end