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; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 1042 | struct http_uri_parser parser; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1043 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1044 | if (!htx) |
| 1045 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1046 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1047 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 1048 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
Christopher Faulet | e720c32 | 2020-09-02 17:25:18 +0200 | [diff] [blame] | 1049 | |
Yves Lafon | b4d3708 | 2021-02-11 11:01:28 +0100 | [diff] [blame] | 1050 | if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 1051 | path = http_parse_path(&parser); |
Christopher Faulet | e720c32 | 2020-09-02 17:25:18 +0200 | [diff] [blame] | 1052 | else |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 1053 | path = iststop(http_parse_path(&parser), '?'); |
Christopher Faulet | e720c32 | 2020-09-02 17:25:18 +0200 | [diff] [blame] | 1054 | |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 1055 | if (!isttest(path)) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1056 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1057 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1058 | /* OK, we got the '/' ! */ |
| 1059 | smp->data.type = SMP_T_STR; |
| 1060 | smp->data.u.str.area = path.ptr; |
Jerome Magnin | 4fb196c | 2020-02-21 10:49:12 +0100 | [diff] [blame] | 1061 | smp->data.u.str.data = path.len; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1062 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1063 | return 1; |
| 1064 | } |
| 1065 | |
| 1066 | /* This produces a concatenation of the first occurrence of the Host header |
| 1067 | * followed by the path component if it begins with a slash ('/'). This means |
| 1068 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 1069 | * If no Host header is found, then the path is returned as-is. The returned |
| 1070 | * value is stored in the trash so it does not need to be marked constant. |
| 1071 | * The returned sample is of type string. |
| 1072 | */ |
| 1073 | static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1074 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1075 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1076 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1077 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1078 | struct buffer *temp; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1079 | struct http_hdr_ctx ctx; |
| 1080 | struct ist path; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 1081 | struct http_uri_parser parser; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1082 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1083 | if (!htx) |
| 1084 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1085 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1086 | ctx.blk = NULL; |
| 1087 | if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len) |
| 1088 | return smp_fetch_path(args, smp, kw, private); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1089 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1090 | /* OK we have the header value in ctx.value */ |
| 1091 | temp = get_trash_chunk(); |
| 1092 | chunk_memcat(temp, ctx.value.ptr, ctx.value.len); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1093 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1094 | /* now retrieve the path */ |
| 1095 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 1096 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 1097 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 1098 | if (isttest(path)) { |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1099 | size_t len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1100 | |
Yves Lafon | b4d3708 | 2021-02-11 11:01:28 +0100 | [diff] [blame] | 1101 | if (kw[4] == 'q' && kw[0] == 'b') { // baseq |
| 1102 | len = path.len; |
| 1103 | } else { |
| 1104 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++) |
| 1105 | ; |
| 1106 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1107 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1108 | if (len && *(path.ptr) == '/') |
| 1109 | chunk_memcat(temp, path.ptr, len); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1110 | } |
| 1111 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1112 | smp->data.type = SMP_T_STR; |
| 1113 | smp->data.u.str = *temp; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1114 | smp->flags = SMP_F_VOL_1ST; |
| 1115 | return 1; |
| 1116 | } |
| 1117 | |
| 1118 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 1119 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 1120 | * This means that '*' will not be added, resulting in exactly the first Host |
| 1121 | * entry. If no Host header is found, then the path is used. The resulting value |
| 1122 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 1123 | * 32-bit integer value. This fetch is useful for tracking per-path activity on |
| 1124 | * high-traffic sites without having to store whole paths. |
| 1125 | */ |
| 1126 | static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1127 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1128 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1129 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1130 | struct htx_sl *sl; |
| 1131 | struct http_hdr_ctx ctx; |
| 1132 | struct ist path; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1133 | unsigned int hash = 0; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 1134 | struct http_uri_parser parser; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1135 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1136 | if (!htx) |
| 1137 | return 0; |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1138 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1139 | ctx.blk = NULL; |
| 1140 | if (http_find_header(htx, ist("Host"), &ctx, 0)) { |
| 1141 | /* OK we have the header value in ctx.value */ |
| 1142 | while (ctx.value.len--) |
| 1143 | hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1144 | } |
| 1145 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1146 | /* now retrieve the path */ |
| 1147 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 1148 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 1149 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 1150 | if (isttest(path)) { |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1151 | size_t len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1152 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1153 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++) |
| 1154 | ; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1155 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1156 | if (len && *(path.ptr) == '/') { |
| 1157 | while (len--) |
| 1158 | hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1159 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1160 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1161 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1162 | hash = full_hash(hash); |
| 1163 | |
| 1164 | smp->data.type = SMP_T_SINT; |
| 1165 | smp->data.u.sint = hash; |
| 1166 | smp->flags = SMP_F_VOL_1ST; |
| 1167 | return 1; |
| 1168 | } |
| 1169 | |
| 1170 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 1171 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 1172 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 1173 | * on the source address length. The path hash is stored before the address so |
| 1174 | * that in environments where IPv6 is insignificant, truncating the output to |
| 1175 | * 8 bytes would still work. |
| 1176 | */ |
| 1177 | static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1178 | { |
| 1179 | struct buffer *temp; |
| 1180 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
| 1181 | |
Willy Tarreau | cd7ca79 | 2019-07-17 16:57:03 +0200 | [diff] [blame] | 1182 | if (!cli_conn || !conn_get_src(cli_conn)) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1183 | return 0; |
| 1184 | |
| 1185 | if (!smp_fetch_base32(args, smp, kw, private)) |
| 1186 | return 0; |
| 1187 | |
| 1188 | temp = get_trash_chunk(); |
| 1189 | *(unsigned int *) temp->area = htonl(smp->data.u.sint); |
| 1190 | temp->data += sizeof(unsigned int); |
| 1191 | |
Willy Tarreau | 9a1efe1 | 2019-07-17 17:13:50 +0200 | [diff] [blame] | 1192 | switch (cli_conn->src->ss_family) { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1193 | case AF_INET: |
| 1194 | memcpy(temp->area + temp->data, |
Willy Tarreau | 9a1efe1 | 2019-07-17 17:13:50 +0200 | [diff] [blame] | 1195 | &((struct sockaddr_in *)cli_conn->src)->sin_addr, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1196 | 4); |
| 1197 | temp->data += 4; |
| 1198 | break; |
| 1199 | case AF_INET6: |
| 1200 | memcpy(temp->area + temp->data, |
Willy Tarreau | 9a1efe1 | 2019-07-17 17:13:50 +0200 | [diff] [blame] | 1201 | &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1202 | 16); |
| 1203 | temp->data += 16; |
| 1204 | break; |
| 1205 | default: |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | smp->data.u.str = *temp; |
| 1210 | smp->data.type = SMP_T_BIN; |
| 1211 | return 1; |
| 1212 | } |
| 1213 | |
| 1214 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 1215 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 1216 | * of type string carrying the whole query string. |
| 1217 | */ |
| 1218 | static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1219 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1220 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1221 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1222 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1223 | char *ptr, *end; |
| 1224 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1225 | if (!htx) |
| 1226 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1227 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1228 | sl = http_get_stline(htx); |
| 1229 | ptr = HTX_SL_REQ_UPTR(sl); |
| 1230 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1231 | |
| 1232 | /* look up the '?' */ |
| 1233 | do { |
| 1234 | if (ptr == end) |
| 1235 | return 0; |
| 1236 | } while (*ptr++ != '?'); |
| 1237 | |
| 1238 | smp->data.type = SMP_T_STR; |
| 1239 | smp->data.u.str.area = ptr; |
| 1240 | smp->data.u.str.data = end - ptr; |
| 1241 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1242 | return 1; |
| 1243 | } |
| 1244 | |
| 1245 | static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1246 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1247 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1248 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1249 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1250 | if (!htx) |
| 1251 | return 0; |
| 1252 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1253 | smp->data.u.sint = 1; |
| 1254 | return 1; |
| 1255 | } |
| 1256 | |
| 1257 | /* return a valid test if the current request is the first one on the connection */ |
| 1258 | static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1259 | { |
Willy Tarreau | 79512b6 | 2020-04-29 11:52:13 +0200 | [diff] [blame] | 1260 | if (!smp->strm) |
| 1261 | return 0; |
| 1262 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1263 | smp->data.type = SMP_T_BOOL; |
| 1264 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
| 1265 | return 1; |
| 1266 | } |
| 1267 | |
Christopher Faulet | a406356 | 2019-08-02 11:51:37 +0200 | [diff] [blame] | 1268 | /* Fetch the authentication method if there is an Authorization header. It |
| 1269 | * relies on get_http_auth() |
| 1270 | */ |
| 1271 | static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1272 | { |
| 1273 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1274 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | a406356 | 2019-08-02 11:51:37 +0200 | [diff] [blame] | 1275 | struct http_txn *txn; |
| 1276 | |
| 1277 | if (!htx) |
| 1278 | return 0; |
| 1279 | |
| 1280 | txn = smp->strm->txn; |
| 1281 | if (!get_http_auth(smp, htx)) |
| 1282 | return 0; |
| 1283 | |
| 1284 | switch (txn->auth.method) { |
| 1285 | case HTTP_AUTH_BASIC: |
| 1286 | smp->data.u.str.area = "Basic"; |
| 1287 | smp->data.u.str.data = 5; |
| 1288 | break; |
| 1289 | case HTTP_AUTH_DIGEST: |
| 1290 | /* Unexpected because not supported */ |
| 1291 | smp->data.u.str.area = "Digest"; |
| 1292 | smp->data.u.str.data = 6; |
| 1293 | break; |
| 1294 | default: |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | smp->data.type = SMP_T_STR; |
| 1299 | smp->flags = SMP_F_CONST; |
| 1300 | return 1; |
| 1301 | } |
| 1302 | |
| 1303 | /* Fetch the user supplied if there is an Authorization header. It relies on |
| 1304 | * get_http_auth() |
| 1305 | */ |
| 1306 | static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1307 | { |
| 1308 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1309 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | a406356 | 2019-08-02 11:51:37 +0200 | [diff] [blame] | 1310 | struct http_txn *txn; |
| 1311 | |
| 1312 | if (!htx) |
| 1313 | return 0; |
| 1314 | |
| 1315 | txn = smp->strm->txn; |
| 1316 | if (!get_http_auth(smp, htx)) |
| 1317 | return 0; |
| 1318 | |
| 1319 | smp->data.type = SMP_T_STR; |
| 1320 | smp->data.u.str.area = txn->auth.user; |
| 1321 | smp->data.u.str.data = strlen(txn->auth.user); |
| 1322 | smp->flags = SMP_F_CONST; |
| 1323 | return 1; |
| 1324 | } |
| 1325 | |
| 1326 | /* Fetch the password supplied if there is an Authorization header. It relies on |
| 1327 | * get_http_auth() |
| 1328 | */ |
| 1329 | static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1330 | { |
| 1331 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1332 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | a406356 | 2019-08-02 11:51:37 +0200 | [diff] [blame] | 1333 | struct http_txn *txn; |
| 1334 | |
| 1335 | if (!htx) |
| 1336 | return 0; |
| 1337 | |
| 1338 | txn = smp->strm->txn; |
| 1339 | if (!get_http_auth(smp, htx)) |
| 1340 | return 0; |
| 1341 | |
| 1342 | smp->data.type = SMP_T_STR; |
| 1343 | smp->data.u.str.area = txn->auth.pass; |
| 1344 | smp->data.u.str.data = strlen(txn->auth.pass); |
| 1345 | smp->flags = SMP_F_CONST; |
| 1346 | return 1; |
| 1347 | } |
| 1348 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1349 | /* Accepts exactly 1 argument of type userlist */ |
| 1350 | static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1351 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1352 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1353 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1354 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1355 | if (args->type != ARGT_USR) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1356 | return 0; |
| 1357 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1358 | if (!htx) |
| 1359 | return 0; |
| 1360 | if (!get_http_auth(smp, htx)) |
| 1361 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1362 | |
| 1363 | smp->data.type = SMP_T_BOOL; |
| 1364 | 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] | 1365 | smp->strm->txn->auth.pass); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1366 | return 1; |
| 1367 | } |
| 1368 | |
| 1369 | /* Accepts exactly 1 argument of type userlist */ |
| 1370 | static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1371 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1372 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1373 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1374 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1375 | if (args->type != ARGT_USR) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1376 | return 0; |
| 1377 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1378 | if (!htx) |
| 1379 | return 0; |
| 1380 | if (!get_http_auth(smp, htx)) |
| 1381 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1382 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1383 | /* if the user does not belong to the userlist or has a wrong password, |
| 1384 | * report that it unconditionally does not match. Otherwise we return |
| 1385 | * a string containing the username. |
| 1386 | */ |
| 1387 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 1388 | smp->strm->txn->auth.pass)) |
| 1389 | return 0; |
| 1390 | |
| 1391 | /* pat_match_auth() will need the user list */ |
| 1392 | smp->ctx.a[0] = args->data.usr; |
| 1393 | |
| 1394 | smp->data.type = SMP_T_STR; |
| 1395 | smp->flags = SMP_F_CONST; |
| 1396 | smp->data.u.str.area = smp->strm->txn->auth.user; |
| 1397 | smp->data.u.str.data = strlen(smp->strm->txn->auth.user); |
| 1398 | |
| 1399 | return 1; |
| 1400 | } |
| 1401 | |
| 1402 | /* Fetch a captured HTTP request header. The index is the position of |
| 1403 | * the "capture" option in the configuration file |
| 1404 | */ |
| 1405 | static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1406 | { |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1407 | struct proxy *fe; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1408 | int idx; |
| 1409 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1410 | if (args->type != ARGT_SINT) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1411 | return 0; |
| 1412 | |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1413 | if (!smp->strm) |
| 1414 | return 0; |
| 1415 | |
| 1416 | fe = strm_fe(smp->strm); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1417 | idx = args->data.sint; |
| 1418 | |
| 1419 | if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL) |
| 1420 | return 0; |
| 1421 | |
| 1422 | smp->data.type = SMP_T_STR; |
| 1423 | smp->flags |= SMP_F_CONST; |
| 1424 | smp->data.u.str.area = smp->strm->req_cap[idx]; |
| 1425 | smp->data.u.str.data = strlen(smp->strm->req_cap[idx]); |
| 1426 | |
| 1427 | return 1; |
| 1428 | } |
| 1429 | |
| 1430 | /* Fetch a captured HTTP response header. The index is the position of |
| 1431 | * the "capture" option in the configuration file |
| 1432 | */ |
| 1433 | static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1434 | { |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1435 | struct proxy *fe; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1436 | int idx; |
| 1437 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1438 | if (args->type != ARGT_SINT) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1439 | return 0; |
| 1440 | |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1441 | if (!smp->strm) |
| 1442 | return 0; |
| 1443 | |
| 1444 | fe = strm_fe(smp->strm); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1445 | idx = args->data.sint; |
| 1446 | |
| 1447 | if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL) |
| 1448 | return 0; |
| 1449 | |
| 1450 | smp->data.type = SMP_T_STR; |
| 1451 | smp->flags |= SMP_F_CONST; |
| 1452 | smp->data.u.str.area = smp->strm->res_cap[idx]; |
| 1453 | smp->data.u.str.data = strlen(smp->strm->res_cap[idx]); |
| 1454 | |
| 1455 | return 1; |
| 1456 | } |
| 1457 | |
| 1458 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 1459 | static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1460 | { |
| 1461 | struct buffer *temp; |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1462 | struct http_txn *txn; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1463 | char *ptr; |
| 1464 | |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1465 | if (!smp->strm) |
| 1466 | return 0; |
| 1467 | |
| 1468 | txn = smp->strm->txn; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1469 | if (!txn || !txn->uri) |
| 1470 | return 0; |
| 1471 | |
| 1472 | ptr = txn->uri; |
| 1473 | |
| 1474 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 1475 | ptr++; |
| 1476 | |
| 1477 | temp = get_trash_chunk(); |
| 1478 | temp->area = txn->uri; |
| 1479 | temp->data = ptr - txn->uri; |
| 1480 | smp->data.u.str = *temp; |
| 1481 | smp->data.type = SMP_T_STR; |
| 1482 | smp->flags = SMP_F_CONST; |
| 1483 | |
| 1484 | return 1; |
| 1485 | |
| 1486 | } |
| 1487 | |
| 1488 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 1489 | static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1490 | { |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1491 | struct http_txn *txn; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1492 | struct ist path; |
| 1493 | const char *ptr; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 1494 | struct http_uri_parser parser; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1495 | |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1496 | if (!smp->strm) |
| 1497 | return 0; |
| 1498 | |
| 1499 | txn = smp->strm->txn; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1500 | if (!txn || !txn->uri) |
| 1501 | return 0; |
| 1502 | |
| 1503 | ptr = txn->uri; |
| 1504 | |
| 1505 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 1506 | ptr++; |
| 1507 | |
| 1508 | if (!*ptr) |
| 1509 | return 0; |
| 1510 | |
Christopher Faulet | 78337bb | 2018-11-15 14:35:18 +0100 | [diff] [blame] | 1511 | /* skip the first space and find space after URI */ |
| 1512 | path = ist2(++ptr, 0); |
| 1513 | while (*ptr != ' ' && *ptr != '\0') |
| 1514 | ptr++; |
| 1515 | path.len = ptr - path.ptr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1516 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 1517 | parser = http_uri_parser_init(path); |
| 1518 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 1519 | if (!isttest(path)) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1520 | return 0; |
| 1521 | |
| 1522 | smp->data.u.str.area = path.ptr; |
| 1523 | smp->data.u.str.data = path.len; |
| 1524 | smp->data.type = SMP_T_STR; |
| 1525 | smp->flags = SMP_F_CONST; |
| 1526 | |
| 1527 | return 1; |
| 1528 | } |
| 1529 | |
| 1530 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 1531 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 1532 | */ |
| 1533 | static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1534 | { |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1535 | struct http_txn *txn; |
| 1536 | |
| 1537 | if (!smp->strm) |
| 1538 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1539 | |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1540 | txn = smp->strm->txn; |
Christopher Faulet | 09f8836 | 2021-04-01 16:00:29 +0200 | [diff] [blame] | 1541 | if (!txn || txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1542 | return 0; |
| 1543 | |
| 1544 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 1545 | smp->data.u.str.area = "HTTP/1.1"; |
| 1546 | else |
| 1547 | smp->data.u.str.area = "HTTP/1.0"; |
| 1548 | |
| 1549 | smp->data.u.str.data = 8; |
| 1550 | smp->data.type = SMP_T_STR; |
| 1551 | smp->flags = SMP_F_CONST; |
| 1552 | return 1; |
| 1553 | |
| 1554 | } |
| 1555 | |
| 1556 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 1557 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 1558 | */ |
| 1559 | static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1560 | { |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1561 | struct http_txn *txn; |
| 1562 | |
| 1563 | if (!smp->strm) |
| 1564 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1565 | |
Willy Tarreau | 0898c2d | 2020-04-29 11:44:54 +0200 | [diff] [blame] | 1566 | txn = smp->strm->txn; |
Christopher Faulet | 09f8836 | 2021-04-01 16:00:29 +0200 | [diff] [blame] | 1567 | if (!txn || txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1568 | return 0; |
| 1569 | |
| 1570 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 1571 | smp->data.u.str.area = "HTTP/1.1"; |
| 1572 | else |
| 1573 | smp->data.u.str.area = "HTTP/1.0"; |
| 1574 | |
| 1575 | smp->data.u.str.data = 8; |
| 1576 | smp->data.type = SMP_T_STR; |
| 1577 | smp->flags = SMP_F_CONST; |
| 1578 | return 1; |
| 1579 | |
| 1580 | } |
| 1581 | |
| 1582 | /* Iterate over all cookies present in a message. The context is stored in |
| 1583 | * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the |
| 1584 | * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on |
| 1585 | * 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] | 1586 | * If provided, the searched cookie name is in args, in args->data.str. If |
| 1587 | * the input options indicate that no iterating is desired, then only last |
| 1588 | * value is fetched if any. If no cookie name is provided, the first cookie |
| 1589 | * value found is fetched. The returned sample is of type CSTR. Can be used |
| 1590 | * to parse cookies in other files. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1591 | */ |
| 1592 | static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1593 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1594 | /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */ |
| 1595 | 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] | 1596 | 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] | 1597 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1598 | struct http_hdr_ctx *ctx = smp->ctx.a[2]; |
| 1599 | struct ist hdr; |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1600 | char *cook = NULL; |
| 1601 | size_t cook_l = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1602 | int found = 0; |
| 1603 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1604 | if (args->type == ARGT_STR) { |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1605 | cook = args->data.str.area; |
| 1606 | cook_l = args->data.str.data; |
| 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 (!ctx) { |
| 1610 | /* first call */ |
| 1611 | ctx = &static_http_hdr_ctx; |
| 1612 | ctx->blk = NULL; |
| 1613 | smp->ctx.a[2] = ctx; |
| 1614 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1615 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1616 | if (!htx) |
| 1617 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1618 | |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 1619 | hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie")); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1620 | |
Maciej Zdeb | dea7c20 | 2020-11-13 09:38:06 +0000 | [diff] [blame] | 1621 | /* OK so basically here, either we want only one value or we want to |
| 1622 | * iterate over all of them and we fetch the next one. In this last case |
| 1623 | * SMP_OPT_ITERATE option is set. |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1624 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1625 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1626 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
| 1627 | /* search for the header from the beginning, we must first initialize |
| 1628 | * the search parameters. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1629 | */ |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1630 | smp->ctx.a[0] = NULL; |
| 1631 | ctx->blk = NULL; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1632 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1633 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1634 | smp->flags |= SMP_F_VOL_HDR; |
| 1635 | while (1) { |
| 1636 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 1637 | if (!smp->ctx.a[0]) { |
| 1638 | if (!http_find_header(htx, hdr, ctx, 0)) |
| 1639 | goto out; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1640 | |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1641 | if (ctx->value.len < cook_l + 1) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1642 | continue; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1643 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1644 | smp->ctx.a[0] = ctx->value.ptr; |
| 1645 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1646 | } |
| 1647 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1648 | smp->data.type = SMP_T_STR; |
| 1649 | smp->flags |= SMP_F_CONST; |
| 1650 | 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] | 1651 | cook, cook_l, |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1652 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 1653 | &smp->data.u.str.area, |
| 1654 | &smp->data.u.str.data); |
| 1655 | if (smp->ctx.a[0]) { |
| 1656 | found = 1; |
Maciej Zdeb | dea7c20 | 2020-11-13 09:38:06 +0000 | [diff] [blame] | 1657 | if (smp->opt & SMP_OPT_ITERATE) { |
| 1658 | /* iterate on cookie value */ |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1659 | smp->flags |= SMP_F_NOT_LAST; |
| 1660 | return 1; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1661 | } |
Maciej Zdeb | dea7c20 | 2020-11-13 09:38:06 +0000 | [diff] [blame] | 1662 | if (args->data.str.data == 0) { |
| 1663 | /* No cookie name, first occurrence returned */ |
| 1664 | break; |
| 1665 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1666 | } |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1667 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1668 | } |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1669 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1670 | /* all cookie headers and values were scanned. If we're looking for the |
| 1671 | * last occurrence, we may return it now. |
| 1672 | */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1673 | out: |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1674 | smp->flags &= ~SMP_F_NOT_LAST; |
| 1675 | return found; |
| 1676 | } |
| 1677 | |
Christopher Faulet | c1f40dd | 2019-05-16 10:07:30 +0200 | [diff] [blame] | 1678 | /* Same than smp_fetch_cookie() but only relies on the sample direction to |
| 1679 | * choose the right channel. So instead of duplicating the code, we just change |
| 1680 | * the keyword and then fallback on smp_fetch_cookie(). |
| 1681 | */ |
| 1682 | static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1683 | { |
| 1684 | kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook"); |
| 1685 | return smp_fetch_cookie(args, smp, kw, private); |
| 1686 | } |
| 1687 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1688 | /* Iterate over all cookies present in a request to count how many occurrences |
| 1689 | * match the name in args and args->data.str.len. If <multi> is non-null, then |
| 1690 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 1691 | * type UINT. Accepts exactly 1 argument of type string. |
| 1692 | */ |
| 1693 | static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1694 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1695 | /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */ |
| 1696 | 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] | 1697 | 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] | 1698 | struct htx *htx = smp_prefetch_htx(smp, chn, check, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1699 | struct http_hdr_ctx ctx; |
| 1700 | struct ist hdr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1701 | char *val_beg, *val_end; |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1702 | char *cook = NULL; |
| 1703 | size_t cook_l = 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1704 | int cnt; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1705 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1706 | if (args->type == ARGT_STR){ |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1707 | cook = args->data.str.area; |
| 1708 | cook_l = args->data.str.data; |
| 1709 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1710 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1711 | if (!htx) |
| 1712 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1713 | |
Christopher Faulet | 16032ab | 2020-04-30 11:30:00 +0200 | [diff] [blame] | 1714 | hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie")); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1715 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1716 | val_end = val_beg = NULL; |
| 1717 | ctx.blk = NULL; |
| 1718 | cnt = 0; |
| 1719 | while (1) { |
| 1720 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 1721 | if (!val_beg) { |
| 1722 | if (!http_find_header(htx, hdr, &ctx, 0)) |
| 1723 | break; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1724 | |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1725 | if (ctx.value.len < cook_l + 1) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1726 | continue; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1727 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1728 | val_beg = ctx.value.ptr; |
| 1729 | val_end = val_beg + ctx.value.len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1730 | } |
| 1731 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1732 | smp->data.type = SMP_T_STR; |
| 1733 | smp->flags |= SMP_F_CONST; |
| 1734 | while ((val_beg = http_extract_cookie_value(val_beg, val_end, |
Christopher Faulet | 97fc8da | 2020-11-13 13:41:04 +0100 | [diff] [blame] | 1735 | cook, cook_l, |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1736 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 1737 | &smp->data.u.str.area, |
| 1738 | &smp->data.u.str.data))) { |
| 1739 | cnt++; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | smp->data.type = SMP_T_SINT; |
| 1744 | smp->data.u.sint = cnt; |
| 1745 | smp->flags |= SMP_F_VOL_HDR; |
| 1746 | return 1; |
| 1747 | } |
| 1748 | |
| 1749 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 1750 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 1751 | */ |
| 1752 | static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1753 | { |
| 1754 | int ret = smp_fetch_cookie(args, smp, kw, private); |
| 1755 | |
| 1756 | if (ret > 0) { |
| 1757 | smp->data.type = SMP_T_SINT; |
| 1758 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 1759 | smp->data.u.str.data); |
| 1760 | } |
| 1761 | |
| 1762 | return ret; |
| 1763 | } |
| 1764 | |
| 1765 | /************************************************************************/ |
| 1766 | /* The code below is dedicated to sample fetches */ |
| 1767 | /************************************************************************/ |
| 1768 | |
| 1769 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1770 | * 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] | 1771 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 1772 | * pointers are updated for next iteration before leaving. |
| 1773 | */ |
| 1774 | 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) |
| 1775 | { |
| 1776 | const char *vstart, *vend; |
| 1777 | struct buffer *temp; |
| 1778 | const char **chunks = (const char **)smp->ctx.a; |
| 1779 | |
| 1780 | if (!http_find_next_url_param(chunks, name, name_len, |
| 1781 | &vstart, &vend, delim)) |
| 1782 | return 0; |
| 1783 | |
| 1784 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 1785 | * if the value is wrapped, copy-it in a buffer. |
| 1786 | */ |
| 1787 | smp->data.type = SMP_T_STR; |
| 1788 | if (chunks[2] && |
| 1789 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 1790 | vend >= chunks[2] && vend <= chunks[3]) { |
| 1791 | /* Wrapped case. */ |
| 1792 | temp = get_trash_chunk(); |
| 1793 | memcpy(temp->area, vstart, chunks[1] - vstart); |
| 1794 | memcpy(temp->area + ( chunks[1] - vstart ), chunks[2], |
| 1795 | vend - chunks[2]); |
| 1796 | smp->data.u.str.area = temp->area; |
| 1797 | smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
| 1798 | } else { |
| 1799 | /* Contiguous case. */ |
| 1800 | smp->data.u.str.area = (char *)vstart; |
| 1801 | smp->data.u.str.data = vend - vstart; |
| 1802 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1803 | } |
| 1804 | |
| 1805 | /* Update context, check wrapping. */ |
| 1806 | chunks[0] = vend; |
| 1807 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 1808 | chunks[1] = chunks[3]; |
| 1809 | chunks[2] = NULL; |
| 1810 | } |
| 1811 | |
| 1812 | if (chunks[0] < chunks[1]) |
| 1813 | smp->flags |= SMP_F_NOT_LAST; |
| 1814 | |
| 1815 | return 1; |
| 1816 | } |
| 1817 | |
| 1818 | /* This function iterates over each parameter of the query string. It uses |
| 1819 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the current |
| 1820 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 1821 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 1822 | * considered. It supports an optional delimiter argument for the beginning of |
| 1823 | * the string in args[1], which defaults to "?". |
| 1824 | */ |
| 1825 | static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1826 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1827 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1828 | char delim = '?'; |
| 1829 | const char *name; |
| 1830 | int name_len; |
| 1831 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1832 | if ((args[0].type && args[0].type != ARGT_STR) || |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1833 | (args[1].type && args[1].type != ARGT_STR)) |
| 1834 | return 0; |
| 1835 | |
| 1836 | name = ""; |
| 1837 | name_len = 0; |
| 1838 | if (args->type == ARGT_STR) { |
| 1839 | name = args->data.str.area; |
| 1840 | name_len = args->data.str.data; |
| 1841 | } |
| 1842 | |
| 1843 | if (args[1].type) |
| 1844 | delim = *args[1].data.str.area; |
| 1845 | |
| 1846 | if (!smp->ctx.a[0]) { // first call, find the query string |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1847 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1848 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1849 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1850 | if (!htx) |
| 1851 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1852 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1853 | sl = http_get_stline(htx); |
| 1854 | smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim); |
| 1855 | if (!smp->ctx.a[0]) |
| 1856 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1857 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1858 | 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] | 1859 | |
| 1860 | /* Assume that the context is filled with NULL pointer |
| 1861 | * before the first call. |
| 1862 | * smp->ctx.a[2] = NULL; |
| 1863 | * smp->ctx.a[3] = NULL; |
| 1864 | */ |
| 1865 | } |
| 1866 | |
| 1867 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 1868 | } |
| 1869 | |
| 1870 | /* This function iterates over each parameter of the body. This requires |
| 1871 | * that the body has been waited for using http-buffer-request. It uses |
| 1872 | * 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] | 1873 | * 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] | 1874 | * optional second part if the body wraps at the end of the buffer. An optional |
| 1875 | * parameter name is passed in args[0], otherwise any parameter is considered. |
| 1876 | */ |
| 1877 | static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1878 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1879 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1880 | const char *name; |
| 1881 | int name_len; |
| 1882 | |
Christopher Faulet | 623af93 | 2021-01-29 11:22:15 +0100 | [diff] [blame] | 1883 | if (args[0].type && args[0].type != ARGT_STR) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1884 | return 0; |
| 1885 | |
| 1886 | name = ""; |
| 1887 | name_len = 0; |
| 1888 | if (args[0].type == ARGT_STR) { |
| 1889 | name = args[0].data.str.area; |
| 1890 | name_len = args[0].data.str.data; |
| 1891 | } |
| 1892 | |
| 1893 | if (!smp->ctx.a[0]) { // first call, find the query string |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 1894 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1895 | struct buffer *temp; |
| 1896 | int32_t pos; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1897 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1898 | if (!htx) |
| 1899 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1900 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1901 | temp = get_trash_chunk(); |
| 1902 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 1903 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1904 | enum htx_blk_type type = htx_get_blk_type(blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1905 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1906 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1907 | break; |
| 1908 | if (type == HTX_BLK_DATA) { |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 1909 | 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] | 1910 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1911 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1912 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1913 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1914 | smp->ctx.a[0] = temp->area; |
| 1915 | smp->ctx.a[1] = temp->area + temp->data; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1916 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1917 | /* Assume that the context is filled with NULL pointer |
| 1918 | * before the first call. |
| 1919 | * smp->ctx.a[2] = NULL; |
| 1920 | * smp->ctx.a[3] = NULL; |
| 1921 | */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1922 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1923 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1924 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1925 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 1926 | } |
| 1927 | |
| 1928 | /* Return the signed integer value for the specified url parameter (see url_param |
| 1929 | * above). |
| 1930 | */ |
| 1931 | static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1932 | { |
| 1933 | int ret = smp_fetch_url_param(args, smp, kw, private); |
| 1934 | |
| 1935 | if (ret > 0) { |
| 1936 | smp->data.type = SMP_T_SINT; |
| 1937 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 1938 | smp->data.u.str.data); |
| 1939 | } |
| 1940 | |
| 1941 | return ret; |
| 1942 | } |
| 1943 | |
| 1944 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 1945 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 1946 | * This means that '*' will not be added, resulting in exactly the first Host |
| 1947 | * entry. If no Host header is found, then the path is used. The resulting value |
| 1948 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 1949 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 1950 | * high-traffic sites without having to store whole paths. |
| 1951 | * this differs from the base32 functions in that it includes the url parameters |
| 1952 | * as well as the path |
| 1953 | */ |
| 1954 | static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1955 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1956 | struct channel *chn = SMP_REQ_CHN(smp); |
Christopher Faulet | 778f5ed | 2020-04-29 15:51:55 +0200 | [diff] [blame] | 1957 | struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1958 | struct http_hdr_ctx ctx; |
| 1959 | struct htx_sl *sl; |
| 1960 | struct ist path; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1961 | unsigned int hash = 0; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 1962 | struct http_uri_parser parser; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1963 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1964 | if (!htx) |
| 1965 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1966 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1967 | ctx.blk = NULL; |
| 1968 | if (http_find_header(htx, ist("Host"), &ctx, 1)) { |
| 1969 | /* OK we have the header value in ctx.value */ |
| 1970 | while (ctx.value.len--) |
| 1971 | hash = *(ctx.value.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 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1974 | /* now retrieve the path */ |
| 1975 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 1976 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 1977 | path = http_parse_path(&parser); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1978 | if (path.len && *(path.ptr) == '/') { |
| 1979 | while (path.len--) |
| 1980 | hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1981 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1982 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1983 | hash = full_hash(hash); |
| 1984 | |
| 1985 | smp->data.type = SMP_T_SINT; |
| 1986 | smp->data.u.sint = hash; |
| 1987 | smp->flags = SMP_F_VOL_1ST; |
| 1988 | return 1; |
| 1989 | } |
| 1990 | |
| 1991 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 1992 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 1993 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 1994 | * on the source address length. The URL hash is stored before the address so |
| 1995 | * that in environments where IPv6 is insignificant, truncating the output to |
| 1996 | * 8 bytes would still work. |
| 1997 | */ |
| 1998 | static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1999 | { |
| 2000 | struct buffer *temp; |
| 2001 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
| 2002 | |
Willy Tarreau | cd7ca79 | 2019-07-17 16:57:03 +0200 | [diff] [blame] | 2003 | if (!cli_conn || !conn_get_src(cli_conn)) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2004 | return 0; |
| 2005 | |
| 2006 | if (!smp_fetch_url32(args, smp, kw, private)) |
| 2007 | return 0; |
| 2008 | |
| 2009 | temp = get_trash_chunk(); |
| 2010 | *(unsigned int *) temp->area = htonl(smp->data.u.sint); |
| 2011 | temp->data += sizeof(unsigned int); |
| 2012 | |
Willy Tarreau | 9a1efe1 | 2019-07-17 17:13:50 +0200 | [diff] [blame] | 2013 | switch (cli_conn->src->ss_family) { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2014 | case AF_INET: |
| 2015 | memcpy(temp->area + temp->data, |
Willy Tarreau | 9a1efe1 | 2019-07-17 17:13:50 +0200 | [diff] [blame] | 2016 | &((struct sockaddr_in *)cli_conn->src)->sin_addr, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2017 | 4); |
| 2018 | temp->data += 4; |
| 2019 | break; |
| 2020 | case AF_INET6: |
| 2021 | memcpy(temp->area + temp->data, |
Willy Tarreau | 9a1efe1 | 2019-07-17 17:13:50 +0200 | [diff] [blame] | 2022 | &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2023 | 16); |
| 2024 | temp->data += 16; |
| 2025 | break; |
| 2026 | default: |
| 2027 | return 0; |
| 2028 | } |
| 2029 | |
| 2030 | smp->data.u.str = *temp; |
| 2031 | smp->data.type = SMP_T_BIN; |
| 2032 | return 1; |
| 2033 | } |
| 2034 | |
| 2035 | /************************************************************************/ |
| 2036 | /* Other utility functions */ |
| 2037 | /************************************************************************/ |
| 2038 | |
| 2039 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 2040 | * keyword. These keywords support an optional positive or negative occurrence |
| 2041 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 2042 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 2043 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 2044 | * error message in case of error, that the caller is responsible for freeing. |
| 2045 | * The initial location must either be freeable or NULL. |
| 2046 | * Note: this function's pointer is checked from Lua. |
| 2047 | */ |
| 2048 | int val_hdr(struct arg *arg, char **err_msg) |
| 2049 | { |
| 2050 | if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) { |
| 2051 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
| 2052 | return 0; |
| 2053 | } |
| 2054 | return 1; |
| 2055 | } |
| 2056 | |
| 2057 | /************************************************************************/ |
| 2058 | /* All supported sample fetch keywords must be declared here. */ |
| 2059 | /************************************************************************/ |
| 2060 | |
| 2061 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 2062 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 2063 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2064 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2065 | { "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] | 2066 | { "baseq", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2067 | |
| 2068 | /* capture are allocated and are permanent in the stream */ |
| 2069 | { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2070 | |
| 2071 | /* retrieve these captures from the HTTP logs */ |
| 2072 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2073 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2074 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2075 | |
| 2076 | { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 2077 | { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2078 | |
| 2079 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 2080 | * are only here to match the ACL's name, are request-only and are used |
| 2081 | * for ACL compatibility only. |
| 2082 | */ |
| 2083 | { "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] | 2084 | { "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] | 2085 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2086 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2087 | |
| 2088 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 2089 | * only here to match the ACL's name, are request-only and are used for |
| 2090 | * ACL compatibility only. |
| 2091 | */ |
Christopher Faulet | c1f40dd | 2019-05-16 10:07:30 +0200 | [diff] [blame] | 2092 | { "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] | 2093 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2094 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2095 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2096 | |
Christopher Faulet | a406356 | 2019-08-02 11:51:37 +0200 | [diff] [blame] | 2097 | { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2098 | { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2099 | { "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] | 2100 | { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
| 2101 | { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2102 | { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2103 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
| 2104 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Christopher Faulet | e720c32 | 2020-09-02 17:25:18 +0200 | [diff] [blame] | 2105 | { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2106 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2107 | |
| 2108 | /* HTTP protocol on the request path */ |
| 2109 | { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2110 | { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2111 | |
| 2112 | /* HTTP version on the request path */ |
| 2113 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2114 | { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2115 | |
| 2116 | { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2117 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2118 | { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2119 | { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2120 | |
| 2121 | { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2122 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2123 | |
| 2124 | /* HTTP version on the response path */ |
| 2125 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2126 | { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2127 | |
Christopher Faulet | e596d18 | 2020-05-05 17:46:34 +0200 | [diff] [blame] | 2128 | { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV }, |
| 2129 | { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2130 | { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2131 | |
| 2132 | { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV }, |
| 2133 | { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV }, |
| 2134 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2135 | /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */ |
| 2136 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2137 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2138 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2139 | |
| 2140 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
| 2141 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2142 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
| 2143 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2144 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2145 | { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2146 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2147 | |
| 2148 | /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */ |
| 2149 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2150 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2151 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2152 | |
| 2153 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2154 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2155 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2156 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2157 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 2158 | { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2159 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2160 | |
| 2161 | /* scook is valid only on the response and is used for ACL compatibility */ |
| 2162 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2163 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2164 | { "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] | 2165 | |
| 2166 | /* shdr is valid only on the response and is used for ACL compatibility */ |
| 2167 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2168 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2169 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 2170 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2171 | |
| 2172 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
| 2173 | { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV }, |
| 2174 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2175 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2176 | { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2177 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2178 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2179 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2180 | { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2181 | { "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] | 2182 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2183 | { /* END */ }, |
| 2184 | }}; |
| 2185 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2186 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2187 | |
| 2188 | /* |
| 2189 | * Local variables: |
| 2190 | * c-indent-level: 8 |
| 2191 | * c-basic-offset: 8 |
| 2192 | * End: |
| 2193 | */ |