Incorrect URL given.

Hi guys

I have a problem with the following script.

The search form is searcher.html and is:

==============================================================  searcher.html
<form action="/cgi-bin/searching.cgi" method="post">
<input type="text" name="query" size="50" />
<input type="submit" />
</form>

============================================================== searching.cgi
#!c:/perl/bin/perl.exe           
use strict;                                                      
use CGI qw(:standard);            
use File::Find;
      
my $query = param("query");

print header();
print start_html();
print "\n<p>For the query $query, these results were found:</p>\n<ol>\n";

undef $/;

find      ( sub      {
       return if ($_ =~ /^\./);                        
       return unless ($_ =~ /\.txt/i);    
       stat $File::Find::name;
       return if -d;
       return unless -r;
                                     
       open(FILE, "< $File::Find::name") or return;
      my $string = <FILE>;
      close (FILE);
                                    
      return unless ($string =~ /\Q$query\E/i);
                                    
      my $page_title = $_;            
       if ($string =~ /<title>(.*?)<\/title>/is)      {
           $page_title = $1;
       }
       print "<li><a href=\"$File::Find::name\">$page_title</a></li>\n"; #Here we output our link.
      },
        '/wamp/'
        );
                    
        print "</ol>\n";
      print end_html();

================================================================

What happens is that the script returns the files with the search criteria in correctly. For example,
If i enter on the form 'test' for the text to find.
The problem is when I click on one of thos results, it goes to the wrong url.

If one of the results is listed as:

test.txt

And I click on this result in the browser, it takes me to:

http://localhost/wamp/www/hi/test.txt

which give me 'Web page unavailable".

The correct path should be:

http://localhost/hi/test.txt

in which case, it is displayed correctly.


------------------------------------------------------
My document root is c:\wamp\www
Search.html is in c:\wamp\www
Searching.cgi is in c:\wamp\apache2\cgi-bin

Any help appreciated.
LVL 1
Simon336697Asked:
Who is Participating?
 
ahoffmannConnect With a Mentor Commented:
($target=$File::Find::name)=~s#/(wamp/www|cgi-bin)##g;
print "<li><a href=\"$target\">$page_title</a></li>\n";

# depends on what you mean by "easier": easier to understand, easier to read, easier to execute, ...
0
 
Perl_DiverConnect With a Mentor Commented:

    * $File::Find::dir is the current directory name,
    * $_ is the current filename within that directory
    * $File::Find::name is the complete pathname to the file.

instead of:

print "<li><a href=\"$File::Find::name\">$page_title</a></li>\n"; #Here we output our link.

you could:

print "<li><a href=\"http://localhost/hi/$_\">$page_title</a></li>\n"; #Here we output our link.
0
 
Simon336697Author Commented:
Hi Perl Diver!

Thank you for that!
It seems to work, and returns entries in sub directories, but I dont know how to get the right href when clicking on a result, where the result is located in a subdirectory. It still points to the current directory.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
ahoffmannCommented:
my $target=$File::Find::name;
$target =~ s#/wamp/www/##;
print "<li><a href=\"$target\">$page_title</a></li>\n";
0
 
Simon336697Author Commented:
It seems like if I do the following..

       print "<li><a href=\"http://localhost/$_\">$page_title</a></li>\n"; #Here we output our link.
      },
        'c:\\wamp\\www\\'
        );

It returns results in all folders starting at c:\wamp\www, and subdirectories.

However the output in the web page for the results, if I click on a file returned that is in a subdirectory eg. c:\wamp\www\hi\hi\testing.txt

The link says it is in http://localhost/testing.txt,
instead of what it should be, which is:
http://localhost/hi/hi/testing.txt

Im getting really confused here.
Either Im just a bit thick or this is tricky or both.

It will return only entries that are in the c:\wamp\www\hi folder, but not subfolders.
0
 
Simon336697Author Commented:
Hi ahoff!
Thank you mate for this.
I tried your change...

my $target=$File::Find::name;
$target =~ s#/wamp/www/##;
print "<li><a href=\"$target\">$page_title</a></li>\n";
      },
     '/wamp/www/'
        );
                   
        print "</ol>\n";
      print end_html();

===================================
Results are returned as follows..

no.txt
nnn.txt
test.txt
ol.txt

If I hover over any of the above, they point to:

http://localhost/cgi-bin/hi/nnn.txt                (should be http://localhost/hi/nnn.txt)
http://localhost/cgi-bin/hi/god/ol.txt            (should be http://localhost/hi/god/ol.txt)
http://localhost/cgi-bin/no.txt                     (should be http://localhost//no.txt 

The location of the cgi script being run here on disk is c:\wamp\apache2\cgi-bin.
Somehow it looks like its putting in the current location of where the script is being run from??
0
 
ahoffmannCommented:
my $target=$File::Find::name;
$target =~ s#/wamp/www/##;
$target =~ s#/cgi-bin##;
print "<li><a href=\"$target\">$page_title</a></li>\n";


# to be improved in many ways
0
 
Simon336697Author Commented:
Thanks ahoff for all your help.

I think this question is just really messy, and I might just allocate points now.
There has to be an easier way to do this I think :>)
0
 
Simon336697Author Commented:
ahoff!
Youre the best......it WORKS!!

Here is the code that does it...thank you.

================================================== search.html
<form action="/cgi-bin/searchcgi.cgi" method="post">
<input type="text" name="query" size="50" />
<input type="submit" />
</form>
=================================================== search.cgi
#!c:/perl/bin/perl.exe          
use strict;                                                      
use CGI qw(:standard);            
use File::Find;
     
my $query = param("query");

print header();
print start_html();
print "\n<p>For the query $query, these results were found:</p>\n<ol>\n";

undef $/;

find      ( sub      {
       return if ($_ =~ /^\./);                        
       return unless ($_ =~ /\.txt/i || /\.xls/i);    
       stat $File::Find::name;
       return if -d;
       return unless -r;
                                     
       open(FILE, "< $File::Find::name") or return;
      my $string = <FILE>;
      close (FILE);
                                   
      return unless ($string =~ /\Q$query\E/i);
                                   
      my $page_title = $_;            
       if ($string =~ /<title>(.*?)<\/title>/is)      {
           $page_title = $1;
       }
       
       my $target=$File::Find::name;

($target=$File::Find::name)=~s#/(wamp/www|cgi-bin)##g;
#print "<li><a href=\"$target\">$page_title</a></li>\n";
print "<li><a href=\"$target\">$page_title</a></li> $File::Find::dir\n";
      },
        '/wamp/www/'
        );
                   
        print "</ol>\n";
      print end_html();
============================================================ End of file.
Usage example:
http://localhost/search.html
In the text box, enter "test" to search for...Submit Query..

For the query test, these results were found:

nnn.txt /wamp/www
no.txt /wamp/www
test.txt /wamp/www
nnn.txt /wamp/www/hi
test.txt /wamp/www/hi
New Microsoft Excel Worksheet.xls /wamp/www/hi/New Folder/New Folder/yeppppppy
nnn.txt /wamp/www/hi/gothere

Thanks so much ahoff!
So in the below, you set the filename path to:
($target=$File::Find::name)=~s#/(wamp/www|cgi-bin)##g;
Are you substituting on the right hand side?

0
 
Simon336697Author Commented:
Sorry in the above, in the search.html,
<form action="/cgi-bin/search.cgi" method="post">

Last question:
Is there any way to slightly modify this, so that instead of returning the files that contain the query string entered in the text box in the form, it returns the FILENAMES that have the text entered in the query string as part of their filename.
So, for example:

Search (form)                                       Results (these files have the search as part of their filename)
Search for "test" --------------------->   test.txt
                                                              thisisatest.xls
                                                              justtesting.doc
                                                             testit.txt
At the moment,
Search (form)                                       Results (these files have the search IN the file itself)
Search for "test" --------------------->   nn.txt
                                                             ooo.txt
0
 
ahoffmannCommented:
> Are you substituting on the right hand side?
yes (using the left), then assign to the left

don't understand you last question, but it's either a simple print somewhere, or please open a new question with the code not working
0
 
Simon336697Author Commented:
Sorry ahoff!
Ill post another question.
And thankU for your help on this one :>)
0
 
Simon336697Author Commented:
ThankU Perl Diver and ahoff...you guys are the best.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.