--- trunk/Estraier.pm 2006/01/05 22:16:21 37 +++ trunk/Estraier.pm 2006/01/05 23:32:31 41 @@ -272,8 +272,8 @@ $draft .= "\n"; - $draft .= join("\n", @{ $self->{dtexts} }) . "\n"; - $draft .= "\t" . join("\n\t", @{ $self->{htexts} }) . "\n"; + $draft .= join("\n", @{ $self->{dtexts} }) . "\n" if ($self->{dtexts}); + $draft .= "\t" . join("\n\t", @{ $self->{htexts} }) . "\n" if ($self->{htexts}); return $draft; } @@ -689,6 +689,11 @@ }; bless($self, $class); + if (@_) { + $self->{debug} = shift; + warn "## Node debug on\n"; + } + $self ? return $self : return undef; } @@ -747,14 +752,16 @@ sub set_auth { my $self = shift; my ($login,$passwd) = @_; - $self->{auth} = encode_base64( "$login:$passwd" ); + my $basic_auth = encode_base64( "$login:$passwd" ); + chomp($basic_auth); + $self->{auth} = $basic_auth; } =head2 status Return status code of last request. - print $res->status; + print $node->status; C<-1> means connection failure. @@ -765,6 +772,72 @@ return $self->{status}; } +=head2 put_doc + +Add a document + + $node->put_doc( $document_draft ) or die "can't add document"; + +Return true on success or false on failture. + +=cut + +sub put_doc { + my $self = shift; + my $doc = shift || return; + return unless ($self->{url}); + $self->shuttle_url( $self->{url} . '/put_doc', + 'text/x-estraier-draft', + $doc->dump_draft, + undef + ) == 200; +} + + +=head2 out_doc + +Remove a document + + $node->out_doc( document_id ) or "can't remove document"; + +Return true on success or false on failture. + +=cut + +sub out_doc { + my $self = shift; + my $id = shift || return; + return unless ($self->{url}); + croak "id must be number" unless ($id =~ m/^\d+$/); + $self->shuttle_url( $self->{url} . '/out_doc', + 'application/x-www-form-urlencoded', + "id=$id", + undef + ) == 200; +} + + +=head2 out_doc_by_uri + +Remove a registrated document using it's uri + + $node->out_doc_by_uri( 'file:///document_url' ) or "can't remove document"; + +Return true on success or false on failture. + +=cut + +sub out_doc_by_uri { + my $self = shift; + my $uri = shift || return; + return unless ($self->{url}); + $self->shuttle_url( $self->{url} . '/out_doc', + 'application/x-www-form-urlencoded', + "uri=$uri", + undef + ) == 200; +} + =head2 shuttle_url This is method which uses C to communicate with Hyper Estraier node @@ -782,9 +855,9 @@ my ($url, $content_type, $reqbody, $resbody) = @_; - my $status = -1; + $self->{status} = -1; - warn $url; + warn "## $url\n" if ($self->{debug}); $url = new URI($url); if ( @@ -812,10 +885,10 @@ $headers .= "GET $query HTTP/1.0\r\n"; } - $headers .= "Host: $url->host:$url->port\r\n"; + $headers .= "Host: " . $url->host . ":" . $url->port . "\r\n"; $headers .= "Connection: close\r\n"; $headers .= "User-Agent: Search-Estraier/$Search::Estraier::VERSION\r\n"; - $headers .= "Content-Type $content_type\r\n"; + $headers .= "Content-Type: $content_type\r\n"; $headers .= "Authorization: Basic $self->{auth}\r\n"; my $len = 0; { @@ -837,11 +910,14 @@ return -1; } + warn $headers if ($self->{debug}); + print $sock $headers or carp "can't send headers to network:\n$headers\n" and return -1; if ($reqbody) { - print $sock $$reqbody or + warn "$reqbody\n" if ($self->{debug}); + print $sock $reqbody or carp "can't send request body to network:\n$$reqbody\n" and return -1; } @@ -851,21 +927,26 @@ return if ($schema !~ /^HTTP/ || ! $res_status); $self->{status} = $res_status; + warn "## response status: $res_status\n" if ($self->{debug}); # skip rest of headers - do { + $line = <$sock>; + while ($line) { $line = <$sock>; - chomp($line); - } until ($line eq ''); + $line =~ s/[\r\n]+$//; + warn "## ", $line || 'NULL', " ##\n" if ($self->{debug}); + }; # read body - my $len = 0; + $len = 0; do { $len = read($sock, my $buf, 8192); $$resbody .= $buf if ($resbody); } while ($len); - return $status; + warn "## response body:\n$$resbody\n" if ($resbody && $self->{debug}); + + return $self->{status}; } ###