272 |
|
|
273 |
$draft .= "\n"; |
$draft .= "\n"; |
274 |
|
|
275 |
$draft .= join("\n", @{ $self->{dtexts} }) . "\n"; |
$draft .= join("\n", @{ $self->{dtexts} }) . "\n" if ($self->{dtexts}); |
276 |
$draft .= "\t" . join("\n\t", @{ $self->{htexts} }) . "\n"; |
$draft .= "\t" . join("\n\t", @{ $self->{htexts} }) . "\n" if ($self->{htexts}); |
277 |
|
|
278 |
return $draft; |
return $draft; |
279 |
} |
} |
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; |
689 |
}; |
}; |
690 |
bless($self, $class); |
bless($self, $class); |
691 |
|
|
692 |
|
if (@_) { |
693 |
|
$self->{debug} = 1; |
694 |
|
warn "## Node debug on\n"; |
695 |
|
} |
696 |
|
|
697 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
698 |
} |
} |
699 |
|
|
752 |
sub set_auth { |
sub set_auth { |
753 |
my $self = shift; |
my $self = shift; |
754 |
my ($login,$passwd) = @_; |
my ($login,$passwd) = @_; |
755 |
$self->{auth} = encode_base64( "$login:$passwd" ); |
my $basic_auth = encode_base64( "$login:$passwd" ); |
756 |
|
chomp($basic_auth); |
757 |
|
$self->{auth} = $basic_auth; |
758 |
} |
} |
759 |
|
|
760 |
=head2 status |
=head2 status |
761 |
|
|
762 |
Return status code of last request. |
Return status code of last request. |
763 |
|
|
764 |
print $res->status; |
print $node->status; |
765 |
|
|
766 |
C<-1> means connection failure. |
C<-1> means connection failure. |
767 |
|
|
772 |
return $self->{status}; |
return $self->{status}; |
773 |
} |
} |
774 |
|
|
775 |
|
=head2 put_doc |
776 |
|
|
777 |
|
$node->put_doc( $document_draft ); |
778 |
|
|
779 |
|
=cut |
780 |
|
|
781 |
|
sub put_doc { |
782 |
|
my $self = shift; |
783 |
|
my $doc = shift || return; |
784 |
|
$self->shuttle_url( $self->{url} . '/put_doc', 'text/x-estraier-draft', $doc->dump_draft, undef); |
785 |
|
} |
786 |
|
|
787 |
=head2 shuttle_url |
=head2 shuttle_url |
788 |
|
|
789 |
This is method which uses C<IO::Socket::INET> to communicate with Hyper Estraier node |
This is method which uses C<IO::Socket::INET> to communicate with Hyper Estraier node |
801 |
|
|
802 |
my ($url, $content_type, $reqbody, $resbody) = @_; |
my ($url, $content_type, $reqbody, $resbody) = @_; |
803 |
|
|
804 |
my $status = -1; |
$self->{status} = -1; |
805 |
|
|
806 |
warn $url; |
warn "## $url\n"; |
807 |
|
|
808 |
$url = new URI($url); |
$url = new URI($url); |
809 |
return -1 unless ($url && $url->scheme && $url->scheme eq 'http' && $url->host && $url->port > 1); |
if ( |
810 |
|
!$url || !$url->scheme || !$url->scheme eq 'http' || |
811 |
|
!$url->host || !$url->port || $url->port < 1 |
812 |
|
) { |
813 |
|
carp "can't parse $url\n"; |
814 |
|
return -1; |
815 |
|
} |
816 |
|
|
817 |
my ($host,$port,$query) = ($url->host, $url->port, $url->path); |
my ($host,$port,$query) = ($url->host, $url->port, $url->path); |
818 |
|
|
821 |
$query = "http://$host:$port/$query"; |
$query = "http://$host:$port/$query"; |
822 |
} |
} |
823 |
|
|
824 |
$query .= '?' + $url->query if ($url->query && ! $reqbody); |
$query .= '?' . $url->query if ($url->query && ! $reqbody); |
825 |
|
|
826 |
my $sock = IO::Socket::INET->new( |
my $headers; |
|
PeerAddr => $host, |
|
|
PeerPort => $port, |
|
|
Proto => 'tcp', |
|
|
Timeout => $self->{timeout} || 90, |
|
|
) || return -1; |
|
827 |
|
|
828 |
if ($reqbody) { |
if ($reqbody) { |
829 |
print $sock "POST $query HTTP/1.0\r\n"; |
$headers .= "POST $query HTTP/1.0\r\n"; |
830 |
} else { |
} else { |
831 |
print $sock "GET $query HTTP/1.0\r\n"; |
$headers .= "GET $query HTTP/1.0\r\n"; |
832 |
} |
} |
833 |
|
|
834 |
print $sock "Host: $url->host:$url->port\r\n"; |
$headers .= "Host: " . $url->host . ":" . $url->port . "\r\n"; |
835 |
print $sock "Connection: close\r\n"; |
$headers .= "Connection: close\r\n"; |
836 |
print $sock "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n"; |
$headers .= "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n"; |
837 |
print $sock "Content-Type $content_type\r\n"; |
$headers .= "Content-Type: $content_type\r\n"; |
838 |
print $sock "Authorization: Basic $self->{auth}\r\n"; |
$headers .= "Authorization: Basic $self->{auth}\r\n"; |
839 |
|
my $len = 0; |
840 |
{ |
{ |
841 |
use bytes; |
use bytes; |
842 |
print $sock "Content-Length: ", length($reqbody), "\r\n"; |
$len = length($reqbody) if ($reqbody); |
843 |
} |
} |
844 |
print $sock "\r\n"; |
$headers .= "Content-Length: $len\r\n"; |
845 |
|
$headers .= "\r\n"; |
846 |
|
|
847 |
print $sock $$reqbody if ($reqbody); |
my $sock = IO::Socket::INET->new( |
848 |
|
PeerAddr => $host, |
849 |
|
PeerPort => $port, |
850 |
|
Proto => 'tcp', |
851 |
|
Timeout => $self->{timeout} || 90, |
852 |
|
); |
853 |
|
|
854 |
|
if (! $sock) { |
855 |
|
carp "can't open socket to $host:$port"; |
856 |
|
return -1; |
857 |
|
} |
858 |
|
|
859 |
|
warn $headers if ($self->{debug}); |
860 |
|
|
861 |
|
print $sock $headers or |
862 |
|
carp "can't send headers to network:\n$headers\n" and return -1; |
863 |
|
|
864 |
|
if ($reqbody) { |
865 |
|
warn $reqbody if ($self->{debug}); |
866 |
|
print $sock $reqbody or |
867 |
|
carp "can't send request body to network:\n$$reqbody\n" and return -1; |
868 |
|
} |
869 |
|
|
870 |
my $line = <$sock>; |
my $line = <$sock>; |
871 |
chomp($line); |
chomp($line); |
873 |
return if ($schema !~ /^HTTP/ || ! $res_status); |
return if ($schema !~ /^HTTP/ || ! $res_status); |
874 |
|
|
875 |
$self->{status} = $res_status; |
$self->{status} = $res_status; |
876 |
|
warn "## response status: $res_status\n" if ($self->{debug}); |
877 |
|
|
878 |
# skip rest of headers |
# skip rest of headers |
879 |
do { |
$line = <$sock>; |
880 |
|
while ($line) { |
881 |
$line = <$sock>; |
$line = <$sock>; |
882 |
chomp($line); |
$line =~ s/[\r\n]+$//; |
883 |
} until ($line eq ''); |
warn "## ", $line || 'NULL', " ##\n" if ($self->{debug}); |
884 |
|
}; |
885 |
|
|
886 |
# read body |
# read body |
887 |
my $len = 0; |
$len = 0; |
888 |
do { |
do { |
889 |
$len = read($sock, my $buf, 8192); |
$len = read($sock, my $buf, 8192); |
890 |
$$resbody .= $buf if ($resbody); |
$$resbody .= $buf if ($resbody); |
891 |
} while ($len); |
} while ($len); |
892 |
|
|
893 |
return $status; |
warn "## response body:\n$$resbody\n" if ($resbody && $self->{debug}); |
894 |
|
|
895 |
|
return $self->{status}; |
896 |
} |
} |
897 |
|
|
898 |
### |
### |