Skip to content

Commit 958f60d

Browse files
committed
Reinstate CloudFlare support + custom protocol
- Reinstating custom support for CF-VISITOR CloudFlare header + options[:protocol] - Fixing failing tests Instead of passing in `X-Forwarded-Proto` to the Rack env, we can now safely rely on `HTTP_` env variables which are automatically set. Per https://github.com/rack/rack/blob/master/SPEC.rdoc#the-environment-: > HTTP_ Variables Variables corresponding to the client-supplied HTTP request headers (i.e., variables whose names begin with HTTP_). The presence or absence of these variables should correspond with the presence or absence of the appropriate HTTP header in the request. See RFC3875 section 4.1.18 for specific behavior.
1 parent 27b1ea5 commit 958f60d

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/prerender_rails.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,16 @@ def get_prerendered_page_response(env)
181181
end
182182
end
183183

184+
def get_cloudflare_scheme(env)
185+
match = /"scheme":"(http|https)"/.match(env['CF-VISITOR'])
186+
match && match[1]
187+
end
188+
184189
def build_api_url(env)
185190
request_obj = Rack::Request.new(env)
186-
url = "#{request_obj.scheme}://#{request_obj.host}#{request_obj.fullpath}"
191+
scheme = @options[:protocol] || get_cloudflare_scheme(env) || request_obj.scheme
192+
url = "#{scheme}://#{request_obj.host}#{request_obj.fullpath}"
193+
187194
prerender_url = get_prerender_service_url()
188195
forward_slash = prerender_url[-1, 1] == '/' ? '' : '/'
189196
"#{prerender_url}#{forward_slash}#{url}"

test/lib/prerender_rails.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,15 @@
200200

201201
# Check X-Forwarded-Proto because Heroku SSL Support terminates at the load balancer
202202
it "should build the correct api url for the Heroku SSL Addon support with single value" do
203-
request = Rack::MockRequest.env_for "http://google.com/search?q=javascript", { 'X-FORWARDED-PROTO' => 'https'}
203+
request = Rack::MockRequest.env_for "http://google.com/search?q=javascript", { 'HTTP_X_FORWARDED_PROTO' => 'https'}
204204
ENV['PRERENDER_SERVICE_URL'] = nil
205205
assert_equal 'http://service.prerender.io/https://google.com/search?q=javascript', @prerender.build_api_url(request)
206206
end
207207

208208

209209
# Check X-Forwarded-Proto because Heroku SSL Support terminates at the load balancer
210210
it "should build the correct api url for the Heroku SSL Addon support with double value" do
211-
request = Rack::MockRequest.env_for "http://google.com/search?q=javascript", { 'X-FORWARDED-PROTO' => 'https,http'}
211+
request = Rack::MockRequest.env_for "http://google.com/search?q=javascript", { 'HTTP_X_FORWARDED_PROTO' => 'https,http'}
212212
ENV['PRERENDER_SERVICE_URL'] = nil
213213
assert_equal 'http://service.prerender.io/https://google.com/search?q=javascript', @prerender.build_api_url(request)
214214
end

0 commit comments

Comments
 (0)