Skip to content

Commit 2a0174f

Browse files
committed
Enable transformation abilities
1 parent fcd8b7f commit 2a0174f

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

lib/rack_reverse_proxy/roundtrip.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@ def proxy
208208
setup_request
209209
setup_response_headers
210210

211-
rack_response
211+
transform_response(rack_response)
212+
end
213+
214+
def transform_response(response)
215+
rule.transform(path, env, response, uri, headers, source_request)
212216
end
213217

214218
def format_headers(headers)

lib/rack_reverse_proxy/rule.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ def get_uri(path, env, *args)
2525
).build_uri
2626
end
2727

28+
def transform(path, env, response, request_uri, *args)
29+
Candidate.new(
30+
self,
31+
has_custom_url,
32+
path,
33+
env,
34+
matches(path, *args)
35+
).transform(response, request_uri)
36+
end
37+
2838
def to_s
2939
%("#{spec}" => "#{url}")
3040
end
@@ -68,6 +78,10 @@ def build_uri
6878
raw_uri
6979
end
7080

81+
def transform(response, request_uri)
82+
matches.transform(response, request_uri)
83+
end
84+
7185
private
7286

7387
attr_reader :rule, :url, :has_custom_url, :path, :env, :matches
@@ -138,6 +152,16 @@ def substitute(url)
138152
end
139153
end
140154

155+
def transform(response, request_uri)
156+
found.inject(response) do |response, match|
157+
if match.respond_to?(:transform)
158+
match.transform(response, request_uri)
159+
else
160+
response
161+
end
162+
end
163+
end
164+
141165
private
142166

143167
attr_reader :spec, :url, :path, :headers, :rackreq, :spec_arity, :has_custom_url

spec/rack/reverse_proxy_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "spec_helper"
2+
require "cgi"
23
require "base64"
34

45
RSpec.describe Rack::ReverseProxy do
@@ -582,6 +583,44 @@ def app
582583
end
583584
end
584585

586+
describe "with a matching and transforming class" do
587+
#:nodoc:
588+
class MatcherAndTransformer
589+
def self.match(path)
590+
MatcherAndTransformer.new
591+
end
592+
593+
def url(path)
594+
"http://example.org/redirecting"
595+
end
596+
597+
def transform(response, request_uri)
598+
status, headers, body = response
599+
location = headers["Location"]
600+
headers["Location"] = "?url=" + CGI.escape(location) + "&request_uri=" + CGI.escape(request_uri.to_s)
601+
[status, headers, body]
602+
end
603+
end
604+
605+
def app
606+
Rack::ReverseProxy.new(dummy_app) do
607+
reverse_proxy MatcherAndTransformer
608+
end
609+
end
610+
611+
it "transforms the proxied response" do
612+
stub_request(:get, "http://example.org/redirecting").to_return(
613+
:headers => {
614+
"Location" => "http://example.org/target"
615+
}
616+
)
617+
618+
get "/"
619+
expect(last_response.headers["Location"])
620+
.to eq("?url=http%3A%2F%2Fexample.org%2Ftarget&request_uri=http%3A%2F%2Fexample.org%2Fredirecting")
621+
end
622+
end
623+
585624
describe "with a matching class" do
586625
#:nodoc:
587626
class RequestMatcher

0 commit comments

Comments
 (0)