Web programming in C ++
Common Gateway Interface or CGI is a set of standards that define how information is exchanged between a Web Server and a Custom Script.
What is CGI?
Common Gateway Interface or CGI is a set of standards that define how information is exchanged between a Web Server and a Custom Script.
CGI Specification currently maintained by NCSA and NCSA defines CGI as follows: Common Gateway Interface or CGI is a standard for external gateway programs to Interface with information from the Server such as HTTP Server .
The current version is CGI / 1.1 and CGI / 1.2 is in development.
Web Browsing
To understand the concept of CGI, we see what happens when we click on a hyperlink to a specific Webpage or URL.
Your browser contacts the HTTP Web Server and requests a URL, for example, filename.
Web Server will parse that URL and will search for filename. If it finds the requested file, the Web Server sends the file back to the browser; otherwise, it sends an error message indicating that you requested a wrong file.
The Web browser receives feedback from the Web Server and displays the received file or error message based on the received response.
However, it is possible to set up HTTP Server in such a way that whenever a file in a particular directory is requested, the file is not sent back; instead it is executed as a program and the output is generated from the program sent to the browser to display.
Common Gateway Interface or CGI is a standard protocol for applications (called CGI programs or CGI scripts) capable of interacting with Web Server and with Client. These CGI programs can be written in Python, PERL, Shell, C or C ++ .
CGI structure diagram
Below is a diagram of a simple structure of CGI:
Configure Web Server
Before you continue with CGI programming, make sure your Web Server supports CGI and it is configured to handle CGI Program. All CGI programs to be executed by HTTP Server are kept in a pre-configured directory. This directory is called CGI directory, and by convention, it is named / var / www / cgi-bin. By convention, CGI files have a .cgi extension, so they are executable in C ++.
By default, Apache Web Server is configured to run CGI Program in / var / www / cgi-bin. If you want to specify any other directory to run your CGI script, then you must modify the following area in the file httpd.conf:
"/var/www/cgi-bin" > AllowOverride None Options ExecCGI Order allow,deny Allow from all "/var/www/cgi-bin" > Options All
First CGI program
Consider the content of the following C ++ program:
#include using namespace std ; int main () { cout << "Content-type:text/htmlrnrn" ; cout << "n" ; cout << "n" ; cout << "Hello World - Chuong trinh CGI dau tienn" ; cout << "n" ; cout << "n" ; cout << "
Hello World! Day is my first CGI program
n" ; cout << "n" ; cout << "n" ; return 0 ; }
Compile the above code and name it cplusplus.cgi. This file is kept in the / var / www / cgi-bin directory and it has the following content. Before running your CGI Program, make sure that you have switched the file mode by using the chmod 755 cplusplus.cgi command on UNIX to make the file executable. The result is:
Hello World! Day is my first CGI program
The C ++ program is a simple program that is writing output on the STDOUT file, for example: the screen. There is an important feature available so the first line is printed as Content-type: text / htmlrnrn. This line sends back to the browser and determines the content type to be displayed on the browser screen. You now understand the basics of CGI and you can write many complex CGI Programs using C ++. A CGI Program in C ++ can interact with any other peripheral systems such as RDBMS, to exchange information.
HTTP Header
The Content-type: text / htmlrnrn line is part of the HTTP Header, which is sent to the browser. All HTTP Headers have the following form:
HTTP Field Name : Noi dung truong Vi du : Content - type : text / htmlrnrn
Here are some important HTTP Headers, which you will use regularly in CGI Programming.
Header DescriptionContent-type: A MIME string that defines the format of the file being returned. Example: Content-type: text / html Expires: Date The date the information expires. It should be used by the browser to decide when a page should refresh. A valid date string should be in the format 01 Jan 1998 12:00:00 GMT Location: URL URL that should be returned instead of the requested URL. You can use this field to redirect a request to any file Last-modified: Date Last modified date of Content-length source: N Length, in bytes, of the data being returned. The browser uses this value to report the estimated download time for a file Set-Cookie: String Set the cookie passed through the stringEnvironment variable of CGI
All CGI Programs will have access to the following environment variables. These variables play an important role while writing any CGI Program:
Variable name DescriptionCONTENT_TYPE Data type of content. Used when the Client sends the attached content to the Server. For example: file upload CONTENT_LENGTH Length of query information. It is only available for HTTP_COOKIE POST requests. Returns the cookies set in the form that are the key / value pairs HTTP_USER_AGENT The request-header field is the User-Agent containing the information about the User Agent that started the request. It is the name of the web browser PATH_INFO Is the path for CGI script QUERY_STRING URL encoding information is sent with GET REMOTE_ADDR method The IP address of the remote host creates that request. It can be useful for log or authentication REMOTE_HOST purposes. The full name of the host creates that request. If this information is not available, REMOTE_ADDR can be used to retrieve the REQUEST_METHOD IP address. The method used to create the request. Common methods are GET and POST SCRIPT_FILENAME Path full to CGI script SCRIPT_NAME Name of CGI script server_name Hostname or IP address of Server SERVER_SOFTWARE Name and version of software that Server is runningThe following is a small CGI program to list all CGI variables.
#include #include 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/htmlrnrn" ; cout << "n" ; cout << "n" ; cout << "Cac bien CGIn" ; cout << "n" ; cout << "n" ; cout << "" ; for ( int i = 0 ; i < 24 ; i ++ ) { cout << "" << ENV [ i ] << "" ; // lay gia tri cua cac bien char * value = getenv ( ENV [ i ]. c_str () ); if ( value != 0 ){ cout << value ; } else { cout << "Bien moi truong nay khong ton tai." ; } cout << "n" ; } cout << "; cout << "n" ; cout << "n" ; return 0 ; }
CGI library in C ++
With real-world examples, you will need to perform many operations by your CGI Program. There is a CGI library written for the C ++ program, which you can download from: ftp://ftp.gnu.org/gnu/cgicc/ and follow these steps to install this library:
$tar xzf cgicc - X . X . X . tar . gz $cd cgicc - X . X . X / $ ./ configure -- prefix =/ usr $make $make install
You can check the Documentation available at: C ++ CGI Lib Documentation.
POST and GET methods
You have a number of situations when you need to transfer some information from the browser to the Web Server and finally to your CGI Program. The two methods used most often by the browser to transmit this information to Web Server are GET methods and POST methods.
Transfer information using the GET method
GET method of encrypted user information is appended to the request page. Is the page and encrypted information differentiated by characters? as follows:
http : //www.test.com/cgi-bin/cpp.cgi?key1=value1&key2=value2
The GET method is the default method for transmitting information from the browser to the Web Server and it creates a long string that appears in the browser's Location: box. Never use the GET method if you have a password or other sensitive information to transmit to the Server. The GET method has a size limit and you can transmit 1024 characters in a Request String.
When using the GET method, the information transmitted by the HTTP Header field is QUERY_STRING and will be accessible in CGI Program via the QUERY_STRING environment variable.
You can transmit information by concatenating a sequence of key / value pairs along with any URL or you can use HTML tags to transmit information using the GET method.
For example, the URL simply uses the GET method
This is a simple URL that will pass two values to the hello_get.py program using the method.
Below is the program to create the CGI Program cpp_get.cgi to handle the input provided by the web browser. We are using the CGI library in C ++, to make it easier to access the transmitted information:
#include #include #include #include #include #include #include #include #include using namespace std ; using namespace cgicc ; int main () { Cgicc formData ; cout << "Content-type:text/htmlrnrn" ; cout << "n" ; cout << "n" ; cout << "Su dung cac phuong thuc GET va POSTn" ; cout << "n" ; cout << "n" ; form_iterator fi = formData . getElement ( "first_name" ); if ( ! fi -> isEmpty () && fi != (* formData ). end ()) { cout << "First name: " << ** fi << endl ; } else { cout << "Khong co du lieu nao duoc nhap vao cho first name" << endl ; } cout << "
n" ; fi = formData . getElement ( "last_name" ); if ( ! fi -> isEmpty () && fi != (* formData ). end ()) { cout << "Last name: " << ** fi << endl ; } else { cout << "Khong co du lieu nao duoc nhap vao cho last name" << endl ; } cout << "
n" ; cout << "n" ; cout << "n" ; return 0 ; }
Now, compile the above program as follows:
$g ++ - o cpp_get . cgi cpp_get . cpp - lcgicc
Create cpp_get.cgi and place it in your CGI directory.
Example of FORM using the GET method
The following simple example will pass two values using HTML FORM and submit button. We are using the same CGI Program as cpp_get.cgi to handle this input.
You should read it
- Message in HTTP
- Header fields in HTTP
- What is HTTP
- Request (HTTP) in HTTP
- How to Install Tomcat on Windows 7
- What is HTTP Security Header? How to use HTTP Security Header
- Configure ISA Server 2006 HTTP Filter
- Response (Response) in HTTP
- How to set and list environment variables in Linux
- Declare variables in SQL Server
- Assign Structure to Diagram in Visio 2010 using Container
- Add Structure to Diagram in Visio 2010 using List and Container
Maybe you are interested
Clouds on Neptune may be created by the Sun The planet has the fastest wind in the solar system, reaching a speed of 2,400 km/h Samsung Galaxy S8 saved 20 lives in a boat crash in the Philippines You definitely won't be rich if you have 1 of the 10 signs below! If you want to succeed, remember these 15 things carefully What kind of creature is the real life Pokemon in Gia Lai?