Web Development in C++
Before learning above C++ web development, we need to learn about CGI
What is CGI?
CGI stands for common gateway interface. CGI is a standard that tells us how the exchange of information happens between the webserver. Exchange of information means how the application takes the user's request and responds to it by sending back the data. This common gateway interface is a part of the Hypertext Transfer Protocol (HTTP).
Now let's take one example and check how the common gateway interface is used.
- When you open a website, the browser will contact the HTTP server and ask for a URL.
- Then the HTTP web server will send the URL and search for the file if the user requests a file. If the file is found, the server will send the file to the browser.
- If the file is not found, then the web server will display an error.
- The browser will take the response from the server and will display if the file is found or it will display the error
Example: Now let's see an example of the CGI in C++
#include <bits/stdc++.h>
using namespace std;
int main ()
{
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>Hello Tutorials and example </title>\n";
cout << "</head>\n";
cout << "<body>\n";
cout << "<h3> <b> First CGI program </b> </h2>\n";
cout << "</body>\n";
cout << "</html>\n";
}
We need to compile the program, and then an executable file will be created with the extension .cgi. We need to place this file inside /var/www/CGI-bin. Before running the CGI program, we need to change the mode of the file using the UNIX command so that the file gets executed
chmod 755 filename.CGI
There are different CGI environment variables:
- CONTENT_LENGTH: The length of the information will be returned.
- CONTENT_TYPE: Data type of the content to be returned will be printed.
- HTTP_COOKIE: In the HTTP headers, the set cookie will be returned.
- HTTP_USER_AGENT: The name of the web browser will be returned.
- PATH_INFO: The path where the CGI script is stored will be returned.
- REMOTE-ADDR: The IP address of the requested derive will be returned.
- REMOTE_HOST: The name of the host making the request will be printed.
- SCRIPT_FILENAME: CGI script of the full path will be returned.
- SCRIPT_NAME: The name of the CGI script will be returned.
- SERVER_NAME: The IP address of the hostname will be returned.
- SERVER_SOFTWARE: The version and the name of the server's software will be returned.
Different HTTP heads are frequently used in CGI programs:
- Content type: This will specify the type of file returned.
- Expires: Date: This will return, when will the file be expired.
- Location: URL: This will change the URL to which the URL is requested.
- Last-modified: Date: The date of the last modification done to the resource is returned.
- Content-length: The length of the data to be returned will be returned. This is used to print the value of the estimated time for downloading the file.
- Set-Cookie: String The cookie will be passed in the form of the string.
CGI program that has all the CGI varaibles:
#include <iostream>
#include <stdlib.h>
using namespace std;
const string ENV[ 24 ] = {
"COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE",
"HTTP_ACCEPT", "HTTP_ACCEPT_ENCODING",
"HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION",
"HTTP_HOST", "HTTP_USER_AGENT", "PATH",
"QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT",
"REQUEST_METHOD", "REQUEST_URI", "SCRIPT_FILENAME",
"SCRIPT_NAME", "SERVER_ADDR", "SERVER_ADMIN",
"SERVER_NAME","SERVER_PORT","SERVER_PROTOCOL",
"SERVER_SIGNATURE","SERVER_SOFTWARE" };
int main () {
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>CGI Environment Variables</title>\n";
cout << "</head>\n";
cout << "<body>\n";
cout << "<table border = \"0\" cellspacing = \"2\">";
for ( int i = 0; i < 24; i++ ) {
cout << "<tr><td>" << ENV[ i ] << "</td><td>";
// attempt to retrieve the value of the environment variable
char *value = getenv( ENV[ i ].c_str() );
if ( value != 0 ) {
cout << value;
} else {
cout << "Environment variable does not exist.";
}
cout << "</td></tr>\n";
}
cout << "</table><\n";
cout << "</body>\n";
cout << "</html>\n";
return 0;
}
Before running the CGI program, we need to change the file mode using the UNIX command so that the file will execute. We need to compile the program, and then an executable file will be created with the extension .cgi. We need to place this file inside /var/www/CGI-bin.