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