2019-07-28 10:31:56 +08:00
|
|
|
#include "stdafx.h"
|
2016-06-23 13:34:54 +08:00
|
|
|
#include "http_servlet.h"
|
2020-06-29 14:34:16 +08:00
|
|
|
#include "http_service.h"
|
2016-06-23 13:34:54 +08:00
|
|
|
|
|
|
|
static char *var_cfg_str;
|
|
|
|
|
|
|
|
acl::master_str_tbl var_conf_str_tab[] = {
|
|
|
|
{ "str", "test_msg", &var_cfg_str },
|
|
|
|
|
|
|
|
{ 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int var_cfg_debug_enable;
|
|
|
|
|
|
|
|
acl::master_bool_tbl var_conf_bool_tab[] = {
|
|
|
|
{ "debug_enable", 1, &var_cfg_debug_enable },
|
|
|
|
|
|
|
|
{ 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int var_cfg_io_timeout;
|
|
|
|
|
|
|
|
acl::master_int_tbl var_conf_int_tab[] = {
|
|
|
|
{ "io_timeout", 120, &var_cfg_io_timeout, 0, 0 },
|
|
|
|
|
|
|
|
{ 0, 0 , 0 , 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
acl::master_int64_tbl var_conf_int64_tab[] = {
|
|
|
|
{ 0, 0 , 0 , 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-06-29 14:34:16 +08:00
|
|
|
http_service::http_service(void)
|
2016-06-23 13:34:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-29 14:34:16 +08:00
|
|
|
http_service::~http_service(void)
|
2016-06-23 13:34:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-29 14:34:16 +08:00
|
|
|
void http_service::on_accept(acl::socket_stream& conn)
|
2016-06-23 13:34:54 +08:00
|
|
|
{
|
|
|
|
logger("connect from %s, fd %d", conn.get_peer(), conn.sock_handle());
|
|
|
|
|
|
|
|
conn.set_rw_timeout(120);
|
|
|
|
|
|
|
|
acl::memcache_session session("127.0.0.1:11211");
|
2020-06-29 14:34:16 +08:00
|
|
|
http_servlet servlet(handlers_, &conn, &session);
|
2016-06-23 13:34:54 +08:00
|
|
|
|
|
|
|
// charset: big5, gb2312, gb18030, gbk, utf-8
|
|
|
|
servlet.setLocalCharset("utf-8");
|
|
|
|
|
|
|
|
while(servlet.doRun()) {}
|
|
|
|
|
|
|
|
logger("disconnect from %s", conn.get_peer());
|
|
|
|
}
|
|
|
|
|
2020-06-29 14:34:16 +08:00
|
|
|
void http_service::proc_pre_jail(void)
|
2016-06-23 13:34:54 +08:00
|
|
|
{
|
|
|
|
logger(">>>proc_pre_jail<<<");
|
|
|
|
}
|
|
|
|
|
2020-06-29 14:34:16 +08:00
|
|
|
void http_service::proc_on_listen(acl::server_socket& ss)
|
2016-12-03 20:09:21 +08:00
|
|
|
{
|
|
|
|
logger(">>>listen %s ok<<<", ss.get_addr());
|
|
|
|
}
|
|
|
|
|
2020-06-29 14:34:16 +08:00
|
|
|
void http_service::proc_on_init(void)
|
2016-06-23 13:34:54 +08:00
|
|
|
{
|
|
|
|
logger(">>>proc_on_init<<<");
|
|
|
|
}
|
|
|
|
|
2020-06-29 14:34:16 +08:00
|
|
|
void http_service::proc_on_exit(void)
|
2016-06-23 13:34:54 +08:00
|
|
|
{
|
|
|
|
logger(">>>proc_on_exit<<<");
|
|
|
|
}
|
2017-07-05 10:27:33 +08:00
|
|
|
|
2020-06-29 14:34:16 +08:00
|
|
|
bool http_service::proc_on_sighup(acl::string&)
|
2017-07-05 10:27:33 +08:00
|
|
|
{
|
|
|
|
logger(">>>proc_on_sighup<<<");
|
2017-09-09 00:30:39 +08:00
|
|
|
return true;
|
2017-07-05 10:27:33 +08:00
|
|
|
}
|
2020-06-29 14:34:16 +08:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
http_service& http_service::Get(const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
Service(http_handler_get, path, fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_service& http_service::Post(const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
Service(http_handler_post, path, fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_service& http_service::Head(const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
Service(http_handler_head, path, fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_service& http_service::Put(const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
Service(http_handler_put, path, fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_service& http_service::Patch(const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
Service(http_handler_patch, path, fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_service& http_service::Connect(const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
Service(http_handler_connect, path, fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_service& http_service::Purge(const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
Service(http_handler_purge, path, fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_service& http_service::Delete(const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
Service(http_handler_delete, path, fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_service& http_service::Options(const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
Service(http_handler_options, path, fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_service& http_service::Propfind(const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
Service(http_handler_profind, path, fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_service& http_service::Websocket(const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
Service(http_handler_websocket, path, fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_service& http_service::Unknown(const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
Service(http_handler_unknown, path, fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_service& http_service::Error(const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
Service(http_handler_error, path, fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_service::Service(int type, const char* path, http_handler_t fn)
|
|
|
|
{
|
|
|
|
if (type >= http_handler_get && type < http_handler_max
|
|
|
|
&& path && *path) {
|
|
|
|
|
|
|
|
// The path should lookup like as "/xxx/" with
|
|
|
|
// lower charactors.
|
|
|
|
|
|
|
|
acl::string buf(path);
|
|
|
|
if (buf[buf.size() - 1] != '/') {
|
|
|
|
buf += '/';
|
|
|
|
}
|
|
|
|
buf.lower();
|
2020-06-29 14:42:23 +08:00
|
|
|
handlers_[type][buf] = fn;
|
2020-06-29 14:34:16 +08:00
|
|
|
}
|
|
|
|
}
|