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