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