aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-12-06 08:10:31 +0000
committerFuwn <[email protected]>2025-12-06 08:10:31 +0000
commiteeea40d4a1cc16283ec41d0a20e1f430fa0389c7 (patch)
tree81e132759dab9490e0141cd3f24cff8aba79207b
parentchore: Support macOS (diff)
downloadmaple-eeea40d4a1cc16283ec41d0a20e1f430fa0389c7.tar.xz
maple-eeea40d4a1cc16283ec41d0a20e1f430fa0389c7.zip
refactor: Optimise request handling and harden Titan parameter parsing
-rw-r--r--maple/gemini.cc4
-rw-r--r--maple/gemini.hh4
-rw-r--r--maple/maple.cc13
-rw-r--r--maple/titan.cc24
4 files changed, 26 insertions, 19 deletions
diff --git a/maple/gemini.cc b/maple/gemini.cc
index 7dbe715..010e844 100644
--- a/maple/gemini.cc
+++ b/maple/gemini.cc
@@ -28,8 +28,8 @@
#include "gemini.hh"
namespace maple::gemini {
-auto handle_client(std::vector<std::string> gemini_files, std::string path,
- std::stringstream &response) -> void {
+auto handle_client(const std::vector<std::string> &gemini_files,
+ std::string path, std::stringstream &response) -> void {
// Check if the route is a file being served
if (std::find(gemini_files.begin(), gemini_files.end(),
".maple/gmi" + path) != gemini_files.end()) {
diff --git a/maple/gemini.hh b/maple/gemini.hh
index e270504..8a7796c 100644
--- a/maple/gemini.hh
+++ b/maple/gemini.hh
@@ -26,8 +26,8 @@
namespace maple {
namespace gemini {
-auto handle_client(std::vector<std::string>, std::string, std::stringstream &)
- -> void;
+auto handle_client(const std::vector<std::string> &, std::string,
+ std::stringstream &) -> void;
}
} // namespace maple
diff --git a/maple/maple.cc b/maple/maple.cc
index 1a1ba5c..71ad82f 100644
--- a/maple/maple.cc
+++ b/maple/maple.cc
@@ -119,8 +119,7 @@ auto main() -> int {
std::array<char, GEMINI_MAXIMUM_REQUEST_SIZE> request{};
SSL_read_ex(ssl, request.begin(), request.size(), &bytes_read);
-
- std::string path(request.data());
+ std::string path(request.data(), bytes_read);
if (path.starts_with("gemini://")) {
request_scheme = 1;
@@ -131,11 +130,9 @@ auto main() -> int {
}
if (request_scheme != 0) {
- path = path.substr(0, bytes_read);
-
// Remove "\r\n" if Gemini
if (request_scheme == 1) {
- path = path.substr(0, path.size() - 2);
+ path.resize(path.size() - 2);
}
if (request_scheme == 1) {
@@ -181,8 +178,10 @@ auto main() -> int {
}
}
- SSL_write(ssl, response.str().c_str(),
- static_cast<int>(response.str().size()));
+ const std::string response_string = response.str();
+
+ SSL_write(ssl, response_string.c_str(),
+ static_cast<int>(response_string.size()));
} else {
std::cout << "received a request with an unsupported url scheme\n";
}
diff --git a/maple/titan.cc b/maple/titan.cc
index 13118f3..2021903 100644
--- a/maple/titan.cc
+++ b/maple/titan.cc
@@ -32,24 +32,28 @@ auto parameters_to_map(const std::vector<std::string> &parameters)
-> std::map<std::string, std::string> {
std::map<std::string, std::string> parameters_map;
- for (auto parameter : parameters) {
+ for (const auto &parameter : parameters) {
// Find the key in `parameter`
const std::size_t parameter_delimiter_position = parameter.find('=');
- const std::string key = parameter.substr(0, parameter_delimiter_position);
- // Remove the key in `parameter`
- parameter.erase(0, parameter_delimiter_position + 1);
+ if (parameter_delimiter_position == std::string::npos) {
+ continue;
+ }
+
+ const std::string key = parameter.substr(0, parameter_delimiter_position);
+ const std::string value =
+ parameter.substr(parameter_delimiter_position + 1);
// Add the key and value to `parameters_map`
- parameters_map.at(key) = parameter;
+ parameters_map.emplace(key, value);
}
return parameters_map;
}
auto handle_client(std::stringstream &response, std::string path,
- const std::string &titan_token,
- std::size_t titan_max_size) -> void {
+ const std::string &titan_token, std::size_t titan_max_size)
+ -> void {
std::vector<std::string> parameters;
// Find path in `path`
std::size_t delimiter_position = path.find(';');
@@ -144,7 +148,11 @@ auto handle_client(std::stringstream &response, std::string path,
update_path = "/index.gmi";
}
- if (parameters_map.at("token") == titan_token) {
+ const auto token_iterator = parameters_map.find("token");
+ const std::string token =
+ token_iterator == parameters_map.end() ? "" : token_iterator->second;
+
+ if (token == titan_token) {
std::ofstream file(".maple/gmi" + update_path);
file << body;