Apache Fast CGI C++程序开发(一)

2015-01-27 05:56:17 · 作者: · 浏览: 32

Apache Fast CGI开发

Author: Kagula

发起日期:2014-11-08

最后更新日期:2014-11-23

环境:

[1]Win 8.1 64bits、 VMWare9,Cent OS 6.532bits, fcgid 2.3.9, boost 1.55, Apache 2.2.25,

[2] Cent OS 6.6,Apache 2.2.15, CMake 2.8.12.2, GCC 4.4.7, boost 1.57

Fast CGI相比CGI,响应速度提高了好几倍(免去了每次收到HTTP客户端请求,创建释放一次进程的开销),所以这里只记录Fast CGI的学习笔记。

准备

Apache是一个后台运行的程序,没有界面。所有的配置,都包含在配置文件里。主配置文件是httpd.conf

Windows 8平台

从官网下载httpd-2.2.25-win32-x86-openssl-0.9.8y.msi文件,选择自定义安装,勾选"Build Headers and Libraries"。这里假设默认路径安装。

Apache上有两种FastCGI模块,本文选择mod_fcgid。

加载mod_fcgid模块

从参考资料[7]下载mod_fcgid-2.3.9-crlf.zip文件,解压后用VisualStudio打开,为项目添加头文件search path

“C:\Program Files (x86)\Apache Software Foundation\Apache2.2\include”,

library path

" C:\Program Files (x86)\ApacheSoftware Foundation\Apache2.2\lib"

Release模式下编译出mod_fcgid.so文件后复制到”C:\Program Files (x86)\Apache Software Foundation\Apache2.2\modules “目录下。

在httpd.conf文件加入下面这行代码,加载fastcgi模块

LoadModule fcgid_module modules/mod_fcgid.so

装载新的module需要重启Apache服务器。

正文

第一个程序,为了说明流程。

下载http://www.fastcgi.com/dist/fcgi.tar.gz,解压后编译。

新建项目

头文件搜索路径

“D:\SDK\fcgi-2.4.1-SNAP-0311112127\include”

库文件搜索路径

“D:\SDK\fcgi-2.4.1-SNAP-0311112127\libfcgi\Debug”

添加依赖库,libfcgi.lib

并把“libfcgi.dll”放在EXE输出所在路径上。

TestFastCGI.exe源文件清单如下:

#include 
  
   
 
int _tmain(int argc, _TCHAR* argv[])
{
         FCGX_Stream*in, *out, *err;
         FCGX_ParamArrayenvp;
 
         while(FCGX_Accept(&in, &out, &err, &envp) >= 0)
         {
                   FCGX_PutS("content-type:text/plain\r\n\r\nHello World From FastCGI\r\n", out);
         }
         return0;
}
  


源代码编译成功后(为了方便调试程序,这里使用DEBUG模式编译),输出到下面的路径中

D:\Workspace\TestFastCGI\Debug

现在这个路径下应该有下面两个文件

TestFastCGI.exe(编译输出文件)

libfcgi.dll(从fcgi工程中复制过来的动态库)

注意:FastCGI可执行程序所依赖的DLL(或则so)文件必须在同一个目录中。

修改Apache的 httpd.conf文件

添加下面的代码

LoadModule fcgid_module modules/mod_fcgid.so

... ...

mod_fcgid.c>

D:\Workspace\TestFastCGI\Debug">

SetHandler fcgid-script

Order allow,deny

Allow from all

ScriptAlias /myfcgid "D:\Workspace\TestFastCGI\Debug\TestFastCGI.exe"

IdleTimeout 3600

ProcessLifeTime 7200

MaxProcessCount 64

DefaultMaxClassProcessCount 8

IPCConnectTimeout 300

IPCCommTimeout 7200

BusyTimeout 300

现在在浏览器中输入“localhost/myfcgid”就打印出了“HelloWorld From FastCGI”字符串。

我们看到/myfcgid路径被映射到了TestFastCGI.exe程序

Q:如何调试程序?

[S1]在任务管理器中停止Apache2.2服务。

[S2]调用“C:\Program Files(x86)\Apache Software Foundation\Apache2.2\bin\httpd.exe”启动Apache服务,现在可以找到程序“AttachProcess”了。

第二个程序,PUT/GET,Cookie,JSON

示例代码:

TestFastCGI.cpp(主程序)

/*!
 * \file TestFastCGI.cpp
 * \date 2014/11/09 20:47
 *
 * \author kagula
 * Contact: lee353086@163.com
 *
 * Fast CGI Demo
 */


#include 
  
   




#include "FCGIHelper.h"

int main(int argc, char* argv[])
{
	FCGX_Stream *in, *out, *err;
	FCGX_ParamArray envp;

	while (FCGX_Accept(&in, &out, &err, &envp) >= 0)
	{
		kagula::CFCGIHelper fcgi(in, out, err, envp);

		//first run, set cookie
#ifdef WIN32
		std::string strTest = fcgi.GetCookie("TEST");
#else
		//It's strange, difference from windows!
		std::string strTest = fcgi.GetCookie("TES");
#endif
		std::string strKagula = fcgi.GetCookie("kagula");

		std::string strJSON = "=====>\r\n";

		if (strTest.empty() == true)
		{
			fcgi.SetCookie("TEST", "first");
			strJSON.append("Set Test Cookie!\r\n");
		}

		if (strKagula.empty() == true)
		{
			fcgi.S