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