After going through a couple of books and websites,
I have been able to build a simple perl web server
just with GET/POST methods. I could easily now
integrate it with another perl application.

Thanks for all your help.

Samy Rengasamy.


Following is the code if any of you want to look at:

#!/usr/bin/perl
use IO::Socket;
use POSIX 'WNOHANG';

push (@INC, 'pwd');

# signal handler for child die events
$SIG{CHLD} = sub { while ( waitpid(-1,WNOHANG)>0 ) { } };
    

use constant PORT => 7007;
my $listen_socket = IO::Socket::INET->new(LocalPort => PORT,
                                      Listen    => 1,
                                      Proto     => 'tcp',
                                      Reuse     => 1);
die "Can't create a listening socket: $@" unless $listen_socket;

warn "Listening for connections...\n";

while (my $connection = $listen_socket->accept) {
      interact($connection);
      $connection->close;
}

sub interact {
    my $handle = shift;
    $| = 0;
    $cnt = 1;

LOOP:
    while (($clientData = $handle->getline)) { 
        if ($clientData eq "\r\n") {
            if ($action eq "POST") {
                $clientData = $handle->getline;
                print "$cnt : $len --> $clientData";
                parseData($clientData);
            }
            respondToClient($handle, $action, %keyValuePairs);
            last LOOP;
        }
        if ($cnt == 1) {
            @words = split(" ", $clientData);
            $action = $words[0];
        }
        else {
            ($key, $value) = split(":", $clientData);
            $keyValuePairs{$key} = $value;
        }
        $len = length($clientData);
        print "$cnt : $len --> $clientData";
        $cnt++;
    }

    $handle->close();

}


sub respondToClient($$%) {
    my ($handle, $action, %keyValuePairs) = @_;

    generateHtml("LOGON");

    print $handle <<ServerResponse;
HTTP/1.0 200 OK
Date: Wed, 31 Jul 2002 14:06:11 GMT
Server: Apache/1.1.1
Content-type: text/html
Content-length: 327
Connection: Keep-Alive
Last-modified: Wed, 31 Jul 2002 14:06:11 GMT

ServerResponse

    open(HTML, "c:\\temp\\x3270c.html");
     
    while (<HTML>) {
        print $handle $_;
    }

    close(HTML);


}


sub generateHtml($) {
    my $panel = shift;
     
    open(HTML, ">c:\\temp\\x3270c.html");

    print HTML <<HTMLData;
<HTML>
<PRE>
<FORM NAME="LOGON" METHOD="POST" ACTION="/index.html">
   Enter Your User ID : <input type="Text" name="userid" size="8"
maxlength="8"> 
   Enter Password     : <input type="Text" name="password" size="8"
maxlength="8"> 

   <input type="Submit" name="submit" value="OK">

   


</FORM>
</PRE>
</HTML>
HTMLData

    close(HTML);

}


sub parseData($) {
    my $formDataString = shift;

    @pairs = split(/[&;]/, $formDataString);            # split on "&" and
";"
 
    foreach $pair (@pairs) {                            # one at a time
      ($key, $value) = split (/=/, $pair);              # split on "="
      $key =~ tr/+/ /;                                  # trans "+" to space
      $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;  # Hex to
ASCII
 
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $value =~ s/<!--(.|\n)*-->//g;                    # no server side
includes
 
      $exp = $_[0];
      $exp2 = $exp % 2;                                 # rightmost bit
      if ($value eq '') {
        if ($exp2 == 1) { $value = $ENV{$key}; }
      }
      $exp = int($exp / 2);                             # strip rightmost
bit
      $exp2 = $exp % 2;
      if ($value eq '') {
        if ($exp2 == 1) { $value = $date_time{$key}; }
      }

      print "names : $key <==> $value\n";

      push(@formData, $key, $value);
      if ($formData{$key}) {                            # already known key
        $formData{$key} .= "$value";                    # add another value
      } else {
        $formData{$key} = $value;
      }
    }
 
}

______________________________________________________________________
Visit http://www.kitchenmaker.com for preethi mixie and santha grinder

Reply via email to