CGI is the interface standard used to connect an external program for communication with the server. The program that works according to such an interface together with the web server is commonly called a gateway, although many prefer the names “script” or “CGI program”. In fact, it allows the use of the input and output console to interact with the client.
The interface itself is designed in such a way that any programming language can be used that can work with standard input-output devices. Scripts for built -in command interpreters of operating systems have such opportunities, so in simple cases even command scripts can be used.
CGI is one of the most common means of creating dynamic sites. This standard is used by such popular web servers as for example, Nginx or Apache.
The server knows how to fix it on a specific host: the port and listen to incoming connections. At a given address, a HTML page is issued if the request is correct, or a page with an error of 404, if the page requested does not exist. A CGI script showing time when pressing the buttons on the main page is also implemented.
#!
htdocs/
cgi-bin/
install.c
utility.c
utility.h
main.c
Makefile
We install it using Make. The installation script will compile and move the executable file to the/USR/Local/Bin folder, then create the .Service file and put it in/etc/systemd/::
cd cgi/
make install
The server can now be launched in the same way as the system services (it only works on Ubuntu, because it places a unit file in the/etc/systemd/system/folder, which is not installed on other systems). According to the default, the server will start at 127.0.0.1:1235. To set your settings, you need to change the config file:
service cgi start
service cgi stop
When starting, the program creates a PID file into which it records the ID of the current process so that in the future you can send a signal to it to complete the work. If an error has occurred during the start of the server, the file is not created.
#!c
//creating PID file
FILE * file = fopen(path_to_pidfile, "a+");
fprintf(file, "%d", getpid());
fclose(file);
//killing running server
FILE * file = fopen(path_to_pidfile, "r+");
fscanf(file, "%d", &pid);
kill(pid, SIGTERM);
fclose(file);
remove(path_to_pidfile);
Next, using a set of functions for working with sockets, we set up our server. If everything has passed without errors, we begin to accept incoming connections. For each adopted connection, we create a separate process, the parental process continues to listen.
#!c
pid_t pid = fork();
if (0 == pid) { //if it is child process - interact connection
handle_request(client_socket, &client_address, htdocs);
exit(0);
} else { //otherwise close socket and continue listening
close(client_socket);
}
The subsidiary reads the request, processes it and gives the page the answer: if the file was found, then the corresponding page is issued, otherwise the user will receive a page with an error of 404. All statics are stored in the HTDOCS server folder.
#!c
read(client_socket, recv_buffer, sizeof(recv_buffer));
char *query_str = parse_request(recv_buffer);
...
GET(client_socket, query_str, maindir);
When clicking on the main page on the "Get Current Time" button, a script that returns the current date/time is performed:
#!c
if (strcmp(query_str + strlen(query_str) - 4, "cgi?") == 0) {
...
execve(main_dir, arg, empty2); //execute cgi-script
close(client_socket);
}
At the moment, it is implemented:
Coming Soon: