protocol-http 0.16.3 → 0.17.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: 8f3dfaca48112b9b8d52096c8cab996403736391102e44a2f9ed6f5e71c2ce7d
4
- data.tar.gz: 611c1ba35c482cba1e4fbeb98daadaf7872b8d2198717c795dfa94a4343e7b38
3
+ metadata.gz: c02d05f1a36b888ef0420bcc999a1c84d81361f85ca1cf6a08d1f17a25dd9050
4
+ data.tar.gz: e65e3f34864adbea7577c8bcd06df99834af884db393a4d625c567266398a1a2
5
5
  SHA512:
6
- metadata.gz: 8d6988b4b2f2c1218c441d05788b88db0aa8890ac70877acc6f87c8ce2f18e32bae5d20505532ece25576c792c1935d42f69bb11ea3e34a8908211ed3fadc88f
7
- data.tar.gz: 111ec27937755f580826ff37e2cdfcce11b8fcc571434e31c12822e355694b5d4431fc378c3e91476368006bf657bcb0be164392302274221586074e0193b03a
6
+ metadata.gz: d5fb517f1f766d093cc54cb8503d215b74606e501f6f0f873b129ad8299900ea8cdae619f2b94a1d9f1c936e37235384f0d505d8773716e7ce6b4af74c53423e
7
+ data.tar.gz: 0e032fc0d601764a39404ddc5e28a98956295e049ca7c0a455977f2226f418db6beea4c65584ee549e8f5b798a4c284dfca71f17e82ccc37a704cf2f04f4c786
@@ -22,8 +22,6 @@
22
22
 
23
23
  require_relative 'readable'
24
24
 
25
- require 'digest/sha2'
26
-
27
25
  module Protocol
28
26
  module HTTP
29
27
  module Body
@@ -58,17 +56,9 @@ module Protocol
58
56
  @length = length
59
57
 
60
58
  @index = 0
61
- @digest = nil
62
59
  end
63
60
 
64
- def digest
65
- if @digest.nil?
66
- @digest = Digest::SHA256.new
67
- @chunks.each{|chunk| @digest.update(chunk)}
68
- end
69
-
70
- @digest.hexdigest
71
- end
61
+ attr :chunks
72
62
 
73
63
  def finish
74
64
  self
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require_relative 'wrapper'
24
+
25
+ require 'digest/sha2'
26
+
27
+ module Protocol
28
+ module HTTP
29
+ module Body
30
+ # Invokes a callback once the body has finished reading.
31
+ class Digestable < Wrapper
32
+ def self.wrap(message, digest = Digest::SHA256.new, &block)
33
+ if body = message&.body and !body.empty?
34
+ message.body = self.new(message.body, digest, block)
35
+ end
36
+ end
37
+
38
+ def initialize(body, digest = Digest::SHA256.new, callback = nil)
39
+ super(body)
40
+
41
+ @digest = digest
42
+ @callback = callback
43
+ end
44
+
45
+ def digest
46
+ @digest
47
+ end
48
+
49
+ def etag
50
+ @digest.hexdigest.dump
51
+ end
52
+
53
+ def read
54
+ if chunk = super
55
+ @digest.update(chunk)
56
+
57
+ return chunk
58
+ else
59
+ @callback&.call(self)
60
+
61
+ return nil
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -33,16 +33,6 @@ module Protocol
33
33
 
34
34
  @chunks = []
35
35
  @index = 0
36
- @digest = nil
37
- end
38
-
39
- def digest
40
- if @digest.nil?
41
- @digest = Digest::SHA256.new
42
- @chunks.each{|chunk| @digest.update(chunk)}
43
- end
44
-
45
- @digest.hexdigest
46
36
  end
47
37
 
48
38
  def empty?
@@ -61,7 +51,6 @@ module Protocol
61
51
  else
62
52
  if chunk = super
63
53
  @chunks << chunk
64
- @digest&.update(chunk)
65
54
  @index += 1
66
55
  end
67
56
  end
@@ -27,6 +27,12 @@ module Protocol
27
27
  module Body
28
28
  # Wrapping body instance. Typically you'd override `#read`.
29
29
  class Wrapper < Readable
30
+ def self.wrap(message)
31
+ if body = message.body
32
+ message.body = self.new(body)
33
+ end
34
+ end
35
+
30
36
  def initialize(body)
31
37
  @body = body
32
38
  end
@@ -53,7 +53,6 @@ module Protocol
53
53
 
54
54
  # Marks where trailers start in the @fields array.
55
55
  @tail = nil
56
- @deferred = []
57
56
  end
58
57
 
59
58
  def initialize_dup(other)
@@ -61,14 +60,13 @@ module Protocol
61
60
 
62
61
  @fields = @fields.dup
63
62
  @indexed = @indexed.dup
64
- @deferred = @deferred.dup
63
+ @tail = nil
65
64
  end
66
65
 
67
66
  def clear
68
67
  @fields.clear
69
68
  @indexed = nil
70
69
  @tail = nil
71
- @deferred.clear
72
70
  end
73
71
 
74
72
  # An array of `[key, value]` pairs.
@@ -84,23 +82,13 @@ module Protocol
84
82
  @tail != nil
85
83
  end
86
84
 
87
- def flatten!
88
- unless @deferred.empty?
89
- @tail ||= @fields.size
90
-
91
- @deferred.each do |key, value|
92
- self.add(key, value.call)
93
- end
94
- end
95
- end
96
-
97
85
  # Enumerate all trailers, including evaluating all deferred headers.
98
86
  def trailers(&block)
99
87
  return nil unless self.include?(TRAILERS)
100
88
 
101
- return to_enum(:trailers) unless block_given?
89
+ trailers!
102
90
 
103
- flatten!
91
+ return to_enum(:trailers) unless block_given?
104
92
 
105
93
  if @tail
106
94
  @fields.drop(@tail).each(&block)
@@ -110,18 +98,9 @@ module Protocol
110
98
  def freeze
111
99
  return if frozen?
112
100
 
113
- # Ensure all deferred headers are evaluated:
114
- self.flatten!
115
-
116
101
  # Ensure @indexed is generated:
117
102
  self.to_h
118
103
 
119
- # Remove all trailers:
120
- self.delete(TRAILERS)
121
-
122
- # No longer has stateful trailers:
123
- @tail = nil
124
-
125
104
  @fields.freeze
126
105
  @indexed.freeze
127
106
 
@@ -161,14 +140,8 @@ module Protocol
161
140
  # Add the specified header key value pair.
162
141
  # @param key [String] the header key.
163
142
  # @param value [String] the header value to assign.
164
- # @yield dynamically generate the value when used as a trailer.
165
- def add(key, value = nil, &block)
166
- if block_given?
167
- @deferred << [key, block]
168
- self[TRAILERS] = key
169
- else
170
- self[key] = value
171
- end
143
+ def add(key, value)
144
+ self[key] = value
172
145
  end
173
146
 
174
147
  # Set the specified header key to the specified value, replacing any existing header keys with the same name.
@@ -28,6 +28,14 @@ require_relative 'response'
28
28
  module Protocol
29
29
  module HTTP
30
30
  class Middleware < Methods
31
+ # Convert a block to a middleware delegate.
32
+ def self.for(&block)
33
+ def block.close
34
+ end
35
+
36
+ return self.new(block)
37
+ end
38
+
31
39
  def initialize(delegate)
32
40
  @delegate = delegate
33
41
  end
@@ -69,6 +69,7 @@ module Protocol
69
69
 
70
70
  def self.[](method, path, headers, body)
71
71
  body = Body::Buffered.wrap(body)
72
+ headers = ::Protocol::HTTP::Headers[headers]
72
73
 
73
74
  self.new(nil, nil, method, path, nil, headers, body)
74
75
  end
@@ -90,6 +90,7 @@ module Protocol
90
90
 
91
91
  def self.[](status, headers = [], body = nil, protocol = nil)
92
92
  body = Body::Buffered.wrap(body)
93
+ headers = ::Protocol::HTTP::Headers[headers]
93
94
 
94
95
  self.new(nil, status, headers, body, protocol)
95
96
  end
@@ -22,6 +22,6 @@
22
22
 
23
23
  module Protocol
24
24
  module HTTP
25
- VERSION = "0.16.3"
25
+ VERSION = "0.17.0"
26
26
  end
27
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.3
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-06 00:00:00.000000000 Z
11
+ date: 2020-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: covered
@@ -83,6 +83,7 @@ files:
83
83
  - lib/protocol/http/accept_encoding.rb
84
84
  - lib/protocol/http/body/buffered.rb
85
85
  - lib/protocol/http/body/deflate.rb
86
+ - lib/protocol/http/body/digestable.rb
86
87
  - lib/protocol/http/body/file.rb
87
88
  - lib/protocol/http/body/head.rb
88
89
  - lib/protocol/http/body/inflate.rb