home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / UserAgent.pm < prev    next >
Encoding:
Perl POD Document  |  2003-11-21  |  39.4 KB  |  1,317 lines

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