faraday 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +74 -0
- data/lib/faraday.rb +3 -1
- data/lib/faraday/adapter.rb +0 -1
- data/lib/faraday/adapter/net_http_persistent.rb +1 -1
- data/lib/faraday/autoload.rb +0 -1
- data/lib/faraday/error.rb +12 -0
- data/lib/faraday/request/retry.rb +1 -1
- data/lib/faraday/version.rb +1 -1
- data/spec/faraday/error_spec.rb +15 -0
- data/spec/faraday/response/raise_error_spec.rb +30 -0
- metadata +17 -4
- data/lib/faraday/adapter/net_http.rb +0 -220
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f907e999805c78e8c2e77cb4d6b29a0a6f8fd8fe1a7df9930f02be7ec113a630
|
4
|
+
data.tar.gz: 1ceac7f2b44d41d11d1ad3f7642daea9198ba3ac2f71a4932cf7d6b7b867f704
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2eaf54d743ed3d7554fb2be17003e81b08c060582f140b06fb55e8aceb7dfcf2428e898f47f3760eddcdb4ad64aeaacc93916672b9ed10fa4a004d0ea1a20e1a
|
7
|
+
data.tar.gz: a700f0bc8999e9a0152843bbfbd23001fa23931614ae9573b13ff839e7af01f38ca2adbb01c82e4b387688496b3caff87357e3946d7500595a2f807313e177d7
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,79 @@
|
|
1
1
|
# Faraday Changelog
|
2
2
|
|
3
|
+
## [v1.2.0](https://github.com/lostisland/faraday/releases/tag/v1.2.0) (2020-12-23)
|
4
|
+
|
5
|
+
### Features
|
6
|
+
|
7
|
+
* Introduces `on_request` and `on_complete` methods in `Faraday::Middleware`. (#1194, @iMacTia)
|
8
|
+
|
9
|
+
### Fixes
|
10
|
+
|
11
|
+
* Require 'date' to avoid retry exception (#1206, @rustygeldmacher)
|
12
|
+
* Fix rdebug recursion issue (#1205, @native-api)
|
13
|
+
* Update call to `em_http_ssl_patch` (#1202, @kylekeesling)
|
14
|
+
* `EmHttp` adapter: drop superfluous loaded? check (#1213, @olleolleolle)
|
15
|
+
* Avoid 1 use of keyword hackery (#1211, @grosser)
|
16
|
+
* Fix #1219 `Net::HTTP` still uses env proxy (#1221, @iMacTia)
|
17
|
+
|
18
|
+
### Documentation
|
19
|
+
|
20
|
+
* Add comment in gemspec to explain exposure of `examples` and `spec` folders. (#1192, @iMacTia)
|
21
|
+
* Adapters, how to create them (#1193, @olleolleolle)
|
22
|
+
* Update documentation on using the logger (#1196, @tijmenb)
|
23
|
+
* Adjust the retry documentation and spec to align with implementation (#1198, @nbeyer)
|
24
|
+
|
25
|
+
### Misc
|
26
|
+
|
27
|
+
* Test against ruby head (#1208, @grosser)
|
28
|
+
|
29
|
+
## [v1.1.0](https://github.com/lostisland/faraday/releases/tag/v1.1.0) (2020-10-17)
|
30
|
+
|
31
|
+
### Features
|
32
|
+
|
33
|
+
* Makes parameters sorting configurable (#1162 @wishdev)
|
34
|
+
* Introduces `flat_encode` option for multipart adapter. (#1163 @iMacTia)
|
35
|
+
* Include request info in exceptions raised by RaiseError Middleware (#1181 @SandroDamilano)
|
36
|
+
|
37
|
+
### Fixes
|
38
|
+
|
39
|
+
* Avoid `last arg as keyword param` warning when building user middleware on Ruby 2.7 (#1153 @dgholz)
|
40
|
+
* Limits net-http-persistent version to < 4.0 (#1156 @iMacTia)
|
41
|
+
* Update `typhoeus` to new stable version (`1.4`) (#1159 @AlexWayfer)
|
42
|
+
* Properly fix test failure with Rack 2.1+. (#1171 @voxik)
|
43
|
+
|
44
|
+
### Documentation
|
45
|
+
|
46
|
+
* Improves documentation on how to contribute to the site by using Docker. (#1175 @iMacTia)
|
47
|
+
* Remove retry_change_requests from documentation (#1185 @stim371)
|
48
|
+
|
49
|
+
### Misc
|
50
|
+
|
51
|
+
* Link from GitHub Actions badge to CI workflow (#1141 @olleolleolle)
|
52
|
+
* Return tests of `Test` adapter (#1147 @AlexWayfer)
|
53
|
+
* Add 1.0 release to wording in CONTRIBUTING (#1155 @olleolleolle)
|
54
|
+
* Fix linting bumping Rubocop to 0.90.0 (#1182 @iMacTia)
|
55
|
+
* Drop `git ls-files` in gemspec (#1183 @utkarsh2102)
|
56
|
+
* Upgrade CI to ruby/setup-ruby (#1187 @gogainda)
|
57
|
+
|
58
|
+
## [v1.0.1](https://github.com/lostisland/faraday/releases/tag/v1.0.1) (2020-03-29)
|
59
|
+
|
60
|
+
### Fixes
|
61
|
+
|
62
|
+
* Use Net::HTTP#start(&block) to ensure closed TCP connections (#1117)
|
63
|
+
* Fully qualify constants to be checked (#1122)
|
64
|
+
* Allows `parse` method to be private/protected in response middleware (#1123)
|
65
|
+
* Encode Spaces in Query Strings as '%20' Instead of '+' (#1125)
|
66
|
+
* Limits rack to v2.0.x (#1127)
|
67
|
+
* Adapter Registry reads also use mutex (#1136)
|
68
|
+
|
69
|
+
### Documentation
|
70
|
+
|
71
|
+
* Retry middleware documentation fix (#1109)
|
72
|
+
* Docs(retry): precise usage of retry-after (#1111)
|
73
|
+
* README: Link the logo to the website (#1112)
|
74
|
+
* Website: add search bar (#1116)
|
75
|
+
* Fix request/response mix-up in docs text (#1132)
|
76
|
+
|
3
77
|
## v1.0
|
4
78
|
|
5
79
|
Features:
|
data/lib/faraday.rb
CHANGED
@@ -9,7 +9,7 @@ require 'faraday/dependency_loader'
|
|
9
9
|
|
10
10
|
unless defined?(::Faraday::Timer)
|
11
11
|
require 'timeout'
|
12
|
-
Timer = Timeout
|
12
|
+
::Faraday::Timer = Timeout
|
13
13
|
end
|
14
14
|
|
15
15
|
require 'faraday/version'
|
@@ -27,6 +27,8 @@ require 'faraday/error'
|
|
27
27
|
require 'faraday/file_part'
|
28
28
|
require 'faraday/param_part'
|
29
29
|
|
30
|
+
require 'faraday/net_http'
|
31
|
+
|
30
32
|
# This is the main namespace for Faraday.
|
31
33
|
#
|
32
34
|
# It provides methods to create {Connection} objects, and HTTP-related
|
data/lib/faraday/adapter.rb
CHANGED
@@ -51,7 +51,7 @@ module Faraday
|
|
51
51
|
|
52
52
|
def perform_request(http, env)
|
53
53
|
http.request env[:url], create_request(env)
|
54
|
-
rescue Errno::ETIMEDOUT => e
|
54
|
+
rescue Errno::ETIMEDOUT, Net::OpenTimeout => e
|
55
55
|
raise Faraday::TimeoutError, e
|
56
56
|
rescue Net::HTTP::Persistent::Error => e
|
57
57
|
raise Faraday::TimeoutError, e if e.message.include? 'Timeout'
|
data/lib/faraday/autoload.rb
CHANGED
data/lib/faraday/error.rb
CHANGED
@@ -28,6 +28,18 @@ module Faraday
|
|
28
28
|
%(#<#{self.class}#{inner}>)
|
29
29
|
end
|
30
30
|
|
31
|
+
def response_status
|
32
|
+
@response[:status] if @response
|
33
|
+
end
|
34
|
+
|
35
|
+
def response_headers
|
36
|
+
@response[:headers] if @response
|
37
|
+
end
|
38
|
+
|
39
|
+
def response_body
|
40
|
+
@response[:body] if @response
|
41
|
+
end
|
42
|
+
|
31
43
|
protected
|
32
44
|
|
33
45
|
# Pulls out potential parent exception and response hash, storing them in
|
@@ -112,7 +112,7 @@ module Faraday
|
|
112
112
|
# not independent of the retry count. This would be useful
|
113
113
|
# if the exception produced is non-recoverable or if the
|
114
114
|
# the HTTP method called is not idempotent.
|
115
|
-
# @option options [Block] :retry_block block that is executed
|
115
|
+
# @option options [Block] :retry_block block that is executed before
|
116
116
|
# every retry. Request environment, middleware options, current number
|
117
117
|
# of retries and the exception is passed to the block as parameters.
|
118
118
|
# @option options [Array] :retry_statuses Array of Integer HTTP status
|
data/lib/faraday/version.rb
CHANGED
data/spec/faraday/error_spec.rb
CHANGED
@@ -13,6 +13,7 @@ RSpec.describe Faraday::ClientError do
|
|
13
13
|
it { expect(subject.message).to eq(exception.message) }
|
14
14
|
it { expect(subject.backtrace).to eq(exception.backtrace) }
|
15
15
|
it { expect(subject.inspect).to eq('#<Faraday::ClientError wrapped=#<RuntimeError: test>>') }
|
16
|
+
it { expect(subject.response_status).to be_nil }
|
16
17
|
end
|
17
18
|
|
18
19
|
context 'with response hash' do
|
@@ -22,6 +23,7 @@ RSpec.describe Faraday::ClientError do
|
|
22
23
|
it { expect(subject.response).to eq(exception) }
|
23
24
|
it { expect(subject.message).to eq('the server responded with status 400') }
|
24
25
|
it { expect(subject.inspect).to eq('#<Faraday::ClientError response={:status=>400}>') }
|
26
|
+
it { expect(subject.response_status).to eq(400) }
|
25
27
|
end
|
26
28
|
|
27
29
|
context 'with string' do
|
@@ -31,6 +33,7 @@ RSpec.describe Faraday::ClientError do
|
|
31
33
|
it { expect(subject.response).to be_nil }
|
32
34
|
it { expect(subject.message).to eq('custom message') }
|
33
35
|
it { expect(subject.inspect).to eq('#<Faraday::ClientError #<Faraday::ClientError: custom message>>') }
|
36
|
+
it { expect(subject.response_status).to be_nil }
|
34
37
|
end
|
35
38
|
|
36
39
|
context 'with anything else #to_s' do
|
@@ -40,6 +43,18 @@ RSpec.describe Faraday::ClientError do
|
|
40
43
|
it { expect(subject.response).to be_nil }
|
41
44
|
it { expect(subject.message).to eq('["error1", "error2"]') }
|
42
45
|
it { expect(subject.inspect).to eq('#<Faraday::ClientError #<Faraday::ClientError: ["error1", "error2"]>>') }
|
46
|
+
it { expect(subject.response_status).to be_nil }
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with exception string and response hash' do
|
50
|
+
let(:exception) { 'custom message' }
|
51
|
+
let(:response) { { status: 400 } }
|
52
|
+
|
53
|
+
it { expect(subject.wrapped_exception).to be_nil }
|
54
|
+
it { expect(subject.response).to eq(response) }
|
55
|
+
it { expect(subject.message).to eq('custom message') }
|
56
|
+
it { expect(subject.inspect).to eq('#<Faraday::ClientError response={:status=>400}>') }
|
57
|
+
it { expect(subject.response_status).to eq(400) }
|
43
58
|
end
|
44
59
|
end
|
45
60
|
end
|
@@ -29,6 +29,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
29
29
|
expect(ex.message).to eq('the server responded with status 400')
|
30
30
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
31
31
|
expect(ex.response[:status]).to eq(400)
|
32
|
+
expect(ex.response_status).to eq(400)
|
33
|
+
expect(ex.response_body).to eq('keep looking')
|
34
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
32
35
|
end
|
33
36
|
end
|
34
37
|
|
@@ -37,6 +40,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
37
40
|
expect(ex.message).to eq('the server responded with status 401')
|
38
41
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
39
42
|
expect(ex.response[:status]).to eq(401)
|
43
|
+
expect(ex.response_status).to eq(401)
|
44
|
+
expect(ex.response_body).to eq('keep looking')
|
45
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
40
46
|
end
|
41
47
|
end
|
42
48
|
|
@@ -45,6 +51,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
45
51
|
expect(ex.message).to eq('the server responded with status 403')
|
46
52
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
47
53
|
expect(ex.response[:status]).to eq(403)
|
54
|
+
expect(ex.response_status).to eq(403)
|
55
|
+
expect(ex.response_body).to eq('keep looking')
|
56
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
48
57
|
end
|
49
58
|
end
|
50
59
|
|
@@ -53,6 +62,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
53
62
|
expect(ex.message).to eq('the server responded with status 404')
|
54
63
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
55
64
|
expect(ex.response[:status]).to eq(404)
|
65
|
+
expect(ex.response_status).to eq(404)
|
66
|
+
expect(ex.response_body).to eq('keep looking')
|
67
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
56
68
|
end
|
57
69
|
end
|
58
70
|
|
@@ -61,6 +73,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
61
73
|
expect(ex.message).to eq('407 "Proxy Authentication Required"')
|
62
74
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
63
75
|
expect(ex.response[:status]).to eq(407)
|
76
|
+
expect(ex.response_status).to eq(407)
|
77
|
+
expect(ex.response_body).to eq('keep looking')
|
78
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
64
79
|
end
|
65
80
|
end
|
66
81
|
|
@@ -69,6 +84,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
69
84
|
expect(ex.message).to eq('the server responded with status 409')
|
70
85
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
71
86
|
expect(ex.response[:status]).to eq(409)
|
87
|
+
expect(ex.response_status).to eq(409)
|
88
|
+
expect(ex.response_body).to eq('keep looking')
|
89
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
72
90
|
end
|
73
91
|
end
|
74
92
|
|
@@ -77,6 +95,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
77
95
|
expect(ex.message).to eq('the server responded with status 422')
|
78
96
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
79
97
|
expect(ex.response[:status]).to eq(422)
|
98
|
+
expect(ex.response_status).to eq(422)
|
99
|
+
expect(ex.response_body).to eq('keep looking')
|
100
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
80
101
|
end
|
81
102
|
end
|
82
103
|
|
@@ -85,6 +106,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
85
106
|
expect(ex.message).to eq('http status could not be derived from the server response')
|
86
107
|
expect(ex.response[:headers]['X-Reason']).to eq('nil')
|
87
108
|
expect(ex.response[:status]).to be_nil
|
109
|
+
expect(ex.response_status).to be_nil
|
110
|
+
expect(ex.response_body).to eq('fail')
|
111
|
+
expect(ex.response_headers['X-Reason']).to eq('nil')
|
88
112
|
end
|
89
113
|
end
|
90
114
|
|
@@ -93,6 +117,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
93
117
|
expect(ex.message).to eq('the server responded with status 499')
|
94
118
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
95
119
|
expect(ex.response[:status]).to eq(499)
|
120
|
+
expect(ex.response_status).to eq(499)
|
121
|
+
expect(ex.response_body).to eq('keep looking')
|
122
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
96
123
|
end
|
97
124
|
end
|
98
125
|
|
@@ -101,6 +128,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
101
128
|
expect(ex.message).to eq('the server responded with status 500')
|
102
129
|
expect(ex.response[:headers]['X-Error']).to eq('bailout')
|
103
130
|
expect(ex.response[:status]).to eq(500)
|
131
|
+
expect(ex.response_status).to eq(500)
|
132
|
+
expect(ex.response_body).to eq('fail')
|
133
|
+
expect(ex.response_headers['X-Error']).to eq('bailout')
|
104
134
|
end
|
105
135
|
end
|
106
136
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "@technoweenie"
|
@@ -10,8 +10,22 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2020-12-
|
13
|
+
date: 2020-12-31 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: faraday-net_http
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '1.0'
|
15
29
|
- !ruby/object:Gem::Dependency
|
16
30
|
name: multipart-post
|
17
31
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,7 +80,6 @@ files:
|
|
66
80
|
- lib/faraday/adapter/em_synchrony/parallel_manager.rb
|
67
81
|
- lib/faraday/adapter/excon.rb
|
68
82
|
- lib/faraday/adapter/httpclient.rb
|
69
|
-
- lib/faraday/adapter/net_http.rb
|
70
83
|
- lib/faraday/adapter/net_http_persistent.rb
|
71
84
|
- lib/faraday/adapter/patron.rb
|
72
85
|
- lib/faraday/adapter/rack.rb
|
@@ -159,7 +172,7 @@ licenses:
|
|
159
172
|
- MIT
|
160
173
|
metadata:
|
161
174
|
homepage_uri: https://lostisland.github.io/faraday
|
162
|
-
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.
|
175
|
+
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.3.0
|
163
176
|
source_code_uri: https://github.com/lostisland/faraday
|
164
177
|
bug_tracker_uri: https://github.com/lostisland/faraday/issues
|
165
178
|
post_install_message:
|
@@ -1,220 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
begin
|
4
|
-
require 'net/https'
|
5
|
-
rescue LoadError
|
6
|
-
warn 'Warning: no such file to load -- net/https. ' \
|
7
|
-
'Make sure openssl is installed if you want ssl support'
|
8
|
-
require 'net/http'
|
9
|
-
end
|
10
|
-
require 'zlib'
|
11
|
-
|
12
|
-
module Faraday
|
13
|
-
class Adapter
|
14
|
-
# Net::HTTP adapter.
|
15
|
-
class NetHttp < Faraday::Adapter
|
16
|
-
exceptions = [
|
17
|
-
IOError,
|
18
|
-
Errno::EADDRNOTAVAIL,
|
19
|
-
Errno::ECONNABORTED,
|
20
|
-
Errno::ECONNREFUSED,
|
21
|
-
Errno::ECONNRESET,
|
22
|
-
Errno::EHOSTUNREACH,
|
23
|
-
Errno::EINVAL,
|
24
|
-
Errno::ENETUNREACH,
|
25
|
-
Errno::EPIPE,
|
26
|
-
Net::HTTPBadResponse,
|
27
|
-
Net::HTTPHeaderSyntaxError,
|
28
|
-
Net::ProtocolError,
|
29
|
-
SocketError,
|
30
|
-
Zlib::GzipFile::Error
|
31
|
-
]
|
32
|
-
|
33
|
-
if defined?(::OpenSSL::SSL::SSLError)
|
34
|
-
exceptions << ::OpenSSL::SSL::SSLError
|
35
|
-
end
|
36
|
-
exceptions << ::Net::OpenTimeout if defined?(::Net::OpenTimeout)
|
37
|
-
|
38
|
-
NET_HTTP_EXCEPTIONS = exceptions.freeze
|
39
|
-
|
40
|
-
def initialize(app = nil, opts = {}, &block)
|
41
|
-
@ssl_cert_store = nil
|
42
|
-
super(app, opts, &block)
|
43
|
-
end
|
44
|
-
|
45
|
-
def build_connection(env)
|
46
|
-
net_http_connection(env).tap do |http|
|
47
|
-
if http.respond_to?(:use_ssl=)
|
48
|
-
http.use_ssl = env[:url].scheme == 'https'
|
49
|
-
end
|
50
|
-
configure_ssl(http, env[:ssl])
|
51
|
-
configure_request(http, env[:request])
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def net_http_connection(env)
|
56
|
-
proxy = env[:request][:proxy]
|
57
|
-
port = env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)
|
58
|
-
if proxy
|
59
|
-
Net::HTTP.new(env[:url].hostname, port,
|
60
|
-
proxy[:uri].hostname, proxy[:uri].port,
|
61
|
-
proxy[:user], proxy[:password])
|
62
|
-
else
|
63
|
-
Net::HTTP.new(env[:url].hostname, port, nil)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def call(env)
|
68
|
-
super
|
69
|
-
http_response = connection(env) do |http|
|
70
|
-
begin
|
71
|
-
perform_request(http, env)
|
72
|
-
rescue *NET_HTTP_EXCEPTIONS => e
|
73
|
-
if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
|
74
|
-
raise Faraday::SSLError, e
|
75
|
-
end
|
76
|
-
|
77
|
-
raise Faraday::ConnectionFailed, e
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
save_response(env, http_response.code.to_i,
|
82
|
-
http_response.body || +'', nil,
|
83
|
-
http_response.message) do |response_headers|
|
84
|
-
http_response.each_header do |key, value|
|
85
|
-
response_headers[key] = value
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
@app.call env
|
90
|
-
rescue Timeout::Error, Errno::ETIMEDOUT => e
|
91
|
-
raise Faraday::TimeoutError, e
|
92
|
-
end
|
93
|
-
|
94
|
-
private
|
95
|
-
|
96
|
-
def create_request(env)
|
97
|
-
request = Net::HTTPGenericRequest.new \
|
98
|
-
env[:method].to_s.upcase, # request method
|
99
|
-
!!env[:body], # is there request body
|
100
|
-
env[:method] != :head, # is there response body
|
101
|
-
env[:url].request_uri, # request uri path
|
102
|
-
env[:request_headers] # request headers
|
103
|
-
|
104
|
-
if env[:body].respond_to?(:read)
|
105
|
-
request.body_stream = env[:body]
|
106
|
-
else
|
107
|
-
request.body = env[:body]
|
108
|
-
end
|
109
|
-
request
|
110
|
-
end
|
111
|
-
|
112
|
-
def perform_request(http, env)
|
113
|
-
if env[:request].stream_response?
|
114
|
-
size = 0
|
115
|
-
yielded = false
|
116
|
-
http_response = request_with_wrapped_block(http, env) do |chunk|
|
117
|
-
if chunk.bytesize.positive? || size.positive?
|
118
|
-
yielded = true
|
119
|
-
size += chunk.bytesize
|
120
|
-
env[:request].on_data.call(chunk, size)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
env[:request].on_data.call(+'', 0) unless yielded
|
124
|
-
# Net::HTTP returns something,
|
125
|
-
# but it's not meaningful according to the docs.
|
126
|
-
http_response.body = nil
|
127
|
-
http_response
|
128
|
-
else
|
129
|
-
request_with_wrapped_block(http, env)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
def request_with_wrapped_block(http, env, &block)
|
134
|
-
if (env[:method] == :get) && !env[:body]
|
135
|
-
# prefer `get` to `request` because the former handles gzip (ruby 1.9)
|
136
|
-
request_via_get_method(http, env, &block)
|
137
|
-
else
|
138
|
-
request_via_request_method(http, env, &block)
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
def request_via_get_method(http, env, &block)
|
143
|
-
# Must use Net::HTTP#start and pass it a block otherwise the server's
|
144
|
-
# TCP socket does not close correctly.
|
145
|
-
http.start do |opened_http|
|
146
|
-
opened_http.get env[:url].request_uri, env[:request_headers], &block
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
def request_via_request_method(http, env, &block)
|
151
|
-
# Must use Net::HTTP#start and pass it a block otherwise the server's
|
152
|
-
# TCP socket does not close correctly.
|
153
|
-
http.start do |opened_http|
|
154
|
-
if block_given?
|
155
|
-
opened_http.request create_request(env) do |response|
|
156
|
-
response.read_body(&block)
|
157
|
-
end
|
158
|
-
else
|
159
|
-
opened_http.request create_request(env)
|
160
|
-
end
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
def configure_ssl(http, ssl)
|
165
|
-
return unless ssl
|
166
|
-
|
167
|
-
http.verify_mode = ssl_verify_mode(ssl)
|
168
|
-
http.cert_store = ssl_cert_store(ssl)
|
169
|
-
|
170
|
-
http.cert = ssl[:client_cert] if ssl[:client_cert]
|
171
|
-
http.key = ssl[:client_key] if ssl[:client_key]
|
172
|
-
http.ca_file = ssl[:ca_file] if ssl[:ca_file]
|
173
|
-
http.ca_path = ssl[:ca_path] if ssl[:ca_path]
|
174
|
-
http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
|
175
|
-
http.ssl_version = ssl[:version] if ssl[:version]
|
176
|
-
http.min_version = ssl[:min_version] if ssl[:min_version]
|
177
|
-
http.max_version = ssl[:max_version] if ssl[:max_version]
|
178
|
-
end
|
179
|
-
|
180
|
-
def configure_request(http, req)
|
181
|
-
if (sec = request_timeout(:read, req))
|
182
|
-
http.read_timeout = sec
|
183
|
-
end
|
184
|
-
|
185
|
-
if (sec = http.respond_to?(:write_timeout=) &&
|
186
|
-
request_timeout(:write, req))
|
187
|
-
http.write_timeout = sec
|
188
|
-
end
|
189
|
-
|
190
|
-
if (sec = request_timeout(:open, req))
|
191
|
-
http.open_timeout = sec
|
192
|
-
end
|
193
|
-
|
194
|
-
# Only set if Net::Http supports it, since Ruby 2.5.
|
195
|
-
http.max_retries = 0 if http.respond_to?(:max_retries=)
|
196
|
-
|
197
|
-
@config_block&.call(http)
|
198
|
-
end
|
199
|
-
|
200
|
-
def ssl_cert_store(ssl)
|
201
|
-
return ssl[:cert_store] if ssl[:cert_store]
|
202
|
-
|
203
|
-
@ssl_cert_store ||= begin
|
204
|
-
# Use the default cert store by default, i.e. system ca certs
|
205
|
-
OpenSSL::X509::Store.new.tap(&:set_default_paths)
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
def ssl_verify_mode(ssl)
|
210
|
-
ssl[:verify_mode] || begin
|
211
|
-
if ssl.fetch(:verify, true)
|
212
|
-
OpenSSL::SSL::VERIFY_PEER
|
213
|
-
else
|
214
|
-
OpenSSL::SSL::VERIFY_NONE
|
215
|
-
end
|
216
|
-
end
|
217
|
-
end
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|