]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: http-htx: Use http reply from the http-errors section
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 14 May 2020 12:49:25 +0000 (14:49 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 20 May 2020 16:27:13 +0000 (18:27 +0200)
When an http reply is configured to use an error message from an http-errors
section, instead of referencing the error message, the http reply is used. To do
so the new http reply type HTTP_REPLY_INDIRECT has been added.

include/types/http_htx.h
src/http_ana.c
src/http_htx.c

index f1f468542941724e90e8a9289b576f5d03b73ca4..c32394392f8e73deaf47efc2e0911fd25f13b008 100644 (file)
@@ -47,10 +47,11 @@ struct http_reply_hdr {
 };
 
 #define HTTP_REPLY_EMPTY    0x00 /* the reply has no payload */
-#define HTTP_REPLY_ERRMSG   0x01 /* the reply is an error message */
+#define HTTP_REPLY_ERRMSG   0x01 /* the reply is an error message (may be NULL) */
 #define HTTP_REPLY_ERRFILES 0x02 /* the reply references an http-errors section */
 #define HTTP_REPLY_RAW      0x03 /* the reply use a raw payload */
 #define HTTP_REPLY_LOGFMT   0x04 /* the reply use a log-format payload */
+#define HTTP_REPLY_INDIRECT 0x05 /* the reply references another http-reply (may be NULL) */
 
 /* Uses by HAProxy to generate internal responses */
 struct http_reply {
@@ -63,7 +64,8 @@ struct http_reply {
                struct buffer obj;            /* A raw string (type = HTTP_REPLY_RAW) */
                struct buffer *errmsg;        /* The error message to use as response (type = HTTP_REPLY_ERRMSG).
                                               * may be NULL, if so rely on the proxy error messages */
-               char          *http_errors;   /* The http-errors section to use (type = HTTP_REPLY_ERRFILES).
+               struct http_reply *reply;     /* The HTTP reply to use as response (type = HTTP_REPLY_INDIRECT) */
+               char *http_errors;            /* The http-errors section to use (type = HTTP_REPLY_ERRFILES).
                                               * Should be resolved during post-check */
        } body;
        struct list list;  /* next http_reply in the global list.
index adadc186f9dca70ea092c0b2b99080158f328a53..6ff6ada1fde0b867c6fcec5198e877ff58dfe567 100644 (file)
@@ -4708,7 +4708,18 @@ int http_reply_message(struct stream *s, struct http_reply *reply)
        s->txn->status = reply->status;
        channel_htx_truncate(res, htx);
 
-       /* HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so */
+       /*
+        * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
+        *
+        * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
+        *   as no payload if NULL. the TXN status code is set with the status
+        *   of the original reply.
+        */
+
+       if (reply->type == HTTP_REPLY_INDIRECT) {
+               if (reply->body.reply)
+                       reply = reply->body.reply;
+       }
 
        if (reply->type == HTTP_REPLY_ERRMSG) {
                /* implicit or explicit error message*/
index 18ac47ce482af0d7577fa8723dd7b9da8366ec30..cefd8f6a716b1932081b9b9ee89f89f37c8ca95c 100644 (file)
@@ -1278,10 +1278,10 @@ int http_check_http_reply(struct http_reply *reply, struct proxy *px, char **err
 
        list_for_each_entry(http_errs, &http_errors_list, list) {
                if (strcmp(http_errs->id, reply->body.http_errors) == 0) {
-                       reply->type = HTTP_REPLY_ERRMSG;
+                       reply->type = HTTP_REPLY_INDIRECT;
                        free(reply->body.http_errors);
-                       reply->body.errmsg = http_errs->errmsg[http_get_status_idx(reply->status)];
-                       if (!reply->body.errmsg)
+                       reply->body.reply = http_errs->replies[http_get_status_idx(reply->status)];
+                       if (!reply->body.reply)
                                ha_warning("Proxy '%s': status '%d' referenced by an http reply "
                                           "not declared in http-errors section '%s'.\n",
                                           px->id, reply->status, http_errs->id);