Apache Fast CGI C++程序开发(三)
l CFCGIHelper::IsGetRequest() { if (_requestMethod == "GET") return true; return false; } bool CFCGIHelper::IsPOSTRequest() { if (_requestMethod == "POST") return true; return false; } void CFCGIHelper::grab_envs() { _remoteIP = FCGX_GetParam("REMOTE_ADDR", _envp) == NULL ? "" : FCGX_GetParam("REMOTE_ADDR", _envp); _serverName = FCGX_GetParam("SERVER_NAME", _envp) == NULL ? "" : FCGX_GetParam("SERVER_NAME", _envp); _requestURI = FCGX_GetParam("REQUEST_URI", _envp) == NULL ? "" : FCGX_GetParam("REQUEST_URI", _envp); _requestMethod = FCGX_GetParam("REQUEST_METHOD", _envp) == NULL ? "" : FCGX_GetParam("REQUEST_METHOD", _envp); _requestMethod = _requestMethod.substr(_requestMethod.find_first_not_of(' '), _requestMethod.find_last_not_of(' ')); transform(_requestMethod.begin(), _requestMethod.end(), _requestMethod.begin(), toupper); _queryString = FCGX_GetParam("QUERY_STRING", _envp) == NULL ? "" : FCGX_GetParam("QUERY_STRING", _envp); _contentType = FCGX_GetParam("CONTENT_TYPE", _envp) == NULL ? "" : FCGX_GetParam("CONTENT_TYPE", _envp); _contentLen = FCGX_GetParam("CONTENT_LENGTH", _envp) == NULL ? "" : FCGX_GetParam("CONTENT_LENGTH", _envp); _httpCookie = FCGX_GetParam("HTTP_COOKIE", _envp) == NULL ? "" : FCGX_GetParam("HTTP_COOKIE", _envp); } void CFCGIHelper::test_envs() { FCGX_FPrintF(_out, "Content-type: text/plain\r\n\r\n"); FCGX_FPrintF(_out, "[CFCGIHelper Test Page]\r\n"); FCGX_FPrintF(_out, "REQUEST_URI: %s\r\n", _requestURI.c_str()); FCGX_FPrintF(_out, "REMOTE_ADDR: %s\r\n", _remoteIP.c_str()); FCGX_FPrintF(_out, "REQUEST_METHOD: %s\r\n", _requestMethod.c_str()); FCGX_FPrintF(_out, "SERVER_NAME: %s\r\n", _serverName.c_str()); FCGX_FPrintF(_out, "SERVER_PORT: %s\r\n", FCGX_GetParam("SERVER_PORT", _envp) == NULL ? "does not exist" : FCGX_GetParam("SERVER_PORT", _envp)); FCGX_FPrintF(_out, "SERVER_PROTOCOL: %s\r\n", FCGX_GetParam("SERVER_PROTOCOL", _envp) == NULL ? "is not exist" : FCGX_GetParam("SERVER_PROTOCOL", _envp)); FCGX_FPrintF(_out, "QUERY_STRING: %s\r\n", _queryString.c_str()); FCGX_FPrintF(_out, "keyValue2Str():\r\n%s\r\n", keyValue2Str(_mapRequestData).c_str()); } void CFCGIHelper::extractFromBody(const char* inBuffer) { std::vector
record; record = split(std::string(inBuffer), "------"); //Test Regular Expression//http://rubular.com/r/XY3EwbmdNH boost::regex reg("(?:[\\\s\\\S]*)Content-Disposition: form-data; name=\"(\\\w+)\"(?:[\\\s]*)(\\\w+)(?:[\\\s]*)"); for (std::string::size_type i = 0; i < record.size(); i++) { boost::cmatch cmat; //Remark: //boost::regex_match can extract string between left brace and right brace symbol! //boost::regex_match match the entire string! if (boost::regex_match(record[i].c_str(), cmat, reg) && cmat.size() == 3) { _mapRequestData[cmat[1]] = cmat[2]; } } } void CFCGIHelper::extractFromQueryString() { std::vector
record; boost::split(record, _queryString, boost::is_any_of("&")); for (std::string::size_type i = 0; i < record.size(); i++) { std::vector
kv; boost::split(kv, record[i], boost::is_any_of("=")); if (kv.size() == 2 && kv[0].size() > 0 && kv[1].size() > 0) { _mapRequestData[kv[0]] = kv[1]; } }//end for }//end func std::string CFCGIHelper::keyValue2Str(std::map
&mapKeyValue) { std::string strR; std::map
::iterator iter = mapKeyValue.begin(); while (iter != mapKeyValue.end()) { strR.append(iter->first); strR.append("="); strR.append(iter->second); strR.append("\r\n"); iter++; } return strR; } void CFCGIHelper::SetResponse(std::string strJSON) { _strResponse = strJSON; } std::string CFCGIHelper::GetCookie(std::string key) { if (_mapCookieRead.find(key) != _mapCookieRead.end()) { return _mapCookieRead[key]; } if (_mapCookieWrite.find(key) != _mapCookieWrite.end()