rack-canonical-host 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ script: bundle exec rspec
3
+ rvm:
4
+ - 1.8.7
5
+ - 1.9.2
6
+ - 1.9.3
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## Rack::CanonicalHost 0.0.5
4
+
5
+ * Rename `ignored_hosts` option to `ignore`
6
+
3
7
  ## Rack::CanonicalHost 0.0.4
4
8
 
5
9
  * Add option to ignored certain hosts ([Eric Allam][rubymaverick])
data/README.md CHANGED
@@ -4,6 +4,11 @@ Rack middleware that lets you define a single host name as the canonical host
4
4
  for your application. Requests for other host names will then be redirected to
5
5
  the canonical host.
6
6
 
7
+ [![Build Status][travis-image]][travis]
8
+
9
+ [travis]: http://travis-ci.org/tylerhunt/rack-canonical-host
10
+ [travis-image]: https://secure.travis-ci.org/tylerhunt/rack-canonical-host.png
11
+
7
12
 
8
13
  ## Installation
9
14
 
@@ -62,11 +67,11 @@ Now, the middleware will only be used if a canonical host has been defined.
62
67
 
63
68
  ### Options
64
69
 
65
- If you'd like the middleware to ignore certain hosts, use the `:ignore_hosts`
70
+ If you'd like the middleware to ignore certain hosts, use the `:ignore`
66
71
  option:
67
72
 
68
73
  ``` ruby
69
- use Rack::CanonicalHost, 'example.com', ignored_hosts: ['api.example.com']
74
+ use Rack::CanonicalHost, 'example.com', ignore: ['api.example.com']
70
75
  ```
71
76
 
72
77
  In this case, requests for the host `api.example.com` will not be redirected.
@@ -0,0 +1,46 @@
1
+ module Rack
2
+ class CanonicalHost
3
+ class Redirect
4
+ HTML_TEMPLATE = <<-HTML
5
+ <!DOCTYPE html>
6
+ <html lang="en-US">
7
+ <head><title>301 Moved Permanently</title></head>
8
+ <body>
9
+ <h1>Moved Permanently</h1>
10
+ <p>The document has moved <a href="%s">here</a>.</p>
11
+ </body>
12
+ </html>
13
+ HTML
14
+
15
+ def initialize(env, host, options={})
16
+ @env = env
17
+ @host = host
18
+ @ignore = options[:ignore]
19
+ end
20
+
21
+ def known_host?
22
+ request_uri.host == @host || ignored?
23
+ end
24
+
25
+ def response
26
+ headers = { 'Location' => new_url, 'Content-Type' => 'text/html' }
27
+ [301, headers, [HTML_TEMPLATE % new_url]]
28
+ end
29
+
30
+ def ignored?
31
+ @ignore.include?(request_uri.host) if @ignore
32
+ end
33
+ private :ignored?
34
+
35
+ def new_url
36
+ request_uri.tap { |uri| uri.host = @host }.to_s
37
+ end
38
+ private :new_url
39
+
40
+ def request_uri
41
+ URI.parse(Rack::Request.new(@env).url)
42
+ end
43
+ private :request_uri
44
+ end
45
+ end
46
+ end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class CanonicalHost
3
- VERSION = '0.0.4'
3
+ VERSION = '0.0.5'
4
4
  end
5
5
  end
@@ -1,44 +1,25 @@
1
1
  require 'rack'
2
+ require 'rack/canonical_host/redirect'
2
3
 
3
- module Rack # :nodoc:
4
+ module Rack
4
5
  class CanonicalHost
5
- HTML_TEMPLATE = <<-HTML
6
- <!DOCTYPE html>
7
- <html lang="en-US">
8
- <head><title>301 Moved Permanently</title></head>
9
- <body>
10
- <h1>Moved Permanently</h1>
11
- <p>The document has moved <a href="%s">here</a>.</p>
12
- </body>
13
- </html>
14
- HTML
15
-
16
6
  def initialize(app, host=nil, options={}, &block)
17
7
  @app = app
18
8
  @host = host
9
+ @options = options
19
10
  @block = block
20
- @ignored_hosts = options[:ignored_hosts] || []
21
11
  end
22
12
 
23
13
  def call(env)
24
- if url = url(env)
25
- [
26
- 301,
27
- { 'Location' => url, 'Content-Type' => 'text/html' },
28
- [HTML_TEMPLATE % url]
29
- ]
30
- else
31
- @app.call(env)
32
- end
33
- end
14
+ host = host(env)
15
+ redirect = Redirect.new(env, host, @options)
34
16
 
35
- def url(env)
36
- if (host = host(env)) && env['SERVER_NAME'] != host && !@ignored_hosts.include?(env['SERVER_NAME'])
37
- url = Rack::Request.new(env).url
38
- url.sub(%r{\A(https?://)(.*?)(:\d+)?(/|$)}, "\\1#{host}\\3/")
17
+ if redirect.known_host?
18
+ @app.call(env)
19
+ else
20
+ redirect.response
39
21
  end
40
22
  end
41
- private :url
42
23
 
43
24
  def host(env)
44
25
  @block ? @block.call(env) || @host : @host
@@ -47,8 +47,8 @@ describe Rack::CanonicalHost do
47
47
  include_context 'matching and non-matching requests'
48
48
  end
49
49
 
50
- context 'with ignored hosts' do
51
- let(:app) { build_app('example.com', ignored_hosts: ['example.net']) }
50
+ context 'with :ignore option' do
51
+ let(:app) { build_app('example.com', :ignore => ['example.net']) }
52
52
 
53
53
  include_context 'matching and non-matching requests'
54
54
 
@@ -68,6 +68,12 @@ describe Rack::CanonicalHost do
68
68
  let(:app) { build_app { 'example.com' } }
69
69
 
70
70
  include_context 'matching and non-matching requests'
71
+
72
+ context 'that returns nil' do
73
+ let(:app) { build_app('example.com') { nil } }
74
+
75
+ include_context 'matching and non-matching requests'
76
+ end
71
77
  end
72
78
  end
73
79
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-canonical-host
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-20 00:00:00.000000000 Z
12
+ date: 2012-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -51,6 +51,7 @@ extra_rdoc_files: []
51
51
  files:
52
52
  - .gitignore
53
53
  - .rspec
54
+ - .travis.yml
54
55
  - CHANGELOG.md
55
56
  - Gemfile
56
57
  - LICENSE
@@ -58,6 +59,7 @@ files:
58
59
  - Rakefile
59
60
  - lib/rack-canonical-host.rb
60
61
  - lib/rack/canonical_host.rb
62
+ - lib/rack/canonical_host/redirect.rb
61
63
  - lib/rack/canonical_host/version.rb
62
64
  - rack-canonical-host.gemspec
63
65
  - spec/rack/canonical_host_spec.rb