32int main(
int argc,
char* argv[]) {
33 core::SNodeC::init(argc, argv);
36 using LegacyClient = web::http::legacy::in::Client;
37 using Request = LegacyClient::Request;
38 using Response = LegacyClient::Response;
39 using LegacySocketAddress = LegacyClient::SocketAddress;
41 const LegacyClient legacyClient(
43 [](
const std::shared_ptr<Request>& req) {
44 VLOG(1) <<
"OnRequestBegin";
46 VLOG(1) <<
"Requesting upgrade to 'websocket' and any of the subprotocols 'subprotocol' and 'echo'";
48 req->set(
"Sec-WebSocket-Protocol",
"subprotocol, echo");
50 if (!req->upgrade(
"/ws/",
51 "upgradeprotocol, websocket",
52 [](
const std::shared_ptr<Request>& req,
const std::shared_ptr<Response>& res) {
53 VLOG(1) <<
"OnResponse";
54 VLOG(2) <<
" Status:";
55 VLOG(2) <<
" " << res->httpVersion <<
" " << res->statusCode <<
" " << res->reason;
56 VLOG(2) <<
" Headers:";
57 for (
const auto& [field, value] : res->headers) {
58 VLOG(2) <<
" " << field +
" = " + value;
61 VLOG(2) <<
" Cookies:";
62 for (
const auto& [name, cookie] : res->cookies) {
63 VLOG(2) <<
" " + name +
" = " + cookie.getValue();
64 for (
const auto& [option, value] : cookie.getOptions()) {
65 VLOG(2) <<
" " + option +
" = " + value;
69 req->upgrade(res, [req](
const std::string& name) {
71 VLOG(1) <<
"Successful upgrade to '" << name <<
"' from options: " << req->header(
"Upgrade");
73 VLOG(1) <<
"Can not upgrade to any of '" << req->header(
"Upgrade") <<
"'";
77 VLOG(1) <<
"Initiating upgrade to any of 'upgradeprotocol, websocket' failed";
80 []([[maybe_unused]]
const std::shared_ptr<Request>& req) {
81 VLOG(1) <<
"OnRequestEnd";
84 legacyClient.connect([instanceName = legacyClient.getConfig().getInstanceName()](
const LegacySocketAddress& socketAddress,
85 const core::socket::State& state) {
87 case core::socket::State::OK:
88 VLOG(1) << instanceName <<
" connected to '" << socketAddress.toString() <<
"'";
90 case core::socket::State::DISABLED:
91 VLOG(1) << instanceName <<
" disabled";
93 case core::socket::State::ERROR:
94 VLOG(1) << instanceName <<
" " << socketAddress.toString() <<
": " << state.what();
96 case core::socket::State::FATAL:
97 VLOG(1) << instanceName <<
" " << socketAddress.toString() <<
": " << state.what();
102 using TlsClient = web::http::tls::in::Client;
103 using Request = TlsClient::Request;
104 using Response = TlsClient::Response;
105 using TLSSocketAddress = TlsClient::SocketAddress;
107 const TlsClient tlsClient(
109 [](
const std::shared_ptr<Request>& req) {
110 VLOG(1) <<
"OnRequestBegin";
112 VLOG(1) <<
"Requesting upgrade to 'websocket' and any of the subprotocols 'subprotocol' and 'echo'";
114 req->set(
"Sec-WebSocket-Protocol",
"subprotocol, echo");
116 if (!req->upgrade(
"/ws/",
117 "upgradeprotocol, websocket",
118 [](
const std::shared_ptr<Request>& req,
const std::shared_ptr<Response>& res) {
119 VLOG(1) <<
"OnResponse";
120 VLOG(2) <<
" Status:";
121 VLOG(2) <<
" " << res->httpVersion <<
" " << res->statusCode <<
" " << res->reason;
122 VLOG(2) <<
" Headers:";
123 for (
const auto& [field, value] : res->headers) {
124 VLOG(2) <<
" " << field +
" = " + value;
127 VLOG(2) <<
" Cookies:";
128 for (
const auto& [name, cookie] : res->cookies) {
129 VLOG(2) <<
" " + name +
" = " + cookie.getValue();
130 for (
const auto& [option, value] : cookie.getOptions()) {
131 VLOG(2) <<
" " + option +
" = " + value;
135 req->upgrade(res, [req](
const std::string& name) {
137 VLOG(1) <<
"Successful upgrade to '" << name <<
"' from options: " << req->header(
"Upgrade");
139 VLOG(1) <<
"Can not upgrade to any of '" << req->header(
"Upgrade") <<
"'";
143 VLOG(1) <<
"Initiating upgrade to any of 'upgradeprotocol, websocket' failed";
146 []([[maybe_unused]]
const std::shared_ptr<Request>& req) {
147 VLOG(1) <<
"OnRequestEnd";
150 tlsClient.connect([instanceName = tlsClient.getConfig().getInstanceName()](
const TLSSocketAddress& socketAddress,
151 const core::socket::State& state) {
153 case core::socket::State::OK:
154 VLOG(1) << instanceName <<
" connected to '" << socketAddress.toString() <<
"'";
156 case core::socket::State::DISABLED:
157 VLOG(1) << instanceName <<
" disabled";
159 case core::socket::State::ERROR:
160 VLOG(1) << instanceName <<
" " << socketAddress.toString() <<
": " << state.what();
162 case core::socket::State::FATAL:
163 VLOG(1) << instanceName <<
" " << socketAddress.toString() <<
": " << state.what();
169 return core::SNodeC::start();