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