/[Search-Estraier]/trunk/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/Estraier.pm

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

revision 29 by dpavlin, Thu Jan 5 15:30:35 2006 UTC revision 36 by dpavlin, Thu Jan 5 21:51:54 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 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 718  sub set_proxy { Line 721  sub set_proxy {
721          $self->{pxport} = $port;          $self->{pxport} = $port;
722  }  }
723    
724  package Search::Estraier::Master;  =head2 set_timeout
725    
726  use Carp;  Specify timeout of connection in seconds
727    
728  =head1 Search::Estraier::Master    $node->set_timeout( 15 );
   
 Controll node master. This requires user with administration priviledges.  
729    
730  =cut  =cut
731    
732  {  sub set_timeout {
733          package RequestAgent;          my $self = shift;
734          our @ISA = qw(LWP::UserAgent);          my $sec = shift;
735            croak "timeout must be number" unless ($sec =~ m/^\d+$/);
736            $self->{timeout} = $sec;
737    }
738    
739          sub new {  =head2 set_auth
                 my $self = LWP::UserAgent::new(@_);  
                 $self->agent("Search-Estraier/$Search::Estraer::VERSION");  
                 $self;  
         }  
740    
741          sub get_basic_credentials {  Specify name and password for authentication to node server.
742                  my($self, $realm, $uri) = @_;  
743  #               return ($user, $password);    $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  =head2 new    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  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            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          foreach my $p (qw/url user passwd/) {          my ($host,$port,$query) = ($url->host, $url->port, $url->path);
793                  croak "need $p" unless ($self->{$p});  
794            if ($self->{pxhost}) {
795                    ($host,$port) = ($self->{pxhost}, $self->{pxport});
796                    $query = "http://$host:$port/$query";
797          }          }
798    
799          $self ? return $self : return undef;          $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    

Legend:
Removed from v.29  
changed lines
  Added in v.36

  ViewVC Help
Powered by ViewVC 1.1.26