580 |
} |
} |
581 |
|
|
582 |
|
|
583 |
package Search::Estraier::Master; |
package Search::Estraier::NodeResult; |
584 |
|
|
585 |
use Carp; |
use Carp qw/croak/; |
586 |
|
|
587 |
=head1 Search::Estraier::Master |
#use Search::Estraier; |
588 |
|
#our @ISA = qw/Search::Estraier/; |
589 |
|
|
590 |
Controll node master. This requires user with administration priviledges. |
=head1 Search::Estraier::NodeResult |
591 |
|
|
592 |
|
=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/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 |
|
|
|
foreach my $p (qw/url user passwd/) { |
|
|
croak "need $p" unless ($self->{$p}); |
|
|
} |
|
|
|
|
692 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
693 |
} |
} |
694 |
|
|
695 |
|
=head2 set_url |
696 |
|
|
697 |
|
Specify URL to node server |
698 |
|
|
699 |
|
$node->set_url('http://localhost:1978'); |
700 |
|
|
701 |
|
=cut |
702 |
|
|
703 |
|
sub set_url { |
704 |
|
my $self = shift; |
705 |
|
$self->{url} = shift; |
706 |
|
} |
707 |
|
|
708 |
|
=head2 set_proxy |
709 |
|
|
710 |
|
Specify proxy server to connect to node server |
711 |
|
|
712 |
|
$node->set_proxy('proxy.example.com', 8080); |
713 |
|
|
714 |
|
=cut |
715 |
|
|
716 |
|
sub set_proxy { |
717 |
|
my $self = shift; |
718 |
|
my ($host,$port) = @_; |
719 |
|
croak "proxy port must be number" unless ($port =~ m/^\d+$/); |
720 |
|
$self->{pxhost} = $host; |
721 |
|
$self->{pxport} = $port; |
722 |
|
} |
723 |
|
|
724 |
|
=head2 set_timeout |
725 |
|
|
726 |
|
Specify timeout of connection in seconds |
727 |
|
|
728 |
|
$node->set_timeout( 15 ); |
729 |
|
|
730 |
|
=cut |
731 |
|
|
732 |
|
sub set_timeout { |
733 |
|
my $self = shift; |
734 |
|
my $sec = shift; |
735 |
|
croak "timeout must be number" unless ($sec =~ m/^\d+$/); |
736 |
|
$self->{timeout} = $sec; |
737 |
|
} |
738 |
|
|
739 |
|
=head2 set_auth |
740 |
|
|
741 |
|
Specify name and password for authentication to node server. |
742 |
|
|
743 |
|
$node->set_auth('clint','eastwood'); |
744 |
|
|
745 |
|
=cut |
746 |
|
|
747 |
|
sub set_auth { |
748 |
|
my $self = shift; |
749 |
|
my ($login,$passwd) = @_; |
750 |
|
$self->{auth} = encode_base64( "$login:$passwd" ); |
751 |
|
} |
752 |
|
|
753 |
|
=head2 status |
754 |
|
|
755 |
|
Return status code of last request. |
756 |
|
|
757 |
|
print $res->status; |
758 |
|
|
759 |
|
C<-1> means connection failure. |
760 |
|
|
761 |
|
=cut |
762 |
|
|
763 |
|
sub status { |
764 |
|
my $self = shift; |
765 |
|
return $self->{status}; |
766 |
|
} |
767 |
|
|
768 |
|
=head2 shuttle_url |
769 |
|
|
770 |
|
This is method which uses C<IO::Socket::INET> to communicate with Hyper Estraier node |
771 |
|
master. |
772 |
|
|
773 |
|
my $rv = shuttle_url( $url, $content_type, \$req_body, \$resbody ); |
774 |
|
|
775 |
|
C<$resheads> and C<$resbody> booleans controll if response headers and/or response |
776 |
|
body will be saved within object. |
777 |
|
|
778 |
|
=cut |
779 |
|
|
780 |
|
sub shuttle_url { |
781 |
|
my $self = shift; |
782 |
|
|
783 |
|
my ($url, $content_type, $reqbody, $resbody) = @_; |
784 |
|
|
785 |
|
my $status = -1; |
786 |
|
|
787 |
|
warn $url; |
788 |
|
|
789 |
|
$url = new URI($url); |
790 |
|
return -1 unless ($url && $url->scheme && $url->scheme eq 'http' && $url->host && $url->port > 1); |
791 |
|
|
792 |
|
my ($host,$port,$query) = ($url->host, $url->port, $url->path); |
793 |
|
|
794 |
|
if ($self->{pxhost}) { |
795 |
|
($host,$port) = ($self->{pxhost}, $self->{pxport}); |
796 |
|
$query = "http://$host:$port/$query"; |
797 |
|
} |
798 |
|
|
799 |
|
$query .= '?' + $url->query if ($url->query && ! $reqbody); |
800 |
|
|
801 |
|
my $sock = IO::Socket::INET->new( |
802 |
|
PeerAddr => $host, |
803 |
|
PeerPort => $port, |
804 |
|
Proto => 'tcp', |
805 |
|
Timeout => $self->{timeout} || 90, |
806 |
|
) || return -1; |
807 |
|
|
808 |
|
if ($reqbody) { |
809 |
|
print $sock "POST $query HTTP/1.0\r\n"; |
810 |
|
} else { |
811 |
|
print $sock "GET $query HTTP/1.0\r\n"; |
812 |
|
} |
813 |
|
|
814 |
|
print $sock "Host: $url->host:$url->port\r\n"; |
815 |
|
print $sock "Connection: close\r\n"; |
816 |
|
print $sock "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n"; |
817 |
|
print $sock "Content-Type $content_type\r\n"; |
818 |
|
print $sock "Authorization: Basic $self->{auth}\r\n"; |
819 |
|
{ |
820 |
|
use bytes; |
821 |
|
print $sock "Content-Length: ", length($reqbody), "\r\n"; |
822 |
|
} |
823 |
|
print $sock "\r\n"; |
824 |
|
|
825 |
|
print $sock $$reqbody if ($reqbody); |
826 |
|
|
827 |
|
my $line = <$sock>; |
828 |
|
chomp($line); |
829 |
|
my ($schema, $res_status, undef) = split(/ */, $line, 3); |
830 |
|
return if ($schema !~ /^HTTP/ || ! $res_status); |
831 |
|
|
832 |
|
$self->{status} = $res_status; |
833 |
|
|
834 |
|
# skip rest of headers |
835 |
|
do { |
836 |
|
$line = <$sock>; |
837 |
|
chomp($line); |
838 |
|
} until ($line eq ''); |
839 |
|
|
840 |
|
# read body |
841 |
|
my $len = 0; |
842 |
|
do { |
843 |
|
$len = read($sock, my $buf, 8192); |
844 |
|
$$resbody .= $buf if ($resbody); |
845 |
|
} while ($len); |
846 |
|
|
847 |
|
return $status; |
848 |
|
} |
849 |
|
|
850 |
### |
### |
851 |
|
|