Further to the previous, here is a possible patch to implement timeout behaviour in LWP::Simple. There is no way to set $timeout passed to _trivial_http_get() included. A global would be easiest I guess given the very simple interface. Defaults to 180 seconds.
$ diff -u Perl/site/lib/LWP/Simple.pm Perl/site/lib/LWP/Simple-patch.p
+m
--- Perl/site/lib/LWP/Simple.pm Tue Jun 22 12:40:14 2004
+++ Perl/site/lib/LWP/Simple-patch.pm Tue Jun 22 12:42:15 2004
@@ -154,9 +154,10 @@
sub _trivial_http_get
{
- my($host, $port, $path) = @_;
+ my($host, $port, $path, $timeout) = @_;
#print "HOST=$host, PORT=$port, PATH=$path\n";
-
+ $timeout ||=180;
+ my $hard_timeout = time() + $timeout;
require IO::Socket;
local($^W) = 0;
my $sock = IO::Socket::INET->new(PeerAddr => $host,
@@ -174,8 +175,15 @@
my $buf = "";
my $n;
- 1 while $n = sysread($sock, $buf, 8*1024, length($buf));
- return undef unless defined($n);
+ while( 1 ) {
+ my $remaining = $hard_timeout - time();
+ return undef if $remaining <= 0;
+ if ( can_read( $sock, $remaining ) ) {
+ $n = sysread($sock, $buf, 8*1024, length($buf));
+ return undef unless defined($n);
+ last if $n == 0; # we are eof
+ }
+ }
if ($buf =~ m,^HTTP/\d+\.\d+\s+(\d+)[^\012]*\012,) {
my $code = $1;
@@ -191,6 +199,15 @@
}
return $buf;
+}
+
+sub can_read {
+ my($sock, $timeout) = @_;
+ my $fbits = '';
+ vec($fbits, fileno($sock), 1) = 1;
+ my $nfound = select($fbits, undef, undef, $timeout);
+ die "select failed: $!" unless defined $nfound;
+ return $nfound > 0;
}
Administrator@JAMES /cygdrive/d
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.