]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: http-fetch: Add a sample to get the transaction status code
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 27 Nov 2023 17:49:47 +0000 (18:49 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 29 Nov 2023 10:11:12 +0000 (11:11 +0100)
It was possible get the status code in the HTTP response and the one
received from the server. Thanks to 'txn.status', it is now possible to get
the transaction status code. It is equivalent to '%ST' in log-format.

Most of time, it is the same than 'status', except if the status code of the
HTTP reply does not match the one used to interrupt the transaction. For
instance, an error file use mapped on 400 containing a 404.

doc/configuration.txt
src/http_fetch.c

index b4827e00f5b2cea335773c875ab95130b8572aac..10ccf2f3a89c5a295f9aac538b301cdd679ef494 100644 (file)
@@ -22844,6 +22844,10 @@ status : integer
 
   It may be used in tcp-check based expect rules.
 
+txn.status : integer
+  Return an integer containing the HTTP status code of the transaction, as
+  reported in the log.
+
 txn.timer.total : integer
   Total active time for the HTTP request, between the moment the proxy received
   the first byte of the request header and the emission of the last byte of the
@@ -23967,6 +23971,7 @@ Please refer to the table below for currently defined variables :
   |   | %ID  | unique-id                                            | string  |
   +---+------+------------------------------------------------------+---------+
   |   | %ST  | status_code                                          | numeric |
+  |   |      | %[txn.status]                                        |         |
   +---+------+------------------------------------------------------+---------+
   |   | %U   | bytes_uploaded       (from client to server)         | numeric |
   |   |      | %[bytes_in]                                          |         |
index 38b1e0b0856b62e4ad29d95307e3d267df41f3f8..1f3e4a05dead76a5f889c90f407655bf2cd83d61 100644 (file)
@@ -445,25 +445,31 @@ static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const ch
        return 1;
 }
 
-/* It returns the server status code */
+/* It returns the server or the txn status code, depending on the keyword */
 static int smp_fetch_srv_status(const struct arg *args, struct sample *smp, const char *kw, void *private)
 {
        struct http_txn *txn;
+       short status;
 
        txn = (smp->strm ? smp->strm->txn : NULL);
        if (!txn)
                return 0;
 
-       if (txn->server_status == -1) {
+       status = (kw[0] == 't' ? txn->status : txn->server_status);
+       if (status == -1) {
                struct channel *chn = SMP_RES_CHN(smp);
                struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
 
                if (!htx)
                        return 0;
+
+               status = (kw[0] == 't' ? txn->status : txn->server_status);
        }
 
+       if (kw[0] != 't')
+               smp->flags = SMP_F_VOL_1ST;
        smp->data.type = SMP_T_SINT;
-       smp->data.u.sint = txn->server_status;
+       smp->data.u.sint = status;
        return 1;
 }
 
@@ -2338,6 +2344,7 @@ static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
        { "shdr_val",           smp_fetch_hdr_val,            ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
 
        { "status",             smp_fetch_stcode,             0,                NULL,    SMP_T_SINT, SMP_USE_HRSHP },
+       { "txn.status",         smp_fetch_srv_status,         0,                NULL,    SMP_T_SINT, SMP_USE_HRSHP },
        { "unique-id",          smp_fetch_uniqueid,           0,                NULL,    SMP_T_STR,  SMP_SRC_L4SRV },
        { "url",                smp_fetch_url,                0,                NULL,    SMP_T_STR,  SMP_USE_HRQHV },
        { "url32",              smp_fetch_url32,              0,                NULL,    SMP_T_SINT, SMP_USE_HRQHV },