-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurl_wrapper.pl
executable file
·87 lines (73 loc) · 2.17 KB
/
curl_wrapper.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env perl
use 5.008000;
use warnings;
use strict;
use Carp;
use English qw(-no_match_vars);
use File::Basename;
use Getopt::Long;
# The path to curl.
use constant CURL => '/usr/bin/curl';
# Variables for command-line options.
my $filename;
# Get the command-line options.
Getopt::Long::Configure('bundling_override');
my $opts_ok = GetOptions( 'o|output=s' => \$filename );
if ( !$opts_ok || !defined $filename || scalar @ARGV != 1 ) {
spew_usage();
exit 1;
}
# Extract the positional parameters.
my ($url) = @ARGV;
# Fetch the file and obtain the HTTP status code.
my $code = fetch_file( $filename, $url );
if ( $code < 200 || $code > 299 ) {
croak "URL upload failed with HTTP status $code\n";
}
if ( !-e $filename ) {
croak "URL upload returned HTTP status $code, but no file was saved";
}
exit;
##########################################################################
# Usage : $code = fetch_file( $filename, $url );
#
# Purpose : Fetches a file from a remote server using curl and not
# following redirects.
#
# Returns : The HTTP status code.
#
# Parameters : $filename - the file name to use when saving the file.
# $url - the URL to retrieve the file from.
#
# Throws : "unable to run curl: $reason" if curl can't be executed.
# "curl exited with status code $code" on non-zero exit.
sub fetch_file {
my ( $filename, $url ) = @_;
# Build the command.
my @cmd = ( CURL, '-w', '%{http_code}', '-sSo', $filename, $url );
# Run the command and retrieve the output.
open my $in, '-|', @cmd
or croak "uanble to run curl: $ERRNO";
my $output = do { local $INPUT_RECORD_SEPARATOR; <$in> };
close $in
or croak "curl exited with status code: $CHILD_ERROR";
return $output;
}
##########################################################################
# Usage : spew_usage()
#
# Purpose : Prints a usage message to STDERR.
#
# Returns : Nothing.
#
# Parameters : None.
#
# Throws : No exceptions.
sub spew_usage {
my $prog = basename $0;
print {*STDERR} <<"END_OF_USAGE";
Usage:
$prog -o filename url
$prog --output=filename url
END_OF_USAGE
}