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