From 2efc5d9fce9baabbce6b41f9d4d26957f7a00c9c Mon Sep 17 00:00:00 2001
From: Joe Mucchiello <jmucchiello@gmail.com>
Date: Sun, 10 Feb 2013 15:58:06 -0500
Subject: [PATCH] http_error callback

Allow user to display status code errors
---
 mongoose.c | 35 +++++++++++++++++++----------------
 mongoose.h |  1 +
 2 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/mongoose.c b/mongoose.c
index 74325e989..4e6cbd787 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -915,24 +915,27 @@ static void send_http_error(struct mg_connection *conn, int status,
   int len = 0;
 
   conn->status_code = status;
-  buf[0] = '\0';
-
-  // Errors 1xx, 204 and 304 MUST NOT send a body
-  if (status > 199 && status != 204 && status != 304) {
-    len = mg_snprintf(conn, buf, sizeof(buf), "Error %d: %s", status, reason);
-    buf[len++] = '\n';
+  if (conn->ctx->callbacks.http_error == NULL ||
+      conn->ctx->callbacks.http_error(conn, status)) {
+    buf[0] = '\0';
+
+    // Errors 1xx, 204 and 304 MUST NOT send a body
+    if (status > 199 && status != 204 && status != 304) {
+      len = mg_snprintf(conn, buf, sizeof(buf), "Error %d: %s", status, reason);
+      buf[len++] = '\n';
+
+      va_start(ap, fmt);
+      len += mg_vsnprintf(conn, buf + len, sizeof(buf) - len, fmt, ap);
+      va_end(ap);
+    }
+    DEBUG_TRACE(("[%s]", buf));
 
-    va_start(ap, fmt);
-    len += mg_vsnprintf(conn, buf + len, sizeof(buf) - len, fmt, ap);
-    va_end(ap);
+    mg_printf(conn, "HTTP/1.1 %d %s\r\n"
+              "Content-Length: %d\r\n"
+              "Connection: %s\r\n\r\n", status, reason, len,
+              suggest_connection_header(conn));
+    conn->num_bytes_sent += mg_printf(conn, "%s", buf);
   }
-  DEBUG_TRACE(("[%s]", buf));
-
-  mg_printf(conn, "HTTP/1.1 %d %s\r\n"
-            "Content-Length: %d\r\n"
-            "Connection: %s\r\n\r\n", status, reason, len,
-            suggest_connection_header(conn));
-  conn->num_bytes_sent += mg_printf(conn, "%s", buf);
 }
 
 #if defined(_WIN32) && !defined(__SYMBIAN32__)
diff --git a/mongoose.h b/mongoose.h
index 20904f8a0..de692f709 100644
--- a/mongoose.h
+++ b/mongoose.h
@@ -67,6 +67,7 @@ struct mg_callbacks {
                              const char *path, size_t *data_len);
   void (*init_lua)(struct mg_connection *, void *lua_context);
   void (*upload)(struct mg_connection *, const char *file_name);
+  int  (*http_error)(struct mg_connection *, int status);
 };
 
 // Start web server.
-- 
GitLab