protocol-http 0.11.2 → 0.12.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: 88089037bbc59e736f08b8df2137277f0c6cc74e5379ce9a4be1c4f7b0e4ae36
4
- data.tar.gz: d955cf9bd433f1d39f9a4a9764a2d13ffc7db492042318c1bc28d54e76a5bbfc
3
+ metadata.gz: 2d04792992fd8c7fdfed955ee45bf999ea6bb32036d1d47b74d4bf69f7f630fd
4
+ data.tar.gz: a79d348523228f4a7a2e11fb2e0a6930612fa0a3dd1c5847878e69b47a248361
5
5
  SHA512:
6
- metadata.gz: 97a2cdef4dc367663d2937ec106c827d48bbb77f2641f1f0816323c530fabbb4f4dad39ec70af7c0c8d22dadbe2f6051a400fb864a9dfc5c90b26531f1de511d
7
- data.tar.gz: b7a54a38afe7a9115146d5994e504c4d5ae054921b51a4cf905a31b41b17609bcc4d427b54b394311a6906c441f39b78b03e174015006bedc54573ede70abc20
6
+ metadata.gz: 39098deff7282f55a8f22017252ee2c52e4860d9d690e4f8806378f4f51dfe93ae28be62eb716471916126a8b29e1e24d78ceffb11ff97b7fc041c6da951b76a
7
+ data.tar.gz: 7c2c96c385a6528eb53bb069474cbf7a38b406a65abc749ad77b412db544963ff8ff17fce2e7617dcb294dc2fcdde6d2bb2d513dd236dd9d84c1a9b26f761719
@@ -67,15 +67,15 @@ module Protocol
67
67
  self.close($!)
68
68
  end
69
69
 
70
- def call(stream)
71
- # Flushing after every chunk is inefficient, but it's also a safe default.
72
- self.each do |chunk|
73
- stream.write(chunk)
74
- stream.flush
75
- end
76
- ensure
77
- stream.close
78
- end
70
+ # def call(stream)
71
+ # # Flushing after every chunk is inefficient, but it's also a safe default.
72
+ # self.each do |chunk|
73
+ # stream.write(chunk)
74
+ # stream.flush
75
+ # end
76
+ # ensure
77
+ # stream.close
78
+ # end
79
79
 
80
80
  # Read all remaining chunks into a single binary string using `#each`.
81
81
  def join
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright, 2017, 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 'url'
24
+
25
+ module Protocol
26
+ module HTTP
27
+ # Represents an individual cookie key-value pair.
28
+ class Cookie
29
+ def initialize(name, value, directives)
30
+ @name = name
31
+ @value = value
32
+ @directives = directives
33
+ end
34
+
35
+ def encoded_name
36
+ URL.escape(@name)
37
+ end
38
+
39
+ def encoded_value
40
+ URL.escape(@value)
41
+ end
42
+
43
+ def to_str
44
+ buffer = String.new.b
45
+
46
+ buffer << encoded_name << '=' << encoded_value
47
+
48
+ if @directives
49
+ @directives.collect do |key, value|
50
+ buffer << ';'
51
+
52
+ case value
53
+ when String
54
+ buffer << key << '=' << value
55
+ when TrueClass
56
+ buffer << key
57
+ end
58
+ end
59
+ end
60
+
61
+ return buffer
62
+ end
63
+
64
+ def self.parse string
65
+ head, *options = string.split(/\s*;\s*/)
66
+
67
+ key, value = head.split('=')
68
+ directives.collect{|directive| directive.split('=', 2)}.to_h
69
+
70
+ self.new(
71
+ URI.decode(key),
72
+ URI.decode(value),
73
+ directives,
74
+ )
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright, 2017, 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 'multiple'
24
+
25
+ module Protocol
26
+ module HTTP
27
+ module Header
28
+ # The Cookie HTTP request header contains stored HTTP cookies previously sent by the server with the Set-Cookie header.
29
+ class Cookie < Multiple
30
+ def to_h
31
+ cookies = self.collect do |string|
32
+ HTTP::Cookie.parse(string)
33
+ end
34
+
35
+ cookies.map{|cookie| [cookie.name, cookie]}.to_h
36
+ end
37
+ end
38
+
39
+ # The Set-Cookie HTTP response header sends cookies from the server to the user agent.
40
+ class SetCookie < Cookie
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright, 2017, 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
+ module Protocol
24
+ module HTTP
25
+ module Header
26
+ # Header value which is split by newline charaters (e.g. cookies).
27
+ class Multiple < Array
28
+ def initialize(value)
29
+ super()
30
+
31
+ self << value
32
+ end
33
+
34
+ def to_s
35
+ join("\n")
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright, 2017, 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
+ module Protocol
24
+ module HTTP
25
+ module Header
26
+ # Header value which is split by commas.
27
+ class Split < Array
28
+ COMMA = /\s*,\s*/
29
+
30
+ def initialize(value)
31
+ super(value.split(COMMA))
32
+ end
33
+
34
+ def << value
35
+ self.push(*value.split(COMMA))
36
+ end
37
+
38
+ def to_s
39
+ join(", ")
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -20,39 +20,16 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
+ require_relative 'header/split'
24
+ require_relative 'header/multiple'
25
+ require_relative 'header/cookie'
26
+
23
27
  module Protocol
24
28
  module HTTP
25
29
  # Headers are an array of key-value pairs. Some header keys represent multiple values.
26
30
  class Headers
27
- # Header value which is split by commas.
28
- class Split < Array
29
- COMMA = /\s*,\s*/
30
-
31
- def initialize(value)
32
- super(value.split(COMMA))
33
- end
34
-
35
- def << value
36
- self.push(*value.split(COMMA))
37
- end
38
-
39
- def to_s
40
- join(", ")
41
- end
42
- end
43
-
44
- # Header value which is split by newline charaters (e.g. cookies).
45
- class Multiple < Array
46
- def initialize(value)
47
- super()
48
-
49
- self << value
50
- end
51
-
52
- def to_s
53
- join("\n")
54
- end
55
- end
31
+ Split = Header::Split
32
+ Multiple = Header::Multiple
56
33
 
57
34
  def self.[] hash
58
35
  self.new(hash.to_a)
@@ -174,10 +151,13 @@ module Protocol
174
151
  'via' => Split,
175
152
  'x-forwarded-for' => Split,
176
153
 
177
- # Headers which may be specified multiple times, but which can't be concatenated.
178
- 'set-cookie' => Multiple,
154
+ # Headers which may be specified multiple times, but which can't be concatenated:
179
155
  'www-authenticate' => Multiple,
180
- 'proxy-authenticate' => Multiple
156
+ 'proxy-authenticate' => Multiple,
157
+
158
+ # Custom headers:
159
+ 'set-cookie' => Header::SetCookie,
160
+ 'cookie' => Header::Cookie,
181
161
  }.tap{|hash| hash.default = Split}
182
162
 
183
163
  # Delete all headers with the given key, and return the merged value.
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Protocol
22
22
  module HTTP
23
- VERSION = "0.11.2"
23
+ VERSION = "0.12.0"
24
24
  end
25
25
  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.11.2
4
+ version: 0.12.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: 2019-07-15 00:00:00.000000000 Z
11
+ date: 2019-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: covered
@@ -93,8 +93,12 @@ files:
93
93
  - lib/protocol/http/body/streamable.rb
94
94
  - lib/protocol/http/body/wrapper.rb
95
95
  - lib/protocol/http/content_encoding.rb
96
+ - lib/protocol/http/cookie.rb
96
97
  - lib/protocol/http/error.rb
97
98
  - lib/protocol/http/header/authorization.rb
99
+ - lib/protocol/http/header/cookie.rb
100
+ - lib/protocol/http/header/multiple.rb
101
+ - lib/protocol/http/header/split.rb
98
102
  - lib/protocol/http/headers.rb
99
103
  - lib/protocol/http/methods.rb
100
104
  - lib/protocol/http/middleware.rb