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