faraday-net_http 3.0.2 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9cad2197cf7df07a668741fb5ae27309b6f7aee396b207268ba63076569ae59f
4
- data.tar.gz: 47fbe72a4c70f85b5659376f0a758088d26c8a6f2901a8320a22bfba872e2c1f
3
+ metadata.gz: 658606c949eb5a714696b89cf5c75cace8c212e9b2df7688ecc5d1bb5ab6442c
4
+ data.tar.gz: 7f52a4ae649cfd20426dc480511ed77c8fa0a2ad8d52e76d9c851516d356d1e2
5
5
  SHA512:
6
- metadata.gz: 7b6cb45789018beedcef079c136c213e6532aba87d2503bd6f1544cf30f3ab3cdda9c08e4164d0f93a056595622cdf9c8f125e19798f8147e3e52f1387c5c800
7
- data.tar.gz: 66131665eb559ad8b1bbba18c12916c3ace677b93a6055269c7def945dced9021f2864df22d9326880a8379602e0ac78fd44f49c137217e05978fa7377cedb3d
6
+ metadata.gz: 3f8aa0043df43b4562a9aa1e2d519e2a3a34e93ca30a293f0a84448c123c518c02bafe21ac79bb0f82d7a41e6e76fd53636f1d5b5db1ef53692001d9bcc658a2
7
+ data.tar.gz: accb8c3da7c1dcf7ebc22d53495d7f438cd1aea2ca625f2d8d5ec22401ddda66e7a013b2bfeffcb3003374f79c18d1c0276be72f117ebb60b57ab8117bfc7d6c
data/README.md CHANGED
@@ -31,11 +31,44 @@ conn = Faraday.new(...) do |f|
31
31
  end
32
32
  ```
33
33
 
34
+ ### Restricting trusted certificate authorities
35
+
36
+ By default, `ca_file` and `ca_path` add certificate authorities to the system
37
+ trust store. They do not restrict trust to only the supplied authorities.
38
+ To restrict trust, provide an explicit certificate store. An empty store lets
39
+ you trust only the authorities supplied through `ca_file` or `ca_path`:
40
+
41
+ ```ruby
42
+ require 'openssl'
43
+
44
+ conn = Faraday.new('https://example.com', ssl: {
45
+ ca_file: '/path/to/trusted-ca.pem',
46
+ cert_store: OpenSSL::X509::Store.new
47
+ })
48
+ ```
49
+
50
+ Certificate and hostname verification remain enabled. Alternatively, populate
51
+ the explicit store yourself with `add_file` or `add_cert` and omit `ca_file`.
52
+
53
+ ### SOCKS proxy
54
+
55
+ A `socks://` proxy is supported when the optional [socksify][socksify] gem (`~> 1.8`) is installed. It is not a dependency of this gem, so add it to your Gemfile:
56
+
57
+ ```ruby
58
+ gem 'socksify', '~> 1.8'
59
+ ```
60
+
61
+ ```ruby
62
+ conn = Faraday.new(url, proxy: 'socks://user:password@127.0.0.1:1080')
63
+ ```
64
+
65
+ Using a `socks://` proxy without socksify raises a `RuntimeError`.
66
+
34
67
  ## Development
35
68
 
36
69
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
70
 
38
- To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](rubygems).
71
+ To release a new version, update the version number in `version.rb`, then commit and push that to `main` branch. Then, use GitHub Releases to create a matching Release. A GitHub Action will publish the gem to Rubygems.org.
39
72
 
40
73
  ## Contributing
41
74
 
@@ -51,6 +84,7 @@ Everyone interacting in the Faraday Net::HTTP adapter project's codebases, issue
51
84
 
52
85
  [faraday]: https://github.com/lostisland/faraday
53
86
  [net-http]: https://ruby-doc.org/stdlib-2.7.0/libdoc/net/http/rdoc/Net/HTTP.html
87
+ [socksify]: https://github.com/astro/socksify-ruby
54
88
  [rubygems]: https://rubygems.org
55
89
  [repo]: https://github.com/lostisland/faraday-net_http
56
90
  [license]: https://github.com/lostisland/faraday-net_http/blob/main/LICENSE.md
@@ -7,6 +7,13 @@ rescue LoadError
7
7
  'Make sure openssl is installed if you want ssl support'
8
8
  require 'net/http'
9
9
  end
10
+
11
+ begin
12
+ require 'socksify/http'
13
+ rescue LoadError
14
+ # socksify is optional and only required when using a SOCKS proxy
15
+ end
16
+
10
17
  require 'zlib'
11
18
 
12
19
  module Faraday
@@ -27,23 +34,27 @@ module Faraday
27
34
  Net::HTTPHeaderSyntaxError,
28
35
  Net::ProtocolError,
29
36
  SocketError,
30
- Zlib::GzipFile::Error
37
+ Zlib::GzipFile::Error,
38
+ Zlib::DataError,
39
+ Zlib::NeedDict,
40
+ Zlib::BufError
31
41
  ]
32
42
 
33
43
  exceptions << ::OpenSSL::SSL::SSLError if defined?(::OpenSSL::SSL::SSLError)
34
44
  exceptions << ::Net::OpenTimeout if defined?(::Net::OpenTimeout)
45
+ exceptions << ::SOCKSError if defined?(::SOCKSError)
35
46
 
36
47
  NET_HTTP_EXCEPTIONS = exceptions.freeze
37
48
 
38
49
  def initialize(app = nil, opts = {}, &block)
39
50
  @ssl_cert_store = nil
51
+ @ssl_cert_store_mutex = Mutex.new
40
52
  super(app, opts, &block)
41
53
  end
42
54
 
43
55
  def build_connection(env)
44
56
  net_http_connection(env).tap do |http|
45
- http.use_ssl = env[:url].scheme == 'https' if http.respond_to?(:use_ssl=)
46
- configure_ssl(http, env[:ssl])
57
+ configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' && env[:ssl]
47
58
  configure_request(http, env[:request])
48
59
  end
49
60
  end
@@ -51,10 +62,13 @@ module Faraday
51
62
  def net_http_connection(env)
52
63
  proxy = env[:request][:proxy]
53
64
  port = env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)
54
- if proxy
65
+ if socks_proxy?(proxy)
66
+ socks_proxy_class(proxy).new(env[:url].hostname, port, nil)
67
+ elsif proxy
55
68
  Net::HTTP.new(env[:url].hostname, port,
56
69
  proxy[:uri].hostname, proxy[:uri].port,
57
- proxy[:user], proxy[:password])
70
+ proxy[:user], proxy[:password],
71
+ nil, proxy[:uri].scheme == 'https')
58
72
  else
59
73
  Net::HTTP.new(env[:url].hostname, port, nil)
60
74
  end
@@ -106,18 +120,14 @@ module Faraday
106
120
  http_response
107
121
  end
108
122
 
109
- def request_with_wrapped_block(http, env)
123
+ def request_with_wrapped_block(http, env, &block)
110
124
  # Must use Net::HTTP#start and pass it a block otherwise the server's
111
125
  # TCP socket does not close correctly.
112
126
  http.start do |opened_http|
113
127
  opened_http.request create_request(env) do |response|
114
128
  save_http_response(env, response)
115
129
 
116
- if block_given?
117
- response.read_body do |chunk|
118
- yield(chunk)
119
- end
120
- end
130
+ response.read_body(&block) if block_given?
121
131
  end
122
132
  end
123
133
  end
@@ -132,13 +142,30 @@ module Faraday
132
142
  end
133
143
  end
134
144
 
145
+ def socks_proxy?(proxy)
146
+ proxy && proxy[:uri].scheme == 'socks'
147
+ end
148
+
149
+ def socks_proxy_class(proxy)
150
+ raise 'SOCKS proxy support requires the socksify gem ~> 1.8' unless Net::HTTP.respond_to?(:socks_proxy)
151
+
152
+ # socksify stores its settings on a class shared by every
153
+ # Net::HTTP.socks_proxy call, so use a subclass to keep proxies isolated.
154
+ Class.new(Net::HTTP).socks_proxy(proxy[:uri].hostname, proxy[:uri].port,
155
+ username: proxy[:user],
156
+ password: proxy[:password])
157
+ end
158
+
135
159
  def configure_ssl(http, ssl)
136
- return unless ssl
160
+ http.use_ssl = true if http.respond_to?(:use_ssl=)
137
161
 
138
162
  http.verify_mode = ssl_verify_mode(ssl)
139
163
  http.cert_store = ssl_cert_store(ssl)
140
164
 
141
- http.cert = ssl[:client_cert] if ssl[:client_cert]
165
+ cert, *extra_chain_cert = ssl[:client_cert]
166
+ http.cert = cert if cert
167
+ http.extra_chain_cert = extra_chain_cert if extra_chain_cert.any?
168
+
142
169
  http.key = ssl[:client_key] if ssl[:client_key]
143
170
  http.ca_file = ssl[:ca_file] if ssl[:ca_file]
144
171
  http.ca_path = ssl[:ca_path] if ssl[:ca_path]
@@ -147,6 +174,7 @@ module Faraday
147
174
  http.min_version = ssl[:min_version] if ssl[:min_version]
148
175
  http.max_version = ssl[:max_version] if ssl[:max_version]
149
176
  http.verify_hostname = ssl[:verify_hostname] if verify_hostname_enabled?(http, ssl)
177
+ http.ciphers = ssl[:ciphers] if ssl[:ciphers]
150
178
  end
151
179
 
152
180
  def configure_request(http, req)
@@ -172,28 +200,29 @@ module Faraday
172
200
  def ssl_cert_store(ssl)
173
201
  return ssl[:cert_store] if ssl[:cert_store]
174
202
 
175
- @ssl_cert_store ||= begin
176
- # Use the default cert store by default, i.e. system ca certs
177
- OpenSSL::X509::Store.new.tap(&:set_default_paths)
178
- end
203
+ # Use the default cert store by default, i.e. system ca certs
204
+ @ssl_cert_store ||= @ssl_cert_store_mutex.synchronize do
205
+ @ssl_cert_store ||= OpenSSL::X509::Store.new.tap(&:set_default_paths)
206
+ end
179
207
  end
180
208
 
181
209
  def ssl_verify_mode(ssl)
182
210
  ssl[:verify_mode] || begin
183
- if ssl.fetch(:verify, true)
184
- OpenSSL::SSL::VERIFY_PEER
185
- else
186
- OpenSSL::SSL::VERIFY_NONE
187
- end
188
- end
211
+ if ssl.fetch(:verify, true)
212
+ OpenSSL::SSL::VERIFY_PEER
213
+ else
214
+ OpenSSL::SSL::VERIFY_NONE
215
+ end
216
+ end
189
217
  end
190
218
 
191
219
  def encoded_body(http_response)
192
220
  body = http_response.body || +''
193
- /\bcharset=([^;]+)/.match(http_response['Content-Type']) do |match|
194
- content_charset = ::Encoding.find(match[1].strip)
195
- body = body.dup if body.frozen?
196
- body.force_encoding(content_charset)
221
+ /(?:\A|;)\s*charset=([^;]+)/i.match(http_response['Content-Type']) do |match|
222
+ charset = match[1].strip
223
+ charset = charset[1...-1] if charset.start_with?('"') && charset.end_with?('"')
224
+ content_charset = ::Encoding.find(charset)
225
+ body = body.dup.force_encoding(content_charset)
197
226
  rescue ArgumentError
198
227
  nil
199
228
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  module NetHttp
5
- VERSION = '3.0.2'
5
+ VERSION = '3.5.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,29 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-net_http
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 3.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan van der Pas
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2022-11-11 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
- name: faraday
13
+ name: net-http
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
- - - ">="
16
+ - - "~>"
18
17
  - !ruby/object:Gem::Version
19
- version: '2.5'
20
- type: :development
18
+ version: '0.5'
19
+ type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
- - - ">="
23
+ - - "~>"
25
24
  - !ruby/object:Gem::Version
26
- version: '2.5'
25
+ version: '0.5'
27
26
  description: Faraday adapter for Net::HTTP
28
27
  email:
29
28
  - janvanderpas@gmail.com
@@ -42,8 +41,8 @@ licenses:
42
41
  metadata:
43
42
  homepage_uri: https://github.com/lostisland/faraday-net_http
44
43
  source_code_uri: https://github.com/lostisland/faraday-net_http
45
- changelog_uri: https://github.com/lostisland/faraday-net_http/releases/tag/v3.0.2
46
- post_install_message:
44
+ changelog_uri: https://github.com/lostisland/faraday-net_http/releases/tag/v3.5.0
45
+ rubygems_mfa_required: 'true'
47
46
  rdoc_options: []
48
47
  require_paths:
49
48
  - lib
@@ -51,15 +50,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
51
50
  requirements:
52
51
  - - ">="
53
52
  - !ruby/object:Gem::Version
54
- version: 2.6.0
53
+ version: 3.0.0
55
54
  required_rubygems_version: !ruby/object:Gem::Requirement
56
55
  requirements:
57
56
  - - ">="
58
57
  - !ruby/object:Gem::Version
59
58
  version: '0'
60
59
  requirements: []
61
- rubygems_version: 3.1.6
62
- signing_key:
60
+ rubygems_version: 4.0.20
63
61
  specification_version: 4
64
62
  summary: Faraday adapter for Net::HTTP
65
63
  test_files: []