--- trunk/Estraier.pm 2006/01/05 21:51:54 36 +++ trunk/Estraier.pm 2006/01/05 22:16:21 37 @@ -661,7 +661,7 @@ package Search::Estraier::Node; -use Carp qw/croak/; +use Carp qw/carp croak/; use URI; use MIME::Base64; use IO::Socket::INET; @@ -787,7 +787,13 @@ warn $url; $url = new URI($url); - return -1 unless ($url && $url->scheme && $url->scheme eq 'http' && $url->host && $url->port > 1); + if ( + !$url || !$url->scheme || !$url->scheme eq 'http' || + !$url->host || !$url->port || $url->port < 1 + ) { + carp "can't parse $url\n"; + return -1; + } my ($host,$port,$query) = ($url->host, $url->port, $url->path); @@ -796,33 +802,48 @@ $query = "http://$host:$port/$query"; } - $query .= '?' + $url->query if ($url->query && ! $reqbody); + $query .= '?' . $url->query if ($url->query && ! $reqbody); - my $sock = IO::Socket::INET->new( - PeerAddr => $host, - PeerPort => $port, - Proto => 'tcp', - Timeout => $self->{timeout} || 90, - ) || return -1; + my $headers; if ($reqbody) { - print $sock "POST $query HTTP/1.0\r\n"; + $headers .= "POST $query HTTP/1.0\r\n"; } else { - print $sock "GET $query HTTP/1.0\r\n"; + $headers .= "GET $query HTTP/1.0\r\n"; } - print $sock "Host: $url->host:$url->port\r\n"; - print $sock "Connection: close\r\n"; - print $sock "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n"; - print $sock "Content-Type $content_type\r\n"; - print $sock "Authorization: Basic $self->{auth}\r\n"; + $headers .= "Host: $url->host:$url->port\r\n"; + $headers .= "Connection: close\r\n"; + $headers .= "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n"; + $headers .= "Content-Type $content_type\r\n"; + $headers .= "Authorization: Basic $self->{auth}\r\n"; + my $len = 0; { use bytes; - print $sock "Content-Length: ", length($reqbody), "\r\n"; + $len = length($reqbody) if ($reqbody); } - print $sock "\r\n"; + $headers .= "Content-Length: $len\r\n"; + $headers .= "\r\n"; - print $sock $$reqbody if ($reqbody); + my $sock = IO::Socket::INET->new( + PeerAddr => $host, + PeerPort => $port, + Proto => 'tcp', + Timeout => $self->{timeout} || 90, + ); + + if (! $sock) { + carp "can't open socket to $host:$port"; + return -1; + } + + print $sock $headers or + carp "can't send headers to network:\n$headers\n" and return -1; + + if ($reqbody) { + print $sock $$reqbody or + carp "can't send request body to network:\n$$reqbody\n" and return -1; + } my $line = <$sock>; chomp($line);