summary refs log tree commit
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-01-24 01:49:43 +0800
committerJoshua Peek <josh@joshpeek.com>2011-01-24 02:05:24 +0800
commit1c2715778d3753dce4d8df19e3d3cc698aed20c6 (patch)
treef8c6a75e292bad0dfefe5f0a3410e0ae7a4ecffa
parent812ac75b327c9f3d6ff5074def9721bf6f19e1de (diff)
downloadrack-1c2715778d3753dce4d8df19e3d3cc698aed20c6.tar.gz
If there is an X-Forwarded-Host header, we should take the absence of a port segment to imply 'standard port', not 'use the SERVER_PORT'
-rw-r--r--lib/rack/request.rb2
-rw-r--r--test/spec_request.rb12
2 files changed, 14 insertions, 0 deletions
diff --git a/lib/rack/request.rb b/lib/rack/request.rb
index 816640ed..5ebd72ea 100644
--- a/lib/rack/request.rb
+++ b/lib/rack/request.rb
@@ -98,6 +98,8 @@ module Rack
         port.to_i
       elsif ssl?
         443
+      elsif @env.has_key?("HTTP_X_FORWARDED_HOST")
+        80
       else
         @env["SERVER_PORT"].to_i
       end
diff --git a/test/spec_request.rb b/test/spec_request.rb
index 6765edca..ddaa172b 100644
--- a/test/spec_request.rb
+++ b/test/spec_request.rb
@@ -84,6 +84,14 @@ describe Rack::Request do
     req = Rack::Request.new \
       Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org", "HTTP_X_FORWARDED_PORT" => "9393")
     req.port.should.equal 9393
+
+    req = Rack::Request.new \
+      Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org:9393", "SERVER_PORT" => "80")
+    req.port.should.equal 9393
+
+    req = Rack::Request.new \
+      Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org", "SERVER_PORT" => "9393")
+    req.port.should.equal 80
   end
 
   should "figure out the correct host with port" do
@@ -102,6 +110,10 @@ describe Rack::Request do
     req = Rack::Request.new \
       Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org:9292")
     req.host_with_port.should.equal "example.org:9292"
+
+    req = Rack::Request.new \
+      Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org", "SERVER_PORT" => "9393")
+    req.host_with_port.should.equal "example.org"
   end
 
   should "parse the query string" do