SNode.C
Loading...
Searching...
No Matches
echoserver.cpp File Reference
Include dependency graph for echoserver.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 57 of file echoserver.cpp.

57 {
58 express::WebApp::init(argc, argv);
59
60 using LegacyWebApp = express::legacy::in::WebApp;
61 using Request = LegacyWebApp::Request;
62 using Response = LegacyWebApp::Response;
63 using SocketAddress = LegacyWebApp::SocketAddress;
64
65 const LegacyWebApp legacyApp("legacy");
66
68
69 legacyApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
70 VLOG(1) << "HTTP GET on legacy /ws";
71
72 const std::string uri = req->originalUrl;
73
74 VLOG(2) << "OriginalUri: " << uri;
75 VLOG(2) << "Uri: " << req->url;
76
77 VLOG(2) << "Host: " << req->get("host");
78 VLOG(2) << "Connection: " << req->get("connection");
79 VLOG(2) << "Origin: " << req->get("origin");
80 VLOG(2) << "Sec-WebSocket-Protocol: " << req->get("sec-websocket-protocol");
81 VLOG(2) << "sec-web-socket-extensions: " << req->get("sec-websocket-extensions");
82 VLOG(2) << "sec-websocket-key: " << req->get("sec-websocket-key");
83 VLOG(2) << "sec-websocket-version: " << req->get("sec-websocket-version");
84 VLOG(2) << "upgrade: " << req->get("upgrade");
85 VLOG(2) << "user-agent: " << req->get("user-agent");
86
87 if (req->get("sec-websocket-protocol").find("echo") != std::string::npos) {
88 res->upgrade(req, [req, res](const std::string& name) {
89 if (!name.empty()) {
90 VLOG(1) << "Successful upgrade to '" << name << "' from options: " << req->get("upgrade");
91 } else {
92 VLOG(1) << "Can not upgrade to any of '" << req->get("upgrade") << "'";
93 }
94 res->end();
95 });
96 } else {
97 res->sendStatus(404);
98 }
99 });
100
101 legacyApp.get("/", [] APPLICATION(req, res) {
102 VLOG(1) << "HTTP GET on "
103 << "/";
104 if (req->url == "/" || req->url == "/index.html") {
105 req->url = "/wstest.html";
106 }
107
108 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR "/html" + req->url;
109 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req](int errnum) {
110 if (errnum == 0) {
111 VLOG(1) << req->url;
112 } else {
113 VLOG(1) << "HTTP response send file failed: " << std::strerror(errnum);
114 }
115 });
116 });
117
118 legacyApp.listen(
119 [instanceName = legacyApp.getConfig().getInstanceName()](const SocketAddress& socketAddress, const core::socket::State& state) {
120 switch (state) {
122 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
123 break;
125 VLOG(1) << instanceName << " disabled";
126 break;
128 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
129 break;
131 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
132 break;
133 }
134 });
135
136 {
137 using TlsWebApp = express::tls::in::WebApp;
138 using Request = TlsWebApp::Request;
139 using Response = TlsWebApp::Response;
140 using SocketAddress = TlsWebApp::SocketAddress;
141
142 const TlsWebApp tlsApp("tls");
143
145
146 tlsApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
147 VLOG(1) << "HTTP GET on tls /ws";
148
149 const std::string uri = req->originalUrl;
150
151 VLOG(2) << "OriginalUri: " << uri;
152 VLOG(2) << "Uri: " << req->url;
153
154 VLOG(2) << "Connection: " << req->get("connection");
155 VLOG(2) << "Host: " << req->get("host");
156 VLOG(2) << "Origin: " << req->get("origin");
157 VLOG(2) << "Sec-WebSocket-Protocol: " << req->get("sec-websocket-protocol");
158 VLOG(2) << "sec-web-socket-extensions: " << req->get("sec-websocket-extensions");
159 VLOG(2) << "sec-websocket-key: " << req->get("sec-websocket-key");
160 VLOG(2) << "sec-websocket-version: " << req->get("sec-websocket-version");
161 VLOG(2) << "upgrade: " << req->get("upgrade");
162 VLOG(2) << "user-agent: " << req->get("user-agent");
163
164 if (req->get("sec-websocket-protocol").find("echo") != std::string::npos) {
165 res->upgrade(req, [req, res](const std::string& name) {
166 if (!name.empty()) {
167 VLOG(1) << "Successful upgrade to '" << name << "' from options: " << req->get("upgrade");
168 } else {
169 VLOG(1) << "Can not upgrade to any of '" << req->get("upgrade") << "'";
170 }
171 res->end();
172 });
173 } else {
174 res->sendStatus(404);
175 }
176 });
177
178 tlsApp.get("/", [] APPLICATION(req, res) {
179 if (req->url == "/" || req->url == "/index.html") {
180 req->url = "/wstest.html";
181 }
182
183 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR "/html" + req->url;
184 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req](int ret) {
185 if (ret != 0) {
186 PLOG(ERROR) << req->url;
187 }
188 });
189 });
190
191 tlsApp.listen(
192 [instanceName = tlsApp.getConfig().getInstanceName()](const SocketAddress& socketAddress, const core::socket::State& state) {
193 switch (state) {
195 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
196 break;
198 VLOG(1) << instanceName << " disabled";
199 break;
201 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
202 break;
204 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
205 break;
206 }
207 });
208 }
209
210 return express::WebApp::start();
211}
#define APPLICATION(req, res)
Definition Router.h:68
static constexpr int DISABLED
Definition State.h:56
static constexpr int ERROR
Definition State.h:57
static constexpr int FATAL
Definition State.h:58
static constexpr int OK
Definition State.h:55
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
WebAppT< web::http::legacy::in::Server > WebApp
Definition WebApp.h:54
WebAppT< web::http::tls::in::Server > WebApp
Definition WebApp.h:54
SocketClient::SocketAddress SocketAddress

References core::socket::State::DISABLED, express::Response::end(), core::socket::State::ERROR, core::socket::State::FATAL, express::Request::get(), core::socket::Socket< ConfigT >::getConfig(), net::config::ConfigInstance::getInstanceName(), express::WebApp::init(), core::socket::stream::SocketServer< SocketAcceptorT, SocketContextFactoryT, Args >::listen(), core::socket::State::OK, express::Request::originalUrl, express::Response::sendStatus(), express::WebApp::start(), net::in::SocketAddress::toString(), express::Response::upgrade(), express::Request::url, express::WebAppT< ServerT >::WebAppT(), and core::socket::State::what().

Here is the call graph for this function: