2014-11-19 00:25:21 +08:00
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include "http_servlet.h"
|
|
|
|
|
|
2015-09-27 20:59:21 +08:00
|
|
|
|
http_servlet::http_servlet(acl::socket_stream* stream, acl::session* session)
|
|
|
|
|
: acl::HttpServlet(stream, session)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2017-09-01 17:43:12 +08:00
|
|
|
|
handlers_["/hello"] = &http_servlet::on_hello;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http_servlet::~http_servlet(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 17:43:12 +08:00
|
|
|
|
bool http_servlet::doError(request_t&, response_t& res)
|
2015-09-27 20:59:21 +08:00
|
|
|
|
{
|
|
|
|
|
res.setStatus(400);
|
2016-12-29 23:58:18 +08:00
|
|
|
|
res.setContentType("text/xml; charset=utf-8");
|
2015-09-27 20:59:21 +08:00
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
|
|
|
|
|
acl::string buf;
|
|
|
|
|
buf.format("<root error='some error happened!' />\r\n");
|
2016-12-29 23:58:18 +08:00
|
|
|
|
res.write(buf);
|
|
|
|
|
res.write(NULL, 0);
|
2015-09-27 20:59:21 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 17:43:12 +08:00
|
|
|
|
bool http_servlet::doOther(request_t&, response_t& res, const char* method)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
res.setStatus(400);
|
2016-12-29 23:58:18 +08:00
|
|
|
|
res.setContentType("text/xml; charset=utf-8");
|
2014-11-19 00:25:21 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
|
2015-09-27 20:59:21 +08:00
|
|
|
|
acl::string buf;
|
|
|
|
|
buf.format("<root error='unkown request method %s' />\r\n", method);
|
2016-12-29 23:58:18 +08:00
|
|
|
|
res.write(buf);
|
|
|
|
|
res.write(NULL, 0);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 17:43:12 +08:00
|
|
|
|
bool http_servlet::doGet(request_t& req, response_t& res)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
return doPost(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 17:43:12 +08:00
|
|
|
|
bool http_servlet::doPost(request_t& req, response_t& res)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
// <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>
|
|
|
|
|
/*
|
|
|
|
|
$<GET_COOKIES>
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-01 17:43:12 +08:00
|
|
|
|
const char* path = req.getPathInfo();
|
|
|
|
|
handler_t handler = path && *path ? handlers_[path] : NULL;
|
|
|
|
|
return handler ? (this->*handler)(req, res) : on_default(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool http_servlet::on_default(request_t& req, response_t& res)
|
|
|
|
|
{
|
|
|
|
|
return on_hello(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool http_servlet::on_hello(request_t& req, response_t& res)
|
|
|
|
|
{
|
2014-11-19 00:25:21 +08:00
|
|
|
|
res.setContentType("text/xml; charset=utf-8") // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
2015-08-09 22:42:51 +08:00
|
|
|
|
.setKeepAlive(req.isKeepAlive()) // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿֳ<F1B1A3B3><D6B3><EFBFBD><EFBFBD><EFBFBD>
|
2015-08-22 18:05:46 +08:00
|
|
|
|
.setContentEncoding(true) // <20>Զ<EFBFBD>֧<EFBFBD><D6A7>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-11-19 00:25:21 +08:00
|
|
|
|
.setChunkedTransferEncoding(true); // <20><><EFBFBD><EFBFBD> chunk <20><><EFBFBD>䷽ʽ
|
|
|
|
|
|
|
|
|
|
const char* param1 = req.getParameter("name1");
|
|
|
|
|
const char* param2 = req.getParameter("name2");
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> xml <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2015-12-20 21:24:28 +08:00
|
|
|
|
acl::xml1 body;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
body.get_root()
|
|
|
|
|
.add_child("root", true)
|
|
|
|
|
.add_child("params", true)
|
|
|
|
|
.add_child("param", true)
|
|
|
|
|
.add_attr("name1", param1 ? param1 : "null")
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("param", true)
|
|
|
|
|
.add_attr("name2", param2 ? param2 : "null");
|
|
|
|
|
acl::string buf;
|
|
|
|
|
body.build_xml(buf);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD>壬<EFBFBD><E5A3AC>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> chunk <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|
|
|
|
// res.write <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 0 <20>Ա<EFBFBD>ʾ chunk <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD>
|
2015-08-09 22:42:51 +08:00
|
|
|
|
return res.write(buf) && res.write(NULL, 0);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|