Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop incorrect port #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions lib/prerender_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,32 +181,21 @@ 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)
new_env = env
if env["CF-VISITOR"]
match = /"scheme":"(http|https)"/.match(env['CF-VISITOR'])
new_env["HTTPS"] = true and new_env["rack.url_scheme"] = "https" and new_env["SERVER_PORT"] = 443 if (match && match[1] == "https")
new_env["HTTPS"] = false and new_env["rack.url_scheme"] = "http" and new_env["SERVER_PORT"] = 80 if (match && match[1] == "http")
end

if env["X-FORWARDED-PROTO"]
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"
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"
end
request_obj = Rack::Request.new(env)
scheme = @options[:protocol] || get_cloudflare_scheme(env) || request_obj.scheme
url = "#{scheme}://#{request_obj.host}#{request_obj.fullpath}"

if @options[:protocol]
new_env["HTTPS"] = true and new_env["rack.url_scheme"] = "https" and new_env["SERVER_PORT"] = 443 if @options[:protocol] == "https"
new_env["HTTPS"] = false and new_env["rack.url_scheme"] = "http" and new_env["SERVER_PORT"] = 80 if @options[:protocol] == "http"
end

url = Rack::Request.new(new_env).url
prerender_url = get_prerender_service_url()
forward_slash = prerender_url[-1, 1] == '/' ? '' : '/'
"#{prerender_url}#{forward_slash}#{url}"
end


def get_prerender_service_url
@options[:prerender_service_url] || ENV['PRERENDER_SERVICE_URL'] || 'http://service.prerender.io/'
end
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