1
# $Id: UserAgent.pm,v 2.1 2001/12/11 21:11:29 gisle Exp $
3
1
package LWP::UserAgent;
3
# $Id: UserAgent.pm,v 2.33 2004/09/16 09:28:22 gisle Exp $
8
LWP::UserAgent - A WWW UserAgent class
12
require LWP::UserAgent;
13
my $ua = LWP::UserAgent->new(env_proxy => 1,
18
$response = $ua->get('http://search.cpan.org/');
22
$request = HTTP::Request->new('GET', 'http://search.cpan.org/');
23
# and then one of these:
24
$response = $ua->request($request); # or
25
$response = $ua->request($request, '/tmp/sss'); # or
26
$response = $ua->request($request, \&callback, 4096);
28
sub callback { my($data, $response, $protocol) = @_; .... }
32
The C<LWP::UserAgent> is a class implementing a World-Wide Web
33
user agent in Perl. It brings together the HTTP::Request,
34
HTTP::Response and the LWP::Protocol classes that form the rest of the
35
core of libwww-perl library. For simple uses this class can be used
36
directly to dispatch WWW requests, alternatively it can be subclassed
37
for application-specific behaviour.
39
In normal use the application creates a C<LWP::UserAgent> object, and then
40
configures it with values for timeouts, proxies, name, etc. It then
41
creates an instance of C<HTTP::Request> for the request that
42
needs to be performed. This request is then passed to one of the UserAgent's
43
request() methods, which dispatches it using the relevant protocol,
44
and returns a C<HTTP::Response> object.
46
There are convenience methods for sending the most common request
47
types; get(), head() and post().
49
The basic approach of the library is to use HTTP style communication
50
for all protocol schemes, i.e. you even receive an C<HTTP::Response>
51
object for gopher or ftp requests. In order to achieve even more
52
similarity to HTTP style communications, gopher menus and file
53
directories are converted to HTML documents.
55
The send_request(), simple_request() and request() methods can process
56
the content of the response in one of three ways: in core, into a
57
file, or into repeated calls to a subroutine. You choose which one by
58
the kind of value passed as the second argument.
60
The in core variant simply stores the content in a scalar 'content'
61
attribute of the response object and is suitable for small HTML
62
replies that might need further parsing. This variant is used if the
63
second argument is missing (or is undef).
65
The filename variant requires a scalar containing a filename as the
66
second argument to the request method and is suitable for large WWW
67
objects which need to be written directly to the file without
68
requiring large amounts of memory. In this case the response object
69
returned from the request method will have an empty content attribute.
70
If the request fails, then the content might not be empty, and the
71
file will be untouched.
73
The subroutine variant requires a reference to callback routine as the
74
second argument to the request method and it can also take an optional
75
chuck size as the third argument. This variant can be used to
76
construct "pipe-lined" processing, where processing of received
77
chuncks can begin before the complete data has arrived. The callback
78
function is called with 3 arguments: the data received this time, a
79
reference to the response object and a reference to the protocol
80
object. The response object returned from the request method will
81
have empty content. If the request fails, then the the callback
82
routine is not called, and the response->content might not be empty.
84
The request can be aborted by calling die() in the callback
85
routine. The die message will be available as the "X-Died" special
86
response header field.
88
The library also allows you to use a subroutine reference as
89
content in the request object. This subroutine should return the
90
content (possibly in pieces) when called. It should return an empty
91
string when there is no more content.
95
The following methods are available:
102
6
use vars qw(@ISA $VERSION);
104
8
require LWP::MemberMixin;
105
9
@ISA = qw(LWP::MemberMixin);
106
$VERSION = sprintf("%d.%03d", q$Revision: 2.1 $ =~ /(\d+)\.(\d+)/);
10
$VERSION = sprintf("%d.%03d", q$Revision: 2.33 $ =~ /(\d+)\.(\d+)/);
108
12
use HTTP::Request ();
109
13
use HTTP::Response ();
536
408
return $response;
539
#---------------------------------------------------------------------------
540
413
# Now the shortcuts...
542
=item $ua->get($url, Header => Value,...);
544
This is a shortcut for C<$ua-E<gt>request(HTTP::Request::Common::GET(
545
$url, Header =E<gt> Value,... ))>. See
546
L<HTTP::Request::Common|HTTP::Request::Common>.
548
=item $ua->post($url, \%formref, Header => Value,...);
550
This is a shortcut for C<$ua-E<gt>request( HTTP::Request::Common::POST(
551
$url, \%formref, Header =E<gt> Value,... ))>. Note that the form
552
reference is optional, and can be either a hashref (C<\%formdata> or C<{
553
'key1' => 'val2', 'key2' => 'val2', ...
554
}>) or an arrayref (C<\@formdata> or
555
C<['key1' => 'val2', 'key2' => 'val2', ...]>). See
556
L<HTTP::Request::Common|HTTP::Request::Common>.
558
=item $ua->head($url, Header => Value,...);
560
This is a shortcut for C<$ua-E<gt>request( HTTP::Request::Common::HEAD(
561
$url, Header =E<gt> Value,... ))>. See
562
L<HTTP::Request::Common|HTTP::Request::Common>.
564
=item $ua->put($url, Header => Value,...);
566
This is a shortcut for C<$ua-E<gt>request( HTTP::Request::Common::PUT(
567
$url, Header =E<gt> Value,... ))>. See
568
L<HTTP::Request::Common|HTTP::Request::Common>.
573
require HTTP::Request::Common;
574
return shift->request( HTTP::Request::Common::GET( @_ ) );
416
require HTTP::Request::Common;
417
my($self, @parameters) = @_;
418
my @suff = $self->_process_colonic_headers(\@parameters,1);
419
return $self->request( HTTP::Request::Common::GET( @parameters ), @suff );
578
require HTTP::Request::Common;
579
return shift->request( HTTP::Request::Common::POST( @_ ) );
424
require HTTP::Request::Common;
425
my($self, @parameters) = @_;
426
my @suff = $self->_process_colonic_headers(\@parameters,2);
427
return $self->request( HTTP::Request::Common::POST( @parameters ), @suff );
583
require HTTP::Request::Common;
584
return shift->request( HTTP::Request::Common::HEAD( @_ ) );
588
require HTTP::Request::Common;
589
return shift->request( HTTP::Request::Common::PUT( @_ ) );
593
#---------------------------------------------------------------------------
432
require HTTP::Request::Common;
433
my($self, @parameters) = @_;
434
my @suff = $self->_process_colonic_headers(\@parameters,1);
435
return $self->request( HTTP::Request::Common::HEAD( @parameters ), @suff );
439
sub _process_colonic_headers {
440
# Process :content_cb / :content_file / :read_size_hint headers.
441
my($self, $args, $start_index) = @_;
444
for(my $i = $start_index; $i < @$args; $i += 2) {
445
next unless defined $args->[$i];
447
#printf "Considering %s => %s\n", $args->[$i], $args->[$i + 1];
449
if($args->[$i] eq ':content_cb') {
450
# Some sanity-checking...
451
$arg = $args->[$i + 1];
452
Carp::croak("A :content_cb value can't be undef") unless defined $arg;
453
Carp::croak("A :content_cb value must be a coderef")
454
unless ref $arg and UNIVERSAL::isa($arg, 'CODE');
457
elsif ($args->[$i] eq ':content_file') {
458
$arg = $args->[$i + 1];
460
# Some sanity-checking...
461
Carp::croak("A :content_file value can't be undef")
463
Carp::croak("A :content_file value can't be a reference")
465
Carp::croak("A :content_file value can't be \"\"")
469
elsif ($args->[$i] eq ':read_size_hint') {
470
$size = $args->[$i + 1];
471
# Bother checking it?
477
splice @$args, $i, 2;
481
# And return a suitable suffix-list for request(REQ,...)
483
return unless defined $arg;
484
return $arg, $size if defined $size;
594
490
# This whole allow/forbid thing is based on man 1 at's way of doing things.
596
=item $ua->protocols_allowed( ); # to read
598
=item $ua->protocols_allowed( \@protocols ); # to set
600
This reads (or sets) this user-agent's list of procotols that
601
C<$ua-E<gt>request> and C<$ua-E<gt>simple_request> will exclusively
604
For example: C<$ua-E<gt>protocols_allowed( [ 'http', 'https'] );>
605
means that this user agent will I<allow only> those protocols,
606
and attempts to use this user-agent to access URLs with any other
607
schemes (like "ftp://...") will result in a 500 error.
609
To delete the list, call:
610
C<$ua-E<gt>protocols_allowed(undef)>
612
By default, an object has neither a protocols_allowed list, nor
613
a protocols_forbidden list.
615
Note that having a protocols_allowed
616
list causes any protocols_forbidden list to be ignored.
618
=item $ua->protocols_forbidden( ); # to read
620
=item $ua->protocols_forbidden( \@protocols ); # to set
622
This reads (or sets) this user-agent's list of procotols that
623
C<$ua-E<gt>request> and C<$ua-E<gt>simple_request> will I<not> allow.
625
For example: C<$ua-E<gt>protocols_forbidden( [ 'file', 'mailto'] );>
626
means that this user-agent will I<not> allow those protocols, and
627
attempts to use this user-agent to access URLs with those schemes
628
will result in a 500 error.
630
To delete the list, call:
631
C<$ua-E<gt>protocols_forbidden(undef)>
633
=item $ua->is_protocol_supported($scheme)
635
You can use this method to test whether this user-agent object supports the
636
specified C<scheme>. (The C<scheme> might be a string (like 'http' or
637
'ftp') or it might be an URI object reference.)
639
Whether a scheme is supported, is determined by $ua's protocols_allowed or
640
protocols_forbidden lists (if any), and by the capabilities
641
of LWP. I.e., this will return TRUE only if LWP supports this protocol
642
I<and> it's permitted for this particular object.
646
492
sub is_protocol_supported
648
494
my($self, $scheme) = @_;
649
495
if (ref $scheme) {
650
496
# assume we got a reference to an URI object
651
497
$scheme = $scheme->scheme;
653
500
Carp::croak("Illegal scheme '$scheme' passed to is_protocol_supported")
654
501
if $scheme =~ /\W/;
655
502
$scheme = lc $scheme;
1127
797
sub _new_response {
1128
798
my($request, $code, $message) = @_;
1129
799
my $response = HTTP::Response->new($code, $message);
1130
800
$response->request($request);
1131
801
$response->header("Client-Date" => HTTP::Date::time2str(time));
802
$response->header("Client-Warning" => "Internal response");
803
$response->header("Content-Type" => "text/plain");
804
$response->content("$code $message\n");
1132
805
return $response;
815
LWP::UserAgent - Web user agent class
819
require LWP::UserAgent;
821
my $ua = LWP::UserAgent->new;
825
my $response = $ua->get('http://search.cpan.org/');
827
if ($response->is_success) {
828
print $response->content; # or whatever
831
die $response->status_line;
836
The C<LWP::UserAgent> is a class implementing a web user agent.
837
C<LWP::UserAgent> objects can be used to dispatch web requests.
839
In normal use the application creates an C<LWP::UserAgent> object, and
840
then configures it with values for timeouts, proxies, name, etc. It
841
then creates an instance of C<HTTP::Request> for the request that
842
needs to be performed. This request is then passed to one of the
843
request method the UserAgent, which dispatches it using the relevant
844
protocol, and returns a C<HTTP::Response> object. There are
845
convenience methods for sending the most common request types: get(),
846
head() and post(). When using these methods then the creation of the
847
request object is hidden as shown in the synopsis above.
849
The basic approach of the library is to use HTTP style communication
850
for all protocol schemes. This means that you will construct
851
C<HTTP::Request> objects and receive C<HTTP::Response> objects even
852
for non-HTTP resources like I<gopher> and I<ftp>. In order to achieve
853
even more similarity to HTTP style communications, gopher menus and
854
file directories are converted to HTML documents.
856
=head1 CONSTRUCTOR METHODS
858
The following constructor methods are available:
862
=item $ua = LWP::UserAgent->new( %options )
864
This method constructs a new C<LWP::UserAgent> object and returns it.
865
Key/value pair arguments may be provided to set up the initial state.
866
The following options correspond to attribute methods described below:
869
----------- --------------------
870
agent "libwww-perl/#.##"
874
default_headers HTTP::Headers->new
878
protocols_allowed undef
879
protocols_forbidden undef
880
requests_redirectable ['GET', 'HEAD']
883
The following additional options are also accepted: If the
884
C<env_proxy> option is passed in with a TRUE value, then proxy
885
settings are read from environment variables (see env_proxy() method
886
below). If the C<keep_alive> option is passed in, then a
887
C<LWP::ConnCache> is set up (see conn_cache() method below). The
888
C<keep_alive> value is passed on as the C<total_capacity> for the
893
Returns a copy of the LWP::UserAgent object.
899
The settings of the configuration attributes modify the behaviour of the
900
C<LWP::UserAgent> when it dispatches requests. Most of these can also
901
be initialized by options passed to the constructor method.
903
The following attributes methods are provided. The attribute value is
904
left unchanged if no argument is given. The return value from each
905
method is the old attribute value.
911
=item $ua->agent( $product_id )
913
Get/set the product token that is used to identify the user agent on
914
the network. The agent value is sent as the "User-Agent" header in
915
the requests. The default is the string returned by the _agent()
918
If the $product_id ends with space then the _agent() string is
921
The user agent string should be one or more simple product identifiers
922
with an optional version number separated by the "/" character.
925
$ua->agent('Checkbot/0.4 ' . $ua->_agent);
926
$ua->agent('Checkbot/0.4 '); # same as above
927
$ua->agent('Mozilla/5.0');
928
$ua->agent(""); # don't identify
932
Returns the default agent identifier. This is a string of the form
933
"libwww-perl/#.##", where "#.##" is substituted with the version number
938
=item $ua->from( $email_address )
940
Get/set the e-mail address for the human user who controls
941
the requesting user agent. The address should be machine-usable, as
942
defined in RFC 822. The C<from> value is send as the "From" header in
943
the requests. Example:
945
$ua->from('gaas@cpan.org');
947
The default is to not send a "From" header. See the default_headers()
948
method for the more general interface that allow any header to be defaulted.
950
=item $ua->cookie_jar
952
=item $ua->cookie_jar( $cookie_jar_obj )
954
Get/set the cookie jar object to use. The only requirement is that
955
the cookie jar object must implement the extract_cookies($request) and
956
add_cookie_header($response) methods. These methods will then be
957
invoked by the user agent as requests are sent and responses are
958
received. Normally this will be a C<HTTP::Cookies> object or some
961
The default is to have no cookie_jar, i.e. never automatically add
962
"Cookie" headers to the requests.
964
Shortcut: If a reference to a plain hash is passed in as the
965
$cookie_jar_object, then it is replaced with an instance of
966
C<HTTP::Cookies> that is initialized based on the hash. This form also
967
automatically loads the C<HTTP::Cookies> module. It means that:
969
$ua->cookie_jar({ file => "$ENV{HOME}/.cookies.txt" });
971
is really just a shortcut for:
973
require HTTP::Cookies;
974
$ua->cookie_jar(HTTP::Cookies->new(file => "$ENV{HOME}/.cookies.txt"));
976
=item $ua->default_headers
978
=item $ua->default_headers( $headers_obj )
980
Get/set the headers object that will provide default header values for
981
any requests sent. By default this will be an empty C<HTTP::Headers>
984
$ua->default_headers->push_header('Accept-Language' => "no, en");
986
=item $ua->default_header( $field )
988
=item $ua->default_header( $field => $value )
990
This is just a short-cut for $ua->default_headers->header( $field =>
993
$ua->default_header('Accept-Language' => "no, en");
995
=item $ua->conn_cache
997
=item $ua->conn_cache( $cache_obj )
999
Get/set the C<LWP::ConnCache> object to use. See L<LWP::ConnCache>
1002
=item $ua->credentials( $netloc, $realm, $uname, $pass )
1004
Set the user name and password to be used for a realm. It is often more
1005
useful to specialize the get_basic_credentials() method instead.
1009
=item $ua->max_size( $bytes )
1011
Get/set the size limit for response content. The default is C<undef>,
1012
which means that there is no limit. If the returned response content
1013
is only partial, because the size limit was exceeded, then a
1014
"Client-Aborted" header will be added to the response. The content
1015
might end up longer than C<max_size> as we abort once appending a
1016
chunk of data makes the length exceed the limit. The "Content-Length"
1017
header, if present, will indicate the length of the full content and
1018
will normally not be the same as C<< length($res->content) >>.
1020
=item $ua->max_redirect
1022
=item $ua->max_redirect( $n )
1024
This reads or sets the object's limit of how many times it will obey
1025
redirection responses in a given request cycle.
1027
By default, the value is 7. This means that if you call request()
1028
method and the response is a redirect elsewhere which is in turn a
1029
redirect, and so on seven times, then LWP gives up after that seventh
1032
=item $ua->parse_head
1034
=item $ua->parse_head( $boolean )
1036
Get/set a value indicating whether we should initialize response
1037
headers from the E<lt>head> section of HTML documents. The default is
1038
TRUE. Do not turn this off, unless you know what you are doing.
1040
=item $ua->protocols_allowed
1042
=item $ua->protocols_allowed( \@protocols )
1044
This reads (or sets) this user agent's list of protocols that the
1045
request methods will exclusively allow. The protocol names are case
1048
For example: C<$ua-E<gt>protocols_allowed( [ 'http', 'https'] );>
1049
means that this user agent will I<allow only> those protocols,
1050
and attempts to use this user agent to access URLs with any other
1051
schemes (like "ftp://...") will result in a 500 error.
1053
To delete the list, call: C<$ua-E<gt>protocols_allowed(undef)>
1055
By default, an object has neither a C<protocols_allowed> list, nor a
1056
C<protocols_forbidden> list.
1058
Note that having a C<protocols_allowed> list causes any
1059
C<protocols_forbidden> list to be ignored.
1061
=item $ua->protocols_forbidden
1063
=item $ua->protocols_forbidden( \@protocols )
1065
This reads (or sets) this user agent's list of protocols that the
1066
request method will I<not> allow. The protocol names are case
1069
For example: C<$ua-E<gt>protocols_forbidden( [ 'file', 'mailto'] );>
1070
means that this user agent will I<not> allow those protocols, and
1071
attempts to use this user agent to access URLs with those schemes
1072
will result in a 500 error.
1074
To delete the list, call: C<$ua-E<gt>protocols_forbidden(undef)>
1076
=item $ua->requests_redirectable
1078
=item $ua->requests_redirectable( \@requests )
1080
This reads or sets the object's list of request names that
1081
C<$ua-E<gt>redirect_ok(...)> will allow redirection for. By
1082
default, this is C<['GET', 'HEAD']>, as per RFC 2616. To
1083
change to include 'POST', consider:
1085
push @{ $ua->requests_redirectable }, 'POST';
1089
=item $ua->timeout( $secs )
1091
Get/set the timeout value in seconds. The default timeout() value is
1092
180 seconds, i.e. 3 minutes.
1094
The requests is aborted if no activity on the connection to the server
1095
is observed for C<timeout> seconds. This means that the time it takes
1096
for the complete transaction and the request() method to actually
1097
return might be longer.
1101
=head2 Proxy attributes
1103
The following methods set up when requests should be passed via a
1108
=item $ua->proxy(\@schemes, $proxy_url)
1110
=item $ua->proxy($scheme, $proxy_url)
1112
Set/retrieve proxy URL for a scheme:
1114
$ua->proxy(['http', 'ftp'], 'http://proxy.sn.no:8001/');
1115
$ua->proxy('gopher', 'http://proxy.sn.no:8001/');
1117
The first form specifies that the URL is to be used for proxying of
1118
access methods listed in the list in the first method argument,
1119
i.e. 'http' and 'ftp'.
1121
The second form shows a shorthand form for specifying
1122
proxy URL for a single access scheme.
1124
=item $ua->no_proxy( $domain, ... )
1126
Do not proxy requests to the given domains. Calling no_proxy without
1127
any domains clears the list of domains. Eg:
1129
$ua->no_proxy('localhost', 'no', ...);
1131
=item $ua->env_proxy
1133
Load proxy settings from *_proxy environment variables. You might
1134
specify proxies like this (sh-syntax):
1136
gopher_proxy=http://proxy.my.place/
1137
wais_proxy=http://proxy.my.place/
1138
no_proxy="localhost,my.domain"
1139
export gopher_proxy wais_proxy no_proxy
1141
csh or tcsh users should use the C<setenv> command to define these
1142
environment variables.
1144
On systems with case insensitive environment variables there exists a
1145
name clash between the CGI environment variables and the C<HTTP_PROXY>
1146
environment variable normally picked up by env_proxy(). Because of
1147
this C<HTTP_PROXY> is not honored for CGI scripts. The
1148
C<CGI_HTTP_PROXY> environment variable can be used instead.
1152
=head1 REQUEST METHODS
1154
The methods described in this section are used to dispatch requests
1155
via the user agent. The following request methods are provided:
1159
=item $ua->get( $url )
1161
=item $ua->get( $url , $field_name => $value, ... )
1163
This method will dispatch a C<GET> request on the given $url. Further
1164
arguments can be given to initialize the headers of the request. These
1165
are given as separate name/value pairs. The return value is a
1166
response object. See L<HTTP::Response> for a description of the
1167
interface it provides.
1169
Fields names that start with ":" are special. These will not
1170
initialize headers of the request but will determine how the response
1171
content is treated. The following special field names are recognized:
1173
:content_file => $filename
1174
:content_cb => \&callback
1175
:read_size_hint => $bytes
1177
If a $filename is provided with the C<:content_file> option, then the
1178
response content will be saved here instead of in the response
1179
object. If a callback is provided with the C<:content_cb> option then
1180
this function will be called for each chunk of the response content as
1181
it is received from the server. If neither of these options are
1182
given, then the response content will accumulate in the response
1183
object itself. This might not be suitable for very large response
1184
bodies. Only one of C<:content_file> or C<:content_cb> can be
1185
specified. The content of unsuccessful responses will always
1186
accumulate in the response object itself, regardless of the
1187
C<:content_file> or C<:content_cb> options passed in.
1189
The C<:read_size_hint> option is passed to the protocol module which
1190
will try to read data from the server in chunks of this size. A
1191
smaller value for the C<:read_size_hint> will result in a higher
1192
number of callback invocations.
1194
The callback function is called with 3 arguments: a chunk of data, a
1195
reference to the response object, and a reference to the protocol
1196
object. The callback can abort the request by invoking die(). The
1197
exception message will show up as the "X-Died" header field in the
1198
response returned by the get() function.
1200
=item $ua->head( $url )
1202
=item $ua->head( $url , $field_name => $value, ... )
1204
This method will dispatch a C<HEAD> request on the given $url.
1205
Otherwise it works like the get() method described above.
1207
=item $ua->post( $url, \%form )
1209
=item $ua->post( $url, \@form )
1211
=item $ua->post( $url, \%form, $field_name => $value, ... )
1213
This method will dispatch a C<POST> request on the given $url, with
1214
%form or @form providing the key/value pairs for the fill-in form
1215
content. Additional headers and content options are the same as for
1218
This method will use the POST() function from C<HTTP::Request::Common>
1219
to build the request. See L<HTTP::Request::Common> for a details on
1220
how to pass form content and other advanced features.
1222
=item $ua->mirror( $url, $filename )
1224
This method will get the document identified by $url and store it in
1225
file called $filename. If the file already exists, then the request
1226
will contain an "If-Modified-Since" header matching the modification
1227
time of the file. If the document on the server has not changed since
1228
this time, then nothing happens. If the document has been updated, it
1229
will be downloaded again. The modification time of the file will be
1230
forced to match that of the server.
1232
The return value is the the response object.
1234
=item $ua->request( $request )
1236
=item $ua->request( $request, $content_file )
1238
=item $ua->request( $request, $content_cb )
1240
=item $ua->request( $request, $content_cb, $read_size_hint )
1242
This method will dispatch the given $request object. Normally this
1243
will be an instance of the C<HTTP::Request> class, but any object with
1244
a similar interface will do. The return value is a response object.
1245
See L<HTTP::Request> and L<HTTP::Response> for a description of the
1246
interface provided by these classes.
1248
The request() method will process redirects and authentication
1249
responses transparently. This means that it may actually send several
1250
simple requests via the simple_request() method described below.
1252
The request methods described above; get(), head(), post() and
1253
mirror(), will all dispatch the request they build via this method.
1254
They are convenience methods that simply hides the creation of the
1255
request object for you.
1257
The $content_file, $content_cb and $read_size_hint all correspond to
1258
options described with the get() method above.
1260
You are allowed to use a CODE reference as C<content> in the request
1261
object passed in. The C<content> function should return the content
1262
when called. The content can be returned in chunks. The content
1263
function will be invoked repeatedly until it return an empty string to
1264
signal that there is no more content.
1266
=item $ua->simple_request( $request )
1268
=item $ua->simple_request( $request, $content_file )
1270
=item $ua->simple_request( $request, $content_cb )
1272
=item $ua->simple_request( $request, $content_cb, $read_size_hint )
1274
This method dispatches a single request and returns the response
1275
received. Arguments are the same as for request() described above.
1277
The difference from request() is that simple_request() will not try to
1278
handle redirects or authentication responses. The request() method
1279
will in fact invoke this method for each simple request it sends.
1281
=item $ua->is_protocol_supported( $scheme )
1283
You can use this method to test whether this user agent object supports the
1284
specified C<scheme>. (The C<scheme> might be a string (like 'http' or
1285
'ftp') or it might be an URI object reference.)
1287
Whether a scheme is supported, is determined by the user agent's
1288
C<protocols_allowed> or C<protocols_forbidden> lists (if any), and by
1289
the capabilities of LWP. I.e., this will return TRUE only if LWP
1290
supports this protocol I<and> it's permitted for this particular
1295
=head2 Callback methods
1297
The following methods will be invoked as requests are processed. These
1298
methods are documented here because subclasses of C<LWP::UserAgent>
1299
might want to override their behaviour.
1303
=item $ua->prepare_request( $request )
1305
This method is invoked by simple_request(). Its task is to modify the
1306
given $request object by setting up various headers based on the
1307
attributes of the user agent. The return value should normally be the
1308
$request object passed in. If a different request object is returned
1309
it will be the one actually processed.
1311
The headers affected by the base implementation are; "User-Agent",
1312
"From", "Range" and "Cookie".
1314
=item $ua->redirect_ok( $prospective_request, $response )
1316
This method is called by request() before it tries to follow a
1317
redirection to the request in $response. This should return a TRUE
1318
value if this redirection is permissible. The $prospective_request
1319
will be the request to be sent if this method returns TRUE.
1321
The base implementation will return FALSE unless the method
1322
is in the object's C<requests_redirectable> list,
1323
FALSE if the proposed redirection is to a "file://..."
1324
URL, and TRUE otherwise.
1326
=item $ua->get_basic_credentials( $realm, $uri, $isproxy )
1328
This is called by request() to retrieve credentials for documents
1329
protected by Basic or Digest Authentication. The arguments passed in
1330
is the $realm provided by the server, the $uri requested and a boolean
1331
flag to indicate if this is authentication against a proxy server.
1333
The method should return a username and password. It should return an
1334
empty list to abort the authentication resolution attempt. Subclasses
1335
can override this method to prompt the user for the information. An
1336
example of this can be found in C<lwp-request> program distributed
1339
The base implementation simply checks a set of pre-stored member
1340
variables, set up with the credentials() method.
1139
1344
=head1 SEE ALSO
1141
See L<LWP> for a complete overview of libwww-perl5. See F<lwp-request> and
1142
F<lwp-mirror> for examples of usage.
1346
See L<LWP> for a complete overview of libwww-perl5. See L<lwpcook>
1347
and the scripts F<lwp-request> and F<lwp-download> for examples of
1350
See L<HTTP::Request> and L<HTTP::Response> for a description of the
1351
message objects dispatched and received. See L<HTTP::Request::Common>
1352
and L<HTML::Form> for other ways to build request objects.
1354
See L<WWW::Mechanize> and L<WWW::Search> for examples of more
1355
specialized user agents based on C<LWP::UserAgent>.
1144
1357
=head1 COPYRIGHT
1146
Copyright 1995-2001 Gisle Aas.
1359
Copyright 1995-2004 Gisle Aas.
1148
1361
This library is free software; you can redistribute it and/or
1149
1362
modify it under the same terms as Perl itself.