An end-to-end client/server protocol that includes both HTTP and CGI. The best way to explain the dynamics of the protocol is to walk you tr...
An end-to-end client/server protocol that includes both HTTP and CGI. The best way to explain the dynamics of the protocol is to walk you trough a POST method invocation. Here’s the step-by-step explanation of this interaction:
- User clicks on the form’s “submit” button: This causes the web browser to collect the data within the form, and then assemble it into one long string of name/value pairs each separated by an ampersand (&). The browser translates spaces within the data into plus (+) symbols.
- The web Browser invokes a POST HTTP method: This is an ordinary HTTP request that specifies a POST method, the URL of the target program in the “cgi-bin” directory, and the typical HTTP headers.
- The HTTP server receives the method invocation via a socket connection: The server parses the message and discovers that it’s a POST for the “cgi-bin” program. So its starts a CGI interaction.
- The HTTP server sets up the environment variables: The CGI protocol uses environment variables as a shared bulletin board for communicating information between the HTTP server and the CGI program. The server environment variables are server_name, request_method, path_info, script name etc.,
- The HTTP server starts a CGI program: The HTTP server executes as instance of the CGI program specified in the URL.
- The CGI program reads the environment variables: In this case, the program discovers by reading the environment variables that it is responding to a POST.
- The CGI program receives the message body via the standard input pipe: The message body contains the famous string of ‘name=value’ items separated by ampersands (&). The content_ length environment variable tells the program how much data is in the string.
- The CGI program does some work: Typically, a CGI program interacts with some back-end resources – like a DBMS or transaction program – to service the form’s request. This information goes into the HTTP response entity, which really is the body of the message. Your program can also choose on provide all the information that goes into the HTTP response headers.
- The CGI program returns the results via the standard output pipe (stdout). The program pipes back the results to the HTTP server via its standard output. The HTTP server receives the results on its standard input. This concludes the CGI interaction.
- The HTTP server returns the results to the Web browser. The HTTP server can either append some response headers to the information it receives from the CGI program.