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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
package HTTP::Tinyish::Curl;
use strict;
use warnings;
use parent qw(HTTP::Tinyish::Base);
use IPC::Run3 qw(run3);
use File::Which qw(which);
use File::Temp ();
my %supports;
my $curl;
sub _slurp {
open my $fh, "<", shift or die $!;
local $/;
<$fh>;
}
sub configure {
my $class = shift;
my %meta;
$curl = which('curl');
eval {
run3([$curl, '--version'], \undef, \my $version, \my $error);
if ($version =~ /^Protocols: (.*)/m) {
my %protocols = map { $_ => 1 } split /\s/, $1;
$supports{http} = 1 if $protocols{http};
$supports{https} = 1 if $protocols{https};
}
$meta{$curl} = $version;
};
\%meta;
}
sub supports { $supports{$_[1]} }
sub new {
my($class, %attr) = @_;
bless \%attr, $class;
}
sub request {
my($self, $method, $url, $opts) = @_;
$opts ||= {};
my(undef, $temp) = File::Temp::tempfile(UNLINK => 1);
my($output, $error);
eval {
run3 [
$curl,
'-X', $method,
($method eq 'HEAD' ? ('--head') : ()),
$self->build_options($url, $opts),
'--dump-header', $temp,
$url,
], \undef, \$output, \$error,
{
binmode_stdout => ":raw",
binmode_stderr => ":raw",
};
};
if ($@ or $?) {
return $self->internal_error($url, $@ || $error);
}
my $res = { url => $url, content => $output };
$self->parse_http_header( _slurp($temp), $res );
$res;
}
sub mirror {
my($self, $url, $file, $opts) = @_;
$opts ||= {};
my(undef, $temp) = File::Temp::tempfile(UNLINK => 1);
my($output, $error);
eval {
run3 [
$curl,
$self->build_options($url, $opts),
'-z', $file,
'-o', $file,
'--dump-header', $temp,
'--remote-time',
$url,
], \undef, \$output, \$error,
{
binmode_stdout => ":raw",
binmode_stderr => ":raw",
};
};
if ($@ or $?) {
return $self->internal_error($url, $@ || $error);
}
my $res = { url => $url, content => $output };
$self->parse_http_header( _slurp($temp), $res );
$res;
}
sub build_options {
my($self, $url, $opts) = @_;
my @options = (
'--silent',
'--show-error',
'--max-time', ($self->{timeout} || 60),
'--user-agent', ($self->{agent} || "HTTP-Tinyish/$HTTP::Tinyish::VERSION"),
);
if (my $max_redirect = exists $self->{max_redirect} ? $self->{max_redirect} : 5) {
push @options, '--location', '--max-redirs', $max_redirect;
}
my %headers;
if ($self->{default_headers}) {
%headers = %{$self->{default_headers}};
}
if ($opts->{headers}) {
%headers = (%headers, %{$opts->{headers}});
}
$self->_translate_headers(\%headers, \@options);
unless ($self->{verify_SSL}) {
push @options, '--insecure';
}
if ($opts->{content}) {
my $content;
if (ref $opts->{content} eq 'CODE') {
while (my $chunk = $opts->{content}->()) {
$content .= $chunk;
}
} else {
$content = $opts->{content};
}
push @options, '--data', $content;
}
@options;
}
sub _translate_headers {
my($self, $headers, $options) = @_;
for my $field (keys %$headers) {
my $value = $headers->{$field};
if (ref $value eq 'ARRAY') {
push @$options, map { ('-H', "$field:$_") } @$value;
} else {
push @$options, '-H', "$field:$value";
}
}
}
1;
|