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 MasterRequest = Client::MasterRequest;
58 using Request = Client::Request;
59 using Response = Client::Response;
60 using SocketAddress = Client::SocketAddress;
62 const Client jsonClient
(
64 [](
const std::shared_ptr<MasterRequest>& req) {
65 VLOG(1) <<
"-- OnRequest";
67 req->
url =
"/index.html";
68 req->
type("application/json");
69 req->
set("Connection", "close");
71 "{\"userId\":1,\"schnitzel\":\"good\",\"hungry\":false}",
72 []([[maybe_unused]]
const std::shared_ptr<Request>& req,
const std::shared_ptr<Response>& res) {
73 VLOG(1) <<
"-- OnResponse";
74 VLOG(1) <<
" Status:";
77 VLOG(1) <<
" " << res->
reason;
79 VLOG(1) <<
" Headers:";
80 for (
const auto& [field, value] : res->
headers) {
81 VLOG(1) <<
" " << field +
" = " + value;
84 VLOG(1) <<
" Cookies:";
85 for (
const auto& [name, cookie] : res->
cookies) {
88 VLOG(1) <<
" " + option +
" = " + value;
92 res->
body.push_back(0);
93 VLOG(1) <<
" Body:\n----------- start body -----------" << res->
body.data() <<
"------------ end body ------------";
95 [](
const std::shared_ptr<Request>&,
const std::string& message) {
96 VLOG(1) <<
"legacy: Request parse error: " << message;
99 []([[maybe_unused]]
const std::shared_ptr<MasterRequest>& req) {
100 LOG(INFO) <<
" -- OnRequestEnd";
106 const SocketAddress& socketAddress,
107 const core::socket::
State& state) {
110 VLOG(1) << instanceName <<
": connected to '" << socketAddress
.toString() <<
"'";
113 VLOG(1) << instanceName <<
": disabled";
116 LOG(ERROR) << instanceName <<
": " << socketAddress
.toString() <<
": " << state
.what();
119 LOG(FATAL) << instanceName <<
": " << socketAddress
.toString() <<
": " << state
.what();
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
146
147
148
149
150
151
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< 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[])