Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Matching spaces with regex routes are broken in 1.3.1. This is a fix. #390

Closed
wants to merge 2 commits into from

2 participants

@niko

Presumably when introducing the ability to pass url encoded parameters within a URL the ability to match URLs with a regex containing spaces got lost. This must have been between 1.2.6 and 1.3.1 as this used to work in 1.2.6.

This patch makes regex routes with spaces working again. Also, umlauts (and perhaps other stuff that got url encoded in routing) now works again for regex routes. I ran the test suite with 1.9.2 and 1.8.7.

The price to pay is what I called %2F shuffle in the route matching. Considering that the slash is our route separator I think it deserves the extra treatment.

Still not working are regex routes with slashes in the regex (like get %r{\A/([\w\/]+)} do ...). I added a test case for this but it is commented out. I'm not sure at all how this should (or even could) be handled.

The alternative to this or a similar patch would be (IMHO) that regex routes loose their first class citizenship in sinatra. As a user I'd expect to be

get '/foo/:bar' do
  pass unless params[:bar] =~ /[\w ]+/
...

somewhat equivalent to

get %r{/foo/([\w ]+)' do
...
@rkh
Owner

I am not sure about this. I actually like that regular expressions run on the input we get, allows you to differentiate cases you can't with the pattern.

@rkh
Owner

Also, you're the only one to complain so far.

@rkh rkh closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 18, 2011
  1. @niko

    fix spaces in regex routes

    niko authored
Commits on Nov 9, 2011
  1. @niko
This page is out of date. Refresh to see the latest.
Showing with 44 additions and 10 deletions.
  1. +7 −10 lib/sinatra/base.rb
  2. +37 −0 test/routing_test.rb
View
17 lib/sinatra/base.rb
@@ -798,10 +798,14 @@ def route_eval
# Returns pass block.
def process_route(pattern, keys, conditions, block = nil, values = [])
@original_params ||= @params
- route = @request.path_info
+
+ # The %2F shuffle seems ugly. But '/' just happens to be our parameter separator.
+ # It deserves the extra treatment.
+ route = Rack::Utils.unescape @request.path_info.gsub('%2F', '%252F')
+
route = '/' if route.empty? and not settings.empty_path_info?
if match = pattern.match(route)
- values += match.captures.to_a.map { |v| force_encoding URI.decode(v) if v }
+ values += match.captures.to_a.map { |v| force_encoding v.gsub('%2F','/') if v }
params =
if keys.any?
keys.zip(values).inject({}) do |hash,(k,v)|
@@ -1218,7 +1222,7 @@ def compile!(verb, path, block, options = {})
def compile(path)
keys = []
if path.respond_to? :to_str
- pattern = path.to_str.gsub(/[^\?\%\\\/\:\*\w]/) { |c| encoded(c) }
+ pattern = path.to_str.gsub(/[^\?\%\\\/\:\*\w\ ]/) { |c| "(?:#{Regexp.escape c})" }
pattern.gsub!(/((:\w+)|\*)/) do |match|
if match == "*"
keys << 'splat'
@@ -1240,13 +1244,6 @@ def compile(path)
end
end
- def encoded(char)
- enc = URI.encode(char)
- enc = "(?:#{Regexp.escape enc}|#{URI.encode char, /./})" if enc == char
- enc = "(?:#{enc}|#{encoded('+')})" if char == " "
- enc
- end
-
public
# Makes the methods defined in the block and in the Modules given
# in `extensions` available to the handlers and templates
View
37 test/routing_test.rb
@@ -459,6 +459,43 @@ class RoutingTest < Test::Unit::TestCase
assert_equal 'Hello World', body
end
+ it "URL works with spaces within regexps" do
+ mock_app {
+ get %r{\A/regexp_space/([\w ]+)} do
+ assert_equal ['how are you'], params['captures']
+ nil
+ end
+ }
+
+ get '/regexp_space/how%20are%20you'
+ assert ok?
+ end
+
+ # pending:
+ # it "URL works with slashes within regexps" do
+ # mock_app {
+ # get %r{\A/regexp_slash/([\w\/]+)} do
+ # assert_equal ['how/are/you'], params['captures']
+ # nil
+ # end
+ # }
+ #
+ # get '/regexp_slash/how%2Fare%2Fyou'
+ # assert ok?
+ # end
+
+ it "URL works with umlauts within regexps" do
+ mock_app {
+ get %r{\A/regexp_umlaut/([äöü])}u do # the 'u' is only needed for ruby 1.8.7
+ assert_equal ['ö'], params['captures']
+ nil
+ end
+ }
+
+ get '/regexp_umlaut/%C3%B6'
+ assert ok?
+ end
+
it 'makes regular expression captures available in params[:captures]' do
mock_app {
get(/^\/fo(.*)\/ba(.*)/) do
Something went wrong with that request. Please try again.