HTTP.pm

filter: Statementstmt 79.3% 23 / 29 Branchbran 50.0% 5 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 33.3% 1 / 3 totaltotal 72.3% 34 / 47 CC 2 SCAR 2.9
CC
2
Cov
72%
new
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.3
« HTTP/NB.pm ↑ Summary
1 package Net::HTTP;
2 4 our $VERSION = '6.24';
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $SOCKET_CLASS;
7 4 ▶unless ($SOCKET_CLASS) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Net::HTTP::SOCKET_CLASS) 3 1 4
8 # Try several, in order of capability and preference
9 3 ▶ if (eval { require IO::Socket::IP }) {
Branch: 3 branches
BranchTrueFalseTotal
if (eval { do { require IO::Socket::IP } }) { } 3 0 3
elsif (eval { do { require IO::Socket::INET6 } }) { } 0 0 0
elsif (eval { do { require IO::Socket::INET } }) { } 0 0 0
10 3 $SOCKET_CLASS = "IO::Socket::IP"; # IPv4+IPv6
11 0 } elsif (eval { require IO::Socket::INET6 }) {
12 0 $SOCKET_CLASS = "IO::Socket::INET6"; # IPv4+IPv6
13 0 } elsif (eval { require IO::Socket::INET }) {
14 0 $SOCKET_CLASS = "IO::Socket::INET"; # IPv4 only
15 } else {
16 0 require IO::Socket;
17 0 $SOCKET_CLASS = "IO::Socket::INET";
18 }
19 }
20 4 require Net::HTTP::Methods;
21 4 require Carp;
22  
23 4 our @ISA = ($SOCKET_CLASS, 'Net::HTTP::Methods');
24  
25 3 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
26 3 my $class = shift;
27 3 ▶ Carp::croak("No Host option provided") unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 2 3
28 2 $class->SUPER::new(@_);
29 }
30  
31 2 ▶sub configure {
Subroutine configure: called CC=1 SCAR=0.0
Pod undocumented
32 2 my($self, $cnf) = @_;
33 2 $self->http_configure($cnf);
34 }
35  
36 2 ▶sub http_connect {
Subroutine http_connect: called CC=1 SCAR=0.0
Pod undocumented
37 2 my($self, $cnf) = @_;
38 2 $self->SUPER::configure($cnf);
39 }
40  
41 4 1;
42  
43 =pod
44  
45 =encoding UTF-8
46  
47 =head1 NAME
48  
49 Net::HTTP - Low-level HTTP connection (client)
50  
51 =head1 VERSION
52  
53 version 6.24
54  
55 =head1 SYNOPSIS
56  
57 use Net::HTTP;
58 my $s = Net::HTTP->new(Host => "www.perl.com") || die $@;
59 $s->write_request(GET => "/", 'User-Agent' => "Mozilla/5.0");
60 my($code, $mess, %h) = $s->read_response_headers;
61  
62 while (1) {
63 my $buf;
64 my $n = $s->read_entity_body($buf, 1024);
65 die "read failed: $!" unless defined $n;
66 last unless $n;
67 print $buf;
68 }
69  
70 =head1 DESCRIPTION
71  
72 The C<Net::HTTP> class is a low-level HTTP client. An instance of the
73 C<Net::HTTP> class represents a connection to an HTTP server. The
74 HTTP protocol is described in RFC 2616. The C<Net::HTTP> class
75 supports C<HTTP/1.0> and C<HTTP/1.1>.
76  
77 C<Net::HTTP> is a sub-class of one of C<IO::Socket::IP> (IPv6+IPv4),
78 C<IO::Socket::INET6> (IPv6+IPv4), or C<IO::Socket::INET> (IPv4 only).
79 You can mix the methods described below with reading and writing from the
80 socket directly. This is not necessary a good idea, unless you know what
81 you are doing.
82  
83 The following methods are provided (in addition to those of
84 C<IO::Socket::INET>):
85  
86 =over
87  
88 =item C<< $s = Net::HTTP->new( %options ) >>
89  
90 The C<Net::HTTP> constructor method takes the same options as
91 C<IO::Socket::INET>'s as well as these:
92  
93 Host: Initial host attribute value
94 KeepAlive: Initial keep_alive attribute value
95 SendTE: Initial send_te attribute_value
96 HTTPVersion: Initial http_version attribute value
97 PeerHTTPVersion: Initial peer_http_version attribute value
98 MaxLineLength: Initial max_line_length attribute value
99 MaxHeaderLines: Initial max_header_lines attribute value
100  
101 The C<Host> option is also the default for C<IO::Socket::INET>'s
102 C<PeerAddr>. The C<PeerPort> defaults to 80 if not provided.
103 The C<PeerPort> specification can also be embedded in the C<PeerAddr>
104 by preceding it with a ":", and closing the IPv6 address on brackets "[]" if
105 necessary: "192.0.2.1:80","[2001:db8::1]:80","any.example.com:80".
106  
107 The C<Listen> option provided by C<IO::Socket::INET>'s constructor
108 method is not allowed.
109  
110 If unable to connect to the given HTTP server then the constructor
111 returns C<undef> and $@ contains the reason. After a successful
112 connect, a C<Net:HTTP> object is returned.
113  
114 =item C<< $s->host >>
115  
116 Get/set the default value of the C<Host> header to send. The $host
117 must not be set to an empty string (or C<undef>) for HTTP/1.1.
118  
119 =item C<< $s->keep_alive >>
120  
121 Get/set the I<keep-alive> value. If this value is TRUE then the
122 request will be sent with headers indicating that the server should try
123 to keep the connection open so that multiple requests can be sent.
124  
125 The actual headers set will depend on the value of the C<http_version>
126 and C<peer_http_version> attributes.
127  
128 =item C<< $s->send_te >>
129  
130 Get/set the a value indicating if the request will be sent with a "TE"
131 header to indicate the transfer encodings that the server can choose to
132 use. The list of encodings announced as accepted by this client depends
133 on availability of the following modules: C<Compress::Raw::Zlib> for
134 I<deflate>, and C<IO::Compress::Gunzip> for I<gzip>.
135  
136 =item C<< $s->http_version >>
137  
138 Get/set the HTTP version number that this client should announce.
139 This value can only be set to "1.0" or "1.1". The default is "1.1".
140  
141 =item C<< $s->peer_http_version >>
142  
143 Get/set the protocol version number of our peer. This value will
144 initially be "1.0", but will be updated by a successful
145 read_response_headers() method call.
146  
147 =item C<< $s->max_line_length >>
148  
149 Get/set a limit on the length of response line and response header
150 lines. The default is 8192. A value of 0 means no limit.
151  
152 =item C<< $s->max_header_length >>
153  
154 Get/set a limit on the number of header lines that a response can
155 have. The default is 128. A value of 0 means no limit.
156  
157 =item C<< $s->format_request($method, $uri, %headers, [$content]) >>
158  
159 Format a request message and return it as a string. If the headers do
160 not include a C<Host> header, then a header is inserted with the value
161 of the C<host> attribute. Headers like C<Connection> and
162 C<Keep-Alive> might also be added depending on the status of the
163 C<keep_alive> attribute.
164  
165 If $content is given (and it is non-empty), then a C<Content-Length>
166 header is automatically added unless it was already present.
167  
168 =item C<< $s->write_request($method, $uri, %headers, [$content]) >>
169  
170 Format and send a request message. Arguments are the same as for
171 format_request(). Returns true if successful.
172  
173 =item C<< $s->format_chunk( $data ) >>
174  
175 Returns the string to be written for the given chunk of data.
176  
177 =item C<< $s->write_chunk($data) >>
178  
179 Will write a new chunk of request entity body data. This method
180 should only be used if the C<Transfer-Encoding> header with a value of
181 C<chunked> was sent in the request. Note, writing zero-length data is
182 a no-op. Use the write_chunk_eof() method to signal end of entity
183 body data.
184  
185 Returns true if successful.
186  
187 =item C<< $s->format_chunk_eof( %trailers ) >>
188  
189 Returns the string to be written for signaling EOF when a
190 C<Transfer-Encoding> of C<chunked> is used.
191  
192 =item C<< $s->write_chunk_eof( %trailers ) >>
193  
194 Will write eof marker for chunked data and optional trailers. Note
195 that trailers should not really be used unless is was signaled
196 with a C<Trailer> header.
197  
198 Returns true if successful.
199  
200 =item C<< ($code, $mess, %headers) = $s->read_response_headers( %opts ) >>
201  
202 Read response headers from server and return it. The $code is the 3
203 digit HTTP status code (see L<HTTP::Status>) and $mess is the textual
204 message that came with it. Headers are then returned as key/value
205 pairs. Since key letter casing is not normalized and the same key can
206 even occur multiple times, assigning these values directly to a hash
207 is not wise. Only the $code is returned if this method is called in
208 scalar context.
209  
210 As a side effect this method updates the 'peer_http_version'
211 attribute.
212  
213 Options might be passed in as key/value pairs. There are currently
214 only two options supported; C<laxed> and C<junk_out>.
215  
216 The C<laxed> option will make read_response_headers() more forgiving
217 towards servers that have not learned how to speak HTTP properly. The
218 C<laxed> option is a boolean flag, and is enabled by passing in a TRUE
219 value. The C<junk_out> option can be used to capture bad header lines
220 when C<laxed> is enabled. The value should be an array reference.
221 Bad header lines will be pushed onto the array.
222  
223 The C<laxed> option must be specified in order to communicate with
224 pre-HTTP/1.0 servers that don't describe the response outcome or the
225 data they send back with a header block. For these servers
226 peer_http_version is set to "0.9" and this method returns (200,
227 "Assumed OK").
228  
229 The method will raise an exception (die) if the server does not speak
230 proper HTTP or if the C<max_line_length> or C<max_header_length>
231 limits are reached. If the C<laxed> option is turned on and
232 C<max_line_length> and C<max_header_length> checks are turned off,
233 then no exception will be raised and this method will always
234 return a response code.
235  
236 =item C<< $n = $s->read_entity_body($buf, $size); >>
237  
238 Reads chunks of the entity body content. Basically the same interface
239 as for read() and sysread(), but the buffer offset argument is not
240 supported yet. This method should only be called after a successful
241 read_response_headers() call.
242  
243 The return value will be C<undef> on read errors, 0 on EOF, -1 if no data
244 could be returned this time, otherwise the number of bytes assigned
245 to $buf. The $buf is set to "" when the return value is -1.
246  
247 You normally want to retry this call if this function returns either
248 -1 or C<undef> with C<$!> as EINTR or EAGAIN (see L<Errno>). EINTR
249 can happen if the application catches signals and EAGAIN can happen if
250 you made the socket non-blocking.
251  
252 This method will raise exceptions (die) if the server does not speak
253 proper HTTP. This can only happen when reading chunked data.
254  
255 =item C<< %headers = $s->get_trailers >>
256  
257 After read_entity_body() has returned 0 to indicate end of the entity
258 body, you might call this method to pick up any trailers.
259  
260 =item C<< $s->_rbuf >>
261  
262 Get/set the read buffer content. The read_response_headers() and
263 read_entity_body() methods use an internal buffer which they will look
264 for data before they actually sysread more from the socket itself. If
265 they read too much, the remaining data will be left in this buffer.
266  
267 =item C<< $s->_rbuf_length >>
268  
269 Returns the number of bytes in the read buffer. This should always be
270 the same as:
271  
272 length($s->_rbuf)
273  
274 but might be more efficient.
275  
276 =back
277  
278 =head1 SUBCLASSING
279  
280 The read_response_headers() and read_entity_body() will invoke the
281 sysread() method when they need more data. Subclasses might want to
282 override this method to control how reading takes place.
283  
284 The object itself is a glob. Subclasses should avoid using hash key
285 names prefixed with C<http_> and C<io_>.
286  
287 =head1 SEE ALSO
288  
289 L<LWP>, L<IO::Socket::INET>, L<Net::HTTP::NB>
290  
291 =head1 AUTHOR
292  
293 Gisle Aas <gisle@activestate.com>
294  
295 =head1 COPYRIGHT AND LICENSE
296  
297 This software is copyright (c) 2001 by Gisle Aas.
298  
299 This is free software; you can redistribute it and/or modify it under
300 the same terms as the Perl 5 programming language system itself.
301  
302 =cut
303  
304 __END__
« HTTP/NB.pm ↑ Summary