/[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 40 by dpavlin, Thu Jan 5 23:00:22 2006 UTC
# Line 272  sub dump_draft { Line 272  sub dump_draft {
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  }  }
# 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 686  sub new { Line 689  sub new {
689          };          };
690          bless($self, $class);          bless($self, $class);
691    
692            if (@_) {
693                    $self->{debug} = 1;
694                    warn "## Node debug on\n";
695            }
696    
697          $self ? return $self : return undef;          $self ? return $self : return undef;
698  }  }
699    
# Line 733  sub set_timeout { Line 741  sub set_timeout {
741          $self->{timeout} = $sec;          $self->{timeout} = $sec;
742  }  }
743    
744  package Search::Estraier::Master;  =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  use Carp;  This is method which uses C<IO::Socket::INET> to communicate with Hyper Estraier node
790    master.
791    
792  =head1 Search::Estraier::Master    my $rv = shuttle_url( $url, $content_type, \$req_body, \$resbody );
793    
794  Controll node master. This requires user with administration priviledges.  C<$resheads> and C<$resbody> booleans controll if response headers and/or response
795    body will be saved within object.
796    
797  =cut  =cut
798    
799  {  sub shuttle_url {
800          package RequestAgent;          my $self = shift;
801          our @ISA = qw(LWP::UserAgent);  
802            my ($url, $content_type, $reqbody, $resbody) = @_;
803    
804            $self->{status} = -1;
805    
806          sub new {          warn "## $url\n";
807                  my $self = LWP::UserAgent::new(@_);  
808                  $self->agent("Search-Estraier/$Search::Estraer::VERSION");          $url = new URI($url);
809                  $self;          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          sub get_basic_credentials {          my ($host,$port,$query) = ($url->host, $url->port, $url->path);
818                  my($self, $realm, $uri) = @_;  
819  #               return ($user, $password);          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  =head2 new          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  Create new connection to node master.          $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 $master = new Search::Estraier::Master(          my $sock = IO::Socket::INET->new(
848          url => 'http://localhost:1978',                  PeerAddr        => $host,
849          user => 'admin',                  PeerPort        => $port,
850          passwd => 'admin',                  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  =cut          warn $headers if ($self->{debug});
860    
861  sub new {          print $sock $headers or
862          my $class = shift;                  carp "can't send headers to network:\n$headers\n" and return -1;
         my $self = {@_};  
         bless($self, $class);  
863    
864          foreach my $p (qw/url user passwd/) {          if ($reqbody) {
865                  croak "need $p" unless ($self->{$p});                  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          $self ? return $self : return undef;          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    

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

  ViewVC Help
Powered by ViewVC 1.1.26