๐Ÿ A very simple static Gemini server, now with Titan support!
cpp gemini titan gemini-protocol titan-protocol
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: Optimise request handling and harden Titan parameter parsing

Fuwn eeea40d4 2a688817

+26 -19
+2 -2
maple/gemini.cc
··· 28 28 #include "gemini.hh" 29 29 30 30 namespace maple::gemini { 31 - auto handle_client(std::vector<std::string> gemini_files, std::string path, 32 - std::stringstream &response) -> void { 31 + auto handle_client(const std::vector<std::string> &gemini_files, 32 + std::string path, std::stringstream &response) -> void { 33 33 // Check if the route is a file being served 34 34 if (std::find(gemini_files.begin(), gemini_files.end(), 35 35 ".maple/gmi" + path) != gemini_files.end()) {
+2 -2
maple/gemini.hh
··· 26 26 27 27 namespace maple { 28 28 namespace gemini { 29 - auto handle_client(std::vector<std::string>, std::string, std::stringstream &) 30 - -> void; 29 + auto handle_client(const std::vector<std::string> &, std::string, 30 + std::stringstream &) -> void; 31 31 } 32 32 } // namespace maple 33 33
+6 -7
maple/maple.cc
··· 119 119 std::array<char, GEMINI_MAXIMUM_REQUEST_SIZE> request{}; 120 120 121 121 SSL_read_ex(ssl, request.begin(), request.size(), &bytes_read); 122 - 123 - std::string path(request.data()); 122 + std::string path(request.data(), bytes_read); 124 123 125 124 if (path.starts_with("gemini://")) { 126 125 request_scheme = 1; ··· 131 130 } 132 131 133 132 if (request_scheme != 0) { 134 - path = path.substr(0, bytes_read); 135 - 136 133 // Remove "\r\n" if Gemini 137 134 if (request_scheme == 1) { 138 - path = path.substr(0, path.size() - 2); 135 + path.resize(path.size() - 2); 139 136 } 140 137 141 138 if (request_scheme == 1) { ··· 181 178 } 182 179 } 183 180 184 - SSL_write(ssl, response.str().c_str(), 185 - static_cast<int>(response.str().size())); 181 + const std::string response_string = response.str(); 182 + 183 + SSL_write(ssl, response_string.c_str(), 184 + static_cast<int>(response_string.size())); 186 185 } else { 187 186 std::cout << "received a request with an unsupported url scheme\n"; 188 187 }
+16 -8
maple/titan.cc
··· 32 32 -> std::map<std::string, std::string> { 33 33 std::map<std::string, std::string> parameters_map; 34 34 35 - for (auto parameter : parameters) { 35 + for (const auto &parameter : parameters) { 36 36 // Find the key in `parameter` 37 37 const std::size_t parameter_delimiter_position = parameter.find('='); 38 - const std::string key = parameter.substr(0, parameter_delimiter_position); 39 38 40 - // Remove the key in `parameter` 41 - parameter.erase(0, parameter_delimiter_position + 1); 39 + if (parameter_delimiter_position == std::string::npos) { 40 + continue; 41 + } 42 + 43 + const std::string key = parameter.substr(0, parameter_delimiter_position); 44 + const std::string value = 45 + parameter.substr(parameter_delimiter_position + 1); 42 46 43 47 // Add the key and value to `parameters_map` 44 - parameters_map.at(key) = parameter; 48 + parameters_map.emplace(key, value); 45 49 } 46 50 47 51 return parameters_map; 48 52 } 49 53 50 54 auto handle_client(std::stringstream &response, std::string path, 51 - const std::string &titan_token, 52 - std::size_t titan_max_size) -> void { 55 + const std::string &titan_token, std::size_t titan_max_size) 56 + -> void { 53 57 std::vector<std::string> parameters; 54 58 // Find path in `path` 55 59 std::size_t delimiter_position = path.find(';'); ··· 144 148 update_path = "/index.gmi"; 145 149 } 146 150 147 - if (parameters_map.at("token") == titan_token) { 151 + const auto token_iterator = parameters_map.find("token"); 152 + const std::string token = 153 + token_iterator == parameters_map.end() ? "" : token_iterator->second; 154 + 155 + if (token == titan_token) { 148 156 std::ofstream file(".maple/gmi" + update_path); 149 157 150 158 file << body;