]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: http-htx: Handle an optional reason when replacing the response status
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 31 Aug 2020 14:43:34 +0000 (16:43 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Tue, 1 Sep 2020 08:55:36 +0000 (10:55 +0200)
When calling the http_replace_res_status() function, an optional reason may now
be set. It is ignored if it points to NULL and the original reason is
preserved. Only the response status is replaced. Otherwise both the status and
the reason are replaced.

It simplifies the API and most of time, avoids an extra call to
http_replace_res_reason().

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

index a02c65c009fa66edc9891e786db838f2067dc3d5..f90b4cc74786b9bbd2c82f8c25c2f29a7598c982 100644 (file)
@@ -47,7 +47,7 @@ int http_replace_req_meth(struct htx *htx, const struct ist meth);
 int http_replace_req_uri(struct htx *htx, const struct ist uri);
 int http_replace_req_path(struct htx *htx, const struct ist path, int with_qs);
 int http_replace_req_query(struct htx *htx, const struct ist query);
-int http_replace_res_status(struct htx *htx, const struct ist status);
+int http_replace_res_status(struct htx *htx, const struct ist status, const struct ist reason);
 int http_replace_res_reason(struct htx *htx, const struct ist reason);
 int http_replace_header_value(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist data);
 int http_replace_header(struct htx *htx, struct http_hdr_ctx *ctx, const struct ist name, const struct ist value);
index 73b6e7618696631b57f160bce4acc9bbd7c53d00..bce84b9eed18401a8080d3982c2031ad84495057 100644 (file)
@@ -2815,9 +2815,7 @@ int http_res_set_status(unsigned int status, struct ist reason, struct stream *s
                reason = ist2(str, strlen(str));
        }
 
-       if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
-               return -1;
-       if (!http_replace_res_reason(htx, reason))
+       if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
                return -1;
        return 0;
 }
index c572697bed118d7a601b30b56eb20e7eb84a2e9b..de65a4dd0a4c9b3de5aa02bb7572962742074490 100644 (file)
@@ -487,11 +487,11 @@ int http_replace_req_query(struct htx *htx, const struct ist query)
 /* Replace the response status in the HTX message <htx> by <status>. It returns
  * 1 on success, otherwise 0.
 */
-int http_replace_res_status(struct htx *htx, const struct ist status)
+int http_replace_res_status(struct htx *htx, const struct ist status, const struct ist reason)
 {
        struct buffer *temp = get_trash_chunk();
        struct htx_sl *sl = http_get_stline(htx);
-       struct ist vsn, reason;
+       struct ist vsn, r;
 
        if (!sl)
                return 0;
@@ -499,13 +499,15 @@ int http_replace_res_status(struct htx *htx, const struct ist status)
        /* Start by copying old uri and version */
        chunk_memcat(temp, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); /* vsn */
        vsn = ist2(temp->area, HTX_SL_RES_VLEN(sl));
-
-       chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
-       reason = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
+       r = reason;
+       if (!isttest(r)) {
+               chunk_memcat(temp, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); /* reason */
+               r = ist2(temp->area + vsn.len, HTX_SL_RES_RLEN(sl));
+       }
 
        /* create the new start line */
        sl->info.res.status = strl2ui(status.ptr, status.len);
-       return http_replace_stline(htx, vsn, status, reason);
+       return http_replace_stline(htx, vsn, status, r);
 }
 
 /* Replace the response reason in the HTX message <htx> by <reason>. It returns