From 75c9a467cb8fdc3007fc7842137fc5b17e741141 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 10 Apr 2022 08:50:36 +0000 Subject: refactor: more explicit auto --- bin/genkey | 0 maple/maple.cc | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 bin/genkey diff --git a/bin/genkey b/bin/genkey old mode 100644 new mode 100755 diff --git a/maple/maple.cc b/maple/maple.cc index 0bae0e9..4480e09 100644 --- a/maple/maple.cc +++ b/maple/maple.cc @@ -61,7 +61,7 @@ auto main() -> int { file_extension.end(), gemini_file_extension.begin(), gemini_file_extension.end(), - [](auto a, auto b) -> bool { + [](char a, char b) -> bool { return std::tolower(a) == std::tolower(b); } )) { -- cgit v1.2.3 From 601721e93082ee7add0fa07937b964da78c39c60 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 10 Apr 2022 09:04:47 +0000 Subject: format: single line formats --- maple/maple.cc | 46 +++++++++++----------------------------------- 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/maple/maple.cc b/maple/maple.cc index 4480e09..a89ce7b 100644 --- a/maple/maple.cc +++ b/maple/maple.cc @@ -61,12 +61,8 @@ auto main() -> int { file_extension.end(), gemini_file_extension.begin(), gemini_file_extension.end(), - [](char a, char b) -> bool { - return std::tolower(a) == std::tolower(b); - } - )) { - gemini_files.push_back(entry.path()); - } + [](char a, char b) -> bool { return std::tolower(a) == std::tolower(b); } + )) { gemini_files.push_back(entry.path()); } } // Inform user of which files will be served @@ -79,33 +75,25 @@ auto main() -> int { SSL_load_error_strings(); ssl_context = SSL_CTX_new(TLS_server_method()); - if (!ssl_context) { - exit_with("unable to create ssl context", true); - } + if (!ssl_context) { exit_with("unable to create ssl context", true); } if (SSL_CTX_use_certificate_file( ssl_context, ".maple/public.pem", SSL_FILETYPE_PEM - ) <= 0) { - exit_with("unable to use certificate file", true); - } + ) <= 0) { exit_with("unable to use certificate file", true); } if (SSL_CTX_use_PrivateKey_file( ssl_context, ".maple/private.pem", SSL_FILETYPE_PEM - ) <= 0) { - exit_with("unable to use private key file", true); - } + ) <= 0) { exit_with("unable to use private key file", true); } socket_address.sin_family = AF_INET; socket_address.sin_port = htons(1965); socket_address.sin_addr.s_addr = htonl(INADDR_ANY); maple_socket = socket(AF_INET, SOCK_STREAM, 0); - if (maple_socket < 0) { - exit_with("unable to create socket", false); - } + if (maple_socket < 0) { exit_with("unable to create socket", false); } // Reuse address. Allows the use of the address instantly after a SIGINT // without having to wait for the socket to die. @@ -116,20 +104,14 @@ auto main() -> int { SO_REUSEADDR, &reuse_addr, sizeof(int) - ) < 0) { - exit_with("unable to set socket options (SO_LINGER)", false); - } + ) < 0) { exit_with("unable to set socket options (SO_LINGER)", false); } if (bind( maple_socket, reinterpret_cast(&socket_address), sizeof(socket_address) - ) < 0) { - exit_with("unable to bind", false); - } - if (listen(maple_socket, 1) < 0) { - exit_with("unable to listen", false); - } + ) < 0) { exit_with("unable to bind", false); } + if (listen(maple_socket, 1) < 0) { exit_with("unable to listen", false); } // Listen and serve connections for (;;) { @@ -165,10 +147,7 @@ auto main() -> int { // hostname, so we will respond with the index. size_t found_first = path.find_first_of('/'); if (found_first != std::string::npos) { - path = path.substr( - found_first, - path.size() - 1 - ); // Remove host + path = path.substr(found_first, path.size() - 1); // Remove host } else { path = "/index.gmi"; } @@ -176,10 +155,7 @@ auto main() -> int { // Remove junk, if any index_of_junk = path.find_first_of('\n'); if (index_of_junk != std::string::npos) { - path.erase( - path.find_first_of('\n') - 1, - path.size() - 1 - ); + path.erase(path.find_first_of('\n') - 1, path.size() - 1); } // Check if the route is a file being served -- cgit v1.2.3 From 6617abdc325bcf49f5504dbf1988069cb04cf176 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 10 Apr 2022 09:06:38 +0000 Subject: docs(readme): update loc --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3dd70a8..3275b98 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Maple A very simple static Gemini server; written within a single file and liberally -spanning 159 lines-of-code. +spanning 135 lines-of-code. ## Usage -- cgit v1.2.3 From af9d32fafd8209ce26a33cf206b44cb80cdc09c2 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 14 Apr 2022 01:54:15 +0000 Subject: refactor: don't redefine file extension --- maple/maple.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maple/maple.cc b/maple/maple.cc index a89ce7b..b500b22 100644 --- a/maple/maple.cc +++ b/maple/maple.cc @@ -37,6 +37,7 @@ auto exit_with[[noreturn]](const char *, bool) -> void; auto main() -> int { sockaddr_in socket_address {}; std::vector gemini_files; + const std::string GEMINI_FILE_EXTENSION = "gmi"; // Try a graceful shutdown when a SIGINT is detected signal(SIGINT, [](int signal_) -> void { @@ -53,14 +54,13 @@ auto main() -> int { std::string file_extension = entry.path().string().substr( entry.path().string().find_last_of('.') + 1 ); - std::string gemini_file_extension = "gmi"; // Only keep track of file if it is a Gemini file if (std::equal( file_extension.begin(), file_extension.end(), - gemini_file_extension.begin(), - gemini_file_extension.end(), + GEMINI_FILE_EXTENSION.begin(), + GEMINI_FILE_EXTENSION.end(), [](char a, char b) -> bool { return std::tolower(a) == std::tolower(b); } )) { gemini_files.push_back(entry.path()); } } -- cgit v1.2.3 From 28a92aecf150debc6c7e50b07fc5e08ffedc1be2 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 14 Apr 2022 02:43:53 +0000 Subject: feat: validate request --- maple/maple.cc | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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 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"; + } } } -- cgit v1.2.3 From 2ee57aab272ccd337b2089360df4bef88eb223cf Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 14 Apr 2022 02:47:59 +0000 Subject: build: compile with c++23 working draft --- build.ninja | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.ninja b/build.ninja index b510ec3..0ac4dfa 100644 --- a/build.ninja +++ b/build.ninja @@ -1,5 +1,5 @@ cc = clang++ -cxxflags = -Weverything -Wno-c++98-compat -std=c++20 +cxxflags = -Weverything -Wno-c++98-compat -std=c++2b ldflags = -lssl -lcrypto out_dir = out name = maple -- cgit v1.2.3 From 63a257c2e23876d19e0d9b78c7b92700ba366704 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 14 Apr 2022 03:01:43 +0000 Subject: feat: add hacking steps Also update lines-of-code count. Also, also, the last few commits were made on a six-hour flight... --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3275b98..1d09ba6 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Maple A very simple static Gemini server; written within a single file and liberally -spanning 135 lines-of-code. +spanning 152 lines-of-code. ## Usage @@ -37,6 +37,20 @@ field which should be modified is the `ports`, if you have to. 1. Build: `ninja` (requires [Ninja](https://ninja-build.org/)) 2. Run: `out/maple` +### Hacking + +If you decide to tamper with Maple, you should give your commits a test. As +there isn't a test suite yet, feel free to use any of these examples: + +```bash +# Using OpenSSL directly to debug different requests: +$ openssl s_client -ign_eof -quiet -connect localhost:1965 <<< \ +> "gemini://localhost:1965" + +# Using Bollux to debug proper requests... or any client! +$ bollux localhost +``` + ## Capsules using Maple [Add yours!](https://github.com/gemrest/maple/edit/main/README.md) -- cgit v1.2.3 From d476435d429377677aa0ddc96ab3931721a5415c Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 14 Apr 2022 03:03:48 +0000 Subject: format: move strings to same line --- maple/maple.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/maple/maple.cc b/maple/maple.cc index 51532f2..8a8d85a 100644 --- a/maple/maple.cc +++ b/maple/maple.cc @@ -193,11 +193,9 @@ auto main() -> int { return std::tolower(a) == std::tolower(b); } )) { - response << "51 The server (Maple) could not find the specified file" - ".\r\n"; + 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"; + response << "59 The server (Maple) received a bad request: Invalid protocol\r\n"; } } } -- cgit v1.2.3 From e48821775d7b8d5a3b769094472b5c71238d7b1c Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 14 Apr 2022 09:16:36 +0000 Subject: fix(build): c++20 instead of c++23 wd --- build.ninja | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.ninja b/build.ninja index 0ac4dfa..b510ec3 100644 --- a/build.ninja +++ b/build.ninja @@ -1,5 +1,5 @@ cc = clang++ -cxxflags = -Weverything -Wno-c++98-compat -std=c++2b +cxxflags = -Weverything -Wno-c++98-compat -std=c++20 ldflags = -lssl -lcrypto out_dir = out name = maple -- cgit v1.2.3