CGI: understand the request and response flow between a web server and a script
The Common Gateway Interface (CGI) lets an HTTP server pass request information to an external program and turn that program’s output into an HTTP response. The application receives request method, path, and some header information as environment-style meta-variables, reads a request body from standard input, and writes response headers and body to standard output. This guide follows the CGI/1.1 contract described in RFC 3875 and makes clear that the RFC is not an Internet Standard.
The Common Gateway Interface (CGI) lets an HTTP server pass request information to an external program and turn that program’s output into an HTTP response. The application receives request method, path, and some header information as environment-style meta-variables, reads a request body from standard input, and writes response headers and body to standard output. This guide follows the CGI/1.1 contract described in RFC 3875 and makes clear that the RFC is not an Internet Standard.
What does CGI connect?
A browser or another HTTP client sends a request to a server. Depending on its configuration, the server selects a CGI program and provides the request meta-variables and body data defined by the CGI interface. The program handles application logic and returns a response to the server. The basic division is that the server handles HTTP work such as connections and transport, while the script handles application work such as data access and document generation.
CGI is not a programming language or framework. It is an interface that defines what information passes between the server and a program and how the program returns a response. RFC 3875 was published in 2004 as an Informational document describing CGI/1.1 practice at the time. It does not guarantee the behavior of every server today.
How does request information reach the program?
The CGI server provides meta-variables that describe the request. The variables available and their details can vary by protocol and implementation. Common values to inspect include:
| Meta-variable | Purpose |
|---|---|
REQUEST_METHOD | HTTP method to process; the program should handle the request according to this value. |
QUERY_STRING | Query component of the URI; the application parses it when needed. |
SCRIPT_NAME | Path identifying the script handling the request. |
PATH_INFO | Additional path information after the script path for the application to interpret. |
CONTENT_TYPE, CONTENT_LENGTH | Information about request body type and length. |
Variables such as HTTP_ACCEPT | Values of corresponding HTTP request headers, commonly represented with an |
The presence of these variables does not make their values safe or settle their application meaning. Behind a proxy, REMOTE_ADDR may identify the proxy directly connected to the server rather than the original user. Header-to-variable mapping and extension variables can depend on server configuration, so verify the contract in the deployed environment.
What does a simple request and response look like?
Suppose a client requests GET /cgi-bin/greet?name=Jin. The server might pass meta-variables such as REQUEST_METHOD=GET and QUERY_STRING=name=Jin to the CGI program. For a request with a body, CGI reads the data from standard input, taking CONTENT_LENGTH and the actual bytes received into account. A query string does not automatically become a name-value data structure; the program must handle URL encoding and validate input.
For a normal document response, the CGI program writes at least a Content-Type header, a blank line, and the body to standard output. Under CGI/1.1, if no success status is specified, 200 is the default for this response type.
Content-Type: text/plain; charset=utf-8
Hello, JinThis is an example of output between a CGI program and the server, not the complete HTTP response sent over the network. The server converts CGI output into an HTTP response. Redirects follow rules that differ from an ordinary document response; consult RFC 3875 and the server documentation.
What should you check when reading or implementing CGI?
**Server mapping:** Find which request paths and methods invoke which program. Do not confuse the script file path with `PATH_INFO`.
**Method and input:** Branch on `REQUEST_METHOD`; read the body with `CONTENT_LENGTH` and the number of bytes actually received in mind. Do not assume a fixed-size buffer can hold arbitrarily large input.
**Output format:** Put a blank line between headers and body and send a correct `Content-Type` for document responses. Keep debug logs off standard output so they do not corrupt the response.
**Paths and permissions:** If using untrusted `PATH_INFO` as a file path, validate `.` and `..` and empty path segments, and prevent resolution outside the allowed directory. Minimize the CGI process account’s file and process permissions.
**Sensitive data and proxies:** Do not pass authentication headers to the script unnecessarily. In a proxy setup, do not assume the direct connection address or a header identifies the end user.
**Timeouts and failures:** A server may terminate a program that produces no output within its configured timeout. Check server logs, the CGI program’s error output, and its exit status together; determine whether any work was already applied before choosing a recovery action.
CGI/1.1 describes information exchange between the server and program, but does not replace application authentication, authorization, input validation, or business rules. When maintaining existing CGI software, check both the server’s execution and environment-variable configuration and the CGI interface rules in RFC 3875.
Sources
[RFC 3875: The Common Gateway Interface (CGI) Version 1.1](https://www.rfc-editor.org/rfc/rfc3875.html)
[RFC 9110: HTTP Semantics](https://www.rfc-editor.org/rfc/rfc9110.html)