simplify_redirector 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/HISTORY +37 -0
- data/MIT-LICENSE +20 -0
- data/README.md +119 -0
- data/Rakefile +22 -0
- data/app/models/redirect_rule.rb +86 -0
- data/app/models/request_environment_rule.rb +23 -0
- data/db/migrate/20120815212612_create_redirect_rules.rb +16 -0
- data/db/migrate/20120823163756_create_request_environment_rules.rb +13 -0
- data/lib/redirector.rb +16 -0
- data/lib/redirector/engine.rb +20 -0
- data/lib/redirector/middleware.rb +95 -0
- data/lib/redirector/regex_attribute.rb +38 -0
- data/lib/redirector/version.rb +3 -0
- data/redirector.gemspec +33 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/news_controller.rb +9 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +59 -0
- data/spec/dummy/config/boot.rb +6 -0
- data/spec/dummy/config/database.travis.yml +26 -0
- data/spec/dummy/config/database.yml.example +17 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +39 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +39 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/db/schema.rb +43 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/factories/redirect_rules.rb +16 -0
- data/spec/factories/request_environment_rules.rb +15 -0
- data/spec/features/middleware_spec.rb +74 -0
- data/spec/models/redirect_rule_spec.rb +194 -0
- data/spec/models/request_environment_rule_spec.rb +58 -0
- data/spec/spec_helper.rb +32 -0
- metadata +287 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 42c4f36de9e435fa6a85e08d397c1e8b06a3216a
|
4
|
+
data.tar.gz: 7d2c1ed72a2b7c4539c56db7603603bd10bc17e9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 25122e7a1cb140ca973379fabddc8db4ba0681ea16fdf42e5d334f84c61dae4e6016ada72ee0dc0b8727824f47184e7a6ef2316602d742848141226fde8cdb72
|
7
|
+
data.tar.gz: 42f2b87fca748930cbc6a039d185fd112ea057e41d278f4f1ba991956042a0315f7c983f91487dc33e44a8a6e55891ae2bd364ff3b226bc6dad382354f87e31d
|
data/HISTORY
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
== 0.1.0 / 2012-08-24
|
2
|
+
* Middleware for redirecting users based on rules stored in the database.
|
3
|
+
* Rules can have a regex or string source to match against.
|
4
|
+
* Regex rules can evaluate the destination using groupings from the source.
|
5
|
+
* Rules can have further request environment conditions to match based on HTTP Headers or Rack environment variables.
|
6
|
+
* RequestEnvironmentRules can be exact string matches or regex matches.
|
7
|
+
* Make regex's case sensitivity configurable
|
8
|
+
|
9
|
+
|
10
|
+
== 0.1.1 / 2012-08-24
|
11
|
+
* Destroy RequestEnvironmentRules when the parent RedirectRule is destroyed
|
12
|
+
* Add some more indexes to redirect_rules table
|
13
|
+
* Allow query strings to be part of the match on RedirectRules
|
14
|
+
|
15
|
+
== 0.1.2 / 2012-08-27
|
16
|
+
* Change ordering on match to prefer exact matches and longer matches
|
17
|
+
|
18
|
+
== 0.1.3 / 2012-08-28
|
19
|
+
* Include request_environment_rules when pulling back possible matches
|
20
|
+
|
21
|
+
== 0.1.4 / 2012-09-07
|
22
|
+
* Allow nested attributes for request environment rules on redirect rules and be set by mass assignment.
|
23
|
+
* [BUG] Allow active to be set to false
|
24
|
+
* [BUG] Handle a nil value for a match group correctly
|
25
|
+
|
26
|
+
== 0.1.5 / 2014-01-16
|
27
|
+
* Officially support Rails 4
|
28
|
+
* Add `silence_sql_logs` config option
|
29
|
+
* Handle ports on redirects properly
|
30
|
+
* Better handle `URI::InvalidURIError` exceptions inside middleware by raising custom error
|
31
|
+
* Minor bug fix
|
32
|
+
|
33
|
+
== 1.0.0 / 2014-03-07
|
34
|
+
* Add `preserve_query` option to preserve the query string from source to destination URL
|
35
|
+
|
36
|
+
== 1.0.1 / 2014-09-26
|
37
|
+
* Fix case sensitive/insensitive matching for non-regex rules
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Brian Landau
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
# Redirector
|
2
|
+
|
3
|
+
[![Code Climate](https://codeclimate.com/github/vigetlabs/redirector.png)](https://codeclimate.com/github/vigetlabs/redirector) [![Build Status](https://travis-ci.org/vigetlabs/redirector.png?branch=master)](https://travis-ci.org/vigetlabs/redirector) [![Coverage Status](https://coveralls.io/repos/vigetlabs/redirector/badge.png?branch=master)](https://coveralls.io/r/vigetlabs/redirector?branch=master) [![Gem Version](https://badge.fury.io/rb/redirector.png)](http://badge.fury.io/rb/redirector)
|
4
|
+
|
5
|
+
|
6
|
+
Redirector is a Rails engine that adds a piece of middleware to the top of your middleware stack that looks for redirect rules stored in your database and redirects you accordingly.
|
7
|
+
|
8
|
+
## Install
|
9
|
+
|
10
|
+
1. Add this to your Gemfile and then `bundle install`:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'redirector'
|
14
|
+
```
|
15
|
+
|
16
|
+
2. `$ rake redirector_engine:install:migrations`
|
17
|
+
3. `$ rake db:migrate`
|
18
|
+
4. Create an interface for admins to manage the redirect rules.
|
19
|
+
|
20
|
+
|
21
|
+
### Config options
|
22
|
+
|
23
|
+
`include_query_in_source`: If you want your redirect rules to also match against the query string as well as the path then you need to set this to `true` (the default is `false`).
|
24
|
+
|
25
|
+
`silence_sql_logs`: This option silences the logging of Redirector related SQL queries in your log file.
|
26
|
+
|
27
|
+
`preserve_query`: Pass the query string parameters through from the source to the target URL.
|
28
|
+
|
29
|
+
`use_environment_variables`: This options disable querying to request_environment_rules table.
|
30
|
+
|
31
|
+
`blacklisted_extensions`: Skip queries for request_path with extension ['.js', '.css', '.jpg', '.png', '.woff', '.ico']
|
32
|
+
|
33
|
+
You can set these inside your configuration in `config/application.rb` of your Rails application like so:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
module MyApplication
|
37
|
+
class Application < Rails::Application
|
38
|
+
# ...
|
39
|
+
|
40
|
+
config.redirector.include_query_in_source = true
|
41
|
+
config.redirector.silence_sql_logs = true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
## Redirect Rule definitions
|
47
|
+
|
48
|
+
Redirect rules have 3 parts:
|
49
|
+
|
50
|
+
1. A Source
|
51
|
+
2. A Destination
|
52
|
+
3. Request environment conditions
|
53
|
+
|
54
|
+
The source defines how to match the incoming request path and the destination is where to send the visitor if the match is made. A source can be a strict string equality match or it can be a regular expression that is matched. If a regular expression is used and it uses groupings, you can reference those groupings inside of the destination. For instance a regex like `/my_custom_path\/([0-9]+)/` could use that grouping in the destination like this `"/my_destination/$1"`. So, if the request path was `"/my_custom_path/10"` then the destination for that rule would be `"/my_destination/10"`.
|
55
|
+
|
56
|
+
Redirect rules can also have further Rack/HTTP environment (mainly HTTP headers) conditions via RequestEnvironmentRules. These define a key in the rack environment passed into the middleware and a value match you require for the redirect rule it's tied too. Similar to the redirect rules these RequestEnvironmentRules can be string matches or regex matches. A redirect rule can have as many of these environment rules as you need.
|
57
|
+
|
58
|
+
When using regex matching on either a redirect rule source or a request environment rule environment value you can specify if you want the matching to be case sensitive or case insensitive with a boolean column that's on the table.
|
59
|
+
|
60
|
+
### Schema Definition
|
61
|
+
|
62
|
+
Here's the schema definition used for the two tables:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
create_table "redirect_rules", :force => true do |t|
|
66
|
+
t.string "source", :null => false # Matched against the request path
|
67
|
+
t.boolean "source_is_regex", :default => false, :null => false # Is the source a regular expression or not
|
68
|
+
t.boolean "source_is_case_sensitive", :default => false, :null => false # Is the source regex cas sensitive or not
|
69
|
+
t.string "destination", :null => false
|
70
|
+
t.boolean "active", :default => false # Should this rule be applied or not
|
71
|
+
t.datetime "created_at", :null => false
|
72
|
+
t.datetime "updated_at", :null => false
|
73
|
+
end
|
74
|
+
|
75
|
+
create_table "request_environment_rules", :force => true do |t|
|
76
|
+
t.integer "redirect_rule_id", :null => false
|
77
|
+
t.string "environment_key_name", :null => false # Name of the enviornment key (e.g. "QUERY_STRING", "HTTP_HOST")
|
78
|
+
t.string "environment_value", :null => false # What to match the value of the specified environment attribute against
|
79
|
+
t.boolean "environment_value_is_regex", :default => false, :null => false # Is the value match a regex or not
|
80
|
+
t.boolean "environment_value_is_case_sensitive", :default => true, :null => false # is the value regex case sensitive or not
|
81
|
+
t.datetime "created_at", :null => false
|
82
|
+
t.datetime "updated_at", :null => false
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
## Databases supported
|
87
|
+
|
88
|
+
* MySQL
|
89
|
+
* PostgreSQL
|
90
|
+
|
91
|
+
If you require support for another database, the only thing that needs to be added is a definition for a SQL regular expression conditional (see `app/models/redirect_rule.rb`). If you create a pull request that adds support for another database, it will most likely be merged in.
|
92
|
+
|
93
|
+
## Contributing to Redirector
|
94
|
+
|
95
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
96
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
97
|
+
* Fork the project.
|
98
|
+
* Start a feature/bugfix branch.
|
99
|
+
* Commit and push until you are happy with your contribution.
|
100
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
101
|
+
* We're using [Appraisal](https://github.com/thoughtbot/appraisal) to test against different Rails versions.
|
102
|
+
* In order to run the tests you'll need to do the following:
|
103
|
+
1. `cp spec/dummy/config/database.yml.example spec/dummy/config/database.yml`
|
104
|
+
2. modify that `spec/dummy/config/database.yml` with your mysql configuration details
|
105
|
+
3. run `appraisal install` (should only need to do this once)
|
106
|
+
4. run `appraisal rake spec`
|
107
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
108
|
+
|
109
|
+
## Copyright
|
110
|
+
|
111
|
+
Copyright (c) 2012 Brian Landau (Viget). See MIT_LICENSE for further details.
|
112
|
+
|
113
|
+
***
|
114
|
+
|
115
|
+
<a href="http://code.viget.com">
|
116
|
+
<img src="http://code.viget.com/github-banner.png" alt="Code At Viget">
|
117
|
+
</a>
|
118
|
+
|
119
|
+
Visit [code.viget.com](http://code.viget.com) to see more projects from [Viget.](https://viget.com)
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'appraisal'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
10
|
+
load 'rails/tasks/engine.rake'
|
11
|
+
|
12
|
+
Bundler::GemHelper.install_tasks
|
13
|
+
|
14
|
+
Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
|
15
|
+
|
16
|
+
require 'rspec/core'
|
17
|
+
require 'rspec/core/rake_task'
|
18
|
+
|
19
|
+
desc "Run all specs in spec directory (excluding plugin specs)"
|
20
|
+
RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
|
21
|
+
|
22
|
+
task :default => :spec
|
@@ -0,0 +1,86 @@
|
|
1
|
+
class RedirectRule < ActiveRecord::Base
|
2
|
+
extend Redirector::RegexAttribute
|
3
|
+
regex_attribute :source
|
4
|
+
|
5
|
+
has_many :request_environment_rules, :inverse_of => :redirect_rule, :dependent => :destroy
|
6
|
+
|
7
|
+
attr_accessible :source,
|
8
|
+
:source_is_regex,
|
9
|
+
:destination,
|
10
|
+
:active,
|
11
|
+
:source_is_case_sensitive,
|
12
|
+
:request_environment_rules_attributes if Redirector.active_record_protected_attributes?
|
13
|
+
|
14
|
+
accepts_nested_attributes_for :request_environment_rules, :allow_destroy => true, :reject_if => :all_blank
|
15
|
+
|
16
|
+
validates :source, :destination, :presence => true
|
17
|
+
validates :active, :inclusion => { :in => ['0', '1', true, false] }
|
18
|
+
|
19
|
+
before_save :strip_source_whitespace
|
20
|
+
|
21
|
+
def self.regex_expression
|
22
|
+
if connection_mysql?
|
23
|
+
'(redirect_rules.source_is_case_sensitive = :true AND :source REGEXP BINARY redirect_rules.source) OR '+
|
24
|
+
'(redirect_rules.source_is_case_sensitive = :false AND :source REGEXP redirect_rules.source)'
|
25
|
+
else
|
26
|
+
'(redirect_rules.source_is_case_sensitive = :true AND :source ~ redirect_rules.source) OR '+
|
27
|
+
'(redirect_rules.source_is_case_sensitive = :false AND :source ~* redirect_rules.source)'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.match_sql_condition
|
32
|
+
<<-SQL
|
33
|
+
redirect_rules.active = :true AND
|
34
|
+
((source_is_regex = :false AND source_is_case_sensitive = :false AND LOWER(redirect_rules.source) = LOWER(:source)) OR
|
35
|
+
(source_is_regex = :false AND source_is_case_sensitive = :true AND #{'BINARY' if connection_mysql?} redirect_rules.source = :source) OR
|
36
|
+
(source_is_regex = :true AND (#{regex_expression})))
|
37
|
+
SQL
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.match_for(source, environment)
|
41
|
+
match_scope = where(match_sql_condition.strip, {:true => true, :false => false, :source => source})
|
42
|
+
match_scope = match_scope.order('redirect_rules.source_is_regex ASC, LENGTH(redirect_rules.source) DESC')
|
43
|
+
|
44
|
+
return match_scope.first unless Redirector.use_environment_variables
|
45
|
+
|
46
|
+
match_scope = match_scope.includes(:request_environment_rules)
|
47
|
+
match_scope = match_scope.references(:request_environment_rules) if Rails.version.to_i == 4
|
48
|
+
match_scope.detect do |rule|
|
49
|
+
rule.request_environment_rules.all? {|env_rule| env_rule.matches?(environment) }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.destination_for(source, environment)
|
54
|
+
return if Redirector.blacklisted_extensions.include? File.extname(source)
|
55
|
+
|
56
|
+
rule = match_for(source, environment)
|
57
|
+
rule.evaluated_destination_for(source) if rule
|
58
|
+
end
|
59
|
+
|
60
|
+
def evaluated_destination_for(request_path)
|
61
|
+
if source_is_regex? && request_path =~ source_regex
|
62
|
+
matches = $~
|
63
|
+
number_of_grouped_matches = matches.length - 1
|
64
|
+
final_destination = destination.dup
|
65
|
+
|
66
|
+
number_of_grouped_matches.downto(1) do |index|
|
67
|
+
final_destination.gsub!(/\$#{index}/, matches[index].to_s)
|
68
|
+
end
|
69
|
+
|
70
|
+
final_destination
|
71
|
+
else
|
72
|
+
destination
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def self.connection_mysql?
|
79
|
+
connection.adapter_name.downcase.include?('mysql')
|
80
|
+
end
|
81
|
+
|
82
|
+
def strip_source_whitespace
|
83
|
+
self.source = self.source.strip
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class RequestEnvironmentRule < ActiveRecord::Base
|
2
|
+
extend Redirector::RegexAttribute
|
3
|
+
regex_attribute :environment_value
|
4
|
+
|
5
|
+
belongs_to :redirect_rule
|
6
|
+
|
7
|
+
attr_accessible :redirect_rule_id,
|
8
|
+
:environment_key_name,
|
9
|
+
:environment_value,
|
10
|
+
:environment_value_is_regex,
|
11
|
+
:environment_value_is_case_sensitive if Redirector.active_record_protected_attributes?
|
12
|
+
|
13
|
+
validates :redirect_rule, :environment_key_name, :environment_value, :presence => true
|
14
|
+
|
15
|
+
def matches?(environment)
|
16
|
+
if environment_value_is_regex?
|
17
|
+
environment[environment_key_name] && environment[environment_key_name] =~ environment_value_regex
|
18
|
+
else
|
19
|
+
environment[environment_key_name] == environment_value
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class CreateRedirectRules < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :redirect_rules do |t|
|
4
|
+
t.string :source, :null => false
|
5
|
+
t.boolean :source_is_regex, :null => false, :default => false
|
6
|
+
t.boolean :source_is_case_sensitive, :null => false, :default => false
|
7
|
+
t.string :destination, :null => false
|
8
|
+
t.boolean :active, :default => false
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
add_index :redirect_rules, :source
|
12
|
+
add_index :redirect_rules, :active
|
13
|
+
add_index :redirect_rules, :source_is_regex
|
14
|
+
add_index :redirect_rules, :source_is_case_sensitive
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class CreateRequestEnvironmentRules < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :request_environment_rules do |t|
|
4
|
+
t.integer :redirect_rule_id, :null => false
|
5
|
+
t.string :environment_key_name, :null => false
|
6
|
+
t.string :environment_value, :null => false
|
7
|
+
t.boolean :environment_value_is_regex, :null => false, :default => false
|
8
|
+
t.boolean :environment_value_is_case_sensitive, :null => false, :default => true
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
add_index :request_environment_rules, :redirect_rule_id
|
12
|
+
end
|
13
|
+
end
|
data/lib/redirector.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module Redirector
|
2
|
+
autoload :Middleware, 'redirector/middleware'
|
3
|
+
autoload :RegexAttribute, 'redirector/regex_attribute'
|
4
|
+
|
5
|
+
mattr_accessor :include_query_in_source
|
6
|
+
mattr_accessor :preserve_query
|
7
|
+
mattr_accessor :silence_sql_logs
|
8
|
+
mattr_accessor :use_environment_variables
|
9
|
+
mattr_accessor :blacklisted_extensions
|
10
|
+
|
11
|
+
def self.active_record_protected_attributes?
|
12
|
+
@active_record_protected_attributes ||= Rails.version.to_i < 4 || defined?(ProtectedAttributes)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
require "redirector/engine"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Redirector
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
config.redirector = ActiveSupport::OrderedOptions.new
|
4
|
+
|
5
|
+
initializer "redirector.add_middleware", :after => 'build_middleware_stack' do |app|
|
6
|
+
app.middleware.insert_before(Rack::Runtime, Redirector::Middleware)
|
7
|
+
end
|
8
|
+
|
9
|
+
initializer "redirector.apply_options" do |app|
|
10
|
+
config = app.config.redirector
|
11
|
+
|
12
|
+
Redirector.include_query_in_source = config.include_query_in_source || false
|
13
|
+
Redirector.preserve_query = config.preserve_query || false
|
14
|
+
Redirector.silence_sql_logs = config.silence_sql_logs || false
|
15
|
+
|
16
|
+
Redirector.use_environment_variables = config.use_environment_variables.nil? ? true : config.use_environment_variables
|
17
|
+
Redirector.blacklisted_extensions = config.blacklisted_extensions || []
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module Redirector
|
2
|
+
class RuleError < StandardError; end
|
3
|
+
|
4
|
+
class Middleware
|
5
|
+
def initialize(application)
|
6
|
+
@application = application
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(environment)
|
10
|
+
Responder.new(@application, environment).response
|
11
|
+
end
|
12
|
+
|
13
|
+
class Responder
|
14
|
+
attr_reader :app, :env
|
15
|
+
|
16
|
+
def initialize(application, environment)
|
17
|
+
@app = application
|
18
|
+
@env = environment
|
19
|
+
end
|
20
|
+
|
21
|
+
def response
|
22
|
+
if redirect?
|
23
|
+
redirect_response
|
24
|
+
else
|
25
|
+
app.call(env)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def redirect?
|
32
|
+
matched_destination.present?
|
33
|
+
end
|
34
|
+
|
35
|
+
def matched_destination
|
36
|
+
@matched_destination ||= with_optional_silencing do
|
37
|
+
RedirectRule.destination_for(request_path, env)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def with_optional_silencing(&block)
|
42
|
+
if Redirector.silence_sql_logs
|
43
|
+
ActiveRecord::Base.logger.silence { yield }
|
44
|
+
else
|
45
|
+
yield
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def request_path
|
50
|
+
if Redirector.include_query_in_source
|
51
|
+
env['ORIGINAL_FULLPATH']
|
52
|
+
else
|
53
|
+
env['PATH_INFO']
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def request_host
|
58
|
+
env['HTTP_HOST'].split(':').first
|
59
|
+
end
|
60
|
+
|
61
|
+
def request_port
|
62
|
+
@request_port ||= begin
|
63
|
+
if env['HTTP_HOST'].include?(':')
|
64
|
+
env['HTTP_HOST'].split(':').last.to_i
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def redirect_response
|
70
|
+
[301, {'Location' => redirect_url_string},
|
71
|
+
[%{You are being redirected <a href="#{redirect_url_string}">#{redirect_url_string}</a>}]]
|
72
|
+
end
|
73
|
+
|
74
|
+
def destination_uri
|
75
|
+
URI.parse(matched_destination)
|
76
|
+
rescue URI::InvalidURIError
|
77
|
+
rule = RedirectRule.match_for(request_path, env)
|
78
|
+
raise Redirector::RuleError, "RedirectRule #{rule.id} generated the bad destination: #{matched_destination}"
|
79
|
+
end
|
80
|
+
|
81
|
+
def redirect_uri
|
82
|
+
destination_uri.tap do |uri|
|
83
|
+
uri.scheme ||= 'http'
|
84
|
+
uri.host ||= request_host
|
85
|
+
uri.port ||= request_port if request_port.present?
|
86
|
+
uri.query ||= env['QUERY_STRING'] if Redirector.preserve_query
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def redirect_url_string
|
91
|
+
@redirect_url_string ||= redirect_uri.to_s
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|