Skip to content

Commit c805e11

Browse files
janraaschwaterlink
authored andcommitted
Remove code dup
Reuse class methods from `Rack::Proxy` courtesy of the `rack-proxy` gem
1 parent 680f339 commit c805e11

File tree

1 file changed

+2
-22
lines changed

1 file changed

+2
-22
lines changed

lib/rack/reverse_proxy.rb

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def initialize(app = nil, &b)
1818

1919
def call(env)
2020
rackreq = Rack::Request.new(env)
21-
matcher = get_matcher(rackreq.fullpath, extract_http_request_headers(rackreq.env), rackreq)
21+
matcher = get_matcher(rackreq.fullpath, Proxy.extract_http_request_headers(rackreq.env), rackreq)
2222
return @app.call(env) if matcher.nil?
2323

2424
if @global_options[:newrelic_instrumentation]
@@ -44,7 +44,7 @@ def proxy(env, source_request, matcher)
4444
target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(uri.request_uri)
4545

4646
# Setup headers
47-
target_request_headers = extract_http_request_headers(source_request.env)
47+
target_request_headers = Proxy.extract_http_request_headers(source_request.env)
4848

4949
if options[:preserve_host]
5050
if uri.port == uri.default_port
@@ -96,26 +96,6 @@ def proxy(env, source_request, matcher)
9696
[target_response.status, response_headers, target_response.body]
9797
end
9898

99-
def extract_http_request_headers(env)
100-
headers = env.reject do |k, v|
101-
!(/^HTTP_[A-Z_]+$/ === k) || v.nil?
102-
end.map do |k, v|
103-
[reconstruct_header_name(k), v]
104-
end.inject(Utils::HeaderHash.new) do |hash, k_v|
105-
k, v = k_v
106-
hash[k] = v
107-
hash
108-
end
109-
110-
x_forwarded_for = (headers["X-Forwarded-For"].to_s.split(/, +/) << env["REMOTE_ADDR"]).join(", ")
111-
112-
headers.merge!("X-Forwarded-For" => x_forwarded_for)
113-
end
114-
115-
def reconstruct_header_name(name)
116-
name.sub(/^HTTP_/, "").gsub("_", "-")
117-
end
118-
11999
def get_matcher(path, headers, rackreq)
120100
matches = @matchers.select do |matcher|
121101
matcher.match?(path, headers, rackreq)

0 commit comments

Comments
 (0)