blib/lib/HTTP/Lite.pm

filter: Statementstmt 11.9% 49 / 411 Branchbran 2.0% 4 / 198 Conditioncond 0.0% 0 / 53 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 27.0% 10 / 37 Podpod 64.5% 20 / 31 totaltotal 10.8% 83 / 763 CC 123 SCAR 75.3
CC
123
Cov
8%
request
4422.0
http_readbytes
110.0
add_to_body
90.0
CRAP 1862.1
↑ Summary
1 package HTTP::Lite;
2  
3 1 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Socket 1.3;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Errno qw(EAGAIN);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '2.44';
11  
12 1 my $BLOCKSIZE = 65536;
13 1 my $CRLF = "\r\n";
14 1 my $URLENCODE_VALID = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.";
15  
16 # Forward declarations
17 sub prepare_post;
18 sub http_write;
19 sub http_readline;
20 sub http_read;
21 sub http_readbytes;
22  
23 # Prepare the urlencode validchars lookup hash
24 1 my @urlencode_valid;
25 1 foreach my $char (split('', $URLENCODE_VALID)) {
26 65 $urlencode_valid[ord $char]=$char;
27 }
28 1 ▶for (my $n=0;$n<256;$n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 256) 256 1 257
29 256 ▶ if (!defined($urlencode_valid[$n])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $urlencode_valid[$n]) 191 65 256
30 191 $urlencode_valid[$n]=sprintf("%%%02X", $n);
31 }
32 }
33  
34 sub new
35 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
36 1 my $self = {};
37 1 bless $self;
38 1 $self->initialize();
39 1 return $self;
40 }
41  
42 sub initialize
43 1 ▶{
Subroutine initialize: called CC=1 SCAR=0.0
Pod undocumented
44 1 my $self = shift;
45 1 $self->reset;
46 1 $self->{timeout} = 120;
47 1 $self->{HTTP11} = 0;
48 1 $self->{DEBUG} = 0;
49 1 $self->{header_at_once} = 0;
50 1 $self->{holdback} = 0; # needed for http_write
51 }
52  
53 sub header_at_once
54 0 ▶{
Subroutine header_at_once: not called CC=1 SCAR=6.9
Pod undocumented
55 0 my $self=shift;
56 0 $self->{header_at_once} = 1;
57 }
58  
59 sub local_addr
60 0 ▶{
Subroutine local_addr: not called CC=2 SCAR=17.9
Pod documented
61 0 my $self = shift;
62 0 my $val = shift;
63 0 my $oldval = $self->{'local_addr'};
64 0 ▶ if (defined($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
65 0 $self->{'local_addr'} = $val;
66 }
67 0 return $oldval;
68 }
69  
70 sub local_port
71 0 ▶{
Subroutine local_port: not called CC=2 SCAR=17.9
Pod documented
72 0 my $self = shift;
73 0 my $val = shift;
74 0 my $oldval = $self->{'local_port'};
75 0 ▶ if (defined($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
76 0 $self->{'local_port'} = $val;
77 }
78 0 return $oldval;
79 }
80  
81 sub method
82 0 ▶{
Subroutine method: not called CC=1 SCAR=6.9
Pod documented
83 0 my $self = shift;
84 0 my $method = shift;
85 0 $method = uc($method);
86 0 $self->{method} = $method;
87 }
88  
89 sub DEBUG
90 0 ▶{
Subroutine DEBUG: not called CC=2 SCAR=17.9
Pod undocumented
91 0 my $self = shift;
92 0 ▶ if ($self->{DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DEBUG"}) 0 0 0
93 0 print STDERR join(" ", @_),"\n";
94 }
95 }
96  
97 sub reset
98 1 ▶{
Subroutine reset: called CC=2 SCAR=6.9
Pod documented
99 1 my $self = shift;
100 1 foreach my $var ("body", "request", "content", "status", "proxy",
101 "proxyport", "resp-protocol", "error-message",
102 "resp-headers", "CBARGS", "callback_function", "callback_params")
103 {
104 12 $self->{$var} = undef;
105 }
106 1 $self->{HTTPReadBuffer} = "";
107 1 $self->{method} = "GET";
108 1 $self->{headers} = { 'user-agent' => "HTTP::Lite/$VERSION" };
109 1 $self->{headermap} = { 'user-agent' => 'User-Agent' };
110 }
111  
112  
113 # URL-encode data
114 0 ▶sub escape {
Subroutine escape: not called CC=1 SCAR=6.9
Pod undocumented
115 0 my $toencode = shift;
116 return join('',
117 0 map { $urlencode_valid[ord $_] } split('', $toencode));
118 }
119  
120 0 ▶sub set_callback {
Subroutine set_callback: not called CC=1 SCAR=6.9
Pod documented
121 0 my ($self, $callback, @callbackparams) = @_;
122 0 $self->{'callback_function'} = $callback;
123 0 $self->{'callback_params'} = [ @callbackparams ];
124 }
125  
126 sub request
127 0 ▶{
Subroutine request: not called CC=66 SCAR=83.9
Pod documented
128 0 my ($self, $url, $data_callback, $cbargs) = @_;
129
130 0 my $method = $self->{method};
131 0 ▶ if (defined($cbargs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cbargs) 0 0 0
132 0 $self->{CBARGS} = $cbargs;
133 }
134  
135 0 my $callback_func = $self->{'callback_function'};
136 0 my $callback_params = $self->{'callback_params'};
137  
138 # Parse URL
139 0 my ($protocol,$host,$junk,$port,$object) =
140 $url =~ m{^([^:/]+)://([^/:]*)(:(\d+))?(/?.*)$};
141  
142 # Only HTTP is supported here
143 0 ▶ if ($protocol ne "http")
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol ne "http") 0 0 0
144 {
145 0 warn "Only http is supported by HTTP::Lite";
146 0 return undef;
147 }
148
149 # Setup the connection
150 0 my $proto = getprotobyname('tcp');
151 0 local *FH;
152 0 socket(FH, PF_INET, SOCK_STREAM, $proto);
153 0 ▶ $port = 80 if !$port;
Branch: 1 branch
BranchTrueFalseTotal
unless $port 0 0 0
154  
155 0 ▶ my $connecthost = $self->{'proxy'} || $host;
Condition0%0/3
$self->{'proxy'} || $host
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'proxy'} || $host
$self->{'proxy'} $host
Truth table0%0/3
A || B
A= $self->{'proxy'}B= $host
ABresult
1X1
011
000
156 0 ▶ $connecthost = $connecthost ? $connecthost : $host;
Branch: 1 branch
BranchTrueFalseTotal
$connecthost ? : 0 0 0
157 0 ▶ my $connectport = $self->{'proxyport'} || $port;
Condition0%0/3
$self->{'proxyport'} || $port
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'proxyport'} || $port
$self->{'proxyport'} $port
Truth table0%0/3
A || B
A= $self->{'proxyport'}B= $port
ABresult
1X1
011
000
158 0 ▶ $connectport = $connectport ? $connectport : $port;
Branch: 1 branch
BranchTrueFalseTotal
$connectport ? : 0 0 0
159 0 my $addr = inet_aton($connecthost);
160 0 ▶ if (!$addr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($addr) 0 0 0
161 0 close(FH);
162 0 return undef;
163 }
164 0 ▶ if ($connecthost ne $host)
Branch: 1 branch
BranchTrueFalseTotal
if ($connecthost ne $host) 0 0 0
165 {
166 # if proxy active, use full URL as object to request
167 0 $object = "$url";
168 }
169  
170 # choose local port and address
171 0 my $local_addr = INADDR_ANY;
172 0 my $local_port = "0";
173 0 ▶ if (defined($self->{'local_addr'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"local_addr"}) 0 0 0
174 0 $local_addr = $self->{'local_addr'};
175 0 ▶ if ($local_addr eq "0.0.0.0" || $local_addr eq "0") {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_addr eq '0.0.0.0' or $local_addr eq '0') { } 0 0 0
Condition0%0/3
$local_addr eq "0.0.0.0" or $local_addr eq 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$local_addr eq "0.0.0.0" or $local_addr eq 0
$local_addr eq "0.0.0.0" $local_addr eq 0
Truth table0%0/3
A or B
A= $local_addr eq "0.0.0.0"B= $local_addr eq 0
ABresult
1X1
011
000
176 0 $local_addr = INADDR_ANY;
177 } else {
178 0 $local_addr = inet_aton($local_addr);
179 }
180 }
181 0 ▶ if (defined($self->{'local_port'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"local_port"}) 0 0 0
182 0 $local_port = $self->{'local_port'};
183 }
184 0 my $paddr = pack_sockaddr_in($local_port, $local_addr);
185 0 ▶ bind(FH, $paddr) || return undef; # Failing to bind is fatal.
Branch: 1 branch
BranchTrueFalseTotal
bind FH, $paddr or return undef 0 0 0
186  
187 0 my $sin = sockaddr_in($connectport,$addr);
188 0 ▶ connect(FH, $sin) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
connect FH, $sin or return undef 0 0 0
189 # Set nonblocking IO on the handle to allow timeouts
190 0 ▶ if ( $^O ne "MSWin32" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "MSWin32") 0 0 0
191 0 fcntl(FH, F_SETFL, O_NONBLOCK);
192 }
193  
194 0 ▶ if (defined($callback_func)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback_func) 0 0 0
195 0 &$callback_func($self, "connect", undef, @$callback_params);
196 }
197  
198 0 ▶ if ($self->{header_at_once}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"header_at_once"}) 0 0 0
199 0 $self->{holdback} = 1; # http_write should buffer only, no sending yet
200 }
201  
202 # Start the request (HTTP/1.1 mode)
203 0 ▶ if ($self->{HTTP11}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'HTTP11'}) { } 0 0 0
204 0 $self->http_write(*FH, "$method $object HTTP/1.1$CRLF");
205 } else {
206 0 $self->http_write(*FH, "$method $object HTTP/1.0$CRLF");
207 }
208  
209 # Add some required headers
210 # we only support a single transaction per request in this version.
211 0 $self->add_req_header("Connection", "close");
212 0 ▶ if ($port != 80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port != 80) { } 0 0 0
213 0 $self->add_req_header("Host", "$host:$port");
214 } else {
215 0 $self->add_req_header("Host", $host);
216 }
217 0 ▶ if (!defined($self->get_req_header("Accept"))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->get_req_header("Accept")) 0 0 0
218 0 $self->add_req_header("Accept", "*/*");
219 }
220  
221 0 ▶ if ($method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "POST") 0 0 0
222 0 $self->http_write(*FH, "Content-Type: application/x-www-form-urlencoded$CRLF");
223 }
224
225 # Purge a couple others
226 0 $self->delete_req_header("Content-Type");
227 0 $self->delete_req_header("Content-Length");
228
229 # Output headers
230 0 foreach my $header ($self->enum_req_headers())
231 {
232 0 my $value = $self->get_req_header($header);
233 0 $self->http_write(*FH, $self->{headermap}{$header}.": ".$value."$CRLF");
234 }
235
236 0 my $content_length;
237 0 ▶ if (defined($self->{content}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"content"}) 0 0 0
238 {
239 0 $content_length = length($self->{content});
240 }
241 0 ▶ if (defined($callback_func)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback_func) 0 0 0
242 0 my $ncontent_length = &$callback_func($self, "content-length", undef, @$callback_params);
243 0 ▶ if (defined($ncontent_length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ncontent_length) 0 0 0
244 0 $content_length = $ncontent_length;
245 }
246 }
247  
248 0 ▶ if ($content_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_length) 0 0 0
249 0 $self->http_write(*FH, "Content-Length: $content_length$CRLF");
250 }
251
252 0 ▶ if (defined($callback_func)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback_func) 0 0 0
253 0 &$callback_func($self, "done-headers", undef, @$callback_params);
254 }
255 # End of headers
256 0 $self->http_write(*FH, "$CRLF");
257
258 0 ▶ if ($self->{header_at_once}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"header_at_once"}) 0 0 0
259 0 $self->{holdback} = 0;
260 0 $self->http_write(*FH, ""); # pseudocall to get http_write going
261 }
262
263 0 my $content_out = 0;
264 0 ▶ if (defined($callback_func)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback_func) 0 0 0
265 0 ▶ while (my $content = &$callback_func($self, "content", undef, @$callback_params)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $content = &$callback_func($self, "content", undef, @$callback_params)) 0 0 0
266 0 $self->http_write(*FH, $content);
267 0 $content_out++;
268 }
269 }
270
271 # Output content, if any
272 0 ▶ if (!$content_out && defined($self->{content}))
Branch: 1 branch
BranchTrueFalseTotal
if (not $content_out and defined $self->{"content"}) 0 0 0
Condition0%0/3
not $content_out and defined $self->{"content"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $content_out and defined $self->{"content"}
not $content_out defined $self->{"content"}
Truth table0%0/3
not(A) and B
A= not $content_outB= defined $self->{"content"}
ABresult
0X0
100
111
273 {
274 0 $self->http_write(*FH, $self->{content});
275 }
276
277 0 ▶ if (defined($callback_func)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback_func) 0 0 0
278 0 &$callback_func($self, "content-done", undef, @$callback_params);
279 }
280  
281  
282 # Read response from server
283 0 my $headmode=1;
284 0 my $chunkmode=0;
285 0 my $chunksize=0;
286 0 my $chunklength=0;
287 0 my $chunk;
288 0 my $line = 0;
289 0 my $data;
290 0 ▶ while ($data = $self->http_read(*FH,$headmode,$chunkmode,$chunksize))
Branch: 1 branch
BranchTrueFalseTotal
if ($data = $self->http_read(*FH, $headmode, $chunkmode, $chunksize)) 0 0 0
291 {
292 $self->{DEBUG} && $self->DEBUG("reading: $chunkmode, $chunksize, $chunklength, $headmode, ".
293 0 ▶ length($self->{'body'}));
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and $self->DEBUG("reading: $chunkmode, $chunksize, $chunklength, $headmode, " . length($self->{"body"})) 0 0 0
294 0 ▶ if ($self->{DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DEBUG"}) 0 0 0
295 0 foreach my $var ("body", "request", "content", "status", "proxy",
296 "proxyport", "resp-protocol", "error-message",
297 "resp-headers", "CBARGS", "HTTPReadBuffer")
298 {
299 0 $self->DEBUG("state $var ".length($self->{$var}));
300 }
301 }
302 0 $line++;
303 0 ▶ if ($line == 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($line == 1) 0 0 0
304 {
305 0 my ($proto,$status,$message) = split(' ', $$data, 3);
306 0 ▶ $self->{DEBUG} && $self->DEBUG("header $$data");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and $self->DEBUG("header $$data") 0 0 0
307 0 $self->{status}=$status;
308 0 $self->{'resp-protocol'}=$proto;
309 0 $self->{'error-message'}=$message;
310 0 next;
311 }
312 0 ▶ if (($headmode || $chunkmode eq "entity-header") && $$data =~ /^[\r\n]*$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($headmode || $chunkmode eq "entity-header" and $$data =~ /^[\r\n]*$/) 0 0 0
Condition0%0/6
$headmode || $chunkmode eq "entity-header" and $$data =~ /^[\r\n]*$/
!ll&&!rl&&r
000
$headmode or $chunkmode eq "entity-header"
l!l&&r!l&&!r
000
MC/DC0%0/3
$headmode || $chunkmode eq "entity-header" and $$data =~ /^[\r\n]*$/
$headmode $chunkmode eq "entity-header" $$data =~ /^[\r\n]*$/
Truth table0%0/5
A or B and C
A= $headmodeB= $chunkmode eq "entity-header"C= $$data =~ /^[\r\n]*$/
ABCresult
00X0
1X00
0100
1X11
0111
313 {
314 0 ▶ if ($chunkmode)
Branch: 1 branch
BranchTrueFalseTotal
if ($chunkmode) 0 0 0
315 {
316 0 $chunkmode = 0;
317 }
318 0 $headmode = 0;
319
320 # Check for Transfer-Encoding
321 0 my $te = $self->get_header("Transfer-Encoding");
322 0 ▶ if (defined($te)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $te) 0 0 0
323 0 my $header = join(' ',@{$te});
324 0 ▶ if ($header =~ /chunked/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /chunked/i) 0 0 0
325 {
326 0 $chunkmode = "chunksize";
327 }
328 }
329 0 next;
330 }
331 0 ▶ if ($headmode || $chunkmode eq "entity-header")
Branch: 2 branches
BranchTrueFalseTotal
if ($headmode or $chunkmode eq 'entity-header') { } 0 0 0
elsif ($chunkmode) { } 0 0 0
Condition0%0/3
$headmode or $chunkmode eq "entity-header"
l!l&&r!l&&!r
000
MC/DC0%0/2
$headmode or $chunkmode eq "entity-header"
$headmode $chunkmode eq "entity-header"
Truth table0%0/3
A or B
A= $headmodeB= $chunkmode eq "entity-header"
ABresult
1X1
011
000
332 {
333 0 my ($var,$datastr) = $$data =~ /^([^:]*):\s*(.*)$/;
334 0 ▶ if (defined($var))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $var) 0 0 0
335 {
336 0 $datastr =~s/[\r\n]$//g;
337 0 $var = lc($var);
338 0 $var =~ s/^(.)/&upper($1)/ge;
339 0 $var =~ s/(-.)/&upper($1)/ge;
340 0 my $hr = ${$self->{'resp-headers'}}{$var};
341 0 ▶ if (!ref($hr))
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $hr) { } 0 0 0
342 {
343 0 $hr = [ $datastr ];
344 }
345 else
346 {
347 0 push @{ $hr }, $datastr;
348 }
349 0 ${$self->{'resp-headers'}}{$var} = $hr;
350 }
351 } elsif ($chunkmode)
352 {
353 0 ▶ if ($chunkmode eq "chunksize")
Branch: 3 branches
BranchTrueFalseTotal
if ($chunkmode eq 'chunksize') { } 0 0 0
elsif ($chunkmode eq 'chunk') { } 0 0 0
elsif ($chunkmode eq 'ignorecrlf') { } 0 0 0
354 {
355 0 $chunksize = $$data;
356 0 $chunksize =~ s/^\s*|;.*$//g;
357 0 $chunksize =~ s/\s*$//g;
358 0 my $cshx = $chunksize;
359 0 ▶ if (length($chunksize) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $chunksize > 0) { } 0 0 0
360 # read another line
361 0 ▶ if ($chunksize !~ /^[a-f0-9]+$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chunksize =~ /^[a-f0-9]+$/i) 0 0 0
362 0 ▶ $self->{DEBUG} && $self->DEBUG("chunksize not a hex string");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and $self->DEBUG("chunksize not a hex string") 0 0 0
363 }
364 0 $chunksize = hex($chunksize);
365 0 ▶ $self->{DEBUG} && $self->DEBUG("chunksize was $chunksize (HEX was $cshx)");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and $self->DEBUG("chunksize was $chunksize (HEX was $cshx)") 0 0 0
366 0 ▶ if ($chunksize == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($chunksize == 0) { } 0 0 0
367 {
368 0 $chunkmode = "entity-header";
369 } else {
370 0 $chunkmode = "chunk";
371 0 $chunklength = 0;
372 }
373 } else {
374 0 ▶ $self->{DEBUG} && $self->DEBUG("chunksize empty string, checking next line!");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and $self->DEBUG("chunksize empty string, checking next line!") 0 0 0
375 }
376 } elsif ($chunkmode eq "chunk")
377 {
378 0 $chunk .= $$data;
379 0 $chunklength += length($$data);
380 0 ▶ if ($chunklength >= $chunksize)
Branch: 1 branch
BranchTrueFalseTotal
if ($chunklength >= $chunksize) 0 0 0
381 {
382 0 $chunkmode = "chunksize";
383 0 ▶ if ($chunklength > $chunksize)
Branch: 2 branches
BranchTrueFalseTotal
if ($chunklength > $chunksize) { } 0 0 0
elsif ($chunklength == $chunksize and not $chunk =~ /$CRLF$/) { } 0 0 0
384 {
385 0 $chunk = substr($chunk,0,$chunksize);
386 }
387 ▶ elsif ($chunklength == $chunksize && $chunk !~ /$CRLF$/)
Condition0%0/3
$chunklength == $chunksize and not $chunk =~ /$CRLF$/
!ll&&!rl&&r
000
MC/DC0%0/2
$chunklength == $chunksize and not $chunk =~ /$CRLF$/
$chunklength == $chunksize not $chunk =~ /$CRLF$/
Truth table0%0/3
A and not(B)
A= $chunklength == $chunksizeB= not $chunk =~ /$CRLF$/
ABresult
0X0
100
111
388 {
389 # chunk data is exactly chunksize -- need CRLF still
390 0 $chunkmode = "ignorecrlf";
391 }
392 0 $self->add_to_body(\$chunk, $data_callback);
393 0 $chunk="";
394 0 $chunklength = 0;
395 0 $chunksize = "";
396 }
397 } elsif ($chunkmode eq "ignorecrlf")
398 {
399 0 $chunkmode = "chunksize";
400 }
401 } else {
402 0 $self->add_to_body($data, $data_callback);
403 }
404 }
405 0 ▶ if (defined($callback_func)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback_func) 0 0 0
406 0 &$callback_func($self, "done", undef, @$callback_params);
407 }
408 0 close(FH);
409 0 return $self->{status};
410 }
411  
412 sub add_to_body
413 0 ▶{
Subroutine add_to_body: not called CC=9 SCAR=45.0
Pod undocumented
414 0 my $self = shift;
415 0 my ($dataref, $data_callback) = @_;
416
417 0 my $callback_func = $self->{'callback_function'};
418 0 my $callback_params = $self->{'callback_params'};
419  
420 0 ▶ if (!defined($data_callback) && !defined($callback_func)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $data_callback || defined $callback_func) { } 0 0 0
Condition0%0/3
defined $data_callback || defined $callback_func
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $data_callback || defined $callback_func
defined $data_callback defined $callback_func
Truth table0%0/3
A || B
A= defined $data_callbackB= defined $callback_func
ABresult
1X1
011
000
421 0 ▶ $self->{DEBUG} && $self->DEBUG("no callback");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and $self->DEBUG("no callback") 0 0 0
422 0 $self->{'body'}.=$$dataref;
423 } else {
424 0 my $newdata;
425 0 ▶ if (defined($callback_func)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback_func) { } 0 0 0
426 0 $newdata = &$callback_func($self, "data", $dataref, @$callback_params);
427 } else {
428 0 $newdata = &$data_callback($self, $dataref, $self->{CBARGS});
429 }
430 0 ▶ if ($self->{DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DEBUG"}) 0 0 0
431 0 $self->DEBUG("callback got back a ".ref($newdata));
432 0 ▶ if (ref($newdata) eq "SCALAR") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $newdata eq "SCALAR") 0 0 0
433 0 $self->DEBUG("callback got back ".length($$newdata)." bytes");
434 }
435 }
436 0 ▶ if (defined($newdata) && ref($newdata) eq "SCALAR") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newdata and ref $newdata eq "SCALAR") 0 0 0
Condition0%0/3
defined $newdata and ref $newdata eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $newdata and ref $newdata eq "SCALAR"
defined $newdata ref $newdata eq "SCALAR"
Truth table0%0/3
A and B
A= defined $newdataB= ref $newdata eq "SCALAR"
ABresult
0X0
100
111
437 0 $self->{'body'} .= $$newdata;
438 }
439 }
440 }
441  
442 sub add_req_header
443 0 ▶{
Subroutine add_req_header: not called CC=3 SCAR=24.8
Pod documented
444 0 my $self = shift;
445 0 my ($header, $value) = @_;
446
447 0 ▶ return unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
448 0 my $lcheader = lc($header);
449 0 ▶ $self->{DEBUG} && $self->DEBUG("add_req_header $header $value");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and $self->DEBUG("add_req_header $header $value") 0 0 0
450 0 ${$self->{headers}}{$lcheader} = $value;
451 0 ${$self->{headermap}}{$lcheader} = $header;
452 }
453  
454 sub get_req_header
455 0 ▶{
Subroutine get_req_header: not called CC=1 SCAR=6.9
Pod documented
456 0 my $self = shift;
457 0 my ($header) = @_;
458
459 0 return $self->{headers}{lc($header)};
460 }
461  
462 sub delete_req_header
463 0 ▶{
Subroutine delete_req_header: not called CC=2 SCAR=17.9
Pod documented
464 0 my $self = shift;
465 0 my ($header) = @_;
466
467 0 my $exists;
468 0 ▶ if ($exists=defined(${$self->{headers}}{lc($header)}))
Branch: 1 branch
BranchTrueFalseTotal
if ($exists = defined ${$$self{"headers"};}{lc $header}) 0 0 0
469 {
470 0 delete ${$self->{headers}}{lc($header)};
471 0 delete ${$self->{headermap}}{lc($header)};
472 }
473 0 return $exists;
474 }
475  
476 sub enum_req_headers
477 0 ▶{
Subroutine enum_req_headers: not called CC=1 SCAR=6.9
Pod undocumented
478 0 my $self = shift;
479 0 my ($header) = @_;
480
481 0 my $exists;
482 0 return keys %{$self->{headermap}};
483 }
484  
485 sub body
486 0 ▶{
Subroutine body: not called CC=1 SCAR=6.9
Pod documented
487 0 my $self = shift;
488 0 return $self->{'body'};
489 }
490  
491 sub status
492 0 ▶{
Subroutine status: not called CC=1 SCAR=6.9
Pod documented
493 0 my $self = shift;
494 0 return $self->{status};
495 }
496  
497 sub protocol
498 0 ▶{
Subroutine protocol: not called CC=1 SCAR=6.9
Pod documented
499 0 my $self = shift;
500 0 return $self->{'resp-protocol'};
501 }
502  
503 sub status_message
504 0 ▶{
Subroutine status_message: not called CC=1 SCAR=6.9
Pod documented
505 0 my $self = shift;
506 0 return $self->{'error-message'};
507 }
508  
509 sub proxy
510 0 ▶{
Subroutine proxy: not called CC=4 SCAR=30.0
Pod documented
511 0 my $self = shift;
512 0 my ($value) = @_;
513
514 # Parse URL
515 0 my ($protocol,$host,$junk,$port,$object) =
516 $value =~ m{^(\S+)://([^/:]*)(:(\d+))?(/.*)$};
517 0 ▶ if (!$host)
Branch: 1 branch
BranchTrueFalseTotal
unless ($host) 0 0 0
518 {
519 0 ($host,$port) = $value =~ /^([^:]+):(.*)$/;
520 }
521  
522 0 ▶ $self->{'proxy'} = $host || $value;
Condition0%0/3
$host || $value
l!l&&r!l&&!r
000
MC/DC0%0/2
$host || $value
$host $value
Truth table0%0/3
A || B
A= $hostB= $value
ABresult
1X1
011
000
523 0 ▶ $self->{'proxyport'} = $port || 80;
Condition0%0/2
$port || 80
l!l
00
MC/DC0%0/1
$port || 80
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
524 }
525  
526 sub headers_array
527 0 ▶{
Subroutine headers_array: not called CC=3 SCAR=24.8
Pod documented
528 0 my $self = shift;
529
530 0 my @array = ();
531
532 0 foreach my $header (keys %{$self->{'resp-headers'}})
533 {
534 0 my $aref = ${$self->{'resp-headers'}}{$header};
535 0 foreach my $value (@$aref)
536 {
537 0 push @array, "$header: $value";
538 }
539 }
540 0 return @array;
541 }
542  
543 sub headers_string
544 0 ▶{
Subroutine headers_string: not called CC=3 SCAR=24.8
Pod documented
545 0 my $self = shift;
546
547 0 my $string = "";
548
549 0 foreach my $header (keys %{$self->{'resp-headers'}})
550 {
551 0 my $aref = ${$self->{'resp-headers'}}{$header};
552 0 foreach my $value (@$aref)
553 {
554 0 $string .= "$header: $value\n";
555 }
556 }
557 0 return $string;
558 }
559  
560 sub get_header
561 0 ▶{
Subroutine get_header: not called CC=1 SCAR=6.9
Pod documented
562 0 my $self = shift;
563 0 my $header = shift;
564  
565 0 return $self->{'resp-headers'}{$header};
566 }
567  
568 sub http11_mode
569 1 ▶{
Subroutine http11_mode: called CC=1 SCAR=0.0
Pod documented
570 1 my $self = shift;
571 1 my $mode = shift;
572  
573 1 $self->{HTTP11} = $mode;
574 }
575  
576 sub prepare_post
577 0 ▶{
Subroutine prepare_post: not called CC=5 SCAR=34.0
Pod documented
578 0 my $self = shift;
579 0 my $varref = shift;
580
581 0 my $body = "";
582 0 ▶ while (my ($var,$value) = map { escape($_) } each %$varref)
Branch: 1 branch
BranchTrueFalseTotal
if (() = map({escape($_);} each %$varref)) 0 0 0
583 {
584 0 ▶ if ($body)
Branch: 1 branch
BranchTrueFalseTotal
if ($body) { } 0 0 0
585 {
586 0 $body .= "&$var=$value";
587 } else {
588 0 $body = "$var=$value";
589 }
590 }
591 0 $self->{content} = $body;
592 $self->{headers}{'Content-Type'} = "application/x-www-form-urlencoded"
593 unless defined ($self->{headers}{'Content-Type'}) and
594 0 ▶ $self->{headers}{'Content-Type'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"headers"}{"Content-Type"} and $self->{"headers"}{"Content-Type"} 0 0 0
Condition0%0/3
defined $self->{"headers"}{"Content-Type"} and $self->{"headers"}{"Content-Type"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"headers"}{"Content-Type"} and $self->{"headers"}{"Content-Type"}
defined $self->{"headers"}{"Content-Type"} $self->{"headers"}{"Content-Type"}
Truth table0%0/3
A and B
A= defined $self->{"headers"}{"Content-Type"}B= $self->{"headers"}{"Content-Type"}
ABresult
0X0
100
111
595 0 $self->{method} = "POST";
596 }
597  
598 sub http_write
599 0 ▶{
Subroutine http_write: not called CC=6 SCAR=37.4
Pod undocumented
600 0 my $self = shift;
601 0 my ($fh,$line) = @_;
602  
603 0 ▶ if ($self->{holdback}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'holdback'}) { } 0 0 0
604 0 $self->{HTTPWriteBuffer} .= $line;
605 0 return;
606 } else {
607 0 ▶ if (defined $self->{HTTPWriteBuffer}) { # copy previously buffered, if any
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"HTTPWriteBuffer"}) 0 0 0
608 0 $line = $self->{HTTPWriteBuffer} . $line;
609 }
610 }
611  
612 0 my $size = length($line);
613 0 my $total_sent = 0;
614 0 my $nbytes;
615 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$total_sent < $size and do { do { $nbytes = syswrite($fh, $line, $size - $total_sent, $total_sent); die $! unless defined $nbytes or $!{"EAGAIN"}; $total_sent += $nbytes } } 0 0 0
616 0 $nbytes = syswrite($fh, $line, $size - $total_sent, $total_sent );
617 0 ▶ die $! unless(defined($nbytes) || $!{EAGAIN}); # non-recoverable error occurred!
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nbytes or $!{"EAGAIN"} 0 0 0
Condition0%0/3
defined $nbytes or $!{"EAGAIN"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $nbytes or $!{"EAGAIN"}
defined $nbytes $!{"EAGAIN"}
Truth table0%0/3
A or B
A= defined $nbytesB= $!{"EAGAIN"}
ABresult
1X1
011
000
618 0 $total_sent += $nbytes;
619 } while ($total_sent < $size);
620 }
621
622 sub http_read
623 0 ▶{
Subroutine http_read: not called CC=9 SCAR=45.0
Pod undocumented
624 0 my $self = shift;
625 0 my ($fh,$headmode,$chunkmode,$chunksize) = @_;
626  
627 0 ▶ $self->{DEBUG} && $self->DEBUG("read handle=$fh, headm=$headmode, chunkm=$chunkmode, chunksize=$chunksize");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and $self->DEBUG("read handle=$fh, headm=$headmode, chunkm=$chunkmode, chunksize=$chunksize") 0 0 0
628  
629 0 my $res;
630 0 ▶ if (($headmode == 0 && $chunkmode eq "0") || ($chunkmode eq "chunk")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headmode == 0 and $chunkmode eq '0' or $chunkmode eq 'chunk') { } 0 0 0
Condition0%0/6
$headmode == 0 and $chunkmode eq 0 or $chunkmode eq "chunk"
l!l&&r!l&&!r
000
$headmode == 0 and $chunkmode eq 0
!ll&&!rl&&r
000
MC/DC0%0/3
$headmode == 0 and $chunkmode eq 0 or $chunkmode eq "chunk"
$headmode == 0 $chunkmode eq 0 $chunkmode eq "chunk"
Truth table0%0/5
A and B or C
A= $headmode == 0B= $chunkmode eq 0C= $chunkmode eq "chunk"
ABCresult
11X1
0X11
1011
0X00
1000
631 0 ▶ my $bytes_to_read = $chunkmode eq "chunk" ?
Branch: 2 branches
BranchTrueFalseTotal
$chunkmode eq 'chunk' ? : 0 0 0
$chunksize < $BLOCKSIZE ? : 0 0 0
632 ($chunksize < $BLOCKSIZE ? $chunksize : $BLOCKSIZE) :
633 $BLOCKSIZE;
634 0 $res = $self->http_readbytes($fh,$self->{timeout},$bytes_to_read);
635 } else {
636 0 $res = $self->http_readline($fh,$self->{timeout});
637 }
638 0 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
639 0 ▶ if ($self->{DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DEBUG"}) 0 0 0
640 0 $self->DEBUG("read got ".length($$res)." bytes");
641 0 my $str = $$res;
642 0 $str =~ s{([\x00-\x1F\x7F-\xFF])}{.}g;
643 0 $self->DEBUG("read: ".$str);
644 }
645 }
646 0 return $res;
647 }
648  
649 sub http_readline
650 0 ▶{
Subroutine http_readline: not called CC=9 SCAR=45.0
Pod undocumented
651 0 my $self = shift;
652 0 my ($fh, $timeout) = @_;
653 0 my $EOL = "\n";
654  
655 0 ▶ $self->{DEBUG} && $self->DEBUG("readline handle=$fh, timeout=$timeout");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and $self->DEBUG("readline handle=$fh, timeout=$timeout") 0 0 0
656
657 # is there a line in the buffer yet?
658 0 ▶ while ($self->{HTTPReadBuffer} !~ /$EOL/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"HTTPReadBuffer"} =~ /$EOL/) 0 0 0
659 {
660 # nope -- wait for incoming data
661 0 my ($inbuf,$bits,$chars) = ("","",0);
662 0 vec($bits,fileno($fh),1)=1;
663 0 my $nfound = select($bits, undef, $bits, $timeout);
664 0 ▶ if ($nfound == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound == 0) { } 0 0 0
665 {
666 # Timed out
667 0 return undef;
668 } else {
669 # Get the data
670 0 $chars = sysread($fh, $inbuf, $BLOCKSIZE);
671 0 ▶ $self->{DEBUG} && $self->DEBUG("sysread $chars bytes");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and $self->DEBUG("sysread $chars bytes") 0 0 0
672 }
673 # End of stream?
674 0 ▶ if ($chars <= 0 && !$!{EAGAIN})
Branch: 1 branch
BranchTrueFalseTotal
if ($chars <= 0 and not $!{"EAGAIN"}) 0 0 0
Condition0%0/3
$chars <= 0 and not $!{"EAGAIN"}
!ll&&!rl&&r
000
MC/DC0%0/2
$chars <= 0 and not $!{"EAGAIN"}
$chars <= 0 not $!{"EAGAIN"}
Truth table0%0/3
A and not(B)
A= $chars <= 0B= not $!{"EAGAIN"}
ABresult
0X0
100
111
675 {
676 0 last;
677 }
678 # tag data onto end of buffer
679 0 $self->{HTTPReadBuffer}.=$inbuf;
680 }
681 # get a single line from the buffer
682 0 my $nlat = index($self->{HTTPReadBuffer}, $EOL);
683 0 my $newline;
684 my $oldline;
685 0 ▶ if ($nlat > -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($nlat > -1) { } 0 0 0
686 {
687 0 $newline = substr($self->{HTTPReadBuffer},0,$nlat+1);
688 0 $oldline = substr($self->{HTTPReadBuffer},$nlat+1);
689 } else {
690 0 $newline = substr($self->{HTTPReadBuffer},0);
691 0 $oldline = "";
692 }
693 # and update the buffer
694 0 $self->{HTTPReadBuffer}=$oldline;
695 0 ▶ return length($newline) ? \$newline : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $newline ? : 0 0 0
696 }
697  
698 sub http_readbytes
699 0 ▶{
Subroutine http_readbytes: not called CC=10 SCAR=47.0
Pod undocumented
700 0 my $self = shift;
701 0 my ($fh, $timeout, $bytes) = @_;
702 0 my $EOL = "\n";
703  
704 0 ▶ $self->{DEBUG} && $self->DEBUG("readbytes handle=$fh, timeout=$timeout, bytes=$bytes");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and $self->DEBUG("readbytes handle=$fh, timeout=$timeout, bytes=$bytes") 0 0 0
705
706 # is there enough data in the buffer yet?
707 0 ▶ while (length($self->{HTTPReadBuffer}) < $bytes)
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"HTTPReadBuffer"} < $bytes) 0 0 0
708 {
709 # nope -- wait for incoming data
710 0 my ($inbuf,$bits,$chars) = ("","",0);
711 0 vec($bits,fileno($fh),1)=1;
712 0 my $nfound = select($bits, undef, $bits, $timeout);
713 0 ▶ if ($nfound == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound == 0) { } 0 0 0
714 {
715 # Timed out
716 0 return undef;
717 } else {
718 # Get the data
719 0 $chars = sysread($fh, $inbuf, $BLOCKSIZE);
720 0 ▶ $self->{DEBUG} && $self->DEBUG("sysread $chars bytes");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and $self->DEBUG("sysread $chars bytes") 0 0 0
721 }
722 # End of stream?
723 0 ▶ if ($chars <= 0 && !$!{EAGAIN})
Branch: 1 branch
BranchTrueFalseTotal
if ($chars <= 0 and not $!{"EAGAIN"}) 0 0 0
Condition0%0/3
$chars <= 0 and not $!{"EAGAIN"}
!ll&&!rl&&r
000
MC/DC0%0/2
$chars <= 0 and not $!{"EAGAIN"}
$chars <= 0 not $!{"EAGAIN"}
Truth table0%0/3
A and not(B)
A= $chars <= 0B= not $!{"EAGAIN"}
ABresult
0X0
100
111
724 {
725 0 last;
726 }
727 # tag data onto end of buffer
728 0 $self->{HTTPReadBuffer}.=$inbuf;
729 }
730 0 my $newline;
731 my $buflen;
732 0 ▶ if (($buflen=length($self->{HTTPReadBuffer})) >= $bytes)
Branch: 1 branch
BranchTrueFalseTotal
if (($buflen = length $self->{'HTTPReadBuffer'}) >= $bytes) { } 0 0 0
733 {
734 0 $newline = substr($self->{HTTPReadBuffer},0,$bytes+1);
735 0 ▶ if ($bytes+1 < $buflen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes + 1 < $buflen) { } 0 0 0
736 0 $self->{HTTPReadBuffer} = substr($self->{HTTPReadBuffer},$bytes+1);
737 } else {
738 0 $self->{HTTPReadBuffer} = "";
739 }
740 } else {
741 0 $newline = substr($self->{HTTPReadBuffer},0);
742 0 $self->{HTTPReadBuffer} = "";
743 }
744 0 ▶ return length($newline) ? \$newline : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $newline ? : 0 0 0
745 }
746  
747 sub upper
748 0 ▶{
Subroutine upper: not called CC=2 SCAR=17.9
Pod undocumented
749 0 my ($str) = @_;
750 0 ▶ if (defined($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) { } 0 0 0
751 0 return uc($str);
752 } else {
753 0 return undef;
754 }
755 }
756  
757 1 1;
758  
759 __END__
↑ Summary