4 use vars qw($ua %loop_check $FULL_LWP @EXPORT @EXPORT_OK $VERSION);
8 @EXPORT = qw(get head getprint getstore mirror);
11 # I really hate this. I was a bad idea to do it in the first place.
12 # Wonder how to get rid of it??? (It even makes LWP::Simple 7% slower
15 push(@EXPORT, @HTTP::Status::EXPORT);
18 $FULL_LWP++ if grep {lc($_) eq "http_proxy"} keys %ENV;
25 if (grep $_ eq '$ua', @_) {
29 Exporter::export($pkg, $callpkg, @_);
36 require LWP::UserAgent;
39 $ua = new LWP::UserAgent; # we create a global UserAgent object
40 my $ver = $LWP::VERSION = $LWP::VERSION; # avoid warning
41 $ua->agent("LWP::Simple/$LWP::VERSION");
56 _init_ua() unless $ua;
58 my $request = HTTP::Request->new(GET => $url);
59 my $response = $ua->request($request);
61 return $response->content if $response->is_success;
69 _init_ua() unless $ua;
71 my $request = HTTP::Request->new(HEAD => $url);
72 my $response = $ua->request($request);
74 if ($response->is_success) {
75 return $response unless wantarray;
76 return (scalar $response->header('Content-Type'),
77 scalar $response->header('Content-Length'),
78 HTTP::Date::str2time($response->header('Last-Modified')),
79 HTTP::Date::str2time($response->header('Expires')),
80 scalar $response->header('Server'),
90 _init_ua() unless $ua;
92 my $request = HTTP::Request->new(GET => $url);
93 local($\) = ""; # ensure standard $OUTPUT_RECORD_SEPARATOR
94 my $callback = sub { print $_[0] };
96 $callback = sub { $_[0] =~ s/\015?\012/\n/g; print $_[0] }
98 my $response = $ua->request($request, $callback);
99 unless ($response->is_success) {
100 print STDERR $response->status_line, " <URL:$url>\n";
108 my($url, $file) = @_;
109 _init_ua() unless $ua;
111 my $request = HTTP::Request->new(GET => $url);
112 my $response = $ua->request($request, $file);
120 my($url, $file) = @_;
121 _init_ua() unless $ua;
122 my $response = $ua->mirror($url, $file);
131 if (!$FULL_LWP && $url =~ m,^http://([^/:\@]+)(?::(\d+))?(/\S*)?$,) {
135 $path = "/" unless defined($path);
136 return _trivial_http_get($host, $port, $path);
139 _init_ua() unless $ua;
140 if (@_ && $url !~ /^\w+:/) {
141 # non-absolute redirect from &_trivial_http_get
142 my($host, $port, $path) = @_;
144 $url = URI->new_abs($url, "http://$host:$port$path");
146 my $request = HTTP::Request->new(GET => $url);
147 my $response = $ua->request($request);
148 return $response->is_success ? $response->content : undef;
153 sub _trivial_http_get
155 my($host, $port, $path) = @_;
156 #print "HOST=$host, PORT=$port, PATH=$path\n";
160 my $sock = IO::Socket::INET->new(PeerAddr => $host,
163 Timeout => 60) || return undef;
166 $netloc .= ":$port" if $port != 80;
167 print $sock join("\015\012" =>
168 "GET $path HTTP/1.0",
170 "User-Agent: lwp-trivial/$VERSION",
175 1 while $n = sysread($sock, $buf, 8*1024, length($buf));
176 return undef unless defined($n);
178 if ($buf =~ m,^HTTP/\d+\.\d+\s+(\d+)[^\012]*\012,) {
180 #print "CODE=$code\n$buf\n";
181 if ($code =~ /^30[1237]/ && $buf =~ /\012Location:\s*(\S+)/i) {
184 return undef if $loop_check{$url}++;
185 return _get($url, $host, $port, $path);
187 return undef unless $code =~ /^2/;
188 $buf =~ s/.+?\015?\012\015?\012//s; # zap header
201 LWP::Simple - simple procedural interface to LWP
205 perl -MLWP::Simple -e 'getprint "http://www.sn.no"'
208 $content = get("http://www.sn.no/");
209 die "Couldn't get it!" unless defined $content;
211 if (mirror("http://www.sn.no/", "foo") == RC_NOT_MODIFIED) {
215 if (is_success(getprint("http://www.sn.no/"))) {
221 This module is meant for people who want a simplified view of the
222 libwww-perl library. It should also be suitable for one-liners. If
223 you need more control or access to the header fields in the requests
224 sent and responses received, then you should use the full object-oriented
225 interface provided by the C<LWP::UserAgent> module.
227 The following functions are provided (and exported) by this module:
233 The get() function will fetch the document identified by the given URL
234 and return it. It returns C<undef> if it fails. The $url argument can
235 be either a simple string or a reference to a URI object.
237 You will not be able to examine the response code or response headers
238 (like 'Content-Type') when you are accessing the web using this
239 function. If you need that information you should use the full OO
240 interface (see L<LWP::UserAgent>).
244 Get document headers. Returns the following 5 values if successful:
245 ($content_type, $document_length, $modified_time, $expires, $server)
247 Returns an empty list if it fails. In scalar context returns TRUE if
252 Get and print a document identified by a URL. The document is printed
253 to the selected default filehandle for output (normally STDOUT) as
254 data is received from the network. If the request fails, then the
255 status code and message are printed on STDERR. The return value is
256 the HTTP response code.
258 =item getstore($url, $file)
260 Gets a document identified by a URL and stores it in the file. The
261 return value is the HTTP response code.
263 =item mirror($url, $file)
265 Get and store a document identified by a URL, using
266 I<If-modified-since>, and checking the I<Content-Length>. Returns
267 the HTTP response code.
271 This module also exports the HTTP::Status constants and procedures.
272 You can use them when you check the response code from getprint(),
273 getstore() or mirror(). The constants are:
276 RC_SWITCHING_PROTOCOLS
280 RC_NON_AUTHORITATIVE_INFORMATION
295 RC_METHOD_NOT_ALLOWED
297 RC_PROXY_AUTHENTICATION_REQUIRED
302 RC_PRECONDITION_FAILED
303 RC_REQUEST_ENTITY_TOO_LARGE
304 RC_REQUEST_URI_TOO_LARGE
305 RC_UNSUPPORTED_MEDIA_TYPE
306 RC_INTERNAL_SERVER_ERROR
309 RC_SERVICE_UNAVAILABLE
311 RC_HTTP_VERSION_NOT_SUPPORTED
313 The HTTP::Status classification functions are:
317 =item is_success($rc)
319 True if response code indicated a successful request.
323 True if response code indicated that an error occurred.
327 The module will also export the LWP::UserAgent object as C<$ua> if you
328 ask for it explicitly.
330 The user agent created by this module will identify itself as
331 "LWP::Simple/#.##" (where "#.##" is the libwww-perl version number)
332 and will initialize its proxy defaults from the environment (by
333 calling $ua->env_proxy).
337 Note that if you are using both LWP::Simple and the very popular CGI.pm
338 module, you may be importing a C<head> function from each module,
339 producing a warning like "Prototype mismatch: sub main::head ($) vs
340 none". Get around this problem by just not importing LWP::Simple's
341 C<head> function, like so:
343 use LWP::Simple qw(!head);
344 use CGI qw(:standard); # then only CGI.pm defines a head()
346 Then if you do need LWP::Simple's C<head> function, you can just call
347 it as C<LWP::Simple::head($url)>.
351 L<LWP>, L<lwpcook>, L<LWP::UserAgent>, L<HTTP::Status>, L<lwp-request>,