source: main/trunk/greenstone2/perllib/cpan/LWP/UserAgent.pm@ 27183

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

Changing to using installed version of LWP that comes from libwww-perl, which is more self-contained than v6.x

File size: 52.9 KB
Line 
1package LWP::UserAgent;
2
3use strict;
4use vars qw(@ISA $VERSION);
5
6require LWP::MemberMixin;
7@ISA = qw(LWP::MemberMixin);
8$VERSION = "5.835";
9
10use HTTP::Request ();
11use HTTP::Response ();
12use HTTP::Date ();
13
14use LWP ();
15use LWP::Protocol ();
16
17use Carp ();
18
19if ($ENV{PERL_LWP_USE_HTTP_10}) {
20 require LWP::Protocol::http10;
21 LWP::Protocol::implementor('http', 'LWP::Protocol::http10');
22 eval {
23 require LWP::Protocol::https10;
24 LWP::Protocol::implementor('https', 'LWP::Protocol::https10');
25 };
26}
27
28
29
30sub new
31{
32 # Check for common user mistake
33 Carp::croak("Options to LWP::UserAgent should be key/value pairs, not hash reference")
34 if ref($_[1]) eq 'HASH';
35
36 my($class, %cnf) = @_;
37
38 my $agent = delete $cnf{agent};
39 my $from = delete $cnf{from};
40 my $def_headers = delete $cnf{default_headers};
41 my $timeout = delete $cnf{timeout};
42 $timeout = 3*60 unless defined $timeout;
43 my $local_address = delete $cnf{local_address};
44 my $use_eval = delete $cnf{use_eval};
45 $use_eval = 1 unless defined $use_eval;
46 my $parse_head = delete $cnf{parse_head};
47 $parse_head = 1 unless defined $parse_head;
48 my $show_progress = delete $cnf{show_progress};
49 my $max_size = delete $cnf{max_size};
50 my $max_redirect = delete $cnf{max_redirect};
51 $max_redirect = 7 unless defined $max_redirect;
52 my $env_proxy = delete $cnf{env_proxy};
53
54 my $cookie_jar = delete $cnf{cookie_jar};
55 my $conn_cache = delete $cnf{conn_cache};
56 my $keep_alive = delete $cnf{keep_alive};
57
58 Carp::croak("Can't mix conn_cache and keep_alive")
59 if $conn_cache && $keep_alive;
60
61
62 my $protocols_allowed = delete $cnf{protocols_allowed};
63 my $protocols_forbidden = delete $cnf{protocols_forbidden};
64
65 my $requests_redirectable = delete $cnf{requests_redirectable};
66 $requests_redirectable = ['GET', 'HEAD']
67 unless defined $requests_redirectable;
68
69 # Actually ""s are just as good as 0's, but for concision we'll just say:
70 Carp::croak("protocols_allowed has to be an arrayref or 0, not \"$protocols_allowed\"!")
71 if $protocols_allowed and ref($protocols_allowed) ne 'ARRAY';
72 Carp::croak("protocols_forbidden has to be an arrayref or 0, not \"$protocols_forbidden\"!")
73 if $protocols_forbidden and ref($protocols_forbidden) ne 'ARRAY';
74 Carp::croak("requests_redirectable has to be an arrayref or 0, not \"$requests_redirectable\"!")
75 if $requests_redirectable and ref($requests_redirectable) ne 'ARRAY';
76
77
78 if (%cnf && $^W) {
79 Carp::carp("Unrecognized LWP::UserAgent options: @{[sort keys %cnf]}");
80 }
81
82 my $self = bless {
83 def_headers => $def_headers,
84 timeout => $timeout,
85 local_address => $local_address,
86 use_eval => $use_eval,
87 show_progress=> $show_progress,
88 max_size => $max_size,
89 max_redirect => $max_redirect,
90 proxy => {},
91 no_proxy => [],
92 protocols_allowed => $protocols_allowed,
93 protocols_forbidden => $protocols_forbidden,
94 requests_redirectable => $requests_redirectable,
95 }, $class;
96
97 $self->agent(defined($agent) ? $agent : $class->_agent)
98 if defined($agent) || !$def_headers || !$def_headers->header("User-Agent");
99 $self->from($from) if $from;
100 $self->cookie_jar($cookie_jar) if $cookie_jar;
101 $self->parse_head($parse_head);
102 $self->env_proxy if $env_proxy;
103
104 $self->protocols_allowed( $protocols_allowed ) if $protocols_allowed;
105 $self->protocols_forbidden($protocols_forbidden) if $protocols_forbidden;
106
107 if ($keep_alive) {
108 $conn_cache ||= { total_capacity => $keep_alive };
109 }
110 $self->conn_cache($conn_cache) if $conn_cache;
111
112 return $self;
113}
114
115
116sub send_request
117{
118 my($self, $request, $arg, $size) = @_;
119 my($method, $url) = ($request->method, $request->uri);
120 my $scheme = $url->scheme;
121
122 local($SIG{__DIE__}); # protect against user defined die handlers
123
124 $self->progress("begin", $request);
125
126 my $response = $self->run_handlers("request_send", $request);
127
128 unless ($response) {
129 my $protocol;
130
131 {
132 # Honor object-specific restrictions by forcing protocol objects
133 # into class LWP::Protocol::nogo.
134 my $x;
135 if($x = $self->protocols_allowed) {
136 if (grep lc($_) eq $scheme, @$x) {
137 }
138 else {
139 require LWP::Protocol::nogo;
140 $protocol = LWP::Protocol::nogo->new;
141 }
142 }
143 elsif ($x = $self->protocols_forbidden) {
144 if(grep lc($_) eq $scheme, @$x) {
145 require LWP::Protocol::nogo;
146 $protocol = LWP::Protocol::nogo->new;
147 }
148 }
149 # else fall thru and create the protocol object normally
150 }
151
152 # Locate protocol to use
153 my $proxy = $request->{proxy};
154 if ($proxy) {
155 $scheme = $proxy->scheme;
156 }
157
158 unless ($protocol) {
159 $protocol = eval { LWP::Protocol::create($scheme, $self) };
160 if ($@) {
161 $@ =~ s/ at .* line \d+.*//s; # remove file/line number
162 $response = _new_response($request, &HTTP::Status::RC_NOT_IMPLEMENTED, $@);
163 if ($scheme eq "https") {
164 $response->message($response->message . " (Crypt::SSLeay or IO::Socket::SSL not installed)");
165 $response->content_type("text/plain");
166 $response->content(<<EOT);
167LWP will support https URLs if either Crypt::SSLeay or IO::Socket::SSL
168is installed. More information at
169<http://search.cpan.org/dist/libwww-perl/README.SSL>.
170EOT
171 }
172 }
173 }
174
175 if (!$response && $self->{use_eval}) {
176 # we eval, and turn dies into responses below
177 eval {
178 $response = $protocol->request($request, $proxy,
179 $arg, $size, $self->{timeout});
180 };
181 if ($@) {
182 $@ =~ s/ at .* line \d+.*//s; # remove file/line number
183 $response = _new_response($request,
184 &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
185 $@);
186 }
187 }
188 elsif (!$response) {
189 $response = $protocol->request($request, $proxy,
190 $arg, $size, $self->{timeout});
191 # XXX: Should we die unless $response->is_success ???
192 }
193 }
194
195 $response->request($request); # record request for reference
196 $response->header("Client-Date" => HTTP::Date::time2str(time));
197
198 $self->run_handlers("response_done", $response);
199
200 $self->progress("end", $response);
201 return $response;
202}
203
204
205sub prepare_request
206{
207 my($self, $request) = @_;
208 die "Method missing" unless $request->method;
209 my $url = $request->uri;
210 die "URL missing" unless $url;
211 die "URL must be absolute" unless $url->scheme;
212
213 $self->run_handlers("request_preprepare", $request);
214
215 if (my $def_headers = $self->{def_headers}) {
216 for my $h ($def_headers->header_field_names) {
217 $request->init_header($h => [$def_headers->header($h)]);
218 }
219 }
220
221 $self->run_handlers("request_prepare", $request);
222
223 return $request;
224}
225
226
227sub simple_request
228{
229 my($self, $request, $arg, $size) = @_;
230
231 # sanity check the request passed in
232 if (defined $request) {
233 if (ref $request) {
234 Carp::croak("You need a request object, not a " . ref($request) . " object")
235 if ref($request) eq 'ARRAY' or ref($request) eq 'HASH' or
236 !$request->can('method') or !$request->can('uri');
237 }
238 else {
239 Carp::croak("You need a request object, not '$request'");
240 }
241 }
242 else {
243 Carp::croak("No request object passed in");
244 }
245
246 eval {
247 $request = $self->prepare_request($request);
248 };
249 if ($@) {
250 $@ =~ s/ at .* line \d+.*//s; # remove file/line number
251 return _new_response($request, &HTTP::Status::RC_BAD_REQUEST, $@);
252 }
253 return $self->send_request($request, $arg, $size);
254}
255
256
257sub request
258{
259 my($self, $request, $arg, $size, $previous) = @_;
260
261 my $response = $self->simple_request($request, $arg, $size);
262 $response->previous($previous) if $previous;
263
264 if ($response->redirects >= $self->{max_redirect}) {
265 $response->header("Client-Warning" =>
266 "Redirect loop detected (max_redirect = $self->{max_redirect})");
267 return $response;
268 }
269
270 if (my $req = $self->run_handlers("response_redirect", $response)) {
271 return $self->request($req, $arg, $size, $response);
272 }
273
274 my $code = $response->code;
275
276 if ($code == &HTTP::Status::RC_MOVED_PERMANENTLY or
277 $code == &HTTP::Status::RC_FOUND or
278 $code == &HTTP::Status::RC_SEE_OTHER or
279 $code == &HTTP::Status::RC_TEMPORARY_REDIRECT)
280 {
281 my $referral = $request->clone;
282
283 # These headers should never be forwarded
284 $referral->remove_header('Host', 'Cookie');
285
286 if ($referral->header('Referer') &&
287 $request->uri->scheme eq 'https' &&
288 $referral->uri->scheme eq 'http')
289 {
290 # RFC 2616, section 15.1.3.
291 # https -> http redirect, suppressing Referer
292 $referral->remove_header('Referer');
293 }
294
295 if ($code == &HTTP::Status::RC_SEE_OTHER ||
296 $code == &HTTP::Status::RC_FOUND)
297 {
298 my $method = uc($referral->method);
299 unless ($method eq "GET" || $method eq "HEAD") {
300 $referral->method("GET");
301 $referral->content("");
302 $referral->remove_content_headers;
303 }
304 }
305
306 # And then we update the URL based on the Location:-header.
307 my $referral_uri = $response->header('Location');
308 {
309 # Some servers erroneously return a relative URL for redirects,
310 # so make it absolute if it not already is.
311 local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
312 my $base = $response->base;
313 $referral_uri = "" unless defined $referral_uri;
314 $referral_uri = $HTTP::URI_CLASS->new($referral_uri, $base)
315 ->abs($base);
316 }
317 $referral->uri($referral_uri);
318
319 return $response unless $self->redirect_ok($referral, $response);
320 return $self->request($referral, $arg, $size, $response);
321
322 }
323 elsif ($code == &HTTP::Status::RC_UNAUTHORIZED ||
324 $code == &HTTP::Status::RC_PROXY_AUTHENTICATION_REQUIRED
325 )
326 {
327 my $proxy = ($code == &HTTP::Status::RC_PROXY_AUTHENTICATION_REQUIRED);
328 my $ch_header = $proxy ? "Proxy-Authenticate" : "WWW-Authenticate";
329 my @challenge = $response->header($ch_header);
330 unless (@challenge) {
331 $response->header("Client-Warning" =>
332 "Missing Authenticate header");
333 return $response;
334 }
335
336 require HTTP::Headers::Util;
337 CHALLENGE: for my $challenge (@challenge) {
338 $challenge =~ tr/,/;/; # "," is used to separate auth-params!!
339 ($challenge) = HTTP::Headers::Util::split_header_words($challenge);
340 my $scheme = shift(@$challenge);
341 shift(@$challenge); # no value
342 $challenge = { @$challenge }; # make rest into a hash
343
344 unless ($scheme =~ /^([a-z]+(?:-[a-z]+)*)$/) {
345 $response->header("Client-Warning" =>
346 "Bad authentication scheme '$scheme'");
347 return $response;
348 }
349 $scheme = $1; # untainted now
350 my $class = "LWP::Authen::\u$scheme";
351 $class =~ s/-/_/g;
352
353 no strict 'refs';
354 unless (%{"$class\::"}) {
355 # try to load it
356 eval "require $class";
357 if ($@) {
358 if ($@ =~ /^Can\'t locate/) {
359 $response->header("Client-Warning" =>
360 "Unsupported authentication scheme '$scheme'");
361 }
362 else {
363 $response->header("Client-Warning" => $@);
364 }
365 next CHALLENGE;
366 }
367 }
368 unless ($class->can("authenticate")) {
369 $response->header("Client-Warning" =>
370 "Unsupported authentication scheme '$scheme'");
371 next CHALLENGE;
372 }
373 return $class->authenticate($self, $proxy, $challenge, $response,
374 $request, $arg, $size);
375 }
376 return $response;
377 }
378 return $response;
379}
380
381
382#
383# Now the shortcuts...
384#
385sub get {
386 require HTTP::Request::Common;
387 my($self, @parameters) = @_;
388 my @suff = $self->_process_colonic_headers(\@parameters,1);
389 return $self->request( HTTP::Request::Common::GET( @parameters ), @suff );
390}
391
392
393sub post {
394 require HTTP::Request::Common;
395 my($self, @parameters) = @_;
396 my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
397 return $self->request( HTTP::Request::Common::POST( @parameters ), @suff );
398}
399
400
401sub head {
402 require HTTP::Request::Common;
403 my($self, @parameters) = @_;
404 my @suff = $self->_process_colonic_headers(\@parameters,1);
405 return $self->request( HTTP::Request::Common::HEAD( @parameters ), @suff );
406}
407
408
409sub _process_colonic_headers {
410 # Process :content_cb / :content_file / :read_size_hint headers.
411 my($self, $args, $start_index) = @_;
412
413 my($arg, $size);
414 for(my $i = $start_index; $i < @$args; $i += 2) {
415 next unless defined $args->[$i];
416
417 #printf "Considering %s => %s\n", $args->[$i], $args->[$i + 1];
418
419 if($args->[$i] eq ':content_cb') {
420 # Some sanity-checking...
421 $arg = $args->[$i + 1];
422 Carp::croak("A :content_cb value can't be undef") unless defined $arg;
423 Carp::croak("A :content_cb value must be a coderef")
424 unless ref $arg and UNIVERSAL::isa($arg, 'CODE');
425
426 }
427 elsif ($args->[$i] eq ':content_file') {
428 $arg = $args->[$i + 1];
429
430 # Some sanity-checking...
431 Carp::croak("A :content_file value can't be undef")
432 unless defined $arg;
433 Carp::croak("A :content_file value can't be a reference")
434 if ref $arg;
435 Carp::croak("A :content_file value can't be \"\"")
436 unless length $arg;
437
438 }
439 elsif ($args->[$i] eq ':read_size_hint') {
440 $size = $args->[$i + 1];
441 # Bother checking it?
442
443 }
444 else {
445 next;
446 }
447 splice @$args, $i, 2;
448 $i -= 2;
449 }
450
451 # And return a suitable suffix-list for request(REQ,...)
452
453 return unless defined $arg;
454 return $arg, $size if defined $size;
455 return $arg;
456}
457
458my @ANI = qw(- \ | /);
459
460sub progress {
461 my($self, $status, $m) = @_;
462 return unless $self->{show_progress};
463
464 local($,, $\);
465 if ($status eq "begin") {
466 print STDERR "** ", $m->method, " ", $m->uri, " ==> ";
467 $self->{progress_start} = time;
468 $self->{progress_lastp} = "";
469 $self->{progress_ani} = 0;
470 }
471 elsif ($status eq "end") {
472 delete $self->{progress_lastp};
473 delete $self->{progress_ani};
474 print STDERR $m->status_line;
475 my $t = time - delete $self->{progress_start};
476 print STDERR " (${t}s)" if $t;
477 print STDERR "\n";
478 }
479 elsif ($status eq "tick") {
480 print STDERR "$ANI[$self->{progress_ani}++]\b";
481 $self->{progress_ani} %= @ANI;
482 }
483 else {
484 my $p = sprintf "%3.0f%%", $status * 100;
485 return if $p eq $self->{progress_lastp};
486 print STDERR "$p\b\b\b\b";
487 $self->{progress_lastp} = $p;
488 }
489 STDERR->flush;
490}
491
492
493#
494# This whole allow/forbid thing is based on man 1 at's way of doing things.
495#
496sub is_protocol_supported
497{
498 my($self, $scheme) = @_;
499 if (ref $scheme) {
500 # assume we got a reference to an URI object
501 $scheme = $scheme->scheme;
502 }
503 else {
504 Carp::croak("Illegal scheme '$scheme' passed to is_protocol_supported")
505 if $scheme =~ /\W/;
506 $scheme = lc $scheme;
507 }
508
509 my $x;
510 if(ref($self) and $x = $self->protocols_allowed) {
511 return 0 unless grep lc($_) eq $scheme, @$x;
512 }
513 elsif (ref($self) and $x = $self->protocols_forbidden) {
514 return 0 if grep lc($_) eq $scheme, @$x;
515 }
516
517 local($SIG{__DIE__}); # protect against user defined die handlers
518 $x = LWP::Protocol::implementor($scheme);
519 return 1 if $x and $x ne 'LWP::Protocol::nogo';
520 return 0;
521}
522
523
524sub protocols_allowed { shift->_elem('protocols_allowed' , @_) }
525sub protocols_forbidden { shift->_elem('protocols_forbidden' , @_) }
526sub requests_redirectable { shift->_elem('requests_redirectable', @_) }
527
528
529sub redirect_ok
530{
531 # RFC 2616, section 10.3.2 and 10.3.3 say:
532 # If the 30[12] status code is received in response to a request other
533 # than GET or HEAD, the user agent MUST NOT automatically redirect the
534 # request unless it can be confirmed by the user, since this might
535 # change the conditions under which the request was issued.
536
537 # Note that this routine used to be just:
538 # return 0 if $_[1]->method eq "POST"; return 1;
539
540 my($self, $new_request, $response) = @_;
541 my $method = $response->request->method;
542 return 0 unless grep $_ eq $method,
543 @{ $self->requests_redirectable || [] };
544
545 if ($new_request->uri->scheme eq 'file') {
546 $response->header("Client-Warning" =>
547 "Can't redirect to a file:// URL!");
548 return 0;
549 }
550
551 # Otherwise it's apparently okay...
552 return 1;
553}
554
555
556sub credentials
557{
558 my $self = shift;
559 my $netloc = lc(shift);
560 my $realm = shift || "";
561 my $old = $self->{basic_authentication}{$netloc}{$realm};
562 if (@_) {
563 $self->{basic_authentication}{$netloc}{$realm} = [@_];
564 }
565 return unless $old;
566 return @$old if wantarray;
567 return join(":", @$old);
568}
569
570
571sub get_basic_credentials
572{
573 my($self, $realm, $uri, $proxy) = @_;
574 return if $proxy;
575 return $self->credentials($uri->host_port, $realm);
576}
577
578
579sub timeout { shift->_elem('timeout', @_); }
580sub local_address{ shift->_elem('local_address',@_); }
581sub max_size { shift->_elem('max_size', @_); }
582sub max_redirect { shift->_elem('max_redirect', @_); }
583sub show_progress{ shift->_elem('show_progress', @_); }
584
585sub parse_head {
586 my $self = shift;
587 if (@_) {
588 my $flag = shift;
589 my $parser;
590 my $old = $self->set_my_handler("response_header", $flag ? sub {
591 my($response, $ua) = @_;
592 require HTML::HeadParser;
593 $parser = HTML::HeadParser->new;
594 $parser->xml_mode(1) if $response->content_is_xhtml;
595 $parser->utf8_mode(1) if $] >= 5.008 && $HTML::Parser::VERSION >= 3.40;
596
597 push(@{$response->{handlers}{response_data}}, {
598 callback => sub {
599 return unless $parser;
600 unless ($parser->parse($_[3])) {
601 my $h = $parser->header;
602 my $r = $_[0];
603 for my $f ($h->header_field_names) {
604 $r->init_header($f, [$h->header($f)]);
605 }
606 undef($parser);
607 }
608 },
609 });
610
611 } : undef,
612 m_media_type => "html",
613 );
614 return !!$old;
615 }
616 else {
617 return !!$self->get_my_handler("response_header");
618 }
619}
620
621sub cookie_jar {
622 my $self = shift;
623 my $old = $self->{cookie_jar};
624 if (@_) {
625 my $jar = shift;
626 if (ref($jar) eq "HASH") {
627 require HTTP::Cookies;
628 $jar = HTTP::Cookies->new(%$jar);
629 }
630 $self->{cookie_jar} = $jar;
631 $self->set_my_handler("request_prepare",
632 $jar ? sub { $jar->add_cookie_header($_[0]); } : undef,
633 );
634 $self->set_my_handler("response_done",
635 $jar ? sub { $jar->extract_cookies($_[0]); } : undef,
636 );
637 }
638 $old;
639}
640
641sub default_headers {
642 my $self = shift;
643 my $old = $self->{def_headers} ||= HTTP::Headers->new;
644 if (@_) {
645 Carp::croak("default_headers not set to HTTP::Headers compatible object")
646 unless @_ == 1 && $_[0]->can("header_field_names");
647 $self->{def_headers} = shift;
648 }
649 return $old;
650}
651
652sub default_header {
653 my $self = shift;
654 return $self->default_headers->header(@_);
655}
656
657sub _agent { "libwww-perl/$LWP::VERSION" }
658
659sub agent {
660 my $self = shift;
661 if (@_) {
662 my $agent = shift;
663 if ($agent) {
664 $agent .= $self->_agent if $agent =~ /\s+$/;
665 }
666 else {
667 undef($agent)
668 }
669 return $self->default_header("User-Agent", $agent);
670 }
671 return $self->default_header("User-Agent");
672}
673
674sub from { # legacy
675 my $self = shift;
676 return $self->default_header("From", @_);
677}
678
679
680sub conn_cache {
681 my $self = shift;
682 my $old = $self->{conn_cache};
683 if (@_) {
684 my $cache = shift;
685 if (ref($cache) eq "HASH") {
686 require LWP::ConnCache;
687 $cache = LWP::ConnCache->new(%$cache);
688 }
689 $self->{conn_cache} = $cache;
690 }
691 $old;
692}
693
694
695sub add_handler {
696 my($self, $phase, $cb, %spec) = @_;
697 $spec{line} ||= join(":", (caller)[1,2]);
698 my $conf = $self->{handlers}{$phase} ||= do {
699 require HTTP::Config;
700 HTTP::Config->new;
701 };
702 $conf->add(%spec, callback => $cb);
703}
704
705sub set_my_handler {
706 my($self, $phase, $cb, %spec) = @_;
707 $spec{owner} = (caller(1))[3] unless exists $spec{owner};
708 $self->remove_handler($phase, %spec);
709 $spec{line} ||= join(":", (caller)[1,2]);
710 $self->add_handler($phase, $cb, %spec) if $cb;
711}
712
713sub get_my_handler {
714 my $self = shift;
715 my $phase = shift;
716 my $init = pop if @_ % 2;
717 my %spec = @_;
718 my $conf = $self->{handlers}{$phase};
719 unless ($conf) {
720 return unless $init;
721 require HTTP::Config;
722 $conf = $self->{handlers}{$phase} = HTTP::Config->new;
723 }
724 $spec{owner} = (caller(1))[3] unless exists $spec{owner};
725 my @h = $conf->find(%spec);
726 if (!@h && $init) {
727 if (ref($init) eq "CODE") {
728 $init->(\%spec);
729 }
730 elsif (ref($init) eq "HASH") {
731 while (my($k, $v) = each %$init) {
732 $spec{$k} = $v;
733 }
734 }
735 $spec{callback} ||= sub {};
736 $spec{line} ||= join(":", (caller)[1,2]);
737 $conf->add(\%spec);
738 return \%spec;
739 }
740 return wantarray ? @h : $h[0];
741}
742
743sub remove_handler {
744 my($self, $phase, %spec) = @_;
745 if ($phase) {
746 my $conf = $self->{handlers}{$phase} || return;
747 my @h = $conf->remove(%spec);
748 delete $self->{handlers}{$phase} if $conf->empty;
749 return @h;
750 }
751
752 return unless $self->{handlers};
753 return map $self->remove_handler($_), sort keys %{$self->{handlers}};
754}
755
756sub handlers {
757 my($self, $phase, $o) = @_;
758 my @h;
759 if ($o->{handlers} && $o->{handlers}{$phase}) {
760 push(@h, @{$o->{handlers}{$phase}});
761 }
762 if (my $conf = $self->{handlers}{$phase}) {
763 push(@h, $conf->matching($o));
764 }
765 return @h;
766}
767
768sub run_handlers {
769 my($self, $phase, $o) = @_;
770 if (defined(wantarray)) {
771 for my $h ($self->handlers($phase, $o)) {
772 my $ret = $h->{callback}->($o, $self, $h);
773 return $ret if $ret;
774 }
775 return undef;
776 }
777
778 for my $h ($self->handlers($phase, $o)) {
779 $h->{callback}->($o, $self, $h);
780 }
781}
782
783
784# depreciated
785sub use_eval { shift->_elem('use_eval', @_); }
786sub use_alarm
787{
788 Carp::carp("LWP::UserAgent->use_alarm(BOOL) is a no-op")
789 if @_ > 1 && $^W;
790 "";
791}
792
793
794sub clone
795{
796 my $self = shift;
797 my $copy = bless { %$self }, ref $self; # copy most fields
798
799 delete $copy->{handlers};
800 delete $copy->{conn_cache};
801
802 # copy any plain arrays and hashes; known not to need recursive copy
803 for my $k (qw(proxy no_proxy requests_redirectable)) {
804 next unless $copy->{$k};
805 if (ref($copy->{$k}) eq "ARRAY") {
806 $copy->{$k} = [ @{$copy->{$k}} ];
807 }
808 elsif (ref($copy->{$k}) eq "HASH") {
809 $copy->{$k} = { %{$copy->{$k}} };
810 }
811 }
812
813 if ($self->{def_headers}) {
814 $copy->{def_headers} = $self->{def_headers}->clone;
815 }
816
817 # re-enable standard handlers
818 $copy->parse_head($self->parse_head);
819
820 # no easy way to clone the cookie jar; so let's just remove it for now
821 $copy->cookie_jar(undef);
822
823 $copy;
824}
825
826
827sub mirror
828{
829 my($self, $url, $file) = @_;
830
831 my $request = HTTP::Request->new('GET', $url);
832
833 # If the file exists, add a cache-related header
834 if ( -e $file ) {
835 my ($mtime) = ( stat($file) )[9];
836 if ($mtime) {
837 $request->header( 'If-Modified-Since' => HTTP::Date::time2str($mtime) );
838 }
839 }
840 my $tmpfile = "$file-$$";
841
842 my $response = $self->request($request, $tmpfile);
843 if ( $response->header('X-Died') ) {
844 die $response->header('X-Died');
845 }
846
847 # Only fetching a fresh copy of the would be considered success.
848 # If the file was not modified, "304" would returned, which
849 # is considered by HTTP::Status to be a "redirect", /not/ "success"
850 if ( $response->is_success ) {
851 my @stat = stat($tmpfile) or die "Could not stat tmpfile '$tmpfile': $!";
852 my $file_length = $stat[7];
853 my ($content_length) = $response->header('Content-length');
854
855 if ( defined $content_length and $file_length < $content_length ) {
856 unlink($tmpfile);
857 die "Transfer truncated: " . "only $file_length out of $content_length bytes received\n";
858 }
859 elsif ( defined $content_length and $file_length > $content_length ) {
860 unlink($tmpfile);
861 die "Content-length mismatch: " . "expected $content_length bytes, got $file_length\n";
862 }
863 # The file was the expected length.
864 else {
865 # Replace the stale file with a fresh copy
866 if ( -e $file ) {
867 # Some dosish systems fail to rename if the target exists
868 chmod 0777, $file;
869 unlink $file;
870 }
871 rename( $tmpfile, $file )
872 or die "Cannot rename '$tmpfile' to '$file': $!\n";
873
874 # make sure the file has the same last modification time
875 if ( my $lm = $response->last_modified ) {
876 utime $lm, $lm, $file;
877 }
878 }
879 }
880 # The local copy is fresh enough, so just delete the temp file
881 else {
882 unlink($tmpfile);
883 }
884 return $response;
885}
886
887
888sub _need_proxy {
889 my($req, $ua) = @_;
890 return if exists $req->{proxy};
891 my $proxy = $ua->{proxy}{$req->uri->scheme} || return;
892 if ($ua->{no_proxy}) {
893 if (my $host = eval { $req->uri->host }) {
894 for my $domain (@{$ua->{no_proxy}}) {
895 if ($host =~ /\Q$domain\E$/) {
896 return;
897 }
898 }
899 }
900 }
901 $req->{proxy} = $HTTP::URI_CLASS->new($proxy);
902}
903
904
905sub proxy
906{
907 my $self = shift;
908 my $key = shift;
909 return map $self->proxy($_, @_), @$key if ref $key;
910
911 Carp::croak("'$key' is not a valid URI scheme") unless $key =~ /^$URI::scheme_re\z/;
912 my $old = $self->{'proxy'}{$key};
913 if (@_) {
914 my $url = shift;
915 if (defined($url) && length($url)) {
916 Carp::croak("Proxy must be specified as absolute URI; '$url' is not") unless $url =~ /^$URI::scheme_re:/;
917 Carp::croak("Bad http proxy specification '$url'") if $url =~ /^https?:/ && $url !~ m,^https?://\w,;
918 }
919 $self->{proxy}{$key} = $url;
920 $self->set_my_handler("request_preprepare", \&_need_proxy)
921 }
922 return $old;
923}
924
925
926sub env_proxy {
927 my ($self) = @_;
928 my($k,$v);
929 while(($k, $v) = each %ENV) {
930 if ($ENV{REQUEST_METHOD}) {
931 # Need to be careful when called in the CGI environment, as
932 # the HTTP_PROXY variable is under control of that other guy.
933 next if $k =~ /^HTTP_/;
934 $k = "HTTP_PROXY" if $k eq "CGI_HTTP_PROXY";
935 }
936 $k = lc($k);
937 next unless $k =~ /^(.*)_proxy$/;
938 $k = $1;
939 if ($k eq 'no') {
940 $self->no_proxy(split(/\s*,\s*/, $v));
941 }
942 else {
943 # Ignore random _proxy variables, allow only valid schemes
944 next unless $k =~ /^$URI::scheme_re\z/;
945 # Ignore xxx_proxy variables if xxx isn't a supported protocol
946 next unless LWP::Protocol::implementor($k);
947 $self->proxy($k, $v);
948 }
949 }
950}
951
952
953sub no_proxy {
954 my($self, @no) = @_;
955 if (@no) {
956 push(@{ $self->{'no_proxy'} }, @no);
957 }
958 else {
959 $self->{'no_proxy'} = [];
960 }
961}
962
963
964sub _new_response {
965 my($request, $code, $message) = @_;
966 my $response = HTTP::Response->new($code, $message);
967 $response->request($request);
968 $response->header("Client-Date" => HTTP::Date::time2str(time));
969 $response->header("Client-Warning" => "Internal response");
970 $response->header("Content-Type" => "text/plain");
971 $response->content("$code $message\n");
972 return $response;
973}
974
975
9761;
977
978__END__
979
980=head1 NAME
981
982LWP::UserAgent - Web user agent class
983
984=head1 SYNOPSIS
985
986 require LWP::UserAgent;
987
988 my $ua = LWP::UserAgent->new;
989 $ua->timeout(10);
990 $ua->env_proxy;
991
992 my $response = $ua->get('http://search.cpan.org/');
993
994 if ($response->is_success) {
995 print $response->decoded_content; # or whatever
996 }
997 else {
998 die $response->status_line;
999 }
1000
1001=head1 DESCRIPTION
1002
1003The C<LWP::UserAgent> is a class implementing a web user agent.
1004C<LWP::UserAgent> objects can be used to dispatch web requests.
1005
1006In normal use the application creates an C<LWP::UserAgent> object, and
1007then configures it with values for timeouts, proxies, name, etc. It
1008then creates an instance of C<HTTP::Request> for the request that
1009needs to be performed. This request is then passed to one of the
1010request method the UserAgent, which dispatches it using the relevant
1011protocol, and returns a C<HTTP::Response> object. There are
1012convenience methods for sending the most common request types: get(),
1013head() and post(). When using these methods then the creation of the
1014request object is hidden as shown in the synopsis above.
1015
1016The basic approach of the library is to use HTTP style communication
1017for all protocol schemes. This means that you will construct
1018C<HTTP::Request> objects and receive C<HTTP::Response> objects even
1019for non-HTTP resources like I<gopher> and I<ftp>. In order to achieve
1020even more similarity to HTTP style communications, gopher menus and
1021file directories are converted to HTML documents.
1022
1023=head1 CONSTRUCTOR METHODS
1024
1025The following constructor methods are available:
1026
1027=over 4
1028
1029=item $ua = LWP::UserAgent->new( %options )
1030
1031This method constructs a new C<LWP::UserAgent> object and returns it.
1032Key/value pair arguments may be provided to set up the initial state.
1033The following options correspond to attribute methods described below:
1034
1035 KEY DEFAULT
1036 ----------- --------------------
1037 agent "libwww-perl/#.###"
1038 from undef
1039 conn_cache undef
1040 cookie_jar undef
1041 default_headers HTTP::Headers->new
1042 local_address undef
1043 max_size undef
1044 max_redirect 7
1045 parse_head 1
1046 protocols_allowed undef
1047 protocols_forbidden undef
1048 requests_redirectable ['GET', 'HEAD']
1049 timeout 180
1050
1051The following additional options are also accepted: If the
1052C<env_proxy> option is passed in with a TRUE value, then proxy
1053settings are read from environment variables (see env_proxy() method
1054below). If the C<keep_alive> option is passed in, then a
1055C<LWP::ConnCache> is set up (see conn_cache() method below). The
1056C<keep_alive> value is passed on as the C<total_capacity> for the
1057connection cache.
1058
1059=item $ua->clone
1060
1061Returns a copy of the LWP::UserAgent object.
1062
1063=back
1064
1065=head1 ATTRIBUTES
1066
1067The settings of the configuration attributes modify the behaviour of the
1068C<LWP::UserAgent> when it dispatches requests. Most of these can also
1069be initialized by options passed to the constructor method.
1070
1071The following attribute methods are provided. The attribute value is
1072left unchanged if no argument is given. The return value from each
1073method is the old attribute value.
1074
1075=over
1076
1077=item $ua->agent
1078
1079=item $ua->agent( $product_id )
1080
1081Get/set the product token that is used to identify the user agent on
1082the network. The agent value is sent as the "User-Agent" header in
1083the requests. The default is the string returned by the _agent()
1084method (see below).
1085
1086If the $product_id ends with space then the _agent() string is
1087appended to it.
1088
1089The user agent string should be one or more simple product identifiers
1090with an optional version number separated by the "/" character.
1091Examples are:
1092
1093 $ua->agent('Checkbot/0.4 ' . $ua->_agent);
1094 $ua->agent('Checkbot/0.4 '); # same as above
1095 $ua->agent('Mozilla/5.0');
1096 $ua->agent(""); # don't identify
1097
1098=item $ua->_agent
1099
1100Returns the default agent identifier. This is a string of the form
1101"libwww-perl/#.###", where "#.###" is substituted with the version number
1102of this library.
1103
1104=item $ua->from
1105
1106=item $ua->from( $email_address )
1107
1108Get/set the e-mail address for the human user who controls
1109the requesting user agent. The address should be machine-usable, as
1110defined in RFC 822. The C<from> value is send as the "From" header in
1111the requests. Example:
1112
1113 $ua->from('[email protected]');
1114
1115The default is to not send a "From" header. See the default_headers()
1116method for the more general interface that allow any header to be defaulted.
1117
1118=item $ua->cookie_jar
1119
1120=item $ua->cookie_jar( $cookie_jar_obj )
1121
1122Get/set the cookie jar object to use. The only requirement is that
1123the cookie jar object must implement the extract_cookies($request) and
1124add_cookie_header($response) methods. These methods will then be
1125invoked by the user agent as requests are sent and responses are
1126received. Normally this will be a C<HTTP::Cookies> object or some
1127subclass.
1128
1129The default is to have no cookie_jar, i.e. never automatically add
1130"Cookie" headers to the requests.
1131
1132Shortcut: If a reference to a plain hash is passed in as the
1133$cookie_jar_object, then it is replaced with an instance of
1134C<HTTP::Cookies> that is initialized based on the hash. This form also
1135automatically loads the C<HTTP::Cookies> module. It means that:
1136
1137 $ua->cookie_jar({ file => "$ENV{HOME}/.cookies.txt" });
1138
1139is really just a shortcut for:
1140
1141 require HTTP::Cookies;
1142 $ua->cookie_jar(HTTP::Cookies->new(file => "$ENV{HOME}/.cookies.txt"));
1143
1144=item $ua->default_headers
1145
1146=item $ua->default_headers( $headers_obj )
1147
1148Get/set the headers object that will provide default header values for
1149any requests sent. By default this will be an empty C<HTTP::Headers>
1150object.
1151
1152=item $ua->default_header( $field )
1153
1154=item $ua->default_header( $field => $value )
1155
1156This is just a short-cut for $ua->default_headers->header( $field =>
1157$value ). Example:
1158
1159 $ua->default_header('Accept-Encoding' => scalar HTTP::Message::decodable());
1160 $ua->default_header('Accept-Language' => "no, en");
1161
1162=item $ua->conn_cache
1163
1164=item $ua->conn_cache( $cache_obj )
1165
1166Get/set the C<LWP::ConnCache> object to use. See L<LWP::ConnCache>
1167for details.
1168
1169=item $ua->credentials( $netloc, $realm )
1170
1171=item $ua->credentials( $netloc, $realm, $uname, $pass )
1172
1173Get/set the user name and password to be used for a realm.
1174
1175The $netloc is a string of the form "<host>:<port>". The username and
1176password will only be passed to this server. Example:
1177
1178 $ua->credentials("www.example.com:80", "Some Realm", "foo", "secret");
1179
1180=item $ua->local_address
1181
1182=item $ua->local_address( $address )
1183
1184Get/set the local interface to bind to for network connections. The interface
1185can be specified as a hostname or an IP address. This value is passed as the
1186C<LocalAddr> argument to L<IO::Socket::INET>.
1187
1188=item $ua->max_size
1189
1190=item $ua->max_size( $bytes )
1191
1192Get/set the size limit for response content. The default is C<undef>,
1193which means that there is no limit. If the returned response content
1194is only partial, because the size limit was exceeded, then a
1195"Client-Aborted" header will be added to the response. The content
1196might end up longer than C<max_size> as we abort once appending a
1197chunk of data makes the length exceed the limit. The "Content-Length"
1198header, if present, will indicate the length of the full content and
1199will normally not be the same as C<< length($res->content) >>.
1200
1201=item $ua->max_redirect
1202
1203=item $ua->max_redirect( $n )
1204
1205This reads or sets the object's limit of how many times it will obey
1206redirection responses in a given request cycle.
1207
1208By default, the value is 7. This means that if you call request()
1209method and the response is a redirect elsewhere which is in turn a
1210redirect, and so on seven times, then LWP gives up after that seventh
1211request.
1212
1213=item $ua->parse_head
1214
1215=item $ua->parse_head( $boolean )
1216
1217Get/set a value indicating whether we should initialize response
1218headers from the E<lt>head> section of HTML documents. The default is
1219TRUE. Do not turn this off, unless you know what you are doing.
1220
1221=item $ua->protocols_allowed
1222
1223=item $ua->protocols_allowed( \@protocols )
1224
1225This reads (or sets) this user agent's list of protocols that the
1226request methods will exclusively allow. The protocol names are case
1227insensitive.
1228
1229For example: C<$ua-E<gt>protocols_allowed( [ 'http', 'https'] );>
1230means that this user agent will I<allow only> those protocols,
1231and attempts to use this user agent to access URLs with any other
1232schemes (like "ftp://...") will result in a 500 error.
1233
1234To delete the list, call: C<$ua-E<gt>protocols_allowed(undef)>
1235
1236By default, an object has neither a C<protocols_allowed> list, nor a
1237C<protocols_forbidden> list.
1238
1239Note that having a C<protocols_allowed> list causes any
1240C<protocols_forbidden> list to be ignored.
1241
1242=item $ua->protocols_forbidden
1243
1244=item $ua->protocols_forbidden( \@protocols )
1245
1246This reads (or sets) this user agent's list of protocols that the
1247request method will I<not> allow. The protocol names are case
1248insensitive.
1249
1250For example: C<$ua-E<gt>protocols_forbidden( [ 'file', 'mailto'] );>
1251means that this user agent will I<not> allow those protocols, and
1252attempts to use this user agent to access URLs with those schemes
1253will result in a 500 error.
1254
1255To delete the list, call: C<$ua-E<gt>protocols_forbidden(undef)>
1256
1257=item $ua->requests_redirectable
1258
1259=item $ua->requests_redirectable( \@requests )
1260
1261This reads or sets the object's list of request names that
1262C<$ua-E<gt>redirect_ok(...)> will allow redirection for. By
1263default, this is C<['GET', 'HEAD']>, as per RFC 2616. To
1264change to include 'POST', consider:
1265
1266 push @{ $ua->requests_redirectable }, 'POST';
1267
1268=item $ua->show_progress
1269
1270=item $ua->show_progress( $boolean )
1271
1272Get/set a value indicating whether a progress bar should be displayed
1273on on the terminal as requests are processed. The default is FALSE.
1274
1275=item $ua->timeout
1276
1277=item $ua->timeout( $secs )
1278
1279Get/set the timeout value in seconds. The default timeout() value is
1280180 seconds, i.e. 3 minutes.
1281
1282The requests is aborted if no activity on the connection to the server
1283is observed for C<timeout> seconds. This means that the time it takes
1284for the complete transaction and the request() method to actually
1285return might be longer.
1286
1287=back
1288
1289=head2 Proxy attributes
1290
1291The following methods set up when requests should be passed via a
1292proxy server.
1293
1294=over
1295
1296=item $ua->proxy(\@schemes, $proxy_url)
1297
1298=item $ua->proxy($scheme, $proxy_url)
1299
1300Set/retrieve proxy URL for a scheme:
1301
1302 $ua->proxy(['http', 'ftp'], 'http://proxy.sn.no:8001/');
1303 $ua->proxy('gopher', 'http://proxy.sn.no:8001/');
1304
1305The first form specifies that the URL is to be used for proxying of
1306access methods listed in the list in the first method argument,
1307i.e. 'http' and 'ftp'.
1308
1309The second form shows a shorthand form for specifying
1310proxy URL for a single access scheme.
1311
1312=item $ua->no_proxy( $domain, ... )
1313
1314Do not proxy requests to the given domains. Calling no_proxy without
1315any domains clears the list of domains. Eg:
1316
1317 $ua->no_proxy('localhost', 'example.com');
1318
1319=item $ua->env_proxy
1320
1321Load proxy settings from *_proxy environment variables. You might
1322specify proxies like this (sh-syntax):
1323
1324 gopher_proxy=http://proxy.my.place/
1325 wais_proxy=http://proxy.my.place/
1326 no_proxy="localhost,example.com"
1327 export gopher_proxy wais_proxy no_proxy
1328
1329csh or tcsh users should use the C<setenv> command to define these
1330environment variables.
1331
1332On systems with case insensitive environment variables there exists a
1333name clash between the CGI environment variables and the C<HTTP_PROXY>
1334environment variable normally picked up by env_proxy(). Because of
1335this C<HTTP_PROXY> is not honored for CGI scripts. The
1336C<CGI_HTTP_PROXY> environment variable can be used instead.
1337
1338=back
1339
1340=head2 Handlers
1341
1342Handlers are code that injected at various phases during the
1343processing of requests. The following methods are provided to manage
1344the active handlers:
1345
1346=over
1347
1348=item $ua->add_handler( $phase => \&cb, %matchspec )
1349
1350Add handler to be invoked in the given processing phase. For how to
1351specify %matchspec see L<HTTP::Config/"Matching">.
1352
1353The possible values $phase and the corresponding callback signatures are:
1354
1355=over
1356
1357=item request_preprepare => sub { my($request, $ua, $h) = @_; ... }
1358
1359The handler is called before the C<request_prepare> and other standard
1360initialization of of the request. This can be used to set up headers
1361and attributes that the C<request_prepare> handler depends on. Proxy
1362initialization should take place here; but in general don't register
1363handlers for this phase.
1364
1365=item request_prepare => sub { my($request, $ua, $h) = @_; ... }
1366
1367The handler is called before the request is sent and can modify the
1368request any way it see fit. This can for instance be used to add
1369certain headers to specific requests.
1370
1371The method can assign a new request object to $_[0] to replace the
1372request that is sent fully.
1373
1374The return value from the callback is ignored. If an exceptions is
1375raised it will abort the request and make the request method return a
1376"400 Bad request" response.
1377
1378=item request_send => sub { my($request, $ua, $h) = @_; ... }
1379
1380This handler get a chance of handling requests before it's sent to the
1381protocol handlers. It should return an HTTP::Response object if it
1382wishes to terminate the processing; otherwise it should return nothing.
1383
1384The C<response_header> and C<response_data> handlers will not be
1385invoked for this response, but the C<response_done> will be.
1386
1387=item response_header => sub { my($response, $ua, $h) = @_; ... }
1388
1389This handler is called right after the response headers have been
1390received, but before any content data. The handler might set up
1391handlers for data and might croak to abort the request.
1392
1393The handler might set the $response->{default_add_content} value to
1394control if any received data should be added to the response object
1395directly. This will initially be false if the $ua->request() method
1396was called with a $content_file or $content_cb argument; otherwise true.
1397
1398=item response_data => sub { my($response, $ua, $h, $data) = @_; ... }
1399
1400This handlers is called for each chunk of data received for the
1401response. The handler might croak to abort the request.
1402
1403This handler need to return a TRUE value to be called again for
1404subsequent chunks for the same request.
1405
1406=item response_done => sub { my($response, $ua, $h) = @_; ... }
1407
1408The handler is called after the response has been fully received, but
1409before any redirect handling is attempted. The handler can be used to
1410extract information or modify the response.
1411
1412=item response_redirect => sub { my($response, $ua, $h) = @_; ... }
1413
1414The handler is called in $ua->request after C<response_done>. If the
1415handler return an HTTP::Request object we'll start over with processing
1416this request instead.
1417
1418=back
1419
1420=item $ua->remove_handler( undef, %matchspec )
1421
1422=item $ua->remove_handler( $phase, %matchspec )
1423
1424Remove handlers that match the given %matchspec. If $phase is not
1425provided remove handlers from all phases.
1426
1427Be careful as calling this function with %matchspec that is not not
1428specific enough can remove handlers not owned by you. It's probably
1429better to use the set_my_handler() method instead.
1430
1431The removed handlers are returned.
1432
1433=item $ua->set_my_handler( $phase, $cb, %matchspec )
1434
1435Set handlers private to the executing subroutine. Works by defaulting
1436an C<owner> field to the %matchspec that holds the name of the called
1437subroutine. You might pass an explicit C<owner> to override this.
1438
1439If $cb is passed as C<undef>, remove the handler.
1440
1441=item $ua->get_my_handler( $phase, %matchspec )
1442
1443=item $ua->get_my_handler( $phase, %matchspec, $init )
1444
1445Will retrieve the matching handler as hash ref.
1446
1447If C<$init> is passed passed as a TRUE value, create and add the
1448handler if it's not found. If $init is a subroutine reference, then
1449it's called with the created handler hash as argument. This sub might
1450populate the hash with extra fields; especially the callback. If
1451$init is a hash reference, merge the hashes.
1452
1453=item $ua->handlers( $phase, $request )
1454
1455=item $ua->handlers( $phase, $response )
1456
1457Returns the handlers that apply to the given request or response at
1458the given processing phase.
1459
1460=back
1461
1462=head1 REQUEST METHODS
1463
1464The methods described in this section are used to dispatch requests
1465via the user agent. The following request methods are provided:
1466
1467=over
1468
1469=item $ua->get( $url )
1470
1471=item $ua->get( $url , $field_name => $value, ... )
1472
1473This method will dispatch a C<GET> request on the given $url. Further
1474arguments can be given to initialize the headers of the request. These
1475are given as separate name/value pairs. The return value is a
1476response object. See L<HTTP::Response> for a description of the
1477interface it provides.
1478
1479There will still be a response object returned when LWP can't connect to the
1480server specified in the URL or when other failures in protocol handlers occur.
1481These internal responses use the standard HTTP status codes, so the responses
1482can't be differentiated by testing the response status code alone. Error
1483responses that LWP generates internally will have the "Client-Warning" header
1484set to the value "Internal response". If you need to differentiate these
1485internal responses from responses that a remote server actually generates, you
1486need to test this header value.
1487
1488Fields names that start with ":" are special. These will not
1489initialize headers of the request but will determine how the response
1490content is treated. The following special field names are recognized:
1491
1492 :content_file => $filename
1493 :content_cb => \&callback
1494 :read_size_hint => $bytes
1495
1496If a $filename is provided with the C<:content_file> option, then the
1497response content will be saved here instead of in the response
1498object. If a callback is provided with the C<:content_cb> option then
1499this function will be called for each chunk of the response content as
1500it is received from the server. If neither of these options are
1501given, then the response content will accumulate in the response
1502object itself. This might not be suitable for very large response
1503bodies. Only one of C<:content_file> or C<:content_cb> can be
1504specified. The content of unsuccessful responses will always
1505accumulate in the response object itself, regardless of the
1506C<:content_file> or C<:content_cb> options passed in.
1507
1508The C<:read_size_hint> option is passed to the protocol module which
1509will try to read data from the server in chunks of this size. A
1510smaller value for the C<:read_size_hint> will result in a higher
1511number of callback invocations.
1512
1513The callback function is called with 3 arguments: a chunk of data, a
1514reference to the response object, and a reference to the protocol
1515object. The callback can abort the request by invoking die(). The
1516exception message will show up as the "X-Died" header field in the
1517response returned by the get() function.
1518
1519=item $ua->head( $url )
1520
1521=item $ua->head( $url , $field_name => $value, ... )
1522
1523This method will dispatch a C<HEAD> request on the given $url.
1524Otherwise it works like the get() method described above.
1525
1526=item $ua->post( $url, \%form )
1527
1528=item $ua->post( $url, \@form )
1529
1530=item $ua->post( $url, \%form, $field_name => $value, ... )
1531
1532=item $ua->post( $url, $field_name => $value,... Content => \%form )
1533
1534=item $ua->post( $url, $field_name => $value,... Content => \@form )
1535
1536=item $ua->post( $url, $field_name => $value,... Content => $content )
1537
1538This method will dispatch a C<POST> request on the given $url, with
1539%form or @form providing the key/value pairs for the fill-in form
1540content. Additional headers and content options are the same as for
1541the get() method.
1542
1543This method will use the POST() function from C<HTTP::Request::Common>
1544to build the request. See L<HTTP::Request::Common> for a details on
1545how to pass form content and other advanced features.
1546
1547=item $ua->mirror( $url, $filename )
1548
1549This method will get the document identified by $url and store it in
1550file called $filename. If the file already exists, then the request
1551will contain an "If-Modified-Since" header matching the modification
1552time of the file. If the document on the server has not changed since
1553this time, then nothing happens. If the document has been updated, it
1554will be downloaded again. The modification time of the file will be
1555forced to match that of the server.
1556
1557The return value is the the response object.
1558
1559=item $ua->request( $request )
1560
1561=item $ua->request( $request, $content_file )
1562
1563=item $ua->request( $request, $content_cb )
1564
1565=item $ua->request( $request, $content_cb, $read_size_hint )
1566
1567This method will dispatch the given $request object. Normally this
1568will be an instance of the C<HTTP::Request> class, but any object with
1569a similar interface will do. The return value is a response object.
1570See L<HTTP::Request> and L<HTTP::Response> for a description of the
1571interface provided by these classes.
1572
1573The request() method will process redirects and authentication
1574responses transparently. This means that it may actually send several
1575simple requests via the simple_request() method described below.
1576
1577The request methods described above; get(), head(), post() and
1578mirror(), will all dispatch the request they build via this method.
1579They are convenience methods that simply hides the creation of the
1580request object for you.
1581
1582The $content_file, $content_cb and $read_size_hint all correspond to
1583options described with the get() method above.
1584
1585You are allowed to use a CODE reference as C<content> in the request
1586object passed in. The C<content> function should return the content
1587when called. The content can be returned in chunks. The content
1588function will be invoked repeatedly until it return an empty string to
1589signal that there is no more content.
1590
1591=item $ua->simple_request( $request )
1592
1593=item $ua->simple_request( $request, $content_file )
1594
1595=item $ua->simple_request( $request, $content_cb )
1596
1597=item $ua->simple_request( $request, $content_cb, $read_size_hint )
1598
1599This method dispatches a single request and returns the response
1600received. Arguments are the same as for request() described above.
1601
1602The difference from request() is that simple_request() will not try to
1603handle redirects or authentication responses. The request() method
1604will in fact invoke this method for each simple request it sends.
1605
1606=item $ua->is_protocol_supported( $scheme )
1607
1608You can use this method to test whether this user agent object supports the
1609specified C<scheme>. (The C<scheme> might be a string (like 'http' or
1610'ftp') or it might be an URI object reference.)
1611
1612Whether a scheme is supported, is determined by the user agent's
1613C<protocols_allowed> or C<protocols_forbidden> lists (if any), and by
1614the capabilities of LWP. I.e., this will return TRUE only if LWP
1615supports this protocol I<and> it's permitted for this particular
1616object.
1617
1618=back
1619
1620=head2 Callback methods
1621
1622The following methods will be invoked as requests are processed. These
1623methods are documented here because subclasses of C<LWP::UserAgent>
1624might want to override their behaviour.
1625
1626=over
1627
1628=item $ua->prepare_request( $request )
1629
1630This method is invoked by simple_request(). Its task is to modify the
1631given $request object by setting up various headers based on the
1632attributes of the user agent. The return value should normally be the
1633$request object passed in. If a different request object is returned
1634it will be the one actually processed.
1635
1636The headers affected by the base implementation are; "User-Agent",
1637"From", "Range" and "Cookie".
1638
1639=item $ua->redirect_ok( $prospective_request, $response )
1640
1641This method is called by request() before it tries to follow a
1642redirection to the request in $response. This should return a TRUE
1643value if this redirection is permissible. The $prospective_request
1644will be the request to be sent if this method returns TRUE.
1645
1646The base implementation will return FALSE unless the method
1647is in the object's C<requests_redirectable> list,
1648FALSE if the proposed redirection is to a "file://..."
1649URL, and TRUE otherwise.
1650
1651=item $ua->get_basic_credentials( $realm, $uri, $isproxy )
1652
1653This is called by request() to retrieve credentials for documents
1654protected by Basic or Digest Authentication. The arguments passed in
1655is the $realm provided by the server, the $uri requested and a boolean
1656flag to indicate if this is authentication against a proxy server.
1657
1658The method should return a username and password. It should return an
1659empty list to abort the authentication resolution attempt. Subclasses
1660can override this method to prompt the user for the information. An
1661example of this can be found in C<lwp-request> program distributed
1662with this library.
1663
1664The base implementation simply checks a set of pre-stored member
1665variables, set up with the credentials() method.
1666
1667=item $ua->progress( $status, $request_or_response )
1668
1669This is called frequently as the response is received regardless of
1670how the content is processed. The method is called with $status
1671"begin" at the start of processing the request and with $state "end"
1672before the request method returns. In between these $status will be
1673the fraction of the response currently received or the string "tick"
1674if the fraction can't be calculated.
1675
1676When $status is "begin" the second argument is the request object,
1677otherwise it is the response object.
1678
1679=back
1680
1681=head1 SEE ALSO
1682
1683See L<LWP> for a complete overview of libwww-perl5. See L<lwpcook>
1684and the scripts F<lwp-request> and F<lwp-download> for examples of
1685usage.
1686
1687See L<HTTP::Request> and L<HTTP::Response> for a description of the
1688message objects dispatched and received. See L<HTTP::Request::Common>
1689and L<HTML::Form> for other ways to build request objects.
1690
1691See L<WWW::Mechanize> and L<WWW::Search> for examples of more
1692specialized user agents based on C<LWP::UserAgent>.
1693
1694=head1 COPYRIGHT
1695
1696Copyright 1995-2009 Gisle Aas.
1697
1698This library is free software; you can redistribute it and/or
1699modify it under the same terms as Perl itself.
Note: See TracBrowser for help on using the repository browser.