add post for server

This commit is contained in:
wyy 2014-04-03 10:00:40 -07:00
parent d0cfd042a4
commit ee8ec6955a
3 changed files with 59 additions and 26 deletions

View File

@ -37,13 +37,14 @@ namespace Husky
virtual ~IRequestHandler(){};
public:
virtual bool do_GET(const HttpReqInfo& httpReq, string& res) const = 0;
virtual bool do_POST(const HttpReqInfo& httpReq, string& res) const = 0;
};
class EpollServer
{
private:
static const size_t LISTEN_QUEUE_LEN = 1024;
static const size_t RECV_BUFFER_SIZE = 1024 * 8;
static const size_t RECV_BUFFER_SIZE = 1024*4;
static const int MAXEPOLLSIZE = 512;
private:
@ -75,7 +76,6 @@ namespace Husky
//int clientSock;
sockaddr_in clientaddr;
socklen_t nSize = sizeof(clientaddr);
//char recvBuf[RECV_BUFFER_SIZE];
struct epoll_event events[MAXEPOLLSIZE];
int nfds, clientSock;
@ -190,8 +190,12 @@ namespace Husky
}
string strRec, strSnd, strRetByHandler;
strRec.resize(RECV_BUFFER_SIZE);
int nRetCode = recv(sockfd, (char*)strRec.c_str(), strRec.size(), 0);
char recvBuf[RECV_BUFFER_SIZE];
int nRetCode = -1;
while(true)
{
memset(recvBuf, 0, sizeof(recvBuf));
nRetCode = recv(sockfd, recvBuf, sizeof(recvBuf) - 1, 0);
if(-1 == nRetCode)
{
LogDebug(strerror(errno));
@ -199,17 +203,29 @@ namespace Husky
}
if(0 == nRetCode)
{
LogDebug("client socket closed gracefully.");
LogDebug("client socket orderly shut down");
return false;
}
strRec += recvBuf;
if(nRetCode != sizeof(recvBuf) - 1)
{
break;
}
}
HttpReqInfo httpReq(strRec);
if(!_reqHandler->do_GET(httpReq, strRetByHandler))
if("GET" == httpReq.getMethod() && !_reqHandler->do_GET(httpReq, strRetByHandler))
{
LogError("do_GET failed.");
return false;
}
if("POST" == httpReq.getMethod() && !_reqHandler->do_POST(httpReq, strRetByHandler))
{
LogError("do_POST failed.");
return false;
}
string_format(strSnd, HTTP_FORMAT, CHARSET_UTF8, strRetByHandler.length(), strRetByHandler.c_str());
if(-1 == send(sockfd, strSnd.c_str(), strSnd.length(), 0))
{
LogError(strerror(errno));

View File

@ -135,7 +135,8 @@ namespace Husky
//message header end
//body begin
_body.assign(headerStr.substr(rpos));
trim(_body);
}
public:
string& operator[] (const string& key)
@ -150,14 +151,23 @@ namespace Husky
{
return _find(_methodGetMap, argKey, res);
}
bool POST(const string& argKey, string& res)const
//bool POST(const string& argKey, string& res)const
//{
// return _find(_methodPostMap, argKey, res);
//}
const string& getMethod() const
{
return _find(_methodPostMap, argKey, res);
return _headerMap.find(KEY_METHOD)->second;
}
const string& getBody() const
{
return _body;
}
private:
std::unordered_map<string, string> _headerMap;
std::unordered_map<string, string> _methodGetMap;
std::unordered_map<string, string> _methodPostMap;
//std::unordered_map<string, string> _methodPostMap;
string _body;
//public:
friend ostream& operator<<(ostream& os, const HttpReqInfo& obj);
private:
@ -215,7 +225,7 @@ namespace Husky
inline std::ostream& operator << (std::ostream& os, const Husky::HttpReqInfo& obj)
{
return os << obj._headerMap << obj._methodGetMap << obj._methodPostMap;
return os << obj._headerMap << obj._methodGetMap/* << obj._methodPostMap*/ << obj._body;
}
}

View File

@ -34,6 +34,13 @@ class ReqHandler: public IRequestHandler
strSnd << words;
return true;
}
virtual bool do_POST(const HttpReqInfo& httpReq, string& strSnd) const
{
vector<string> words;
_segment.cut(httpReq.getBody(), words);
strSnd << words;
return true;
}
private:
MixSegment _segment;
};