protocol-http 0.28.1 → 0.29.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a890dc1781e2ea174d0f534394aa9f22fe4324b8bdffca39960e3e6d74f555aa
4
- data.tar.gz: 44d2ff083bce62bd4f33c3310734b09565abda466c976cb84b92c984c25b9642
3
+ metadata.gz: e5839d2adb8415083224423beb2d5a3c0a50e22090a75424276e1a0939cc800a
4
+ data.tar.gz: 0517ecffe3b1f6c486b1cba2b5cbf4b3b7a8daf2cb11469b73d0d61cb5492cb1
5
5
  SHA512:
6
- metadata.gz: 00cfd9adcdd9cf55455bc0e87a1729b721d7db85b858d83f3695e160c8541c76b719ed0d2d2f9b04eb458cc1d4b829a8e9f075cf4eab39275081b29152d28b3c
7
- data.tar.gz: b01e10053404dfa78f2d20ed6fa9fd320623efc2b3fe255ad30864153207d00ba58b0b829898c7f7b900a7506ec7ad0126b1aefc4da22a9811ed23401f6e6e73
6
+ metadata.gz: 5fe865bc6ae53bfbd34ca9e43c6c38d2dcb3874b0655b1960936960b690cfefc0203ecde64ea7ea3af17a8a1093382d98f419e5d88b24708af99ef210a282e7e
7
+ data.tar.gz: 5c576ba8d79c0c6d21022d224b936f307640e2a785685cb82ec2eb0da588fcc8cbb215d2b7a05572ab91274978acc1f3a39edc0dfb5946fbef32dafa51389296
checksums.yaml.gz.sig CHANGED
Binary file
@@ -11,25 +11,29 @@ module Protocol
11
11
  module Body
12
12
  # A body which buffers all it's contents.
13
13
  class Buffered < Readable
14
- # Wraps an array into a buffered body.
14
+ # Tries to wrap an object in a {Buffered} instance.
15
15
  #
16
16
  # For compatibility, also accepts anything that behaves like an `Array(String)`.
17
17
  #
18
18
  # @parameter body [String | Array(String) | Readable | nil] the body to wrap.
19
19
  # @returns [Readable | nil] the wrapped body or nil if nil was given.
20
- def self.wrap(body)
21
- if body.is_a?(Readable)
22
- return body
23
- elsif body.is_a?(Array)
24
- return self.new(body)
25
- elsif body.is_a?(String)
26
- return self.new([body])
27
- elsif body
28
- return self.for(body)
20
+ def self.wrap(object)
21
+ if object.is_a?(Readable)
22
+ return object
23
+ elsif object.is_a?(Array)
24
+ return self.new(object)
25
+ elsif object.is_a?(String)
26
+ return self.new([object])
27
+ elsif object
28
+ return self.read(object)
29
29
  end
30
30
  end
31
31
 
32
- def self.for(body)
32
+ # Read the entire body into a buffered representation.
33
+ #
34
+ # @parameter body [Readable] the body to read.
35
+ # @returns [Buffered] the buffered body.
36
+ def self.read(body)
33
37
  chunks = []
34
38
 
35
39
  body.each do |chunk|
@@ -77,8 +81,14 @@ module Protocol
77
81
  @chunks << chunk
78
82
  end
79
83
 
84
+ def rewindable?
85
+ true
86
+ end
87
+
80
88
  def rewind
81
89
  @index = 0
90
+
91
+ return true
82
92
  end
83
93
 
84
94
  def inspect
@@ -24,6 +24,14 @@ module Protocol
24
24
  @callback = callback
25
25
  end
26
26
 
27
+ def rewindable?
28
+ false
29
+ end
30
+
31
+ def rewind
32
+ false
33
+ end
34
+
27
35
  def finish
28
36
  super.tap do
29
37
  if @callback
@@ -29,6 +29,14 @@ module Protocol
29
29
  false
30
30
  end
31
31
 
32
+ def rewindable?
33
+ false
34
+ end
35
+
36
+ def rewind
37
+ false
38
+ end
39
+
32
40
  def length
33
41
  nil
34
42
  end
@@ -58,7 +66,7 @@ module Protocol
58
66
  # @returns [Buffered] The buffered body.
59
67
  def finish
60
68
  # Internally, this invokes `self.each` which then invokes `self.close`.
61
- Buffered.for(self)
69
+ Buffered.read(self)
62
70
  end
63
71
 
64
72
  # Enumerate all chunks until finished, then invoke `#close`.
@@ -11,6 +11,16 @@ module Protocol
11
11
  module Body
12
12
  # A body which buffers all it's contents as it is `#read`.
13
13
  class Rewindable < Wrapper
14
+ def self.wrap(message)
15
+ if body = message.body
16
+ if body.rewindable?
17
+ body
18
+ else
19
+ message.body = self.new(body)
20
+ end
21
+ end
22
+ end
23
+
14
24
  def initialize(body)
15
25
  super(body)
16
26
 
@@ -26,7 +36,9 @@ module Protocol
26
36
  (@index < @chunks.size) || super
27
37
  end
28
38
 
29
- # A rewindable body wraps some other body. Convert it to a buffered body
39
+ # A rewindable body wraps some other body. Convert it to a buffered body. The buffered body will share the same chunks as the rewindable body.
40
+ #
41
+ # @returns [Buffered] the buffered body.
30
42
  def buffered
31
43
  Buffered.new(@chunks)
32
44
  end
@@ -54,6 +66,10 @@ module Protocol
54
66
  @index = 0
55
67
  end
56
68
 
69
+ def rewindable?
70
+ true
71
+ end
72
+
57
73
  def inspect
58
74
  "\#<#{self.class} #{@index}/#{@chunks.size} chunks read>"
59
75
  end
@@ -89,12 +89,25 @@ module Protocol
89
89
  # Will avoid reading from the underlying stream if there is buffered data available.
90
90
  #
91
91
  # @parameter length [Integer] The maximum number of bytes to read.
92
- def read_partial(length = nil)
92
+ def read_partial(length = nil, buffer = nil)
93
93
  if @buffer
94
- buffer = @buffer
94
+ if buffer
95
+ buffer.replace(@buffer)
96
+ else
97
+ buffer = @buffer
98
+ end
95
99
  @buffer = nil
96
100
  else
97
- buffer = read_next
101
+ if chunk = read_next
102
+ if buffer
103
+ buffer.replace(chunk)
104
+ else
105
+ buffer = chunk
106
+ end
107
+ else
108
+ buffer&.clear
109
+ buffer = nil
110
+ end
98
111
  end
99
112
 
100
113
  if buffer and length
@@ -111,8 +124,8 @@ module Protocol
111
124
  end
112
125
 
113
126
  # Similar to {read_partial} but raises an `EOFError` if the stream is at EOF.
114
- def readpartial(length)
115
- read_partial(length) or raise EOFError, "End of file reached!"
127
+ def readpartial(length, buffer = nil)
128
+ read_partial(length, buffer) or raise EOFError, "End of file reached!"
116
129
  end
117
130
 
118
131
  # Read data from the stream without blocking if possible.
@@ -10,6 +10,10 @@ module Protocol
10
10
  module Body
11
11
  # Wrapping body instance. Typically you'd override `#read`.
12
12
  class Wrapper < Readable
13
+ # Wrap the body of the given message in a new instance of this class.
14
+ #
15
+ # @parameter message [Request | Response] the message to wrap.
16
+ # @returns [Wrapper | nil] the wrapped body or nil if the body was nil.
13
17
  def self.wrap(message)
14
18
  if body = message.body
15
19
  message.body = self.new(body)
@@ -42,6 +46,14 @@ module Protocol
42
46
  @body.ready?
43
47
  end
44
48
 
49
+ def rewind
50
+ @body.rewind
51
+ end
52
+
53
+ def rewindable?
54
+ @body.rewindable?
55
+ end
56
+
45
57
  def length
46
58
  @body.length
47
59
  end
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Protocol
7
7
  module HTTP
8
- VERSION = "0.28.1"
8
+ VERSION = "0.29.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-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.28.1
4
+ version: 0.29.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -47,7 +47,7 @@ cert_chain:
47
47
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
48
48
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
49
49
  -----END CERTIFICATE-----
50
- date: 2024-07-19 00:00:00.000000000 Z
50
+ date: 2024-08-28 00:00:00.000000000 Z
51
51
  dependencies: []
52
52
  description:
53
53
  email:
metadata.gz.sig CHANGED
Binary file