blib/lib/HTTP/Curl.pm untested

filter: Statementstmt 0.0% 0 / 144 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 37 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 268 CC 36 SCAR 62.3
CC
36
Cov
0%
_prepare
812.0
_do_http
12.0
do_http
12.0
CRAP 508.4
1 package HTTP::Curl;
2  
3 use strict;
4 use warnings;
5 our $VERSION = '1.04';
6  
7 use Net::Curl::Easy qw(/^CURLOPT_/ CURLE_OK CURLINFO_EFFECTIVE_URL CURLE_WRITE_ERROR CURLE_OPERATION_TIMEDOUT CURLE_RECV_ERROR);
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  
20 my @headers = ();
21 @headers = map { $_ . ": " . $$opt{headers}{$_} } keys %{$$opt{headers}} if $$opt{headers};
22  
23 if ($$opt{method} and $$opt{method} eq "POST" ) {
24 $easy->setopt(CURLOPT_POST, 1);
25 unless ($$opt{headers}{"Content-Type"}) {
26 push @headers, "Content-Type: application/x-www-form-urlencoded";
27 }
28 if (ref $$opt{body} eq "CODE") {
29 $easy->setopt(CURLOPT_POST, 1);
30 $easy->setopt(CURLOPT_UPLOAD, 1);
31 $easy->setopt(CURLOPT_CUSTOMREQUEST, "POST");
32  
33 my $buf = "";
34 my $body_sub = $$opt{body};
35 $easy->setopt(CURLOPT_READFUNCTION, sub {
36 my ( $easy, $maxlen, $uservar ) = @_;
37 $buf ||= $body_sub->();
38 if ($buf) {
39 return \ substr $buf, 0, $maxlen, "";
40 } else {
41 return CURLE_OK;
42 }
43 } );
44 } else {
45 $easy->setopt(CURLOPT_POSTFIELDS, $$opt{body});
46 }
47 }
48  
49 $easy->setopt(CURLOPT_HTTPHEADER, \@headers) if @headers;
50  
51 my $max_redirect = defined $$opt{max_redirect} ? $$opt{max_redirect} : 7;
52 if ($max_redirect) {
53 $easy->setopt(CURLOPT_FOLLOWLOCATION, 1);
54 $easy->setopt(CURLOPT_MAXREDIRS, $max_redirect);
55 }
56  
57  
58 if ($$opt{cookie}) {
59 $easy->setopt(CURLOPT_COOKIEFILE, $$opt{cookie});
60 $easy->setopt(CURLOPT_COOKIEJAR, $$opt{cookie});
61 } elsif (defined $$opt{cookie}) {
62 $easy->setopt(CURLOPT_COOKIEFILE, "");
63 }
64  
65 my $on_header = $$opt{on_header};
66 my $on_body = $$opt{on_body};
67  
68 $easy->setopt(CURLOPT_WRITEHEADER, \ my $_headers);
69  
70 my $body = "";
71 $easy->setopt(CURLOPT_FILE, \$body) unless $on_body;
72  
73 $easy->setopt(CURLOPT_USERAGENT, $$opt{agent}) if $$opt{agent};
74 $easy->setopt(CURLOPT_REFERER, $$opt{referer}) if $$opt{referer};
75 $easy->setopt(CURLOPT_TIMEOUT, $$opt{timeout}) if $$opt{timeout};
76  
77 if (my $proxy = $$opt{proxy}) {
78 $proxy =~ s!^socks://!socks5://!;
79 $easy->setopt(CURLOPT_PROXY, $proxy);
80 }
81  
82 $easy->setopt(CURLOPT_ACCEPT_ENCODING, "") if $$opt{compressed} or $$opt{gzip};
83  
84 $easy->setopt(CURLOPT_FORBID_REUSE, $$opt{persistent} ? 0 : 1) if exists $$opt{persistent};
85  
86 my ($is_success, $headers, $redirects);
87  
88 my $max_size = $$opt{max_size};
89 my $aborted_by_max_size = 0;
90  
91 my $body_size = 0;
92  
93 if ($max_size or $on_header or $on_body) {
94 my $cb_write = sub {
95 my ($easy, $data, $uservar) = @_;
96 my $size = length $data;
97 $body_size += $size;
98 if ($on_header) {
99 ($is_success, $headers, $redirects) = _headers($easy, $url, $_headers);
100 my $r = $on_header->($is_success, $headers, $redirects);
101 $on_header = undef;
102 $r or return 0;
103 }
104 if ($on_body) {
105 $on_body->($data) or return 0;
106 } else {
107 $body .= $data;
108 }
109 if ($max_size and $body_size > $max_size) {
110 $aborted_by_max_size = 1;
111 return 0;
112 }
113 return $size;
114 };
115 $easy->setopt(CURLOPT_WRITEFUNCTION, $cb_write);
116 } else {
117 $easy->setopt(CURLOPT_WRITEFUNCTION, undef);
118 }
119  
120 my $finish = sub {
121 my ($easy, $result) = @_;
122  
123 if ($_headers) {
124 ($is_success, $headers, $redirects) = _headers($easy, $url, $_headers) unless $headers;
125 if ($result == CURLE_WRITE_ERROR and $aborted_by_max_size) {
126 $is_success = 0;
127 $$headers{"Status"} = 599;
128 $$headers{"Reason"} = "MaxSize";
129 } elsif ($result == CURLE_OPERATION_TIMEDOUT) {
130 $is_success = 0;
131 $$headers{"Status"} = 599;
132 $$headers{"Reason"} = "Timeout";
133 } elsif ($result == CURLE_RECV_ERROR) {
134 $is_success = 0;
135 $$headers{"Status"} = 599;
136 $$headers{"Reason"} = "$result";
137 }
138 $easy = undef;
139 return ($is_success, $body, $headers, $redirects);
140 } else {
141 $easy = undef;
142 return (0, undef, { Status => 500, Reason => "$result", URL => $url }, []);
143 }
144  
145 };
146  
147 return $finish;
148 }
149  
150  
151 sub _do_http {
152 my ($easy, $url, $opt) = @_;
153  
154 my $finish = _prepare($easy, $url, $opt);
155  
156 eval { $easy->perform() };
157 if ($@) {
158 if (ref $@ eq "Net::Curl::Easy::Code" ) {
159 return $finish->($easy, $@);
160 } else {
161 die $@;
162 }
163 } else {
164 return $finish->($easy, CURLE_OK);
165 }
166 }
167  
168  
169 sub do_http {
170 if (@_ == 5) {
171 my ($multi_ev, $easy, $url, $opt, $cb) = @_;
172 my $finish = _prepare($easy, $url, $opt);
173 if ($multi_ev) {
174 $multi_ev->($easy, sub { $cb->($finish->(@_)) }, 4 * 60);
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;
241  
242 __END__