Skip to content

Commit 27b1ea5

Browse files
committed
Drop incorrect port
`Rack::Request#url` includes the port of the original request (despite accurate scheme updates). This becomes an issue when LB terminates SSL: - client request is made to https://example.com:443, - LB request is http://example.com:80 - build_api_url returns correct scheme but wrong port: https://example:80 Since we only need the request url from the env, this changeset manually fetches the correct scheme (Rack accounts for FWD headers already), the host (w/out port as opposed to #host_with_port), and the fullpath (pathname + query params) Another benefit of this changeset is that it no longer mutates the original env object (assigning to `new_env` is by reference). Caveat: im dropping @options[:protocol] bc there's x-fwd-proto should handle this already.
1 parent 5a786a5 commit 27b1ea5

File tree

1 file changed

+2
-20
lines changed

1 file changed

+2
-20
lines changed

lib/prerender_rails.rb

+2-20
Original file line numberDiff line numberDiff line change
@@ -181,32 +181,14 @@ def get_prerendered_page_response(env)
181181
end
182182
end
183183

184-
185184
def build_api_url(env)
186-
new_env = env
187-
if env["CF-VISITOR"]
188-
match = /"scheme":"(http|https)"/.match(env['CF-VISITOR'])
189-
new_env["HTTPS"] = true and new_env["rack.url_scheme"] = "https" and new_env["SERVER_PORT"] = 443 if (match && match[1] == "https")
190-
new_env["HTTPS"] = false and new_env["rack.url_scheme"] = "http" and new_env["SERVER_PORT"] = 80 if (match && match[1] == "http")
191-
end
192-
193-
if env["X-FORWARDED-PROTO"]
194-
new_env["HTTPS"] = true and new_env["rack.url_scheme"] = "https" and new_env["SERVER_PORT"] = 443 if env["X-FORWARDED-PROTO"].split(',')[0] == "https"
195-
new_env["HTTPS"] = false and new_env["rack.url_scheme"] = "http" and new_env["SERVER_PORT"] = 80 if env["X-FORWARDED-PROTO"].split(',')[0] == "http"
196-
end
197-
198-
if @options[:protocol]
199-
new_env["HTTPS"] = true and new_env["rack.url_scheme"] = "https" and new_env["SERVER_PORT"] = 443 if @options[:protocol] == "https"
200-
new_env["HTTPS"] = false and new_env["rack.url_scheme"] = "http" and new_env["SERVER_PORT"] = 80 if @options[:protocol] == "http"
201-
end
202-
203-
url = Rack::Request.new(new_env).url
185+
request_obj = Rack::Request.new(env)
186+
url = "#{request_obj.scheme}://#{request_obj.host}#{request_obj.fullpath}"
204187
prerender_url = get_prerender_service_url()
205188
forward_slash = prerender_url[-1, 1] == '/' ? '' : '/'
206189
"#{prerender_url}#{forward_slash}#{url}"
207190
end
208191

209-
210192
def get_prerender_service_url
211193
@options[:prerender_service_url] || ENV['PRERENDER_SERVICE_URL'] || 'http://service.prerender.io/'
212194
end

0 commit comments

Comments
 (0)