Common input interface
Common Gateway Interface (in English Common Gateway Interface, abbreviated CGI) is an important technology of the World Wide Web that allows a client (web browser) to request data from a program running on a web server. CGI specifies a standard for transferring data between the client and the program. It is a communication mechanism between the web server and an external application whose final execution result is MIME objects. The applications that run on the server are called CGIs.
CGI applications were one of the first practices of creating dynamic content for web pages. In a CGI application, the web server passes the client's requests to an external program. This program can be written in any language that the server supports, although for portability reasons scripting languages are often used. The output of such a program is sent to the client instead of the traditional static file.
CGI has made it possible to implement new and varied functions in web pages, in such a way that this interface quickly became a standard, being implemented on all kinds of web servers.
CGI Form of Performance
The following describes how a CGI works schematically:
- In the first instance, the server receives a request (the client has activated a URL containing the CGI), and checks if it is an invocation of a CGI.
- Subsequently, the server prepares the environment to execute the application. This information comes mainly from the client.
- Next, the server runs the app, capturing its standard output.
- The application then performs its function: as a result of its activity a MIME object is generated that the application writes in its standard output.
- Finally, when the application ends, the server sends the information produced, along with its own information, to the client, which was in a state of expectation. It is the responsibility of the application to announce the type of MIME object that is generated (CONTENT_TYPE field).
Programming a CGI
A CGI program can be written in any programming language that produces an executable file. Among the most common languages are: C, C++, Perl, Java, Visual Basic, Cobol... However, since the CGI receives the parameters in the form of text, a language that allows manipulation of strings will be useful. characters in a simple way, such as Perl. Perl is an interpreted language that allows simple file and text manipulations, as well as string extraction and manipulation, coupled with fast and easy searches.
Information Exchange: Environment Variables
The following data are environment variables that are passed by CGI programs:
- Specific server variables:
- SERVER_SOFTWARE — name and version of the web server.
- SERVER_NAME — server computer name, can be an IP address.
- GATEWAY_INTERFACE - CGI version.
- Specific variations of the request:
- SERVER_PROTOCOL — version HTTP.
- SERVER_PORT - TCP port.
- REQUEST_METHOD — name of the HTTP method.
- PATH_INFO - suffix of the route.
- PATH_TRANSLATED — corresponds to the full server route, if PATH_INFO is present.
- SCRIPT_NAME - programme route. Example: /cgi-bin/script.cgi.
- QUERY_STRING — the part of the URL after the character?. the query chain can be composed of *Name=value separated by character " (example: var1=val1"var2=val2...) used to send data from a web form using the GET method.
- REMOTE_HOST - customer's host name.
- REMOTE_ADDR — Client IP address.
- AUTH_TYPE - identification type.
- REMOTE_USER variable used for certain types of authentication (AUTH_TYPE).
- REMOTE_IDENT — variable to identify some TCP connections.
- CONTENT_TYPE — type of application content.
- CONTENT_LENGTH — input data size.
- The variables that pass through the customer's user agent (HTTP_ACCEPT, HTTP_ACCEPT_LANGUAGE, HTTP_USER_AGENT, HTTP_COOKIE etc.) contain values of their corresponding HTTP headers so their data does not change.
Common types of CGI
- Access Counter: Count the number of times a given page has been requested. The value is saved in a file. Every time it is invoked it increases, for its subsequent visualization.
- Searcher: Locate pages containing the specified terms. Use a table that lists the words and for each one specify the pages where it is found.
- Mail: Gets structured information from the user.
- Contributions: Allows to add links or annotations to a page, indicating the origin of the addendum.
- Use statistics: Presents information about the events produced on the WWW server. The server maintains a record (log) of events that have occurred.
- Remote server administration: Allows to interact with the server from WWW. Invokes programs that control or modify the behavior of the server.
CGI activation scenario
- Initial situation: The client requests the invocation of a CGI, either involuntaryly (single information is sent) or explicitly (formulary). In the form there are pairs of variable type=value. The HTTP method specified in the form can be GET or POST.
On the server instead, the configuration file specifies a cgi-bin directory with capacity to run programs. There may be other files and other programs that the server and its CGIs can access. - The client clicks the SUBMIT type button on the form: Depending on the method, a message containing the information on the form on the header (for GET) or on the body of the message (for POST). The message is sent to the server, adding customer information that the browser itself knows. The client is waiting to receive a MIME object as a server response.
- The server receives the request message or launches the CGI program: The server compares the message information you know from your configuration file, thus determining the validity of the request. The server actually wonders: Is this URL? Do you have all the permits?
Prepare the environment by adding information to the one communicated by the customer's browser. If it is GET, the information from the form (variable parts = value) is defined in QUERY_STRING. The server then operates the CGI. If it is POST, the information is placed in the standard input of the CGI. The execution of the CGI is finally started and the server waits for it to end. - CGI execution: The CGI accesses the environment variables. Check or adapt the operation according to the GET or POST method established in REQUEST_METHOD: if it were to GET, the information will be in QUERY_STRING, while if it is POST, the standard input will be taken.
A MIME object is constructed to be sent to the client. The first write shall announce the type of the object: CONTENT_TYPE: type/subtype.
- The server returns to work: The server adds to its CGI response a header indicating its size (CONTENT_LENGTH).
- The customer receives the answer: Interpretation of the answer. Visualization with the browser.
Contenido relacionado
Heater
Maximum transfer unit
Mysql