shuber-proxy 1.2.1 → 1.2.2
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/CHANGELOG +7 -0
- data/README.markdown +2 -2
- data/init.rb +1 -1
- data/lib/huberry/proxy/action_controller/abstract_request.rb +43 -0
- data/lib/huberry/proxy/action_controller/base.rb +122 -0
- data/lib/huberry/proxy/action_controller/named_route_collection.rb +32 -0
- data/lib/huberry/proxy/action_controller/url_rewriter.rb +20 -0
- data/lib/huberry/proxy/action_view/url_helper.rb +20 -0
- data/lib/proxy.rb +10 -10
- metadata +7 -7
- data/lib/huberry/action_controller/abstract_request.rb +0 -41
- data/lib/huberry/action_controller/base.rb +0 -120
- data/lib/huberry/action_controller/named_route_collection.rb +0 -30
- data/lib/huberry/action_controller/url_rewriter.rb +0 -18
- data/lib/huberry/action_view/url_helper.rb +0 -18
data/CHANGELOG
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
2009-01-08 - Sean Huber (shuber@huberry.com)
|
2
|
+
* Add Proxy namespace
|
3
|
+
* Remove .gitignore
|
4
|
+
|
5
|
+
2008-12-09 - Sean Huber (shuber@huberry.com)
|
6
|
+
* Update README
|
7
|
+
|
1
8
|
2008-12-08 - Sean Huber (shuber@huberry.com)
|
2
9
|
* Fix bug - lib/proxy.rb was not included in gemspec
|
3
10
|
|
data/README.markdown
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Proxy
|
2
2
|
=====
|
3
3
|
|
4
|
-
A gem/plugin that allows rails applications to dynamically respond to proxied requests by detecting forwarded host/uri headers and setting the session domain, default host, and relative url root. The plugin adds this functionality to calls to
|
4
|
+
A gem/plugin that allows rails applications to dynamically respond to proxied requests by detecting forwarded host/uri headers and setting the session domain, default host, and relative url root. The plugin adds this functionality to calls to url\_for, named route helpers, and view url helpers while still allowing you to specifically set the :host and :only\_path options to override this behavior.
|
5
5
|
|
6
6
|
The original session domain, default host, and relative url root will be restored after each request.
|
7
7
|
|
@@ -33,7 +33,7 @@ Now imagine the client had an existing ordering system already running at /order
|
|
33
33
|
|
34
34
|
All the client has to do is proxy /neworders to http://client.example.com/orders and this plugin will automatically detect the forwarded request uri and set the relative url root for your application accordingly. Now whenever urls are generated, they will correctly use /neworders as the relative url root instead of /orders.
|
35
35
|
|
36
|
-
Note: this plugin looks for a request header called 'HTTP\_X\
|
36
|
+
Note: this plugin looks for a request header called 'HTTP\_X\_FORWARDED\_URI' to detect the relative root url by default, but this can be overwritten like so:
|
37
37
|
|
38
38
|
ActionController::AbstractRequest.forwarded_uri_header_name = 'SOME_CUSTOM_HEADER_NAME'
|
39
39
|
|
data/init.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require_dependency
|
1
|
+
require_dependency 'proxy'
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Huberry
|
2
|
+
module Proxy
|
3
|
+
module ActionController
|
4
|
+
module AbstractRequest
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval do
|
7
|
+
mattr_accessor :forwarded_uri_header_name
|
8
|
+
self.forwarded_uri_header_name = 'HTTP_X_FORWARDED_URI'
|
9
|
+
memoize :forwarded_hosts, :forwarded_uris if respond_to? :memoize
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Parses the forwarded host header and returns an array of forwarded hosts
|
14
|
+
#
|
15
|
+
# For example:
|
16
|
+
#
|
17
|
+
# If the HTTP_X_FORWARDED_HOST header was set to
|
18
|
+
# 'some-domain.com, some-other-domain.com, and-another-domain.com'
|
19
|
+
#
|
20
|
+
# This method would return ['some-domain.com', 'some-other-domain.com', 'and-another-domain.com']
|
21
|
+
#
|
22
|
+
# Returns an empty array if there aren't any forwarded hosts
|
23
|
+
def forwarded_hosts
|
24
|
+
env['HTTP_X_FORWARDED_HOST'].to_s.split(/,\s*/)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Parses the forwarded uri header and returns an array of forwarded uris
|
28
|
+
#
|
29
|
+
# For example:
|
30
|
+
#
|
31
|
+
# If the HTTP_X_FORWARDED_URI header was set to
|
32
|
+
# '/some/path, /some/other/path, /and/another/path'
|
33
|
+
#
|
34
|
+
# This method would return ['/some/path, '/some/other/path', '/and/another/path']
|
35
|
+
#
|
36
|
+
# Returns an empty array if there aren't any forwarded uris
|
37
|
+
def forwarded_uris
|
38
|
+
env[self.forwarded_uri_header_name].to_s.split(/,\s*/)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
module Huberry
|
2
|
+
module Proxy
|
3
|
+
module ActionController
|
4
|
+
module Base
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval do
|
7
|
+
before_filter :set_proxy_relative_url_root
|
8
|
+
around_filter :swap_default_host
|
9
|
+
around_filter :swap_relative_url_root
|
10
|
+
around_filter :swap_session_domain
|
11
|
+
cattr_accessor :original_relative_url_root
|
12
|
+
cattr_accessor :proxy_relative_url_root
|
13
|
+
class << self; delegate :relative_url_root, :relative_url_root=, :to => ::ActionController::AbstractRequest unless ::ActionController::Base.respond_to? :relative_url_root; end
|
14
|
+
alias_method_chain :redirect_to, :proxy
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
# Calculates the <tt>relative_url_root</tt> by parsing the request path out of the
|
21
|
+
# first forwarded uri
|
22
|
+
#
|
23
|
+
# For example:
|
24
|
+
#
|
25
|
+
# http://example.com/manage/videos/new
|
26
|
+
# gets proxied to
|
27
|
+
# http://your-domain.com/videos/new
|
28
|
+
#
|
29
|
+
# The first forwarded uri would be: /manage/videos/new
|
30
|
+
# and the request path would be: /videos/new
|
31
|
+
#
|
32
|
+
# So this method would return: /manage
|
33
|
+
def parse_proxy_relative_url_root
|
34
|
+
request.forwarded_uris.first.gsub(/#{Regexp.escape(request.path)}$/, '')
|
35
|
+
end
|
36
|
+
|
37
|
+
# Calculates the <tt>session_domain</tt> by parsing the first domain.tld out of the
|
38
|
+
# first forwarded host and prepending a '.'
|
39
|
+
#
|
40
|
+
# For example:
|
41
|
+
#
|
42
|
+
# http://example.com/manage/videos/new
|
43
|
+
# http://some.other-domain.com/videos/new
|
44
|
+
# both get proxied to
|
45
|
+
# http://your-domain.com/videos/new
|
46
|
+
#
|
47
|
+
# The resulting session domain for the first url would be: '.example.com'
|
48
|
+
# The resulting session domain for the second url would be: '.other-domain.com'
|
49
|
+
def parse_session_domain
|
50
|
+
".#{$1}" if /([^\.]+\.[^\.]+)$/.match(request.forwarded_hosts.first)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Forces redirects to use the <tt>default_url_options[:host]</tt> if it exists unless a host
|
54
|
+
# is already set
|
55
|
+
#
|
56
|
+
# For example:
|
57
|
+
#
|
58
|
+
# http://example.com
|
59
|
+
# gets proxied to
|
60
|
+
# http://your-domain.com
|
61
|
+
#
|
62
|
+
# If you have an action that calls <tt>redirect_to new_videos_path</tt>, the example.com domain
|
63
|
+
# would be used instead of your-domain.com
|
64
|
+
def redirect_to_with_proxy(*args)
|
65
|
+
args[0] = request.protocol + ::ActionController::UrlWriter.default_url_options[:host] + args.first if args.first.is_a?(String) && !%r{^\w+://.*}.match(args.first) && !::ActionController::UrlWriter.default_url_options[:host].blank?
|
66
|
+
redirect_to_without_proxy(*args)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Sets the <tt>proxy_relative_url_root</tt> using the +parse_proxy_relative_url_root+ method
|
70
|
+
# to calculate it
|
71
|
+
#
|
72
|
+
# Sets the <tt>proxy_relative_url_root</tt> to nil if there aren't any forwarded uris
|
73
|
+
def set_proxy_relative_url_root
|
74
|
+
::ActionController::Base.proxy_relative_url_root = request.forwarded_uris.empty? ? nil : parse_proxy_relative_url_root
|
75
|
+
end
|
76
|
+
|
77
|
+
# Sets the <tt>default_url_options[:host]</tt> to the first forwarded host if there are any
|
78
|
+
#
|
79
|
+
# The original default host is restored after each request and can be accessed by calling
|
80
|
+
# <tt>ActionController::UrlWriter.default_url_options[:original_host]</tt>
|
81
|
+
def swap_default_host
|
82
|
+
::ActionController::UrlWriter.default_url_options[:original_host] = ::ActionController::UrlWriter.default_url_options[:host]
|
83
|
+
::ActionController::UrlWriter.default_url_options[:host] = request.forwarded_hosts.first unless request.forwarded_hosts.empty?
|
84
|
+
begin
|
85
|
+
yield
|
86
|
+
ensure
|
87
|
+
::ActionController::UrlWriter.default_url_options[:host] = ::ActionController::UrlWriter.default_url_options[:original_host]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# Sets the <tt>relative_url_root</tt> to the <tt>proxy_relative_url_root</tt> unless it's nil
|
92
|
+
#
|
93
|
+
# The original relative url root is restored after each request and can be accessed by calling
|
94
|
+
# <tt>ActionController::Base.original_relative_url_root</tt>
|
95
|
+
def swap_relative_url_root
|
96
|
+
::ActionController::Base.original_relative_url_root = ::ActionController::Base.relative_url_root
|
97
|
+
::ActionController::Base.relative_url_root = ::ActionController::Base.proxy_relative_url_root unless ::ActionController::Base.proxy_relative_url_root.nil?
|
98
|
+
begin
|
99
|
+
yield
|
100
|
+
ensure
|
101
|
+
::ActionController::Base.relative_url_root = ::ActionController::Base.original_relative_url_root
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Sets the <tt>session_options[:session_domain]</tt> to the result of the +parse_session_domain+ method
|
106
|
+
# unless there aren't any forwarded hosts
|
107
|
+
#
|
108
|
+
# The original session domain is restored after each request and can be accessed by calling
|
109
|
+
# <tt>ActionController::Base.session_options[:original_session_domain]</tt>
|
110
|
+
def swap_session_domain
|
111
|
+
::ActionController::Base.session_options[:original_session_domain] = ::ActionController::Base.session_options[:session_domain]
|
112
|
+
::ActionController::Base.session_options[:session_domain] = parse_session_domain unless request.forwarded_hosts.empty?
|
113
|
+
begin
|
114
|
+
yield
|
115
|
+
ensure
|
116
|
+
::ActionController::Base.session_options[:session_domain] = ::ActionController::Base.session_options[:original_session_domain]
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Huberry
|
2
|
+
module Proxy
|
3
|
+
module ActionController
|
4
|
+
module NamedRouteCollection
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval { alias_method_chain :define_url_helper, :proxy }
|
7
|
+
end
|
8
|
+
|
9
|
+
# Named route url helpers (not path helpers) don't seem to work correctly
|
10
|
+
# with forwarded hosts unless we explicitly set the option:
|
11
|
+
#
|
12
|
+
# :only_path => false
|
13
|
+
#
|
14
|
+
# This method only sets that option if it isn't set already
|
15
|
+
def define_url_helper_with_proxy(route, name, kind, options)
|
16
|
+
define_url_helper_without_proxy(route, name, kind, options)
|
17
|
+
if kind == :url
|
18
|
+
selector = url_helper_name(name, kind)
|
19
|
+
@module.module_eval do
|
20
|
+
define_method "#{selector}_with_proxy" do |*args|
|
21
|
+
args << {} unless args.last.is_a? Hash
|
22
|
+
args.last[:only_path] ||= false
|
23
|
+
send "#{selector}_without_proxy", *args
|
24
|
+
end
|
25
|
+
alias_method_chain selector, :proxy
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Huberry
|
2
|
+
module Proxy
|
3
|
+
module ActionController
|
4
|
+
module UrlRewriter
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval { alias_method_chain :rewrite_url, :proxy }
|
7
|
+
end
|
8
|
+
|
9
|
+
# Adds the default :host option unless already specified
|
10
|
+
#
|
11
|
+
# It will not set the :host option if <tt>options</tt> is not a hash or
|
12
|
+
# if the <tt>ActionController::UrlWriter.default_url_options[:host]</tt> is blank
|
13
|
+
def rewrite_url_with_proxy(options)
|
14
|
+
options[:host] ||= ::ActionController::UrlWriter.default_url_options[:host] if options.is_a?(Hash) && !::ActionController::UrlWriter.default_url_options[:host].blank?
|
15
|
+
rewrite_url_without_proxy(options)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Huberry
|
2
|
+
module Proxy
|
3
|
+
module ActionView
|
4
|
+
module UrlHelper
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval { alias_method_chain :url_for, :proxy }
|
7
|
+
end
|
8
|
+
|
9
|
+
# Adds the default :host option unless already specified
|
10
|
+
#
|
11
|
+
# It will not set the :host option if <tt>options</tt> is not a hash or
|
12
|
+
# if the <tt>ActionController::UrlWriter.default_url_options[:host]</tt> is blank
|
13
|
+
def url_for_with_proxy(options = {})
|
14
|
+
options[:host] ||= ::ActionController::UrlWriter.default_url_options[:host] if options.is_a?(Hash) && !::ActionController::UrlWriter.default_url_options[:host].blank?
|
15
|
+
url_for_without_proxy(options)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/proxy.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
require 'huberry/action_controller/abstract_request'
|
2
|
-
require 'huberry/action_controller/base'
|
3
|
-
require 'huberry/action_controller/named_route_collection'
|
4
|
-
require 'huberry/action_controller/url_rewriter'
|
5
|
-
require 'huberry/action_view/url_helper'
|
1
|
+
require 'huberry/proxy/action_controller/abstract_request'
|
2
|
+
require 'huberry/proxy/action_controller/base'
|
3
|
+
require 'huberry/proxy/action_controller/named_route_collection'
|
4
|
+
require 'huberry/proxy/action_controller/url_rewriter'
|
5
|
+
require 'huberry/proxy/action_view/url_helper'
|
6
6
|
|
7
|
-
ActionController::AbstractRequest.send :include, Huberry::ActionController::AbstractRequest
|
8
|
-
ActionController::Base.send :include, Huberry::ActionController::Base
|
9
|
-
ActionController::Routing::RouteSet::NamedRouteCollection.send :include, Huberry::ActionController::NamedRouteCollection
|
10
|
-
ActionController::UrlRewriter.send :include, Huberry::ActionController::UrlRewriter
|
11
|
-
ActionView::Base.send :include, Huberry::ActionView::UrlHelper
|
7
|
+
ActionController::AbstractRequest.send :include, Huberry::Proxy::ActionController::AbstractRequest
|
8
|
+
ActionController::Base.send :include, Huberry::Proxy::ActionController::Base
|
9
|
+
ActionController::Routing::RouteSet::NamedRouteCollection.send :include, Huberry::Proxy::ActionController::NamedRouteCollection
|
10
|
+
ActionController::UrlRewriter.send :include, Huberry::Proxy::ActionController::UrlRewriter
|
11
|
+
ActionView::Base.send :include, Huberry::Proxy::ActionView::UrlHelper
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shuber-proxy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Huber
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-01-08 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -24,11 +24,11 @@ extra_rdoc_files: []
|
|
24
24
|
files:
|
25
25
|
- CHANGELOG
|
26
26
|
- init.rb
|
27
|
-
- lib/huberry/action_controller/abstract_request.rb
|
28
|
-
- lib/huberry/action_controller/base.rb
|
29
|
-
- lib/huberry/action_controller/named_route_collection.rb
|
30
|
-
- lib/huberry/action_controller/url_rewriter.rb
|
31
|
-
- lib/huberry/action_view/url_helper.rb
|
27
|
+
- lib/huberry/proxy/action_controller/abstract_request.rb
|
28
|
+
- lib/huberry/proxy/action_controller/base.rb
|
29
|
+
- lib/huberry/proxy/action_controller/named_route_collection.rb
|
30
|
+
- lib/huberry/proxy/action_controller/url_rewriter.rb
|
31
|
+
- lib/huberry/proxy/action_view/url_helper.rb
|
32
32
|
- lib/proxy.rb
|
33
33
|
- MIT-LICENSE
|
34
34
|
- Rakefile
|
@@ -1,41 +0,0 @@
|
|
1
|
-
module Huberry
|
2
|
-
module ActionController
|
3
|
-
module AbstractRequest
|
4
|
-
def self.included(base)
|
5
|
-
base.class_eval do
|
6
|
-
mattr_accessor :forwarded_uri_header_name
|
7
|
-
self.forwarded_uri_header_name = 'HTTP_X_FORWARDED_URI'
|
8
|
-
memoize :forwarded_hosts, :forwarded_uris if respond_to? :memoize
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
# Parses the forwarded host header and returns an array of forwarded hosts
|
13
|
-
#
|
14
|
-
# For example:
|
15
|
-
#
|
16
|
-
# If the HTTP_X_FORWARDED_HOST header was set to
|
17
|
-
# 'some-domain.com, some-other-domain.com, and-another-domain.com'
|
18
|
-
#
|
19
|
-
# This method would return ['some-domain.com', 'some-other-domain.com', 'and-another-domain.com']
|
20
|
-
#
|
21
|
-
# Returns an empty array if there aren't any forwarded hosts
|
22
|
-
def forwarded_hosts
|
23
|
-
env['HTTP_X_FORWARDED_HOST'].to_s.split(/,\s*/)
|
24
|
-
end
|
25
|
-
|
26
|
-
# Parses the forwarded uri header and returns an array of forwarded uris
|
27
|
-
#
|
28
|
-
# For example:
|
29
|
-
#
|
30
|
-
# If the HTTP_X_FORWARDED_URI header was set to
|
31
|
-
# '/some/path, /some/other/path, /and/another/path'
|
32
|
-
#
|
33
|
-
# This method would return ['/some/path, '/some/other/path', '/and/another/path']
|
34
|
-
#
|
35
|
-
# Returns an empty array if there aren't any forwarded uris
|
36
|
-
def forwarded_uris
|
37
|
-
env[self.forwarded_uri_header_name].to_s.split(/,\s*/)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
@@ -1,120 +0,0 @@
|
|
1
|
-
module Huberry
|
2
|
-
module ActionController
|
3
|
-
module Base
|
4
|
-
def self.included(base)
|
5
|
-
base.class_eval do
|
6
|
-
before_filter :set_proxy_relative_url_root
|
7
|
-
around_filter :swap_default_host
|
8
|
-
around_filter :swap_relative_url_root
|
9
|
-
around_filter :swap_session_domain
|
10
|
-
cattr_accessor :original_relative_url_root
|
11
|
-
cattr_accessor :proxy_relative_url_root
|
12
|
-
class << self; delegate :relative_url_root, :relative_url_root=, :to => ::ActionController::AbstractRequest unless ::ActionController::Base.respond_to? :relative_url_root; end
|
13
|
-
alias_method_chain :redirect_to, :proxy
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
protected
|
18
|
-
|
19
|
-
# Calculates the <tt>relative_url_root</tt> by parsing the request path out of the
|
20
|
-
# first forwarded uri
|
21
|
-
#
|
22
|
-
# For example:
|
23
|
-
#
|
24
|
-
# http://example.com/manage/videos/new
|
25
|
-
# gets proxied to
|
26
|
-
# http://your-domain.com/videos/new
|
27
|
-
#
|
28
|
-
# The first forwarded uri would be: /manage/videos/new
|
29
|
-
# and the request path would be: /videos/new
|
30
|
-
#
|
31
|
-
# So this method would return: /manage
|
32
|
-
def parse_proxy_relative_url_root
|
33
|
-
request.forwarded_uris.first.gsub(/#{Regexp.escape(request.path)}$/, '')
|
34
|
-
end
|
35
|
-
|
36
|
-
# Calculates the <tt>session_domain</tt> by parsing the first domain.tld out of the
|
37
|
-
# first forwarded host and prepending a '.'
|
38
|
-
#
|
39
|
-
# For example:
|
40
|
-
#
|
41
|
-
# http://example.com/manage/videos/new
|
42
|
-
# http://some.other-domain.com/videos/new
|
43
|
-
# both get proxied to
|
44
|
-
# http://your-domain.com/videos/new
|
45
|
-
#
|
46
|
-
# The resulting session domain for the first url would be: '.example.com'
|
47
|
-
# The resulting session domain for the second url would be: '.other-domain.com'
|
48
|
-
def parse_session_domain
|
49
|
-
".#{$1}" if /([^\.]+\.[^\.]+)$/.match(request.forwarded_hosts.first)
|
50
|
-
end
|
51
|
-
|
52
|
-
# Forces redirects to use the <tt>default_url_options[:host]</tt> if it exists unless a host
|
53
|
-
# is already set
|
54
|
-
#
|
55
|
-
# For example:
|
56
|
-
#
|
57
|
-
# http://example.com
|
58
|
-
# gets proxied to
|
59
|
-
# http://your-domain.com
|
60
|
-
#
|
61
|
-
# If you have an action that calls <tt>redirect_to new_videos_path</tt>, the example.com domain
|
62
|
-
# would be used instead of your-domain.com
|
63
|
-
def redirect_to_with_proxy(*args)
|
64
|
-
args[0] = request.protocol + ::ActionController::UrlWriter.default_url_options[:host] + args.first if args.first.is_a?(String) && !%r{^\w+://.*}.match(args.first) && !::ActionController::UrlWriter.default_url_options[:host].blank?
|
65
|
-
redirect_to_without_proxy(*args)
|
66
|
-
end
|
67
|
-
|
68
|
-
# Sets the <tt>proxy_relative_url_root</tt> using the +parse_proxy_relative_url_root+ method
|
69
|
-
# to calculate it
|
70
|
-
#
|
71
|
-
# Sets the <tt>proxy_relative_url_root</tt> to nil if there aren't any forwarded uris
|
72
|
-
def set_proxy_relative_url_root
|
73
|
-
::ActionController::Base.proxy_relative_url_root = request.forwarded_uris.empty? ? nil : parse_proxy_relative_url_root
|
74
|
-
end
|
75
|
-
|
76
|
-
# Sets the <tt>default_url_options[:host]</tt> to the first forwarded host if there are any
|
77
|
-
#
|
78
|
-
# The original default host is restored after each request and can be accessed by calling
|
79
|
-
# <tt>ActionController::UrlWriter.default_url_options[:original_host]</tt>
|
80
|
-
def swap_default_host
|
81
|
-
::ActionController::UrlWriter.default_url_options[:original_host] = ::ActionController::UrlWriter.default_url_options[:host]
|
82
|
-
::ActionController::UrlWriter.default_url_options[:host] = request.forwarded_hosts.first unless request.forwarded_hosts.empty?
|
83
|
-
begin
|
84
|
-
yield
|
85
|
-
ensure
|
86
|
-
::ActionController::UrlWriter.default_url_options[:host] = ::ActionController::UrlWriter.default_url_options[:original_host]
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
# Sets the <tt>relative_url_root</tt> to the <tt>proxy_relative_url_root</tt> unless it's nil
|
91
|
-
#
|
92
|
-
# The original relative url root is restored after each request and can be accessed by calling
|
93
|
-
# <tt>ActionController::Base.original_relative_url_root</tt>
|
94
|
-
def swap_relative_url_root
|
95
|
-
::ActionController::Base.original_relative_url_root = ::ActionController::Base.relative_url_root
|
96
|
-
::ActionController::Base.relative_url_root = ::ActionController::Base.proxy_relative_url_root unless ::ActionController::Base.proxy_relative_url_root.nil?
|
97
|
-
begin
|
98
|
-
yield
|
99
|
-
ensure
|
100
|
-
::ActionController::Base.relative_url_root = ::ActionController::Base.original_relative_url_root
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
# Sets the <tt>session_options[:session_domain]</tt> to the result of the +parse_session_domain+ method
|
105
|
-
# unless there aren't any forwarded hosts
|
106
|
-
#
|
107
|
-
# The original session domain is restored after each request and can be accessed by calling
|
108
|
-
# <tt>ActionController::Base.session_options[:original_session_domain]</tt>
|
109
|
-
def swap_session_domain
|
110
|
-
::ActionController::Base.session_options[:original_session_domain] = ::ActionController::Base.session_options[:session_domain]
|
111
|
-
::ActionController::Base.session_options[:session_domain] = parse_session_domain unless request.forwarded_hosts.empty?
|
112
|
-
begin
|
113
|
-
yield
|
114
|
-
ensure
|
115
|
-
::ActionController::Base.session_options[:session_domain] = ::ActionController::Base.session_options[:original_session_domain]
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module Huberry
|
2
|
-
module ActionController
|
3
|
-
module NamedRouteCollection
|
4
|
-
def self.included(base)
|
5
|
-
base.class_eval { alias_method_chain :define_url_helper, :proxy }
|
6
|
-
end
|
7
|
-
|
8
|
-
# Named route url helpers (not path helpers) don't seem to work correctly
|
9
|
-
# with forwarded hosts unless we explicitly set the option:
|
10
|
-
#
|
11
|
-
# :only_path => false
|
12
|
-
#
|
13
|
-
# This method only sets that option if it isn't set already
|
14
|
-
def define_url_helper_with_proxy(route, name, kind, options)
|
15
|
-
define_url_helper_without_proxy(route, name, kind, options)
|
16
|
-
if kind == :url
|
17
|
-
selector = url_helper_name(name, kind)
|
18
|
-
@module.module_eval do
|
19
|
-
define_method "#{selector}_with_proxy" do |*args|
|
20
|
-
args << {} unless args.last.is_a? Hash
|
21
|
-
args.last[:only_path] ||= false
|
22
|
-
send "#{selector}_without_proxy", *args
|
23
|
-
end
|
24
|
-
alias_method_chain selector, :proxy
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Huberry
|
2
|
-
module ActionController
|
3
|
-
module UrlRewriter
|
4
|
-
def self.included(base)
|
5
|
-
base.class_eval { alias_method_chain :rewrite_url, :proxy }
|
6
|
-
end
|
7
|
-
|
8
|
-
# Adds the default :host option unless already specified
|
9
|
-
#
|
10
|
-
# It will not set the :host option if <tt>options</tt> is not a hash or
|
11
|
-
# if the <tt>ActionController::UrlWriter.default_url_options[:host]</tt> is blank
|
12
|
-
def rewrite_url_with_proxy(options)
|
13
|
-
options[:host] ||= ::ActionController::UrlWriter.default_url_options[:host] if options.is_a?(Hash) && !::ActionController::UrlWriter.default_url_options[:host].blank?
|
14
|
-
rewrite_url_without_proxy(options)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Huberry
|
2
|
-
module ActionView
|
3
|
-
module UrlHelper
|
4
|
-
def self.included(base)
|
5
|
-
base.class_eval { alias_method_chain :url_for, :proxy }
|
6
|
-
end
|
7
|
-
|
8
|
-
# Adds the default :host option unless already specified
|
9
|
-
#
|
10
|
-
# It will not set the :host option if <tt>options</tt> is not a hash or
|
11
|
-
# if the <tt>ActionController::UrlWriter.default_url_options[:host]</tt> is blank
|
12
|
-
def url_for_with_proxy(options = {})
|
13
|
-
options[:host] ||= ::ActionController::UrlWriter.default_url_options[:host] if options.is_a?(Hash) && !::ActionController::UrlWriter.default_url_options[:host].blank?
|
14
|
-
url_for_without_proxy(options)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|