请教老哥一个C++的问题
错误问题
[19:51:18 (18438)][SgiParser.cpp:158][ERROR] Exception: basic_string::substr
这是SgiParser.cpp:158代码
/*************************************************************************
> Date: Mon 14 Dec 2015 05:41:31 PM CST
> Author: Simon
> Desc:
*************************************************************************/
#include "SgiParser.h"
#include <string>
#include "libredis/RedisClient.h"
#include "jsoncpp/json.h"
#include "Command.h"
#include "CommandMgr.h"
#include "libcrypto/UrlEncode.h"
CSgiParser::CSgiParser(uint32 nBufferSize):CHttpConnection(nBufferSize)
{
m_sRlpStr = "/v2/live/rlp";
m_sInternalKeepalive = "/fkeepalive";
}
CSgiParser::~CSgiParser()
{
m_mCommands = NULL;
}
void CSgiParser::Init()
{
m_mCommands = CCommandMgr::GetCommandPtr(m_nIndex);
dzlog_info("m_mCommands : %p, nIndex=%d\n", m_mCommands, int32(m_nIndex) );
}
void CSgiParser::Handle()
{
try
{
dzlog_debug("Entering Handle method");
if (NULL == m_mCommands)
{
SendCode(500);
dzlog_error("m_mCommands is NULL");
return;
}
ResetLast();
STR sUri;
GetRequestUri(sUri);
dzlog_debug("Request URI: %s", sUri.c_str());
// Parse command from URI
std::size_t pos = sUri.find('?');
if (pos == std::string::npos)
m_sCommand = sUri;
else
m_sCommand = sUri.substr(0, pos);
dzlog_debug("Parsed command: %s", m_sCommand.c_str());
// Handle keepalive request
if (m_sCommand == m_sInternalKeepalive)
{
SendCode(200);
dzlog_debug("Keepalive request handled");
return;
}
// Special handling for rlp interface
STR sCheckStr = m_sCommand;
std::string::size_type idx = m_sCommand.find(m_sRlpStr);
if (idx != std::string::npos)
{
sCheckStr = m_sRlpStr;
}
SetInterface(sCheckStr);
dzlog_debug("Interface set to: %s", sCheckStr.c_str());
// Check if the command is defined
Map_RequestCommand::iterator itFind = m_mCommands->find(sCheckStr);
if (itFind == m_mCommands->end())
{
itFind = m_mCommands->find("/");
if (itFind == m_mCommands->end())
{
dzlog_error("Command not found: %s", sCheckStr.c_str());
SendCode(404);
return;
}
}
InitUrlIndex();
dzlog_debug("URL index initialized");
// Parse parameters
if (sUri.size() > m_sCommand.size())
{
STR sParams = sUri.substr(m_sCommand.size() + 1);
dzlog_debug("Request parameters: %s", sParams.c_str());
std::size_t pos1;
while ((pos1 = sParams.find('=')) != std::string::npos)
{
STR sKey = sParams.substr(0, pos1);
sParams = sParams.substr(sKey.size() + 1);
STR sValue;
std::size_t pos2 = sParams.find('&');
if (pos2 == std::string::npos)
{
sValue = sParams;
sParams = "";
}
else
{
sValue = sParams.substr(0, pos2);
sParams = sParams.substr(sValue.size() + 1);
}
// Decrypt
CCryptoBase* pCryptor = itFind->second->GetCryptor();
if (pCryptor == NULL)
{
dzlog_error("CCryptoBase::Create failed");
SendCode(500);
return;
}
sValue = url_decode(sValue);
STR sRandStr;
sValue = pCryptor->Decrypto(sKey.c_str(), sValue.c_str(), sValue.length(), NULL, sRandStr);
if (!strcmp(sKey.c_str(), "i"))
{
m_sIRandStr = sRandStr;
}
else if (!strcmp(sKey.c_str(), "e"))
{
m_sERandStr = sRandStr;
}
dzlog_debug("Parameter: %s = %s", sKey.c_str(), sValue.c_str());
m_mapParam[sKey] = sValue;
}
}
dzlog_debug("Parameters parsed and decrypted");
itFind->second->Reset(this);
dzlog_debug("Command reset");
itFind->second->Handle();
dzlog_debug("Command handled");
AddHandleFinishCnt();
dzlog_debug("Handle method finished successfully");
}
catch (const std::exception& e)
{
SendCode(500);
// 输出详细的字符串处理信息
dzlog_error("String processing error occurred:");
dzlog_error("Request URI: %s", sUri.c_str());
dzlog_error("Parsed command: %s", m_sCommand.c_str());
dzlog_error("Command substring index: %zu", pos);
dzlog_error("Command length: %zu", m_sCommand.size());
dzlog_error("Exception: %s", e.what());
}
catch (...)
{
SendCode(500);
dzlog_error("Unknown exception caught");
}
}
void CSgiParser::ResetLast()
{
m_sCommand = "";
m_mapParam.clear();
}
STR CSgiParser::GetUriIdxContent(int32 idx)
{
if (idx >= (int32)m_vUriIdxVec.size())
{
return "";
}
return m_vUriIdxVec[idx];
}
void CSgiParser::InitUrlIndex()
{
StdStrUtils::SplitString(m_sCommand, m_vUriIdxVec, "/");
}
void CSgiParser::InitFileID()
{
size_t index = m_sCommand.find_last_of("/");
dzlog_debug("m_sCommand: %zu", m_sCommand);
dzlog_debug("Index: %zu, String size: %zu", index, m_sCommand.size());
if (index != std::string::npos && index + 1 < m_sCommand.size()) {
STR tmp = m_sCommand.substr(index + 1);
dzlog_debug("File ID: %s", tmp.c_str()); // 在这里添加打印语句
} else {
dzlog_error("Invalid index for substr: %zu", index);
}
}
STR CSgiParser::GetCommand() const
{
return m_sCommand;
}
STR CSgiParser::GetQuery(const SGI_STRING &sQuery) const
{
Map_Str::const_iterator iter = m_mapParam.find(sQuery);
if( iter != m_mapParam.end() )
return iter->second;
return "";
}