You write some C++ code, compile it and get your output on black screen. Too mainstream, right!
Here's a trick...what if you can take your output to your web-browser. Of-course you have to take the extra pain but it'll be worth it.
The audacity to speak above lines are given to me by something called "CGI". So what is CGI?
Here's a trick...what if you can take your output to your web-browser. Of-course you have to take the extra pain but it'll be worth it.
The audacity to speak above lines are given to me by something called "CGI". So what is CGI?
- The Common Gateway Interface, or CGI, is a set of standards that define how information is exchanged between the web server and a custom script.
- The CGI specs are currently maintained by the NCSA and NCSA defines CGI is as follows:
- The Common Gateway Interface, or CGI, is a standard for external gateway programs to interface with information servers such as HTTP servers.
- The current version is CGI/1.1 and CGI/1.2 is under progress.
I know that was too much , so here is how CGI actually works:
- To understand the concept of CGI, let's see what happens when we click a hyperlink to browse a particular web page or URL.
- Your browser contacts the HTTP web server and demand for the URL ie. filename.
- Web Server will parse the URL and will look for the filename. If it finds requested file then web server sends that file back to the browser otherwise sends an error message indicating that you have requested a wrong file.
- Web browser takes response from web server and displays either the received file or error message based on the received response.
However, it is
possible to set up the HTTP server in such a way that whenever a file
in a certain directory is requested, that file is not sent back;
instead it is executed as a program, and produced output from the
program is sent back to your browser to display.
The Common
Gateway Interface (CGI) is a standard protocol for enabling
applications (called CGI programs or CGI scripts) to interact with
Web servers and with clients. These CGI programs can be a written in
Python, PERL, Shell, C or C++ etc.
Web Server Configuration
Before
you proceed with CGI Programming, make sure that your Web Server
supports CGI and it is configured to handle CGI Programs. All the CGI
Programs to be executed by the HTTP server are kept in a
pre-configured directory. This directory is called CGI directory and
by convention it is named as /var/www/cgi-bin. By convention CGI
files will have extension as .cgi, though they are C++
executable.
By
default, Apache Web Server is configured to run CGI programs in
/var/www/cgi-bin. If you want to specify any other directory to run
your CGI scripts, you can modify the following section in the
httpd.conf file:
<Directory "/var/www/cgi-bin"> AllowOverride None Options ExecCGI Order allow,deny Allow from all </Directory> <Directory "/var/www/cgi-bin"> Options All </Directory>
Here,
I assumed that you have Web Server up and running successfully and
you are able to run any other CGI program like Perl or Shell etc.
First CGI Program
Consider
the following C++ Program content:
#include <iostream> using namespace std; int main () { cout << "Content-type:text/html\r\n\r\n"; cout << "<html>\n"; cout << "<head>\n"; cout << "<title>Hello World - First CGI Program</title>\n"; cout << "</head>\n"; cout << "<body>\n"; cout << "<h2>Hello World! This is my first CGI program</h2>\n"; cout << "</body>\n"; cout << "</html>\n"; return 0; }
Compile
above code and name the executable as cplusplus.cgi. This file is
being kept in /var/www/cgi-bin directory and it has following
content. Before running your CGI program make sure you have change
mode of file using chmod
755 cplusplus.cgi UNIX
command to make file executable. Now if you click cplusplus.cgi then
this produces the following output:
No comments:
Post a Comment