Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add subdomain option to redirect to an ssl enabled subdomain

  • Loading branch information...
commit 2d2c56681446746b9fd0e7c7c5fe2a35eb95fde0 1 parent fecbbeb
@qrush authored josh committed
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
Please sign in to comment.
Something went wrong with that request. Please try again.