📄 useragent.pm
字号:
=item $ua->credentials($netloc, $realm, $uname, $pass)
Set the user name and password to be used for a realm. It is often more
useful to specialize the get_basic_credentials() method instead.
=cut
sub credentials
{
my($self, $netloc, $realm, $uid, $pass) = @_;
@{ $self->{'basic_authentication'}{$netloc}{$realm} } = ($uid, $pass);
}
=item $ua->get_basic_credentials($realm, $uri, [$proxy])
This is called by request() to retrieve credentials for a Realm
protected by Basic Authentication or Digest Authentication.
Should return username and password in a list. Return undef to abort
the authentication resolution atempts.
This implementation simply checks a set of pre-stored member
variables. Subclasses can override this method to e.g. ask the user
for a username/password. An example of this can be found in
C<lwp-request> program distributed with this library.
=cut
sub get_basic_credentials
{
my($self, $realm, $uri, $proxy) = @_;
return if $proxy;
my $netloc = $uri->netloc;
if (exists $self->{'basic_authentication'}{$netloc}{$realm}) {
return @{ $self->{'basic_authentication'}{$netloc}{$realm} };
}
return (undef, undef);
}
=item $ua->agent([$product_id])
Get/set the product token that is used to identify the user agent on
the network. The agent value is sent as the "User-Agent" header in
the requests. The default agent name is "libwww-perl/#.##", where
"#.##" is substitued with the version numer of this library.
The user agent string should be one or more simple product identifiers
with an optional version number separated by the "/" character.
Examples are:
$ua->agent('Checkbot/0.4 ' . $ua->agent);
$ua->agent('Mozilla/5.0');
=item $ua->from([$email_address])
Get/set the Internet e-mail address for the human user who controls
the requesting user agent. The address should be machine-usable, as
defined in RFC 822. The from value is send as the "From" header in
the requests. There is no default. Example:
$ua->from('aas@sn.no');
=item $ua->timeout([$secs])
Get/set the timeout value in seconds. The default timeout() value is
180 seconds, i.e. 3 minutes.
=item $ua->cookie_jar([$cookies])
Get/set the I<HTTP::Cookies> object to use. The default is to have no
cookie_jar, i.e. never automatically add "Cookie" headers to the
requests.
=item $ua->parse_head([$boolean])
Get/set a value indicating wether we should initialize response
headers from the E<lt>head> section of HTML documents. The default is
TRUE. Do not turn this off, unless you know what you are doing.
=item $ua->max_size([$bytes])
Get/set the size limit for response content. The default is undef,
which means that there is not limit. If the returned response content
is only partial, because the size limit was exceeded, then a
"X-Content-Range" header will be added to the response.
=cut
sub timeout { shift->_elem('timeout', @_); }
sub agent { shift->_elem('agent', @_); }
sub from { shift->_elem('from', @_); }
sub cookie_jar { shift->_elem('cookie_jar',@_); }
sub parse_head { shift->_elem('parse_head',@_); }
sub max_size { shift->_elem('max_size', @_); }
# depreciated
sub use_eval { shift->_elem('use_eval', @_); }
sub use_alarm
{
Carp::carp("LWP::UserAgent->use_alarm(BOOL) is a no-op")
if @_ > 1 && $^W;
"";
}
# Declarations of AutoLoaded methods
sub clone;
sub is_protocol_supported;
sub mirror;
sub proxy;
sub env_proxy;
sub no_proxy;
sub _need_proxy;
1;
__END__
=item $ua->clone;
Returns a copy of the LWP::UserAgent object
=cut
sub clone
{
my $self = shift;
my $copy = bless { %$self }, ref $self; # copy most fields
# elements that are references must be handled in a special way
$copy->{'no_proxy'} = [ @{$self->{'no_proxy'}} ]; # copy array
$copy;
}
=item $ua->is_protocol_supported($scheme)
You can use this method to query if the library currently support the
specified C<scheme>. The C<scheme> might be a string (like 'http' or
'ftp') or it might be an URI::URL object reference.
=cut
sub is_protocol_supported
{
my($self, $scheme) = @_;
if (ref $scheme) {
# assume we got a reference to an URI::URL object
$scheme = $scheme->abs->scheme;
} else {
Carp::croak("Illeal scheme '$scheme' passed to is_protocol_supported")
if $scheme =~ /\W/;
$scheme = lc $scheme;
}
return LWP::Protocol::implementor($scheme);
}
=item $ua->mirror($url, $file)
Get and store a document identified by a URL, using If-Modified-Since,
and checking of the Content-Length. Returns a reference to the
response object.
=cut
sub mirror
{
my($self, $url, $file) = @_;
LWP::Debug::trace('()');
my $request = new HTTP::Request('GET', $url);
if (-e $file) {
my($mtime) = (stat($file))[9];
if($mtime) {
$request->header('If-Modified-Since' =>
HTTP::Date::time2str($mtime));
}
}
my $tmpfile = "$file-$$";
my $response = $self->request($request, $tmpfile);
if ($response->is_success) {
my $file_length = (stat($tmpfile))[7];
my($content_length) = $response->header('Content-length');
if (defined $content_length and $file_length < $content_length) {
unlink($tmpfile);
die "Transfer truncated: " .
"only $file_length out of $content_length bytes received\n";
} elsif (defined $content_length and $file_length > $content_length) {
unlink($tmpfile);
die "Content-length mismatch: " .
"expected $content_length bytes, got $file_length\n";
} else {
# OK
if (-e $file) {
# Some dosish systems fail to rename if the target exists
chmod 0777, $file;
unlink $file;
}
rename($tmpfile, $file) or
die "Cannot rename '$tmpfile' to '$file': $!\n";
if (my $lm = $response->last_modified) {
# make sure the file has the same last modification time
utime $lm, $lm, $file;
}
}
} else {
unlink($tmpfile);
}
return $response;
}
=item $ua->proxy(...)
Set/retrieve proxy URL for a scheme:
$ua->proxy(['http', 'ftp'], 'http://proxy.sn.no:8001/');
$ua->proxy('gopher', 'http://proxy.sn.no:8001/');
The first form specifies that the URL is to be used for proxying of
access methods listed in the list in the first method argument,
i.e. 'http' and 'ftp'.
The second form shows a shorthand form for specifying
proxy URL for a single access scheme.
=cut
sub proxy
{
my($self, $key, $proxy) = @_;
LWP::Debug::trace("$key, $proxy");
if (!ref($key)) { # single scalar passed
my $old = $self->{'proxy'}{$key};
$self->{'proxy'}{$key} = $proxy;
return $old;
} elsif (ref($key) eq 'ARRAY') {
for(@$key) { # array passed
$self->{'proxy'}{$_} = $proxy;
}
}
return undef;
}
=item $ua->env_proxy()
Load proxy settings from *_proxy environment variables. You might
specify proxies like this (sh-syntax):
gopher_proxy=http://proxy.my.place/
wais_proxy=http://proxy.my.place/
no_proxy="my.place"
export gopher_proxy wais_proxy no_proxy
Csh or tcsh users should use the C<setenv> command to define these
envirionment variables.
=cut
sub env_proxy {
my ($self) = @_;
my($k,$v);
while(($k, $v) = each %ENV) {
$k = lc($k);
next unless $k =~ /^(.*)_proxy$/;
$k = $1;
if ($k eq 'no') {
$self->no_proxy(split(/\s*,\s*/, $v));
}
else {
$self->proxy($k, $v);
}
}
}
=item $ua->no_proxy($domain,...)
Do not proxy requests to the given domains. Calling no_proxy without
any domains clears the list of domains. Eg:
$ua->no_proxy('localhost', 'no', ...);
=cut
sub no_proxy {
my($self, @no) = @_;
if (@no) {
push(@{ $self->{'no_proxy'} }, @no);
}
else {
$self->{'no_proxy'} = [];
}
}
# Private method which returns the URL of the Proxy configured for this
# URL, or undefined if none is configured.
sub _need_proxy
{
my($self, $url) = @_;
$url = new URI::URL($url) unless ref $url;
LWP::Debug::trace("($url)");
# check the list of noproxies
if (@{ $self->{'no_proxy'} }) {
my $host = $url->host;
return undef unless defined $host;
my $domain;
for $domain (@{ $self->{'no_proxy'} }) {
if ($host =~ /$domain$/) {
LWP::Debug::trace("no_proxy configured");
return undef;
}
}
}
# Currently configured per scheme.
# Eventually want finer granularity
my $scheme = $url->scheme;
if (exists $self->{'proxy'}{$scheme}) {
LWP::Debug::debug('Proxied');
return new URI::URL($self->{'proxy'}{$scheme});
}
LWP::Debug::debug('Not proxied');
undef;
}
1;
=back
=head1 SEE ALSO
See L<LWP> for a complete overview of libwww-perl5. See F<lwp-request> and
F<lwp-mirror> for examples of usage.
=head1 COPYRIGHT
Copyright 1995-1997 Gisle Aas.
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -