Skip to content

Commit

Permalink
Reinstate CloudFlare support + custom protocol
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Peleg committed May 18, 2020
1 parent 27b1ea5 commit 22467c9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/prerender_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,16 @@ def get_prerendered_page_response(env)
end
end

def get_cloudflare_scheme(env)
match = /"scheme":"(http|https)"/.match(env['CF-VISITOR'])
match && match[1]
end

def build_api_url(env)
request_obj = Rack::Request.new(env)
url = "#{request_obj.scheme}://#{request_obj.host}#{request_obj.fullpath}"
scheme = @options[:protocol] || get_cloudflare_scheme(env) || request_obj.scheme
url = "#{scheme}://#{request_obj.host}#{request_obj.fullpath}"

prerender_url = get_prerender_service_url()
forward_slash = prerender_url[-1, 1] == '/' ? '' : '/'
"#{prerender_url}#{forward_slash}#{url}"
Expand Down
4 changes: 2 additions & 2 deletions test/lib/prerender_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,15 @@

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


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

0 comments on commit 22467c9

Please sign in to comment.