fix bug about space in httpstr

This commit is contained in:
wyy 2014-06-12 23:58:47 +08:00
parent 8f5d08b7ae
commit 0ee13c8c06
2 changed files with 28 additions and 12 deletions

View File

@ -17,7 +17,6 @@
#include <sys/epoll.h> #include <sys/epoll.h>
#include <fcntl.h> #include <fcntl.h>
#include "HttpReqInfo.hpp" #include "HttpReqInfo.hpp"
#include "Limonp/InitOnOff.hpp"
@ -199,6 +198,11 @@ namespace Husky
} }
HttpReqInfo httpReq(strRec); HttpReqInfo httpReq(strRec);
if(!httpReq)
{
LogError("HttpReqInfo invalid.");
return false;
}
if("GET" == httpReq.getMethod() && !_reqHandler.do_GET(httpReq, strRetByHandler)) if("GET" == httpReq.getMethod() && !_reqHandler.do_GET(httpReq, strRetByHandler))
{ {
LogError("do_GET failed."); LogError("do_GET failed.");

View File

@ -5,6 +5,7 @@
#include <string> #include <string>
#include "Limonp/Logger.hpp" #include "Limonp/Logger.hpp"
#include "Limonp/StringUtil.hpp" #include "Limonp/StringUtil.hpp"
#include "Limonp/InitOnOff.hpp"
namespace Husky namespace Husky
{ {
@ -70,25 +71,30 @@ namespace Husky
} }
} }
class HttpReqInfo class HttpReqInfo: public InitOnOff
{ {
public: public:
HttpReqInfo(const string& headerStr) HttpReqInfo(const string& headerStr)
{
_setInitFlag(_init(headerStr));
}
private:
bool _init(const string& headerStr)
{ {
size_t lpos = 0, rpos = 0; size_t lpos = 0, rpos = 0;
vector<string> buf; vector<string> buf;
rpos = headerStr.find("\n", lpos); rpos = headerStr.find("\n", lpos);
if(string::npos == rpos) if(string::npos == rpos)
{ {
LogError("headerStr illegal."); LogError("headerStr[%s] illegal.", headerStr.c_str());
return; return false;
} }
string firstline(headerStr, lpos, rpos - lpos); string firstline(headerStr, lpos, rpos - lpos);
trim(firstline); trim(firstline);
if(!split(firstline, buf, " ") || 3 != buf.size()) if(!split(firstline, buf, " ") || 3 != buf.size())
{ {
LogError("parse header first line failed."); LogError("parse header firstline[%s] failed.", firstline.c_str());
return; return false;
} }
_headerMap[KEY_METHOD] = trim(buf[0]); _headerMap[KEY_METHOD] = trim(buf[0]);
_headerMap[KEY_PATH] = trim(buf[1]); _headerMap[KEY_PATH] = trim(buf[1]);
@ -103,8 +109,8 @@ namespace Husky
lpos = rpos + 1; lpos = rpos + 1;
if(lpos >= headerStr.size()) if(lpos >= headerStr.size())
{ {
LogError("headerStr illegal"); LogError("headerStr[%s] illegal.", headerStr.c_str());
return; return false;
} }
//message header begin //message header begin
while(lpos < headerStr.size() && string::npos != (rpos = headerStr.find('\n', lpos)) && rpos > lpos) while(lpos < headerStr.size() && string::npos != (rpos = headerStr.find('\n', lpos)) && rpos > lpos)
@ -121,8 +127,8 @@ namespace Husky
trim(v); trim(v);
if(k.empty()||v.empty()) if(k.empty()||v.empty())
{ {
LogError("headerStr illegal."); LogError("headerStr[%s] illegal.", headerStr.c_str());
return; return false;
} }
upper(k); upper(k);
_headerMap[k] = v; _headerMap[k] = v;
@ -133,11 +139,17 @@ namespace Husky
//body begin //body begin
_body.assign(headerStr.substr(rpos)); _body.assign(headerStr.substr(rpos));
trim(_body); trim(_body);
return true;
} }
public: public:
string& operator[] (const string& key) //string& operator[] (const string& key)
//{
// return _headerMap[key];
//}
const string& set(const string& key, const string& value)
{ {
return _headerMap[key]; return _headerMap[key] = value;
} }
bool find(const string& key, string& res)const bool find(const string& key, string& res)const
{ {