protocol-http 0.14.4 → 0.15.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: 6a49c7a92140dc05ce537c23ddb54a8e5b9c9c610d560459b92fb1d5f69aaef4
4
- data.tar.gz: 249972b584e5c4bd0e13d54e88acbbb65a2b361d92dcd06427ed127ec192f2e8
3
+ metadata.gz: 5af6588e75cd95086183ad1e1d27a4c70d2a59d284b7640cbe3d22b7630c3318
4
+ data.tar.gz: a03a42a4d0bda22e902a66720d884a53351a298e8637744463e653415ca7e0b5
5
5
  SHA512:
6
- metadata.gz: 62e4a29eba1014db803d8fbdb944551050540515aa35a9635f3fcbf6bbe40b935c44e6f516c0ef85275d1084f504639060a5c7ca3ac3753fb59a57d733dba027
7
- data.tar.gz: 0da73f93d930607d4468327c581ff7bedcc60699603334aed92354fe46b5dee48dd9a20c08973c2de3fde127bcfe2a13efe4f11a89f01a404c42c91f556b2ce8
6
+ metadata.gz: c8c7ecb9521a14e3f17e471fb3a805bb4b6b58ef29fcddce709e633ab2dd5a2ac3cc1c0673ac45947766ec888caae7c604dc510c920a187b23fe0a66d88d063c
7
+ data.tar.gz: ee2cd54ee6b2f2a864ad193706c1f46ba116b755da6b31d118224b6c2ada54df331803e9bc36089667a3f105bce14810bb44ae6d2ce1246b5857163ee988f56a
@@ -22,6 +22,8 @@
22
22
 
23
23
  require_relative 'readable'
24
24
 
25
+ require 'digest/sha2'
26
+
25
27
  module Protocol
26
28
  module HTTP
27
29
  module Body
@@ -30,9 +32,9 @@ module Protocol
30
32
  # Wraps an array into a buffered body.
31
33
  # @return [Readable, nil] the wrapped body or nil if nil was given.
32
34
  def self.wrap(body)
33
- if body.is_a? Readable
35
+ if body.is_a?(Readable)
34
36
  return body
35
- elsif body.is_a? Array
37
+ elsif body.is_a?(Array)
36
38
  return self.new(body)
37
39
  elsif body.is_a?(String)
38
40
  return self.new([body])
@@ -56,6 +58,11 @@ module Protocol
56
58
  @length = length
57
59
 
58
60
  @index = 0
61
+ @digest = nil
62
+ end
63
+
64
+ def digest
65
+ @digest ||= compute_digest
59
66
  end
60
67
 
61
68
  def finish
@@ -79,6 +86,7 @@ module Protocol
79
86
  end
80
87
 
81
88
  def write(chunk)
89
+ @digest = nil
82
90
  @chunks << chunk
83
91
  end
84
92
 
@@ -89,6 +97,14 @@ module Protocol
89
97
  def inspect
90
98
  "\#<#{self.class} #{@chunks.size} chunks, #{self.length} bytes>"
91
99
  end
100
+
101
+ private
102
+
103
+ def compute_digest
104
+ algorithm = Digest::SHA256.new
105
+ @chunks.each{|chunk| algorithm.update(chunk)}
106
+ return algorithm.hexdigest
107
+ end
92
108
  end
93
109
  end
94
110
  end
@@ -0,0 +1,51 @@
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 'readable'
24
+
25
+ module Protocol
26
+ module HTTP
27
+ module Body
28
+ class Head < Readable
29
+ def self.for(body)
30
+ head = self.new(body.length)
31
+
32
+ body.close
33
+
34
+ return head
35
+ end
36
+
37
+ def initialize(length)
38
+ @length = length
39
+ end
40
+
41
+ def empty?
42
+ true
43
+ end
44
+
45
+ def length
46
+ @length
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -63,7 +63,6 @@ module Protocol
63
63
  def each
64
64
  while chunk = self.read
65
65
  yield chunk
66
- # chunk.clear
67
66
  end
68
67
  ensure
69
68
  self.close($!)
@@ -78,7 +77,11 @@ module Protocol
78
77
  chunk.clear
79
78
  end
80
79
 
81
- return buffer
80
+ if buffer.empty?
81
+ return nil
82
+ else
83
+ return buffer
84
+ end
82
85
  end
83
86
  end
84
87
  end
@@ -28,22 +28,17 @@ module Protocol
28
28
  # Invokes a callback once the body has finished reading.
29
29
  class Streamable < Wrapper
30
30
  def self.wrap(message, &block)
31
- if body = message&.body
32
- if remaining = body.length
33
- remaining = Integer(remaining)
34
- end
35
-
36
- message.body = self.new(message.body, block, remaining)
31
+ if body = message&.body and !body.empty?
32
+ message.body = self.new(message.body, block)
37
33
  else
38
34
  yield
39
35
  end
40
36
  end
41
37
 
42
- def initialize(body, callback, remaining = nil)
38
+ def initialize(body, callback)
43
39
  super(body)
44
40
 
45
41
  @callback = callback
46
- @remaining = remaining
47
42
  end
48
43
 
49
44
  def finish
@@ -67,18 +62,6 @@ module Protocol
67
62
  @body = nil
68
63
  end
69
64
  end
70
-
71
- def read
72
- if chunk = super
73
- @remaining -= chunk.bytesize if @remaining
74
- else
75
- if @remaining and @remaining > 0
76
- raise EOFError, "Expected #{@remaining} more bytes!"
77
- end
78
- end
79
-
80
- return chunk
81
- end
82
65
  end
83
66
  end
84
67
  end
@@ -28,11 +28,11 @@ module Protocol
28
28
  class Request
29
29
  prepend Body::Reader
30
30
 
31
- def initialize(scheme = nil, authority = nil, method = nil, path = nil, version = nil, headers = [], body = nil, protocol = nil)
31
+ def initialize(scheme = nil, authority = nil, method = nil, target = nil, version = nil, headers = [], body = nil, protocol = nil)
32
32
  @scheme = scheme
33
33
  @authority = authority
34
34
  @method = method
35
- @path = path
35
+ @target = target
36
36
  @version = version
37
37
  @headers = headers
38
38
  @body = body
@@ -42,7 +42,7 @@ module Protocol
42
42
  attr_accessor :scheme
43
43
  attr_accessor :authority
44
44
  attr_accessor :method
45
- attr_accessor :path
45
+ attr_accessor :target
46
46
  attr_accessor :version
47
47
  attr_accessor :headers
48
48
  attr_accessor :body
@@ -53,6 +53,14 @@ module Protocol
53
53
  connection.call(self)
54
54
  end
55
55
 
56
+ def path
57
+ @target
58
+ end
59
+
60
+ def path= value
61
+ @target = value
62
+ end
63
+
56
64
  def head?
57
65
  @method == Methods::HEAD
58
66
  end
@@ -72,7 +80,7 @@ module Protocol
72
80
  end
73
81
 
74
82
  def to_s
75
- "#{@scheme}://#{@authority}: #{@method} #{@path} #{@version}"
83
+ "#{@scheme}://#{@authority}: #{@method} #{@target} #{@version}"
76
84
  end
77
85
  end
78
86
  end
@@ -100,6 +100,7 @@ module Protocol
100
100
  parent[top] = value
101
101
  end
102
102
 
103
+ # TODO use native C extension from `Trenni::Reference`.
103
104
  def self.decode(string, maximum = 8, symbolize_keys: false)
104
105
  parameters = {}
105
106
 
@@ -22,6 +22,6 @@
22
22
 
23
23
  module Protocol
24
24
  module HTTP
25
- VERSION = "0.14.4"
25
+ VERSION = "0.15.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.14.4
4
+ version: 0.15.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-03-10 00:00:00.000000000 Z
11
+ date: 2020-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: covered
@@ -85,6 +85,7 @@ files:
85
85
  - lib/protocol/http/body/buffered.rb
86
86
  - lib/protocol/http/body/deflate.rb
87
87
  - lib/protocol/http/body/file.rb
88
+ - lib/protocol/http/body/head.rb
88
89
  - lib/protocol/http/body/inflate.rb
89
90
  - lib/protocol/http/body/readable.rb
90
91
  - lib/protocol/http/body/reader.rb