2017-07-20 23:26:51 +08:00
|
|
|
|
#include "stdafx.h"
|
2018-02-27 11:24:15 +08:00
|
|
|
|
#include "daemon/version.h"
|
2017-07-20 23:26:51 +08:00
|
|
|
|
#include "action/action.h"
|
|
|
|
|
#include "http_servlet.h"
|
|
|
|
|
|
|
|
|
|
http_servlet::http_servlet(acl::socket_stream* stream, acl::session* session,
|
2017-07-22 21:32:21 +08:00
|
|
|
|
const char* addr, const char* path)
|
2017-07-20 23:26:51 +08:00
|
|
|
|
: acl::HttpServlet(stream, session)
|
|
|
|
|
, addr_(addr)
|
2017-07-22 21:32:21 +08:00
|
|
|
|
, conf_(path)
|
2017-07-20 23:26:51 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2018-02-12 15:47:57 +08:00
|
|
|
|
res.setContentType("text/plain")
|
|
|
|
|
.setKeepAlive(req.isKeepAlive());
|
|
|
|
|
|
2018-02-27 11:24:15 +08:00
|
|
|
|
acl::string body;
|
|
|
|
|
body.format("%s version: %s\r\n", MASTER_NAME, MASTER_VERSION);
|
2018-02-12 15:47:57 +08:00
|
|
|
|
return reply(req, res, 200, body);
|
2017-07-20 23:26:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2017-07-22 21:32:21 +08:00
|
|
|
|
action.set_conf(conf_);
|
2017-07-22 18:08:23 +08:00
|
|
|
|
int status = action.run(buf);
|
2017-07-20 23:26:51 +08:00
|
|
|
|
return reply_json(req, res, status, buf);
|
|
|
|
|
}
|