Common Gateway Interface (CGI) and Web Server Interaction

Classified in Computers

Written on in English with a size of 3.5 KB

What is a Common Gateway Interface (CGI)?

A CGI (Common Gateway Interface) is a program that runs in real time on a Web Server in response to a request from a browser. When this happens, the Web Server executes a child process to receive the data sent by the user (if it exists), makes some silky sheets of the same data available as environment variables, and captures the output of the program to send a response to the browser. The CGI's purpose is to provide intelligence and interactivity to a website.

Basic CGI Structure in C Language

The basic body of a CGI in C language is:

/* Hello World Program */
/* Compile in Linux: gcc hello.c -o hello.cgi */

#include <stdio.h>

int main() {
    printf("Content-Type: text/html\n\n");
    printf("<html>Hello World</html>\n");
    return 0;
}

The CGI Execution Process

  • The WWW client requests a service from a CGI application.
  • The HTTPD server receives the request and input data.
  • The server creates an environment and creates variables in it with the input data.
  • The server runs the CGI application site in this environment.
  • The application processes the CGI environment variables and retrieves the input data.
  • The CGI application runs, producing a result on its standard output.
  • The HTTP server redirects the standard output of the CGI application to the WWW client.
  • The WWW client receives the result of your query.

Integrating HTML Forms with CGI

Forms are an essential element of CGI. The forms allow you to enter structured information into a WWW client that can be used as input to a CGI application. To support HTML forms, the language allows the definition of different types of fields. The syntax of the marker is the following form: <FORM METHOD="method" ACTION="CGI URL">.

Processing Form Data

One of the most important functions of the CGI is the process of data entered by the visitor filling out a form. The feature of HTML forms is the most common form of carrying user data to a CGI program. It offers the possibility of user input through various types of elements, such as text boxes, buttons, classifieds, and graphics.

The CGI can receive the contents of a form in two basic ways:

  1. Using the environment variable QUERY_STRING.
  2. By standard entry, in which case the environment variable CONTENT_LENGTH indicates the length of the string to be read.

When the user submits the form, the script receives the data as name=value pairs. The names are what we define in the labels using the attribute name, and values are what is given by the value attribute or what the user typed or selected.

Data String Organization and Encoding

The organization of the string that contains the pairs and reaches the CGI is this: "name1=value1&name2=value2&name3=value3". It is worth observing that the special representative characters are sent in digital format or some other representation; for example, the sign + is a blank, but sometimes %20 is used. With it, you must convert %xx sequences to the value of the character whose ASCII value is represented; for instance, the = sign is represented by %3d. Do not forget this is only regarding the values the user enters.

Related entries: