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