2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#include "core/SNodeC.h"
43#include "web/http/legacy/in/Client.h"
44#include "web/http/tls/in/Client.h"
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
48#include "log/Logger.h"
54int main(
int argc,
char* argv[]) {
58 using LegacyClient = web::http::legacy::
in::Client;
59 using MasterRequest = LegacyClient::MasterRequest;
60 using Request = LegacyClient::Request;
61 using Response = LegacyClient::Response;
62 using LegacySocketAddress = LegacyClient::SocketAddress;
64 const LegacyClient legacyClient
(
66 [](
const std::shared_ptr<MasterRequest>& req) {
69 VLOG(1) << connectionName <<
": OnRequestBegin";
71 req->
set("Sec-WebSocket-Protocol", "subprotocol, echo");
76 [connectionName](
bool success) {
77 VLOG(1) << connectionName <<
": HTTP Upgrade (http -> websocket) start " << (success ?
"success" :
"failed");
79 [connectionName]([[maybe_unused]]
const std::shared_ptr<Request>& req,
80 const std::shared_ptr<Response>& res,
81 [[maybe_unused]]
bool success) {
82 VLOG(1) << connectionName <<
": Upgrade success:";
84 VLOG(1) << connectionName <<
": Requested: " << req->
header("upgrade");
85 VLOG(1) << connectionName <<
": Selected: " << res->
get("upgrade");
87 [connectionName](
const std::shared_ptr<Request>&,
const std::string& message) {
88 VLOG(1) << connectionName <<
": Request parse error: " << message;
91 []([[maybe_unused]]
const std::shared_ptr<MasterRequest>& req) {
94 VLOG(1) << connectionName <<
": OnRequestEnd";
98 const core::socket::
State& state) {
101 VLOG(1) << instanceName <<
" connected to '" << socketAddress
.toString() <<
"'";
104 VLOG(1) << instanceName <<
" disabled";
115 using TlsClient = web::http::tls::
in::Client;
116 using MasterRequest = TlsClient::MasterRequest;
117 using Request = TlsClient::Request;
118 using Response = TlsClient::Response;
119 using TLSSocketAddress = TlsClient::SocketAddress;
121 const TlsClient tlsClient
(
123 [](
const std::shared_ptr<MasterRequest>& req) {
126 VLOG(1) << connectionName <<
": OnRequestBegin";
128 req->
set("Sec-WebSocket-Protocol", "subprotocol, echo");
133 [connectionName](
bool success) {
134 VLOG(1) << connectionName <<
": HTTP Upgrade (http -> websocket) start " << (success ?
"success" :
"failed");
136 [connectionName]([[maybe_unused]]
const std::shared_ptr<Request>& req,
137 [[maybe_unused]]
const std::shared_ptr<Response>& res,
138 [[maybe_unused]]
bool success) {
140 [connectionName](
const std::shared_ptr<Request>&,
const std::string& message) {
141 VLOG(1) << connectionName <<
": Request parse error: " << message;
144 []([[maybe_unused]]
const std::shared_ptr<MasterRequest>& req) {
147 VLOG(1) << connectionName <<
": OnRequestEnd";
151 const core::socket::
State& state) {
154 VLOG(1) << instanceName <<
" connected to '" << socketAddress
.toString() <<
"'";
157 VLOG(1) << instanceName <<
" disabled";
static void init(int argc, char *argv[])
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Config & getConfig() const
static constexpr int DISABLED
static constexpr int ERROR
static constexpr int FATAL
void connect(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const std::string & getConnectionName() const
SocketConnection * getSocketConnection() const
const std::string & getInstanceName() const
std::string toString(bool expanded=true) const override
Client(const std::string &name, std::function< void(const std::shared_ptr< MasterRequest > &)> &&onHttpConnected, std::function< void(const std::shared_ptr< MasterRequest > &)> &&onHttpDisconnected)
bool upgrade(const std::string &url, const std::string &protocols, const std::function< void(bool)> &onUpgradeInitiate, const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &, bool)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError)
Request & set(const std::string &field, const std::string &value, bool overwrite=true)
SocketContext * getSocketContext() const
std::string header(const std::string &field) const
const std::string & get(const std::string &key, int i=0) const
int main(int argc, char *argv[])