From 3d5d2d6f808761c2398d86f94e3cc18b320986c0 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 6 Feb 2017 14:31:39 +0000 Subject: [PATCH] [Minor] Use some implicit status messages for HTTP replies --- src/libutil/http.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/libutil/http.c b/src/libutil/http.c index deddc56a4..472ca0f5d 100644 --- a/src/libutil/http.c +++ b/src/libutil/http.c @@ -1740,6 +1740,8 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, if (conn->type == RSPAMD_HTTP_SERVER) { /* Format reply */ if (msg->method < HTTP_SYMBOLS) { + rspamd_ftok_t status; + ptm = gmtime (&msg->date); t = *ptm; rspamd_snprintf (datebuf, sizeof(datebuf), @@ -1750,17 +1752,40 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, mime_type = encrypted ? "application/octet-stream" : "text/plain"; } + + if (msg->status == NULL || msg->status->len == 0) { + if (msg->code == 200) { + RSPAMD_FTOK_ASSIGN (&status, "OK"); + } + else if (msg->code == 404) { + RSPAMD_FTOK_ASSIGN (&status, "Not Found"); + } + else if (msg->code == 403) { + RSPAMD_FTOK_ASSIGN (&status, "Forbidden"); + } + else if (msg->code >= 500 && msg->code < 600) { + RSPAMD_FTOK_ASSIGN (&status, "Internal Server Error"); + } + else { + RSPAMD_FTOK_ASSIGN (&status, "Undefined Error"); + } + } + else { + status.begin = msg->status->str; + status.len = msg->status->len; + } + if (encrypted) { /* Internal reply (encrypted) */ meth_len = rspamd_snprintf (repbuf, replen, - "HTTP/1.1 %d %V\r\n" + "HTTP/1.1 %d %T\r\n" "Connection: close\r\n" "Server: %s\r\n" "Date: %s\r\n" "Content-Length: %z\r\n" "Content-Type: %s", /* NO \r\n at the end ! */ - msg->code, msg->status, "rspamd/" RVERSION, datebuf, + msg->code, &status, "rspamd/" RVERSION, datebuf, bodylen, mime_type); enclen += meth_len; /* External reply */ @@ -1776,13 +1801,13 @@ rspamd_http_message_write_header (const gchar* mime_type, gboolean encrypted, else { meth_len = rspamd_printf_fstring (buf, - "HTTP/1.1 %d %V\r\n" + "HTTP/1.1 %d %T\r\n" "Connection: close\r\n" "Server: %s\r\n" "Date: %s\r\n" "Content-Length: %z\r\n" "Content-Type: %s\r\n", - msg->code, msg->status, "rspamd/" RVERSION, datebuf, + msg->code, &status, "rspamd/" RVERSION, datebuf, bodylen, mime_type); } } -- 2.39.5