summaryrefslogtreecommitdiffstats
path: root/BrowserSource/Proxy/HTTPMapper.cpp
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2023-07-03 18:44:43 -0700
committeryum <yum.food.vr@gmail.com>2023-07-03 19:36:13 -0700
commit76ae7c28ea6224b2c919122d5dc71bcc00a0ecaa (patch)
tree9723fd02715d747cfc439d1f66d36821a56069e9 /BrowserSource/Proxy/HTTPMapper.cpp
parent7888ccc96d001512dd3bdc01f299856e86c876f5 (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/HTTPMapper.cpp')
-rw-r--r--BrowserSource/Proxy/HTTPMapper.cpp69
1 files changed, 69 insertions, 0 deletions
diff --git a/BrowserSource/Proxy/HTTPMapper.cpp b/BrowserSource/Proxy/HTTPMapper.cpp
new file mode 100644
index 0000000..af4f6d0
--- /dev/null
+++ b/BrowserSource/Proxy/HTTPMapper.cpp
@@ -0,0 +1,69 @@
+#include "HTTPMapper.h"
+
+#include <sstream>
+#include <map>
+
+namespace {
+ // Source: RFC 2616 section 6.1.1
+ const std::map<int, std::string> kStatusCodeToString{
+ {100, "Continue" },
+ {101, "Switching Protocols"},
+ {200, "OK"},
+ {201, "Created"},
+ {202, "Accepted"},
+ {203, "Non-Authoritative Information"},
+ {204, "No Content"},
+ {205, "Reset Content"},
+ {206, "Partial Content"},
+ {300, "Multiple Choices"},
+ {301, "Moved Permanently"},
+ {302, "Found"},
+ {303, "See Other"},
+ {304, "Not Modified"},
+ {305, "Use Proxy"},
+ {307, "Temporary Redirect"},
+ {400, "Bad Request"},
+ {401, "Unauthorized"},
+ {402, "Payment Required"},
+ {403, "Forbidden"},
+ {404, "Not Found"},
+ {405, "Method Not Allowed"},
+ {406, "Not Acceptable"},
+ };
+}
+
+namespace WebServer {
+ std::string HTTPMapper::Map(const int status_code,
+ const std::string& payload, const ContentType type) {
+ switch (type) {
+ case HTML:
+ return HTTPMapperHTML().Map(status_code, payload);
+ case JSON:
+ return HTTPMapperJSON().Map(status_code, payload);
+ }
+ }
+
+ std::string HTTPMapperHTML::Map(const int status_code,
+ const std::string& payload) {
+ std::ostringstream oss;
+ // This might throw and crash the app, but that's ok, just don't use an unsupported code.
+ oss << "HTTP/1.1 " << status_code << " " << kStatusCodeToString.at(status_code) << "\r\n";
+ oss << "Content-Type: text/html\r\n";
+ oss << "Content-Length: " << std::to_string(payload.size()) << "\r\n";
+ oss << "\r\n";
+ oss << payload;
+ return oss.str();
+ }
+
+ std::string HTTPMapperJSON::Map(const int status_code,
+ const std::string& payload) {
+ std::ostringstream oss;
+ // This might throw and crash the app, but that's ok, just don't use an unsupported code.
+ oss << "HTTP/1.1 " << status_code << " " << kStatusCodeToString.at(status_code) << "\r\n";
+ oss << "Content-Type: application/json\r\n";
+ oss << "Content-Length: " << std::to_string(payload.size()) << "\r\n";
+ oss << "\r\n";
+ oss << payload;
+ return oss.str();
+ }
+} \ No newline at end of file