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
data/lib/httpx/options.rb CHANGED
@@ -397,33 +397,39 @@ module HTTPX
397
397
 
398
398
  private
399
399
 
400
+ module OptionMethodsCache
401
+ def option_integer(value)
402
+ value = Integer(value) unless value.respond_to?(:infinite?) && value.infinite?
403
+ raise TypeError, "`:#{__method__.to_s.delete_prefix("option_")}` must be positive" unless value.positive?
404
+
405
+ value
406
+ end
407
+
408
+ def option_hash(value)
409
+ Hash[value]
410
+ end
411
+
412
+ def option_itself(value)
413
+ value
414
+ end
415
+ end.freeze
416
+
400
417
  # number options
401
418
  %i[
402
419
  max_concurrent_requests max_requests window_size buffer_size
403
420
  max_response_body_size max_response_headers max_response_header_value_size
404
421
  body_threshold_size debug_level
405
422
  ].each do |option|
406
- class_eval(<<-OUT, __FILE__, __LINE__ + 1)
407
- # converts +v+ into an Integer before setting the +#{option}+ option.
408
- private def option_#{option}(value) # private def option_max_requests(v)
409
- value = Integer(value) unless value.respond_to?(:infinite?) && value.infinite?
410
- raise TypeError, "`:#{option}` must be positive" unless value.positive? # raise TypeError, ":max_requests must be positive" unless value.positive?
411
-
412
- value
413
- end
414
- OUT
423
+ define_method(:"option_#{option}", OptionMethodsCache.instance_method(:option_integer))
415
424
  end
416
425
 
417
426
  # hashable options
418
427
  %i[ssl http2_settings resolver_options pool_options].each do |option|
419
- class_eval(<<-OUT, __FILE__, __LINE__ + 1)
420
- # converts +v+ into an Hash before setting the +#{option}+ option.
421
- private def option_#{option}(value) # def option_ssl(v)
422
- Hash[value]
423
- end
424
- OUT
428
+ define_method(:"option_#{option}", OptionMethodsCache.instance_method(:option_hash))
425
429
  end
426
430
 
431
+ CACHE = Module.new
432
+
427
433
  %i[
428
434
  request_class response_class headers_class request_body_class
429
435
  response_body_class connection_class http1_class http2_class
@@ -431,11 +437,8 @@ module HTTPX
431
437
  io fallback_protocol debug debug_redact
432
438
  compress_request_body decompress_response_body
433
439
  persistent close_on_fork
434
- ].each do |method_name|
435
- class_eval(<<-OUT, __FILE__, __LINE__ + 1)
436
- # sets +v+ as the value of the +#{method_name}+ option
437
- private def option_#{method_name}(v); v; end # private def option_smth(v); v; end
438
- OUT
440
+ ].each do |option|
441
+ define_method(:"option_#{option}", OptionMethodsCache.instance_method(:option_itself))
439
442
  end
440
443
 
441
444
  def option_origin(value)
@@ -156,7 +156,6 @@ module HTTPX
156
156
 
157
157
  class << self
158
158
  def load_dependencies(*)
159
- require "set"
160
159
  require "digest/sha2"
161
160
  require "cgi/escape"
162
161
  end
@@ -106,7 +106,7 @@ module HTTPX
106
106
  def fetch_response(request, *)
107
107
  response = super
108
108
 
109
- return unless response
109
+ return response unless response.is_a?(Response)
110
110
 
111
111
  if cacheable_request?(request) && cacheable_response?(request, response) && !response.cached?
112
112
  log { "caching response for #{request.uri}..." }
@@ -34,6 +34,7 @@ module HTTPX
34
34
  end
35
35
 
36
36
  def plugin(*args, &blk)
37
+ # ruby 2.7 requires explicit named splat
37
38
  super(*args).tap do |sess|
38
39
  CALLBACKS.each do |cb|
39
40
  next unless callbacks_for?(cb)
@@ -69,12 +70,12 @@ module HTTPX
69
70
  connection.on(:open) do
70
71
  next unless connection.current_session == self
71
72
 
72
- emit_or_callback_error(:connection_opened, connection.origin, connection.io.socket)
73
+ emit_or_callback_error(:connection_opened, connection.origin, connection.io.socket, connection: connection)
73
74
  end
74
- connection.on(:callback_connection_closed) do
75
- next unless connection.current_session == self
75
+ connection.on(:callback_connection_closed) do |previous_session, error|
76
+ next unless previous_session == self
76
77
 
77
- emit_or_callback_error(:connection_closed, connection.origin) if connection.used?
78
+ emit_or_callback_error(:connection_closed, connection.origin, error, connection: connection) if connection.used?
78
79
  end
79
80
 
80
81
  connection
@@ -84,32 +85,35 @@ module HTTPX
84
85
  super
85
86
 
86
87
  request.on(:headers) do
87
- emit_or_callback_error(:request_started, request)
88
+ emit_or_callback_error(:request_started, request, connection: request.connection)
88
89
  end
89
90
  request.on(:body_chunk) do |chunk|
90
- emit_or_callback_error(:request_body_chunk, request, chunk)
91
+ emit_or_callback_error(:request_body_chunk, request, chunk, connection: request.connection)
91
92
  end
92
93
  request.on(:done) do
93
- emit_or_callback_error(:request_completed, request)
94
+ emit_or_callback_error(:request_completed, request, connection: request.connection)
94
95
  end
95
96
 
96
97
  request.on(:response_started) do |res|
98
+ # .last_connection happens when an error happens before the request is prepared to be sent,
99
+ # i.e. when connecting fails.
100
+ connection = request.connection || request.last_connection
97
101
  if res.is_a?(Response)
98
- emit_or_callback_error(:response_started, request, res)
102
+ emit_or_callback_error(:response_started, request, res, connection: connection)
99
103
  res.on(:chunk_received) do |chunk|
100
- emit_or_callback_error(:response_body_chunk, request, res, chunk)
104
+ emit_or_callback_error(:response_body_chunk, request, res, chunk, connection: connection)
101
105
  end
102
106
  else
103
- emit_or_callback_error(:request_error, request, res.error)
107
+ emit_or_callback_error(:request_error, request, res.error, connection: connection)
104
108
  end
105
109
  end
106
- request.on(:response) do |res|
107
- emit_or_callback_error(:response_completed, request, res) if res.is_a?(Response)
110
+ request.on(:response_completed) do |res|
111
+ emit_or_callback_error(:response_completed, request, res, connection: request.connection)
108
112
  end
109
113
  end
110
114
 
111
- def emit_or_callback_error(*args)
112
- emit(*args)
115
+ def emit_or_callback_error(...)
116
+ emit(...)
113
117
  rescue StandardError => e
114
118
  ex = CallbackError.new(e.message)
115
119
  ex.set_backtrace(e.backtrace)
@@ -124,6 +128,12 @@ module HTTPX
124
128
  end
125
129
 
126
130
  module RequestMethods
131
+ # allows getting the connection that the request was sent over
132
+ attr_reader :connection
133
+
134
+ # gets the failed connection the request was supposed to be sent over
135
+ attr_accessor :last_connection
136
+
127
137
  def drain_body
128
138
  super.tap do |chunk|
129
139
  emit(:body_chunk, chunk) if chunk
@@ -135,17 +145,69 @@ module HTTPX
135
145
  end
136
146
  end
137
147
 
148
+ module ResponseMethods
149
+ def finish!
150
+ # emit before connection is unassigned
151
+ @request.emit(:response_completed, self)
152
+
153
+ super
154
+ end
155
+ end
156
+
138
157
  module ConnectionMethods
158
+ def initialize(*)
159
+ super
160
+ @last_error = nil
161
+ end
162
+
163
+ def on_connect_error(e)
164
+ with_last_error(e) do
165
+ super
166
+ end
167
+ end
168
+
169
+ def on_error(e, request = nil)
170
+ with_current_connection(request) do
171
+ with_last_error(e) do
172
+ super
173
+ end
174
+ end
175
+ end
176
+
139
177
  private
140
178
 
141
- def disconnect
142
- return if @exhausted
179
+ def on_parser_error(request, _)
180
+ with_current_connection(request) do
181
+ super
182
+ end
183
+ end
143
184
 
144
- return unless @current_session && @current_selector
185
+ def with_current_connection(request)
186
+ return yield unless request
187
+
188
+ begin
189
+ prev_conn = request.connection
190
+ request.connection = self
191
+ request.last_connection = self
192
+ yield
193
+ ensure
194
+ request.connection = prev_conn if request.connection == self
195
+ request.last_connection = nil
196
+ end
197
+ end
145
198
 
146
- emit(:callback_connection_closed)
199
+ def with_last_error(e)
200
+ @last_error = e
201
+ yield
202
+ ensure
203
+ @last_error = nil if @last_error == e
204
+ end
147
205
 
148
- super
206
+ def disconnect
207
+ session = @current_session
208
+ super.tap do |was_closed|
209
+ emit(:callback_connection_closed, session, @last_error) if was_closed
210
+ end
149
211
  end
150
212
  end
151
213
  end
@@ -81,15 +81,13 @@ module HTTPX
81
81
  end
82
82
 
83
83
  def write(chunk)
84
- @content_digest_buffer.write(chunk) if @content_digest_buffer
84
+ @content_digest_buffer&.write(chunk)
85
85
  super
86
86
  end
87
87
 
88
88
  def close
89
- if @content_digest_buffer
90
- @content_digest_buffer.close
91
- @content_digest_buffer = nil
92
- end
89
+ @content_digest_buffer&.close
90
+ @content_digest_buffer = nil
93
91
  super
94
92
  end
95
93
  end
@@ -104,9 +104,15 @@ module HTTPX
104
104
  end
105
105
 
106
106
  module ConnectionMethods
107
- def send_request_to_parser(request)
107
+ private
108
+
109
+ def set_request_timeouts(request)
108
110
  super
109
111
 
112
+ set_request_expect_timeout(request)
113
+ end
114
+
115
+ def set_request_expect_timeout(request)
110
116
  return unless request.headers["expect"] == "100-continue"
111
117
 
112
118
  expect_timeout = request.options.expect_timeout
@@ -118,6 +124,7 @@ module HTTPX
118
124
  if request.state == :expect && !request.expects?
119
125
  Expect.no_expect_store.add(request.origin)
120
126
  request.headers.delete("expect")
127
+ request.transition(:body)
121
128
  consume
122
129
  end
123
130
  end
@@ -76,10 +76,10 @@ module HTTPX
76
76
  super
77
77
  end
78
78
 
79
- def interests
80
- return if connecting? && @pending.none?(&:current_context?)
81
-
82
- super
79
+ def closed_for_select?
80
+ # @fiber-switch-guard
81
+ # a closed connection still holding requests will reconnect once the fiber closing its socket resumes.
82
+ super && @pending.empty?
83
83
  end
84
84
 
85
85
  def on_io_error(e)
@@ -88,17 +88,12 @@ module HTTPX
88
88
  # @fiber-switch-guard
89
89
  # sockets closed during fiber scheduler switches are raised in separate fibers than the fiber the
90
90
  # socket may be used in. this check verifies that this is actually about this socket.
91
- return unless to_io.closed?
92
-
93
- if @state == :closing
94
- # @fiber-switch-guard
95
- # if the connection is reused across fibers, the socket may have been closed in the other fiber
96
- # and switched here during the process, so continue what it was doing and transition to closed
97
- # via #call.
91
+ unless to_io.closed? && !backlog?
98
92
  call
99
- elsif !backlog?
100
- super
93
+ return
101
94
  end
95
+
96
+ super
102
97
  end
103
98
 
104
99
  def on_connect_error(e)
@@ -107,24 +102,50 @@ module HTTPX
107
102
  # @fiber-switch-guard
108
103
  # sockets closed during fiber scheduler switches are raised in separate fibers than the fiber the
109
104
  # socket may be used in. this check verifies that this is actually about this socket.
110
- return unless to_io.closed? && !backlog?
105
+ unless to_io.closed? && !backlog?
106
+ call
107
+ return
108
+ end
111
109
 
112
110
  super
113
111
  end
114
112
 
115
113
  def pending_count
116
- count = @pending.count(&:current_context?)
117
- count += @parser.pending.count(&:current_context?) if @parser.respond_to?(:pending)
118
- count
114
+ @pending.count(&:current_context?)
115
+ end
116
+
117
+ def interests
118
+ idling if (@state == :closed) && !no_more_requests?
119
+
120
+ super
119
121
  end
120
122
 
121
123
  private
122
124
 
125
+ def handle_transition(nextstate)
126
+ case nextstate
127
+ when :inactive
128
+ # @fiber-switch-guard
129
+ # connections may have requests (pending or inflight) from other fibers.
130
+ return if @state == :open && backlog?
131
+ end
132
+ super
133
+ end
134
+
135
+ def no_requests_to_handle?
136
+ pending_count.zero? && @parser&.no_requests_to_handle?
137
+ end
138
+
139
+ # @fiber-switch-guard
140
+ # pending requests from other fibers are still being waited for.
141
+ def clear_pending_requests
142
+ @pending.reject!(&:current_context?)
143
+ end
144
+
123
145
  # checks whether the connection has any pending request (which the connection itself may
124
146
  # have stored, or it may be somewhere in the parser).
125
147
  def backlog?
126
- @pending.any? ||
127
- (@parser && (@parser.pending.any? || @parser.requests.any?))
148
+ @pending.any? || @parser&.backlog? || @parser&.waiting_for_ping?
128
149
  end
129
150
  end
130
151
 
@@ -138,6 +159,14 @@ module HTTPX
138
159
 
139
160
  super
140
161
  end
162
+
163
+ def backlog?
164
+ !no_more_requests?
165
+ end
166
+
167
+ def no_requests_to_handle?
168
+ @requests.none?(&:current_context?)
169
+ end
141
170
  end
142
171
 
143
172
  module HTTP2Methods
@@ -162,6 +191,14 @@ module HTTPX
162
191
  super
163
192
  end
164
193
 
194
+ def backlog?
195
+ !no_more_requests?
196
+ end
197
+
198
+ def no_requests_to_handle?
199
+ @streams.each_key.none?(&:current_context?)
200
+ end
201
+
165
202
  private
166
203
 
167
204
  def on_close(_, error, _)
@@ -68,29 +68,30 @@ module HTTPX
68
68
  end
69
69
 
70
70
  def upgrade_to_h2c(request, response)
71
- enqueue_pending_requests_from_parser(@parser)
71
+ @parser.reset_requests
72
72
 
73
- @parser = request.options.h2c_class.new(@write_buffer, @options)
73
+ @parser = request.options.h2c_class.new(@write_buffer, @pending, @options)
74
74
  set_parser_callbacks(@parser)
75
- @inflight += 1 # request is being completed below
76
75
  @parser.upgrade(request, response)
77
76
  @upgrade_protocol = "h2c"
78
77
  end
79
78
 
80
79
  private
81
80
 
82
- def send_request_to_parser(request)
83
- super
81
+ def send_request_to_parser(request, *)
82
+ super.tap do |is_inflight|
83
+ next unless is_inflight
84
84
 
85
- return unless request.headers["upgrade"] == "h2c" && parser.is_a?(Connection::HTTP1)
85
+ next unless request.headers["upgrade"] == "h2c" && parser.is_a?(Connection::HTTP1)
86
86
 
87
- max_concurrent_requests = parser.max_concurrent_requests
87
+ max_concurrent_requests = parser.max_concurrent_requests
88
88
 
89
- return if max_concurrent_requests == 1
89
+ next if max_concurrent_requests == 1
90
90
 
91
- parser.max_concurrent_requests = 1
92
- request.once(:response) do
93
- parser.max_concurrent_requests = max_concurrent_requests
91
+ parser.max_concurrent_requests = 1
92
+ request.once(:response) do
93
+ parser.max_concurrent_requests = max_concurrent_requests
94
+ end
94
95
  end
95
96
  end
96
97
  end
@@ -270,10 +270,10 @@ module HTTPX
270
270
 
271
271
  private
272
272
 
273
- def send_request_to_parser(request)
273
+ def send_request_to_parser(request, *)
274
274
  # if original request was in the queue, but has since been finished
275
275
  # in the hedge connection.
276
- return if request.response&.finished?
276
+ return false if request.response&.finished?
277
277
 
278
278
  super
279
279
  end
@@ -41,9 +41,10 @@ module HTTPX
41
41
 
42
42
  if ntlm
43
43
  request.authorize(ntlm.negotiate)
44
- probe_response = wrap { super(request).first }
44
+ responses = wrap { super(request) }
45
+ probe_response = responses.first
45
46
 
46
- return probe_response unless probe_response.is_a?(Response)
47
+ return responses unless probe_response.is_a?(Response)
47
48
 
48
49
  if probe_response.status == 401 && ntlm.can_authenticate?(probe_response.headers["www-authenticate"])
49
50
  request.transition(:idle)
@@ -51,7 +52,7 @@ module HTTPX
51
52
  request.authorize(ntlm.authenticate(request, probe_response.headers["www-authenticate"]).encode("utf-8"))
52
53
  super(request)
53
54
  else
54
- probe_response
55
+ responses
55
56
  end
56
57
  else
57
58
  super(request)
@@ -67,9 +67,10 @@ module HTTPX
67
67
 
68
68
  if ntlm
69
69
  request.authorize(ntlm.negotiate)
70
- probe_response = wrap { super(request).first }
70
+ responses = wrap { super(request) }
71
+ probe_response = responses.first
71
72
 
72
- return probe_response unless probe_response.is_a?(Response)
73
+ return responses unless probe_response.is_a?(Response)
73
74
 
74
75
  if probe_response.status == 401 && ntlm.can_authenticate?(probe_response.headers["www-authenticate"])
75
76
  request.transition(:idle)
@@ -78,7 +79,7 @@ module HTTPX
78
79
  probe_response.headers["www-authenticate"]).encode("utf-8"))
79
80
  super(request)
80
81
  else
81
- probe_response
82
+ responses
82
83
  end
83
84
  else
84
85
  super(request)
@@ -290,7 +290,7 @@ module HTTPX
290
290
 
291
291
  def reset_auth_header_value!
292
292
  super.tap do
293
- @oauth_session.reset if @oauth_session
293
+ @oauth_session&.reset!
294
294
  end
295
295
  end
296
296
 
@@ -67,7 +67,15 @@ module HTTPX
67
67
 
68
68
  # whether the error can be safely retried without booking threshold attempts.
69
69
  def safe_reconnectable_error?(error)
70
- SAFE_REONNECTABLE_ERRORS.any? { |klass| error.is_a?(klass) }
70
+ case error
71
+ when Connection::HTTP2::GoawayError
72
+ # recurring GOAWAY which never accepts streams should escalate error
73
+ !error.unrecoverable?
74
+ when *SAFE_REONNECTABLE_ERRORS
75
+ true
76
+ else
77
+ false
78
+ end
71
79
  end
72
80
 
73
81
  def can_reconnect?(_, response)
@@ -16,9 +16,22 @@ module HTTPX
16
16
  # :enable_proxy_tunnel :: when <tt>true</tt>, it forces the establishment of the CONNECT proxy
17
17
  # tunnel, in cases where it wouldn't otherwise normally establish it
18
18
  # (i.e. if the request is an http:// request).
19
+ # :connect_request_class :: class of the request object used for the CONNECT tunnel request.
19
20
  module OptionsMethods
21
+ def extend_with_plugin_classes(pl)
22
+ super
23
+
24
+ @connect_request_class = @request_class.dup
25
+ Options::SET_TEMPORARY_NAME[@connect_request_class, pl]
26
+ @connect_request_class.__send__(:include, ConnectRequestMethods)
27
+ end
28
+
20
29
  private
21
30
 
31
+ def option_connect_request_class(val)
32
+ val
33
+ end
34
+
22
35
  def option_proxy_headers(value)
23
36
  value = value.dup if value.frozen?
24
37
 
@@ -80,8 +93,6 @@ module HTTPX
80
93
  if @state == :connecting
81
94
  # proxy connect related requests should not be reenqueed
82
95
  @parser.reset
83
- @inflight -= @parser.pending.size
84
- @parser.pending.clear
85
96
  end
86
97
 
87
98
  super
@@ -114,24 +125,26 @@ module HTTPX
114
125
  @tunnel_parser || begin
115
126
  if @parser
116
127
  # in case this happens, requests from @parser need to be reenqueued for delivery.
117
- enqueue_pending_requests_from_parser(@parser)
128
+ @parser.reset_requests
118
129
  @parser = nil
119
130
  end
120
131
 
121
132
  @tunnel_parser = @parser = parser =
122
- parser_type(@io.protocol).new(@write_buffer, @options.merge(max_concurrent_requests: 1))
133
+ parser_type(@io.protocol).new(@write_buffer, @pending, @options.merge(max_concurrent_requests: 1))
123
134
  parser.extend(ProxyParser)
135
+ parser.on(:request_accepted, &method(:set_request_timeouts))
124
136
  parser.on(:response, &method(:__http_on_connect))
125
137
  parser.on(:close) do
126
138
  next unless @parser
127
139
 
128
140
  reset
129
141
  end
142
+ parser.on(:error, &method(:on_parser_error))
130
143
  parser.on(:reset) do
131
- if parser.pending.empty? && parser.empty?
144
+ if parser.empty?
132
145
  reset
133
146
  else
134
- enqueue_pending_requests_from_parser(parser)
147
+ parser.reset_requests
135
148
 
136
149
  initial_state = @state
137
150
 
@@ -152,7 +165,7 @@ module HTTPX
152
165
  if initial_state == :connecting
153
166
  parser = tunnel_parser
154
167
  parser.reset
155
- if @pending.first.is_a?(ConnectRequest)
168
+ if @pending.first.is_a?(@options.connect_request_class)
156
169
  connect_request = @pending.shift # this happened when reenqueing
157
170
  end
158
171
  end
@@ -160,10 +173,7 @@ module HTTPX
160
173
  idling
161
174
 
162
175
  @tunnel_parser = @parser = parser
163
- if connect_request
164
- @inflight += 1
165
- parser.send(connect_request)
166
- end
176
+ parser.send(connect_request) if connect_request
167
177
  transition(:connecting)
168
178
  end
169
179
  end
@@ -197,7 +207,7 @@ module HTTPX
197
207
  # all requests in the queue are not only ALL HTTPS, but they also share the certificate,
198
208
  # and therefore, will share the connection.
199
209
  #
200
- connect_request = ConnectRequest.new(req.uri, @options)
210
+ connect_request = @options.connect_request_class.new(req.uri, @options)
201
211
  send(connect_request)
202
212
  else
203
213
  handle_transition(:connected)
@@ -205,7 +215,6 @@ module HTTPX
205
215
  end
206
216
 
207
217
  def __http_on_connect(request, response)
208
- @inflight -= 1
209
218
  if response.is_a?(Response) && response.status == 200
210
219
  req = @pending.first
211
220
  request_uri = req.uri
@@ -221,8 +230,7 @@ module HTTPX
221
230
  request.headers["proxy-authorization"] = @options.proxy.authenticate(request, response.headers["proxy-authenticate"])
222
231
  send(request)
223
232
  else
224
- pending = @pending + @parser.pending
225
- while (req = pending.shift)
233
+ while (req = @pending.shift)
226
234
  response.finish!
227
235
  req.response = response
228
236
  req.emit_response(response)
@@ -252,7 +260,7 @@ module HTTPX
252
260
  end
253
261
  end
254
262
 
255
- class ConnectRequest < Request
263
+ module ConnectRequestMethods
256
264
  def initialize(uri, options)
257
265
  super("CONNECT", uri, options)
258
266
  @headers = options.headers_class.new