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