acl/lib_acl_cpp/samples/http/http_servlet/http_servlet.cpp

106 lines
3.0 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "http_servlet.h"
http_servlet::http_servlet(acl::redis_client_cluster& cluster, size_t max_conns)
{
// <20><><EFBFBD><EFBFBD> session <20><EFBFBD><E6B4A2><EFBFBD><EFBFBD>
session_ = new acl::redis_session(cluster, max_conns);
}
http_servlet::~http_servlet(void)
{
delete session_;
}
bool http_servlet::doUnknown(acl::HttpServletRequest&,
acl::HttpServletResponse& res)
{
res.setStatus(400);
res.setContentType("text/html; charset=");
// <20><><EFBFBD><EFBFBD> http <20><>Ӧͷ
if (res.sendHeader() == false)
return false;
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
acl::string buf("<root error='unkown request method' />\r\n");
(void) res.getOutputStream().write(buf);
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)
{
#if 0
2015-06-03 22:59:21 +08:00
const char* session_name = req.getSession().getAttribute("session_name");
if (*session_name == 0)
{
req.getSession().setAttribute("session_name", "name");
req.getSession().setMaxAge(100);
}
session_name = req.getSession().getAttribute("session_name");
2015-06-03 22:59:21 +08:00
const char* session_user = req.getSession().getAttribute("session_user");
if (*session_user == 0)
req.getSession().setAttribute("session_user", "user");
session_user = req.getSession().getAttribute("session_user");
#else
const char* session_name = "name", *session_user = "user";
#endif
// ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> cookie
const char* cookie_name = req.getCookieValue("cookie_name");
bool keep_alive = req.isKeepAlive();
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>
acl::xml1 body;
body.get_root()
.add_child("root", true)
.add_child("session", true)
.add_child("session_name", true)
.set_text(session_name)
.get_parent()
.add_child("session_user", true)
.set_text(session_user)
.get_parent()
.get_parent()
.add_child("cookie", true)
.add_child("cookie_name", true)
.set_text(cookie_name ? cookie_name : "")
.get_parent()
.get_parent()
.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);
#if 0
res.setContentType("text/xml; charset=utf-8") // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
.setKeepAlive(keep_alive) // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ񱣳ֳ<F1B1A3B3><D6B3><EFBFBD><EFBFBD><EFBFBD>
//.setContentLength(buf.length());
#else
res.setContentType("text/xml; charset=utf-8") // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
.setKeepAlive(keep_alive) // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ񱣳ֳ<F1B1A3B3><D6B3><EFBFBD><EFBFBD><EFBFBD>
.setContentEncoding(true) // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
.setChunkedTransferEncoding(true); // <20><><EFBFBD><EFBFBD> chunk <20><><EFBFBD>ʽ
#endif
//logger("access http://%s%s", req.getRemoteAddr(), req.getRequestUri());
// <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>
return res.write(buf) && res.write(NULL, 0);
}