Reputation: 3094
This is a simple Perl script which takes 2 parameters - a string (which is a URL) and an integer value.
All I am trying to do is to run curl requests on the passed URL for the given period of time (in seconds).
All's well when the URL is simple like http://admin:admin@localhost:5074/?q=james
but the problem comes when there is &
in the URL.
The problem is system()
which make curl requests keeps running even after exiting the loop.
What's going on?
use strict;
use warnings;
use URI::Escape;
my $url = uri_escape($ARGV[0]);
print "URL=".$url."\n";
my $duration = $ARGV[1];
print $duration;
my $start = time();
my $end = $start + $duration;
my $request = 0;
while(time() <= $end ){
system("curl --user admin:admin --digest ".uri_unescape($url));
$request++;
}
print "------------------------ END ----------------------------------\n";
print "URL = ".uri_unescape($url)." \n";
print "Total requests made = ".$request."\n";
print "Reqs/sec = ".($request/$duration)."/sec \n";
#print $start."\n";
#print $end."\n";
print "Duration = ".$duration." sec \n";
print "-------------------------------------------------------------------\n";
The command is:
perl curl.pl "http://admin:admin@localhost:5074/?q=james&format=xml" 5
Upvotes: 0
Views: 475
Reputation: 50274
There is a &
character in your URL which is being interpreted by the shell as 'run this command in the background'. You need to escape metacharacters such as &
properly to avoid having the shell interpret them, either via:
use String::ShellQuote qw( shell_quote );
system("curl --user admin:admin --digest " . shell_quote($url));
or the preferred:
system('curl', '--user', 'admin:admin', '--digest', $url);
The latter avoids creating a shell at all, and forks/execs curl
directly with the given arguments, which are escaped automatically. It is faster and more secure. See the system perldoc for more information.
As an aside, you should consider using LWP. It's part of the Perl core for newer Perls (no installation) and you don't need to worry about escaping anything.
Upvotes: 8