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 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 19 | #include <haproxy/api.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 20 | #include <haproxy/arg.h> |
Willy Tarreau | ac13aea | 2020-06-04 10:36:03 +0200 | [diff] [blame] | 21 | #include <haproxy/auth.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 22 | #include <haproxy/base64.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 23 | #include <haproxy/channel.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 24 | #include <haproxy/chunk.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 25 | #include <haproxy/connection.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 26 | #include <haproxy/global.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 27 | #include <haproxy/h1.h> |
Willy Tarreau | c6fe884 | 2020-06-04 09:00:02 +0200 | [diff] [blame] | 28 | #include <haproxy/h1_htx.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 29 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 30 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 31 | #include <haproxy/http_fetch.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 32 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 33 | #include <haproxy/htx.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 34 | #include <haproxy/obj_type.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 35 | #include <haproxy/pool.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 36 | #include <haproxy/sample.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 37 | #include <haproxy/stream.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 38 | #include <haproxy/tools.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 39 | #include <haproxy/version.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 40 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 41 | |
| 42 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 43 | static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx; |
Richard Russo | 458eafb | 2019-07-31 11:45:56 -0700 | [diff] [blame] | 44 | /* this is used to convert raw connection buffers to htx */ |
| 45 | static THREAD_LOCAL struct buffer static_raw_htx_chunk; |
| 46 | static THREAD_LOCAL char *static_raw_htx_buf; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 47 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 48 | #define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL) |
| 49 | #define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 50 | |
Richard Russo | 458eafb | 2019-07-31 11:45:56 -0700 | [diff] [blame] | 51 | /* This function returns the static htx chunk, where raw connections get |
| 52 | * converted to HTX as needed for samplxsing. |
| 53 | */ |
| 54 | struct buffer *get_raw_htx_chunk(void) |
| 55 | { |
| 56 | chunk_reset(&static_raw_htx_chunk); |
| 57 | return &static_raw_htx_chunk; |
| 58 | } |
| 59 | |
| 60 | static int alloc_raw_htx_chunk_per_thread() |
| 61 | { |
| 62 | static_raw_htx_buf = malloc(global.tune.bufsize); |
| 63 | if (!static_raw_htx_buf) |
| 64 | return 0; |
| 65 | chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize); |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | static void free_raw_htx_chunk_per_thread() |
| 70 | { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 71 | ha_free(&static_raw_htx_buf); |
Richard Russo | 458eafb | 2019-07-31 11:45:56 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread); |
| 75 | REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread); |
| 76 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 77 | /* |
| 78 | * Returns the data from Authorization header. Function may be called more |
| 79 | * than once so data is stored in txn->auth_data. When no header is found |
| 80 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 81 | * searching again for something we are unable to find anyway. However, if |
| 82 | * the result if valid, the cache is not reused because we would risk to |
| 83 | * have the credentials overwritten by another stream in parallel. |
Willy Tarreau | eae8372 | 2020-04-29 11:52:51 +0200 | [diff] [blame] | 84 | * The caller is responsible for passing a sample with a valid stream/txn, |
| 85 | * and a valid htx. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 86 | */ |
| 87 | |
Christopher Faulet | cd76195 | 2019-07-15 13:58:29 +0200 | [diff] [blame] | 88 | static int get_http_auth(struct sample *smp, struct htx *htx) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 89 | { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 90 | struct stream *s = smp->strm; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 91 | struct http_txn *txn = s->txn; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 92 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 93 | struct ist hdr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 94 | struct buffer auth_method; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 95 | char *p; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 96 | int len; |
| 97 | |
| 98 | #ifdef DEBUG_AUTH |
| 99 | printf("Auth for stream %p: %d\n", s, txn->auth.method); |
| 100 | #endif |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 101 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 102 | return 0; |
| 103 | |
| 104 | txn->auth.method = HTTP_AUTH_WRONG; |
| 105 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 106 | if (txn->flags & TX_USE_PX_CONN) |
| 107 | hdr = ist("Proxy-Authorization"); |
| 108 | else |
| 109 | hdr = ist("Authorization"); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 110 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 111 | ctx.blk = NULL; |
| 112 | if (!http_find_header(htx, hdr, &ctx, 0)) |
| 113 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 114 | |
Willy Tarreau | 1725493 | 2020-09-02 07:08:47 +0200 | [diff] [blame] | 115 | p = memchr(ctx.value.ptr, ' ', ctx.value.len); |
| 116 | if (!p || p == ctx.value.ptr) /* if no space was found or if the space is the first character */ |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 117 | return 0; |
Willy Tarreau | 1725493 | 2020-09-02 07:08:47 +0200 | [diff] [blame] | 118 | len = p - ctx.value.ptr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 119 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 120 | if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1) |
| 121 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 122 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 123 | 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] | 124 | |
| 125 | if (!strncasecmp("Basic", auth_method.area, auth_method.data)) { |
| 126 | struct buffer *http_auth = get_trash_chunk(); |
| 127 | |
| 128 | len = base64dec(txn->auth.method_data.area, |
| 129 | txn->auth.method_data.data, |
| 130 | http_auth->area, global.tune.bufsize - 1); |
| 131 | |
| 132 | if (len < 0) |
| 133 | return 0; |
| 134 | |
| 135 | |
| 136 | http_auth->area[len] = '\0'; |
| 137 | |
| 138 | p = strchr(http_auth->area, ':'); |
| 139 | |
| 140 | if (!p) |
| 141 | return 0; |
| 142 | |
| 143 | txn->auth.user = http_auth->area; |
| 144 | *p = '\0'; |
| 145 | txn->auth.pass = p+1; |
| 146 | |
| 147 | txn->auth.method = HTTP_AUTH_BASIC; |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 155 | * which means that a request or response is ready. If some data is missing, |
| 156 | * 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] | 157 | * to extract data from L7. If <vol> is non-null during a prefetch, another |
| 158 | * test is made to ensure the required information is not gone. |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 159 | * |
| 160 | * The function returns : |
| 161 | * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 162 | * decide whether or not an HTTP message is present ; |
| 163 | * NULL if the requested data cannot be fetched or if it is certain that |
Willy Tarreau | eae8372 | 2020-04-29 11:52:51 +0200 | [diff] [blame] | 164 | * we'll never have any HTTP message there; this includes null strm or chn. |
Willy Tarreau | a6d9879 | 2020-08-12 14:04:52 +0200 | [diff] [blame] | 165 | * NULL if the sample's direction does not match the channel's (i.e. the |
| 166 | * function was asked to work on the wrong channel) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 167 | * The HTX message if ready |
| 168 | */ |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 169 | struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, struct check *check, int vol) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 170 | { |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 171 | struct stream *s = smp->strm; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 172 | struct http_txn *txn = NULL; |
| 173 | struct htx *htx = NULL; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 174 | struct http_msg *msg; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 175 | struct htx_sl *sl; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 176 | |
Willy Tarreau | a6d9879 | 2020-08-12 14:04:52 +0200 | [diff] [blame] | 177 | if (chn && |
| 178 | (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ && (chn->flags & CF_ISRESP)) || |
| 179 | ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES && !(chn->flags & CF_ISRESP)))) |
| 180 | return 0; |
| 181 | |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 182 | /* Note: it is possible that <s> is NULL when called before stream |
| 183 | * initialization (eg: tcp-request connection), so this function is the |
| 184 | * one responsible for guarding against this case for all HTTP users. |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 185 | * |
| 186 | * In the health check context, the stream and the channel must be NULL |
| 187 | * and <check> must be set. In this case, only the input buffer, |
| 188 | * corresponding to the response, is considered. It is the caller |
| 189 | * responsibility to provide <check>. |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 190 | */ |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 191 | BUG_ON(check && (s || chn)); |
| 192 | if (!s || !chn) { |
| 193 | if (check) { |
| 194 | htx = htxbuf(&check->bi); |
| 195 | |
| 196 | /* Analyse not yet started */ |
| 197 | if (htx_is_empty(htx) || htx->first == -1) |
| 198 | return NULL; |
| 199 | |
| 200 | sl = http_get_stline(htx); |
| 201 | if (vol && !sl) { |
| 202 | /* The start-line was already forwarded, it is too late to fetch anything */ |
| 203 | return NULL; |
| 204 | } |
| 205 | goto end; |
| 206 | } |
| 207 | |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 208 | return NULL; |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 209 | } |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 210 | |
| 211 | if (!s->txn) { |
| 212 | if (unlikely(!http_alloc_txn(s))) |
| 213 | return NULL; /* not enough memory */ |
| 214 | http_init_txn(s); |
| 215 | txn = s->txn; |
| 216 | } |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 217 | txn = s->txn; |
| 218 | msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp); |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 219 | |
Christopher Faulet | eca8854 | 2019-04-03 10:12:42 +0200 | [diff] [blame] | 220 | if (IS_HTX_STRM(s)) { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 221 | htx = htxbuf(&chn->buf); |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 222 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 223 | if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR)) |
| 224 | return NULL; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 225 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 226 | if (msg->msg_state < HTTP_MSG_BODY) { |
| 227 | /* Analyse not yet started */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 228 | if (htx_is_empty(htx) || htx->first == -1) { |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 229 | /* Parsing is done by the mux, just wait */ |
| 230 | smp->flags |= SMP_F_MAY_CHANGE; |
| 231 | return NULL; |
| 232 | } |
| 233 | } |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 234 | sl = http_get_stline(htx); |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 235 | if (vol && !sl) { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 236 | /* The start-line was already forwarded, it is too late to fetch anything */ |
| 237 | return NULL; |
| 238 | } |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 239 | } |
Christopher Faulet | eca8854 | 2019-04-03 10:12:42 +0200 | [diff] [blame] | 240 | else { /* RAW mode */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 241 | struct buffer *buf; |
| 242 | struct h1m h1m; |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 243 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 244 | union h1_sl h1sl; |
| 245 | unsigned int flags = HTX_FL_NONE; |
| 246 | int ret; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 247 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 248 | /* no HTTP fetch on the response in TCP mode */ |
| 249 | if (chn->flags & CF_ISRESP) |
| 250 | return NULL; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 251 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 252 | /* Now we are working on the request only */ |
| 253 | buf = &chn->buf; |
| 254 | if (b_head(buf) + b_data(buf) > b_wrap(buf)) |
| 255 | b_slow_realign(buf, trash.area, 0); |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 256 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 257 | h1m_init_req(&h1m); |
| 258 | ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf), |
| 259 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl); |
| 260 | if (ret <= 0) { |
| 261 | /* Invalid or too big*/ |
| 262 | if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite)) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 263 | return NULL; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 264 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 265 | /* wait for a full request */ |
| 266 | smp->flags |= SMP_F_MAY_CHANGE; |
| 267 | return NULL; |
| 268 | } |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 269 | |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 270 | /* OK we just got a valid HTTP message. We have to convert it |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 271 | * into an HTX message. |
| 272 | */ |
| 273 | if (unlikely(h1sl.rq.v.len == 0)) { |
| 274 | /* try to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 275 | if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 276 | return NULL; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 277 | h1sl.rq.v = ist("HTTP/1.0"); |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 278 | } |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 279 | |
| 280 | /* Set HTX start-line flags */ |
| 281 | if (h1m.flags & H1_MF_VER_11) |
| 282 | flags |= HTX_SL_F_VER_11; |
| 283 | if (h1m.flags & H1_MF_XFER_ENC) |
| 284 | flags |= HTX_SL_F_XFER_ENC; |
| 285 | flags |= HTX_SL_F_XFER_LEN; |
| 286 | if (h1m.flags & H1_MF_CHNK) |
| 287 | flags |= HTX_SL_F_CHNK; |
| 288 | else if (h1m.flags & H1_MF_CLEN) |
| 289 | flags |= HTX_SL_F_CLEN; |
| 290 | |
Richard Russo | 458eafb | 2019-07-31 11:45:56 -0700 | [diff] [blame] | 291 | htx = htx_from_buf(get_raw_htx_chunk()); |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 292 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v); |
| 293 | if (!sl || !htx_add_all_headers(htx, hdrs)) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 294 | return NULL; |
Willy Tarreau | ce9bbf5 | 2019-05-13 08:32:31 +0200 | [diff] [blame] | 295 | sl->info.req.meth = h1sl.rq.meth; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* OK we just got a valid HTTP message. If not already done by |
| 299 | * HTTP analyzers, we have some minor preparation to perform so |
| 300 | * that further checks can rely on HTTP tests. |
| 301 | */ |
| 302 | if (sl && msg->msg_state < HTTP_MSG_BODY) { |
| 303 | if (!(chn->flags & CF_ISRESP)) { |
| 304 | txn->meth = sl->info.req.meth; |
| 305 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 306 | s->flags |= SF_REDIRECTABLE; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 307 | } |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 308 | else |
| 309 | txn->status = sl->info.res.status; |
| 310 | if (sl->flags & HTX_SL_F_VER_11) |
| 311 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* everything's OK */ |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 315 | end: |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 316 | return htx; |
| 317 | } |
| 318 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 319 | /* This function fetches the method of current HTTP request and stores |
| 320 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 321 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 322 | * in <len> and <ptr> is NULL ; |
| 323 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 324 | * <len> to its length. |
| 325 | * This is intended to be used with pat_match_meth() only. |
| 326 | */ |
| 327 | static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 328 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 329 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 330 | struct http_txn *txn; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 331 | int meth; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 332 | |
Willy Tarreau | a6d9879 | 2020-08-12 14:04:52 +0200 | [diff] [blame] | 333 | txn = smp->strm->txn; |
| 334 | if (!txn) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 335 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 336 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 337 | meth = txn->meth; |
| 338 | smp->data.type = SMP_T_METH; |
| 339 | smp->data.u.meth.meth = meth; |
| 340 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | a6d9879 | 2020-08-12 14:04:52 +0200 | [diff] [blame] | 341 | struct htx *htx; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 342 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 343 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 344 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) { |
| 345 | /* ensure the indexes are not affected */ |
| 346 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 347 | } |
Willy Tarreau | a6d9879 | 2020-08-12 14:04:52 +0200 | [diff] [blame] | 348 | |
| 349 | htx = smp_prefetch_htx(smp, chn, NULL, 0); |
| 350 | if (!htx) |
| 351 | return 0; |
| 352 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 353 | sl = http_get_stline(htx); |
| 354 | smp->flags |= SMP_F_CONST; |
| 355 | smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl); |
| 356 | smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 357 | } |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 358 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 359 | return 1; |
| 360 | } |
| 361 | |
| 362 | static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 363 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 364 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 365 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 366 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 367 | char *ptr; |
| 368 | int len; |
| 369 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 370 | if (!htx) |
| 371 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 372 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 373 | sl = http_get_stline(htx); |
| 374 | len = HTX_SL_REQ_VLEN(sl); |
| 375 | ptr = HTX_SL_REQ_VPTR(sl); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 376 | |
| 377 | while ((len-- > 0) && (*ptr++ != '/')); |
| 378 | if (len <= 0) |
| 379 | return 0; |
| 380 | |
| 381 | smp->data.type = SMP_T_STR; |
| 382 | smp->data.u.str.area = ptr; |
| 383 | smp->data.u.str.data = len; |
| 384 | |
| 385 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 386 | return 1; |
| 387 | } |
| 388 | |
| 389 | static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 390 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 391 | struct channel *chn = SMP_RES_CHN(smp); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 392 | struct check *check = objt_check(smp->sess->origin); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 393 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 394 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 395 | char *ptr; |
| 396 | int len; |
| 397 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 398 | if (!htx) |
| 399 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 400 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 401 | sl = http_get_stline(htx); |
| 402 | len = HTX_SL_RES_VLEN(sl); |
| 403 | ptr = HTX_SL_RES_VPTR(sl); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 404 | |
| 405 | while ((len-- > 0) && (*ptr++ != '/')); |
| 406 | if (len <= 0) |
| 407 | return 0; |
| 408 | |
| 409 | smp->data.type = SMP_T_STR; |
| 410 | smp->data.u.str.area = ptr; |
| 411 | smp->data.u.str.data = len; |
| 412 | |
| 413 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 414 | return 1; |
| 415 | } |
| 416 | |
| 417 | /* 3. Check on Status Code. We manipulate integers here. */ |
| 418 | static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 419 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 420 | struct channel *chn = SMP_RES_CHN(smp); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 421 | struct check *check = objt_check(smp->sess->origin); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 422 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 423 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 424 | char *ptr; |
| 425 | int len; |
| 426 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 427 | if (!htx) |
| 428 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 429 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 430 | sl = http_get_stline(htx); |
| 431 | len = HTX_SL_RES_CLEN(sl); |
| 432 | ptr = HTX_SL_RES_CPTR(sl); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 433 | |
| 434 | smp->data.type = SMP_T_SINT; |
| 435 | smp->data.u.sint = __strl2ui(ptr, len); |
| 436 | smp->flags = SMP_F_VOL_1ST; |
| 437 | return 1; |
| 438 | } |
| 439 | |
| 440 | static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 441 | { |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 442 | struct ist unique_id; |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 443 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 444 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 445 | return 0; |
| 446 | |
Willy Tarreau | a1062a4 | 2020-04-29 11:50:38 +0200 | [diff] [blame] | 447 | if (!smp->strm) |
| 448 | return 0; |
| 449 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 450 | unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id); |
| 451 | if (!isttest(unique_id)) |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 452 | return 0; |
| 453 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 454 | smp->data.u.str.area = smp->strm->unique_id.ptr; |
| 455 | smp->data.u.str.data = smp->strm->unique_id.len; |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 456 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 457 | smp->flags = SMP_F_CONST; |
| 458 | return 1; |
| 459 | } |
| 460 | |
| 461 | /* Returns a string block containing all headers including the |
Joseph Herlant | 942eea3 | 2018-11-15 13:57:22 -0800 | [diff] [blame] | 462 | * empty line which separes headers from the body. This is useful |
| 463 | * for some headers analysis. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 464 | */ |
| 465 | static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 466 | { |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 467 | /* possible keywords: req.hdrs, res.hdrs */ |
| 468 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 469 | struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 470 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 471 | struct buffer *temp; |
| 472 | int32_t pos; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 473 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 474 | if (!htx) |
| 475 | return 0; |
| 476 | temp = get_trash_chunk(); |
| 477 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 478 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 479 | enum htx_blk_type type = htx_get_blk_type(blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 480 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 481 | if (type == HTX_BLK_HDR) { |
| 482 | struct ist n = htx_get_blk_name(htx, blk); |
| 483 | struct ist v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 484 | |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 485 | if (!h1_format_htx_hdr(n, v, temp)) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 486 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 487 | } |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 488 | else if (type == HTX_BLK_EOH) { |
| 489 | if (!chunk_memcat(temp, "\r\n", 2)) |
| 490 | return 0; |
| 491 | break; |
| 492 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 493 | } |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 494 | smp->data.type = SMP_T_STR; |
| 495 | smp->data.u.str = *temp; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 496 | return 1; |
| 497 | } |
| 498 | |
| 499 | /* Returns the header request in a length/value encoded format. |
| 500 | * This is useful for exchanges with the SPOE. |
| 501 | * |
| 502 | * A "length value" is a multibyte code encoding numbers. It uses the |
| 503 | * SPOE format. The encoding is the following: |
| 504 | * |
| 505 | * Each couple "header name" / "header value" is composed |
| 506 | * like this: |
| 507 | * "length value" "header name bytes" |
| 508 | * "length value" "header value bytes" |
| 509 | * When the last header is reached, the header name and the header |
| 510 | * value are empty. Their length are 0 |
| 511 | */ |
| 512 | static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 513 | { |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 514 | /* possible keywords: req.hdrs_bin, res.hdrs_bin */ |
| 515 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 516 | struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 517 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 518 | struct buffer *temp; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 519 | char *p, *end; |
| 520 | int32_t pos; |
| 521 | int ret; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 522 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 523 | if (!htx) |
| 524 | return 0; |
| 525 | temp = get_trash_chunk(); |
| 526 | p = temp->area; |
| 527 | end = temp->area + temp->size; |
| 528 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 529 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 530 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 531 | struct ist n, v; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 532 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 533 | if (type == HTX_BLK_HDR) { |
| 534 | n = htx_get_blk_name(htx,blk); |
| 535 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 536 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 537 | /* encode the header name. */ |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 538 | ret = encode_varint(n.len, &p, end); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 539 | if (ret == -1) |
| 540 | return 0; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 541 | if (p + n.len > end) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 542 | return 0; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 543 | memcpy(p, n.ptr, n.len); |
| 544 | p += n.len; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 545 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 546 | /* encode the header value. */ |
| 547 | ret = encode_varint(v.len, &p, end); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 548 | if (ret == -1) |
| 549 | return 0; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 550 | if (p + v.len > end) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 551 | return 0; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 552 | memcpy(p, v.ptr, v.len); |
| 553 | p += v.len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 554 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 555 | } |
| 556 | else if (type == HTX_BLK_EOH) { |
| 557 | /* encode the end of the header list with empty |
| 558 | * header name and header value. |
| 559 | */ |
| 560 | ret = encode_varint(0, &p, end); |
| 561 | if (ret == -1) |
| 562 | return 0; |
| 563 | ret = encode_varint(0, &p, end); |
| 564 | if (ret == -1) |
| 565 | return 0; |
| 566 | break; |
| 567 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 568 | } |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 569 | |
| 570 | /* Initialise sample data which will be filled. */ |
| 571 | smp->data.type = SMP_T_BIN; |
| 572 | smp->data.u.str.area = temp->area; |
| 573 | smp->data.u.str.data = p - temp->area; |
| 574 | smp->data.u.str.size = temp->size; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 575 | return 1; |
| 576 | } |
| 577 | |
| 578 | /* returns the longest available part of the body. This requires that the body |
| 579 | * has been waited for using http-buffer-request. |
| 580 | */ |
| 581 | static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 582 | { |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 583 | /* possible keywords: req.body, res.body */ |
| 584 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 585 | struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 586 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 587 | struct buffer *temp; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 588 | int32_t pos; |
Christopher Faulet | a9ffc41 | 2020-11-25 08:08:08 +0100 | [diff] [blame] | 589 | int finished = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 590 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 591 | if (!htx) |
| 592 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 593 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 594 | temp = get_trash_chunk(); |
| 595 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 596 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 597 | enum htx_blk_type type = htx_get_blk_type(blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 598 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 599 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) { |
Christopher Faulet | a9ffc41 | 2020-11-25 08:08:08 +0100 | [diff] [blame] | 600 | finished = 1; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 601 | break; |
Christopher Faulet | a9ffc41 | 2020-11-25 08:08:08 +0100 | [diff] [blame] | 602 | } |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 603 | if (type == HTX_BLK_DATA) { |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 604 | if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0)) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 605 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 606 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 607 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 608 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 609 | smp->data.type = SMP_T_BIN; |
| 610 | smp->data.u.str = *temp; |
| 611 | smp->flags = SMP_F_VOL_TEST; |
Willy Tarreau | 9dc92b2 | 2020-06-15 18:01:10 +0200 | [diff] [blame] | 612 | |
Christopher Faulet | a9ffc41 | 2020-11-25 08:08:08 +0100 | [diff] [blame] | 613 | if (!finished && (check || (chn && !channel_full(chn, global.tune.maxrewrite) && |
| 614 | !(chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR))))) |
Willy Tarreau | 9dc92b2 | 2020-06-15 18:01:10 +0200 | [diff] [blame] | 615 | smp->flags |= SMP_F_MAY_CHANGE; |
| 616 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 617 | return 1; |
| 618 | } |
| 619 | |
| 620 | |
| 621 | /* returns the available length of the body. This requires that the body |
| 622 | * has been waited for using http-buffer-request. |
| 623 | */ |
| 624 | static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 625 | { |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 626 | /* possible keywords: req.body_len, res.body_len */ |
| 627 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 628 | struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 629 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 630 | int32_t pos; |
| 631 | unsigned long long len = 0; |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 632 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 633 | if (!htx) |
| 634 | return 0; |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 635 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 636 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 637 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 638 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 639 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 640 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 641 | break; |
| 642 | if (type == HTX_BLK_DATA) |
| 643 | len += htx_get_blksz(blk); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 644 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 645 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 646 | smp->data.type = SMP_T_SINT; |
| 647 | smp->data.u.sint = len; |
| 648 | smp->flags = SMP_F_VOL_TEST; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 649 | return 1; |
| 650 | } |
| 651 | |
| 652 | |
| 653 | /* returns the advertised length of the body, or the advertised size of the |
| 654 | * chunks available in the buffer. This requires that the body has been waited |
| 655 | * for using http-buffer-request. |
| 656 | */ |
| 657 | static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 658 | { |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 659 | /* possible keywords: req.body_size, res.body_size */ |
| 660 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 661 | struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 662 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 663 | int32_t pos; |
| 664 | unsigned long long len = 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 665 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 666 | if (!htx) |
| 667 | return 0; |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 668 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 669 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 670 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 671 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 672 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 673 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 674 | break; |
| 675 | if (type == HTX_BLK_DATA) |
| 676 | len += htx_get_blksz(blk); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 677 | } |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 678 | if (htx->extra != ULLONG_MAX) |
| 679 | len += htx->extra; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 680 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 681 | smp->data.type = SMP_T_SINT; |
| 682 | smp->data.u.sint = len; |
| 683 | smp->flags = SMP_F_VOL_TEST; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 684 | return 1; |
| 685 | } |
| 686 | |
| 687 | |
| 688 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
| 689 | static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 690 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 691 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 692 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 693 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 694 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 695 | if (!htx) |
| 696 | return 0; |
| 697 | sl = http_get_stline(htx); |
| 698 | smp->data.type = SMP_T_STR; |
| 699 | smp->data.u.str.area = HTX_SL_REQ_UPTR(sl); |
| 700 | smp->data.u.str.data = HTX_SL_REQ_ULEN(sl); |
| 701 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 702 | return 1; |
| 703 | } |
| 704 | |
| 705 | static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 706 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 707 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 708 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 709 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 710 | struct sockaddr_storage addr; |
| 711 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 712 | if (!htx) |
| 713 | return 0; |
| 714 | sl = http_get_stline(htx); |
| 715 | url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 716 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 717 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 718 | return 0; |
| 719 | |
| 720 | smp->data.type = SMP_T_IPV4; |
| 721 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
| 722 | smp->flags = 0; |
| 723 | return 1; |
| 724 | } |
| 725 | |
| 726 | static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 727 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 728 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 729 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 730 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 731 | struct sockaddr_storage addr; |
| 732 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 733 | if (!htx) |
| 734 | return 0; |
| 735 | sl = http_get_stline(htx); |
| 736 | 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] | 737 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 738 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 739 | return 0; |
| 740 | |
| 741 | smp->data.type = SMP_T_SINT; |
| 742 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
| 743 | smp->flags = 0; |
| 744 | return 1; |
| 745 | } |
| 746 | |
| 747 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 748 | * Accepts an optional argument of type string containing the header field name, |
| 749 | * and an optional argument of type signed or unsigned integer to request an |
| 750 | * explicit occurrence of the header. Note that in the event of a missing name, |
| 751 | * headers are considered from the first one. It does not stop on commas and |
| 752 | * returns full lines instead (useful for User-Agent or Date for example). |
| 753 | */ |
| 754 | static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 755 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 756 | /* possible keywords: req.fhdr, res.fhdr */ |
| 757 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 758 | struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 759 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 760 | struct http_hdr_ctx *ctx = smp->ctx.a[0]; |
| 761 | struct ist name; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 762 | int occ = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 763 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 764 | if (!ctx) { |
| 765 | /* first call */ |
| 766 | ctx = &static_http_hdr_ctx; |
| 767 | ctx->blk = NULL; |
| 768 | smp->ctx.a[0] = ctx; |
| 769 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 770 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 771 | if (args[0].type != ARGT_STR) |
| 772 | return 0; |
Tim Duesterhus | 92c696e | 2021-02-28 16:11:36 +0100 | [diff] [blame] | 773 | name = ist2(args[0].data.str.area, args[0].data.str.data); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 774 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 775 | if (args[1].type == ARGT_SINT) |
| 776 | occ = args[1].data.sint; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 777 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 778 | if (!htx) |
| 779 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 780 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 781 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 782 | /* search for header from the beginning */ |
| 783 | ctx->blk = NULL; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 784 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 785 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 786 | /* no explicit occurrence and single fetch => last header by default */ |
| 787 | occ = -1; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 788 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 789 | if (!occ) |
| 790 | /* prepare to report multiple occurrences for ACL fetches */ |
| 791 | smp->flags |= SMP_F_NOT_LAST; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 792 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 793 | smp->data.type = SMP_T_STR; |
| 794 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
| 795 | if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data)) |
| 796 | return 1; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 797 | smp->flags &= ~SMP_F_NOT_LAST; |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 802 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 803 | * returns full lines instead (useful for User-Agent or Date for example). |
| 804 | */ |
| 805 | static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 806 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 807 | /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */ |
| 808 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 809 | struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 810 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 811 | struct http_hdr_ctx ctx; |
| 812 | struct ist name; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 813 | int cnt; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 814 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 815 | if (!htx) |
| 816 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 817 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 818 | if (args->type == ARGT_STR) { |
Tim Duesterhus | 92c696e | 2021-02-28 16:11:36 +0100 | [diff] [blame] | 819 | name = ist2(args->data.str.area, args->data.str.data); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 820 | } else { |
Tim Duesterhus | 68a088d | 2021-02-28 16:11:37 +0100 | [diff] [blame] | 821 | name = IST_NULL; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 822 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 823 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 824 | ctx.blk = NULL; |
| 825 | cnt = 0; |
| 826 | while (http_find_header(htx, name, &ctx, 1)) |
| 827 | cnt++; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 828 | smp->data.type = SMP_T_SINT; |
| 829 | smp->data.u.sint = cnt; |
| 830 | smp->flags = SMP_F_VOL_HDR; |
| 831 | return 1; |
| 832 | } |
| 833 | |
| 834 | static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 835 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 836 | /* possible keywords: req.hdr_names, res.hdr_names */ |
| 837 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 838 | struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 839 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 840 | struct buffer *temp; |
| 841 | char del = ','; |
| 842 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 843 | int32_t pos; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 844 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 845 | if (!htx) |
| 846 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 847 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 848 | if (args->type == ARGT_STR) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 849 | del = *args[0].data.str.area; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 850 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 851 | temp = get_trash_chunk(); |
| 852 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 853 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 854 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 855 | struct ist n; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 856 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 857 | if (type == HTX_BLK_EOH) |
| 858 | break; |
| 859 | if (type != HTX_BLK_HDR) |
| 860 | continue; |
| 861 | n = htx_get_blk_name(htx, blk); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 862 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 863 | if (temp->data) |
| 864 | temp->area[temp->data++] = del; |
| 865 | chunk_memcat(temp, n.ptr, n.len); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | smp->data.type = SMP_T_STR; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 869 | smp->data.u.str = *temp; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 870 | smp->flags = SMP_F_VOL_HDR; |
| 871 | return 1; |
| 872 | } |
| 873 | |
| 874 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 875 | * Accepts an optional argument of type string containing the header field name, |
| 876 | * and an optional argument of type signed or unsigned integer to request an |
| 877 | * explicit occurrence of the header. Note that in the event of a missing name, |
| 878 | * headers are considered from the first one. |
| 879 | */ |
| 880 | static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 881 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 882 | /* possible keywords: req.hdr / hdr, res.hdr / shdr */ |
| 883 | struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 884 | struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 885 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 886 | struct http_hdr_ctx *ctx = smp->ctx.a[0]; |
| 887 | struct ist name; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 888 | int occ = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 889 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 890 | if (!ctx) { |
| 891 | /* first call */ |
| 892 | ctx = &static_http_hdr_ctx; |
| 893 | ctx->blk = NULL; |
| 894 | smp->ctx.a[0] = ctx; |
| 895 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 896 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 897 | if (args[0].type != ARGT_STR) |
| 898 | return 0; |
Tim Duesterhus | 92c696e | 2021-02-28 16:11:36 +0100 | [diff] [blame] | 899 | name = ist2(args[0].data.str.area, args[0].data.str.data); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 900 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 901 | if (args[1].type == ARGT_SINT) |
| 902 | occ = args[1].data.sint; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 903 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 904 | if (!htx) |
| 905 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 906 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 907 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 908 | /* search for header from the beginning */ |
| 909 | ctx->blk = NULL; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 910 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 911 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 912 | /* no explicit occurrence and single fetch => last header by default */ |
| 913 | occ = -1; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 914 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 915 | if (!occ) |
| 916 | /* prepare to report multiple occurrences for ACL fetches */ |
| 917 | smp->flags |= SMP_F_NOT_LAST; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 918 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 919 | smp->data.type = SMP_T_STR; |
| 920 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
| 921 | if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data)) |
| 922 | return 1; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 923 | |
| 924 | smp->flags &= ~SMP_F_NOT_LAST; |
| 925 | return 0; |
| 926 | } |
| 927 | |
Christopher Faulet | c1f40dd | 2019-05-16 10:07:30 +0200 | [diff] [blame] | 928 | /* Same than smp_fetch_hdr() but only relies on the sample direction to choose |
| 929 | * the right channel. So instead of duplicating the code, we just change the |
| 930 | * keyword and then fallback on smp_fetch_hdr(). |
| 931 | */ |
| 932 | static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 933 | { |
| 934 | kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr"); |
| 935 | return smp_fetch_hdr(args, smp, kw, private); |
| 936 | } |
| 937 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 938 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 939 | * Accepts exactly 1 argument of type string. |
| 940 | */ |
| 941 | static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 942 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 943 | /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */ |
| 944 | struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 945 | struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 946 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 947 | struct http_hdr_ctx ctx; |
| 948 | struct ist name; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 949 | int cnt; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 950 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 951 | if (!htx) |
| 952 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 953 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 954 | if (args->type == ARGT_STR) { |
Tim Duesterhus | 92c696e | 2021-02-28 16:11:36 +0100 | [diff] [blame] | 955 | name = ist2(args->data.str.area, args->data.str.data); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 956 | } else { |
Tim Duesterhus | 68a088d | 2021-02-28 16:11:37 +0100 | [diff] [blame] | 957 | name = IST_NULL; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 958 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 959 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 960 | ctx.blk = NULL; |
| 961 | cnt = 0; |
| 962 | while (http_find_header(htx, name, &ctx, 0)) |
| 963 | cnt++; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 964 | |
| 965 | smp->data.type = SMP_T_SINT; |
| 966 | smp->data.u.sint = cnt; |
| 967 | smp->flags = SMP_F_VOL_HDR; |
| 968 | return 1; |
| 969 | } |
| 970 | |
| 971 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 972 | * takes a mandatory argument of type string and an optional one of type int |
| 973 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 974 | * may or may not be appropriate for everything. |
| 975 | */ |
| 976 | static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 977 | { |
| 978 | int ret = smp_fetch_hdr(args, smp, kw, private); |
| 979 | |
| 980 | if (ret > 0) { |
| 981 | smp->data.type = SMP_T_SINT; |
| 982 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 983 | smp->data.u.str.data); |
| 984 | } |
| 985 | |
| 986 | return ret; |
| 987 | } |
| 988 | |
| 989 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 990 | * and an optional one of type int to designate a specific occurrence. |
| 991 | * It returns an IPv4 or IPv6 address. |
| 992 | */ |
| 993 | static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 994 | { |
| 995 | int ret; |
Tim Duesterhus | 5cd0087 | 2020-06-26 15:44:48 +0200 | [diff] [blame] | 996 | struct buffer *temp = get_trash_chunk(); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 997 | |
| 998 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
Tim Duesterhus | 5cd0087 | 2020-06-26 15:44:48 +0200 | [diff] [blame] | 999 | if (smp->data.u.str.data < temp->size - 1) { |
| 1000 | memcpy(temp->area, smp->data.u.str.area, |
| 1001 | smp->data.u.str.data); |
| 1002 | temp->area[smp->data.u.str.data] = '\0'; |
| 1003 | if (url2ipv4((char *) temp->area, &smp->data.u.ipv4)) { |
| 1004 | smp->data.type = SMP_T_IPV4; |
| 1005 | break; |
| 1006 | } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) { |
| 1007 | smp->data.type = SMP_T_IPV6; |
| 1008 | break; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1012 | /* if the header doesn't match an IP address, fetch next one */ |
| 1013 | if (!(smp->flags & SMP_F_NOT_LAST)) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1014 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1015 | } |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1016 | return ret; |
| 1017 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1018 | |
Christopher Faulet | e720c32 | 2020-09-02 17:25:18 +0200 | [diff] [blame] | 1019 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the |
| 1020 | * first '/' after the possible hostname. It ends before the possible '?' except |
| 1021 | * for 'pathq' keyword. |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1022 | */ |
| 1023 | static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1024 | { |
| 1025 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1026 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1027 | struct htx_sl *sl; |
| 1028 | struct ist path; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1029 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1030 | if (!htx) |
| 1031 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1032 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1033 | sl = http_get_stline(htx); |
Christopher Faulet | e720c32 | 2020-09-02 17:25:18 +0200 | [diff] [blame] | 1034 | path = http_get_path(htx_sl_req_uri(sl)); |
| 1035 | |
Yves Lafon | b4d3708 | 2021-02-11 11:01:28 +0100 | [diff] [blame] | 1036 | if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq |
Christopher Faulet | e720c32 | 2020-09-02 17:25:18 +0200 | [diff] [blame] | 1037 | path = http_get_path(htx_sl_req_uri(sl)); |
| 1038 | else |
| 1039 | path = iststop(http_get_path(htx_sl_req_uri(sl)), '?'); |
| 1040 | |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 1041 | if (!isttest(path)) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1042 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1043 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1044 | /* OK, we got the '/' ! */ |
| 1045 | smp->data.type = SMP_T_STR; |
| 1046 | smp->data.u.str.area = path.ptr; |
Jerome Magnin | 4fb196c | 2020-02-21 10:49:12 +0100 | [diff] [blame] | 1047 | smp->data.u.str.data = path.len; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1048 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1049 | return 1; |
| 1050 | } |
| 1051 | |
| 1052 | /* This produces a concatenation of the first occurrence of the Host header |
| 1053 | * followed by the path component if it begins with a slash ('/'). This means |
| 1054 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 1055 | * If no Host header is found, then the path is returned as-is. The returned |
| 1056 | * value is stored in the trash so it does not need to be marked constant. |
| 1057 | * The returned sample is of type string. |
| 1058 | */ |
| 1059 | static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1060 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1061 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1062 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1063 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1064 | struct buffer *temp; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1065 | struct http_hdr_ctx ctx; |
| 1066 | struct ist path; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1067 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1068 | if (!htx) |
| 1069 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1070 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1071 | ctx.blk = NULL; |
| 1072 | if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len) |
| 1073 | return smp_fetch_path(args, smp, kw, private); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1074 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1075 | /* OK we have the header value in ctx.value */ |
| 1076 | temp = get_trash_chunk(); |
| 1077 | chunk_memcat(temp, ctx.value.ptr, ctx.value.len); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1078 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1079 | /* now retrieve the path */ |
| 1080 | sl = http_get_stline(htx); |
| 1081 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 1082 | if (isttest(path)) { |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1083 | size_t len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1084 | |
Yves Lafon | b4d3708 | 2021-02-11 11:01:28 +0100 | [diff] [blame] | 1085 | if (kw[4] == 'q' && kw[0] == 'b') { // baseq |
| 1086 | len = path.len; |
| 1087 | } else { |
| 1088 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++) |
| 1089 | ; |
| 1090 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1091 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1092 | if (len && *(path.ptr) == '/') |
| 1093 | chunk_memcat(temp, path.ptr, len); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1094 | } |
| 1095 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1096 | smp->data.type = SMP_T_STR; |
| 1097 | smp->data.u.str = *temp; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1098 | smp->flags = SMP_F_VOL_1ST; |
| 1099 | return 1; |
| 1100 | } |
| 1101 | |
| 1102 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 1103 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 1104 | * This means that '*' will not be added, resulting in exactly the first Host |
| 1105 | * entry. If no Host header is found, then the path is used. The resulting value |
| 1106 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 1107 | * 32-bit integer value. This fetch is useful for tracking per-path activity on |
| 1108 | * high-traffic sites without having to store whole paths. |
| 1109 | */ |
| 1110 | static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1111 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1112 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1113 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1114 | struct htx_sl *sl; |
| 1115 | struct http_hdr_ctx ctx; |
| 1116 | struct ist path; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1117 | unsigned int hash = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1118 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1119 | if (!htx) |
| 1120 | return 0; |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1121 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1122 | ctx.blk = NULL; |
| 1123 | if (http_find_header(htx, ist("Host"), &ctx, 0)) { |
| 1124 | /* OK we have the header value in ctx.value */ |
| 1125 | while (ctx.value.len--) |
| 1126 | hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1127 | } |
| 1128 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1129 | /* now retrieve the path */ |
| 1130 | sl = http_get_stline(htx); |
| 1131 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 1132 | if (isttest(path)) { |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1133 | size_t len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1134 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1135 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++) |
| 1136 | ; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1137 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1138 | if (len && *(path.ptr) == '/') { |
| 1139 | while (len--) |
| 1140 | hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1141 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1142 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1143 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1144 | hash = full_hash(hash); |
| 1145 | |
| 1146 | smp->data.type = SMP_T_SINT; |
| 1147 | smp->data.u.sint = hash; |
| 1148 | smp->flags = SMP_F_VOL_1ST; |
| 1149 | return 1; |
| 1150 | } |
| 1151 | |
| 1152 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 1153 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 1154 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 1155 | * on the source address length. The path hash is stored before the address so |
| 1156 | * that in environments where IPv6 is insignificant, truncating the output to |
| 1157 | * 8 bytes would still work. |
| 1158 | */ |
| 1159 | static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1160 | { |
| 1161 | struct buffer *temp; |
| 1162 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
| 1163 | |
Willy Tarreau | cd7ca79 | 2019-07-17 16:57:03 +0200 | [diff] [blame] | 1164 | if (!cli_conn || !conn_get_src(cli_conn)) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1165 | return 0; |
| 1166 | |
| 1167 | if (!smp_fetch_base32(args, smp, kw, private)) |
| 1168 | return 0; |
| 1169 | |
| 1170 | temp = get_trash_chunk(); |
| 1171 | *(unsigned int *) temp->area = htonl(smp->data.u.sint); |
| 1172 | temp->data += sizeof(unsigned int); |
| 1173 | |
Willy Tarreau | 9a1efe1 | 2019-07-17 17:13:50 +0200 | [diff] [blame] | 1174 | switch (cli_conn->src->ss_family) { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1175 | case AF_INET: |
| 1176 | memcpy(temp->area + temp->data, |
Willy Tarreau | 9a1efe1 | 2019-07-17 17:13:50 +0200 | [diff] [blame] | 1177 | &((struct sockaddr_in *)cli_conn->src)->sin_addr, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1178 | 4); |
| 1179 | temp->data += 4; |
| 1180 | break; |
| 1181 | case AF_INET6: |
| 1182 | memcpy(temp->area + temp->data, |
Willy Tarreau | 9a1efe1 | 2019-07-17 17:13:50 +0200 | [diff] [blame] | 1183 | &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1184 | 16); |
| 1185 | temp->data += 16; |
| 1186 | break; |
| 1187 | default: |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | smp->data.u.str = *temp; |
| 1192 | smp->data.type = SMP_T_BIN; |
| 1193 | return 1; |
| 1194 | } |
| 1195 | |
| 1196 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 1197 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 1198 | * of type string carrying the whole query string. |
| 1199 | */ |
| 1200 | static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1201 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1202 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1203 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1204 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1205 | char *ptr, *end; |
| 1206 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1207 | if (!htx) |
| 1208 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1209 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1210 | sl = http_get_stline(htx); |
| 1211 | ptr = HTX_SL_REQ_UPTR(sl); |
| 1212 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1213 | |
| 1214 | /* look up the '?' */ |
| 1215 | do { |
| 1216 | if (ptr == end) |
| 1217 | return 0; |
| 1218 | } while (*ptr++ != '?'); |
| 1219 | |
| 1220 | smp->data.type = SMP_T_STR; |
| 1221 | smp->data.u.str.area = ptr; |
| 1222 | smp->data.u.str.data = end - ptr; |
| 1223 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1224 | return 1; |
| 1225 | } |
| 1226 | |
| 1227 | static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1228 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1229 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1230 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1231 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1232 | if (!htx) |
| 1233 | return 0; |
| 1234 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1235 | smp->data.u.sint = 1; |
| 1236 | return 1; |
| 1237 | } |
| 1238 | |
| 1239 | /* return a valid test if the current request is the first one on the connection */ |
| 1240 | static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1241 | { |
Willy Tarreau | 79512b6 | 2020-04-29 11:52:13 +0200 | [diff] [blame] | 1242 | if (!smp->strm) |
| 1243 | return 0; |
| 1244 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1245 | smp->data.type = SMP_T_BOOL; |
| 1246 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
| 1247 | return 1; |
| 1248 | } |
| 1249 | |
Christopher Faulet | a406356 | 2019-08-02 11:51:37 +0200 | [diff] [blame] | 1250 | /* Fetch the authentication method if there is an Authorization header. It |
| 1251 | * relies on get_http_auth() |
| 1252 | */ |
| 1253 | static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1254 | { |
| 1255 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1256 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | a406356 | 2019-08-02 11:51:37 +0200 | [diff] [blame] | 1257 | struct http_txn *txn; |
| 1258 | |
| 1259 | if (!htx) |
| 1260 | return 0; |
| 1261 | |
| 1262 | txn = smp->strm->txn; |
| 1263 | if (!get_http_auth(smp, htx)) |
| 1264 | return 0; |
| 1265 | |
| 1266 | switch (txn->auth.method) { |
| 1267 | case HTTP_AUTH_BASIC: |
| 1268 | smp->data.u.str.area = "Basic"; |
| 1269 | smp->data.u.str.data = 5; |
| 1270 | break; |
| 1271 | case HTTP_AUTH_DIGEST: |
| 1272 | /* Unexpected because not supported */ |
| 1273 | smp->data.u.str.area = "Digest"; |
| 1274 | smp->data.u.str.data = 6; |
| 1275 | break; |
| 1276 | default: |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
| 1280 | smp->data.type = SMP_T_STR; |
| 1281 | smp->flags = SMP_F_CONST; |
| 1282 | return 1; |
| 1283 | } |
| 1284 | |
| 1285 | /* Fetch the user supplied if there is an Authorization header. It relies on |
| 1286 | * get_http_auth() |
| 1287 | */ |
| 1288 | static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1289 | { |
| 1290 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1291 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | a406356 | 2019-08-02 11:51:37 +0200 | [diff] [blame] | 1292 | struct http_txn *txn; |
| 1293 | |
| 1294 | if (!htx) |
| 1295 | return 0; |
| 1296 | |
| 1297 | txn = smp->strm->txn; |
| 1298 | if (!get_http_auth(smp, htx)) |
| 1299 | return 0; |
| 1300 | |
| 1301 | smp->data.type = SMP_T_STR; |
| 1302 | smp->data.u.str.area = txn->auth.user; |
| 1303 | smp->data.u.str.data = strlen(txn->auth.user); |
| 1304 | smp->flags = SMP_F_CONST; |
| 1305 | return 1; |
| 1306 | } |
| 1307 | |
| 1308 | /* Fetch the password supplied if there is an Authorization header. It relies on |
| 1309 | * get_http_auth() |
| 1310 | */ |
| 1311 | static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1312 | { |
| 1313 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1314 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | a406356 | 2019-08-02 11:51:37 +0200 | [diff] [blame] | 1315 | struct http_txn *txn; |
| 1316 | |
| 1317 | if (!htx) |
| 1318 | return 0; |
| 1319 | |
| 1320 | txn = smp->strm->txn; |
| 1321 | if (!get_http_auth(smp, htx)) |
| 1322 | return 0; |
| 1323 | |
| 1324 | smp->data.type = SMP_T_STR; |
| 1325 | smp->data.u.str.area = txn->auth.pass; |
| 1326 | smp->data.u.str.data = strlen(txn->auth.pass); |
| 1327 | smp->flags = SMP_F_CONST; |
| 1328 | return 1; |
| 1329 | } |
| 1330 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1331 | /* Accepts exactly 1 argument of type userlist */ |
| 1332 | static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1333 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1334 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1335 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1336 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1337 | if (args->type != ARGT_USR) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1338 | return 0; |
| 1339 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1340 | if (!htx) |
| 1341 | return 0; |
| 1342 | if (!get_http_auth(smp, htx)) |
| 1343 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1344 | |
| 1345 | smp->data.type = SMP_T_BOOL; |
| 1346 | 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] | 1347 | smp->strm->txn->auth.pass); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1348 | return 1; |
| 1349 | } |
| 1350 | |
| 1351 | /* Accepts exactly 1 argument of type userlist */ |
| 1352 | static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1353 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1354 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1355 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1356 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1357 | if (args->type != ARGT_USR) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1358 | return 0; |
| 1359 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1360 | if (!htx) |
| 1361 | return 0; |
| 1362 | if (!get_http_auth(smp, htx)) |
| 1363 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1364 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1365 | /* if the user does not belong to the userlist or has a wrong password, |
| 1366 | * report that it unconditionally does not match. Otherwise we return |
| 1367 | * a string containing the username. |
| 1368 | */ |
| 1369 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 1370 | smp->strm->txn->auth.pass)) |
| 1371 | return 0; |
| 1372 | |
| 1373 | /* pat_match_auth() will need the user list */ |
| 1374 | smp->ctx.a[0] = args->data.usr; |
| 1375 | |
| 1376 | smp->data.type = SMP_T_STR; |
| 1377 | smp->flags = SMP_F_CONST; |
| 1378 | smp->data.u.str.area = smp->strm->txn->auth.user; |
| 1379 | smp->data.u.str.data = strlen(smp->strm->txn->auth.user); |
| 1380 | |
| 1381 | return 1; |
| 1382 | } |
| 1383 | |
| 1384 | /* Fetch a captured HTTP request header. The index is the position of |
| 1385 | * the "capture" option in the configuration file |
| 1386 | */ |
| 1387 | static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1388 | { |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1389 | struct proxy *fe; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1390 | int idx; |
| 1391 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1392 | if (args->type != ARGT_SINT) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1393 | return 0; |
| 1394 | |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1395 | if (!smp->strm) |
| 1396 | return 0; |
| 1397 | |
| 1398 | fe = strm_fe(smp->strm); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1399 | idx = args->data.sint; |
| 1400 | |
| 1401 | if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL) |
| 1402 | return 0; |
| 1403 | |
| 1404 | smp->data.type = SMP_T_STR; |
| 1405 | smp->flags |= SMP_F_CONST; |
| 1406 | smp->data.u.str.area = smp->strm->req_cap[idx]; |
| 1407 | smp->data.u.str.data = strlen(smp->strm->req_cap[idx]); |
| 1408 | |
| 1409 | return 1; |
| 1410 | } |
| 1411 | |
| 1412 | /* Fetch a captured HTTP response header. The index is the position of |
| 1413 | * the "capture" option in the configuration file |
| 1414 | */ |
| 1415 | static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1416 | { |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1417 | struct proxy *fe; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1418 | int idx; |
| 1419 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1420 | if (args->type != ARGT_SINT) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1421 | return 0; |
| 1422 | |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1423 | if (!smp->strm) |
| 1424 | return 0; |
| 1425 | |
| 1426 | fe = strm_fe(smp->strm); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1427 | idx = args->data.sint; |
| 1428 | |
| 1429 | if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL) |
| 1430 | return 0; |
| 1431 | |
| 1432 | smp->data.type = SMP_T_STR; |
| 1433 | smp->flags |= SMP_F_CONST; |
| 1434 | smp->data.u.str.area = smp->strm->res_cap[idx]; |
| 1435 | smp->data.u.str.data = strlen(smp->strm->res_cap[idx]); |
| 1436 | |
| 1437 | return 1; |
| 1438 | } |
| 1439 | |
| 1440 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 1441 | static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1442 | { |
| 1443 | struct buffer *temp; |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1444 | struct http_txn *txn; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1445 | char *ptr; |
| 1446 | |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1447 | if (!smp->strm) |
| 1448 | return 0; |
| 1449 | |
| 1450 | txn = smp->strm->txn; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1451 | if (!txn || !txn->uri) |
| 1452 | return 0; |
| 1453 | |
| 1454 | ptr = txn->uri; |
| 1455 | |
| 1456 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 1457 | ptr++; |
| 1458 | |
| 1459 | temp = get_trash_chunk(); |
| 1460 | temp->area = txn->uri; |
| 1461 | temp->data = ptr - txn->uri; |
| 1462 | smp->data.u.str = *temp; |
| 1463 | smp->data.type = SMP_T_STR; |
| 1464 | smp->flags = SMP_F_CONST; |
| 1465 | |
| 1466 | return 1; |
| 1467 | |
| 1468 | } |
| 1469 | |
| 1470 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 1471 | static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1472 | { |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1473 | struct http_txn *txn; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1474 | struct ist path; |
| 1475 | const char *ptr; |
| 1476 | |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1477 | if (!smp->strm) |
| 1478 | return 0; |
| 1479 | |
| 1480 | txn = smp->strm->txn; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1481 | if (!txn || !txn->uri) |
| 1482 | return 0; |
| 1483 | |
| 1484 | ptr = txn->uri; |
| 1485 | |
| 1486 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 1487 | ptr++; |
| 1488 | |
| 1489 | if (!*ptr) |
| 1490 | return 0; |
| 1491 | |
Christopher Faulet | 78337bb | 2018-11-15 14:35:18 +0100 | [diff] [blame] | 1492 | /* skip the first space and find space after URI */ |
| 1493 | path = ist2(++ptr, 0); |
| 1494 | while (*ptr != ' ' && *ptr != '\0') |
| 1495 | ptr++; |
| 1496 | path.len = ptr - path.ptr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1497 | |
Christopher Faulet | 78337bb | 2018-11-15 14:35:18 +0100 | [diff] [blame] | 1498 | path = http_get_path(path); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 1499 | if (!isttest(path)) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1500 | return 0; |
| 1501 | |
| 1502 | smp->data.u.str.area = path.ptr; |
| 1503 | smp->data.u.str.data = path.len; |
| 1504 | smp->data.type = SMP_T_STR; |
| 1505 | smp->flags = SMP_F_CONST; |
| 1506 | |
| 1507 | return 1; |
| 1508 | } |
| 1509 | |
| 1510 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 1511 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 1512 | */ |
| 1513 | static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1514 | { |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1515 | struct http_txn *txn; |
| 1516 | |
| 1517 | if (!smp->strm) |
| 1518 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1519 | |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1520 | txn = smp->strm->txn; |
Christopher Faulet | 711ed6a | 2019-07-16 14:16:10 +0200 | [diff] [blame] | 1521 | if (!txn || txn->req.msg_state >= HTTP_MSG_BODY) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1522 | return 0; |
| 1523 | |
| 1524 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 1525 | smp->data.u.str.area = "HTTP/1.1"; |
| 1526 | else |
| 1527 | smp->data.u.str.area = "HTTP/1.0"; |
| 1528 | |
| 1529 | smp->data.u.str.data = 8; |
| 1530 | smp->data.type = SMP_T_STR; |
| 1531 | smp->flags = SMP_F_CONST; |
| 1532 | return 1; |
| 1533 | |
| 1534 | } |
| 1535 | |
| 1536 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 1537 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 1538 | */ |
| 1539 | static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1540 | { |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1541 | struct http_txn *txn; |
| 1542 | |
| 1543 | if (!smp->strm) |
| 1544 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1545 | |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1546 | txn = smp->strm->txn; |
Christopher Faulet | 711ed6a | 2019-07-16 14:16:10 +0200 | [diff] [blame] | 1547 | if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1548 | return 0; |
| 1549 | |
| 1550 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 1551 | smp->data.u.str.area = "HTTP/1.1"; |
| 1552 | else |
| 1553 | smp->data.u.str.area = "HTTP/1.0"; |
| 1554 | |
| 1555 | smp->data.u.str.data = 8; |
| 1556 | smp->data.type = SMP_T_STR; |
| 1557 | smp->flags = SMP_F_CONST; |
| 1558 | return 1; |
| 1559 | |
| 1560 | } |
| 1561 | |
| 1562 | /* Iterate over all cookies present in a message. The context is stored in |
| 1563 | * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the |
| 1564 | * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on |
| 1565 | * the direction, multiple cookies may be parsed on the same line or not. |
Maciej Zdeb | dea7c20 | 2020-11-13 09:38:06 +0000 | [diff] [blame] | 1566 | * If provided, the searched cookie name is in args, in args->data.str. If |
| 1567 | * the input options indicate that no iterating is desired, then only last |
| 1568 | * value is fetched if any. If no cookie name is provided, the first cookie |
| 1569 | * value found is fetched. The returned sample is of type CSTR. Can be used |
| 1570 | * to parse cookies in other files. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1571 | */ |
| 1572 | static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1573 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1574 | /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */ |
| 1575 | struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 1576 | struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 1577 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1578 | struct http_hdr_ctx *ctx = smp->ctx.a[2]; |
| 1579 | struct ist hdr; |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1580 | char *cook = NULL; |
| 1581 | size_t cook_l = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1582 | int found = 0; |
| 1583 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1584 | if (args->type == ARGT_STR) { |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1585 | cook = args->data.str.area; |
| 1586 | cook_l = args->data.str.data; |
| 1587 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1588 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1589 | if (!ctx) { |
| 1590 | /* first call */ |
| 1591 | ctx = &static_http_hdr_ctx; |
| 1592 | ctx->blk = NULL; |
| 1593 | smp->ctx.a[2] = ctx; |
| 1594 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1595 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1596 | if (!htx) |
| 1597 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1598 | |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 1599 | hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie")); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1600 | |
Maciej Zdeb | dea7c20 | 2020-11-13 09:38:06 +0000 | [diff] [blame] | 1601 | /* OK so basically here, either we want only one value or we want to |
| 1602 | * iterate over all of them and we fetch the next one. In this last case |
| 1603 | * SMP_OPT_ITERATE option is set. |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1604 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1605 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1606 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
| 1607 | /* search for the header from the beginning, we must first initialize |
| 1608 | * the search parameters. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1609 | */ |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1610 | smp->ctx.a[0] = NULL; |
| 1611 | ctx->blk = NULL; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1612 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1613 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1614 | smp->flags |= SMP_F_VOL_HDR; |
| 1615 | while (1) { |
| 1616 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 1617 | if (!smp->ctx.a[0]) { |
| 1618 | if (!http_find_header(htx, hdr, ctx, 0)) |
| 1619 | goto out; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1620 | |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1621 | if (ctx->value.len < cook_l + 1) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1622 | continue; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1623 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1624 | smp->ctx.a[0] = ctx->value.ptr; |
| 1625 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1626 | } |
| 1627 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1628 | smp->data.type = SMP_T_STR; |
| 1629 | smp->flags |= SMP_F_CONST; |
| 1630 | smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1], |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1631 | cook, cook_l, |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1632 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 1633 | &smp->data.u.str.area, |
| 1634 | &smp->data.u.str.data); |
| 1635 | if (smp->ctx.a[0]) { |
| 1636 | found = 1; |
Maciej Zdeb | dea7c20 | 2020-11-13 09:38:06 +0000 | [diff] [blame] | 1637 | if (smp->opt & SMP_OPT_ITERATE) { |
| 1638 | /* iterate on cookie value */ |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1639 | smp->flags |= SMP_F_NOT_LAST; |
| 1640 | return 1; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1641 | } |
Maciej Zdeb | dea7c20 | 2020-11-13 09:38:06 +0000 | [diff] [blame] | 1642 | if (args->data.str.data == 0) { |
| 1643 | /* No cookie name, first occurrence returned */ |
| 1644 | break; |
| 1645 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1646 | } |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1647 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1648 | } |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1649 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1650 | /* all cookie headers and values were scanned. If we're looking for the |
| 1651 | * last occurrence, we may return it now. |
| 1652 | */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1653 | out: |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1654 | smp->flags &= ~SMP_F_NOT_LAST; |
| 1655 | return found; |
| 1656 | } |
| 1657 | |
Christopher Faulet | c1f40dd | 2019-05-16 10:07:30 +0200 | [diff] [blame] | 1658 | /* Same than smp_fetch_cookie() but only relies on the sample direction to |
| 1659 | * choose the right channel. So instead of duplicating the code, we just change |
| 1660 | * the keyword and then fallback on smp_fetch_cookie(). |
| 1661 | */ |
| 1662 | static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1663 | { |
| 1664 | kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook"); |
| 1665 | return smp_fetch_cookie(args, smp, kw, private); |
| 1666 | } |
| 1667 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1668 | /* Iterate over all cookies present in a request to count how many occurrences |
| 1669 | * match the name in args and args->data.str.len. If <multi> is non-null, then |
| 1670 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 1671 | * type UINT. Accepts exactly 1 argument of type string. |
| 1672 | */ |
| 1673 | static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1674 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1675 | /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */ |
| 1676 | struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Christopher Faulet | f98e626 | 2020-05-06 09:42:04 +0200 | [diff] [blame] | 1677 | struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL); |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 1678 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1679 | struct http_hdr_ctx ctx; |
| 1680 | struct ist hdr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1681 | char *val_beg, *val_end; |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1682 | char *cook = NULL; |
| 1683 | size_t cook_l = 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1684 | int cnt; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1685 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1686 | if (args->type == ARGT_STR){ |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1687 | cook = args->data.str.area; |
| 1688 | cook_l = args->data.str.data; |
| 1689 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1690 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1691 | if (!htx) |
| 1692 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1693 | |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 1694 | hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie")); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1695 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1696 | val_end = val_beg = NULL; |
| 1697 | ctx.blk = NULL; |
| 1698 | cnt = 0; |
| 1699 | while (1) { |
| 1700 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 1701 | if (!val_beg) { |
| 1702 | if (!http_find_header(htx, hdr, &ctx, 0)) |
| 1703 | break; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1704 | |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1705 | if (ctx.value.len < cook_l + 1) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1706 | continue; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1707 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1708 | val_beg = ctx.value.ptr; |
| 1709 | val_end = val_beg + ctx.value.len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1710 | } |
| 1711 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1712 | smp->data.type = SMP_T_STR; |
| 1713 | smp->flags |= SMP_F_CONST; |
| 1714 | while ((val_beg = http_extract_cookie_value(val_beg, val_end, |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1715 | cook, cook_l, |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1716 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 1717 | &smp->data.u.str.area, |
| 1718 | &smp->data.u.str.data))) { |
| 1719 | cnt++; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | smp->data.type = SMP_T_SINT; |
| 1724 | smp->data.u.sint = cnt; |
| 1725 | smp->flags |= SMP_F_VOL_HDR; |
| 1726 | return 1; |
| 1727 | } |
| 1728 | |
| 1729 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 1730 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 1731 | */ |
| 1732 | static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1733 | { |
| 1734 | int ret = smp_fetch_cookie(args, smp, kw, private); |
| 1735 | |
| 1736 | if (ret > 0) { |
| 1737 | smp->data.type = SMP_T_SINT; |
| 1738 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 1739 | smp->data.u.str.data); |
| 1740 | } |
| 1741 | |
| 1742 | return ret; |
| 1743 | } |
| 1744 | |
| 1745 | /************************************************************************/ |
| 1746 | /* The code below is dedicated to sample fetches */ |
| 1747 | /************************************************************************/ |
| 1748 | |
| 1749 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1750 | * string whose first contiguous chunk has its beginning in ctx->a[0] and end |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1751 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 1752 | * pointers are updated for next iteration before leaving. |
| 1753 | */ |
| 1754 | 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) |
| 1755 | { |
| 1756 | const char *vstart, *vend; |
| 1757 | struct buffer *temp; |
| 1758 | const char **chunks = (const char **)smp->ctx.a; |
| 1759 | |
| 1760 | if (!http_find_next_url_param(chunks, name, name_len, |
| 1761 | &vstart, &vend, delim)) |
| 1762 | return 0; |
| 1763 | |
| 1764 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 1765 | * if the value is wrapped, copy-it in a buffer. |
| 1766 | */ |
| 1767 | smp->data.type = SMP_T_STR; |
| 1768 | if (chunks[2] && |
| 1769 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 1770 | vend >= chunks[2] && vend <= chunks[3]) { |
| 1771 | /* Wrapped case. */ |
| 1772 | temp = get_trash_chunk(); |
| 1773 | memcpy(temp->area, vstart, chunks[1] - vstart); |
| 1774 | memcpy(temp->area + ( chunks[1] - vstart ), chunks[2], |
| 1775 | vend - chunks[2]); |
| 1776 | smp->data.u.str.area = temp->area; |
| 1777 | smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
| 1778 | } else { |
| 1779 | /* Contiguous case. */ |
| 1780 | smp->data.u.str.area = (char *)vstart; |
| 1781 | smp->data.u.str.data = vend - vstart; |
| 1782 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1783 | } |
| 1784 | |
| 1785 | /* Update context, check wrapping. */ |
| 1786 | chunks[0] = vend; |
| 1787 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 1788 | chunks[1] = chunks[3]; |
| 1789 | chunks[2] = NULL; |
| 1790 | } |
| 1791 | |
| 1792 | if (chunks[0] < chunks[1]) |
| 1793 | smp->flags |= SMP_F_NOT_LAST; |
| 1794 | |
| 1795 | return 1; |
| 1796 | } |
| 1797 | |
| 1798 | /* This function iterates over each parameter of the query string. It uses |
| 1799 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the current |
| 1800 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 1801 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 1802 | * considered. It supports an optional delimiter argument for the beginning of |
| 1803 | * the string in args[1], which defaults to "?". |
| 1804 | */ |
| 1805 | static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1806 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1807 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1808 | char delim = '?'; |
| 1809 | const char *name; |
| 1810 | int name_len; |
| 1811 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1812 | if ((args[0].type && args[0].type != ARGT_STR) || |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1813 | (args[1].type && args[1].type != ARGT_STR)) |
| 1814 | return 0; |
| 1815 | |
| 1816 | name = ""; |
| 1817 | name_len = 0; |
| 1818 | if (args->type == ARGT_STR) { |
| 1819 | name = args->data.str.area; |
| 1820 | name_len = args->data.str.data; |
| 1821 | } |
| 1822 | |
| 1823 | if (args[1].type) |
| 1824 | delim = *args[1].data.str.area; |
| 1825 | |
| 1826 | if (!smp->ctx.a[0]) { // first call, find the query string |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1827 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1828 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1829 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1830 | if (!htx) |
| 1831 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1832 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1833 | sl = http_get_stline(htx); |
| 1834 | smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim); |
| 1835 | if (!smp->ctx.a[0]) |
| 1836 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1837 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1838 | smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1839 | |
| 1840 | /* Assume that the context is filled with NULL pointer |
| 1841 | * before the first call. |
| 1842 | * smp->ctx.a[2] = NULL; |
| 1843 | * smp->ctx.a[3] = NULL; |
| 1844 | */ |
| 1845 | } |
| 1846 | |
| 1847 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 1848 | } |
| 1849 | |
| 1850 | /* This function iterates over each parameter of the body. This requires |
| 1851 | * that the body has been waited for using http-buffer-request. It uses |
| 1852 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1853 | * contiguous part of the body, and optionally ctx->a[2..3] to reference the |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1854 | * optional second part if the body wraps at the end of the buffer. An optional |
| 1855 | * parameter name is passed in args[0], otherwise any parameter is considered. |
| 1856 | */ |
| 1857 | static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1858 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1859 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1860 | const char *name; |
| 1861 | int name_len; |
| 1862 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1863 | if (args[0].type && args[0].type != ARGT_STR) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1864 | return 0; |
| 1865 | |
| 1866 | name = ""; |
| 1867 | name_len = 0; |
| 1868 | if (args[0].type == ARGT_STR) { |
| 1869 | name = args[0].data.str.area; |
| 1870 | name_len = args[0].data.str.data; |
| 1871 | } |
| 1872 | |
| 1873 | if (!smp->ctx.a[0]) { // first call, find the query string |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 1874 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1875 | struct buffer *temp; |
| 1876 | int32_t pos; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1877 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1878 | if (!htx) |
| 1879 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1880 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1881 | temp = get_trash_chunk(); |
| 1882 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 1883 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1884 | enum htx_blk_type type = htx_get_blk_type(blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1885 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1886 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1887 | break; |
| 1888 | if (type == HTX_BLK_DATA) { |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 1889 | if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0)) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1890 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1891 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1892 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1893 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1894 | smp->ctx.a[0] = temp->area; |
| 1895 | smp->ctx.a[1] = temp->area + temp->data; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1896 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1897 | /* Assume that the context is filled with NULL pointer |
| 1898 | * before the first call. |
| 1899 | * smp->ctx.a[2] = NULL; |
| 1900 | * smp->ctx.a[3] = NULL; |
| 1901 | */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1902 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1903 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1904 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1905 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 1906 | } |
| 1907 | |
| 1908 | /* Return the signed integer value for the specified url parameter (see url_param |
| 1909 | * above). |
| 1910 | */ |
| 1911 | static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1912 | { |
| 1913 | int ret = smp_fetch_url_param(args, smp, kw, private); |
| 1914 | |
| 1915 | if (ret > 0) { |
| 1916 | smp->data.type = SMP_T_SINT; |
| 1917 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 1918 | smp->data.u.str.data); |
| 1919 | } |
| 1920 | |
| 1921 | return ret; |
| 1922 | } |
| 1923 | |
| 1924 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 1925 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 1926 | * This means that '*' will not be added, resulting in exactly the first Host |
| 1927 | * entry. If no Host header is found, then the path is used. The resulting value |
| 1928 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 1929 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 1930 | * high-traffic sites without having to store whole paths. |
| 1931 | * this differs from the base32 functions in that it includes the url parameters |
| 1932 | * as well as the path |
| 1933 | */ |
| 1934 | static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1935 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1936 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1937 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1938 | struct http_hdr_ctx ctx; |
| 1939 | struct htx_sl *sl; |
| 1940 | struct ist path; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1941 | unsigned int hash = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1942 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1943 | if (!htx) |
| 1944 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1945 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1946 | ctx.blk = NULL; |
| 1947 | if (http_find_header(htx, ist("Host"), &ctx, 1)) { |
| 1948 | /* OK we have the header value in ctx.value */ |
| 1949 | while (ctx.value.len--) |
| 1950 | hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1951 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1952 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1953 | /* now retrieve the path */ |
| 1954 | sl = http_get_stline(htx); |
| 1955 | path = http_get_path(htx_sl_req_uri(sl)); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1956 | if (path.len && *(path.ptr) == '/') { |
| 1957 | while (path.len--) |
| 1958 | hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1959 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1960 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1961 | hash = full_hash(hash); |
| 1962 | |
| 1963 | smp->data.type = SMP_T_SINT; |
| 1964 | smp->data.u.sint = hash; |
| 1965 | smp->flags = SMP_F_VOL_1ST; |
| 1966 | return 1; |
| 1967 | } |
| 1968 | |
| 1969 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 1970 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 1971 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 1972 | * on the source address length. The URL hash is stored before the address so |
| 1973 | * that in environments where IPv6 is insignificant, truncating the output to |
| 1974 | * 8 bytes would still work. |
| 1975 | */ |
| 1976 | static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1977 | { |
| 1978 | struct buffer *temp; |
| 1979 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
| 1980 | |
Willy Tarreau | cd7ca79 | 2019-07-17 16:57:03 +0200 | [diff] [blame] | 1981 | if (!cli_conn || !conn_get_src(cli_conn)) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1982 | return 0; |
| 1983 | |
| 1984 | if (!smp_fetch_url32(args, smp, kw, private)) |
| 1985 | return 0; |
| 1986 | |
| 1987 | temp = get_trash_chunk(); |
| 1988 | *(unsigned int *) temp->area = htonl(smp->data.u.sint); |
| 1989 | temp->data += sizeof(unsigned int); |
| 1990 | |
Willy Tarreau | 9a1efe1 | 2019-07-17 17:13:50 +0200 | [diff] [blame] | 1991 | switch (cli_conn->src->ss_family) { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1992 | case AF_INET: |
| 1993 | memcpy(temp->area + temp->data, |
Willy Tarreau | 9a1efe1 | 2019-07-17 17:13:50 +0200 | [diff] [blame] | 1994 | &((struct sockaddr_in *)cli_conn->src)->sin_addr, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1995 | 4); |
| 1996 | temp->data += 4; |
| 1997 | break; |
| 1998 | case AF_INET6: |
| 1999 | memcpy(temp->area + temp->data, |
Willy Tarreau | 9a1efe1 | 2019-07-17 17:13:50 +0200 | [diff] [blame] | 2000 | &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2001 | 16); |
| 2002 | temp->data += 16; |
| 2003 | break; |
| 2004 | default: |
| 2005 | return 0; |
| 2006 | } |
| 2007 | |
| 2008 | smp->data.u.str = *temp; |
| 2009 | smp->data.type = SMP_T_BIN; |
| 2010 | return 1; |
| 2011 | } |
| 2012 | |
| 2013 | /************************************************************************/ |
| 2014 | /* Other utility functions */ |
| 2015 | /************************************************************************/ |
| 2016 | |
| 2017 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 2018 | * keyword. These keywords support an optional positive or negative occurrence |
| 2019 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 2020 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 2021 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 2022 | * error message in case of error, that the caller is responsible for freeing. |
| 2023 | * The initial location must either be freeable or NULL. |
| 2024 | * Note: this function's pointer is checked from Lua. |
| 2025 | */ |
| 2026 | int val_hdr(struct arg *arg, char **err_msg) |
| 2027 | { |
| 2028 | if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) { |
| 2029 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
| 2030 | return 0; |
| 2031 | } |
| 2032 | return 1; |
| 2033 | } |
| 2034 | |
| 2035 | /************************************************************************/ |
| 2036 | /* All supported sample fetch keywords must be declared here. */ |
| 2037 | /************************************************************************/ |
| 2038 | |
| 2039 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 2040 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 2041 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2042 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2043 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
Yves Lafon | b4d3708 | 2021-02-11 11:01:28 +0100 | [diff] [blame] | 2044 | { "baseq", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2045 | |
| 2046 | /* capture are allocated and are permanent in the stream */ |
| 2047 | { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2048 | |
| 2049 | /* retrieve these captures from the HTTP logs */ |
| 2050 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2051 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2052 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2053 | |
| 2054 | { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 2055 | { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2056 | |
| 2057 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 2058 | * are only here to match the ACL's name, are request-only and are used |
| 2059 | * for ACL compatibility only. |
| 2060 | */ |
| 2061 | { "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] | 2062 | { "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] | 2063 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2064 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2065 | |
| 2066 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 2067 | * only here to match the ACL's name, are request-only and are used for |
| 2068 | * ACL compatibility only. |
| 2069 | */ |
Christopher Faulet | c1f40dd | 2019-05-16 10:07:30 +0200 | [diff] [blame] | 2070 | { "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] | 2071 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2072 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2073 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2074 | |
Christopher Faulet | a406356 | 2019-08-02 11:51:37 +0200 | [diff] [blame] | 2075 | { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2076 | { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2077 | { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2078 | { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
| 2079 | { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2080 | { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2081 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
| 2082 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Christopher Faulet | e720c32 | 2020-09-02 17:25:18 +0200 | [diff] [blame] | 2083 | { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2084 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2085 | |
| 2086 | /* HTTP protocol on the request path */ |
| 2087 | { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2088 | { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2089 | |
| 2090 | /* HTTP version on the request path */ |
| 2091 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2092 | { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2093 | |
| 2094 | { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2095 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2096 | { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2097 | { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2098 | |
| 2099 | { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2100 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2101 | |
| 2102 | /* HTTP version on the response path */ |
| 2103 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2104 | { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2105 | |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 2106 | { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV }, |
| 2107 | { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2108 | { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2109 | |
| 2110 | { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV }, |
| 2111 | { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV }, |
| 2112 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2113 | /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */ |
| 2114 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2115 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2116 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2117 | |
| 2118 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
| 2119 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2120 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
| 2121 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2122 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2123 | { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2124 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2125 | |
| 2126 | /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */ |
| 2127 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2128 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2129 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2130 | |
| 2131 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2132 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2133 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2134 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2135 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 2136 | { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2137 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2138 | |
| 2139 | /* scook is valid only on the response and is used for ACL compatibility */ |
| 2140 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2141 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2142 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2143 | { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */ |
| 2144 | |
| 2145 | /* shdr is valid only on the response and is used for ACL compatibility */ |
| 2146 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2147 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2148 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 2149 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2150 | |
| 2151 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
| 2152 | { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV }, |
| 2153 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2154 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2155 | { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2156 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2157 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2158 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2159 | { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2160 | { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 2161 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2162 | { /* END */ }, |
| 2163 | }}; |
| 2164 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2165 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2166 | |
| 2167 | /* |
| 2168 | * Local variables: |
| 2169 | * c-indent-level: 8 |
| 2170 | * c-basic-offset: 8 |
| 2171 | * End: |
| 2172 | */ |