From 5545a43c18daef7226fde66a942f7051d7aa3988 Mon Sep 17 00:00:00 2001 From: bel Date: Tue, 26 Jul 2016 22:45:47 +0200 Subject: [PATCH] Add content type and charset to the default error page --- src/civetweb.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/civetweb.c b/src/civetweb.c index c258acd1..51d8206e 100644 --- a/src/civetweb.c +++ b/src/civetweb.c @@ -2830,7 +2830,7 @@ send_http_error(struct mg_connection *conn, int status, const char *fmt, ...) { char buf[MG_BUF_LEN]; va_list ap; - int len, i, page_handler_found, scope, truncated; + int len, i, page_handler_found, scope, truncated, has_body; char date[64]; time_t curtime = time(NULL); const char *error_handler = NULL; @@ -2917,17 +2917,24 @@ send_http_error(struct mg_connection *conn, int status, const char *fmt, ...) /* No custom error page. Send default error page. */ gmt_time_string(date, sizeof(date), &curtime); + /* Errors 1xx, 204 and 304 MUST NOT send a body */ + has_body = (status > 199 && status != 204 && status != 304); + conn->must_close = 1; mg_printf(conn, "HTTP/1.1 %d %s\r\n", status, status_text); send_no_cache_header(conn); + if (has_body) { + mg_printf(conn, + "%s", + "Content-Type: text/plain; charset=utf-8\r\n"); + } mg_printf(conn, "Date: %s\r\n" "Connection: close\r\n\r\n", date); /* Errors 1xx, 204 and 304 MUST NOT send a body */ - if (status > 199 && status != 204 && status != 304) { - + if (has_body) { mg_printf(conn, "Error %d: %s\n", status, status_text); if (fmt != NULL) { @@ -11389,7 +11396,8 @@ ssl_get_client_cert_info(struct mg_connection *conn) /* Calculate SHA1 fingerprint and store as a hex string */ ulen = 0; ASN1_digest((int (*)())i2d_X509, digest, (char *)cert, buf, &ulen); - if (!hexdump2string(buf, (int)ulen, str_finger, (int)sizeof(str_finger))) { + if (!hexdump2string( + buf, (int)ulen, str_finger, (int)sizeof(str_finger))) { *str_finger = 0; }