#webtech2
Thread

CGI (thread):

Full form - Common Gateway Interface

Principles of CGI programming:
• it defines how information is transferred from web server to an external application and external application to web server
• Independent of any programming language - external applications may be developed using a programming language that fits the application

Advantages:
• It provides a very simple interface for making http requests to web server.
• It is not necessary to have any special library or any api to make a CGI.
• CGI programs rely on the standard concepts of standard input, standard output, and environment variables to communicate with the Web server.
• Independent of any programming language - external applications may be developed using a programming language that fits the application

Structure of a CGI program
Any CGI script consists of basically three steps:
• Read parameters passed to this script
• Process these parameters
• Write HTML response to the standard output
Example:
```#include <stdio.h>
include <stdlib.h>
int main(void) {
long a, b;
printf("Content-Type:text/html\n\n");
char *data = getenv("QUERY_STRING");
sscanf(data,"a=%d&b=%d", &a, &b);
printf("%d + %d = %ld", a, b, a + b);
return 0;
}```

How to distinguish if a request is a CGI request or not?
> The server needs a way to know which URLs map to scripts and which URLs just map to ordinary HTML files. For CGI this is usually done by creating CGI directories on the server. This is done in the server setup and tells the server that all files in a particular top-level directory are CGI scripts (located somewhere on the disk) to be executed when requested. Moreover, Some servers can also be set up to not use CGI directories and instead require that all CGI programs have file names ending in .cgi.

4b