SNode.C
Loading...
Searching...
No Matches
EventSource.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
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 WEB_HTTP_TLS_IN_EVENTSOURCE_H
43#define WEB_HTTP_TLS_IN_EVENTSOURCE_H
44
45#include "web/http/client/tools/EventSource.h"
46#include "web/http/tls/in/Client.h"
47
48#include <memory>
49
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
52#endif // DOXYGEN_SHOULD_SKIP_THIS
53
54namespace web::http::tls::in {
55
56 class EventSource : public web::http::client::tools::EventSourceT<web::http::tls::in::Client> {
57 private:
58 using Super = web::http::client::tools::EventSourceT<web::http::tls::in::Client>;
59
60 public:
61 inline ~EventSource() override;
62
63 friend inline std::shared_ptr<web::http::client::tools::EventSource> EventSource(const std::string& scheme, //
64 const SocketAddress& socketAddress,
65 const std::string& path);
66 };
67
69 }
70
71 inline std::shared_ptr<web::http::client::tools::EventSource>
72 EventSource(const std::string& scheme, const net::in::SocketAddress& socketAddress, const std::string& path) {
73 std::shared_ptr<class EventSource> eventSource = std::make_shared<class EventSource>();
74 eventSource->init(scheme, socketAddress, path);
75
76 return eventSource;
77 }
78
79 inline std::shared_ptr<web::http::client::tools::EventSource>
80 EventSource(const std::string& host, uint16_t port, const std::string& path) {
81 return EventSource("http", net::in::SocketAddress(host, port), path);
82 }
83
84 inline std::shared_ptr<web::http::client::tools::EventSource> EventSource(const std::string& url) {
85 std::shared_ptr<web::http::client::tools::EventSource> eventSource;
86
87 static const std::regex re(
88 R"(^(?:(https?)://)?(?![^/]*@)((?:(?:25[0-5]|2[0-4]\d|1?\d{1,2})\.){3}(?:25[0-5]|2[0-4]\d|1?\d{1,2})|[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*)(?::([0-9]{1,5}))?([^?#]*)(\?[^#]*)?(?:#.*)?$)",
89 std::regex::ECMAScript | std::regex::icase);
90
91 std::smatch match;
92 if (std::regex_match(url, match, re)) {
93 // [1] scheme (optional) -> "http" or "https"
94 // [2] host (required) -> hostname or IPv4 token (no [ ], no :)
95 // [3] port (optional) -> digits
96 // [4] path (may be empty)
97 // [5] query (optional)
98
99 const std::string& scheme = match[1].matched ? match[1].str() : "http";
100 const std::string& host = match[2].str();
101 const uint16_t port = match[3].matched ? static_cast<uint16_t>(std::stoi(match[3].str())) : static_cast<uint16_t>(80);
102 const std::string& path = (match[4].matched ? match[4].str() : "/");
103 const std::string& query = (match[5].matched ? match[5].str() : "");
104
105 if (scheme == "https") {
106 eventSource = EventSource(scheme, net::in::SocketAddress(host, port), path + query);
107 } else {
108 LOG(ERROR) << "Scheme not valid: " << scheme;
109 }
110 } else {
111 LOG(ERROR) << "EventSource url not accepted: " << url;
112 }
113
114 return eventSource;
115 }
116
117 inline std::shared_ptr<web::http::client::tools::EventSource> EventSource(std::string origin, std::string path) {
118 if (!origin.empty() && origin.at(origin.length() - 1) == '/') {
119 origin.pop_back();
120 }
121
122 if (!path.empty() && path.at(0) != '/') {
123 path = "/" + path;
124 }
125
126 return EventSource(!origin.empty() ? origin + path : "");
127 }
128
129} // namespace web::http::tls::in
130
131#endif // WEB_HTTP_TLS_IN_EVENTSOURCE_H
SocketAddress(const std::string &ipOrHostname, uint16_t port)
void init(const std::string &scheme, const SocketAddress &socketAddress, const std::string &path)
web::http::client::tools::EventSourceT< web::http::tls::in::Client > Super
Definition EventSource.h:58
std::shared_ptr< web::http::client::tools::EventSource > EventSource(const std::string &scheme, const net::in::SocketAddress &socketAddress, const std::string &path)
Definition EventSource.h:72
std::shared_ptr< web::http::client::tools::EventSource > EventSource(const std::string &host, uint16_t port, const std::string &path)
Definition EventSource.h:80
web::http::client::Client< net::in::stream::tls::SocketClient > Client
Definition Client.h:54