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"
45#ifndef DOXYGEN_SHOULD_SKIP_THIS
47#include "log/Logger.h"
53int main(
int argc,
char* argv[]) {
56 using Client = web::http::legacy::
in::Client;
57 using Request = Client::Request;
58 using Response = Client::Response;
59 using SocketAddress = Client::SocketAddress;
61 const Client jsonClient
(
63 [](
const std::shared_ptr<Request>& req) {
64 VLOG(1) <<
"-- OnRequest";
66 req->
url =
"/index.html";
67 req->
type("application/json");
68 req->
set("Connection", "close");
69 req->
send("{\"userId\":1,\"schnitzel\":\"good\",\"hungry\":false}",
70 []([[maybe_unused]]
const std::shared_ptr<Request>& req,
const std::shared_ptr<Response>& res) {
71 VLOG(1) <<
"-- OnResponse";
72 VLOG(1) <<
" Status:";
75 VLOG(1) <<
" " << res->
reason;
77 VLOG(1) <<
" Headers:";
78 for (
const auto& [field, value] : res->
headers) {
79 VLOG(1) <<
" " << field +
" = " + value;
82 VLOG(1) <<
" Cookies:";
83 for (
const auto& [name, cookie] : res->
cookies) {
86 VLOG(1) <<
" " + option +
" = " + value;
90 res->
body.push_back(0);
91 VLOG(1) <<
" Body:\n----------- start body -----------" << res->
body.data()
92 <<
"------------ end body ------------";
95 []([[maybe_unused]]
const std::shared_ptr<Request>& req) {
96 LOG(INFO) <<
" -- OnRequestEnd";
102 const SocketAddress& socketAddress,
103 const core::socket::
State& state) {
106 VLOG(1) << instanceName <<
": connected to '" << socketAddress
.toString() <<
"'";
109 VLOG(1) << instanceName <<
": disabled";
112 LOG(ERROR) << instanceName <<
": " << socketAddress
.toString() <<
": " << state
.what();
115 LOG(FATAL) << instanceName <<
": " << socketAddress
.toString() <<
": " << state
.what();
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
142
143
144
145
146
147
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
const std::string & getInstanceName() const
std::string toString(bool expanded=true) const override
void connect(const std::string &ipOrHostname, uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
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)
bool send(const std::string &chunk, 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)
Request & type(const std::string &type)
CiStringMap< std::string > headers
CiStringMap< CookieOptions > cookies
int main(int argc, char *argv[])