#!/usr/bin/perl
##########################################################################
# get a URL content (html or otherwise) from a host server
# optionally snip a piece of the returned content
# Receives the url requested
# the case sensitive string to search for (optional)
# the offset from the beginning of the string (optional)
# the length of the snipet (optional)
# Returns the data requested, or an error message
##########################################################################
use CGI "param";
$requestURL = $ARGV[0] || param(url);
$searchFor = $ARGV[1] || param(search);
$offset = $ARGV[2] || param("offset");
$length = $ARGV[3] || param("length");
if ($requestURL eq '') {
print ("Usage: $0 requestedURL [searchString offset length] \n");
exit(-1);
}
if ($searchFor && ($offset == '' || $length == '')) {
print ("Usage: $0 requestedURL [searchString offset length] \n");
exit(-2);
}
$data = getURL($requestURL);
if ($searchFor) {
$data = snip(\$data, $searchFor, $offset, $length);
}
print "Content-type: text/html \n";
print "\n";
print "
\n";
print " The Extract is....
\n";
print "$data \n";
############################################################################
# Get an HTML page or other URL from the web
# Receives the url requested
# Returns the data received or an error message
############################################################################
sub getURL {
my $url = $_[0]; # get requested URL from @_
$url = "http://" . $url; # prepend "http://"
use LWP::UserAgent; # use LWP UserAgent method
$browser = new LWP::UserAgent; # create a new user agent
my $req = new HTTP::Request(GET => $url); # create a request
my $resp = $browser->request($req); # send req. & get response
($resp->is_success)
? return($resp->content)
: return('Bad request, or URL not found');
}
########################################################################
# Snip a piece of data from large data
# Receives a 'reference' to a large data string
# a string to search for
# an offset from the beginning of the search string
# the length of the returned snipped
# Returns the data snippet
########################################################################
sub snip {
my $dataref = $_[0]; # get the reference to large data
my $find = $_[1]; # get the search string
my $from = $_[2]; # get the offset + or -
my $lgth = $_[3]; # get the length of the snipet
$foundAt = index($$dataref, $find); # search for $find in $data
if ($foundAt == -1) {
return("String not found");
}
my $snipet = substr($$dataref, $foundAt+$from, $lgth); # snip it
return $snipet;
}