acl/app/master/tools/master_ctld/http_servlet.cpp

118 lines
2.9 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "action/action.h"
#include "http_servlet.h"
http_servlet::http_servlet(acl::socket_stream* stream, acl::session* session,
const char* addr)
: acl::HttpServlet(stream, session)
, addr_(addr)
{
}
http_servlet::~http_servlet(void)
{
}
bool http_servlet::replyf(acl::HttpServletRequest& req,
acl::HttpServletResponse& res, int status, const char* fmt, ...)
{
acl::string buf;
va_list ap;
va_start(ap, fmt);
buf.vformat(fmt, ap);
va_end(ap);
return reply(req, res, status, buf);
}
bool http_servlet::reply(acl::HttpServletRequest&,
acl::HttpServletResponse& res, int status, const acl::string& buf)
{
res.setStatus(status)
.setContentType("text/plain; charset=utf-8")
.setContentLength(buf.size());
return res.write(buf, buf.size()) && res.write(NULL, 0);
}
bool http_servlet::reply_json(acl::HttpServletRequest&,
acl::HttpServletResponse& res, int status, const acl::string& data)
{
res.setStatus(status)
.setContentType("text/json; charset=utf-8")
.setContentLength(data.size());
return res.write(data, data.size()) && res.write(NULL, 0);
}
bool http_servlet::doError(acl::HttpServletRequest&,
acl::HttpServletResponse& res)
{
res.setStatus(400);
res.setContentType("text/xml; charset=utf-8");
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
acl::string buf;
buf.format("<root error='some error happened!' />\r\n");
res.write(buf);
res.write(NULL, 0);
return false;
}
bool http_servlet::doOther(acl::HttpServletRequest&,
acl::HttpServletResponse& res, const char* method)
{
res.setStatus(400);
res.setContentType("text/xml; charset=utf-8");
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
acl::string buf;
buf.format("<root error='unkown request method %s' />\r\n", method);
res.write(buf);
res.write(NULL, 0);
return false;
}
bool http_servlet::doGet(acl::HttpServletRequest& req,
acl::HttpServletResponse& res)
{
return doPost(req, res);
}
bool http_servlet::doPost(acl::HttpServletRequest& req,
acl::HttpServletResponse& res)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ http session <20><><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>֤
// <20><> master_service.cpp <20>ĺ<EFBFBD><C4BA><EFBFBD> thread_on_read <20><><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>
// memcached <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/*
const char* sid = req.getSession().getAttribute("sid");
if (*sid == 0)
req.getSession().setAttribute("sid", "xxxxxx");
sid = req.getSession().getAttribute("sid");
*/
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫȡ<D2AA><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> cookie <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
/*
*/
res.setContentType("text/xml; charset=utf-8") // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
.setKeepAlive(req.isKeepAlive()) // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ񱣳ֳ<F1B1A3B3><D6B3><EFBFBD><EFBFBD><EFBFBD>
.setContentEncoding(true) // <20>Զ<EFBFBD>֧<EFBFBD><D6A7>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
.setChunkedTransferEncoding(false); // chunk <20><><EFBFBD>ʽ
const char* cmd = req.getParameter("cmd");
if (cmd == NULL || *cmd == 0)
{
logger_error("cmd not found");
return replyf(req, res, 400, "%s", "no cmd");
}
#define EQ !strcasecmp
acl::string buf;
2017-07-22 18:08:23 +08:00
commands_action action(addr_, req, res, cmd);
int status = action.run(buf);
return reply_json(req, res, status, buf);
}