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