/[Search-Estraier]/trunk/lib/Search/Estraier.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/Search/Estraier.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 30 by dpavlin, Thu Jan 5 15:33:48 2006 UTC revision 33 by dpavlin, Thu Jan 5 17:54:18 2006 UTC
# Line 662  sub hint { Line 662  sub hint {
662  package Search::Estraier::Node;  package Search::Estraier::Node;
663    
664  use Carp qw/croak/;  use Carp qw/croak/;
665    use URI;
666    use URI::Escape qw/uri_escape/;
667    use IO::Socket::INET;
668    
669  =head1 Search::Estraier::Node  =head1 Search::Estraier::Node
670    
# Line 675  sub new { Line 678  sub new {
678          my $class = shift;          my $class = shift;
679          my $self = {          my $self = {
680                  pxport => -1,                  pxport => -1,
681                  timeout => -1,                  timeout => 0,   # this used to be -1
682                  dnum => -1,                  dnum => -1,
683                  wnum => -1,                  wnum => -1,
684                  size => -1.0,                  size => -1.0,
# Line 733  sub set_timeout { Line 736  sub set_timeout {
736          $self->{timeout} = $sec;          $self->{timeout} = $sec;
737  }  }
738    
739  package Search::Estraier::Master;  =head2 set_auth
740    
741  use Carp;  Specify name and password for authentication to node server.
742    
743  =head1 Search::Estraier::Master    $node->set_auth('clint','eastwood');
   
 Controll node master. This requires user with administration priviledges.  
744    
745  =cut  =cut
746    
747  {  sub set_auth {
748          package RequestAgent;          my $self = shift;
749          our @ISA = qw(LWP::UserAgent);          my ($login,$passwd) = @_;
750            $self->{auth} = uri_escape( "$login:$passwd" );
751    }
752    
753          sub new {  =head2 status
                 my $self = LWP::UserAgent::new(@_);  
                 $self->agent("Search-Estraier/$Search::Estraer::VERSION");  
                 $self;  
         }  
754    
755          sub get_basic_credentials {  Return status code of last request.
                 my($self, $realm, $uri) = @_;  
 #               return ($user, $password);  
         }  
 }  
756    
757      print $res->status;
758    
759    C<-1> means connection failure.
760    
761  =head2 new  =cut
762    
763  Create new connection to node master.  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 $master = new Search::Estraier::Master(    my $rv = shuttle_url( $url, $content_type, \$req_body, \$resbody );
774          url => 'http://localhost:1978',  
775          user => 'admin',  C<$resheads> and C<$resbody> booleans controll if response headers and/or response
776          passwd => 'admin',  body will be saved within object.
   );  
777    
778  =cut  =cut
779    
780  sub new {  sub shuttle_url {
781          my $class = shift;          my $self = shift;
782          my $self = {@_};  
783          bless($self, $class);          my ($url, $content_type, $reqbody, $resbody) = @_;
784    
785            my $status = -1;
786    
787          foreach my $p (qw/url user passwd/) {          $url = new URI($url);
788                  croak "need $p" unless ($self->{$p});          return unless ($url->scheme ne 'http' || ! $url->host || $url->port < 1);
789    
790            my ($host,$port,$query) = ($url->host, $url->port, $url->path);
791    
792            if ($self->{pxhost}) {
793                    ($host,$port) = ($self->{pxhost}, $self->{pxport});
794                    $query = "http://$host:$port/$query";
795          }          }
796    
797          $self ? return $self : return undef;          $query .= '?' + $url->query if ($url->query && ! $reqbody);
798  }  
799            my $sock = IO::Socket::INET->new(
800                    PeerAddr        => $host,
801                    PeerPort        => $port,
802                    Proto           => 'tcp',
803                    Timeout         => $self->{timeout} || 90,
804            ) || return -1;
805    
806            if ($reqbody) {
807                    print $sock "POST $query HTTP/1.0\r\n";
808            } else {
809                    print $sock "GET $query HTTP/1.0\r\n";
810            }
811    
812            print $sock "Host: $url->host:$url->port\r\n";
813            print $sock "Connection: close\r\n";
814            print $sock "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n";
815            print $sock "Content-Type $content_type\r\n";
816            print $sock "Authorization: Basic $self->{auth}\r\n";
817            {
818                    use bytes;
819                    print $sock "Content-Length: ", length($reqbody), "\r\n";
820            }
821            print $sock "\r\n";
822    
823            print $sock $$reqbody if ($reqbody);
824    
825            my $line = <$sock>;
826            chomp($line);
827            my ($schema, $res_status, undef) = split(/  */, $line, 3);
828            return if ($schema !~ /^HTTP/ || ! $res_status);
829    
830            $self->{status} = $res_status;
831    
832            # skip rest of headers
833            do {
834                    $line = <$sock>;
835                    chomp($line);
836            } until ($line eq '');
837    
838            # read body
839            my $len = 0;
840            do {
841                    $len = read($sock, my $buf, 8192);
842                    $$resbody .= $buf if ($resbody);
843            } while ($len);
844    
845            return $status;
846    }
847    
848  ###  ###
849    

Legend:
Removed from v.30  
changed lines
  Added in v.33

  ViewVC Help
Powered by ViewVC 1.1.26