Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Convert protocol relative urls that start with // to the protocol spe…
Browse files Browse the repository at this point in the history
…cific version using rack.url_scheme
  • Loading branch information
rojotek committed Sep 15, 2014
1 parent 2b75aa6 commit 001e5e4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/pdfkit/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ def call(env)

private

# Change relative paths to absolute
# Change relative paths to absolute, and relative protocols to absolute protocols
def translate_paths(body, env)
# Host with protocol
root = PDFKit.configuration.root_url || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
body = body.gsub(/(href|src)=(['"])\/([^\/]([^\"']*|[^"']*))['"]/, '\1=\2' + root + '\3\2')

body.gsub(/(href|src)=(['"])\/([^\/]([^\"']*|[^"']*))['"]/, '\1=\2' + root + '\3\2')
protocol = "#{env['rack.url_scheme']}://"
body.gsub(/(href|src)=(['"])\/\/([^\"']*|[^"']*)['"]/,'\1=\2' + protocol + '\3\2')
end

def rendering_pdf?
Expand Down
2 changes: 1 addition & 1 deletion spec/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def mock_app(options = {}, conditions = {}, custom_headers = {})
it "should correctly parse relative url with double quotes" do
@body = %{<link href='//fonts.googleapis.com/css?family=Open+Sans:400,600' rel='stylesheet' type='text/css'>}
body = @pdf.send :translate_paths, @body, @env
expect(body).to eq("<link href='//fonts.googleapis.com/css?family=Open+Sans:400,600' rel='stylesheet' type='text/css'>")
expect(body).to eq("<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,600' rel='stylesheet' type='text/css'>")
end

it "should return the body even if there are no valid substitutions found" do
Expand Down

0 comments on commit 001e5e4

Please sign in to comment.