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"
51int main(
int argc,
char* argv[]) {
54 using Client = web::http::legacy::
in::
Client;
55 using MasterRequest = Client::MasterRequest;
56 using Request = Client::Request;
57 using Response = Client::Response;
58 using SocketAddress = Client::SocketAddress;
60 const Client jsonClient
(
62 [](
const std::shared_ptr<MasterRequest>& req) {
63 VLOG(1) <<
"-- OnRequest";
65 req->
url =
"/index.html";
66 req->
type("application/json");
67 req->
set("Connection", "close");
69 R"({"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() <<
"------------ end body ------------";
93 [](
const std::shared_ptr<Request>&,
const std::string& message) {
94 VLOG(1) <<
"legacy: Request parse error: " << message;
97 []([[maybe_unused]]
const std::shared_ptr<MasterRequest>& req) {
98 LOG(INFO) <<
" -- OnRequestEnd";
104 const SocketAddress& socketAddress,
105 const core::socket::
State& state) {
108 VLOG(1) << instanceName <<
": connected to '" << socketAddress
.toString() <<
"'";
111 VLOG(1) << instanceName <<
": disabled";
114 LOG(ERROR) << instanceName <<
": " << socketAddress
.toString() <<
": " << state
.what();
117 LOG(FATAL) << instanceName <<
": " << socketAddress
.toString() <<
": " << state
.what();
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
144
145
146
147
148
149
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
const Super & 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< MasterRequest > &)> &&onHttpConnected, std::function< void(const std::shared_ptr< MasterRequest > &)> &&onHttpDisconnected)
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)
Request & set(const std::string &field, const std::string &value, bool overwrite=true)
Request & type(const std::string &type)
CiStringMap< std::string > headers
CiStringMap< CookieOptions > cookies
int main(int argc, char *argv[])
web::http::client::Client< net::in::stream::legacy::SocketClient > Client