1
+ require "rack_reverse_proxy/response_builder"
2
+
1
3
module RackReverseProxy
2
4
# FIXME: Enable them and fix issues during refactoring
3
5
# rubocop:disable Metrics/ClassLength
4
6
5
7
# RoundTrip represents one request-response made by rack-reverse-proxy
6
8
# middleware.
7
9
class RoundTrip
8
- def initialize ( app , env , global_options , rules )
10
+ def initialize ( app , env , global_options , rules , response_builder_klass = ResponseBuilder )
9
11
@app = app
10
12
@env = env
11
13
@global_options = global_options
12
14
@rules = rules
15
+ @response_builder_klass = response_builder_klass
13
16
end
14
17
15
18
def call
@@ -20,7 +23,7 @@ def call
20
23
21
24
private
22
25
23
- attr_reader :app , :env , :global_options , :rules
26
+ attr_reader :app , :env , :global_options , :rules , :response_builder_klass
24
27
25
28
def new_relic?
26
29
global_options [ :newrelic_instrumentation ]
@@ -89,7 +92,7 @@ def host_header
89
92
def set_forwarded_host
90
93
return unless options [ :x_forwarded_host ]
91
94
target_request_headers [ "X-Forwarded-Host" ] = source_request . host
92
- target_request_headers [ "X-Forwarded-Port" ] = " #{ source_request . port } "
95
+ target_request_headers [ "X-Forwarded-Port" ] = source_request . port . to_s
93
96
end
94
97
95
98
def initialize_http_header
@@ -133,25 +136,11 @@ def content_type?
133
136
end
134
137
135
138
def target_response
136
- @_target_response ||= Rack :: HttpStreamingResponse . new (
139
+ @_target_response ||= response_builder_klass . new (
137
140
target_request ,
138
- uri . host ,
139
- uri . port
140
- )
141
- end
142
-
143
- def set_read_timeout
144
- return unless read_timeout?
145
- target_response . read_timeout = options [ :timeout ]
146
- end
147
-
148
- def read_timeout?
149
- options [ :timeout ] . to_i > 0
150
- end
151
-
152
- def auto_use_ssl
153
- return unless "https" == uri . scheme
154
- target_response . use_ssl = true
141
+ uri ,
142
+ options
143
+ ) . fetch
155
144
end
156
145
157
146
def response_headers
@@ -180,7 +169,7 @@ def replace_location_header
180
169
end
181
170
182
171
def response_location
183
- @_response_location ||= URI ( response_headers [ "location " ] )
172
+ @_response_location ||= URI ( response_headers [ "Location " ] )
184
173
end
185
174
186
175
def need_replace_location?
@@ -197,9 +186,7 @@ def setup_request
197
186
set_content_type
198
187
end
199
188
200
- def setup_response
201
- set_read_timeout
202
- auto_use_ssl
189
+ def setup_response_headers
203
190
replace_location_header
204
191
end
205
192
@@ -212,7 +199,7 @@ def proxy
212
199
return https_redirect if need_https_redirect?
213
200
214
201
setup_request
215
- setup_response
202
+ setup_response_headers
216
203
217
204
rack_response
218
205
end
@@ -266,7 +253,7 @@ def matches
266
253
267
254
def non_ambiguous_match
268
255
return unless ambiguous_match?
269
- fail Errors ::AmbiguousMatch . new ( path , matches )
256
+ raise Errors ::AmbiguousMatch . new ( path , matches )
270
257
end
271
258
272
259
def ambiguous_match?
0 commit comments