commit 6b49660: [Minor] Use sane HTTP codes in case of proxy errors

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Nov 12 12:35:11 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-11-12 12:30:34 +0000
URL: https://github.com/rspamd/rspamd/commit/6b49660d24022dd4637c69456f31de24264f9718 (HEAD -> master)

[Minor] Use sane HTTP codes in case of proxy errors

---
 src/libutil/fstring.h |  1 +
 src/rspamd_proxy.c    | 32 ++++++++++++++++++++++++++++++--
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/src/libutil/fstring.h b/src/libutil/fstring.h
index 6102215d4..730a59c1c 100644
--- a/src/libutil/fstring.h
+++ b/src/libutil/fstring.h
@@ -37,6 +37,7 @@ typedef struct f_str_s {
 
 #define RSPAMD_FSTRING_DATA(s) ((s)->str)
 #define RSPAMD_FSTRING_LEN(s) ((s)->len)
+#define RSPAMD_FSTRING_LIT(lit) rspamd_fstring_new_init((lit), sizeof(lit) - 1)
 
 typedef struct f_str_tok {
 	gsize len;
diff --git a/src/rspamd_proxy.c b/src/rspamd_proxy.c
index ae51e3e5b..e56b9b16f 100644
--- a/src/rspamd_proxy.c
+++ b/src/rspamd_proxy.c
@@ -1474,8 +1474,36 @@ proxy_client_write_error (struct rspamd_proxy_session *session, gint code,
 	}
 	else {
 		reply = rspamd_http_new_message (HTTP_RESPONSE);
-		reply->code = code;
-		reply->status = rspamd_fstring_new_init (status, strlen (status));
+
+		switch (code) {
+		case ETIMEDOUT:
+			reply->code = 504;
+			reply->status = RSPAMD_FSTRING_LIT ("Gateway timeout");
+			break;
+		case ECONNRESET:
+		case ECONNABORTED:
+			reply->code = 502;
+			reply->status = RSPAMD_FSTRING_LIT ("Gateway connection reset");
+			break;
+		case ECONNREFUSED:
+			reply->code = 502;
+			reply->status = RSPAMD_FSTRING_LIT ("Gateway connection refused");
+			break;
+		default:
+			if (code >= 300) {
+				/* Likely HTTP error */
+				reply->code = code;
+				reply->status = rspamd_fstring_new_init (status, strlen (status));
+			}
+			else {
+				reply->code = 502;
+				reply->status = RSPAMD_FSTRING_LIT ("Unknown gateway error: ");
+				reply->status = rspamd_fstring_append (reply->status,
+						status, strlen (status));
+			}
+			break;
+		}
+
 		rspamd_http_connection_write_message (session->client_conn,
 				reply, NULL, NULL, session,
 				session->ctx->timeout);


More information about the Commits mailing list