A bit of clean-ups and license fixes

This commit is contained in:
Alex Hultman
2018-11-22 01:29:28 +01:00
parent 63e483b815
commit a25e2ab397
7 changed files with 120 additions and 65 deletions
+34 -31
View File
@@ -5,10 +5,13 @@
int main(int argc, char **argv) {
// here we have it
struct PerSocketData {
};
uWS::App().get("/hello", [](auto *res, auto *req) {
res->end("Hello HTTP!");
}).ws("/*", {
}).ws<void>("/*", {
/*.open = */[](auto *ws, auto *req) {
},
@@ -33,47 +36,47 @@ int main(int argc, char **argv) {
}
}).run();
return 0;
// return 0;
AsyncFileStreamer *asyncFileStreamer = new AsyncFileStreamer("/home/alexhultman/v0.15/public");
// AsyncFileStreamer *asyncFileStreamer = new AsyncFileStreamer("/home/alexhultman/v0.15/public");
uWS::/*SSL*/App(/*{
.key_file_name = "/home/alexhultman/uWebSockets/misc/ssl/key.pem",
.cert_file_name = "/home/alexhultman/uWebSockets/misc/ssl/cert.pem",
.dh_params_file_name = "/home/alexhultman/dhparams.pem",
.passphrase = "1234"
}*/)/*.get("/*", [](auto *res, auto *req) {
// uWS::/*SSL*/App(/*{
// .key_file_name = "/home/alexhultman/uWebSockets/misc/ssl/key.pem",
// .cert_file_name = "/home/alexhultman/uWebSockets/misc/ssl/cert.pem",
// .dh_params_file_name = "/home/alexhultman/dhparams.pem",
// .passphrase = "1234"
// }*/)/*.get("/*", [](auto *res, auto *req) {
res->end("GET /WILDCARD");
// res->end("GET /WILDCARD");
})*/.get("/:param1/:param2", [](auto *res, auto *req) {
// })*/.get("/:param1/:param2", [](auto *res, auto *req) {
res->write("GET /:param1/:param2 = ");
res->write(req->getParameter(0));
res->write(" and ");
res->end(req->getParameter(1));
// res->write("GET /:param1/:param2 = ");
// res->write(req->getParameter(0));
// res->write(" and ");
// res->end(req->getParameter(1));
}).post("/hello", [asyncFileStreamer](auto *res, auto *req) {
// }).post("/hello", [asyncFileStreamer](auto *res, auto *req) {
// depending on the file type we want to also add mime!
//asyncFileStreamer->streamFile(res, req->getUrl());
// // depending on the file type we want to also add mime!
// //asyncFileStreamer->streamFile(res, req->getUrl());
res->end("POST /hello");
// res->end("POST /hello");
}).get("/hello", [](auto *res, auto *req) {
// }).get("/hello", [](auto *res, auto *req) {
res->end("GET /hello");
// res->end("GET /hello");
}).unhandled([](auto *res, auto *req) {
// }).unhandled([](auto *res, auto *req) {
res->writeStatus("404 Not Found");
res->writeHeader("Content-Type", "text/html; charset=utf-8");
res->end("<h1>404 Not Found</h1><i>µWebSockets v0.15</i>");
// res->writeStatus("404 Not Found");
// res->writeHeader("Content-Type", "text/html; charset=utf-8");
// res->end("<h1>404 Not Found</h1><i>µWebSockets v0.15</i>");
}).listen(3000, [](auto *token) {
if (token) {
std::cout << "Listening on port " << 3000 << std::endl;
}
}).run();
// }).listen(3000, [](auto *token) {
// if (token) {
// std::cout << "Listening on port " << 3000 << std::endl;
// }
// }).run();
}
+17 -33
View File
@@ -32,10 +32,8 @@ namespace uWS {
template <bool SSL>
struct TemplatedApp {
private:
/* The app always owns at least one http context, but creates websocket contexts on demand */
HttpContext<SSL> *httpContext;
// the app does not own a websocket context, it is created on .ws(...) calls on demand!
public:
~TemplatedApp() {
@@ -48,27 +46,28 @@ public:
TemplatedApp(us_ssl_socket_context_options sslOptions = {}) {
httpContext = uWS::HttpContext<SSL>::create(uWS::Loop::defaultLoop(), &sslOptions);
// construct the websocket cintext? no! on demand!
}
struct WebSocketBehavior {
std::function<void(void *, HttpRequest *)> open = nullptr;
std::function<void(uWS::WebSocket<false, true> *, std::string_view, uWS::OpCode)> message = nullptr;
std::function<void(uWS::WebSocket<SSL, true> *, HttpRequest *)> open = nullptr;
std::function<void(uWS::WebSocket<SSL, true> *, std::string_view, uWS::OpCode)> message = nullptr;
};
template <class UserData>
TemplatedApp &ws(std::string pattern, WebSocketBehavior &&behavior) {
/* Every route has its own websocket context with its own behavior and user data type */
auto *webSocketContext = WebSocketContext<SSL, true>::create(Loop::defaultLoop(), (typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) httpContext);
// init the websocket context here!
uWS::WebSocketContext<SSL, true> *webSocketContext = uWS::WebSocketContext<SSL, true>::create(uWS::Loop::defaultLoop(), (typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) httpContext);
/* Copy all handlers */
webSocketContext->getExt()->messageHandler = behavior.message;
return get(pattern, [webSocketContext, this, behavior](auto *res, auto *req) {
/* If we have this header set, it's a websocket */
std::string_view secWebSocketKey = req->getHeader("sec-websocket-key");
if (secWebSocketKey.length()) {
// todo: negotiate extensions such as compression here and pass autobahn fully
// note: OpenSSL can be used here to speed this up somewhat
char secWebSocketAccept[29] = {};
WebSocketHandshake::generate(secWebSocketKey.data(), secWebSocketAccept);
@@ -79,37 +78,22 @@ public:
->writeHeader("Sec-WebSocket-Accept", secWebSocketAccept)
->end();
//std::cout << "Adopting" << std::endl;
// adopting will immediately delete the socket! we cannot rely on reading anything on it
// rely on http context data
// todo: sizeof websocket
/* Adopting a socket invalidates it, do not rely on it directly to carry any data */
WebSocket<SSL, true> *webSocket = (WebSocket<SSL, true> *) StaticDispatch<SSL>::static_dispatch(us_ssl_socket_context_adopt_socket, us_socket_context_adopt_socket)(
(typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) webSocketContext, (typename StaticDispatch<SSL>::SOCKET_TYPE *) res, 150);
webSocket->init();
//std::cout << "adopted" << std::endl;
(typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) webSocketContext, (typename StaticDispatch<SSL>::SOCKET_TYPE *) res, /*sizeof(WebSocketData)*/ 150);
httpContext->upgradeToWebSocket(
webSocket
webSocket->init()
);
// we should hand the new socket to the handler
behavior.open(webSocket, req);
if (behavior.open) {
behavior.open(webSocket, req);
}
} else {
std::cout << "This is not a websocket so fuck off!" << std::endl;
// maybe pass this one to a HTTP handler on the websocket
// note: this calls the http close handler inline
/* For now we do not support having HTTP and websocket routes on the same URL */
res->close();
}
});
return *this;
+3
View File
@@ -153,6 +153,9 @@ public:
httpContextData->upgradedWebSocket = newSocket;
// I jhave no idea what this does
return true;
/*SOCKET_CONTEXT_TYPE *getSocketContext() {
return (SOCKET_CONTEXT_TYPE *) this;
+18 -1
View File
@@ -1,3 +1,19 @@
/*
* Copyright 2018 Alex Hultman and contributors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef WEBSOCKET_H
#define WEBSOCKET_H
@@ -15,8 +31,9 @@ struct WebSocket : AsyncSocket<SSL> {
private:
typedef AsyncSocket<SSL> Super;
void init() {
void *init() {
new (us_socket_ext((us_socket *) this)) WebSocketData;
return this;
}
public:
+16
View File
@@ -1,3 +1,19 @@
/*
* Copyright 2018 Alex Hultman and contributors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef WEBSOCKETCONTEXT_H
#define WEBSOCKETCONTEXT_H
+16
View File
@@ -1,3 +1,19 @@
/*
* Copyright 2018 Alex Hultman and contributors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef WEBSOCKETCONTEXTDATA_H
#define WEBSOCKETCONTEXTDATA_H
+16
View File
@@ -1,3 +1,19 @@
/*
* Copyright 2018 Alex Hultman and contributors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef WEBSOCKETDATA_H
#define WEBSOCKETDATA_H