Browse files

Add subdomain option to redirect to an ssl enabled subdomain

  • Loading branch information...
1 parent fecbbeb commit 2d2c56681446746b9fd0e7c7c5fe2a35eb95fde0 @qrush qrush committed with Jan 13, 2011
Showing with 11 additions and 2 deletions.
  1. +4 −2 lib/rack/ssl.rb
  2. +7 −0 test/test_ssl.rb
View
6 lib/rack/ssl.rb
@@ -16,7 +16,8 @@ def initialize(app, options = {})
@hsts = {} if @hsts.nil? || @hsts == true
@hsts = self.class.default_hsts_options.merge(@hsts) if @hsts
- @exclude = options[:exclude]
+ @exclude = options[:exclude]
+ @subdomain = options[:subdomain]
end
def call(env)
@@ -46,7 +47,8 @@ def scheme(env)
def redirect_to_https(env)
req = Request.new(env)
- location = req.url.sub(/^http:/, 'https:')
+ location = "https://#{[@subdomain, req.host].compact.join('.')}#{req.fullpath}"
+
[301, hsts_headers.merge({'Content-Type' => "text/html", 'Location' => location}), []]
end
View
7 test/test_ssl.rb
@@ -88,4 +88,11 @@ def test_no_cookies
get "https://example.org/"
assert !last_response.headers['Set-Cookie']
end
+
+ def test_redirect_to_secure_subdomain
+ self.app = Rack::SSL.new(default_app, :subdomain => "ssl")
+ get "http://example.org/path?key=value"
+ assert_equal "https://ssl.example.org/path?key=value",
+ last_response.headers['Location']
+ end
end

0 comments on commit 2d2c566

Please sign in to comment.