Lecture Notes Eight: Building a CGI Processor (Part II). Some more HTTP: GET vs. POST.

Here's a picture:

Use the form, type something in the field, then push proceed.

What's $ENV{REQUEST_METHOD} and what's the URL like?

Remember the GET /index.html HTTP1.1 of last time?

Can you issue this request: GET /cgi-bin/process?one=abc HTTP1.1?

That's what the browser does, too.

The rule is that all the names of the fields, and their values go to the server as one string.

Names and values are connected by an equal sign.

Pairs of the type name=value are connected by ampersands.

This string needs to be taken apart by the receiving script.

It also needs to be decoded.

We will discuss all this in class, plus pattern matching, then the following program.


if    ($ENV{"REQUEST_METHOD"} eq 'GET' ) { 
  $me = $ENV{"SCRIPT_NAME"}; 
  print qq{ 
    <form method=POST action=$me> 
    Please write your thoughts below: <p> 
    <textarea name="thoughts" rows=5 cols=60></textarea> 
    <p> Also please write your e-mail address here: 
    <input type="text" name="email"> <p>     
    <input type="submit"> 
} elsif ($ENV{"REQUEST_METHOD"} eq 'POST') { 
  print "Called with POST.<pre>";
  foreach $k (keys %in) {
      print $k, " --> ", $in{$k}, "<br>"; 
} else {
  print "Method not supported.\n"; 
sub printHeader  { print "Content-type: text/html\n\n<html><body>"; } 

sub printTrailer { print "</body></html>"; }

sub readParse {
    if      ($ENV{"REQUEST_METHOD"} eq 'GET' ) {
	$input = $ENV{"QUERY_STRING"}; 
    } elsif ($ENV{"REQUEST_METHOD"} eq 'POST') {
	read (STDIN, $input, $ENV{"CONTENT_LENGTH"}); 
    } else {
	print "Unsupported method."; 

    @input = split(/\&/, $input); 
    foreach $elem (@input) {
	$elem =~ s/%(..)/chr(hex($1))/ge;
	$elem =~ s/\+/ /g; 
	($key, $value) = split(/\=/, $elem); 
	$in{$key} = $value; 
Expect this set of notes to be reworked (re-written) by Friday. I didn't say which Friday.
Last updated: Sep 20, 2001 by Adrian German for A348/A548