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 Request = LegacyClient::Request;
60 using Response = LegacyClient::Response;
61 using LegacySocketAddress = LegacyClient::SocketAddress;
63 const LegacyClient legacyClient
(
65 [](
const std::shared_ptr<Request>& req) {
68 VLOG(1) << connectionName <<
": OnRequestBegin";
70 req->
set("Sec-WebSocket-Protocol", "subprotocol, echo");
75 [connectionName](
const std::shared_ptr<Request>& req,
bool success) {
76 VLOG(1) << connectionName <<
": HTTP Upgrade (http -> " << req->
header("upgrade") <<
"|"
77 << req->
header("Sec-WebSocket-Protocol") <<
") start " << (success ?
"success" :
"failed");
79 [connectionName]([[maybe_unused]]
const std::shared_ptr<Request>& req,
80 [[maybe_unused]]
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");
88 []([[maybe_unused]]
const std::shared_ptr<Request>& req) {
91 VLOG(1) << connectionName <<
": OnRequestEnd";
95 const core::socket::
State& state) {
98 VLOG(1) << instanceName <<
" connected to '" << socketAddress
.toString() <<
"'";
101 VLOG(1) << instanceName <<
" disabled";
112 using TlsClient = web::http::tls::
in::Client;
113 using Request = TlsClient::Request;
114 using Response = TlsClient::Response;
115 using TLSSocketAddress = TlsClient::SocketAddress;
117 const TlsClient tlsClient
(
119 [](
const std::shared_ptr<Request>& req) {
122 VLOG(1) << connectionName <<
": OnRequestBegin";
124 req->
set("Sec-WebSocket-Protocol", "subprotocol, echo");
129 [connectionName](
const std::shared_ptr<Request>& req,
bool success) {
130 VLOG(1) << connectionName <<
": HTTP Upgrade (http -> " << req->
header("upgrade") <<
"|"
131 << req->
header("Sec-WebSocket-Protocol") <<
") start " << (success ?
"success" :
"failed");
133 [connectionName]([[maybe_unused]]
const std::shared_ptr<Request>& req,
134 [[maybe_unused]]
const std::shared_ptr<Response>& res,
135 [[maybe_unused]]
bool success) {
138 []([[maybe_unused]]
const std::shared_ptr<Request>& req) {
141 VLOG(1) << connectionName <<
": OnRequestEnd";
145 const core::socket::
State& state) {
148 VLOG(1) << instanceName <<
" connected to '" << socketAddress
.toString() <<
"'";
151 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< Request > &)> &&onRequestBegin, std::function< void(const std::shared_ptr< Request > &)> &&onRequestEnd)
Request & set(const std::string &field, const std::string &value, bool overwrite=true)
bool upgrade(const std::string &url, const std::string &protocols, const std::function< void(const std::shared_ptr< Request > &, 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=responseParseError)
std::string header(const std::string &field)
SocketContext * getSocketContext() const
const std::string & get(const std::string &key, int i=0) const
int main(int argc, char *argv[])