source: main/trunk/greenstone2/perllib/cpan/Net/HTTP.pm@ 27181

Last change on this file since 27181 was 27181, checked in by davidb, 11 years ago

Latest libwww-perl (v6x) isn't as self-sufficeint as earlier (v5.x) in terms of supporting Perl modules. Dropping back to to this earlier version so activate.pl runs smoothly when system-installed Perl on Unix system does not have the LWP and related modules installed

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