mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-04 00:02:03 -04:00
Fixes Rubocop Style/GuardClause (#931)
This commit is contained in:
parent
e342992751
commit
ded4f6ef9a
@ -6,27 +6,27 @@
|
||||
# Note that changes in the inspected code, or installation of new
|
||||
# versions of RuboCop, may require this file to be generated again.
|
||||
|
||||
# Offense count: 40
|
||||
# Offense count: 38
|
||||
Metrics/AbcSize:
|
||||
Max: 79
|
||||
|
||||
# Offense count: 5
|
||||
# Configuration parameters: CountComments.
|
||||
Metrics/ClassLength:
|
||||
Max: 212
|
||||
Max: 211
|
||||
|
||||
# Offense count: 18
|
||||
# Offense count: 16
|
||||
Metrics/CyclomaticComplexity:
|
||||
Max: 16
|
||||
|
||||
# Offense count: 44
|
||||
# Offense count: 42
|
||||
# Configuration parameters: CountComments, ExcludedMethods.
|
||||
Metrics/MethodLength:
|
||||
Max: 65
|
||||
Max: 56
|
||||
|
||||
# Offense count: 16
|
||||
# Offense count: 13
|
||||
Metrics/PerceivedComplexity:
|
||||
Max: 18
|
||||
Max: 16
|
||||
|
||||
# Offense count: 3
|
||||
Style/ClassVars:
|
||||
@ -60,28 +60,12 @@ Style/GlobalVars:
|
||||
Exclude:
|
||||
- 'script/generate_certs'
|
||||
|
||||
# Offense count: 21
|
||||
# Configuration parameters: MinBodyLength.
|
||||
Style/GuardClause:
|
||||
Exclude:
|
||||
- 'lib/faraday/adapter/em_http.rb'
|
||||
- 'lib/faraday/adapter/em_http_ssl_patch.rb'
|
||||
- 'lib/faraday/adapter/em_synchrony.rb'
|
||||
- 'lib/faraday/adapter/httpclient.rb'
|
||||
- 'lib/faraday/adapter/net_http.rb'
|
||||
- 'lib/faraday/adapter/net_http_persistent.rb'
|
||||
- 'lib/faraday/adapter/patron.rb'
|
||||
- 'lib/faraday/connection.rb'
|
||||
- 'lib/faraday/request/retry.rb'
|
||||
- 'lib/faraday/request/url_encoded.rb'
|
||||
- 'lib/faraday/utils/headers.rb'
|
||||
|
||||
# Offense count: 1
|
||||
Style/MultipleComparison:
|
||||
Exclude:
|
||||
- 'lib/faraday/encoders/flat_params_encoder.rb'
|
||||
|
||||
# Offense count: 287
|
||||
# Offense count: 297
|
||||
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
||||
# URISchemes: http, https
|
||||
Metrics/LineLength:
|
||||
|
@ -35,33 +35,35 @@ module Faraday
|
||||
|
||||
# Reads out proxy settings from env into options
|
||||
def configure_proxy(options, env)
|
||||
if (proxy = request_options(env)[:proxy])
|
||||
options[:proxy] = {
|
||||
host: proxy[:uri].host,
|
||||
port: proxy[:uri].port,
|
||||
authorization: [proxy[:user], proxy[:password]]
|
||||
}
|
||||
end
|
||||
proxy = request_options(env)[:proxy]
|
||||
return unless proxy
|
||||
|
||||
options[:proxy] = {
|
||||
host: proxy[:uri].host,
|
||||
port: proxy[:uri].port,
|
||||
authorization: [proxy[:user], proxy[:password]]
|
||||
}
|
||||
end
|
||||
|
||||
# Reads out host and port settings from env into options
|
||||
def configure_socket(options, env)
|
||||
if (bind = request_options(env)[:bind])
|
||||
options[:bind] = {
|
||||
host: bind[:host],
|
||||
port: bind[:port]
|
||||
}
|
||||
end
|
||||
bind = request_options(env)[:bind]
|
||||
return unless bind
|
||||
|
||||
options[:bind] = {
|
||||
host: bind[:host],
|
||||
port: bind[:port]
|
||||
}
|
||||
end
|
||||
|
||||
# Reads out SSL certificate settings from env into options
|
||||
def configure_ssl(options, env)
|
||||
if env[:url].scheme == 'https' && env[:ssl]
|
||||
options[:ssl] = {
|
||||
cert_chain_file: env[:ssl][:ca_file],
|
||||
verify_peer: env[:ssl].fetch(:verify, true)
|
||||
}
|
||||
end
|
||||
return unless env[:url].scheme == 'https' && env[:ssl]
|
||||
|
||||
options[:ssl] = {
|
||||
cert_chain_file: env[:ssl][:ca_file],
|
||||
verify_peer: env[:ssl].fetch(:verify, true)
|
||||
}
|
||||
end
|
||||
|
||||
# Reads out timeout settings from env into options
|
||||
@ -128,17 +130,13 @@ module Faraday
|
||||
raise_error(error) if error
|
||||
end
|
||||
rescue EventMachine::Connectify::CONNECTError => err
|
||||
if err.message.include?('Proxy Authentication Required')
|
||||
raise Faraday::ConnectionFailed, %(407 "Proxy Authentication Required ")
|
||||
else
|
||||
raise Faraday::ConnectionFailed, err
|
||||
end
|
||||
raise Faraday::ConnectionFailed, %(407 "Proxy Authentication Required ") if err.message.include?('Proxy Authentication Required')
|
||||
|
||||
raise Faraday::ConnectionFailed, err
|
||||
rescue StandardError => err
|
||||
if defined?(OpenSSL) && err.is_a?(OpenSSL::SSL::SSLError)
|
||||
raise Faraday::SSLError, err
|
||||
else
|
||||
raise
|
||||
end
|
||||
raise Faraday::SSLError, err if defined?(OpenSSL) && err.is_a?(OpenSSL::SSL::SSLError)
|
||||
|
||||
raise
|
||||
end
|
||||
|
||||
# TODO: reuse the connection to support pipelining
|
||||
|
@ -6,41 +6,38 @@ require 'em-http'
|
||||
# EventMachine patch to make SSL work.
|
||||
module EmHttpSslPatch
|
||||
def ssl_verify_peer(cert_string)
|
||||
cert = nil
|
||||
begin
|
||||
cert = OpenSSL::X509::Certificate.new(cert_string)
|
||||
@last_seen_cert = OpenSSL::X509::Certificate.new(cert_string)
|
||||
rescue OpenSSL::X509::CertificateError
|
||||
return false
|
||||
end
|
||||
|
||||
@last_seen_cert = cert
|
||||
raise OpenSSL::SSL::SSLError, %(unable to verify the server certificate for "#{host}") unless certificate_store.verify(@last_seen_cert)
|
||||
|
||||
if certificate_store.verify(@last_seen_cert)
|
||||
begin
|
||||
certificate_store.add_cert(@last_seen_cert)
|
||||
rescue OpenSSL::X509::StoreError => e
|
||||
raise e unless e.message == 'cert already in hash table'
|
||||
end
|
||||
true
|
||||
else
|
||||
raise OpenSSL::SSL::SSLError, %(unable to verify the server certificate for "#{host}")
|
||||
begin
|
||||
certificate_store.add_cert(@last_seen_cert)
|
||||
rescue OpenSSL::X509::StoreError => e
|
||||
raise e unless e.message == 'cert already in hash table'
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
def ssl_handshake_completed
|
||||
return true unless verify_peer?
|
||||
|
||||
if OpenSSL::SSL.verify_certificate_identity(@last_seen_cert, host)
|
||||
true
|
||||
else
|
||||
raise OpenSSL::SSL::SSLError, %(host "#{host}" does not match the server certificate)
|
||||
end
|
||||
raise OpenSSL::SSL::SSLError, %(host "#{host}" does not match the server certificate) unless verified_cert_identity?
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
def verify_peer?
|
||||
parent.connopts.tls[:verify_peer]
|
||||
end
|
||||
|
||||
def verified_cert_identity?
|
||||
OpenSSL::SSL.verify_certificate_identity(@last_seen_cert, host)
|
||||
end
|
||||
|
||||
def host
|
||||
parent.uri.host
|
||||
end
|
||||
|
@ -80,25 +80,19 @@ module Faraday
|
||||
rescue Errno::ECONNREFUSED
|
||||
raise Faraday::ConnectionFailed, $ERROR_INFO
|
||||
rescue EventMachine::Connectify::CONNECTError => err
|
||||
if err.message.include?('Proxy Authentication Required')
|
||||
raise Faraday::ConnectionFailed, %(407 "Proxy Authentication Required ")
|
||||
else
|
||||
raise Faraday::ConnectionFailed, err
|
||||
end
|
||||
raise Faraday::ConnectionFailed, %(407 "Proxy Authentication Required ") if err.message.include?('Proxy Authentication Required')
|
||||
|
||||
raise Faraday::ConnectionFailed, err
|
||||
rescue Errno::ETIMEDOUT => err
|
||||
raise Faraday::TimeoutError, err
|
||||
rescue RuntimeError => err
|
||||
if err.message == 'connection closed by server'
|
||||
raise Faraday::ConnectionFailed, err
|
||||
else
|
||||
raise
|
||||
end
|
||||
raise Faraday::ConnectionFailed, err if err.message == 'connection closed by server'
|
||||
|
||||
raise
|
||||
rescue StandardError => err
|
||||
if defined?(OpenSSL) && err.is_a?(OpenSSL::SSL::SSLError)
|
||||
raise Faraday::SSLError, err
|
||||
else
|
||||
raise
|
||||
end
|
||||
raise Faraday::SSLError, err if defined?(OpenSSL) && err.is_a?(OpenSSL::SSL::SSLError)
|
||||
|
||||
raise
|
||||
end
|
||||
|
||||
def create_request(env)
|
||||
|
@ -53,19 +53,15 @@ module Faraday
|
||||
rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
|
||||
raise Faraday::TimeoutError, $ERROR_INFO
|
||||
rescue ::HTTPClient::BadResponseError => err
|
||||
if err.message.include?('status 407')
|
||||
raise Faraday::ConnectionFailed, %(407 "Proxy Authentication Required ")
|
||||
else
|
||||
raise Faraday::ClientError, $ERROR_INFO
|
||||
end
|
||||
raise Faraday::ConnectionFailed, %(407 "Proxy Authentication Required ") if err.message.include?('status 407')
|
||||
|
||||
raise Faraday::ClientError, $ERROR_INFO
|
||||
rescue Errno::ECONNREFUSED, IOError, SocketError
|
||||
raise Faraday::ConnectionFailed, $ERROR_INFO
|
||||
rescue StandardError => err
|
||||
if defined?(OpenSSL) && err.is_a?(OpenSSL::SSL::SSLError)
|
||||
raise Faraday::SSLError, err
|
||||
else
|
||||
raise
|
||||
end
|
||||
raise Faraday::SSLError, err if defined?(OpenSSL) && err.is_a?(OpenSSL::SSL::SSLError)
|
||||
|
||||
raise
|
||||
end
|
||||
|
||||
# @param bind [Hash]
|
||||
@ -97,16 +93,19 @@ module Faraday
|
||||
|
||||
# @param req [Hash]
|
||||
def configure_timeouts(req)
|
||||
if req[:timeout]
|
||||
client.connect_timeout = req[:timeout]
|
||||
client.receive_timeout = req[:timeout]
|
||||
client.send_timeout = req[:timeout]
|
||||
end
|
||||
configure_timeout(req) if req[:timeout]
|
||||
configure_open_timeout(req) if req[:open_timeout]
|
||||
end
|
||||
|
||||
if req[:open_timeout]
|
||||
client.connect_timeout = req[:open_timeout]
|
||||
client.send_timeout = req[:open_timeout]
|
||||
end
|
||||
def configure_timeout(req)
|
||||
client.connect_timeout = req[:timeout]
|
||||
client.receive_timeout = req[:timeout]
|
||||
client.send_timeout = req[:timeout]
|
||||
end
|
||||
|
||||
def configure_open_timeout(req)
|
||||
client.connect_timeout = req[:open_timeout]
|
||||
client.send_timeout = req[:open_timeout]
|
||||
end
|
||||
|
||||
def configure_client
|
||||
|
@ -47,11 +47,9 @@ module Faraday
|
||||
begin
|
||||
http_response = perform_request(http, env)
|
||||
rescue *NET_HTTP_EXCEPTIONS => err
|
||||
if defined?(OpenSSL) && err.is_a?(OpenSSL::SSL::SSLError)
|
||||
raise Faraday::SSLError, err
|
||||
else
|
||||
raise Faraday::ConnectionFailed, err
|
||||
end
|
||||
raise Faraday::SSLError, err if defined?(OpenSSL) && err.is_a?(OpenSSL::SSL::SSLError)
|
||||
|
||||
raise Faraday::ConnectionFailed, err
|
||||
end
|
||||
|
||||
save_response(env, http_response.code.to_i, http_response.body || '', nil, http_response.message) do |response_headers|
|
||||
|
@ -44,23 +44,21 @@ module Faraday
|
||||
rescue Errno::ETIMEDOUT => error
|
||||
raise Faraday::TimeoutError, error
|
||||
rescue Net::HTTP::Persistent::Error => error
|
||||
if error.message.include? 'Timeout'
|
||||
raise Faraday::TimeoutError, error
|
||||
elsif error.message.include? 'connection refused'
|
||||
raise Faraday::ConnectionFailed, error
|
||||
else
|
||||
raise
|
||||
end
|
||||
raise Faraday::TimeoutError, error if error.message.include? 'Timeout'
|
||||
|
||||
raise Faraday::ConnectionFailed, error if error.message.include? 'connection refused'
|
||||
|
||||
raise
|
||||
end
|
||||
|
||||
def configure_ssl(http, ssl)
|
||||
http_set(http, :verify_mode, ssl_verify_mode(ssl))
|
||||
http_set(http, :cert_store, ssl_cert_store(ssl))
|
||||
http_set(http, :cert_store, ssl_cert_store(ssl))
|
||||
|
||||
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]
|
||||
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 http_set(http, attr, value)
|
||||
|
@ -45,17 +45,13 @@ module Faraday
|
||||
|
||||
@app.call env
|
||||
rescue ::Patron::TimeoutError => err
|
||||
if connection_timed_out_message?(err.message)
|
||||
raise Faraday::ConnectionFailed, err
|
||||
else
|
||||
raise Faraday::TimeoutError, err
|
||||
end
|
||||
raise Faraday::ConnectionFailed, err if connection_timed_out_message?(err.message)
|
||||
|
||||
raise Faraday::TimeoutError, err
|
||||
rescue ::Patron::Error => err
|
||||
if err.message.include?('code 407')
|
||||
raise Faraday::ConnectionFailed, %(407 "Proxy Authentication Required ")
|
||||
else
|
||||
raise Faraday::ConnectionFailed, err
|
||||
end
|
||||
raise Faraday::ConnectionFailed, %(407 "Proxy Authentication Required ") if err.message.include?('code 407')
|
||||
|
||||
raise Faraday::ConnectionFailed, err
|
||||
end
|
||||
|
||||
if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
|
||||
|
@ -571,10 +571,10 @@ module Faraday
|
||||
|
||||
def find_default_proxy
|
||||
uri = ENV['http_proxy']
|
||||
if uri && !uri.empty?
|
||||
uri = 'http://' + uri if uri !~ /^http/i
|
||||
uri
|
||||
end
|
||||
return unless uri && !uri.empty?
|
||||
|
||||
uri = 'http://' + uri if uri !~ /^http/i
|
||||
uri
|
||||
end
|
||||
|
||||
def proxy_for_request(url)
|
||||
|
@ -139,11 +139,9 @@ module Faraday
|
||||
end
|
||||
end
|
||||
|
||||
if exception.is_a?(Faraday::RetriableResponse)
|
||||
exception.response
|
||||
else
|
||||
raise
|
||||
end
|
||||
raise unless exception.is_a?(Faraday::RetriableResponse)
|
||||
|
||||
exception.response
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -26,10 +26,10 @@ module Faraday
|
||||
# @param env [Faraday::Env]
|
||||
# @yield [request_body] Body of the request
|
||||
def match_content_type(env)
|
||||
if process_request?(env)
|
||||
env.request_headers[CONTENT_TYPE] ||= self.class.mime_type
|
||||
yield(env.body) unless env.body.respond_to?(:to_str)
|
||||
end
|
||||
return unless process_request?(env)
|
||||
|
||||
env.request_headers[CONTENT_TYPE] ||= self.class.mime_type
|
||||
yield(env.body) unless env.body.respond_to?(:to_str)
|
||||
end
|
||||
|
||||
# @param env [Faraday::Env]
|
||||
|
@ -70,10 +70,11 @@ module Faraday
|
||||
|
||||
def delete(key)
|
||||
key = KeyMap[key]
|
||||
if (key = @names[key.downcase])
|
||||
@names.delete key.downcase
|
||||
super(key)
|
||||
end
|
||||
key = @names[key.downcase]
|
||||
return unless key
|
||||
|
||||
@names.delete key.downcase
|
||||
super(key)
|
||||
end
|
||||
|
||||
def include?(key)
|
||||
|
Loading…
x
Reference in New Issue
Block a user