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> |
Willy Tarreau | 538746a | 2018-12-11 10:59:20 +0100 | [diff] [blame] | 36 | #include <proto/hdr_idx.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 37 | #include <proto/http_fetch.h> |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 38 | #include <proto/http_htx.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 39 | #include <proto/log.h> |
| 40 | #include <proto/obj_type.h> |
| 41 | #include <proto/proto_http.h> |
| 42 | #include <proto/sample.h> |
| 43 | #include <proto/stream.h> |
| 44 | |
| 45 | |
| 46 | /* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */ |
| 47 | static THREAD_LOCAL struct hdr_ctx static_hdr_ctx; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 48 | static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx; |
| 49 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 50 | #define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL) |
| 51 | #define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 52 | |
| 53 | /* |
| 54 | * Returns the data from Authorization header. Function may be called more |
| 55 | * than once so data is stored in txn->auth_data. When no header is found |
| 56 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 57 | * searching again for something we are unable to find anyway. However, if |
| 58 | * the result if valid, the cache is not reused because we would risk to |
| 59 | * have the credentials overwritten by another stream in parallel. |
| 60 | */ |
| 61 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 62 | static int get_http_auth(struct sample *smp) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 63 | { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 64 | struct stream *s = smp->strm; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 65 | struct http_txn *txn = s->txn; |
| 66 | struct buffer auth_method; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 67 | char *h, *p; |
| 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 | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 78 | if (IS_HTX_STRM(s) || (smp->px->mode == PR_MODE_TCP)) { |
| 79 | /* HTX version */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 80 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 81 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 82 | struct ist hdr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 83 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 84 | if (txn->flags & TX_USE_PX_CONN) |
| 85 | hdr = ist("Proxy-Authorization"); |
| 86 | else |
| 87 | hdr = ist("Authorization"); |
| 88 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 89 | ctx.blk = NULL; |
| 90 | if (!http_find_header(htx, hdr, &ctx, 0)) |
| 91 | return 0; |
| 92 | |
| 93 | p = memchr(ctx.value.ptr, ' ', ctx.value.len); |
| 94 | len = p - ctx.value.ptr; |
| 95 | if (!p || len <= 0) |
| 96 | return 0; |
| 97 | |
| 98 | if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1) |
| 99 | return 0; |
| 100 | |
| 101 | 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] | 102 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 103 | else { |
| 104 | /* LEGACY version */ |
| 105 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 106 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 107 | if (txn->flags & TX_USE_PX_CONN) { |
| 108 | h = "Proxy-Authorization"; |
| 109 | len = strlen(h); |
| 110 | } else { |
| 111 | h = "Authorization"; |
| 112 | len = strlen(h); |
| 113 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 114 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 115 | if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx)) |
| 116 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 117 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 118 | h = ctx.line + ctx.val; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 119 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 120 | p = memchr(h, ' ', ctx.vlen); |
| 121 | len = p - h; |
| 122 | if (!p || len <= 0) |
| 123 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 124 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 125 | if (chunk_initlen(&auth_method, h, 0, len) != 1) |
| 126 | return 0; |
| 127 | |
| 128 | chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1); |
| 129 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 130 | |
| 131 | if (!strncasecmp("Basic", auth_method.area, auth_method.data)) { |
| 132 | struct buffer *http_auth = get_trash_chunk(); |
| 133 | |
| 134 | len = base64dec(txn->auth.method_data.area, |
| 135 | txn->auth.method_data.data, |
| 136 | http_auth->area, global.tune.bufsize - 1); |
| 137 | |
| 138 | if (len < 0) |
| 139 | return 0; |
| 140 | |
| 141 | |
| 142 | http_auth->area[len] = '\0'; |
| 143 | |
| 144 | p = strchr(http_auth->area, ':'); |
| 145 | |
| 146 | if (!p) |
| 147 | return 0; |
| 148 | |
| 149 | txn->auth.user = http_auth->area; |
| 150 | *p = '\0'; |
| 151 | txn->auth.pass = p+1; |
| 152 | |
| 153 | txn->auth.method = HTTP_AUTH_BASIC; |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 161 | * which means that a request or response is ready. If some data is missing, |
| 162 | * 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] | 163 | * to extract data from L7. If <vol> is non-null during a prefetch, another |
| 164 | * test is made to ensure the required information is not gone. |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 165 | * |
| 166 | * The function returns : |
| 167 | * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 168 | * decide whether or not an HTTP message is present ; |
| 169 | * NULL if the requested data cannot be fetched or if it is certain that |
| 170 | * we'll never have any HTTP message there ; |
| 171 | * The HTX message if ready |
| 172 | */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 173 | 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] | 174 | { |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 175 | struct stream *s = smp->strm; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 176 | struct http_txn *txn = NULL; |
| 177 | struct htx *htx = NULL; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 178 | struct http_msg *msg; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 179 | struct htx_sl *sl; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 180 | |
| 181 | /* Note: it is possible that <s> is NULL when called before stream |
| 182 | * initialization (eg: tcp-request connection), so this function is the |
| 183 | * one responsible for guarding against this case for all HTTP users. |
| 184 | */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 185 | if (!s || !chn) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 186 | return NULL; |
| 187 | |
| 188 | if (!s->txn) { |
| 189 | if (unlikely(!http_alloc_txn(s))) |
| 190 | return NULL; /* not enough memory */ |
| 191 | http_init_txn(s); |
| 192 | txn = s->txn; |
| 193 | } |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 194 | txn = s->txn; |
| 195 | msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp); |
| 196 | smp->data.type = SMP_T_BOOL; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 197 | |
Christopher Faulet | eca8854 | 2019-04-03 10:12:42 +0200 | [diff] [blame] | 198 | if (IS_HTX_STRM(s)) { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 199 | htx = htxbuf(&chn->buf); |
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 | if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR)) |
| 202 | return NULL; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 203 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 204 | if (msg->msg_state < HTTP_MSG_BODY) { |
| 205 | /* Analyse not yet started */ |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 206 | if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) { |
| 207 | /* Parsing is done by the mux, just wait */ |
| 208 | smp->flags |= SMP_F_MAY_CHANGE; |
| 209 | return NULL; |
| 210 | } |
| 211 | } |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 212 | sl = http_find_stline(htx); |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 213 | if (vol && !sl) { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 214 | /* The start-line was already forwarded, it is too late to fetch anything */ |
| 215 | return NULL; |
| 216 | } |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 217 | } |
Christopher Faulet | eca8854 | 2019-04-03 10:12:42 +0200 | [diff] [blame] | 218 | else { /* RAW mode */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 219 | struct buffer *buf; |
| 220 | struct h1m h1m; |
| 221 | struct http_hdr hdrs[MAX_HTTP_HDR]; |
| 222 | union h1_sl h1sl; |
| 223 | unsigned int flags = HTX_FL_NONE; |
| 224 | int ret; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 225 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 226 | /* no HTTP fetch on the response in TCP mode */ |
| 227 | if (chn->flags & CF_ISRESP) |
| 228 | return NULL; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 229 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 230 | /* Now we are working on the request only */ |
| 231 | buf = &chn->buf; |
| 232 | if (b_head(buf) + b_data(buf) > b_wrap(buf)) |
| 233 | b_slow_realign(buf, trash.area, 0); |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 234 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 235 | h1m_init_req(&h1m); |
| 236 | ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf), |
| 237 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl); |
| 238 | if (ret <= 0) { |
| 239 | /* Invalid or too big*/ |
| 240 | if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite)) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 241 | return NULL; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 242 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 243 | /* wait for a full request */ |
| 244 | smp->flags |= SMP_F_MAY_CHANGE; |
| 245 | return NULL; |
| 246 | } |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 247 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 248 | /* OK we just got a valid HTTP mesage. We have to convert it |
| 249 | * into an HTX message. |
| 250 | */ |
| 251 | if (unlikely(h1sl.rq.v.len == 0)) { |
| 252 | /* try to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 253 | if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 254 | return NULL; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 255 | h1sl.rq.v = ist("HTTP/1.0"); |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 256 | } |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 257 | |
| 258 | /* Set HTX start-line flags */ |
| 259 | if (h1m.flags & H1_MF_VER_11) |
| 260 | flags |= HTX_SL_F_VER_11; |
| 261 | if (h1m.flags & H1_MF_XFER_ENC) |
| 262 | flags |= HTX_SL_F_XFER_ENC; |
| 263 | flags |= HTX_SL_F_XFER_LEN; |
| 264 | if (h1m.flags & H1_MF_CHNK) |
| 265 | flags |= HTX_SL_F_CHNK; |
| 266 | else if (h1m.flags & H1_MF_CLEN) |
| 267 | flags |= HTX_SL_F_CLEN; |
| 268 | |
| 269 | htx = htx_from_buf(get_trash_chunk()); |
| 270 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v); |
| 271 | if (!sl || !htx_add_all_headers(htx, hdrs)) |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 272 | return NULL; |
Willy Tarreau | ce9bbf5 | 2019-05-13 08:32:31 +0200 | [diff] [blame] | 273 | sl->info.req.meth = h1sl.rq.meth; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* OK we just got a valid HTTP message. If not already done by |
| 277 | * HTTP analyzers, we have some minor preparation to perform so |
| 278 | * that further checks can rely on HTTP tests. |
| 279 | */ |
| 280 | if (sl && msg->msg_state < HTTP_MSG_BODY) { |
| 281 | if (!(chn->flags & CF_ISRESP)) { |
| 282 | txn->meth = sl->info.req.meth; |
| 283 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 284 | s->flags |= SF_REDIRECTABLE; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 285 | } |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 286 | else |
| 287 | txn->status = sl->info.res.status; |
| 288 | if (sl->flags & HTX_SL_F_VER_11) |
| 289 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /* everything's OK */ |
| 293 | smp->data.u.sint = 1; |
| 294 | return htx; |
| 295 | } |
| 296 | |
| 297 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 298 | * which means that a request or response is ready. If some data is missing, |
| 299 | * a parsing attempt is made. This is useful in TCP-based ACLs which are able |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 300 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 301 | * another test is made to ensure the required information is not gone. |
| 302 | * |
| 303 | * The function returns : |
| 304 | * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to |
| 305 | * decide whether or not an HTTP message is present ; |
| 306 | * 0 if the requested data cannot be fetched or if it is certain that |
| 307 | * we'll never have any HTTP message there ; |
| 308 | * 1 if an HTTP message is ready |
| 309 | */ |
| 310 | int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt, |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 311 | struct channel *chn, struct sample *smp, int req_vol) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 312 | { |
| 313 | struct http_txn *txn; |
| 314 | struct http_msg *msg; |
| 315 | |
| 316 | /* Note: it is possible that <s> is NULL when called before stream |
| 317 | * initialization (eg: tcp-request connection), so this function is the |
| 318 | * one responsible for guarding against this case for all HTTP users. |
| 319 | */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 320 | if (!s || !chn) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 321 | return 0; |
| 322 | |
| 323 | if (!s->txn) { |
| 324 | if (unlikely(!http_alloc_txn(s))) |
| 325 | return 0; /* not enough memory */ |
| 326 | http_init_txn(s); |
| 327 | } |
| 328 | txn = s->txn; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 329 | smp->data.type = SMP_T_BOOL; |
| 330 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 331 | if (chn->flags & CF_ISRESP) { |
| 332 | /* Check for a dependency on a response */ |
| 333 | if (txn->rsp.msg_state < HTTP_MSG_BODY) { |
| 334 | smp->flags |= SMP_F_MAY_CHANGE; |
| 335 | return 0; |
| 336 | } |
| 337 | goto end; |
| 338 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 339 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 340 | /* Check for a dependency on a request */ |
| 341 | msg = &txn->req; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 342 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 343 | if (req_vol && (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) { |
| 344 | return 0; /* data might have moved and indexes changed */ |
| 345 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 346 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 347 | /* If the buffer does not leave enough free space at the end, we must |
| 348 | * first realign it. |
| 349 | */ |
| 350 | if (ci_head(chn) > b_orig(&chn->buf) && |
| 351 | ci_head(chn) + ci_data(chn) > b_wrap(&chn->buf) - global.tune.maxrewrite) |
| 352 | channel_slow_realign(chn, trash.area); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 353 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 354 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
| 355 | if (msg->msg_state == HTTP_MSG_ERROR) |
| 356 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 357 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 358 | /* Try to decode HTTP request */ |
| 359 | if (likely(msg->next < ci_data(chn))) |
| 360 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 361 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 362 | /* Still no valid request ? */ |
| 363 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
| 364 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
| 365 | channel_full(chn, global.tune.maxrewrite)) { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 366 | return 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 367 | } |
| 368 | /* wait for final state */ |
| 369 | smp->flags |= SMP_F_MAY_CHANGE; |
| 370 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 373 | /* OK we just got a valid HTTP message. We have some minor |
| 374 | * preparation to perform so that further checks can rely |
| 375 | * on HTTP tests. |
| 376 | */ |
| 377 | |
| 378 | /* If the message was parsed but was too large, we must absolutely |
| 379 | * return an error so that it is not processed. At the moment this |
| 380 | * cannot happen, but if the parsers are to change in the future, |
| 381 | * we want this check to be maintained. |
| 382 | */ |
| 383 | if (unlikely(ci_head(chn) + ci_data(chn) > |
| 384 | b_wrap(&chn->buf) - global.tune.maxrewrite)) { |
| 385 | msg->err_state = msg->msg_state; |
| 386 | msg->msg_state = HTTP_MSG_ERROR; |
| 387 | smp->data.u.sint = 1; |
| 388 | return 1; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 391 | txn->meth = find_http_meth(ci_head(chn), msg->sl.rq.m_l); |
| 392 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 393 | s->flags |= SF_REDIRECTABLE; |
| 394 | |
| 395 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 396 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 397 | } |
| 398 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 399 | end: |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 400 | /* everything's OK */ |
| 401 | smp->data.u.sint = 1; |
| 402 | return 1; |
| 403 | } |
| 404 | |
| 405 | /* This function fetches the method of current HTTP request and stores |
| 406 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 407 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 408 | * in <len> and <ptr> is NULL ; |
| 409 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 410 | * <len> to its length. |
| 411 | * This is intended to be used with pat_match_meth() only. |
| 412 | */ |
| 413 | static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 414 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 415 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 416 | int meth; |
| 417 | struct http_txn *txn; |
| 418 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 419 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 420 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 421 | struct htx *htx = smp_prefetch_htx(smp, chn, 0); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 422 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 423 | if (!htx) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 424 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 425 | |
| 426 | txn = smp->strm->txn; |
| 427 | meth = txn->meth; |
| 428 | smp->data.type = SMP_T_METH; |
| 429 | smp->data.u.meth.meth = meth; |
| 430 | if (meth == HTTP_METH_OTHER) { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 431 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 432 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 433 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 434 | /* ensure the indexes are not affected */ |
| 435 | return 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 436 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 437 | sl = http_find_stline(htx); |
| 438 | smp->flags |= SMP_F_CONST; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 439 | smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl); |
| 440 | smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 441 | } |
| 442 | smp->flags |= SMP_F_VOL_1ST; |
| 443 | } |
| 444 | else { |
| 445 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 446 | CHECK_HTTP_MESSAGE_FIRST_PERM(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 447 | |
| 448 | txn = smp->strm->txn; |
| 449 | meth = txn->meth; |
| 450 | smp->data.type = SMP_T_METH; |
| 451 | smp->data.u.meth.meth = meth; |
| 452 | if (meth == HTTP_METH_OTHER) { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 453 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 454 | /* ensure the indexes are not affected */ |
| 455 | return 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 456 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 457 | smp->flags |= SMP_F_CONST; |
| 458 | smp->data.u.meth.str.data = txn->req.sl.rq.m_l; |
| 459 | smp->data.u.meth.str.area = ci_head(txn->req.chn); |
| 460 | } |
| 461 | smp->flags |= SMP_F_VOL_1ST; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 462 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 463 | return 1; |
| 464 | } |
| 465 | |
| 466 | static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 467 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 468 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 469 | struct http_txn *txn; |
| 470 | char *ptr; |
| 471 | int len; |
| 472 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 473 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 474 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 475 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 476 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 477 | |
| 478 | if (!htx) |
| 479 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 480 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 481 | sl = http_find_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 482 | len = HTX_SL_REQ_VLEN(sl); |
| 483 | ptr = HTX_SL_REQ_VPTR(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 484 | } |
| 485 | else { |
| 486 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 487 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 488 | |
| 489 | txn = smp->strm->txn; |
| 490 | len = txn->req.sl.rq.v_l; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 491 | ptr = ci_head(chn) + txn->req.sl.rq.v; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 492 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 493 | |
| 494 | while ((len-- > 0) && (*ptr++ != '/')); |
| 495 | if (len <= 0) |
| 496 | return 0; |
| 497 | |
| 498 | smp->data.type = SMP_T_STR; |
| 499 | smp->data.u.str.area = ptr; |
| 500 | smp->data.u.str.data = len; |
| 501 | |
| 502 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 503 | return 1; |
| 504 | } |
| 505 | |
| 506 | static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 507 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 508 | struct channel *chn = SMP_RES_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 509 | struct http_txn *txn; |
| 510 | char *ptr; |
| 511 | int len; |
| 512 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 513 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 514 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 515 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 516 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 517 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 518 | if (!htx) |
| 519 | return 0; |
| 520 | |
| 521 | sl = http_find_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 522 | len = HTX_SL_RES_VLEN(sl); |
| 523 | ptr = HTX_SL_RES_VPTR(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 524 | } |
| 525 | else { |
| 526 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 527 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 528 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 529 | txn = smp->strm->txn; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 530 | len = txn->rsp.sl.st.v_l; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 531 | ptr = ci_head(chn); |
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 | |
| 534 | while ((len-- > 0) && (*ptr++ != '/')); |
| 535 | if (len <= 0) |
| 536 | return 0; |
| 537 | |
| 538 | smp->data.type = SMP_T_STR; |
| 539 | smp->data.u.str.area = ptr; |
| 540 | smp->data.u.str.data = len; |
| 541 | |
| 542 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 543 | return 1; |
| 544 | } |
| 545 | |
| 546 | /* 3. Check on Status Code. We manipulate integers here. */ |
| 547 | static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 548 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 549 | struct channel *chn = SMP_RES_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 550 | struct http_txn *txn; |
| 551 | char *ptr; |
| 552 | int len; |
| 553 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 554 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 555 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 556 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 557 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 558 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 559 | if (!htx) |
| 560 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 561 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 562 | sl = http_find_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 563 | len = HTX_SL_RES_CLEN(sl); |
| 564 | ptr = HTX_SL_RES_CPTR(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 565 | } |
| 566 | else { |
| 567 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 568 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 569 | |
| 570 | txn = smp->strm->txn; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 571 | len = txn->rsp.sl.st.c_l; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 572 | ptr = ci_head(chn) + txn->rsp.sl.st.c; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 573 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 574 | |
| 575 | smp->data.type = SMP_T_SINT; |
| 576 | smp->data.u.sint = __strl2ui(ptr, len); |
| 577 | smp->flags = SMP_F_VOL_1ST; |
| 578 | return 1; |
| 579 | } |
| 580 | |
| 581 | static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 582 | { |
| 583 | if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id)) |
| 584 | return 0; |
| 585 | |
| 586 | if (!smp->strm->unique_id) { |
| 587 | if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
| 588 | return 0; |
| 589 | smp->strm->unique_id[0] = '\0'; |
| 590 | } |
| 591 | smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id, |
| 592 | UNIQUEID_LEN, &smp->sess->fe->format_unique_id); |
| 593 | |
| 594 | smp->data.type = SMP_T_STR; |
| 595 | smp->data.u.str.area = smp->strm->unique_id; |
| 596 | smp->flags = SMP_F_CONST; |
| 597 | return 1; |
| 598 | } |
| 599 | |
| 600 | /* Returns a string block containing all headers including the |
Joseph Herlant | 942eea3 | 2018-11-15 13:57:22 -0800 | [diff] [blame] | 601 | * empty line which separes headers from the body. This is useful |
| 602 | * for some headers analysis. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 603 | */ |
| 604 | static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 605 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 606 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 607 | struct http_txn *txn; |
| 608 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 609 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 610 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 611 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 612 | struct buffer *temp; |
| 613 | int32_t pos; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 614 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 615 | if (!htx) |
| 616 | return 0; |
| 617 | temp = get_trash_chunk(); |
| 618 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 619 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 620 | enum htx_blk_type type = htx_get_blk_type(blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 621 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 622 | if (type == HTX_BLK_HDR) { |
| 623 | struct ist n = htx_get_blk_name(htx, blk); |
| 624 | struct ist v = htx_get_blk_value(htx, blk); |
| 625 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 626 | if (!htx_hdr_to_h1(n, v, temp)) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 627 | return 0; |
| 628 | } |
| 629 | else if (type == HTX_BLK_EOH) { |
| 630 | if (!chunk_memcat(temp, "\r\n", 2)) |
| 631 | return 0; |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | smp->data.type = SMP_T_STR; |
| 636 | smp->data.u.str = *temp; |
| 637 | |
| 638 | } |
| 639 | else { |
| 640 | /* LEGACY version */ |
| 641 | struct http_msg *msg; |
| 642 | struct hdr_idx *idx; |
| 643 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 644 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 645 | |
| 646 | txn = smp->strm->txn; |
| 647 | idx = &txn->hdr_idx; |
| 648 | msg = &txn->req; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 649 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 650 | smp->data.type = SMP_T_STR; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 651 | smp->data.u.str.area = ci_head(chn) + hdr_idx_first_pos(idx); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 652 | smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 + |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 653 | (ci_head(chn)[msg->eoh] == '\r'); |
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 | return 1; |
| 656 | } |
| 657 | |
| 658 | /* Returns the header request in a length/value encoded format. |
| 659 | * This is useful for exchanges with the SPOE. |
| 660 | * |
| 661 | * A "length value" is a multibyte code encoding numbers. It uses the |
| 662 | * SPOE format. The encoding is the following: |
| 663 | * |
| 664 | * Each couple "header name" / "header value" is composed |
| 665 | * like this: |
| 666 | * "length value" "header name bytes" |
| 667 | * "length value" "header value bytes" |
| 668 | * When the last header is reached, the header name and the header |
| 669 | * value are empty. Their length are 0 |
| 670 | */ |
| 671 | static int smp_fetch_hdrs_bin(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 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 674 | struct http_txn *txn; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 675 | struct buffer *temp; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 676 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 677 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 678 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 679 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 680 | struct buffer *temp; |
| 681 | char *p, *end; |
| 682 | int32_t pos; |
| 683 | int ret; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 684 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 685 | if (!htx) |
| 686 | return 0; |
| 687 | temp = get_trash_chunk(); |
| 688 | p = temp->area; |
| 689 | end = temp->area + temp->size; |
| 690 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 691 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 692 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 693 | struct ist n, v; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 694 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 695 | if (type == HTX_BLK_HDR) { |
| 696 | n = htx_get_blk_name(htx,blk); |
| 697 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 698 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 699 | /* encode the header name. */ |
| 700 | ret = encode_varint(n.len, &p, end); |
| 701 | if (ret == -1) |
| 702 | return 0; |
| 703 | if (p + n.len > end) |
| 704 | return 0; |
| 705 | memcpy(p, n.ptr, n.len); |
| 706 | p += n.len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 707 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 708 | /* encode the header value. */ |
| 709 | ret = encode_varint(v.len, &p, end); |
| 710 | if (ret == -1) |
| 711 | return 0; |
| 712 | if (p + v.len > end) |
| 713 | return 0; |
| 714 | memcpy(p, v.ptr, v.len); |
| 715 | p += v.len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 716 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 717 | } |
| 718 | else if (type == HTX_BLK_EOH) { |
| 719 | /* encode the end of the header list with empty |
| 720 | * header name and header value. |
| 721 | */ |
| 722 | ret = encode_varint(0, &p, end); |
| 723 | if (ret == -1) |
| 724 | return 0; |
| 725 | ret = encode_varint(0, &p, end); |
| 726 | if (ret == -1) |
| 727 | return 0; |
| 728 | break; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /* Initialise sample data which will be filled. */ |
| 733 | smp->data.type = SMP_T_BIN; |
| 734 | smp->data.u.str.area = temp->area; |
| 735 | smp->data.u.str.data = p - temp->area; |
| 736 | smp->data.u.str.size = temp->size; |
| 737 | } |
| 738 | else { |
| 739 | /* LEGACY version */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 740 | struct hdr_idx *idx; |
| 741 | const char *cur_ptr, *cur_next, *p; |
| 742 | int old_idx, cur_idx; |
| 743 | struct hdr_idx_elem *cur_hdr; |
| 744 | const char *hn, *hv; |
| 745 | int hnl, hvl; |
| 746 | int ret; |
| 747 | char *buf; |
| 748 | char *end; |
| 749 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 750 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 751 | |
| 752 | temp = get_trash_chunk(); |
| 753 | buf = temp->area; |
| 754 | end = temp->area + temp->size; |
| 755 | |
| 756 | txn = smp->strm->txn; |
| 757 | idx = &txn->hdr_idx; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 758 | |
| 759 | /* Build array of headers. */ |
| 760 | old_idx = 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 761 | cur_next = ci_head(chn) + hdr_idx_first_pos(idx); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 762 | while (1) { |
| 763 | cur_idx = idx->v[old_idx].next; |
| 764 | if (!cur_idx) |
| 765 | break; |
| 766 | old_idx = cur_idx; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 767 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 768 | cur_hdr = &idx->v[cur_idx]; |
| 769 | cur_ptr = cur_next; |
| 770 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 771 | |
| 772 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 773 | * and the next header starts at cur_next. We'll check |
| 774 | * this header in the list as well as against the default |
| 775 | * rule. |
| 776 | */ |
| 777 | |
| 778 | /* look for ': *'. */ |
| 779 | hn = cur_ptr; |
| 780 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 781 | if (p >= cur_ptr+cur_hdr->len) |
| 782 | continue; |
| 783 | hnl = p - hn; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 784 | p++; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 785 | while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t')) |
| 786 | p++; |
| 787 | if (p >= cur_ptr + cur_hdr->len) |
| 788 | continue; |
| 789 | hv = p; |
| 790 | hvl = cur_ptr + cur_hdr->len-p; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 791 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 792 | /* encode the header name. */ |
| 793 | ret = encode_varint(hnl, &buf, end); |
| 794 | if (ret == -1) |
| 795 | return 0; |
| 796 | if (buf + hnl > end) |
| 797 | return 0; |
| 798 | memcpy(buf, hn, hnl); |
| 799 | buf += hnl; |
| 800 | |
| 801 | /* encode and copy the value. */ |
| 802 | ret = encode_varint(hvl, &buf, end); |
| 803 | if (ret == -1) |
| 804 | return 0; |
| 805 | if (buf + hvl > end) |
| 806 | return 0; |
| 807 | memcpy(buf, hv, hvl); |
| 808 | buf += hvl; |
| 809 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 810 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 811 | /* encode the end of the header list with empty |
| 812 | * header name and header value. |
| 813 | */ |
| 814 | ret = encode_varint(0, &buf, end); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 815 | if (ret == -1) |
| 816 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 817 | ret = encode_varint(0, &buf, end); |
| 818 | if (ret == -1) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 819 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 820 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 821 | /* Initialise sample data which will be filled. */ |
| 822 | smp->data.type = SMP_T_BIN; |
| 823 | smp->data.u.str.area = temp->area; |
| 824 | smp->data.u.str.data = buf - temp->area; |
| 825 | smp->data.u.str.size = temp->size; |
| 826 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 827 | return 1; |
| 828 | } |
| 829 | |
| 830 | /* returns the longest available part of the body. This requires that the body |
| 831 | * has been waited for using http-buffer-request. |
| 832 | */ |
| 833 | static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 834 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 835 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 836 | struct buffer *temp; |
| 837 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 838 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 839 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 840 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 841 | int32_t pos; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 842 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 843 | if (!htx) |
| 844 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 845 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 846 | temp = get_trash_chunk(); |
| 847 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 848 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 849 | enum htx_blk_type type = htx_get_blk_type(blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 850 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 851 | if (type == HTX_BLK_EOM || type == HTX_BLK_EOD) |
| 852 | break; |
| 853 | if (type == HTX_BLK_DATA) { |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 854 | if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0)) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 855 | return 0; |
| 856 | } |
| 857 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 858 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 859 | smp->data.type = SMP_T_BIN; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 860 | smp->data.u.str = *temp; |
| 861 | smp->flags = SMP_F_VOL_TEST; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 862 | } |
| 863 | else { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 864 | /* LEGACY version */ |
| 865 | struct http_msg *msg; |
| 866 | unsigned long len; |
| 867 | unsigned long block1; |
| 868 | char *body; |
| 869 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 870 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 871 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 872 | msg = &smp->strm->txn->req; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 873 | len = http_body_bytes(msg); |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 874 | body = c_ptr(chn, -http_data_rewind(msg)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 875 | |
| 876 | block1 = len; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 877 | if (block1 > b_wrap(&chn->buf) - body) |
| 878 | block1 = b_wrap(&chn->buf) - body; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 879 | |
| 880 | if (block1 == len) { |
| 881 | /* buffer is not wrapped (or empty) */ |
| 882 | smp->data.type = SMP_T_BIN; |
| 883 | smp->data.u.str.area = body; |
| 884 | smp->data.u.str.data = len; |
| 885 | smp->flags = SMP_F_VOL_TEST | SMP_F_CONST; |
| 886 | } |
| 887 | else { |
| 888 | /* buffer is wrapped, we need to defragment it */ |
| 889 | temp = get_trash_chunk(); |
| 890 | memcpy(temp->area, body, block1); |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 891 | memcpy(temp->area + block1, b_orig(&chn->buf), len - block1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 892 | smp->data.type = SMP_T_BIN; |
| 893 | smp->data.u.str.area = temp->area; |
| 894 | smp->data.u.str.data = len; |
| 895 | smp->flags = SMP_F_VOL_TEST; |
| 896 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 897 | } |
| 898 | return 1; |
| 899 | } |
| 900 | |
| 901 | |
| 902 | /* returns the available length of the body. This requires that the body |
| 903 | * has been waited for using http-buffer-request. |
| 904 | */ |
| 905 | static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 906 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 907 | struct channel *chn = SMP_REQ_CHN(smp); |
| 908 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 909 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 910 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 911 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 912 | int32_t pos; |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 913 | unsigned long long len = 0; |
| 914 | |
| 915 | if (!htx) |
| 916 | return 0; |
| 917 | |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 918 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 919 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 920 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 921 | |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 922 | if (type == HTX_BLK_EOM || type == HTX_BLK_EOD) |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 923 | break; |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 924 | if (type == HTX_BLK_DATA) |
| 925 | len += htx_get_blksz(blk); |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | smp->data.type = SMP_T_SINT; |
| 929 | smp->data.u.sint = len; |
| 930 | |
| 931 | smp->flags = SMP_F_VOL_TEST; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 932 | } |
| 933 | else { |
| 934 | /* LEGACY version */ |
| 935 | struct http_msg *msg; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 936 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 937 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 938 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 939 | msg = &smp->strm->txn->req; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 940 | smp->data.type = SMP_T_SINT; |
| 941 | smp->data.u.sint = http_body_bytes(msg); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 942 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 943 | smp->flags = SMP_F_VOL_TEST; |
| 944 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 945 | return 1; |
| 946 | } |
| 947 | |
| 948 | |
| 949 | /* returns the advertised length of the body, or the advertised size of the |
| 950 | * chunks available in the buffer. This requires that the body has been waited |
| 951 | * for using http-buffer-request. |
| 952 | */ |
| 953 | static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 954 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 955 | struct channel *chn = SMP_REQ_CHN(smp); |
| 956 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 957 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 958 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 959 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 960 | int32_t pos; |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 961 | unsigned long long len = 0; |
| 962 | |
| 963 | if (!htx) |
| 964 | return 0; |
| 965 | |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 966 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 967 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 968 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 969 | |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 970 | if (type == HTX_BLK_EOM || type == HTX_BLK_EOD) |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 971 | break; |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 972 | if (type == HTX_BLK_DATA) |
| 973 | len += htx_get_blksz(blk); |
Christopher Faulet | c16317d | 2018-12-12 14:11:22 +0100 | [diff] [blame] | 974 | } |
| 975 | if (htx->extra != ULLONG_MAX) |
| 976 | len += htx->extra; |
| 977 | |
| 978 | smp->data.type = SMP_T_SINT; |
| 979 | smp->data.u.sint = len; |
| 980 | |
| 981 | smp->flags = SMP_F_VOL_TEST; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 982 | } |
| 983 | else { |
| 984 | /* LEGACY version */ |
| 985 | struct http_msg *msg; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 986 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 987 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 988 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 989 | msg = &smp->strm->txn->req; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 990 | smp->data.type = SMP_T_SINT; |
| 991 | smp->data.u.sint = msg->body_len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 992 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 993 | smp->flags = SMP_F_VOL_TEST; |
| 994 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 995 | return 1; |
| 996 | } |
| 997 | |
| 998 | |
| 999 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
| 1000 | static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1001 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1002 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1003 | struct http_txn *txn; |
| 1004 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1005 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1006 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1007 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1008 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1009 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1010 | if (!htx) |
| 1011 | return 0; |
| 1012 | sl = http_find_stline(htx); |
| 1013 | smp->data.type = SMP_T_STR; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1014 | smp->data.u.str.area = HTX_SL_REQ_UPTR(sl); |
| 1015 | smp->data.u.str.data = HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1016 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1017 | } |
| 1018 | else { |
| 1019 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1020 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1021 | |
| 1022 | txn = smp->strm->txn; |
| 1023 | smp->data.type = SMP_T_STR; |
| 1024 | smp->data.u.str.data = txn->req.sl.rq.u_l; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1025 | smp->data.u.str.area = ci_head(chn) + txn->req.sl.rq.u; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1026 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1027 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1028 | return 1; |
| 1029 | } |
| 1030 | |
| 1031 | static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1032 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1033 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1034 | struct http_txn *txn; |
| 1035 | struct sockaddr_storage addr; |
| 1036 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1037 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1038 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1039 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1040 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1041 | |
| 1042 | if (!htx) |
| 1043 | return 0; |
| 1044 | sl = http_find_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1045 | 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] | 1046 | } |
| 1047 | else { |
| 1048 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1049 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1050 | |
| 1051 | txn = smp->strm->txn; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1052 | url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1053 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1054 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1055 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 1056 | return 0; |
| 1057 | |
| 1058 | smp->data.type = SMP_T_IPV4; |
| 1059 | smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr; |
| 1060 | smp->flags = 0; |
| 1061 | return 1; |
| 1062 | } |
| 1063 | |
| 1064 | static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1065 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1066 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1067 | struct http_txn *txn; |
| 1068 | struct sockaddr_storage addr; |
| 1069 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1070 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1071 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1072 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1073 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1074 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1075 | if (!htx) |
| 1076 | return 0; |
| 1077 | sl = http_find_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1078 | 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] | 1079 | } |
| 1080 | else { |
| 1081 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1082 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1083 | |
| 1084 | txn = smp->strm->txn; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1085 | url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1086 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1087 | if (((struct sockaddr_in *)&addr)->sin_family != AF_INET) |
| 1088 | return 0; |
| 1089 | |
| 1090 | smp->data.type = SMP_T_SINT; |
| 1091 | smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port); |
| 1092 | smp->flags = 0; |
| 1093 | return 1; |
| 1094 | } |
| 1095 | |
| 1096 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 1097 | * Accepts an optional argument of type string containing the header field name, |
| 1098 | * and an optional argument of type signed or unsigned integer to request an |
| 1099 | * explicit occurrence of the header. Note that in the event of a missing name, |
| 1100 | * headers are considered from the first one. It does not stop on commas and |
| 1101 | * returns full lines instead (useful for User-Agent or Date for example). |
| 1102 | */ |
| 1103 | static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1104 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1105 | /* possible keywords: req.fhdr, res.fhdr */ |
| 1106 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1107 | int occ = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1108 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1109 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1110 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1111 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1112 | struct http_hdr_ctx *ctx = smp->ctx.a[0]; |
| 1113 | struct ist name; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1114 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1115 | if (!ctx) { |
| 1116 | /* first call */ |
| 1117 | ctx = &static_http_hdr_ctx; |
| 1118 | ctx->blk = NULL; |
| 1119 | smp->ctx.a[0] = ctx; |
| 1120 | } |
| 1121 | |
| 1122 | if (args) { |
| 1123 | if (args[0].type != ARGT_STR) |
| 1124 | return 0; |
| 1125 | name.ptr = args[0].data.str.area; |
| 1126 | name.len = args[0].data.str.data; |
| 1127 | |
| 1128 | if (args[1].type == ARGT_SINT) |
| 1129 | occ = args[1].data.sint; |
| 1130 | } |
| 1131 | |
| 1132 | if (!htx) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1133 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1134 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1135 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 1136 | /* search for header from the beginning */ |
| 1137 | ctx->blk = NULL; |
| 1138 | |
| 1139 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 1140 | /* no explicit occurrence and single fetch => last header by default */ |
| 1141 | occ = -1; |
| 1142 | |
| 1143 | if (!occ) |
| 1144 | /* prepare to report multiple occurrences for ACL fetches */ |
| 1145 | smp->flags |= SMP_F_NOT_LAST; |
| 1146 | |
| 1147 | smp->data.type = SMP_T_STR; |
| 1148 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
| 1149 | if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data)) |
| 1150 | return 1; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1151 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1152 | else { |
| 1153 | /* LEGACY version */ |
| 1154 | struct hdr_idx *idx; |
| 1155 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 1156 | const struct http_msg *msg; |
| 1157 | const char *name_str = NULL; |
| 1158 | int name_len = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1159 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1160 | if (!ctx) { |
| 1161 | /* first call */ |
| 1162 | ctx = &static_hdr_ctx; |
| 1163 | ctx->idx = 0; |
| 1164 | smp->ctx.a[0] = ctx; |
| 1165 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1166 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1167 | if (args) { |
| 1168 | if (args[0].type != ARGT_STR) |
| 1169 | return 0; |
| 1170 | name_str = args[0].data.str.area; |
| 1171 | name_len = args[0].data.str.data; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1172 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1173 | if (args[1].type == ARGT_SINT) |
| 1174 | occ = args[1].data.sint; |
| 1175 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1176 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1177 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1178 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1179 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1180 | msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1181 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1182 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 1183 | /* search for header from the beginning */ |
| 1184 | ctx->idx = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1185 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1186 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 1187 | /* no explicit occurrence and single fetch => last header by default */ |
| 1188 | occ = -1; |
| 1189 | |
| 1190 | if (!occ) |
| 1191 | /* prepare to report multiple occurrences for ACL fetches */ |
| 1192 | smp->flags |= SMP_F_NOT_LAST; |
| 1193 | |
| 1194 | smp->data.type = SMP_T_STR; |
| 1195 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
| 1196 | if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data)) |
| 1197 | return 1; |
| 1198 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1199 | smp->flags &= ~SMP_F_NOT_LAST; |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 1204 | * Accepts exactly 1 argument of type string. It does not stop on commas and |
| 1205 | * returns full lines instead (useful for User-Agent or Date for example). |
| 1206 | */ |
| 1207 | static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1208 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1209 | /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */ |
| 1210 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1211 | int cnt; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1212 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1213 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1214 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1215 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1216 | struct http_hdr_ctx ctx; |
| 1217 | struct ist name; |
| 1218 | |
| 1219 | if (!htx) |
| 1220 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1221 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1222 | if (args && args->type == ARGT_STR) { |
| 1223 | name.ptr = args->data.str.area; |
| 1224 | name.len = args->data.str.data; |
Olivier Houchard | e2c78cd | 2018-11-21 13:49:48 +0100 | [diff] [blame] | 1225 | } else { |
| 1226 | name.ptr = NULL; |
| 1227 | name.len = 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | ctx.blk = NULL; |
| 1231 | cnt = 0; |
| 1232 | while (http_find_header(htx, name, &ctx, 1)) |
| 1233 | cnt++; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1234 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1235 | else { |
| 1236 | /* LEGACY version */ |
| 1237 | struct hdr_idx *idx; |
| 1238 | struct hdr_ctx ctx; |
| 1239 | const struct http_msg *msg; |
| 1240 | const char *name = NULL; |
| 1241 | int len = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1242 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1243 | if (args && args->type == ARGT_STR) { |
| 1244 | name = args->data.str.area; |
| 1245 | len = args->data.str.data; |
| 1246 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1247 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1248 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1249 | |
| 1250 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1251 | msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1252 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1253 | ctx.idx = 0; |
| 1254 | cnt = 0; |
| 1255 | while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx)) |
| 1256 | cnt++; |
| 1257 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1258 | |
| 1259 | smp->data.type = SMP_T_SINT; |
| 1260 | smp->data.u.sint = cnt; |
| 1261 | smp->flags = SMP_F_VOL_HDR; |
| 1262 | return 1; |
| 1263 | } |
| 1264 | |
| 1265 | static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1266 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1267 | /* possible keywords: req.hdr_names, res.hdr_names */ |
| 1268 | struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1269 | struct buffer *temp; |
| 1270 | char del = ','; |
| 1271 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1272 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1273 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1274 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1275 | int32_t pos; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1276 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1277 | if (!htx) |
| 1278 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1279 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1280 | if (args && args->type == ARGT_STR) |
| 1281 | del = *args[0].data.str.area; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1282 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1283 | temp = get_trash_chunk(); |
| 1284 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 1285 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1286 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1287 | struct ist n; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1288 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1289 | if (type == HTX_BLK_EOH) |
| 1290 | break; |
| 1291 | if (type != HTX_BLK_HDR) |
| 1292 | continue; |
| 1293 | n = htx_get_blk_name(htx, blk); |
| 1294 | |
| 1295 | if (temp->data) |
| 1296 | temp->area[temp->data++] = del; |
| 1297 | chunk_memcat(temp, n.ptr, n.len); |
| 1298 | } |
| 1299 | } |
| 1300 | else { |
| 1301 | /* LEGACY version */ |
| 1302 | struct hdr_idx *idx; |
| 1303 | struct hdr_ctx ctx; |
| 1304 | const struct http_msg *msg; |
| 1305 | |
| 1306 | if (args && args->type == ARGT_STR) |
| 1307 | del = *args[0].data.str.area; |
| 1308 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1309 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1310 | |
| 1311 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1312 | msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1313 | |
| 1314 | temp = get_trash_chunk(); |
| 1315 | |
| 1316 | ctx.idx = 0; |
| 1317 | while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) { |
| 1318 | if (temp->data) |
| 1319 | temp->area[temp->data++] = del; |
| 1320 | memcpy(temp->area + temp->data, ctx.line, ctx.del); |
| 1321 | temp->data += ctx.del; |
| 1322 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | smp->data.type = SMP_T_STR; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1326 | smp->data.u.str = *temp; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1327 | smp->flags = SMP_F_VOL_HDR; |
| 1328 | return 1; |
| 1329 | } |
| 1330 | |
| 1331 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 1332 | * Accepts an optional argument of type string containing the header field name, |
| 1333 | * and an optional argument of type signed or unsigned integer to request an |
| 1334 | * explicit occurrence of the header. Note that in the event of a missing name, |
| 1335 | * headers are considered from the first one. |
| 1336 | */ |
| 1337 | static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1338 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1339 | /* possible keywords: req.hdr / hdr, res.hdr / shdr */ |
| 1340 | struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1341 | int occ = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1342 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1343 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1344 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1345 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1346 | struct http_hdr_ctx *ctx = smp->ctx.a[0]; |
| 1347 | struct ist name; |
| 1348 | |
| 1349 | if (!ctx) { |
| 1350 | /* first call */ |
| 1351 | ctx = &static_http_hdr_ctx; |
| 1352 | ctx->blk = NULL; |
| 1353 | smp->ctx.a[0] = ctx; |
| 1354 | } |
| 1355 | |
| 1356 | if (args) { |
| 1357 | if (args[0].type != ARGT_STR) |
| 1358 | return 0; |
| 1359 | name.ptr = args[0].data.str.area; |
| 1360 | name.len = args[0].data.str.data; |
| 1361 | |
| 1362 | if (args[1].type == ARGT_SINT) |
| 1363 | occ = args[1].data.sint; |
| 1364 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1365 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1366 | if (!htx) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1367 | return 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1368 | |
| 1369 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 1370 | /* search for header from the beginning */ |
| 1371 | ctx->blk = NULL; |
| 1372 | |
| 1373 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 1374 | /* no explicit occurrence and single fetch => last header by default */ |
| 1375 | occ = -1; |
| 1376 | |
| 1377 | if (!occ) |
| 1378 | /* prepare to report multiple occurrences for ACL fetches */ |
| 1379 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1380 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1381 | smp->data.type = SMP_T_STR; |
| 1382 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
| 1383 | if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data)) |
| 1384 | return 1; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1385 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1386 | else { |
| 1387 | /* LEGACY version */ |
| 1388 | struct hdr_idx *idx; |
| 1389 | struct hdr_ctx *ctx = smp->ctx.a[0]; |
| 1390 | const struct http_msg *msg; |
| 1391 | const char *name_str = NULL; |
| 1392 | int name_len = 0; |
| 1393 | |
| 1394 | if (!ctx) { |
| 1395 | /* first call */ |
| 1396 | ctx = &static_hdr_ctx; |
| 1397 | ctx->idx = 0; |
| 1398 | smp->ctx.a[0] = ctx; |
| 1399 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1400 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1401 | if (args) { |
| 1402 | if (args[0].type != ARGT_STR) |
| 1403 | return 0; |
| 1404 | name_str = args[0].data.str.area; |
| 1405 | name_len = args[0].data.str.data; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1406 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1407 | if (args[1].type == ARGT_SINT) |
| 1408 | occ = args[1].data.sint; |
| 1409 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1410 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1411 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1412 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1413 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1414 | msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1415 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1416 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
| 1417 | /* search for header from the beginning */ |
| 1418 | ctx->idx = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1419 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1420 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 1421 | /* no explicit occurrence and single fetch => last header by default */ |
| 1422 | occ = -1; |
| 1423 | |
| 1424 | if (!occ) |
| 1425 | /* prepare to report multiple occurrences for ACL fetches */ |
| 1426 | smp->flags |= SMP_F_NOT_LAST; |
| 1427 | |
| 1428 | smp->data.type = SMP_T_STR; |
| 1429 | smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST; |
| 1430 | if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data)) |
| 1431 | return 1; |
| 1432 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1433 | |
| 1434 | smp->flags &= ~SMP_F_NOT_LAST; |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 1439 | * Accepts exactly 1 argument of type string. |
| 1440 | */ |
| 1441 | static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1442 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1443 | /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */ |
| 1444 | struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1445 | int cnt; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1446 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1447 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1448 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1449 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1450 | struct http_hdr_ctx ctx; |
| 1451 | struct ist name; |
| 1452 | |
| 1453 | if (!htx) |
| 1454 | return 0; |
| 1455 | |
| 1456 | if (args && args->type == ARGT_STR) { |
| 1457 | name.ptr = args->data.str.area; |
| 1458 | name.len = args->data.str.data; |
Olivier Houchard | e2c78cd | 2018-11-21 13:49:48 +0100 | [diff] [blame] | 1459 | } else { |
| 1460 | name.ptr = NULL; |
| 1461 | name.len = 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1462 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1463 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1464 | ctx.blk = NULL; |
| 1465 | cnt = 0; |
| 1466 | while (http_find_header(htx, name, &ctx, 0)) |
| 1467 | cnt++; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1468 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1469 | else { |
| 1470 | /* LEGACY version */ |
| 1471 | struct hdr_idx *idx; |
| 1472 | struct hdr_ctx ctx; |
| 1473 | const struct http_msg *msg; |
| 1474 | const char *name = NULL; |
| 1475 | int len = 0; |
| 1476 | |
| 1477 | if (args && args->type == ARGT_STR) { |
| 1478 | name = args->data.str.area; |
| 1479 | len = args->data.str.data; |
| 1480 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1481 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1482 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1483 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1484 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1485 | msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1486 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1487 | ctx.idx = 0; |
| 1488 | cnt = 0; |
| 1489 | while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx)) |
| 1490 | cnt++; |
| 1491 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1492 | |
| 1493 | smp->data.type = SMP_T_SINT; |
| 1494 | smp->data.u.sint = cnt; |
| 1495 | smp->flags = SMP_F_VOL_HDR; |
| 1496 | return 1; |
| 1497 | } |
| 1498 | |
| 1499 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 1500 | * takes a mandatory argument of type string and an optional one of type int |
| 1501 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 1502 | * may or may not be appropriate for everything. |
| 1503 | */ |
| 1504 | static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1505 | { |
| 1506 | int ret = smp_fetch_hdr(args, smp, kw, private); |
| 1507 | |
| 1508 | if (ret > 0) { |
| 1509 | smp->data.type = SMP_T_SINT; |
| 1510 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 1511 | smp->data.u.str.data); |
| 1512 | } |
| 1513 | |
| 1514 | return ret; |
| 1515 | } |
| 1516 | |
| 1517 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 1518 | * and an optional one of type int to designate a specific occurrence. |
| 1519 | * It returns an IPv4 or IPv6 address. |
| 1520 | */ |
| 1521 | static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1522 | { |
| 1523 | int ret; |
| 1524 | |
| 1525 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
| 1526 | if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) { |
| 1527 | smp->data.type = SMP_T_IPV4; |
| 1528 | break; |
| 1529 | } else { |
| 1530 | struct buffer *temp = get_trash_chunk(); |
| 1531 | if (smp->data.u.str.data < temp->size - 1) { |
| 1532 | memcpy(temp->area, smp->data.u.str.area, |
| 1533 | smp->data.u.str.data); |
| 1534 | temp->area[smp->data.u.str.data] = '\0'; |
| 1535 | if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) { |
| 1536 | smp->data.type = SMP_T_IPV6; |
| 1537 | break; |
| 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | /* if the header doesn't match an IP address, fetch next one */ |
| 1543 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 1544 | return 0; |
| 1545 | } |
| 1546 | return ret; |
| 1547 | } |
| 1548 | |
| 1549 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 1550 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 1551 | */ |
| 1552 | static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1553 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1554 | struct channel *chn = SMP_REQ_CHN(smp); |
| 1555 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1556 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1557 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1558 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1559 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1560 | struct ist path; |
| 1561 | size_t len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1562 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1563 | if (!htx) |
| 1564 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1565 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1566 | sl = http_find_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1567 | path = http_get_path(htx_sl_req_uri(sl)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1568 | if (!path.ptr) |
| 1569 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1570 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1571 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++) |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1572 | ; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1573 | |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1574 | /* OK, we got the '/' ! */ |
| 1575 | smp->data.type = SMP_T_STR; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1576 | smp->data.u.str.area = path.ptr; |
| 1577 | smp->data.u.str.data = len; |
| 1578 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1579 | } |
| 1580 | else { |
| 1581 | struct http_txn *txn; |
| 1582 | char *ptr, *end; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1583 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1584 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1585 | |
| 1586 | txn = smp->strm->txn; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1587 | end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1588 | ptr = http_txn_get_path(txn); |
| 1589 | if (!ptr) |
| 1590 | return 0; |
| 1591 | |
| 1592 | /* OK, we got the '/' ! */ |
| 1593 | smp->data.type = SMP_T_STR; |
| 1594 | smp->data.u.str.area = ptr; |
| 1595 | |
| 1596 | while (ptr < end && *ptr != '?') |
| 1597 | ptr++; |
| 1598 | |
| 1599 | smp->data.u.str.data = ptr - smp->data.u.str.area; |
| 1600 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1601 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1602 | return 1; |
| 1603 | } |
| 1604 | |
| 1605 | /* This produces a concatenation of the first occurrence of the Host header |
| 1606 | * followed by the path component if it begins with a slash ('/'). This means |
| 1607 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 1608 | * If no Host header is found, then the path is returned as-is. The returned |
| 1609 | * value is stored in the trash so it does not need to be marked constant. |
| 1610 | * The returned sample is of type string. |
| 1611 | */ |
| 1612 | static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1613 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1614 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1615 | struct buffer *temp; |
| 1616 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1617 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1618 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1619 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1620 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1621 | struct http_hdr_ctx ctx; |
| 1622 | struct ist path; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1623 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1624 | if (!htx) |
| 1625 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1626 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1627 | ctx.blk = NULL; |
| 1628 | if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len) |
| 1629 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1630 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1631 | /* OK we have the header value in ctx.value */ |
| 1632 | temp = get_trash_chunk(); |
| 1633 | chunk_memcat(temp, ctx.value.ptr, ctx.value.len); |
| 1634 | |
| 1635 | /* now retrieve the path */ |
| 1636 | sl = http_find_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1637 | path = http_get_path(htx_sl_req_uri(sl)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1638 | if (path.ptr) { |
| 1639 | size_t len; |
| 1640 | |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1641 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++) |
| 1642 | ; |
| 1643 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1644 | if (len && *(path.ptr) == '/') |
| 1645 | chunk_memcat(temp, path.ptr, len); |
| 1646 | } |
| 1647 | |
| 1648 | smp->data.type = SMP_T_STR; |
| 1649 | smp->data.u.str = *temp; |
| 1650 | } |
| 1651 | else { |
| 1652 | /* LEGACY version */ |
| 1653 | struct http_txn *txn; |
| 1654 | char *ptr, *end, *beg; |
| 1655 | struct hdr_ctx ctx; |
| 1656 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1657 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1658 | |
| 1659 | txn = smp->strm->txn; |
| 1660 | ctx.idx = 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1661 | if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1662 | return smp_fetch_path(args, smp, kw, private); |
| 1663 | |
| 1664 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 1665 | temp = get_trash_chunk(); |
| 1666 | memcpy(temp->area, ctx.line + ctx.val, ctx.vlen); |
| 1667 | smp->data.type = SMP_T_STR; |
| 1668 | smp->data.u.str.area = temp->area; |
| 1669 | smp->data.u.str.data = ctx.vlen; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1670 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1671 | /* now retrieve the path */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1672 | end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1673 | beg = http_txn_get_path(txn); |
| 1674 | if (!beg) |
| 1675 | beg = end; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1676 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1677 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 1678 | |
| 1679 | if (beg < ptr && *beg == '/') { |
| 1680 | memcpy(smp->data.u.str.area + smp->data.u.str.data, beg, |
| 1681 | ptr - beg); |
| 1682 | smp->data.u.str.data += ptr - beg; |
| 1683 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | smp->flags = SMP_F_VOL_1ST; |
| 1687 | return 1; |
| 1688 | } |
| 1689 | |
| 1690 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 1691 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 1692 | * This means that '*' will not be added, resulting in exactly the first Host |
| 1693 | * entry. If no Host header is found, then the path is used. The resulting value |
| 1694 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 1695 | * 32-bit integer value. This fetch is useful for tracking per-path activity on |
| 1696 | * high-traffic sites without having to store whole paths. |
| 1697 | */ |
| 1698 | static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1699 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1700 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1701 | unsigned int hash = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1702 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1703 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1704 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1705 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1706 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1707 | struct http_hdr_ctx ctx; |
| 1708 | struct ist path; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1709 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1710 | if (!htx) |
| 1711 | return 0; |
| 1712 | |
| 1713 | ctx.blk = NULL; |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1714 | if (http_find_header(htx, ist("Host"), &ctx, 0)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1715 | /* OK we have the header value in ctx.value */ |
| 1716 | while (ctx.value.len--) |
| 1717 | hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash; |
| 1718 | } |
| 1719 | |
| 1720 | /* now retrieve the path */ |
| 1721 | sl = http_find_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1722 | path = http_get_path(htx_sl_req_uri(sl)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1723 | if (path.ptr) { |
| 1724 | size_t len; |
| 1725 | |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1726 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++) |
| 1727 | ; |
| 1728 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1729 | if (len && *(path.ptr) == '/') { |
| 1730 | while (len--) |
| 1731 | hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash; |
| 1732 | } |
| 1733 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1734 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1735 | else { |
| 1736 | /* LEGACY version */ |
| 1737 | struct http_txn *txn; |
| 1738 | struct hdr_ctx ctx; |
| 1739 | char *ptr, *beg, *end; |
| 1740 | int len; |
| 1741 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1742 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1743 | |
| 1744 | txn = smp->strm->txn; |
| 1745 | ctx.idx = 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1746 | if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1747 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 1748 | ptr = ctx.line + ctx.val; |
| 1749 | len = ctx.vlen; |
| 1750 | while (len--) |
| 1751 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 1752 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1753 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1754 | /* now retrieve the path */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1755 | end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1756 | beg = http_txn_get_path(txn); |
| 1757 | if (!beg) |
| 1758 | beg = end; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1759 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1760 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1761 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1762 | if (beg < ptr && *beg == '/') { |
| 1763 | while (beg < ptr) |
| 1764 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 1765 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1766 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1767 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1768 | hash = full_hash(hash); |
| 1769 | |
| 1770 | smp->data.type = SMP_T_SINT; |
| 1771 | smp->data.u.sint = hash; |
| 1772 | smp->flags = SMP_F_VOL_1ST; |
| 1773 | return 1; |
| 1774 | } |
| 1775 | |
| 1776 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 1777 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 1778 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 1779 | * on the source address length. The path hash is stored before the address so |
| 1780 | * that in environments where IPv6 is insignificant, truncating the output to |
| 1781 | * 8 bytes would still work. |
| 1782 | */ |
| 1783 | static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1784 | { |
| 1785 | struct buffer *temp; |
| 1786 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
| 1787 | |
| 1788 | if (!cli_conn) |
| 1789 | return 0; |
| 1790 | |
| 1791 | if (!smp_fetch_base32(args, smp, kw, private)) |
| 1792 | return 0; |
| 1793 | |
| 1794 | temp = get_trash_chunk(); |
| 1795 | *(unsigned int *) temp->area = htonl(smp->data.u.sint); |
| 1796 | temp->data += sizeof(unsigned int); |
| 1797 | |
| 1798 | switch (cli_conn->addr.from.ss_family) { |
| 1799 | case AF_INET: |
| 1800 | memcpy(temp->area + temp->data, |
| 1801 | &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, |
| 1802 | 4); |
| 1803 | temp->data += 4; |
| 1804 | break; |
| 1805 | case AF_INET6: |
| 1806 | memcpy(temp->area + temp->data, |
| 1807 | &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, |
| 1808 | 16); |
| 1809 | temp->data += 16; |
| 1810 | break; |
| 1811 | default: |
| 1812 | return 0; |
| 1813 | } |
| 1814 | |
| 1815 | smp->data.u.str = *temp; |
| 1816 | smp->data.type = SMP_T_BIN; |
| 1817 | return 1; |
| 1818 | } |
| 1819 | |
| 1820 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 1821 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 1822 | * of type string carrying the whole query string. |
| 1823 | */ |
| 1824 | static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1825 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1826 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1827 | char *ptr, *end; |
| 1828 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1829 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1830 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1831 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1832 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1833 | |
| 1834 | if (!htx) |
| 1835 | return 0; |
| 1836 | |
| 1837 | sl = http_find_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1838 | ptr = HTX_SL_REQ_UPTR(sl); |
| 1839 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1840 | } |
| 1841 | else { |
| 1842 | /* LEGACY version */ |
| 1843 | struct http_txn *txn; |
| 1844 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1845 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1846 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1847 | txn = smp->strm->txn; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1848 | ptr = ci_head(chn) + txn->req.sl.rq.u; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1849 | end = ptr + txn->req.sl.rq.u_l; |
| 1850 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1851 | |
| 1852 | /* look up the '?' */ |
| 1853 | do { |
| 1854 | if (ptr == end) |
| 1855 | return 0; |
| 1856 | } while (*ptr++ != '?'); |
| 1857 | |
| 1858 | smp->data.type = SMP_T_STR; |
| 1859 | smp->data.u.str.area = ptr; |
| 1860 | smp->data.u.str.data = end - ptr; |
| 1861 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1862 | return 1; |
| 1863 | } |
| 1864 | |
| 1865 | static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1866 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1867 | struct channel *chn = SMP_REQ_CHN(smp); |
| 1868 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1869 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1870 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1871 | struct htx *htx = smp_prefetch_htx(smp, chn, 0); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1872 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1873 | if (!htx) |
| 1874 | return 0; |
| 1875 | } |
| 1876 | else { |
| 1877 | /* LEGACY version */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1878 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1879 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 1880 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 1881 | */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1882 | CHECK_HTTP_MESSAGE_FIRST_PERM(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1883 | } |
| 1884 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1885 | smp->data.u.sint = 1; |
| 1886 | return 1; |
| 1887 | } |
| 1888 | |
| 1889 | /* return a valid test if the current request is the first one on the connection */ |
| 1890 | static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1891 | { |
| 1892 | smp->data.type = SMP_T_BOOL; |
| 1893 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
| 1894 | return 1; |
| 1895 | } |
| 1896 | |
| 1897 | /* Accepts exactly 1 argument of type userlist */ |
| 1898 | static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1899 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1900 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1901 | |
| 1902 | if (!args || args->type != ARGT_USR) |
| 1903 | return 0; |
| 1904 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1905 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1906 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1907 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1908 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1909 | if (!htx) |
| 1910 | return 0; |
| 1911 | } |
| 1912 | else { |
| 1913 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1914 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1915 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1916 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1917 | if (!get_http_auth(smp)) |
| 1918 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1919 | smp->data.type = SMP_T_BOOL; |
| 1920 | 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] | 1921 | smp->strm->txn->auth.pass); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1922 | return 1; |
| 1923 | } |
| 1924 | |
| 1925 | /* Accepts exactly 1 argument of type userlist */ |
| 1926 | static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1927 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1928 | struct channel *chn = SMP_REQ_CHN(smp); |
| 1929 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1930 | if (!args || args->type != ARGT_USR) |
| 1931 | return 0; |
| 1932 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1933 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1934 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1935 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1936 | |
| 1937 | if (!htx) |
| 1938 | return 0; |
| 1939 | } |
| 1940 | else { |
| 1941 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1942 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1943 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1944 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1945 | if (!get_http_auth(smp)) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1946 | return 0; |
| 1947 | |
| 1948 | /* if the user does not belong to the userlist or has a wrong password, |
| 1949 | * report that it unconditionally does not match. Otherwise we return |
| 1950 | * a string containing the username. |
| 1951 | */ |
| 1952 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 1953 | smp->strm->txn->auth.pass)) |
| 1954 | return 0; |
| 1955 | |
| 1956 | /* pat_match_auth() will need the user list */ |
| 1957 | smp->ctx.a[0] = args->data.usr; |
| 1958 | |
| 1959 | smp->data.type = SMP_T_STR; |
| 1960 | smp->flags = SMP_F_CONST; |
| 1961 | smp->data.u.str.area = smp->strm->txn->auth.user; |
| 1962 | smp->data.u.str.data = strlen(smp->strm->txn->auth.user); |
| 1963 | |
| 1964 | return 1; |
| 1965 | } |
| 1966 | |
| 1967 | /* Fetch a captured HTTP request header. The index is the position of |
| 1968 | * the "capture" option in the configuration file |
| 1969 | */ |
| 1970 | static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1971 | { |
| 1972 | struct proxy *fe = strm_fe(smp->strm); |
| 1973 | int idx; |
| 1974 | |
| 1975 | if (!args || args->type != ARGT_SINT) |
| 1976 | return 0; |
| 1977 | |
| 1978 | idx = args->data.sint; |
| 1979 | |
| 1980 | if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL) |
| 1981 | return 0; |
| 1982 | |
| 1983 | smp->data.type = SMP_T_STR; |
| 1984 | smp->flags |= SMP_F_CONST; |
| 1985 | smp->data.u.str.area = smp->strm->req_cap[idx]; |
| 1986 | smp->data.u.str.data = strlen(smp->strm->req_cap[idx]); |
| 1987 | |
| 1988 | return 1; |
| 1989 | } |
| 1990 | |
| 1991 | /* Fetch a captured HTTP response header. The index is the position of |
| 1992 | * the "capture" option in the configuration file |
| 1993 | */ |
| 1994 | static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1995 | { |
| 1996 | struct proxy *fe = strm_fe(smp->strm); |
| 1997 | int idx; |
| 1998 | |
| 1999 | if (!args || args->type != ARGT_SINT) |
| 2000 | return 0; |
| 2001 | |
| 2002 | idx = args->data.sint; |
| 2003 | |
| 2004 | if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL) |
| 2005 | return 0; |
| 2006 | |
| 2007 | smp->data.type = SMP_T_STR; |
| 2008 | smp->flags |= SMP_F_CONST; |
| 2009 | smp->data.u.str.area = smp->strm->res_cap[idx]; |
| 2010 | smp->data.u.str.data = strlen(smp->strm->res_cap[idx]); |
| 2011 | |
| 2012 | return 1; |
| 2013 | } |
| 2014 | |
| 2015 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 2016 | static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2017 | { |
| 2018 | struct buffer *temp; |
| 2019 | struct http_txn *txn = smp->strm->txn; |
| 2020 | char *ptr; |
| 2021 | |
| 2022 | if (!txn || !txn->uri) |
| 2023 | return 0; |
| 2024 | |
| 2025 | ptr = txn->uri; |
| 2026 | |
| 2027 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 2028 | ptr++; |
| 2029 | |
| 2030 | temp = get_trash_chunk(); |
| 2031 | temp->area = txn->uri; |
| 2032 | temp->data = ptr - txn->uri; |
| 2033 | smp->data.u.str = *temp; |
| 2034 | smp->data.type = SMP_T_STR; |
| 2035 | smp->flags = SMP_F_CONST; |
| 2036 | |
| 2037 | return 1; |
| 2038 | |
| 2039 | } |
| 2040 | |
| 2041 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 2042 | static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2043 | { |
| 2044 | struct http_txn *txn = smp->strm->txn; |
| 2045 | struct ist path; |
| 2046 | const char *ptr; |
| 2047 | |
| 2048 | if (!txn || !txn->uri) |
| 2049 | return 0; |
| 2050 | |
| 2051 | ptr = txn->uri; |
| 2052 | |
| 2053 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 2054 | ptr++; |
| 2055 | |
| 2056 | if (!*ptr) |
| 2057 | return 0; |
| 2058 | |
Christopher Faulet | 78337bb | 2018-11-15 14:35:18 +0100 | [diff] [blame] | 2059 | /* skip the first space and find space after URI */ |
| 2060 | path = ist2(++ptr, 0); |
| 2061 | while (*ptr != ' ' && *ptr != '\0') |
| 2062 | ptr++; |
| 2063 | path.len = ptr - path.ptr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2064 | |
Christopher Faulet | 78337bb | 2018-11-15 14:35:18 +0100 | [diff] [blame] | 2065 | path = http_get_path(path); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2066 | if (!path.ptr) |
| 2067 | return 0; |
| 2068 | |
| 2069 | smp->data.u.str.area = path.ptr; |
| 2070 | smp->data.u.str.data = path.len; |
| 2071 | smp->data.type = SMP_T_STR; |
| 2072 | smp->flags = SMP_F_CONST; |
| 2073 | |
| 2074 | return 1; |
| 2075 | } |
| 2076 | |
| 2077 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 2078 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 2079 | */ |
| 2080 | static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2081 | { |
| 2082 | struct http_txn *txn = smp->strm->txn; |
| 2083 | |
| 2084 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
| 2085 | return 0; |
| 2086 | |
| 2087 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 2088 | smp->data.u.str.area = "HTTP/1.1"; |
| 2089 | else |
| 2090 | smp->data.u.str.area = "HTTP/1.0"; |
| 2091 | |
| 2092 | smp->data.u.str.data = 8; |
| 2093 | smp->data.type = SMP_T_STR; |
| 2094 | smp->flags = SMP_F_CONST; |
| 2095 | return 1; |
| 2096 | |
| 2097 | } |
| 2098 | |
| 2099 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 2100 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 2101 | */ |
| 2102 | static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2103 | { |
| 2104 | struct http_txn *txn = smp->strm->txn; |
| 2105 | |
| 2106 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
| 2107 | return 0; |
| 2108 | |
| 2109 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 2110 | smp->data.u.str.area = "HTTP/1.1"; |
| 2111 | else |
| 2112 | smp->data.u.str.area = "HTTP/1.0"; |
| 2113 | |
| 2114 | smp->data.u.str.data = 8; |
| 2115 | smp->data.type = SMP_T_STR; |
| 2116 | smp->flags = SMP_F_CONST; |
| 2117 | return 1; |
| 2118 | |
| 2119 | } |
| 2120 | |
| 2121 | /* Iterate over all cookies present in a message. The context is stored in |
| 2122 | * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the |
| 2123 | * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on |
| 2124 | * the direction, multiple cookies may be parsed on the same line or not. |
| 2125 | * The cookie name is in args and the name length in args->data.str.len. |
| 2126 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 2127 | * that no iterating is desired, then only last value is fetched if any. |
| 2128 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 2129 | * files. |
| 2130 | */ |
| 2131 | static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2132 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2133 | /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */ |
| 2134 | struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2135 | int occ = 0; |
| 2136 | int found = 0; |
| 2137 | |
| 2138 | if (!args || args->type != ARGT_STR) |
| 2139 | return 0; |
| 2140 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2141 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2142 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2143 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2144 | struct http_hdr_ctx *ctx = smp->ctx.a[2]; |
| 2145 | struct ist hdr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2146 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2147 | if (!ctx) { |
| 2148 | /* first call */ |
| 2149 | ctx = &static_http_hdr_ctx; |
| 2150 | ctx->blk = NULL; |
| 2151 | smp->ctx.a[2] = ctx; |
| 2152 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2153 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2154 | if (!htx) |
| 2155 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2156 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2157 | hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie")); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2158 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2159 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 2160 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 2161 | occ = -1; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2162 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2163 | /* OK so basically here, either we want only one value and it's the |
| 2164 | * last one, or we want to iterate over all of them and we fetch the |
| 2165 | * next one. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2166 | */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2167 | |
| 2168 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
| 2169 | /* search for the header from the beginning, we must first initialize |
| 2170 | * the search parameters. |
| 2171 | */ |
| 2172 | smp->ctx.a[0] = NULL; |
| 2173 | ctx->blk = NULL; |
| 2174 | } |
| 2175 | |
| 2176 | smp->flags |= SMP_F_VOL_HDR; |
| 2177 | while (1) { |
| 2178 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 2179 | if (!smp->ctx.a[0]) { |
| 2180 | if (!http_find_header(htx, hdr, ctx, 0)) |
| 2181 | goto out; |
| 2182 | |
| 2183 | if (ctx->value.len < args->data.str.data + 1) |
| 2184 | continue; |
| 2185 | |
| 2186 | smp->ctx.a[0] = ctx->value.ptr; |
| 2187 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len; |
| 2188 | } |
| 2189 | |
| 2190 | smp->data.type = SMP_T_STR; |
| 2191 | smp->flags |= SMP_F_CONST; |
| 2192 | smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1], |
| 2193 | args->data.str.area, args->data.str.data, |
| 2194 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 2195 | &smp->data.u.str.area, |
| 2196 | &smp->data.u.str.data); |
| 2197 | if (smp->ctx.a[0]) { |
| 2198 | found = 1; |
| 2199 | if (occ >= 0) { |
| 2200 | /* one value was returned into smp->data.u.str.{str,len} */ |
| 2201 | smp->flags |= SMP_F_NOT_LAST; |
| 2202 | return 1; |
| 2203 | } |
| 2204 | } |
| 2205 | /* if we're looking for last occurrence, let's loop */ |
| 2206 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2207 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2208 | else { |
| 2209 | /* LEGACY version */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2210 | struct hdr_idx *idx; |
| 2211 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2212 | const char *hdr_name; |
| 2213 | int hdr_name_len; |
| 2214 | char *sol; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2215 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2216 | if (!ctx) { |
| 2217 | /* first call */ |
| 2218 | ctx = &static_hdr_ctx; |
| 2219 | ctx->idx = 0; |
| 2220 | smp->ctx.a[2] = ctx; |
| 2221 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2222 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2223 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2224 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2225 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2226 | if (!(chn->flags & CF_ISRESP)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2227 | hdr_name = "Cookie"; |
| 2228 | hdr_name_len = 6; |
| 2229 | } else { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2230 | hdr_name = "Set-Cookie"; |
| 2231 | hdr_name_len = 10; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2232 | } |
| 2233 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2234 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 2235 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 2236 | occ = -1; |
| 2237 | |
| 2238 | /* OK so basically here, either we want only one value and it's the |
| 2239 | * last one, or we want to iterate over all of them and we fetch the |
| 2240 | * next one. |
| 2241 | */ |
| 2242 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2243 | sol = ci_head(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2244 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
| 2245 | /* search for the header from the beginning, we must first initialize |
| 2246 | * the search parameters. |
| 2247 | */ |
| 2248 | smp->ctx.a[0] = NULL; |
| 2249 | ctx->idx = 0; |
| 2250 | } |
| 2251 | |
| 2252 | smp->flags |= SMP_F_VOL_HDR; |
| 2253 | |
| 2254 | while (1) { |
| 2255 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 2256 | if (!smp->ctx.a[0]) { |
| 2257 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 2258 | goto out; |
| 2259 | |
| 2260 | if (ctx->vlen < args->data.str.data + 1) |
| 2261 | continue; |
| 2262 | |
| 2263 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 2264 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
| 2265 | } |
| 2266 | |
| 2267 | smp->data.type = SMP_T_STR; |
| 2268 | smp->flags |= SMP_F_CONST; |
| 2269 | smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1], |
| 2270 | args->data.str.area, args->data.str.data, |
| 2271 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 2272 | &smp->data.u.str.area, &smp->data.u.str.data); |
| 2273 | if (smp->ctx.a[0]) { |
| 2274 | found = 1; |
| 2275 | if (occ >= 0) { |
| 2276 | /* one value was returned into smp->data.u.str.{str,len} */ |
| 2277 | smp->flags |= SMP_F_NOT_LAST; |
| 2278 | return 1; |
| 2279 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2280 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2281 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2282 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2283 | } |
| 2284 | /* all cookie headers and values were scanned. If we're looking for the |
| 2285 | * last occurrence, we may return it now. |
| 2286 | */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2287 | out: |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2288 | smp->flags &= ~SMP_F_NOT_LAST; |
| 2289 | return found; |
| 2290 | } |
| 2291 | |
| 2292 | /* Iterate over all cookies present in a request to count how many occurrences |
| 2293 | * match the name in args and args->data.str.len. If <multi> is non-null, then |
| 2294 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 2295 | * type UINT. Accepts exactly 1 argument of type string. |
| 2296 | */ |
| 2297 | static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2298 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2299 | /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */ |
| 2300 | struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp)); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2301 | char *val_beg, *val_end; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2302 | int cnt; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2303 | |
| 2304 | if (!args || args->type != ARGT_STR) |
| 2305 | return 0; |
| 2306 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2307 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2308 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2309 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2310 | struct http_hdr_ctx ctx; |
| 2311 | struct ist hdr; |
| 2312 | |
| 2313 | if (!htx) |
| 2314 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2315 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2316 | hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie")); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2317 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2318 | val_end = val_beg = NULL; |
| 2319 | ctx.blk = NULL; |
| 2320 | cnt = 0; |
| 2321 | while (1) { |
| 2322 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 2323 | if (!val_beg) { |
| 2324 | if (!http_find_header(htx, hdr, &ctx, 0)) |
| 2325 | break; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2326 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2327 | if (ctx.value.len < args->data.str.data + 1) |
| 2328 | continue; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2329 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2330 | val_beg = ctx.value.ptr; |
| 2331 | val_end = val_beg + ctx.value.len; |
| 2332 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2333 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2334 | smp->data.type = SMP_T_STR; |
| 2335 | smp->flags |= SMP_F_CONST; |
| 2336 | while ((val_beg = http_extract_cookie_value(val_beg, val_end, |
| 2337 | args->data.str.area, args->data.str.data, |
| 2338 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 2339 | &smp->data.u.str.area, |
| 2340 | &smp->data.u.str.data))) { |
| 2341 | cnt++; |
| 2342 | } |
| 2343 | } |
| 2344 | } |
| 2345 | else { |
| 2346 | /* LEGACY version */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2347 | struct hdr_idx *idx; |
| 2348 | struct hdr_ctx ctx; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2349 | const char *hdr_name; |
| 2350 | int hdr_name_len; |
| 2351 | char *sol; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2352 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2353 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2354 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2355 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2356 | if (!(chn->flags & CF_ISRESP)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2357 | hdr_name = "Cookie"; |
| 2358 | hdr_name_len = 6; |
| 2359 | } else { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2360 | hdr_name = "Set-Cookie"; |
| 2361 | hdr_name_len = 10; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2362 | } |
| 2363 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2364 | sol = ci_head(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2365 | val_end = val_beg = NULL; |
| 2366 | ctx.idx = 0; |
| 2367 | cnt = 0; |
| 2368 | |
| 2369 | while (1) { |
| 2370 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 2371 | if (!val_beg) { |
| 2372 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 2373 | break; |
| 2374 | |
| 2375 | if (ctx.vlen < args->data.str.data + 1) |
| 2376 | continue; |
| 2377 | |
| 2378 | val_beg = ctx.line + ctx.val; |
| 2379 | val_end = val_beg + ctx.vlen; |
| 2380 | } |
| 2381 | |
| 2382 | smp->data.type = SMP_T_STR; |
| 2383 | smp->flags |= SMP_F_CONST; |
| 2384 | while ((val_beg = http_extract_cookie_value(val_beg, val_end, |
| 2385 | args->data.str.area, args->data.str.data, |
| 2386 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 2387 | &smp->data.u.str.area, &smp->data.u.str.data))) { |
| 2388 | cnt++; |
| 2389 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2390 | } |
| 2391 | } |
| 2392 | |
| 2393 | smp->data.type = SMP_T_SINT; |
| 2394 | smp->data.u.sint = cnt; |
| 2395 | smp->flags |= SMP_F_VOL_HDR; |
| 2396 | return 1; |
| 2397 | } |
| 2398 | |
| 2399 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 2400 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 2401 | */ |
| 2402 | static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2403 | { |
| 2404 | int ret = smp_fetch_cookie(args, smp, kw, private); |
| 2405 | |
| 2406 | if (ret > 0) { |
| 2407 | smp->data.type = SMP_T_SINT; |
| 2408 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 2409 | smp->data.u.str.data); |
| 2410 | } |
| 2411 | |
| 2412 | return ret; |
| 2413 | } |
| 2414 | |
| 2415 | /************************************************************************/ |
| 2416 | /* The code below is dedicated to sample fetches */ |
| 2417 | /************************************************************************/ |
| 2418 | |
| 2419 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 2420 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 2421 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 2422 | * pointers are updated for next iteration before leaving. |
| 2423 | */ |
| 2424 | 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) |
| 2425 | { |
| 2426 | const char *vstart, *vend; |
| 2427 | struct buffer *temp; |
| 2428 | const char **chunks = (const char **)smp->ctx.a; |
| 2429 | |
| 2430 | if (!http_find_next_url_param(chunks, name, name_len, |
| 2431 | &vstart, &vend, delim)) |
| 2432 | return 0; |
| 2433 | |
| 2434 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 2435 | * if the value is wrapped, copy-it in a buffer. |
| 2436 | */ |
| 2437 | smp->data.type = SMP_T_STR; |
| 2438 | if (chunks[2] && |
| 2439 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 2440 | vend >= chunks[2] && vend <= chunks[3]) { |
| 2441 | /* Wrapped case. */ |
| 2442 | temp = get_trash_chunk(); |
| 2443 | memcpy(temp->area, vstart, chunks[1] - vstart); |
| 2444 | memcpy(temp->area + ( chunks[1] - vstart ), chunks[2], |
| 2445 | vend - chunks[2]); |
| 2446 | smp->data.u.str.area = temp->area; |
| 2447 | smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
| 2448 | } else { |
| 2449 | /* Contiguous case. */ |
| 2450 | smp->data.u.str.area = (char *)vstart; |
| 2451 | smp->data.u.str.data = vend - vstart; |
| 2452 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 2453 | } |
| 2454 | |
| 2455 | /* Update context, check wrapping. */ |
| 2456 | chunks[0] = vend; |
| 2457 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 2458 | chunks[1] = chunks[3]; |
| 2459 | chunks[2] = NULL; |
| 2460 | } |
| 2461 | |
| 2462 | if (chunks[0] < chunks[1]) |
| 2463 | smp->flags |= SMP_F_NOT_LAST; |
| 2464 | |
| 2465 | return 1; |
| 2466 | } |
| 2467 | |
| 2468 | /* This function iterates over each parameter of the query string. It uses |
| 2469 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the current |
| 2470 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 2471 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 2472 | * considered. It supports an optional delimiter argument for the beginning of |
| 2473 | * the string in args[1], which defaults to "?". |
| 2474 | */ |
| 2475 | static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2476 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2477 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2478 | char delim = '?'; |
| 2479 | const char *name; |
| 2480 | int name_len; |
| 2481 | |
| 2482 | if (!args || |
| 2483 | (args[0].type && args[0].type != ARGT_STR) || |
| 2484 | (args[1].type && args[1].type != ARGT_STR)) |
| 2485 | return 0; |
| 2486 | |
| 2487 | name = ""; |
| 2488 | name_len = 0; |
| 2489 | if (args->type == ARGT_STR) { |
| 2490 | name = args->data.str.area; |
| 2491 | name_len = args->data.str.data; |
| 2492 | } |
| 2493 | |
| 2494 | if (args[1].type) |
| 2495 | delim = *args[1].data.str.area; |
| 2496 | |
| 2497 | if (!smp->ctx.a[0]) { // first call, find the query string |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2498 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2499 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2500 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2501 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2502 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2503 | if (!htx) |
| 2504 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2505 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2506 | sl = http_find_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2507 | smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2508 | if (!smp->ctx.a[0]) |
| 2509 | return 0; |
| 2510 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2511 | smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2512 | } |
| 2513 | else { |
| 2514 | /* LEGACY version */ |
| 2515 | struct http_msg *msg; |
| 2516 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2517 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2518 | |
| 2519 | msg = &smp->strm->txn->req; |
| 2520 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2521 | smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u, |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2522 | msg->sl.rq.u_l, delim); |
| 2523 | if (!smp->ctx.a[0]) |
| 2524 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2525 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2526 | smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2527 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2528 | |
| 2529 | /* Assume that the context is filled with NULL pointer |
| 2530 | * before the first call. |
| 2531 | * smp->ctx.a[2] = NULL; |
| 2532 | * smp->ctx.a[3] = NULL; |
| 2533 | */ |
| 2534 | } |
| 2535 | |
| 2536 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 2537 | } |
| 2538 | |
| 2539 | /* This function iterates over each parameter of the body. This requires |
| 2540 | * that the body has been waited for using http-buffer-request. It uses |
| 2541 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 2542 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 2543 | * optional second part if the body wraps at the end of the buffer. An optional |
| 2544 | * parameter name is passed in args[0], otherwise any parameter is considered. |
| 2545 | */ |
| 2546 | static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2547 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2548 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2549 | const char *name; |
| 2550 | int name_len; |
| 2551 | |
| 2552 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 2553 | return 0; |
| 2554 | |
| 2555 | name = ""; |
| 2556 | name_len = 0; |
| 2557 | if (args[0].type == ARGT_STR) { |
| 2558 | name = args[0].data.str.area; |
| 2559 | name_len = args[0].data.str.data; |
| 2560 | } |
| 2561 | |
| 2562 | if (!smp->ctx.a[0]) { // first call, find the query string |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2563 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2564 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2565 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2566 | struct buffer *temp; |
| 2567 | int32_t pos; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2568 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2569 | if (!htx) |
| 2570 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2571 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2572 | temp = get_trash_chunk(); |
| 2573 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 2574 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 2575 | enum htx_blk_type type = htx_get_blk_type(blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2576 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2577 | if (type == HTX_BLK_EOM || type == HTX_BLK_EOD) |
| 2578 | break; |
| 2579 | if (type == HTX_BLK_DATA) { |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 2580 | if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0)) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2581 | return 0; |
| 2582 | } |
| 2583 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2584 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2585 | smp->ctx.a[0] = temp->area; |
| 2586 | smp->ctx.a[1] = temp->area + temp->data; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2587 | |
| 2588 | /* Assume that the context is filled with NULL pointer |
| 2589 | * before the first call. |
| 2590 | * smp->ctx.a[2] = NULL; |
| 2591 | * smp->ctx.a[3] = NULL; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2592 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2593 | } |
| 2594 | else { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2595 | /* LEGACY version */ |
| 2596 | struct http_msg *msg; |
| 2597 | unsigned long len; |
| 2598 | unsigned long block1; |
| 2599 | char *body; |
| 2600 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2601 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2602 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2603 | msg = &smp->strm->txn->req; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2604 | len = http_body_bytes(msg); |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2605 | body = c_ptr(chn, -http_data_rewind(msg)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2606 | |
| 2607 | block1 = len; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2608 | if (block1 > b_wrap(&chn->buf) - body) |
| 2609 | block1 = b_wrap(&chn->buf) - body; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2610 | |
| 2611 | if (block1 == len) { |
| 2612 | /* buffer is not wrapped (or empty) */ |
| 2613 | smp->ctx.a[0] = body; |
| 2614 | smp->ctx.a[1] = body + len; |
| 2615 | |
| 2616 | /* Assume that the context is filled with NULL pointer |
| 2617 | * before the first call. |
| 2618 | * smp->ctx.a[2] = NULL; |
| 2619 | * smp->ctx.a[3] = NULL; |
| 2620 | */ |
| 2621 | } |
| 2622 | else { |
| 2623 | /* buffer is wrapped, we need to defragment it */ |
| 2624 | smp->ctx.a[0] = body; |
| 2625 | smp->ctx.a[1] = body + block1; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2626 | smp->ctx.a[2] = b_orig(&chn->buf); |
| 2627 | smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 ); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2628 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2629 | } |
| 2630 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2631 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2632 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 2633 | } |
| 2634 | |
| 2635 | /* Return the signed integer value for the specified url parameter (see url_param |
| 2636 | * above). |
| 2637 | */ |
| 2638 | static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2639 | { |
| 2640 | int ret = smp_fetch_url_param(args, smp, kw, private); |
| 2641 | |
| 2642 | if (ret > 0) { |
| 2643 | smp->data.type = SMP_T_SINT; |
| 2644 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 2645 | smp->data.u.str.data); |
| 2646 | } |
| 2647 | |
| 2648 | return ret; |
| 2649 | } |
| 2650 | |
| 2651 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 2652 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 2653 | * This means that '*' will not be added, resulting in exactly the first Host |
| 2654 | * entry. If no Host header is found, then the path is used. The resulting value |
| 2655 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 2656 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 2657 | * high-traffic sites without having to store whole paths. |
| 2658 | * this differs from the base32 functions in that it includes the url parameters |
| 2659 | * as well as the path |
| 2660 | */ |
| 2661 | static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2662 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2663 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2664 | unsigned int hash = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2665 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2666 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2667 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2668 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2669 | struct http_hdr_ctx ctx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2670 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2671 | struct ist path; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2672 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2673 | if (!htx) |
| 2674 | return 0; |
| 2675 | |
| 2676 | ctx.blk = NULL; |
| 2677 | if (http_find_header(htx, ist("Host"), &ctx, 1)) { |
| 2678 | /* OK we have the header value in ctx.value */ |
| 2679 | while (ctx.value.len--) |
| 2680 | hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash; |
| 2681 | } |
| 2682 | |
| 2683 | /* now retrieve the path */ |
| 2684 | sl = http_find_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2685 | path = http_get_path(htx_sl_req_uri(sl)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2686 | while (path.len > 0 && *(path.ptr) != '?') { |
| 2687 | path.ptr++; |
| 2688 | path.len--; |
| 2689 | } |
| 2690 | if (path.len && *(path.ptr) == '/') { |
| 2691 | while (path.len--) |
| 2692 | hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash; |
| 2693 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2694 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2695 | else { |
| 2696 | /* LEGACY version */ |
| 2697 | struct http_txn *txn; |
| 2698 | struct hdr_ctx ctx; |
| 2699 | char *ptr, *beg, *end; |
| 2700 | int len; |
| 2701 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2702 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2703 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2704 | txn = smp->strm->txn; |
| 2705 | ctx.idx = 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2706 | if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2707 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 2708 | ptr = ctx.line + ctx.val; |
| 2709 | len = ctx.vlen; |
| 2710 | while (len--) |
| 2711 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 2712 | } |
| 2713 | |
| 2714 | /* now retrieve the path */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2715 | end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2716 | beg = http_txn_get_path(txn); |
| 2717 | if (!beg) |
| 2718 | beg = end; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2719 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2720 | for (ptr = beg; ptr < end ; ptr++); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2721 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2722 | if (beg < ptr && *beg == '/') { |
| 2723 | while (beg < ptr) |
| 2724 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 2725 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2726 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2727 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2728 | hash = full_hash(hash); |
| 2729 | |
| 2730 | smp->data.type = SMP_T_SINT; |
| 2731 | smp->data.u.sint = hash; |
| 2732 | smp->flags = SMP_F_VOL_1ST; |
| 2733 | return 1; |
| 2734 | } |
| 2735 | |
| 2736 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 2737 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 2738 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 2739 | * on the source address length. The URL hash is stored before the address so |
| 2740 | * that in environments where IPv6 is insignificant, truncating the output to |
| 2741 | * 8 bytes would still work. |
| 2742 | */ |
| 2743 | static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2744 | { |
| 2745 | struct buffer *temp; |
| 2746 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
| 2747 | |
| 2748 | if (!cli_conn) |
| 2749 | return 0; |
| 2750 | |
| 2751 | if (!smp_fetch_url32(args, smp, kw, private)) |
| 2752 | return 0; |
| 2753 | |
| 2754 | temp = get_trash_chunk(); |
| 2755 | *(unsigned int *) temp->area = htonl(smp->data.u.sint); |
| 2756 | temp->data += sizeof(unsigned int); |
| 2757 | |
| 2758 | switch (cli_conn->addr.from.ss_family) { |
| 2759 | case AF_INET: |
| 2760 | memcpy(temp->area + temp->data, |
| 2761 | &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, |
| 2762 | 4); |
| 2763 | temp->data += 4; |
| 2764 | break; |
| 2765 | case AF_INET6: |
| 2766 | memcpy(temp->area + temp->data, |
| 2767 | &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, |
| 2768 | 16); |
| 2769 | temp->data += 16; |
| 2770 | break; |
| 2771 | default: |
| 2772 | return 0; |
| 2773 | } |
| 2774 | |
| 2775 | smp->data.u.str = *temp; |
| 2776 | smp->data.type = SMP_T_BIN; |
| 2777 | return 1; |
| 2778 | } |
| 2779 | |
| 2780 | /************************************************************************/ |
| 2781 | /* Other utility functions */ |
| 2782 | /************************************************************************/ |
| 2783 | |
| 2784 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 2785 | * keyword. These keywords support an optional positive or negative occurrence |
| 2786 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 2787 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 2788 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 2789 | * error message in case of error, that the caller is responsible for freeing. |
| 2790 | * The initial location must either be freeable or NULL. |
| 2791 | * Note: this function's pointer is checked from Lua. |
| 2792 | */ |
| 2793 | int val_hdr(struct arg *arg, char **err_msg) |
| 2794 | { |
| 2795 | if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) { |
| 2796 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
| 2797 | return 0; |
| 2798 | } |
| 2799 | return 1; |
| 2800 | } |
| 2801 | |
| 2802 | /************************************************************************/ |
| 2803 | /* All supported sample fetch keywords must be declared here. */ |
| 2804 | /************************************************************************/ |
| 2805 | |
| 2806 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 2807 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 2808 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2809 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2810 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2811 | |
| 2812 | /* capture are allocated and are permanent in the stream */ |
| 2813 | { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2814 | |
| 2815 | /* retrieve these captures from the HTTP logs */ |
| 2816 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2817 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2818 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2819 | |
| 2820 | { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 2821 | { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2822 | |
| 2823 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 2824 | * are only here to match the ACL's name, are request-only and are used |
| 2825 | * for ACL compatibility only. |
| 2826 | */ |
| 2827 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2828 | { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
| 2829 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2830 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2831 | |
| 2832 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 2833 | * only here to match the ACL's name, are request-only and are used for |
| 2834 | * ACL compatibility only. |
| 2835 | */ |
| 2836 | { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
| 2837 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2838 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2839 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2840 | |
| 2841 | { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
| 2842 | { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2843 | { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2844 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
| 2845 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2846 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2847 | |
| 2848 | /* HTTP protocol on the request path */ |
| 2849 | { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2850 | { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2851 | |
| 2852 | /* HTTP version on the request path */ |
| 2853 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2854 | { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2855 | |
| 2856 | { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2857 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2858 | { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2859 | { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2860 | |
| 2861 | { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2862 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2863 | |
| 2864 | /* HTTP version on the response path */ |
| 2865 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2866 | { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2867 | |
| 2868 | /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */ |
| 2869 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2870 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2871 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2872 | |
| 2873 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
| 2874 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2875 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
| 2876 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2877 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2878 | { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2879 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2880 | |
| 2881 | /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */ |
| 2882 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2883 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2884 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2885 | |
| 2886 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2887 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2888 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2889 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2890 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 2891 | { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2892 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2893 | |
| 2894 | /* scook is valid only on the response and is used for ACL compatibility */ |
| 2895 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2896 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2897 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2898 | { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */ |
| 2899 | |
| 2900 | /* shdr is valid only on the response and is used for ACL compatibility */ |
| 2901 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2902 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2903 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 2904 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2905 | |
| 2906 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
| 2907 | { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV }, |
| 2908 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2909 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2910 | { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2911 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2912 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2913 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2914 | { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2915 | { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2916 | { /* END */ }, |
| 2917 | }}; |
| 2918 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2919 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2920 | |
| 2921 | /* |
| 2922 | * Local variables: |
| 2923 | * c-indent-level: 8 |
| 2924 | * c-basic-offset: 8 |
| 2925 | * End: |
| 2926 | */ |