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) {
66 VLOG(1) <<
"OnRequestBegin";
68 VLOG(1) <<
"Requesting upgrade to 'websocket' and any of the subprotocols 'subprotocol' and 'echo'";
70 req->
set("Sec-WebSocket-Protocol", "subprotocol, echo");
73 "upgradeprotocol, websocket",
74 [](
const std::shared_ptr<Request>& req,
const std::shared_ptr<Response>& res) {
75 VLOG(1) <<
"OnResponse";
76 VLOG(2) <<
" Status:";
78 VLOG(2) <<
" Headers:";
79 for (
const auto& [field, value] : res->
headers) {
80 VLOG(2) <<
" " << field +
" = " + value;
83 VLOG(2) <<
" Cookies:";
84 for (
const auto& [name, cookie] : res->
cookies) {
87 VLOG(2) <<
" " + option +
" = " + value;
91 req->
upgrade(res
, [req](
const std::string& name) {
93 VLOG(1) <<
"Successful upgrade to '" << name <<
"' from options: " << req->
header("Upgrade");
95 VLOG(1) <<
"Can not upgrade to any of '" << req->
header("Upgrade") <<
"'";
99 VLOG(1) <<
"Initiating upgrade to any of 'upgradeprotocol, websocket' failed";
102 []([[maybe_unused]]
const std::shared_ptr<Request>& req) {
103 VLOG(1) <<
"OnRequestEnd";
107 const core::socket::
State& state) {
110 VLOG(1) << instanceName <<
" connected to '" << socketAddress
.toString() <<
"'";
113 VLOG(1) << instanceName <<
" disabled";
124 using TlsClient = web::http::tls::
in::Client;
125 using Request = TlsClient::Request;
126 using Response = TlsClient::Response;
127 using TLSSocketAddress = TlsClient::SocketAddress;
129 const TlsClient tlsClient
(
131 [](
const std::shared_ptr<Request>& req) {
132 VLOG(1) <<
"OnRequestBegin";
134 VLOG(1) <<
"Requesting upgrade to 'websocket' and any of the subprotocols 'subprotocol' and 'echo'";
136 req->
set("Sec-WebSocket-Protocol", "subprotocol, echo");
139 "upgradeprotocol, websocket",
140 [](
const std::shared_ptr<Request>& req,
const std::shared_ptr<Response>& res) {
141 VLOG(1) <<
"OnResponse";
142 VLOG(2) <<
" Status:";
144 VLOG(2) <<
" Headers:";
145 for (
const auto& [field, value] : res->
headers) {
146 VLOG(2) <<
" " << field +
" = " + value;
149 VLOG(2) <<
" Cookies:";
150 for (
const auto& [name, cookie] : res->
cookies) {
153 VLOG(2) <<
" " + option +
" = " + value;
157 req->
upgrade(res
, [req](
const std::string& name) {
159 VLOG(1) <<
"Successful upgrade to '" << name <<
"' from options: " << req->
header("Upgrade");
161 VLOG(1) <<
"Can not upgrade to any of '" << req->
header("Upgrade") <<
"'";
165 VLOG(1) <<
"Initiating upgrade to any of 'upgradeprotocol, websocket' failed";
168 []([[maybe_unused]]
const std::shared_ptr<Request>& req) {
169 VLOG(1) <<
"OnRequestEnd";
173 const core::socket::
State& state) {
176 VLOG(1) << instanceName <<
" connected to '" << socketAddress
.toString() <<
"'";
179 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 & getInstanceName() const
std::string toString(bool expanded=true) const override
const std::map< std::string, std::string > & getOptions() const
const std::string & getValue() const
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)
std::string header(const std::string &field)
bool upgrade(const std::string &url, const std::string &protocols, const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError=responseParseError)
void upgrade(const std::shared_ptr< Response > &response, const std::function< void(const std::string &)> &status)
CiStringMap< std::string > headers
CiStringMap< CookieOptions > cookies
int main(int argc, char *argv[])