Permalink
Browse files

Test added to check to ensure that quoted values are properly parsed

Using a regular expression to identify quoted string values, could be optimized
  • Loading branch information...
scytrin committed Dec 3, 2009
1 parent 35b562e commit fb4f2b5fe26a0e3821ac0f6361a3885bd88b42ca
Showing with 12 additions and 2 deletions.
  1. +6 −1 lib/rack/utils.rb
  2. +6 −1 test/spec_rack_utils.rb
@@ -38,7 +38,9 @@ def parse_query(qs, d = nil)

(qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p|
k, v = p.split('=', 2).map { |x| unescape(x) }

if v =~ /^("|')(.*)\1$/
v = $2.gsub('\\'+$1, $1)
end
if cur = params[k]
if cur.class == Array
params[k] << v
@@ -67,6 +69,9 @@ def parse_nested_query(qs, d = nil)
module_function :parse_nested_query

def normalize_params(params, name, v = nil)
if v and v =~ /^("|')(.*)\1$/
v = $2.gsub('\\'+$1, $1)
end
name =~ %r(\A[\[\]]*([^\[\]]+)\]*)
k = $1 || ''
after = $' || ''
@@ -30,7 +30,10 @@
end

specify "should parse query strings correctly" do
Rack::Utils.parse_query("foo=bar").should.equal "foo" => "bar"
Rack::Utils.parse_query("foo=bar").
should.equal "foo" => "bar"
Rack::Utils.parse_query("foo=\"bar\"").
should.equal "foo" => "bar"
Rack::Utils.parse_query("foo=bar&foo=quux").
should.equal "foo" => ["bar", "quux"]
Rack::Utils.parse_query("foo=1&bar=2").
@@ -47,6 +50,8 @@
should.equal "foo" => ""
Rack::Utils.parse_nested_query("foo=bar").
should.equal "foo" => "bar"
Rack::Utils.parse_nested_query("foo=\"bar\"").
should.equal "foo" => "bar"

Rack::Utils.parse_nested_query("foo=bar&foo=quux").
should.equal "foo" => "quux"

5 comments on commit fb4f2b5

@raggi

This comment has been minimized.

Copy link
Member

raggi replied Dec 19, 2010

Why was this added, it appears to be a problem for some frameworks, I'm looking for some kind of standards reference, but can't find one.

@scytrin

This comment has been minimized.

Copy link
Member Author

scytrin replied Dec 20, 2010

RFC2109 refers to the value of a attribute pair being either a token or a quoted string.
http://groups.google.com/group/rack-devel/browse_thread/thread/8cc9d8384e136ac4

@jeremy

This comment has been minimized.

Copy link
Member

jeremy replied Dec 20, 2010

RFC 2109 applies to cookies, but this strips quotes from all query params. In a URI, the query part is (nearly) free-form: http://tools.ietf.org/html/rfc3986#section-3.4

@jeremy

This comment has been minimized.

Copy link
Member

jeremy replied Dec 20, 2010

(btw raggi, it was reverted in dae12e0)

@raggi

This comment has been minimized.

Copy link
Member

raggi replied Dec 20, 2010

yup, and i cherry picked into the 1.1 branch in prep for 1.1.1

Please sign in to comment.