SNode.C
Loading...
Searching...
No Matches
echoserver.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, 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#include "express/legacy/in/WebApp.h"
43#include "express/middleware/VerboseRequest.h"
44#include "express/tls/in/WebApp.h"
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#include "log/Logger.h"
49
50#include <algorithm>
51#include <cstring>
52#include <list>
53
54#endif /* DOXYGEN_SHOULD_SKIP_THIS */
55
56using namespace express;
57
58int main(int argc, char* argv[]) {
59 express::WebApp::init(argc, argv);
60
61 using LegacyWebApp = express::legacy::in::WebApp;
62 using Request = LegacyWebApp::Request;
63 using Response = LegacyWebApp::Response;
64 using SocketAddress = LegacyWebApp::SocketAddress;
65
66 const LegacyWebApp legacyApp("legacy");
67
68 legacyApp.use(express::middleware::VerboseRequest());
69
70 legacyApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
71 const std::string connectionName = res->getSocketContext()->getSocketConnection()->getConnectionName();
72
73 res->upgrade(req, [req, res, connectionName](const std::string& name) {
74 if (!name.empty()) {
75 VLOG(1) << connectionName << ": Successful upgrade:";
76 VLOG(1) << connectionName << ": Requested: " << req->get("upgrade");
77 VLOG(1) << connectionName << ": Selected: " << name;
78
79 res->end();
80 } else {
81 VLOG(1) << connectionName << ": Can not upgrade to any of '" << req->get("upgrade") << "'";
82
83 res->sendStatus(404);
84 }
85 });
86 });
87
88 legacyApp.get("/", [] APPLICATION(req, res) {
89 VLOG(1) << "HTTP GET on "
90 << "/";
91 if (req->url == "/" || req->url == "/index.html") {
92 req->url = "/wstest.html";
93 }
94
95 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR "/html" + req->url;
96 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req, res](int errnum) {
97 if (errnum == 0) {
98 VLOG(1) << req->url;
99 } else {
100 VLOG(1) << "HTTP response send file failed: " << std::strerror(errnum);
101 res->sendStatus(404);
102 }
103 });
104 });
105
106 legacyApp
107 .setOnInitState([]([[maybe_unused]] core::eventreceiver::AcceptEventReceiver* acceptEventReceiver) {
108 VLOG(0) << "------------------- Legacy Server Init: " << acceptEventReceiver;
109 if (acceptEventReceiver->isEnabled()) {
110 acceptEventReceiver->stopListen();
111 }
112 })
114 [instanceName = legacyApp.getConfig().getInstanceName()](const SocketAddress& socketAddress, const core::socket::State& state) {
115 switch (state) {
116 case core::socket::State::OK:
117 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
118 break;
119 case core::socket::State::DISABLED:
120 VLOG(1) << instanceName << " disabled";
121 break;
122 case core::socket::State::ERROR:
123 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
124 break;
125 case core::socket::State::FATAL:
126 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
127 break;
128 }
129 });
130
131 VLOG(1) << "Legacy Routes:";
132 for (std::string& route : legacyApp.getRoutes()) {
133 route.erase(std::remove(route.begin(), route.end(), '$'), route.end());
134
135 VLOG(1) << " " << route;
136 }
137
138 {
139 using TlsWebApp = express::tls::in::WebApp;
140 using Request = TlsWebApp::Request;
141 using Response = TlsWebApp::Response;
142 using SocketAddress = TlsWebApp::SocketAddress;
143
144 const TlsWebApp tlsApp("tls");
145
146 tlsApp.use(express::middleware::VerboseRequest());
147
148 tlsApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
149 const std::string connectionName = res->getSocketContext()->getSocketConnection()->getConnectionName();
150
151 res->upgrade(req, [req, res, connectionName](const std::string& name) {
152 if (!name.empty()) {
153 VLOG(1) << connectionName << ": Upgrade success:";
154 VLOG(1) << connectionName << ": Requested: " << req->get("upgrade");
155 VLOG(1) << connectionName << ": Selected: " << name;
156
157 res->end();
158 } else {
159 VLOG(1) << connectionName << ": Can not upgrade to any of '" << req->get("upgrade") << "'";
160
161 res->sendStatus(404);
162 }
163 });
164 });
165
166 tlsApp.get("/", [] APPLICATION(req, res) {
167 if (req->url == "/" || req->url == "/index.html") {
168 req->url = "/wstest.html";
169 }
170
171 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR "/html" + req->url;
172 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req, res](int errnum) {
173 if (errnum == 0) {
174 VLOG(1) << req->url;
175 } else {
176 VLOG(1) << "HTTP response send file failed: " << std::strerror(errnum);
177 res->sendStatus(404);
178 }
179 });
180 });
181
182 tlsApp
183 .setOnInitState([]([[maybe_unused]] core::eventreceiver::AcceptEventReceiver* acceptEventReceiver) {
184 VLOG(0) << "------------------- TLS Server Init: " << acceptEventReceiver;
185 })
186 .listen([instanceName = tlsApp.getConfig().getInstanceName()](const SocketAddress& socketAddress,
187 const core::socket::State& state) {
188 switch (state) {
189 case core::socket::State::OK:
190 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
191 break;
192 case core::socket::State::DISABLED:
193 VLOG(1) << instanceName << " disabled";
194 break;
195 case core::socket::State::ERROR:
196 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
197 break;
198 case core::socket::State::FATAL:
199 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
200 break;
201 }
202 });
203
204 VLOG(1) << "Tls Routes:";
205 for (std::string& route : legacyApp.getRoutes()) {
206 route.erase(std::remove(route.begin(), route.end(), '$'), route.end());
207
208 VLOG(1) << " " << route;
209 }
210 }
211
212 return express::WebApp::start();
213}
214
215/*
2162021-05-14 10:21:39 0000000002: accept-encoding: gzip, deflate, br
2172021-05-14 10:21:39 0000000002: accept-language: en-us,en;q=0.9,de-at;q=0.8,de-de;q=0.7,de;q=0.6
2182021-05-14 10:21:39 0000000002: cache-control: no-cache
2192021-05-14 10:21:39 0000000002: connection: upgrade, keep-alive
2202021-05-14 10:21:39 0000000002: host: localhost:8080
2212021-05-14 10:21:39 0000000002: origin: file://
2222021-05-14 10:21:39 0000000002: pragma: no-cache
2232021-05-14 10:21:39 0000000002: sec-websocket-extensions: permessage-deflate; client_max_window_bits
2242021-05-14 10:21:39 0000000002: sec-websocket-key: et6vtby1wwyooittpidflw==
2252021-05-14 10:21:39 0000000002: sec-websocket-version: 13
2262021-05-14 10:21:39 0000000002: upgrade: websocket
2272021-05-14 10:21:39 0000000002: user-agent: mozilla/5.0 (x11; linux x86_64) applewebkit/537.36 (khtml, like gecko)
228chrome/90.0.4430.212 safari/537.36
229*/
#define APPLICATION(req, res)
Definition Router.h:68
Config & getConfig() const
Definition Socket.hpp:65
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
const std::string & getConnectionName() const
SocketConnection * getSocketConnection() const
const SocketServer & listen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const SocketServer & setOnInitState(const std::function< void(core::eventreceiver::AcceptEventReceiver *)> &onInitState, bool initialize=false) const
std::string url
Definition Request.h:98
const std::string & get(const std::string &key, int i=0) const
Definition Request.cpp:95
void sendFile(const std::string &file, const std::function< void(int)> &callback)
Definition Response.cpp:181
void sendStatus(int state, const std::string &html={})
Definition Response.cpp:95
web::http::server::SocketContext * getSocketContext() const
Definition Response.cpp:68
void upgrade(const std::shared_ptr< Request > &request, const std::function< void(const std::string)> &status)
Definition Response.cpp:173
std::list< std::string > getRoutes() const
Definition Router.cpp:96
WebAppT(const std::string &name)
Definition WebAppT.h:74
static void init(int argc, char *argv[])
Definition WebApp.cpp:56
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition WebApp.cpp:60
const std::string & getInstanceName() const
std::string toString(bool expanded=true) const override
int main(int argc, char *argv[])