faraday-net_http 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1013f1c809b1d5d0f3852af8ffd6b598e144600b3c6dde6210ed06dd73e247cc
4
+ data.tar.gz: 94d5d5203458905c69d3a14773a76e29831799097ada86e2952a1ee4de81bb70
5
+ SHA512:
6
+ metadata.gz: 61654e5b5332e37cd1cca36999c08ed314acfb693030f950b9c2ffec687cbf796e7602bafbdf743ef3c8f38e396bb92767083a24436d73cacc22b8763ab415eb
7
+ data.tar.gz: bd432a059127978db61a14791355bbfe5afb1f7ac5e5ebac0c3bdc76b447198d2bf77998a4e9a4e091b51d1471c29354985f20444256a9fb1b526d311c9f74af
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Jan van der Pas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Faraday Net::HTTP adapter
2
+
3
+ This gem is a [Faraday][faraday] adapter for the [Net::HTTP][net-http] library. Faraday is an HTTP client library that provides a common interface over many adapters. Every adapter is defined into it's own gem. This gem defines the adapter for `Net::HTTP` the HTTP library that's included into the standard library of Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'faraday-net_http'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install faraday-net_http
20
+
21
+ ## Usage
22
+
23
+ Configure your Faraday connection to use this adapter like this:
24
+
25
+ ```ruby
26
+ connection = Faraday.new(url, conn_options) do |conn|
27
+ conn.adapter(:net_http)
28
+ end
29
+ ```
30
+
31
+ For more information on how to setup your Faraday connection and adapters usage, please refer to the [Faraday Website][faraday-website].
32
+
33
+ ## Development
34
+
35
+ 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.
36
+
37
+ 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).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on [GitHub][repo].
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [license][license].
46
+
47
+ ## Code of Conduct
48
+
49
+ Everyone interacting in the Faraday Net::HTTP adapter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct][code-of-conduct].
50
+
51
+ [faraday]: https://github.com/lostisland/faraday
52
+ [faraday-website]: https://lostisland.github.io/faraday
53
+ [net-http]: https://ruby-doc.org/stdlib-2.7.0/libdoc/net/http/rdoc/Net/HTTP.html
54
+ [rubygems]: https://rubygems.org
55
+ [repo]: https://github.com/lostisland/faraday-net_http
56
+ [license]: https://github.com/lostisland/faraday-net_http/blob/main/LICENSE.md
57
+ [code-of-conduct]: https://github.com/lostisland/faraday-net_http/blob/main/CODE_OF_CONDUCT.md
@@ -0,0 +1,215 @@
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
+ # rubocop:disable Metrics/ClassLength
13
+ module Faraday
14
+ class Adapter
15
+ # Net::HTTP adapter.
16
+ class NetHttp < Faraday::Adapter
17
+ exceptions = [
18
+ IOError,
19
+ Errno::EADDRNOTAVAIL,
20
+ Errno::ECONNABORTED,
21
+ Errno::ECONNREFUSED,
22
+ Errno::ECONNRESET,
23
+ Errno::EHOSTUNREACH,
24
+ Errno::EINVAL,
25
+ Errno::ENETUNREACH,
26
+ Errno::EPIPE,
27
+ Net::HTTPBadResponse,
28
+ Net::HTTPHeaderSyntaxError,
29
+ Net::ProtocolError,
30
+ SocketError,
31
+ Zlib::GzipFile::Error
32
+ ]
33
+
34
+ exceptions << ::OpenSSL::SSL::SSLError if defined?(::OpenSSL::SSL::SSLError)
35
+ exceptions << ::Net::OpenTimeout if defined?(::Net::OpenTimeout)
36
+
37
+ NET_HTTP_EXCEPTIONS = exceptions.freeze
38
+
39
+ def initialize(app = nil, opts = {}, &block)
40
+ @ssl_cert_store = nil
41
+ super(app, opts, &block)
42
+ end
43
+
44
+ def build_connection(env)
45
+ net_http_connection(env).tap do |http|
46
+ http.use_ssl = env[:url].scheme == 'https' if http.respond_to?(:use_ssl=)
47
+ configure_ssl(http, env[:ssl])
48
+ configure_request(http, env[:request])
49
+ end
50
+ end
51
+
52
+ def net_http_connection(env)
53
+ klass = if (proxy = env[:request][:proxy])
54
+ Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port,
55
+ proxy[:user], proxy[:password])
56
+ else
57
+ Net::HTTP
58
+ end
59
+ port = env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)
60
+ klass.new(env[:url].hostname, port)
61
+ end
62
+
63
+ def call(env)
64
+ super
65
+ http_response = connection(env) do |http|
66
+ perform_request(http, env)
67
+ rescue *NET_HTTP_EXCEPTIONS => e
68
+ raise Faraday::SSLError, e if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
69
+
70
+ raise Faraday::ConnectionFailed, e
71
+ end
72
+
73
+ save_response(env, http_response.code.to_i,
74
+ http_response.body || +'', nil,
75
+ http_response.message) do |response_headers|
76
+ http_response.each_header do |key, value|
77
+ response_headers[key] = value
78
+ end
79
+ end
80
+
81
+ @app.call env
82
+ rescue Timeout::Error, Errno::ETIMEDOUT => e
83
+ raise Faraday::TimeoutError, e
84
+ end
85
+
86
+ private
87
+
88
+ def create_request(env)
89
+ request = Net::HTTPGenericRequest.new \
90
+ env[:method].to_s.upcase, # request method
91
+ !!env[:body], # is there request body
92
+ env[:method] != :head, # is there response body
93
+ env[:url].request_uri, # request uri path
94
+ env[:request_headers] # request headers
95
+
96
+ if env[:body].respond_to?(:read)
97
+ request.body_stream = env[:body]
98
+ else
99
+ request.body = env[:body]
100
+ end
101
+ request
102
+ end
103
+
104
+ def perform_request(http, env)
105
+ if env[:request].stream_response?
106
+ size = 0
107
+ yielded = false
108
+ http_response = request_with_wrapped_block(http, env) do |chunk|
109
+ if chunk.bytesize.positive? || size.positive?
110
+ yielded = true
111
+ size += chunk.bytesize
112
+ env[:request].on_data.call(chunk, size)
113
+ end
114
+ end
115
+ env[:request].on_data.call(+'', 0) unless yielded
116
+ # Net::HTTP returns something,
117
+ # but it's not meaningful according to the docs.
118
+ http_response.body = nil
119
+ http_response
120
+ else
121
+ request_with_wrapped_block(http, env)
122
+ end
123
+ end
124
+
125
+ def request_with_wrapped_block(http, env, &block)
126
+ if (env[:method] == :get) && !env[:body]
127
+ # prefer `get` to `request` because the former handles gzip (ruby 1.9)
128
+ request_via_get_method(http, env, &block)
129
+ else
130
+ request_via_request_method(http, env, &block)
131
+ end
132
+ end
133
+
134
+ def request_via_get_method(http, env, &block)
135
+ # Must use Net::HTTP#start and pass it a block otherwise the server's
136
+ # TCP socket does not close correctly.
137
+ http.start do |opened_http|
138
+ opened_http.get env[:url].request_uri, env[:request_headers], &block
139
+ end
140
+ end
141
+
142
+ def request_via_request_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
+ if block_given?
147
+ opened_http.request create_request(env) do |response|
148
+ response.read_body(&block)
149
+ end
150
+ else
151
+ opened_http.request create_request(env)
152
+ end
153
+ end
154
+ end
155
+
156
+ # rubocop:disable Metrics/AbcSize
157
+ def configure_ssl(http, ssl)
158
+ return unless ssl
159
+
160
+ http.verify_mode = ssl_verify_mode(ssl)
161
+ http.cert_store = ssl_cert_store(ssl)
162
+
163
+ http.cert = ssl[:client_cert] if ssl[:client_cert]
164
+ http.key = ssl[:client_key] if ssl[:client_key]
165
+ http.ca_file = ssl[:ca_file] if ssl[:ca_file]
166
+ http.ca_path = ssl[:ca_path] if ssl[:ca_path]
167
+ http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
168
+ http.ssl_version = ssl[:version] if ssl[:version]
169
+ http.min_version = ssl[:min_version] if ssl[:min_version]
170
+ http.max_version = ssl[:max_version] if ssl[:max_version]
171
+ end
172
+ # rubocop:enable Metrics/AbcSize
173
+
174
+ def configure_request(http, req)
175
+ if (sec = request_timeout(:read, req))
176
+ http.read_timeout = sec
177
+ end
178
+
179
+ if (sec = http.respond_to?(:write_timeout=) &&
180
+ request_timeout(:write, req))
181
+ http.write_timeout = sec
182
+ end
183
+
184
+ if (sec = request_timeout(:open, req))
185
+ http.open_timeout = sec
186
+ end
187
+
188
+ # Only set if Net::Http supports it, since Ruby 2.5.
189
+ http.max_retries = 0 if http.respond_to?(:max_retries=)
190
+
191
+ @config_block&.call(http)
192
+ end
193
+
194
+ def ssl_cert_store(ssl)
195
+ return ssl[:cert_store] if ssl[:cert_store]
196
+
197
+ @ssl_cert_store ||= begin
198
+ # Use the default cert store by default, i.e. system ca certs
199
+ OpenSSL::X509::Store.new.tap(&:set_default_paths)
200
+ end
201
+ end
202
+
203
+ def ssl_verify_mode(ssl)
204
+ ssl[:verify_mode] || begin
205
+ if ssl.fetch(:verify, true)
206
+ OpenSSL::SSL::VERIFY_PEER
207
+ else
208
+ OpenSSL::SSL::VERIFY_NONE
209
+ end
210
+ end
211
+ end
212
+ end
213
+ end
214
+ end
215
+ # rubocop:enable Metrics/ClassLength
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday/adapter'
5
+
6
+ require_relative 'adapter/net_http'
7
+ require_relative 'net_http/version'
8
+
9
+ module Faraday
10
+ module NetHttp
11
+ Faraday::Adapter.register_middleware(net_http: Faraday::Adapter::NetHttp)
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ module NetHttp
5
+ VERSION = '0.0.5'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday-net_http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Jan van der Pas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-10-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.19.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.19.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: multipart-parser
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.1.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.1.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.4'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.91.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.91.1
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-packaging
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.5'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.5'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-performance
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '1.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '1.0'
153
+ description: Faraday adapter for Net::HTTP
154
+ email:
155
+ - janvanderpas@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - LICENSE.md
161
+ - README.md
162
+ - lib/faraday/adapter/net_http.rb
163
+ - lib/faraday/net_http.rb
164
+ - lib/faraday/net_http/version.rb
165
+ homepage: https://github.com/lostisland/faraday-net_http
166
+ licenses:
167
+ - MIT
168
+ metadata:
169
+ homepage_uri: https://github.com/lostisland/faraday-net_http
170
+ source_code_uri: https://github.com/lostisland/faraday-net_http
171
+ changelog_uri: https://github.com/lostisland/faraday-net_http
172
+ post_install_message:
173
+ rdoc_options: []
174
+ require_paths:
175
+ - lib
176
+ required_ruby_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: 2.4.0
181
+ required_rubygems_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ requirements: []
187
+ rubygems_version: 3.1.4
188
+ signing_key:
189
+ specification_version: 4
190
+ summary: Faraday adapter for Net::HTTP
191
+ test_files: []