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!