acl/lib_acl_cpp/samples/websocket/demo/http_servlet.cpp

271 lines
6.1 KiB
C++
Raw Normal View History

#include "stdafx.h"
2016-09-20 19:55:38 +08:00
#include "acl_cpp/http/websocket.hpp"
#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>
2016-09-20 19:55:38 +08:00
session_ = new acl::redis_session(cluster, max_conns);
}
http_servlet::~http_servlet(void)
{
delete session_;
}
bool http_servlet::doError(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;
buf.format("<root error='some error happened!' />\r\n");
(void) res.getOutputStream().write(buf);
return false;
}
2016-09-20 19:55:38 +08:00
bool http_servlet::doUnknown(acl::HttpServletRequest&,
acl::HttpServletResponse& res)
{
res.setStatus(400);
res.setContentType("text/html; charset=");
// <20><><EFBFBD><EFBFBD> http <20><>Ӧͷ
2016-09-20 19:55:38 +08:00
if (res.sendHeader() == false)
return false;
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
2016-09-20 19:55:38 +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)
{
printf("in doGet\r\n");
return doPost(req, res);
}
bool http_servlet::doPing(acl::websocket& in, acl::websocket& out)
{
unsigned long long len = in.get_frame_payload_len();
if (len == 0)
return out.send_frame_pong((const void*) NULL, 0);
2016-09-20 19:55:38 +08:00
out.reset().set_frame_fin(true)
.set_frame_opcode(acl::FRAME_PONG)
.set_frame_payload_len(len);
char buf[8192];
2019-06-10 13:00:25 +08:00
while (true) {
2016-09-20 19:55:38 +08:00
int ret = in.read_frame_data(buf, sizeof(buf) - 1);
if (ret == 0)
break;
2019-06-10 13:00:25 +08:00
if (ret < 0) {
2016-09-20 19:55:38 +08:00
printf("read_frame_data error\r\n");
return false;
}
buf[ret] = 0;
printf("read: [%s]\r\n", buf);
2019-06-10 13:00:25 +08:00
if (out.send_frame_data(buf, ret) == false) {
2016-09-20 19:55:38 +08:00
printf("send_frame_data error\r\n");
return false;
}
}
return true;
}
bool http_servlet::doPong(acl::websocket& in, acl::websocket&)
{
unsigned long long len = in.get_frame_payload_len();
if (len == 0)
return true;
char buf[8192];
2019-06-10 13:00:25 +08:00
while (true) {
2016-09-20 19:55:38 +08:00
int ret = in.read_frame_data(buf, sizeof(buf) - 1);
if (ret == 0)
break;
2019-06-10 13:00:25 +08:00
if (ret < 0) {
2016-09-20 19:55:38 +08:00
printf("read_frame_data error\r\n");
return false;
}
buf[ret] = 0;
printf("read: [%s]\r\n", buf);
}
return true;
}
bool http_servlet::doClose(acl::websocket&, acl::websocket&)
{
return false;
}
bool http_servlet::doMsg(acl::websocket& in, acl::websocket& out)
{
unsigned long long len = in.get_frame_payload_len();
out.reset().set_frame_fin(true)
.set_frame_opcode(acl::FRAME_TEXT)
.set_frame_payload_len(len);
char buf[8192];
2019-06-10 13:00:25 +08:00
while (true) {
2016-09-20 19:55:38 +08:00
int ret = in.read_frame_data(buf, sizeof(buf) - 1);
if (ret == 0)
break;
2019-06-10 13:00:25 +08:00
if (ret < 0) {
2016-09-20 19:55:38 +08:00
printf("read_frame_data error\r\n");
return false;
}
buf[ret] = 0;
printf("read: [%s]\r\n", buf);
2019-06-10 13:00:25 +08:00
if (out.send_frame_data(buf, ret) == false) {
2016-09-20 19:55:38 +08:00
printf("send_frame_data error\r\n");
return false;
}
}
2019-12-13 16:46:28 +08:00
#if 1
if (!sendBannder(out)) {
return false;
}
#endif
2016-09-20 19:55:38 +08:00
sleep(1);
char info[256];
2019-06-10 13:00:25 +08:00
snprintf(info, sizeof(info), "hello world!\r\n");
2016-09-20 19:55:38 +08:00
out.reset().set_frame_fin(true)
.set_frame_opcode(acl::FRAME_TEXT)
.set_frame_payload_len(strlen(info));
2019-06-10 13:00:25 +08:00
if (out.send_frame_data(info, strlen(info)) == false) {
2016-09-20 19:55:38 +08:00
printf("send_frame_data error\r\n");
return false;
}
sleep(1);
2019-06-10 13:00:25 +08:00
snprintf(info, sizeof(info), "hello zsx!\r\n");
2016-09-20 19:55:38 +08:00
out.reset().set_frame_fin(true)
.set_frame_opcode(acl::FRAME_TEXT)
.set_frame_payload_len(strlen(info));
2019-06-10 13:00:25 +08:00
if (out.send_frame_data(info, strlen(info)) == false) {
2016-09-20 19:55:38 +08:00
printf("send_frame_data error\r\n");
return false;
}
sleep(1);
2019-06-10 13:00:25 +08:00
snprintf(info, sizeof(info), "GoodBye!\r\n");
2016-09-20 19:55:38 +08:00
out.reset().set_frame_fin(true)
.set_frame_opcode(acl::FRAME_TEXT)
.set_frame_payload_len(strlen(info));
2019-06-10 13:00:25 +08:00
if (out.send_frame_data(info, strlen(info)) == false) {
2016-09-20 19:55:38 +08:00
printf("send_frame_data error\r\n");
return false;
}
2020-05-08 22:35:13 +08:00
return false;
2016-09-20 19:55:38 +08:00
}
2019-12-13 16:46:28 +08:00
bool http_servlet::sendBannder(acl::websocket& out)
{
char banner[256];
snprintf(banner, sizeof(banner), "Welcome!\r\n");
for (int i = 0; i < 5; i++) {
out.reset().set_frame_fin(true)
.set_frame_opcode(acl::FRAME_TEXT)
.set_frame_payload_len(strlen(banner));
if (!out.send_frame_data(banner, strlen(banner))) {
printf("send_frame_data error\r\n");
return false;
}
}
return true;
}
bool http_servlet::doWebSocket(acl::HttpServletRequest& req,
2016-09-20 19:55:38 +08:00
acl::HttpServletResponse&)
{
acl::socket_stream& ss = req.getSocketStream();
acl::websocket in(ss), out(ss);
2019-12-13 16:46:28 +08:00
#if 1
if (!sendBannder(out)) {
return false;
}
#endif
2019-06-10 13:00:25 +08:00
while (true) {
if (in.read_frame_head() == false) {
2016-09-20 19:55:38 +08:00
printf("read_frame_head error\r\n");
return false;
}
bool ret;
unsigned char opcode = in.get_frame_opcode();
printf("opcode: 0x%x\r\n", opcode);
2019-06-10 13:00:25 +08:00
switch (opcode) {
2016-09-20 19:55:38 +08:00
case acl::FRAME_PING:
ret = doPing(in, out);
break;
case acl::FRAME_PONG:
ret = doPong(in, out);
break;
case acl::FRAME_CLOSE:
ret = doClose(in, out);
break;
case acl::FRAME_TEXT:
2019-12-13 16:46:28 +08:00
ret = doMsg(in, out);
break;
2016-09-20 19:55:38 +08:00
case acl::FRAME_BINARY:
ret = doMsg(in, out);
break;
case acl::FRAME_CONTINUATION:
ret = false;
break;
default:
2019-12-13 16:46:28 +08:00
printf(">>got invalid\r\n");
2016-09-20 19:55:38 +08:00
ret = false;
break;
}
if (ret == false)
return false;
}
// XXX: NOT REACHED
return false;
}
bool http_servlet::doPost(acl::HttpServletRequest& req,
2016-09-20 19:55:38 +08:00
acl::HttpServletResponse& res)
{
res.setContentType("text/html; charset=utf-8") // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
.setContentEncoding(true) // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
.setChunkedTransferEncoding(false); // <20><><EFBFBD><EFBFBD> chunk <20><><EFBFBD>ʽ
2016-09-20 19:55:38 +08:00
acl::string html_file;
html_file << var_cfg_html_path << "/client.html";
acl::string buf;
2019-06-10 13:00:25 +08:00
if (acl::ifstream::load(html_file, &buf) == false) {
logger_error("load %s error %s",
html_file.c_str(), acl::last_serror());
return doError(req, res);
}
2016-09-20 19:55:38 +08:00
// <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>
2016-09-20 19:55:38 +08:00
return res.write(buf) && res.write(NULL, 0);
}