httpx 1.9.0 → 1.9.2

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.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/doc/release_notes/1_8_1.md +1 -1
  3. data/doc/release_notes/1_8_2.md +4 -4
  4. data/doc/release_notes/1_8_3.md +3 -5
  5. data/doc/release_notes/1_8_4.md +1 -1
  6. data/doc/release_notes/1_9_0.md +7 -7
  7. data/doc/release_notes/1_9_1.md +32 -0
  8. data/doc/release_notes/1_9_2.md +19 -0
  9. data/lib/httpx/adapters/faraday.rb +1 -1
  10. data/lib/httpx/adapters/webmock.rb +15 -2
  11. data/lib/httpx/connection/http1.rb +24 -17
  12. data/lib/httpx/connection/http2.rb +110 -30
  13. data/lib/httpx/connection.rb +164 -167
  14. data/lib/httpx/io/tcp.rb +24 -7
  15. data/lib/httpx/options.rb +23 -20
  16. data/lib/httpx/plugins/aws_sigv4.rb +0 -1
  17. data/lib/httpx/plugins/cache.rb +1 -1
  18. data/lib/httpx/plugins/callbacks.rb +81 -19
  19. data/lib/httpx/plugins/content_digest.rb +3 -5
  20. data/lib/httpx/plugins/expect.rb +8 -1
  21. data/lib/httpx/plugins/fiber_concurrency.rb +56 -19
  22. data/lib/httpx/plugins/h2c.rb +12 -11
  23. data/lib/httpx/plugins/hedging.rb +2 -2
  24. data/lib/httpx/plugins/ntlm_auth.rb +4 -3
  25. data/lib/httpx/plugins/ntlm_v2_auth.rb +4 -3
  26. data/lib/httpx/plugins/oauth.rb +1 -1
  27. data/lib/httpx/plugins/persistent.rb +9 -1
  28. data/lib/httpx/plugins/proxy/http.rb +24 -16
  29. data/lib/httpx/plugins/proxy/socks5.rb +4 -4
  30. data/lib/httpx/plugins/proxy.rb +9 -16
  31. data/lib/httpx/plugins/query.rb +1 -0
  32. data/lib/httpx/plugins/stream.rb +12 -10
  33. data/lib/httpx/plugins/stream_bidi.rb +4 -0
  34. data/lib/httpx/plugins/tracing.rb +60 -21
  35. data/lib/httpx/plugins/upgrade/h2.rb +2 -2
  36. data/lib/httpx/request.rb +1 -1
  37. data/lib/httpx/resolver/https.rb +3 -2
  38. data/lib/httpx/resolver/native.rb +19 -9
  39. data/lib/httpx/resolver/resolver.rb +2 -2
  40. data/lib/httpx/resolver/system.rb +4 -0
  41. data/lib/httpx/resolver.rb +5 -5
  42. data/lib/httpx/response.rb +5 -1
  43. data/lib/httpx/selector.rb +108 -7
  44. data/lib/httpx/session.rb +20 -24
  45. data/lib/httpx/timers.rb +3 -5
  46. data/lib/httpx/transcoder/json.rb +1 -1
  47. data/lib/httpx/version.rb +1 -1
  48. data/lib/httpx.rb +1 -0
  49. data/sig/chainable.rbs +2 -1
  50. data/sig/connection/http1.rbs +4 -2
  51. data/sig/connection/http2.rbs +8 -2
  52. data/sig/connection.rbs +13 -8
  53. data/sig/io/tcp.rbs +5 -0
  54. data/sig/options.rbs +9 -0
  55. data/sig/plugins/callbacks.rbs +21 -1
  56. data/sig/plugins/expect.rbs +6 -0
  57. data/sig/plugins/fiber_concurrency.rbs +10 -1
  58. data/sig/plugins/proxy/http.rbs +3 -1
  59. data/sig/plugins/proxy.rbs +4 -0
  60. data/sig/plugins/stream_bidi.rbs +0 -2
  61. data/sig/plugins/tracing.rbs +10 -2
  62. data/sig/resolver/https.rbs +2 -0
  63. data/sig/resolver/native.rbs +3 -0
  64. data/sig/resolver/resolver.rbs +1 -1
  65. data/sig/resolver/system.rbs +2 -0
  66. data/sig/resolver.rbs +1 -1
  67. data/sig/selector.rbs +15 -1
  68. data/sig/session.rbs +2 -4
  69. metadata +5 -1
@@ -42,10 +42,6 @@ module HTTPX
42
42
  end
43
43
  end
44
44
 
45
- def connecting?
46
- super || @state == :authenticating || @state == :negotiating
47
- end
48
-
49
45
  def interests
50
46
  if @state == :connecting || @state == :authenticating || @state == :negotiating
51
47
  return @write_buffer.empty? ? :r : :w
@@ -56,6 +52,10 @@ module HTTPX
56
52
 
57
53
  private
58
54
 
55
+ def proxy_connecting?(state = @state)
56
+ super || state == :authenticating || state == :negotiating
57
+ end
58
+
59
59
  def handle_transition(nextstate)
60
60
  return super unless @options.proxy && @options.proxy.uri.scheme == "socks5"
61
61
 
@@ -11,7 +11,7 @@ module HTTPX
11
11
  #
12
12
  # * HTTP proxies
13
13
  # * HTTPS proxies
14
- # * Socks4 /Socks4a proxies
14
+ # * Socks4 proxies (also supports Socks4a)
15
15
  # * Socks5 proxies
16
16
  #
17
17
  # https://gitlab.com/os85/httpx/wikis/Proxy
@@ -157,7 +157,7 @@ module HTTPX
157
157
  # :username :: the username to use when authenticating with the proxy
158
158
  # :password :: the password to use when authenticating with the proxy
159
159
  # :no_proxy :: A list of hostnames (or regex patterns) for which the proxy should be bypassed
160
- # (ex: <tt>["example.com", "localhost", /.*\.google\.com/]</tt>).
160
+ # (ex: <tt>["example.com", "localhost", /.*\.google\.com/]</tt>). Can also receive ["*"] as a catch-all.
161
161
  #
162
162
  # if you systems setting environment variables like `HTTP_PROXY` or `NO_PROXY` though, you won't need to set
163
163
  # most of the above.
@@ -284,10 +284,10 @@ module HTTPX
284
284
  @proxy_uri || super
285
285
  end
286
286
 
287
- def connecting?
287
+ def connecting?(*args)
288
288
  return super unless @options.proxy
289
289
 
290
- super || @state == :connecting || @state == :connected
290
+ super || proxy_connecting?(*args)
291
291
  end
292
292
 
293
293
  def call
@@ -309,15 +309,6 @@ module HTTPX
309
309
  raise e
310
310
  end
311
311
 
312
- def reset
313
- return super unless @options.proxy
314
-
315
- @state = :open
316
-
317
- super
318
- # emit(:close)
319
- end
320
-
321
312
  private
322
313
 
323
314
  def initialize_type(uri, options)
@@ -326,6 +317,10 @@ module HTTPX
326
317
  "tcp"
327
318
  end
328
319
 
320
+ def proxy_connecting?(state = @state)
321
+ %i[connecting connected].include?(state)
322
+ end
323
+
329
324
  def connect
330
325
  return super unless @options.proxy
331
326
 
@@ -342,9 +337,7 @@ module HTTPX
342
337
 
343
338
  case nextstate
344
339
  when :closing
345
- # this is a hack so that we can use the super method
346
- # and it'll think that the current state is open
347
- @state = :open if @state == :connecting
340
+ @parser = nil if proxy_connecting?
348
341
  end
349
342
  super
350
343
  end
@@ -14,6 +14,7 @@ module HTTPX
14
14
  end
15
15
 
16
16
  module InstanceMethods
17
+ # sends a QUERY request for the given +uri+, using +options+ for each.
17
18
  def query(*uri, **options)
18
19
  request("QUERY", uri, **options)
19
20
  end
@@ -125,12 +125,12 @@ module HTTPX
125
125
  # https://gitlab.com/os85/httpx/wikis/Stream
126
126
  #
127
127
  module Stream
128
- STREAM_REQUEST_OPTIONS = { timeout: { read_timeout: Float::INFINITY, operation_timeout: 60 }.freeze }.freeze
128
+ STREAM_REQUEST_OPTIONS = { timeout: { operation_timeout: 60 }.freeze }.freeze
129
129
 
130
130
  def self.extra_options(options)
131
131
  options.merge(
132
132
  stream: false,
133
- timeout: { read_timeout: Float::INFINITY, operation_timeout: 60 },
133
+ timeout: { operation_timeout: 60 },
134
134
  stream_response_class: Class.new(StreamResponse, &Options::SET_TEMPORARY_NAME).freeze
135
135
  )
136
136
  end
@@ -140,14 +140,6 @@ module HTTPX
140
140
  # :stream :: whether the request to process should be handled as a stream (defaults to <tt>false</tt>).
141
141
  # :stream_response_class :: Class used to build the stream response object.
142
142
  module OptionsMethods
143
- def option_stream(val)
144
- val
145
- end
146
-
147
- def option_stream_response_class(value)
148
- value
149
- end
150
-
151
143
  def extend_with_plugin_classes(pl)
152
144
  return super unless defined?(pl::StreamResponseMethods)
153
145
 
@@ -157,6 +149,16 @@ module HTTPX
157
149
 
158
150
  super
159
151
  end
152
+
153
+ private
154
+
155
+ def option_stream(val)
156
+ val
157
+ end
158
+
159
+ def option_stream_response_class(value)
160
+ value
161
+ end
160
162
  end
161
163
 
162
164
  module InstanceMethods
@@ -158,6 +158,10 @@ module HTTPX
158
158
  @closed ? :closed : :open
159
159
  end
160
160
 
161
+ def closed_for_select?
162
+ @closed
163
+ end
164
+
161
165
  # noop
162
166
  def log(**, &_); end
163
167
 
@@ -70,11 +70,14 @@ module HTTPX::Plugins
70
70
  # the connection to connect.
71
71
  attr_accessor :handshake_time
72
72
 
73
+ # time at which the request was first handed to a connection, or last moved back to waiting for one.
74
+ attr_accessor :enqueue_time
75
+
73
76
  # intercepts request initialization to inject the tracing logic.
74
77
  def initialize(*)
75
78
  super
76
79
 
77
- @init_time = @handshake_time = nil
80
+ @init_time = @handshake_time = @enqueue_time = nil
78
81
 
79
82
  tracer = @options.tracer
80
83
 
@@ -85,12 +88,14 @@ module HTTPX::Plugins
85
88
 
86
89
  # request is reset when it's retried.
87
90
  @init_time = @handshake_time = nil
91
+
92
+ # going back to idle means waiting to be sent again.
93
+ @enqueue_time = ::Time.now.utc
88
94
  end
89
95
  on(:headers) do
90
96
  # the usual request init time (when not including the connection handshake)
91
97
  # should be the time the request is buffered the first time.
92
- @init_time = ::Time.now.utc
93
- @init_time -= @handshake_time if @handshake_time
98
+ @init_time = calculate_init_time
94
99
 
95
100
  tracer.start(self)
96
101
  end
@@ -102,14 +107,27 @@ module HTTPX::Plugins
102
107
  # Example is the :ssrf_filter plugin, which raises an error on
103
108
  # initialize if the host is an IP which matches against the known set.
104
109
  # in such cases, we'll just set here right here.
105
- unless @init_time
106
- @init_time = ::Time.now.utc
107
-
108
- @init_time -= @handshake_time if @handshake_time
109
- end
110
+ @init_time ||= calculate_init_time
110
111
 
111
112
  super
112
113
  end
114
+
115
+ private
116
+
117
+ def calculate_init_time
118
+ init_time = ::Time.now.utc
119
+
120
+ return init_time unless (handshake_time = @handshake_time)
121
+
122
+ init_time -= handshake_time
123
+
124
+ enqueue_time = @enqueue_time
125
+
126
+ # handshake time is measured for the connection, and may happen before the request was sent to it.
127
+ return init_time unless enqueue_time && init_time < enqueue_time
128
+
129
+ enqueue_time
130
+ end
113
131
  end
114
132
 
115
133
  # Connection mixin
@@ -120,7 +138,15 @@ module HTTPX::Plugins
120
138
  @init_time = nil
121
139
  end
122
140
 
123
- def send_request_to_parser(request)
141
+ def send(request)
142
+ # requests may be rerouted to other connections (i.e. when coalescing), so only the first
143
+ # connection the request is handed to marks it.
144
+ request.enqueue_time ||= ::Time.now.utc
145
+
146
+ super
147
+ end
148
+
149
+ def send_request_to_parser(request, *)
124
150
  if connecting? && @init_time
125
151
  # at this point, the connection has connected, but hasn't yet transitioned to :open,
126
152
  # so it's the time the requests which caused this connection to connect, are finally being
@@ -144,8 +170,24 @@ module HTTPX::Plugins
144
170
  @init_time = nil
145
171
  end
146
172
 
173
+ def disconnect
174
+ prevstate = @state
175
+
176
+ super.tap do |was_closed|
177
+ # reset init time of connections abandoned while connecting.
178
+ @init_time = nil if was_closed && connecting?(prevstate)
179
+ end
180
+ end
181
+
147
182
  private
148
183
 
184
+ def handle_transition(*)
185
+ super
186
+
187
+ # no longer relevant after connection established.
188
+ @init_time = nil unless connecting?
189
+ end
190
+
149
191
  def handle_error(er, request = nil)
150
192
  if connecting? && @init_time
151
193
  # bookkeep handshake time back into pending requests, which were never initialized with it.
@@ -176,24 +218,21 @@ module HTTPX::Plugins
176
218
  def initialize(*)
177
219
  super
178
220
 
179
- @handshake_time = nil
180
221
  @handshake_init_time = ::Time.now.utc
181
222
  end
182
223
 
183
- def send(request, head = false)
184
- if head
185
- # only true for pending requests waiting on the handshake, so handshake time accrues.
186
- request.handshake_time ||= 0.0
187
- request.handshake_time += @handshake_time
188
- end
189
-
190
- super
191
- end
192
-
193
224
  private
194
225
 
195
226
  def on_settings(*)
196
- @handshake_time = ::Time.now.utc - @handshake_init_time unless @handshake_completed
227
+ unless @handshake_completed
228
+ # pending requests must wait for the handshake completion
229
+ handshake_time = ::Time.now.utc - @handshake_init_time
230
+
231
+ @pending.each do |request|
232
+ request.handshake_time ||= 0.0
233
+ request.handshake_time += handshake_time
234
+ end
235
+ end
197
236
 
198
237
  super
199
238
  end
@@ -33,9 +33,9 @@ module HTTPX
33
33
  end
34
34
 
35
35
  def upgrade_to_h2
36
- enqueue_pending_requests_from_parser(@parser)
36
+ @parser.reset_requests
37
37
 
38
- @parser = @options.http2_class.new(@write_buffer, @options)
38
+ @parser = @options.http2_class.new(@write_buffer, @pending, @options)
39
39
  set_parser_callbacks(@parser)
40
40
  @upgrade_protocol = "h2"
41
41
 
data/lib/httpx/request.rb CHANGED
@@ -180,7 +180,7 @@ module HTTPX
180
180
  end
181
181
 
182
182
  def can_buffer?
183
- @state != :done
183
+ @state != :done && @state != :expect
184
184
  end
185
185
 
186
186
  def started?
@@ -32,7 +32,8 @@ module HTTPX
32
32
  }.freeze
33
33
 
34
34
  def_delegators :@resolver_connection, :connecting?, :to_io, :call, :close,
35
- :closed?, :deactivate, :terminate, :inflight?, :handle_socket_timeout
35
+ :closed?, :deactivate, :terminate, :inflight?, :handle_socket_timeout,
36
+ :closed_for_select?
36
37
 
37
38
  def initialize(_, options)
38
39
  super
@@ -49,7 +50,7 @@ module HTTPX
49
50
  end
50
51
 
51
52
  def state
52
- @resolver_connection ? @resolver_connection.state : :idle
53
+ @resolver_connection&.state || :idle
53
54
  end
54
55
 
55
56
  def <<(connection)
@@ -38,12 +38,11 @@ module HTTPX
38
38
  @search = Array(@resolver_options[:search]).map { |srch| srch.scan(/[^.]+/) }
39
39
  @_timeouts = Array(@resolver_options[:timeouts])
40
40
  @timeouts = Hash.new { |timeouts, host| timeouts[host] = @_timeouts.dup }
41
- @name = nil
41
+ @name = @message_id = @timer = nil
42
42
  @queries = {}
43
43
  @read_buffer = "".b
44
44
  @write_buffer = Buffer.new(@resolver_options[:packet_size])
45
45
  @state = :idle
46
- @timer = nil
47
46
  end
48
47
 
49
48
  def close
@@ -52,7 +51,7 @@ module HTTPX
52
51
 
53
52
  def force_close(*)
54
53
  @timer.cancel if @timer
55
- @timer = @name = nil
54
+ @timer = @name = @message_id = nil
56
55
  @queries.clear
57
56
  @timeouts.clear
58
57
  close
@@ -87,6 +86,10 @@ module HTTPX
87
86
  end
88
87
  end
89
88
 
89
+ def closed_for_select?
90
+ @state == :closed
91
+ end
92
+
90
93
  def interests
91
94
  case @state
92
95
  when :idle
@@ -170,6 +173,7 @@ module HTTPX
170
173
  if nameserver && @ns_index < nameserver.size
171
174
  log { "resolver #{FAMILY_TYPES[@record_type]}: failed resolving on nameserver #{@nameserver[@ns_index - 1]} (#{e.message})" }
172
175
  transition(:idle)
176
+ transition(:open)
173
177
  @timeouts.clear
174
178
  retry
175
179
  else
@@ -195,7 +199,7 @@ module HTTPX
195
199
  @timer = @current_selector.after(timeout) do
196
200
  next unless @connections.include?(connection)
197
201
 
198
- @timer = @name = nil
202
+ @timer = @name = @message_id = nil
199
203
 
200
204
  do_retry(h, connection, timeout)
201
205
  end
@@ -310,7 +314,13 @@ module HTTPX
310
314
  end
311
315
 
312
316
  def parse(buffer)
313
- code, result = Resolver.decode_dns_answer(buffer)
317
+ code, result, message_id = Resolver.decode_dns_answer(buffer)
318
+
319
+ # discard late message answers for no longer inflight queries.
320
+ if message_id && message_id != @message_id
321
+ log { "resolver #{FAMILY_TYPES[@record_type]}: discarding answer for a query no longer in flight" }
322
+ return
323
+ end
314
324
 
315
325
  case code
316
326
  when :ok
@@ -392,7 +402,7 @@ module HTTPX
392
402
  orig_name = name
393
403
  # absolute name
394
404
  name_labels = Resolv::DNS::Name.create(name).to_a
395
- name = @queries.each_key.first { |hname| name_labels == Resolv::DNS::Name.create(hname).to_a }
405
+ name = @queries.each_key.find { |hname| name_labels == Resolv::DNS::Name.create(hname).to_a }
396
406
 
397
407
  # probably a retried query for which there's an answer
398
408
  unless name
@@ -462,11 +472,10 @@ module HTTPX
462
472
  @queries[hostname] = connection
463
473
  end
464
474
 
465
- @name = hostname
466
-
467
475
  log { "resolver #{FAMILY_TYPES[@record_type]}: query for #{hostname}" }
468
476
  begin
469
477
  @write_buffer << encode_dns_query(hostname)
478
+ @name = hostname
470
479
  rescue Resolv::DNS::EncodeError => e
471
480
  reset_hostname(hostname, connection: connection)
472
481
  @connections.delete(connection)
@@ -479,6 +488,7 @@ module HTTPX
479
488
  message_id = Resolver.generate_id
480
489
  msg = Resolver.encode_dns_query(hostname, type: @record_type, message_id: message_id)
481
490
  msg[0, 2] = [msg.size, message_id].pack("nn") if @socket_type == :tcp
491
+ @message_id = message_id
482
492
  msg
483
493
  end
484
494
 
@@ -574,7 +584,7 @@ module HTTPX
574
584
  def reset_query
575
585
  @timer.cancel
576
586
 
577
- @timer = @name = nil
587
+ @timer = @name = @message_id = nil
578
588
  end
579
589
 
580
590
  def reset_hostname(hostname, connection: @queries.delete(hostname), reset_candidates: true)
@@ -29,9 +29,9 @@ module HTTPX
29
29
 
30
30
  attr_reader :family, :options
31
31
 
32
- attr_writer :current_selector, :current_session
32
+ attr_writer :current_session
33
33
 
34
- attr_accessor :multi
34
+ attr_accessor :current_selector, :multi
35
35
 
36
36
  def initialize(family, options)
37
37
  @family = family
@@ -90,6 +90,10 @@ module HTTPX
90
90
  nil
91
91
  end
92
92
 
93
+ def closed_for_select?
94
+ @state == :closed
95
+ end
96
+
93
97
  def interests
94
98
  return if @queries.empty?
95
99
 
@@ -51,9 +51,9 @@ module HTTPX
51
51
  end
52
52
 
53
53
  # no domain was found
54
- return :no_domain_found if message.rcode == Resolv::DNS::RCode::NXDomain
54
+ return :no_domain_found, nil, message.id if message.rcode == Resolv::DNS::RCode::NXDomain
55
55
 
56
- return :message_truncated if message.tc == 1
56
+ return :message_truncated, nil, message.id if message.tc == 1
57
57
 
58
58
  if message.rcode != Resolv::DNS::RCode::NoError
59
59
  case message.rcode
@@ -62,9 +62,9 @@ module HTTPX
62
62
  # but the DNS resolver state.
63
63
  Resolv::DNS::RCode::NotImp,
64
64
  Resolv::DNS::RCode::Refused
65
- return :retriable_error, message.rcode
65
+ return :retriable_error, message.rcode, message.id
66
66
  else
67
- return :dns_error, message.rcode
67
+ return :dns_error, message.rcode, message.id
68
68
  end
69
69
  end
70
70
 
@@ -89,7 +89,7 @@ module HTTPX
89
89
  end
90
90
  end
91
91
 
92
- [:ok, addresses]
92
+ [:ok, addresses, message.id]
93
93
  end
94
94
 
95
95
  private
@@ -259,7 +259,11 @@ module HTTPX
259
259
  if c
260
260
  c.strip!
261
261
  c.delete_prefix!("charset=")
262
- c = c[1..-2] if c.start_with?('"') && c.end_with?('"')
262
+ c.gsub!(/\A"(.*?)(?<!\\)"\z/) do
263
+ c2 = ::Regexp.last_match(1)
264
+ c2.gsub!(/\\(.)/, '\1')
265
+ c2
266
+ end
263
267
  @charset = c
264
268
  end
265
269
  end