๐Ÿ 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.

fix(maple.cc): fix all possible memory leaks

Fuwn f2ad8a4b 3ecf9bbd

+26 -18
+23 -15
maple/maple.cc
··· 40 40 const std::string GEMINI_FILE_EXTENSION = "gmi"; 41 41 42 42 // Check if the user is want to support Titan and set it up 43 - maple::setup_environment(titan, titan_token, titan_max_size); 43 + if (maple::setup_environment(titan, titan_token, titan_max_size) == 1) { 44 + return EXIT_FAILURE; 45 + } 44 46 45 47 // Try a graceful shutdown when a SIGINT is detected 46 48 signal(SIGINT, [](int _signal) -> void { ··· 76 78 } 77 79 78 80 // Setup SSL 79 - maple::setup_ssl(); 81 + if (maple::setup_ssl() == EXIT_FAILURE) { return EXIT_FAILURE; } 80 82 81 83 // Listen and serve connections 82 84 for (;;) { ··· 89 91 &socket_address_length 90 92 ); 91 93 92 - if (client < 0) { maple::exit_with("unable to accept", false); } 94 + if (client < 0) { 95 + return maple::prepare_exit_with("unable to accept", false); 96 + } 93 97 94 98 ssl = SSL_new(maple::ssl_context); 95 99 ··· 190 194 } 191 195 192 196 namespace maple { 193 - auto exit_with[[noreturn]](const char *message, bool ssl) -> void { 197 + auto prepare_exit_with(const char *message, bool ssl) -> int { 194 198 perror(message); 195 199 196 200 if (ssl) { ERR_print_errors_fp(stderr); } 197 201 198 - std::exit(EXIT_FAILURE); 202 + return EXIT_FAILURE; 199 203 } 200 204 201 205 auto setup_environment( 202 206 bool &titan, 203 207 std::string &titan_token, 204 208 size_t &titan_max_size 205 - ) -> void { 209 + ) -> int { 206 210 char *titan_environment = std::getenv("TITAN"); 207 211 208 212 if (titan_environment == nullptr) { ··· 237 241 std::stoi(unvalidated_titan_max_size) 238 242 ); 239 243 } catch (...) { 240 - maple::exit_with( 244 + return maple::prepare_exit_with( 241 245 "TITAN_MAX_SIZE could not be interpreted as an integer", 242 246 false 243 247 ); ··· 247 251 titan = true; 248 252 } 249 253 } 254 + 255 + return 0; 250 256 } 251 257 252 - auto setup_ssl() -> void { 258 + auto setup_ssl() -> int { 253 259 sockaddr_in socket_address {}; 254 260 255 261 // Setup OpenSSL ··· 259 265 maple::ssl_context = SSL_CTX_new(TLS_server_method()); 260 266 261 267 if (!maple::ssl_context) { 262 - maple::exit_with("unable to create ssl context", true); 268 + return maple::prepare_exit_with("unable to create ssl context", true); 263 269 } 264 270 265 271 if (SSL_CTX_use_certificate_file( ··· 267 273 ".maple/public.pem", 268 274 SSL_FILETYPE_PEM 269 275 ) <= 0) { 270 - maple::exit_with("unable to use certificate file", true); 276 + return maple::prepare_exit_with("unable to use certificate file", true); 271 277 } 272 278 if (SSL_CTX_use_PrivateKey_file( 273 279 maple::ssl_context, 274 280 ".maple/private.pem", 275 281 SSL_FILETYPE_PEM 276 282 ) <= 0) { 277 - maple::exit_with("unable to use private key file", true); 283 + return maple::prepare_exit_with("unable to use private key file", true); 278 284 } 279 285 280 286 socket_address.sin_family = AF_INET; ··· 284 290 maple::maple_socket = socket(AF_INET, SOCK_STREAM, 0); 285 291 286 292 if (maple::maple_socket < 0) { 287 - maple::exit_with("unable to create socket", false); 293 + return maple::prepare_exit_with("unable to create socket", false); 288 294 } 289 295 290 296 // Reuse address. Allows the use of the address instantly after a SIGINT ··· 297 303 &reuse_addr, 298 304 sizeof(int) 299 305 ) < 0) { 300 - maple::exit_with("unable to set socket options (SO_LINGER)", false); 306 + return maple::prepare_exit_with("unable to set socket options (SO_LINGER)", false); 301 307 } 302 308 303 309 if (bind( ··· 305 311 reinterpret_cast<sockaddr *>(&socket_address), 306 312 sizeof(socket_address) 307 313 ) < 0) { 308 - maple::exit_with("unable to bind", false); 314 + return maple::prepare_exit_with("unable to bind", false); 309 315 } 310 316 if (listen(maple::maple_socket, 1) < 0) { 311 - maple::exit_with("unable to listen", false); 317 + return maple::prepare_exit_with("unable to listen", false); 312 318 } 319 + 320 + return 0; 313 321 } 314 322 }
+3 -3
maple/maple.hh
··· 27 27 static int maple_socket; 28 28 static SSL_CTX *ssl_context; 29 29 30 - auto exit_with[[noreturn]](const char *, bool) -> void; 31 - auto setup_environment(bool &, std::string &, size_t &) -> void; 32 - auto setup_ssl() -> void; 30 + auto prepare_exit_with(const char *, bool) -> int; 31 + auto setup_environment(bool &, std::string &, size_t &) -> int; 32 + auto setup_ssl() -> int; 33 33 } 34 34 35 35 #endif // MAPLE_HH