diff options
| author | Fuwn <[email protected]> | 2022-04-14 02:43:53 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-04-14 02:43:53 +0000 |
| commit | 28a92aecf150debc6c7e50b07fc5e08ffedc1be2 (patch) | |
| tree | d6831775b7b490286aacf078a6b8864a3c0d396a | |
| parent | refactor: don't redefine file extension (diff) | |
| download | maple-28a92aecf150debc6c7e50b07fc5e08ffedc1be2.tar.xz maple-28a92aecf150debc6c7e50b07fc5e08ffedc1be2.zip | |
feat: validate request
| -rw-r--r-- | maple/maple.cc | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/maple/maple.cc b/maple/maple.cc index b500b22..51532f2 100644 --- a/maple/maple.cc +++ b/maple/maple.cc @@ -38,6 +38,7 @@ auto main() -> int { sockaddr_in socket_address {}; std::vector<std::string> gemini_files; const std::string GEMINI_FILE_EXTENSION = "gmi"; + const std::string GEMINI_PROTOCOL = "gemini://"; // Try a graceful shutdown when a SIGINT is detected signal(SIGINT, [](int signal_) -> void { @@ -135,12 +136,14 @@ auto main() -> int { } else { std::stringstream response; size_t index_of_junk; + std::string protocol; SSL_read(ssl, request, sizeof(request)); std::string path(request); path = path.substr(0, path.size() - 2); // Remove "\r\n" + protocol = path.substr(0, 9); path.erase(0, 9); // Remove "gemini://" // Try to remove the host, if you cannot; it must be a trailing slash-less @@ -181,7 +184,21 @@ auto main() -> int { response << "20 text/gemini\r\n" << buffer.str(); } else { - response << "51 The server (Maple) could not find the specified file.\r\n"; + if (std::equal( + protocol.begin(), + protocol.end(), + GEMINI_PROTOCOL.begin(), + GEMINI_PROTOCOL.end(), + [](char a, char b) -> bool { + return std::tolower(a) == std::tolower(b); + } + )) { + response << "51 The server (Maple) could not find the specified file" + ".\r\n"; + } else { + response << "59 The server (Maple) received a bad request: Invalid " + "protocol\r\n"; + } } } |