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