<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package LWP::UserAgent;

use strict;

use parent qw(LWP::MemberMixin);

use Carp ();
use File::Copy ();
use HTTP::Request ();
use HTTP::Response ();
use HTTP::Date ();

use LWP ();
use HTTP::Status ();
use LWP::Protocol ();
use Module::Load qw( load );

use Scalar::Util qw(blessed openhandle);
use Try::Tiny qw(try catch);

our $VERSION = '6.76';

sub new
{
    # Check for common user mistake
    Carp::croak("Options to LWP::UserAgent should be key/value pairs, not hash reference")
        if ref($_[1]) eq 'HASH';

    my($class, %cnf) = @_;

    my $agent = delete $cnf{agent};
    my $from  = delete $cnf{from};
    my $def_headers = delete $cnf{default_headers};
    my $timeout = delete $cnf{timeout};
    $timeout = 3*60 unless defined $timeout;
    my $local_address = delete $cnf{local_address};
    my $ssl_opts = delete $cnf{ssl_opts} || {};
    unless (exists $ssl_opts-&gt;{verify_hostname}) {
	# The processing of HTTPS_CA_* below is for compatibility with Crypt::SSLeay
	if (exists $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}) {
	    $ssl_opts-&gt;{verify_hostname} = $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME};
	}
	elsif ($ENV{HTTPS_CA_FILE} || $ENV{HTTPS_CA_DIR}) {
	    # Crypt-SSLeay compatibility (verify peer certificate; but not the hostname)
	    $ssl_opts-&gt;{verify_hostname} = 0;
	    $ssl_opts-&gt;{SSL_verify_mode} = 1;
	}
	else {
	    $ssl_opts-&gt;{verify_hostname} = 1;
	}
    }
    unless (exists $ssl_opts-&gt;{SSL_ca_file}) {
	if (my $ca_file = $ENV{PERL_LWP_SSL_CA_FILE} || $ENV{HTTPS_CA_FILE}) {
	    $ssl_opts-&gt;{SSL_ca_file} = $ca_file;
	}
    }
    unless (exists $ssl_opts-&gt;{SSL_ca_path}) {
	if (my $ca_path = $ENV{PERL_LWP_SSL_CA_PATH} || $ENV{HTTPS_CA_DIR}) {
	    $ssl_opts-&gt;{SSL_ca_path} = $ca_path;
	}
    }
    my $use_eval = delete $cnf{use_eval};
    $use_eval = 1 unless defined $use_eval;
    my $parse_head = delete $cnf{parse_head};
    $parse_head = 1 unless defined $parse_head;
    my $send_te = delete $cnf{send_te};
    $send_te = 1 unless defined $send_te;
    my $show_progress = delete $cnf{show_progress};
    my $max_size = delete $cnf{max_size};
    my $max_redirect = delete $cnf{max_redirect};
    $max_redirect = 7 unless defined $max_redirect;
    my $env_proxy = exists $cnf{env_proxy} ? delete $cnf{env_proxy} : $ENV{PERL_LWP_ENV_PROXY};
    my $no_proxy = exists $cnf{no_proxy} ? delete $cnf{no_proxy} : [];
    Carp::croak(qq{no_proxy must be an arrayref, not $no_proxy!}) if ref $no_proxy ne 'ARRAY';

    my $cookie_jar = delete $cnf{cookie_jar};
    my $conn_cache = delete $cnf{conn_cache};
    my $keep_alive = delete $cnf{keep_alive};

    Carp::croak("Can't mix conn_cache and keep_alive")
	  if $conn_cache &amp;&amp; $keep_alive;

    my $protocols_allowed   = delete $cnf{protocols_allowed};
    my $protocols_forbidden = delete $cnf{protocols_forbidden};

    my $requests_redirectable = delete $cnf{requests_redirectable};
    $requests_redirectable = ['GET', 'HEAD']
      unless defined $requests_redirectable;

    my $cookie_jar_class = delete $cnf{cookie_jar_class};
    $cookie_jar_class = 'HTTP::Cookies'
      unless defined $cookie_jar_class;

    # Actually ""s are just as good as 0's, but for concision we'll just say:
    Carp::croak("protocols_allowed has to be an arrayref or 0, not \"$protocols_allowed\"!")
      if $protocols_allowed and ref($protocols_allowed) ne 'ARRAY';
    Carp::croak("protocols_forbidden has to be an arrayref or 0, not \"$protocols_forbidden\"!")
      if $protocols_forbidden and ref($protocols_forbidden) ne 'ARRAY';
    Carp::croak("requests_redirectable has to be an arrayref or 0, not \"$requests_redirectable\"!")
      if $requests_redirectable and ref($requests_redirectable) ne 'ARRAY';

    if (%cnf &amp;&amp; $^W) {
	Carp::carp("Unrecognized LWP::UserAgent options: @{[sort keys %cnf]}");
    }

    my $self = bless {
        def_headers           =&gt; $def_headers,
        timeout               =&gt; $timeout,
        local_address         =&gt; $local_address,
        ssl_opts              =&gt; $ssl_opts,
        use_eval              =&gt; $use_eval,
        show_progress         =&gt; $show_progress,
        max_size              =&gt; $max_size,
        max_redirect          =&gt; $max_redirect,
        # We set proxy later as we do validation on the values
        proxy                 =&gt; {},
        no_proxy              =&gt; [ @{ $no_proxy } ],
        protocols_allowed     =&gt; $protocols_allowed,
        protocols_forbidden   =&gt; $protocols_forbidden,
        requests_redirectable =&gt; $requests_redirectable,
        send_te               =&gt; $send_te,
        cookie_jar_class      =&gt; $cookie_jar_class,
    }, $class;

    $self-&gt;agent(defined($agent) ? $agent : $class-&gt;_agent)
        if defined($agent) || !$def_headers || !$def_headers-&gt;header("User-Agent");
    $self-&gt;from($from) if $from;
    $self-&gt;cookie_jar($cookie_jar) if $cookie_jar;
    $self-&gt;parse_head($parse_head);
    $self-&gt;env_proxy if $env_proxy;

    if (exists $cnf{proxy}) {
        Carp::croak(qq{proxy must be an arrayref, not $cnf{proxy}!})
            if ref $cnf{proxy} ne 'ARRAY';
        $self-&gt;proxy($cnf{proxy});
    }

    $self-&gt;protocols_allowed(  $protocols_allowed  ) if $protocols_allowed;
    $self-&gt;protocols_forbidden($protocols_forbidden) if $protocols_forbidden;

    if ($keep_alive) {
	$conn_cache ||= { total_capacity =&gt; $keep_alive };
    }
    $self-&gt;conn_cache($conn_cache) if $conn_cache;

    return $self;
}


sub send_request
{
    my($self, $request, $arg, $size) = @_;
    my($method, $url) = ($request-&gt;method, $request-&gt;uri);
    my $scheme = $url-&gt;scheme;

    local($SIG{__DIE__});  # protect against user defined die handlers

    $self-&gt;progress("begin", $request);

    my $response = $self-&gt;run_handlers("request_send", $request);

    unless ($response) {
        my $protocol;

        {
            # Honor object-specific restrictions by forcing protocol objects
            #  into class LWP::Protocol::nogo.
            my $x;
            if($x = $self-&gt;protocols_allowed) {
                if (grep lc($_) eq $scheme, @$x) {
                }
                else {
                    require LWP::Protocol::nogo;
                    $protocol = LWP::Protocol::nogo-&gt;new;
                }
            }
            elsif ($x = $self-&gt;protocols_forbidden) {
                if(grep lc($_) eq $scheme, @$x) {
                    require LWP::Protocol::nogo;
                    $protocol = LWP::Protocol::nogo-&gt;new;
                }
            }
            # else fall thru and create the protocol object normally
        }

        # Locate protocol to use
        my $proxy = $request-&gt;{proxy};
        if ($proxy) {
            $scheme = $proxy-&gt;scheme;
        }

        unless ($protocol) {
            try {
                $protocol = LWP::Protocol::create($scheme, $self);
            }
            catch {
                my $error = $_;
                $error =~ s/ at .* line \d+.*//s;  # remove file/line number
                $response =  _new_response($request, HTTP::Status::RC_NOT_IMPLEMENTED, $error);
                if ($scheme eq "https") {
                    $response-&gt;message($response-&gt;message . " (LWP::Protocol::https not installed)");
                    $response-&gt;content_type("text/plain");
                    $response-&gt;content(&lt;&lt;EOT);
LWP will support https URLs if the LWP::Protocol::https module
is installed.
EOT
                }
            };
        }

        if (!$response &amp;&amp; $self-&gt;{use_eval}) {
            # we eval, and turn dies into responses below
            try {
                $response = $protocol-&gt;request($request, $proxy, $arg, $size, $self-&gt;{timeout}) || die "No response returned by $protocol";
            }
            catch {
                my $error = $_;
                if (blessed($error) &amp;&amp; $error-&gt;isa("HTTP::Response")) {
                    $response = $error;
                    $response-&gt;request($request);
                }
                else {
                    my $full = $error;
                    (my $status = $error) =~ s/\n.*//s;
                    $status =~ s/ at .* line \d+.*//s;  # remove file/line number
                    my $code = ($status =~ s/^(\d\d\d)\s+//) ? $1 : HTTP::Status::RC_INTERNAL_SERVER_ERROR;
                    $response = _new_response($request, $code, $status, $full);
                }
            };
        }
        elsif (!$response) {
            $response = $protocol-&gt;request($request, $proxy,
                                           $arg, $size, $self-&gt;{timeout});
            # XXX: Should we die unless $response-&gt;is_success ???
        }
    }

    $response-&gt;request($request);  # record request for reference
    $response-&gt;header("Client-Date" =&gt; HTTP::Date::time2str(time));

    $self-&gt;run_handlers("response_done", $response);

    $self-&gt;progress("end", $response);
    return $response;
}


sub prepare_request
{
    my($self, $request) = @_;
    die "Method missing" unless $request-&gt;method;
    my $url = $request-&gt;uri;
    die "URL missing" unless $url;
    die "URL must be absolute" unless $url-&gt;scheme;

    $self-&gt;run_handlers("request_preprepare", $request);

    if (my $def_headers = $self-&gt;{def_headers}) {
	for my $h ($def_headers-&gt;header_field_names) {
	    $request-&gt;init_header($h =&gt; [$def_headers-&gt;header($h)]);
	}
    }

    $self-&gt;run_handlers("request_prepare", $request);

    return $request;
}


sub simple_request
{
    my($self, $request, $arg, $size) = @_;

    # sanity check the request passed in
    if (defined $request) {
	if (ref $request) {
	    Carp::croak("You need a request object, not a " . ref($request) . " object")
	      if ref($request) eq 'ARRAY' or ref($request) eq 'HASH' or
		 !$request-&gt;can('method') or !$request-&gt;can('uri');
	}
	else {
	    Carp::croak("You need a request object, not '$request'");
	}
    }
    else {
        Carp::croak("No request object passed in");
    }

    my $error;
    try {
        $request = $self-&gt;prepare_request($request);
    }
    catch {
        $error = $_;
        $error =~ s/ at .* line \d+.*//s;  # remove file/line number
    };

    if ($error) {
        return _new_response($request, HTTP::Status::RC_BAD_REQUEST, $error);
    }
    return $self-&gt;send_request($request, $arg, $size);
}


sub request {
    my ($self, $request, $arg, $size, $previous) = @_;

    my $response = $self-&gt;simple_request($request, $arg, $size);
    $response-&gt;previous($previous) if $previous;

    if ($response-&gt;redirects &gt;= $self-&gt;{max_redirect}) {
        if ($response-&gt;header('Location')) {
            $response-&gt;header("Client-Warning" =&gt;
                "Redirect loop detected (max_redirect = $self-&gt;{max_redirect})"
            );
        }
        return $response;
    }

    if (my $req = $self-&gt;run_handlers("response_redirect", $response)) {
        return $self-&gt;request($req, $arg, $size, $response);
    }

    my $code = $response-&gt;code;

    if (   $code == HTTP::Status::RC_MOVED_PERMANENTLY
        or $code == HTTP::Status::RC_FOUND
        or $code == HTTP::Status::RC_SEE_OTHER
        or $code == HTTP::Status::RC_TEMPORARY_REDIRECT
        or $code == HTTP::Status::RC_PERMANENT_REDIRECT)
    {
        my $referral = $request-&gt;clone;

        # These headers should never be forwarded
        $referral-&gt;remove_header('Host', 'Cookie');

        if (   $referral-&gt;header('Referer')
            &amp;&amp; $request-&gt;uri-&gt;scheme eq 'https'
            &amp;&amp; $referral-&gt;uri-&gt;scheme eq 'http')
        {
            # RFC 2616, section 15.1.3.
            # https -&gt; http redirect, suppressing Referer
            $referral-&gt;remove_header('Referer');
        }

        if (   $code == HTTP::Status::RC_SEE_OTHER
            || $code == HTTP::Status::RC_FOUND)
        {
            my $method = uc($referral-&gt;method);
            unless ($method eq "GET" || $method eq "HEAD") {
                $referral-&gt;method("GET");
                $referral-&gt;content("");
                $referral-&gt;remove_content_headers;
            }
        }

        # And then we update the URL based on the Location:-header.
        my $referral_uri = $response-&gt;header('Location');
        {
            # Some servers erroneously return a relative URL for redirects,
            # so make it absolute if it not already is.
            local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
            my $base = $response-&gt;base;
            $referral_uri = "" unless defined $referral_uri;
            $referral_uri
                = $HTTP::URI_CLASS-&gt;new($referral_uri, $base)-&gt;abs($base);
        }
        $referral-&gt;uri($referral_uri);

        return $response unless $self-&gt;redirect_ok($referral, $response);
        return $self-&gt;request($referral, $arg, $size, $response);

    }
    elsif ($code == HTTP::Status::RC_UNAUTHORIZED
        || $code == HTTP::Status::RC_PROXY_AUTHENTICATION_REQUIRED)
    {
        my $proxy = ($code == HTTP::Status::RC_PROXY_AUTHENTICATION_REQUIRED);
        my $ch_header
            = $proxy || $request-&gt;method eq 'CONNECT'
            ? "Proxy-Authenticate"
            : "WWW-Authenticate";
        my @challenges = $response-&gt;header($ch_header);
        unless (@challenges) {
            $response-&gt;header(
                "Client-Warning" =&gt; "Missing Authenticate header");
            return $response;
        }

        require HTTP::Headers::Util;
        CHALLENGE: for my $challenge (@challenges) {
            $challenge =~ tr/,/;/;    # "," is used to separate auth-params!!
            ($challenge) = HTTP::Headers::Util::split_header_words($challenge);
            my $scheme = shift(@$challenge);
            shift(@$challenge);       # no value
            $challenge = {@$challenge};    # make rest into a hash

            unless ($scheme =~ /^([a-z]+(?:-[a-z]+)*)$/) {
                $response-&gt;header(
                    "Client-Warning" =&gt; "Bad authentication scheme '$scheme'");
                return $response;
            }
            $scheme = $1;                  # untainted now
            my $class = "LWP::Authen::\u$scheme";
            $class =~ tr/-/_/;

            no strict 'refs';
            unless (%{"$class\::"}) {
                # try to load it
                my $error;
                try {
                    (my $req = $class) =~ s{::}{/}g;
                    $req .= '.pm' unless $req =~ /\.pm$/;
                    require $req;
                }
                catch {
                    $error = $_;
                };
                if ($error) {
                    if ($error =~ /^Can\'t locate/) {
                        $response-&gt;header("Client-Warning" =&gt;
                                "Unsupported authentication scheme '$scheme'");
                    }
                    else {
                        $response-&gt;header("Client-Warning" =&gt; $error);
                    }
                    next CHALLENGE;
                }
            }
            unless ($class-&gt;can("authenticate")) {
                $response-&gt;header("Client-Warning" =&gt;
                        "Unsupported authentication scheme '$scheme'");
                next CHALLENGE;
            }
            my $re = $class-&gt;authenticate($self, $proxy, $challenge, $response,
                $request, $arg, $size);

            next CHALLENGE if $re-&gt;code == HTTP::Status::RC_UNAUTHORIZED;
            return $re;
        }
        return $response;
    }
    return $response;
}

#
# Now the shortcuts...
#
sub get {
    require HTTP::Request::Common;
    my($self, @parameters) = @_;
    my @suff = $self-&gt;_process_colonic_headers(\@parameters,1);
    return $self-&gt;request( HTTP::Request::Common::GET( @parameters ), @suff );
}

sub _maybe_copy_default_content_type {
    my $self = shift;
    my $req  = shift;

    my $default_ct = $self-&gt;default_header('Content-Type');
    return unless defined $default_ct;

    # drop url
    shift;

    # adapted from HTTP::Request::Common::request_type_with_data
    my $content;
    $content = shift if @_ and ref $_[0];

    # We only care about the final value, really
    my $ct;

    my ($k, $v);
    while (($k, $v) = splice(@_, 0, 2)) {
        if (lc($k) eq 'content') {
            $content = $v;
        }
        elsif (lc($k) eq 'content-type') {
            $ct = $v;
        }
    }

    # Content-type provided and truthy? skip
    return if $ct;

    # Content is not just a string? Then it must be x-www-form-urlencoded
    return if defined $content &amp;&amp; ref($content);

    # Provide default
    $req-&gt;header('Content-Type' =&gt; $default_ct);
}

sub post {
    require HTTP::Request::Common;
    my($self, @parameters) = @_;
    my @suff = $self-&gt;_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
    my $req = HTTP::Request::Common::POST(@parameters);
    $self-&gt;_maybe_copy_default_content_type($req, @parameters);
    return $self-&gt;request($req, @suff);
}


sub head {
    require HTTP::Request::Common;
    my($self, @parameters) = @_;
    my @suff = $self-&gt;_process_colonic_headers(\@parameters,1);
    return $self-&gt;request( HTTP::Request::Common::HEAD( @parameters ), @suff );
}

sub patch {
    require HTTP::Request::Common;
    my($self, @parameters) = @_;
    my @suff = $self-&gt;_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));

    # this work-around is in place as HTTP::Request::Common
    # did not implement a patch convenience method until
    # version 6.12. Once we can bump the prereq to at least
    # that version, we can use ::PATCH instead of this hack
    my $req = HTTP::Request::Common::PUT(@parameters);
    $req-&gt;method('PATCH');

    $self-&gt;_maybe_copy_default_content_type($req, @parameters);
    return $self-&gt;request($req, @suff);
}

sub put {
    require HTTP::Request::Common;
    my($self, @parameters) = @_;
    my @suff = $self-&gt;_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
    my $req = HTTP::Request::Common::PUT(@parameters);
    $self-&gt;_maybe_copy_default_content_type($req, @parameters);
    return $self-&gt;request($req, @suff);
}


sub delete {
    require HTTP::Request::Common;
    my($self, @parameters) = @_;
    my @suff = $self-&gt;_process_colonic_headers(\@parameters,1);
    return $self-&gt;request( HTTP::Request::Common::DELETE( @parameters ), @suff );
}


sub _process_colonic_headers {
    # Process :content_cb / :content_file / :read_size_hint headers.
    my($self, $args, $start_index) = @_;

    my($arg, $size);
    for(my $i = $start_index; $i &lt; @$args; $i += 2) {
	next unless defined $args-&gt;[$i];

	#printf "Considering %s =&gt; %s\n", $args-&gt;[$i], $args-&gt;[$i + 1];

	if($args-&gt;[$i] eq ':content_cb') {
	    # Some sanity-checking...
	    $arg = $args-&gt;[$i + 1];
	    Carp::croak("A :content_cb value can't be undef") unless defined $arg;
	    Carp::croak("A :content_cb value must be a coderef")
		unless ref $arg and UNIVERSAL::isa($arg, 'CODE');

	}
	elsif ($args-&gt;[$i] eq ':content_file') {
	    $arg = $args-&gt;[$i + 1];

	    # Some sanity-checking...
	    Carp::croak("A :content_file value can't be undef")
		unless defined $arg;

	    unless ( defined openhandle($arg) ) {
		    Carp::croak("A :content_file value can't be a reference")
			if ref $arg;
		    Carp::croak("A :content_file value can't be \"\"")
			unless length $arg;
	    }
	}
	elsif ($args-&gt;[$i] eq ':read_size_hint') {
	    $size = $args-&gt;[$i + 1];
	    # Bother checking it?

	}
	else {
	    next;
	}
	splice @$args, $i, 2;
	$i -= 2;
    }

    # And return a suitable suffix-list for request(REQ,...)

    return             unless defined $arg;
    return $arg, $size if     defined $size;
    return $arg;
}


sub is_online {
    my $self = shift;
    return 1 if $self-&gt;get("http://www.msftncsi.com/ncsi.txt")-&gt;content eq "Microsoft NCSI";
    return 1 if $self-&gt;get("http://www.apple.com")-&gt;content =~ m,&lt;title&gt;Apple&lt;/title&gt;,;
    return 0;
}


my @ANI = qw(- \ | /);

sub progress {
    my($self, $status, $m) = @_;
    return unless $self-&gt;{show_progress};

    local($,, $\);
    if ($status eq "begin") {
        print STDERR "** ", $m-&gt;method, " ", $m-&gt;uri, " ==&gt; ";
        $self-&gt;{progress_start} = time;
        $self-&gt;{progress_lastp} = "";
        $self-&gt;{progress_ani} = 0;
    }
    elsif ($status eq "end") {
        delete $self-&gt;{progress_lastp};
        delete $self-&gt;{progress_ani};
        print STDERR $m-&gt;status_line;
        my $t = time - delete $self-&gt;{progress_start};
        print STDERR " (${t}s)" if $t;
        print STDERR "\n";
    }
    elsif ($status eq "tick") {
        print STDERR "$ANI[$self-&gt;{progress_ani}++]\b";
        $self-&gt;{progress_ani} %= @ANI;
    }
    else {
        my $p = sprintf "%3.0f%%", $status * 100;
        return if $p eq $self-&gt;{progress_lastp};
        print STDERR "$p\b\b\b\b";
        $self-&gt;{progress_lastp} = $p;
    }
    STDERR-&gt;flush;
}


#
# This whole allow/forbid thing is based on man 1 at's way of doing things.
#
sub is_protocol_supported
{
    my($self, $scheme) = @_;
    if (ref $scheme) {
	# assume we got a reference to an URI object
	$scheme = $scheme-&gt;scheme;
    }
    else {
	Carp::croak("Illegal scheme '$scheme' passed to is_protocol_supported")
	    if $scheme =~ /\W/;
	$scheme = lc $scheme;
    }

    my $x;
    if(ref($self) and $x       = $self-&gt;protocols_allowed) {
      return 0 unless grep lc($_) eq $scheme, @$x;
    }
    elsif (ref($self) and $x = $self-&gt;protocols_forbidden) {
      return 0 if grep lc($_) eq $scheme, @$x;
    }

    local($SIG{__DIE__});  # protect against user defined die handlers
    $x = LWP::Protocol::implementor($scheme);
    return 1 if $x and $x ne 'LWP::Protocol::nogo';
    return 0;
}


sub protocols_allowed      { shift-&gt;_elem('protocols_allowed'    , @_) }
sub protocols_forbidden    { shift-&gt;_elem('protocols_forbidden'  , @_) }
sub requests_redirectable  { shift-&gt;_elem('requests_redirectable', @_) }


sub redirect_ok
{
    # RFC 2616, section 10.3.2 and 10.3.3 say:
    #  If the 30[12] status code is received in response to a request other
    #  than GET or HEAD, the user agent MUST NOT automatically redirect the
    #  request unless it can be confirmed by the user, since this might
    #  change the conditions under which the request was issued.

    # Note that this routine used to be just:
    #  return 0 if $_[1]-&gt;method eq "POST";  return 1;

    my($self, $new_request, $response) = @_;
    my $method = $response-&gt;request-&gt;method;
    return 0 unless grep $_ eq $method,
      @{ $self-&gt;requests_redirectable || [] };

    if ($new_request-&gt;uri-&gt;scheme eq 'file') {
      $response-&gt;header("Client-Warning" =&gt;
			"Can't redirect to a file:// URL!");
      return 0;
    }

    # Otherwise it's apparently okay...
    return 1;
}

sub credentials {
    my $self   = shift;
    my $netloc = lc(shift || '');
    my $realm  = shift || "";
    my $old    = $self-&gt;{basic_authentication}{$netloc}{$realm};
    if (@_) {
        $self-&gt;{basic_authentication}{$netloc}{$realm} = [@_];
    }
    return unless $old;
    return @$old if wantarray;
    return join(":", @$old);
}

sub get_basic_credentials
{
    my($self, $realm, $uri, $proxy) = @_;
    return if $proxy;
    return $self-&gt;credentials($uri-&gt;host_port, $realm);
}


sub timeout
{
    my $self = shift;
    my $old = $self-&gt;{timeout};
    if (@_) {
        $self-&gt;{timeout} = shift;
        if (my $conn_cache = $self-&gt;conn_cache) {
            for my $conn ($conn_cache-&gt;get_connections) {
                $conn-&gt;timeout($self-&gt;{timeout});
            }
        }
    }
    return $old;
}

sub local_address{ shift-&gt;_elem('local_address',@_); }
sub max_size     { shift-&gt;_elem('max_size',     @_); }
sub max_redirect { shift-&gt;_elem('max_redirect', @_); }
sub show_progress{ shift-&gt;_elem('show_progress', @_); }
sub send_te      { shift-&gt;_elem('send_te',      @_); }

sub ssl_opts {
    my $self = shift;
    if (@_ == 1) {
	my $k = shift;
	return $self-&gt;{ssl_opts}{$k};
    }
    if (@_) {
	my $old;
	while (@_) {
	    my($k, $v) = splice(@_, 0, 2);
	    $old = $self-&gt;{ssl_opts}{$k} unless @_;
	    if (defined $v) {
		$self-&gt;{ssl_opts}{$k} = $v;
	    }
	    else {
		delete $self-&gt;{ssl_opts}{$k};
	    }
	}
	%{$self-&gt;{ssl_opts}} = (%{$self-&gt;{ssl_opts}}, @_);
	return $old;
    }

    my @opts= sort keys %{$self-&gt;{ssl_opts}};
    return @opts;
}

sub parse_head {
    my $self = shift;
    if (@_) {
        my $flag = shift;
        my $parser;
        my $old = $self-&gt;set_my_handler("response_header", $flag ? sub {
               my($response, $ua) = @_;
               require HTML::HeadParser;
               $parser = HTML::HeadParser-&gt;new;
               $parser-&gt;xml_mode(1) if $response-&gt;content_is_xhtml;
               $parser-&gt;utf8_mode(1) if $HTML::Parser::VERSION &gt;= 3.40;

               push(@{$response-&gt;{handlers}{response_data}}, {
		   callback =&gt; sub {
		       return unless $parser;
		       unless ($parser-&gt;parse($_[3])) {
			   my $h = $parser-&gt;header;
			   my $r = $_[0];
			   for my $f ($h-&gt;header_field_names) {
			       $r-&gt;init_header($f, [$h-&gt;header($f)]);
			   }
			   undef($parser);
		       }
		   },
	       });

            } : undef,
            m_media_type =&gt; "html",
        );
        return !!$old;
    }
    else {
        return !!$self-&gt;get_my_handler("response_header");
    }
}

sub cookie_jar {
    my $self = shift;
    my $old = $self-&gt;{cookie_jar};

    return $old unless @_;

    my $jar = shift;
    if (ref($jar) eq "HASH") {
        my $class = $self-&gt;{cookie_jar_class};
        try {
            load($class);
            $jar = $class-&gt;new(%$jar);
        }
        catch {
            my $error = $_;
            if ($error =~ /Can't locate/) {
                die "cookie_jar_class '$class' not found\n";
            }
            else {
                die "$error\n";
            }
        };
    }
    $self-&gt;{cookie_jar} = $jar;
    $self-&gt;set_my_handler("request_prepare",
        $jar ? sub {
            return if $_[0]-&gt;header("Cookie");
            $jar-&gt;add_cookie_header($_[0]);
        } : undef,
    );
    $self-&gt;set_my_handler("response_done",
        $jar ? sub { $jar-&gt;extract_cookies($_[0]); } : undef,
    );

    return $old;
}

sub default_headers {
    my $self = shift;
    my $old = $self-&gt;{def_headers} ||= HTTP::Headers-&gt;new;
    if (@_) {
	Carp::croak("default_headers not set to HTTP::Headers compatible object")
	    unless @_ == 1 &amp;&amp; $_[0]-&gt;can("header_field_names");
	$self-&gt;{def_headers} = shift;
    }
    return $old;
}

sub default_header {
    my $self = shift;
    return $self-&gt;default_headers-&gt;header(@_);
}

sub _agent { "libwww-perl/$VERSION" }

sub agent {
    my $self = shift;
    if (@_) {
	my $agent = shift;
        if ($agent) {
            $agent .= $self-&gt;_agent if $agent =~ /\s+$/;
        }
        else {
            undef($agent)
        }
        return $self-&gt;default_header("User-Agent", $agent);
    }
    return $self-&gt;default_header("User-Agent");
}

sub from {  # legacy
    my $self = shift;
    return $self-&gt;default_header("From", @_);
}


sub conn_cache {
    my $self = shift;
    my $old  = $self-&gt;{conn_cache};
    if (@_) {
        my $cache = shift;
        if ( ref($cache) eq "HASH" ) {
            require LWP::ConnCache;
            $cache = LWP::ConnCache-&gt;new(%$cache);
        }
        elsif ( defined $cache)  {
            for my $conn ( $cache-&gt;get_connections ) {
                $conn-&gt;timeout( $self-&gt;timeout );
            }
        }
        $self-&gt;{conn_cache} = $cache;
    }
    return $old;
}


sub add_handler {
    my($self, $phase, $cb, %spec) = @_;
    $spec{line} ||= join(":", (caller)[1,2]);
    my $conf = $self-&gt;{handlers}{$phase} ||= do {
        require HTTP::Config;
        HTTP::Config-&gt;new;
    };
    $conf-&gt;add(%spec, callback =&gt; $cb);
}

sub set_my_handler {
    my($self, $phase, $cb, %spec) = @_;
    $spec{owner} = (caller(1))[3] unless exists $spec{owner};
    $self-&gt;remove_handler($phase, %spec);
    $spec{line} ||= join(":", (caller)[1,2]);
    $self-&gt;add_handler($phase, $cb, %spec) if $cb;
}

sub get_my_handler {
    my $self = shift;
    my $phase = shift;
    my $init = pop if @_ % 2;
    my %spec = @_;
    my $conf = $self-&gt;{handlers}{$phase};
    unless ($conf) {
        return unless $init;
        require HTTP::Config;
        $conf = $self-&gt;{handlers}{$phase} = HTTP::Config-&gt;new;
    }
    $spec{owner} = (caller(1))[3] unless exists $spec{owner};
    my @h = $conf-&gt;find(%spec);
    if (!@h &amp;&amp; $init) {
        if (ref($init) eq "CODE") {
            $init-&gt;(\%spec);
        }
        elsif (ref($init) eq "HASH") {
            $spec{$_}= $init-&gt;{$_}
                for keys %$init;
        }
        $spec{callback} ||= sub {};
        $spec{line} ||= join(":", (caller)[1,2]);
        $conf-&gt;add(\%spec);
        return \%spec;
    }
    return wantarray ? @h : $h[0];
}

sub remove_handler {
    my($self, $phase, %spec) = @_;
    if ($phase) {
        my $conf = $self-&gt;{handlers}{$phase} || return;
        my @h = $conf-&gt;remove(%spec);
        delete $self-&gt;{handlers}{$phase} if $conf-&gt;empty;
        return @h;
    }

    return unless $self-&gt;{handlers};
    return map $self-&gt;remove_handler($_), sort keys %{$self-&gt;{handlers}};
}

sub handlers {
    my($self, $phase, $o) = @_;
    my @h;
    if ($o-&gt;{handlers} &amp;&amp; $o-&gt;{handlers}{$phase}) {
        push(@h, @{$o-&gt;{handlers}{$phase}});
    }
    if (my $conf = $self-&gt;{handlers}{$phase}) {
        push(@h, $conf-&gt;matching($o));
    }
    return @h;
}

sub run_handlers {
    my($self, $phase, $o) = @_;

    # here we pass $_[2] to the callbacks, instead of $o, so that they
    # can assign to it; e.g. request_prepare is documented to allow
    # that
    if (defined(wantarray)) {
        for my $h ($self-&gt;handlers($phase, $o)) {
            my $ret = $h-&gt;{callback}-&gt;($_[2], $self, $h);
            return $ret if $ret;
        }
        return undef;
    }

    for my $h ($self-&gt;handlers($phase, $o)) {
        $h-&gt;{callback}-&gt;($_[2], $self, $h);
    }
}


# deprecated
sub use_eval   { shift-&gt;_elem('use_eval',  @_); }
sub use_alarm
{
    Carp::carp("LWP::UserAgent-&gt;use_alarm(BOOL) is a no-op")
	if @_ &gt; 1 &amp;&amp; $^W;
    "";
}


sub clone
{
    my $self = shift;
    my $copy = bless { %$self }, ref $self;  # copy most fields

    delete $copy-&gt;{handlers};
    delete $copy-&gt;{conn_cache};

    # copy any plain arrays and hashes; known not to need recursive copy
    for my $k (qw(proxy no_proxy requests_redirectable ssl_opts)) {
        next unless $copy-&gt;{$k};
        if (ref($copy-&gt;{$k}) eq "ARRAY") {
            $copy-&gt;{$k} = [ @{$copy-&gt;{$k}} ];
        }
        elsif (ref($copy-&gt;{$k}) eq "HASH") {
            $copy-&gt;{$k} = { %{$copy-&gt;{$k}} };
        }
    }

    if ($self-&gt;{def_headers}) {
        $copy-&gt;{def_headers} = $self-&gt;{def_headers}-&gt;clone;
    }

    # re-enable standard handlers
    $copy-&gt;parse_head($self-&gt;parse_head);

    # no easy way to clone the cookie jar; so let's just remove it for now
    $copy-&gt;cookie_jar(undef);

    $copy;
}


sub mirror
{
    my($self, $url, $file) = @_;

    die "Local file name is missing" unless defined $file &amp;&amp; length $file;

    my $request = HTTP::Request-&gt;new('GET', $url);

    # If the file exists, add a cache-related header
    if ( -e $file ) {
        my ($mtime) = ( stat($file) )[9];
        if ($mtime) {
            $request-&gt;header( 'If-Modified-Since' =&gt; HTTP::Date::time2str($mtime) );
        }
    }

    require File::Temp;
    my ($tmpfh, $tmpfile) = File::Temp::tempfile("$file-XXXXXX");
    close($tmpfh) or die "Could not close tmpfile '$tmpfile': $!";

    my $response = $self-&gt;request($request, $tmpfile);
    if ( $response-&gt;header('X-Died') ) {
        unlink($tmpfile);
        die $response-&gt;header('X-Died');
    }

    # Only fetching a fresh copy of the file would be considered success.
    # If the file was not modified, "304" would returned, which
    # is considered by HTTP::Status to be a "redirect", /not/ "success"
    if ( $response-&gt;is_success ) {
        my @stat        = stat($tmpfile) or die "Could not stat tmpfile '$tmpfile': $!";
        my $file_length = $stat[7];
        my ($content_length) = $response-&gt;header('Content-length');

        if ( defined $content_length and $file_length &lt; $content_length ) {
            unlink($tmpfile);
            die "Transfer truncated: only $file_length out of $content_length bytes received\n";
        }
        elsif ( defined $content_length and $file_length &gt; $content_length ) {
            unlink($tmpfile);
            die "Content-length mismatch: expected $content_length bytes, got $file_length\n";
        }
        # The file was the expected length.
        else {
            # Replace the stale file with a fresh copy
            # File::Copy will attempt to do it atomically,
            # and fall back to a delete + copy if that fails.
            File::Copy::move( $tmpfile, $file )
                or die "Cannot rename '$tmpfile' to '$file': $!\n";

            # Set standard file permissions if umask is supported.
            # If not, leave what File::Temp created in effect.
            if ( defined(my $umask = umask()) ) {
                my $mode = 0666 &amp;~ $umask;
                chmod $mode, $file
                    or die sprintf("Cannot chmod %o '%s': %s\n", $mode, $file, $!);
            }

            # make sure the file has the same last modification time
            if ( my $lm = $response-&gt;last_modified ) {
                utime $lm, $lm, $file
                    or warn "Cannot update modification time of '$file': $!\n";
            }
        }
    }
    # The local copy is fresh enough, so just delete the temp file
    else {
        unlink($tmpfile);
    }
    return $response;
}


sub _need_proxy {
    my($req, $ua) = @_;
    return if exists $req-&gt;{proxy};
    my $proxy = $ua-&gt;{proxy}{$req-&gt;uri-&gt;scheme} || return;
    if ($ua-&gt;{no_proxy}) {
        if (my $host = eval { $req-&gt;uri-&gt;host }) {
            for my $domain (@{$ua-&gt;{no_proxy}}) {
                $domain =~ s/^\.//;
                return if $host =~ /(?:^|\.)\Q$domain\E$/;
            }
        }
    }
    $req-&gt;{proxy} = $HTTP::URI_CLASS-&gt;new($proxy);
}


sub proxy {
    my $self = shift;
    my $key  = shift;
    if (!@_ &amp;&amp; ref $key eq 'ARRAY') {
        die 'odd number of items in proxy arrayref!' unless @{$key} % 2 == 0;

        # This map reads the elements of $key 2 at a time
        return
            map { $self-&gt;proxy($key-&gt;[2 * $_], $key-&gt;[2 * $_ + 1]) }
            (0 .. @{$key} / 2 - 1);
    }
    return map { $self-&gt;proxy($_, @_) } @$key if ref $key;

    Carp::croak("'$key' is not a valid URI scheme") unless $key =~ /^$URI::scheme_re\z/;
    my $old = $self-&gt;{'proxy'}{$key};
    if (@_) {
        my $url = shift;
        if (defined($url) &amp;&amp; length($url)) {
            Carp::croak("Proxy must be specified as absolute URI; '$url' is not") unless $url =~ /^$URI::scheme_re:/;
            Carp::croak("Bad http proxy specification '$url'") if $url =~ /^https?:/ &amp;&amp; $url !~ m,^https?://[\w[],;
        }
        $self-&gt;{proxy}{$key} = $url;
        $self-&gt;set_my_handler("request_preprepare", \&amp;_need_proxy)
    }
    return $old;
}


sub env_proxy {
    my ($self) = @_;
    require Encode;
    require Encode::Locale;
    my $env_request_method= $ENV{REQUEST_METHOD};
    my %seen;
    foreach my $k (sort keys %ENV) {
        my $real_key= $k;
        my $v= $ENV{$k}
            or next;
        if ( $env_request_method ) {
            # Need to be careful when called in the CGI environment, as
            # the HTTP_PROXY variable is under control of that other guy.
            next if $k =~ /^HTTP_/;
            $k = "HTTP_PROXY" if $k eq "CGI_HTTP_PROXY";
        }
	$k = lc($k);
        if (my $from_key= $seen{$k}) {
            warn "Environment contains multiple differing definitions for '$k'.\n".
                 "Using value from '$from_key' ($ENV{$from_key}) and ignoring '$real_key' ($v)"
                if $v ne $ENV{$from_key};
            next;
        } else {
            $seen{$k}= $real_key;
        }

	next unless $k =~ /^(.*)_proxy$/;
	$k = $1;
	if ($k eq 'no') {
	    $self-&gt;no_proxy(split(/\s*,\s*/, $v));
	}
	else {
            # Ignore random _proxy variables, allow only valid schemes
            next unless $k =~ /^$URI::scheme_re\z/;
            # Ignore xxx_proxy variables if xxx isn't a supported protocol
            next unless LWP::Protocol::implementor($k);
	    $self-&gt;proxy($k, Encode::decode(locale =&gt; $v));
	}
    }
}


sub no_proxy {
    my($self, @no) = @_;
    if (@no) {
	push(@{ $self-&gt;{'no_proxy'} }, @no);
    }
    else {
	$self-&gt;{'no_proxy'} = [];
    }
}


sub _new_response {
    my($request, $code, $message, $content) = @_;
    $message ||= HTTP::Status::status_message($code);
    my $response = HTTP::Response-&gt;new($code, $message);
    $response-&gt;request($request);
    $response-&gt;header("Client-Date" =&gt; HTTP::Date::time2str(time));
    $response-&gt;header("Client-Warning" =&gt; "Internal response");
    $response-&gt;header("Content-Type" =&gt; "text/plain");
    $response-&gt;content($content || "$code $message\n");
    return $response;
}


1;

__END__

=pod

=head1 NAME

LWP::UserAgent - Web user agent class

=head1 SYNOPSIS

    use strict;
    use warnings;

    use LWP::UserAgent ();

    my $ua = LWP::UserAgent-&gt;new(timeout =&gt; 10);
    $ua-&gt;env_proxy;

    my $response = $ua-&gt;get('http://example.com');

    if ($response-&gt;is_success) {
        print $response-&gt;decoded_content;
    }
    else {
        die $response-&gt;status_line;
    }

Extra layers of security (note the C&lt;cookie_jar&gt; and C&lt;protocols_allowed&gt;):

    use strict;
    use warnings;

    use HTTP::CookieJar::LWP ();
    use LWP::UserAgent       ();

    my $jar = HTTP::CookieJar::LWP-&gt;new;
    my $ua  = LWP::UserAgent-&gt;new(
        cookie_jar        =&gt; $jar,
        protocols_allowed =&gt; ['http', 'https'],
        timeout           =&gt; 10,
    );

    $ua-&gt;env_proxy;

    my $response = $ua-&gt;get('http://example.com');

    if ($response-&gt;is_success) {
        print $response-&gt;decoded_content;
    }
    else {
        die $response-&gt;status_line;
    }

=head1 DESCRIPTION

The L&lt;LWP::UserAgent&gt; is a class implementing a web user agent.
L&lt;LWP::UserAgent&gt; objects can be used to dispatch web requests.

In normal use the application creates an L&lt;LWP::UserAgent&gt; object, and
then configures it with values for timeouts, proxies, name, etc. It
then creates an instance of L&lt;HTTP::Request&gt; for the request that
needs to be performed. This request is then passed to one of the
request method the UserAgent, which dispatches it using the relevant
protocol, and returns a L&lt;HTTP::Response&gt; object.  There are
convenience methods for sending the most common request types:
L&lt;LWP::UserAgent/get&gt;, L&lt;LWP::UserAgent/head&gt;, L&lt;LWP::UserAgent/post&gt;,
L&lt;LWP::UserAgent/put&gt; and L&lt;LWP::UserAgent/delete&gt;.  When using these
methods, the creation of the request object is hidden as shown in the
synopsis above.

The basic approach of the library is to use HTTP-style communication
for all protocol schemes.  This means that you will construct
L&lt;HTTP::Request&gt; objects and receive L&lt;HTTP::Response&gt; objects even
for non-HTTP resources like I&lt;gopher&gt; and I&lt;ftp&gt;.  In order to achieve
even more similarity to HTTP-style communications, I&lt;gopher&gt; menus and
file directories are converted to HTML documents.

=head1 CONSTRUCTOR METHODS

The following constructor methods are available:

=head2 clone

    my $ua2 = $ua-&gt;clone;

Returns a copy of the L&lt;LWP::UserAgent&gt; object.

B&lt;CAVEAT&gt;: Please be aware that the clone method does not copy or clone your
C&lt;cookie_jar&gt; attribute. Due to the limited restrictions on what can be used
for your cookie jar, there is no way to clone the attribute. The C&lt;cookie_jar&gt;
attribute will be C&lt;undef&gt; in the new object instance.

=head2 new

    my $ua = LWP::UserAgent-&gt;new( %options )

This method constructs a new L&lt;LWP::UserAgent&gt; object and returns it.
Key/value pair arguments may be provided to set up the initial state.
The following options correspond to attribute methods described below:

   KEY                     DEFAULT
   -----------             --------------------
   agent                   "libwww-perl/#.###"
   conn_cache              undef
   cookie_jar              undef
   cookie_jar_class        HTTP::Cookies
   default_headers         HTTP::Headers-&gt;new
   from                    undef
   local_address           undef
   max_redirect            7
   max_size                undef
   no_proxy                []
   parse_head              1
   protocols_allowed       undef
   protocols_forbidden     undef
   proxy                   {}
   requests_redirectable   ['GET', 'HEAD']
   send_te                 1
   show_progress           undef
   ssl_opts                { verify_hostname =&gt; 1 }
   timeout                 180

The following additional options are also accepted: If the C&lt;env_proxy&gt; option
is passed in with a true value, then proxy settings are read from environment
variables (see L&lt;LWP::UserAgent/env_proxy&gt;). If C&lt;env_proxy&gt; isn't provided, the
C&lt;PERL_LWP_ENV_PROXY&gt; environment variable controls if
L&lt;LWP::UserAgent/env_proxy&gt; is called during initialization.  If the
C&lt;keep_alive&gt; option value is defined and non-zero, then an C&lt;LWP::ConnCache&gt; is set up (see
L&lt;LWP::UserAgent/conn_cache&gt;).  The C&lt;keep_alive&gt; value is passed on as the
C&lt;total_capacity&gt; for the connection cache.

C&lt;proxy&gt; must be set as an arrayref of key/value pairs. C&lt;no_proxy&gt; takes an
arrayref of domains.

=head1 ATTRIBUTES

The settings of the configuration attributes modify the behaviour of the
L&lt;LWP::UserAgent&gt; when it dispatches requests.  Most of these can also
be initialized by options passed to the constructor method.

The following attribute methods are provided.  The attribute value is
left unchanged if no argument is given.  The return value from each
method is the old attribute value.

=head2 agent

    my $agent = $ua-&gt;agent;
    $ua-&gt;agent('Checkbot/0.4 ');    # append the default to the end
    $ua-&gt;agent('Mozilla/5.0');
    $ua-&gt;agent("");                 # don't identify

Get/set the product token that is used to identify the user agent on
the network. The agent value is sent as the C&lt;User-Agent&gt; header in
the requests.

The default is a string of the form C&lt;libwww-perl/#.###&gt;, where C&lt;#.###&gt; is
substituted with the version number of this library.

If the provided string ends with space, the default C&lt;libwww-perl/#.###&gt;
string is appended to it.

The user agent string should be one or more simple product identifiers
with an optional version number separated by the C&lt;/&gt; character.

=head2 conn_cache

    my $cache_obj = $ua-&gt;conn_cache;
    $ua-&gt;conn_cache( $cache_obj );

Get/set the L&lt;LWP::ConnCache&gt; object to use.  See L&lt;LWP::ConnCache&gt;
for details.

=head2 cookie_jar

    my $jar = $ua-&gt;cookie_jar;
    $ua-&gt;cookie_jar( $cookie_jar_obj );

Get/set the cookie jar object to use.  The only requirement is that
the cookie jar object must implement the C&lt;extract_cookies($response)&gt; and
C&lt;add_cookie_header($request)&gt; methods.  These methods will then be
invoked by the user agent as requests are sent and responses are
received.  Normally this will be a L&lt;HTTP::Cookies&gt; object or some
subclass.  You are, however, encouraged to use L&lt;HTTP::CookieJar::LWP&gt;
instead.  See L&lt;/"BEST PRACTICES"&gt; for more information.

    use HTTP::CookieJar::LWP ();

    my $jar = HTTP::CookieJar::LWP-&gt;new;
    my $ua = LWP::UserAgent-&gt;new( cookie_jar =&gt; $jar );

    # or after object creation
    $ua-&gt;cookie_jar( $cookie_jar );

The default is to have no cookie jar, i.e. never automatically add
C&lt;Cookie&gt; headers to the requests.

If C&lt;$jar&gt; contains an unblessed hash reference, a new cookie jar object is
created for you automatically. The object is of the class set with the
C&lt;cookie_jar_class&gt; constructor argument, which defaults to L&lt;HTTP::Cookies&gt;.

  $ua-&gt;cookie_jar({ file =&gt; "$ENV{HOME}/.cookies.txt" });

is really just a shortcut for:

  require HTTP::Cookies;
  $ua-&gt;cookie_jar(HTTP::Cookies-&gt;new(file =&gt; "$ENV{HOME}/.cookies.txt"));

As described above and in L&lt;/"BEST PRACTICES"&gt;, you should set
C&lt;cookie_jar_class&gt; to C&lt;"HTTP::CookieJar::LWP"&gt; to get a safer cookie jar.

  my $ua = LWP::UserAgent-&gt;new( cookie_jar_class =&gt; 'HTTP::CookieJar::LWP' );
  $ua-&gt;cookie_jar({}); # HTTP::CookieJar::LWP takes no args

These can also be combined into the constructor, so a jar is created at
instantiation.

  my $ua = LWP::UserAgent-&gt;new(
    cookie_jar_class =&gt; 'HTTP::CookieJar::LWP',
    cookie_jar       =&gt;  {},
  );

=head2 credentials

    my $creds = $ua-&gt;credentials();
    $ua-&gt;credentials( $netloc, $realm );
    $ua-&gt;credentials( $netloc, $realm, $uname, $pass );
    $ua-&gt;credentials("www.example.com:80", "Some Realm", "foo", "secret");

Get/set the user name and password to be used for a realm.

The C&lt;$netloc&gt; is a string of the form C&lt;&lt; &lt;host&gt;:&lt;port&gt; &gt;&gt;.  The username and
password will only be passed to this server.

=head2 default_header

    $ua-&gt;default_header( $field );
    $ua-&gt;default_header( $field =&gt; $value );
    $ua-&gt;default_header('Accept-Encoding' =&gt; scalar HTTP::Message::decodable());
    $ua-&gt;default_header('Accept-Language' =&gt; "no, en");

This is just a shortcut for
C&lt;&lt; $ua-&gt;default_headers-&gt;header( $field =&gt; $value ) &gt;&gt;.

=head2 default_headers

    my $headers = $ua-&gt;default_headers;
    $ua-&gt;default_headers( $headers_obj );

Get/set the headers object that will provide default header values for
any requests sent.  By default this will be an empty L&lt;HTTP::Headers&gt;
object.

=head2 from

    my $from = $ua-&gt;from;
    $ua-&gt;from('foo@bar.com');

Get/set the email address for the human user who controls
the requesting user agent.  The address should be machine-usable, as
defined in L&lt;RFC2822|https://tools.ietf.org/html/rfc2822&gt;. The C&lt;from&gt; value
is sent as the C&lt;From&gt; header in the requests.

The default is to not send a C&lt;From&gt; header.  See
L&lt;LWP::UserAgent/default_headers&gt; for the more general interface that allow
any header to be defaulted.


=head2 local_address

    my $address = $ua-&gt;local_address;
    $ua-&gt;local_address( $address );

Get/set the local interface to bind to for network connections.  The interface
can be specified as a hostname or an IP address.  This value is passed as the
C&lt;LocalAddr&gt; argument to L&lt;IO::Socket::INET&gt;.

=head2 max_redirect

    my $max = $ua-&gt;max_redirect;
    $ua-&gt;max_redirect( $n );

This reads or sets the object's limit of how many times it will obey
redirection responses in a given request cycle.

By default, the value is C&lt;7&gt;. This means that if you call L&lt;LWP::UserAgent/request&gt;
and the response is a redirect elsewhere which is in turn a
redirect, and so on seven times, then LWP gives up after that seventh
request.

=head2 max_size

    my $size = $ua-&gt;max_size;
    $ua-&gt;max_size( $bytes );

Get/set the size limit for response content.  The default is C&lt;undef&gt;,
which means that there is no limit.  If the returned response content
is only partial, because the size limit was exceeded, then a
C&lt;Client-Aborted&gt; header will be added to the response.  The content
might end up longer than C&lt;max_size&gt; as we abort once appending a
chunk of data makes the length exceed the limit.  The C&lt;Content-Length&gt;
header, if present, will indicate the length of the full content and
will normally not be the same as C&lt;&lt; length($res-&gt;content) &gt;&gt;.

=head2 parse_head

    my $bool = $ua-&gt;parse_head;
    $ua-&gt;parse_head( $boolean );

Get/set a value indicating whether we should initialize response
headers from the E&lt;lt&gt;head&gt; section of HTML documents. The default is
true. I&lt;Do not turn this off&gt; unless you know what you are doing.

=head2 protocols_allowed

    my $aref = $ua-&gt;protocols_allowed;      # get allowed protocols
    $ua-&gt;protocols_allowed( \@protocols );  # allow ONLY these
    $ua-&gt;protocols_allowed(undef);          # delete the list
    $ua-&gt;protocols_allowed(['http',]);      # ONLY allow http

By default, an object has neither a C&lt;protocols_allowed&gt; list, nor a
L&lt;LWP::UserAgent/protocols_forbidden&gt; list.

This reads (or sets) this user agent's list of protocols that the
request methods will exclusively allow.  The protocol names are case
insensitive.

For example: C&lt;&lt; $ua-&gt;protocols_allowed( [ 'http', 'https'] ); &gt;&gt;
means that this user agent will I&lt;allow only&gt; those protocols,
and attempts to use this user agent to access URLs with any other
schemes (like C&lt;ftp://...&gt;) will result in a 500 error.

Note that having a C&lt;protocols_allowed&gt; list causes any
L&lt;LWP::UserAgent/protocols_forbidden&gt; list to be ignored.

=head2 protocols_forbidden

    my $aref = $ua-&gt;protocols_forbidden;    # get the forbidden list
    $ua-&gt;protocols_forbidden(\@protocols);  # do not allow these
    $ua-&gt;protocols_forbidden(['http',]);    # All http reqs get a 500
    $ua-&gt;protocols_forbidden(undef);        # delete the list

This reads (or sets) this user agent's list of protocols that the
request method will I&lt;not&gt; allow. The protocol names are case
insensitive.

For example: C&lt;&lt; $ua-&gt;protocols_forbidden( [ 'file', 'mailto'] ); &gt;&gt;
means that this user agent will I&lt;not&gt; allow those protocols, and
attempts to use this user agent to access URLs with those schemes
will result in a 500 error.

=head2 requests_redirectable

    my $aref = $ua-&gt;requests_redirectable;
    $ua-&gt;requests_redirectable( \@requests );
    $ua-&gt;requests_redirectable(['GET', 'HEAD',]); # the default

This reads or sets the object's list of request names that
L&lt;LWP::UserAgent/redirect_ok&gt; will allow redirection for. By default, this
is C&lt;['GET', 'HEAD']&gt;, as per L&lt;RFC 2616|https://tools.ietf.org/html/rfc2616&gt;.
To change to include C&lt;POST&gt;, consider:

   push @{ $ua-&gt;requests_redirectable }, 'POST';

=head2 send_te

    my $bool = $ua-&gt;send_te;
    $ua-&gt;send_te( $boolean );

If true, will send a C&lt;TE&gt; header along with the request. The default is
true. Set it to false to disable the C&lt;TE&gt; header for systems who can't
handle it.

=head2 show_progress

    my $bool = $ua-&gt;show_progress;
    $ua-&gt;show_progress( $boolean );

Get/set a value indicating whether a progress bar should be displayed
on the terminal as requests are processed. The default is false.

=head2 ssl_opts

    my @keys = $ua-&gt;ssl_opts;
    my $val = $ua-&gt;ssl_opts( $key );
    $ua-&gt;ssl_opts( $key =&gt; $value );

Get/set the options for SSL connections.  Without argument return the list
of options keys currently set.  With a single argument return the current
value for the given option.  With 2 arguments set the option value and return
the old.  Setting an option to the value C&lt;undef&gt; removes this option.

The options that LWP relates to are:

=over

=item C&lt;verify_hostname&gt; =&gt; $bool

When TRUE LWP will for secure protocol schemes ensure it connects to servers
that have a valid certificate matching the expected hostname.  If FALSE no
checks are made and you can't be sure that you communicate with the expected peer.
The no checks behaviour was the default for libwww-perl-5.837 and earlier releases.

This option is initialized from the C&lt;PERL_LWP_SSL_VERIFY_HOSTNAME&gt; environment
variable.  If this environment variable isn't set; then C&lt;verify_hostname&gt;
defaults to 1.

=item C&lt;SSL_ca_file&gt; =&gt; $path

The path to a file containing Certificate Authority certificates.
A default setting for this option is provided by checking the environment
variables C&lt;PERL_LWP_SSL_CA_FILE&gt; and C&lt;HTTPS_CA_FILE&gt; in order.

=item C&lt;SSL_ca_path&gt; =&gt; $path

The path to a directory containing files containing Certificate Authority
certificates.
A default setting for this option is provided by checking the environment
variables C&lt;PERL_LWP_SSL_CA_PATH&gt; and C&lt;HTTPS_CA_DIR&gt; in order.

=back

Other options can be set and are processed directly by the SSL Socket implementation
in use.  See L&lt;IO::Socket::SSL&gt; or L&lt;Net::SSL&gt; for details.

The libwww-perl core no longer bundles protocol plugins for SSL.  You will need
to install L&lt;LWP::Protocol::https&gt; separately to enable support for processing
https-URLs.

=head2 timeout

    my $secs = $ua-&gt;timeout;
    $ua-&gt;timeout( $secs );

Get/set the timeout value in seconds. The default value is
180 seconds, i.e. 3 minutes.

The request is aborted if no activity on the connection to the server
is observed for C&lt;timeout&gt; seconds.  This means that the time it takes
for the complete transaction and the L&lt;LWP::UserAgent/request&gt; method to
actually return might be longer.

When a request times out, a response object is still returned.  The response
will have a standard HTTP Status Code (500).  This response will have the
"Client-Warning" header set to the value of "Internal response".  See the
L&lt;LWP::UserAgent/get&gt; method description below for further details.

=head1 PROXY ATTRIBUTES

The following methods set up when requests should be passed via a
proxy server.

=head2 env_proxy

    $ua-&gt;env_proxy;

Load proxy settings from C&lt;*_proxy&gt; environment variables.  You might
specify proxies like this (sh-syntax):

  gopher_proxy=http://proxy.my.place/
  wais_proxy=http://proxy.my.place/
  no_proxy="localhost,example.com"
  export gopher_proxy wais_proxy no_proxy

csh or tcsh users should use the C&lt;setenv&gt; command to define these
environment variables.

On systems with case insensitive environment variables there exists a
name clash between the CGI environment variables and the C&lt;HTTP_PROXY&gt;
environment variable normally picked up by C&lt;env_proxy&gt;.  Because of
this C&lt;HTTP_PROXY&gt; is not honored for CGI scripts.  The
C&lt;CGI_HTTP_PROXY&gt; environment variable can be used instead.

=head2 no_proxy

    $ua-&gt;no_proxy( @domains );
    $ua-&gt;no_proxy('localhost', 'example.com');
    $ua-&gt;no_proxy(); # clear the list

Do not proxy requests to the given domains, including subdomains.
Calling C&lt;no_proxy&gt; without any domains clears the list of domains.

=head2 proxy

    $ua-&gt;proxy(\@schemes, $proxy_url)
    $ua-&gt;proxy(['http', 'ftp'], 'http://proxy.sn.no:8001/');

    # For a single scheme:
    $ua-&gt;proxy($scheme, $proxy_url)
    $ua-&gt;proxy('gopher', 'http://proxy.sn.no:8001/');

    # To set multiple proxies at once:
    $ua-&gt;proxy([
        ftp =&gt; 'http://ftp.example.com:8001/',
        [ 'http', 'https' ] =&gt; 'http://http.example.com:8001/',
    ]);

Set/retrieve proxy URL for a scheme.

The first form specifies that the URL is to be used as a proxy for
access methods listed in the list in the first method argument,
i.e. C&lt;http&gt; and C&lt;ftp&gt;.

The second form shows a shorthand form for specifying
proxy URL for a single access scheme.

The third form demonstrates setting multiple proxies at once. This is also
the only form accepted by the constructor.

=head1 HANDLERS

Handlers are code that injected at various phases during the
processing of requests.  The following methods are provided to manage
the active handlers:

=head2 add_handler

    $ua-&gt;add_handler( $phase =&gt; \&amp;cb, %matchspec )

Add handler to be invoked in the given processing phase.  For how to
specify C&lt;%matchspec&gt; see L&lt;HTTP::Config/"Matching"&gt;.

The possible values C&lt;$phase&gt; and the corresponding callback signatures are as
follows.  Note that the handlers are documented in the order in which they will
be run, which is:

    request_preprepare
    request_prepare
    request_send
    response_header
    response_data
    response_done
    response_redirect

=over

=item request_preprepare =&gt; sub { my($request, $ua, $handler) = @_; ... }

The handler is called before the C&lt;request_prepare&gt; and other standard
initialization of the request.  This can be used to set up headers
and attributes that the C&lt;request_prepare&gt; handler depends on.  Proxy
initialization should take place here; but in general don't register
handlers for this phase.

=item request_prepare =&gt; sub { my($request, $ua, $handler) = @_; ... }

The handler is called before the request is sent and can modify the
request any way it see fit.  This can for instance be used to add
certain headers to specific requests.

The method can assign a new request object to C&lt;$_[0]&gt; to replace the
request that is sent fully.

The return value from the callback is ignored.  If an exception is
raised it will abort the request and make the request method return a
"400 Bad request" response.

=item request_send =&gt; sub { my($request, $ua, $handler) = @_; ... }

This handler gets a chance of handling requests before they're sent to the
protocol handlers.  It should return an L&lt;HTTP::Response&gt; object if it
wishes to terminate the processing; otherwise it should return nothing.

The C&lt;response_header&gt; and C&lt;response_data&gt; handlers will not be
invoked for this response, but the C&lt;response_done&gt; will be.

=item response_header =&gt; sub { my($response, $ua, $handler) = @_; ... }

This handler is called right after the response headers have been
received, but before any content data.  The handler might set up
handlers for data and might croak to abort the request.

The handler might set the C&lt;&lt; $response-&gt;{default_add_content} &gt;&gt; value to
control if any received data should be added to the response object
directly.  This will initially be false if the C&lt;&lt; $ua-&gt;request() &gt;&gt; method
was called with a C&lt;$content_file&gt; or C&lt;$content_cb argument&gt;; otherwise true.

=item response_data =&gt; sub { my($response, $ua, $handler, $data) = @_; ... }

This handler is called for each chunk of data received for the
response.  The handler might croak to abort the request.

This handler needs to return a TRUE value to be called again for
subsequent chunks for the same request.

=item response_done =&gt; sub { my($response, $ua, $handler) = @_; ... }

The handler is called after the response has been fully received, but
before any redirect handling is attempted.  The handler can be used to
extract information or modify the response.

=item response_redirect =&gt; sub { my($response, $ua, $handler) = @_; ... }

The handler is called in C&lt;&lt; $ua-&gt;request &gt;&gt; after C&lt;response_done&gt;.  If the
handler returns an L&lt;HTTP::Request&gt; object we'll start over with processing
this request instead.

=back

For all of these, C&lt;$handler&gt; is a code reference to the handler that
is currently being run.

=head2 get_my_handler

    $ua-&gt;get_my_handler( $phase, %matchspec );
    $ua-&gt;get_my_handler( $phase, %matchspec, $init );

Will retrieve the matching handler as hash ref.

If C&lt;$init&gt; is passed as a true value, create and add the
handler if it's not found.  If C&lt;$init&gt; is a subroutine reference, then
it's called with the created handler hash as argument.  This sub might
populate the hash with extra fields; especially the callback.  If
C&lt;$init&gt; is a hash reference, merge the hashes.

=head2 handlers

    $ua-&gt;handlers( $phase, $request )
    $ua-&gt;handlers( $phase, $response )

Returns the handlers that apply to the given request or response at
the given processing phase.

=head2 remove_handler

    $ua-&gt;remove_handler( undef, %matchspec );
    $ua-&gt;remove_handler( $phase, %matchspec );
    $ua-&gt;remove_handler(); # REMOVE ALL HANDLERS IN ALL PHASES

Remove handlers that match the given C&lt;%matchspec&gt;.  If C&lt;$phase&gt; is not
provided, remove handlers from all phases.

Be careful as calling this function with C&lt;%matchspec&gt; that is not
specific enough can remove handlers not owned by you.  It's probably
better to use the L&lt;LWP::UserAgent/set_my_handler&gt; method instead.

The removed handlers are returned.

=head2 set_my_handler

    $ua-&gt;set_my_handler( $phase, $cb, %matchspec );
    $ua-&gt;set_my_handler($phase, undef); # remove handler for phase

Set handlers private to the executing subroutine.  Works by defaulting
an C&lt;owner&gt; field to the C&lt;%matchspec&gt; that holds the name of the called
subroutine.  You might pass an explicit C&lt;owner&gt; to override this.

If C&lt;$cb&gt; is passed as C&lt;undef&gt;, remove the handler.

=head1 REQUEST METHODS

The methods described in this section are used to dispatch requests
via the user agent.  The following request methods are provided:

=head2 delete

    my $res = $ua-&gt;delete( $url );
    my $res = $ua-&gt;delete( $url, $field_name =&gt; $value, ... );

This method will dispatch a C&lt;DELETE&gt; request on the given URL.  Additional
headers and content options are the same as for the L&lt;LWP::UserAgent/get&gt;
method.

This method will use the C&lt;DELETE()&gt; function from L&lt;HTTP::Request::Common&gt;
to build the request.  See L&lt;HTTP::Request::Common&gt; for a details on
how to pass form content and other advanced features.

=head2 get

    my $res = $ua-&gt;get( $url );
    my $res = $ua-&gt;get( $url , $field_name =&gt; $value, ... );

This method will dispatch a C&lt;GET&gt; request on the given URL.  Further
arguments can be given to initialize the headers of the request. These
are given as separate name/value pairs.  The return value is a
response object.  See L&lt;HTTP::Response&gt; for a description of the
interface it provides.

There will still be a response object returned when LWP can't connect to the
server specified in the URL or when other failures in protocol handlers occur.
These internal responses use the standard HTTP status codes, so the responses
can't be differentiated by testing the response status code alone.  Error
responses that LWP generates internally will have the "Client-Warning" header
set to the value "Internal response".  If you need to differentiate these
internal responses from responses that a remote server actually generates, you
need to test this header value.

Fields names that start with ":" are special.  These will not
initialize headers of the request but will determine how the response
content is treated.  The following special field names are recognized:

    ':content_file'   =&gt; $filename # or $filehandle
    ':content_cb'     =&gt; \&amp;callback
    ':read_size_hint' =&gt; $bytes

If a C&lt;$filename&gt; or C&lt;$filehandle&gt; is provided with the C&lt;:content_file&gt;
option, then the response content will be saved here instead of in
the response object.  The C&lt;$filehandle&gt; may also be an object with
an open file descriptor, such as a L&lt;File::Temp&gt; object.
If a callback is provided with the C&lt;:content_cb&gt; option then
this function will be called for each chunk of the response content as
it is received from the server.  If neither of these options are
given, then the response content will accumulate in the response
object itself.  This might not be suitable for very large response
bodies.  Only one of C&lt;:content_file&gt; or C&lt;:content_cb&gt; can be
specified.  The content of unsuccessful responses will always
accumulate in the response object itself, regardless of the
C&lt;:content_file&gt; or C&lt;:content_cb&gt; options passed in.  Note that errors
writing to the content file (for example due to permission denied
or the filesystem being full) will be reported via the C&lt;Client-Aborted&gt;
or C&lt;X-Died&gt; response headers, and not the C&lt;is_success&gt; method.

The C&lt;:read_size_hint&gt; option is passed to the protocol module which
will try to read data from the server in chunks of this size.  A
smaller value for the C&lt;:read_size_hint&gt; will result in a higher
number of callback invocations.

The callback function is called with 3 arguments: a chunk of data, a
reference to the response object, and a reference to the protocol
object.  The callback can abort the request by invoking C&lt;die()&gt;.  The
exception message will show up as the "X-Died" header field in the
response returned by the C&lt;&lt; $ua-&gt;get() &gt;&gt; method.

=head2 head

    my $res = $ua-&gt;head( $url );
    my $res = $ua-&gt;head( $url , $field_name =&gt; $value, ... );

This method will dispatch a C&lt;HEAD&gt; request on the given URL.
Otherwise it works like the L&lt;LWP::UserAgent/get&gt; method described above.

=head2 is_protocol_supported

    my $bool = $ua-&gt;is_protocol_supported( $scheme );

You can use this method to test whether this user agent object supports the
specified C&lt;scheme&gt;.  (The C&lt;scheme&gt; might be a string (like C&lt;http&gt; or
C&lt;ftp&gt;) or it might be an L&lt;URI&gt; object reference.)

Whether a scheme is supported is determined by the user agent's
C&lt;protocols_allowed&gt; or C&lt;protocols_forbidden&gt; lists (if any), and by
the capabilities of LWP.  I.e., this will return true only if LWP
supports this protocol I&lt;and&gt; it's permitted for this particular
object.

=head2 is_online

    my $bool = $ua-&gt;is_online;

Tries to determine if you have access to the Internet. Returns C&lt;1&gt; (true)
if the built-in heuristics determine that the user agent is
able to access the Internet (over HTTP) or C&lt;0&gt; (false).

See also L&lt;LWP::Online&gt;.

=head2 mirror

    my $res = $ua-&gt;mirror( $url, $filename );

This method will get the document identified by URL and store it in
file called C&lt;$filename&gt;.  If the file already exists, then the request
will contain an C&lt;If-Modified-Since&gt; header matching the modification
time of the file.  If the document on the server has not changed since
this time, then nothing happens.  If the document has been updated, it
will be downloaded again.  The modification time of the file will be
forced to match that of the server.

Uses L&lt;File::Copy/move&gt; to attempt to atomically replace the C&lt;$filename&gt;.

The return value is an L&lt;HTTP::Response&gt; object.

=head2 patch

    # Any version of HTTP::Message works with this form:
    my $res = $ua-&gt;patch( $url, $field_name =&gt; $value, Content =&gt; $content );

    # Using hash or array references requires HTTP::Message &gt;= 6.12
    use HTTP::Request 6.12;
    my $res = $ua-&gt;patch( $url, \%form );
    my $res = $ua-&gt;patch( $url, \@form );
    my $res = $ua-&gt;patch( $url, \%form, $field_name =&gt; $value, ... );
    my $res = $ua-&gt;patch( $url, $field_name =&gt; $value, Content =&gt; \%form );
    my $res = $ua-&gt;patch( $url, $field_name =&gt; $value, Content =&gt; \@form );

This method will dispatch a C&lt;PATCH&gt; request on the given URL, with
C&lt;%form&gt; or C&lt;@form&gt; providing the key/value pairs for the fill-in form
content. Additional headers and content options are the same as for
the L&lt;LWP::UserAgent/get&gt; method.

CAVEAT:

This method can only accept content that is in key-value pairs when using
L&lt;HTTP::Request::Common&gt; prior to version C&lt;6.12&gt;. Any use of hash or array
references will result in an error prior to version C&lt;6.12&gt;.

This method will use the C&lt;PATCH&gt; function from L&lt;HTTP::Request::Common&gt;
to build the request.  See L&lt;HTTP::Request::Common&gt; for a details on
how to pass form content and other advanced features.

=head2 post

    my $res = $ua-&gt;post( $url, \%form );
    my $res = $ua-&gt;post( $url, \@form );
    my $res = $ua-&gt;post( $url, \%form, $field_name =&gt; $value, ... );
    my $res = $ua-&gt;post( $url, $field_name =&gt; $value, Content =&gt; \%form );
    my $res = $ua-&gt;post( $url, $field_name =&gt; $value, Content =&gt; \@form );
    my $res = $ua-&gt;post( $url, $field_name =&gt; $value, Content =&gt; $content );

This method will dispatch a C&lt;POST&gt; request on the given URL, with
C&lt;%form&gt; or C&lt;@form&gt; providing the key/value pairs for the fill-in form
content. Additional headers and content options are the same as for
the L&lt;LWP::UserAgent/get&gt; method.

This method will use the C&lt;POST&gt; function from L&lt;HTTP::Request::Common&gt;
to build the request.  See L&lt;HTTP::Request::Common&gt; for a details on
how to pass form content and other advanced features.

=head2 put

    # Any version of HTTP::Message works with this form:
    my $res = $ua-&gt;put( $url, $field_name =&gt; $value, Content =&gt; $content );

    # Using hash or array references requires HTTP::Message &gt;= 6.07
    use HTTP::Request 6.07;
    my $res = $ua-&gt;put( $url, \%form );
    my $res = $ua-&gt;put( $url, \@form );
    my $res = $ua-&gt;put( $url, \%form, $field_name =&gt; $value, ... );
    my $res = $ua-&gt;put( $url, $field_name =&gt; $value, Content =&gt; \%form );
    my $res = $ua-&gt;put( $url, $field_name =&gt; $value, Content =&gt; \@form );

This method will dispatch a C&lt;PUT&gt; request on the given URL, with
C&lt;%form&gt; or C&lt;@form&gt; providing the key/value pairs for the fill-in form
content. Additional headers and content options are the same as for
the L&lt;LWP::UserAgent/get&gt; method.

CAVEAT:

This method can only accept content that is in key-value pairs when using
L&lt;HTTP::Request::Common&gt; prior to version C&lt;6.07&gt;. Any use of hash or array
references will result in an error prior to version C&lt;6.07&gt;.

This method will use the C&lt;PUT&gt; function from L&lt;HTTP::Request::Common&gt;
to build the request.  See L&lt;HTTP::Request::Common&gt; for a details on
how to pass form content and other advanced features.

=head2 request

    my $res = $ua-&gt;request( $request );
    my $res = $ua-&gt;request( $request, $content_file );
    my $res = $ua-&gt;request( $request, $content_cb );
    my $res = $ua-&gt;request( $request, $content_cb, $read_size_hint );

This method will dispatch the given C&lt;$request&gt; object. Normally this
will be an instance of the L&lt;HTTP::Request&gt; class, but any object with
a similar interface will do. The return value is an L&lt;HTTP::Response&gt; object.

The C&lt;request&gt; method will process redirects and authentication
responses transparently. This means that it may actually send several
simple requests via the L&lt;LWP::UserAgent/simple_request&gt; method described below.

The request methods described above; L&lt;LWP::UserAgent/get&gt;, L&lt;LWP::UserAgent/head&gt;,
L&lt;LWP::UserAgent/post&gt; and L&lt;LWP::UserAgent/mirror&gt; will all dispatch the request
they build via this method. They are convenience methods that simply hide the
creation of the request object for you.

The C&lt;$content_file&gt;, C&lt;$content_cb&gt; and C&lt;$read_size_hint&gt; all correspond to
options described with the L&lt;LWP::UserAgent/get&gt; method above. Note that errors
writing to the content file (for example due to permission denied
or the filesystem being full) will be reported via the C&lt;Client-Aborted&gt;
or C&lt;X-Died&gt; response headers, and not the C&lt;is_success&gt; method.

You are allowed to use a CODE reference as C&lt;content&gt; in the request
object passed in.  The C&lt;content&gt; function should return the content
when called.  The content can be returned in chunks.  The content
function will be invoked repeatedly until it return an empty string to
signal that there is no more content.

=head2 simple_request

    my $request = HTTP::Request-&gt;new( ... );
    my $res = $ua-&gt;simple_request( $request );
    my $res = $ua-&gt;simple_request( $request, $content_file );
    my $res = $ua-&gt;simple_request( $request, $content_cb );
    my $res = $ua-&gt;simple_request( $request, $content_cb, $read_size_hint );

This method dispatches a single request and returns the response
received.  Arguments are the same as for the L&lt;LWP::UserAgent/request&gt; described above.

The difference from L&lt;LWP::UserAgent/request&gt; is that C&lt;simple_request&gt; will not try to
handle redirects or authentication responses.  The L&lt;LWP::UserAgent/request&gt; method
will, in fact, invoke this method for each simple request it sends.

=head1 CALLBACK METHODS

The following methods will be invoked as requests are processed. These
methods are documented here because subclasses of L&lt;LWP::UserAgent&gt;
might want to override their behaviour.

=head2 get_basic_credentials

    # This checks wantarray and can either return an array:
    my ($user, $pass) = $ua-&gt;get_basic_credentials( $realm, $uri, $isproxy );
    # or a string that looks like "user:pass"
    my $creds = $ua-&gt;get_basic_credentials($realm, $uri, $isproxy);

This is called by L&lt;LWP::UserAgent/request&gt; to retrieve credentials for documents
protected by Basic or Digest Authentication.  The arguments passed in
is the C&lt;$realm&gt; provided by the server, the C&lt;$uri&gt; requested and a
C&lt;boolean flag&gt; to indicate if this is authentication against a proxy server.

The method should return a username and password.  It should return an
empty list to abort the authentication resolution attempt.  Subclasses
can override this method to prompt the user for the information. An
example of this can be found in C&lt;lwp-request&gt; program distributed
with this library.

The base implementation simply checks a set of pre-stored member
variables, set up with the L&lt;LWP::UserAgent/credentials&gt; method.

=head2 prepare_request

    $request = $ua-&gt;prepare_request( $request );

This method is invoked by L&lt;LWP::UserAgent/simple_request&gt;. Its task is
to modify the given C&lt;$request&gt; object by setting up various headers based
on the attributes of the user agent. The return value should normally be the
C&lt;$request&gt; object passed in.  If a different request object is returned
it will be the one actually processed.

The headers affected by the base implementation are; C&lt;User-Agent&gt;,
C&lt;From&gt;, C&lt;Range&gt; and C&lt;Cookie&gt;.

=head2 progress

    my $prog = $ua-&gt;progress( $status, $request_or_response );

This is called frequently as the response is received regardless of
how the content is processed.  The method is called with C&lt;$status&gt;
"begin" at the start of processing the request and with C&lt;$state&gt; "end"
before the request method returns.  In between these C&lt;$status&gt; will be
the fraction of the response currently received or the string "tick"
if the fraction can't be calculated.

When C&lt;$status&gt; is "begin" the second argument is the L&lt;HTTP::Request&gt; object,
otherwise it is the L&lt;HTTP::Response&gt; object.

=head2 redirect_ok

    my $bool = $ua-&gt;redirect_ok( $prospective_request, $response );

This method is called by L&lt;LWP::UserAgent/request&gt; before it tries to follow a
redirection to the request in C&lt;$response&gt;.  This should return a true
value if this redirection is permissible.  The C&lt;$prospective_request&gt;
will be the request to be sent if this method returns true.

The base implementation will return false unless the method
is in the object's C&lt;requests_redirectable&gt; list,
false if the proposed redirection is to a C&lt;file://...&gt;
URL, and true otherwise.

=head1 BEST PRACTICES

The default settings can get you up and running quickly, but there are settings
you can change in order to make your life easier.

=head2 Handling Cookies

You are encouraged to install L&lt;Mozilla::PublicSuffix&gt; and use
L&lt;HTTP::CookieJar::LWP&gt; as your cookie jar.  L&lt;HTTP::CookieJar::LWP&gt; provides a
better security model matching that of current Web browsers when
L&lt;Mozilla::PublicSuffix&gt; is installed.

    use HTTP::CookieJar::LWP ();

    my $jar = HTTP::CookieJar::LWP-&gt;new;
    my $ua = LWP::UserAgent-&gt;new( cookie_jar =&gt; $jar );

See L&lt;/"cookie_jar"&gt; for more information.

=head2 Managing Protocols

C&lt;protocols_allowed&gt; gives you the ability to allow arbitrary protocols.

    my $ua = LWP::UserAgent-&gt;new(
        protocols_allowed =&gt; [ 'http', 'https' ]
    );

This will prevent you from inadvertently following URLs like
C&lt;file:///etc/passwd&gt;.  See L&lt;/"protocols_allowed"&gt;.

C&lt;protocols_forbidden&gt; gives you the ability to deny arbitrary protocols.

    my $ua = LWP::UserAgent-&gt;new(
        protocols_forbidden =&gt; [ 'file', 'mailto', 'ssh', ]
    );

This can also prevent you from inadvertently following URLs like
C&lt;file:///etc/passwd&gt;.  See L&lt;/protocols_forbidden&gt;.

=head1 SEE ALSO

See L&lt;LWP&gt; for a complete overview of libwww-perl5.  See L&lt;lwpcook&gt;
and the scripts F&lt;lwp-request&gt; and F&lt;lwp-download&gt; for examples of
usage.

See L&lt;HTTP::Request&gt; and L&lt;HTTP::Response&gt; for a description of the
message objects dispatched and received.  See L&lt;HTTP::Request::Common&gt;
and L&lt;HTML::Form&gt; for other ways to build request objects.

See L&lt;WWW::Mechanize&gt; and L&lt;WWW::Search&gt; for examples of more
specialized user agents based on L&lt;LWP::UserAgent&gt;.

=head1 COPYRIGHT AND LICENSE

Copyright 1995-2009 Gisle Aas.

This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=cut
</pre></body></html>