/[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 36 by dpavlin, Thu Jan 5 21:51:54 2006 UTC revision 41 by dpavlin, Thu Jan 5 23:32:31 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;  use URI;
666  use MIME::Base64;  use MIME::Base64;
667  use IO::Socket::INET;  use IO::Socket::INET;
# Line 689  sub new { Line 689  sub new {
689          };          };
690          bless($self, $class);          bless($self, $class);
691    
692            if (@_) {
693                    $self->{debug} = shift;
694                    warn "## Node debug on\n";
695            }
696    
697          $self ? return $self : return undef;          $self ? return $self : return undef;
698  }  }
699    
# Line 747  Specify name and password for authentica Line 752  Specify name and password for authentica
752  sub set_auth {  sub set_auth {
753          my $self = shift;          my $self = shift;
754          my ($login,$passwd) = @_;          my ($login,$passwd) = @_;
755          $self->{auth} = encode_base64( "$login:$passwd" );          my $basic_auth = encode_base64( "$login:$passwd" );
756            chomp($basic_auth);
757            $self->{auth} = $basic_auth;
758  }  }
759    
760  =head2 status  =head2 status
761    
762  Return status code of last request.  Return status code of last request.
763    
764    print $res->status;    print $node->status;
765    
766  C<-1> means connection failure.  C<-1> means connection failure.
767    
# Line 765  sub status { Line 772  sub status {
772          return $self->{status};          return $self->{status};
773  }  }
774    
775    =head2 put_doc
776    
777    Add a document
778    
779      $node->put_doc( $document_draft ) or die "can't add document";
780    
781    Return true on success or false on failture.
782    
783    =cut
784    
785    sub put_doc {
786            my $self = shift;
787            my $doc = shift || return;
788            return unless ($self->{url});
789            $self->shuttle_url( $self->{url} . '/put_doc',
790                    'text/x-estraier-draft',
791                    $doc->dump_draft,
792                    undef
793            ) == 200;
794    }
795    
796    
797    =head2 out_doc
798    
799    Remove a document
800    
801      $node->out_doc( document_id ) or "can't remove document";
802    
803    Return true on success or false on failture.
804    
805    =cut
806    
807    sub out_doc {
808            my $self = shift;
809            my $id = shift || return;
810            return unless ($self->{url});
811            croak "id must be number" unless ($id =~ m/^\d+$/);
812            $self->shuttle_url( $self->{url} . '/out_doc',
813                    'application/x-www-form-urlencoded',
814                    "id=$id",
815                    undef
816            ) == 200;
817    }
818    
819    
820    =head2 out_doc_by_uri
821    
822    Remove a registrated document using it's uri
823    
824      $node->out_doc_by_uri( 'file:///document_url' ) or "can't remove document";
825    
826    Return true on success or false on failture.
827    
828    =cut
829    
830    sub out_doc_by_uri {
831            my $self = shift;
832            my $uri = shift || return;
833            return unless ($self->{url});
834            $self->shuttle_url( $self->{url} . '/out_doc',
835                    'application/x-www-form-urlencoded',
836                    "uri=$uri",
837                    undef
838            ) == 200;
839    }
840    
841  =head2 shuttle_url  =head2 shuttle_url
842    
843  This is method which uses C<IO::Socket::INET> to communicate with Hyper Estraier node  This is method which uses C<IO::Socket::INET> to communicate with Hyper Estraier node
# Line 782  sub shuttle_url { Line 855  sub shuttle_url {
855    
856          my ($url, $content_type, $reqbody, $resbody) = @_;          my ($url, $content_type, $reqbody, $resbody) = @_;
857    
858          my $status = -1;          $self->{status} = -1;
859    
860          warn $url;          warn "## $url\n" if ($self->{debug});
861    
862          $url = new URI($url);          $url = new URI($url);
863          return -1 unless ($url && $url->scheme && $url->scheme eq 'http' && $url->host && $url->port > 1);          if (
864                            !$url || !$url->scheme || !$url->scheme eq 'http' ||
865                            !$url->host || !$url->port || $url->port < 1
866                    ) {
867                    carp "can't parse $url\n";
868                    return -1;
869            }
870    
871          my ($host,$port,$query) = ($url->host, $url->port, $url->path);          my ($host,$port,$query) = ($url->host, $url->port, $url->path);
872    
# Line 796  sub shuttle_url { Line 875  sub shuttle_url {
875                  $query = "http://$host:$port/$query";                  $query = "http://$host:$port/$query";
876          }          }
877    
878          $query .= '?' + $url->query if ($url->query && ! $reqbody);          $query .= '?' . $url->query if ($url->query && ! $reqbody);
879    
880          my $sock = IO::Socket::INET->new(          my $headers;
                 PeerAddr        => $host,  
                 PeerPort        => $port,  
                 Proto           => 'tcp',  
                 Timeout         => $self->{timeout} || 90,  
         ) || return -1;  
881    
882          if ($reqbody) {          if ($reqbody) {
883                  print $sock "POST $query HTTP/1.0\r\n";                  $headers .= "POST $query HTTP/1.0\r\n";
884          } else {          } else {
885                  print $sock "GET $query HTTP/1.0\r\n";                  $headers .= "GET $query HTTP/1.0\r\n";
886          }          }
887    
888          print $sock "Host: $url->host:$url->port\r\n";          $headers .= "Host: " . $url->host . ":" . $url->port . "\r\n";
889          print $sock "Connection: close\r\n";          $headers .= "Connection: close\r\n";
890          print $sock "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n";          $headers .= "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n";
891          print $sock "Content-Type $content_type\r\n";          $headers .= "Content-Type: $content_type\r\n";
892          print $sock "Authorization: Basic $self->{auth}\r\n";          $headers .= "Authorization: Basic $self->{auth}\r\n";
893            my $len = 0;
894          {          {
895                  use bytes;                  use bytes;
896                  print $sock "Content-Length: ", length($reqbody), "\r\n";                  $len = length($reqbody) if ($reqbody);
897            }
898            $headers .= "Content-Length: $len\r\n";
899            $headers .= "\r\n";
900    
901            my $sock = IO::Socket::INET->new(
902                    PeerAddr        => $host,
903                    PeerPort        => $port,
904                    Proto           => 'tcp',
905                    Timeout         => $self->{timeout} || 90,
906            );
907    
908            if (! $sock) {
909                    carp "can't open socket to $host:$port";
910                    return -1;
911          }          }
         print $sock "\r\n";  
912    
913          print $sock $$reqbody if ($reqbody);          warn $headers if ($self->{debug});
914    
915            print $sock $headers or
916                    carp "can't send headers to network:\n$headers\n" and return -1;
917    
918            if ($reqbody) {
919                    warn "$reqbody\n" if ($self->{debug});
920                    print $sock $reqbody or
921                            carp "can't send request body to network:\n$$reqbody\n" and return -1;
922            }
923    
924          my $line = <$sock>;          my $line = <$sock>;
925          chomp($line);          chomp($line);
# Line 830  sub shuttle_url { Line 927  sub shuttle_url {
927          return if ($schema !~ /^HTTP/ || ! $res_status);          return if ($schema !~ /^HTTP/ || ! $res_status);
928    
929          $self->{status} = $res_status;          $self->{status} = $res_status;
930            warn "## response status: $res_status\n" if ($self->{debug});
931    
932          # skip rest of headers          # skip rest of headers
933          do {          $line = <$sock>;
934            while ($line) {
935                  $line = <$sock>;                  $line = <$sock>;
936                  chomp($line);                  $line =~ s/[\r\n]+$//;
937          } until ($line eq '');                  warn "## ", $line || 'NULL', " ##\n" if ($self->{debug});
938            };
939    
940          # read body          # read body
941          my $len = 0;          $len = 0;
942          do {          do {
943                  $len = read($sock, my $buf, 8192);                  $len = read($sock, my $buf, 8192);
944                  $$resbody .= $buf if ($resbody);                  $$resbody .= $buf if ($resbody);
945          } while ($len);          } while ($len);
946    
947          return $status;          warn "## response body:\n$$resbody\n" if ($resbody && $self->{debug});
948    
949            return $self->{status};
950  }  }
951    
952  ###  ###

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

  ViewVC Help
Powered by ViewVC 1.1.26