Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP samples fetching |
| 3 | * |
| 4 | * Copyright 2000-2018 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <sys/types.h> |
| 14 | |
| 15 | #include <ctype.h> |
| 16 | #include <string.h> |
| 17 | #include <time.h> |
| 18 | |
| 19 | #include <common/base64.h> |
| 20 | #include <common/chunk.h> |
| 21 | #include <common/compat.h> |
| 22 | #include <common/config.h> |
| 23 | #include <common/debug.h> |
Willy Tarreau | afba57a | 2018-12-11 13:44:24 +0100 | [diff] [blame] | 24 | #include <common/h1.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 25 | #include <common/http.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 26 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 27 | #include <common/initcall.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 28 | #include <common/memory.h> |
| 29 | #include <common/standard.h> |
| 30 | #include <common/version.h> |
| 31 | |
| 32 | #include <types/global.h> |
| 33 | |
| 34 | #include <proto/arg.h> |
| 35 | #include <proto/auth.h> |
Willy Tarreau | 538746a | 2018-12-11 10:59:20 +0100 | [diff] [blame] | 36 | #include <proto/hdr_idx.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 37 | #include <proto/http_fetch.h> |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 38 | #include <proto/http_htx.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 39 | #include <proto/log.h> |
| 40 | #include <proto/obj_type.h> |
| 41 | #include <proto/proto_http.h> |
| 42 | #include <proto/sample.h> |
| 43 | #include <proto/stream.h> |
| 44 | |
| 45 | |
| 46 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 47 | static THREAD_LOCAL struct hdr_ctx static_hdr_ctx; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 48 | static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx; |
| 49 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 50 | #define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL) |
| 51 | #define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 52 | |
| 53 | /* |
| 54 | * Returns the data from Authorization header. Function may be called more |
| 55 | * than once so data is stored in txn->auth_data. When no header is found |
| 56 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 57 | * searching again for something we are unable to find anyway. However, if |
| 58 | * the result if valid, the cache is not reused because we would risk to |
| 59 | * have the credentials overwritten by another stream in parallel. |
| 60 | */ |
| 61 | |
Christopher Faulet | e98411b | 2019-07-15 13:58:29 +0200 | [diff] [blame] | 62 | static int get_http_auth(struct sample *smp, struct htx *htx) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 63 | { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 64 | struct stream *s = smp->strm; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 65 | struct http_txn *txn = s->txn; |
| 66 | struct buffer auth_method; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 67 | char *h, *p; |
| 68 | int len; |
| 69 | |
| 70 | #ifdef DEBUG_AUTH |
| 71 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
| 72 | #endif |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 73 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 74 | return 0; |
| 75 | |
| 76 | txn->auth.method = HTTP_AUTH_WRONG; |
| 77 | |
Christopher Faulet | e98411b | 2019-07-15 13:58:29 +0200 | [diff] [blame] | 78 | if (htx) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 79 | /* HTX version */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 80 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 81 | struct ist hdr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 82 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 83 | if (txn->flags & TX_USE_PX_CONN) |
| 84 | hdr = ist("Proxy-Authorization"); |
| 85 | else |
| 86 | hdr = ist("Authorization"); |
| 87 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 88 | ctx.blk = NULL; |
| 89 | if (!http_find_header(htx, hdr, &ctx, 0)) |
| 90 | return 0; |
| 91 | |
| 92 | p = memchr(ctx.value.ptr, ' ', ctx.value.len); |
| 93 | len = p - ctx.value.ptr; |
| 94 | if (!p || len <= 0) |
| 95 | return 0; |
| 96 | |
| 97 | if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1) |
| 98 | return 0; |
| 99 | |
| 100 | chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 101 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 102 | else { |
| 103 | /* LEGACY version */ |
| 104 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 105 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 106 | if (txn->flags & TX_USE_PX_CONN) { |
| 107 | h = "Proxy-Authorization"; |
| 108 | len = strlen(h); |
| 109 | } else { |
| 110 | h = "Authorization"; |
| 111 | len = strlen(h); |
| 112 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 113 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 114 | if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx)) |
| 115 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 116 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 117 | h = ctx.line + ctx.val; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 118 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 119 | p = memchr(h, ' ', ctx.vlen); |
| 120 | len = p - h; |
| 121 | if (!p || len <= 0) |
| 122 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 123 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 124 | if (chunk_initlen(&auth_method, h, 0, len) != 1) |
| 125 | return 0; |
| 126 | |
| 127 | chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1); |
| 128 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 129 | |
| 130 | if (!strncasecmp("Basic", auth_method.area, auth_method.data)) { |
| 131 | struct buffer *http_auth = get_trash_chunk(); |
| 132 | |
| 133 | len = base64dec(txn->auth.method_data.area, |
| 134 | txn->auth.method_data.data, |
| 135 | http_auth->area, global.tune.bufsize - 1); |
| 136 | |
| 137 | if (len < 0) |
| 138 | return 0; |
| 139 | |
| 140 | |
| 141 | http_auth->area[len] = '\0'; |
| 142 | |
| 143 | p = strchr(http_auth->area, ':'); |
| 144 | |
| 145 | if (!p) |
| 146 | return 0; |
| 147 | |
| 148 | txn->auth.user = http_auth->area; |
| 149 | *p = '\0'; |
| 150 | txn->auth.pass = p+1; |
| 151 | |
| 152 | txn->auth.method = HTTP_AUTH_BASIC; |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 160 | * which means that a request or response is ready. If some data is missing, |
| 161 | * a parsing attempt is made. This is useful in TCP-based ACLs which are able |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 162 | * to extract data from L7. If <vol> is non-null during a prefetch, another |
| 163 | * test is made to ensure the required information is not gone. |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 164 | * |
| 165 | * The function returns : |
| 166 | * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 167 | * decide whether or not an HTTP message is present ; |
| 168 | * NULL if the requested data cannot be fetched or if it is certain that |
| 169 | * we'll never have any HTTP message there ; |
| 170 | * The HTX message if ready |
| 171 | */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 172 | struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, int vol) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 173 | { |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 174 | struct stream *s = smp->strm; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 175 | struct http_txn *txn = NULL; |
| 176 | struct htx *htx = NULL; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 177 | struct http_msg *msg; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 178 | struct htx_sl *sl; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 179 | |
| 180 | /* Note: it is possible that <s> is NULL when called before stream |
| 181 | * initialization (eg: tcp-request connection), so this function is the |
| 182 | * one responsible for guarding against this case for all HTTP users. |
| 183 | */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 184 | if (!s || !chn) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 185 | return NULL; |
| 186 | |
| 187 | if (!s->txn) { |
| 188 | if (unlikely(!http_alloc_txn(s))) |
| 189 | return NULL; /* not enough memory */ |
| 190 | http_init_txn(s); |
| 191 | txn = s->txn; |
| 192 | } |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 193 | txn = s->txn; |
| 194 | msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp); |
| 195 | smp->data.type = SMP_T_BOOL; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 196 | |
Christopher Faulet | eca8854 | 2019-04-03 10:12:42 +0200 | [diff] [blame] | 197 | if (IS_HTX_STRM(s)) { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 198 | htx = htxbuf(&chn->buf); |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 199 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 200 | if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR)) |
| 201 | return NULL; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 202 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 203 | if (msg->msg_state < HTTP_MSG_BODY) { |
| 204 | /* Analyse not yet started */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 205 | if (htx_is_empty(htx) || htx->first == -1) { |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 206 | /* Parsing is done by the mux, just wait */ |
| 207 | smp->flags |= SMP_F_MAY_CHANGE; |
| 208 | return NULL; |
| 209 | } |
| 210 | } |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 211 | sl = http_get_stline(htx); |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 212 | if (vol && !sl) { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 213 | /* The start-line was already forwarded, it is too late to fetch anything */ |
| 214 | return NULL; |
| 215 | } |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 216 | } |
Christopher Faulet | eca8854 | 2019-04-03 10:12:42 +0200 | [diff] [blame] | 217 | else { /* RAW mode */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 218 | struct buffer *buf; |
| 219 | struct h1m h1m; |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 220 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 221 | union h1_sl h1sl; |
| 222 | unsigned int flags = HTX_FL_NONE; |
| 223 | int ret; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 224 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 225 | /* no HTTP fetch on the response in TCP mode */ |
| 226 | if (chn->flags & CF_ISRESP) |
| 227 | return NULL; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 228 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 229 | /* Now we are working on the request only */ |
| 230 | buf = &chn->buf; |
| 231 | if (b_head(buf) + b_data(buf) > b_wrap(buf)) |
| 232 | b_slow_realign(buf, trash.area, 0); |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 233 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 234 | h1m_init_req(&h1m); |
| 235 | ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf), |
| 236 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl); |
| 237 | if (ret <= 0) { |
| 238 | /* Invalid or too big*/ |
| 239 | if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite)) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 240 | return NULL; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 241 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 242 | /* wait for a full request */ |
| 243 | smp->flags |= SMP_F_MAY_CHANGE; |
| 244 | return NULL; |
| 245 | } |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 246 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 247 | /* OK we just got a valid HTTP mesage. We have to convert it |
| 248 | * into an HTX message. |
| 249 | */ |
| 250 | if (unlikely(h1sl.rq.v.len == 0)) { |
| 251 | /* try to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 252 | if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 253 | return NULL; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 254 | h1sl.rq.v = ist("HTTP/1.0"); |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 255 | } |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 256 | |
| 257 | /* Set HTX start-line flags */ |
| 258 | if (h1m.flags & H1_MF_VER_11) |
| 259 | flags |= HTX_SL_F_VER_11; |
| 260 | if (h1m.flags & H1_MF_XFER_ENC) |
| 261 | flags |= HTX_SL_F_XFER_ENC; |
| 262 | flags |= HTX_SL_F_XFER_LEN; |
| 263 | if (h1m.flags & H1_MF_CHNK) |
| 264 | flags |= HTX_SL_F_CHNK; |
| 265 | else if (h1m.flags & H1_MF_CLEN) |
| 266 | flags |= HTX_SL_F_CLEN; |
| 267 | |
| 268 | htx = htx_from_buf(get_trash_chunk()); |
| 269 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v); |
| 270 | if (!sl || !htx_add_all_headers(htx, hdrs)) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 271 | return NULL; |
Willy Tarreau | ce9bbf5 | 2019-05-13 08:32:31 +0200 | [diff] [blame] | 272 | sl->info.req.meth = h1sl.rq.meth; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* OK we just got a valid HTTP message. If not already done by |
| 276 | * HTTP analyzers, we have some minor preparation to perform so |
| 277 | * that further checks can rely on HTTP tests. |
| 278 | */ |
| 279 | if (sl && msg->msg_state < HTTP_MSG_BODY) { |
| 280 | if (!(chn->flags & CF_ISRESP)) { |
| 281 | txn->meth = sl->info.req.meth; |
| 282 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 283 | s->flags |= SF_REDIRECTABLE; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 284 | } |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 285 | else |
| 286 | txn->status = sl->info.res.status; |
| 287 | if (sl->flags & HTX_SL_F_VER_11) |
| 288 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* everything's OK */ |
| 292 | smp->data.u.sint = 1; |
| 293 | return htx; |
| 294 | } |
| 295 | |
| 296 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 297 | * which means that a request or response is ready. If some data is missing, |
| 298 | * a parsing attempt is made. This is useful in TCP-based ACLs which are able |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 299 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 300 | * another test is made to ensure the required information is not gone. |
| 301 | * |
| 302 | * The function returns : |
| 303 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 304 | * decide whether or not an HTTP message is present ; |
| 305 | * 0 if the requested data cannot be fetched or if it is certain that |
| 306 | * we'll never have any HTTP message there ; |
| 307 | * 1 if an HTTP message is ready |
| 308 | */ |
| 309 | int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt, |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 310 | struct channel *chn, struct sample *smp, int req_vol) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 311 | { |
| 312 | struct http_txn *txn; |
| 313 | struct http_msg *msg; |
| 314 | |
| 315 | /* Note: it is possible that <s> is NULL when called before stream |
| 316 | * initialization (eg: tcp-request connection), so this function is the |
| 317 | * one responsible for guarding against this case for all HTTP users. |
| 318 | */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 319 | if (!s || !chn) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 320 | return 0; |
| 321 | |
| 322 | if (!s->txn) { |
| 323 | if (unlikely(!http_alloc_txn(s))) |
| 324 | return 0; /* not enough memory */ |
| 325 | http_init_txn(s); |
| 326 | } |
| 327 | txn = s->txn; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 328 | smp->data.type = SMP_T_BOOL; |
| 329 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 330 | if (chn->flags & CF_ISRESP) { |
| 331 | /* Check for a dependency on a response */ |
| 332 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 333 | smp->flags |= SMP_F_MAY_CHANGE; |
| 334 | return 0; |
| 335 | } |
| 336 | goto end; |
| 337 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 338 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 339 | /* Check for a dependency on a request */ |
| 340 | msg = &txn->req; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 341 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 342 | if (req_vol && (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) { |
| 343 | return 0; /* data might have moved and indexes changed */ |
| 344 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 345 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 346 | /* If the buffer does not leave enough free space at the end, we must |
| 347 | * first realign it. |
| 348 | */ |
| 349 | if (ci_head(chn) > b_orig(&chn->buf) && |
| 350 | ci_head(chn) + ci_data(chn) > b_wrap(&chn->buf) - global.tune.maxrewrite) |
| 351 | channel_slow_realign(chn, trash.area); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 352 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 353 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
| 354 | if (msg->msg_state == HTTP_MSG_ERROR) |
| 355 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 356 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 357 | /* Try to decode HTTP request */ |
| 358 | if (likely(msg->next < ci_data(chn))) |
| 359 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 360 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 361 | /* Still no valid request ? */ |
| 362 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
| 363 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
| 364 | channel_full(chn, global.tune.maxrewrite)) { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 365 | return 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 366 | } |
| 367 | /* wait for final state */ |
| 368 | smp->flags |= SMP_F_MAY_CHANGE; |
| 369 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 372 | /* OK we just got a valid HTTP message. We have some minor |
| 373 | * preparation to perform so that further checks can rely |
| 374 | * on HTTP tests. |
| 375 | */ |
| 376 | |
| 377 | /* If the message was parsed but was too large, we must absolutely |
| 378 | * return an error so that it is not processed. At the moment this |
| 379 | * cannot happen, but if the parsers are to change in the future, |
| 380 | * we want this check to be maintained. |
| 381 | */ |
| 382 | if (unlikely(ci_head(chn) + ci_data(chn) > |
| 383 | b_wrap(&chn->buf) - global.tune.maxrewrite)) { |
| 384 | msg->err_state = msg->msg_state; |
| 385 | msg->msg_state = HTTP_MSG_ERROR; |
| 386 | smp->data.u.sint = 1; |
| 387 | return 1; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 388 | } |
| 389 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 390 | txn->meth = find_http_meth(ci_head(chn), msg->sl.rq.m_l); |
| 391 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 392 | s->flags |= SF_REDIRECTABLE; |
| 393 | |
| 394 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 395 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 398 | end: |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 399 | /* everything's OK */ |
| 400 | smp->data.u.sint = 1; |
| 401 | return 1; |
| 402 | } |
| 403 | |
| 404 | /* This function fetches the method of current HTTP request and stores |
| 405 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 406 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 407 | * in <len> and <ptr> is NULL ; |
| 408 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 409 | * <len> to its length. |
| 410 | * This is intended to be used with pat_match_meth() only. |
| 411 | */ |
| 412 | static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 413 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 414 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 415 | int meth; |
| 416 | struct http_txn *txn; |
| 417 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 418 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 419 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 420 | struct htx *htx = smp_prefetch_htx(smp, chn, 0); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 421 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 422 | if (!htx) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 423 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 424 | |
| 425 | txn = smp->strm->txn; |
| 426 | meth = txn->meth; |
| 427 | smp->data.type = SMP_T_METH; |
| 428 | smp->data.u.meth.meth = meth; |
| 429 | if (meth == HTTP_METH_OTHER) { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 430 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 431 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 432 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 433 | /* ensure the indexes are not affected */ |
| 434 | return 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 435 | } |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 436 | sl = http_get_stline(htx); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 437 | smp->flags |= SMP_F_CONST; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 438 | smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl); |
| 439 | smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 440 | } |
| 441 | smp->flags |= SMP_F_VOL_1ST; |
| 442 | } |
| 443 | else { |
| 444 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 445 | CHECK_HTTP_MESSAGE_FIRST_PERM(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 446 | |
| 447 | txn = smp->strm->txn; |
| 448 | meth = txn->meth; |
| 449 | smp->data.type = SMP_T_METH; |
| 450 | smp->data.u.meth.meth = meth; |
| 451 | if (meth == HTTP_METH_OTHER) { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 452 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 453 | /* ensure the indexes are not affected */ |
| 454 | return 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 455 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 456 | smp->flags |= SMP_F_CONST; |
| 457 | smp->data.u.meth.str.data = txn->req.sl.rq.m_l; |
| 458 | smp->data.u.meth.str.area = ci_head(txn->req.chn); |
| 459 | } |
| 460 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 461 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 462 | return 1; |
| 463 | } |
| 464 | |
| 465 | static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 466 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 467 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 468 | struct http_txn *txn; |
| 469 | char *ptr; |
| 470 | int len; |
| 471 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 472 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 473 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 474 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 475 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 476 | |
| 477 | if (!htx) |
| 478 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 479 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 480 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 481 | len = HTX_SL_REQ_VLEN(sl); |
| 482 | ptr = HTX_SL_REQ_VPTR(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 483 | } |
| 484 | else { |
| 485 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 486 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 487 | |
| 488 | txn = smp->strm->txn; |
| 489 | len = txn->req.sl.rq.v_l; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 490 | ptr = ci_head(chn) + txn->req.sl.rq.v; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 491 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 492 | |
| 493 | while ((len-- > 0) && (*ptr++ != '/')); |
| 494 | if (len <= 0) |
| 495 | return 0; |
| 496 | |
| 497 | smp->data.type = SMP_T_STR; |
| 498 | smp->data.u.str.area = ptr; |
| 499 | smp->data.u.str.data = len; |
| 500 | |
| 501 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 502 | return 1; |
| 503 | } |
| 504 | |
| 505 | static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 506 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 507 | struct channel *chn = SMP_RES_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 508 | struct http_txn *txn; |
| 509 | char *ptr; |
| 510 | int len; |
| 511 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 512 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 513 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 514 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 515 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 516 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 517 | if (!htx) |
| 518 | return 0; |
| 519 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 520 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 521 | len = HTX_SL_RES_VLEN(sl); |
| 522 | ptr = HTX_SL_RES_VPTR(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 523 | } |
| 524 | else { |
| 525 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 526 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 527 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 528 | txn = smp->strm->txn; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 529 | len = txn->rsp.sl.st.v_l; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 530 | ptr = ci_head(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 531 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 532 | |
| 533 | while ((len-- > 0) && (*ptr++ != '/')); |
| 534 | if (len <= 0) |
| 535 | return 0; |
| 536 | |
| 537 | smp->data.type = SMP_T_STR; |
| 538 | smp->data.u.str.area = ptr; |
| 539 | smp->data.u.str.data = len; |
| 540 | |
| 541 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 542 | return 1; |
| 543 | } |
| 544 | |
| 545 | /* 3. Check on Status Code. We manipulate integers here. */ |
| 546 | static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 547 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 548 | struct channel *chn = SMP_RES_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 549 | struct http_txn *txn; |
| 550 | char *ptr; |
| 551 | int len; |
| 552 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 553 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 554 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 555 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 556 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 557 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 558 | if (!htx) |
| 559 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 560 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 561 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 562 | len = HTX_SL_RES_CLEN(sl); |
| 563 | ptr = HTX_SL_RES_CPTR(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 564 | } |
| 565 | else { |
| 566 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 567 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 568 | |
| 569 | txn = smp->strm->txn; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 570 | len = txn->rsp.sl.st.c_l; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 571 | ptr = ci_head(chn) + txn->rsp.sl.st.c; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 572 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 573 | |
| 574 | smp->data.type = SMP_T_SINT; |
| 575 | smp->data.u.sint = __strl2ui(ptr, len); |
| 576 | smp->flags = SMP_F_VOL_1ST; |
| 577 | return 1; |
| 578 | } |
| 579 | |
| 580 | static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 581 | { |
| 582 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 583 | return 0; |
| 584 | |
| 585 | if (!smp->strm->unique_id) { |
| 586 | if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
| 587 | return 0; |
| 588 | smp->strm->unique_id[0] = '\0'; |
| 589 | } |
| 590 | smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id, |
| 591 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 592 | |
| 593 | smp->data.type = SMP_T_STR; |
| 594 | smp->data.u.str.area = smp->strm->unique_id; |
| 595 | smp->flags = SMP_F_CONST; |
| 596 | return 1; |
| 597 | } |
| 598 | |
| 599 | /* Returns a string block containing all headers including the |
Joseph Herlant | 942eea3 | 2018-11-15 13:57:22 -0800 | [diff] [blame] | 600 | * empty line which separes headers from the body. This is useful |
| 601 | * for some headers analysis. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 602 | */ |
| 603 | static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 604 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 605 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 606 | struct http_txn *txn; |
| 607 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 608 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 609 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 610 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 611 | struct buffer *temp; |
| 612 | int32_t pos; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 613 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 614 | if (!htx) |
| 615 | return 0; |
| 616 | temp = get_trash_chunk(); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 617 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 618 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 619 | enum htx_blk_type type = htx_get_blk_type(blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 620 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 621 | if (type == HTX_BLK_HDR) { |
| 622 | struct ist n = htx_get_blk_name(htx, blk); |
| 623 | struct ist v = htx_get_blk_value(htx, blk); |
| 624 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 625 | if (!htx_hdr_to_h1(n, v, temp)) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 626 | return 0; |
| 627 | } |
| 628 | else if (type == HTX_BLK_EOH) { |
| 629 | if (!chunk_memcat(temp, "\r\n", 2)) |
| 630 | return 0; |
| 631 | break; |
| 632 | } |
| 633 | } |
| 634 | smp->data.type = SMP_T_STR; |
| 635 | smp->data.u.str = *temp; |
| 636 | |
| 637 | } |
| 638 | else { |
| 639 | /* LEGACY version */ |
| 640 | struct http_msg *msg; |
| 641 | struct hdr_idx *idx; |
| 642 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 643 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 644 | |
| 645 | txn = smp->strm->txn; |
| 646 | idx = &txn->hdr_idx; |
| 647 | msg = &txn->req; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 648 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 649 | smp->data.type = SMP_T_STR; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 650 | smp->data.u.str.area = ci_head(chn) + hdr_idx_first_pos(idx); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 651 | smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 + |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 652 | (ci_head(chn)[msg->eoh] == '\r'); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 653 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 654 | return 1; |
| 655 | } |
| 656 | |
| 657 | /* Returns the header request in a length/value encoded format. |
| 658 | * This is useful for exchanges with the SPOE. |
| 659 | * |
| 660 | * A "length value" is a multibyte code encoding numbers. It uses the |
| 661 | * SPOE format. The encoding is the following: |
| 662 | * |
| 663 | * Each couple "header name" / "header value" is composed |
| 664 | * like this: |
| 665 | * "length value" "header name bytes" |
| 666 | * "length value" "header value bytes" |
| 667 | * When the last header is reached, the header name and the header |
| 668 | * value are empty. Their length are 0 |
| 669 | */ |
| 670 | static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 671 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 672 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 673 | struct http_txn *txn; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 674 | struct buffer *temp; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 675 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 676 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 677 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 678 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 679 | struct buffer *temp; |
| 680 | char *p, *end; |
| 681 | int32_t pos; |
| 682 | int ret; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 683 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 684 | if (!htx) |
| 685 | return 0; |
| 686 | temp = get_trash_chunk(); |
| 687 | p = temp->area; |
| 688 | end = temp->area + temp->size; |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 689 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 690 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 691 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 692 | struct ist n, v; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 693 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 694 | if (type == HTX_BLK_HDR) { |
| 695 | n = htx_get_blk_name(htx,blk); |
| 696 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 697 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 698 | /* encode the header name. */ |
| 699 | ret = encode_varint(n.len, &p, end); |
| 700 | if (ret == -1) |
| 701 | return 0; |
| 702 | if (p + n.len > end) |
| 703 | return 0; |
| 704 | memcpy(p, n.ptr, n.len); |
| 705 | p += n.len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 706 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 707 | /* encode the header value. */ |
| 708 | ret = encode_varint(v.len, &p, end); |
| 709 | if (ret == -1) |
| 710 | return 0; |
| 711 | if (p + v.len > end) |
| 712 | return 0; |
| 713 | memcpy(p, v.ptr, v.len); |
| 714 | p += v.len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 715 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 716 | } |
| 717 | else if (type == HTX_BLK_EOH) { |
| 718 | /* encode the end of the header list with empty |
| 719 | * header name and header value. |
| 720 | */ |
| 721 | ret = encode_varint(0, &p, end); |
| 722 | if (ret == -1) |
| 723 | return 0; |
| 724 | ret = encode_varint(0, &p, end); |
| 725 | if (ret == -1) |
| 726 | return 0; |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /* Initialise sample data which will be filled. */ |
| 732 | smp->data.type = SMP_T_BIN; |
| 733 | smp->data.u.str.area = temp->area; |
| 734 | smp->data.u.str.data = p - temp->area; |
| 735 | smp->data.u.str.size = temp->size; |
| 736 | } |
| 737 | else { |
| 738 | /* LEGACY version */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 739 | struct hdr_idx *idx; |
| 740 | const char *cur_ptr, *cur_next, *p; |
| 741 | int old_idx, cur_idx; |
| 742 | struct hdr_idx_elem *cur_hdr; |
| 743 | const char *hn, *hv; |
| 744 | int hnl, hvl; |
| 745 | int ret; |
| 746 | char *buf; |
| 747 | char *end; |
| 748 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 749 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 750 | |
| 751 | temp = get_trash_chunk(); |
| 752 | buf = temp->area; |
| 753 | end = temp->area + temp->size; |
| 754 | |
| 755 | txn = smp->strm->txn; |
| 756 | idx = &txn->hdr_idx; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 757 | |
| 758 | /* Build array of headers. */ |
| 759 | old_idx = 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 760 | cur_next = ci_head(chn) + hdr_idx_first_pos(idx); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 761 | while (1) { |
| 762 | cur_idx = idx->v[old_idx].next; |
| 763 | if (!cur_idx) |
| 764 | break; |
| 765 | old_idx = cur_idx; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 766 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 767 | cur_hdr = &idx->v[cur_idx]; |
| 768 | cur_ptr = cur_next; |
| 769 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 770 | |
| 771 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 772 | * and the next header starts at cur_next. We'll check |
| 773 | * this header in the list as well as against the default |
| 774 | * rule. |
| 775 | */ |
| 776 | |
| 777 | /* look for ': *'. */ |
| 778 | hn = cur_ptr; |
| 779 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 780 | if (p >= cur_ptr+cur_hdr->len) |
| 781 | continue; |
| 782 | hnl = p - hn; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 783 | p++; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 784 | while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t')) |
| 785 | p++; |
| 786 | if (p >= cur_ptr + cur_hdr->len) |
| 787 | continue; |
| 788 | hv = p; |
| 789 | hvl = cur_ptr + cur_hdr->len-p; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 790 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 791 | /* encode the header name. */ |
| 792 | ret = encode_varint(hnl, &buf, end); |
| 793 | if (ret == -1) |
| 794 | return 0; |
| 795 | if (buf + hnl > end) |
| 796 | return 0; |
| 797 | memcpy(buf, hn, hnl); |
| 798 | buf += hnl; |
| 799 | |
| 800 | /* encode and copy the value. */ |
| 801 | ret = encode_varint(hvl, &buf, end); |
| 802 | if (ret == -1) |
| 803 | return 0; |
| 804 | if (buf + hvl > end) |
| 805 | return 0; |
| 806 | memcpy(buf, hv, hvl); |
| 807 | buf += hvl; |
| 808 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 809 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 810 | /* encode the end of the header list with empty |
| 811 | * header name and header value. |
| 812 | */ |
| 813 | ret = encode_varint(0, &buf, end); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 814 | if (ret == -1) |
| 815 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 816 | ret = encode_varint(0, &buf, end); |
| 817 | if (ret == -1) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 818 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 819 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 820 | /* Initialise sample data which will be filled. */ |
| 821 | smp->data.type = SMP_T_BIN; |
| 822 | smp->data.u.str.area = temp->area; |
| 823 | smp->data.u.str.data = buf - temp->area; |
| 824 | smp->data.u.str.size = temp->size; |
| 825 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 826 | return 1; |
| 827 | } |
| 828 | |
| 829 | /* returns the longest available part of the body. This requires that the body |
| 830 | * has been waited for using http-buffer-request. |
| 831 | */ |
| 832 | static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 833 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 834 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 835 | struct buffer *temp; |
| 836 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 837 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 838 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 839 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 840 | int32_t pos; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 841 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 842 | if (!htx) |
| 843 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 844 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 845 | temp = get_trash_chunk(); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 846 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 847 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 848 | enum htx_blk_type type = htx_get_blk_type(blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 849 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 850 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 851 | break; |
| 852 | if (type == HTX_BLK_DATA) { |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 853 | if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0)) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 854 | return 0; |
| 855 | } |
| 856 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 857 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 858 | smp->data.type = SMP_T_BIN; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 859 | smp->data.u.str = *temp; |
| 860 | smp->flags = SMP_F_VOL_TEST; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 861 | } |
| 862 | else { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 863 | /* LEGACY version */ |
| 864 | struct http_msg *msg; |
| 865 | unsigned long len; |
| 866 | unsigned long block1; |
| 867 | char *body; |
| 868 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 869 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 870 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 871 | msg = &smp->strm->txn->req; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 872 | len = http_body_bytes(msg); |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 873 | body = c_ptr(chn, -http_data_rewind(msg)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 874 | |
| 875 | block1 = len; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 876 | if (block1 > b_wrap(&chn->buf) - body) |
| 877 | block1 = b_wrap(&chn->buf) - body; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 878 | |
| 879 | if (block1 == len) { |
| 880 | /* buffer is not wrapped (or empty) */ |
| 881 | smp->data.type = SMP_T_BIN; |
| 882 | smp->data.u.str.area = body; |
| 883 | smp->data.u.str.data = len; |
| 884 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 885 | } |
| 886 | else { |
| 887 | /* buffer is wrapped, we need to defragment it */ |
| 888 | temp = get_trash_chunk(); |
| 889 | memcpy(temp->area, body, block1); |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 890 | memcpy(temp->area + block1, b_orig(&chn->buf), len - block1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 891 | smp->data.type = SMP_T_BIN; |
| 892 | smp->data.u.str.area = temp->area; |
| 893 | smp->data.u.str.data = len; |
| 894 | smp->flags = SMP_F_VOL_TEST; |
| 895 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 896 | } |
| 897 | return 1; |
| 898 | } |
| 899 | |
| 900 | |
| 901 | /* returns the available length of the body. This requires that the body |
| 902 | * has been waited for using http-buffer-request. |
| 903 | */ |
| 904 | static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 905 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 906 | struct channel *chn = SMP_REQ_CHN(smp); |
| 907 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 908 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 909 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 910 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 911 | int32_t pos; |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 912 | unsigned long long len = 0; |
| 913 | |
| 914 | if (!htx) |
| 915 | return 0; |
| 916 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 917 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 918 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 919 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 920 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 921 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 922 | break; |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 923 | if (type == HTX_BLK_DATA) |
| 924 | len += htx_get_blksz(blk); |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | smp->data.type = SMP_T_SINT; |
| 928 | smp->data.u.sint = len; |
| 929 | |
| 930 | smp->flags = SMP_F_VOL_TEST; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 931 | } |
| 932 | else { |
| 933 | /* LEGACY version */ |
| 934 | struct http_msg *msg; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 935 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 936 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 937 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 938 | msg = &smp->strm->txn->req; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 939 | smp->data.type = SMP_T_SINT; |
| 940 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 941 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 942 | smp->flags = SMP_F_VOL_TEST; |
| 943 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 944 | return 1; |
| 945 | } |
| 946 | |
| 947 | |
| 948 | /* returns the advertised length of the body, or the advertised size of the |
| 949 | * chunks available in the buffer. This requires that the body has been waited |
| 950 | * for using http-buffer-request. |
| 951 | */ |
| 952 | static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 953 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 954 | struct channel *chn = SMP_REQ_CHN(smp); |
| 955 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 956 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 957 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 958 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 959 | int32_t pos; |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 960 | unsigned long long len = 0; |
| 961 | |
| 962 | if (!htx) |
| 963 | return 0; |
| 964 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 965 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 966 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 967 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 968 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 969 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 970 | break; |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 971 | if (type == HTX_BLK_DATA) |
| 972 | len += htx_get_blksz(blk); |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 973 | } |
| 974 | if (htx->extra != ULLONG_MAX) |
| 975 | len += htx->extra; |
| 976 | |
| 977 | smp->data.type = SMP_T_SINT; |
| 978 | smp->data.u.sint = len; |
| 979 | |
| 980 | smp->flags = SMP_F_VOL_TEST; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 981 | } |
| 982 | else { |
| 983 | /* LEGACY version */ |
| 984 | struct http_msg *msg; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 985 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 986 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 987 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 988 | msg = &smp->strm->txn->req; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 989 | smp->data.type = SMP_T_SINT; |
| 990 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 991 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 992 | smp->flags = SMP_F_VOL_TEST; |
| 993 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 994 | return 1; |
| 995 | } |
| 996 | |
| 997 | |
| 998 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
| 999 | static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1000 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1001 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1002 | struct http_txn *txn; |
| 1003 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1004 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1005 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1006 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1007 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1008 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1009 | if (!htx) |
| 1010 | return 0; |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1011 | sl = http_get_stline(htx); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1012 | smp->data.type = SMP_T_STR; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1013 | smp->data.u.str.area = HTX_SL_REQ_UPTR(sl); |
| 1014 | smp->data.u.str.data = HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1015 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1016 | } |
| 1017 | else { |
| 1018 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1019 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1020 | |
| 1021 | txn = smp->strm->txn; |
| 1022 | smp->data.type = SMP_T_STR; |
| 1023 | smp->data.u.str.data = txn->req.sl.rq.u_l; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1024 | smp->data.u.str.area = ci_head(chn) + txn->req.sl.rq.u; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1025 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1026 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1027 | return 1; |
| 1028 | } |
| 1029 | |
| 1030 | static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1031 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1032 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1033 | struct http_txn *txn; |
| 1034 | struct sockaddr_storage addr; |
| 1035 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1036 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1037 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1038 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1039 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1040 | |
| 1041 | if (!htx) |
| 1042 | return 0; |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1043 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1044 | url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1045 | } |
| 1046 | else { |
| 1047 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1048 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1049 | |
| 1050 | txn = smp->strm->txn; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1051 | url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1052 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1053 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1054 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 1055 | return 0; |
| 1056 | |
| 1057 | smp->data.type = SMP_T_IPV4; |
| 1058 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
| 1059 | smp->flags = 0; |
| 1060 | return 1; |
| 1061 | } |
| 1062 | |
| 1063 | static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1064 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1065 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1066 | struct http_txn *txn; |
| 1067 | struct sockaddr_storage addr; |
| 1068 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1069 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1070 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1071 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1072 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1073 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1074 | if (!htx) |
| 1075 | return 0; |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1076 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1077 | url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1078 | } |
| 1079 | else { |
| 1080 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1081 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1082 | |
| 1083 | txn = smp->strm->txn; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1084 | url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1085 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1086 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 1087 | return 0; |
| 1088 | |
| 1089 | smp->data.type = SMP_T_SINT; |
| 1090 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
| 1091 | smp->flags = 0; |
| 1092 | return 1; |
| 1093 | } |
| 1094 | |
| 1095 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 1096 | * Accepts an optional argument of type string containing the header field name, |
| 1097 | * and an optional argument of type signed or unsigned integer to request an |
| 1098 | * explicit occurrence of the header. Note that in the event of a missing name, |
| 1099 | * headers are considered from the first one. It does not stop on commas and |
| 1100 | * returns full lines instead (useful for User-Agent or Date for example). |
| 1101 | */ |
| 1102 | static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1103 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1104 | /* possible keywords: req.fhdr, res.fhdr */ |
| 1105 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1106 | int occ = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1107 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1108 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1109 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1110 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1111 | struct http_hdr_ctx *ctx = smp->ctx.a[0]; |
| 1112 | struct ist name; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1113 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1114 | if (!ctx) { |
| 1115 | /* first call */ |
| 1116 | ctx = &static_http_hdr_ctx; |
| 1117 | ctx->blk = NULL; |
| 1118 | smp->ctx.a[0] = ctx; |
| 1119 | } |
| 1120 | |
| 1121 | if (args) { |
| 1122 | if (args[0].type != ARGT_STR) |
| 1123 | return 0; |
| 1124 | name.ptr = args[0].data.str.area; |
| 1125 | name.len = args[0].data.str.data; |
| 1126 | |
| 1127 | if (args[1].type == ARGT_SINT) |
| 1128 | occ = args[1].data.sint; |
| 1129 | } |
| 1130 | |
| 1131 | if (!htx) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1132 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1133 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1134 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 1135 | /* search for header from the beginning */ |
| 1136 | ctx->blk = NULL; |
| 1137 | |
| 1138 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 1139 | /* no explicit occurrence and single fetch => last header by default */ |
| 1140 | occ = -1; |
| 1141 | |
| 1142 | if (!occ) |
| 1143 | /* prepare to report multiple occurrences for ACL fetches */ |
| 1144 | smp->flags |= SMP_F_NOT_LAST; |
| 1145 | |
| 1146 | smp->data.type = SMP_T_STR; |
| 1147 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
| 1148 | if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data)) |
| 1149 | return 1; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1150 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1151 | else { |
| 1152 | /* LEGACY version */ |
| 1153 | struct hdr_idx *idx; |
| 1154 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 1155 | const struct http_msg *msg; |
| 1156 | const char *name_str = NULL; |
| 1157 | int name_len = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1158 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1159 | if (!ctx) { |
| 1160 | /* first call */ |
| 1161 | ctx = &static_hdr_ctx; |
| 1162 | ctx->idx = 0; |
| 1163 | smp->ctx.a[0] = ctx; |
| 1164 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1165 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1166 | if (args) { |
| 1167 | if (args[0].type != ARGT_STR) |
| 1168 | return 0; |
| 1169 | name_str = args[0].data.str.area; |
| 1170 | name_len = args[0].data.str.data; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1171 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1172 | if (args[1].type == ARGT_SINT) |
| 1173 | occ = args[1].data.sint; |
| 1174 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1175 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1176 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1177 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1178 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1179 | msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1180 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1181 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 1182 | /* search for header from the beginning */ |
| 1183 | ctx->idx = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1184 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1185 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 1186 | /* no explicit occurrence and single fetch => last header by default */ |
| 1187 | occ = -1; |
| 1188 | |
| 1189 | if (!occ) |
| 1190 | /* prepare to report multiple occurrences for ACL fetches */ |
| 1191 | smp->flags |= SMP_F_NOT_LAST; |
| 1192 | |
| 1193 | smp->data.type = SMP_T_STR; |
| 1194 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
| 1195 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data)) |
| 1196 | return 1; |
| 1197 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1198 | smp->flags &= ~SMP_F_NOT_LAST; |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 1203 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 1204 | * returns full lines instead (useful for User-Agent or Date for example). |
| 1205 | */ |
| 1206 | static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1207 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1208 | /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */ |
| 1209 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1210 | int cnt; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1211 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1212 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1213 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1214 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1215 | struct http_hdr_ctx ctx; |
| 1216 | struct ist name; |
| 1217 | |
| 1218 | if (!htx) |
| 1219 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1220 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1221 | if (args && args->type == ARGT_STR) { |
| 1222 | name.ptr = args->data.str.area; |
| 1223 | name.len = args->data.str.data; |
Olivier Houchard | e2c78cd | 2018-11-21 13:49:48 +0100 | [diff] [blame] | 1224 | } else { |
| 1225 | name.ptr = NULL; |
| 1226 | name.len = 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | ctx.blk = NULL; |
| 1230 | cnt = 0; |
| 1231 | while (http_find_header(htx, name, &ctx, 1)) |
| 1232 | cnt++; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1233 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1234 | else { |
| 1235 | /* LEGACY version */ |
| 1236 | struct hdr_idx *idx; |
| 1237 | struct hdr_ctx ctx; |
| 1238 | const struct http_msg *msg; |
| 1239 | const char *name = NULL; |
| 1240 | int len = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1241 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1242 | if (args && args->type == ARGT_STR) { |
| 1243 | name = args->data.str.area; |
| 1244 | len = args->data.str.data; |
| 1245 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1246 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1247 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1248 | |
| 1249 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1250 | msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1251 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1252 | ctx.idx = 0; |
| 1253 | cnt = 0; |
| 1254 | while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx)) |
| 1255 | cnt++; |
| 1256 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1257 | |
| 1258 | smp->data.type = SMP_T_SINT; |
| 1259 | smp->data.u.sint = cnt; |
| 1260 | smp->flags = SMP_F_VOL_HDR; |
| 1261 | return 1; |
| 1262 | } |
| 1263 | |
| 1264 | static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1265 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1266 | /* possible keywords: req.hdr_names, res.hdr_names */ |
| 1267 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1268 | struct buffer *temp; |
| 1269 | char del = ','; |
| 1270 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1271 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1272 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1273 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1274 | int32_t pos; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1275 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1276 | if (!htx) |
| 1277 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1278 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1279 | if (args && args->type == ARGT_STR) |
| 1280 | del = *args[0].data.str.area; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1281 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1282 | temp = get_trash_chunk(); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 1283 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1284 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1285 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1286 | struct ist n; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1287 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1288 | if (type == HTX_BLK_EOH) |
| 1289 | break; |
| 1290 | if (type != HTX_BLK_HDR) |
| 1291 | continue; |
| 1292 | n = htx_get_blk_name(htx, blk); |
| 1293 | |
| 1294 | if (temp->data) |
| 1295 | temp->area[temp->data++] = del; |
| 1296 | chunk_memcat(temp, n.ptr, n.len); |
| 1297 | } |
| 1298 | } |
| 1299 | else { |
| 1300 | /* LEGACY version */ |
| 1301 | struct hdr_idx *idx; |
| 1302 | struct hdr_ctx ctx; |
| 1303 | const struct http_msg *msg; |
| 1304 | |
| 1305 | if (args && args->type == ARGT_STR) |
| 1306 | del = *args[0].data.str.area; |
| 1307 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1308 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1309 | |
| 1310 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1311 | msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1312 | |
| 1313 | temp = get_trash_chunk(); |
| 1314 | |
| 1315 | ctx.idx = 0; |
| 1316 | while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) { |
| 1317 | if (temp->data) |
| 1318 | temp->area[temp->data++] = del; |
| 1319 | memcpy(temp->area + temp->data, ctx.line, ctx.del); |
| 1320 | temp->data += ctx.del; |
| 1321 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | smp->data.type = SMP_T_STR; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1325 | smp->data.u.str = *temp; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1326 | smp->flags = SMP_F_VOL_HDR; |
| 1327 | return 1; |
| 1328 | } |
| 1329 | |
| 1330 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 1331 | * Accepts an optional argument of type string containing the header field name, |
| 1332 | * and an optional argument of type signed or unsigned integer to request an |
| 1333 | * explicit occurrence of the header. Note that in the event of a missing name, |
| 1334 | * headers are considered from the first one. |
| 1335 | */ |
| 1336 | static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1337 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1338 | /* possible keywords: req.hdr / hdr, res.hdr / shdr */ |
| 1339 | struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1340 | int occ = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1341 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1342 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1343 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1344 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1345 | struct http_hdr_ctx *ctx = smp->ctx.a[0]; |
| 1346 | struct ist name; |
| 1347 | |
| 1348 | if (!ctx) { |
| 1349 | /* first call */ |
| 1350 | ctx = &static_http_hdr_ctx; |
| 1351 | ctx->blk = NULL; |
| 1352 | smp->ctx.a[0] = ctx; |
| 1353 | } |
| 1354 | |
| 1355 | if (args) { |
| 1356 | if (args[0].type != ARGT_STR) |
| 1357 | return 0; |
| 1358 | name.ptr = args[0].data.str.area; |
| 1359 | name.len = args[0].data.str.data; |
| 1360 | |
| 1361 | if (args[1].type == ARGT_SINT) |
| 1362 | occ = args[1].data.sint; |
| 1363 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1364 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1365 | if (!htx) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1366 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1367 | |
| 1368 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 1369 | /* search for header from the beginning */ |
| 1370 | ctx->blk = NULL; |
| 1371 | |
| 1372 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 1373 | /* no explicit occurrence and single fetch => last header by default */ |
| 1374 | occ = -1; |
| 1375 | |
| 1376 | if (!occ) |
| 1377 | /* prepare to report multiple occurrences for ACL fetches */ |
| 1378 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1379 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1380 | smp->data.type = SMP_T_STR; |
| 1381 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
| 1382 | if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data)) |
| 1383 | return 1; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1384 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1385 | else { |
| 1386 | /* LEGACY version */ |
| 1387 | struct hdr_idx *idx; |
| 1388 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 1389 | const struct http_msg *msg; |
| 1390 | const char *name_str = NULL; |
| 1391 | int name_len = 0; |
| 1392 | |
| 1393 | if (!ctx) { |
| 1394 | /* first call */ |
| 1395 | ctx = &static_hdr_ctx; |
| 1396 | ctx->idx = 0; |
| 1397 | smp->ctx.a[0] = ctx; |
| 1398 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1399 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1400 | if (args) { |
| 1401 | if (args[0].type != ARGT_STR) |
| 1402 | return 0; |
| 1403 | name_str = args[0].data.str.area; |
| 1404 | name_len = args[0].data.str.data; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1405 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1406 | if (args[1].type == ARGT_SINT) |
| 1407 | occ = args[1].data.sint; |
| 1408 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1409 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1410 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1411 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1412 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1413 | msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1414 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1415 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 1416 | /* search for header from the beginning */ |
| 1417 | ctx->idx = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1418 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1419 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 1420 | /* no explicit occurrence and single fetch => last header by default */ |
| 1421 | occ = -1; |
| 1422 | |
| 1423 | if (!occ) |
| 1424 | /* prepare to report multiple occurrences for ACL fetches */ |
| 1425 | smp->flags |= SMP_F_NOT_LAST; |
| 1426 | |
| 1427 | smp->data.type = SMP_T_STR; |
| 1428 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
| 1429 | if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data)) |
| 1430 | return 1; |
| 1431 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1432 | |
| 1433 | smp->flags &= ~SMP_F_NOT_LAST; |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
Christopher Faulet | c1f40dd | 2019-05-16 10:07:30 +0200 | [diff] [blame] | 1437 | /* Same than smp_fetch_hdr() but only relies on the sample direction to choose |
| 1438 | * the right channel. So instead of duplicating the code, we just change the |
| 1439 | * keyword and then fallback on smp_fetch_hdr(). |
| 1440 | */ |
| 1441 | static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1442 | { |
| 1443 | kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr"); |
| 1444 | return smp_fetch_hdr(args, smp, kw, private); |
| 1445 | } |
| 1446 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1447 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 1448 | * Accepts exactly 1 argument of type string. |
| 1449 | */ |
| 1450 | static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1451 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1452 | /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */ |
| 1453 | struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1454 | int cnt; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1455 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1456 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1457 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1458 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1459 | struct http_hdr_ctx ctx; |
| 1460 | struct ist name; |
| 1461 | |
| 1462 | if (!htx) |
| 1463 | return 0; |
| 1464 | |
| 1465 | if (args && args->type == ARGT_STR) { |
| 1466 | name.ptr = args->data.str.area; |
| 1467 | name.len = args->data.str.data; |
Olivier Houchard | e2c78cd | 2018-11-21 13:49:48 +0100 | [diff] [blame] | 1468 | } else { |
| 1469 | name.ptr = NULL; |
| 1470 | name.len = 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1471 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1472 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1473 | ctx.blk = NULL; |
| 1474 | cnt = 0; |
| 1475 | while (http_find_header(htx, name, &ctx, 0)) |
| 1476 | cnt++; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1477 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1478 | else { |
| 1479 | /* LEGACY version */ |
| 1480 | struct hdr_idx *idx; |
| 1481 | struct hdr_ctx ctx; |
| 1482 | const struct http_msg *msg; |
| 1483 | const char *name = NULL; |
| 1484 | int len = 0; |
| 1485 | |
| 1486 | if (args && args->type == ARGT_STR) { |
| 1487 | name = args->data.str.area; |
| 1488 | len = args->data.str.data; |
| 1489 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1490 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1491 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1492 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1493 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1494 | msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1495 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1496 | ctx.idx = 0; |
| 1497 | cnt = 0; |
| 1498 | while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx)) |
| 1499 | cnt++; |
| 1500 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1501 | |
| 1502 | smp->data.type = SMP_T_SINT; |
| 1503 | smp->data.u.sint = cnt; |
| 1504 | smp->flags = SMP_F_VOL_HDR; |
| 1505 | return 1; |
| 1506 | } |
| 1507 | |
| 1508 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 1509 | * takes a mandatory argument of type string and an optional one of type int |
| 1510 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 1511 | * may or may not be appropriate for everything. |
| 1512 | */ |
| 1513 | static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1514 | { |
| 1515 | int ret = smp_fetch_hdr(args, smp, kw, private); |
| 1516 | |
| 1517 | if (ret > 0) { |
| 1518 | smp->data.type = SMP_T_SINT; |
| 1519 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 1520 | smp->data.u.str.data); |
| 1521 | } |
| 1522 | |
| 1523 | return ret; |
| 1524 | } |
| 1525 | |
| 1526 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 1527 | * and an optional one of type int to designate a specific occurrence. |
| 1528 | * It returns an IPv4 or IPv6 address. |
| 1529 | */ |
| 1530 | static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1531 | { |
| 1532 | int ret; |
| 1533 | |
| 1534 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
| 1535 | if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) { |
| 1536 | smp->data.type = SMP_T_IPV4; |
| 1537 | break; |
| 1538 | } else { |
| 1539 | struct buffer *temp = get_trash_chunk(); |
| 1540 | if (smp->data.u.str.data < temp->size - 1) { |
| 1541 | memcpy(temp->area, smp->data.u.str.area, |
| 1542 | smp->data.u.str.data); |
| 1543 | temp->area[smp->data.u.str.data] = '\0'; |
| 1544 | if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) { |
| 1545 | smp->data.type = SMP_T_IPV6; |
| 1546 | break; |
| 1547 | } |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | /* if the header doesn't match an IP address, fetch next one */ |
| 1552 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 1553 | return 0; |
| 1554 | } |
| 1555 | return ret; |
| 1556 | } |
| 1557 | |
| 1558 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 1559 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 1560 | */ |
| 1561 | static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1562 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1563 | struct channel *chn = SMP_REQ_CHN(smp); |
| 1564 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1565 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1566 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1567 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1568 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1569 | struct ist path; |
| 1570 | size_t len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1571 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1572 | if (!htx) |
| 1573 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1574 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1575 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1576 | path = http_get_path(htx_sl_req_uri(sl)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1577 | if (!path.ptr) |
| 1578 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1579 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1580 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++) |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1581 | ; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1582 | |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1583 | /* OK, we got the '/' ! */ |
| 1584 | smp->data.type = SMP_T_STR; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1585 | smp->data.u.str.area = path.ptr; |
| 1586 | smp->data.u.str.data = len; |
| 1587 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1588 | } |
| 1589 | else { |
| 1590 | struct http_txn *txn; |
| 1591 | char *ptr, *end; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1592 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1593 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1594 | |
| 1595 | txn = smp->strm->txn; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1596 | end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1597 | ptr = http_txn_get_path(txn); |
| 1598 | if (!ptr) |
| 1599 | return 0; |
| 1600 | |
| 1601 | /* OK, we got the '/' ! */ |
| 1602 | smp->data.type = SMP_T_STR; |
| 1603 | smp->data.u.str.area = ptr; |
| 1604 | |
| 1605 | while (ptr < end && *ptr != '?') |
| 1606 | ptr++; |
| 1607 | |
| 1608 | smp->data.u.str.data = ptr - smp->data.u.str.area; |
| 1609 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1610 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1611 | return 1; |
| 1612 | } |
| 1613 | |
| 1614 | /* This produces a concatenation of the first occurrence of the Host header |
| 1615 | * followed by the path component if it begins with a slash ('/'). This means |
| 1616 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 1617 | * If no Host header is found, then the path is returned as-is. The returned |
| 1618 | * value is stored in the trash so it does not need to be marked constant. |
| 1619 | * The returned sample is of type string. |
| 1620 | */ |
| 1621 | static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1622 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1623 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1624 | struct buffer *temp; |
| 1625 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1626 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1627 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1628 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1629 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1630 | struct http_hdr_ctx ctx; |
| 1631 | struct ist path; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1632 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1633 | if (!htx) |
| 1634 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1635 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1636 | ctx.blk = NULL; |
| 1637 | if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len) |
| 1638 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1639 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1640 | /* OK we have the header value in ctx.value */ |
| 1641 | temp = get_trash_chunk(); |
| 1642 | chunk_memcat(temp, ctx.value.ptr, ctx.value.len); |
| 1643 | |
| 1644 | /* now retrieve the path */ |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1645 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1646 | path = http_get_path(htx_sl_req_uri(sl)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1647 | if (path.ptr) { |
| 1648 | size_t len; |
| 1649 | |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1650 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++) |
| 1651 | ; |
| 1652 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1653 | if (len && *(path.ptr) == '/') |
| 1654 | chunk_memcat(temp, path.ptr, len); |
| 1655 | } |
| 1656 | |
| 1657 | smp->data.type = SMP_T_STR; |
| 1658 | smp->data.u.str = *temp; |
| 1659 | } |
| 1660 | else { |
| 1661 | /* LEGACY version */ |
| 1662 | struct http_txn *txn; |
| 1663 | char *ptr, *end, *beg; |
| 1664 | struct hdr_ctx ctx; |
| 1665 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1666 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1667 | |
| 1668 | txn = smp->strm->txn; |
| 1669 | ctx.idx = 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1670 | if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1671 | return smp_fetch_path(args, smp, kw, private); |
| 1672 | |
| 1673 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 1674 | temp = get_trash_chunk(); |
| 1675 | memcpy(temp->area, ctx.line + ctx.val, ctx.vlen); |
| 1676 | smp->data.type = SMP_T_STR; |
| 1677 | smp->data.u.str.area = temp->area; |
| 1678 | smp->data.u.str.data = ctx.vlen; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1679 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1680 | /* now retrieve the path */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1681 | end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1682 | beg = http_txn_get_path(txn); |
| 1683 | if (!beg) |
| 1684 | beg = end; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1685 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1686 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 1687 | |
| 1688 | if (beg < ptr && *beg == '/') { |
| 1689 | memcpy(smp->data.u.str.area + smp->data.u.str.data, beg, |
| 1690 | ptr - beg); |
| 1691 | smp->data.u.str.data += ptr - beg; |
| 1692 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | smp->flags = SMP_F_VOL_1ST; |
| 1696 | return 1; |
| 1697 | } |
| 1698 | |
| 1699 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 1700 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 1701 | * This means that '*' will not be added, resulting in exactly the first Host |
| 1702 | * entry. If no Host header is found, then the path is used. The resulting value |
| 1703 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 1704 | * 32-bit integer value. This fetch is useful for tracking per-path activity on |
| 1705 | * high-traffic sites without having to store whole paths. |
| 1706 | */ |
| 1707 | static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1708 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1709 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1710 | unsigned int hash = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1711 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1712 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1713 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1714 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1715 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1716 | struct http_hdr_ctx ctx; |
| 1717 | struct ist path; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1718 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1719 | if (!htx) |
| 1720 | return 0; |
| 1721 | |
| 1722 | ctx.blk = NULL; |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1723 | if (http_find_header(htx, ist("Host"), &ctx, 0)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1724 | /* OK we have the header value in ctx.value */ |
| 1725 | while (ctx.value.len--) |
| 1726 | hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash; |
| 1727 | } |
| 1728 | |
| 1729 | /* now retrieve the path */ |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1730 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1731 | path = http_get_path(htx_sl_req_uri(sl)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1732 | if (path.ptr) { |
| 1733 | size_t len; |
| 1734 | |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1735 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++) |
| 1736 | ; |
| 1737 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1738 | if (len && *(path.ptr) == '/') { |
| 1739 | while (len--) |
| 1740 | hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash; |
| 1741 | } |
| 1742 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1743 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1744 | else { |
| 1745 | /* LEGACY version */ |
| 1746 | struct http_txn *txn; |
| 1747 | struct hdr_ctx ctx; |
| 1748 | char *ptr, *beg, *end; |
| 1749 | int len; |
| 1750 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1751 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1752 | |
| 1753 | txn = smp->strm->txn; |
| 1754 | ctx.idx = 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1755 | if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1756 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 1757 | ptr = ctx.line + ctx.val; |
| 1758 | len = ctx.vlen; |
| 1759 | while (len--) |
| 1760 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 1761 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1762 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1763 | /* now retrieve the path */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1764 | end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1765 | beg = http_txn_get_path(txn); |
| 1766 | if (!beg) |
| 1767 | beg = end; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1768 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1769 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1770 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1771 | if (beg < ptr && *beg == '/') { |
| 1772 | while (beg < ptr) |
| 1773 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 1774 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1775 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1776 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1777 | hash = full_hash(hash); |
| 1778 | |
| 1779 | smp->data.type = SMP_T_SINT; |
| 1780 | smp->data.u.sint = hash; |
| 1781 | smp->flags = SMP_F_VOL_1ST; |
| 1782 | return 1; |
| 1783 | } |
| 1784 | |
| 1785 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 1786 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 1787 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 1788 | * on the source address length. The path hash is stored before the address so |
| 1789 | * that in environments where IPv6 is insignificant, truncating the output to |
| 1790 | * 8 bytes would still work. |
| 1791 | */ |
| 1792 | static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1793 | { |
| 1794 | struct buffer *temp; |
| 1795 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
| 1796 | |
| 1797 | if (!cli_conn) |
| 1798 | return 0; |
| 1799 | |
| 1800 | if (!smp_fetch_base32(args, smp, kw, private)) |
| 1801 | return 0; |
| 1802 | |
| 1803 | temp = get_trash_chunk(); |
| 1804 | *(unsigned int *) temp->area = htonl(smp->data.u.sint); |
| 1805 | temp->data += sizeof(unsigned int); |
| 1806 | |
| 1807 | switch (cli_conn->addr.from.ss_family) { |
| 1808 | case AF_INET: |
| 1809 | memcpy(temp->area + temp->data, |
| 1810 | &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, |
| 1811 | 4); |
| 1812 | temp->data += 4; |
| 1813 | break; |
| 1814 | case AF_INET6: |
| 1815 | memcpy(temp->area + temp->data, |
| 1816 | &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, |
| 1817 | 16); |
| 1818 | temp->data += 16; |
| 1819 | break; |
| 1820 | default: |
| 1821 | return 0; |
| 1822 | } |
| 1823 | |
| 1824 | smp->data.u.str = *temp; |
| 1825 | smp->data.type = SMP_T_BIN; |
| 1826 | return 1; |
| 1827 | } |
| 1828 | |
| 1829 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 1830 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 1831 | * of type string carrying the whole query string. |
| 1832 | */ |
| 1833 | static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1834 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1835 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1836 | char *ptr, *end; |
| 1837 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1838 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1839 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1840 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1841 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1842 | |
| 1843 | if (!htx) |
| 1844 | return 0; |
| 1845 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1846 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1847 | ptr = HTX_SL_REQ_UPTR(sl); |
| 1848 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1849 | } |
| 1850 | else { |
| 1851 | /* LEGACY version */ |
| 1852 | struct http_txn *txn; |
| 1853 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1854 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1855 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1856 | txn = smp->strm->txn; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1857 | ptr = ci_head(chn) + txn->req.sl.rq.u; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1858 | end = ptr + txn->req.sl.rq.u_l; |
| 1859 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1860 | |
| 1861 | /* look up the '?' */ |
| 1862 | do { |
| 1863 | if (ptr == end) |
| 1864 | return 0; |
| 1865 | } while (*ptr++ != '?'); |
| 1866 | |
| 1867 | smp->data.type = SMP_T_STR; |
| 1868 | smp->data.u.str.area = ptr; |
| 1869 | smp->data.u.str.data = end - ptr; |
| 1870 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1871 | return 1; |
| 1872 | } |
| 1873 | |
| 1874 | static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1875 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1876 | struct channel *chn = SMP_REQ_CHN(smp); |
| 1877 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1878 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1879 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1880 | struct htx *htx = smp_prefetch_htx(smp, chn, 0); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1881 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1882 | if (!htx) |
| 1883 | return 0; |
| 1884 | } |
| 1885 | else { |
| 1886 | /* LEGACY version */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1887 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1888 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 1889 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 1890 | */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1891 | CHECK_HTTP_MESSAGE_FIRST_PERM(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1892 | } |
| 1893 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1894 | smp->data.u.sint = 1; |
| 1895 | return 1; |
| 1896 | } |
| 1897 | |
| 1898 | /* return a valid test if the current request is the first one on the connection */ |
| 1899 | static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1900 | { |
| 1901 | smp->data.type = SMP_T_BOOL; |
| 1902 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
| 1903 | return 1; |
| 1904 | } |
| 1905 | |
| 1906 | /* Accepts exactly 1 argument of type userlist */ |
| 1907 | static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1908 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1909 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1910 | |
| 1911 | if (!args || args->type != ARGT_USR) |
| 1912 | return 0; |
| 1913 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1914 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1915 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1916 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1917 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1918 | if (!htx) |
| 1919 | return 0; |
Christopher Faulet | e98411b | 2019-07-15 13:58:29 +0200 | [diff] [blame] | 1920 | if (!get_http_auth(smp, htx)) |
| 1921 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1922 | } |
| 1923 | else { |
| 1924 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1925 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | e98411b | 2019-07-15 13:58:29 +0200 | [diff] [blame] | 1926 | if (!get_http_auth(smp, NULL)) |
| 1927 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1928 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1929 | |
| 1930 | smp->data.type = SMP_T_BOOL; |
| 1931 | smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user, |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1932 | smp->strm->txn->auth.pass); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1933 | return 1; |
| 1934 | } |
| 1935 | |
| 1936 | /* Accepts exactly 1 argument of type userlist */ |
| 1937 | static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1938 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1939 | struct channel *chn = SMP_REQ_CHN(smp); |
| 1940 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1941 | if (!args || args->type != ARGT_USR) |
| 1942 | return 0; |
| 1943 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1944 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1945 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1946 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1947 | |
| 1948 | if (!htx) |
| 1949 | return 0; |
Christopher Faulet | e98411b | 2019-07-15 13:58:29 +0200 | [diff] [blame] | 1950 | if (!get_http_auth(smp, htx)) |
| 1951 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1952 | } |
| 1953 | else { |
| 1954 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1955 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | e98411b | 2019-07-15 13:58:29 +0200 | [diff] [blame] | 1956 | if (!get_http_auth(smp, NULL)) |
| 1957 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1958 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1959 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1960 | /* if the user does not belong to the userlist or has a wrong password, |
| 1961 | * report that it unconditionally does not match. Otherwise we return |
| 1962 | * a string containing the username. |
| 1963 | */ |
| 1964 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 1965 | smp->strm->txn->auth.pass)) |
| 1966 | return 0; |
| 1967 | |
| 1968 | /* pat_match_auth() will need the user list */ |
| 1969 | smp->ctx.a[0] = args->data.usr; |
| 1970 | |
| 1971 | smp->data.type = SMP_T_STR; |
| 1972 | smp->flags = SMP_F_CONST; |
| 1973 | smp->data.u.str.area = smp->strm->txn->auth.user; |
| 1974 | smp->data.u.str.data = strlen(smp->strm->txn->auth.user); |
| 1975 | |
| 1976 | return 1; |
| 1977 | } |
| 1978 | |
| 1979 | /* Fetch a captured HTTP request header. The index is the position of |
| 1980 | * the "capture" option in the configuration file |
| 1981 | */ |
| 1982 | static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1983 | { |
| 1984 | struct proxy *fe = strm_fe(smp->strm); |
| 1985 | int idx; |
| 1986 | |
| 1987 | if (!args || args->type != ARGT_SINT) |
| 1988 | return 0; |
| 1989 | |
| 1990 | idx = args->data.sint; |
| 1991 | |
| 1992 | if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL) |
| 1993 | return 0; |
| 1994 | |
| 1995 | smp->data.type = SMP_T_STR; |
| 1996 | smp->flags |= SMP_F_CONST; |
| 1997 | smp->data.u.str.area = smp->strm->req_cap[idx]; |
| 1998 | smp->data.u.str.data = strlen(smp->strm->req_cap[idx]); |
| 1999 | |
| 2000 | return 1; |
| 2001 | } |
| 2002 | |
| 2003 | /* Fetch a captured HTTP response header. The index is the position of |
| 2004 | * the "capture" option in the configuration file |
| 2005 | */ |
| 2006 | static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2007 | { |
| 2008 | struct proxy *fe = strm_fe(smp->strm); |
| 2009 | int idx; |
| 2010 | |
| 2011 | if (!args || args->type != ARGT_SINT) |
| 2012 | return 0; |
| 2013 | |
| 2014 | idx = args->data.sint; |
| 2015 | |
| 2016 | if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL) |
| 2017 | return 0; |
| 2018 | |
| 2019 | smp->data.type = SMP_T_STR; |
| 2020 | smp->flags |= SMP_F_CONST; |
| 2021 | smp->data.u.str.area = smp->strm->res_cap[idx]; |
| 2022 | smp->data.u.str.data = strlen(smp->strm->res_cap[idx]); |
| 2023 | |
| 2024 | return 1; |
| 2025 | } |
| 2026 | |
| 2027 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 2028 | static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2029 | { |
| 2030 | struct buffer *temp; |
| 2031 | struct http_txn *txn = smp->strm->txn; |
| 2032 | char *ptr; |
| 2033 | |
| 2034 | if (!txn || !txn->uri) |
| 2035 | return 0; |
| 2036 | |
| 2037 | ptr = txn->uri; |
| 2038 | |
| 2039 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 2040 | ptr++; |
| 2041 | |
| 2042 | temp = get_trash_chunk(); |
| 2043 | temp->area = txn->uri; |
| 2044 | temp->data = ptr - txn->uri; |
| 2045 | smp->data.u.str = *temp; |
| 2046 | smp->data.type = SMP_T_STR; |
| 2047 | smp->flags = SMP_F_CONST; |
| 2048 | |
| 2049 | return 1; |
| 2050 | |
| 2051 | } |
| 2052 | |
| 2053 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 2054 | static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2055 | { |
| 2056 | struct http_txn *txn = smp->strm->txn; |
| 2057 | struct ist path; |
| 2058 | const char *ptr; |
| 2059 | |
| 2060 | if (!txn || !txn->uri) |
| 2061 | return 0; |
| 2062 | |
| 2063 | ptr = txn->uri; |
| 2064 | |
| 2065 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 2066 | ptr++; |
| 2067 | |
| 2068 | if (!*ptr) |
| 2069 | return 0; |
| 2070 | |
Christopher Faulet | 78337bb | 2018-11-15 14:35:18 +0100 | [diff] [blame] | 2071 | /* skip the first space and find space after URI */ |
| 2072 | path = ist2(++ptr, 0); |
| 2073 | while (*ptr != ' ' && *ptr != '\0') |
| 2074 | ptr++; |
| 2075 | path.len = ptr - path.ptr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2076 | |
Christopher Faulet | 78337bb | 2018-11-15 14:35:18 +0100 | [diff] [blame] | 2077 | path = http_get_path(path); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2078 | if (!path.ptr) |
| 2079 | return 0; |
| 2080 | |
| 2081 | smp->data.u.str.area = path.ptr; |
| 2082 | smp->data.u.str.data = path.len; |
| 2083 | smp->data.type = SMP_T_STR; |
| 2084 | smp->flags = SMP_F_CONST; |
| 2085 | |
| 2086 | return 1; |
| 2087 | } |
| 2088 | |
| 2089 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 2090 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 2091 | */ |
| 2092 | static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2093 | { |
| 2094 | struct http_txn *txn = smp->strm->txn; |
| 2095 | |
| 2096 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
| 2097 | return 0; |
| 2098 | |
| 2099 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 2100 | smp->data.u.str.area = "HTTP/1.1"; |
| 2101 | else |
| 2102 | smp->data.u.str.area = "HTTP/1.0"; |
| 2103 | |
| 2104 | smp->data.u.str.data = 8; |
| 2105 | smp->data.type = SMP_T_STR; |
| 2106 | smp->flags = SMP_F_CONST; |
| 2107 | return 1; |
| 2108 | |
| 2109 | } |
| 2110 | |
| 2111 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 2112 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 2113 | */ |
| 2114 | static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2115 | { |
| 2116 | struct http_txn *txn = smp->strm->txn; |
| 2117 | |
| 2118 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
| 2119 | return 0; |
| 2120 | |
| 2121 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 2122 | smp->data.u.str.area = "HTTP/1.1"; |
| 2123 | else |
| 2124 | smp->data.u.str.area = "HTTP/1.0"; |
| 2125 | |
| 2126 | smp->data.u.str.data = 8; |
| 2127 | smp->data.type = SMP_T_STR; |
| 2128 | smp->flags = SMP_F_CONST; |
| 2129 | return 1; |
| 2130 | |
| 2131 | } |
| 2132 | |
| 2133 | /* Iterate over all cookies present in a message. The context is stored in |
| 2134 | * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the |
| 2135 | * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on |
| 2136 | * the direction, multiple cookies may be parsed on the same line or not. |
| 2137 | * The cookie name is in args and the name length in args->data.str.len. |
| 2138 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 2139 | * that no iterating is desired, then only last value is fetched if any. |
| 2140 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 2141 | * files. |
| 2142 | */ |
| 2143 | static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2144 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2145 | /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */ |
| 2146 | struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2147 | int occ = 0; |
| 2148 | int found = 0; |
| 2149 | |
| 2150 | if (!args || args->type != ARGT_STR) |
| 2151 | return 0; |
| 2152 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2153 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2154 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2155 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2156 | struct http_hdr_ctx *ctx = smp->ctx.a[2]; |
| 2157 | struct ist hdr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2158 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2159 | if (!ctx) { |
| 2160 | /* first call */ |
| 2161 | ctx = &static_http_hdr_ctx; |
| 2162 | ctx->blk = NULL; |
| 2163 | smp->ctx.a[2] = ctx; |
| 2164 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2165 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2166 | if (!htx) |
| 2167 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2168 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2169 | hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie")); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2170 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2171 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 2172 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 2173 | occ = -1; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2174 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2175 | /* OK so basically here, either we want only one value and it's the |
| 2176 | * last one, or we want to iterate over all of them and we fetch the |
| 2177 | * next one. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2178 | */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2179 | |
| 2180 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
| 2181 | /* search for the header from the beginning, we must first initialize |
| 2182 | * the search parameters. |
| 2183 | */ |
| 2184 | smp->ctx.a[0] = NULL; |
| 2185 | ctx->blk = NULL; |
| 2186 | } |
| 2187 | |
| 2188 | smp->flags |= SMP_F_VOL_HDR; |
| 2189 | while (1) { |
| 2190 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 2191 | if (!smp->ctx.a[0]) { |
| 2192 | if (!http_find_header(htx, hdr, ctx, 0)) |
| 2193 | goto out; |
| 2194 | |
| 2195 | if (ctx->value.len < args->data.str.data + 1) |
| 2196 | continue; |
| 2197 | |
| 2198 | smp->ctx.a[0] = ctx->value.ptr; |
| 2199 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len; |
| 2200 | } |
| 2201 | |
| 2202 | smp->data.type = SMP_T_STR; |
| 2203 | smp->flags |= SMP_F_CONST; |
| 2204 | smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1], |
| 2205 | args->data.str.area, args->data.str.data, |
| 2206 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 2207 | &smp->data.u.str.area, |
| 2208 | &smp->data.u.str.data); |
| 2209 | if (smp->ctx.a[0]) { |
| 2210 | found = 1; |
| 2211 | if (occ >= 0) { |
| 2212 | /* one value was returned into smp->data.u.str.{str,len} */ |
| 2213 | smp->flags |= SMP_F_NOT_LAST; |
| 2214 | return 1; |
| 2215 | } |
| 2216 | } |
| 2217 | /* if we're looking for last occurrence, let's loop */ |
| 2218 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2219 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2220 | else { |
| 2221 | /* LEGACY version */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2222 | struct hdr_idx *idx; |
| 2223 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2224 | const char *hdr_name; |
| 2225 | int hdr_name_len; |
| 2226 | char *sol; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2227 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2228 | if (!ctx) { |
| 2229 | /* first call */ |
| 2230 | ctx = &static_hdr_ctx; |
| 2231 | ctx->idx = 0; |
| 2232 | smp->ctx.a[2] = ctx; |
| 2233 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2234 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2235 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2236 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2237 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2238 | if (!(chn->flags & CF_ISRESP)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2239 | hdr_name = "Cookie"; |
| 2240 | hdr_name_len = 6; |
| 2241 | } else { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2242 | hdr_name = "Set-Cookie"; |
| 2243 | hdr_name_len = 10; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2244 | } |
| 2245 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2246 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 2247 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 2248 | occ = -1; |
| 2249 | |
| 2250 | /* OK so basically here, either we want only one value and it's the |
| 2251 | * last one, or we want to iterate over all of them and we fetch the |
| 2252 | * next one. |
| 2253 | */ |
| 2254 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2255 | sol = ci_head(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2256 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
| 2257 | /* search for the header from the beginning, we must first initialize |
| 2258 | * the search parameters. |
| 2259 | */ |
| 2260 | smp->ctx.a[0] = NULL; |
| 2261 | ctx->idx = 0; |
| 2262 | } |
| 2263 | |
| 2264 | smp->flags |= SMP_F_VOL_HDR; |
| 2265 | |
| 2266 | while (1) { |
| 2267 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 2268 | if (!smp->ctx.a[0]) { |
| 2269 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 2270 | goto out; |
| 2271 | |
| 2272 | if (ctx->vlen < args->data.str.data + 1) |
| 2273 | continue; |
| 2274 | |
| 2275 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 2276 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
| 2277 | } |
| 2278 | |
| 2279 | smp->data.type = SMP_T_STR; |
| 2280 | smp->flags |= SMP_F_CONST; |
| 2281 | smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1], |
| 2282 | args->data.str.area, args->data.str.data, |
| 2283 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 2284 | &smp->data.u.str.area, &smp->data.u.str.data); |
| 2285 | if (smp->ctx.a[0]) { |
| 2286 | found = 1; |
| 2287 | if (occ >= 0) { |
| 2288 | /* one value was returned into smp->data.u.str.{str,len} */ |
| 2289 | smp->flags |= SMP_F_NOT_LAST; |
| 2290 | return 1; |
| 2291 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2292 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2293 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2294 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2295 | } |
| 2296 | /* all cookie headers and values were scanned. If we're looking for the |
| 2297 | * last occurrence, we may return it now. |
| 2298 | */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2299 | out: |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2300 | smp->flags &= ~SMP_F_NOT_LAST; |
| 2301 | return found; |
| 2302 | } |
| 2303 | |
Christopher Faulet | c1f40dd | 2019-05-16 10:07:30 +0200 | [diff] [blame] | 2304 | /* Same than smp_fetch_cookie() but only relies on the sample direction to |
| 2305 | * choose the right channel. So instead of duplicating the code, we just change |
| 2306 | * the keyword and then fallback on smp_fetch_cookie(). |
| 2307 | */ |
| 2308 | static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2309 | { |
| 2310 | kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook"); |
| 2311 | return smp_fetch_cookie(args, smp, kw, private); |
| 2312 | } |
| 2313 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2314 | /* Iterate over all cookies present in a request to count how many occurrences |
| 2315 | * match the name in args and args->data.str.len. If <multi> is non-null, then |
| 2316 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 2317 | * type UINT. Accepts exactly 1 argument of type string. |
| 2318 | */ |
| 2319 | static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2320 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2321 | /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */ |
| 2322 | struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2323 | char *val_beg, *val_end; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2324 | int cnt; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2325 | |
| 2326 | if (!args || args->type != ARGT_STR) |
| 2327 | return 0; |
| 2328 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2329 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2330 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2331 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2332 | struct http_hdr_ctx ctx; |
| 2333 | struct ist hdr; |
| 2334 | |
| 2335 | if (!htx) |
| 2336 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2337 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2338 | hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie")); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2339 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2340 | val_end = val_beg = NULL; |
| 2341 | ctx.blk = NULL; |
| 2342 | cnt = 0; |
| 2343 | while (1) { |
| 2344 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 2345 | if (!val_beg) { |
| 2346 | if (!http_find_header(htx, hdr, &ctx, 0)) |
| 2347 | break; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2348 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2349 | if (ctx.value.len < args->data.str.data + 1) |
| 2350 | continue; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2351 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2352 | val_beg = ctx.value.ptr; |
| 2353 | val_end = val_beg + ctx.value.len; |
| 2354 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2355 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2356 | smp->data.type = SMP_T_STR; |
| 2357 | smp->flags |= SMP_F_CONST; |
| 2358 | while ((val_beg = http_extract_cookie_value(val_beg, val_end, |
| 2359 | args->data.str.area, args->data.str.data, |
| 2360 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 2361 | &smp->data.u.str.area, |
| 2362 | &smp->data.u.str.data))) { |
| 2363 | cnt++; |
| 2364 | } |
| 2365 | } |
| 2366 | } |
| 2367 | else { |
| 2368 | /* LEGACY version */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2369 | struct hdr_idx *idx; |
| 2370 | struct hdr_ctx ctx; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2371 | const char *hdr_name; |
| 2372 | int hdr_name_len; |
| 2373 | char *sol; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2374 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2375 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2376 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2377 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2378 | if (!(chn->flags & CF_ISRESP)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2379 | hdr_name = "Cookie"; |
| 2380 | hdr_name_len = 6; |
| 2381 | } else { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2382 | hdr_name = "Set-Cookie"; |
| 2383 | hdr_name_len = 10; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2384 | } |
| 2385 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2386 | sol = ci_head(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2387 | val_end = val_beg = NULL; |
| 2388 | ctx.idx = 0; |
| 2389 | cnt = 0; |
| 2390 | |
| 2391 | while (1) { |
| 2392 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 2393 | if (!val_beg) { |
| 2394 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 2395 | break; |
| 2396 | |
| 2397 | if (ctx.vlen < args->data.str.data + 1) |
| 2398 | continue; |
| 2399 | |
| 2400 | val_beg = ctx.line + ctx.val; |
| 2401 | val_end = val_beg + ctx.vlen; |
| 2402 | } |
| 2403 | |
| 2404 | smp->data.type = SMP_T_STR; |
| 2405 | smp->flags |= SMP_F_CONST; |
| 2406 | while ((val_beg = http_extract_cookie_value(val_beg, val_end, |
| 2407 | args->data.str.area, args->data.str.data, |
| 2408 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 2409 | &smp->data.u.str.area, &smp->data.u.str.data))) { |
| 2410 | cnt++; |
| 2411 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | smp->data.type = SMP_T_SINT; |
| 2416 | smp->data.u.sint = cnt; |
| 2417 | smp->flags |= SMP_F_VOL_HDR; |
| 2418 | return 1; |
| 2419 | } |
| 2420 | |
| 2421 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 2422 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 2423 | */ |
| 2424 | static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2425 | { |
| 2426 | int ret = smp_fetch_cookie(args, smp, kw, private); |
| 2427 | |
| 2428 | if (ret > 0) { |
| 2429 | smp->data.type = SMP_T_SINT; |
| 2430 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 2431 | smp->data.u.str.data); |
| 2432 | } |
| 2433 | |
| 2434 | return ret; |
| 2435 | } |
| 2436 | |
| 2437 | /************************************************************************/ |
| 2438 | /* The code below is dedicated to sample fetches */ |
| 2439 | /************************************************************************/ |
| 2440 | |
| 2441 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 2442 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 2443 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 2444 | * pointers are updated for next iteration before leaving. |
| 2445 | */ |
| 2446 | static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2447 | { |
| 2448 | const char *vstart, *vend; |
| 2449 | struct buffer *temp; |
| 2450 | const char **chunks = (const char **)smp->ctx.a; |
| 2451 | |
| 2452 | if (!http_find_next_url_param(chunks, name, name_len, |
| 2453 | &vstart, &vend, delim)) |
| 2454 | return 0; |
| 2455 | |
| 2456 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 2457 | * if the value is wrapped, copy-it in a buffer. |
| 2458 | */ |
| 2459 | smp->data.type = SMP_T_STR; |
| 2460 | if (chunks[2] && |
| 2461 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 2462 | vend >= chunks[2] && vend <= chunks[3]) { |
| 2463 | /* Wrapped case. */ |
| 2464 | temp = get_trash_chunk(); |
| 2465 | memcpy(temp->area, vstart, chunks[1] - vstart); |
| 2466 | memcpy(temp->area + ( chunks[1] - vstart ), chunks[2], |
| 2467 | vend - chunks[2]); |
| 2468 | smp->data.u.str.area = temp->area; |
| 2469 | smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
| 2470 | } else { |
| 2471 | /* Contiguous case. */ |
| 2472 | smp->data.u.str.area = (char *)vstart; |
| 2473 | smp->data.u.str.data = vend - vstart; |
| 2474 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 2475 | } |
| 2476 | |
| 2477 | /* Update context, check wrapping. */ |
| 2478 | chunks[0] = vend; |
| 2479 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 2480 | chunks[1] = chunks[3]; |
| 2481 | chunks[2] = NULL; |
| 2482 | } |
| 2483 | |
| 2484 | if (chunks[0] < chunks[1]) |
| 2485 | smp->flags |= SMP_F_NOT_LAST; |
| 2486 | |
| 2487 | return 1; |
| 2488 | } |
| 2489 | |
| 2490 | /* This function iterates over each parameter of the query string. It uses |
| 2491 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the current |
| 2492 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 2493 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 2494 | * considered. It supports an optional delimiter argument for the beginning of |
| 2495 | * the string in args[1], which defaults to "?". |
| 2496 | */ |
| 2497 | static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2498 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2499 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2500 | char delim = '?'; |
| 2501 | const char *name; |
| 2502 | int name_len; |
| 2503 | |
| 2504 | if (!args || |
| 2505 | (args[0].type && args[0].type != ARGT_STR) || |
| 2506 | (args[1].type && args[1].type != ARGT_STR)) |
| 2507 | return 0; |
| 2508 | |
| 2509 | name = ""; |
| 2510 | name_len = 0; |
| 2511 | if (args->type == ARGT_STR) { |
| 2512 | name = args->data.str.area; |
| 2513 | name_len = args->data.str.data; |
| 2514 | } |
| 2515 | |
| 2516 | if (args[1].type) |
| 2517 | delim = *args[1].data.str.area; |
| 2518 | |
| 2519 | if (!smp->ctx.a[0]) { // first call, find the query string |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2520 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2521 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2522 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2523 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2524 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2525 | if (!htx) |
| 2526 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2527 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2528 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2529 | smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2530 | if (!smp->ctx.a[0]) |
| 2531 | return 0; |
| 2532 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2533 | smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2534 | } |
| 2535 | else { |
| 2536 | /* LEGACY version */ |
| 2537 | struct http_msg *msg; |
| 2538 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2539 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2540 | |
| 2541 | msg = &smp->strm->txn->req; |
| 2542 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2543 | smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u, |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2544 | msg->sl.rq.u_l, delim); |
| 2545 | if (!smp->ctx.a[0]) |
| 2546 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2547 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2548 | smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2549 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2550 | |
| 2551 | /* Assume that the context is filled with NULL pointer |
| 2552 | * before the first call. |
| 2553 | * smp->ctx.a[2] = NULL; |
| 2554 | * smp->ctx.a[3] = NULL; |
| 2555 | */ |
| 2556 | } |
| 2557 | |
| 2558 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 2559 | } |
| 2560 | |
| 2561 | /* This function iterates over each parameter of the body. This requires |
| 2562 | * that the body has been waited for using http-buffer-request. It uses |
| 2563 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 2564 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 2565 | * optional second part if the body wraps at the end of the buffer. An optional |
| 2566 | * parameter name is passed in args[0], otherwise any parameter is considered. |
| 2567 | */ |
| 2568 | static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2569 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2570 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2571 | const char *name; |
| 2572 | int name_len; |
| 2573 | |
| 2574 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 2575 | return 0; |
| 2576 | |
| 2577 | name = ""; |
| 2578 | name_len = 0; |
| 2579 | if (args[0].type == ARGT_STR) { |
| 2580 | name = args[0].data.str.area; |
| 2581 | name_len = args[0].data.str.data; |
| 2582 | } |
| 2583 | |
| 2584 | if (!smp->ctx.a[0]) { // first call, find the query string |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2585 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2586 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2587 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2588 | struct buffer *temp; |
| 2589 | int32_t pos; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2590 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2591 | if (!htx) |
| 2592 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2593 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2594 | temp = get_trash_chunk(); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 2595 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2596 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 2597 | enum htx_blk_type type = htx_get_blk_type(blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2598 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 2599 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2600 | break; |
| 2601 | if (type == HTX_BLK_DATA) { |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 2602 | if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0)) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2603 | return 0; |
| 2604 | } |
| 2605 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2606 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2607 | smp->ctx.a[0] = temp->area; |
| 2608 | smp->ctx.a[1] = temp->area + temp->data; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2609 | |
| 2610 | /* Assume that the context is filled with NULL pointer |
| 2611 | * before the first call. |
| 2612 | * smp->ctx.a[2] = NULL; |
| 2613 | * smp->ctx.a[3] = NULL; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2614 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2615 | } |
| 2616 | else { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2617 | /* LEGACY version */ |
| 2618 | struct http_msg *msg; |
| 2619 | unsigned long len; |
| 2620 | unsigned long block1; |
| 2621 | char *body; |
| 2622 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2623 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2624 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2625 | msg = &smp->strm->txn->req; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2626 | len = http_body_bytes(msg); |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2627 | body = c_ptr(chn, -http_data_rewind(msg)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2628 | |
| 2629 | block1 = len; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2630 | if (block1 > b_wrap(&chn->buf) - body) |
| 2631 | block1 = b_wrap(&chn->buf) - body; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2632 | |
| 2633 | if (block1 == len) { |
| 2634 | /* buffer is not wrapped (or empty) */ |
| 2635 | smp->ctx.a[0] = body; |
| 2636 | smp->ctx.a[1] = body + len; |
| 2637 | |
| 2638 | /* Assume that the context is filled with NULL pointer |
| 2639 | * before the first call. |
| 2640 | * smp->ctx.a[2] = NULL; |
| 2641 | * smp->ctx.a[3] = NULL; |
| 2642 | */ |
| 2643 | } |
| 2644 | else { |
| 2645 | /* buffer is wrapped, we need to defragment it */ |
| 2646 | smp->ctx.a[0] = body; |
| 2647 | smp->ctx.a[1] = body + block1; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2648 | smp->ctx.a[2] = b_orig(&chn->buf); |
| 2649 | smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 ); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2650 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2651 | } |
| 2652 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2653 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2654 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 2655 | } |
| 2656 | |
| 2657 | /* Return the signed integer value for the specified url parameter (see url_param |
| 2658 | * above). |
| 2659 | */ |
| 2660 | static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2661 | { |
| 2662 | int ret = smp_fetch_url_param(args, smp, kw, private); |
| 2663 | |
| 2664 | if (ret > 0) { |
| 2665 | smp->data.type = SMP_T_SINT; |
| 2666 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 2667 | smp->data.u.str.data); |
| 2668 | } |
| 2669 | |
| 2670 | return ret; |
| 2671 | } |
| 2672 | |
| 2673 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 2674 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 2675 | * This means that '*' will not be added, resulting in exactly the first Host |
| 2676 | * entry. If no Host header is found, then the path is used. The resulting value |
| 2677 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 2678 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 2679 | * high-traffic sites without having to store whole paths. |
| 2680 | * this differs from the base32 functions in that it includes the url parameters |
| 2681 | * as well as the path |
| 2682 | */ |
| 2683 | static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2684 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2685 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2686 | unsigned int hash = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2687 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2688 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2689 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2690 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2691 | struct http_hdr_ctx ctx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2692 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2693 | struct ist path; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2694 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2695 | if (!htx) |
| 2696 | return 0; |
| 2697 | |
| 2698 | ctx.blk = NULL; |
| 2699 | if (http_find_header(htx, ist("Host"), &ctx, 1)) { |
| 2700 | /* OK we have the header value in ctx.value */ |
| 2701 | while (ctx.value.len--) |
| 2702 | hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash; |
| 2703 | } |
| 2704 | |
| 2705 | /* now retrieve the path */ |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2706 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2707 | path = http_get_path(htx_sl_req_uri(sl)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2708 | while (path.len > 0 && *(path.ptr) != '?') { |
| 2709 | path.ptr++; |
| 2710 | path.len--; |
| 2711 | } |
| 2712 | if (path.len && *(path.ptr) == '/') { |
| 2713 | while (path.len--) |
| 2714 | hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash; |
| 2715 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2716 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2717 | else { |
| 2718 | /* LEGACY version */ |
| 2719 | struct http_txn *txn; |
| 2720 | struct hdr_ctx ctx; |
| 2721 | char *ptr, *beg, *end; |
| 2722 | int len; |
| 2723 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2724 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2725 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2726 | txn = smp->strm->txn; |
| 2727 | ctx.idx = 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2728 | if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2729 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 2730 | ptr = ctx.line + ctx.val; |
| 2731 | len = ctx.vlen; |
| 2732 | while (len--) |
| 2733 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 2734 | } |
| 2735 | |
| 2736 | /* now retrieve the path */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2737 | end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2738 | beg = http_txn_get_path(txn); |
| 2739 | if (!beg) |
| 2740 | beg = end; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2741 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2742 | for (ptr = beg; ptr < end ; ptr++); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2743 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2744 | if (beg < ptr && *beg == '/') { |
| 2745 | while (beg < ptr) |
| 2746 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 2747 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2748 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2749 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2750 | hash = full_hash(hash); |
| 2751 | |
| 2752 | smp->data.type = SMP_T_SINT; |
| 2753 | smp->data.u.sint = hash; |
| 2754 | smp->flags = SMP_F_VOL_1ST; |
| 2755 | return 1; |
| 2756 | } |
| 2757 | |
| 2758 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 2759 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 2760 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 2761 | * on the source address length. The URL hash is stored before the address so |
| 2762 | * that in environments where IPv6 is insignificant, truncating the output to |
| 2763 | * 8 bytes would still work. |
| 2764 | */ |
| 2765 | static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2766 | { |
| 2767 | struct buffer *temp; |
| 2768 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
| 2769 | |
| 2770 | if (!cli_conn) |
| 2771 | return 0; |
| 2772 | |
| 2773 | if (!smp_fetch_url32(args, smp, kw, private)) |
| 2774 | return 0; |
| 2775 | |
| 2776 | temp = get_trash_chunk(); |
| 2777 | *(unsigned int *) temp->area = htonl(smp->data.u.sint); |
| 2778 | temp->data += sizeof(unsigned int); |
| 2779 | |
| 2780 | switch (cli_conn->addr.from.ss_family) { |
| 2781 | case AF_INET: |
| 2782 | memcpy(temp->area + temp->data, |
| 2783 | &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, |
| 2784 | 4); |
| 2785 | temp->data += 4; |
| 2786 | break; |
| 2787 | case AF_INET6: |
| 2788 | memcpy(temp->area + temp->data, |
| 2789 | &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, |
| 2790 | 16); |
| 2791 | temp->data += 16; |
| 2792 | break; |
| 2793 | default: |
| 2794 | return 0; |
| 2795 | } |
| 2796 | |
| 2797 | smp->data.u.str = *temp; |
| 2798 | smp->data.type = SMP_T_BIN; |
| 2799 | return 1; |
| 2800 | } |
| 2801 | |
| 2802 | /************************************************************************/ |
| 2803 | /* Other utility functions */ |
| 2804 | /************************************************************************/ |
| 2805 | |
| 2806 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 2807 | * keyword. These keywords support an optional positive or negative occurrence |
| 2808 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 2809 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 2810 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 2811 | * error message in case of error, that the caller is responsible for freeing. |
| 2812 | * The initial location must either be freeable or NULL. |
| 2813 | * Note: this function's pointer is checked from Lua. |
| 2814 | */ |
| 2815 | int val_hdr(struct arg *arg, char **err_msg) |
| 2816 | { |
| 2817 | if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) { |
| 2818 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
| 2819 | return 0; |
| 2820 | } |
| 2821 | return 1; |
| 2822 | } |
| 2823 | |
| 2824 | /************************************************************************/ |
| 2825 | /* All supported sample fetch keywords must be declared here. */ |
| 2826 | /************************************************************************/ |
| 2827 | |
| 2828 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 2829 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 2830 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2831 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2832 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2833 | |
| 2834 | /* capture are allocated and are permanent in the stream */ |
| 2835 | { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2836 | |
| 2837 | /* retrieve these captures from the HTTP logs */ |
| 2838 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2839 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2840 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2841 | |
| 2842 | { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 2843 | { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2844 | |
| 2845 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 2846 | * are only here to match the ACL's name, are request-only and are used |
| 2847 | * for ACL compatibility only. |
| 2848 | */ |
| 2849 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Christopher Faulet | c1f40dd | 2019-05-16 10:07:30 +0200 | [diff] [blame] | 2850 | { "cookie", smp_fetch_chn_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2851 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2852 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2853 | |
| 2854 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 2855 | * only here to match the ACL's name, are request-only and are used for |
| 2856 | * ACL compatibility only. |
| 2857 | */ |
Christopher Faulet | c1f40dd | 2019-05-16 10:07:30 +0200 | [diff] [blame] | 2858 | { "hdr", smp_fetch_chn_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2859 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2860 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2861 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2862 | |
| 2863 | { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
| 2864 | { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2865 | { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2866 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
| 2867 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2868 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2869 | |
| 2870 | /* HTTP protocol on the request path */ |
| 2871 | { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2872 | { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2873 | |
| 2874 | /* HTTP version on the request path */ |
| 2875 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2876 | { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2877 | |
| 2878 | { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2879 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2880 | { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2881 | { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2882 | |
| 2883 | { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2884 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2885 | |
| 2886 | /* HTTP version on the response path */ |
| 2887 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2888 | { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2889 | |
| 2890 | /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */ |
| 2891 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2892 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2893 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2894 | |
| 2895 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
| 2896 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2897 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
| 2898 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2899 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2900 | { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2901 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2902 | |
| 2903 | /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */ |
| 2904 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2905 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2906 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2907 | |
| 2908 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2909 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2910 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2911 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2912 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 2913 | { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2914 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2915 | |
| 2916 | /* scook is valid only on the response and is used for ACL compatibility */ |
| 2917 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2918 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2919 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2920 | { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */ |
| 2921 | |
| 2922 | /* shdr is valid only on the response and is used for ACL compatibility */ |
| 2923 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2924 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2925 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 2926 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2927 | |
| 2928 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
| 2929 | { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV }, |
| 2930 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2931 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2932 | { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2933 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2934 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2935 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2936 | { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2937 | { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2938 | { /* END */ }, |
| 2939 | }}; |
| 2940 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2941 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2942 | |
| 2943 | /* |
| 2944 | * Local variables: |
| 2945 | * c-indent-level: 8 |
| 2946 | * c-basic-offset: 8 |
| 2947 | * End: |
| 2948 | */ |