rack-strip-www 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rack-strip-www.rb +37 -0
  2. metadata +63 -0
@@ -0,0 +1,37 @@
1
+ require 'uri'
2
+
3
+ class RackStripWWW
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ if env['REQUEST_METHOD'] != 'GET'
10
+ @app.call(env)
11
+ else
12
+ host = env['HTTP_HOST'] || env['SERVER_NAME']
13
+ stripped_host = strip_www(host)
14
+
15
+ if stripped_host == host
16
+ @app.call(env)
17
+ else
18
+ scheme = env['rack.url_scheme']
19
+ path = env['PATH_INFO']
20
+ path = '' if path == '/'
21
+ query = env['QUERY_STRING']
22
+ query = '?' + query unless query == ''
23
+ new_url = "#{scheme}://#{stripped_host}#{path}#{query}"
24
+ [301, { 'Location' => new_url }, new_url]
25
+ end
26
+ end
27
+ end
28
+
29
+ def strip_www(host)
30
+ case host
31
+ when /^www\.(([^.]+\.){1,2}[a-z]+)$/
32
+ $1
33
+ else
34
+ host
35
+ end
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-strip-www
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Daniel Brockman
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-04 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email:
23
+ - daniel@brockman.se
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/rack-strip-www.rb
32
+ has_rdoc: true
33
+ homepage: http://github.com/dbrock/rack-strip-www
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.3.6
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Rack middleware that redirects www.example.com to example.com.
62
+ test_files: []
63
+