File Coverage

blib/lib/LWP/Protocol/http.pm
Criterion Covered Total %
statement 153 245 62.4
branch 65 172 37.7
condition 27 108 25.0
subroutine 15 17 88.2
pod 1 4 25.0
total 261 546 47.8


line stmt bran cond sub pod time code
1             package LWP::Protocol::http;
2             $LWP::Protocol::http::VERSION = '6.29';
3 5     5   644 use strict;
  5         12  
  5         267  
4              
5             require HTTP::Response;
6             require HTTP::Status;
7             require Net::HTTP;
8              
9 5     5   26 use base qw(LWP::Protocol);
  5         10  
  5         12081  
10              
11             our @EXTRA_SOCK_OPTS;
12             my $CRLF = "\015\012";
13              
14             sub _new_socket
15             {
16 44     44   114 my($self, $host, $port, $timeout) = @_;
17              
18             # IPv6 literal IP address should be [bracketed] to remove
19             # ambiguity between ip address and port number.
20 44 50 33     138 if ( ($host =~ /:/) && ($host !~ /^\[/) ) {
21 0         0 $host = "[$host]";
22             }
23              
24 44         218 local($^W) = 0; # IO::Socket::INET can be noisy
25             my $sock = $self->socket_class->new(PeerAddr => $host,
26             PeerPort => $port,
27             LocalAddr => $self->{ua}{local_address},
28             Proto => 'tcp',
29             Timeout => $timeout,
30             KeepAlive => !!$self->{ua}{conn_cache},
31 44         125 SendTE => 1,
32             $self->_extra_sock_opts($host, $port),
33             );
34              
35 44 100       207752 unless ($sock) {
36             # IO::Socket::INET leaves additional error messages in $@
37 1         13 my $status = "Can't connect to $host:$port";
38 1 50 33     33 if ($@ =~ /\bconnect: (.*)/ ||
    50 33        
      33        
      33        
39             $@ =~ /\b(Bad hostname)\b/ ||
40             $@ =~ /\b(nodename nor servname provided, or not known)\b/ ||
41             $@ =~ /\b(certificate verify failed)\b/ ||
42             $@ =~ /\b(Crypt-SSLeay can't verify hostnames)\b/
43             ) {
44 0         0 $status .= " ($1)";
45             } elsif ($@) {
46 1         7 $status .= " ($@)";
47             }
48 1         31 die "$status\n\n$@";
49             }
50              
51             # perl 5.005's IO::Socket does not have the blocking method.
52 43         88 eval { $sock->blocking(0); };
  43         121  
53              
54 43         460 $sock;
55             }
56              
57             sub socket_type
58             {
59 1     1 0 5 return "http";
60             }
61              
62             sub socket_class
63             {
64 44     44 0 71 my $self = shift;
65 44   33     288 (ref($self) || $self) . "::Socket";
66             }
67              
68             sub _extra_sock_opts # to be overridden by subclass
69             {
70 44     44   317 return @EXTRA_SOCK_OPTS;
71             }
72              
73             sub _check_sock
74       43     {
75             #my($self, $req, $sock) = @_;
76             }
77              
78             sub _get_sock_info
79             {
80 43     43   112 my($self, $res, $sock) = @_;
81 43 50       169 if (defined(my $peerhost = $sock->peerhost)) {
82 43         2553 $res->header("Client-Peer" => "$peerhost:" . $sock->peerport);
83             }
84             }
85              
86             sub _fixup_header
87             {
88 43     43   114 my($self, $h, $url, $proxy) = @_;
89              
90             # Extract 'Host' header
91 43         146 my $hhost = $url->authority;
92 43 50       746 if ($hhost =~ s/^([^\@]*)\@//) { # get rid of potential "user:pass@"
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 0 0     0 if (defined($1) && not $h->header('Authorization')) {
97 0         0 require URI::Escape;
98 0         0 $h->authorization_basic(map URI::Escape::uri_unescape($_),
99             split(":", $1, 2));
100             }
101             }
102 43         171 $h->init_header('Host' => $hhost);
103              
104 43 100 66     1550 if ($proxy && $url->scheme ne 'https') {
105             # Check the proxy URI's userinfo() for proxy credentials
106             # export http_proxy="http://proxyuser:proxypass@proxyhost:port".
107             # For https only the initial CONNECT requests needs authorization.
108 1         30 my $p_auth = $proxy->userinfo();
109 1 50       19 if(defined $p_auth) {
110 0         0 require URI::Escape;
111 0         0 $h->proxy_authorization_basic(map URI::Escape::uri_unescape($_),
112             split(":", $p_auth, 2))
113             }
114             }
115             }
116              
117             sub hlist_remove {
118 0     0 0 0 my($hlist, $k) = @_;
119 0         0 $k = lc $k;
120 0         0 for (my $i = @$hlist - 2; $i >= 0; $i -= 2) {
121 0 0       0 next unless lc($hlist->[$i]) eq $k;
122 0         0 splice(@$hlist, $i, 2);
123             }
124             }
125              
126             sub request
127             {
128 44     44 1 108 my($self, $request, $proxy, $arg, $size, $timeout) = @_;
129              
130 44   50     210 $size ||= 4096;
131              
132             # check method
133 44         132 my $method = $request->method;
134 44 50       667 unless ($method =~ /^[A-Za-z0-9_!\#\$%&\'*+\-.^\`|~]+$/) { # HTTP token
135 0         0 return HTTP::Response->new( HTTP::Status::RC_BAD_REQUEST,
136             'Library does not allow method ' .
137             "$method for 'http:' URLs");
138             }
139              
140 44         138 my $url = $request->uri;
141              
142             # Proxying SSL with a http proxy needs issues a CONNECT request to build a
143             # tunnel and then upgrades the tunnel to SSL. But when doing keep-alive the
144             # https request does not need to be the first request in the connection, so
145             # we need to distinguish between
146             # - not yet connected (create socket and ssl upgrade)
147             # - connected but not inside ssl tunnel (ssl upgrade)
148             # - inside ssl tunnel to the target - once we are in the tunnel to the
149             # target we cannot only reuse the tunnel for more https requests with the
150             # same target
151              
152 44   33     389 my $ssl_tunnel = $proxy && $url->scheme eq 'https'
153             && $url->host.":".$url->port;
154              
155 44 100       235 my ($host,$port) = $proxy
156             ? ($proxy->host,$proxy->port)
157             : ($url->host,$url->port);
158             my $fullpath =
159             $method eq 'CONNECT' ? $url->host . ":" . $url->port :
160             $proxy && ! $ssl_tunnel ? $url->as_string :
161 44 100 66     3043 do {
    50          
162 42         162 my $path = $url->path_query;
163 42 50       584 $path = "/$path" if $path !~m{^/};
164 42         95 $path
165             };
166              
167 44         97 my $socket;
168 44         136 my $conn_cache = $self->{ua}{conn_cache};
169 44         91 my $cache_key;
170 44 100       96 if ( $conn_cache ) {
171 1         4 $cache_key = "$host:$port";
172             # For https we reuse the socket immediately only if it has an established
173             # tunnel to the target. Otherwise a CONNECT request followed by an SSL
174             # upgrade need to be done first. The request itself might reuse an
175             # existing non-ssl connection to the proxy
176 1 50       3 $cache_key .= "!".$ssl_tunnel if $ssl_tunnel;
177 1 50       5 if ( $socket = $conn_cache->withdraw($self->socket_type,$cache_key)) {
178 0 0       0 if ($socket->can_read(0)) {
179             # if the socket is readable, then either the peer has closed the
180             # connection or there are some garbage bytes on it. In either
181             # case we abandon it.
182 0         0 $socket->close;
183 0         0 $socket = undef;
184             } # else use $socket
185             else {
186 0         0 $socket->timeout($timeout);
187             }
188             }
189             }
190              
191 44 50 33     171 if ( ! $socket && $ssl_tunnel ) {
192             my $proto_https = LWP::Protocol::create('https',$self->{ua})
193 0 0       0 or die "no support for scheme https found";
194              
195             # only if ssl socket class is IO::Socket::SSL we can upgrade
196             # a plain socket to SSL. In case of Net::SSL we fall back to
197             # the old version
198 0 0       0 if ( my $upgrade_sub = $proto_https->can('_upgrade_sock')) {
199 0         0 my $response = $self->request(
200             HTTP::Request->new('CONNECT',"http://$ssl_tunnel"),
201             $proxy,
202             undef,$size,$timeout
203             );
204 0 0       0 $response->is_success or die
205             "establishing SSL tunnel failed: ".$response->status_line;
206             $socket = $upgrade_sub->($proto_https,
207 0 0       0 $response->{client_socket},$url)
208             or die "SSL upgrade failed: $@";
209             } else {
210 0         0 $socket = $proto_https->_new_socket($url->host,$url->port,$timeout);
211             }
212             }
213              
214 44 50       97 if ( ! $socket ) {
215             # connect to remote site w/o reusing established socket
216 44         132 $socket = $self->_new_socket($host, $port, $timeout );
217             }
218              
219 43         96 my $http_version = "";
220 43 50       184 if (my $proto = $request->protocol) {
221 0 0       0 if ($proto =~ /^(?:HTTP\/)?(1.\d+)$/) {
222 0         0 $http_version = $1;
223 0         0 $socket->http_version($http_version);
224 0 0       0 $socket->send_te(0) if $http_version eq "1.0";
225             }
226             }
227              
228 43         599 $self->_check_sock($request, $socket);
229              
230 43         68 my @h;
231 43         114 my $request_headers = $request->headers->clone;
232 43         2747 $self->_fixup_header($request_headers, $url, $proxy);
233              
234             $request_headers->scan(sub {
235 160     160   2090 my($k, $v) = @_;
236 160         267 $k =~ s/^://;
237 160         246 $v =~ s/\n/ /g;
238 160         406 push(@h, $k, $v);
239 43         338 });
240              
241 43         360 my $content_ref = $request->content_ref;
242 43 50       694 $content_ref = $$content_ref if ref($$content_ref);
243 43         86 my $chunked;
244             my $has_content;
245              
246 43 50       140 if (ref($content_ref) eq 'CODE') {
247 0         0 my $clen = $request_headers->header('Content-Length');
248 0 0       0 $has_content++ if $clen;
249 0 0       0 unless (defined $clen) {
250 0         0 push(@h, "Transfer-Encoding" => "chunked");
251 0         0 $has_content++;
252 0         0 $chunked++;
253             }
254             }
255             else {
256             # Set (or override) Content-Length header
257 43         136 my $clen = $request_headers->header('Content-Length');
258 43 100 66     1680 if (defined($$content_ref) && length($$content_ref)) {
    50          
259 2         5 $has_content = length($$content_ref);
260 2 50 33     7 if (!defined($clen) || $clen ne $has_content) {
261 2 50       6 if (defined $clen) {
262 0         0 warn "Content-Length header value was wrong, fixed";
263 0         0 hlist_remove(\@h, 'Content-Length');
264             }
265 2         3 push(@h, 'Content-Length' => $has_content);
266             }
267             }
268             elsif ($clen) {
269 0         0 warn "Content-Length set when there is no content, fixed";
270 0         0 hlist_remove(\@h, 'Content-Length');
271             }
272             }
273              
274 43         101 my $write_wait = 0;
275 43 50 50     113 $write_wait = 2
276             if ($request_headers->header("Expect") || "") =~ /100-continue/;
277              
278 43         1500 my $req_buf = $socket->format_request($method, $fullpath, @h);
279             #print "------\n$req_buf\n------\n";
280              
281 43 50 66     102938 if (!$has_content || $write_wait || $has_content > 8*1024) {
      66        
282             WRITE:
283             {
284             # Since this just writes out the header block it should almost
285             # always succeed to send the whole buffer in a single write call.
286 41         66 my $n = $socket->syswrite($req_buf, length($req_buf));
  41         214  
287 41 50       2704 unless (defined $n) {
288 0 0   1   0 redo WRITE if $!{EINTR};
  1         308  
  1         1027  
  1         7  
289 0 0 0     0 if ($!{EWOULDBLOCK} || $!{EAGAIN}) {
290 0         0 select(undef, undef, undef, 0.1);
291 0         0 redo WRITE;
292             }
293 0         0 die "write failed: $!";
294             }
295 41 50       109 if ($n) {
296 41         113 substr($req_buf, 0, $n, "");
297             }
298             else {
299 0         0 select(undef, undef, undef, 0.5);
300             }
301 41 50       121 redo WRITE if length $req_buf;
302             }
303             }
304              
305 43         117 my($code, $mess, @junk);
306 43         0 my $drop_connection;
307              
308 43 100       93 if ($has_content) {
309 2         4 my $eof;
310             my $wbuf;
311 2         3 my $woffset = 0;
312             INITIAL_READ:
313 2 50       6 if ($write_wait) {
    50          
314             # skip filling $wbuf when waiting for 100-continue
315             # because if the response is a redirect or auth required
316             # the request will be cloned and there is no way
317             # to reset the input stream
318             # return here via the label after the 100-continue is read
319             }
320             elsif (ref($content_ref) eq 'CODE') {
321 0         0 my $buf = &$content_ref();
322 0 0       0 $buf = "" unless defined($buf);
323 0 0       0 $buf = sprintf "%x%s%s%s", length($buf), $CRLF, $buf, $CRLF
324             if $chunked;
325 0 0       0 substr($buf, 0, 0) = $req_buf if $req_buf;
326 0         0 $wbuf = \$buf;
327             }
328             else {
329 2 50       5 if ($req_buf) {
330 2         4 my $buf = $req_buf . $$content_ref;
331 2         4 $wbuf = \$buf;
332             }
333             else {
334 0         0 $wbuf = $content_ref;
335             }
336 2         3 $eof = 1;
337             }
338              
339 2         5 my $fbits = '';
340 2         7 vec($fbits, fileno($socket), 1) = 1;
341              
342             WRITE:
343 2   66     10 while ($write_wait || $woffset < length($$wbuf)) {
344              
345 2         4 my $sel_timeout = $timeout;
346 2 50       5 if ($write_wait) {
347 0 0       0 $sel_timeout = $write_wait if $write_wait < $sel_timeout;
348             }
349 2         3 my $time_before;
350 2 50       4 $time_before = time if $sel_timeout;
351              
352 2         3 my $rbits = $fbits;
353 2 50       5 my $wbits = $write_wait ? undef : $fbits;
354 2         4 my $sel_timeout_before = $sel_timeout;
355             SELECT:
356             {
357 2         4 my $nfound = select($rbits, $wbits, undef, $sel_timeout);
  2         12  
358 2 50       6 if ($nfound < 0) {
359 0 0 0     0 if ($!{EINTR} || $!{EWOULDBLOCK} || $!{EAGAIN}) {
      0        
360 0 0       0 if ($time_before) {
361 0         0 $sel_timeout = $sel_timeout_before - (time - $time_before);
362 0 0       0 $sel_timeout = 0 if $sel_timeout < 0;
363             }
364 0         0 redo SELECT;
365             }
366 0         0 die "select failed: $!";
367             }
368             }
369              
370 2 50       5 if ($write_wait) {
371 0         0 $write_wait -= time - $time_before;
372 0 0       0 $write_wait = 0 if $write_wait < 0;
373             }
374              
375 2 50 33     11 if (defined($rbits) && $rbits =~ /[^\0]/) {
376             # readable
377 0         0 my $buf = $socket->_rbuf;
378 0         0 my $n = $socket->sysread($buf, 1024, length($buf));
379 0 0 0     0 unless (defined $n) {
380 0 0 0     0 die "read failed: $!" unless $!{EINTR} || $!{EWOULDBLOCK} || $!{EAGAIN};
      0        
381             # if we get here the rest of the block will do nothing
382             # and we will retry the read on the next round
383             }
384             elsif ($n == 0) {
385             # the server closed the connection before we finished
386             # writing all the request content. No need to write any more.
387             $drop_connection++;
388             last WRITE;
389             }
390 0         0 $socket->_rbuf($buf);
391 0 0 0     0 if (!$code && $buf =~ /\015?\012\015?\012/) {
392             # a whole response header is present, so we can read it without blocking
393 0         0 ($code, $mess, @h) = $socket->read_response_headers(laxed => 1,
394             junk_out => \@junk,
395             );
396 0 0       0 if ($code eq "100") {
397 0         0 $write_wait = 0;
398 0         0 undef($code);
399 0         0 goto INITIAL_READ;
400             }
401             else {
402 0         0 $drop_connection++;
403 0         0 last WRITE;
404             # XXX should perhaps try to abort write in a nice way too
405             }
406             }
407             }
408 2 50 33     11 if (defined($wbits) && $wbits =~ /[^\0]/) {
409 2         9 my $n = $socket->syswrite($$wbuf, length($$wbuf), $woffset);
410 2 50 33     133 unless (defined $n) {
411 0 0 0     0 die "write failed: $!" unless $!{EINTR} || $!{EWOULDBLOCK} || $!{EAGAIN};
      0        
412 0         0 $n = 0; # will retry write on the next round
413             }
414             elsif ($n == 0) {
415             die "write failed: no bytes written";
416             }
417 2         4 $woffset += $n;
418              
419 2 50 33     13 if (!$eof && $woffset >= length($$wbuf)) {
420             # need to refill buffer from $content_ref code
421 0         0 my $buf = &$content_ref();
422 0 0       0 $buf = "" unless defined($buf);
423 0 0       0 $eof++ unless length($buf);
424 0 0       0 $buf = sprintf "%x%s%s%s", length($buf), $CRLF, $buf, $CRLF
425             if $chunked;
426 0         0 $wbuf = \$buf;
427 0         0 $woffset = 0;
428             }
429             }
430             } # WRITE
431             }
432              
433 43 50       273 ($code, $mess, @h) = $socket->read_response_headers(laxed => 1, junk_out => \@junk)
434             unless $code;
435 43 50       52992 ($code, $mess, @h) = $socket->read_response_headers(laxed => 1, junk_out => \@junk)
436             if $code eq "100";
437              
438 43         348 my $response = HTTP::Response->new($code, $mess);
439 43         2378 my $peer_http_version = $socket->peer_http_version;
440 43         581 $response->protocol("HTTP/$peer_http_version");
441             {
442 43         411 local $HTTP::Headers::TRANSLATE_UNDERSCORE;
  43         86  
443 43         174 $response->push_header(@h);
444             }
445 43 50       3315 $response->push_header("Client-Junk" => \@junk) if @junk;
446              
447 43         192 $response->request($request);
448 43         500 $self->_get_sock_info($response, $socket);
449              
450 43 50       4542 if ($method eq "CONNECT") {
451 0         0 $response->{client_socket} = $socket; # so it can be picked up
452 0         0 return $response;
453             }
454              
455 43 50       153 if (my @te = $response->remove_header('Transfer-Encoding')) {
456 0         0 $response->push_header('Client-Transfer-Encoding', \@te);
457             }
458 43         1301 $response->push_header('Client-Response-Num', scalar $socket->increment_response_count);
459              
460 43         1754 my $complete;
461             $response = $self->collect($arg, $response, sub {
462 68     68   126 my $buf = ""; #prevent use of uninitialized value in SSLeay.xs
463 68         135 my $n;
464             READ:
465             {
466 68         100 $n = $socket->read_entity_body($buf, $size);
  68         272  
467 68 50       5163 unless (defined $n) {
468 0 0 0     0 redo READ if $!{EINTR} || $!{EWOULDBLOCK} || $!{EAGAIN} || $!{ENOTTY};
      0        
      0        
469 0         0 die "read failed: $!";
470             }
471 68 50       184 redo READ if $n == -1;
472             }
473 68 100       153 $complete++ if !$n;
474 68         355 return \$buf;
475 43         346 } );
476 43 100       239 $drop_connection++ unless $complete;
477              
478 43         167 @h = $socket->get_trailers;
479 43 50       487 if (@h) {
480 0         0 local $HTTP::Headers::TRANSLATE_UNDERSCORE;
481 0         0 $response->push_header(@h);
482             }
483              
484             # keep-alive support
485 43 100       101 unless ($drop_connection) {
486 42 50       84 if ($cache_key) {
487 0   0     0 my %connection = map { (lc($_) => 1) }
  0         0  
488             split(/\s*,\s*/, ($response->header("Connection") || ""));
489 0 0 0     0 if (($peer_http_version eq "1.1" && !$connection{close}) ||
      0        
490             $connection{"keep-alive"})
491             {
492 0         0 $conn_cache->deposit($self->socket_type, $cache_key, $socket);
493             }
494             }
495             }
496              
497 43         2666 $response;
498             }
499              
500              
501             #-----------------------------------------------------------
502             package # hide from PAUSE
503             LWP::Protocol::http::SocketMethods;
504              
505             sub ping {
506 0     0   0 my $self = shift;
507 0         0 !$self->can_read(0);
508             }
509              
510             sub increment_response_count {
511 43     43   79 my $self = shift;
512 43         59 return ++${*$self}{'myhttp_response_count'};
  43         223  
513             }
514              
515             #-----------------------------------------------------------
516             package # hide from PAUSE
517             LWP::Protocol::http::Socket;
518              
519 5     5   1128 use base qw(LWP::Protocol::http::SocketMethods Net::HTTP);
  5         10  
  5         2540  
520              
521             1;