rack-force_domain 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -6,15 +6,17 @@ Directs all traffic to a single domain via 301 redirects.
6
6
 
7
7
  ### config.ru
8
8
  use Rack::ForceDomain, ENV["DOMAIN"]
9
-
9
+
10
10
  ### environment.rb
11
11
  config.middleware.use Rack::ForceDomain, ENV["DOMAIN"]
12
12
 
13
13
  ### Heroku Config
14
14
 
15
- heroku config add DOMAIN="foo.com"
15
+ heroku config:add DOMAIN="foo.com"
16
16
 
17
17
 
18
18
  Now all requests to www.foo.com (or anything else pointed at the app) will 301 to foo.com.
19
19
 
20
20
  If the `$DOMAIN` environment variable is missing, no redirection will occur.
21
+
22
+ You can also give provide a port along with your domain "foo.com:3000".
@@ -3,13 +3,27 @@ require 'rack'
3
3
  class Rack::ForceDomain
4
4
  def initialize(app, domain)
5
5
  @app = app
6
- @domain = domain
6
+ if domain and domain != ""
7
+ @domain = domain
8
+
9
+ # set host from domain
10
+ domain_parts = domain.split(":")
11
+ @host = domain_parts[0]
12
+
13
+ # add port if applicable
14
+ if domain_parts[1]
15
+ port = domain_parts[1].to_i
16
+ @port = port if port > 0
17
+ end
18
+ end
7
19
  end
8
20
 
9
21
  def call(env)
10
22
  request = Rack::Request.new(env)
11
- if @domain and request.host != @domain
12
- fake_request = Rack::Request.new(env.merge("HTTP_HOST" => @domain))
23
+ host_mismatch = (@host && request.host != @host)
24
+ port_mismatch = (@port && request.port != @port)
25
+ if host_mismatch or port_mismatch
26
+ fake_request = Rack::Request.new(env.merge("HTTP_HOST" => @domain, "SERVER_PORT" => @port || request.port))
13
27
  Rack::Response.new([], 301, "Location" => fake_request.url).finish
14
28
  else
15
29
  @app.call(env)
@@ -21,6 +21,14 @@ class ForceDomainTest < Test::Unit::TestCase
21
21
  assert !@called, "should not have passed through"
22
22
  end
23
23
 
24
+ def test_should_301_if_port_is_wrong
25
+ app = Rack::ForceDomain.new(lambda{|env| @called = true; [200, [], {}] } , "foo.com:3000")
26
+ status, body, headers = app.call(Rack::MockRequest.env_for("http://foo.com/baz"))
27
+ assert_equal 301, status
28
+ assert_equal "http://foo.com:3000/baz", headers["Location"]
29
+ assert !@called, "should not have passed through"
30
+ end
31
+
24
32
  def test_should_passthrough_for_correct_domain
25
33
  app = Rack::ForceDomain.new(lambda{|env| @called = true; [200, [], {}] } , "foo.com")
26
34
  status, body, headers = app.call(Rack::MockRequest.env_for("http://foo.com/baz"))
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-force_domain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Tom Lea
@@ -9,7 +15,7 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-02-04 00:00:00 +00:00
18
+ date: 2011-04-16 00:00:00 +01:00
13
19
  default_executable:
14
20
  dependencies: []
15
21
 
@@ -37,21 +43,27 @@ rdoc_options:
37
43
  require_paths:
38
44
  - lib
39
45
  required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
40
47
  requirements:
41
48
  - - ">="
42
49
  - !ruby/object:Gem::Version
50
+ hash: 3
51
+ segments:
52
+ - 0
43
53
  version: "0"
44
- version:
45
54
  required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
46
56
  requirements:
47
57
  - - ">="
48
58
  - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
49
62
  version: "0"
50
- version:
51
63
  requirements: []
52
64
 
53
65
  rubyforge_project: rack-force_domain
54
- rubygems_version: 1.3.5
66
+ rubygems_version: 1.3.7
55
67
  signing_key:
56
68
  specification_version: 3
57
69
  summary: Force all visitors onto a single domain.