3 require LWP::MemberMixin;
4 @ISA = qw(LWP::MemberMixin);
12 my %ImplementedBy = (); # scheme => classname
18 my($class, $scheme, $ua) = @_;
24 # historical/redundant
25 max_size => $ua->{max_size},
34 my($scheme, $ua) = @_;
35 my $impclass = LWP::Protocol::implementor($scheme) or
36 Carp::croak("Protocol scheme '$scheme' is not supported");
38 # hand-off to scheme specific implementation sub-class
39 my $protocol = $impclass->new($scheme, $ua);
47 my($scheme, $impclass) = @_;
50 $ImplementedBy{$scheme} = $impclass;
52 my $ic = $ImplementedBy{$scheme};
55 return '' unless $scheme =~ /^([.+\-\w]+)$/; # check valid URL schemes
56 $scheme = $1; # untaint
57 $scheme =~ s/[.+\-]/_/g; # make it a legal module name
59 # scheme not yet known, look for a 'use'd implementation
60 $ic = "LWP::Protocol::$scheme"; # default location
61 $ic = "LWP::Protocol::nntp" if $scheme eq 'news'; #XXX ugly hack
63 # check we actually have one for the scheme:
64 unless (@{"${ic}::ISA"}) {
68 if ($@ =~ /Can't locate/) { #' #emacs get confused by '
76 $ImplementedBy{$scheme} = $ic if $ic;
83 my($self, $request, $proxy, $arg, $size, $timeout) = @_;
84 Carp::croak('LWP::Protocol::request() needs to be overridden in subclasses');
89 sub timeout { shift->_elem('timeout', @_); }
90 sub max_size { shift->_elem('max_size', @_); }
95 my ($self, $arg, $response, $collector) = @_;
97 my($ua, $max_size) = @{$self}{qw(ua max_size)};
100 local $\; # protect the print below from surprises
101 if (!defined($arg) || !$response->is_success) {
102 $response->{default_add_content} = 1;
104 elsif (!ref($arg) && length($arg)) {
105 open(my $fh, ">", $arg) or die "Can't write to '$arg': $!";
107 push(@{$response->{handlers}{response_data}}, {
109 print $fh $_[3] or die "Can't write to '$arg': $!";
113 push(@{$response->{handlers}{response_done}}, {
115 close($fh) or die "Can't write to '$arg': $!";
120 elsif (ref($arg) eq 'CODE') {
121 push(@{$response->{handlers}{response_data}}, {
123 &$arg($_[3], $_[0], $self);
129 die "Unexpected collect argument '$arg'";
132 $ua->run_handlers("response_header", $response);
134 if (delete $response->{default_add_content}) {
135 push(@{$response->{handlers}{response_data}}, {
137 $_[0]->add_content($_[3]);
144 my $content_size = 0;
145 my $length = $response->content_length;
148 while ($content = &$collector, length $$content) {
149 for my $h ($ua->handlers("response_data", $response)) {
151 unless ($h->{callback}->($response, $ua, $h, $$content)) {
152 # XXX remove from $response->{handlers}{response_data} if present
156 $content_size += length($$content);
157 $ua->progress(($length ? ($content_size / $length) : "tick"), $response);
158 if (defined($max_size) && $content_size > $max_size) {
159 $response->push_header("Client-Aborted", "max_size");
165 delete $response->{handlers}{response_data};
166 delete $response->{handlers} unless %{$response->{handlers}};
169 $response->push_header('X-Died' => $err);
170 $response->push_header("Client-Aborted", "die");
180 my($self, $arg, $response) = @_;
181 my $content = \ $_[3];
183 $self->collect($arg, $response, sub {
184 return $content if $first--;
196 LWP::Protocol - Base class for LWP protocols
200 package LWP::Protocol::foo;
201 require LWP::Protocol;
202 @ISA=qw(LWP::Protocol);
206 This class is used a the base class for all protocol implementations
207 supported by the LWP library.
209 When creating an instance of this class using
210 C<LWP::Protocol::create($url)>, and you get an initialised subclass
211 appropriate for that access method. In other words, the
212 LWP::Protocol::create() function calls the constructor for one of its
215 All derived LWP::Protocol classes need to override the request()
216 method which is used to service a request. The overridden method can
217 make use of the collect() function to collect together chunks of data
220 The following methods and functions are provided:
224 =item $prot = LWP::Protocol->new()
226 The LWP::Protocol constructor is inherited by subclasses. As this is a
227 virtual base class this method should B<not> be called directly.
229 =item $prot = LWP::Protocol::create($scheme)
231 Create an object of the class implementing the protocol to handle the
232 given scheme. This is a function, not a method. It is more an object
233 factory than a constructor. This is the function user agents should
234 use to access protocols.
236 =item $class = LWP::Protocol::implementor($scheme, [$class])
238 Get and/or set implementor class for a scheme. Returns '' if the
239 specified scheme is not supported.
241 =item $prot->request(...)
243 $response = $protocol->request($request, $proxy, undef);
244 $response = $protocol->request($request, $proxy, '/tmp/sss');
245 $response = $protocol->request($request, $proxy, \&callback, 1024);
247 Dispatches a request over the protocol, and returns a response
248 object. This method needs to be overridden in subclasses. Refer to
249 L<LWP::UserAgent> for description of the arguments.
251 =item $prot->collect($arg, $response, $collector)
253 Called to collect the content of a request, and process it
254 appropriately into a scalar, file, or by calling a callback. If $arg
255 is undefined, then the content is stored within the $response. If
256 $arg is a simple scalar, then $arg is interpreted as a file name and
257 the content is written to this file. If $arg is a reference to a
258 routine, then content is passed to this routine.
260 The $collector is a routine that will be called and which is
261 responsible for returning pieces (as ref to scalar) of the content to
262 process. The $collector signals EOF by returning a reference to an
265 The return value from collect() is the $response object reference.
267 B<Note:> We will only use the callback or file argument if
268 $response->is_success(). This avoids sending content data for
269 redirects and authentication responses to the callback which would be
272 =item $prot->collect_once($arg, $response, $content)
274 Can be called when the whole response content is available as
275 $content. This will invoke collect() with a collector callback that
276 returns a reference to $content the first time and an empty string the
283 Inspect the F<LWP/Protocol/file.pm> and F<LWP/Protocol/http.pm> files
284 for examples of usage.
288 Copyright 1995-2001 Gisle Aas.
290 This library is free software; you can redistribute it and/or
291 modify it under the same terms as Perl itself.