From: Christopher Faulet Date: Mon, 27 Nov 2023 17:49:47 +0000 (+0100) Subject: MINOR: http-fetch: Add a sample to get the transaction status code X-Git-Tag: v2.9-dev12~29 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2de9e3ae24dda5c4ea1d4f2a4bd9358e8ab2f712;p=thirdparty%2Fhaproxy.git MINOR: http-fetch: Add a sample to get the transaction status code 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. --- diff --git a/doc/configuration.txt b/doc/configuration.txt index b4827e00f5..10ccf2f3a8 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -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] | | diff --git a/src/http_fetch.c b/src/http_fetch.c index 38b1e0b085..1f3e4a05de 100644 --- a/src/http_fetch.c +++ b/src/http_fetch.c @@ -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 },