diff options
| author | Fuwn <[email protected]> | 2024-06-11 07:06:57 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-06-11 07:06:57 +0000 |
| commit | 7a2a8f07c4f0b3c880540ac4991ba1ce8c0f7958 (patch) | |
| tree | bd9efba547e0f757e09ad648216e6ec4564ed406 | |
| parent | build: switch from ninja to tup (diff) | |
| download | maple-7a2a8f07c4f0b3c880540ac4991ba1ce8c0f7958.tar.xz maple-7a2a8f07c4f0b3c880540ac4991ba1ce8c0f7958.zip | |
refactor: tighen clang-tidy
| -rw-r--r-- | Tupfile | 6 | ||||
| -rw-r--r-- | maple/gemini.cc | 12 | ||||
| -rw-r--r-- | maple/maple.cc | 78 | ||||
| -rw-r--r-- | maple/maple.hh | 2 | ||||
| -rw-r--r-- | maple/titan.cc | 23 | ||||
| -rw-r--r-- | maple/titan.hh | 2 |
6 files changed, 72 insertions, 51 deletions
@@ -8,15 +8,15 @@ BUILD_DIRECTORY = build # Compiler Configuration CXX = clang++ CXX_EXTENSION = cc -CXX_FLAGS = -std=c++23 -I $(INCLUDE_DIRECTORY) -Weverything -Wno-padded -Wno-c++98-compat -MMD -fno-diagnostics-show-note-include-stack -Wno-c++98-compat-pedantic +CXX_FLAGS = -std=c++23 -I $(INCLUDE_DIRECTORY) -Weverything -Wno-padded -Wno-c++98-compat -MMD -Wno-c++98-compat-pedantic LD_FLAGS = -lssl -lcrypto # Clang-Tidy Configuration -CLANG_TIDY_CHECKS = '-*,bugprone-*,clang-analyzer-*,concurrency-*,cppcoreguildelines-*,llvm-*,misc-*,modernize-*,performance-*,portability-*,readability-*,-readability-magic-numbers,-llvm-header-guard,-bugprone-suspicious-include,-readability-function-cognitive-complexity,-bugprone-exception-escape' +CLANG_TIDY_CHECKS = '-*,bugprone-*,clang-analyzer-*,concurrency-*,cppcoreguildelines-*,llvm-*,misc-*,modernize-*,performance-*,portability-*,readability-*,-readability-function-cognitive-complexity,-bugprone-easily-swappable-parameters,-concurrency-mt-unsafe' CLANG_TIDY_FLAGS = -checks=$(CLANG_TIDY_CHECKS) -warnings-as-errors=* -quiet # : foreach $(SOURCE_DIRECTORY)/*.$(CXX_EXTENSION) $(INCLUDE_DIRECTORY)/*.hh |> clang-format -i -style=LLVM %f |> -# : foreach $(SOURCE_DIRECTORY)/*.$(CXX_EXTENSION) |> clang-tidy $(CLANG_TIDY_FLAGS) %f -- $(CXX_FLAGS) |> +: foreach $(SOURCE_DIRECTORY)/*.$(CXX_EXTENSION) |> clang-tidy $(CLANG_TIDY_FLAGS) %f -- $(CXX_FLAGS) |> : foreach $(SOURCE_DIRECTORY)/*.$(CXX_EXTENSION) |> ^j^ $(CXX) $(CXX_FLAGS) -MF $(BUILD_DIRECTORY)/%B.d -c %f -o %o |> $(BUILD_DIRECTORY)/%B.o | $(BUILD_DIRECTORY)/%B.d : $(BUILD_DIRECTORY)/*.o |> $(CXX) $(LD_FLAGS) %f -o %o |> $(BUILD_DIRECTORY)/$(NAME) diff --git a/maple/gemini.cc b/maple/gemini.cc index 83ad3ee..7dbe715 100644 --- a/maple/gemini.cc +++ b/maple/gemini.cc @@ -18,13 +18,16 @@ * SPDX-License-Identifier: GPL-3.0-only */ +#include <algorithm> #include <fstream> #include <iostream> +#include <sstream> +#include <string> +#include <vector> #include "gemini.hh" -namespace maple { -namespace gemini { +namespace maple::gemini { auto handle_client(std::vector<std::string> gemini_files, std::string path, std::stringstream &response) -> void { // Check if the route is a file being served @@ -43,7 +46,6 @@ auto handle_client(std::vector<std::string> gemini_files, std::string path, } } - std::cout << "requested " << path << std::endl; + std::cout << "requested " << path << '\n'; } -} // namespace gemini -} // namespace maple +} // namespace maple::gemini diff --git a/maple/maple.cc b/maple/maple.cc index 45e5ac1..85ec862 100644 --- a/maple/maple.cc +++ b/maple/maple.cc @@ -18,12 +18,21 @@ * SPDX-License-Identifier: GPL-3.0-only */ -#include <arpa/inet.h> +#include <algorithm> +#include <array> +#include <asm-generic/socket.h> +#include <cctype> #include <csignal> +#include <cstddef> +#include <cstdio> +#include <cstdlib> #include <filesystem> #include <iostream> -#include <map> +#include <netinet/in.h> #include <openssl/err.h> +#include <openssl/ssl.h> +#include <sstream> +#include <string> #include <sys/socket.h> #include <unistd.h> #include <vector> @@ -36,7 +45,7 @@ auto main() -> int { std::vector<std::string> gemini_files; bool titan = false; std::string titan_token; - size_t titan_max_size = 0; + std::size_t titan_max_size = 0; const std::string GEMINI_FILE_EXTENSION = "gmi"; // Check if the user is want to support Titan and set it up @@ -46,7 +55,7 @@ auto main() -> int { // Try a graceful shutdown when a SIGINT is detected signal(SIGINT, [](int _signal) -> void { - std::cout << "shutdown(" << _signal << ")" << std::endl; + std::cout << "shutdown(" << _signal << ")\n"; close(maple::maple_socket); SSL_CTX_free(maple::ssl_context); @@ -61,8 +70,9 @@ auto main() -> int { // 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(), - [](char a, char b) -> bool { - return std::tolower(a) == std::tolower(b); + [](char character_a, char character_b) -> bool { + return std::tolower(character_a) == + std::tolower(character_b); })) { gemini_files.push_back(entry.path()); } @@ -70,7 +80,7 @@ auto main() -> int { // Inform user of which files will be served for (const std::string &file : gemini_files) { - std::cout << "serving " << file << std::endl; + std::cout << "serving " << file << '\n'; } // Setup SSL @@ -83,9 +93,9 @@ auto main() -> int { sockaddr_in socket_address_{}; unsigned int socket_address_length = sizeof(socket_address_); SSL *ssl; - int client = accept(maple::maple_socket, - reinterpret_cast<sockaddr *>(&socket_address_), - &socket_address_length); + const int client = accept(maple::maple_socket, + reinterpret_cast<sockaddr *>(&socket_address_), + &socket_address_length); if (client < 0) { return maple::prepare_exit_with("unable to accept", false); @@ -99,14 +109,15 @@ auto main() -> int { ERR_print_errors_fp(stderr); } else { std::stringstream response; - size_t index_of_junk; + std::size_t index_of_junk; int request_scheme; // Gemini = 1, Titan = 2, Error = 0 - size_t bytes_read; - char request[1024]{}; + std::size_t bytes_read; + constexpr std::size_t GEMINI_MAXIMUM_REQUEST_SIZE = 1024; + std::array<char, GEMINI_MAXIMUM_REQUEST_SIZE> request{}; - SSL_read_ex(ssl, request, sizeof(request), &bytes_read); + SSL_read_ex(ssl, request.begin(), request.size(), &bytes_read); - std::string path(request); + std::string path(request.data()); if (path.starts_with("gemini://")) { request_scheme = 1; @@ -125,14 +136,18 @@ auto main() -> int { } if (request_scheme == 1) { - path.erase(0, 9); // Remove "gemini://" + constexpr std::size_t GEMINI_PROTOCOL_LENGTH = 9; + + path.erase(0, GEMINI_PROTOCOL_LENGTH); // Remove "gemini://" } else { - path.erase(0, 8); // Remove "titan://" + constexpr std::size_t TITAN_PROTOCOL_LENGTH = 8; + + path.erase(0, TITAN_PROTOCOL_LENGTH); // Remove "titan://" } // Try to remove the host, if you cannot; it must be a trailing // slash-less hostname, so we will respond with the index. - size_t found_first = path.find_first_of('/'); + const std::size_t found_first = path.find_first_of('/'); if (found_first != std::string::npos) { path = path.substr(found_first, @@ -166,8 +181,7 @@ auto main() -> int { SSL_write(ssl, response.str().c_str(), static_cast<int>(response.str().size())); } else { - std::cout << "received a request with an unsupported url scheme" - << std::endl; + std::cout << "received a request with an unsupported url scheme\n"; } } @@ -189,7 +203,7 @@ auto prepare_exit_with(const char *message, bool ssl) -> int { } auto setup_environment(bool &titan, std::string &titan_token, - size_t &titan_max_size) -> int { + std::size_t &titan_max_size) -> int { char *titan_environment = std::getenv("TITAN"); if (titan_environment == nullptr) { @@ -197,10 +211,10 @@ auto setup_environment(bool &titan, std::string &titan_token, } else { std::string valid_titan_environment(titan_environment); - std::transform(valid_titan_environment.begin(), - valid_titan_environment.end(), - valid_titan_environment.begin(), - [](unsigned char c) -> int { return std::tolower(c); }); + std::transform( + valid_titan_environment.begin(), valid_titan_environment.end(), + valid_titan_environment.begin(), + [](unsigned char character) -> int { return std::tolower(character); }); if (valid_titan_environment == "true" || valid_titan_environment == "1") { char *unvalidated_titan_token = std::getenv("TITAN_TOKEN"); @@ -213,13 +227,15 @@ auto setup_environment(bool &titan, std::string &titan_token, } if (unvalidated_titan_max_size == nullptr) { - titan_max_size = 1024; + constexpr std::size_t TITAN_MAX_SIZE = 1024; - std::cout << "no TITAN_MAX_SIZE set, defaulting to 1024" << std::endl; + titan_max_size = TITAN_MAX_SIZE; + + std::cout << "no TITAN_MAX_SIZE set, defaulting to 1024\n"; } else { try { titan_max_size = - static_cast<size_t>(std::stoi(unvalidated_titan_max_size)); + static_cast<std::size_t>(std::stoi(unvalidated_titan_max_size)); } catch (...) { return maple::prepare_exit_with( "TITAN_MAX_SIZE could not be interpreted as an integer", false); @@ -242,7 +258,7 @@ auto setup_ssl() -> int { maple::ssl_context = SSL_CTX_new(TLS_server_method()); - if (!maple::ssl_context) { + if (maple::ssl_context == nullptr) { return maple::prepare_exit_with("unable to create ssl context", true); } @@ -256,8 +272,10 @@ auto setup_ssl() -> int { return maple::prepare_exit_with("unable to use private key file", true); } + constexpr std::size_t GEMINI_PORT = 1965; + socket_address.sin_family = AF_INET; - socket_address.sin_port = htons(1965); + socket_address.sin_port = htons(GEMINI_PORT); socket_address.sin_addr.s_addr = htonl(INADDR_ANY); maple::maple_socket = socket(AF_INET, SOCK_STREAM, 0); diff --git a/maple/maple.hh b/maple/maple.hh index 5f72357..0ae6a2d 100644 --- a/maple/maple.hh +++ b/maple/maple.hh @@ -29,7 +29,7 @@ static int maple_socket; static SSL_CTX *ssl_context; auto prepare_exit_with(const char *, bool) -> int; -auto setup_environment(bool &, std::string &, size_t &) -> int; +auto setup_environment(bool &, std::string &, std::size_t &) -> int; auto setup_ssl() -> int; } // namespace maple diff --git a/maple/titan.cc b/maple/titan.cc index 03ea19e..13118f3 100644 --- a/maple/titan.cc +++ b/maple/titan.cc @@ -18,22 +18,24 @@ * SPDX-License-Identifier: GPL-3.0-only */ +#include <cstddef> #include <fstream> #include <map> +#include <sstream> +#include <string> #include <vector> #include "titan.hh" -namespace maple { -namespace titan { +namespace maple::titan { auto parameters_to_map(const std::vector<std::string> ¶meters) -> std::map<std::string, std::string> { std::map<std::string, std::string> parameters_map; for (auto parameter : parameters) { // Find the key in `parameter` - size_t parameter_delimiter_position = parameter.find('='); - std::string key = parameter.substr(0, parameter_delimiter_position); + 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); @@ -47,12 +49,12 @@ auto parameters_to_map(const std::vector<std::string> ¶meters) auto handle_client(std::stringstream &response, std::string path, const std::string &titan_token, - size_t titan_max_size) -> void { + std::size_t titan_max_size) -> void { std::vector<std::string> parameters; // Find path in `path` - size_t delimiter_position = path.find(';'); + std::size_t delimiter_position = path.find(';'); std::string update_path = path.substr(0, delimiter_position); - std::string body = path.substr(path.find('\n') + 1, path.length() - 1); + const std::string body = path.substr(path.find('\n') + 1, path.length() - 1); path.erase(path.find('\n') - 1, path.length() - 1); // parameters.push_back(update_path); @@ -121,8 +123,8 @@ auto handle_client(std::stringstream &response, std::string path, } try { - size_t body_size = - static_cast<size_t>(std::stoi(parameters_map.at("size"))); + const std::size_t body_size = + static_cast<std::size_t>(std::stoi(parameters_map.at("size"))); if (body_size > titan_max_size) { response << "20 text/gemini\r\nThe server (Maple) received a body " @@ -157,5 +159,4 @@ auto handle_client(std::stringstream &response, std::string path, break; } } -} // namespace titan -} // namespace maple +} // namespace maple::titan diff --git a/maple/titan.hh b/maple/titan.hh index 06a94da..e8cd8b1 100644 --- a/maple/titan.hh +++ b/maple/titan.hh @@ -32,7 +32,7 @@ auto parameters_to_map(const std::vector<std::string> &) -> std::map<std::string, std::string>; auto handle_client(std::stringstream &, std::string, const std::string &, - size_t) -> void; + std::size_t) -> void; } // namespace titan } // namespace maple |