Skip to content

Commit

Permalink
Merge branch '1.0' of https://github.com/lostisland/faraday into feat…
Browse files Browse the repository at this point in the history
…ure/#762-rspec

# Conflicts:
#	lib/faraday.rb
#	lib/faraday/connection.rb
#	test/adapters/logger_test.rb
#	test/parameters_test.rb
  • Loading branch information
iMacTia committed Jul 21, 2018
2 parents eb08c1e + 82a8a0b commit c4f03b7
Show file tree
Hide file tree
Showing 32 changed files with 438 additions and 176 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -20,3 +20,4 @@ vendor/bundle

## IDEs
.idea/
.yardoc/
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -55,7 +55,7 @@ Since the default middleware stack uses url\_encoded middleware and default adap
```ruby
conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.response :logger # log requests and responses to $stdout
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end

Expand All @@ -80,7 +80,7 @@ response.body

conn.get '/nigiri', { :name => 'Maguro' } # GET http://sushi.com/nigiri?name=Maguro

conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
req.url '/search', :page => 2
req.params['limit'] = 100
end
Expand Down Expand Up @@ -132,7 +132,7 @@ conn.get do |req|
req.options.context = {
foo: 'foo',
bar: 'bar'
}
}
end
```

Expand Down
2 changes: 1 addition & 1 deletion lib/faraday.rb
Expand Up @@ -16,7 +16,7 @@
# conn.get '/'
#
module Faraday
VERSION = "0.14.0"
VERSION = "0.15.2"
METHODS_WITH_QUERY = %w[get head delete]
METHODS_WITH_BODY = %w[post put patch]

Expand Down
4 changes: 2 additions & 2 deletions lib/faraday/adapter.rb
@@ -1,5 +1,5 @@
module Faraday
# Public: This is a base class for all Faraday adapters. Adapters are
# Base class for all Faraday adapters. Adapters are
# responsible for fulfilling a Faraday request.
class Adapter < Middleware
CONTENT_LENGTH = 'Content-Length'.freeze
Expand All @@ -16,7 +16,7 @@ class Adapter < Middleware
:rack => [:Rack, 'rack'],
:httpclient => [:HTTPClient, 'httpclient']

# Public: This module marks an Adapter as supporting parallel requests.
# This module marks an Adapter as supporting parallel requests.
module Parallelism
attr_writer :supports_parallel
def supports_parallel?() @supports_parallel end
Expand Down
17 changes: 14 additions & 3 deletions lib/faraday/adapter/em_http.rb
@@ -1,10 +1,11 @@
module Faraday
class Adapter
# EventMachine adapter is useful for either asynchronous requests
# when in EM reactor loop or for making parallel requests in
# EventMachine adapter. This adapter is useful for either asynchronous
# requests when in an EM reactor loop, or for making parallel requests in
# synchronous code.
class EMHttp < Faraday::Adapter
module Options
# @return [Hash]
def connection_config(env)
options = {}
configure_proxy(options, env)
Expand All @@ -30,6 +31,7 @@ def read_body(env)
body.respond_to?(:read) ? body.read : body
end

# Reads out proxy settings from env into options
def configure_proxy(options, env)
if proxy = request_options(env)[:proxy]
options[:proxy] = {
Expand All @@ -40,6 +42,7 @@ def configure_proxy(options, env)
end
end

# Reads out host and port settings from env into options
def configure_socket(options, env)
if bind = request_options(env)[:bind]
options[:bind] = {
Expand All @@ -49,6 +52,7 @@ def configure_socket(options, env)
end
end

# Reads out SSL certificate settings from env into options
def configure_ssl(options, env)
if env[:url].scheme == 'https' && env[:ssl]
options[:ssl] = {
Expand All @@ -58,12 +62,14 @@ def configure_ssl(options, env)
end
end

# Reads out timeout settings from env into options
def configure_timeout(options, env)
timeout, open_timeout = request_options(env).values_at(:timeout, :open_timeout)
options[:connect_timeout] = options[:inactivity_timeout] = timeout
options[:connect_timeout] = open_timeout if open_timeout
end

# Reads out compression header settings from env into options
def configure_compression(options, env)
if env[:method] == :get and not options[:head].key? 'accept-encoding'
options[:head]['accept-encoding'] = 'gzip, compressed'
Expand All @@ -81,6 +87,7 @@ def request_options(env)

self.supports_parallel = true

# @return [Manager]
def self.setup_parallel_manager(options = nil)
Manager.new
end
Expand Down Expand Up @@ -176,17 +183,20 @@ def raise_error(msg)
raise errklass, msg
end

# @return [Boolean]
def parallel?(env)
!!env[:parallel_manager]
end

# The parallel manager is designed to start an EventMachine loop
# This parallel manager is designed to start an EventMachine loop
# and block until all registered requests have been completed.
class Manager
# @see reset
def initialize
reset
end

# Re-initializes instance variables
def reset
@registered_procs = []
@num_registered = 0
Expand All @@ -195,6 +205,7 @@ def reset
@running = false
end

# @return [Boolean]
def running?() @running end

def add
Expand Down
1 change: 1 addition & 0 deletions lib/faraday/adapter/em_http_ssl_patch.rb
@@ -1,6 +1,7 @@
require 'openssl'
require 'em-http'

# EventMachine patch to make SSL work.
module EmHttpSslPatch
def ssl_verify_peer(cert_string)
cert = nil
Expand Down
2 changes: 2 additions & 0 deletions lib/faraday/adapter/em_synchrony.rb
Expand Up @@ -2,6 +2,7 @@

module Faraday
class Adapter
# EventMachine Synchrony adapter.
class EMSynchrony < Faraday::Adapter
include EMHttp::Options

Expand All @@ -13,6 +14,7 @@ class EMSynchrony < Faraday::Adapter

self.supports_parallel = true

# @return [ParallelManager]
def self.setup_parallel_manager(options = {})
ParallelManager.new
end
Expand Down
16 changes: 10 additions & 6 deletions lib/faraday/adapter/em_synchrony/parallel_manager.rb
@@ -1,10 +1,14 @@
module Faraday
class Adapter
class EMSynchrony < Faraday::Adapter
# A parallel manager for EMSynchrony.
class ParallelManager

# Add requests to queue. The `request` argument should be a
# `EM::HttpRequest` object.
# Add requests to queue.
#
# @param request [EM::HttpRequest]
# @param method [Symbol, String] HTTP method
# @param args [Array] the rest of the positional arguments
def add(request, method, *args, &block)
queue << {
:request => request,
Expand Down Expand Up @@ -60,7 +64,7 @@ def perform
multi.perform
end

end # ParallelManager
end # EMSynchrony
end # Adapter
end # Faraday
end
end
end
end
5 changes: 5 additions & 0 deletions lib/faraday/adapter/excon.rb
@@ -1,5 +1,6 @@
module Faraday
class Adapter
# Excon adapter.
class Excon < Faraday::Adapter
dependency 'excon'

Expand All @@ -15,6 +16,9 @@ def call(env)
opts[:client_key] = ssl[:client_key] if ssl[:client_key]
opts[:certificate] = ssl[:certificate] if ssl[:certificate]
opts[:private_key] = ssl[:private_key] if ssl[:private_key]
opts[:ssl_version] = ssl[:version] if ssl[:version]
opts[:ssl_min_version] = ssl[:min_version] if ssl[:min_version]
opts[:ssl_max_version] = ssl[:max_version] if ssl[:max_version]

# https://github.com/geemus/excon/issues/106
# https://github.com/jruby/jruby-ossl/issues/19
Expand Down Expand Up @@ -70,6 +74,7 @@ def call(env)
raise Error::TimeoutError, err
end

# @return [Excon]
def create_connection(env, opts)
::Excon.new(env[:url].to_s, opts.merge(@connection_options))
end
Expand Down
11 changes: 11 additions & 0 deletions lib/faraday/adapter/httpclient.rb
@@ -1,8 +1,10 @@
module Faraday
class Adapter
# HTTPClient adapter.
class HTTPClient < Faraday::Adapter
dependency 'httpclient'

# @return [HTTPClient]
def client
@client ||= ::HTTPClient.new
end
Expand Down Expand Up @@ -64,18 +66,23 @@ def call(env)
end
end

# @param bind [Hash]
def configure_socket(bind)
client.socket_local.host = bind[:host]
client.socket_local.port = bind[:port]
end

# Configure proxy URI and any user credentials.
#
# @param proxy [Hash]
def configure_proxy(proxy)
client.proxy = proxy[:uri]
if proxy[:user] && proxy[:password]
client.set_proxy_auth proxy[:user], proxy[:password]
end
end

# @param ssl [Hash]
def configure_ssl(ssl)
ssl_config = client.ssl_config
ssl_config.verify_mode = ssl_verify_mode(ssl)
Expand All @@ -88,6 +95,7 @@ def configure_ssl(ssl)
ssl_config.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
end

# @param req [Hash]
def configure_timeouts(req)
if req[:timeout]
client.connect_timeout = req[:timeout]
Expand All @@ -105,6 +113,8 @@ def configure_client
@config_block.call(client) if @config_block
end

# @param ssl [Hash]
# @return [OpenSSL::X509::Store]
def ssl_cert_store(ssl)
return ssl[:cert_store] if ssl[:cert_store]
# Memoize the cert store so that the same one is passed to
Expand All @@ -118,6 +128,7 @@ def ssl_cert_store(ssl)
end
end

# @param ssl [Hash]
def ssl_verify_mode(ssl)
ssl[:verify_mode] || begin
if ssl.fetch(:verify, true)
Expand Down
17 changes: 14 additions & 3 deletions lib/faraday/adapter/net_http.rb
Expand Up @@ -8,6 +8,7 @@

module Faraday
class Adapter
# Net::HTTP adapter.
class NetHttp < Faraday::Adapter
NET_HTTP_EXCEPTIONS = [
IOError,
Expand All @@ -28,6 +29,11 @@ class NetHttp < Faraday::Adapter
NET_HTTP_EXCEPTIONS << OpenSSL::SSL::SSLError if defined?(OpenSSL)
NET_HTTP_EXCEPTIONS << Net::OpenTimeout if defined?(Net::OpenTimeout)

def initialize(app = nil, opts = {}, &block)
@cert_store = nil
super(app, opts, &block)
end

def call(env)
super
with_net_http_connection(env) do |http|
Expand Down Expand Up @@ -140,21 +146,26 @@ def configure_ssl(http, ssl)
http.ca_path = ssl[:ca_path] if ssl[:ca_path]
http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
http.ssl_version = ssl[:version] if ssl[:version]
http.min_version = ssl[:min_version] if ssl[:min_version]
http.max_version = ssl[:max_version] if ssl[:max_version]
end

def configure_request(http, req)
http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
http.open_timeout = req[:open_timeout] if req[:open_timeout]
# Only set if Net::Http supports it, since Ruby 2.5.
http.max_retries = 0 if http.respond_to?(:max_retries=)

@config_block.call(http) if @config_block
end

def ssl_cert_store(ssl)
return ssl[:cert_store] if ssl[:cert_store]
return @cert_store if @cert_store
# Use the default cert store by default, i.e. system ca certs
cert_store = OpenSSL::X509::Store.new
cert_store.set_default_paths
cert_store
@cert_store = OpenSSL::X509::Store.new
@cert_store.set_default_paths
@cert_store
end

def ssl_verify_mode(ssl)
Expand Down
32 changes: 18 additions & 14 deletions lib/faraday/adapter/net_http_persistent.rb
@@ -1,20 +1,22 @@
module Faraday
class Adapter
# Net::HTTP::Persistent adapter.
class NetHttpPersistent < NetHttp
dependency 'net/http/persistent'

private

def net_http_connection(env)
proxy_uri = proxy_uri(env)

cached_connection env[:url], proxy_uri do
@cached_connection ||=
if Net::HTTP::Persistent.instance_method(:initialize).parameters.first == [:key, :name]
Net::HTTP::Persistent.new(name: 'Faraday', proxy: proxy_uri)
Net::HTTP::Persistent.new(name: 'Faraday')
else
Net::HTTP::Persistent.new('Faraday', proxy_uri)
Net::HTTP::Persistent.new('Faraday')
end
end

proxy_uri = proxy_uri(env)
@cached_connection.proxy = proxy_uri if @cached_connection.proxy_uri != proxy_uri
@cached_connection
end

def proxy_uri(env)
Expand Down Expand Up @@ -46,17 +48,19 @@ def perform_request(http, env)
end

def configure_ssl(http, ssl)
http.verify_mode = ssl_verify_mode(ssl)
http.cert_store = ssl_cert_store(ssl)
http_set(http, :verify_mode, ssl_verify_mode(ssl))
http_set(http, :cert_store, ssl_cert_store(ssl))

http.certificate = ssl[:client_cert] if ssl[:client_cert]
http.private_key = ssl[:client_key] if ssl[:client_key]
http.ca_file = ssl[:ca_file] if ssl[:ca_file]
http.ssl_version = ssl[:version] if ssl[:version]
http_set(http, :certificate, ssl[:client_cert]) if ssl[:client_cert]
http_set(http, :private_key, ssl[:client_key]) if ssl[:client_key]
http_set(http, :ca_file, ssl[:ca_file]) if ssl[:ca_file]
http_set(http, :ssl_version, ssl[:version]) if ssl[:version]
end

def cached_connection(url, proxy_uri)
(@cached_connection ||= {})[[url.scheme, url.host, url.port, proxy_uri]] ||= yield
def http_set(http, attr, value)
if http.send(attr) != value
http.send("#{attr}=", value)
end
end
end
end
Expand Down

0 comments on commit c4f03b7

Please sign in to comment.