Forum Moderators: coopster & phranque

Message Too Old, No Replies

Problem with Perl script in folder other than root

         

smallcompany

5:56 am on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have a Perl script that parses all HTML files within my site. If I keep the script in the root of my site, in .htaccess I would have these three lines to make it work:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ parser.cgi?file=$1 [QSA,L]

Now I have one of the sites being run on a VPS plan where the hosting company keeps CGI scripts in a separate folder. I had no problem with adjusting the path to the script itself in order to call it for parsing:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ /var/www/cgi-bin/parser.cgi?file=$1 [QSA,L]

.htaccess file is in the root of the site which is in /var/www/html/

Now it happens that parsing Perl script cannot find any of the requested files except the home page, so 404 happens on any but home page access (no index.html, but domain name only).

Here is the part of the script that takes care of file “slurp”.


#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use CGI qw/:cgi/;
use Data::Dumper;
use HTML::LinkExtor;
use URI;
my $q = new CGI;
my %vars = $q->Vars();
my $file = $vars{'file'};
delete $vars{'file'};
my $content = slurp("$file.html");

...then some additional code that works with variables, and then...

print $q->header(), $content;

This line above is the output after script does the work.

I wonder if I need to declare the path to that “file” variable? If true, how? The reason I think this way is the fact that the script works just fine if it’s in the site’s root directory on any shared hosting plan.

Thanks

phranque

9:36 am on May 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



yes - the first fix i would try is an absolute file path.
something like this:
my $filepath = '/var/www/html/' . $vars{'file'} . '.html';
my $content = slurp("$filepath");

smallcompany

5:23 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Didn't do it.

I played with the line that would incorporate the absolute path with no luck.

Among many I tried this:

my $filepath = '/var/www/html/' . $file . '.html';

Would this be a wrong syntax?

smallcompany

7:09 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh my, why can't you simply pour the programming and syntax knowledge into my head - I made it to work! Yet, no-knowledge pain hurts like a cancer as I could use was my own logics.

my $filepath = '/var/www/html/' . $file;
my $content = slurp("$filepath.html");

Thank for the hint!

chorny

6:42 pm on May 30, 2008 (gmt 0)

10+ Year Member



1. add "use strict;use warnings;use diagnostics;" at start of your code

2. for 'slurp' you need to install and use File::Slurp

smallcompany

4:53 pm on May 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



2. for 'slurp' you need to install and use File::Slurp

The script works fine without this if everything is in root folder.

Would you say that File::Slurp is necessary if you use slurp in your code?

Thanks

chorny

6:54 pm on May 31, 2008 (gmt 0)

10+ Year Member


Yes, use File::Slurp qw/slurp/; is necessary. Also you need to check $file for path symbols for security. Activating taint mode also would be good for security.

smallcompany

4:36 am on Jun 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



But I don't have File::Slurp in the code and the script works just fine.

edit: sorry, first post said DO HAVE

chorny

9:02 am on Jun 1, 2008 (gmt 0)

10+ Year Member



Then there is sub slurp {..} defined in your code.

phranque

10:09 am on Jun 1, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



yes smallcompany - i must admit i am also puzzled by how/why your code works as shown withut and external package.
slurp is not a built-in perl function.

smallcompany

4:41 pm on Jun 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



sub slurp

sub slurp {
local $/ = undef;

open my $fh, $_[0] or do404();
my $slurp = <$fh>;
return $slurp;
}

Would this cover what File::Slurp is for?

chorny

5:47 pm on Jun 1, 2008 (gmt 0)

10+ Year Member



yes

smallcompany

5:59 pm on Jun 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So the only reason a coder would put it in in this way was a need for that 404 hack, I guess. I remember my custom 404 did not work properly when he created it at first time.

The script is all about parsing the whole HTML and inserting variables to the end of URLs so they get carried over while you browse through the site.

Thanks very much for clarification.