rack-normalize-domain 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rack/normalize-domain.rb +65 -0
- metadata +66 -0
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
class Rack::NormalizeDomain
|
5
|
+
def initialize(app, canned_strategy=nil, &block)
|
6
|
+
@app = app
|
7
|
+
@normalizer = get_normalizer(canned_strategy, block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def get_normalizer(canned_strategy, block)
|
11
|
+
canned_normalizer = get_canned_normalizer(canned_strategy)
|
12
|
+
custom_normalizer = block || lambda { |host| host }
|
13
|
+
|
14
|
+
lambda { |host| custom_normalizer[canned_normalizer[host]] }
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_canned_normalizer(strategy)
|
18
|
+
case strategy
|
19
|
+
when nil, :strip_www
|
20
|
+
lambda { |host| host.sub(/^www\./, '') }
|
21
|
+
else
|
22
|
+
fail "Unknown strategy: #{strategy}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def call(env)
|
27
|
+
request = Request.new(@normalizer, env)
|
28
|
+
|
29
|
+
if request.needs_normalization?
|
30
|
+
[301, { 'Location' => request.normalized_url }, '']
|
31
|
+
else
|
32
|
+
@app.call(env)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Request
|
37
|
+
def initialize(normalizer, env)
|
38
|
+
@normalizer = normalizer
|
39
|
+
@env = env
|
40
|
+
end
|
41
|
+
|
42
|
+
def needs_normalization?
|
43
|
+
http_get? and not already_normalized?
|
44
|
+
end
|
45
|
+
|
46
|
+
def normalized_url
|
47
|
+
"#{scheme}://#{normalized_host}#{path}#{query}"
|
48
|
+
end
|
49
|
+
|
50
|
+
def already_normalized?; host == normalized_host end
|
51
|
+
def normalized_host; @normalizer.call(host) end
|
52
|
+
|
53
|
+
def http_get?; verb == 'GET' end
|
54
|
+
def verb; @env['REQUEST_METHOD'] end
|
55
|
+
|
56
|
+
def scheme; @env['rack.url_scheme'] end
|
57
|
+
def host; @env['HTTP_HOST'] || @env['SERVER_NAME'] end
|
58
|
+
|
59
|
+
def path; raw_path == '/' ? '' : raw_path end
|
60
|
+
def query; raw_query == '' ? '' : "?#{raw_query}" end
|
61
|
+
|
62
|
+
def raw_path; @env['PATH_INFO'] end
|
63
|
+
def raw_query; @env['QUERY_STRING'] end
|
64
|
+
end
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-normalize-domain
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Daniel Brockman
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-25 00:00:00 Z
|
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/normalize-domain.rb
|
32
|
+
homepage: http://github.com/dbrock/rack-normalize-domain
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
hash: 3
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.7.2
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Rack middleware that normalizes the domain of GET requests.
|
65
|
+
test_files: []
|
66
|
+
|