faraday-net_http_persistent 1.1.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6b05db120e4bfe54ba8aac6765a61332ec277fec5ef9b51329adee5b9f59bb0f
4
- data.tar.gz: cdc170d7512ff9abe24ef22da6fccc9847a400464fab5cefe62b5e67db36c2c2
3
+ metadata.gz: f0b25e05ab410cbcbabe4c2ffef6bdebbf17b816ab0585299ccfc2ab6e84a6f2
4
+ data.tar.gz: 633f9198253455e34c773e4fc9c1bd0de5f19c464b57fe49181ea422c8e77cad
5
5
  SHA512:
6
- metadata.gz: 48155794e2df01a3920d67e7d0b4a07c5992be88d51dfa76fc7d83a75d20bc44710f8c583523e65fc7ea6f27edd949848a8880d9c87b826ed90df9b07ba0d344
7
- data.tar.gz: 9c38ab8831b88851c36efcc839e170ea2e62612a1d6f70cd669b36e282fd38241f553a1839046c5522d85fc4eb810b316a35e94aec7a02975f504356331fe668
6
+ metadata.gz: de8e7458abed9d8503ba67b4241d64a4b02baa528b316e0e10394dbfe40cfdb0a8a9ba9b71c5dd204c2a8a7c89aed6a529013aa52ee158335962fb46cadb3b01
7
+ data.tar.gz: bed233f50fd228cac2044a5a57892f368d76f779c29b41514abbd3190a10d89ccc08968aa85a9c7b40f8cf44bb014cb1ef7a83a1c1d018b0479538d7307731be
data/README.md CHANGED
@@ -7,36 +7,29 @@ This gem is a [Faraday][faraday] adapter for the [Net::HTTP::Persistent gem][net
7
7
 
8
8
  ## Installation
9
9
 
10
- Add these lines to your application's Gemfile:
10
+ Add this to your application's Gemfile:
11
11
 
12
12
  ```ruby
13
- gem 'faraday-net_http_persistent'
14
- gem 'net-http-persistent', '>= 3.1'
13
+ gem 'faraday-net_http_persistent', '~> 2.0'
15
14
  ```
16
15
 
17
16
  And then execute:
18
17
 
19
18
  $ bundle
20
19
 
21
- Or install them yourself as:
22
-
23
- $ gem install net_http_persistent -v '>= 3.1'
24
- $ gem install faraday-net_http_persistent
25
-
26
20
  ## Usage
27
21
 
28
- Configure your Faraday connection to use this adapter instead of the default one:
29
-
30
22
  ```ruby
31
- connection = Faraday.new(url, conn_options) do |conn|
32
- # Your other middleware goes here...
33
- conn.adapter :net_http_persistent
23
+ require 'faraday/net_http_persistent'
24
+
25
+ conn = Faraday.new(...) do |f|
26
+ f.adapter :net_http_persistent, pool_size: 5 do |http|
27
+ # yields Net::HTTP::Persistent
28
+ http.idle_timeout = 100
29
+ end
34
30
  end
35
31
  ```
36
32
 
37
- For more information on how to setup your Faraday connection and adapters usage,
38
- please refer to the [Faraday Website][faraday-website].
39
-
40
33
  ## Development
41
34
 
42
35
  After checking out the repo, run `bin/setup` to install dependencies.
@@ -62,9 +55,9 @@ The gem is available as open source under the terms of the [MIT License][mit-lic
62
55
 
63
56
  This project is intended to be a safe, welcoming space for collaboration.
64
57
  Everyone interacting in the Faraday::Http project’s codebases, issue trackers,
65
- chat rooms and mailing lists is expected to follow the [code of conduct].
58
+ chat rooms and mailing lists is expected to follow the [code of conduct][code-of-conduct].
66
59
 
67
- [code-of-conduct]: https://github.com/lostisland/faraday-http/blob/master/.github/CODE_OF_CONDUCT.md
60
+ [code-of-conduct]: https://github.com/lostisland/faraday-http/blob/main/CODE_OF_CONDUCT.md
68
61
  [covenant]: http://contributor-covenant.org
69
62
  [faraday]: https://github.com/lostisland/faraday
70
63
  [faraday-website]: https://lostisland.github.io/faraday
@@ -1,33 +1,143 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "net/http/persistent"
4
+
3
5
  module Faraday
4
6
  class Adapter
5
7
  # Net::HTTP::Persistent adapter.
6
- class NetHttpPersistent < NetHttp
7
- dependency "net/http/persistent"
8
+ class NetHttpPersistent < Faraday::Adapter
9
+ exceptions = [
10
+ IOError,
11
+ Errno::EADDRNOTAVAIL,
12
+ Errno::EALREADY,
13
+ Errno::ECONNABORTED,
14
+ Errno::ECONNREFUSED,
15
+ Errno::ECONNRESET,
16
+ Errno::EHOSTUNREACH,
17
+ Errno::EINVAL,
18
+ Errno::ENETUNREACH,
19
+ Errno::EPIPE,
20
+ Net::HTTPBadResponse,
21
+ Net::HTTPHeaderSyntaxError,
22
+ Net::ProtocolError,
23
+ SocketError,
24
+ Zlib::GzipFile::Error
25
+ ]
26
+
27
+ exceptions << ::OpenSSL::SSL::SSLError if defined?(::OpenSSL::SSL::SSLError)
28
+ # TODO (breaking): Enable this to make it consistent with net_http adapter.
29
+ # See https://github.com/lostisland/faraday/issues/718#issuecomment-344549382
30
+ # exceptions << ::Net::OpenTimeout if defined?(::Net::OpenTimeout)
31
+
32
+ NET_HTTP_EXCEPTIONS = exceptions.freeze
33
+
34
+ def initialize(app = nil, opts = {}, &block)
35
+ @ssl_cert_store = nil
36
+ super(app, opts, &block)
37
+ end
38
+
39
+ def call(env)
40
+ super
41
+ connection(env) do |http|
42
+ perform_request(http, env)
43
+ rescue Net::HTTP::Persistent::Error => e
44
+ raise Faraday::TimeoutError, e if e.message.include?("Timeout")
45
+
46
+ raise Faraday::ConnectionFailed, e if e.message.include?("connection refused")
47
+
48
+ raise
49
+ rescue *NET_HTTP_EXCEPTIONS => e
50
+ raise Faraday::SSLError, e if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
51
+
52
+ raise Faraday::ConnectionFailed, e
53
+ end
54
+ @app.call env
55
+ rescue Timeout::Error, Errno::ETIMEDOUT => e
56
+ raise Faraday::TimeoutError, e
57
+ end
58
+
59
+ def close
60
+ @cached_connection&.shutdown
61
+ end
8
62
 
9
63
  private
10
64
 
11
- def net_http_connection(env)
12
- @cached_connection ||=
13
- if Net::HTTP::Persistent.instance_method(:initialize)
14
- .parameters.first == %i[key name]
15
- options = {name: "Faraday"}
16
- if @connection_options.key?(:pool_size)
17
- options[:pool_size] = @connection_options[:pool_size]
18
- end
19
- Net::HTTP::Persistent.new(**options)
20
- else
21
- Net::HTTP::Persistent.new("Faraday")
65
+ def build_connection(env)
66
+ net_http_connection(env).tap do |http|
67
+ http.use_ssl = env[:url].scheme == "https" if http.respond_to?(:use_ssl=)
68
+ configure_ssl(http, env[:ssl])
69
+ configure_request(http, env[:request])
70
+ end
71
+ end
72
+
73
+ def create_request(env)
74
+ request = Net::HTTPGenericRequest.new \
75
+ env[:method].to_s.upcase, # request method
76
+ !!env[:body], # is there request body
77
+ env[:method] != :head, # is there response body
78
+ env[:url].request_uri, # request uri path
79
+ env[:request_headers] # request headers
80
+
81
+ if env[:body].respond_to?(:read)
82
+ request.body_stream = env[:body]
83
+ else
84
+ request.body = env[:body]
85
+ end
86
+ request
87
+ end
88
+
89
+ def save_http_response(env, http_response)
90
+ save_response(
91
+ env, http_response.code.to_i, nil, nil, http_response.message, finished: false
92
+ ) do |response_headers|
93
+ http_response.each_header do |key, value|
94
+ response_headers[key] = value
22
95
  end
96
+ end
97
+ end
23
98
 
24
- proxy_uri = proxy_uri(env)
25
- if @cached_connection.proxy_uri != proxy_uri
26
- @cached_connection.proxy = proxy_uri
99
+ def configure_request(http, req)
100
+ if (sec = request_timeout(:read, req))
101
+ http.read_timeout = sec
102
+ end
103
+
104
+ if (sec = http.respond_to?(:write_timeout=) &&
105
+ request_timeout(:write, req))
106
+ http.write_timeout = sec
107
+ end
108
+
109
+ if (sec = request_timeout(:open, req))
110
+ http.open_timeout = sec
27
111
  end
112
+
113
+ # Only set if Net::Http supports it, since Ruby 2.5.
114
+ http.max_retries = 0 if http.respond_to?(:max_retries=)
115
+
116
+ @config_block&.call(http)
117
+ end
118
+
119
+ def ssl_cert_store(ssl)
120
+ return ssl[:cert_store] if ssl[:cert_store]
121
+
122
+ # Use the default cert store by default, i.e. system ca certs
123
+ @ssl_cert_store ||= OpenSSL::X509::Store.new.tap(&:set_default_paths)
124
+ end
125
+
126
+ def net_http_connection(env)
127
+ @cached_connection ||= Net::HTTP::Persistent.new(**init_options)
128
+
129
+ proxy_uri = proxy_uri(env)
130
+ @cached_connection.proxy = proxy_uri if @cached_connection.proxy_uri != proxy_uri
28
131
  @cached_connection
29
132
  end
30
133
 
134
+ def init_options
135
+ options = {}
136
+ options[:name] = @connection_options.fetch(:name, "Faraday")
137
+ options[:pool_size] = @connection_options[:pool_size] if @connection_options.key?(:pool_size)
138
+ options
139
+ end
140
+
31
141
  def proxy_uri(env)
32
142
  proxy_uri = nil
33
143
  if (proxy = env[:request][:proxy])
@@ -36,31 +146,36 @@ module Faraday
36
146
  else
37
147
  ::URI.parse(proxy[:uri].to_s)
38
148
  end
39
- proxy_uri.user = proxy_uri.password = nil
40
- # awful patch for net-http-persistent 2.8
41
- # not unescaping user/password
42
- if proxy[:user]
43
- (class << proxy_uri; self; end).class_eval do
44
- define_method(:user) { proxy[:user] }
45
- define_method(:password) { proxy[:password] }
46
- end
47
- end
149
+ proxy_uri.user = proxy[:user] if proxy[:user]
150
+ proxy_uri.password = proxy[:password] if proxy[:password]
48
151
  end
49
152
  proxy_uri
50
153
  end
51
154
 
52
155
  def perform_request(http, env)
53
- http.request env[:url], create_request(env)
54
- rescue Errno::ETIMEDOUT, Net::OpenTimeout => e
55
- raise Faraday::TimeoutError, e
56
- rescue Net::HTTP::Persistent::Error => e
57
- raise Faraday::TimeoutError, e if e.message.include? "Timeout"
58
-
59
- if e.message.include? "connection refused"
60
- raise Faraday::ConnectionFailed, e
156
+ if env.stream_response?
157
+ http_response = env.stream_response do |&on_data|
158
+ request_with_wrapped_block(http, env, &on_data)
159
+ end
160
+ http_response.body = nil
161
+ else
162
+ http_response = request_with_wrapped_block(http, env)
61
163
  end
164
+ env.response_body = encoded_body(http_response)
165
+ env.response.finish(env)
166
+ http_response
167
+ end
62
168
 
63
- raise
169
+ def request_with_wrapped_block(http, env)
170
+ http.request(env[:url], create_request(env)) do |response|
171
+ save_http_response(env, response)
172
+
173
+ if block_given?
174
+ response.read_body do |chunk|
175
+ yield(chunk)
176
+ end
177
+ end
178
+ end
64
179
  end
65
180
 
66
181
  SSL_CONFIGURATIONS = {
@@ -69,8 +184,15 @@ module Faraday
69
184
  ca_file: :ca_file,
70
185
  ssl_version: :version,
71
186
  min_version: :min_version,
72
- max_version: :max_version
73
- }.freeze
187
+ max_version: :max_version,
188
+ verify_hostname: :verify_hostname
189
+ }
190
+
191
+ if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new("2.11.0")
192
+ SSL_CONFIGURATIONS[:ciphers] = :ciphers
193
+ end
194
+
195
+ SSL_CONFIGURATIONS.freeze
74
196
 
75
197
  def configure_ssl(http, ssl)
76
198
  return unless ssl
@@ -79,13 +201,34 @@ module Faraday
79
201
  http_set(http, :cert_store, ssl_cert_store(ssl))
80
202
 
81
203
  SSL_CONFIGURATIONS
82
- .select { |_, key| ssl[key] }
204
+ .select { |_, key| !ssl[key].nil? }
83
205
  .each { |target, key| http_set(http, target, ssl[key]) }
84
206
  end
85
207
 
86
208
  def http_set(http, attr, value)
87
209
  http.send("#{attr}=", value) if http.send(attr) != value
88
210
  end
211
+
212
+ def ssl_verify_mode(ssl)
213
+ ssl[:verify_mode] ||
214
+ if ssl.fetch(:verify, true)
215
+ OpenSSL::SSL::VERIFY_PEER
216
+ else
217
+ OpenSSL::SSL::VERIFY_NONE
218
+ end
219
+ end
220
+
221
+ def encoded_body(http_response)
222
+ body = http_response.body || +""
223
+ /\bcharset=\s*(.+?)\s*(;|$)/.match(http_response["Content-Type"]) do |match|
224
+ content_charset = ::Encoding.find(match.captures.first)
225
+ body = body.dup if body.frozen?
226
+ body.force_encoding(content_charset)
227
+ rescue ArgumentError
228
+ nil
229
+ end
230
+ body
231
+ end
89
232
  end
90
233
  end
91
234
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  module NetHttpPersistent
5
- VERSION = "1.1.0"
5
+ VERSION = "2.3.0"
6
6
  end
7
7
  end
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "adapter/net_http_persistent"
4
- require_relative "net_http_persistent/version"
3
+ require "faraday"
4
+ require "faraday/adapter/net_http_persistent"
5
+ require "faraday/net_http_persistent/version"
5
6
 
6
7
  module Faraday
7
8
  module NetHttpPersistent
metadata CHANGED
@@ -1,141 +1,49 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-net_http_persistent
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Rogers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-18 00:00:00.000000000 Z
11
+ date: 2024-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: net-http-persistent
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '3.1'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '3.1'
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '2.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '2.0'
41
13
  - !ruby/object:Gem::Dependency
42
14
  name: faraday
43
15
  requirement: !ruby/object:Gem::Requirement
44
16
  requirements:
45
17
  - - "~>"
46
18
  - !ruby/object:Gem::Version
47
- version: '1.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '1.0'
55
- - !ruby/object:Gem::Dependency
56
- name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '13.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '13.0'
69
- - !ruby/object:Gem::Dependency
70
- name: rspec
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '3.0'
76
- type: :development
19
+ version: '2.5'
20
+ type: :runtime
77
21
  prerelease: false
78
22
  version_requirements: !ruby/object:Gem::Requirement
79
23
  requirements:
80
24
  - - "~>"
81
25
  - !ruby/object:Gem::Version
82
- version: '3.0'
26
+ version: '2.5'
83
27
  - !ruby/object:Gem::Dependency
84
- name: simplecov
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: 0.19.0
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: 0.19.0
97
- - !ruby/object:Gem::Dependency
98
- name: standardrb
28
+ name: net-http-persistent
99
29
  requirement: !ruby/object:Gem::Requirement
100
30
  requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: '1.0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
31
+ - - ">="
109
32
  - !ruby/object:Gem::Version
110
- version: '1.0'
111
- - !ruby/object:Gem::Dependency
112
- name: multipart-parser
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - "~>"
33
+ version: 4.0.4
34
+ - - "<"
116
35
  - !ruby/object:Gem::Version
117
- version: 0.1.1
118
- type: :development
36
+ version: '5'
37
+ type: :runtime
119
38
  prerelease: false
120
39
  version_requirements: !ruby/object:Gem::Requirement
121
40
  requirements:
122
- - - "~>"
123
- - !ruby/object:Gem::Version
124
- version: 0.1.1
125
- - !ruby/object:Gem::Dependency
126
- name: webmock
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - "~>"
41
+ - - ">="
130
42
  - !ruby/object:Gem::Version
131
- version: '3.4'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - "~>"
43
+ version: 4.0.4
44
+ - - "<"
137
45
  - !ruby/object:Gem::Version
138
- version: '3.4'
46
+ version: '5'
139
47
  description: Faraday adapter for NetHttpPersistent
140
48
  email:
141
49
  - me@mikerogers.io
@@ -154,7 +62,7 @@ licenses:
154
62
  metadata:
155
63
  homepage_uri: https://github.com/lostisland/faraday-net_http_persistent
156
64
  source_code_uri: https://github.com/lostisland/faraday-net_http_persistent
157
- changelog_uri: https://github.com/lostisland/faraday-net_http_persistent
65
+ changelog_uri: https://github.com/lostisland/faraday-net_http_persistent/releases/tag/v2.3.0
158
66
  post_install_message:
159
67
  rdoc_options: []
160
68
  require_paths:
@@ -163,7 +71,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
163
71
  requirements:
164
72
  - - ">="
165
73
  - !ruby/object:Gem::Version
166
- version: 2.4.0
74
+ version: 2.6.0
167
75
  required_rubygems_version: !ruby/object:Gem::Requirement
168
76
  requirements:
169
77
  - - ">="