661 |
|
|
662 |
package Search::Estraier::Node; |
package Search::Estraier::Node; |
663 |
|
|
664 |
use Carp qw/croak/; |
use Carp qw/carp croak/; |
665 |
use URI; |
use URI; |
666 |
use MIME::Base64; |
use MIME::Base64; |
667 |
use IO::Socket::INET; |
use IO::Socket::INET; |
787 |
warn $url; |
warn $url; |
788 |
|
|
789 |
$url = new URI($url); |
$url = new URI($url); |
790 |
return -1 unless ($url && $url->scheme && $url->scheme eq 'http' && $url->host && $url->port > 1); |
if ( |
791 |
|
!$url || !$url->scheme || !$url->scheme eq 'http' || |
792 |
|
!$url->host || !$url->port || $url->port < 1 |
793 |
|
) { |
794 |
|
carp "can't parse $url\n"; |
795 |
|
return -1; |
796 |
|
} |
797 |
|
|
798 |
my ($host,$port,$query) = ($url->host, $url->port, $url->path); |
my ($host,$port,$query) = ($url->host, $url->port, $url->path); |
799 |
|
|
802 |
$query = "http://$host:$port/$query"; |
$query = "http://$host:$port/$query"; |
803 |
} |
} |
804 |
|
|
805 |
$query .= '?' + $url->query if ($url->query && ! $reqbody); |
$query .= '?' . $url->query if ($url->query && ! $reqbody); |
806 |
|
|
807 |
my $sock = IO::Socket::INET->new( |
my $headers; |
|
PeerAddr => $host, |
|
|
PeerPort => $port, |
|
|
Proto => 'tcp', |
|
|
Timeout => $self->{timeout} || 90, |
|
|
) || return -1; |
|
808 |
|
|
809 |
if ($reqbody) { |
if ($reqbody) { |
810 |
print $sock "POST $query HTTP/1.0\r\n"; |
$headers .= "POST $query HTTP/1.0\r\n"; |
811 |
} else { |
} else { |
812 |
print $sock "GET $query HTTP/1.0\r\n"; |
$headers .= "GET $query HTTP/1.0\r\n"; |
813 |
} |
} |
814 |
|
|
815 |
print $sock "Host: $url->host:$url->port\r\n"; |
$headers .= "Host: $url->host:$url->port\r\n"; |
816 |
print $sock "Connection: close\r\n"; |
$headers .= "Connection: close\r\n"; |
817 |
print $sock "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n"; |
$headers .= "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n"; |
818 |
print $sock "Content-Type $content_type\r\n"; |
$headers .= "Content-Type $content_type\r\n"; |
819 |
print $sock "Authorization: Basic $self->{auth}\r\n"; |
$headers .= "Authorization: Basic $self->{auth}\r\n"; |
820 |
|
my $len = 0; |
821 |
{ |
{ |
822 |
use bytes; |
use bytes; |
823 |
print $sock "Content-Length: ", length($reqbody), "\r\n"; |
$len = length($reqbody) if ($reqbody); |
824 |
} |
} |
825 |
print $sock "\r\n"; |
$headers .= "Content-Length: $len\r\n"; |
826 |
|
$headers .= "\r\n"; |
827 |
|
|
828 |
print $sock $$reqbody if ($reqbody); |
my $sock = IO::Socket::INET->new( |
829 |
|
PeerAddr => $host, |
830 |
|
PeerPort => $port, |
831 |
|
Proto => 'tcp', |
832 |
|
Timeout => $self->{timeout} || 90, |
833 |
|
); |
834 |
|
|
835 |
|
if (! $sock) { |
836 |
|
carp "can't open socket to $host:$port"; |
837 |
|
return -1; |
838 |
|
} |
839 |
|
|
840 |
|
print $sock $headers or |
841 |
|
carp "can't send headers to network:\n$headers\n" and return -1; |
842 |
|
|
843 |
|
if ($reqbody) { |
844 |
|
print $sock $$reqbody or |
845 |
|
carp "can't send request body to network:\n$$reqbody\n" and return -1; |
846 |
|
} |
847 |
|
|
848 |
my $line = <$sock>; |
my $line = <$sock>; |
849 |
chomp($line); |
chomp($line); |