Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rack::Request support for HTTP_X_FORWARDED_{PORT,SSL}

  • Loading branch information...
commit 35bb5ba6746b5d346de9202c004cc926039650c7 1 parent bff192e
@stephencelis stephencelis authored josh committed
Showing with 51 additions and 5 deletions.
  1. +12 −4 lib/rack/request.rb
  2. +39 −1 test/spec_request.rb
View
16 lib/rack/request.rb
@@ -65,6 +65,8 @@ def content_charset
def scheme
if @env['HTTPS'] == 'on'
'https'
+ elsif @env['HTTP_X_FORWARDED_SSL'] == 'on'
+ 'https'
elsif @env['HTTP_X_FORWARDED_PROTO']
@env['HTTP_X_FORWARDED_PROTO'].split(',')[0]
else
@@ -83,11 +85,17 @@ def host_with_port
@env['HTTP_HOST'] || "#{@env['SERVER_NAME'] || @env['SERVER_ADDR']}:#{@env['SERVER_PORT']}"
end
end
-
+
def port
- host, port = host_with_port.split(/:/)
-
- (port || @env["SERVER_PORT"]).to_i
+ if port = host_with_port.split(/:/)[1]
+ port.to_i
+ elsif port = @env['HTTP_X_FORWARDED_PORT']
+ port.to_i
+ elsif ssl?
+ 443
+ else
+ @env["SERVER_PORT"].to_i
+ end
end
def host
View
40 test/spec_request.rb
@@ -58,6 +58,10 @@
req.port.should.equal 80
req = Rack::Request.new \
+ Rack::MockRequest.env_for("/", "HTTP_HOST" => "www2.example.org:81")
+ req.port.should.equal 81
+
+ req = Rack::Request.new \
Rack::MockRequest.env_for("/", "SERVER_NAME" => "example.org", "SERVER_PORT" => "9292")
req.port.should.equal 9292
@@ -68,6 +72,36 @@
req = Rack::Request.new \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org")
req.port.should.equal 80
+
+ req = Rack::Request.new \
+ Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org", "HTTP_X_FORWARDED_SSL" => "on")
+ req.port.should.equal 443
+
+ req = Rack::Request.new \
+ Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org", "HTTP_X_FORWARDED_PROTO" => "https")
+ req.port.should.equal 443
+
+ 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
+ end
+
+ should "figure out the correct host with port" do
+ req = Rack::Request.new \
+ Rack::MockRequest.env_for("/", "HTTP_HOST" => "www2.example.org")
+ req.host_with_port.should.equal "www2.example.org"
+
+ req = Rack::Request.new \
+ Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81")
+ req.host_with_port.should.equal "localhost:81"
+
+ req = Rack::Request.new \
+ Rack::MockRequest.env_for("/", "SERVER_NAME" => "example.org", "SERVER_PORT" => "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:9292")
+ req.host_with_port.should.equal "example.org:9292"
end
should "parse the query string" do
@@ -250,10 +284,14 @@
request.scheme.should.equal "https"
request.should.be.ssl?
+ request = Rack::Request.new(Rack::MockRequest.env_for("/", 'HTTP_HOST' => 'www.example.org:8443', 'HTTP_X_FORWARDED_SSL' => 'on'))
+ request.scheme.should.equal "https"
+ request.should.be.ssl?
+
request = Rack::Request.new(Rack::MockRequest.env_for("/", 'HTTP_X_FORWARDED_PROTO' => 'https'))
request.scheme.should.equal "https"
request.should.be.ssl?
-
+
request = Rack::Request.new(Rack::MockRequest.env_for("/", 'HTTP_X_FORWARDED_PROTO' => 'https, http, http'))
request.scheme.should.equal "https"
request.should.be.ssl
Please sign in to comment.
Something went wrong with that request. Please try again.