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