acl/lib_acl_cpp/samples/http_servlet2/http_servlet.cpp

109 lines
2.2 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-11-19 00:25:21 +08:00
#include "http_servlet.h"
http_servlet::http_servlet(acl::socket_stream* stream, acl::session* session)
2021-01-17 18:57:37 +08:00
: acl::HttpServlet(stream, session)
2014-11-19 00:25:21 +08:00
{
}
http_servlet::~http_servlet(void)
{
}
bool http_servlet::doError(acl::HttpServletRequest&, acl::HttpServletResponse&)
{
return false;
}
bool http_servlet::doUnknown(acl::HttpServletRequest&,
acl::HttpServletResponse& res)
{
res.setStatus(400);
res.setContentType("text/html; charset=");
// <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
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)
{
bool keep_alive = req.isKeepAlive();
res.setContentType("text/html; charset=utf-8") // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
.setKeepAlive(keep_alive) // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ񱣳ֳ<F1B1A3B3><D6B3><EFBFBD><EFBFBD><EFBFBD>
.setChunkedTransferEncoding(true); // chunked <20><><EFBFBD><EFBFBD>ģʽ
2014-11-19 00:25:21 +08:00
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
2020-07-26 16:28:16 +08:00
acl::string buf("first line\r\nsecond line\r\nthird line\r\n\r\n");
if (res.write(buf) == false)
2014-11-19 00:25:21 +08:00
{
logger_error("write error!");
return false;
}
for (int i = 0; i < 10; i++)
{
2021-01-17 18:57:37 +08:00
buf = "hello";
2020-07-26 16:28:16 +08:00
if (res.write(buf) == false)
2014-11-19 00:25:21 +08:00
{
logger_error("write error!");
return false;
}
2020-07-26 16:28:16 +08:00
if (res.write((acl::string)" ") == false)
2014-11-19 00:25:21 +08:00
{
logger_error("write error!");
return false;
}
2020-07-26 16:28:16 +08:00
if (res.write((acl::string)"world") == false)
2014-11-19 00:25:21 +08:00
{
logger_error("write error!");
return false;
}
2020-07-26 16:28:16 +08:00
if (res.write((acl::string)"\r\n") == false)
2014-11-19 00:25:21 +08:00
{
logger_error("write error!");
return false;
}
}
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 10; i++)
{
2020-07-26 16:28:16 +08:00
if (res.write((acl::string)"X") == false)
2014-11-19 00:25:21 +08:00
{
logger_error("write error!");
return false;
}
}
2020-07-26 16:28:16 +08:00
if (res.write((acl::string)"\r\n") == false)
2014-11-19 00:25:21 +08:00
{
logger_error("write error!");
return false;
}
}
// <20><><EFBFBD><EFBFBD>һ<EFBFBD>в<EFBFBD>д \r\n
2020-07-26 16:28:16 +08:00
if (res.write((acl::string)"Bye") == false)
2014-11-19 00:25:21 +08:00
{
logger_error("write error!");
return false;
}
return res.write(NULL, 0) && keep_alive;
}