JonathanBrown
IS-IT--Management
I have a little mini-perl webserver which calls http_handler.pl which has some code to take form input.
For this part in http_handler:
print $fh '<FORM action="/hello_post.pl" method="POST">First Name: <input type="text" name="first_name"> <br> Last Name: <input type="text" name="last_name"> <input type="submit" value="Submit"></FORM>';
.... how can I have the user input pass to the hello_post.pl subroutine ? I'd have to require hello_post.pl (in either http_handler.pl or pl-websrv.pl ?), but then how would I change that print statement to have the form input just passed to the hello_post.pl subroutine and then displayed on the page?
What I actually want to end up doing is different then just inputting/displaying back names, but I am just cobbling together others' code to see if I can get the concept of this down... Any help would be great! Thanks!
Here are the 3 files (pl-websrv.pl, http_handler.pl, and hello_post.pl)
pl-websrv.pl:
##CUT##
use strict;
use warnings;
use Socket;
use IO::Select;
use threads;
use threads::shared;
$| = 1;
# The following variables should be set within init_webserver_extension
use vars qw/
$port_listen
/;
require "http_handler.pl";
init_webserver_extension();
local *S;
socket (S, PF_INET , SOCK_STREAM , getprotobyname('tcp')) or die "couldn't open socket: $!";
setsockopt (S, SOL_SOCKET, SO_REUSEADDR, 1);
bind (S, sockaddr_in($port_listen, INADDR_ANY));
listen (S, 5) or die "don't hear anything: $!";
my $ss = IO::Select->new();
$ss -> add (*S);
while(1) {
my @connections_pending = $ss->can_read();
foreach (@connections_pending) {
my $fh;
my $remote = accept($fh, $_);
my($port,$iaddr) = sockaddr_in($remote);
my $peeraddress = inet_ntoa($iaddr);
my $t = threads->create(\&new_connection, $fh);
$t->detach();
}
}
sub extract_vars {
my $line = shift;
my %vars;
foreach my $part (split '&', $line) {
$part =~ /^(.*)=(.*)$/;
my $n = $1;
my $v = $2;
$n =~ s/%(..)/chr(hex($1))/eg;
$v =~ s/%(..)/chr(hex($1))/eg;
$vars{$n}=$v;
}
return \%vars;
}
sub new_connection {
my $fh = shift;
binmode $fh;
my %req;
$req{HEADER}={};
my $request_line = <$fh>;
my $first_line = "";
while ($request_line ne "\r\n") {
unless ($request_line) {
close $fh;
}
chomp $request_line;
unless ($first_line) {
$first_line = $request_line;
my @parts = split(" ", $first_line);
if (@parts != 3) {
close $fh;
}
$req{METHOD} = $parts[0];
$req{OBJECT} = $parts[1];
}
else {
my ($name, $value) = split(": ", $request_line);
$name = lc $name;
$req{HEADER}{$name} = $value;
}
$request_line = <$fh>;
}
http_request_handler($fh, \%req);
close $fh;
}
##CUT##
http_handler.pl:
##CUT##
sub http_request_handler {
my $fh = shift;
my $req_ = shift;
my %req = %$req_;
my %header = %{$req{HEADER}};
print $fh '<FORM action="/hello_post.pl" method="POST">First Name: <input type="text" name="first_name"> <br> Last Name: <input type="text" name="last_name"> <input type="submit" value="Submit"></FORM>';
sub init_webserver_extension {
$port_listen = 8888;
}
1;
##CUT##
hello_post.pl:
##CUT##
sub hello_post {
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}else {
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$first_name = $FORM{first_name};
$last_name = $FORM{last_name};
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Hello - Second CGI Program</title>";
print "</head>";
print "<body>";
print "<h2>Hello $first_name $last_name - Second CGI Program</h2>";
print "</body>";
print "</html>";
}
1;
##CUT##
For this part in http_handler:
print $fh '<FORM action="/hello_post.pl" method="POST">First Name: <input type="text" name="first_name"> <br> Last Name: <input type="text" name="last_name"> <input type="submit" value="Submit"></FORM>';
.... how can I have the user input pass to the hello_post.pl subroutine ? I'd have to require hello_post.pl (in either http_handler.pl or pl-websrv.pl ?), but then how would I change that print statement to have the form input just passed to the hello_post.pl subroutine and then displayed on the page?
What I actually want to end up doing is different then just inputting/displaying back names, but I am just cobbling together others' code to see if I can get the concept of this down... Any help would be great! Thanks!
Here are the 3 files (pl-websrv.pl, http_handler.pl, and hello_post.pl)
pl-websrv.pl:
##CUT##
use strict;
use warnings;
use Socket;
use IO::Select;
use threads;
use threads::shared;
$| = 1;
# The following variables should be set within init_webserver_extension
use vars qw/
$port_listen
/;
require "http_handler.pl";
init_webserver_extension();
local *S;
socket (S, PF_INET , SOCK_STREAM , getprotobyname('tcp')) or die "couldn't open socket: $!";
setsockopt (S, SOL_SOCKET, SO_REUSEADDR, 1);
bind (S, sockaddr_in($port_listen, INADDR_ANY));
listen (S, 5) or die "don't hear anything: $!";
my $ss = IO::Select->new();
$ss -> add (*S);
while(1) {
my @connections_pending = $ss->can_read();
foreach (@connections_pending) {
my $fh;
my $remote = accept($fh, $_);
my($port,$iaddr) = sockaddr_in($remote);
my $peeraddress = inet_ntoa($iaddr);
my $t = threads->create(\&new_connection, $fh);
$t->detach();
}
}
sub extract_vars {
my $line = shift;
my %vars;
foreach my $part (split '&', $line) {
$part =~ /^(.*)=(.*)$/;
my $n = $1;
my $v = $2;
$n =~ s/%(..)/chr(hex($1))/eg;
$v =~ s/%(..)/chr(hex($1))/eg;
$vars{$n}=$v;
}
return \%vars;
}
sub new_connection {
my $fh = shift;
binmode $fh;
my %req;
$req{HEADER}={};
my $request_line = <$fh>;
my $first_line = "";
while ($request_line ne "\r\n") {
unless ($request_line) {
close $fh;
}
chomp $request_line;
unless ($first_line) {
$first_line = $request_line;
my @parts = split(" ", $first_line);
if (@parts != 3) {
close $fh;
}
$req{METHOD} = $parts[0];
$req{OBJECT} = $parts[1];
}
else {
my ($name, $value) = split(": ", $request_line);
$name = lc $name;
$req{HEADER}{$name} = $value;
}
$request_line = <$fh>;
}
http_request_handler($fh, \%req);
close $fh;
}
##CUT##
http_handler.pl:
##CUT##
sub http_request_handler {
my $fh = shift;
my $req_ = shift;
my %req = %$req_;
my %header = %{$req{HEADER}};
print $fh '<FORM action="/hello_post.pl" method="POST">First Name: <input type="text" name="first_name"> <br> Last Name: <input type="text" name="last_name"> <input type="submit" value="Submit"></FORM>';
sub init_webserver_extension {
$port_listen = 8888;
}
1;
##CUT##
hello_post.pl:
##CUT##
sub hello_post {
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}else {
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$first_name = $FORM{first_name};
$last_name = $FORM{last_name};
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Hello - Second CGI Program</title>";
print "</head>";
print "<body>";
print "<h2>Hello $first_name $last_name - Second CGI Program</h2>";
print "</body>";
print "</html>";
}
1;
##CUT##