Perl script for retrieving a URL webpage after creating a socket

Perl script for retrieving a URL webpage after creating a socket

am 13.07.2005 14:14:29 von amarjeet_md

Hi,
I have written a Perl script which is used to retrive a URL after
creating a socket.But I am unable to execute the script as I get an
error message "blind at HTTPSO~1.PL line 12".I have tried to debug this
error message since the past 3 days but I am unable to debug the same.

The script which I have used is:
use Socket;
use FileHandle;

$test = 0;

&getinput;
my $port = 3128;
my $host = ('localhost');
my $hisiaddr = inet_aton($host) or die "unknown host";
my $hispaddr = sockaddr_in($port, $hisiaddr);
socket (SOCKET , PF_INET, SOCK_STREAM, $proto) or die "socket";
connect (SOCKET, $hispaddr) or die "blind";
SOCKET -> autoflush(1);
print SOCKET "GET $form{url} HTTP/1.0\n\n";
my $incoming = "";
my @headers;
my $read = 0;
foreach () {
if ($read == -1) {
$incoming .= $_;
} else {
push (@headers,$_);
}
if ($_ =~ /^\s*\n\s*$/) {$read = -1;}
}
close SOCKET;
print "Content-Type: text/html\n\n";
if ($headers[0] !~ /.*200.*/){exit 0;}
if ($test) {exit 0};
print $incoming;
sub getinput{
$_ = $ENV{'QUERY_STRING'};
my @pairs = split(/&/, $_);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$form{$name} = $value;
}
}


Any help regarding this issue would be greatly appreciated.

Thanks,
Amarjeet

Re: Perl script for retrieving a URL webpage after creating a socket

am 15.07.2005 14:41:23 von Paul Lalli

amarjeet_md@yahoo.com wrote:
> Hi,
> I have written a Perl script which is used to retrive a URL after
> creating a socket.

Are you sure this is something you really need to be doing? There are
many modules that take care of the lowlevel stuff for you. See, for
example, the LWP family of modules.

> But I am unable to execute the script as I get an
> error message "blind at HTTPSO~1.PL line 12".I have tried to debug this
> error message since the past 3 days but I am unable to debug the same.

.... how, exactly, did you attempt to debug this?

>
> The script which I have used is:
> use Socket;
> use FileHandle;

You have forgotten
use strct;
use warnings;

These two tools are invaluable to writing and debugging Perl programs.

>
> $test = 0;
>
> &getinput;

Please do not call subroutines with the & prefix, unless you know what
side effects that causes, and desire those side effects. (Here, you
don't).

> my $port = 3128;
> my $host = ('localhost');
> my $hisiaddr = inet_aton($host) or die "unknown host";
> my $hispaddr = sockaddr_in($port, $hisiaddr);
> socket (SOCKET , PF_INET, SOCK_STREAM, $proto) or die "socket";
> connect (SOCKET, $hispaddr) or die "blind";

Why aren't you printing out the error message for any of these calls?
Add the $! variable to the die message. I would think that would be
the *first* step of debugging. Otherwise, how could you possibly know
why connect() failed?


> SOCKET -> autoflush(1);
> print SOCKET "GET $form{url} HTTP/1.0\n\n";
> my $incoming = "";
> my @headers;
> my $read = 0;
> foreach () {
> if ($read == -1) {
> $incoming .= $_;
> } else {
> push (@headers,$_);
> }
> if ($_ =~ /^\s*\n\s*$/) {$read = -1;}
> }
> close SOCKET;
> print "Content-Type: text/html\n\n";
> if ($headers[0] !~ /.*200.*/){exit 0;}
> if ($test) {exit 0};
> print $incoming;
> sub getinput{
> $_ = $ENV{'QUERY_STRING'};
> my @pairs = split(/&/, $_);
> foreach $pair (@pairs)
> {
> ($name, $value) = split(/=/, $pair);
> $value =~ tr/+/ /;
> $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
> $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
> $form{$name} = $value;
> }

Ugggh. Please don't do this. Self-rolled query string parsing is
almost never written correctly enough. Please use one of the standard
modules that does this for you.


> }
>
> Any help regarding this issue would be greatly appreciated.

Without a doubt, the biggest help would be asking Perl *why* the call
to connect dies.

Paul Lalli