--- trunk/Estraier.pm 2006/01/06 21:05:05 58 +++ trunk/Estraier.pm 2006/01/07 01:21:28 61 @@ -4,7 +4,7 @@ use strict; use warnings; -our $VERSION = '0.00'; +our $VERSION = '0.01'; =head1 NAME @@ -205,7 +205,8 @@ sub attr_names { my $self = shift; - croak "attr_names return array, not scalar" if (! wantarray); + return unless ($self->{attrs}); + #croak "attr_names return array, not scalar" if (! wantarray); return sort keys %{ $self->{attrs} }; } @@ -221,8 +222,8 @@ sub attr { my $self = shift; my $name = shift; - - return $self->{'attrs'}->{ $name }; + return unless (defined($name) && $self->{attrs}); + return $self->{attrs}->{ $name }; } @@ -236,8 +237,8 @@ sub texts { my $self = shift; - confess "texts return array, not scalar" if (! wantarray); - return @{ $self->{dtexts} }; + #confess "texts return array, not scalar" if (! wantarray); + return @{ $self->{dtexts} } if ($self->{dtexts}); } @@ -251,7 +252,7 @@ sub cat_texts { my $self = shift; - return join(' ',@{ $self->{dtexts} }); + return join(' ',@{ $self->{dtexts} }) if ($self->{dtexts}); } @@ -460,7 +461,7 @@ sub attrs { my $self = shift; #croak "attrs return array, not scalar" if (! wantarray); - return @{ $self->{attrs} }; + return @{ $self->{attrs} } if ($self->{attrs}); } @@ -1176,7 +1177,7 @@ my $rv = $self->shuttle_url( $self->{url} . '/search', 'application/x-www-form-urlencoded', - $self->cond_to_query( $cond ), + $self->cond_to_query( $cond, $depth ), \$resbody, ); return if ($rv != 200); @@ -1270,7 +1271,7 @@ Return URI encoded string generated from Search::Estraier::Condition - my $args = $node->cond_to_query( $cond ); + my $args = $node->cond_to_query( $cond, $depth ); =cut @@ -1279,6 +1280,7 @@ my $cond = shift || return; croak "condition must be Search::Estraier::Condition, not '$cond->isa'" unless ($cond->isa('Search::Estraier::Condition')); + my $depth = shift; my @args; @@ -1306,7 +1308,7 @@ push @args, 'options=' . $options; } - push @args, 'depth=' . $self->{depth} if ($self->{depth}); + push @args, 'depth=' . $depth if ($depth); push @args, 'wwidth=' . $self->{wwidth}; push @args, 'hwidth=' . $self->{hwidth}; push @args, 'awidth=' . $self->{awidth}; @@ -1327,6 +1329,8 @@ =cut +use LWP::UserAgent; + sub shuttle_url { my $self = shift; @@ -1345,81 +1349,37 @@ return -1; } - 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; + my $ua = LWP::UserAgent->new; + $ua->agent( "Search-Estraier/$Search::Estraier::VERSION" ); + my $req; if ($reqbody) { - $headers .= "POST $query HTTP/1.0\r\n"; + $req = HTTP::Request->new(POST => $url); } else { - $headers .= "GET $query HTTP/1.0\r\n"; + $req = HTTP::Request->new(GET => $url); } - $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 $sock = IO::Socket::INET->new( - PeerAddr => $host, - PeerPort => $port, - Proto => 'tcp', - Timeout => $self->{timeout} || 90, - ); + $req->headers->header( 'Host' => $url->host . ":" . $url->port ); + $req->headers->header( 'Connection', 'close' ); + $req->headers->header( 'Authorization', 'Basic ' . $self->{auth} ); + $req->content_type( $content_type ); - if (! $sock) { - carp "can't open socket to $host:$port"; - return -1; - } - - warn $headers if ($self->{debug}); - - print $sock $headers or - carp "can't send headers to network:\n$headers\n" and return -1; + warn $req->headers->as_string,"\n" if ($self->{debug}); if ($reqbody) { warn "$reqbody\n" if ($self->{debug}); - print $sock $reqbody or - carp "can't send request body to network:\n$$reqbody\n" and return -1; + $req->content( $reqbody ); } - my $line = <$sock>; - chomp($line); - my ($schema, $res_status, undef) = split(/ */, $line, 3); - return if ($schema !~ /^HTTP/ || ! $res_status); - - $self->{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" if ($self->{debug}); - }; + my $res = $ua->request($req) || croak "can't make request to $url: $!"; + + warn "## response status: ",$res->status_line,"\n" if ($self->{debug}); + + return -1 if (! $res->is_success); + + ($self->{status}, $self->{status_message}) = split(/\s+/, $res->status_line, 2); - # read body - $len = 0; - do { - $len = read($sock, my $buf, 8192); - $$resbody .= $buf if ($resbody); - } while ($len); + $$resbody .= $res->content; warn "## response body:\n$$resbody\n" if ($resbody && $self->{debug});