]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Use sane HTTP codes in case of proxy errors
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 12 Nov 2019 12:30:34 +0000 (12:30 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 12 Nov 2019 12:30:34 +0000 (12:30 +0000)
src/libutil/fstring.h
src/rspamd_proxy.c

index 6102215d4efc7b4b0cd14eda77c151da1d4ac077..730a59c1c36def5d0ecefcfee2b8a8dfa9454f16 100644 (file)
@@ -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;
index ae51e3e5bff2b587cce4fabe8850d228b167a7b0..e56b9b16f5ae3c96360342b09a626dcbc25bee21 100644 (file)
@@ -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);