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 |
} |
} |
580 |
} |
} |
581 |
|
|
582 |
|
|
583 |
package Search::Estraier::Master; |
package Search::Estraier::NodeResult; |
584 |
|
|
585 |
use Carp; |
use Carp qw/croak/; |
586 |
|
|
587 |
|
#use Search::Estraier; |
588 |
|
#our @ISA = qw/Search::Estraier/; |
589 |
|
|
590 |
=head1 Search::Estraier::Master |
=head1 Search::Estraier::NodeResult |
591 |
|
|
592 |
Controll node master. This requires user with administration priviledges. |
=head2 new |
593 |
|
|
594 |
|
my $res = new Search::HyperEstraier::NodeResult( |
595 |
|
docs => @array_of_rdocs, |
596 |
|
hits => %hash_with_hints, |
597 |
|
); |
598 |
|
|
599 |
=cut |
=cut |
600 |
|
|
601 |
{ |
sub new { |
602 |
package RequestAgent; |
my $class = shift; |
603 |
our @ISA = qw(LWP::UserAgent); |
my $self = {@_}; |
604 |
|
bless($self, $class); |
605 |
|
|
606 |
sub new { |
foreach my $f (qw/docs hints/) { |
607 |
my $self = LWP::UserAgent::new(@_); |
croak "missing $f for ResultDocument" unless defined($self->{$f}); |
|
$self->agent("Search-Estraier/$Search::Estraer::VERSION"); |
|
|
$self; |
|
608 |
} |
} |
609 |
|
|
610 |
sub get_basic_credentials { |
$self ? return $self : return undef; |
|
my($self, $realm, $uri) = @_; |
|
|
# return ($user, $password); |
|
|
} |
|
611 |
} |
} |
612 |
|
|
613 |
|
=head2 doc_num |
614 |
|
|
615 |
|
Return number of documents |
616 |
|
|
617 |
=head2 new |
print $res->doc_num; |
618 |
|
|
619 |
Create new connection to node master. |
=cut |
620 |
|
|
621 |
my $master = new Search::Estraier::Master( |
sub doc_num { |
622 |
url => 'http://localhost:1978', |
my $self = shift; |
623 |
user => 'admin', |
return $#{$self->{docs}}; |
624 |
passwd => 'admin', |
} |
625 |
); |
|
626 |
|
=head2 get_doc |
627 |
|
|
628 |
|
Return single document |
629 |
|
|
630 |
|
my $doc = $res->get_doc( 42 ); |
631 |
|
|
632 |
|
Returns undef if document doesn't exist. |
633 |
|
|
634 |
|
=cut |
635 |
|
|
636 |
|
sub get_doc { |
637 |
|
my $self = shift; |
638 |
|
my $num = shift; |
639 |
|
croak "expect number as argument" unless ($num =~ m/^\d+$/); |
640 |
|
return undef if ($num < 0 || $num > $self->{docs}); |
641 |
|
return $self->{docs}->[$num]; |
642 |
|
} |
643 |
|
|
644 |
|
=head2 hint |
645 |
|
|
646 |
|
Return specific hint from results. |
647 |
|
|
648 |
|
print $rec->hint( 'VERSION' ); |
649 |
|
|
650 |
|
Possible hints are: C<VERSION>, C<NODE>, C<HIT>, C<HINT#n>, C<DOCNUM>, C<WORDNUM>, |
651 |
|
C<TIME>, C<LINK#n>, C<VIEW>. |
652 |
|
|
653 |
|
=cut |
654 |
|
|
655 |
|
sub hint { |
656 |
|
my $self = shift; |
657 |
|
my $key = shift || return; |
658 |
|
return $self->{hints}->{$key}; |
659 |
|
} |
660 |
|
|
661 |
|
|
662 |
|
package Search::Estraier::Node; |
663 |
|
|
664 |
|
use Carp qw/carp croak/; |
665 |
|
use URI; |
666 |
|
use MIME::Base64; |
667 |
|
use IO::Socket::INET; |
668 |
|
|
669 |
|
=head1 Search::Estraier::Node |
670 |
|
|
671 |
|
=head2 new |
672 |
|
|
673 |
|
my $node = new Search::HyperEstraier::Node; |
674 |
|
|
675 |
=cut |
=cut |
676 |
|
|
677 |
sub new { |
sub new { |
678 |
my $class = shift; |
my $class = shift; |
679 |
my $self = {@_}; |
my $self = { |
680 |
|
pxport => -1, |
681 |
|
timeout => 0, # this used to be -1 |
682 |
|
dnum => -1, |
683 |
|
wnum => -1, |
684 |
|
size => -1.0, |
685 |
|
wwidth => 480, |
686 |
|
hwidth => 96, |
687 |
|
awidth => 96, |
688 |
|
status => -1, |
689 |
|
}; |
690 |
bless($self, $class); |
bless($self, $class); |
691 |
|
|
692 |
foreach my $p (qw/url user passwd/) { |
if (@_) { |
693 |
croak "need $p" unless ($self->{$p}); |
$self->{debug} = 1; |
694 |
|
warn "## Node debug on\n"; |
695 |
} |
} |
696 |
|
|
697 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
698 |
} |
} |
699 |
|
|
700 |
|
=head2 set_url |
701 |
|
|
702 |
|
Specify URL to node server |
703 |
|
|
704 |
|
$node->set_url('http://localhost:1978'); |
705 |
|
|
706 |
|
=cut |
707 |
|
|
708 |
|
sub set_url { |
709 |
|
my $self = shift; |
710 |
|
$self->{url} = shift; |
711 |
|
} |
712 |
|
|
713 |
|
=head2 set_proxy |
714 |
|
|
715 |
|
Specify proxy server to connect to node server |
716 |
|
|
717 |
|
$node->set_proxy('proxy.example.com', 8080); |
718 |
|
|
719 |
|
=cut |
720 |
|
|
721 |
|
sub set_proxy { |
722 |
|
my $self = shift; |
723 |
|
my ($host,$port) = @_; |
724 |
|
croak "proxy port must be number" unless ($port =~ m/^\d+$/); |
725 |
|
$self->{pxhost} = $host; |
726 |
|
$self->{pxport} = $port; |
727 |
|
} |
728 |
|
|
729 |
|
=head2 set_timeout |
730 |
|
|
731 |
|
Specify timeout of connection in seconds |
732 |
|
|
733 |
|
$node->set_timeout( 15 ); |
734 |
|
|
735 |
|
=cut |
736 |
|
|
737 |
|
sub set_timeout { |
738 |
|
my $self = shift; |
739 |
|
my $sec = shift; |
740 |
|
croak "timeout must be number" unless ($sec =~ m/^\d+$/); |
741 |
|
$self->{timeout} = $sec; |
742 |
|
} |
743 |
|
|
744 |
|
=head2 set_auth |
745 |
|
|
746 |
|
Specify name and password for authentication to node server. |
747 |
|
|
748 |
|
$node->set_auth('clint','eastwood'); |
749 |
|
|
750 |
|
=cut |
751 |
|
|
752 |
|
sub set_auth { |
753 |
|
my $self = shift; |
754 |
|
my ($login,$passwd) = @_; |
755 |
|
my $basic_auth = encode_base64( "$login:$passwd" ); |
756 |
|
chomp($basic_auth); |
757 |
|
$self->{auth} = $basic_auth; |
758 |
|
} |
759 |
|
|
760 |
|
=head2 status |
761 |
|
|
762 |
|
Return status code of last request. |
763 |
|
|
764 |
|
print $node->status; |
765 |
|
|
766 |
|
C<-1> means connection failure. |
767 |
|
|
768 |
|
=cut |
769 |
|
|
770 |
|
sub status { |
771 |
|
my $self = shift; |
772 |
|
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 |
788 |
|
|
789 |
|
This is method which uses C<IO::Socket::INET> to communicate with Hyper Estraier node |
790 |
|
master. |
791 |
|
|
792 |
|
my $rv = shuttle_url( $url, $content_type, \$req_body, \$resbody ); |
793 |
|
|
794 |
|
C<$resheads> and C<$resbody> booleans controll if response headers and/or response |
795 |
|
body will be saved within object. |
796 |
|
|
797 |
|
=cut |
798 |
|
|
799 |
|
sub shuttle_url { |
800 |
|
my $self = shift; |
801 |
|
|
802 |
|
my ($url, $content_type, $reqbody, $resbody) = @_; |
803 |
|
|
804 |
|
$self->{status} = -1; |
805 |
|
|
806 |
|
warn "## $url\n"; |
807 |
|
|
808 |
|
$url = new URI($url); |
809 |
|
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); |
818 |
|
|
819 |
|
if ($self->{pxhost}) { |
820 |
|
($host,$port) = ($self->{pxhost}, $self->{pxport}); |
821 |
|
$query = "http://$host:$port/$query"; |
822 |
|
} |
823 |
|
|
824 |
|
$query .= '?' . $url->query if ($url->query && ! $reqbody); |
825 |
|
|
826 |
|
my $headers; |
827 |
|
|
828 |
|
if ($reqbody) { |
829 |
|
$headers .= "POST $query HTTP/1.0\r\n"; |
830 |
|
} else { |
831 |
|
$headers .= "GET $query HTTP/1.0\r\n"; |
832 |
|
} |
833 |
|
|
834 |
|
$headers .= "Host: " . $url->host . ":" . $url->port . "\r\n"; |
835 |
|
$headers .= "Connection: close\r\n"; |
836 |
|
$headers .= "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n"; |
837 |
|
$headers .= "Content-Type: $content_type\r\n"; |
838 |
|
$headers .= "Authorization: Basic $self->{auth}\r\n"; |
839 |
|
my $len = 0; |
840 |
|
{ |
841 |
|
use bytes; |
842 |
|
$len = length($reqbody) if ($reqbody); |
843 |
|
} |
844 |
|
$headers .= "Content-Length: $len\r\n"; |
845 |
|
$headers .= "\r\n"; |
846 |
|
|
847 |
|
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>; |
871 |
|
chomp($line); |
872 |
|
my ($schema, $res_status, undef) = split(/ */, $line, 3); |
873 |
|
return if ($schema !~ /^HTTP/ || ! $res_status); |
874 |
|
|
875 |
|
$self->{status} = $res_status; |
876 |
|
warn "## response status: $res_status\n" if ($self->{debug}); |
877 |
|
|
878 |
|
# skip rest of headers |
879 |
|
$line = <$sock>; |
880 |
|
while ($line) { |
881 |
|
$line = <$sock>; |
882 |
|
$line =~ s/[\r\n]+$//; |
883 |
|
warn "## ", $line || 'NULL', " ##\n" if ($self->{debug}); |
884 |
|
}; |
885 |
|
|
886 |
|
# read body |
887 |
|
$len = 0; |
888 |
|
do { |
889 |
|
$len = read($sock, my $buf, 8192); |
890 |
|
$$resbody .= $buf if ($resbody); |
891 |
|
} while ($len); |
892 |
|
|
893 |
|
warn "## response body:\n$$resbody\n" if ($resbody && $self->{debug}); |
894 |
|
|
895 |
|
return $self->{status}; |
896 |
|
} |
897 |
|
|
898 |
### |
### |
899 |
|
|