| 1 | package HTTP::Curl; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | our $VERSION = '1.07'; | ||
| 6 | |||
| 7 | use Net::Curl::Easy qw(/^CURLOPT_/ CURLE_OK CURLINFO_EFFECTIVE_URL /^CURLE_/ CURLAUTH_ANY); | ||
| 8 | |||
| 9 | |||
| 10 | BEGIN { | ||
| 11 | Net::Curl::Easy->can('CURLOPT_ACCEPT_ENCODING') or die "Rebuild Net::Curl with libcurl 7.21.6 or newer\n"; | ||
| 12 | Net::Curl::Easy->can('CURLOPT_COOKIEFILE') or die "Rebuild curl with Cookies support\n"; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub _prepare { | ||
| 16 | my ($easy, $url, $opt) = @_; | ||
| 17 | |||
| 18 | $easy->setopt(CURLOPT_URL, $url); | ||
| 19 | $easy->setopt(CURLOPT_VERBOSE, 1) if $$opt{verbose}; | ||
| 20 | |||
| 21 | my @headers = (); | ||
| 22 | @headers = map { $_ . ": " . $$opt{headers}{$_} } keys %{$$opt{headers}} if $$opt{headers}; | ||
| 23 | |||
| 24 | if ($$opt{method} and $$opt{method} eq "POST" ) { | ||
| 25 | $easy->setopt(CURLOPT_POST, 1); | ||
| 26 | unless ($$opt{headers}{"Content-Type"}) { | ||
| 27 | push @headers, "Content-Type: application/x-www-form-urlencoded"; | ||
| 28 | } | ||
| 29 | if (ref $$opt{body} eq "CODE") { | ||
| 30 | $easy->setopt(CURLOPT_POST, 1); | ||
| 31 | $easy->setopt(CURLOPT_UPLOAD, 1); | ||
| 32 | $easy->setopt(CURLOPT_CUSTOMREQUEST, "POST"); | ||
| 33 | |||
| 34 | my $buf = ""; | ||
| 35 | my $body_sub = $$opt{body}; | ||
| 36 | $easy->setopt(CURLOPT_READFUNCTION, sub { | ||
| 37 | my ( $easy, $maxlen, $uservar ) = @_; | ||
| 38 | $buf ||= $body_sub->(); | ||
| 39 | if ($buf) { | ||
| 40 | return \ substr $buf, 0, $maxlen, ""; | ||
| 41 | } else { | ||
| 42 | return CURLE_OK; | ||
| 43 | } | ||
| 44 | } ); | ||
| 45 | } else { | ||
| 46 | $easy->setopt(CURLOPT_POSTFIELDS, $$opt{body}); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | $easy->setopt(CURLOPT_HTTPHEADER, \@headers) if @headers; | ||
| 51 | |||
| 52 | my $max_redirect = defined $$opt{max_redirect} ? $$opt{max_redirect} : 7; | ||
| 53 | if ($max_redirect) { | ||
| 54 | $easy->setopt(CURLOPT_FOLLOWLOCATION, 1); | ||
| 55 | $easy->setopt(CURLOPT_MAXREDIRS, $max_redirect); | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | if ($$opt{cookie}) { | ||
| 60 | $easy->setopt(CURLOPT_COOKIEFILE, $$opt{cookie}); | ||
| 61 | $easy->setopt(CURLOPT_COOKIEJAR, $$opt{cookie}); | ||
| 62 | } elsif (defined $$opt{cookie}) { | ||
| 63 | $easy->setopt(CURLOPT_COOKIEFILE, ""); | ||
| 64 | } | ||
| 65 | |||
| 66 | my $on_header = $$opt{on_header}; | ||
| 67 | my $on_body = $$opt{on_body}; | ||
| 68 | |||
| 69 | $easy->setopt(CURLOPT_WRITEHEADER, \ my $_headers); | ||
| 70 | |||
| 71 | my $body = ""; | ||
| 72 | $easy->setopt(CURLOPT_FILE, \$body) unless $on_body; | ||
| 73 | |||
| 74 | $easy->setopt(CURLOPT_USERAGENT, $$opt{agent}) if $$opt{agent}; | ||
| 75 | $easy->setopt(CURLOPT_REFERER, $$opt{referer}) if $$opt{referer}; | ||
| 76 | $easy->setopt(CURLOPT_TIMEOUT, $$opt{timeout}) if $$opt{timeout}; | ||
| 77 | |||
| 78 | if (my $proxy = $$opt{proxy}) { | ||
| 79 | $proxy =~ s!^socks://!socks5://!; | ||
| 80 | $easy->setopt(CURLOPT_PROXY, $proxy); | ||
| 81 | if (my $proxy_auth = $$opt{proxy_auth}) { | ||
| 82 | $easy->setopt(CURLOPT_PROXYAUTH, CURLAUTH_ANY); | ||
| 83 | $easy->setopt(CURLOPT_PROXYUSERPWD, join ":", @$proxy_auth); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | $easy->setopt(CURLOPT_ACCEPT_ENCODING, "") if $$opt{compressed} or $$opt{gzip}; | ||
| 88 | |||
| 89 | $easy->setopt(CURLOPT_FORBID_REUSE, $$opt{persistent} ? 0 : 1) if exists $$opt{persistent}; | ||
| 90 | |||
| 91 | my ($is_success, $headers, $redirects); | ||
| 92 | |||
| 93 | my $max_size = $$opt{max_size}; | ||
| 94 | my $aborted_by_max_size = 0; | ||
| 95 | |||
| 96 | my $body_size = 0; | ||
| 97 | |||
| 98 | if ($max_size or $on_header or $on_body) { | ||
| 99 | my $cb_write = sub { | ||
| 100 | my ($easy, $data, $uservar) = @_; | ||
| 101 | my $size = length $data; | ||
| 102 | $body_size += $size; | ||
| 103 | if ($on_header) { | ||
| 104 | ($is_success, $headers, $redirects) = _headers($easy, $url, $_headers); | ||
| 105 | my $r = $on_header->($is_success, $headers, $redirects); | ||
| 106 | $on_header = undef; | ||
| 107 | $r or return 0; | ||
| 108 | } | ||
| 109 | if ($on_body) { | ||
| 110 | $on_body->($data) or return 0; | ||
| 111 | } else { | ||
| 112 | $body .= $data; | ||
| 113 | } | ||
| 114 | if ($max_size and $body_size > $max_size) { | ||
| 115 | $aborted_by_max_size = 1; | ||
| 116 | return 0; | ||
| 117 | } | ||
| 118 | return $size; | ||
| 119 | }; | ||
| 120 | $easy->setopt(CURLOPT_WRITEFUNCTION, $cb_write); | ||
| 121 | } else { | ||
| 122 | $easy->setopt(CURLOPT_WRITEFUNCTION, undef); | ||
| 123 | } | ||
| 124 | |||
| 125 | my $finish = sub { | ||
| 126 | my ($easy, $result) = @_; | ||
| 127 | |||
| 128 | if ($_headers) { | ||
| 129 | ($is_success, $headers, $redirects) = _headers($easy, $url, $_headers) unless $headers; | ||
| 130 | if ($result == CURLE_WRITE_ERROR and $aborted_by_max_size) { | ||
| 131 | $is_success = 0; | ||
| 132 | $$headers{"Status"} = 599; | ||
| 133 | $$headers{"Reason"} = "MaxSize"; | ||
| 134 | } elsif ($result == CURLE_OPERATION_TIMEDOUT) { | ||
| 135 | $is_success = 0; | ||
| 136 | $$headers{"Status"} = 599; | ||
| 137 | $$headers{"Reason"} = "Timeout"; | ||
| 138 | } elsif ($result == CURLE_RECV_ERROR) { | ||
| 139 | $is_success = 0; | ||
| 140 | unless ($$headers{"Status"} == 407) { | ||
| 141 | $$headers{"Status"} = 599; | ||
| 142 | $$headers{"Reason"} = "$result"; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | $easy = undef; | ||
| 146 | return ($is_success, $body, $headers, $redirects); | ||
| 147 | } else { | ||
| 148 | $easy = undef; | ||
| 149 | return (0, undef, { Status => 500, Reason => "$result", URL => $url }, []); | ||
| 150 | } | ||
| 151 | |||
| 152 | }; | ||
| 153 | |||
| 154 | return $finish; | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | sub _do_http { | ||
| 159 | my ($easy, $url, $opt) = @_; | ||
| 160 | |||
| 161 | my $finish = _prepare($easy, $url, $opt); | ||
| 162 | |||
| 163 | eval { $easy->perform() }; | ||
| 164 | if ($@) { | ||
| 165 | if (ref $@ eq "Net::Curl::Easy::Code" ) { | ||
| 166 | return $finish->($easy, $@); | ||
| 167 | } else { | ||
| 168 | die $@; | ||
| 169 | } | ||
| 170 | } else { | ||
| 171 | return $finish->($easy, CURLE_OK); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | sub do_http { | ||
| 177 | if (@_ == 5) { | ||
| 178 | my ($multi_ev, $easy, $url, $opt, $cb) = @_; | ||
| 179 | my $finish = _prepare($easy, $url, $opt); | ||
| 180 | if ($multi_ev) { | ||
| 181 | my $timeout = $$opt{timeout} || 300; | ||
| 182 | $multi_ev->($easy, sub { $cb->($finish->(@_)) }, $timeout); | ||
| 183 | } else { | ||
| 184 | $cb->(_do_http($easy, $url, $opt)); | ||
| 185 | } | ||
| 186 | } else { | ||
| 187 | goto &_do_http; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | sub _parse_headers { | ||
| 194 | my ($url, $h) = @_; | ||
| 195 | |||
| 196 | if ($h =~ m/^\r?\n$/) { | ||
| 197 | $h = "HTTP/0.9 200 Assumed OK\r\n"; | ||
| 198 | } | ||
| 199 | |||
| 200 | $h =~ s/(,\r*\n)\s+/, /g; # fix for old standard, multyline header | ||
| 201 | |||
| 202 | my ($status_line, @h) = split /\r?\n/, $h; | ||
| 203 | my ($protocol, $status, $reason) = $status_line =~ m/(HTTP\/\d(?:\.\d)?)\s+(\d+)(?:\s+(.+))?/; | ||
| 204 | |||
| 205 | my %h = (); | ||
| 206 | foreach (@h) { | ||
| 207 | my ($k, $v) = split /:\s*/, $_, 2; | ||
| 208 | my $h = lc $k; | ||
| 209 | push @{$h{$h}}, $v if $v; | ||
| 210 | } | ||
| 211 | |||
| 212 | return { | ||
| 213 | Protocol => $protocol, | ||
| 214 | Status => $status, | ||
| 215 | Reason => ($reason || ""), | ||
| 216 | URL => $url, | ||
| 217 | map { $_ => join ",", @{$h{$_}} } keys %h | ||
| 218 | }; | ||
| 219 | } | ||
| 220 | |||
| 221 | |||
| 222 | sub __headers { | ||
| 223 | my ($url, $htext, @h) = @_; | ||
| 224 | my $h = _parse_headers($url, $htext); | ||
| 225 | if (@h) { | ||
| 226 | return $h, __headers($$h{location} || $url, @h); | ||
| 227 | } else { | ||
| 228 | return $h; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | sub _headers { | ||
| 234 | my ($easy, $url, $headers) = @_; | ||
| 235 | |||
| 236 | my ($h, @hr) = reverse __headers($url, split /\r?\n\r?\n/, $headers); | ||
| 237 | |||
| 238 | my $status = $$h{Status}; | ||
| 239 | my $is_success = ($status >= 200 and $status < 300) ? 1 : 0; | ||
| 240 | |||
| 241 | $$h{URL} = $easy->getinfo(CURLINFO_EFFECTIVE_URL); | ||
| 242 | |||
| 243 | return $is_success, $h, \@hr; | ||
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | |||
| 248 | 1; | ||
| 249 | |||
| 250 | __END__ |