Seamless SSL/non-SSL swapping, new Context-centric interface

This commit is contained in:
Alex Hultman
2018-06-20 22:26:40 +02:00
parent d3901aceae
commit 73aa9cae6b
5 changed files with 217 additions and 123 deletions
+12 -7
View File
@@ -1,23 +1,28 @@
// this is roughly the interfaces I plan for (with changes and additional helpers added with time)
// much speaks for a header-only or header-mostly implementation now that uSockets is properly isolating its internal headers
#include "uWS.h"
#include "Context.h"
int main() {
std::cout << "HttpSocket size: " << sizeof(HttpSocket::Data) << std::endl;
std::cout << "HttpSocket size: " << sizeof(HttpSocket<true>::Data) << std::endl;
char *buffer = new char[512];
// either use this, or use onHttpRoute but not both
uWS::defaultHub.onHttpRequest([buffer](HttpSocket *s, HttpRequest *req) {
// SSL options are given via uSockets structure
us_ssl_socket_context_options options = {};
options.key_file_name = "/home/alexhultman/uWebSockets/misc/ssl/key.pem";
options.cert_file_name = "/home/alexhultman/uWebSockets/misc/ssl/cert.pem";
options.passphrase = "1234";
uWS::SSLContext(options).onHttpRequest([buffer](auto *s, HttpRequest *req) {
if (req->getUrl() == "/") {
s->writeStatus(200)->writeHeader("Server", "µWebSockets v0.15")->end(buffer, 512);
s->writeStatus(200)->writeHeader("Hello", "World")->end(buffer, 512);
} else {
std::cout << "Got HTTP request at URL: " << req->getUrl() << std::endl;
}
}).listen("localhost", 3000, 0).run();
}).listen("localhost", 3000, 0);
// todo: important swapping to SSL should be .secureListen(same interfaces) and work out of the box!
uWS::defaultHub.run();
}