From 365dd369e5bcd8ffc4c4fc025377c1744de15785 Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Sun, 1 Oct 2023 17:11:51 -0700 Subject: [PATCH] Fix problem parsing ill-formed URI query parameters such as "?&a". --- lib/rdf/model/uri.rb | 21 +++++++++++---------- spec/model_uri_spec.rb | 1 + 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/rdf/model/uri.rb b/lib/rdf/model/uri.rb index e6deb7aa..f56391eb 100644 --- a/lib/rdf/model/uri.rb +++ b/lib/rdf/model/uri.rb @@ -1240,17 +1240,18 @@ def query_values(return_type=Hash) query.to_s.split('&'). inject(return_type == Hash ? {} : []) do |memo,kv| k,v = kv.to_s.split('=', 2) - next if k.to_s.empty? - k = CGI.unescape(k) - v = CGI.unescape(v) if v - if return_type == Hash - case memo[k] - when nil then memo[k] = v - when Array then memo[k] << v - else memo[k] = [memo[k], v] + unless k.to_s.empty? + k = CGI.unescape(k) + v = CGI.unescape(v) if v + if return_type == Hash + case memo[k] + when nil then memo[k] = v + when Array then memo[k] << v + else memo[k] = [memo[k], v] + end + else + memo << [k, v].compact end - else - memo << [k, v].compact end memo end diff --git a/spec/model_uri_spec.rb b/spec/model_uri_spec.rb index 8d00d7ba..4813d3b7 100644 --- a/spec/model_uri_spec.rb +++ b/spec/model_uri_spec.rb @@ -749,6 +749,7 @@ "?one.two.three=four" => {"one.two.three" => "four"}, "?one[two][three]=four&one[two][five]=six" => {"one[two][three]" => "four", "one[two][five]" => "six"}, "?one=two&one=three&one=four" => {'one' => ['two', 'three', 'four']}, + "?&a" => {'a' => nil}, }.each do |uri, result| it uri do if result