๐Ÿ 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: replace errors with function

Fuwn d557da39 0cb2fad5

+16 -21
+16 -21
maple/maple.cc
··· 32 32 static int maple_socket; 33 33 static SSL_CTX *ssl_context; 34 34 35 + auto exit_with[[noreturn]](const char *, bool) -> void; 36 + 35 37 auto main() -> int { 36 38 sockaddr_in socket_address {}; 37 39 std::vector<std::string> gemini_files; ··· 78 80 79 81 ssl_context = SSL_CTX_new(TLS_server_method()); 80 82 if (!ssl_context) { 81 - perror("unable to create ssl context"); 82 - ERR_print_errors_fp(stderr); 83 - std::exit(EXIT_FAILURE); 83 + exit_with("unable to create ssl context", true); 84 84 } 85 85 86 86 if (SSL_CTX_use_certificate_file( ··· 88 88 ".maple/public.pem", 89 89 SSL_FILETYPE_PEM 90 90 ) <= 0) { 91 - perror("unable to use certificate file"); 92 - ERR_print_errors_fp(stderr); 93 - std::exit(EXIT_FAILURE); 91 + exit_with("unable to use certificate file", true); 94 92 } 95 93 if (SSL_CTX_use_PrivateKey_file( 96 94 ssl_context, 97 95 ".maple/private.pem", 98 96 SSL_FILETYPE_PEM 99 97 ) <= 0) { 100 - perror("unable to use private key file"); 101 - ERR_print_errors_fp(stderr); 102 - std::exit(EXIT_FAILURE); 98 + exit_with("unable to use private key file", true); 103 99 } 104 100 105 101 socket_address.sin_family = AF_INET; ··· 108 104 109 105 maple_socket = socket(AF_INET, SOCK_STREAM, 0); 110 106 if (maple_socket < 0) { 111 - perror("unable to create socket"); 112 - std::exit(EXIT_FAILURE); 107 + exit_with("unable to create socket", false); 113 108 } 114 109 115 110 // Reuse address. Allows the use of the address instantly after a SIGINT ··· 122 117 &reuse_addr, 123 118 sizeof(int) 124 119 ) < 0) { 125 - perror("unable to set socket options (SO_LINGER)"); 126 - std::exit(EXIT_FAILURE); 120 + exit_with("unable to set socket options (SO_LINGER)", false); 127 121 } 128 122 129 123 if (bind( ··· 131 125 reinterpret_cast<sockaddr *>(&socket_address), 132 126 sizeof(socket_address) 133 127 ) < 0) { 134 - perror("unable to bind"); 135 - std::exit(EXIT_FAILURE); 128 + exit_with("unable to bind", false); 136 129 } 137 130 if (listen(maple_socket, 1) < 0) { 138 - perror("unable to listen"); 139 - std::exit(EXIT_FAILURE); 131 + exit_with("unable to listen", false); 140 132 } 141 133 142 134 // Listen and serve connections ··· 151 143 ); 152 144 char request[1024]; 153 145 154 - if (client < 0) { 155 - perror("unable to accept"); 156 - std::exit(EXIT_FAILURE); 157 - } 146 + if (client < 0) { exit_with("unable to accept", false); } 158 147 159 148 ssl = SSL_new(ssl_context); 160 149 SSL_set_fd(ssl, client); ··· 230 219 close(client); 231 220 } 232 221 } 222 + 223 + auto exit_with[[noreturn]](const char *message, bool ssl) -> void { 224 + perror(message); 225 + if (ssl) { ERR_print_errors_fp(stderr); } 226 + std::exit(EXIT_FAILURE); 227 + }