protocol-http1 0.24.0 → 0.26.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0854252b3602c1271818e28024b62e1a4373521052b75ba0aa879a1f77a6e84e'
4
- data.tar.gz: 2ad1603fc177c4b8493f304806a1f8baf4dc147b0f1707d463f534fb1e740a62
3
+ metadata.gz: 868c9501fb3b9a49db11985bb2807cdbfe9ed6c5337c0e01fabd5f7f360874e0
4
+ data.tar.gz: 91a076094374bbdd0b42938e7d6dcaaf8cd24170d21da0043be63e4ebe719ffb
5
5
  SHA512:
6
- metadata.gz: 213525d129854f2198cb1be6be3d99a74d7add2e4a8dc57ed72d3bd1854b29535256120dfcbd735f1823d91bb6bf27fee6571d93d8e85f0031a404be99b39e01
7
- data.tar.gz: 32f7a63f81f830a77839fa154b69bfa5a8dc316d579cc48e10d2a786e92806960c36821b3f53da67c3b1e90ba29394264ae76c621d7608c62a0d56224dabec1f
6
+ metadata.gz: 14e0e59673d857dd2e37bcf8b9b1ef607d9490445c13bd515530aabd04dd75f93eca1055a3168384b0fe47f3197494b6be544e3e4f767d55456a4808e090e738
7
+ data.tar.gz: ea52162a1762c35cf7ea76630c51f589f52385baf5819b7a4010cec1a341e0ec153ffba1970bead118ebf7e309e63d400a2bc7e81195cd8c1d547c924c4c83d9
checksums.yaml.gz.sig CHANGED
Binary file
@@ -22,23 +22,27 @@ module Protocol
22
22
  @count = 0
23
23
  end
24
24
 
25
+ attr :count
26
+
27
+ def length
28
+ # We only know the length once we've read everything. This is because the length is not known until the final chunk is read.
29
+ if @finished
30
+ @length
31
+ end
32
+ end
33
+
25
34
  def empty?
26
35
  @connection.nil?
27
36
  end
28
37
 
29
- def discard
38
+ def close(error = nil)
30
39
  if connection = @connection
31
40
  @connection = nil
32
41
 
33
- # We only close the connection if we haven't completed reading the entire body:
34
42
  unless @finished
35
43
  connection.close_read
36
44
  end
37
45
  end
38
- end
39
-
40
- def close(error = nil)
41
- self.discard
42
46
 
43
47
  super
44
48
  end
@@ -82,7 +86,8 @@ module Protocol
82
86
  return chunk
83
87
  else
84
88
  # The connection has been closed before we have read the requested length:
85
- self.discard
89
+ @connection.close_read
90
+ @connection = nil
86
91
  end
87
92
  end
88
93
 
@@ -23,18 +23,14 @@ module Protocol
23
23
  @connection.nil? or @remaining == 0
24
24
  end
25
25
 
26
- def discard
26
+ def close(error = nil)
27
27
  if connection = @connection
28
28
  @connection = nil
29
29
 
30
- if @remaining != 0
30
+ unless @remaining == 0
31
31
  connection.close_read
32
32
  end
33
33
  end
34
- end
35
-
36
- def close(error = nil)
37
- self.discard
38
34
 
39
35
  super
40
36
  end
@@ -39,8 +39,12 @@ module Protocol
39
39
  def read
40
40
  @connection&.readpartial(BLOCK_SIZE)
41
41
  rescue EOFError
42
- @connection.receive_end_stream!
43
- @connection = nil
42
+ if connection = @connection
43
+ @connection = nil
44
+ connection.receive_end_stream!
45
+ end
46
+
47
+ return nil
44
48
  end
45
49
 
46
50
  def inspect
@@ -91,8 +91,8 @@ module Protocol
91
91
  # │ ▼ └───┬────┘ ▼
92
92
  # │ ┌──────────┐ │ ┌──────────┐
93
93
  # │ │ half │ │ │ half │
94
- # │ │ closed │ │ │ closed │
95
- # │ │ (remote) │ │ │ (local) │
94
+ # │ │ closed │ │ send R / │ closed │
95
+ # │ │ (remote) │ │ recv R │ (local) │
96
96
  # │ └────┬─────┘ │ └─────┬────┘
97
97
  # │ │ │ │
98
98
  # │ │ send ES / │ recv ES / │
@@ -104,7 +104,8 @@ module Protocol
104
104
  # persistent └────────┘
105
105
  # ```
106
106
  #
107
- # - `ES`: the body was fully received or sent (end of stream)
107
+ # - `ES`: the body was fully received or sent (end of stream).
108
+ # - `R`: the connection was closed unexpectedly (reset).
108
109
  #
109
110
  # State transition methods use a trailing "!".
110
111
  attr_accessor :state
@@ -176,17 +177,33 @@ module Protocol
176
177
  # @return [IO] the underlying non-blocking IO.
177
178
  def hijack!
178
179
  @persistent = false
179
- stream = @stream
180
180
 
181
- @stream.flush
182
- @stream = nil
183
-
184
- return stream
181
+ if stream = @stream
182
+ @stream = nil
183
+ stream.flush
184
+
185
+ self.closed!
186
+
187
+ return stream
188
+ end
189
+ end
190
+
191
+ def close_read
192
+ @persistent = false
193
+ @stream&.close_read
194
+ self.receive_end_stream!
185
195
  end
186
196
 
187
197
  # Close the connection and underlying stream.
188
- def close
189
- @stream&.close
198
+ def close(error = nil)
199
+ @persistent = false
200
+
201
+ if stream = @stream
202
+ @stream = nil
203
+ stream.close
204
+ end
205
+
206
+ self.closed!(error)
190
207
  end
191
208
 
192
209
  def open!
@@ -264,10 +281,6 @@ module Protocol
264
281
  read_line? or raise EOFError
265
282
  end
266
283
 
267
- def close_read
268
- @stream.close_read
269
- end
270
-
271
284
  def read_request_line
272
285
  return unless line = read_line?
273
286
 
@@ -354,6 +367,16 @@ module Protocol
354
367
  return HTTP::Headers.new(fields)
355
368
  end
356
369
 
370
+ def send_end_stream!
371
+ if @state == :open
372
+ @state = :half_closed_local
373
+ elsif @state == :half_closed_remote
374
+ self.closed!
375
+ else
376
+ raise ProtocolError, "Cannot send end stream in #{@state}!"
377
+ end
378
+ end
379
+
357
380
  # @param protocol [String] the protocol to upgrade to.
358
381
  def write_upgrade_body(protocol, body = nil)
359
382
  # Once we upgrade the connection, it can no longer handle other requests:
@@ -374,6 +397,8 @@ module Protocol
374
397
  end
375
398
 
376
399
  return @stream
400
+ ensure
401
+ self.send_end_stream!
377
402
  end
378
403
 
379
404
  def write_tunnel_body(version, body = nil)
@@ -394,6 +419,8 @@ module Protocol
394
419
  end
395
420
 
396
421
  return @stream
422
+ ensure
423
+ self.send_end_stream!
397
424
  end
398
425
 
399
426
  def write_empty_body(body)
@@ -401,6 +428,8 @@ module Protocol
401
428
  @stream.flush
402
429
 
403
430
  body&.close
431
+ ensure
432
+ self.send_end_stream!
404
433
  end
405
434
 
406
435
  def write_fixed_length_body(body, length, head)
@@ -433,6 +462,8 @@ module Protocol
433
462
  if chunk_length != length
434
463
  raise ContentLengthError, "Wrote #{chunk_length} bytes, but content length was #{length} bytes!"
435
464
  end
465
+ ensure
466
+ self.send_end_stream!
436
467
  end
437
468
 
438
469
  def write_chunked_body(body, head, trailer = nil)
@@ -467,6 +498,8 @@ module Protocol
467
498
  end
468
499
 
469
500
  @stream.flush
501
+ ensure
502
+ self.send_end_stream!
470
503
  end
471
504
 
472
505
  def write_body_and_close(body, head)
@@ -488,34 +521,24 @@ module Protocol
488
521
 
489
522
  @stream.flush
490
523
  @stream.close_write
524
+ ensure
525
+ self.send_end_stream!
491
526
  end
492
527
 
493
- def idle!
494
- @state = :idle
495
- end
496
-
497
- def closed!
498
- unless @state == :half_closed_local or @state == :half_closed_remote
499
- raise ProtocolError, "Cannot close in #{@state}!"
500
- end
501
-
502
- if @persistent
503
- self.idle!
528
+ # Transition to the closed state.
529
+ #
530
+ # If no error occurred, and the connection is persistent, this will immediately transition to the idle state.
531
+ #
532
+ # @parameter error [Exxception] the error that caused the connection to close.
533
+ def closed!(error = nil)
534
+ if @persistent and !error
535
+ # If there was no error, and the connection is persistent, we can reuse it:
536
+ @state = :idle
504
537
  else
505
538
  @state = :closed
506
539
  end
507
540
  end
508
541
 
509
- def send_end_stream!
510
- if @state == :open
511
- @state = :half_closed_local
512
- elsif @state == :half_closed_remote
513
- self.closed!
514
- else
515
- raise ProtocolError, "Cannot send end stream in #{@state}!"
516
- end
517
- end
518
-
519
542
  def write_body(version, body, head = false, trailer = nil)
520
543
  # HTTP/1.0 cannot in any case handle trailers.
521
544
  if version == HTTP10 # or te: trailers was not present (strictly speaking not required.)
@@ -545,8 +568,6 @@ module Protocol
545
568
  write_connection_header(version)
546
569
  write_body_and_close(body, head)
547
570
  end
548
- ensure
549
- send_end_stream!
550
571
  end
551
572
 
552
573
  def receive_end_stream!
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Protocol
7
7
  module HTTP1
8
- VERSION = "0.24.0"
8
+ VERSION = "0.26.0"
9
9
  end
10
10
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-http1
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.24.0
4
+ version: 0.26.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -42,7 +42,7 @@ cert_chain:
42
42
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
43
43
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
44
44
  -----END CERTIFICATE-----
45
- date: 2024-09-18 00:00:00.000000000 Z
45
+ date: 2024-09-23 00:00:00.000000000 Z
46
46
  dependencies:
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: protocol-http
metadata.gz.sig CHANGED
@@ -1,2 +1,2 @@
1
- .���֗Zt��"��(×��Y�.GC#�
2
- �0�V}ǻ&j�Mę���W��]'¦�۩xt�|�[�{�V+�ML��3�)�*����[�!��+��`M��^���u�~I�?��U��9vk���ϗrZ�9�6}N�궮+v,��տP('.��K[T�L\��s̀(&�2h�Ӝ��*�n@� O���uuQ��x���})4ʧy�N���}d�����"A�.e�,��F
1
+ �������5�?% z��%�F�HT�+�Fڼ�� ���K�Iu/�A��*��s�c���Ǟ�������}����Y�6���!$�i�1Y�Z��E�&������g:)ʈ@����f�P�&WR���zA~��{X��l��~��-Q���z��vwws�)5�%0i�z6��s?�cu����&��jB����q�K2/@�`ǣL*�đ� LJ���gsq��Dlp��b���/r�����:�;h�5����e���%^��l����$t-7��Ќ���c�(��)h@@�S�1g�N\�
2
+ !���Nk��L�)`",#v���j��z��<���+��;Gm9��W�*�P\�[�(¨$�����bTf���8q���K�