From: Simon Ruderich Date: Mon, 29 Jul 2013 02:26:33 +0000 (+0200) Subject: Simplify functions reporting errors to the client. X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=2a478be51f6e6d8f56895e3fee3b122625042d8c;p=tlsproxy%2Ftlsproxy.git Simplify functions reporting errors to the client. --- diff --git a/src/connection.c b/src/connection.c index 7f2071a..54e5671 100644 --- a/src/connection.c +++ b/src/connection.c @@ -21,6 +21,7 @@ #include "connection.h" #include "verify.h" +#include #include #include #include @@ -545,54 +546,30 @@ static int read_http_request(FILE *client_fd, char *request, size_t length) { } static void send_bad_request(FILE *client_fd) { -#define RESPONSE_ERROR "400 Bad Request" -#define RESPONSE_MSG "Your browser sent an invalid request." - fprintf(client_fd, HTTP_RESPONSE_FORMAT, - RESPONSE_ERROR, RESPONSE_ERROR, RESPONSE_ERROR, - RESPONSE_MSG); + const char error[] = "400 Bad Request"; + const char msg[] = "Your browser sent an invalid request."; + fprintf(client_fd, HTTP_RESPONSE_FORMAT, error, error, error, msg); fflush(client_fd); -#undef RESPONSE_ERROR -#undef RESPONSE_MSG } static void send_forwarding_failure(FILE *client_fd) { -#define RESPONSE_ERROR "503 Forwarding failure" -#define RESPONSE_MSG "Failed to connect to server, check logs." - fprintf(client_fd, HTTP_RESPONSE_FORMAT, - RESPONSE_ERROR, RESPONSE_ERROR, RESPONSE_ERROR, - RESPONSE_MSG); + const char error[] = "503 Forwarding failure"; + const char msg[] = "Failed to connect to server, check logs."; + fprintf(client_fd, HTTP_RESPONSE_FORMAT, error, error, error, msg); fflush(client_fd); -#undef RESPONSE_ERROR -#undef RESPONSE_MSG } static void tls_send_invalid_cert_message(gnutls_session_t session) { -#define RESPONSE_ERROR "500 Internal Server Error" -#define RESPONSE_MSG "Server certificate validation failed, check logs." + const char error[] = "500 Internal Server Error"; + const char msg[] = "Server certificate validation failed, check logs."; int result; - char buffer[sizeof(HTTP_RESPONSE_FORMAT) - 1 /* '\0' */ - - 4 * 2 /* four %s */ - + (sizeof(RESPONSE_ERROR) - 1 /* '\0' */) * 3 - + sizeof(RESPONSE_MSG) - 1 /* '\0' */ - + 1 /* '\0' */]; - - result = snprintf(buffer, sizeof(buffer), - HTTP_RESPONSE_FORMAT, - RESPONSE_ERROR, RESPONSE_ERROR, RESPONSE_ERROR, - RESPONSE_MSG); - if (result < 0) { - LOG_PERROR(ERROR, - "tls_send_invalid_cert_message(): snprintf failed"); - return; - } else if ((size_t)result >= sizeof(buffer)) { - LOG(ERROR, - "tls_send_invalid_cert_message(): snprintf buffer too short"); - return; - } + char buffer[sizeof(HTTP_RESPONSE_FORMAT) + + 3 * sizeof(error) + sizeof(msg)]; + + result = snprintf(buffer, sizeof(buffer), HTTP_RESPONSE_FORMAT, + error, error, error, msg); + assert(result > 0 && (size_t)result < sizeof(buffer)); - gnutls_record_send(session, buffer, sizeof(buffer) - 1); - /* don't send trailing '\0' */ -#undef RESPONSE_ERROR -#undef RESPONSE_MSG + gnutls_record_send(session, buffer, strlen(buffer)); }