Skip to content

Commit bbf3729

Browse files
author
Peter Schrammel
committed
adding global options
1 parent 159d109 commit bbf3729

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

README.rdoc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,24 @@ Below is an example for configuring the middleware:
1717

1818
use Rack::ReverseProxy do
1919
# Forward the path /test* to http://example.com/test*
20+
reverse_proxy_options :preserve_host => true
21+
2022
reverse_proxy '/test', 'http://example.com/'
2123

2224
# Forward the path /foo/* to http://example.com/bar/*
23-
reverse_proxy /^\/foo(\/.*)$/, 'http://example.com/bar$1'
25+
reverse_proxy /^\/foo(\/.*)$/, 'http://example.com/bar$1', :username => 'name', :password => 'basic_auth_secret'
2426
end
2527

2628
app = proc do |env|
2729
[ 200, {'Content-Type' => 'text/plain'}, "b" ]
2830
end
2931
run app
3032

33+
revers_proxy_options set global options for all reverse proxies. Available options are:
34+
* :preserve_proxy
35+
* :username username for basic auth
36+
* :password password for basic auth
37+
3138
== Note on Patches/Pull Requests
3239

3340
* Fork the project.

lib/rack/reverse_proxy.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ReverseProxy
66
def initialize(app = nil, &b)
77
@app = app || lambda { [404, [], []] }
88
@matchers = []
9-
@global_options = {}
9+
@global_options = {:preserve_host => false}
1010
instance_eval &b if block_given?
1111
end
1212

@@ -16,7 +16,7 @@ def call(env)
1616
return @app.call(env) if matcher.nil?
1717

1818
uri = matcher.get_uri(rackreq.fullpath)
19-
all_opts = matcher.options #TODO: merge with global opts
19+
all_opts = @global_options.dup.merge(matcher.options)
2020
headers = Rack::Utils::HeaderHash.new
2121
env.each { |key, value|
2222
if key =~ /HTTP_(.*)/
@@ -81,6 +81,10 @@ def create_response_headers http_response
8181
end
8282

8383

84+
def reverse_proxy_options(options)
85+
@global_options=options
86+
end
87+
8488
def reverse_proxy matcher, url, opts={}
8589
raise GenericProxyURI.new(url) if matcher.is_a?(String) && URI(url).class == URI::Generic
8690
@matchers << ReverseProxyMatcher.new(matcher,url,opts)
@@ -121,7 +125,7 @@ class ReverseProxyMatcher
121125
def initialize(matching,url,options)
122126
@matching=matching
123127
@url=url
124-
@options={:preserve_host => false}.merge(options)
128+
@options=options
125129
@matching_regexp= matching.kind_of?(Regexp) ? matching : /^#{matching.to_s}/
126130
end
127131

0 commit comments

Comments
 (0)