diff --git a/lib/prerender_rails.rb b/lib/prerender_rails.rb index bcc0d79..3392d02 100644 --- a/lib/prerender_rails.rb +++ b/lib/prerender_rails.rb @@ -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}" diff --git a/test/lib/prerender_rails.rb b/test/lib/prerender_rails.rb index 60d66d9..060f4e2 100644 --- a/test/lib/prerender_rails.rb +++ b/test/lib/prerender_rails.rb @@ -200,7 +200,7 @@ # 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 @@ -208,7 +208,7 @@ # 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