A bit of clean-ups and license fixes
This commit is contained in:
+34
-31
@@ -5,10 +5,13 @@
|
|||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
// here we have it
|
struct PerSocketData {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
uWS::App().get("/hello", [](auto *res, auto *req) {
|
uWS::App().get("/hello", [](auto *res, auto *req) {
|
||||||
res->end("Hello HTTP!");
|
res->end("Hello HTTP!");
|
||||||
}).ws("/*", {
|
}).ws<void>("/*", {
|
||||||
/*.open = */[](auto *ws, auto *req) {
|
/*.open = */[](auto *ws, auto *req) {
|
||||||
|
|
||||||
},
|
},
|
||||||
@@ -33,47 +36,47 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
}).run();
|
}).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(/*{
|
// uWS::/*SSL*/App(/*{
|
||||||
.key_file_name = "/home/alexhultman/uWebSockets/misc/ssl/key.pem",
|
// .key_file_name = "/home/alexhultman/uWebSockets/misc/ssl/key.pem",
|
||||||
.cert_file_name = "/home/alexhultman/uWebSockets/misc/ssl/cert.pem",
|
// .cert_file_name = "/home/alexhultman/uWebSockets/misc/ssl/cert.pem",
|
||||||
.dh_params_file_name = "/home/alexhultman/dhparams.pem",
|
// .dh_params_file_name = "/home/alexhultman/dhparams.pem",
|
||||||
.passphrase = "1234"
|
// .passphrase = "1234"
|
||||||
}*/)/*.get("/*", [](auto *res, auto *req) {
|
// }*/)/*.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("GET /:param1/:param2 = ");
|
||||||
res->write(req->getParameter(0));
|
// res->write(req->getParameter(0));
|
||||||
res->write(" and ");
|
// res->write(" and ");
|
||||||
res->end(req->getParameter(1));
|
// 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!
|
// // depending on the file type we want to also add mime!
|
||||||
//asyncFileStreamer->streamFile(res, req->getUrl());
|
// //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->writeStatus("404 Not Found");
|
||||||
res->writeHeader("Content-Type", "text/html; charset=utf-8");
|
// res->writeHeader("Content-Type", "text/html; charset=utf-8");
|
||||||
res->end("<h1>404 Not Found</h1><i>µWebSockets v0.15</i>");
|
// res->end("<h1>404 Not Found</h1><i>µWebSockets v0.15</i>");
|
||||||
|
|
||||||
}).listen(3000, [](auto *token) {
|
// }).listen(3000, [](auto *token) {
|
||||||
if (token) {
|
// if (token) {
|
||||||
std::cout << "Listening on port " << 3000 << std::endl;
|
// std::cout << "Listening on port " << 3000 << std::endl;
|
||||||
}
|
// }
|
||||||
}).run();
|
// }).run();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,10 +32,8 @@ namespace uWS {
|
|||||||
template <bool SSL>
|
template <bool SSL>
|
||||||
struct TemplatedApp {
|
struct TemplatedApp {
|
||||||
private:
|
private:
|
||||||
|
/* The app always owns at least one http context, but creates websocket contexts on demand */
|
||||||
HttpContext<SSL> *httpContext;
|
HttpContext<SSL> *httpContext;
|
||||||
|
|
||||||
// the app does not own a websocket context, it is created on .ws(...) calls on demand!
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
~TemplatedApp() {
|
~TemplatedApp() {
|
||||||
@@ -48,27 +46,28 @@ public:
|
|||||||
|
|
||||||
TemplatedApp(us_ssl_socket_context_options sslOptions = {}) {
|
TemplatedApp(us_ssl_socket_context_options sslOptions = {}) {
|
||||||
httpContext = uWS::HttpContext<SSL>::create(uWS::Loop::defaultLoop(), &sslOptions);
|
httpContext = uWS::HttpContext<SSL>::create(uWS::Loop::defaultLoop(), &sslOptions);
|
||||||
|
|
||||||
// construct the websocket cintext? no! on demand!
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct WebSocketBehavior {
|
struct WebSocketBehavior {
|
||||||
std::function<void(void *, HttpRequest *)> open = nullptr;
|
std::function<void(uWS::WebSocket<SSL, true> *, HttpRequest *)> open = nullptr;
|
||||||
std::function<void(uWS::WebSocket<false, true> *, std::string_view, uWS::OpCode)> message = 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) {
|
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!
|
/* Copy all handlers */
|
||||||
uWS::WebSocketContext<SSL, true> *webSocketContext = uWS::WebSocketContext<SSL, true>::create(uWS::Loop::defaultLoop(), (typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) httpContext);
|
|
||||||
|
|
||||||
webSocketContext->getExt()->messageHandler = behavior.message;
|
webSocketContext->getExt()->messageHandler = behavior.message;
|
||||||
|
|
||||||
return get(pattern, [webSocketContext, this, behavior](auto *res, auto *req) {
|
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");
|
std::string_view secWebSocketKey = req->getHeader("sec-websocket-key");
|
||||||
if (secWebSocketKey.length()) {
|
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
|
// note: OpenSSL can be used here to speed this up somewhat
|
||||||
char secWebSocketAccept[29] = {};
|
char secWebSocketAccept[29] = {};
|
||||||
WebSocketHandshake::generate(secWebSocketKey.data(), secWebSocketAccept);
|
WebSocketHandshake::generate(secWebSocketKey.data(), secWebSocketAccept);
|
||||||
@@ -79,37 +78,22 @@ public:
|
|||||||
->writeHeader("Sec-WebSocket-Accept", secWebSocketAccept)
|
->writeHeader("Sec-WebSocket-Accept", secWebSocketAccept)
|
||||||
->end();
|
->end();
|
||||||
|
|
||||||
//std::cout << "Adopting" << std::endl;
|
/* Adopting a socket invalidates it, do not rely on it directly to carry any data */
|
||||||
|
|
||||||
// adopting will immediately delete the socket! we cannot rely on reading anything on it
|
|
||||||
// rely on http context data
|
|
||||||
|
|
||||||
// todo: sizeof websocket
|
|
||||||
WebSocket<SSL, true> *webSocket = (WebSocket<SSL, true> *) StaticDispatch<SSL>::static_dispatch(us_ssl_socket_context_adopt_socket, us_socket_context_adopt_socket)(
|
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);
|
(typename StaticDispatch<SSL>::SOCKET_CONTEXT_TYPE *) webSocketContext, (typename StaticDispatch<SSL>::SOCKET_TYPE *) res, /*sizeof(WebSocketData)*/ 150);
|
||||||
|
|
||||||
webSocket->init();
|
|
||||||
|
|
||||||
//std::cout << "adopted" << std::endl;
|
|
||||||
|
|
||||||
httpContext->upgradeToWebSocket(
|
httpContext->upgradeToWebSocket(
|
||||||
webSocket
|
webSocket->init()
|
||||||
);
|
);
|
||||||
|
|
||||||
// we should hand the new socket to the handler
|
if (behavior.open) {
|
||||||
behavior.open(webSocket, req);
|
behavior.open(webSocket, req);
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
/* For now we do not support having HTTP and websocket routes on the same URL */
|
||||||
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
|
|
||||||
res->close();
|
res->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
|
|||||||
@@ -153,6 +153,9 @@ public:
|
|||||||
|
|
||||||
httpContextData->upgradedWebSocket = newSocket;
|
httpContextData->upgradedWebSocket = newSocket;
|
||||||
|
|
||||||
|
// I jhave no idea what this does
|
||||||
|
return true;
|
||||||
|
|
||||||
|
|
||||||
/*SOCKET_CONTEXT_TYPE *getSocketContext() {
|
/*SOCKET_CONTEXT_TYPE *getSocketContext() {
|
||||||
return (SOCKET_CONTEXT_TYPE *) this;
|
return (SOCKET_CONTEXT_TYPE *) this;
|
||||||
|
|||||||
+18
-1
@@ -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
|
#ifndef WEBSOCKET_H
|
||||||
#define WEBSOCKET_H
|
#define WEBSOCKET_H
|
||||||
|
|
||||||
@@ -15,8 +31,9 @@ struct WebSocket : AsyncSocket<SSL> {
|
|||||||
private:
|
private:
|
||||||
typedef AsyncSocket<SSL> Super;
|
typedef AsyncSocket<SSL> Super;
|
||||||
|
|
||||||
void init() {
|
void *init() {
|
||||||
new (us_socket_ext((us_socket *) this)) WebSocketData;
|
new (us_socket_ext((us_socket *) this)) WebSocketData;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
#ifndef WEBSOCKETCONTEXT_H
|
||||||
#define WEBSOCKETCONTEXT_H
|
#define WEBSOCKETCONTEXT_H
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
#ifndef WEBSOCKETCONTEXTDATA_H
|
||||||
#define WEBSOCKETCONTEXTDATA_H
|
#define WEBSOCKETCONTEXTDATA_H
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
#ifndef WEBSOCKETDATA_H
|
||||||
#define WEBSOCKETDATA_H
|
#define WEBSOCKETDATA_H
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user