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