I won't look at this to closely, the first glance raises enough questions.
So, a few points, do you really intend an exit with every file served?
Do you really want your handler to fetch a URL, as a proxy, or should you
instead redirect to the URL with the query string attached?
Consider the following:
1) use IO::File for your filehandles, at least it's been good to me.
2) return status codes and set http headers
3) instead of just printing "No good" how about "No good: $requestpage"
or something else informative about the failure
Finally, couldn't this be expressed as a rewrite rule?
Meanwhile, back at the ranch...
>
> Hi, I need a script that gets a url, and if it doesn't have a
> query_string, adds one to the url and requests it again. Then, when it
> comes with a query, reads the file, print it and exit.
>
> The matter is that the script enters a loop as it is requesting pages
> that trigger the script again. But i've been doing tests with this code
> and the second time is called, it doesn't print anything and I dunno why.
> Here's the code:
>
> sub handler {
>
> my $r = shift;
> my $uri = $r->parsed_uri;
> my $query = $uri->query;
> my $path = $uri->path;
> my $root = 'http://undertow';
> # Add a query to the page to request
> my $requestpage = $root . $path . "?hello";
> my @pathinfo = split ('/', $path);
> my $page = $pathinfo[$#pathinfo];
>
> if (defined $query) {
> #r->print("there's a query: $query");
> # Open the file to send
> open (FILE, '/var/www/$path');
> my @data = <FILE>;
> close(FILE);
> # Print it and exit
> $r->print(@data);
> exit;
> } else { # The first request enters here
> my $request = HTTP::Request->new ("GET" => $requestpage);
> my $response = $ua->request($request);
> $r->content_type("text/html");
> $r->send_http_header;
> if ($response->is_success) {
> $r->print($response->content);
> } else {
> $r->print("No good");
> }
>
> }
> }
> 1;
>
> If we first request index.html, it goes into else stuff and requests the
> page as index.html?hello, so it goes through the handler again and should
> enter the 'if (defined $query)' stuff. Well it actually does but it
> doesn't return anything: just "No good". ( I've tried printing
> $r->print("Works\n") instead of $r->print(@data) but it doesn't work
> neither ). I don't know what's going wrong here and so I'm asking you.
>
> TIA for all. Bye!
>
>
>
>
>
>
--
Salon Internet http://www.salon.com/
HTTP mechanic, Perl diver, Mebwaster, Some of the above
Ian Kallen <[EMAIL PROTECTED]> / AIM: iankallen / Fax: (561) 619-0995