CGI-Scripts

When you press the "submit" button in a form, the contents of the form is sent to the WWW-server, and a program is run. Programs for handling forms must follow the Common Gateway Interface (CGI) definition. The standard imagemap program for handling (server-side) clickable images are also CGI-scripts.

Without the CGI-scripts on the server, server-side imagemaps and forms do not work. If you copy all the nodes from this course onto your local PC, clicking on clickable images and pressing the submit button in forms will generate an error message, because the CGI-script cannot be run on the appropriate server.

A CGI-script receives two types of input: a number of environment variables and some standard input containing attribute/value pairs. There are GET and POST requests. The difference is that with a GET request the standard input is also passed to the CGI-script as an environment variable, while with a POST request it is "real" standard input.

CGI-scripts can be written in any programming language. We only give an example in C. The code uses a set of standard routines for decoding the attribute/value pairs.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#include "util.h"
/* defines routines for decoding attribute/value pairs:
   char *makeword(char *line, char stop);
   char *fmakeword(FILE *f, char stop, int *len);
   char x2c(char *what);
   void unescape_url(char *url);
   void plustospace(char *str);
*/

typedef struct {
    char *name;
    char *val;
} entry;

int main(int argc, char *argv[]) {
    entry entries[6];
    register int x,m=0;
    int cl;

    char *name = "", *sex = "", *student = "",
         *interest = "", *comments = "";

    printf("Content-type: text/html\n\n");
    
    cl = atoi(getenv("CONTENT_LENGTH"));

    /* we make sure we do not read more than cl */

    for(x=0;cl && (!feof(stdin));x++) {
        m=x;
        entries[x].val = fmakeword(stdin,'&',&cl);
        plustospace(entries[x].val);
        unescape_url(entries[x].val);
        entries[x].name = makeword(entries[x].val,'=');
    }

    for (x = 0; x <= m; x++) {
        if (strcmp(entries[x].name,"name") == 0)
	    name = entries[x].val;
        if (strcmp(entries[x].name,"sex") == 0)
	    sex = entries[x].val;
        if (strcmp(entries[x].name,"student") == 0)
	    student = entries[x].val;
        if (strcmp(entries[x].name,"interest") == 0)
	    interest = entries[x].val;
	if (strcmp(entries[x].name,"comments") == 0)
	    comments = entries[x].val;
    }

    printf("<HTML>\n<HEAD>\n<TITLE>Result of Example Form</TITLE></HEAD>\n");
    printf("<BODY BGCOLOR=\"#FFFFFF\">\n");
    printf("<H2>Result of Example Form</H2>\n");

    printf("<BR>Your name is %s\n", name);
    printf("<BR>Your sex is %s\n",
           (*sex=='m')?"male":(*sex=='f')
             ?"female":"undefined");
    printf("<BR>You are%s a student\n",
           (*student=='\0')?" not":"");
    printf("<BR>Your WWW-interest is %s\n", interest);
    if (*comments != '\0')
      printf(
        "<P>\nYou made the following comments:\n<PRE>\n%s\n</PRE>\n",
             comments);

    printf("</BODY>\n</HTML>\n");
    exit(0);
}