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