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

2015-01-27 05:56:17 · 作者: · 浏览: 40
etCookie("kagula", "second"); strJSON.append("Set kagula Cookie!\r\n"); } if (strTest.empty() == false && strKagula.empty() == false) { //if have cookies, output! strJSON.append("{\"TEST\",\""); strJSON.append(strTest); strJSON.append("\"},"); strJSON.append("{\"kagula\",\""); strJSON.append(strKagula); strJSON.append("\"}"); } fcgi.SetResponse(strJSON); }//end while return 0; }

FCGHelper.h

/*!
 * \file FCGIHelper.h
 * \date 2014/11/09 20:36
 *
 * \author kagula
 * Contact: lee353086@163.com
 *
 * Function: JSon Server Demo
 *
 * Support:POST/GET Method Request and return JSON object.
*/
#ifndef _CFGIHELPER_H_
#define _CFGIHELPER_H_

#include 
#include 
   
     #include 
    
      static const long MAX_BODY_LEN = 2*1024; namespace kagula { /* LastUpdateData: 2013-11-16 Test Environment [1]Win8.1, Visual Studio 2013 Update4, boost 1.55 [2]CentOS6.5, CMake 2.8.12.2, GCC 4.4.7 Reference [1]CGI Environment Variables http://www.cgi101.com/class/ch3/text.
     html */ class CFCGIHelper { public: CFCGIHelper(FCGX_Stream *in, FCGX_Stream *out, FCGX_Stream *err, FCGX_ParamArray &envp); ~CFCGIHelper(); bool IsGetRequest(); bool IsPOSTRequest(); void SetResponse(std::string strJSON); void SetCookie(std::string key, std::string value); std::string GetCookie(std::string key); std::map
     
       _mapRequestData; private: std::string _strResponse; FCGX_Stream *_in; FCGX_Stream *_out; FCGX_Stream *_err; FCGX_ParamArray _envp; std::string _remoteIP; std::string _serverName; std::string _requestURI; std::string _requestMethod; std::string _queryString; std::string _contentType; std::string _contentLen; std::string _httpCookie; std::map
      
        _mapCookieRead; std::map
       
         _mapCookieWrite; void grab_envs(); void test_envs(); void extractFromBody(const char* inBuffer); void extractFromQueryString(); void Str2KeyValue(std::string strSrc, std::map
        
          &mapKeyValue); std::string keyValue2Str(std::map
         
           &mapKeyValue); }; } #endif
         
        
       
      
     
    
   


FCGHelper.cpp

/*!
 * \file FCGIHelper.cpp
 * \date 2014/11/09 20:48
 *
 * \author kagula
 * Contact: lee353086@163.com
 *
 * Support:POST/GET Method Request and return JSON object.
 */
#include "FCGIHelper.h"

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #pragma warning ( disable : 4129 ) namespace kagula { std::vector
      
        split(std::string str, std::string pattern) { std::string::size_type pos; std::vector
       
         result; str += pattern; std::string::size_type size = str.size(); for (unsigned int i = 0; i < size; i++) { pos = str.find(pattern, i); if (pos < size) { std::string s = str.substr(i, pos - i); result.push_back(s); i = pos + pattern.size() - 1; } } return result; } CFCGIHelper::CFCGIHelper(FCGX_Stream *in, FCGX_Stream *out, FCGX_Stream *err, FCGX_ParamArray &envp) { //initialization internal data _in = in; _out = out; _err = err; _envp = envp; //get environment grab_envs(); //extract query key-value char body[MAX_BODY_LEN]; memset(body, 0, sizeof(body)); if (IsPOSTRequest()) { FCGX_GetStr(body, sizeof(body), in); if (_contentType == "application/x-www-form-urlencoded" || _contentType == "application/x-url-encoded") { _queryString = (char *)&body; } else { extractFromBody(body); }//end if }//end if if (_queryString.empty() == false) { extractFromQueryString(); } Str2KeyValue(_httpCookie, _mapCookieRead); }//end func CFCGIHelper::~CFCGIHelper() { if (_strResponse.empty()) { test_envs(); } else { //define header! std::string body; if (_mapCookieWrite.size() > 0) { //define cookie if has! body = "Content-type: text/html; charset=UTF-8\r\n"; std::map
        
         ::iterator iterCookie = _mapCookieWrite.begin(); /* example: set-cookie:key=value:a:b; expires=Sun, 08-Jun-2014 14:27:09 GMT; path=/; domain=.domain.com\r\n set-cookie:key2=value2:a:b; expires=Sat, 08-Dec-2012 14:27:09 GMT; path=/; domain=.domain.com; HttpOnly\r\n\r\n */ while (iterCookie != _mapCookieWrite.end()) { std::string cookie = "Set-Cookie: "; cookie.append(iterCookie->first); cookie.append("="); cookie.append(iterCookie->second); cookie.append("\r\n"); body.append(cookie); iterCookie++; } /* indicate HTTP header is end. if no the "\r\n\r\n" flag, will throw out internal error! */ body.append("\r\n"); } else { body = "Content-type: application/json\r\n\r\n"; } body.append(_strResponse); FCGX_FPrintF(_out, "%s\r\n", body.c_str()); } } boo