Skip to content
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
26 changes: 21 additions & 5 deletions lib/prerender_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ def initialize(app, options={})
@options[:blacklist] = [@options[:blacklist]] if @options[:blacklist].is_a? String
@extensions_to_ignore = @options[:extensions_to_ignore] if @options[:extensions_to_ignore]
@crawler_user_agents = @options[:crawler_user_agents] if @options[:crawler_user_agents]

timeout = options.delete(:timeout)
@options[:open_timeout] = @options[:open_timeout] || timeout
@options[:read_timeout] = @options[:read_timeout] || timeout

@app = app
end

Expand Down Expand Up @@ -181,11 +186,9 @@ def get_prerendered_page_response(env)
}
headers['X-Prerender-Token'] = ENV['PRERENDER_TOKEN'] if ENV['PRERENDER_TOKEN']
headers['X-Prerender-Token'] = @options[:prerender_token] if @options[:prerender_token]
req = Net::HTTP::Get.new(url.request_uri, headers)
req.basic_auth(ENV['PRERENDER_USERNAME'], ENV['PRERENDER_PASSWORD']) if @options[:basic_auth]
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true if url.scheme == 'https'
response = http.request(req)

http = build_http_client(url)
response = http.request(build_http_request(url, headers))
if response['Content-Encoding'] == 'gzip'
response.body = ActiveSupport::Gzip.decompress(response.body)
response['Content-Length'] = response.body.bytesize
Expand All @@ -210,6 +213,19 @@ def get_prerendered_page_response(env)
end
end

def build_http_request(uri, headers)
Net::HTTP::Get.new(uri.request_uri, headers).tap do |req|
req.basic_auth(ENV["PRERENDER_USERNAME"], ENV["PRERENDER_PASSWORD"]) if @options[:basic_auth]
end
end

def build_http_client(uri)
Net::HTTP.new(uri.host, uri.port).tap do |http|
http.use_ssl = true if uri.scheme == "https"
http.open_timeout = @options[:open_timeout] if @options[:open_timeout]
http.read_timeout = @options[:read_timeout] if @options[:read_timeout]
end
end

def build_api_url(env)
new_env = env
Expand Down
35 changes: 35 additions & 0 deletions test/lib/prerender_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,39 @@
end
end

describe "#build_http_client" do
it "should set use_ssl for https uri" do
@prerender = Rack::Prerender.new(@app)
uri = URI.parse("https://google.com/search?q=javascript")
http_client = @prerender.build_http_client(uri)

assert_equal true, http_client.use_ssl?
end

it "should set use_ssl for http uri" do
@prerender = Rack::Prerender.new(@app)
uri = URI.parse("http://google.com/search?q=javascript")
http_client = @prerender.build_http_client(uri)

assert_equal false, http_client.use_ssl?
end

it "should use global timeout if provided" do
@prerender = Rack::Prerender.new(@app, timeout: 10)
uri = URI.parse("https://google.com/search?q=javascript")
http_client = @prerender.build_http_client(uri)

assert_equal 10, http_client.open_timeout
assert_equal 10, http_client.read_timeout
end

it "should respect open_timeout" do
@prerender = Rack::Prerender.new(@app, open_timeout: 5, read_timeout: 10)
uri = URI.parse("https://google.com/search?q=javascript")
http_client = @prerender.build_http_client(uri)

assert_equal 5, http_client.open_timeout
assert_equal 10, http_client.read_timeout
end
end
end