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