Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allowing the specification of the non-SSL port alongside the SSL port.

  • Loading branch information...
commit fec95edaa0c92fd62ad9fa7bb48a4239abf3e276 1 parent 93fa9b8
@pat pat authored
Showing with 54 additions and 3 deletions.
  1. +14 −3 lib/ssl_requirement.rb
  2. +40 −0 test/ssl_requirement_test.rb
View
17 lib/ssl_requirement.rb
@@ -24,7 +24,8 @@
module SslRequirement
extend ActiveSupport::Concern
- mattr_writer :ssl_host, :ssl_port, :non_ssl_host, :disable_ssl_check
+ mattr_writer :ssl_host, :ssl_port, :non_ssl_host, :non_ssl_port,
+ :disable_ssl_check
mattr_accessor :redirect_status
def self.ssl_host
@@ -39,6 +40,10 @@ def self.non_ssl_host
determine_host(@@non_ssl_host) rescue nil
end
+ def self.non_ssl_port
+ @@non_ssl_port ||= 80
+ end
+
# mattr_reader would generate both ssl_host and self.ssl_host
def ssl_host
SslRequirement.ssl_host
@@ -52,6 +57,10 @@ def non_ssl_host
SslRequirement.non_ssl_host
end
+ def non_ssl_port
+ SslRequirement.non_ssl_port
+ end
+
def self.disable_ssl_check?
@@disable_ssl_check ||= false
end
@@ -135,9 +144,11 @@ def determine_host_and_port(request, ssl)
request_port = request.port
if ssl
- "#{(ssl_host || request_host)}#{determine_port_string(request_port || ssl_port)}"
+ port = (request_port == non_ssl_port) ? ssl_port : (request_port || ssl_port)
+ "#{(ssl_host || request_host)}#{determine_port_string(port)}"
else
- "#{(non_ssl_host || request_host)}#{determine_port_string(request_port)}"
+ port = (request_port == ssl_port) ? non_ssl_port : (request_port || non_ssl_port)
+ "#{(non_ssl_host || request_host)}#{determine_port_string(port)}"
end
end
View
40 test/ssl_requirement_test.rb
@@ -159,6 +159,20 @@ def test_redirect_to_https_preserves_non_normal_port
assert_match %r{^https://.*:4567/}, @response.headers['Location']
end
+ def test_redirect_to_https_ignores_known_non_ssl_port
+ SslRequirement.non_ssl_port = 4567
+
+ assert_not_equal "on", @request.env["HTTPS"]
+ @request.host = 'www.example.com:4567'
+ @request.port = 4567
+
+ get :b
+ assert_response :redirect
+ assert_match %r{^https://.+\.com/}, @response.headers['Location']
+
+ SslRequirement.non_ssl_port = 80
+ end
+
def test_redirect_to_https_does_not_preserve_normal_port
assert_not_equal "on", @request.env["HTTPS"]
get :b
@@ -166,6 +180,32 @@ def test_redirect_to_https_does_not_preserve_normal_port
assert_match %r{^https://.*[^:]/}, @response.headers['Location']
end
+ def redirect_to_http_preserves_non_normal_port
+ @request.env['HTTPS'] = "on"
+ @request.host = 'www.example.com:4567'
+ @request.port = 4567
+
+ get :d
+
+ assert_response :redirect
+ assert_match %r{^http://.*:4567/}, @response.headers['Location']
+ end
+
+ def test_redirect_to_http_ignores_known_ssl_port
+ SslRequirement.ssl_port = 6789
+
+ @request.env['HTTPS'] = "on"
+ @request.host = 'www.example.com:6789'
+ @request.port = 6789
+
+ get :d
+
+ assert_response :redirect
+ assert_match %r{^http://.*\.com/}, @response.headers['Location']
+
+ SslRequirement.ssl_port = 443
+ end
+
# flash-related tests
def test_redirect_to_https_preserves_flash
Please sign in to comment.
Something went wrong with that request. Please try again.