-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_server.cpp
More file actions
118 lines (93 loc) · 3.57 KB
/
Copy pathwebhook_server.cpp
File metadata and controls
118 lines (93 loc) · 3.57 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "webhook_server.h"
wh_http_client::wh_http_client(boost::asio::io_context& ioc, callback_webhook callback) : m_name("webhook client"),
m_socket(ioc),
m_callback(callback)
{
}
void wh_http_client::start(std::vector<pointer>* clients)
{
m_clients_list = clients;
clients->push_back(shared_from_this());
read_request();
}
void wh_http_client::stop()
{
m_socket.shutdown(tcp::socket::shutdown_send);
auto it = find(m_clients_list->begin(), m_clients_list->end(), shared_from_this());
m_clients_list->erase(it);
}
void wh_http_client::read_request()
{
http::async_read(m_socket, m_buffer, m_request, [this](boost::beast::error_code ec, std::size_t)
{
if(ec)
{
mlogger::error("%s Ошибка чтения данных %d: %s",
ec.value(),
ec.message().c_str());
stop();
}
else
process_request();
});
}
void wh_http_client::process_request()
{
switch (m_request.method())
{
case http::verb::post:
break;
default:
send_response(http::status::bad_request);
return;
}
auto content_type = m_request["Content-Type"];
if(0 != content_type.to_string().find("text/plain"))
{
send_response(http::status::unsupported_media_type);
return;
}
m_callback(m_request.body());
send_response(http::status::ok);
}
void wh_http_client::send_response(http::status status)
{
m_response.result(status);
m_response.version(11);
m_response.set(http::field::server, "Beast");
m_response.set(http::field::transfer_encoding, "chunked");
m_response.body() = "";
m_response.prepare_payload();
http::async_write(m_socket, m_response, boost::bind(&wh_http_client::stop, this));
}
//=================================================================================
webhook_server::~webhook_server()
{
m_ioc.stop();
m_ioc_thread.join();
while(clients.size())
clients.front()->stop();
if(nullptr != m_acceptor)
delete m_acceptor;
}
void webhook_server::start()
{
m_acceptor = new tcp::acceptor(m_ioc, ip::tcp::endpoint(/*ip::tcp::v4()*/ip::address_v4::loopback(), m_port));
start_accept();
m_ioc.reset();
m_ioc_thread = boost::thread(boost::bind(&boost::asio::io_context::run, boost::ref(m_ioc)));
}
void webhook_server::start_accept()
{
wh_http_client::pointer new_client = wh_http_client::create(m_ioc, m_callback);
m_acceptor->async_accept(new_client->socket(),
boost::bind(&webhook_server::on_accept, this, new_client, boost::asio::placeholders::error));
}
void webhook_server::on_accept(wh_http_client::pointer client, const boost::beast::error_code& ec)
{
if(!ec)
{
client->start(&clients);
}
start_accept();
}