faraday-net_http_persistent 2.0.2 → 2.2.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: 0dd625c45c96455b07b3ea4a2345006c6bf18ace0e91e1d29d8760c32a851880
4
- data.tar.gz: 6413a859583e2dc5d94916c32873899096b7c7cd44ea47bb64f19623e4e5cec5
3
+ metadata.gz: 191595752d82777e7edb7c18a0b3fd85953281b7c65ebb5e11f9fa3a0a36326f
4
+ data.tar.gz: 9bc82207a4020fe7b8df7fcee8e35bdd30308ad1555ff060ce87ff050f641c06
5
5
  SHA512:
6
- metadata.gz: 5a847b8b8bf9131ee264b9878d3aca9748f358cd01ef432954e42a97847c9981e482cfc8914925c18680617c2016b8b28df5e593c28c4b5c9cc23a00b9337f47
7
- data.tar.gz: 9487254402d21fe7be681a15a7212586fbf84995f2ac0ff764cb7de4a306e70bd87a8dd70ab915920a716c22b2021c83dc4bf89664294cd5f072941b57379bd2
6
+ metadata.gz: 8c6c9f078bef0572b9831b7e16fda71a331be9297b21c3d6dbb6c36713650dfe37ac5a4d2866977f9b4af87bbb06d0394f78a378c92fd0b60d50dd5a88feb378
7
+ data.tar.gz: fbeb087ef43f055a51168af96b6eab2d0fbb0f28f95548677de7cc5d543ec042a7f757d5094ba422eb14e6215a30814663ba87f5e274d4cea0554295cf5665f3
@@ -1,30 +1,142 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "faraday/net_http"
4
3
  require "net/http/persistent"
5
4
 
6
5
  module Faraday
7
6
  class Adapter
8
7
  # Net::HTTP::Persistent adapter.
9
- class NetHttpPersistent < NetHttp
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
62
+
10
63
  private
11
64
 
12
- def net_http_connection(env)
13
- @cached_connection ||=
14
- if Net::HTTP::Persistent.instance_method(:initialize)
15
- .parameters.first == %i[key name]
16
- options = {name: "Faraday"}
17
- options[:pool_size] = @connection_options[:pool_size] if @connection_options.key?(:pool_size)
18
- Net::HTTP::Persistent.new(**options)
19
- else
20
- 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
21
95
  end
96
+ end
97
+ end
98
+
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
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)
22
128
 
23
129
  proxy_uri = proxy_uri(env)
24
130
  @cached_connection.proxy = proxy_uri if @cached_connection.proxy_uri != proxy_uri
25
131
  @cached_connection
26
132
  end
27
133
 
134
+ def init_options
135
+ options = {name: "Faraday"}
136
+ options[:pool_size] = @connection_options[:pool_size] if @connection_options.key?(:pool_size)
137
+ options
138
+ end
139
+
28
140
  def proxy_uri(env)
29
141
  proxy_uri = nil
30
142
  if (proxy = env[:request][:proxy])
@@ -33,48 +145,36 @@ module Faraday
33
145
  else
34
146
  ::URI.parse(proxy[:uri].to_s)
35
147
  end
36
- proxy_uri.user = proxy_uri.password = nil
37
- # awful patch for net-http-persistent 2.8
38
- # not unescaping user/password
39
- if proxy[:user]
40
- (class << proxy_uri; self; end).class_eval do
41
- define_method(:user) { proxy[:user] }
42
- define_method(:password) { proxy[:password] }
43
- end
44
- end
148
+ proxy_uri.user = proxy[:user] if proxy[:user]
149
+ proxy_uri.password = proxy[:password] if proxy[:password]
45
150
  end
46
151
  proxy_uri
47
152
  end
48
153
 
49
154
  def perform_request(http, env)
50
- if env[:request].stream_response?
51
- size = 0
52
- yielded = false
53
-
54
- http_response = http.request(env[:url], create_request(env)) do |response|
55
- response.read_body do |chunk|
56
- if chunk.bytesize.positive? || size.positive?
57
- yielded = true
58
- size += chunk.bytesize
59
- env[:request].on_data.call(chunk, size)
60
- end
61
- end
155
+ if env.stream_response?
156
+ http_response = env.stream_response do |&on_data|
157
+ request_with_wrapped_block(http, env, &on_data)
62
158
  end
63
-
64
- env[:request].on_data.call(+"", 0) unless yielded
65
159
  http_response.body = nil
66
- http_response
67
160
  else
68
- http.request(env[:url], create_request(env))
161
+ http_response = request_with_wrapped_block(http, env)
69
162
  end
70
- rescue Errno::ETIMEDOUT, Net::OpenTimeout => e
71
- raise Faraday::TimeoutError, e
72
- rescue Net::HTTP::Persistent::Error => e
73
- raise Faraday::TimeoutError, e if e.message.include? "Timeout"
163
+ env.response_body = encoded_body(http_response)
164
+ env.response.finish(env)
165
+ http_response
166
+ end
74
167
 
75
- raise Faraday::ConnectionFailed, e if e.message.include? "connection refused"
168
+ def request_with_wrapped_block(http, env)
169
+ http.request(env[:url], create_request(env)) do |response|
170
+ save_http_response(env, response)
76
171
 
77
- raise
172
+ if block_given?
173
+ response.read_body do |chunk|
174
+ yield(chunk)
175
+ end
176
+ end
177
+ end
78
178
  end
79
179
 
80
180
  SSL_CONFIGURATIONS = {
@@ -83,7 +183,8 @@ module Faraday
83
183
  ca_file: :ca_file,
84
184
  ssl_version: :version,
85
185
  min_version: :min_version,
86
- max_version: :max_version
186
+ max_version: :max_version,
187
+ verify_hostname: :verify_hostname
87
188
  }.freeze
88
189
 
89
190
  def configure_ssl(http, ssl)
@@ -93,13 +194,34 @@ module Faraday
93
194
  http_set(http, :cert_store, ssl_cert_store(ssl))
94
195
 
95
196
  SSL_CONFIGURATIONS
96
- .select { |_, key| ssl[key] }
197
+ .select { |_, key| !ssl[key].nil? }
97
198
  .each { |target, key| http_set(http, target, ssl[key]) }
98
199
  end
99
200
 
100
201
  def http_set(http, attr, value)
101
202
  http.send("#{attr}=", value) if http.send(attr) != value
102
203
  end
204
+
205
+ def ssl_verify_mode(ssl)
206
+ ssl[:verify_mode] ||
207
+ if ssl.fetch(:verify, true)
208
+ OpenSSL::SSL::VERIFY_PEER
209
+ else
210
+ OpenSSL::SSL::VERIFY_NONE
211
+ end
212
+ end
213
+
214
+ def encoded_body(http_response)
215
+ body = http_response.body || +""
216
+ /\bcharset=\s*(.+?)\s*(;|$)/.match(http_response["Content-Type"]) do |match|
217
+ content_charset = ::Encoding.find(match.captures.first)
218
+ body = body.dup if body.frozen?
219
+ body.force_encoding(content_charset)
220
+ rescue ArgumentError
221
+ nil
222
+ end
223
+ body
224
+ end
103
225
  end
104
226
  end
105
227
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  module NetHttpPersistent
5
- VERSION = "2.0.2"
5
+ VERSION = "2.2.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,43 +1,49 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-net_http_persistent
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.2.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: 2022-08-09 00:00:00.000000000 Z
11
+ date: 2024-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: faraday-net_http
14
+ name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "<"
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3'
19
+ version: '2.5'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "<"
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3'
26
+ version: '2.5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: net-http-persistent
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '4.0'
33
+ version: 4.0.4
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '5'
34
37
  type: :runtime
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
37
40
  requirements:
38
- - - "~>"
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 4.0.4
44
+ - - "<"
39
45
  - !ruby/object:Gem::Version
40
- version: '4.0'
46
+ version: '5'
41
47
  description: Faraday adapter for NetHttpPersistent
42
48
  email:
43
49
  - me@mikerogers.io
@@ -56,7 +62,7 @@ licenses:
56
62
  metadata:
57
63
  homepage_uri: https://github.com/lostisland/faraday-net_http_persistent
58
64
  source_code_uri: https://github.com/lostisland/faraday-net_http_persistent
59
- changelog_uri: https://github.com/lostisland/faraday-net_http_persistent/releases/tag/v2.0.2
65
+ changelog_uri: https://github.com/lostisland/faraday-net_http_persistent/releases/tag/v2.2.0
60
66
  post_install_message:
61
67
  rdoc_options: []
62
68
  require_paths: