Skip to content

Commit

Permalink
Test added to check to ensure that quoted values are properly parsed
Browse files Browse the repository at this point in the history
Using a regular expression to identify quoted string values, could be optimized
  • Loading branch information
scytrin committed Dec 3, 2009
1 parent 35b562e commit fb4f2b5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/rack/utils.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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 = $' || ''
Expand Down
7 changes: 6 additions & 1 deletion test/spec_rack_utils.rb
Expand Up @@ -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").
Expand All @@ -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"
Expand Down

5 comments on commit fb4f2b5

@raggi
Copy link
Member

@raggi raggi commented on fb4f2b5 Dec 19, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

@jeremy jeremy commented on fb4f2b5 Dec 20, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

@jeremy jeremy commented on fb4f2b5 Dec 20, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(btw raggi, it was reverted in dae12e0)

@raggi
Copy link
Member

@raggi raggi commented on fb4f2b5 Dec 20, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.