home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _2984d035cfb099f5a833d6fe545f7bc1 < prev    next >
Encoding:
Text File  |  2004-06-01  |  40.8 KB  |  1,321 lines

  1. package LWP::UserAgent;
  2.  
  3. # $Id: UserAgent.pm,v 2.31 2004/04/10 11:26:14 gisle Exp $
  4.  
  5. use strict;
  6. use vars qw(@ISA $VERSION);
  7.  
  8. require LWP::MemberMixin;
  9. @ISA = qw(LWP::MemberMixin);
  10. $VERSION = sprintf("%d.%03d", q$Revision: 2.31 $ =~ /(\d+)\.(\d+)/);
  11.  
  12. use HTTP::Request ();
  13. use HTTP::Response ();
  14. use HTTP::Date ();
  15.  
  16. use LWP ();
  17. use LWP::Debug ();
  18. use LWP::Protocol ();
  19.  
  20. use Carp ();
  21.  
  22. if ($ENV{PERL_LWP_USE_HTTP_10}) {
  23.     require LWP::Protocol::http10;
  24.     LWP::Protocol::implementor('http', 'LWP::Protocol::http10');
  25.     eval {
  26.         require LWP::Protocol::https10;
  27.         LWP::Protocol::implementor('https', 'LWP::Protocol::https10');
  28.     };
  29. }
  30.  
  31.  
  32.  
  33. sub new
  34. {
  35.     my($class, %cnf) = @_;
  36.     LWP::Debug::trace('()');
  37.  
  38.     my $agent = delete $cnf{agent};
  39.     $agent = $class->_agent unless defined $agent;
  40.  
  41.     my $from  = delete $cnf{from};
  42.     my $timeout = delete $cnf{timeout};
  43.     $timeout = 3*60 unless defined $timeout;
  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 $max_size = delete $cnf{max_size};
  49.     my $max_redirect = delete $cnf{max_redirect};
  50.     $max_redirect = 7 unless defined $max_redirect;
  51.     my $env_proxy = delete $cnf{env_proxy};
  52.  
  53.     my $cookie_jar = delete $cnf{cookie_jar};
  54.     my $conn_cache = delete $cnf{conn_cache};
  55.     my $keep_alive = delete $cnf{keep_alive};
  56.     
  57.     Carp::croak("Can't mix conn_cache and keep_alive")
  58.       if $conn_cache && $keep_alive;
  59.  
  60.  
  61.     my $protocols_allowed   = delete $cnf{protocols_allowed};
  62.     my $protocols_forbidden = delete $cnf{protocols_forbidden};
  63.     
  64.     my $requests_redirectable = delete $cnf{requests_redirectable};
  65.     $requests_redirectable = ['GET', 'HEAD']
  66.       unless defined $requests_redirectable;
  67.  
  68.     # Actually ""s are just as good as 0's, but for concision we'll just say:
  69.     Carp::croak("protocols_allowed has to be an arrayref or 0, not \"$protocols_allowed\"!")
  70.       if $protocols_allowed and ref($protocols_allowed) ne 'ARRAY';
  71.     Carp::croak("protocols_forbidden has to be an arrayref or 0, not \"$protocols_forbidden\"!")
  72.       if $protocols_forbidden and ref($protocols_forbidden) ne 'ARRAY';
  73.     Carp::croak("requests_redirectable has to be an arrayref or 0, not \"$requests_redirectable\"!")
  74.       if $requests_redirectable and ref($requests_redirectable) ne 'ARRAY';
  75.  
  76.  
  77.     if (%cnf && $^W) {
  78.     Carp::carp("Unrecognized LWP::UserAgent options: @{[sort keys %cnf]}");
  79.     }
  80.  
  81.     my $self = bless {
  82.               from         => $from,
  83.               timeout      => $timeout,
  84.               use_eval     => $use_eval,
  85.               parse_head   => $parse_head,
  86.               max_size     => $max_size,
  87.               max_redirect => $max_redirect,
  88.               proxy        => undef,
  89.               no_proxy     => [],
  90.                       protocols_allowed     => $protocols_allowed,
  91.                       protocols_forbidden   => $protocols_forbidden,
  92.                       requests_redirectable => $requests_redirectable,
  93.              }, $class;
  94.  
  95.     $self->agent($agent) if $agent;
  96.     $self->cookie_jar($cookie_jar) if $cookie_jar;
  97.     $self->env_proxy if $env_proxy;
  98.  
  99.     $self->protocols_allowed(  $protocols_allowed  ) if $protocols_allowed;
  100.     $self->protocols_forbidden($protocols_forbidden) if $protocols_forbidden;
  101.  
  102.     if ($keep_alive) {
  103.     $conn_cache ||= { total_capacity => $keep_alive };
  104.     }
  105.     $self->conn_cache($conn_cache) if $conn_cache;
  106.  
  107.     return $self;
  108. }
  109.  
  110.  
  111. # private method.  check sanity of given $request
  112. sub _request_sanity_check {
  113.     my($self, $request) = @_;
  114.     # some sanity checking
  115.     if (defined $request) {
  116.     if (ref $request) {
  117.         Carp::croak("You need a request object, not a " . ref($request) . " object")
  118.           if ref($request) eq 'ARRAY' or ref($request) eq 'HASH' or
  119.          !$request->can('method') or !$request->can('uri');
  120.     }
  121.     else {
  122.         Carp::croak("You need a request object, not '$request'");
  123.     }
  124.     }
  125.     else {
  126.         Carp::croak("No request object passed in");
  127.     }
  128. }
  129.  
  130.  
  131. sub send_request
  132. {
  133.     my($self, $request, $arg, $size) = @_;
  134.     $self->_request_sanity_check($request);
  135.  
  136.     my($method, $url) = ($request->method, $request->uri);
  137.  
  138.     local($SIG{__DIE__});  # protect against user defined die handlers
  139.  
  140.     # Check that we have a METHOD and a URL first
  141.     return _new_response($request, &HTTP::Status::RC_BAD_REQUEST, "Method missing")
  142.     unless $method;
  143.     return _new_response($request, &HTTP::Status::RC_BAD_REQUEST, "URL missing")
  144.     unless $url;
  145.     return _new_response($request, &HTTP::Status::RC_BAD_REQUEST, "URL must be absolute")
  146.     unless $url->scheme;
  147.  
  148.     LWP::Debug::trace("$method $url");
  149.  
  150.     # Locate protocol to use
  151.     my $scheme = '';
  152.     my $proxy = $self->_need_proxy($url);
  153.     if (defined $proxy) {
  154.     $scheme = $proxy->scheme;
  155.     }
  156.     else {
  157.     $scheme = $url->scheme;
  158.     }
  159.  
  160.     my $protocol;
  161.  
  162.     {
  163.       # Honor object-specific restrictions by forcing protocol objects
  164.       #  into class LWP::Protocol::nogo.
  165.       my $x;
  166.       if($x       = $self->protocols_allowed) {
  167.         if(grep lc($_) eq $scheme, @$x) {
  168.           LWP::Debug::trace("$scheme URLs are among $self\'s allowed protocols (@$x)");
  169.         }
  170.         else {
  171.           LWP::Debug::trace("$scheme URLs aren't among $self\'s allowed protocols (@$x)");
  172.           require LWP::Protocol::nogo;
  173.           $protocol = LWP::Protocol::nogo->new;
  174.         }
  175.       }
  176.       elsif ($x = $self->protocols_forbidden) {
  177.         if(grep lc($_) eq $scheme, @$x) {
  178.           LWP::Debug::trace("$scheme URLs are among $self\'s forbidden protocols (@$x)");
  179.           require LWP::Protocol::nogo;
  180.           $protocol = LWP::Protocol::nogo->new;
  181.         }
  182.         else {
  183.           LWP::Debug::trace("$scheme URLs aren't among $self\'s forbidden protocols (@$x)");
  184.         }
  185.       }
  186.       # else fall thru and create the protocol object normally
  187.     }
  188.  
  189.     unless($protocol) {
  190.       $protocol = eval { LWP::Protocol::create($scheme, $self) };
  191.       if ($@) {
  192.     $@ =~ s/ at .* line \d+.*//s;  # remove file/line number
  193.     my $response =  _new_response($request, &HTTP::Status::RC_NOT_IMPLEMENTED, $@);
  194.     if ($scheme eq "https") {
  195.         $response->message($response->message . " (Crypt::SSLeay not installed)");
  196.         $response->content_type("text/plain");
  197.         $response->content(<<EOT);
  198. LWP will support https URLs if the Crypt::SSLeay module is installed.
  199. More information at <http://www.linpro.no/lwp/libwww-perl/README.SSL>.
  200. EOT
  201.     }
  202.     return $response;
  203.       }
  204.     }
  205.  
  206.     # Extract fields that will be used below
  207.     my ($timeout, $cookie_jar, $use_eval, $parse_head, $max_size) =
  208.       @{$self}{qw(timeout cookie_jar use_eval parse_head max_size)};
  209.  
  210.     my $response;
  211.     if ($use_eval) {
  212.     # we eval, and turn dies into responses below
  213.     eval {
  214.         $response = $protocol->request($request, $proxy,
  215.                        $arg, $size, $timeout);
  216.     };
  217.     if ($@) {
  218.         $@ =~ s/ at .* line \d+.*//s;  # remove file/line number
  219.         $response = _new_response($request,
  220.                       &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
  221.                       $@);
  222.     }
  223.     }
  224.     else {
  225.     $response = $protocol->request($request, $proxy,
  226.                        $arg, $size, $timeout);
  227.     # XXX: Should we die unless $response->is_success ???
  228.     }
  229.  
  230.     $response->request($request);  # record request for reference
  231.     $cookie_jar->extract_cookies($response) if $cookie_jar;
  232.     $response->header("Client-Date" => HTTP::Date::time2str(time));
  233.     return $response;
  234. }
  235.  
  236.  
  237. sub prepare_request
  238. {
  239.     my($self, $request) = @_;
  240.     $self->_request_sanity_check($request);
  241.  
  242.     # Extract fields that will be used below
  243.     my ($agent, $from, $cookie_jar, $max_size) =
  244.       @{$self}{qw(agent from cookie_jar max_size)};
  245.  
  246.     # Set User-Agent and From headers if they are defined
  247.     $request->init_header('User-Agent' => $agent) if $agent;
  248.     $request->init_header('From' => $from) if $from;
  249.     if (defined $max_size) {
  250.     my $last = $max_size - 1;
  251.     $last = 0 if $last < 0;  # there is no way to actually request no content
  252.     $request->init_header('Range' => "bytes=0-$last");
  253.     }
  254.     $cookie_jar->add_cookie_header($request) if $cookie_jar;
  255.  
  256.     return($request);
  257. }
  258.  
  259.  
  260. sub simple_request
  261. {
  262.     my($self, $request, $arg, $size) = @_;
  263.     $self->_request_sanity_check($request);
  264.     my $new_request = $self->prepare_request($request);
  265.     return($self->send_request($new_request, $arg, $size));
  266. }
  267.  
  268.  
  269. sub request
  270. {
  271.     my($self, $request, $arg, $size, $previous) = @_;
  272.  
  273.     LWP::Debug::trace('()');
  274.  
  275.     my $response = $self->simple_request($request, $arg, $size);
  276.  
  277.     my $code = $response->code;
  278.     $response->previous($previous) if defined $previous;
  279.  
  280.     LWP::Debug::debug('Simple response: ' .
  281.               (HTTP::Status::status_message($code) ||
  282.                "Unknown code $code"));
  283.  
  284.     if ($code == &HTTP::Status::RC_MOVED_PERMANENTLY or
  285.     $code == &HTTP::Status::RC_FOUND or
  286.     $code == &HTTP::Status::RC_SEE_OTHER or
  287.     $code == &HTTP::Status::RC_TEMPORARY_REDIRECT)
  288.     {
  289.     my $referral = $request->clone;
  290.  
  291.     # These headers should never be forwarded
  292.     $referral->remove_header('Host', 'Cookie');
  293.     
  294.     if ($referral->header('Referer') &&
  295.         $request->url->scheme eq 'https' &&
  296.         $referral->url->scheme eq 'http')
  297.     {
  298.         # RFC 2616, section 15.1.3.
  299.         LWP::Debug::trace("https -> http redirect, suppressing Referer");
  300.         $referral->remove_header('Referer');
  301.     }
  302.  
  303.     if ($code == &HTTP::Status::RC_SEE_OTHER ||
  304.         $code == &HTTP::Status::RC_FOUND) 
  305.         {
  306.         my $method = uc($referral->method);
  307.         unless ($method eq "GET" || $method eq "HEAD") {
  308.         $referral->method("GET");
  309.         $referral->content("");
  310.         $referral->remove_content_headers;
  311.         }
  312.     }
  313.  
  314.     # And then we update the URL based on the Location:-header.
  315.     my $referral_uri = $response->header('Location');
  316.     {
  317.         # Some servers erroneously return a relative URL for redirects,
  318.         # so make it absolute if it not already is.
  319.         local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
  320.         my $base = $response->base;
  321.         $referral_uri = "" unless defined $referral_uri;
  322.         $referral_uri = $HTTP::URI_CLASS->new($referral_uri, $base)
  323.                     ->abs($base);
  324.     }
  325.     $referral->url($referral_uri);
  326.  
  327.     # Check for loop in the redirects, we only count
  328.     my $count = 0;
  329.     my $r = $response;
  330.     while ($r) {
  331.         if (++$count > $self->{max_redirect}) {
  332.         $response->header("Client-Warning" =>
  333.                   "Redirect loop detected (max_redirect = $self->{max_redirect})");
  334.         return $response;
  335.         }
  336.         $r = $r->previous;
  337.     }
  338.  
  339.     return $response unless $self->redirect_ok($referral, $response);
  340.     return $self->request($referral, $arg, $size, $response);
  341.  
  342.     }
  343.     elsif ($code == &HTTP::Status::RC_UNAUTHORIZED ||
  344.          $code == &HTTP::Status::RC_PROXY_AUTHENTICATION_REQUIRED
  345.         )
  346.     {
  347.     my $proxy = ($code == &HTTP::Status::RC_PROXY_AUTHENTICATION_REQUIRED);
  348.     my $ch_header = $proxy ?  "Proxy-Authenticate" : "WWW-Authenticate";
  349.     my @challenge = $response->header($ch_header);
  350.     unless (@challenge) {
  351.         $response->header("Client-Warning" => 
  352.                   "Missing Authenticate header");
  353.         return $response;
  354.     }
  355.  
  356.     require HTTP::Headers::Util;
  357.     CHALLENGE: for my $challenge (@challenge) {
  358.         $challenge =~ tr/,/;/;  # "," is used to separate auth-params!!
  359.         ($challenge) = HTTP::Headers::Util::split_header_words($challenge);
  360.         my $scheme = lc(shift(@$challenge));
  361.         shift(@$challenge); # no value
  362.         $challenge = { @$challenge };  # make rest into a hash
  363.         for (keys %$challenge) {       # make sure all keys are lower case
  364.         $challenge->{lc $_} = delete $challenge->{$_};
  365.         }
  366.  
  367.         unless ($scheme =~ /^([a-z]+(?:-[a-z]+)*)$/) {
  368.         $response->header("Client-Warning" => 
  369.                   "Bad authentication scheme '$scheme'");
  370.         return $response;
  371.         }
  372.         $scheme = $1;  # untainted now
  373.         my $class = "LWP::Authen::\u$scheme";
  374.         $class =~ s/-/_/g;
  375.  
  376.         no strict 'refs';
  377.         unless (%{"$class\::"}) {
  378.         # try to load it
  379.         eval "require $class";
  380.         if ($@) {
  381.             if ($@ =~ /^Can\'t locate/) {
  382.             $response->header("Client-Warning" =>
  383.                       "Unsupported authentication scheme '$scheme'");
  384.             }
  385.             else {
  386.             $response->header("Client-Warning" => $@);
  387.             }
  388.             next CHALLENGE;
  389.         }
  390.         }
  391.         unless ($class->can("authenticate")) {
  392.         $response->header("Client-Warning" =>
  393.                   "Unsupported authentication scheme '$scheme'");
  394.         next CHALLENGE;
  395.         }
  396.         return $class->authenticate($self, $proxy, $challenge, $response,
  397.                     $request, $arg, $size);
  398.     }
  399.     return $response;
  400.     }
  401.     return $response;
  402. }
  403.  
  404.  
  405. #
  406. # Now the shortcuts...
  407. #
  408. sub get {
  409.     require HTTP::Request::Common;
  410.     my($self, @parameters) = @_;
  411.     my @suff = $self->_process_colonic_headers(\@parameters,1);
  412.     return $self->request( HTTP::Request::Common::GET( @parameters ), @suff );
  413. }
  414.  
  415.  
  416. sub post {
  417.     require HTTP::Request::Common;
  418.     my($self, @parameters) = @_;
  419.     my @suff = $self->_process_colonic_headers(\@parameters,2);
  420.     return $self->request( HTTP::Request::Common::POST( @parameters ), @suff );
  421. }
  422.  
  423.  
  424. sub head {
  425.     require HTTP::Request::Common;
  426.     my($self, @parameters) = @_;
  427.     my @suff = $self->_process_colonic_headers(\@parameters,1);
  428.     return $self->request( HTTP::Request::Common::HEAD( @parameters ), @suff );
  429. }
  430.  
  431.  
  432. sub _process_colonic_headers {
  433.     # Process :content_cb / :content_file / :read_size_hint headers.
  434.     my($self, $args, $start_index) = @_;
  435.  
  436.     my($arg, $size);
  437.     for(my $i = $start_index; $i < @$args; $i += 2) {
  438.     next unless defined $args->[$i];
  439.  
  440.     #printf "Considering %s => %s\n", $args->[$i], $args->[$i + 1];
  441.  
  442.     if($args->[$i] eq ':content_cb') {
  443.         # Some sanity-checking...
  444.         $arg = $args->[$i + 1];
  445.         Carp::croak("A :content_cb value can't be undef") unless defined $arg;
  446.         Carp::croak("A :content_cb value must be a coderef")
  447.         unless ref $arg and UNIVERSAL::isa($arg, 'CODE');
  448.         
  449.     }
  450.     elsif ($args->[$i] eq ':content_file') {
  451.         $arg = $args->[$i + 1];
  452.  
  453.         # Some sanity-checking...
  454.         Carp::croak("A :content_file value can't be undef")
  455.         unless defined $arg;
  456.         Carp::croak("A :content_file value can't be a reference")
  457.         if ref $arg;
  458.         Carp::croak("A :content_file value can't be \"\"")
  459.         unless length $arg;
  460.  
  461.     }
  462.     elsif ($args->[$i] eq ':read_size_hint') {
  463.         $size = $args->[$i + 1];
  464.         # Bother checking it?
  465.  
  466.     }
  467.     else {
  468.         next;
  469.     }
  470.     splice @$args, $i, 2;
  471.     $i -= 2;
  472.     }
  473.  
  474.     # And return a suitable suffix-list for request(REQ,...)
  475.  
  476.     return             unless defined $arg;
  477.     return $arg, $size if     defined $size;
  478.     return $arg;
  479. }
  480.  
  481.  
  482. #
  483. # This whole allow/forbid thing is based on man 1 at's way of doing things.
  484. #
  485. sub is_protocol_supported
  486. {
  487.     my($self, $scheme) = @_;
  488.     if (ref $scheme) {
  489.     # assume we got a reference to an URI object
  490.     $scheme = $scheme->scheme;
  491.     }
  492.     else {
  493.     Carp::croak("Illegal scheme '$scheme' passed to is_protocol_supported")
  494.         if $scheme =~ /\W/;
  495.     $scheme = lc $scheme;
  496.     }
  497.  
  498.     my $x;
  499.     if(ref($self) and $x       = $self->protocols_allowed) {
  500.       return 0 unless grep lc($_) eq $scheme, @$x;
  501.     }
  502.     elsif (ref($self) and $x = $self->protocols_forbidden) {
  503.       return 0 if grep lc($_) eq $scheme, @$x;
  504.     }
  505.  
  506.     local($SIG{__DIE__});  # protect against user defined die handlers
  507.     $x = LWP::Protocol::implementor($scheme);
  508.     return 1 if $x and $x ne 'LWP::Protocol::nogo';
  509.     return 0;
  510. }
  511.  
  512.  
  513. sub protocols_allowed      { shift->_elem('protocols_allowed'    , @_) }
  514. sub protocols_forbidden    { shift->_elem('protocols_forbidden'  , @_) }
  515. sub requests_redirectable  { shift->_elem('requests_redirectable', @_) }
  516.  
  517.  
  518. sub redirect_ok
  519. {
  520.     # RFC 2616, section 10.3.2 and 10.3.3 say:
  521.     #  If the 30[12] status code is received in response to a request other
  522.     #  than GET or HEAD, the user agent MUST NOT automatically redirect the
  523.     #  request unless it can be confirmed by the user, since this might
  524.     #  change the conditions under which the request was issued.
  525.  
  526.     # Note that this routine used to be just:
  527.     #  return 0 if $_[1]->method eq "POST";  return 1;
  528.  
  529.     my($self, $new_request, $response) = @_;
  530.     my $method = $response->request->method;
  531.     return 0 unless grep $_ eq $method,
  532.       @{ $self->requests_redirectable || [] };
  533.     
  534.     if ($new_request->url->scheme eq 'file') {
  535.       $response->header("Client-Warning" =>
  536.             "Can't redirect to a file:// URL!");
  537.       return 0;
  538.     }
  539.     
  540.     # Otherwise it's apparently okay...
  541.     return 1;
  542. }
  543.  
  544.  
  545. sub credentials
  546. {
  547.     my($self, $netloc, $realm, $uid, $pass) = @_;
  548.     @{ $self->{'basic_authentication'}{lc($netloc)}{$realm} } =
  549.     ($uid, $pass);
  550. }
  551.  
  552.  
  553. sub get_basic_credentials
  554. {
  555.     my($self, $realm, $uri, $proxy) = @_;
  556.     return if $proxy;
  557.  
  558.     my $host_port = lc($uri->host_port);
  559.     if (exists $self->{'basic_authentication'}{$host_port}{$realm}) {
  560.     return @{ $self->{'basic_authentication'}{$host_port}{$realm} };
  561.     }
  562.  
  563.     return (undef, undef);
  564. }
  565.  
  566.  
  567. sub agent {
  568.     my $self = shift;
  569.     my $old = $self->{agent};
  570.     if (@_) {
  571.     my $agent = shift;
  572.     $agent .= $self->_agent if $agent && $agent =~ /\s+$/;
  573.     $self->{agent} = $agent;
  574.     }
  575.     $old;
  576. }
  577.  
  578.  
  579. sub _agent       { "libwww-perl/$LWP::VERSION" }
  580.  
  581. sub timeout      { shift->_elem('timeout',      @_); }
  582. sub from         { shift->_elem('from',         @_); }
  583. sub parse_head   { shift->_elem('parse_head',   @_); }
  584. sub max_size     { shift->_elem('max_size',     @_); }
  585. sub max_redirect { shift->_elem('max_redirect', @_); }
  586.  
  587.  
  588. sub cookie_jar {
  589.     my $self = shift;
  590.     my $old = $self->{cookie_jar};
  591.     if (@_) {
  592.     my $jar = shift;
  593.     if (ref($jar) eq "HASH") {
  594.         require HTTP::Cookies;
  595.         $jar = HTTP::Cookies->new(%$jar);
  596.     }
  597.     $self->{cookie_jar} = $jar;
  598.     }
  599.     $old;
  600. }
  601.  
  602.  
  603. sub conn_cache {
  604.     my $self = shift;
  605.     my $old = $self->{conn_cache};
  606.     if (@_) {
  607.     my $cache = shift;
  608.     if (ref($cache) eq "HASH") {
  609.         require LWP::ConnCache;
  610.         $cache = LWP::ConnCache->new(%$cache);
  611.     }
  612.     $self->{conn_cache} = $cache;
  613.     }
  614.     $old;
  615. }
  616.  
  617.  
  618. # depreciated
  619. sub use_eval   { shift->_elem('use_eval',  @_); }
  620. sub use_alarm
  621. {
  622.     Carp::carp("LWP::UserAgent->use_alarm(BOOL) is a no-op")
  623.     if @_ > 1 && $^W;
  624.     "";
  625. }
  626.  
  627.  
  628. sub clone
  629. {
  630.     my $self = shift;
  631.     my $copy = bless { %$self }, ref $self;  # copy most fields
  632.  
  633.     # elements that are references must be handled in a special way
  634.     $copy->{'proxy'} = { %{$self->{'proxy'}} };
  635.     $copy->{'no_proxy'} = [ @{$self->{'no_proxy'}} ];  # copy array
  636.  
  637.     # remove reference to objects for now
  638.     delete $copy->{cookie_jar};
  639.     delete $copy->{conn_cache};
  640.  
  641.     $copy;
  642. }
  643.  
  644.  
  645. sub mirror
  646. {
  647.     my($self, $url, $file) = @_;
  648.  
  649.     LWP::Debug::trace('()');
  650.     my $request = HTTP::Request->new('GET', $url);
  651.  
  652.     if (-e $file) {
  653.     my($mtime) = (stat($file))[9];
  654.     if($mtime) {
  655.         $request->header('If-Modified-Since' =>
  656.                  HTTP::Date::time2str($mtime));
  657.     }
  658.     }
  659.     my $tmpfile = "$file-$$";
  660.  
  661.     my $response = $self->request($request, $tmpfile);
  662.     if ($response->is_success) {
  663.  
  664.     my $file_length = (stat($tmpfile))[7];
  665.     my($content_length) = $response->header('Content-length');
  666.  
  667.     if (defined $content_length and $file_length < $content_length) {
  668.         unlink($tmpfile);
  669.         die "Transfer truncated: " .
  670.         "only $file_length out of $content_length bytes received\n";
  671.     }
  672.     elsif (defined $content_length and $file_length > $content_length) {
  673.         unlink($tmpfile);
  674.         die "Content-length mismatch: " .
  675.         "expected $content_length bytes, got $file_length\n";
  676.     }
  677.     else {
  678.         # OK
  679.         if (-e $file) {
  680.         # Some dosish systems fail to rename if the target exists
  681.         chmod 0777, $file;
  682.         unlink $file;
  683.         }
  684.         rename($tmpfile, $file) or
  685.         die "Cannot rename '$tmpfile' to '$file': $!\n";
  686.  
  687.         if (my $lm = $response->last_modified) {
  688.         # make sure the file has the same last modification time
  689.         utime $lm, $lm, $file;
  690.         }
  691.     }
  692.     }
  693.     else {
  694.     unlink($tmpfile);
  695.     }
  696.     return $response;
  697. }
  698.  
  699.  
  700. sub proxy
  701. {
  702.     my $self = shift;
  703.     my $key  = shift;
  704.  
  705.     LWP::Debug::trace("$key @_");
  706.  
  707.     return map $self->proxy($_, @_), @$key if ref $key;
  708.  
  709.     my $old = $self->{'proxy'}{$key};
  710.     $self->{'proxy'}{$key} = shift if @_;
  711.     return $old;
  712. }
  713.  
  714.  
  715. sub env_proxy {
  716.     my ($self) = @_;
  717.     my($k,$v);
  718.     while(($k, $v) = each %ENV) {
  719.     if ($ENV{REQUEST_METHOD}) {
  720.         # Need to be careful when called in the CGI environment, as
  721.         # the HTTP_PROXY variable is under control of that other guy.
  722.         next if $k =~ /^HTTP_/;
  723.         $k = "HTTP_PROXY" if $k eq "CGI_HTTP_PROXY";
  724.     }
  725.     $k = lc($k);
  726.     next unless $k =~ /^(.*)_proxy$/;
  727.     $k = $1;
  728.     if ($k eq 'no') {
  729.         $self->no_proxy(split(/\s*,\s*/, $v));
  730.     }
  731.     else {
  732.         $self->proxy($k, $v);
  733.     }
  734.     }
  735. }
  736.  
  737.  
  738. sub no_proxy {
  739.     my($self, @no) = @_;
  740.     if (@no) {
  741.     push(@{ $self->{'no_proxy'} }, @no);
  742.     }
  743.     else {
  744.     $self->{'no_proxy'} = [];
  745.     }
  746. }
  747.  
  748.  
  749. # Private method which returns the URL of the Proxy configured for this
  750. # URL, or undefined if none is configured.
  751. sub _need_proxy
  752. {
  753.     my($self, $url) = @_;
  754.     $url = $HTTP::URI_CLASS->new($url) unless ref $url;
  755.  
  756.     my $scheme = $url->scheme || return;
  757.     if (my $proxy = $self->{'proxy'}{$scheme}) {
  758.     if (@{ $self->{'no_proxy'} }) {
  759.         if (my $host = eval { $url->host }) {
  760.         for my $domain (@{ $self->{'no_proxy'} }) {
  761.             if ($host =~ /\Q$domain\E$/) {
  762.             LWP::Debug::trace("no_proxy configured");
  763.             return;
  764.             }
  765.         }
  766.         }
  767.     }
  768.     LWP::Debug::debug("Proxied to $proxy");
  769.     return $HTTP::URI_CLASS->new($proxy);
  770.     }
  771.     LWP::Debug::debug('Not proxied');
  772.     undef;
  773. }
  774.  
  775.  
  776. sub _new_response {
  777.     my($request, $code, $message) = @_;
  778.     my $response = HTTP::Response->new($code, $message);
  779.     $response->request($request);
  780.     $response->header("Client-Date" => HTTP::Date::time2str(time));
  781.     $response->header("Client-Warning" => "Internal response");
  782.     $response->header("Content-Type" => "text/plain");
  783.     $response->content("$code $message\n");
  784.     return $response;
  785. }
  786.  
  787.  
  788. 1;
  789.  
  790. __END__
  791.  
  792. =head1 NAME
  793.  
  794. LWP::UserAgent - Web user agent class
  795.  
  796. =head1 SYNOPSIS
  797.  
  798.  require LWP::UserAgent;
  799.  
  800.  my $ua = LWP::UserAgent->new;
  801.  $ua->timeout(10);
  802.  $ua->env_proxy;
  803.  
  804.  my $response = $ua->get('http://search.cpan.org/');
  805.  
  806.  if ($response->is_success) {
  807.      print $response->content;  # or whatever
  808.  }
  809.  else {
  810.      die $response->status_line;
  811.  }
  812.  
  813. =head1 DESCRIPTION
  814.  
  815. The C<LWP::UserAgent> is a class implementing a web user agent.
  816. C<LWP::UserAgent> objects can be used to dispatch web requests.
  817.  
  818. In normal use the application creates an C<LWP::UserAgent> object, and
  819. then configures it with values for timeouts, proxies, name, etc. It
  820. then creates an instance of C<HTTP::Request> for the request that
  821. needs to be performed. This request is then passed to one of the
  822. request method the UserAgent, which dispatches it using the relevant
  823. protocol, and returns a C<HTTP::Response> object.  There are
  824. convenience methods for sending the most common request types: get(),
  825. head() and post().  When using these methods then the creation of the
  826. request object is hidden as shown in the synopsis above.
  827.  
  828. The basic approach of the library is to use HTTP style communication
  829. for all protocol schemes.  This means that you will construct
  830. C<HTTP::Request> objects and receive C<HTTP::Response> objects even
  831. for non-HTTP resources like I<gopher> and I<ftp>.  In order to achieve
  832. even more similarity to HTTP style communications, gopher menus and
  833. file directories are converted to HTML documents.
  834.  
  835. =head1 CONSTRUCTOR METHODS
  836.  
  837. The following constructor methods are available:
  838.  
  839. =over 4
  840.  
  841. =item $ua = LWP::UserAgent->new( %options )
  842.  
  843. This method constructs a new C<LWP::UserAgent> object and returns it.
  844. Key/value pair arguments may be provided to set up the initial state.
  845. The following options correspond to attribute methods described below:
  846.  
  847.    KEY                     DEFAULT
  848.    -----------             --------------------
  849.    agent                   "libwww-perl/#.##"
  850.    from                    undef
  851.    conn_cache              undef
  852.    cookie_jar              undef
  853.    max_size                undef
  854.    max_redirect            7
  855.    parse_head              1
  856.    protocols_allowed       undef
  857.    protocols_forbidden     undef
  858.    requests_redirectable   ['GET', 'HEAD']
  859.    timeout                 180
  860.  
  861. The following additional options are also accepted: If the
  862. C<env_proxy> option is passed in with a TRUE value, then proxy
  863. settings are read from environment variables (see env_proxy() method
  864. below).  If the C<keep_alive> option is passed in, then a
  865. C<LWP::ConnCache> is set up (see conn_cache() method below).  The
  866. C<keep_alive> value is passed on as the C<total_capacity> for the
  867. connection cache.
  868.  
  869. =item $ua->clone
  870.  
  871. Returns a copy of the LWP::UserAgent object.
  872.  
  873. =back
  874.  
  875. =head1 ATTRIBUTES
  876.  
  877. The settings of the configuration attributes modify the behaviour of the
  878. C<LWP::UserAgent> when it dispatches requests.  Most of these can also
  879. be initialized by options passed to the constructor method.
  880.  
  881. The following attributes methods are provided.  The attribute value is
  882. left unchanged if no argument is given.  The return value from each
  883. method is the old attribute value.
  884.  
  885. =over
  886.  
  887. =item $ua->agent
  888.  
  889. =item $ua->agent( $product_id )
  890.  
  891. Get/set the product token that is used to identify the user agent on
  892. the network.  The agent value is sent as the "User-Agent" header in
  893. the requests.  The default is the string returned by the _agent()
  894. method (see below).
  895.  
  896. If the $product_id ends with space then the _agent() string is
  897. appended to it.
  898.  
  899. The user agent string should be one or more simple product identifiers
  900. with an optional version number separated by the "/" character.
  901. Examples are:
  902.  
  903.   $ua->agent('Checkbot/0.4 ' . $ua->_agent);
  904.   $ua->agent('Checkbot/0.4 ');    # same as above
  905.   $ua->agent('Mozilla/5.0');
  906.   $ua->agent("");                 # don't identify
  907.  
  908. =item $ua->_agent
  909.  
  910. Returns the default agent identifier.  This is a string of the form
  911. "libwww-perl/#.##", where "#.##" is substituted with the version number
  912. of this library.
  913.  
  914. =item $ua->from
  915.  
  916. =item $ua->from( $email_address )
  917.  
  918. Get/set the e-mail address for the human user who controls
  919. the requesting user agent.  The address should be machine-usable, as
  920. defined in RFC 822.  The C<from> value is send as the "From" header in
  921. the requests.  Example:
  922.  
  923.   $ua->from('gaas@cpan.org');
  924.  
  925. The default is to not send a "From" header.
  926.  
  927. =item $ua->cookie_jar
  928.  
  929. =item $ua->cookie_jar( $cookie_jar_obj )
  930.  
  931. Get/set the cookie jar object to use.  The only requirement is that
  932. the cookie jar object must implement the extract_cookies($request) and
  933. add_cookie_header($response) methods.  These methods will then be
  934. invoked by the user agent as requests are sent and responses are
  935. received.  Normally this will be a C<HTTP::Cookies> object or some
  936. subclass.
  937.  
  938. The default is to have no cookie_jar, i.e. never automatically add
  939. "Cookie" headers to the requests.
  940.  
  941. Shortcut: If a reference to a plain hash is passed in as the
  942. $cookie_jar_object, then it is replaced with an instance of
  943. C<HTTP::Cookies> that is initialized based on the hash.  This form also
  944. automatically loads the C<HTTP::Cookies> module.  It means that:
  945.  
  946.   $ua->cookie_jar({ file => "$ENV{HOME}/.cookies.txt" });
  947.  
  948. is really just a shortcut for:
  949.  
  950.   require HTTP::Cookies;
  951.   $ua->cookie_jar(HTTP::Cookies->new(file => "$ENV{HOME}/.cookies.txt"));
  952.  
  953. =item $ua->conn_cache
  954.  
  955. =item $ua->conn_cache( $cache_obj )
  956.  
  957. Get/set the C<LWP::ConnCache> object to use.  See L<LWP::ConnCache>
  958. for details.
  959.  
  960. =item $ua->credentials( $netloc, $realm, $uname, $pass )
  961.  
  962. Set the user name and password to be used for a realm.  It is often more
  963. useful to specialize the get_basic_credentials() method instead.
  964.  
  965. =item $ua->max_size
  966.  
  967. =item $ua->max_size( $bytes )
  968.  
  969. Get/set the size limit for response content.  The default is C<undef>,
  970. which means that there is no limit.  If the returned response content
  971. is only partial, because the size limit was exceeded, then a
  972. "Client-Aborted" header will be added to the response.  The content
  973. might end up longer than C<max_size> as we abort once appending a
  974. chunk of data makes the length exceed the limit.  The "Content-Length"
  975. header, if present, will indicate the length of the full content and
  976. will normally not be the same as C<< length($res->content) >>.
  977.  
  978. =item $ua->max_redirect
  979.  
  980. =item $ua->max_redirect( $n )
  981.  
  982. This reads or sets the object's limit of how many times it will obey
  983. redirection responses in a given request cycle.
  984.  
  985. By default, the value is 7. This means that if you call request()
  986. method and the response is a redirect elsewhere which is in turn a
  987. redirect, and so on seven times, then LWP gives up after that seventh
  988. request.
  989.  
  990. =item $ua->parse_head
  991.  
  992. =item $ua->parse_head( $boolean )
  993.  
  994. Get/set a value indicating whether we should initialize response
  995. headers from the E<lt>head> section of HTML documents. The default is
  996. TRUE.  Do not turn this off, unless you know what you are doing.
  997.  
  998. =item $ua->protocols_allowed
  999.  
  1000. =item $ua->protocols_allowed( \@protocols )
  1001.  
  1002. This reads (or sets) this user agent's list of protocols that the
  1003. request methods will exclusively allow.  The protocol names are case
  1004. insensitive.
  1005.  
  1006. For example: C<$ua-E<gt>protocols_allowed( [ 'http', 'https'] );>
  1007. means that this user agent will I<allow only> those protocols,
  1008. and attempts to use this user agent to access URLs with any other
  1009. schemes (like "ftp://...") will result in a 500 error.
  1010.  
  1011. To delete the list, call: C<$ua-E<gt>protocols_allowed(undef)>
  1012.  
  1013. By default, an object has neither a C<protocols_allowed> list, nor a
  1014. C<protocols_forbidden> list.
  1015.  
  1016. Note that having a C<protocols_allowed> list causes any
  1017. C<protocols_forbidden> list to be ignored.
  1018.  
  1019. =item $ua->protocols_forbidden
  1020.  
  1021. =item $ua->protocols_forbidden( \@protocols )
  1022.  
  1023. This reads (or sets) this user agent's list of protocols that the
  1024. request method will I<not> allow. The protocol names are case
  1025. insensitive.
  1026.  
  1027. For example: C<$ua-E<gt>protocols_forbidden( [ 'file', 'mailto'] );>
  1028. means that this user agent will I<not> allow those protocols, and
  1029. attempts to use this user agent to access URLs with those schemes
  1030. will result in a 500 error.
  1031.  
  1032. To delete the list, call: C<$ua-E<gt>protocols_forbidden(undef)>
  1033.  
  1034. =item $ua->requests_redirectable
  1035.  
  1036. =item $ua->requests_redirectable( \@requests )
  1037.  
  1038. This reads or sets the object's list of request names that
  1039. C<$ua-E<gt>redirect_ok(...)> will allow redirection for.  By
  1040. default, this is C<['GET', 'HEAD']>, as per RFC 2616.  To
  1041. change to include 'POST', consider:
  1042.  
  1043.    push @{ $ua->requests_redirectable }, 'POST';
  1044.  
  1045. =item $ua->timeout
  1046.  
  1047. =item $ua->timeout( $secs )
  1048.  
  1049. Get/set the timeout value in seconds. The default timeout() value is
  1050. 180 seconds, i.e. 3 minutes.
  1051.  
  1052. The requests is aborted if no activity on the connection to the server
  1053. is observed for C<timeout> seconds.  This means that the time it takes
  1054. for the complete transaction and the request() method to actually
  1055. return might be longer.
  1056.  
  1057. =back
  1058.  
  1059. =head2 Proxy attributes
  1060.  
  1061. The following methods set up when requests should be passed via a
  1062. proxy server.
  1063.  
  1064. =over
  1065.  
  1066. =item $ua->proxy(\@schemes, $proxy_url)
  1067.  
  1068. =item $ua->proxy($scheme, $proxy_url)
  1069.  
  1070. Set/retrieve proxy URL for a scheme:
  1071.  
  1072.  $ua->proxy(['http', 'ftp'], 'http://proxy.sn.no:8001/');
  1073.  $ua->proxy('gopher', 'http://proxy.sn.no:8001/');
  1074.  
  1075. The first form specifies that the URL is to be used for proxying of
  1076. access methods listed in the list in the first method argument,
  1077. i.e. 'http' and 'ftp'.
  1078.  
  1079. The second form shows a shorthand form for specifying
  1080. proxy URL for a single access scheme.
  1081.  
  1082. =item $ua->no_proxy( $domain, ... )
  1083.  
  1084. Do not proxy requests to the given domains.  Calling no_proxy without
  1085. any domains clears the list of domains. Eg:
  1086.  
  1087.  $ua->no_proxy('localhost', 'no', ...);
  1088.  
  1089. =item $ua->env_proxy
  1090.  
  1091. Load proxy settings from *_proxy environment variables.  You might
  1092. specify proxies like this (sh-syntax):
  1093.  
  1094.   gopher_proxy=http://proxy.my.place/
  1095.   wais_proxy=http://proxy.my.place/
  1096.   no_proxy="localhost,my.domain"
  1097.   export gopher_proxy wais_proxy no_proxy
  1098.  
  1099. csh or tcsh users should use the C<setenv> command to define these
  1100. environment variables.
  1101.  
  1102. On systems with case insensitive environment variables there exists a
  1103. name clash between the CGI environment variables and the C<HTTP_PROXY>
  1104. environment variable normally picked up by env_proxy().  Because of
  1105. this C<HTTP_PROXY> is not honored for CGI scripts.  The
  1106. C<CGI_HTTP_PROXY> environment variable can be used instead.
  1107.  
  1108. =back
  1109.  
  1110. =head1 REQUEST METHODS
  1111.  
  1112. The methods described in this section are used to dispatch requests
  1113. via the user agent.  The following request methods are provided:
  1114.  
  1115. =over
  1116.  
  1117. =item $ua->get( $url )
  1118.  
  1119. =item $ua->get( $url , $field_name => $value, ... )
  1120.  
  1121. This method will dispatch a C<GET> request on the given $url.  Further
  1122. arguments can be given to initialize the headers of the request. These
  1123. are given as separate name/value pairs.  The return value is a
  1124. response object.  See L<HTTP::Response> for a description of the
  1125. interface it provides.
  1126.  
  1127. Fields names that start with ":" are special.  These will not
  1128. initialize headers of the request but will determine how the response
  1129. content is treated.  The following special field names are recognized:
  1130.  
  1131.     :content_file   => $filename
  1132.     :content_cb     => \&callback
  1133.     :read_size_hint => $bytes
  1134.  
  1135. If a $filename is provided with the C<:content_file> option, then the
  1136. response content will be saved here instead of in the response
  1137. object.  If a callback is provided with the C<:content_cb> option then
  1138. this function will be called for each chunk of the response content as
  1139. it is received from the server.  If neither of these options are
  1140. given, then the response content will accumulate in the response
  1141. object itself.  This might not be suitable for very large response
  1142. bodies.  Only one of C<:content_file> or C<:content_cb> can be
  1143. specified.  The content of unsuccessful responses will always
  1144. accumulate in the response object itself, regardless of the
  1145. C<:content_file> or C<:content_cb> options passed in.
  1146.  
  1147. The C<:read_size_hint> option is passed to the protocol module which
  1148. will try to read data from the server in chunks of this size.  A
  1149. smaller value for the C<:read_size_hint> will result in a higher
  1150. number of callback invocations.
  1151.  
  1152. The callback function is called with 3 arguments: a chunk of data, a
  1153. reference to the response object, and a reference to the protocol
  1154. object.  The callback can abort the request by invoking die().  The
  1155. exception message will show up as the "X-Died" header field in the
  1156. response returned by the get() function.
  1157.  
  1158. =item $ua->head( $url )
  1159.  
  1160. =item $ua->head( $url , $field_name => $value, ... )
  1161.  
  1162. This method will dispatch a C<HEAD> request on the given $url.
  1163. Otherwise it works like the get() method described above.
  1164.  
  1165. =item $ua->post( $url, \%form )
  1166.  
  1167. =item $ua->post( $url, \@form )
  1168.  
  1169. =item $ua->post( $url, \%form, $field_name => $value, ... )
  1170.  
  1171. This method will dispatch a C<POST> request on the given $url, with
  1172. %form or @form providing the key/value pairs for the fill-in form
  1173. content. Additional headers and content options are the same as for
  1174. the get() method.
  1175.  
  1176. This method will use the POST() function from C<HTTP::Request::Common>
  1177. to build the request.  See L<HTTP::Request::Common> for a details on
  1178. how to pass form content and other advanced features.
  1179.  
  1180. =item $ua->mirror( $url, $filename )
  1181.  
  1182. This method will get the document identified by $url and store it in
  1183. file called $filename.  If the file already exists, then the request
  1184. will contain an "If-Modified-Since" header matching the modification
  1185. time of the file.  If the document on the server has not changed since
  1186. this time, then nothing happens.  If the document has been updated, it
  1187. will be downloaded again.  The modification time of the file will be
  1188. forced to match that of the server.
  1189.  
  1190. The return value is the the response object.
  1191.  
  1192. =item $ua->request( $request )
  1193.  
  1194. =item $ua->request( $request, $content_file )
  1195.  
  1196. =item $ua->request( $request, $content_cb )
  1197.  
  1198. =item $ua->request( $request, $content_cb, $read_size_hint )
  1199.  
  1200. This method will dispatch the given $request object.  Normally this
  1201. will be an instance of the C<HTTP::Request> class, but any object with
  1202. a similar interface will do.  The return value is a response object.
  1203. See L<HTTP::Request> and L<HTTP::Response> for a description of the
  1204. interface provided by these classes.
  1205.  
  1206. The request() method will process redirects and authentication
  1207. responses transparently.  This means that it may actually send several
  1208. simple requests via the simple_request() method described below.
  1209.  
  1210. The request methods described above; get(), head(), post() and
  1211. mirror(), will all dispatch the request they build via this method.
  1212. They are convenience methods that simply hides the creation of the
  1213. request object for you.
  1214.  
  1215. The $content_file, $content_cb and $read_size_hint all correspond to
  1216. options described with the get() method above.
  1217.  
  1218. You are allowed to use a CODE reference as C<content> in the request
  1219. object passed in.  The C<content> function should return the content
  1220. when called.  The content can be returned in chunks.  The content
  1221. function will be invoked repeatedly until it return an empty string to
  1222. signal that there is no more content.
  1223.  
  1224. =item $ua->simple_request( $request )
  1225.  
  1226. =item $ua->simple_request( $request, $content_file )
  1227.  
  1228. =item $ua->simple_request( $request, $content_cb )
  1229.  
  1230. =item $ua->simple_request( $request, $content_cb, $read_size_hint )
  1231.  
  1232. This method dispatches a single request and returns the response
  1233. received.  Arguments are the same as for request() described above.
  1234.  
  1235. The difference from request() is that simple_request() will not try to
  1236. handle redirects or authentication responses.  The request() method
  1237. will in fact invoke this method for each simple request it sends.
  1238.  
  1239. =item $ua->is_protocol_supported( $scheme )
  1240.  
  1241. You can use this method to test whether this user agent object supports the
  1242. specified C<scheme>.  (The C<scheme> might be a string (like 'http' or
  1243. 'ftp') or it might be an URI object reference.)
  1244.  
  1245. Whether a scheme is supported, is determined by the user agent's
  1246. C<protocols_allowed> or C<protocols_forbidden> lists (if any), and by
  1247. the capabilities of LWP.  I.e., this will return TRUE only if LWP
  1248. supports this protocol I<and> it's permitted for this particular
  1249. object.
  1250.  
  1251. =back
  1252.  
  1253. =head2 Callback methods
  1254.  
  1255. The following methods will be invoked as requests are processed. These
  1256. methods are documented here because subclasses of C<LWP::UserAgent>
  1257. might want to override their behaviour.
  1258.  
  1259. =over
  1260.  
  1261. =item $ua->prepare_request( $request )
  1262.  
  1263. This method is invoked by simple_request().  Its task is to modify the
  1264. given $request object by setting up various headers based on the
  1265. attributes of the user agent. The return value should normally be the
  1266. $request object passed in.  If a different request object is returned
  1267. it will be the one actually processed.
  1268.  
  1269. The headers affected by the base implementation are; "User-Agent",
  1270. "From", "Range" and "Cookie".
  1271.  
  1272. =item $ua->redirect_ok( $prospective_request, $response )
  1273.  
  1274. This method is called by request() before it tries to follow a
  1275. redirection to the request in $response.  This should return a TRUE
  1276. value if this redirection is permissible.  The $prospective_request
  1277. will be the request to be sent if this method returns TRUE.
  1278.  
  1279. The base implementation will return FALSE unless the method
  1280. is in the object's C<requests_redirectable> list,
  1281. FALSE if the proposed redirection is to a "file://..."
  1282. URL, and TRUE otherwise.
  1283.  
  1284. =item $ua->get_basic_credentials( $realm, $uri, $isproxy )
  1285.  
  1286. This is called by request() to retrieve credentials for documents
  1287. protected by Basic or Digest Authentication.  The arguments passed in
  1288. is the $realm provided by the server, the $uri requested and a boolean
  1289. flag to indicate if this is authentication against a proxy server.
  1290.  
  1291. The method should return a username and password.  It should return an
  1292. empty list to abort the authentication resolution attempt.  Subclasses
  1293. can override this method to prompt the user for the information. An
  1294. example of this can be found in C<lwp-request> program distributed
  1295. with this library.
  1296.  
  1297. The base implementation simply checks a set of pre-stored member
  1298. variables, set up with the credentials() method.
  1299.  
  1300. =back
  1301.  
  1302. =head1 SEE ALSO
  1303.  
  1304. See L<LWP> for a complete overview of libwww-perl5.  See L<lwpcook>
  1305. and the scripts F<lwp-request> and F<lwp-download> for examples of
  1306. usage.
  1307.  
  1308. See L<HTTP::Request> and L<HTTP::Response> for a description of the
  1309. message objects dispatched and received.  See L<HTTP::Request::Common>
  1310. and L<HTML::Form> for other ways to build request objects.
  1311.  
  1312. See L<WWW::Mechanize> and L<WWW::Search> for examples of more
  1313. specialized user agents based on C<LWP::UserAgent>.
  1314.  
  1315. =head1 COPYRIGHT
  1316.  
  1317. Copyright 1995-2004 Gisle Aas.
  1318.  
  1319. This library is free software; you can redistribute it and/or
  1320. modify it under the same terms as Perl itself.
  1321.