From 0ee13c8c06c73576f18179f702818cf883d5bbe9 Mon Sep 17 00:00:00 2001 From: wyy Date: Thu, 12 Jun 2014 23:58:47 +0800 Subject: [PATCH] fix bug about space in httpstr --- server/Husky/EpollServer.hpp | 6 +++++- server/Husky/HttpReqInfo.hpp | 34 +++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/server/Husky/EpollServer.hpp b/server/Husky/EpollServer.hpp index ad876e7..cf5f625 100644 --- a/server/Husky/EpollServer.hpp +++ b/server/Husky/EpollServer.hpp @@ -17,7 +17,6 @@ #include #include #include "HttpReqInfo.hpp" -#include "Limonp/InitOnOff.hpp" @@ -199,6 +198,11 @@ namespace Husky } HttpReqInfo httpReq(strRec); + if(!httpReq) + { + LogError("HttpReqInfo invalid."); + return false; + } if("GET" == httpReq.getMethod() && !_reqHandler.do_GET(httpReq, strRetByHandler)) { LogError("do_GET failed."); diff --git a/server/Husky/HttpReqInfo.hpp b/server/Husky/HttpReqInfo.hpp index abcc246..f5a985d 100644 --- a/server/Husky/HttpReqInfo.hpp +++ b/server/Husky/HttpReqInfo.hpp @@ -5,6 +5,7 @@ #include #include "Limonp/Logger.hpp" #include "Limonp/StringUtil.hpp" +#include "Limonp/InitOnOff.hpp" namespace Husky { @@ -70,25 +71,30 @@ namespace Husky } } - class HttpReqInfo + class HttpReqInfo: public InitOnOff { public: HttpReqInfo(const string& headerStr) + { + _setInitFlag(_init(headerStr)); + } + private: + bool _init(const string& headerStr) { size_t lpos = 0, rpos = 0; vector buf; rpos = headerStr.find("\n", lpos); if(string::npos == rpos) { - LogError("headerStr illegal."); - return; + LogError("headerStr[%s] illegal.", headerStr.c_str()); + return false; } string firstline(headerStr, lpos, rpos - lpos); trim(firstline); if(!split(firstline, buf, " ") || 3 != buf.size()) { - LogError("parse header first line failed."); - return; + LogError("parse header firstline[%s] failed.", firstline.c_str()); + return false; } _headerMap[KEY_METHOD] = trim(buf[0]); _headerMap[KEY_PATH] = trim(buf[1]); @@ -103,8 +109,8 @@ namespace Husky lpos = rpos + 1; if(lpos >= headerStr.size()) { - LogError("headerStr illegal"); - return; + LogError("headerStr[%s] illegal.", headerStr.c_str()); + return false; } //message header begin while(lpos < headerStr.size() && string::npos != (rpos = headerStr.find('\n', lpos)) && rpos > lpos) @@ -121,8 +127,8 @@ namespace Husky trim(v); if(k.empty()||v.empty()) { - LogError("headerStr illegal."); - return; + LogError("headerStr[%s] illegal.", headerStr.c_str()); + return false; } upper(k); _headerMap[k] = v; @@ -133,11 +139,17 @@ namespace Husky //body begin _body.assign(headerStr.substr(rpos)); trim(_body); + return true; } + 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 {