3 require LWP::MemberMixin;
4 @ISA = qw(LWP::MemberMixin);
12 my %ImplementedBy = (); # scheme => classname
18 my($class, $scheme, $ua) = @_;
24 # historical/redundant
25 parse_head => $ua->{parse_head},
26 max_size => $ua->{max_size},
35 my($scheme, $ua) = @_;
36 my $impclass = LWP::Protocol::implementor($scheme) or
37 Carp::croak("Protocol scheme '$scheme' is not supported");
39 # hand-off to scheme specific implementation sub-class
40 my $protocol = $impclass->new($scheme, $ua);
48 my($scheme, $impclass) = @_;
51 $ImplementedBy{$scheme} = $impclass;
53 my $ic = $ImplementedBy{$scheme};
56 return '' unless $scheme =~ /^([.+\-\w]+)$/; # check valid URL schemes
57 $scheme = $1; # untaint
58 $scheme =~ s/[.+\-]/_/g; # make it a legal module name
60 # scheme not yet known, look for a 'use'd implementation
61 $ic = "LWP::Protocol::$scheme"; # default location
62 $ic = "LWP::Protocol::nntp" if $scheme eq 'news'; #XXX ugly hack
64 # check we actually have one for the scheme:
65 unless (@{"${ic}::ISA"}) {
69 if ($@ =~ /Can't locate/) { #' #emacs get confused by '
77 $ImplementedBy{$scheme} = $ic if $ic;
84 my($self, $request, $proxy, $arg, $size, $timeout) = @_;
85 Carp::croak('LWP::Protocol::request() needs to be overridden in subclasses');
90 sub timeout { shift->_elem('timeout', @_); }
91 sub parse_head { shift->_elem('parse_head', @_); }
92 sub max_size { shift->_elem('max_size', @_); }
97 my ($self, $arg, $response, $collector) = @_;
99 my($ua, $parse_head, $max_size) = @{$self}{qw(ua parse_head max_size)};
102 if ($parse_head && $response->_is_html) {
103 require HTML::HeadParser;
104 $parser = HTML::HeadParser->new($response->{'_headers'});
105 $parser->xml_mode(1) if $response->_is_xhtml;
106 $parser->utf8_mode(1) if $] >= 5.008 && $HTML::Parser::VERSION >= 3.40;
108 my $content_size = 0;
109 my $length = $response->content_length;
111 if (!defined($arg) || !$response->is_success) {
113 while ($content = &$collector, length $$content) {
115 $parser->parse($$content) or undef($parser);
117 LWP::Debug::debug("read " . length($$content) . " bytes");
118 $response->add_content($$content);
119 $content_size += length($$content);
120 $ua->progress(($length ? ($content_size / $length) : "tick"), $response);
121 if (defined($max_size) && $content_size > $max_size) {
122 LWP::Debug::debug("Aborting because size limit exceeded");
123 $response->push_header("Client-Aborted", "max_size");
130 open(OUT, ">$arg") or
131 return HTTP::Response->new(&HTTP::Status::RC_INTERNAL_SERVER_ERROR,
132 "Cannot write to '$arg': $!");
134 local($\) = ""; # ensure standard $OUTPUT_RECORD_SEPARATOR
135 while ($content = &$collector, length $$content) {
137 $parser->parse($$content) or undef($parser);
139 LWP::Debug::debug("read " . length($$content) . " bytes");
140 print OUT $$content or die "Can't write to '$arg': $!";
141 $content_size += length($$content);
142 $ua->progress(($length ? ($content_size / $length) : "tick"), $response);
143 if (defined($max_size) && $content_size > $max_size) {
144 LWP::Debug::debug("Aborting because size limit exceeded");
145 $response->push_header("Client-Aborted", "max_size");
149 close(OUT) or die "Can't write to '$arg': $!";
151 elsif (ref($arg) eq 'CODE') {
153 while ($content = &$collector, length $$content) {
155 $parser->parse($$content) or undef($parser);
157 LWP::Debug::debug("read " . length($$content) . " bytes");
159 &$arg($$content, $response, $self);
163 $response->push_header('X-Died' => $@);
164 $response->push_header("Client-Aborted", "die");
167 $content_size += length($$content);
168 $ua->progress(($length ? ($content_size / $length) : "tick"), $response);
172 return HTTP::Response->new(&HTTP::Status::RC_INTERNAL_SERVER_ERROR,
173 "Unexpected collect argument '$arg'");
181 my($self, $arg, $response) = @_;
182 my $content = \ $_[3];
184 $self->collect($arg, $response, sub {
185 return $content if $first--;
197 LWP::Protocol - Base class for LWP protocols
201 package LWP::Protocol::foo;
202 require LWP::Protocol;
203 @ISA=qw(LWP::Protocol);
207 This class is used a the base class for all protocol implementations
208 supported by the LWP library.
210 When creating an instance of this class using
211 C<LWP::Protocol::create($url)>, and you get an initialised subclass
212 appropriate for that access method. In other words, the
213 LWP::Protocol::create() function calls the constructor for one of its
216 All derived LWP::Protocol classes need to override the request()
217 method which is used to service a request. The overridden method can
218 make use of the collect() function to collect together chunks of data
221 The following methods and functions are provided:
225 =item $prot = LWP::Protocol->new()
227 The LWP::Protocol constructor is inherited by subclasses. As this is a
228 virtual base class this method should B<not> be called directly.
230 =item $prot = LWP::Protocol::create($scheme)
232 Create an object of the class implementing the protocol to handle the
233 given scheme. This is a function, not a method. It is more an object
234 factory than a constructor. This is the function user agents should
235 use to access protocols.
237 =item $class = LWP::Protocol::implementor($scheme, [$class])
239 Get and/or set implementor class for a scheme. Returns '' if the
240 specified scheme is not supported.
242 =item $prot->request(...)
244 $response = $protocol->request($request, $proxy, undef);
245 $response = $protocol->request($request, $proxy, '/tmp/sss');
246 $response = $protocol->request($request, $proxy, \&callback, 1024);
248 Dispatches a request over the protocol, and returns a response
249 object. This method needs to be overridden in subclasses. Refer to
250 L<LWP::UserAgent> for description of the arguments.
252 =item $prot->collect($arg, $response, $collector)
254 Called to collect the content of a request, and process it
255 appropriately into a scalar, file, or by calling a callback. If $arg
256 is undefined, then the content is stored within the $response. If
257 $arg is a simple scalar, then $arg is interpreted as a file name and
258 the content is written to this file. If $arg is a reference to a
259 routine, then content is passed to this routine.
261 The $collector is a routine that will be called and which is
262 responsible for returning pieces (as ref to scalar) of the content to
263 process. The $collector signals EOF by returning a reference to an
266 The return value from collect() is the $response object reference.
268 B<Note:> We will only use the callback or file argument if
269 $response->is_success(). This avoids sending content data for
270 redirects and authentication responses to the callback which would be
273 =item $prot->collect_once($arg, $response, $content)
275 Can be called when the whole response content is available as
276 $content. This will invoke collect() with a collector callback that
277 returns a reference to $content the first time and an empty string the
282 Inspect the F<LWP/Protocol/file.pm> and F<LWP/Protocol/http.pm> files
283 for examples of usage.
287 Copyright 1995-2001 Gisle Aas.
289 This library is free software; you can redistribute it and/or
290 modify it under the same terms as Perl itself.