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