blib/lib/LWP/Protocol/http/SocksChain10.pm

filter: Statementstmt 12.9% 18 / 139 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 30.0% 3 / 10 Podpod 100% 1 / 1 totaltotal 6.9% 22 / 319 CC 62 SCAR 75.0
CC
62
Cov
7%
request
2550.0
_new_socket
30.0
__ANON__
30.0
CRAP 1801.4
↑ Summary
1 ########################################################################
2 #
3 # $Id: SocksChain10.pm,v 1.7 2009-12-28 15:12:16 gosha Exp $
4 #
5 # Copyright (C) Igor V. Okunev gosha<at>prv.mts-nn.ru 2005 - 2009
6 #
7 # All rights reserved. This library is free software;
8 # you can redistribute it and/or modify it under the
9 # same terms as Perl itself.
10 #
11 ########################################################################
12  
13 package LWP::Protocol::http::SocksChain10;
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use vars qw( @ISA $VERSION @EXTRA_SOCK_OPTS );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Net::SC;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 require HTTP::Response;
21 1 require HTTP::Status;
22 1 require IO::Select;
23 1 require LWP::Protocol;
24  
25 1 ($VERSION='$Revision: 1.7 $')=~s/^\S+\s+(\S+)\s+.*/$1/;
26  
27 1 local $^W = 1;
28  
29 1 @ISA = qw(LWP::Protocol);
30  
31 1 my $CRLF = "\015\012";
32  
33 sub _new_socket
34 0 ▶{
Subroutine _new_socket: not called CC=5 SCAR=34.0
35 0 my($self, $host, $port, $timeout ) = @_;
36  
37  
38 0 ▶ my $sc = Net::SC->new( TimeOut => $timeout,
Condition0%0/2
'Net::SC'->new('TimeOut', $timeout, $self->_extra_sock_opts($host, $port)) || die($!)
l!l
00
MC/DC0%0/1
'Net::SC'->new('TimeOut', $timeout, $self->_extra_sock_opts($host, $port)) || die($!)
'Net::SC'->new('TimeOut', $timeout, $self->_extra_sock_opts($host, $port))
Truth table0%0/2
A
A= 'Net::SC'->new('TimeOut', $timeout, $self->_extra_sock_opts($host, $port))
Aresult
11
00
39 $self->_extra_sock_opts($host, $port),
40 ) || die $!;
41  
42 0 ▶ unless ( ( my $rc = $sc->connect( $host, $port ) ) == SOCKS_OKAY ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((my $rc = $sc->connect($host, $port)) == SOCKS_OKAY()) 0 0 0
43 0 die socks_error($rc) . "\n";
44 }
45  
46 0 ▶ my $sock = $sc->sh || die $!;
Condition0%0/2
$sc->sh || die($!)
l!l
00
MC/DC0%0/1
$sc->sh || die($!)
$sc->sh
Truth table0%0/2
A
A= $sc->sh
Aresult
11
00
47  
48 0 ▶ unless ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 0 0 0
49 # IO::Socket leaves additional error messages in $@
50 0 $@ =~ s/^.*?: //;
51 0 die "Can't connect to $host:$port ($@)";
52 }
53  
54 # perl 5.005's IO::Socket does not have the blocking method.
55 0 eval { $sock->blocking(0); };
56  
57 0 return $sock;
58 }
59  
60 sub _extra_sock_opts # to be overridden by subclass
61 0 ▶{
Subroutine _extra_sock_opts: not called CC=1 SCAR=6.9
62 0 return @EXTRA_SOCK_OPTS;
63 }
64  
65  
66 sub _check_sock
67 0 ▶{
Subroutine _check_sock: not called CC=1 SCAR=0.0
68 #my($self, $req, $sock) = @_;
69 }
70  
71 sub _get_sock_info
72 0 ▶{
Subroutine _get_sock_info: not called CC=2 SCAR=17.9
73 0 my($self, $res, $sock) = @_;
74  
75 0 ▶ if (defined(my $peerhost = $sock->peerhost)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $peerhost = $sock->peerhost)) 0 0 0
76 0 $res->header("Client-Peer" => "$peerhost:" . $sock->peerport);
77 }
78 }
79  
80 sub _fixup_header
81 0 ▶{
Subroutine _fixup_header: not called CC=4 SCAR=30.0
82 0 my($self, $h, $url) = @_;
83  
84 0 $h->remove_header('Connection'); # need support here to be useful
85  
86 # HTTP/1.1 will require us to send the 'Host' header, so we might
87 # as well start now.
88 0 my $hhost = $url->authority;
89
90 0 $hhost =~ s/^([^\@]*)\@//; # get rid of potential "user:pass@"
91 0 ▶ $h->header('Host' => $hhost) unless defined $h->header('Host');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h->header("Host") 0 0 0
92  
93 # add authorization header if we need them. HTTP URLs do
94 # not really support specification of user and password, but
95 # we allow it.
96 0 ▶ if (defined($1) && not $h->header('Authorization')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1 and not $h->header("Authorization")) 0 0 0
Condition0%0/3
defined $1 and not $h->header("Authorization")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $1 and not $h->header("Authorization")
defined $1 not $h->header("Authorization")
Truth table0%0/3
A and not(B)
A= defined $1B= not $h->header("Authorization")
ABresult
0X0
100
111
97 0 require URI::Escape;
98 0 $h->authorization_basic(map URI::Escape::uri_unescape($_),
99 split(":", $1, 2));
100 }
101 }
102  
103  
104 sub request
105 0 ▶{
Subroutine request: not called CC=50 SCAR=78.4
Pod documented
106 0 my($self, $request, undef, $arg, $size, $timeout) = @_;
107  
108 0 ▶ $size ||= 4096;
Condition0%0/2
$size ||= 4096
l!l
00
MC/DC0%0/1
$size ||= 4096
$size
Truth table0%0/2
A
A= $size
Aresult
11
00
109  
110 # check method
111 0 my $method = $request->method;
112  
113 0 ▶ unless ($method =~ /^[A-Za-z0-9_!\#\$%&\'*+\-.^\`|~]+$/) { # HTTP token
Branch: 1 branch
BranchTrueFalseTotal
unless ($method =~ /^[A-Za-z0-9_!\#\$%&\'*+\-.^\`|~]+$/) 0 0 0
114 0 return new HTTP::Response &HTTP::Status::RC_BAD_REQUEST,
115 'Library does not allow method ' .
116 "$method for 'http:' URLs";
117 }
118  
119 0 my $url = $request->url;
120  
121 0 my $host = $url->host;
122 0 my $port = $url->port;
123 0 my $fullpath = $url->path_query;
124  
125 0 ▶ $fullpath = "/" unless length $fullpath;
Branch: 1 branch
BranchTrueFalseTotal
unless length $fullpath 0 0 0
126  
127 # connect to remote site
128 0 my $socket = $self->_new_socket( $host, $port, $timeout );
129  
130 0 $self->_check_sock($request, $socket);
131  
132 0 ▶ my $sel = IO::Select->new($socket) if $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout 0 0 0
133  
134 0 my $request_line = "$method $fullpath HTTP/1.0$CRLF";
135  
136 0 my $h = $request->headers->clone;
137 0 my $cont_ref = $request->content_ref;
138  
139 0 ▶ $cont_ref = $$cont_ref if ref($$cont_ref);
Branch: 1 branch
BranchTrueFalseTotal
if ref $$cont_ref 0 0 0
140
141 0 my $ctype = ref($cont_ref);
142  
143 # If we're sending content we *have* to specify a content length
144 # otherwise the server won't know a messagebody is coming.
145 0 ▶ if ($ctype eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctype eq 'CODE') { } 0 0 0
146 0 ▶ die 'No Content-Length header for request with dynamic content'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h->header("Content-Length") or $h->content_type =~ m[^multipart/] 0 0 0
Condition0%0/3
defined $h->header("Content-Length") or $h->content_type =~ m[^multipart/]
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $h->header("Content-Length") or $h->content_type =~ m[^multipart/]
defined $h->header("Content-Length") $h->content_type =~ m[^multipart/]
Truth table0%0/3
A or B
A= defined $h->header("Content-Length")B= $h->content_type =~ m[^multipart/]
ABresult
1X1
011
000
147 unless defined($h->header('Content-Length')) ||
148 $h->content_type =~ /^multipart\//;
149 # For HTTP/1.1 we could have used chunked transfer encoding...
150 } else {
151 0 ▶ $h->header('Content-Length' => length $$cont_ref)
Branch: 1 branch
BranchTrueFalseTotal
if defined $$cont_ref and length $$cont_ref 0 0 0
Condition0%0/3
defined $$cont_ref and length $$cont_ref
!ll&&!rl&&r
000
MC/DC0%0/2
defined $$cont_ref and length $$cont_ref
defined $$cont_ref length $$cont_ref
Truth table0%0/3
A and B
A= defined $$cont_refB= length $$cont_ref
ABresult
0X0
100
111
152 if defined($$cont_ref) && length($$cont_ref);
153 }
154  
155 0 $self->_fixup_header($h, $url);
156  
157 0 my $buf = $request_line . $h->as_string($CRLF) . $CRLF;
158 0 my $n; # used for return value from syswrite/sysread
159 my $length;
160 0 my $offset;
161  
162 # syswrite $buf
163 0 $length = length($buf);
164 0 $offset = 0;
165
166 0 ▶ while ( $offset < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $length) 0 0 0
167 0 ▶ die "write timeout" if $timeout && !$sel->can_write($timeout);
Branch: 1 branch
BranchTrueFalseTotal
if $timeout and not $sel->can_write($timeout) 0 0 0
Condition0%0/3
$timeout and not $sel->can_write($timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
$timeout and not $sel->can_write($timeout)
$timeout not $sel->can_write($timeout)
Truth table0%0/3
A and not(B)
A= $timeoutB= not $sel->can_write($timeout)
ABresult
0X0
100
111
168 0 $n = $socket->syswrite($buf, $length-$offset, $offset );
169 0 ▶ die $! unless defined($n);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
170 0 $offset += $n;
171 }
172
173 0 ▶ if ($ctype eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ctype eq 'CODE') { } 0 0 0
elsif (defined $$cont_ref and length $$cont_ref) { } 0 0 0
174 0 ▶ while ( ($buf = &$cont_ref()), defined($buf) && length($buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf = &$cont_ref(), defined $buf && length $buf) 0 0 0
Condition0%0/3
defined $buf && length $buf
!ll&&!rl&&r
000
MC/DC0%0/2
defined $buf && length $buf
defined $buf length $buf
Truth table0%0/3
A && B
A= defined $bufB= length $buf
ABresult
0X0
100
111
175 # syswrite $buf
176 0 $length = length($buf);
177 0 $offset = 0;
178 0 ▶ while ( $offset < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $length) 0 0 0
179 0 ▶ die "write timeout" if $timeout && !$sel->can_write($timeout);
Branch: 1 branch
BranchTrueFalseTotal
if $timeout and not $sel->can_write($timeout) 0 0 0
Condition0%0/3
$timeout and not $sel->can_write($timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
$timeout and not $sel->can_write($timeout)
$timeout not $sel->can_write($timeout)
Truth table0%0/3
A and not(B)
A= $timeoutB= not $sel->can_write($timeout)
ABresult
0X0
100
111
180 0 $n = $socket->syswrite($buf, $length-$offset, $offset );
181 0 ▶ die $! unless defined($n);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
182 0 $offset += $n;
183 }
184 }
185 ▶ } elsif (defined($$cont_ref) && length($$cont_ref)) {
Condition0%0/3
defined $$cont_ref and length $$cont_ref
!ll&&!rl&&r
000
MC/DC0%0/2
defined $$cont_ref and length $$cont_ref
defined $$cont_ref length $$cont_ref
Truth table0%0/3
A and B
A= defined $$cont_refB= length $$cont_ref
ABresult
0X0
100
111
186 # syswrite $$cont_ref
187 0 $length = length($$cont_ref);
188 0 $offset = 0;
189 0 ▶ while ( $offset < $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $length) 0 0 0
190 0 ▶ die "write timeout" if $timeout && !$sel->can_write($timeout);
Branch: 1 branch
BranchTrueFalseTotal
if $timeout and not $sel->can_write($timeout) 0 0 0
Condition0%0/3
$timeout and not $sel->can_write($timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
$timeout and not $sel->can_write($timeout)
$timeout not $sel->can_write($timeout)
Truth table0%0/3
A and not(B)
A= $timeoutB= not $sel->can_write($timeout)
ABresult
0X0
100
111
191 0 $n = $socket->syswrite($$cont_ref, $length-$offset, $offset );
192 0 ▶ die $! unless defined($n);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
193 0 $offset += $n;
194 }
195 }
196  
197 # read response line from server
198  
199 0 my $response;
200 0 $buf = '';
201  
202 # Inside this loop we will read the response line and all headers
203 # found in the response.
204 0 while (1) {
205 0 ▶ die "read timeout" if $timeout && !$sel->can_read($timeout);
Branch: 1 branch
BranchTrueFalseTotal
if $timeout and not $sel->can_read($timeout) 0 0 0
Condition0%0/3
$timeout and not $sel->can_read($timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
$timeout and not $sel->can_read($timeout)
$timeout not $sel->can_read($timeout)
Truth table0%0/3
A and not(B)
A= $timeoutB= not $sel->can_read($timeout)
ABresult
0X0
100
111
206 0 $n = $socket->sysread($buf, $size, length($buf));
207 0 ▶ die $! unless defined($n);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
208 0 ▶ die "unexpected EOF before status line seen" unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
209  
210 0 ▶ if ($buf =~ s/^(HTTP\/\d+\.\d+)[ \t]+(\d+)[ \t]*([^\012]*)\012//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($buf =~ s[^(HTTP/\d+\.\d+)[ \t]+(\d+)[ \t]*([^\012]*)\012][]) { } 0 0 0
elsif (length $buf >= 5 and not $buf =~ m[^HTTP/] or $buf =~ /\012/) { } 0 0 0
211 # HTTP/1.0 response or better
212 0 my($ver,$code,$msg) = ($1, $2, $3);
213 0 $msg =~ s/\015$//;
214 0 $response = HTTP::Response->new($code, $msg);
215 0 $response->protocol($ver);
216  
217 # ensure that we have read all headers. The headers will be
218 # terminated by two blank lines
219 0 ▶ until ($buf =~ /^\015?\012/ || $buf =~ /\015?\012\015?\012/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buf =~ /^\015?\012/ or $buf =~ /\015?\012\015?\012/) 0 0 0
Condition0%0/3
$buf =~ /^\015?\012/ or $buf =~ /\015?\012\015?\012/
l!l&&r!l&&!r
000
MC/DC0%0/2
$buf =~ /^\015?\012/ or $buf =~ /\015?\012\015?\012/
$buf =~ /^\015?\012/ $buf =~ /\015?\012\015?\012/
Truth table0%0/3
A or B
A= $buf =~ /^\015?\012/B= $buf =~ /\015?\012\015?\012/
ABresult
1X1
011
000
220 # must read more if we can...
221 0 ▶ die "read timeout" if $timeout && !$sel->can_read($timeout);
Branch: 1 branch
BranchTrueFalseTotal
if $timeout and not $sel->can_read($timeout) 0 0 0
Condition0%0/3
$timeout and not $sel->can_read($timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
$timeout and not $sel->can_read($timeout)
$timeout not $sel->can_read($timeout)
Truth table0%0/3
A and not(B)
A= $timeoutB= not $sel->can_read($timeout)
ABresult
0X0
100
111
222 0 my $old_len = length($buf);
223 0 $n = $socket->sysread($buf, $size, $old_len);
224 0 ▶ die $! unless defined($n);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
225 0 ▶ die "unexpected EOF before all headers seen" unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
226 }
227  
228 # now we start parsing the headers. The strategy is to
229 # remove one line at a time from the beginning of the header
230 # buffer ($res).
231 0 my($key, $val);
232 0 ▶ while ($buf =~ s/([^\012]*)\012//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ s/([^\012]*)\012//) 0 0 0
233 0 my $line = $1;
234  
235 # if we need to restore as content when illegal headers
236 # are found.
237 0 my $save = "$line\012";
238
239 0 $line =~ s/\015$//;
240 0 ▶ last unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 0 0
241  
242 0 ▶ if ($line =~ /^([a-zA-Z0-9_\-.]+)\s*:\s*(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^([a-zA-Z0-9_\-.]+)\s*:\s*(.*)/) { } 0 0 0
elsif ($line =~ /^\s+(.*)/ and $key) { } 0 0 0
243 0 ▶ $response->push_header($key, $val) if $key;
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
244 0 ($key, $val) = ($1, $2);
245 ▶ } elsif ($line =~ /^\s+(.*)/ && $key) {
Condition0%0/3
$line =~ /^\s+(.*)/ and $key
!ll&&!rl&&r
000
MC/DC0%0/2
$line =~ /^\s+(.*)/ and $key
$line =~ /^\s+(.*)/ $key
Truth table0%0/3
A and B
A= $line =~ /^\s+(.*)/B= $key
ABresult
0X0
100
111
246 0 $val .= " $1";
247 } else {
248 0 $response->push_header("Client-Bad-Header-Line" => $line);
249 }
250 }
251 0 ▶ $response->push_header($key, $val) if $key;
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
252 0 last;
253
254 ▶ } elsif ((length($buf) >= 5 and $buf !~ /^HTTP\//) or
Condition0%0/6
length $buf >= 5 and not $buf =~ m[^HTTP/] or $buf =~ /\012/
l!l&&r!l&&!r
000
length $buf >= 5 and not $buf =~ m[^HTTP/]
!ll&&!rl&&r
000
MC/DC0%0/3
length $buf >= 5 and not $buf =~ m[^HTTP/] or $buf =~ /\012/
length $buf >= 5 not $buf =~ m[^HTTP/] $buf =~ /\012/
Truth table0%0/5
A and not(B) or C
A= length $buf >= 5B= not $buf =~ m[^HTTP/]C= $buf =~ /\012/
ABCresult
11X1
0X11
1011
0X00
1000
255 $buf =~ /\012/ ) {
256 # HTTP/0.9 or worse
257 0 $response = HTTP::Response->new(&HTTP::Status::RC_OK, "OK");
258 0 $response->protocol('HTTP/0.9');
259 0 last;
260  
261 } else {
262 # need more data
263 }
264 };
265 0 $response->request($request);
266 0 $self->_get_sock_info($response, $socket);
267  
268 0 ▶ if ($method eq "CONNECT") {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "CONNECT") 0 0 0
269 0 $response->{client_socket} = $socket; # so it can be picked up
270 0 $response->content($buf); # in case we read more than the headers
271 0 return $response;
272 }
273  
274 0 my $usebuf = length($buf) > 0;
275 0 ▶ $response = $self->collect($arg, $response, sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
276 0 ▶ if ($usebuf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usebuf) 0 0 0
277 0 $usebuf = 0;
278 0 return \$buf;
279 }
280 0 ▶ die "read timeout" if $timeout && !$sel->can_read($timeout);
Branch: 1 branch
BranchTrueFalseTotal
if $timeout and not $sel->can_read($timeout) 0 0 0
Condition0%0/3
$timeout and not $sel->can_read($timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
$timeout and not $sel->can_read($timeout)
$timeout not $sel->can_read($timeout)
Truth table0%0/3
A and not(B)
A= $timeoutB= not $sel->can_read($timeout)
ABresult
0X0
100
111
281 0 my $n = $socket->sysread($buf, $size);
282 0 ▶ die $! unless defined($n);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
283 0 return \$buf;
284 0 } );
285  
286 # $socket->close;
287  
288 0 $response;
289 }
290  
291 1 1;
292  
293 __END__
↑ Summary