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