Permalink
Browse files

Merge remote branch 'origin/nosqueeze'

  • Loading branch information...
2 parents 99acee3 + 065ba85 commit 8e6bc663d1a72f717dc913faf72cc84097da8f8a @josh josh committed Dec 26, 2009
Showing with 38 additions and 7 deletions.
  1. +8 −7 lib/rack/urlmap.rb
  2. +30 −0 test/spec_rack_urlmap.rb
View
@@ -28,25 +28,26 @@ def remap(map)
raise ArgumentError, "paths need to start with /"
end
location = location.chomp('/')
+ match = Regexp.new("^#{Regexp.quote(location).gsub('/', '/+')}(.*)", nil, 'n')
- [host, location, app]
- }.sort_by { |(h, l, a)| [h ? -h.size : (-1.0 / 0.0), -l.size] } # Longest path first
+ [host, location, match, app]
+ }.sort_by { |(h, l, m, a)| [h ? -h.size : (-1.0 / 0.0), -l.size] } # Longest path first
end
def call(env)
- path = env["PATH_INFO"].to_s.squeeze("/")
+ path = env["PATH_INFO"].to_s
script_name = env['SCRIPT_NAME']
hHost, sName, sPort = env.values_at('HTTP_HOST','SERVER_NAME','SERVER_PORT')
- @mapping.each { |host, location, app|
+ @mapping.each { |host, location, match, app|
next unless (hHost == host || sName == host \
|| (host.nil? && (hHost == sName || hHost == sName+':'+sPort)))
- next unless location == path[0, location.size]
- next unless path[location.size] == nil || path[location.size] == ?/
+ next unless path =~ match && rest = $1
+ next unless rest.empty? || rest[0] == ?/
return app.call(
env.merge(
'SCRIPT_NAME' => (script_name + location),
- 'PATH_INFO' => path[location.size..-1]))
+ 'PATH_INFO' => rest))
}
[404, {"Content-Type" => "text/plain", "X-Cascade" => "pass"}, ["Not Found: #{path}"]]
end
View
@@ -44,6 +44,12 @@
res["X-ScriptName"].should.equal "/foo/bar"
res["X-PathInfo"].should.equal "/"
+ res = Rack::MockRequest.new(map).get("/foo///bar//quux")
+ res.status.should.equal 200
+ res.should.be.ok
+ res["X-ScriptName"].should.equal "/foo/bar"
+ res["X-PathInfo"].should.equal "//quux"
+
res = Rack::MockRequest.new(map).get("/foo/quux", "SCRIPT_NAME" => "/bleh")
res.should.be.ok
res["X-ScriptName"].should.equal "/bleh/foo"
@@ -182,4 +188,28 @@
res["X-PathInfo"].should.equal "/"
res["X-ScriptName"].should.equal ""
end
+
+ specify "should not squeeze slashes" do
+ map = Rack::URLMap.new("/" => lambda { |env|
+ [200,
+ { "Content-Type" => "text/plain",
+ "X-Position" => "root",
+ "X-PathInfo" => env["PATH_INFO"],
+ "X-ScriptName" => env["SCRIPT_NAME"]
+ }, [""]]},
+ "/foo" => lambda { |env|
+ [200,
+ { "Content-Type" => "text/plain",
+ "X-Position" => "foo",
+ "X-PathInfo" => env["PATH_INFO"],
+ "X-ScriptName" => env["SCRIPT_NAME"]
+ }, [""]]}
+ )
+
+ res = Rack::MockRequest.new(map).get("/http://example.org/bar")
+ res.should.be.ok
+ res["X-Position"].should.equal "root"
+ res["X-PathInfo"].should.equal "/http://example.org/bar"
+ res["X-ScriptName"].should.equal ""
+ end
end

0 comments on commit 8e6bc66

Please sign in to comment.