From 6b49660d24022dd4637c69456f31de24264f9718 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 12 Nov 2019 12:30:34 +0000 Subject: [PATCH] [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); -- 2.39.5