35 {
37
39 using Request = LegacyWebApp::Request;
40 using Response = LegacyWebApp::Response;
42
43 const LegacyWebApp legacyApp("legacy");
44
46
48 VLOG(1) << "HTTP GET on "
49 << "/";
50 if (req->url == "/" || req->url == "/index.html") {
51 req->url = "/wstest.html";
52 }
53
54 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR "/html" + req->url;
55 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req](int errnum) {
56 if (errnum == 0) {
57 VLOG(1) << req->url;
58 } else {
59 VLOG(1) << "HTTP response send file failed: " << std::strerror(errnum);
60 }
61 });
62 });
63
64 legacyApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
65 VLOG(1) << "HTTP GET on legacy /ws";
66
67 const std::string uri = req->originalUrl;
68
69 VLOG(2) << "OriginalUri: " << uri;
70 VLOG(2) << "Uri: " << req->url;
71
72 VLOG(2) << "Host: " << req->get("host");
73 VLOG(2) << "Connection: " << req->get("connection");
74 VLOG(2) << "Origin: " << req->get("origin");
75 VLOG(2) << "Sec-WebSocket-Protocol: " << req->get("sec-websocket-protocol");
76 VLOG(2) << "sec-web-socket-extensions: " << req->get("sec-websocket-extensions");
77 VLOG(2) << "sec-websocket-key: " << req->get("sec-websocket-key");
78 VLOG(2) << "sec-websocket-version: " << req->get("sec-websocket-version");
79 VLOG(2) << "upgrade: " << req->get("upgrade");
80 VLOG(2) << "user-agent: " << req->get("user-agent");
81
82 if (req->get("sec-websocket-protocol").find("echo") != std::string::npos) {
83 res->upgrade(req, [req, res](const std::string& name) {
84 if (!name.empty()) {
85 VLOG(1) << "Successful upgrade to '" << name << "' from options: " << req->get("upgrade");
86 } else {
87 VLOG(1) << "Can not upgrade to any of '" << req->get("upgrade") << "'";
88 }
89 res->end();
90 });
91 } else {
92 res->sendStatus(404);
93 }
94 });
95
96 legacyApp.listen(
98 switch (state) {
100 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
101 break;
103 VLOG(1) << instanceName << " disabled";
104 break;
106 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
107 break;
109 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
110 break;
111 }
112 });
113
114 {
116 using Request = TlsWebApp::Request;
117 using Response = TlsWebApp::Response;
119
120 const TlsWebApp tlsApp("tls");
121
123
125 if (req->url == "/" || req->url == "/index.html") {
126 req->url = "/wstest.html";
127 }
128
129 VLOG(1) << CMAKE_CURRENT_SOURCE_DIR "/html" + req->url;
130 res->sendFile(CMAKE_CURRENT_SOURCE_DIR "/html" + req->url, [req](int ret) {
131 if (ret != 0) {
132 PLOG(ERROR) << req->url;
133 }
134 });
135 });
136
137 tlsApp.get("/ws", [](const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
138 VLOG(1) << "HTTP GET on tls /ws";
139
140 const std::string uri = req->originalUrl;
141
142 VLOG(2) << "OriginalUri: " << uri;
143 VLOG(2) << "Uri: " << req->url;
144
145 VLOG(2) << "Connection: " << req->get("connection");
146 VLOG(2) << "Host: " << req->get("host");
147 VLOG(2) << "Origin: " << req->get("origin");
148 VLOG(2) << "Sec-WebSocket-Protocol: " << req->get("sec-websocket-protocol");
149 VLOG(2) << "sec-web-socket-extensions: " << req->get("sec-websocket-extensions");
150 VLOG(2) << "sec-websocket-key: " << req->get("sec-websocket-key");
151 VLOG(2) << "sec-websocket-version: " << req->get("sec-websocket-version");
152 VLOG(2) << "upgrade: " << req->get("upgrade");
153 VLOG(2) << "user-agent: " << req->get("user-agent");
154
155 if (req->get("sec-websocket-protocol").find("echo") != std::string::npos) {
156 res->upgrade(req, [req, res](const std::string& name) {
157 if (!name.empty()) {
158 VLOG(1) << "Successful upgrade to '" << name << "' from options: " << req->get("upgrade");
159 } else {
160 VLOG(1) << "Can not upgrade to any of '" << req->get("upgrade") << "'";
161 }
162 res->end();
163 });
164 } else {
165 res->sendStatus(404);
166 }
167 });
168
169 tlsApp.listen(
171 switch (state) {
173 VLOG(1) << instanceName << " listening on '" << socketAddress.toString() << "'";
174 break;
176 VLOG(1) << instanceName << " disabled";
177 break;
179 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
180 break;
182 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
183 break;
184 }
185 });
186 }
187
189}
#define APPLICATION(req, res)
static constexpr int DISABLED
static constexpr int ERROR
static constexpr int FATAL
static void init(int argc, char *argv[])
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
WebAppT< web::http::legacy::in::ServerBase< web::http::server::Request, web::http::server::Response > > WebApp
WebAppT< web::http::tls::in::ServerBase< web::http::server::Request, web::http::server::Response > > WebApp
SocketClient::SocketAddress SocketAddress