Wieso funktioniert LWP nicht mit Strato?
Gelesen bei AboutWebDesign.de
URL: http://www.aboutwebdesign.de/awd/content/1008173059.shtml
Leider unterstützt Strato das praktische LWP-Perl-Modul nicht. Wie kann man das umgehen?
Folgendes Beispiel zeigt eine Funktion, die das Problem umgeht - der Code ist sicherlich nicht optimal, funktioniert aber.
sub getURL
{
my $url = shift;
if ($url =~ m,^http://([^/:]+)(?::(\d+))?(/\S*)?$,) {
my $host = $1;
my $port = $2 || 80;
my $path = $3;
$path = "/" unless defined($path);
require IO::Socket;
local($^W) = 0;
my $sock = IO::Socket::INET->new(PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp',
Timeout => 60) || return;
$sock->autoflush;
my $netloc = $host;
$netloc .= ":$port" if $port != 80;
print $sock join("\015\012" =>
"GET $path HTTP/1.0",
"Host: $netloc",
"User-Agent: lwp-trivial/$VERSION",
"", "");
my $buf = "";
my $n;
1 while $n = sysread($sock, $buf, 8*1024, length($buf));
return undef unless defined($n);
if ($buf =~ m,^HTTP/\d+\.\d+\s+(\d+)[^\012]*\012,) {
my $code = $1;
if ($code =~ /^30[1237]/ && $buf =~ /\012Location:\s*(\S+)/) {
my $url = $1;
return undef if $loop_check{$url}++;
return _get($url);
}
return undef unless $code =~ /^2/;
$buf =~ s/.+?\015?\012\015?\012//s;
}
return $buf;
}
}
Aufgerufen wird die Subroutine mit &getURL($url). Der Code ist äußerst stark an entsprechende Funktionalitäten im LWP-Modul selbst angelehnt.