CPANIDX/HTTP.pm

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