-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxhttps_server.cpp
More file actions
67 lines (60 loc) · 2.87 KB
/
xhttps_server.cpp
File metadata and controls
67 lines (60 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2017-2018 Telos Foundation & contributors
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "xhttps_server.h"
namespace top
{
namespace httpserver
{
void xhttps_server::start(const std::string &cert_file, const std::string &private_key_file, uint16_t nPort, uint32_t nThreadNum)
{
m_pServer = std::make_shared<HttpsServer>(cert_file,private_key_file);
m_pServer->config.port = nPort;
m_pServer->config.thread_pool_size = nThreadNum;
m_pServer->resource["^/$"]["POST"] = std::bind(&xhttps_server::start_service, this, std::placeholders::_1, std::placeholders::_2);
m_pServer->resource["^/$"]["OPTIONS"] = [](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) {
*response << "HTTP/1.1 200 OK\r\n";
*response << "Access-Control-Allow-Origin:*\r\n";
*response << "Access-Control-Request-Method:POST\r\n";
*response << "Access-Control-Allow-Headers:Origin,X-Requested-With,Content-Type,Accept\r\n";
*response << "Access-Control-Request-Headers:Content-type\r\n";
};
m_pServer->on_error = [](std::shared_ptr<HttpsServer::Request> request, const SimpleWeb::error_code & ec) {
// Handle errors here
// Note that connection timeouts will also call this handle with ec set to SimpleWeb::errc::operation_canceled
//xwarn("on_error:%s,%d", ec.code().message().c_str(), ec.code().value());
};
m_server_thread = std::thread([this]() {
// Start server
this->m_pServer->start();
});
}
void xhttps_server::start_service(std::shared_ptr<SimpleWeb::ServerBase<SimpleWeb::HTTPS>::Response> response, std::shared_ptr<SimpleWeb::ServerBase<SimpleWeb::HTTPS>::Request> request)
{
// Retrieve string:
auto content = request->content.string();
xjson_proc json_proc;
if(!json_proc.parse_json(content))
{
response->write(json_proc.get_response());
return;
}
if(!m_rule_mgr.filter(json_proc))
{
response->write(json_proc.get_response());
return;
}
if(m_action_mgr.do_action(json_proc))
{
response->write(json_proc.get_response());
return;
}
SET_JSON_PROC_RESPONSE(xhttp_error_code::server_unknown_error)
response->write(json_proc.get_response());
}
xhttps_server::~xhttps_server()
{
m_server_thread.join();
}
}
}