httpx 0.15.4 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/doc/release_notes/0_16_0.md +93 -0
  3. data/lib/httpx.rb +6 -3
  4. data/lib/httpx/adapters/faraday.rb +3 -11
  5. data/lib/httpx/buffer.rb +1 -1
  6. data/lib/httpx/callbacks.rb +1 -1
  7. data/lib/httpx/chainable.rb +15 -8
  8. data/lib/httpx/connection.rb +2 -2
  9. data/lib/httpx/connection/http1.rb +3 -1
  10. data/lib/httpx/connection/http2.rb +1 -11
  11. data/lib/httpx/errors.rb +11 -11
  12. data/lib/httpx/io/ssl.rb +2 -2
  13. data/lib/httpx/io/tls.rb +1 -1
  14. data/lib/httpx/options.rb +78 -73
  15. data/lib/httpx/parser/http1.rb +1 -1
  16. data/lib/httpx/plugins/aws_sigv4.rb +10 -9
  17. data/lib/httpx/plugins/compression.rb +12 -11
  18. data/lib/httpx/plugins/cookies.rb +20 -7
  19. data/lib/httpx/plugins/cookies/cookie.rb +4 -2
  20. data/lib/httpx/plugins/cookies/jar.rb +20 -1
  21. data/lib/httpx/plugins/digest_authentication.rb +15 -11
  22. data/lib/httpx/plugins/expect.rb +19 -15
  23. data/lib/httpx/plugins/follow_redirects.rb +9 -9
  24. data/lib/httpx/plugins/grpc.rb +72 -46
  25. data/lib/httpx/plugins/grpc/call.rb +4 -1
  26. data/lib/httpx/plugins/ntlm_authentication.rb +8 -6
  27. data/lib/httpx/plugins/proxy.rb +4 -6
  28. data/lib/httpx/plugins/proxy/socks4.rb +2 -1
  29. data/lib/httpx/plugins/proxy/socks5.rb +2 -1
  30. data/lib/httpx/plugins/proxy/ssh.rb +9 -9
  31. data/lib/httpx/plugins/retries.rb +25 -21
  32. data/lib/httpx/plugins/upgrade.rb +7 -6
  33. data/lib/httpx/registry.rb +1 -1
  34. data/lib/httpx/request.rb +4 -12
  35. data/lib/httpx/resolver/https.rb +0 -2
  36. data/lib/httpx/response.rb +45 -18
  37. data/lib/httpx/selector.rb +2 -5
  38. data/lib/httpx/session.rb +19 -8
  39. data/lib/httpx/session2.rb +21 -0
  40. data/lib/httpx/transcoder/body.rb +1 -1
  41. data/lib/httpx/transcoder/chunker.rb +2 -1
  42. data/lib/httpx/version.rb +1 -1
  43. data/sig/buffer.rbs +2 -0
  44. data/sig/chainable.rbs +24 -28
  45. data/sig/connection.rbs +20 -8
  46. data/sig/connection/http1.rbs +3 -3
  47. data/sig/connection/http2.rbs +1 -1
  48. data/sig/errors.rbs +35 -1
  49. data/sig/headers.rbs +5 -5
  50. data/sig/httpx.rbs +4 -1
  51. data/sig/loggable.rbs +3 -1
  52. data/sig/options.rbs +35 -32
  53. data/sig/plugins/authentication.rbs +1 -1
  54. data/sig/plugins/aws_sdk_authentication.rbs +5 -1
  55. data/sig/plugins/aws_sigv4.rbs +1 -2
  56. data/sig/plugins/basic_authentication.rbs +1 -1
  57. data/sig/plugins/compression.rbs +4 -6
  58. data/sig/plugins/cookies.rbs +4 -5
  59. data/sig/plugins/cookies/cookie.rbs +5 -7
  60. data/sig/plugins/cookies/jar.rbs +9 -10
  61. data/sig/plugins/digest_authentication.rbs +2 -3
  62. data/sig/plugins/expect.rbs +2 -4
  63. data/sig/plugins/follow_redirects.rbs +3 -5
  64. data/sig/plugins/grpc.rbs +4 -7
  65. data/sig/plugins/h2c.rbs +0 -2
  66. data/sig/plugins/multipart.rbs +2 -4
  67. data/sig/plugins/ntlm_authentication.rbs +2 -3
  68. data/sig/plugins/persistent.rbs +3 -8
  69. data/sig/plugins/proxy.rbs +7 -7
  70. data/sig/plugins/proxy/ssh.rbs +4 -4
  71. data/sig/plugins/push_promise.rbs +0 -2
  72. data/sig/plugins/retries.rbs +4 -8
  73. data/sig/plugins/stream.rbs +1 -1
  74. data/sig/plugins/upgrade.rbs +2 -3
  75. data/sig/pool.rbs +1 -2
  76. data/sig/registry.rbs +1 -1
  77. data/sig/request.rbs +2 -2
  78. data/sig/resolver.rbs +7 -0
  79. data/sig/resolver/native.rbs +9 -5
  80. data/sig/resolver/resolver_mixin.rbs +4 -5
  81. data/sig/resolver/system.rbs +2 -0
  82. data/sig/response.rbs +17 -11
  83. data/sig/selector.rbs +6 -6
  84. data/sig/session.rbs +19 -14
  85. data/sig/transcoder.rbs +11 -4
  86. data/sig/transcoder/body.rbs +6 -1
  87. data/sig/transcoder/chunker.rbs +8 -2
  88. data/sig/transcoder/form.rbs +2 -1
  89. data/sig/transcoder/json.rbs +1 -0
  90. data/sig/utils.rbs +2 -0
  91. metadata +5 -3
  92. data/lib/httpx/request2.rb +0 -14
@@ -10,6 +10,7 @@ module HTTPX
10
10
  def initialize(response)
11
11
  @response = response
12
12
  @decoder = ->(z) { z }
13
+ @consumed = false
13
14
  end
14
15
 
15
16
  def inspect
@@ -25,7 +26,7 @@ module HTTPX
25
26
  end
26
27
 
27
28
  def trailing_metadata
28
- return unless @response.body.closed?
29
+ return unless @consumed
29
30
 
30
31
  @response.trailing_metadata
31
32
  end
@@ -40,8 +41,10 @@ module HTTPX
40
41
  Message.stream(@response).each do |message|
41
42
  y << @decoder.call(message)
42
43
  end
44
+ @consumed = true
43
45
  end
44
46
  else
47
+ @consumed = true
45
48
  @decoder.call(Message.unary(@response))
46
49
  end
47
50
  end
@@ -15,13 +15,15 @@ module HTTPX
15
15
  end
16
16
 
17
17
  def extra_options(options)
18
- Class.new(options.class) do
19
- def_option(:ntlm, <<-OUT)
20
- raise Error, ":ntlm must be a #{NTLMParams}" unless value.is_a?(#{NTLMParams})
18
+ options.merge(max_concurrent_requests: 1)
19
+ end
20
+ end
21
+
22
+ module OptionsMethods
23
+ def option_ntlm(value)
24
+ raise TypeError, ":ntlm must be a #{NTLMParams}" unless value.is_a?(NTLMParams)
21
25
 
22
- value
23
- OUT
24
- end.new(options).merge(max_concurrent_requests: 1)
26
+ value
25
27
  end
26
28
  end
27
29
 
@@ -64,13 +64,11 @@ module HTTPX
64
64
  klass.plugin(:"proxy/socks4")
65
65
  klass.plugin(:"proxy/socks5")
66
66
  end
67
+ end
67
68
 
68
- def extra_options(options)
69
- Class.new(options.class) do
70
- def_option(:proxy, <<-OUT)
71
- value.is_a?(#{Parameters}) ? value : Hash[value]
72
- OUT
73
- end.new(options)
69
+ module OptionsMethods
70
+ def option_proxy(value)
71
+ value.is_a?(Parameters) ? value : Hash[value]
74
72
  end
75
73
  end
76
74
 
@@ -4,7 +4,8 @@ require "resolv"
4
4
  require "ipaddr"
5
5
 
6
6
  module HTTPX
7
- Socks4Error = Class.new(Error)
7
+ class Socks4Error < Error; end
8
+
8
9
  module Plugins
9
10
  module Proxy
10
11
  module Socks4
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HTTPX
4
- Socks5Error = Class.new(Error)
4
+ class Socks5Error < Error; end
5
+
5
6
  module Plugins
6
7
  module Proxy
7
8
  module Socks5
@@ -6,16 +6,16 @@ module HTTPX
6
6
  module Plugins
7
7
  module Proxy
8
8
  module SSH
9
- def self.load_dependencies(*)
10
- require "net/ssh/gateway"
9
+ class << self
10
+ def load_dependencies(*)
11
+ require "net/ssh/gateway"
12
+ end
11
13
  end
12
14
 
13
- def self.extra_options(options)
14
- Class.new(options.class) do
15
- def_option(:proxy, <<-OUT)
16
- Hash[value]
17
- OUT
18
- end.new(options)
15
+ module OptionsMethods
16
+ def option_proxy(value)
17
+ Hash[value]
18
+ end
19
19
  end
20
20
 
21
21
  module InstanceMethods
@@ -65,7 +65,7 @@ module HTTPX
65
65
  when "http"
66
66
  TCPSocket.open("localhost", port)
67
67
  else
68
- raise Error, "unexpected scheme: #{request_uri.scheme}"
68
+ raise TypeError, "unexpected scheme: #{request_uri.scheme}"
69
69
  end
70
70
  end
71
71
  end
@@ -17,39 +17,43 @@ module HTTPX
17
17
  Errno::ECONNRESET,
18
18
  Errno::ECONNABORTED,
19
19
  Errno::EPIPE,
20
- (TLSError if defined?(TLSError)),
20
+ TLSError,
21
21
  TimeoutError,
22
22
  Parser::Error,
23
23
  Errno::EINVAL,
24
24
  Errno::ETIMEDOUT].freeze
25
25
 
26
26
  def self.extra_options(options)
27
- Class.new(options.class) do
28
- def_option(:retry_after, <<-OUT)
29
- # return early if callable
30
- unless value.respond_to?(:call)
31
- value = Integer(value)
32
- raise Error, ":retry_after must be positive" unless value.positive?
33
- end
27
+ options.merge(max_retries: MAX_RETRIES)
28
+ end
29
+
30
+ module OptionsMethods
31
+ def option_retry_after(value)
32
+ # return early if callable
33
+ unless value.respond_to?(:call)
34
+ value = Integer(value)
35
+ raise TypeError, ":retry_after must be positive" unless value.positive?
36
+ end
34
37
 
35
- value
36
- OUT
38
+ value
39
+ end
37
40
 
38
- def_option(:max_retries, <<-OUT)
39
- num = Integer(value)
40
- raise Error, ":max_retries must be positive" unless num.positive?
41
+ def option_max_retries(value)
42
+ num = Integer(value)
43
+ raise TypeError, ":max_retries must be positive" unless num.positive?
41
44
 
42
- num
43
- OUT
45
+ num
46
+ end
44
47
 
45
- def_option(:retry_change_requests)
48
+ def option_retry_change_requests(v)
49
+ v
50
+ end
46
51
 
47
- def_option(:retry_on, <<-OUT)
48
- raise ":retry_on must be called with the response" unless value.respond_to?(:call)
52
+ def option_retry_on(value)
53
+ raise ":retry_on must be called with the response" unless value.respond_to?(:call)
49
54
 
50
- value
51
- OUT
52
- end.new(options).merge(max_retries: MAX_RETRIES)
55
+ value
56
+ end
53
57
  end
54
58
 
55
59
  module InstanceMethods
@@ -18,14 +18,15 @@ module HTTPX
18
18
  upgrade_handlers = Module.new do
19
19
  extend Registry
20
20
  end
21
+ options.merge(upgrade_handlers: upgrade_handlers)
22
+ end
23
+ end
21
24
 
22
- Class.new(options.class) do
23
- def_option(:upgrade_handlers, <<-OUT)
24
- raise Error, ":upgrade_handlers must be a registry" unless value.respond_to?(:registry)
25
+ module OptionsMethods
26
+ def option_upgrade_handlers(value)
27
+ raise TypeError, ":upgrade_handlers must be a registry" unless value.respond_to?(:registry)
25
28
 
26
- value
27
- OUT
28
- end.new(options).merge(upgrade_handlers: upgrade_handlers)
29
+ value
29
30
  end
30
31
  end
31
32
 
@@ -31,7 +31,7 @@ module HTTPX
31
31
  #
32
32
  module Registry
33
33
  # Base Registry Error
34
- Error = Class.new(Error)
34
+ class Error < Error; end
35
35
 
36
36
  def self.extended(klass)
37
37
  super
data/lib/httpx/request.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "delegate"
3
4
  require "forwardable"
4
5
 
5
6
  module HTTPX
@@ -155,7 +156,7 @@ module HTTPX
155
156
  end
156
157
  # :nocov:
157
158
 
158
- class Body
159
+ class Body < SimpleDelegator
159
160
  class << self
160
161
  def new(*, options)
161
162
  return options.body if options.body.is_a?(self)
@@ -177,6 +178,7 @@ module HTTPX
177
178
 
178
179
  @headers["content-type"] ||= @body.content_type
179
180
  @headers["content-length"] = @body.bytesize unless unbounded_body?
181
+ super(@body)
180
182
  end
181
183
 
182
184
  def each(&block)
@@ -214,7 +216,7 @@ module HTTPX
214
216
 
215
217
  def stream(body)
216
218
  encoded = body
217
- encoded = Transcoder.registry("chunker").encode(body) if chunked?
219
+ encoded = Transcoder.registry("chunker").encode(body.enum_for(:each)) if chunked?
218
220
  encoded
219
221
  end
220
222
 
@@ -238,16 +240,6 @@ module HTTPX
238
240
  "#{unbounded_body? ? "stream" : "@bytesize=#{bytesize}"}>"
239
241
  end
240
242
  # :nocov:
241
-
242
- def respond_to_missing?(meth, *args)
243
- @body.respond_to?(meth, *args) || super
244
- end
245
-
246
- def method_missing(meth, *args, &block)
247
- return super unless @body.respond_to?(meth)
248
-
249
- @body.__send__(meth, *args, &block)
250
- end
251
243
  end
252
244
 
253
245
  def transition(nextstate)
@@ -24,8 +24,6 @@ module HTTPX
24
24
  record_types: RECORD_TYPES.keys,
25
25
  }.freeze
26
26
 
27
- def_delegator :@connections, :empty?
28
-
29
27
  def_delegators :@resolver_connection, :connecting?, :to_io, :call, :close
30
28
 
31
29
  def initialize(options)
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "objspace"
3
4
  require "stringio"
4
5
  require "tempfile"
5
6
  require "fileutils"
@@ -92,6 +93,16 @@ module HTTPX
92
93
  @length = 0
93
94
  @buffer = nil
94
95
  @state = :idle
96
+ ObjectSpace.define_finalizer(self, self.class.finalize(@buffer))
97
+ end
98
+
99
+ def self.finalize(buffer)
100
+ proc {
101
+ return unless buffer
102
+
103
+ @buffer.close
104
+ @buffer.unlink if @buffer.respond_to?(:unlink)
105
+ }
95
106
  end
96
107
 
97
108
  def closed?
@@ -134,18 +145,26 @@ module HTTPX
134
145
  end
135
146
 
136
147
  def to_s
137
- rewind
138
- if @buffer
139
- content = @buffer.read
148
+ case @buffer
149
+ when StringIO
150
+ begin
151
+ @buffer.string.force_encoding(@encoding)
152
+ rescue ArgumentError
153
+ @buffer.string
154
+ end
155
+ when Tempfile, File
156
+ rewind
157
+ content = _with_same_buffer_pos { @buffer.read }
140
158
  begin
141
- return content.force_encoding(@encoding)
159
+ content.force_encoding(@encoding)
142
160
  rescue ArgumentError # ex: unknown encoding name - utf
143
- return content
161
+ content
144
162
  end
163
+ when nil
164
+ "".b
165
+ else
166
+ @buffer
145
167
  end
146
- "".b
147
- ensure
148
- close
149
168
  end
150
169
  alias_method :to_str, :to_s
151
170
 
@@ -177,7 +196,11 @@ module HTTPX
177
196
  end
178
197
 
179
198
  def ==(other)
180
- to_s == other.to_s
199
+ if other.respond_to?(:read)
200
+ _with_same_buffer_pos { FileUtils.compare_stream(@buffer, other) }
201
+ else
202
+ to_s == other.to_s
203
+ end
181
204
  end
182
205
 
183
206
  # :nocov:
@@ -204,7 +227,7 @@ module HTTPX
204
227
  @buffer = Tempfile.new("httpx", encoding: Encoding::BINARY, mode: File::RDWR)
205
228
  else
206
229
  @state = :memory
207
- @buffer = StringIO.new("".b, File::RDWR)
230
+ @buffer = StringIO.new("".b)
208
231
  end
209
232
  when :memory
210
233
  if @length > @threshold_size
@@ -222,6 +245,18 @@ module HTTPX
222
245
 
223
246
  return unless %i[memory buffer].include?(@state)
224
247
  end
248
+
249
+ def _with_same_buffer_pos
250
+ return yield unless @buffer && @buffer.respond_to?(:pos)
251
+
252
+ current_pos = @buffer.pos
253
+ @buffer.rewind
254
+ begin
255
+ yield
256
+ rescue StandardError
257
+ @buffer.pos = current_pos
258
+ end
259
+ end
225
260
  end
226
261
  end
227
262
 
@@ -286,14 +321,6 @@ module HTTPX
286
321
  def raise_for_status
287
322
  raise @error
288
323
  end
289
-
290
- # rubocop:disable Style/MissingRespondToMissing
291
- def method_missing(meth, *, &block)
292
- raise NoMethodError, "undefined response method `#{meth}' for error response" if @options.response_class.public_method_defined?(meth)
293
-
294
- super
295
- end
296
- # rubocop:enable Style/MissingRespondToMissing
297
324
  end
298
325
  end
299
326
 
@@ -43,9 +43,6 @@ class HTTPX::Selector
43
43
 
44
44
  private
45
45
 
46
- READ_INTERESTS = %i[r rw].freeze
47
- WRITE_INTERESTS = %i[w rw].freeze
48
-
49
46
  def select_many(interval, &block)
50
47
  selectables, r, w = nil
51
48
 
@@ -64,8 +61,8 @@ class HTTPX::Selector
64
61
  selectables.each do |io|
65
62
  interests = io.interests
66
63
 
67
- (r ||= []) << io if READ_INTERESTS.include?(interests)
68
- (w ||= []) << io if WRITE_INTERESTS.include?(interests)
64
+ (r ||= []) << io if READABLE.include?(interests)
65
+ (w ||= []) << io if WRITABLE.include?(interests)
69
66
  end
70
67
 
71
68
  if @selectables.empty?
data/lib/httpx/session.rb CHANGED
@@ -15,8 +15,6 @@ module HTTPX
15
15
  end
16
16
 
17
17
  def wrap
18
- return unless block_given?
19
-
20
18
  begin
21
19
  prev_persistent = @persistent
22
20
  @persistent = true
@@ -31,6 +29,8 @@ module HTTPX
31
29
  end
32
30
 
33
31
  def request(*args, **options)
32
+ raise ArgumentError, "must perform at least one request" if args.empty?
33
+
34
34
  requests = args.first.is_a?(Request) ? args : build_requests(*args, options)
35
35
  responses = send_requests(*requests, options)
36
36
  return responses.first if responses.size == 1
@@ -247,9 +247,8 @@ module HTTPX
247
247
  if !@plugins.include?(pl)
248
248
  @plugins << pl
249
249
  pl.load_dependencies(self, &block) if pl.respond_to?(:load_dependencies)
250
+
250
251
  @default_options = @default_options.dup
251
- @default_options = pl.extra_options(@default_options, &block) if pl.respond_to?(:extra_options)
252
- @default_options = @default_options.merge(options) if options
253
252
 
254
253
  include(pl::InstanceMethods) if defined?(pl::InstanceMethods)
255
254
  extend(pl::ClassMethods) if defined?(pl::ClassMethods)
@@ -266,14 +265,26 @@ module HTTPX
266
265
  opts.response_body_class.__send__(:include, pl::ResponseBodyMethods) if defined?(pl::ResponseBodyMethods)
267
266
  opts.response_body_class.extend(pl::ResponseBodyClassMethods) if defined?(pl::ResponseBodyClassMethods)
268
267
  opts.connection_class.__send__(:include, pl::ConnectionMethods) if defined?(pl::ConnectionMethods)
268
+ if defined?(pl::OptionsMethods)
269
+ opts.options_class.__send__(:include, pl::OptionsMethods)
270
+
271
+ (pl::OptionsMethods.instance_methods - Object.instance_methods).each do |meth|
272
+ opts.options_class.method_added(meth)
273
+ end
274
+ @default_options = opts.options_class.new(opts)
275
+ end
276
+
277
+ @default_options = pl.extra_options(@default_options) if pl.respond_to?(:extra_options)
278
+ @default_options = @default_options.merge(options) if options
279
+
269
280
  pl.configure(self, &block) if pl.respond_to?(:configure)
270
281
 
271
282
  @default_options.freeze
272
283
  elsif options
273
284
  # this can happen when two plugins are loaded, an one of them calls the other under the hood,
274
285
  # albeit changing some default.
275
- @default_options = @default_options.dup
276
- @default_options = @default_options.merge(options)
286
+ @default_options = pl.extra_options(@default_options) if pl.respond_to?(:extra_options)
287
+ @default_options = @default_options.merge(options) if options
277
288
 
278
289
  @default_options.freeze
279
290
  end
@@ -283,8 +294,8 @@ module HTTPX
283
294
  # :nocov:
284
295
  def plugins(pls)
285
296
  warn ":#{__method__} is deprecated, use :plugin instead"
286
- pls.each do |pl, *args|
287
- plugin(pl, *args)
297
+ pls.each do |pl|
298
+ plugin(pl)
288
299
  end
289
300
  self
290
301
  end