Current location: Hot Scripts Forums » Programming Languages » Perl » maintaing state with a server


maintaing state with a server

Reply
  #1 (permalink)  
Old 12-09-05, 07:23 AM
Betty Betty is offline
New Member
 
Join Date: Dec 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
maintaing state with a server

Hi especially to anyone and everyone who is able to shed light and ultimately assist me in solving my problems.

I am a "newbie", Urm and I am having trouble. I am tying to test a few scripts from a book and have modified them slighly before modifying them significantly to match my program needs. below i have pasted the 3 cgi progs and 2 html script that I am testing. I start the interests html and the get the following error:

CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers.

Your help is more than appreciated!


cookie_server.pl script

#!/usr/bin/perl

require "sockets.pl";

srand (time|$$);

$HTTP_server = "128.197.27.7";
$separator = "\034";
$expire_time = 15 * 60;

%DATA = ();
$max_cookies = 10;
$no_cookies = 0;

$error = 500;
$success = 200;

( ($port) = &listen_to_port (SOCKET) ) || die "Cannot create socket.", "\n";
print "The Cookie Server is running on port number: $port", "\n";


while (1) {
( ($ip_name, $ip_address) = &accept_connection (COOKIE, SOCKET) )
|| die "Could not accept connection.", "\n";

select (COOKIE);
$cookie = undef;

if ($ip_address ne $HTTP_server) {
&print_status ($error, "You are not allowed to connect to server.");
} else {
&print_status ($success, "Welcome from $ip_name ($ip_address)");

while (<COOKIE>) {
s/[\000-\037]//g;
s/^\s*(.*)\b\s*/$1/;

if ( ($remote_address) = /^new\s*(\S+)$/) {
if ($cookie) {
&print_status ($error, "You already have a cookie!");
} else {
if ($no_cookies >= $max_cookies) {
&print_status ($error, "Cookie limit reached.");
} else {
do {
$cookie = &generate_new_cookie ($remote_address);
} until (!$DATA{$cookie});

$no_cookies++;
$DATA{$cookie} = join("::", $remote_address,
$cookie, time);
&print_status ($success, $cookie);
}
}

} elsif ( ($check_cookie, $remote_address) =
/^cookie\s*(\S+)\s*(\S+)/) {

if ($cookie) {
&print_status ($error, "You already specified a cookie.");
} else {
if ($DATA{$check_cookie}) {
($old_address) = split(/::/, $DATA{$check_cookie});

if ($old_address ne $remote_address) {
&print_status ($error, "Incorrect IP address.");
} else {
$cookie = $check_cookie;
&print_status ($success, "Cookie $cookie set.");
}
} else {
&print_status ($error, "Cookie does not exist.");
}
}

} elsif ( ($variable, $value) = /^(\w+)\s*=\s*(.*)$/) {
if ($cookie) {
$key = join ($separator, $cookie, $variable);
$DATA{$key} = $value;
&print_status ($success, "$variable=$value");
} else {
&print_status ($error, "You must specify a cookie.");
}

} elsif (/^list$/) {
if ($cookie) {
foreach $key (keys %DATA) {
$string = join ("", $cookie, $separator);

if ( ($variable) = $key =~ /^$string(.*)$/) {
&print_status ($success, "$variable=$DATA{$key}");
}
}
print ".", "\n";

} else {
&print_status ($error, "You don't have a cookie yet.");
}

} elsif (/^delete$/) {
if ($cookie) {
&remove_cookie ($cookie);
&print_status ($success, "Cookie $cookie deleted.");
} else {
&print_status ($error, "Select a cookie to delete.");
}

} elsif (/^exit|quit$/) {
$cookie = undef;
&print_status ($success, "Bye.");
last;
} elsif (!/^\s*$/) {
&print_status ($error, "Invalid command.");
}
}
}

&close_connection (COOKIE);

&expire_old_cookies();
}

exit(0);

sub print_status
{
local ($status, $message) = @_;

print $status, ": ", $message, "\n";
}

sub generate_new_cookie
{
local ($remote) = @_;
local ($random, $temp_address, $cookie_string, $new_cookie);

$random = rand (time);
($temp_address = $remote) =~ s/\.//g;
$cookie_string = join ("", $temp_address, time) / $random;
$new_cookie = crypt ($cookie_string, $random);

return ($new_cookie);
}

sub expire_old_cookies
{
local ($current_time, $key, $cookie_time);

$current_time = time;

foreach $key (keys %DATA) {
if ($key !~ /$separator/) {
$cookie_time = (split(/::/, $DATA{$key}))[2];
if ( $current_time >= ($cookie_time + $expire_time) ) {
&remove_cookie ($key);
}
}
}
}

sub remove_cookie
{
local ($cookie_key) = @_;
local ($key, $exact_cookie);

$exact_cookie = (split(/::/, $DATA{$cookie_key}))[1];

foreach $key (keys %DATA) {
if ($key =~ /$exact_cookie/) {
delete $DATA{$key};
}
}

$no_cookies--;
}

cookie_client.pl
#!/usr/bin/perl

require "sockets.pl";

&webmaster = "Bess Obarotimi" (bess@lordtel.com"):
$remote_address = $ENV{'REMOTE_ADDR'};

$cookie_server = "cgi.lordtel.com";
( ($cookieport) = &listen_to_port (SOCKET) ) || die "Cannot create socket.", "\n";

$document_root = "www.lordtel.com/htdocs";
$error = "Cookie Client Error";

&parse_form_data (*FORM);
$start_form = $FORM{'start'};
$next_form = $FORM{'next'};
$cookie = $FORM{'Magic_Cookie'};

if ($start_form) {
$cookie = &get_new_cookie ();
&parse_form ($start_form, $cookie);

} elsif ($next_form) {
&save_current_form ($cookie);
&parse_form ($next_form, $cookie);

} else {
if ($cookie) {
&last_form ($cookie);
} else {
&return_error (500, $error,
"You have executed this script in an invalid manner.");
}
}

exit (0);

sub open_and_check
{
local ($first_line);

&open_connection (COOKIE, $cookie_server, $cookie_port)
|| &return_error (500, $error, "Could not connect to cookie server.");

chop ($first_line = <COOKIE>);

if ($first_line !~ /^200/) {
&return_error (500, $error, "Cookie server returned an error.");
}
}

sub get_new_cookie
{
local ($cookie_line, $new_cookie);

&open_and_check ();
print COOKIE "new ", $remote_address, "\n";
chop ($cookie_line = <COOKIE>);
&close_connection (COOKIE);

if ( ($new_cookie) = $cookie_line =~ /^200: (\S+)$/) {
return ($new_cookie);
} else {
&return_error (500, $error, "New cookie was not created.");
}
}

sub parse_form
{
local ($form, $magic_cookie) = @_;
local ($path_to_form);

if ($form =~ /\.\./) {
&return_error (500, $error, "What are you trying to do?");
}

$path_to_form = join ("/", $document_root, $form);

open (FILE, "<" . $path_to_form)
|| &return_error (500, $error, "Could not open form.");

print "Content-type: text/html", "\n\n";

while (<FILE>) {
if (/-\*Cookie\*-/) {
s//$magic_cookie/g;
}
print;
}

close (FILE);
}

sub save_current_form
{
local ($magic_cookie) = @_;
local ($ignore_fields, $cookie_line, $key);

$ignore_fields = '(start|next|Magic_Cookie)';

&open_and_check ();
print COOKIE "cookie $magic_cookie $remote_address", "\n";
chop ($cookie_line = <COOKIE>);

if ($cookie_line =~ /^200/) {
foreach $key (keys %FORM) {
next if ($key =~ /\b$ignore_fields\b/o);

print COOKIE $key, "=", $FORM{$key}, "\n";
chop ($cookie_line = <COOKIE>);

if ($cookie_line !~ /^200/) {
&return_error (500, $error, "Form info. could not be stored.");
}
}
} else {
&return_error (500, $error, "The cookie could not be set.");
}

&close_connection (COOKIE);
}

sub last_form
{
local ($magic_cookie) = @_;
local ($cookie_line, $key_value, $key, $value);

&open_and_check ();
print COOKIE "cookie $magic_cookie $remote_address", "\n";
chop ($cookie_line = <COOKIE>);

if ($cookie_line =~ /^200/) {
print COOKIE "list", "\n";
&display_all_items ();

print COOKIE "delete", "\n";

} else {
&return_error (500, $error, "The cookie could not be set.");
}

&close_connection (COOKIE);
}

sub display_all_items
{
local ($key_value, $key, $value);

print "Content-type: text/html", "\n\n";
print "<HTML>", "\n";
print "<HEAD><TITLE>Summary</TITLE></HEAD>", "\n";
print "<BODY>", "\n";
print "<H1>Summary and Results</H1>", "\n";
print "Here are the items/options that you selected:", "<HR>", "\n";

while (<COOKIE>) {
chop;
last if (/^\.$/);

$key_value = (split (/\s/, $_, 2))[1];
($key, $value) = split (/=/, $key_value), "\n";

print "<B>", $key, " = ", $value, "</B>", "<BR>", "\n";
}

foreach $key (keys %FORM) {
next if ($key =~ /^Magic_Cookie$/);

print "<B>", $key, " = ", $FORM{$key}, "</B>", "<BR>", "\n";
}

print "</BODY></HTML>", "\n";
}

sub parse_form_data
{
local (*FORM_DATA) = @_;

local ($query_string, @key_value_pairs, $key_value, $key, $value);

read (STDIN, $query_string, $ENV{'CONTENT_LENGTH'});

if ($ENV{'QUERY_STRING'}) {
$query_string = join("&", $query_string, $ENV{'QUERY_STRING'});
}

@key_value_pairs = split (/&/, $query_string);

foreach $key_value (@key_value_pairs) {
($key, $value) = split (/=/, $key_value);
$key =~ tr/+/ /;
$value =~ tr/+/ /;

$key =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
$value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;

if (defined($FORM_DATA{$key})) {
$FORM_DATA{$key} = join ("\0", $FORM_DATA{$key}, $value);
} else {
$FORM_DATA{$key} = $value;
}
}
}

sub return_error
{
local ($status, $keyword, $message) = @_;

print "Content-type: text/html", "\n";
print "Status: ", $status, " ", $keyword, "\n\n";

print <<End_of_Error;

<title>CGI Program - Unexpected Error</title>
<h1>$keyword</h1>
<hr>$message</hr>
Please contact $webmaster for more information.

End_of_Error

exit(1);
}


sockets.pl
#!/usr/bin/perl

##++
## Socket Library v2.0
## Last modified: November 23, 1995
##
## Copyright (c) 1995 by Shishir Gundavaram
## All Rights Reserved
##
## E-Mail: shishir@ora.com
##
## Permission to use, copy, and distribute is hereby granted,
## providing that the above copyright notice and this permission
## appear in all copies and in supporting documentation.
##--

require "sys/socket.ph";

sub open_connection
{
local ($socket, $remote_host, $remote_service) = @_;
local ($current_host, $current_address, $remote_address,
$remote_port_number, $current_port, $remote_port, $protocol);

$current_host = &get_current_host ();
$current_address = &get_packed_address ($current_host);

$remote_address = &get_packed_address ($remote_host);
$remote_port_number = &get_port_number ($remote_service);

$current_port = &create_socket_structure (0, $current_address);
$remote_port = &create_socket_structure ($remote_port_number,
$remote_address);

$protocol = (getprotobyname ("tcp"))[2];

socket ($socket, &AF_INET, &SOCK_STREAM, $protocol) || return (0);
bind ($socket, $current_port) || return (0);
connect ($socket, $remote_port) || return (0);

&unbuffer_socket ($socket);

return (1);
}

sub listen_to_port
{
local ($socket, $service_or_number) = @_;
local ($current_host, $current_address, $protocol,
$specified_port_number, $specified_port);

$current_host = &get_current_host ();
$current_address = &get_packed_address ($current_host);

$protocol = (getprotobyname ("tcp"))[2];

if (defined ($service_or_number)) {
$specified_port_number = &get_port_number ($service_or_number);
} else {
$specified_port_number = 0;
}

$specified_port = &create_socket_structure ($specified_port_number,
$current_address);

socket ($socket, &AF_INET, &SOCK_STREAM, $protocol) || return (0);
bind ($socket, $specified_port) || return (0);
listen ($socket, 5) || return (0);

unless (defined ($service_or_number)) {
$specified_port_number = &get_socket_port ($socket);
}

return ($specified_port_number);
}

sub accept_connection
{
local ($connection, $socket) = @_;
local ($IP_number, $IP_name);

accept ($connection, $socket) || return (0);

&unbuffer_socket ($connection);

$IP_number = &where_from ($connection);
$IP_name = &IP_number_to_name ($IP_number);

return ($IP_name, $IP_number);
}

sub strip_control_chars
{
local ($input) = @_;

$input =~ s/[\000-\037]//g;
return ($input);
}

sub close_connection
{
local ($connection) = @_;

&flush_socket ($connection);
close ($connection);
}

################################################## ##########################

sub get_current_host
{
local ($host);

unless ($host = `hostname`) {
$host = `uname -n`;
}

chop ($host);

return ($host);
}

sub create_socket_structure
{
local ($port_number, $packed_address) = @_;
local ($socket_template, $structure);

$socket_template = "S n a4 x8";

$structure =
pack ($socket_template, &AF_INET, $port_number, $packed_address);

return ($structure);
}

sub get_port_number
{
local ($service_or_port) = @_;
local ($port_number);

if ($service_or_port !~ /^\d+$/) {
$port_number =
(getservbyname ($service_or_port, "tcp"))[2];
} else {
$port_number = $service_or_port;
}

return ($port_number);
}

sub get_socket_port
{
local ($socket_handle) = @_;
local ($socket_template, $socket_address, $port_number);

$socket_template = "S n a4 x8";

$socket_address = getsockname ($socket_handle);
$port_number = (unpack ($socket_template, $socket_address))[1];

return ($port_number);
}

sub unbuffer_socket
{
local ($socket) = @_;
local ($current_handle);

$current_handle = select ($socket);
$| = 1;
select ($current_handle);
}

sub flush_socket
{
local ($socket) = @_;
local ($current_handle);

##++
## Based on the code from the flush.pl library
##--

$current_handle = select ($socket);
$| = 1;
print "";
$| = 0;
select ($current_handle);
}

sub get_packed_address
{
local ($IP_name_or_number) = @_;
local ($packed_address, @decimal_address);

if ($IP_name_or_number =~ /^\d+\.{3}\d+$/) {
@decimal_address = split (/\./, $IP_name_or_number);
$packed_address = pack ("C4", @decimal_address);
} else {
$packed_address = (gethostbyname ($IP_name_or_number))[4];
}

return ($packed_address);
}

sub packed_address_to_IP_number
{
local ($packed_address) = @_;
local (@decimal_address, $IP_number);

@decimal_address = unpack ("C4", $packed_address);
$IP_number = join (".", @decimal_address);

return ($IP_number);
}

sub where_from
{
local ($socket) = @_;
local ($socket_template, $internet_address, $packed_address,
$IP_number);

$socket_template = "S n a4 x8";
$internet_address = getpeername ($socket);
$packed_address = (unpack ($socket_template, $internet_address))[2];
$IP_number = &packed_address_to_IP_number ($packed_address);

return ($IP_number);
}

sub IP_number_to_name
{
local ($IP_number) = @_;
local ($packed_IP_address, $IP_name);

$packed_IP_address = &get_packed_address ($IP_number);
($IP_name) = gethostbyaddr ($packed_IP_address, 2);

return ($IP_name);
}

sub IP_name_to_number
{
local ($IP_name) = @_;
local ($packed_IP_address, $IP_number);

$packed_IP_address = &get_packed_address ($IP_name);
$IP_number = &packed_address_to_IP_number ($packed_IP_address);

return ($IP_number);
}

1;


interests.html
<HTML>
<HEAD><TITLE>College/School Survey</TITLE></HEAD>
<BODY>
<H1>Interests</H1>
<HR>
<FORM ACTION="/cgi-bin/cookie_client.pl?next=/location.html" METHOD="POST">
<INPUT TYPE="hidden" NAME="Magic_Cookie" VALUE="-*Cookie*-">
What subject are you interested in? <BR>
<INPUT TYPE="text" NAME="subject" SIZE=40>
<P>
What extra-curricular activity do you enjoy the most? <BR>
<INPUT TYPE="text" NAME="interest" SIZE=40>
<P>
<INPUT TYPE="submit" VALUE="See Next Form!">
<INPUT TYPE="reset" VALUE="Clear the form">
</FORM>
<HR>
</BODY>
</HTML>

location.html
<HTML>
<HEAD><TITLE>College/School Survey</TITLE></HEAD>
<BODY>
<H1>Location</H1>
<HR>
<FORM ACTION="/cgi-bin/cookie_client.pl" METHOD="POST">
<INPUT TYPE="hidden" NAME="Magic_Cookie" VALUE="-*Cookie*-">
Where would you like to go to school? <BR>
<INPUT TYPE="text" NAME="city" SIZE=40>
<P>
What type of college do you prefer? <BR>
<INPUT TYPE="text" NAME="type" SIZE=40>
<P>
<INPUT TYPE="submit" VALUE="Get Summary!">
<INPUT TYPE="reset" VALUE="Clear the form">
</FORM>
<HR>
</BODY>
</HTML>
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Free web site, control panel, and dedicated IP with game server purchase for only $25 twastudios General Advertisements 3 10-20-05 06:13 AM
Free Server Security Audit by Touch Support TSGradyR General Advertisements 0 03-30-05 11:35 PM
Admin Lite by Touch Support TSGradyR General Advertisements 0 02-23-05 07:04 PM
Secure Managed Dedicated Server @ $149/month - NO SETUP FEES! Newbie2000 General Advertisements 1 12-16-04 02:27 PM
FREE Team Speak server w/ every purchase of a Call of Duty Server twastudios General Advertisements 0 10-31-03 01:14 AM


All times are GMT -5. The time now is 04:08 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.