protocol-http 0.13.1 → 0.14.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: 1593ccb0ebfb4ce5ef909963649b47e0d1f8b4b9b89f9f0338566dbf6060faae
4
- data.tar.gz: 3277cbdce30d537b66a16d269e06bb5751f018191cf68c986da66dfc6ffbb8a8
3
+ metadata.gz: 01134dcd19c1c455c4708bb156394142cf7d1c0fad2c77c6215b38be4bf222f1
4
+ data.tar.gz: 3d5d904768b5db52a90a671334f67e9b1aa94a25fec68104ca51165d448c3a63
5
5
  SHA512:
6
- metadata.gz: 0f5bf28393d98394e21bc031dcd99f221f5fce66357dabb295b841b1ce36e87675eb8d89b03ac8f9999b04547d436362deb4821a41cb243bd9a8c8f63209afff
7
- data.tar.gz: 4eab7664bc1cb4ca1a6cd04b421f1cfba1fa9609ccdd988045b7564946c1f9345e383ef12324806dfeaaeedd2f256adefab885cc4d9d20b31ba91f728a14e2c0
6
+ metadata.gz: d0ea0d0cafc40fd91c8b4ce519e5ff8de4b11001313e5124deb4ededc17a1c05cf173f695646b4361ee827a925654f4cf454b60d9715e5db67c33ebecf3f0c7b
7
+ data.tar.gz: 516d31553b772881473ba0008c52e7081a1c274d3c3d000bc5818cd765f9e9cc34aa492d4665660813bbdf2911beae67b7c72b293ea53fed490d07fdba60b7e8
@@ -82,10 +82,6 @@ module Protocol
82
82
  @chunks << chunk
83
83
  end
84
84
 
85
- def close(error = nil)
86
- @chunks << nil
87
- end
88
-
89
85
  def rewind
90
86
  @index = 0
91
87
  end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright, 2020, 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 'rewindable'
24
+ require_relative 'streamable'
25
+
26
+ module Protocol
27
+ module HTTP
28
+ module Body
29
+ class Cacheable < Rewindable
30
+ def self.wrap(message, &block)
31
+ if body = message.body
32
+ # Create a rewindable body wrapping the message body:
33
+ rewindable = Rewindable.new(body)
34
+
35
+ # Set the message body to the rewindable body:
36
+ message.body = rewindable
37
+
38
+ # Wrap the message with the callback:
39
+ Streamable.wrap(message) do |error|
40
+ unless error
41
+ yield message, rewindable.buffered
42
+ end
43
+ end
44
+ else
45
+ yield message, nil
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -21,6 +21,7 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  require_relative 'wrapper'
24
+ require_relative 'buffered'
24
25
 
25
26
  module Protocol
26
27
  module HTTP
@@ -34,6 +35,11 @@ module Protocol
34
35
  @index = 0
35
36
  end
36
37
 
38
+ # A rewindable body wraps some other body. Convert it to a buffered body
39
+ def buffered
40
+ Buffered.new(@chunks)
41
+ end
42
+
37
43
  def read
38
44
  if @index < @chunks.size
39
45
  chunk = @chunks[@index]
@@ -28,7 +28,7 @@ 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 message and body = message.body
31
+ if body = message&.body
32
32
  if remaining = body.length
33
33
  remaining = Integer(remaining)
34
34
  end
@@ -58,11 +58,11 @@ module Protocol
58
58
  end
59
59
  end
60
60
 
61
- def close(*)
61
+ def close(error = nil)
62
62
  if @body
63
63
  super
64
64
 
65
- @callback.call
65
+ @callback.call(error)
66
66
 
67
67
  @body = nil
68
68
  end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2020, 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 'split'
24
+
25
+ module Protocol
26
+ module HTTP
27
+ module Header
28
+ # Header value which is split by newline charaters (e.g. cookies).
29
+ class CacheControl < Split
30
+ PRIVATE = 'private'
31
+ PUBLIC = 'public'
32
+ MAX_AGE = 'max-age'
33
+
34
+ def initialize(value)
35
+ super(value.downcase)
36
+ end
37
+
38
+ def << value
39
+ super(value.downcase)
40
+ end
41
+
42
+ def private?
43
+ self.include?(PRIVATE)
44
+ end
45
+
46
+ def public?
47
+ self.include?(PUBLIC)
48
+ end
49
+
50
+ def max_age
51
+ if value = self.find{|value| value.start_with?(MAX_AGE)}
52
+ _, age = value.split('=', 2)
53
+
54
+ return Integer(age)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -24,6 +24,7 @@ require_relative 'header/split'
24
24
  require_relative 'header/multiple'
25
25
  require_relative 'header/cookie'
26
26
  require_relative 'header/connection'
27
+ require_relative 'header/cache_control'
27
28
 
28
29
  module Protocol
29
30
  module HTTP
@@ -147,6 +148,7 @@ module Protocol
147
148
  'max-forwards' => false,
148
149
 
149
150
  'connection' => Header::Connection,
151
+ 'cache-control' => Header::CacheControl,
150
152
 
151
153
  # Headers specifically for proxies:
152
154
  'via' => Split,
@@ -51,7 +51,7 @@ module Protocol
51
51
  end
52
52
 
53
53
  def to_app
54
- @use.reverse.inject(@app) {|app, use| use.call(app).freeze}
54
+ @use.reverse.inject(@app) {|app, use| use.call(app)}
55
55
  end
56
56
  end
57
57
 
@@ -22,6 +22,6 @@
22
22
 
23
23
  module Protocol
24
24
  module HTTP
25
- VERSION = "0.13.1"
25
+ VERSION = "0.14.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.13.1
4
+ version: 0.14.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-02-01 00:00:00.000000000 Z
11
+ date: 2020-03-05 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.rb
84
84
  - lib/protocol/http/accept_encoding.rb
85
85
  - lib/protocol/http/body/buffered.rb
86
+ - lib/protocol/http/body/cacheable.rb
86
87
  - lib/protocol/http/body/deflate.rb
87
88
  - lib/protocol/http/body/file.rb
88
89
  - lib/protocol/http/body/inflate.rb
@@ -96,6 +97,7 @@ files:
96
97
  - lib/protocol/http/cookie.rb
97
98
  - lib/protocol/http/error.rb
98
99
  - lib/protocol/http/header/authorization.rb
100
+ - lib/protocol/http/header/cache_control.rb
99
101
  - lib/protocol/http/header/connection.rb
100
102
  - lib/protocol/http/header/cookie.rb
101
103
  - lib/protocol/http/header/multiple.rb