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

2015-01-27 05:56:17 · 作者: · 浏览: 33
) { return _mapCookieWrite[key]; } return std::string(); } void CFCGIHelper::SetCookie(std::string key, std::string value) { _mapCookieWrite[key] = value; _mapCookieRead[key] = value; } void CFCGIHelper::Str2KeyValue(std::string strSrc, std::map &mapKeyValue) { std::vector line; boost::split(line, strSrc, boost::is_any_of(";")); for (unsigned int i = 0; i < line.size(); i++) { std::vector vecT; boost::split(vecT, line[i], boost::is_any_of("=")); if (vecT.size() == 2) { vecT[0] = vecT[0].substr(vecT[0].find_first_not_of(' '), vecT[0].find_last_not_of(' ')); mapKeyValue[vecT[0]] = vecT[1]; }//end if }//end for } }//end class

HTMLPage.h

#ifndef _CHTMLPage_H_
#define _CHTMLPage_H_

#ifdef WIN32
#pragma once
#endif // WIN32


#include 
  
   

namespace kagula
{
	//https://github.com/einsteinx2/WaveBoxUrlRedirector/blob/master/wavebox_url_redirector.c
	class CHTMLPage
	{
	public:
		// Send the not found header, used for unregistered URLs
		void not_found(FCGX_Stream* out);

		// Send the not implemented header, used for request types other 
		// than GET and POST
		void unimplemented(FCGX_Stream* out);
	};
}

#endif
  


HTMLPage.cpp

#include "HTMLPage.h"

namespace kagula
{
	// Send the not found header, used for unregistered URLs
	void CHTMLPage::not_found(FCGX_Stream* out)
	{
		FCGX_FPrintF(out, "Status: 404 Not Found\r\n"
			"Content-type: text/html\r\n"
			"\r\n");
	}

	// Send the not implemented header, used for request types other 
	// than GET and POST
	void CHTMLPage::unimplemented(FCGX_Stream* out)
	{
		FCGX_FPrintF(out, "Status: 501 Method Not Implemented\r\n"
			"Allow: GET, POST\r\n"
			"Content-type: text/html\r\n"
			"\r\n");
	}
}

移植到CentOS

我们的程序依赖boost库,在centOS上安装boost之前建议使用

yum install libicu libicu-devel zlib zlib-develpython-devel bzip2-devel texinfo gcc-c++

安装ICU等依赖项,然后参考资料[14]进行安装。

在LInux上安装FCGI

[S1]下载FCGI包

wget http://www.fastcgi.com/dist/fcgi.tar.gz

[S2]参考下面的URL

http://redmine.lighttpd.net/projects/spawn-fcgi/wiki/Build

用第一次方法安装FCGI。可能会碰到EOF未定义的问题, 给报错的cpp文件加一个就可以了。

[S3]

默认头文件路径"/usr/include"

默认库文件路径"/usr/lib"

在这个路径下可以看到"libfcgi.a","libfcgi.so"文件或链接.

下面是CMakeLists.txt清单

#设置项目名称
project(TestFastCGI)

#要求CMake的最低版本为2.8
cmake_minimum_required(VERSION 2.8)

#For Boost library
add_definitions(-DBOOST_ALL_NO_LIB)

set(Boost_USE_STATIC_LIBS    OFF)  # using dynamic files
set(Boost_USE_MULTITHREADED  ON)
set(Boost_USE_STATIC_RUNTIME OFF)

find_package(Boost 1.55 COMPONENTS regex REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

#For FCGI library
set(FCGI_INCLUDE_DIRS /usr/include/)
set(FCGI_LIBRARY_DIRS /usr/lib/)
include_directories(${FCGI_INCLUDE_DIRS})

#用于将当前目录下的所有源文件的名字保存在变量 DIR_SRCS 中
aux_source_directory(. DIR_SRCS)

#用于指定从一组源文件 source1 source2 … sourceN(在变量DIR_SRCS中定义) 
#编译出一个可执行文件且命名为CMake_Tutorial1
add_executable(TestFastCGI ${DIR_SRCS})

#We need some third party libraries to run our program!
target_link_libraries(TestFastCGI ${Boost_LIBRARIES} fcgi)

我们可以查找CMake FindBoost的帮助,命令如下

cmake --help-module FindBoost

把包含cpp文件的目录上传到Cent OS后。

切换到Cent OS

在包换TestFastCGI.cpp的目录下,建立build子目录,使用下面的

cmake ..

make

编译出名为TestFastCGI可执行程序。

现在使用“./TestFastCGI”命令,可以正常运行,接下去配置到Apache上。

为Cent OS Apache配置mod_fcgid模块

下载mod_fcgid-2.3.9.tar.gz文件,解压后会看到

con