C++ Web编程

译者 yaronli (http://www.yaronspace.cn/blog)

原文地址:http://www.tutorialspoint.com/cplusplus/cpp_web_programming.htm

什么是CGI

CGI( Common Gateway Interface ),公共网关接口是一组标准,该标准定义了在 web server和普通脚本间是如何交换信息的。

Web浏览 (Web browsing)

为了更好的理解CGI的概念,我们看看当用户为了访问某个页面或者url点击链接时所发生的事情:

  • 你的浏览器联系到HTTP 服务器,然后请求对应的URL,即对应的文件;
  • Web服务器会解析这个URL,然后查找对应文件名。如果找到这个文件,Web服务器就会返回文件的内容给浏览器,否则 的话会返回一条错误信息;
  • Web 浏览器从Web服务器获得响应信息,然后解析返回的文件内容或者是错误信息;

但是,可以这样设置HTTP服务器,当一个文件被请求时,不是将文件内容返回,而是将它作为程序来执行,并将程序产生的输出返回给你的浏览器来显示。

公共网关接口(CGI)就是这样一种协议,它使应用程序(叫做CGI程序或者是CGI脚本)能够与Web服务器交互。CGI程序可以使用Python、perl、Shell、C或者C++来编写。

CGI的架构图

下图展现了CGI的简单架构:

Web服务器的配置

在你处理CGI程序之前,请确定你的Web服务器支持CGI程序并且它配置为支持能够处理CGI程序。所有的被HTTP服务器执行CGI程序都会预先配置在规定的目录,这个目录传统上被命名为/var/www/cgi-bin/,默认情况下cgi程序的扩展名为.cgi,尽管它们是C++可执行文件。

默认情况下,Apache Web服务器的CGI程序目录被配置在/var/www/cgi-bin/。如果你想更改目录,请修改在httpd.conf中下面的片段:

   AllowOverride None
   Options ExecCGI
   Order allow,deny
   Allow from all

Options All

译者注(关于apache cgi的配置请参考http://lamp.linux.gov.cn/Apache/ApacheMenu/howto/cgi.html#page-header)

第一CGI程序

考虑下面的代码:

#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;
}

编译上面的代码并将其命名为cpluscplus.cgi,将这个文件放置到/var/www/cgi-bin/下,在运行该程序之前,确保你使用chmod 755 cplusplus.cgi 命令来修改文件权限。现在你可以点击下面的链接cplusplus.cgi然后就会生成如下的内容:

Hello World! This is my first CGI program

面的C++程序简单将输出到STDOUT文件,在第一行输出Content-type:text/html\r\n\r\n一个额外的特点。这行被发送给浏览器并且指示在浏览器中显示的内容类型。你现在可能已经理解了CGI程序的概念了,现在你可以使用Python来编程更复杂的程序了。一个C++的CGI程序可以与外部的其他系统,如RDBM 交互信息。

HTTP

Content-type:text/html\r\n\r\n 作为HTTP头的一部分被发送给浏览器;所有的HTTP头都是下面的形式:

HTTP Field Name: Field Content

For Example
Content-type: text/html\r\n\r\n

Content-type:text/html\r\n\r\n 作为HTTP头的一部分被发送给浏览器;所有的HTTP头都是下面的形式:

HTTP Field Name: Field Content
 
For Example
Content-type: text/html\r\n\r\n

下面的HTTP头部你可能会经常用到:

下面的HTTP头部你可能会经常用到:

Header Description
Content-type: A MIME string defining the format of the file being returned. Example is Content-type:text/html
Expires: Date The date the information becomes invalid. This should be used by the browser to decide when a page needs to be refreshed. A valid date string should be in the format 01 Jan 1998 12:00:00 GMT.
Location: URL The URL that should be returned instead of the URL requested. You can use this filed to redirect a request to any file.
Last-modified: Date The date of last modification of the resource.
Content-length: N The 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 string

CGI环境变量


所有的CGI程序都可以访问到下面的环境变量,这些环境变量都写任何CGI程序都非常重要。

Variable Name Description
CONTENT_TYPE The data type of the content. Used when the client is sending attached content to the server. For example file upload etc.
CONTENT_LENGTH The length of the query information. It’s available only for POST requests
HTTP_COOKIE Return the set cookies in the form of key & value pair.
HTTP_USER_AGENT The User-Agent request-header field contains information about the user agent originating the request. Its name of the web browser.
PATH_INFO The path for the CGI script.
QUERY_STRING The URL-encoded information that is sent with GET method request.
REMOTE_ADDR The IP address of the remote host making the request. This can be useful for logging or for authentication purpose.
REMOTE_HOST The fully qualified name of the host making the request. If this information is not available then REMOTE_ADDR can be used to get IR address.
REQUEST_METHOD The method used to make the request. The most common methods are GET and POST.
SCRIPT_FILENAME The full path to the CGI script.
SCRIPT_NAME The name of the CGI script.
SERVER_NAME The server’s hostname or IP Address
SERVER_SOFTWARE The name and version of the software the server is running.

下面的CGI程序就是显示出所有的CGI环境变量。

#include <iostream>
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 Envrionment 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 value of 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;
}

C++ CGI库

对于真实的例子,你需要通过你的CGI程序做很多操作,下面是针对C++开发来说的CGI库,ftp://ftp.gnu.org/gnu/cgicc/

$tar xzf cgicc-X.X.X.tar.gz
$cd cgicc-X.X.X/
$./configure --prefix=/usr
$make
$make install

下面内容是介绍如何是CGI库来提取GET和POST方法的参数信息和如何设置cookie的,通过参考程序很好理解,暂不翻译了。

来自yaronspace.cn  本文链接:http://yaronspace.cn/blog/archives/1251