|
|
Net::HTTP - Low-level HTTP client connection
This module is experimental. Details of its interface is likely to change in the future.
use Net::HTTP; my $s = Net::HTTP->new(Host => "www.perl.com) || die $@; $s->write_request(GET => "/", 'User-Agent' => "Mozilla/5.0"); my($code, $mess, %h) = $s->read_response_headers;
while (1) { my $buf; my $n = $s->read_entity_body($buf, 1024); last unless $n; print $buf; }
The Net::HTTP
class is a low-level HTTP client. An instance of the
Net::HTTP
class represents a connection to an HTTP server. The
HTTP protocol is described in RFC 2616.
Net::HTTP
is a sub-class of IO::Socket::INET
. You can mix the
methods described below with reading and writing from the socket
directly. This is not necessary a good idea, unless you know what you
are doing.
The following methods are provided (in addition to those of
IO::Socket::INET
):
Net::HTTP
constructor takes the same options as
IO::Socket::INET
as well as these:
Host: Initial host attribute value KeepAlive: Initial keep_alive attribute value SendTE: Initial send_te attribute_value HTTPVersion: Initial http_version attribute value PeerHTTPVersion: Initial peer_http_version attribute value MaxLineLength: Initial max_line_length attribute value MaxHeaderLines: Initial max_header_lines attribute value
Host
header to send. The $host
should not be set to an empty string (or undef
).
The actual headers set will depend on the value of the http_version
and peer_http_version
attributes.
Compress::Zlib
module is installed then this will
annouce that this client accept both the deflate and gzip
encodings.
read_response_headers()
method call.
Host
header, then a header is inserted with the value
of the host
attribute. Headers like Connection
and
Keep-Alive
might also be added depending on the status of the
keep_alive
attribute.
If $content is given (and it is non-empty), then a Content-Length
header is automatically added unless it was already present.
write_chunk($data)
Transfer-Encoding
header with a value of
chunked
was sent in the request. Note, writing zero-length data is
a no-op. Use the write_chunk_eof()
method to signal end of entity
body data.
Returns true if successful.
format_chunk($data)
write_chunk_eof(%trailers)
Trailer
header.
Returns true if successful.
format_chunk_eof(%trailers)
As a side effect this method updates the 'peer_http_version' attribute.
The method will raise exceptions (die) if the server does not speak proper HTTP.
Options might be passed in as key/value pairs. There are currently
only two options supported; laxed
and junk_out
.
The laxed
option will make read_response_headers
more forgiving
towards servers that have not learned how to speak HTTP properly. The
<laxed> option is a boolean flag, and is enabled by passing in a TRUE
value. The junk_out
option can be used to capture bad header lines
when laxed
is enabled. The value should be an array reference.
Bad header lines will be pushed onto the array.
read()
and sysread(), but buffer offset is not supported yet.
This method should only be called after a successful
read_response_headers()
call.
The return value will be undef
on errors, 0 on EOF, -1 if no data
could be returned this time, and otherwise the number of bytes added
to $buf.
This method might raise exceptions (die) if the server does not speak proper HTTP.
read_entity_body()
has returned 0 to indicate end of the entity
body, you might call this method to pick up any trailers.
read_response_headers()
and
read_entity_body()
methods use an internal buffer which they will look
for data before they actually sysread more from the socket itself. If
they read too much, the remaining data will be left in this buffer.
The read_response_headers()
and read_entity_body()
will invoke the
sysread()
method when they need more data. Subclasses might want to
override this method to contol how reading takes place.
The object itself is a glob. Subclasses should avoid using hash key
names prefixed with http_
and io_
.
the LWP manpage, the IO::Socket::INET manpage, the Net::HTTP::NB manpage
Copyright 2001 Gisle Aas.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.