Class: Faraday::Adapter::NetHttp
Constant Summary
collapse
- NET_HTTP_EXCEPTIONS =
[
EOFError,
Errno::ECONNABORTED,
Errno::ECONNREFUSED,
Errno::ECONNRESET,
Errno::EINVAL,
Net::HTTPBadResponse,
Net::HTTPHeaderSyntaxError,
Net::ProtocolError,
SocketError
]
CONTENT_LENGTH
Instance Attribute Summary
Attributes included from Parallelism
#supports_parallel
Instance Method Summary
collapse
adapter?, #prepend_proxy_auth_string, #save_response
#all_loaded_constants, #autoload_all, #load_autoloaded_constants
#lookup_middleware, #register_middleware
#inherited, #supports_parallel?
Methods inherited from Middleware
adapter?, dependency, inherited, #initialize, loaded?, new
Instance Method Details
#call(env) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/faraday/adapter/net_http.rb', line 25
def call(env)
super
http = net_http_connection(env)
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]
begin
http_response = perform_request(http, env)
rescue *NET_HTTP_EXCEPTIONS
raise Error::ConnectionFailed, $!
end
save_response(env, http_response.code.to_i, http_response.body) do ||
http_response. do |key, value|
[key] = value
end
end
@app.call env
rescue Timeout::Error => err
raise Faraday::Error::TimeoutError, err
end
|
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/faraday/adapter/net_http.rb', line 84
def configure_ssl(http, ssl)
http.use_ssl = true
http.verify_mode = ssl_verify_mode(ssl)
http.cert_store = ssl_cert_store(ssl)
http.cert = ssl[:client_cert] if ssl[:client_cert]
http.key = ssl[:client_key] if ssl[:client_key]
http.ca_file = ssl[:ca_file] if ssl[:ca_file]
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]
end
|
#create_request(env) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/faraday/adapter/net_http.rb', line 51
def create_request(env)
request = Net::HTTPGenericRequest.new \
env[:method].to_s.upcase, !!env[:body], :head != env[:method], env[:url].request_uri, env[:request_headers]
if env[:body].respond_to?(:read)
request.body_stream = env[:body]
else
request.body = env[:body]
end
request
end
|
#net_http_connection(env) ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/faraday/adapter/net_http.rb', line 76
def net_http_connection(env)
if proxy = env[:request][:proxy]
Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password])
else
Net::HTTP
end.new(env[:url].host, env[:url].port)
end
|
67
68
69
70
71
72
73
74
|
# File 'lib/faraday/adapter/net_http.rb', line 67
def perform_request(http, env)
if :get == env[:method] and !env[:body]
http.get env[:url].request_uri, env[:request_headers]
else
http.request create_request(env)
end
end
|
#ssl_cert_store(ssl) ⇒ Object
97
98
99
100
101
102
103
|
# File 'lib/faraday/adapter/net_http.rb', line 97
def ssl_cert_store(ssl)
return ssl[:cert_store] if ssl[:cert_store]
cert_store = OpenSSL::X509::Store.new
cert_store.set_default_paths
cert_store
end
|
#ssl_verify_mode(ssl) ⇒ Object
105
106
107
108
109
110
111
112
113
|
# File 'lib/faraday/adapter/net_http.rb', line 105
def ssl_verify_mode(ssl)
ssl[:verify_mode] || begin
if ssl.fetch(:verify, true)
OpenSSL::SSL::VERIFY_PEER
else
OpenSSL::SSL::VERIFY_NONE
end
end
end
|