From a2ddfe6913c1884bfef6314d00cf2b708281ff79 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 28 Aug 2023 08:38:30 -0700 Subject: Bring uSockets & uWebSockets forks into Bun's repository (#4372) * Move uWebSockets and uSockets forks into Bun's repository * Update Makefile * Update settings.json * Update libuwsockets.cpp * Remove backends we won't be using * Update bindings.cpp --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- packages/bun-uws/examples/HttpServer.cpp | 90 ++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 packages/bun-uws/examples/HttpServer.cpp (limited to 'packages/bun-uws/examples/HttpServer.cpp') diff --git a/packages/bun-uws/examples/HttpServer.cpp b/packages/bun-uws/examples/HttpServer.cpp new file mode 100644 index 000000000..6c72bdff1 --- /dev/null +++ b/packages/bun-uws/examples/HttpServer.cpp @@ -0,0 +1,90 @@ +/* This is a simple HTTP(S) web server much like Python's SimpleHTTPServer */ + +#include + +/* Helpers for this example */ +#include "helpers/AsyncFileReader.h" +#include "helpers/AsyncFileStreamer.h" +#include "helpers/Middleware.h" + +/* optparse */ +#define OPTPARSE_IMPLEMENTATION +#include "helpers/optparse.h" + +int main(int argc, char **argv) { + + int option; + struct optparse options; + optparse_init(&options, argv); + + struct optparse_long longopts[] = { + {"port", 'p', OPTPARSE_REQUIRED}, + {"help", 'h', OPTPARSE_NONE}, + {"passphrase", 'a', OPTPARSE_REQUIRED}, + {"key", 'k', OPTPARSE_REQUIRED}, + {"cert", 'c', OPTPARSE_REQUIRED}, + {"dh_params", 'd', OPTPARSE_REQUIRED}, + {0} + }; + + int port = 3000; + struct us_socket_context_options_t ssl_options = {}; + + while ((option = optparse_long(&options, longopts, nullptr)) != -1) { + switch (option) { + case 'p': + port = atoi(options.optarg); + break; + case 'a': + ssl_options.passphrase = options.optarg; + break; + case 'c': + ssl_options.cert_file_name = options.optarg; + break; + case 'k': + ssl_options.key_file_name = options.optarg; + break; + case 'd': + ssl_options.dh_params_file_name = options.optarg; + break; + case 'h': + case '?': + fail: + std::cout << "Usage: " << argv[0] << " [--help] [--port ] [--key ] [--cert ] [--passphrase ] [--dh_params ] " << std::endl; + return 0; + } + } + + char *root = optparse_arg(&options); + if (!root) { + goto fail; + } + + AsyncFileStreamer asyncFileStreamer(root); + + /* Either serve over HTTP or HTTPS */ + struct us_socket_context_options_t empty_ssl_options = {}; + if (memcmp(&ssl_options, &empty_ssl_options, sizeof(empty_ssl_options))) { + /* HTTPS */ + uWS::SSLApp(ssl_options).get("/*", [&asyncFileStreamer](auto *res, auto *req) { + serveFile(res, req); + asyncFileStreamer.streamFile(res, req->getUrl()); + }).listen(port, [port, root](auto *token) { + if (token) { + std::cout << "Serving " << root << " over HTTPS a " << port << std::endl; + } + }).run(); + } else { + /* HTTP */ + uWS::App().get("/*", [&asyncFileStreamer](auto *res, auto *req) { + serveFile(res, req); + asyncFileStreamer.streamFile(res, req->getUrl()); + }).listen(port, [port, root](auto *token) { + if (token) { + std::cout << "Serving " << root << " over HTTP a " << port << std::endl; + } + }).run(); + } + + std::cout << "Failed to listen to port " << port << std::endl; +} -- cgit v1.2.3