diff --git a/lib/shrimp/configuration.rb b/lib/shrimp/configuration.rb index eacd238..bc3f250 100644 --- a/lib/shrimp/configuration.rb +++ b/lib/shrimp/configuration.rb @@ -5,7 +5,9 @@ class Configuration attr_accessor :default_options attr_writer :phantomjs - [:format, :margin, :zoom, :orientation, :tmpdir, :rendering_timeout, :rendering_time, :command_config_file, :viewport_width, :viewport_height, :max_redirect_count].each do |m| + [:format, :margin, :zoom, :orientation, :tmpdir, :rendering_timeout, + :rendering_time, :command_config_file, :viewport_width, :viewport_height, + :max_redirect_count, :basic_auth_username, :basic_auth_password].each do |m| define_method("#{m}=") do |val| @default_options[m]=val end @@ -23,7 +25,9 @@ def initialize :command_config_file => File.expand_path('../config.json', __FILE__), :viewport_width => 600, :viewport_height => 600, - :max_redirect_count => 0 + :max_redirect_count => 0, + :basic_auth_username => nil, + :basic_auth_password => nil, } end diff --git a/lib/shrimp/phantom.rb b/lib/shrimp/phantom.rb index 9b689ff..5bd43cd 100644 --- a/lib/shrimp/phantom.rb +++ b/lib/shrimp/phantom.rb @@ -76,8 +76,10 @@ def cmd timeout, viewport_width, viewport_height, - max_redirect_count - ].join(" ") + max_redirect_count, + options[:basic_auth_username], + options[:basic_auth_password] + ].join(" ").rstrip end # Public: initializes a new Phantom Object diff --git a/lib/shrimp/rasterize.js b/lib/shrimp/rasterize.js index 731382e..3abfbe8 100644 --- a/lib/shrimp/rasterize.js +++ b/lib/shrimp/rasterize.js @@ -10,6 +10,8 @@ var viewport_width = system.args[10] || 600, viewport_height = system.args[11] || 600, redirects_num = system.args[12] || 0, + basic_auth_username = system.args[13], + basic_auth_password = system.args[14], cookies = {}, address, output, size; @@ -21,7 +23,7 @@ function error(msg) { } function print_usage() { - console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom] [margin] [orientation] [cookie_file] [render_time] [time_out] [viewport_width] [viewport_height] [max_redirects_count]'); + console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom] [margin] [orientation] [cookie_file] [render_time] [time_out] [viewport_width] [viewport_height] [max_redirects_count] [basic_auth_username] [basic_auth_password]'); console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"'); } @@ -110,7 +112,7 @@ if (cookie_file) { phantom.cookies = cookies; } -if (system.args.length < 3 || system.args.length > 13) { +if (system.args.length < 3 || system.args.length > 15) { print_usage() && phantom.exit(2); } else { address = system.args[1]; @@ -120,6 +122,10 @@ if (system.args.length < 3 || system.args.length > 13) { viewportSize: { width: viewport_width, height: viewport_height + }, + settings: { + userName: basic_auth_username, + password: basic_auth_password } }; diff --git a/spec/shrimp/phantom_spec.rb b/spec/shrimp/phantom_spec.rb index 9d4978b..56fe0cb 100644 --- a/spec/shrimp/phantom_spec.rb +++ b/spec/shrimp/phantom_spec.rb @@ -65,6 +65,24 @@ def valid_pdf(io) phantom.cmd.should end_with " 10" end + it "should pass basic auth options into cmd line" do + basic_auth_username = 'frodo9finger' + basic_auth_password = 'foobarbaz' + phantom = Shrimp::Phantom.new("file://#{testfile}", { + :margin => "2cm", + :max_redirect_count => 10, + :basic_auth_username => basic_auth_username, + :basic_auth_password => basic_auth_password }, { }, + "#{Dir.tmpdir}/test.pdf" + ) + + phantom.cmd.should include "test.pdf A4 1 2cm portrait" + phantom.cmd.should include "file://#{testfile}" + phantom.cmd.should include "lib/shrimp/rasterize.js" + phantom.cmd.should include basic_auth_username + phantom.cmd.should end_with basic_auth_password + end + it "should properly escape arguments" do malicious_uri = "file:///hello';shutdown" bogus_phantom = Shrimp::Phantom.new(malicious_uri)