2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#include "express/legacy/in/WebApp.h"
43#include "express/middleware/VerboseRequest.h"
44#include "express/tls/in/WebApp.h"
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
48#include "log/Logger.h"
55using namespace express;
57int main(
int argc,
char* argv[]) {
60 using LegacyWebApp = express::legacy::in::WebApp;
61 using Request = LegacyWebApp::Request;
62 using Response = LegacyWebApp::Response;
63 using SocketAddress = LegacyWebApp::SocketAddress;
65 const LegacyWebApp legacyApp
("legacy");
67 legacyApp.use(express::middleware::VerboseRequest());
69 legacyApp.get(
"/ws", [](
const std::shared_ptr<Request>& req,
const std::shared_ptr<Response>& res) {
70 VLOG(1) <<
"HTTP GET on legacy /ws";
74 VLOG(2) <<
"OriginalUri: " << uri;
75 VLOG(2) <<
"Uri: " << req->
url;
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");
87 if (req->
get("sec-websocket-protocol").find(
"echo") != std::string::npos) {
88 res->
upgrade(req
, [req, res](
const std::string& name) {
90 VLOG(1) <<
"Successful upgrade to '" << name <<
"' from options: " << req->
get("upgrade");
92 VLOG(1) <<
"Can not upgrade to any of '" << req->
get("upgrade") <<
"'";
102 VLOG(1) <<
"HTTP GET on "
104 if (req->
url ==
"/" || req->
url ==
"/index.html") {
105 req->
url =
"/wstest.html";
108 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR
"/html" + req->url;
109 res->sendFile(CMAKE_CURRENT_SOURCE_DIR
"/html" + req->url, [req](
int errnum) {
113 VLOG(1) <<
"HTTP response send file failed: " << std::strerror(errnum);
122 VLOG(1) << instanceName <<
" listening on '" << socketAddress
.toString() <<
"'";
125 VLOG(1) << instanceName <<
" disabled";
137 using TlsWebApp = express::tls::in::WebApp;
138 using Request = TlsWebApp::Request;
139 using Response = TlsWebApp::Response;
140 using SocketAddress = TlsWebApp::SocketAddress;
142 const TlsWebApp tlsApp
("tls");
144 tlsApp.use(express::middleware::VerboseRequest());
146 tlsApp.get(
"/ws", [](
const std::shared_ptr<Request>& req,
const std::shared_ptr<Response>& res) {
147 VLOG(1) <<
"HTTP GET on tls /ws";
151 VLOG(2) <<
"OriginalUri: " << uri;
152 VLOG(2) <<
"Uri: " << req->
url;
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");
164 if (req->
get("sec-websocket-protocol").find(
"echo") != std::string::npos) {
165 res->
upgrade(req
, [req, res](
const std::string& name) {
167 VLOG(1) <<
"Successful upgrade to '" << name <<
"' from options: " << req->
get("upgrade");
169 VLOG(1) <<
"Can not upgrade to any of '" << req->
get("upgrade") <<
"'";
179 if (req->
url ==
"/" || req->
url ==
"/index.html") {
180 req->
url =
"/wstest.html";
183 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR
"/html" + req->url;
184 res->sendFile(CMAKE_CURRENT_SOURCE_DIR
"/html" + req->url, [req](
int ret) {
186 PLOG(ERROR) << req->url;
195 VLOG(1) << instanceName <<
" listening on '" << socketAddress
.toString() <<
"'";
198 VLOG(1) << instanceName <<
" disabled";
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#define APPLICATION(req, res)
Config & getConfig() const
static constexpr int DISABLED
static constexpr int ERROR
static constexpr int FATAL
void listen(const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const std::string & get(const std::string &key, int i=0) const
void sendStatus(int state)
void upgrade(const std::shared_ptr< Request > &request, const std::function< void(const std::string)> &status)
WebAppT(const std::string &name)
static void init(int argc, char *argv[])
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
const std::string & getInstanceName() const
std::string toString(bool expanded=true) const override
int main(int argc, char *argv[])