i am trying to write a mini proxy server to authenticate and run some other
checking on http requests before they are passed to a squid server.

I have got the following program which works for very basic requests but
cannot deal with forms or anything complex.

I will detail the problems after the snip:

<snip>

#!/usr/bin/perl

# Standard common-sense modules
use strict;
use warnings;

# Modules for getting http pages
use LWP::UserAgent;
use URI::URL;
use HTTP::Request;
use HTTP::Headers;

# Modules for dealing with the sockets
use IO::Socket;
use Net::hostent;

# Modules for DNS lookups and mysql connection
use Net::MySQL;
use Net::DNS;

# The port the proxy server will run on
my $PORT = 8850;

# urn on autoflushing
$| = 1;

# Get the config file
our %CONFIG;
require './proxy.conf';

# Manage correct internet line endings
my $EOL1 = "\012";
my $EOL2 = "\015";

# Do all the logging in one function
sub logmsg
{
        my ($mesg) = @_;
        my $date = `date`;
        chomp($date);
        print LOG "[$date]: $mesg\n";
        print "[$date]: $mesg\n";
}

my $mysql;

# Connect to the mysql database for authentication if specified in the
# config file
if ($CONFIG{'use_auth_mysql'} eq 'true')
{
        my $mysql = Net::MySQL->new(
                                hostname => $CONFIG{'mysql_host'},
                                database => $CONFIG{'mysql_name'},
                                user     => $CONFIG{'mysql_user'},
                                password => $CONFIG{'mysql_pass'}
                                );
}

# Open the listening socket
my $server = IO::Socket::INET->new(
                                Proto => 'tcp',
                                LocalPort => $PORT,
                                Listen=> SOMAXCONN,
                                Reuse => 1
                                );

# Die unless the socket is ok
die "can't setup server: $!\n" unless $server;

# Open a logfile
open (LOG, ">>$CONFIG{'logfile'}") || die "couldn't open file, $!\n";

# Specify SIG INT sub
$SIG{'INT'} = 'shutdown';
sub shutdown
{
        logmsg "SIGINT caught";
        if ($CONFIG{'use_auth_mysql'} eq 'true')
        {
                logmsg "Closing connection to database...";
                $mysql->close;
        }
        logmsg "Closing client connections...";
        $server->close;
        logmsg "Closing logfile...";
        close LOG;
        exit;
}

# Print message to show startup ok
logmsg ("Server accepting clients on $PORT");

# Fork to allow multiple connections
fork();

# For every client connection....
while (my $client = $server->accept())
{
        # Force autoflushing
        $client->autoflush(1);

        # Get the remote client ip/hostname
        my $hostinfo = gethostbyaddr($client->peeraddr);
        my $remote_info = $hostinfo->name || $client->peerhost;

        my ($url, @headers);

        # Get the request from the client...
        while (my $input = <$client>)
        {
                if ($input =~ m/^GET /)
                {
                        # Get the requested url from the supplied headers
                        $input =~ s/GET //;
                        $input =~ s/ HTTP\/1.1//;
                        $input =~ s/$EOL1//;
                        $input =~ s/$EOL2//;
                        $url = $input;
                }
                else
                {
                        # Add the input to the list of headers
                        push (@headers, $input);
                        if ($input =~ m/Cookie/)
                        {
                                last;
                        }
                        print "$input";
                }
        }

        print @headers;

        my $hdrs = new HTTP::Headers(@headers);

        my $url2 = new URI::URL($url);
        my $req = new HTTP::Request('GET', $url, $hdrs);
        my $ua = new LWP::UserAgent;
        my $resp = $ua->request($req);


        if ($resp->is_success)
        {
                my $results = $resp->content;
                print $client "$results";
                chomp($url);
                logmsg ("$url - $remote_info - ok");
        }
        else
        {
                chomp($url);
                logmsg ("$url - $remote_info - ".$resp->message);
        }
}

</snip>

I believe the problem is recieving the headers. In the while() loop that
reads the headers from the browser, the browser never gives a ^D so the
while loop hangs.

Any suggestion on how to cure this or any other ideas? 

cheers

Mat Harris                      OpenGPG Public Key ID: C37D57D9
[EMAIL PROTECTED]        www.genestate.com       

Attachment: msg38670/pgp00000.pgp
Description: PGP signature

Reply via email to