John Whitney has asked for the wisdom of the Perl Monks concerning the following question:

I'm a stale programmer of 20 years ago who is trying to write a Windows application with an IE 4.0 HTML/DHTML/JavaScript front-end, and a Perl server.pl back-end, both on the same PC.

I use http://localhost:6789/ to send requests to Perl from the HTML pages. I know how to receive in my server.pl a GET request, and access the arguments passed in the query string.

I don't know how to do the same for a POST request, i.e. access the passed hidden variables, ostensibly from STDIN. For whatever it's value, I using a Windows 98 PC using ActiveState's Perl.

Here is the code around the processing of the GET and POST requests...

while ($client = $server->accept()) { $client->autoflush(1); $request = <$client>; $request =~ tr/\x0d\x0a//d; # Remove and CR's and LF's if ($request =~ m|^GET /(.*?)(\?(.*))? HTTP/1.[01]|) { # GET request print STDERR "GET request\n"; $page = uri_unescape($1); $querystring = uri_unescape($3); PROCESS_REQUEST (); } elsif ($request =~ m|^POST /(.*) HTTP/1.[01]|) { # POST request print STDERR "POST request\n"; $page = uri_unescape($1); # How to read in the POST's passed hidden variables here ? ? ? PROCESS_REQUEST (); } else { REDIRECT_TO_ERROR_PAGE ('Unprocessed request.'); } close($client); }


Any help greatly appreciated.

Replies are listed 'Best First'.
Re: Processing POST request in simple Windows server.
by BUU (Prior) on Oct 22, 2004 at 07:41 UTC
    This is why Jesus Christ, our lord and saviour in heaven, came down to earth for three days and wrote the CGI.pm module. After completing this great task, he was cruelly struck down by the romans, in the prime of his life. Will you let his death be in vain, or will you choose to utilize this great work?
      According to the POD, the author of CGI.pm is Lincoln D. Stein with contributions by a number of others. I don't believe that Jesus Christ (or any other deity for that matter) is amongst the list of contributors.
Re: Processing POST request in simple Windows server.
by gothic_mallard (Pilgrim) on Oct 22, 2004 at 07:47 UTC

    I'd recommend you take a look at CGI.pm that handles all this kind of thing for you in a nice little module.

    --- Jay

    All code is untested unless otherwise stated.

      Appreciate the response.

      I looked at CGI.pm and the documentation regarding it. But all the examples I could find using CGI.pm were the case where there was just the need to process one POST/GET request..

      In my case I've created a server that needs to process many POST/GET requests. What I am missing is the following loop....

      use cgi while (a request to localhost:6789 server) { process the request with CGI.pm modules }
      here's the code that makes an attempt at this loop....

      use IO::Socket; use Net::hostent; use CGI; # Constants $cnst_port = 6789; # Port we want to listen to $cnst_server = 'http://localhost:' . $cnst_port . '/'; # Server addre +ss $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $cnst_po +rt, Listen => SOMAXCONN, Reuse => 1 ); die "Can\'t set up server" unless $server; print STDERR "Server Version 0.637\n" . "Server accepting clients via $cnst_server\n"; while ($client = $server->accept()) { $client->autoflush(1); $cgi = new CGI; $request_path_translated = $cgi->path_translated(); $request_query_string = $cgi->query_string(); $request_request_method = $cgi->request_method(); print STDERR "request_path_translated = $request_path_translated\n"; print STDERR "request_query_string = $request_query_string\n"; print STDERR "request_request_method = $request_request_method\n"; RETURN_DUMMY_PAGE (); close($client); } sub RETURN_DUMMY_PAGE { print $client 'HTTP/1.0 200 OK' . "\n" . 'Content-type: text/html' . "\n\n" . '<HTML>' . "\n" . ' <HEAD><TITLE>Asayo - LED Sign Software XL</TITLE></HEAD>' . "\n" + . ' <BODY><H1>Dummy Page</H1></BODY>' . "\n" . '</HTML>' . "\n"; }
      The dummy page gets returned each time, but the CGI calls return nothing..

      Any ideas?
Re: Processing POST request in simple Windows server.
by DrHyde (Prior) on Oct 22, 2004 at 09:30 UTC
    If you want an http server, use one that someone else has written, such as Apache, don't write your own. You *will* get it wrong.
Re: Processing POST request in simple Windows server.
by Anonymous Monk on Oct 22, 2004 at 22:46 UTC
    $post=<$client>