]> ruderich.org/simon Gitweb - tlsproxy/tlsproxy.git/commitdiff
Simplify functions reporting errors to the client.
authorSimon Ruderich <simon@ruderich.org>
Mon, 29 Jul 2013 02:26:33 +0000 (04:26 +0200)
committerSimon Ruderich <simon@ruderich.org>
Mon, 29 Jul 2013 02:26:33 +0000 (04:26 +0200)
src/connection.c

index 7f2071a7975340737e68acdd06237ae759e4d5ca..54e5671fc8fc14fd21e85f1c5ce1688707ea8011 100644 (file)
@@ -21,6 +21,7 @@
 #include "connection.h"
 #include "verify.h"
 
+#include <assert.h>
 #include <errno.h>
 #include <netdb.h>
 #include <poll.h>
@@ -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));
 }