Skip to content

Commit

Permalink
Merge pull request #1 from rweald/rack-www-compliant
Browse files Browse the repository at this point in the history
Custom Subdomain Code
  • Loading branch information
stjhimy committed May 27, 2011
2 parents a6a3227 + 2f02dd4 commit c302eb6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.gem
*.rbc
21 changes: 13 additions & 8 deletions lib/rack/www.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
module Rack
class WWW
def initialize(app, options = {})
@options = {:www => true}.merge(options)
@options = {:redirect => true, :subdomain => "www" }.merge(options)
@options[:redirect] = @options[:www] if @options[:www] != nil
@app = app
@www = @options[:www]
@redirect = @options[:redirect]
@message = @options[:message]
@subdomain = @options[:subdomain]
end

def call(env)
if (already_www?(env) && @www) || (!already_www?(env) && !@www)
if (already_subdomain?(env) && @redirect) || (!already_subdomain?(env) && !@redirect)
@app.call(env)
else
url = prepare_url(env)
Expand All @@ -21,26 +23,29 @@ def call(env)
end

private
def already_www?(env)
env["HTTP_HOST"].downcase =~ /^(www.)/
def already_subdomain?(env)
env["HTTP_HOST"].downcase =~ /^(#{@subdomain}.)/
end

def prepare_url(env)
scheme = env["rack.url_scheme"]
host = env["SERVER_NAME"].gsub(/^(www.)/, "")

host = env["SERVER_NAME"].gsub(/^(#{@subdomain}.)/, "")

path = env["PATH_INFO"]

query_string = ""
if !env["QUERY_STRING"].empty?
query_string = "?" + env["QUERY_STRING"]
end

if @www == true
host = "://www." + host
if @redirect == true
host = "://#{@subdomain}." + host
else
host = "://" + host
end
scheme + host + path + query_string
end

end
end
25 changes: 25 additions & 0 deletions test/test_www.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,29 @@ def app
get "http://example.com/"
assert_equal last_response.body, ""
end

test 'allows for custom subdomain' do
self.app = Rack::WWW.new(default_app, :www => true, :subdomain => "secure")
get 'http://example.com'
assert_equal 'http://secure.example.com/', last_response.headers['Location']
end

test 'allows use of redirect as alias for www' do
self.app = Rack::WWW.new(default_app, :redirect => true, :subdomain => "secure")
get 'http://example.com'
assert_equal 'http://secure.example.com/', last_response.headers['Location']
end

test 'redirects to a non subdomain if redirect is false' do
self.app = Rack::WWW.new(default_app, :redirect => false)
get "http://example.com/"
assert last_response.ok?
end

test 'Redirects to a non subdomain url and keep the right query string when param :www => false' do
self.app = Rack::WWW.new(default_app, :redirect => false)
get "http://www.example.com/path/1?param=test"
assert_equal "http://example.com/path/1?param=test", last_response.headers['Location']
end

end

0 comments on commit c302eb6

Please sign in to comment.