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