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