aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-04-14 02:43:53 +0000
committerFuwn <[email protected]>2022-04-14 02:43:53 +0000
commit28a92aecf150debc6c7e50b07fc5e08ffedc1be2 (patch)
treed6831775b7b490286aacf078a6b8864a3c0d396a
parentrefactor: don't redefine file extension (diff)
downloadmaple-28a92aecf150debc6c7e50b07fc5e08ffedc1be2.tar.xz
maple-28a92aecf150debc6c7e50b07fc5e08ffedc1be2.zip
feat: validate request
-rw-r--r--maple/maple.cc19
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";
+ }
}
}