diff options
| author | yum <yum.food.vr@gmail.com> | 2023-07-03 18:44:43 -0700 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2023-07-03 19:36:13 -0700 |
| commit | 76ae7c28ea6224b2c919122d5dc71bcc00a0ecaa (patch) | |
| tree | 9723fd02715d747cfc439d1f66d36821a56069e9 /BrowserSource/Proxy/WebServer.h | |
| parent | 7888ccc96d001512dd3bdc01f299856e86c876f5 (diff) | |
Begin work on proxy server
Create a simple server with 3 endpoints:
* /create_session: Create a session and return its identifier.
* /set_transcript: Update a session's transcript.
* /get_transcript: Fetch a session's transcript.
Right now the session ID provides authentication *and* authorization.
There is no public/private ID so you have to trust whoever you share
your ID with.
IDs are long and generated by the server, so it should be somewhat
secure against low-effort hacking.
Other updates:
* Drop whisper_requirements.txt - no longer needed.
* Vendor curl to make it easier to interact with the server.
TODO:
* Fuzz test the server.
Diffstat (limited to 'BrowserSource/Proxy/WebServer.h')
| -rw-r--r-- | BrowserSource/Proxy/WebServer.h | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/BrowserSource/Proxy/WebServer.h b/BrowserSource/Proxy/WebServer.h new file mode 100644 index 0000000..7815e89 --- /dev/null +++ b/BrowserSource/Proxy/WebServer.h @@ -0,0 +1,48 @@ +#pragma once + +#include <stdint.h> + +#include <functional> +#include <future> +#include <map> +#include <mutex> +#include <string> +#include <vector> + +#include "WebCommon.h" + +namespace WebServer { + class WebServer { + public: + WebServer(std::uint16_t port); + + typedef std::function<void( + int& status_code, + std::string& payload, + ContentType& type)> handler_t; + + bool RegisterPathHandler(const std::string& method, + const std::string& path, handler_t&& handler); + void RegisterDefaultHandler(handler_t&& handler); + + bool Run(volatile bool* run); + + private: + // Dispatch requests by mapping from (method, path) to handler. + // Dispatch key is (method, path) in that order. + typedef std::tuple<std::string, std::string> dispatch_key_t; + static inline dispatch_key_t GetDispatchKey(const std::string& method, const std::string& path) + { + return dispatch_key_t(method, path); + } + + typedef std::map<dispatch_key_t, handler_t> dispatch_map_t; + dispatch_map_t dispatch_map_; + handler_t default_handler_; + + const uint16_t port_; + + std::vector<std::future<void>> connections_; + }; +} + |
