http-protocol 0.12.2 → 0.13.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: 1648aa911c5bb62d7da0d85545dbf1de8a6c2bb3deb7da34bdb1a1b2a7a62ebb
4
- data.tar.gz: ed8606f2c1e33439481e1512328ea65f346e2bc18116251f545aadec76a3e422
3
+ metadata.gz: 6a7da4c00bbe6c3eeb8c56f46f4d4e78ff133f360b11396229b620873c33c35b
4
+ data.tar.gz: b822849d7f1614eab1a469d446569ac5456a7b3921f2fa2402a89deb2fdc2a88
5
5
  SHA512:
6
- metadata.gz: 7d235ba93875ef76ab55412a919e540b867b01f0793ddb17b159567ac608e42b72def6b52be7dab494802bb6ee8f4cf510a73a7a8a66287c0fefb8b973a15d42
7
- data.tar.gz: 2a600cc9274d9d23452b785156aa753cbed209f241e225d6bcf2aba8ccd451a8c84636b77366d7aea52072fb724dbcddc22ce21a37fc28991593ef55e4a7a114
6
+ metadata.gz: 9e3856fed9a60a1793b0dbcfc08d860b8e5a1b1d4a12b383cd64bee0775851068a18f56fd6c2c3d3afc5d60dda0810e93e5d45d579c603431196f81b5aef3df2
7
+ data.tar.gz: 2fdb86e2258946bf0b92c15358817a56bea6b640f091f11903c1455f7f5fd5fa272d31135818612d661d7c9160eb765e5113b49867c15c1e575f5c0731dd2b8b
@@ -18,6 +18,8 @@
18
18
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
19
  # THE SOFTWARE.
20
20
 
21
+ require_relative 'url'
22
+
21
23
  module HTTP
22
24
  module Protocol
23
25
  # A relative reference, excluding any authority.
@@ -71,15 +73,15 @@ module HTTP
71
73
 
72
74
  def append(buffer)
73
75
  if query_string?
74
- buffer << escape_path(@path) << '?' << @query_string
75
- buffer << '&' << encode(@parameters) if parameters?
76
+ buffer << URL.escape_path(@path) << '?' << @query_string
77
+ buffer << '&' << URL.encode(@parameters) if parameters?
76
78
  else
77
- buffer << escape_path(@path)
78
- buffer << '?' << encode(@parameters) if parameters?
79
+ buffer << URL.escape_path(@path)
80
+ buffer << '?' << URL.encode(@parameters) if parameters?
79
81
  end
80
82
 
81
83
  if fragment?
82
- buffer << '#' << escape(@fragment)
84
+ buffer << '#' << URL.escape(@fragment)
83
85
  end
84
86
 
85
87
  return buffer
@@ -144,45 +146,6 @@ module HTTP
144
146
  return path.join('/')
145
147
  end
146
148
  end
147
-
148
- # Escapes a generic string, using percent encoding.
149
- def escape(string)
150
- encoding = string.encoding
151
- string.b.gsub(/([^a-zA-Z0-9_.\-]+)/) do |m|
152
- '%' + m.unpack('H2' * m.bytesize).join('%').upcase
153
- end.force_encoding(encoding)
154
- end
155
-
156
- # According to https://tools.ietf.org/html/rfc3986#section-3.3, we escape non-pchar.
157
- NON_PCHAR = /([^a-zA-Z0-9_\-\.~!$&'()*+,;=:@\/]+)/.freeze
158
-
159
- # Escapes a path
160
- def escape_path(path)
161
- encoding = path.encoding
162
- path.b.gsub(NON_PCHAR) do |m|
163
- '%' + m.unpack('H2' * m.bytesize).join('%').upcase
164
- end.force_encoding(encoding)
165
- end
166
-
167
- # Encodes a hash or array into a query string
168
- def encode(value, prefix = nil)
169
- case value
170
- when Array
171
- return value.map {|v|
172
- encode(v, "#{prefix}[]")
173
- }.join("&")
174
- when Hash
175
- return value.map {|k, v|
176
- encode(v, prefix ? "#{prefix}[#{escape(k.to_s)}]" : escape(k.to_s))
177
- }.reject(&:empty?).join('&')
178
- when nil
179
- return prefix
180
- else
181
- raise ArgumentError, "value must be a Hash" if prefix.nil?
182
-
183
- return "#{prefix}=#{escape(value.to_s)}"
184
- end
185
- end
186
149
  end
187
150
  end
188
151
  end
@@ -0,0 +1,118 @@
1
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module HTTP
22
+ module Protocol
23
+ module URL
24
+ # Escapes a generic string, using percent encoding.
25
+ def self.escape(string, encoding = string.encoding)
26
+ string.b.gsub(/([^a-zA-Z0-9_.\-]+)/) do |m|
27
+ '%' + m.unpack('H2' * m.bytesize).join('%').upcase
28
+ end.force_encoding(encoding)
29
+ end
30
+
31
+ def self.unescape(string, encoding = string.encoding)
32
+ string.b.gsub(/%(\h\h)/) do |hex|
33
+ Integer(hex, 16).chr
34
+ end.force_encoding(encoding)
35
+ end
36
+
37
+ # According to https://tools.ietf.org/html/rfc3986#section-3.3, we escape non-pchar.
38
+ NON_PCHAR = /([^a-zA-Z0-9_\-\.~!$&'()*+,;=:@\/]+)/.freeze
39
+
40
+ # Escapes a path
41
+ def self.escape_path(path)
42
+ encoding = path.encoding
43
+ path.b.gsub(NON_PCHAR) do |m|
44
+ '%' + m.unpack('H2' * m.bytesize).join('%').upcase
45
+ end.force_encoding(encoding)
46
+ end
47
+
48
+ # Encodes a hash or array into a query string
49
+ def self.encode(value, prefix = nil)
50
+ case value
51
+ when Array
52
+ return value.map {|v|
53
+ self.encode(v, "#{prefix}[]")
54
+ }.join("&")
55
+ when Hash
56
+ return value.map {|k, v|
57
+ self.encode(v, prefix ? "#{prefix}[#{escape(k.to_s)}]" : escape(k.to_s))
58
+ }.reject(&:empty?).join('&')
59
+ when nil
60
+ return prefix
61
+ else
62
+ raise ArgumentError, "value must be a Hash" if prefix.nil?
63
+
64
+ return "#{prefix}=#{escape(value.to_s)}"
65
+ end
66
+ end
67
+
68
+ def self.scan(string)
69
+ # TODO Ruby 2.6 doesn't require `.each`
70
+ string.split('&').each do |assignment|
71
+ key, value = assignment.split('=', 2)
72
+
73
+ yield unescape(key), unescape(value)
74
+ end
75
+ end
76
+
77
+ def self.split(name)
78
+ name.scan(/([^\[]+)|(?:\[(.*?)\])/).flatten!.compact!
79
+ end
80
+
81
+ def self.assign(name, value, parent)
82
+ top, *middle = self.split(name)
83
+
84
+ yield(top, middle) if block_given?
85
+
86
+ middle.each_with_index do |key, index|
87
+ if key.empty?
88
+ parent = (parent[top] ||= Array.new)
89
+ top = parent.size
90
+
91
+ if nested = middle[index+1] and last = parent.last
92
+ top -= 1 unless last.include?(nested)
93
+ end
94
+ else
95
+ parent = (parent[top] ||= Hash.new)
96
+ top = key
97
+ end
98
+ end
99
+
100
+ parent[top] = value
101
+ end
102
+
103
+ def self.decode(string, maximum = 8)
104
+ parameters = {}
105
+
106
+ self.scan(string) do |key, value|
107
+ self.assign(key, value, parameters) do |top, middle|
108
+ if maximum and middle.count > maximum
109
+ raise ArgumentError, "Key length exceeded limit!"
110
+ end
111
+ end
112
+ end
113
+
114
+ return parameters
115
+ end
116
+ end
117
+ end
118
+ end
@@ -20,6 +20,6 @@
20
20
 
21
21
  module HTTP
22
22
  module Protocol
23
- VERSION = "0.12.2"
23
+ VERSION = "0.13.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-protocol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.2
4
+ version: 0.13.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-02-02 00:00:00.000000000 Z
11
+ date: 2019-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http-hpack
@@ -123,6 +123,7 @@ files:
123
123
  - lib/http/protocol/http2/window_update_frame.rb
124
124
  - lib/http/protocol/methods.rb
125
125
  - lib/http/protocol/reference.rb
126
+ - lib/http/protocol/url.rb
126
127
  - lib/http/protocol/version.rb
127
128
  homepage: https://github.com/socketry/http-protocol
128
129
  licenses: []
@@ -142,7 +143,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
143
  - !ruby/object:Gem::Version
143
144
  version: '0'
144
145
  requirements: []
145
- rubygems_version: 3.0.2
146
+ rubyforge_project:
147
+ rubygems_version: 2.7.8
146
148
  signing_key:
147
149
  specification_version: 4
148
150
  summary: Provides abstractions to handle HTTP1 and HTTP2 protocols.