protocol-url 0.6.0 → 0.8.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: 10d18a95d6f20f5ca2457a52a2defa8d961ecf26eb372d45bea1f2d339f3a5c6
4
- data.tar.gz: f15a84b24bd971e6262306b84e271196b5c20cde76983eba4625af28b0895ddd
3
+ metadata.gz: 0e251b8fa72bc3017a56cfa385bf2bc7b9e1e4ef8add2bcd12fb8321cc349c75
4
+ data.tar.gz: 700835c1db54aead624eb89003c7daa42d9ad992f7139c336e2a5293291db619
5
5
  SHA512:
6
- metadata.gz: e1493486247a9e36b696bf62225a33888905d7e4a5a83b31357dc67d227b3a6f481844e2423befc5cc21f4c0a14aebc4ea507faa68a06a50490619e455396950
7
- data.tar.gz: ee45f80e209b66b2a30d6b155188138a513e9a2c632bf1a4edc0322823e9bef209d2515babc955030c43c26c9da9ea687303a2aa91fa9704a2ac0594405d8cda
6
+ metadata.gz: 6285b5b91ba997d2f822fcc4ffd49b96dffcc1336483bd043f42a736dfb3bbe4e145e492044a201de1c8f0f3e363ae816d64af79e0412b2ce4715ec69e3b4d9c
7
+ data.tar.gz: 6e4c0817905afb9b17e8e0eceae42e7ea24d6c0af6a57773d7395355b2e7b597d043c9d7795ddf62194790f0a3f53f6346d7950598721c4b079e7a419e95f793
checksums.yaml.gz.sig CHANGED
@@ -1 +1 @@
1
- q;+�ߎ� ���:c�ٜj�g��¾tC[7�ѣ����UpR�����NK�����j�� e&�.ň��:p͠y�={�{��e͕V Y��+a c��Z6�T�&�*l NX���^Ӏq~� ��/���<<X�H�`\����7 h=]�����s����eSfaKߣ�κ�U�.+�m��st�o���[Dg�.�����Ft�v�� &I(Kvm��{9�h��հ~ 15��7��e�̓���f}\:칙�аK]�q�X��o̍S� q3wUz�:?*#wlKjC
1
+ K"7z<\�m.zQѕ8cEw��F)?f}��؋> x��h;I�� a�����V�5n>�\Cfq��'A)������ܕ��J��$h�W�?�X���&X�T� Nc��9���OC�,�@���y�Q�n�'�QR�H�iC���I�ͬ��U����FfI�Fk��5��V\�ofO �0���F�)&_�uKr�d��� щ�?wt�7�������<,���c�Ҭ�ܷ��|�\��?ҍ���c� �b@Qk�o��,�$�����)\�r�O�_�s���ĭ84���coQyA�r31iR&8�����sgakp��_'� +��R���꬚�.X�\пD�@�pc&�0#��Clm?��+��ܜ�va��CD
@@ -10,13 +10,13 @@ module Protocol
10
10
  module FormData
11
11
  # Builds nested form data from names and values.
12
12
  class Nested
13
- # The default maximum depth of a bracketed form name.
14
- MAXIMUM_DEPTH = 8
13
+ # The bracketed form name depth limit.
14
+ DEPTH_LIMIT = 8
15
15
 
16
16
  # Initialize the nested form data.
17
- # @parameter maximum_depth [Integer | Nil] The maximum depth of a bracketed form name.
18
- def initialize(maximum_depth: MAXIMUM_DEPTH)
19
- @maximum_depth = maximum_depth
17
+ # @parameter depth_limit [Integer | Nil] The bracketed form name depth limit.
18
+ def initialize(depth_limit: DEPTH_LIMIT)
19
+ @depth_limit = depth_limit
20
20
  @root = {}
21
21
  end
22
22
 
@@ -31,8 +31,8 @@ module Protocol
31
31
  raise ArgumentError, "Invalid form data name: #{name.inspect}!"
32
32
  end
33
33
 
34
- if @maximum_depth and keys.size > @maximum_depth
35
- raise RangeError, "Form data depth exceeded limit of #{@maximum_depth}!"
34
+ if @depth_limit and keys.size > @depth_limit
35
+ raise RangeError, "Form data depth exceeded limit of #{@depth_limit}!"
36
36
  end
37
37
 
38
38
  Encoding.assign(keys, value, @root)
@@ -13,20 +13,20 @@ module Protocol
13
13
  class Parser
14
14
  CONTENT_TYPE = "application/x-www-form-urlencoded"
15
15
 
16
- # The default maximum encoded body size.
17
- MAXIMUM_TOTAL_SIZE = 2 * 1024 * 1024
16
+ # The encoded body size limit.
17
+ SIZE_LIMIT = 2 * 1024 * 1024
18
18
 
19
- # The default maximum number of form pairs.
20
- MAXIMUM_PAIR_COUNT = 1024
19
+ # The form pair count limit.
20
+ PAIR_COUNT_LIMIT = 1024
21
21
 
22
22
  # Initialize the form data parser.
23
- # @parameter maximum_total_size [Integer | Nil] The maximum encoded body size.
24
- # @parameter maximum_pair_count [Integer | Nil] The maximum number of form pairs.
25
- # @parameter maximum_depth [Integer | Nil] The maximum depth of a bracketed form name.
26
- def initialize(maximum_total_size: MAXIMUM_TOTAL_SIZE, maximum_pair_count: MAXIMUM_PAIR_COUNT, maximum_depth: Nested::MAXIMUM_DEPTH)
27
- @maximum_total_size = maximum_total_size
28
- @maximum_pair_count = maximum_pair_count
29
- @maximum_depth = maximum_depth
23
+ # @parameter size_limit [Integer | Nil] The encoded body size limit.
24
+ # @parameter pair_count_limit [Integer | Nil] The form pair count limit.
25
+ # @parameter depth_limit [Integer | Nil] The bracketed form name depth limit.
26
+ def initialize(size_limit: SIZE_LIMIT, pair_count_limit: PAIR_COUNT_LIMIT, depth_limit: Nested::DEPTH_LIMIT)
27
+ @size_limit = size_limit
28
+ @pair_count_limit = pair_count_limit
29
+ @depth_limit = depth_limit
30
30
  end
31
31
 
32
32
  # Parse URL-encoded form data into a nested hash.
@@ -34,17 +34,16 @@ module Protocol
34
34
  # When a block is given, each decoded value is passed through the block before assignment. The value returned by the block is assigned to the result.
35
35
  #
36
36
  # @parameter body [Object] A readable body which yields chunks from `#read`.
37
+ # @parameter result [Object] The result to populate. It must support `#add` and `#to_h`.
37
38
  # @yields {|name, value| ...} Each decoded form pair before assignment.
38
39
  # @returns [Hash] The nested form data.
39
- def parse(body)
40
- nested = Nested.new(maximum_depth: @maximum_depth)
41
-
40
+ def parse(body, result = make_result)
42
41
  each(body) do |name, value|
43
42
  value = yield(name, value) if block_given?
44
- nested.add(name, value)
43
+ result.add(name, value)
45
44
  end
46
45
 
47
- return nested.to_h
46
+ return result.to_h
48
47
  end
49
48
 
50
49
  # Incrementally enumerate URL-encoded form data as ordered name/value pairs.
@@ -55,14 +54,14 @@ module Protocol
55
54
  return to_enum(__method__, body) unless block_given?
56
55
 
57
56
  buffer = String.new.b
58
- total_size = 0
57
+ size = 0
59
58
  pair_count = 0
60
59
 
61
60
  while chunk = body.read
62
61
  break if chunk.empty?
63
62
 
64
- total_size += chunk.bytesize
65
- check_limit(:total_size, total_size, @maximum_total_size)
63
+ size += chunk.bytesize
64
+ check_limit(:size, size, @size_limit)
66
65
  buffer << chunk
67
66
 
68
67
  while separator = buffer.index("&")
@@ -71,7 +70,7 @@ module Protocol
71
70
 
72
71
  unless assignment.empty?
73
72
  pair_count += 1
74
- check_limit(:pair_count, pair_count, @maximum_pair_count)
73
+ check_limit(:pair_count, pair_count, @pair_count_limit)
75
74
  yield_pair(assignment) {|name, value| yield name, value}
76
75
  end
77
76
  end
@@ -79,7 +78,7 @@ module Protocol
79
78
 
80
79
  unless buffer.empty?
81
80
  pair_count += 1
82
- check_limit(:pair_count, pair_count, @maximum_pair_count)
81
+ check_limit(:pair_count, pair_count, @pair_count_limit)
83
82
  yield_pair(buffer) {|name, value| yield name, value}
84
83
  end
85
84
 
@@ -88,6 +87,10 @@ module Protocol
88
87
 
89
88
  private
90
89
 
90
+ def make_result
91
+ return Nested.new(depth_limit: @depth_limit)
92
+ end
93
+
91
94
  def yield_pair(assignment)
92
95
  name, value = assignment.split("=", 2)
93
96
 
@@ -102,9 +105,9 @@ module Protocol
102
105
  return Encoding.unescape(component.tr("+", " "))
103
106
  end
104
107
 
105
- def check_limit(name, value, maximum)
106
- if maximum and value > maximum
107
- raise RangeError, "Form data #{name} exceeded limit of #{maximum}!"
108
+ def check_limit(name, value, limit)
109
+ if limit and value > limit
110
+ raise RangeError, "Form data #{name} exceeded limit of #{limit}!"
108
111
  end
109
112
  end
110
113
  end
@@ -7,6 +7,6 @@
7
7
  module Protocol
8
8
  # @namespace
9
9
  module URL
10
- VERSION = "0.6.0"
10
+ VERSION = "0.8.0"
11
11
  end
12
12
  end
data/readme.md CHANGED
@@ -34,6 +34,14 @@ This project is best served by a collaborative and respectful environment. Treat
34
34
 
35
35
  Please see the [project releases](https://socketry.github.io/protocol-url/releases/index) for all releases.
36
36
 
37
+ ### v0.8.0
38
+
39
+ - Use consistent limit naming for form data parser constraints.
40
+
41
+ ### v0.7.0
42
+
43
+ - Allow `Protocol::URL::FormData::Parser#parse` to populate a supplied result object.
44
+
37
45
  ### v0.6.0
38
46
 
39
47
  - Add `Protocol::URL::FormData::Parser` for incremental, limited parsing of `application/x-www-form-urlencoded` form data.
data/releases.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Releases
2
2
 
3
+ ## v0.8.0
4
+
5
+ - Use consistent limit naming for form data parser constraints.
6
+
7
+ ## v0.7.0
8
+
9
+ - Allow `Protocol::URL::FormData::Parser#parse` to populate a supplied result object.
10
+
3
11
  ## v0.6.0
4
12
 
5
13
  - Add `Protocol::URL::FormData::Parser` for incremental, limited parsing of `application/x-www-form-urlencoded` form data.
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-url
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
metadata.gz.sig CHANGED
Binary file