SNode.C
Loading...
Searching...
No Matches
httplowlevelclient.cpp
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
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#include "core/SNodeC.h"
43#include "core/socket/stream/SocketContext.h"
44#include "core/socket/stream/SocketContextFactory.h"
45#include "net/in/stream/legacy/SocketClient.h"
46#include "net/in/stream/tls/SocketClient.h"
47#include "web/http/client/ResponseParser.h"
48
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
51#include "log/Logger.h"
52
53#include <cstddef>
54#include <openssl/ssl.h>
55#include <openssl/x509v3.h>
56#include <string>
57
58// IWYU pragma: no_include <openssl/ssl3.h>
59// IWYU pragma: no_include <openssl/x509.h>
60// IWYU pragma: no_include <openssl/types.h>
61// IWYU pragma: no_include <openssl/asn1.h>
62// IWYU pragma: no_include <openssl/obj_mac.h>
63// IWYU pragma: no_include <openssl/crypto.h>
64
65#endif /* DOXYGEN_SHOULD_SKIP_THIS */
66
67namespace apps::http {
68
69 static web::http::client::ResponseParser* getResponseParser(core::socket::stream::SocketContext* socketContext) {
70 web::http::client::ResponseParser* responseParser = new web::http::client::ResponseParser(
71 socketContext,
72 []() {
73 VLOG(1) << "++ OnStarted";
74 },
75 []([[maybe_unused]] web::http::client::Response&& res) {
76 VLOG(1) << "++ OnParsed";
77 },
78 [](int status, const std::string& reason) {
79 VLOG(1) << "++ OnError: " + std::to_string(status) + " - " + reason;
80 });
81
82 return responseParser;
83 }
84
85 class SimpleSocketProtocol : public core::socket::stream::SocketContext {
86 public:
87 explicit SimpleSocketProtocol(core::socket::stream::SocketConnection* socketConnection)
88 : core::socket::stream::SocketContext(socketConnection) {
90 }
91
93
94 void onConnected() override {
95 VLOG(1) << "SimpleSocketProtocol connected";
96 }
97 void onDisconnected() override {
98 VLOG(1) << "SimpleSocketProtocol disconnected";
99 }
100
101 bool onSignal([[maybe_unused]] int signum) override {
102 return true;
103 }
104
105 std::size_t onReceivedFromPeer() override {
107 }
108
109 void onWriteError(int errnum) override {
112 }
113
114 void onReadError(int errnum) override {
117 }
118
119 private:
120 web::http::client::ResponseParser* responseParser;
121 };
122
124 delete responseParser;
125 }
126
128 public:
130
131 private:
132 core::socket::stream::SocketContext* create(core::socket::stream::SocketConnection* socketConnection) override {
133 return new SimpleSocketProtocol(socketConnection);
134 }
135 };
136
138 }
139
140} // namespace apps::http
141
142namespace tls {
143
144 using SocketClient = net::in::stream::tls::SocketClient<apps::http::SimpleSocketProtocolFactory>;
145 using SocketAddress = SocketClient::SocketAddress;
146 using SocketConnection = SocketClient::SocketConnection;
147
148 SocketClient getClient() {
149 SocketClient tlsClient(
150 "tls",
151 [](SocketConnection* socketConnection) { // onConnect
152 VLOG(1) << "OnConnect";
153
154 VLOG(1) << "\tServer: " << socketConnection->getRemoteAddress().toString();
155 VLOG(1) << "\tClient: " << socketConnection->getLocalAddress().toString();
156
157 /* Enable automatic hostname checks */
158 // X509_VERIFY_PARAM* param = SSL_get0_param(socketConnection->getSSL());
159
160 // X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
161 // if (!X509_VERIFY_PARAM_set1_host(param, "localhost", sizeof("localhost") - 1)) {
162 // // handle error
163 // socketConnection->close();
164 // }
165 },
166 [](SocketConnection* socketConnection) { // onConnected
167 VLOG(1) << "OnConnected";
168
169 X509* server_cert = SSL_get_peer_certificate(socketConnection->getSSL());
170 if (server_cert != nullptr) {
171 const long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
172
173 VLOG(1) << " Server certificate: " + std::string(X509_verify_cert_error_string(verifyErr));
174
175 char* str = X509_NAME_oneline(X509_get_subject_name(server_cert), nullptr, 0);
176 VLOG(1) << " Subject: " + std::string(str);
177 OPENSSL_free(str);
178
179 str = X509_NAME_oneline(X509_get_issuer_name(server_cert), nullptr, 0);
180 VLOG(1) << " Issuer: " + std::string(str);
181 OPENSSL_free(str);
182
183 // We could do all sorts of certificate verification stuff here before deallocating the certificate.
184
185 GENERAL_NAMES* subjectAltNames =
186 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr));
187#ifdef __GNUC__
188#pragma GCC diagnostic push
189#ifdef __has_warning
190#if __has_warning("-Wused-but-marked-unused")
191#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
192#endif
193#endif
194#endif
195 const int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
196#ifdef __GNUC_
197#pragma GCC diagnostic pop
198#endif
199 VLOG(1) << "\t Subject alternative name count: " << altNameCount;
200 for (int32_t i = 0; i < altNameCount; ++i) {
201#ifdef __GNUC__
202#pragma GCC diagnostic push
203#ifdef __has_warning
204#if __has_warning("-Wused-but-marked-unused")
205#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
206#endif
207#endif
208#endif
209 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
210#ifdef __GNUC_
211#pragma GCC diagnostic pop
212#endif
213 if (generalName->type == GEN_URI) {
214 const std::string subjectAltName =
215 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
216 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
217 VLOG(1) << "\t SAN (URI): '" + subjectAltName;
218 } else if (generalName->type == GEN_DNS) {
219 const std::string subjectAltName =
220 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
221 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
222 VLOG(1) << "\t SAN (DNS): '" + subjectAltName;
223 } else {
224 VLOG(1) << "\t SAN (Type): '" + std::to_string(generalName->type);
225 }
226 }
227#ifdef __GNUC__
228#pragma GCC diagnostic push
229#ifdef __has_warning
230#if __has_warning("-Wused-but-marked-unused")
231#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
232#endif
233#endif
234#endif
235 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
236#ifdef __GNUC_
237#pragma GCC diagnostic pop
238#endif
239 X509_free(server_cert);
240 } else {
241 VLOG(1) << " Server certificate: no certificate";
242 }
243
244 socketConnection->sendToPeer("GET /index.html HTTP/1.1\r\nConnection: close\r\n\r\n"); // Connection: close\r\n\r\n");
245 },
246 [](SocketConnection* socketConnection) { // onDisconnect
247 VLOG(1) << "OnDisconnect";
248
249 VLOG(1) << "\tServer: " + socketConnection->getRemoteAddress().toString();
250 VLOG(1) << "\tClient: " + socketConnection->getLocalAddress().toString();
251
252 });
253
254 const SocketAddress remoteAddress("localhost", 8088);
255
256 tlsClient.connect(remoteAddress,
257 [instanceName = tlsClient.getConfig().getInstanceName()](
258 const SocketAddress& socketAddress,
259 const core::socket::State& state) { // example.com:81 simulate connnect timeout
260 switch (state) {
261 case core::socket::State::OK:
262 VLOG(1) << instanceName << ": connected to '" << socketAddress.toString() << "'";
263 break;
264 case core::socket::State::DISABLED:
265 VLOG(1) << instanceName << ": disabled";
266 break;
267 case core::socket::State::ERROR:
268 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
269 break;
270 case core::socket::State::FATAL:
271 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
272 break;
273 }
274 });
275 return tlsClient;
276 }
277
278} // namespace tls
279
280namespace legacy {
281
282 using SocketClient = net::in::stream::legacy::SocketClient<apps::http::SimpleSocketProtocolFactory>;
283 using SocketAddress = SocketClient::SocketAddress;
284 using SocketConnection = SocketClient::SocketConnection;
285
286 SocketClient getLegacyClient() {
287 SocketClient legacyClient(
288 "legacy",
289 [](SocketConnection* socketConnection) { // OnConnect
290 VLOG(1) << "OnConnect";
291
292 VLOG(1) << "\tServer: " << socketConnection->getRemoteAddress().toString();
293 VLOG(1) << "\tClient: " << socketConnection->getLocalAddress().toString();
294 },
295 [](SocketConnection* socketConnection) { // onConnected
296 VLOG(1) << "OnConnected";
297
298 socketConnection->sendToPeer("GET /index.html HTTP/1.1\r\nConnection: close\r\n\r\n"); // Connection: close\r\n\r\n");
299 },
300 [](SocketConnection* socketConnection) { // onDisconnect
301 VLOG(1) << "OnDisconnect";
302
303 VLOG(1) << "\tServer: " << socketConnection->getRemoteAddress().toString();
304 VLOG(1) << "\tClient: " << socketConnection->getLocalAddress().toString();
305 });
306
307 SocketAddress remoteAddress("localhost", 8080);
308
309 remoteAddress.init();
310
311 VLOG(1) << "###############': " << remoteAddress.getCanonName();
312 VLOG(1) << "###############': " << remoteAddress.toString();
313
314 legacyClient.connect(remoteAddress,
315 [instanceName = legacyClient.getConfig().getInstanceName()](
316 const tls::SocketAddress& socketAddress,
317 const core::socket::State& state) { // example.com:81 simulate connnect timeout
318 switch (state) {
319 case core::socket::State::OK:
320 VLOG(1) << instanceName << ": connected to '" << socketAddress.toString() << "'";
321 break;
322 case core::socket::State::DISABLED:
323 VLOG(1) << instanceName << ": disabled";
324 break;
325 case core::socket::State::ERROR:
326 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
327 break;
328 case core::socket::State::FATAL:
329 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
330 break;
331 }
332 });
333
334 return legacyClient;
335 }
336
337} // namespace legacy
338
339int main(int argc, char* argv[]) {
340 core::SNodeC::init(argc, argv);
341
342 {
343 const legacy::SocketAddress legacyRemoteAddress("localhost", 8080);
344
345 const legacy::SocketClient legacyClient = legacy::getLegacyClient();
346
347 legacyClient.connect(legacyRemoteAddress,
348 [instanceName = legacyClient.getConfig().getInstanceName()](
349 const tls::SocketAddress& socketAddress,
350 const core::socket::State& state) { // example.com:81 simulate connnect timeout
351 switch (state) {
352 case core::socket::State::OK:
353 VLOG(1) << instanceName << ": connected to '" << socketAddress.toString() << "'";
354 break;
355 case core::socket::State::DISABLED:
356 VLOG(1) << instanceName << ": disabled";
357 break;
358 case core::socket::State::ERROR:
359 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
360 break;
361 case core::socket::State::FATAL:
362 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
363 break;
364 }
365 });
366
367 const tls::SocketAddress tlsRemoteAddress = tls::SocketAddress("localhost", 8088);
368
369 const tls::SocketClient tlsClient = tls::getClient();
370
371 tlsClient.connect(tlsRemoteAddress,
372 [instanceName = tlsClient.getConfig().getInstanceName()](
373 const tls::SocketAddress& socketAddress,
374 const core::socket::State& state) { // example.com:81 simulate connnect timeout
375 switch (state) {
376 case core::socket::State::OK:
377 VLOG(1) << instanceName << ": connected to '" << socketAddress.toString() << "'";
378 break;
379 case core::socket::State::DISABLED:
380 VLOG(1) << instanceName << ": disabled";
381 break;
382 case core::socket::State::ERROR:
383 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
384 break;
385 case core::socket::State::FATAL:
386 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
387 break;
388 }
389 });
390 }
391
392 return core::SNodeC::start();
393}
core::socket::stream::SocketContext * create(core::socket::stream::SocketConnection *socketConnection) override
web::http::client::ResponseParser * responseParser
SimpleSocketProtocol(core::socket::stream::SocketConnection *socketConnection)
void onReadError(int errnum) override
void onWriteError(int errnum) override
std::size_t onReceivedFromPeer() override
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
Config & getConfig() const
Definition Socket.hpp:60
static constexpr int DISABLED
Definition State.h:56
static constexpr int ERROR
Definition State.h:57
std::string what() const
Definition State.cpp:114
static constexpr int FATAL
Definition State.h:58
static constexpr int OK
Definition State.h:55
void connect(const SocketAddress &remoteAddress, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const SocketAddress & getRemoteAddress() const final
const SocketAddress & getLocalAddress() const final
void sendToPeer(const std::string &data)
SocketContext(core::socket::stream::SocketConnection *socketConnection)
void onReadError(int errnum) override
void shutdownWrite(bool forceClose=false)
void onWriteError(int errnum) override
const std::string & getInstanceName() const
SocketAddress(const std::string &ipOrHostname, uint16_t port)
std::string getCanonName() const
std::string toString(bool expanded=true) const override
void init(const Hints &hints={.aiFlags=0,.aiSockType=0,.aiProtocol=0})
std::size_t parse()
Definition Parser.cpp:92
ResponseParser(core::socket::stream::SocketContext *socketContext, const std::function< void()> &onResponseStart, const std::function< void(Response &&)> &onResponseParsed, const std::function< void(int, const std::string &)> &onResponseParseError)
int main(int argc, char *argv[])
static web::http::client::ResponseParser * getResponseParser(core::socket::stream::SocketContext *socketContext)
SocketClient getLegacyClient()
SocketClient getClient()