24.1. CGI History and Operation

CGI has been around almost as long as the Web. It was developed as a means to extend early HTTP servers, allowing users to submit data to the server and receive appropriate content based on that submission.

Perl was perhaps the earliest means of accomplishing CGI, although any programming language capable of reading from standard input (STDIN) and writing to standard output (STDOUT) can be used for CGI purposes.

24.1.1. Understanding HTTP Request and Response

At its simplest, the HTTP protocol works by the server taking a request from a client and returning a response to that request. In most cases, the request is for a document whose content is sent in the server's response. A variety of other operations are possible between client and server, but we will concentrate on this simple "I want a document" request and "here it is" response. This operation is diagrammed in Figure 24-1.

The client request typically resembles the following:

GET  somepath/somedocument  HTTP/1.1

Note that this section provides the bare essentials regarding HTTP requests and responses. More detail on this subject is covered in Chapter 1 in the section "The Web Protocol."

Figure 24-1. Figure 24-1

This request asks for a document in a particular directory (governed by somepath) and tells the server that the client speaks HTTP version 1.1. For example, to request the top-level ...

Get Web Standards Programmer's Reference: HTML, CSS, JavaScript®, Perl, Python®, and PHP now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.