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