acl/lib_acl_cpp/samples/http_servlet/http_servlet.cpp

145 lines
3.4 KiB
C++
Raw Normal View History

// http_servlet.cpp : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Ӧ<CCA8>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㡣
2014-11-19 00:25:21 +08:00
//
#include "stdafx.h"
#include "acl_cpp/lib_acl.hpp"
2022-01-28 02:46:55 +08:00
//static void (*format)(const char*, ...) = acl::log::msg1;
2014-11-19 00:25:21 +08:00
using namespace acl;
//////////////////////////////////////////////////////////////////////////
class http_servlet : public HttpServlet
{
public:
http_servlet(socket_stream* stream, session* session)
: HttpServlet(stream, session)
2014-11-19 00:25:21 +08:00
{
}
~http_servlet(void)
{
}
virtual bool doGet(HttpServletRequest& req, HttpServletResponse& res)
{
return doPost(req, res);
}
virtual bool doPost(HttpServletRequest& req, HttpServletResponse& res)
{
const char* sid = req.getSession().getAttribute("sid");
if (*sid == 0)
req.getSession().setAttribute("sid", "xxxxxx");
sid = req.getSession().getAttribute("sid");
const char* cookie1 = req.getCookieValue("name1");
const char* cookie2 = req.getCookieValue("name2");
// <20><><EFBFBD><EFBFBD> HTTP <20><>Ӧͷ
2014-11-19 00:25:21 +08:00
res.addCookie("name1", "value1");
res.addCookie("name2", "value2", ".test.com", "/", 3600 * 24);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧͷ<D3A6><CDB7>״̬<D7B4><CCAC>
2014-11-19 00:25:21 +08:00
//res.setStatus(200);
// <20><><EFBFBD>ַ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
2014-11-19 00:25:21 +08:00
if (0)
res.setContentType("text/xml; charset=gb2312");
else
{
res.setContentType("text/xml");
res.setCharacterEncoding("gb2312");
}
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>
xml1 body;
2014-11-19 00:25:21 +08:00
body.get_root().add_child("root", true)
.add_child("sessions", true)
.add_child("session", true)
.add_attr("sid", sid ? sid : "null")
.get_parent()
.get_parent()
.add_child("cookies", true)
.add_child("cookie", true)
.add_attr("name1", cookie1 ? cookie1 : "null")
.get_parent()
.add_child("cookie", true)
.add_attr("name2", cookie2 ? cookie2 : "null")
.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");
string buf;
body.build_xml(buf);
// <20><><EFBFBD><EFBFBD> http <20><>Ӧͷ
2014-11-19 00:25:21 +08:00
if (res.sendHeader() == false)
return false;
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
2014-11-19 00:25:21 +08:00
if (res.getOutputStream().write(buf) == -1)
return false;
return true;
}
protected:
private:
};
//////////////////////////////////////////////////////////////////////////
class master_service : public master_proc
{
public:
master_service() {}
~master_service() {}
protected:
virtual void on_accept(socket_stream* stream)
{
memcache_session session("127.0.0.1:11211");
http_servlet servlet(stream, &session);
2014-11-19 00:25:21 +08:00
servlet.setLocalCharset("gb2312");
servlet.doRun();
2014-11-19 00:25:21 +08:00
}
};
//////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
acl::acl_cpp_init();
master_service service;
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
2021-01-17 18:57:37 +08:00
if (argc == 1 || (argc >= 2 && strcmp(argv[1], "alone") == 0))
2014-11-19 00:25:21 +08:00
{
2022-01-28 02:46:55 +08:00
// format = (int (*)(const char*, ...)) printf;
2021-01-17 18:57:37 +08:00
printf("listen: 0.0.0.0|8888 ...\r\n");
service.run_alone("0.0.0.0|8888", NULL, 0); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>ʽ
2014-11-19 00:25:21 +08:00
}
else
{
#ifdef WIN32
2022-01-28 02:46:55 +08:00
// format = (void (*)(const char*, ...)) printf;
2021-01-17 18:57:37 +08:00
printf("listen: 0.0.0.0|8888 ...\r\n");
service.run_alone("0.0.0.0|8888", NULL, 1); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>ʽ
#else
service.run_daemon(argc, argv); // acl_master <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>
#endif
}
2014-11-19 00:25:21 +08:00
return 0;
}