Skip to content

Commit 365dd36

Browse files
committed
Fix problem parsing ill-formed URI query parameters such as "?&a".
1 parent 8864eac commit 365dd36

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

lib/rdf/model/uri.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,17 +1240,18 @@ def query_values(return_type=Hash)
12401240
query.to_s.split('&').
12411241
inject(return_type == Hash ? {} : []) do |memo,kv|
12421242
k,v = kv.to_s.split('=', 2)
1243-
next if k.to_s.empty?
1244-
k = CGI.unescape(k)
1245-
v = CGI.unescape(v) if v
1246-
if return_type == Hash
1247-
case memo[k]
1248-
when nil then memo[k] = v
1249-
when Array then memo[k] << v
1250-
else memo[k] = [memo[k], v]
1243+
unless k.to_s.empty?
1244+
k = CGI.unescape(k)
1245+
v = CGI.unescape(v) if v
1246+
if return_type == Hash
1247+
case memo[k]
1248+
when nil then memo[k] = v
1249+
when Array then memo[k] << v
1250+
else memo[k] = [memo[k], v]
1251+
end
1252+
else
1253+
memo << [k, v].compact
12511254
end
1252-
else
1253-
memo << [k, v].compact
12541255
end
12551256
memo
12561257
end

spec/model_uri_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@
749749
"?one.two.three=four" => {"one.two.three" => "four"},
750750
"?one[two][three]=four&one[two][five]=six" => {"one[two][three]" => "four", "one[two][five]" => "six"},
751751
"?one=two&one=three&one=four" => {'one' => ['two', 'three', 'four']},
752+
"?&a" => {'a' => nil},
752753
}.each do |uri, result|
753754
it uri do
754755
if result

0 commit comments

Comments
 (0)