/[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 37 by dpavlin, Thu Jan 5 22:16:21 2006 UTC
# Line 661  sub hint { Line 661  sub hint {
661    
662  package Search::Estraier::Node;  package Search::Estraier::Node;
663    
664  use Carp qw/croak/;  use Carp qw/carp croak/;
665    use URI;
666    use MIME::Base64;
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} = encode_base64( "$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    sub status {
764            my $self = shift;
765            return $self->{status};
766    }
767    
768    =head2 shuttle_url
769    
770  Create new connection to node master.  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          foreach my $p (qw/url user passwd/) {          my $status = -1;
786                  croak "need $p" unless ($self->{$p});  
787            warn $url;
788    
789            $url = new URI($url);
790            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          $self ? return $self : return undef;          my ($host,$port,$query) = ($url->host, $url->port, $url->path);
799  }  
800            if ($self->{pxhost}) {
801                    ($host,$port) = ($self->{pxhost}, $self->{pxport});
802                    $query = "http://$host:$port/$query";
803            }
804    
805            $query .= '?' . $url->query if ($url->query && ! $reqbody);
806    
807            my $headers;
808    
809            if ($reqbody) {
810                    $headers .= "POST $query HTTP/1.0\r\n";
811            } else {
812                    $headers .= "GET $query HTTP/1.0\r\n";
813            }
814    
815            $headers .= "Host: $url->host:$url->port\r\n";
816            $headers .= "Connection: close\r\n";
817            $headers .= "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n";
818            $headers .= "Content-Type $content_type\r\n";
819            $headers .= "Authorization: Basic $self->{auth}\r\n";
820            my $len = 0;
821            {
822                    use bytes;
823                    $len = length($reqbody) if ($reqbody);
824            }
825            $headers .= "Content-Length: $len\r\n";
826            $headers .= "\r\n";
827    
828            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>;
849            chomp($line);
850            my ($schema, $res_status, undef) = split(/  */, $line, 3);
851            return if ($schema !~ /^HTTP/ || ! $res_status);
852    
853            $self->{status} = $res_status;
854    
855            # skip rest of headers
856            do {
857                    $line = <$sock>;
858                    chomp($line);
859            } until ($line eq '');
860    
861            # read body
862            my $len = 0;
863            do {
864                    $len = read($sock, my $buf, 8192);
865                    $$resbody .= $buf if ($resbody);
866            } while ($len);
867    
868            return $status;
869    }
870    
871  ###  ###
872    

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

  ViewVC Help
Powered by ViewVC 1.1.26