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