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