acl/app/wizard_demo/httpd_proxy/http_servlet.cpp

243 lines
6.2 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "tcp_transfer.h"
2022-01-24 18:36:08 +08:00
#include "http_transfer.h"
2019-03-14 15:24:11 +08:00
#include "http_servlet.h"
2022-01-24 18:36:08 +08:00
http_servlet::http_servlet(acl::socket_stream* stream, acl::session* session,
2022-08-26 17:51:41 +08:00
acl::sslbase_conf* ssl_conf, int http_port /* 80 */, int https_port /* 443 */)
2019-03-14 15:24:11 +08:00
: acl::HttpServlet(stream, session)
2022-08-26 17:51:41 +08:00
, ssl_conf_(ssl_conf)
, http_port_(http_port)
, https_port_(https_port)
2019-03-14 15:24:11 +08:00
{
2022-01-24 18:36:08 +08:00
handlers_["/hello"] = &http_servlet::on_hello;
2019-03-14 15:24:11 +08:00
}
http_servlet::~http_servlet(void)
{
}
bool http_servlet::doError(request_t&, response_t& res)
{
res.setStatus(400);
res.setContentType("text/xml; charset=utf-8");
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
2019-03-14 15:24:11 +08:00
acl::string buf;
buf.format("<root error='some error happened!' />\r\n");
res.write(buf);
res.write(NULL, 0);
return false;
}
bool http_servlet::doOther(request_t&, response_t& res, const char* method)
{
res.setStatus(400);
res.setContentType("text/xml; charset=utf-8");
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
2019-03-14 15:24:11 +08:00
acl::string buf;
buf.format("<root error='unkown request method %s' />\r\n", method);
res.write(buf);
res.write(NULL, 0);
return false;
}
bool http_servlet::doGet(request_t& req, response_t& res)
{
2022-01-24 18:36:08 +08:00
const char* path = req.getPathInfo();
handler_t handler = path && *path ? handlers_[path] : NULL;
return handler ? (this->*handler)(req, res) : transfer_get(req, res);
2019-03-14 15:24:11 +08:00
}
bool http_servlet::doPost(request_t& req, response_t& res)
{
const char* path = req.getPathInfo();
handler_t handler = path && *path ? handlers_[path] : NULL;
return handler ? (this->*handler)(req, res) : transfer_post(req, res);
2019-03-14 15:24:11 +08:00
}
2022-01-24 18:36:08 +08:00
bool http_servlet::on_hello(request_t& req, response_t& res)
2019-03-14 15:24:11 +08:00
{
res.setContentType("text/html; charset=utf-8") // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
.setKeepAlive(req.isKeepAlive()) // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ񱣳ֳ<F1B1A3B3><D6B3><EFBFBD><EFBFBD><EFBFBD>
.setContentEncoding(true) // <20>Զ<EFBFBD>֧<EFBFBD><D6A7>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
.setChunkedTransferEncoding(true); // <20><><EFBFBD><EFBFBD> chunk <20><><EFBFBD>ʽ
2019-03-14 15:24:11 +08:00
acl::string buf;
buf.format("<html><body>xxxxxxx<br>\r\n");
if (res.write(buf) == false) {
printf("write error\r\n");
return false;
}
acl::json* json = req.getJson();
if (json == NULL) {
printf("json null\r\n");
} else {
printf("json is [%s]\r\n", json->to_string().c_str());
}
2022-01-19 23:47:53 +08:00
for (size_t i = 0; i < 1; i++) {
2019-03-14 15:24:11 +08:00
buf.format("hello world=%d<br>\r\n", (int) i);
if (res.write(buf) == false) {
printf("write error\r\n");
return false;
}
if (i % 10000 == 0) {
2019-03-14 15:24:11 +08:00
sleep(1);
printf("i=%d\n", (int) i);
}
}
buf = "</body></html><br>\r\n";
printf("write ok\n");
return res.write(buf) && res.write(NULL, 0);
}
bool http_servlet::transfer_get(request_t& req, response_t& res)
2022-01-19 23:47:53 +08:00
{
2022-03-31 18:35:14 +08:00
http_transfer* fiber_peer = new
2022-08-26 17:51:41 +08:00
http_transfer(ssl_conf_, acl::HTTP_METHOD_GET, req, res,
ssl_conf_ ? https_port_ : http_port_);
2022-03-31 18:35:14 +08:00
fiber_peer->start();
2022-01-19 23:47:53 +08:00
2022-01-24 18:36:08 +08:00
bool keep_alive;
2022-03-31 18:35:14 +08:00
fiber_peer->wait(&keep_alive);
delete fiber_peer;
return keep_alive && req.isKeepAlive();
2022-01-24 18:36:08 +08:00
}
2022-01-19 23:47:53 +08:00
bool http_servlet::transfer_post(request_t& req, response_t& res)
2022-01-24 18:36:08 +08:00
{
2022-03-31 18:35:14 +08:00
http_transfer* fiber_peer = new
2022-08-26 17:51:41 +08:00
http_transfer(ssl_conf_, acl::HTTP_METHOD_POST, req, res,
ssl_conf_ ? https_port_ : http_port_);
2022-03-31 18:35:14 +08:00
fiber_peer->start();
2022-01-24 18:36:08 +08:00
bool keep_alive;
2022-03-31 18:35:14 +08:00
fiber_peer->wait(&keep_alive);
2022-01-24 18:36:08 +08:00
2022-03-31 18:35:14 +08:00
delete fiber_peer;
2022-01-24 18:36:08 +08:00
printf("transfer_post finished\r\n");
return keep_alive && req.isKeepAlive();
2022-01-19 23:47:53 +08:00
}
2022-02-16 12:00:12 +08:00
bool http_servlet::doConnect(request_t& req, response_t&)
2019-03-14 15:24:11 +08:00
{
// CONNECT 127.0.0.1:22 HTTP/1.0
// HTTP/1.1 200 Connection Established
const char* phost = req.getRemoteHost();
if (phost == NULL || *phost == 0) {
2022-01-19 23:47:53 +08:00
logger_error("getRemoteHost null");
2019-03-14 15:24:11 +08:00
return false;
}
2022-01-19 23:47:53 +08:00
acl::string host;
const char* port = strrchr(phost, ':');
if (port == NULL) {
host.format("%s|80", phost);
} else if (*(port + 1) == 0) {
host.format("%s80", phost);
} else {
host = phost;
}
2022-02-16 18:16:58 +08:00
printf("remote host=%s, current fiber=%p\r\n", host.c_str(), acl_fiber_running());
2019-03-14 15:24:11 +08:00
2022-03-31 18:35:14 +08:00
acl::socket_stream* peer = new acl::socket_stream;
if (!peer->open(host, 5, 5, acl::time_unit_s)) {
logger_error("connect %s error %s", host.c_str(), acl::last_serror());
2022-03-31 18:35:14 +08:00
delete peer;
2019-03-14 15:24:11 +08:00
return false;
}
2022-09-06 15:20:02 +08:00
printf("connect %s ok, fd=%d\r\n", host.c_str(), (int) peer->sock_handle());
2019-03-14 15:24:11 +08:00
#define USE_REFER
2022-04-02 23:59:44 +08:00
#ifdef USE_REFER
acl::socket_stream* local = &req.getSocketStream();
#else
2022-03-31 18:35:14 +08:00
acl::socket_stream* local = new acl::socket_stream;
local->open(req.getSocketStream().sock_handle());
2022-04-02 23:59:44 +08:00
#endif
2019-03-14 15:24:11 +08:00
#if 0
2019-03-14 15:24:11 +08:00
const char* ok = "";
res.setContentLength(0);
if (!res.write(ok, 1)) {
logger_error("write connect header error");
2019-03-14 15:24:11 +08:00
return false;
}
#else
const char* ok = "HTTP/1.1 200 Connection Established\r\n\r\n";
size_t n = strlen(ok);
2022-03-31 18:35:14 +08:00
if (local->write(ok, n) != (int) n) {
logger_error("write connect response error");
2022-03-31 18:35:14 +08:00
delete peer;
2022-04-02 16:31:12 +08:00
local->unbind_sock();
2022-03-31 18:35:14 +08:00
delete local;
return false;
}
#endif
2019-03-14 15:24:11 +08:00
transfer_tcp(*local, *peer);
2022-03-31 18:35:14 +08:00
2022-04-02 23:59:44 +08:00
#ifndef USE_REFER
2022-04-02 16:31:12 +08:00
int fd = local->unbind_sock();
if (fd == -1) {
acl::socket_stream& ss = req.getSocketStream();
2022-04-02 23:48:10 +08:00
logger_warn("The socket=%d has been closed before!",
ss.sock_handle());
2022-04-02 16:31:12 +08:00
ss.unbind_sock();
}
2022-03-31 18:35:14 +08:00
delete local;
2022-04-02 23:59:44 +08:00
#endif
delete peer;
2019-03-14 15:24:11 +08:00
return false;
}
bool http_servlet::transfer_tcp(acl::socket_stream& local,
acl::socket_stream& peer)
2019-03-14 15:24:11 +08:00
{
2022-08-26 21:42:14 +08:00
local.set_rw_timeout(30);
peer.set_rw_timeout(30);
2022-03-31 18:35:14 +08:00
tcp_transfer* fiber_local = new
tcp_transfer(acl_fiber_running(), local, peer, false);
tcp_transfer* fiber_peer = new
tcp_transfer(acl_fiber_running(), peer, local, false);
2019-03-14 15:24:11 +08:00
2022-03-31 18:35:14 +08:00
fiber_local->set_peer(fiber_peer);
fiber_local->set_local(true);
2019-03-14 15:24:11 +08:00
2022-03-31 18:35:14 +08:00
fiber_peer->set_peer(fiber_local);
fiber_peer->set_local(false);
fiber_peer->start();
fiber_local->start();
//int fd_local = local.sock_handle();
//int fd_peer = peer.sock_handle();
//printf("wait local fiber, local fd=%d, peer fd=%d\r\n", fd_local, fd_peer);
2022-03-31 18:35:14 +08:00
fiber_local->wait();
//printf("local fiber done, local fd=%d, peer fd=%d\r\n", fd_local, fd_peer);
//printf("wait peer fiber, local fd=%d, peer fd=%d\r\n", fd_local, fd_peer);
2022-03-31 18:35:14 +08:00
fiber_peer->wait();
//printf("peer fiber done, local fd=%d, peer fd=%d\r\n", fd_local, fd_peer);
2019-03-14 15:24:11 +08:00
//printf("transfer_tcp finished, local fd=%d, %d, peer fd=%d, %d\r\n",
// fiber_local.get_input().sock_handle(), fd_local,
// fiber_local.get_output().sock_handle(), fd_peer);
2022-03-31 18:35:14 +08:00
delete fiber_peer;
delete fiber_local;
2019-03-14 15:24:11 +08:00
return true;
}