Skip to content

Commit 02b3fc8

Browse files
author
Peter Schrammel
committed
url can be a lambda/Proc
1 parent c61625e commit 02b3fc8

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

lib/rack/reverse_proxy.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def call(env)
1515
matcher = get_matcher rackreq.fullpath
1616
return @app.call(env) if matcher.nil?
1717

18-
uri = matcher.get_uri(rackreq.fullpath)
18+
uri = matcher.get_uri(rackreq.fullpath,env)
1919
all_opts = @global_options.dup.merge(matcher.options)
2020
headers = Rack::Utils::HeaderHash.new
2121
env.each { |key, value|
@@ -86,7 +86,7 @@ def reverse_proxy_options(options)
8686
end
8787

8888
def reverse_proxy matcher, url, opts={}
89-
raise GenericProxyURI.new(url) if matcher.is_a?(String) && URI(url).class == URI::Generic
89+
raise GenericProxyURI.new(url) if matcher.is_a?(String) && url.is_a?(String) && URI(url).class == URI::Generic
9090
@matchers << ReverseProxyMatcher.new(matcher,url,opts)
9191
end
9292
end
@@ -135,12 +135,13 @@ def match?(path)
135135
match_path(path) ? true : false
136136
end
137137

138-
def get_uri(path)
139-
if url =~/\$\d/
140-
match_path(path).to_a.each_with_index { |m, i| url.gsub!("$#{i.to_s}", m) }
141-
URI(url)
138+
def get_uri(path,env)
139+
_url=(url.respond_to?(:call) ? url.call(env) : url)
140+
if _url =~/\$\d/
141+
match_path(path).to_a.each_with_index { |m, i| _url.gsub!("$#{i.to_s}", m) }
142+
URI(_url)
142143
else
143-
URI.join(url, path)
144+
URI.join(_url, path)
144145
end
145146
end
146147
def to_s

spec/rack/reverse_proxy_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def dummy_app
1616
def app
1717
Rack::ReverseProxy.new(dummy_app) do
1818
reverse_proxy '/test', 'http://example.com/', {:preserve_host => true}
19+
reverse_proxy '/2test', lambda{'http://example.com/'}
20+
1921
end
2022
end
2123

@@ -31,6 +33,12 @@ def app
3133
last_response.body.should == "Proxied App"
3234
end
3335

36+
it "should proxy requests to a lambda url when a pattern is matched" do
37+
stub_request(:get, 'http://example.com/2test').to_return({:body => "Proxied App2"})
38+
get '/2test'
39+
last_response.body.should == "Proxied App2"
40+
end
41+
3442
it "the response header should never contain Status" do
3543
stub_request(:any, 'example.com/test/stuff').to_return(:headers => {'Status' => '200 OK'})
3644
get '/test/stuff'

0 commit comments

Comments
 (0)