faraday-net_http_persistent 1.2.0 → 2.1.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: f1bc1f8930b55722004909277859a02d2b792fb7efdd09381a852c06085eeb7f
4
- data.tar.gz: 7ad77ee2a562d6479e57c361366ab3753e5f32e179839c00bff89232dce4d3f8
3
+ metadata.gz: ffe757e1437110d467bf12e5b02dfb9c645148637fda7dbd819960bc606a083f
4
+ data.tar.gz: ea0f218c8a5cc9fff614d7642148059e98c3664fb66a8c9a82b520c19aa4f74a
5
5
  SHA512:
6
- metadata.gz: 62fd182c8b193d450544f3d655b87f01a552fe6845e3f67094b1d0f5b94904713ed0f11ea185af40a7bd170c4cef69adad69e1cf4982e7a5b2eedb23de7e12bf
7
- data.tar.gz: d6c37fe53505c0bf528b4451b9494d4265f2e37e23e38967e18c7c65d92b5c6c9bbc8fe9c2a8606e555a356085b543750f8c72449e9344acb3feebbb903a7d78
6
+ metadata.gz: 704efa657b1583d555b29a6cc71d75e936dc2dab9ac419d09d92a0a433eba72470f48b7dede5a005c8604a7a36dae62b86c53249be1090819df472a3bee55542
7
+ data.tar.gz: ec494b194dbbf5dd42c91166a2cd17de25d57d7cf0155cabaf4c9dda60d906592cd8fa8558b7a6d34b523e3bedaeeaffa39b03beedfc56459bdc6bc454a6a8f0
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.
@@ -1,33 +1,138 @@
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
8
58
 
9
59
  private
10
60
 
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")
61
+ def build_connection(env)
62
+ net_http_connection(env).tap do |http|
63
+ http.use_ssl = env[:url].scheme == "https" if http.respond_to?(:use_ssl=)
64
+ configure_ssl(http, env[:ssl])
65
+ configure_request(http, env[:request])
66
+ end
67
+ end
68
+
69
+ def create_request(env)
70
+ request = Net::HTTPGenericRequest.new \
71
+ env[:method].to_s.upcase, # request method
72
+ !!env[:body], # is there request body
73
+ env[:method] != :head, # is there response body
74
+ env[:url].request_uri, # request uri path
75
+ env[:request_headers] # request headers
76
+
77
+ if env[:body].respond_to?(:read)
78
+ request.body_stream = env[:body]
79
+ else
80
+ request.body = env[:body]
81
+ end
82
+ request
83
+ end
84
+
85
+ def save_http_response(env, http_response)
86
+ save_response(
87
+ env, http_response.code.to_i, nil, nil, http_response.message, finished: false
88
+ ) do |response_headers|
89
+ http_response.each_header do |key, value|
90
+ response_headers[key] = value
22
91
  end
92
+ end
93
+ end
23
94
 
24
- proxy_uri = proxy_uri(env)
25
- if @cached_connection.proxy_uri != proxy_uri
26
- @cached_connection.proxy = proxy_uri
95
+ def configure_request(http, req)
96
+ if (sec = request_timeout(:read, req))
97
+ http.read_timeout = sec
98
+ end
99
+
100
+ if (sec = http.respond_to?(:write_timeout=) &&
101
+ request_timeout(:write, req))
102
+ http.write_timeout = sec
103
+ end
104
+
105
+ if (sec = request_timeout(:open, req))
106
+ http.open_timeout = sec
27
107
  end
108
+
109
+ # Only set if Net::Http supports it, since Ruby 2.5.
110
+ http.max_retries = 0 if http.respond_to?(:max_retries=)
111
+
112
+ @config_block&.call(http)
113
+ end
114
+
115
+ def ssl_cert_store(ssl)
116
+ return ssl[:cert_store] if ssl[:cert_store]
117
+
118
+ # Use the default cert store by default, i.e. system ca certs
119
+ @ssl_cert_store ||= OpenSSL::X509::Store.new.tap(&:set_default_paths)
120
+ end
121
+
122
+ def net_http_connection(env)
123
+ @cached_connection ||= Net::HTTP::Persistent.new(**init_options)
124
+
125
+ proxy_uri = proxy_uri(env)
126
+ @cached_connection.proxy = proxy_uri if @cached_connection.proxy_uri != proxy_uri
28
127
  @cached_connection
29
128
  end
30
129
 
130
+ def init_options
131
+ options = {name: "Faraday"}
132
+ options[:pool_size] = @connection_options[:pool_size] if @connection_options.key?(:pool_size)
133
+ options
134
+ end
135
+
31
136
  def proxy_uri(env)
32
137
  proxy_uri = nil
33
138
  if (proxy = env[:request][:proxy])
@@ -36,50 +141,36 @@ module Faraday
36
141
  else
37
142
  ::URI.parse(proxy[:uri].to_s)
38
143
  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
144
+ proxy_uri.user = proxy[:user] if proxy[:user]
145
+ proxy_uri.password = proxy[:password] if proxy[:password]
48
146
  end
49
147
  proxy_uri
50
148
  end
51
149
 
52
150
  def perform_request(http, env)
53
- if env[:request].stream_response?
54
- size = 0
55
- yielded = false
56
-
57
- http_response = http.request(env[:url], create_request(env)) do |response|
58
- response.read_body do |chunk|
59
- if chunk.bytesize.positive? || size.positive?
60
- yielded = true
61
- size += chunk.bytesize
62
- env[:request].on_data.call(chunk, size)
63
- end
64
- end
151
+ if env.stream_response?
152
+ http_response = env.stream_response do |&on_data|
153
+ request_with_wrapped_block(http, env, &on_data)
65
154
  end
66
-
67
- env[:request].on_data.call(+"", 0) unless yielded
68
155
  http_response.body = nil
69
- http_response
70
156
  else
71
- http.request(env[:url], create_request(env))
157
+ http_response = request_with_wrapped_block(http, env)
72
158
  end
73
- rescue Errno::ETIMEDOUT, Net::OpenTimeout => e
74
- raise Faraday::TimeoutError, e
75
- rescue Net::HTTP::Persistent::Error => e
76
- raise Faraday::TimeoutError, e if e.message.include? "Timeout"
159
+ env.response_body = encoded_body(http_response)
160
+ env.response.finish(env)
161
+ http_response
162
+ end
77
163
 
78
- if e.message.include? "connection refused"
79
- raise Faraday::ConnectionFailed, e
80
- end
164
+ def request_with_wrapped_block(http, env)
165
+ http.request(env[:url], create_request(env)) do |response|
166
+ save_http_response(env, response)
81
167
 
82
- raise
168
+ if block_given?
169
+ response.read_body do |chunk|
170
+ yield(chunk)
171
+ end
172
+ end
173
+ end
83
174
  end
84
175
 
85
176
  SSL_CONFIGURATIONS = {
@@ -105,6 +196,27 @@ module Faraday
105
196
  def http_set(http, attr, value)
106
197
  http.send("#{attr}=", value) if http.send(attr) != value
107
198
  end
199
+
200
+ def ssl_verify_mode(ssl)
201
+ ssl[:verify_mode] ||
202
+ if ssl.fetch(:verify, true)
203
+ OpenSSL::SSL::VERIFY_PEER
204
+ else
205
+ OpenSSL::SSL::VERIFY_NONE
206
+ end
207
+ end
208
+
209
+ def encoded_body(http_response)
210
+ body = http_response.body || +""
211
+ /\bcharset=\s*(.+?)\s*(;|$)/.match(http_response["Content-Type"]) do |match|
212
+ content_charset = ::Encoding.find(match.captures.first)
213
+ body = body.dup if body.frozen?
214
+ body.force_encoding(content_charset)
215
+ rescue ArgumentError
216
+ nil
217
+ end
218
+ body
219
+ end
108
220
  end
109
221
  end
110
222
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  module NetHttpPersistent
5
- VERSION = "1.2.0"
5
+ VERSION = "2.1.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,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-net_http_persistent
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 2.1.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-07-12 00:00:00.000000000 Z
11
+ date: 2022-08-11 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
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '3.0'
83
- - !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
19
+ version: '2.5'
20
+ type: :runtime
91
21
  prerelease: false
92
22
  version_requirements: !ruby/object:Gem::Requirement
93
23
  requirements:
94
24
  - - "~>"
95
25
  - !ruby/object:Gem::Version
96
- version: 0.19.0
26
+ version: '2.5'
97
27
  - !ruby/object:Gem::Dependency
98
- name: standardrb
99
- requirement: !ruby/object:Gem::Requirement
100
- 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
- - - "~>"
109
- - !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
- - - "~>"
116
- - !ruby/object:Gem::Version
117
- version: 0.1.1
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - "~>"
123
- - !ruby/object:Gem::Version
124
- version: 0.1.1
125
- - !ruby/object:Gem::Dependency
126
- name: webmock
28
+ name: net-http-persistent
127
29
  requirement: !ruby/object:Gem::Requirement
128
30
  requirements:
129
31
  - - "~>"
130
32
  - !ruby/object:Gem::Version
131
- version: '3.4'
132
- type: :development
33
+ version: '4.0'
34
+ type: :runtime
133
35
  prerelease: false
134
36
  version_requirements: !ruby/object:Gem::Requirement
135
37
  requirements:
136
38
  - - "~>"
137
39
  - !ruby/object:Gem::Version
138
- version: '3.4'
40
+ version: '4.0'
139
41
  description: Faraday adapter for NetHttpPersistent
140
42
  email:
141
43
  - me@mikerogers.io
@@ -154,7 +56,7 @@ licenses:
154
56
  metadata:
155
57
  homepage_uri: https://github.com/lostisland/faraday-net_http_persistent
156
58
  source_code_uri: https://github.com/lostisland/faraday-net_http_persistent
157
- changelog_uri: https://github.com/lostisland/faraday-net_http_persistent
59
+ changelog_uri: https://github.com/lostisland/faraday-net_http_persistent/releases/tag/v2.1.0
158
60
  post_install_message:
159
61
  rdoc_options: []
160
62
  require_paths:
@@ -163,7 +65,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
163
65
  requirements:
164
66
  - - ">="
165
67
  - !ruby/object:Gem::Version
166
- version: 2.4.0
68
+ version: 2.6.0
167
69
  required_rubygems_version: !ruby/object:Gem::Requirement
168
70
  requirements:
169
71
  - - ">="