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