diff options
author | James Tucker <jftucker@gmail.com> | 2014-07-18 15:28:20 -0700 |
---|---|---|
committer | James Tucker <jftucker@gmail.com> | 2014-07-18 15:29:07 -0700 |
commit | 2754224570978756fdbaf3f1842a229d3ab95913 (patch) | |
tree | 7f7d6d254df792a797c3bffb189f8a6aac7ed555 | |
parent | d1f32cdf4a798f7ebeaacda4013736dba20b60e1 (diff) | |
download | rack-2754224570978756fdbaf3f1842a229d3ab95913.tar.gz |
UrlMap: Enable case-insensitive domain matching
Closes #668. Supersedes #668, because it dropped port matching.
-rw-r--r-- | lib/rack/urlmap.rb | 20 | ||||
-rw-r--r-- | test/spec_urlmap.rb | 23 |
2 files changed, 40 insertions, 3 deletions
diff --git a/lib/rack/urlmap.rb b/lib/rack/urlmap.rb index d301ce9b..df9e7d6d 100644 --- a/lib/rack/urlmap.rb +++ b/lib/rack/urlmap.rb @@ -48,9 +48,10 @@ module Rack sPort = env['SERVER_PORT'] @mapping.each do |host, location, match, app| - unless hHost == host \ - || sName == host \ - || (!host && (hHost == sName || hHost == sName+':'+sPort)) + unless casecmp?(hHost, host) \ + || casecmp?(sName, host) \ + || (!host && (casecmp?(hHost, sName) || + casecmp?(hHost, sName+':'+sPort))) next end @@ -71,6 +72,19 @@ module Rack env['PATH_INFO'] = path env['SCRIPT_NAME'] = script_name end + + private + def casecmp?(v1, v2) + # if both nil, or they're the same string + return true if v1 == v2 + + # if either are nil... (but they're not the same) + return false if v1.nil? + return false if v2.nil? + + # otherwise check they're not case-insensitive the same + v1.casecmp(v2).zero? + end end end diff --git a/test/spec_urlmap.rb b/test/spec_urlmap.rb index 316c7254..2ef41cdc 100644 --- a/test/spec_urlmap.rb +++ b/test/spec_urlmap.rb @@ -210,4 +210,27 @@ describe Rack::URLMap do res["X-PathInfo"].should.equal "/http://example.org/bar" res["X-ScriptName"].should.equal "" end + + should "not be case sensitive with hosts" do + map = Rack::Lint.new(Rack::URLMap.new("http://example.org/" => lambda { |env| + [200, + { "Content-Type" => "text/plain", + "X-Position" => "root", + "X-PathInfo" => env["PATH_INFO"], + "X-ScriptName" => env["SCRIPT_NAME"] + }, [""]]} + )) + + res = Rack::MockRequest.new(map).get("http://example.org/") + res.should.be.ok + res["X-Position"].should.equal "root" + res["X-PathInfo"].should.equal "/" + res["X-ScriptName"].should.equal "" + + res = Rack::MockRequest.new(map).get("http://EXAMPLE.ORG/") + res.should.be.ok + res["X-Position"].should.equal "root" + res["X-PathInfo"].should.equal "/" + res["X-ScriptName"].should.equal "" + end end |