FastCGI收发jpg图片的例子

测试代码

TestFCGI.cpp

#include "FCGIHelper.h"
#include "StringHelper.h"

#ifdef WIN32
#pragma comment(lib,"libfcgi.lib")
#endif

int main(int argc, char* argv[])  
{  
	FCGX_Request request;  
	FCGX_Init();  
	FCGX_InitRequest(&request, 0, 0);  

	bool isNeedResponseTheSameAgain_OnlyNeedInPostMethodRequest = false;

	while(FCGX_Accept_r(&request) >= 0)  
	{  
		kagula::CFCGIHelper fcgi(&request);

		/*
		//return jpg file in disk, only valid in windows OS.
		//after every XXXXFinish routine, you need call continue statement.
		fcgi.WriteJpgFileResponseAndFinish("d:\\t.jpg");
		continue;
		*/

		if (isNeedResponseTheSameAgain_OnlyNeedInPostMethodRequest)
		{
			isNeedResponseTheSameAgain_OnlyNeedInPostMethodRequest = false;
			fcgi.WriteLastSendAndFinish();
			continue;
		}
		

		if (fcgi._requestData.data.find("testType")!=fcgi._requestData.data.end())
		{
			string testType = kagula::StringHelper::Array2Str(fcgi._requestData.data["testType"].value);

			if (testType == "jpg" )
			{
				//return jpg data.
				if (fcgi._requestData.data.find("MyJPGFile")!=fcgi._requestData.data.end())
				{
					//if client using post method, you write jpg response, fast cgi will call us again!
					//image get only by get method request, so here(post method) will be called by twice.
					//if client using get method request, no need response the same again.
					fcgi.WriteResponseAndFinish("jpg","",fcgi._requestData.data["MyJPGFile"].value);
					isNeedResponseTheSameAgain_OnlyNeedInPostMethodRequest = true;
					continue;
				}
			} else if (testType == "json")
			{
				//return json object.
				fcgi.WriteResponseAndFinish("json","{name:\"kagula\"}",std::list<char>());
				continue;
			}else if (testType == "text")
			{
				//return html page.
				fcgi.WriteResponseAndFinish("text","<h1>Hello,World</h1>",std::list<char>());
				continue;
			}
		}	

		//return fcgi run status.
		fcgi.WriteResponseAndFinish("","",std::list<char>());
	}  
	return 0;  
}  


依赖的代码

FCGIHelper.cpp

#include "FCGIHelper.h"

#include <algorithm>
#include <vector>

#include <boost/regex.hpp>
#include <boost/algorithm/string.hpp>

#include "StringHelper.h"
#include "FileHelper.h"

#pragma warning ( disable : 4129 ) 

namespace kagula
{
	std::vector<char> CFCGIHelper::_vecLastSend;

	bool CFCGIHelper::getBlockInfo(std::list<char> &block,std::string &outBlockInfo)
	{
		if ( block.size()<6 )
		{
			return false;
		}		

		std::list<char> buf;
		std::list<char> retData;
		for (std::list<char>::iterator iter = block.begin();
			iter!=block.end();iter++)
		{
			retData.push_back(*iter);

			buf.push_back(*iter);
			if (buf.size()>=4)
			{
				if (kagula::StringHelper::IsEqualBinary(buf, kagula::StringHelper::g_crlfcrlf))
				{
					break;
				}
				buf.pop_front();
			}//end if
		}//end for

		//
		outBlockInfo= kagula::StringHelper::Array2Str(retData);
		if (outBlockInfo.size()>4)
		{
			outBlockInfo = outBlockInfo.substr(0,outBlockInfo.size()-4);
		}

		return true;
	}

	bool CFCGIHelper::getBlockBody(const std::list<char> &block, std::list<char> &outBlockbody)
	{
		std::list<char> listRet;
		if ( block.size()<6 )
		{
			return false;
		}

		bool bFind = false;
		std::list<char> buf;

		std::list<char>::const_iterator iter = block.begin();
		for (;iter!=block.end();iter++)
		{
			buf.push_back(*iter);
			if (buf.size()>=4)
			{
				if (kagula::StringHelper::IsEqualBinary(buf,kagula::StringHelper::g_crlfcrlf))
				{
					bFind = true;
					break;
				}
				buf.pop_front();
			}//end if
		}//end for

		if (!bFind)
		{
			return false;
		}

		iter++;
		while(iter!=block.end())
		{
			outBlockbody.push_back(*iter);
			iter++;
		}

		return true;
	}

	//输入数据的形式“Content-Disposition: form-data; name="userKey"”
	std::map<std::string,std::string> CFCGIHelper::extractSemicolonData(std::string &strInfo)
	{
		std::map<std::string,std::string> mapReturn;

		std::vector<std::string> vecItems;
		boost::split(vecItems, strInfo, boost::is_any_of(";\n"));
		for (s
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值