--- trunk/Estraier.pm 2006/01/05 15:33:48 30 +++ trunk/Estraier.pm 2006/01/05 22:36:10 39 @@ -661,7 +661,10 @@ package Search::Estraier::Node; -use Carp qw/croak/; +use Carp qw/carp croak/; +use URI; +use MIME::Base64; +use IO::Socket::INET; =head1 Search::Estraier::Node @@ -675,7 +678,7 @@ my $class = shift; my $self = { pxport => -1, - timeout => -1, + timeout => 0, # this used to be -1 dnum => -1, wnum => -1, size => -1.0, @@ -686,6 +689,11 @@ }; bless($self, $class); + if (@_) { + $self->{debug} = 1; + warn "## Node debug on\n"; + } + $self ? return $self : return undef; } @@ -733,59 +741,146 @@ $self->{timeout} = $sec; } -package Search::Estraier::Master; +=head2 set_auth + +Specify name and password for authentication to node server. + + $node->set_auth('clint','eastwood'); + +=cut + +sub set_auth { + my $self = shift; + my ($login,$passwd) = @_; + $self->{auth} = encode_base64( "$login:$passwd" ); +} + +=head2 status + +Return status code of last request. + + print $res->status; + +C<-1> means connection failure. + +=cut + +sub status { + my $self = shift; + return $self->{status}; +} + +=head2 shuttle_url -use Carp; +This is method which uses C to communicate with Hyper Estraier node +master. -=head1 Search::Estraier::Master + my $rv = shuttle_url( $url, $content_type, \$req_body, \$resbody ); -Controll node master. This requires user with administration priviledges. +C<$resheads> and C<$resbody> booleans controll if response headers and/or response +body will be saved within object. =cut -{ - package RequestAgent; - our @ISA = qw(LWP::UserAgent); +sub shuttle_url { + my $self = shift; + + my ($url, $content_type, $reqbody, $resbody) = @_; + + my $status = -1; - sub new { - my $self = LWP::UserAgent::new(@_); - $self->agent("Search-Estraier/$Search::Estraer::VERSION"); - $self; + warn "## $url\n"; + + $url = new URI($url); + if ( + !$url || !$url->scheme || !$url->scheme eq 'http' || + !$url->host || !$url->port || $url->port < 1 + ) { + carp "can't parse $url\n"; + return -1; } - sub get_basic_credentials { - my($self, $realm, $uri) = @_; -# return ($user, $password); + my ($host,$port,$query) = ($url->host, $url->port, $url->path); + + if ($self->{pxhost}) { + ($host,$port) = ($self->{pxhost}, $self->{pxport}); + $query = "http://$host:$port/$query"; } -} + $query .= '?' . $url->query if ($url->query && ! $reqbody); + my $headers; -=head2 new + if ($reqbody) { + $headers .= "POST $query HTTP/1.0\r\n"; + } else { + $headers .= "GET $query HTTP/1.0\r\n"; + } -Create new connection to node master. + $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 .= "Authorization: Basic $self->{auth}\r\n"; + my $len = 0; + { + use bytes; + $len = length($reqbody) if ($reqbody); + } + $headers .= "Content-Length: $len\r\n"; + $headers .= "\r\n"; - my $master = new Search::Estraier::Master( - url => 'http://localhost:1978', - user => 'admin', - passwd => 'admin', - ); + my $sock = IO::Socket::INET->new( + PeerAddr => $host, + PeerPort => $port, + Proto => 'tcp', + Timeout => $self->{timeout} || 90, + ); + + if (! $sock) { + carp "can't open socket to $host:$port"; + return -1; + } -=cut + warn "## headers:\n$headers\n" if ($self->{debug}); -sub new { - my $class = shift; - my $self = {@_}; - bless($self, $class); + print $sock $headers or + carp "can't send headers to network:\n$headers\n" and return -1; - foreach my $p (qw/url user passwd/) { - croak "need $p" unless ($self->{$p}); + if ($reqbody) { + warn "## request body:\n$headers\n" if ($self->{debug}); + print $sock $$reqbody or + carp "can't send request body to network:\n$$reqbody\n" and return -1; } - $self ? return $self : return undef; -} + my $line = <$sock>; + chomp($line); + my ($schema, $res_status, undef) = split(/ */, $line, 3); + return if ($schema !~ /^HTTP/ || ! $res_status); + + $status = $res_status; + warn "## response status: $res_status\n" if ($self->{debug}); + + # skip rest of headers + $line = <$sock>; + while ($line) { + $line = <$sock>; + $line =~ s/[\r\n]+$//; + warn "## ", $line || 'NULL', " ##\n"; + }; + # read body + $len = 0; + do { + $len = read($sock, my $buf, 8192); + $$resbody .= $buf if ($resbody); + } while ($len); + + warn "## response body:\n$$resbody\n" if ($self->{debug}); + + return $status; +} ###