SNode.C
Loading...
Searching...
No Matches
clients.h
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025, 2026
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#ifndef APPS_HTTP_MODEL_CLIENTS_H
43#define APPS_HTTP_MODEL_CLIENTS_H
44
45#define QUOTE_INCLUDE(a) STR_INCLUDE(a)
46#define STR_INCLUDE(a) #a
47
48// clang-format off
49#define CLIENT_INCLUDE QUOTE_INCLUDE(web/http/STREAM/NET/Client.h)
50
51#define EVENTSOURCE_INCLUDE QUOTE_INCLUDE(web/http/STREAM/NET/EventSource.h)
52
53// clang-format on
54
55#include CLIENT_INCLUDE // IWYU pragma: export
56#include EVENTSOURCE_INCLUDE // IWYU pragma: export
57
58#ifndef DOXYGEN_SHOULD_SKIP_THIS
59
60#include "log/Logger.h"
61#include "web/http/http_utils.h"
62
63#if (STREAM_TYPE == TLS) // tls
64#include <cstddef>
65#include <openssl/ssl.h>
66#include <openssl/x509v3.h>
67#endif
68
69#endif /* DOXYGEN_SHOULD_SKIP_THIS */
70
71static void logResponse(const std::shared_ptr<web::http::client::Request>& req, const std::shared_ptr<web::http::client::Response>& res) {
72 VLOG(1) << req->getSocketContext()->getSocketConnection()->getConnectionName() << " HTTP response: " << req->method << " " << req->url
73 << " HTTP/" << req->httpMajor << "." << req->httpMinor << "\n"
74 << httputils::toString(req->method,
75 req->url,
76 "HTTP/" + std::to_string(req->httpMajor) + "." + std::to_string(req->httpMinor),
77 req->getQueries(),
78 req->getHeaders(),
79 req->getTrailer(),
80 req->getCookies(),
81 {})
82 << "\n"
83 << httputils::toString(res->httpVersion, res->statusCode, res->reason, res->headers, res->cookies, res->body);
84}
85
86#if (STREAM_TYPE == LEGACY) // legacy
87
88namespace apps::http::legacy {
89
95
96 inline Client getClient() {
97 Client client(
98 "httpclient",
99 [](const std::shared_ptr<MasterRequest>& req) {
100 VLOG(1) << req->getSocketContext()->getSocketConnection()->getConnectionName() << ": OnRequestStart";
101
102 req->httpMajor = 1;
103 req->httpMinor = 1;
104 req->url = "/";
105 req->set("Connection", "keep-alive");
106 req->setTrailer("MyTrailer",
107 "MyTrailerValue"); // The "Trailer" header field should be populated but the Trailer itself should not be
108 // send here because there is no content which is send using "Transfer-Encoding:chunked"
109 req->end(
110 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
111 logResponse(req, res);
112 },
113 [](const std::shared_ptr<Request>&, const std::string&) {
114 });
115
116 req->httpMinor = 1;
117 req->method = "GET";
118 req->url = "/sendfile/";
119 req->set("Connection", "keep-alive");
120 req->sendFile(
121 "/home/voc/projects/snodec/snode.c/CMakeLists.tt",
122 [req](int ret) {
123 if (ret == 0) {
124 VLOG(1) << req->getSocketContext()->getSocketConnection()->getConnectionName()
125 << " HTTP: Request accepted: GET / HTTP/" << req->httpMajor << "." << req->httpMinor;
126 VLOG(1) << " /home/voc/projects/snodec/snode.c/CMakeLists.tt";
127 } else {
128 LOG(ERROR) << req->getSocketContext()->getSocketConnection()->getConnectionName()
129 << " HTTP: Request failed: GET / HTTP/" << req->httpMajor << "." << req->httpMinor;
130 PLOG(ERROR) << " /home/voc/projects/snodec/snode.c/CMakeLists.tt";
131 }
132 },
133 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
134 logResponse(req, res);
135 },
136 [](const std::shared_ptr<Request>&, const std::string&) {
137 });
138
139 req->init();
140 req->httpMinor = 1;
141 req->url = "/third";
142 req->set("Connection", "keep-alive");
143 req->setTrailer("MyTrailer",
144 "MyTrailerValue"); // The "Trailer" header field should be populated but the Trailer itself should not be
145 // send here because there is no content which is send using "Transfer-Encoding:chunked"
146 req->end(
147 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
148 logResponse(req, res);
149 },
150 [](const std::shared_ptr<Request>&, const std::string&) {
151 });
152
153#define LONG
154#ifdef LONG
155 req->url = "/";
156 req->set("Connection", "keep-alive");
157 req->end(
158 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
159 logResponse(req, res);
160 },
161 [](const std::shared_ptr<Request>&, const std::string&) {
162 });
163
164 req->httpMinor = 1;
165 req->url = "/index.html";
166 // req->set("Connection", "keep-alive");
167 req->end(
168 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
169 logResponse(req, res);
170 },
171 [](const std::shared_ptr<Request>&, const std::string&) {
172 });
173 req->url = "/";
174 req->set("Connection", "keep-alive");
175 req->end(
176 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
177 logResponse(req, res);
178 },
179 [req](const std::shared_ptr<Request>&, const std::string&) {
180 });
181 req->url = "/index.html";
182 req->set("Connection", "keep-alive");
183 req->end(
184 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
185 logResponse(req, res);
186 },
187 [req](const std::shared_ptr<Request>&, const std::string&) {
188 });
189 req->url = "/";
190 req->set("Connection", "keep-alive");
191 req->end(
192 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
193 logResponse(req, res);
194 },
195 [req](const std::shared_ptr<Request>&, const std::string&) {
196 });
197 req->url = "/index.html";
198 req->set("Connection", "keep-alive");
199 req->end(
200 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
201 logResponse(req, res);
202 },
203 [req](const std::shared_ptr<Request>&, const std::string&) {
204 });
205 req->url = "/";
206 req->set("Connection", "keep-alive");
207 req->end(
208 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
209 logResponse(req, res);
210 },
211 [req](const std::shared_ptr<Request>&, const std::string&) {
212 });
213 req->url = "/index.html";
214 req->set("Connection", "keep-alive");
215 req->end(
216 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
217 logResponse(req, res);
218 },
219 [req](const std::shared_ptr<Request>&, const std::string&) {
220 });
221 req->url = "/";
222 req->set("Connection", "keep-alive");
223 req->end(
224 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
225 logResponse(req, res);
226 },
227 [req](const std::shared_ptr<Request>&, const std::string&) {
228 });
229 req->url = "/index.html";
230 req->set("Connection", "keep-alive");
231 req->end(
232 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
233 logResponse(req, res);
234 },
235 [req](const std::shared_ptr<Request>&, const std::string&) {
236 });
237 req->url = "/";
238 req->set("Connection", "keep-alive");
239 req->end(
240 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
241 logResponse(req, res);
242 },
243 [req](const std::shared_ptr<Request>&, const std::string&) {
244 });
245 req->url = "/index.html";
246 req->set("Connection", "keep-alive");
247 req->end(
248 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
249 logResponse(req, res);
250 },
251 [req](const std::shared_ptr<Request>&, const std::string&) {
252 });
253 req->url = "/";
254 req->set("Connection", "keep-alive");
255 req->end(
256 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
257 logResponse(req, res);
258 },
259 [req](const std::shared_ptr<Request>&, const std::string&) {
260 });
261 req->url = "/index.html";
262 req->set("Connection", "keep-alive");
263 req->end(
264 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
265 logResponse(req, res);
266 },
267 [req](const std::shared_ptr<Request>&, const std::string&) {
268 });
269 req->url = "/";
270 req->set("Connection", "keep-alive");
271 req->end(
272 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
273 logResponse(req, res);
274 },
275 [req](const std::shared_ptr<Request>&, const std::string&) {
276 });
277 req->url = "/index.html";
278 req->set("Connection", "keep-alive");
279 req->end(
280 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
281 logResponse(req, res);
282 },
283 [req](const std::shared_ptr<Request>&, const std::string&) {
284 });
285 req->url = "/";
286 req->set("Connection", "close");
287 req->end(
288 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
289 logResponse(req, res);
290 },
291 [req](const std::shared_ptr<Request>&, const std::string&) {
292 });
293
294 const std::shared_ptr<web::http::client::tools::EventSource> eventStream_1 =
295 web::http::legacy::NET::EventSource("http://" + req->hostFieldValue, "/sse?hihi=3");
296
297 if (eventStream_1) {
298 eventStream_1->onOpen([]() {
299 VLOG(0) << "OnOpen 1";
300 });
301
302 eventStream_1->onError([]() {
303 VLOG(0) << "OnError 1";
304 });
305
306 eventStream_1->onMessage([](const web::http::client::tools::EventSource::MessageEvent& message) {
307 VLOG(0) << "OnMessage 1:1: " << message.data;
308 });
309 eventStream_1->onMessage([](const web::http::client::tools::EventSource::MessageEvent& message) {
310 VLOG(0) << "OnMessage 1:2: " << message.data;
311 });
312 eventStream_1->addEventListener("myevent", [](const web::http::client::tools::EventSource::MessageEvent& message) {
313 VLOG(0) << "EventListener for 'myevent' 1:1: " << message.lastEventId << " : " << message.data;
314 });
315 eventStream_1->addEventListener("myevent", [](const web::http::client::tools::EventSource::MessageEvent& message) {
316 VLOG(0) << "EventListener for 'myevent' 1:2: " << message.lastEventId << " : " << message.data;
317 });
318
319 core::timer::Timer::singleshotTimer(
320 [eventStream_1]() {
321 eventStream_1->close();
322 },
323 5);
324 }
325
326 auto eventStream_2 = web::http::legacy::NET::EventSource("http://localhost:8080/sse");
327
328 if (eventStream_2) {
329 eventStream_2->onOpen([]() {
330 VLOG(0) << "OnOpen 2";
331 });
332
333 eventStream_2->onError([]() {
334 VLOG(0) << "OnError 2";
335 });
336
337 eventStream_2->onMessage([](const web::http::client::tools::EventSource::MessageEvent& message) {
338 VLOG(0) << "OnMessage 2:1: " << message.data;
339 });
340 eventStream_2->onMessage([](const web::http::client::tools::EventSource::MessageEvent& message) {
341 VLOG(0) << "OnMessage 2:2: " << message.data;
342 });
343 eventStream_2->addEventListener("myevent", [](const web::http::client::tools::EventSource::MessageEvent& message) {
344 VLOG(0) << "EventListener for 'myevent' 2:1: " << message.lastEventId << " : " << message.data;
345 });
346 eventStream_2->addEventListener("myevent", [](const web::http::client::tools::EventSource::MessageEvent& message) {
347 VLOG(0) << "EventListener for 'myevent' 2:2: " << message.lastEventId << " : " << message.data;
348 });
349 }
350
351 /*
352 req->httpMinor = 1;
353 req->method = "GET";
354 req->url = "/";
355 req->set("Test", "aaa");
356 req->setTrailer("MyTrailer1",
357 "MyTrailerValue1"); // Full trailer processing. Header field "Trailer" set and the Trailer itself is also
358 // sent because here content will be sent with "Transfer-Encoding:chunked"
359 req->setTrailer("MyTrailer2", "MyTrailerValue2");
360 req->setTrailer("MyTrailer3", "MyTrailerValue3");
361 req->setTrailer("MyTrailer4", "MyTrailerValue4");
362 req->setTrailer("MyTrailer5", "MyTrailerValue5");
363 req->setTrailer("MyTrailer6", "MyTrailerValue6");
364 req->query("Query1", "QueryValue1");
365 req->query("Query2", "QueryValue2");
366 req->sendFile(
367 "/home/voc/projects/snodec/snode.c/CMakeLists.txt",
368 [req](int ret) {
369 if (ret == 0) {
370 VLOG(1) << req->getSocketContext()->getSocketConnection()->getConnectionName()
371 << " HTTP: Request accepted: GET / HTTP/" << req->httpMajor << "." << req->httpMinor;
372 VLOG(1) << " /home/voc/projects/snodec/snode.c/CMakeLists.txt";
373 } else {
374 LOG(ERROR) << req->getSocketContext()->getSocketConnection()->getConnectionName()
375 << " HTTP: Request failed: GET / HTTP/" << req->httpMajor << "." << req->httpMinor;
376 PLOG(ERROR) << " /home/voc/projects/snodec/snode.c/CMakeLists.txt";
377 }
378 },
379 [&req](const std::shared_ptr<Request>& reqa, const std::shared_ptr<Response>& res) {
380 logResponse(reqa, res);
381
382 req->method = "GET";
383 req->url = "/sdfsdf";
384 req->set("Connection", "close");
385 req->set("Test", "bbb");
386 req->sendFile(
387 "/home/voc/projects/snodec/snode.c/CMakeLists.txt",
388 [&req](int ret) {
389 if (ret == 0) {
390 VLOG(1) << req->getSocketContext()->getSocketConnection()->getConnectionName()
391 << " HTTP: Request accepted: GET / HTTP/" << req->httpMajor << "." << req->httpMinor;
392 VLOG(1) << " /home/voc/projects/snodec/snode.c/CMakeLists.txt";
393 } else {
394 LOG(ERROR) << req->getSocketContext()->getSocketConnection()->getConnectionName()
395 << " HTTP: Request failed: GET / HTTP/" << req->httpMajor << "." << req->httpMinor;
396 PLOG(ERROR) << " /home/voc/projects/snodec/snode.c/CMakeLists.txt";
397 }
398 },
399 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
400 logResponse(req, res);
401 },
402 [req](const std::shared_ptr<Request>&, const std::string&) {
403 });
404 },
405 [req](const std::shared_ptr<Request>&, const std::string&) {
406 });
407 req->method = "GET";
408 req->url = "/";
409 // req->set("Connection", "close");
410 req->set("Test", "xxx");
411 req->end(
412 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
413 logResponse(req, res);
414 },
415 [req](const std::shared_ptr<Request>&, const std::string&) {
416 });
417 core::EventReceiver::atNextTick([&req]() {
418 req->method = "GET";
419 req->url = "/";
420 req->set("Connection", "keep-alive");
421 req->set("Test", "ddd");
422 req->sendFile(
423 "/home/voc/projects/snodec/snode.c/CMakeLists.txt",
424 [req](int ret) {
425 if (ret == 0) {
426 VLOG(1) << req->getSocketContext()->getSocketConnection()->getConnectionName()
427 << " HTTP: Request accepted: GET / HTTP/" << req->httpMajor << "." << req->httpMinor;
428 VLOG(1) << " /home/voc/projects/snodec/snode.c/CMakeLists.txt";
429 } else {
430 LOG(ERROR) << req->getSocketContext()->getSocketConnection()->getConnectionName()
431 << " HTTP: Request failed: GET / HTTP/" << req->httpMajor << "." << req->httpMinor;
432 PLOG(ERROR) << " /home/voc/projects/snodec/snode.c/CMakeLists.txt";
433 }
434 },
435 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
436 logResponse(req, res);
437 },
438 [req](const std::shared_ptr<Request>&, const std::string&) {
439 });
440
441 req->method = "GET";
442 req->url = "/";
443 req->set("Connection", "keep-alive");
444 req->set("Test", "eee");
445 req->setTrailer("MyTrailer1", "MyTrailerValue1");
446 req->setTrailer("MyTrailer2", "MyTrailerValue2");
447 req->sendFile(
448 "/home/voc/projects/snodec/snode.c/CMakeLists.txt",
449 [req](int ret) {
450 if (ret == 0) {
451 VLOG(1) << req->getSocketContext()->getSocketConnection()->getConnectionName()
452 << " HTTP: Request accepted: GET / HTTP/" << req->httpMajor << "." << req->httpMinor;
453 VLOG(1) << " /home/voc/projects/snodec/snode.c/CMakeLists.txt";
454 } else {
455 LOG(ERROR) << req->getSocketContext()->getSocketConnection()->getConnectionName()
456 << " HTTP: Request failed: GET / HTTP/" << req->httpMajor << "." << req->httpMinor;
457 PLOG(ERROR) << " /home/voc/projects/snodec/snode.c/CMakeLists.txt";
458 }
459 },
460 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
461 logResponse(req, res);
462 },
463 [req](const std::shared_ptr<Request>&, const std::string&) {
464 });
465 });
466 */
467#endif
468 },
469 []([[maybe_unused]] const std::shared_ptr<Request>& req) {
470 VLOG(1) << req->getSocketContext()->getSocketConnection()->getConnectionName() << ": OnRequestEnd";
471 });
472
473 client.setOnConnect([](SocketConnection* socketConnection) { // onConnect
474 VLOG(1) << socketConnection->getConnectionName() << ": OnConnect";
475
476 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
477 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
478 });
479
480 client.setOnDisconnect([](SocketConnection* socketConnection) { // onDisconnect
481 VLOG(1) << socketConnection->getConnectionName() << ": OnDisconnect";
482
483 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
484 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
485 });
486
487 return client;
488 }
489
490} // namespace apps::http::legacy
491
492#endif // (STREAM_TYPE == LEGACY) // legacy
493
494#if (STREAM_TYPE == TLS) // tls
495
496namespace apps::http::tls {
497
503
504 inline Client getClient() {
505 Client client(
506 "httpclient",
507 [](const std::shared_ptr<MasterRequest>& req) {
508 VLOG(1) << req->getSocketContext()->getSocketConnection()->getConnectionName() << ": OnRequestStart";
509
510 req->url = "/";
511 req->set("Connection", "keep-alive");
512 req->end(
513 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
514 logResponse(req, res);
515 },
516 [](const std::shared_ptr<Request>&, const std::string&) {
517 });
518 req->url = "/";
519 req->set("Connection", "keep-alive");
520 req->end(
521 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
522 logResponse(req, res);
523 },
524 [](const std::shared_ptr<Request>&, const std::string&) {
525 });
526 req->url = "/index.html";
527 req->set("Connection", "keep-alive");
528 req->end(
529 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
530 logResponse(req, res);
531 },
532 [](const std::shared_ptr<Request>&, const std::string&) {
533 });
534 req->url = "/";
535 req->set("Connection", "keep-alive");
536 req->end(
537 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
538 logResponse(req, res);
539 },
540 [](const std::shared_ptr<Request>&, const std::string&) {
541 });
542 req->url = "/index.html";
543 req->set("Connection", "keep-alive");
544 req->end(
545 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
546 logResponse(req, res);
547 },
548 [](const std::shared_ptr<Request>&, const std::string&) {
549 });
550 req->url = "/";
551 req->set("Connection", "keep-alive");
552 req->end(
553 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
554 logResponse(req, res);
555 },
556 [](const std::shared_ptr<Request>&, const std::string&) {
557 });
558 req->url = "/index.html";
559 req->set("Connection", "keep-alive");
560 req->end(
561 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
562 logResponse(req, res);
563 },
564 [](const std::shared_ptr<Request>&, const std::string&) {
565 });
566 req->url = "/";
567 req->set("Connection", "keep-alive");
568 req->end(
569 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
570 logResponse(req, res);
571 },
572 [](const std::shared_ptr<Request>&, const std::string&) {
573 });
574 req->url = "/index.html";
575 req->set("Connection", "keep-alive");
576 req->end(
577 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
578 logResponse(req, res);
579 },
580 [](const std::shared_ptr<Request>&, const std::string&) {
581 });
582 req->url = "/";
583 req->set("Connection", "keep-alive");
584 req->end(
585 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
586 logResponse(req, res);
587 },
588 [](const std::shared_ptr<Request>&, const std::string&) {
589 });
590 req->url = "/index.html";
591 req->set("Connection", "keep-alive");
592 req->end(
593 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
594 logResponse(req, res);
595 },
596 [](const std::shared_ptr<Request>&, const std::string&) {
597 });
598 req->url = "/";
599 req->set("Connection", "keep-alive");
600 req->end(
601 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
602 logResponse(req, res);
603 },
604 [](const std::shared_ptr<Request>&, const std::string&) {
605 });
606 req->url = "/index.html";
607 req->set("Connection", "keep-alive");
608 req->end(
609 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
610 logResponse(req, res);
611 },
612 [](const std::shared_ptr<Request>&, const std::string&) {
613 });
614 req->url = "/";
615 req->set("Connection", "keep-alive");
616 req->end(
617 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
618 logResponse(req, res);
619 },
620 [](const std::shared_ptr<Request>&, const std::string&) {
621 });
622 req->url = "/index.html";
623 req->set("Connection", "keep-alive");
624 req->end(
625 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
626 logResponse(req, res);
627 },
628 [](const std::shared_ptr<Request>&, const std::string&) {
629 });
630 req->url = "/";
631 req->set("Connection", "keep-alive");
632 req->end(
633 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
634 logResponse(req, res);
635 },
636 [](const std::shared_ptr<Request>&, const std::string&) {
637 });
638 req->url = "/index.html";
639 req->set("Connection", "keep-alive");
640 req->end(
641 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
642 logResponse(req, res);
643 },
644 [](const std::shared_ptr<Request>&, const std::string&) {
645 });
646 req->url = "/";
647 req->set("Connection", "close");
648 req->end(
649 [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
650 logResponse(req, res);
651 },
652 [](const std::shared_ptr<Request>&, const std::string&) {
653 });
654 },
655 []([[maybe_unused]] const std::shared_ptr<Request>& req) {
656 VLOG(1) << req->getSocketContext()->getSocketConnection()->getConnectionName() << ": OnRequestEnd";
657 });
658
659 client.setOnConnect([](SocketConnection* socketConnection) { // onConnect
660 VLOG(1) << "OnConnect " << socketConnection->getConnectionName();
661
662 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
663 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
664
665 /* Enable automatic hostname checks */
666 // X509_VERIFY_PARAM* param = SSL_get0_param(socketConnection->getSSL());
667
668 // X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
669 // if (!X509_VERIFY_PARAM_set1_host(param, "localhost", sizeof("localhost") - 1)) {
670 // // handle error
671 // socketConnection->close();
672 // }
673 });
674
675 client.setOnConnected([](SocketConnection* socketConnection) { // onConnected
676 VLOG(1) << socketConnection->getConnectionName() << ": OnConnected";
677 X509* server_cert = SSL_get_peer_certificate(socketConnection->getSSL());
678 if (server_cert != nullptr) {
679 long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
680
681 VLOG(1) << "\tPeer certificate verifyErr = " + std::to_string(verifyErr) + ": " +
682 std::string(X509_verify_cert_error_string(verifyErr));
683
684 char* str = X509_NAME_oneline(X509_get_subject_name(server_cert), nullptr, 0);
685 VLOG(1) << "\t Subject: " + std::string(str);
686 OPENSSL_free(str);
687
688 str = X509_NAME_oneline(X509_get_issuer_name(server_cert), nullptr, 0);
689 VLOG(1) << "\t Issuer: " + std::string(str);
690 OPENSSL_free(str);
691
692 // We could do all sorts of certificate verification stuff here before deallocating the certificate.
693
694 GENERAL_NAMES* subjectAltNames =
695 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr));
696
697 int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
698
699 VLOG(1) << "\t Subject alternative name count: " << altNameCount;
700 for (int32_t i = 0; i < altNameCount; ++i) {
701 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
702 if (generalName->type == GEN_URI) {
703 std::string subjectAltName =
704 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
705 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
706 VLOG(1) << "\t SAN (URI): '" + subjectAltName;
707 } else if (generalName->type == GEN_DNS) {
708 std::string subjectAltName =
709 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
710 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
711 VLOG(1) << "\t SAN (DNS): '" + subjectAltName;
712 } else {
713 VLOG(1) << "\t SAN (Type): '" + std::to_string(generalName->type);
714 }
715 }
716
717 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
718
719 X509_free(server_cert);
720 } else {
721 VLOG(1) << "\tPeer certificate: no certificate";
722 }
723 });
724
725 client.setOnDisconnect([](SocketConnection* socketConnection) { // onDisconnect
726 VLOG(1) << socketConnection->getConnectionName() << ": OnDisconnect";
727
728 VLOG(1) << "\tLocal: " << socketConnection->getLocalAddress().toString();
729 VLOG(1) << "\tPeer: " << socketConnection->getRemoteAddress().toString();
730 });
731
732 return client;
733 }
734
735} // namespace apps::http::tls
736
737#endif // (STREAM_TYPE == TLS) // tls
738
739#endif // APPS_HTTP_MODEL_CLIENTS_H
#define LOG(level)
Definition Logger.h:148
#define PLOG(level)
Definition Logger.h:152
#define VLOG(level)
Definition Logger.h:164
static void init(int argc, char *argv[])
Definition SNodeC.cpp:54
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition SNodeC.cpp:60
LogMessage(Level level, int verboseLevel=-1, bool withErrno=false)
Definition Logger.cpp:280
int main(int argc, char *argv[])
#define QUOTE_INCLUDE(a)
Definition clients.h:47
#define STR_INCLUDE(a)
Definition servers.h:48
static void logResponse(const std::shared_ptr< web::http::client::Request > &req, const std::shared_ptr< web::http::client::Response > &res)
Definition clients.h:71
#define LONG
Client getClient()
Definition clients.h:96
Client getClient()
Definition clients.h:504