blib/lib/LWP/Protocol/http10.pm untested

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