rack-no-www 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ ## 0.0.2 (June 14, 2011)
2
+
3
+ - Populate CHANGELOG.
4
+ - Streamline package files and spec helper.
5
+ - FIX: Spec for Ruby 1.9.2 compatibility.
6
+ - FIX: Add `Content-Type` header for 301 Redirect.
7
+ - Update content, format, and whitespace of README.
8
+
9
+
10
+ ## 0.0.1 (June 22, 2010)
11
+
12
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ rack-no-www
2
+ ===========
3
+
4
+ This piece of simple middlweware catches requests that begin with "www"
5
+ and redirects them to the equivalent "non-www" address. For example, a
6
+ request to "http://www.example.org" will be redirected to
7
+ "http://example.org".
8
+
9
+ This kind of redirect adheres to the [no-www philosophy][1], which
10
+ advocates websites having a canonical address without the wasteful "www"
11
+ subdomain. For a site to have a canonical address, it must have a
12
+ single, preferred URL. If a site has multiple URLs that return the same
13
+ content (e.g., a CNAME alias for "http://www.example.org" that points to
14
+ "http://example.org"), search engines might interpret these URLs as
15
+ *different* resources, thus [affecting search rankings and their
16
+ appearance][2]. That's why a site should have a canonical URL. But since
17
+ many users still think "www" is a necessary part of a web address, it's
18
+ important to allow "www" requests and redirect these requests to the
19
+ "non-www" equivalent.
20
+
21
+ Redirecting to a canonical URL might better be performed at the DNS
22
+ level, or directly by the webserver (e.g., [Apache][3] or [nginx][4]).
23
+ Alas, not all DNS services allow such configuration, and some
24
+ cloud-based hosting solutions, (e.g., [Heroku][5]) don't allow explicit
25
+ server configuration. Hence this piece of middleware.
26
+
27
+
28
+ Installation
29
+ ------------
30
+
31
+ $ gem install rack-no-www
32
+
33
+
34
+ Usage
35
+ -----
36
+
37
+ Simply `require 'rack/no-www'` where appropriate, and then include the
38
+ middleware in the Rack stack. In Rails 3, for example, the
39
+ `config/application.rb` might be adjusted accordingly to place the
40
+ middleware at the top of the Rack stack::
41
+
42
+ Module MyApp
43
+ class Application < Rails::Application
44
+ ...
45
+ if Rails.env.production?
46
+ config.middleware.insert_before Rack::Lock, Rack::NoWWW
47
+ end
48
+ end
49
+ end
50
+
51
+
52
+ Credits
53
+ -------
54
+
55
+ This gem is simply a packaged version (with tests) of the idea outlined [here][6] by Trevor Turk.
56
+
57
+
58
+ [1]: http://no-www.org
59
+ [2]: http://www.google.com/support/webmasters/bin/answer.py?answer=139066
60
+ [3]: http://www.plexusweb.com/staff/travis/blog/post/274/Redirect-www-Subdomain-to-no-www
61
+ [4]: http://snippets.aktagon.com/snippets/59-How-to-improve-your-PageRank-with-301-permanent-redirects-when-using-Nginx
62
+ [5]: http://heroku.com
63
+ [6]: http://trevorturk.com/2009/11/05/no-www-rack-middleware/
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class NoWWW
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
data/lib/rack/no-www.rb CHANGED
@@ -19,7 +19,8 @@ module Rack
19
19
 
20
20
  private
21
21
  def no_www_request(env)
22
- { 'Location' => Rack::Request.new(env).url.sub(/www\./i, '') }
22
+ { 'Location' => Rack::Request.new(env).url.sub(/www\./i, ''),
23
+ 'Content-Type' => 'text/html' }
23
24
  end
24
25
 
25
26
  end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rack/no-www/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rack-no-www"
7
+ s.version = Rack::NoWWW::VERSION
8
+ s.authors = ["logicaltext"]
9
+ s.email = ["logicaltext@logicaltext.com"]
10
+ s.homepage = "http://github.com/logicaltext/rack-no-www"
11
+ s.summary = "Rack middleware for redirecting 'www' requests"
12
+ s.description = "Rack middleware for redirecting 'www' requests, " \
13
+ "based on the original idea by trevorturk "\
14
+ "(http://trevorturk.com/2009/11/05/no-www-rack-middleware/)."
15
+
16
+ s.required_rubygems_version = ">= 1.3.6"
17
+ s.rubyforge_project = "rack-no-www"
18
+
19
+ s.add_development_dependency "rspec", "~> 2.0"
20
+ s.add_development_dependency "rack-test"
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Rack::NoWWW" do
4
+
5
+ include Rack::Test::Methods
6
+
7
+ def app
8
+ mock_endpoint = lambda { |env| [200, {}, ['Hello, world.']] }
9
+ app = Rack::NoWWW.new(mock_endpoint)
10
+ end
11
+
12
+ describe "when receiving a request with a 'www'" do
13
+
14
+ before(:each) do
15
+ request '/', {'HTTP_HOST' => 'www.example.org' }
16
+ end
17
+
18
+ it "should issue a 301 redirect" do
19
+ last_response.status.should == 301
20
+ end
21
+
22
+ it "should redirect to the URL without the 'www'" do
23
+ last_response.headers['Location'].should == "http://example.org/"
24
+ end
25
+
26
+ it "should have a text/html content type" do
27
+ last_response.headers['Content-Type'].should == "text/html"
28
+ end
29
+
30
+ it "should have a body of 'Moved Permanently\\n'" do
31
+ last_response.body.should == "Moved Permanently\n"
32
+ end
33
+
34
+ end
35
+
36
+ describe "when receiving a request without a 'www'" do
37
+ it "should fall through to the app" do
38
+ get '/'
39
+ last_response.status.should == 200
40
+ last_response.body.should == "Hello, world."
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,4 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require "rack/test"
4
+ require "rack/no-www"
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-no-www
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 0
9
- - 1
10
- version: 0.0.1
4
+ prerelease:
5
+ version: 0.0.2
11
6
  platform: ruby
12
7
  authors:
13
8
  - logicaltext
@@ -15,10 +10,30 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2010-06-22 00:00:00 -04:00
19
- default_executable:
20
- dependencies: []
21
-
13
+ date: 2011-06-15 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: "2.0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rack-test
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
22
37
  description: Rack middleware for redirecting 'www' requests, based on the original idea by trevorturk (http://trevorturk.com/2009/11/05/no-www-rack-middleware/).
23
38
  email:
24
39
  - logicaltext@logicaltext.com
@@ -29,11 +44,18 @@ extensions: []
29
44
  extra_rdoc_files: []
30
45
 
31
46
  files:
32
- - lib/rack/no-www/version.rb
33
- - lib/rack/no-www.rb
34
- - lib/rack-no-www.rb
47
+ - CHANGELOG.md
48
+ - Gemfile
35
49
  - LICENSE
36
- has_rdoc: true
50
+ - README.md
51
+ - Rakefile
52
+ - autotest/discover.rb
53
+ - lib/rack-no-www.rb
54
+ - lib/rack/no-www.rb
55
+ - lib/rack/no-www/version.rb
56
+ - rack-no-www.gemspec
57
+ - spec/rack_no_www_spec.rb
58
+ - spec/spec_helper.rb
37
59
  homepage: http://github.com/logicaltext/rack-no-www
38
60
  licenses: []
39
61
 
@@ -47,27 +69,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
47
69
  requirements:
48
70
  - - ">="
49
71
  - !ruby/object:Gem::Version
50
- hash: 3
51
- segments:
52
- - 0
53
72
  version: "0"
54
73
  required_rubygems_version: !ruby/object:Gem::Requirement
55
74
  none: false
56
75
  requirements:
57
76
  - - ">="
58
77
  - !ruby/object:Gem::Version
59
- hash: 23
60
- segments:
61
- - 1
62
- - 3
63
- - 6
64
78
  version: 1.3.6
65
79
  requirements: []
66
80
 
67
81
  rubyforge_project: rack-no-www
68
- rubygems_version: 1.3.7
82
+ rubygems_version: 1.8.5
69
83
  signing_key:
70
84
  specification_version: 3
71
85
  summary: Rack middleware for redirecting 'www' requests
72
- test_files: []
73
-
86
+ test_files:
87
+ - spec/rack_no_www_spec.rb
88
+ - spec/spec_helper.rb