Redirect - A Irie Pascal sample CGI program

This CGI program sends the visitor somewhere else.

Program Listing

	program redirect(input, output);
	const
	MaxBuffer = 256;
	BASE = 'http://www.irietools.com/';
	var
	buffer : string[MaxBuffer];
	NewLocation : string;
procedure Init; begin NewLocation := BASE end;
procedure GenerateHTTPHeader; begin writeln('Content-type: text/html'); writeln; end;
procedure GetCGIData; var RequestMethod : string;
procedure GetRequest; begin (* GetRequest *) buffer := getenv('QUERY_STRING') end; (* GetRequest *)
procedure PostRequest; var len, i : 0..maxint; err : integer; ContentLength : string; c : char; begin (* PostRequest *) buffer := ''; ContentLength := getenv('CONTENT_LENGTH'); if ContentLength <> '' then val(ContentLength, len, err) else len := 0; if len <= MaxBuffer then for i := 1 to len do begin read(c); buffer := buffer + c end end; (* PostRequest *)
begin (* GetCGIData *) RequestMethod := getenv('REQUEST_METHOD'); if RequestMethod = 'GET' then GetRequest else PostRequest end; (* GetCGIData *)
procedure ProcessCGIData; var i, num, p : integer; EncodedVariable, DecodedVariable, name, value : string;
procedure ProcessNameValuePair(var name, value : string); begin if (name = 'lstnavigation') or (name = 'navigation') or (name = 'goto') then begin if value <> '[none]' then if lowercase(copy(value, 1, 5)) = 'http:' then NewLocation := value else NewLocation := BASE + value end else ; (* do nothing we have an undefined form element *) end;
begin (* ProcessCGIData *) num := CountWords(buffer, '&'); for i := 1 to num do begin EncodedVariable := CopyWord(buffer, i, '&'); DecodedVariable := URLDecode(EncodedVariable); p := pos('=', DecodedVariable); if p > 0 then begin name := lowercase(trim(copy(DecodedVariable, 1, p-1))); value := lowercase(trim(copy(DecodedVariable, p+1))); ProcessNameValuePair(name, value); end end end; (* ProcessCGIData *)
procedure GenerateResponse;
procedure GenerateHTMLHeader; begin writeln('<html>'); writeln('<head>'); writeln('<meta name="Description" content="Redirect New Location">'); writeln('<meta http-equiv="Refresh" content="0;URL=', NewLocation, '">'); writeln('<title>Redirect to New Location</title>'); writeln('</head>'); end;
procedure GenerateHTMLFooter; begin writeln('<hr>'); writeln('<p>'); writeln('Redirect 1.0 Copyright &copy; 1999-2001, Stuart King<br>'); writeln('Home page <a href="http://www.irietools.com/">www.irietools.com</a>'); writeln('</p>'); writeln('</body>'); writeln('</html>'); end;
begin (* GenerateResponse *) GenerateHTMLHeader; writeln('<body bgcolor="#FFE8E8">'); writeln('<p>You should be automatically taken to the next page.</p>'); writeln('<p>However if your browser does not support redirection '); writeln('click <a href="', NewLocation, '">here</a></p>'); GenerateHTMLFooter; end; (* GenerateResponse *)
begin GenerateHTTPHeader; Init; GetCGIData; ProcessCGIData; GenerateResponse; end.

Program Description

The program calls the following procedures:

  1. GenerateHTTPHeader

    GenerateHTTPHeader writes the HTTP response header:

    	content-type: text/html
    	

    which tells the client (usually a web browser) that the content of the response is a text file, and further that this text file is an html text file.

    Notice that the built-in procedure writeln is used to write the response header to the standard output stream, and on to the waiting web server.

    Notice also the second writeln, which is used to write a blank line after the response header. The blank line tells the web server that the response header is finished, and that what follows is the content of the response (in this case, an html file)

  2. Init

    Init initializes the variable NewLocation which is used to store the URL of the page to send the visitor to.

  3. GetCGIData

    GetCGIData retrieves the name/value pairs passed to the program (whether from a GET request or a POST request, and stores them in a buffer.

    NOTE: In addition to the information passed to CGI programs in environment variables (see cgiinfo), additional information can be sent to the application in the form of a string containing one or more name/value pairs.

    The name/value pairs are seperated from each other by "&" characters.

    Each name/value pair is stored as name=value.

    where "name" identifies the information being passed and "value" is the actual content being passed.

    So for example, the string "age=10&language=en" contains two name/value pairs, age=10 and language=en.

  4. ProcessCGIData

    ProcessCGIData seperates each name/value pair and decodes them, then the "name" part is seperated from the "value" part, and ProcessNameValuePair is called. ProcessNameValuePair checks to make sure that the "name" is one of the expected names, and updates the variable NewLocation with the URL of the page to redirect to.

    Which means that by passing a name/value pair to this program you can control the URL of the page that the visitor gets sent to.

  5. GenerateResponse
    1. GenerateResponse generates the HTML header (using the meta-tag http-equiv to actually send the visitor to another page).
    2. GenerateResponse also generates a page body with a link to the new URL so that if the visitor's browser doesn't respond to the meta tag then the visitor can click on the link.
    3. Then GenerateResponse writes the HTML footer which is basically just a copyright notice.