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 | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 206 | if (htx_is_empty(htx) || htx->first == -1) { |
Christopher Faulet | ef453ed | 2018-10-24 21:39:27 +0200 | [diff] [blame] | 207 | /* Parsing is done by the mux, just wait */ |
| 208 | smp->flags |= SMP_F_MAY_CHANGE; |
| 209 | return NULL; |
| 210 | } |
| 211 | } |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 212 | sl = http_get_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 | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 437 | sl = http_get_stline(htx); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 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 | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 481 | sl = http_get_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 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 521 | sl = http_get_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 | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 562 | sl = http_get_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(); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 618 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 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; |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 690 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 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(); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 847 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 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 | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 851 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 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 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 918 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 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 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 922 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
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 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 966 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Dragan Dosen | 5a60668 | 2019-02-14 12:30:53 +0100 | [diff] [blame] | 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 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 970 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
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; |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1012 | sl = http_get_stline(htx); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 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; |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1044 | sl = http_get_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; |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1077 | sl = http_get_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(); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 1284 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 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 | |
Christopher Faulet | c1f40dd | 2019-05-16 10:07:30 +0200 | [diff] [blame] | 1438 | /* Same than smp_fetch_hdr() but only relies on the sample direction to choose |
| 1439 | * the right channel. So instead of duplicating the code, we just change the |
| 1440 | * keyword and then fallback on smp_fetch_hdr(). |
| 1441 | */ |
| 1442 | static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1443 | { |
| 1444 | kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr"); |
| 1445 | return smp_fetch_hdr(args, smp, kw, private); |
| 1446 | } |
| 1447 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1448 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 1449 | * Accepts exactly 1 argument of type string. |
| 1450 | */ |
| 1451 | static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1452 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1453 | /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */ |
| 1454 | 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] | 1455 | int cnt; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1456 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1457 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1458 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1459 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1460 | struct http_hdr_ctx ctx; |
| 1461 | struct ist name; |
| 1462 | |
| 1463 | if (!htx) |
| 1464 | return 0; |
| 1465 | |
| 1466 | if (args && args->type == ARGT_STR) { |
| 1467 | name.ptr = args->data.str.area; |
| 1468 | name.len = args->data.str.data; |
Olivier Houchard | e2c78cd | 2018-11-21 13:49:48 +0100 | [diff] [blame] | 1469 | } else { |
| 1470 | name.ptr = NULL; |
| 1471 | name.len = 0; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1472 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1473 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1474 | ctx.blk = NULL; |
| 1475 | cnt = 0; |
| 1476 | while (http_find_header(htx, name, &ctx, 0)) |
| 1477 | cnt++; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1478 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1479 | else { |
| 1480 | /* LEGACY version */ |
| 1481 | struct hdr_idx *idx; |
| 1482 | struct hdr_ctx ctx; |
| 1483 | const struct http_msg *msg; |
| 1484 | const char *name = NULL; |
| 1485 | int len = 0; |
| 1486 | |
| 1487 | if (args && args->type == ARGT_STR) { |
| 1488 | name = args->data.str.area; |
| 1489 | len = args->data.str.data; |
| 1490 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1491 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1492 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1493 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1494 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1495 | 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] | 1496 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1497 | ctx.idx = 0; |
| 1498 | cnt = 0; |
| 1499 | while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx)) |
| 1500 | cnt++; |
| 1501 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1502 | |
| 1503 | smp->data.type = SMP_T_SINT; |
| 1504 | smp->data.u.sint = cnt; |
| 1505 | smp->flags = SMP_F_VOL_HDR; |
| 1506 | return 1; |
| 1507 | } |
| 1508 | |
| 1509 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 1510 | * takes a mandatory argument of type string and an optional one of type int |
| 1511 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 1512 | * may or may not be appropriate for everything. |
| 1513 | */ |
| 1514 | static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1515 | { |
| 1516 | int ret = smp_fetch_hdr(args, smp, kw, private); |
| 1517 | |
| 1518 | if (ret > 0) { |
| 1519 | smp->data.type = SMP_T_SINT; |
| 1520 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 1521 | smp->data.u.str.data); |
| 1522 | } |
| 1523 | |
| 1524 | return ret; |
| 1525 | } |
| 1526 | |
| 1527 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 1528 | * and an optional one of type int to designate a specific occurrence. |
| 1529 | * It returns an IPv4 or IPv6 address. |
| 1530 | */ |
| 1531 | static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1532 | { |
| 1533 | int ret; |
| 1534 | |
| 1535 | while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) { |
| 1536 | if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) { |
| 1537 | smp->data.type = SMP_T_IPV4; |
| 1538 | break; |
| 1539 | } else { |
| 1540 | struct buffer *temp = get_trash_chunk(); |
| 1541 | if (smp->data.u.str.data < temp->size - 1) { |
| 1542 | memcpy(temp->area, smp->data.u.str.area, |
| 1543 | smp->data.u.str.data); |
| 1544 | temp->area[smp->data.u.str.data] = '\0'; |
| 1545 | if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) { |
| 1546 | smp->data.type = SMP_T_IPV6; |
| 1547 | break; |
| 1548 | } |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | /* if the header doesn't match an IP address, fetch next one */ |
| 1553 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 1554 | return 0; |
| 1555 | } |
| 1556 | return ret; |
| 1557 | } |
| 1558 | |
| 1559 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 1560 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 1561 | */ |
| 1562 | static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1563 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1564 | struct channel *chn = SMP_REQ_CHN(smp); |
| 1565 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1566 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1567 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1568 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1569 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1570 | struct ist path; |
| 1571 | size_t len; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1572 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1573 | if (!htx) |
| 1574 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1575 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1576 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1577 | path = http_get_path(htx_sl_req_uri(sl)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1578 | if (!path.ptr) |
| 1579 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1580 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1581 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++) |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1582 | ; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1583 | |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1584 | /* OK, we got the '/' ! */ |
| 1585 | smp->data.type = SMP_T_STR; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1586 | smp->data.u.str.area = path.ptr; |
| 1587 | smp->data.u.str.data = len; |
| 1588 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1589 | } |
| 1590 | else { |
| 1591 | struct http_txn *txn; |
| 1592 | char *ptr, *end; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1593 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1594 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1595 | |
| 1596 | txn = smp->strm->txn; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1597 | 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] | 1598 | ptr = http_txn_get_path(txn); |
| 1599 | if (!ptr) |
| 1600 | return 0; |
| 1601 | |
| 1602 | /* OK, we got the '/' ! */ |
| 1603 | smp->data.type = SMP_T_STR; |
| 1604 | smp->data.u.str.area = ptr; |
| 1605 | |
| 1606 | while (ptr < end && *ptr != '?') |
| 1607 | ptr++; |
| 1608 | |
| 1609 | smp->data.u.str.data = ptr - smp->data.u.str.area; |
| 1610 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1611 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1612 | return 1; |
| 1613 | } |
| 1614 | |
| 1615 | /* This produces a concatenation of the first occurrence of the Host header |
| 1616 | * followed by the path component if it begins with a slash ('/'). This means |
| 1617 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 1618 | * If no Host header is found, then the path is returned as-is. The returned |
| 1619 | * value is stored in the trash so it does not need to be marked constant. |
| 1620 | * The returned sample is of type string. |
| 1621 | */ |
| 1622 | static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1623 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1624 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1625 | struct buffer *temp; |
| 1626 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1627 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1628 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1629 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1630 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1631 | struct http_hdr_ctx ctx; |
| 1632 | struct ist path; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1633 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1634 | if (!htx) |
| 1635 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1636 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1637 | ctx.blk = NULL; |
| 1638 | if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len) |
| 1639 | return smp_fetch_path(args, smp, kw, private); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1640 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1641 | /* OK we have the header value in ctx.value */ |
| 1642 | temp = get_trash_chunk(); |
| 1643 | chunk_memcat(temp, ctx.value.ptr, ctx.value.len); |
| 1644 | |
| 1645 | /* now retrieve the path */ |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1646 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1647 | path = http_get_path(htx_sl_req_uri(sl)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1648 | if (path.ptr) { |
| 1649 | size_t len; |
| 1650 | |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1651 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++) |
| 1652 | ; |
| 1653 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1654 | if (len && *(path.ptr) == '/') |
| 1655 | chunk_memcat(temp, path.ptr, len); |
| 1656 | } |
| 1657 | |
| 1658 | smp->data.type = SMP_T_STR; |
| 1659 | smp->data.u.str = *temp; |
| 1660 | } |
| 1661 | else { |
| 1662 | /* LEGACY version */ |
| 1663 | struct http_txn *txn; |
| 1664 | char *ptr, *end, *beg; |
| 1665 | struct hdr_ctx ctx; |
| 1666 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1667 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1668 | |
| 1669 | txn = smp->strm->txn; |
| 1670 | ctx.idx = 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1671 | 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] | 1672 | return smp_fetch_path(args, smp, kw, private); |
| 1673 | |
| 1674 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 1675 | temp = get_trash_chunk(); |
| 1676 | memcpy(temp->area, ctx.line + ctx.val, ctx.vlen); |
| 1677 | smp->data.type = SMP_T_STR; |
| 1678 | smp->data.u.str.area = temp->area; |
| 1679 | smp->data.u.str.data = ctx.vlen; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1680 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1681 | /* now retrieve the path */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1682 | 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] | 1683 | beg = http_txn_get_path(txn); |
| 1684 | if (!beg) |
| 1685 | beg = end; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1686 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1687 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 1688 | |
| 1689 | if (beg < ptr && *beg == '/') { |
| 1690 | memcpy(smp->data.u.str.area + smp->data.u.str.data, beg, |
| 1691 | ptr - beg); |
| 1692 | smp->data.u.str.data += ptr - beg; |
| 1693 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | smp->flags = SMP_F_VOL_1ST; |
| 1697 | return 1; |
| 1698 | } |
| 1699 | |
| 1700 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 1701 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 1702 | * This means that '*' will not be added, resulting in exactly the first Host |
| 1703 | * entry. If no Host header is found, then the path is used. The resulting value |
| 1704 | * is hashed using the path hash followed by a full avalanche hash and provides a |
| 1705 | * 32-bit integer value. This fetch is useful for tracking per-path activity on |
| 1706 | * high-traffic sites without having to store whole paths. |
| 1707 | */ |
| 1708 | static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1709 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1710 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1711 | unsigned int hash = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1712 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1713 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1714 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1715 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1716 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1717 | struct http_hdr_ctx ctx; |
| 1718 | struct ist path; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1719 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1720 | if (!htx) |
| 1721 | return 0; |
| 1722 | |
| 1723 | ctx.blk = NULL; |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1724 | if (http_find_header(htx, ist("Host"), &ctx, 0)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1725 | /* OK we have the header value in ctx.value */ |
| 1726 | while (ctx.value.len--) |
| 1727 | hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash; |
| 1728 | } |
| 1729 | |
| 1730 | /* now retrieve the path */ |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1731 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1732 | path = http_get_path(htx_sl_req_uri(sl)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1733 | if (path.ptr) { |
| 1734 | size_t len; |
| 1735 | |
Dragan Dosen | 8861e1c | 2019-02-12 19:50:31 +0100 | [diff] [blame] | 1736 | for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++) |
| 1737 | ; |
| 1738 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1739 | if (len && *(path.ptr) == '/') { |
| 1740 | while (len--) |
| 1741 | hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash; |
| 1742 | } |
| 1743 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1744 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1745 | else { |
| 1746 | /* LEGACY version */ |
| 1747 | struct http_txn *txn; |
| 1748 | struct hdr_ctx ctx; |
| 1749 | char *ptr, *beg, *end; |
| 1750 | int len; |
| 1751 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1752 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1753 | |
| 1754 | txn = smp->strm->txn; |
| 1755 | ctx.idx = 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1756 | 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] | 1757 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 1758 | ptr = ctx.line + ctx.val; |
| 1759 | len = ctx.vlen; |
| 1760 | while (len--) |
| 1761 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 1762 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1763 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1764 | /* now retrieve the path */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1765 | 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] | 1766 | beg = http_txn_get_path(txn); |
| 1767 | if (!beg) |
| 1768 | beg = end; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1769 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1770 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1771 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1772 | if (beg < ptr && *beg == '/') { |
| 1773 | while (beg < ptr) |
| 1774 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 1775 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1776 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1777 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1778 | hash = full_hash(hash); |
| 1779 | |
| 1780 | smp->data.type = SMP_T_SINT; |
| 1781 | smp->data.u.sint = hash; |
| 1782 | smp->flags = SMP_F_VOL_1ST; |
| 1783 | return 1; |
| 1784 | } |
| 1785 | |
| 1786 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 1787 | * path as returned by smp_fetch_base32(). The idea is to have per-source and |
| 1788 | * per-path counters. The result is a binary block from 8 to 20 bytes depending |
| 1789 | * on the source address length. The path hash is stored before the address so |
| 1790 | * that in environments where IPv6 is insignificant, truncating the output to |
| 1791 | * 8 bytes would still work. |
| 1792 | */ |
| 1793 | static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1794 | { |
| 1795 | struct buffer *temp; |
| 1796 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
| 1797 | |
| 1798 | if (!cli_conn) |
| 1799 | return 0; |
| 1800 | |
| 1801 | if (!smp_fetch_base32(args, smp, kw, private)) |
| 1802 | return 0; |
| 1803 | |
| 1804 | temp = get_trash_chunk(); |
| 1805 | *(unsigned int *) temp->area = htonl(smp->data.u.sint); |
| 1806 | temp->data += sizeof(unsigned int); |
| 1807 | |
| 1808 | switch (cli_conn->addr.from.ss_family) { |
| 1809 | case AF_INET: |
| 1810 | memcpy(temp->area + temp->data, |
| 1811 | &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, |
| 1812 | 4); |
| 1813 | temp->data += 4; |
| 1814 | break; |
| 1815 | case AF_INET6: |
| 1816 | memcpy(temp->area + temp->data, |
| 1817 | &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, |
| 1818 | 16); |
| 1819 | temp->data += 16; |
| 1820 | break; |
| 1821 | default: |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
| 1825 | smp->data.u.str = *temp; |
| 1826 | smp->data.type = SMP_T_BIN; |
| 1827 | return 1; |
| 1828 | } |
| 1829 | |
| 1830 | /* Extracts the query string, which comes after the question mark '?'. If no |
| 1831 | * question mark is found, nothing is returned. Otherwise it returns a sample |
| 1832 | * of type string carrying the whole query string. |
| 1833 | */ |
| 1834 | static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1835 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1836 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1837 | char *ptr, *end; |
| 1838 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1839 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1840 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1841 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1842 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1843 | |
| 1844 | if (!htx) |
| 1845 | return 0; |
| 1846 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1847 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1848 | ptr = HTX_SL_REQ_UPTR(sl); |
| 1849 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1850 | } |
| 1851 | else { |
| 1852 | /* LEGACY version */ |
| 1853 | struct http_txn *txn; |
| 1854 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1855 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1856 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1857 | txn = smp->strm->txn; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1858 | ptr = ci_head(chn) + txn->req.sl.rq.u; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1859 | end = ptr + txn->req.sl.rq.u_l; |
| 1860 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1861 | |
| 1862 | /* look up the '?' */ |
| 1863 | do { |
| 1864 | if (ptr == end) |
| 1865 | return 0; |
| 1866 | } while (*ptr++ != '?'); |
| 1867 | |
| 1868 | smp->data.type = SMP_T_STR; |
| 1869 | smp->data.u.str.area = ptr; |
| 1870 | smp->data.u.str.data = end - ptr; |
| 1871 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 1872 | return 1; |
| 1873 | } |
| 1874 | |
| 1875 | static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1876 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1877 | struct channel *chn = SMP_REQ_CHN(smp); |
| 1878 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1879 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1880 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1881 | struct htx *htx = smp_prefetch_htx(smp, chn, 0); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1882 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1883 | if (!htx) |
| 1884 | return 0; |
| 1885 | } |
| 1886 | else { |
| 1887 | /* LEGACY version */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1888 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1889 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 1890 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 1891 | */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1892 | CHECK_HTTP_MESSAGE_FIRST_PERM(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1893 | } |
| 1894 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1895 | smp->data.u.sint = 1; |
| 1896 | return 1; |
| 1897 | } |
| 1898 | |
| 1899 | /* return a valid test if the current request is the first one on the connection */ |
| 1900 | static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1901 | { |
| 1902 | smp->data.type = SMP_T_BOOL; |
| 1903 | smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST); |
| 1904 | return 1; |
| 1905 | } |
| 1906 | |
| 1907 | /* Accepts exactly 1 argument of type userlist */ |
| 1908 | static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1909 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1910 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1911 | |
| 1912 | if (!args || args->type != ARGT_USR) |
| 1913 | return 0; |
| 1914 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1915 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1916 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1917 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1918 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1919 | if (!htx) |
| 1920 | return 0; |
| 1921 | } |
| 1922 | else { |
| 1923 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1924 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1925 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1926 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1927 | if (!get_http_auth(smp)) |
| 1928 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1929 | smp->data.type = SMP_T_BOOL; |
| 1930 | 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] | 1931 | smp->strm->txn->auth.pass); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1932 | return 1; |
| 1933 | } |
| 1934 | |
| 1935 | /* Accepts exactly 1 argument of type userlist */ |
| 1936 | static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1937 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1938 | struct channel *chn = SMP_REQ_CHN(smp); |
| 1939 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1940 | if (!args || args->type != ARGT_USR) |
| 1941 | return 0; |
| 1942 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 1943 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1944 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 1945 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1946 | |
| 1947 | if (!htx) |
| 1948 | return 0; |
| 1949 | } |
| 1950 | else { |
| 1951 | /* LEGACY version */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 1952 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1953 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1954 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 1955 | if (!get_http_auth(smp)) |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 1956 | return 0; |
| 1957 | |
| 1958 | /* if the user does not belong to the userlist or has a wrong password, |
| 1959 | * report that it unconditionally does not match. Otherwise we return |
| 1960 | * a string containing the username. |
| 1961 | */ |
| 1962 | if (!check_user(args->data.usr, smp->strm->txn->auth.user, |
| 1963 | smp->strm->txn->auth.pass)) |
| 1964 | return 0; |
| 1965 | |
| 1966 | /* pat_match_auth() will need the user list */ |
| 1967 | smp->ctx.a[0] = args->data.usr; |
| 1968 | |
| 1969 | smp->data.type = SMP_T_STR; |
| 1970 | smp->flags = SMP_F_CONST; |
| 1971 | smp->data.u.str.area = smp->strm->txn->auth.user; |
| 1972 | smp->data.u.str.data = strlen(smp->strm->txn->auth.user); |
| 1973 | |
| 1974 | return 1; |
| 1975 | } |
| 1976 | |
| 1977 | /* Fetch a captured HTTP request header. The index is the position of |
| 1978 | * the "capture" option in the configuration file |
| 1979 | */ |
| 1980 | static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1981 | { |
| 1982 | struct proxy *fe = strm_fe(smp->strm); |
| 1983 | int idx; |
| 1984 | |
| 1985 | if (!args || args->type != ARGT_SINT) |
| 1986 | return 0; |
| 1987 | |
| 1988 | idx = args->data.sint; |
| 1989 | |
| 1990 | if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL) |
| 1991 | return 0; |
| 1992 | |
| 1993 | smp->data.type = SMP_T_STR; |
| 1994 | smp->flags |= SMP_F_CONST; |
| 1995 | smp->data.u.str.area = smp->strm->req_cap[idx]; |
| 1996 | smp->data.u.str.data = strlen(smp->strm->req_cap[idx]); |
| 1997 | |
| 1998 | return 1; |
| 1999 | } |
| 2000 | |
| 2001 | /* Fetch a captured HTTP response header. The index is the position of |
| 2002 | * the "capture" option in the configuration file |
| 2003 | */ |
| 2004 | static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2005 | { |
| 2006 | struct proxy *fe = strm_fe(smp->strm); |
| 2007 | int idx; |
| 2008 | |
| 2009 | if (!args || args->type != ARGT_SINT) |
| 2010 | return 0; |
| 2011 | |
| 2012 | idx = args->data.sint; |
| 2013 | |
| 2014 | if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL) |
| 2015 | return 0; |
| 2016 | |
| 2017 | smp->data.type = SMP_T_STR; |
| 2018 | smp->flags |= SMP_F_CONST; |
| 2019 | smp->data.u.str.area = smp->strm->res_cap[idx]; |
| 2020 | smp->data.u.str.data = strlen(smp->strm->res_cap[idx]); |
| 2021 | |
| 2022 | return 1; |
| 2023 | } |
| 2024 | |
| 2025 | /* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */ |
| 2026 | static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2027 | { |
| 2028 | struct buffer *temp; |
| 2029 | struct http_txn *txn = smp->strm->txn; |
| 2030 | char *ptr; |
| 2031 | |
| 2032 | if (!txn || !txn->uri) |
| 2033 | return 0; |
| 2034 | |
| 2035 | ptr = txn->uri; |
| 2036 | |
| 2037 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 2038 | ptr++; |
| 2039 | |
| 2040 | temp = get_trash_chunk(); |
| 2041 | temp->area = txn->uri; |
| 2042 | temp->data = ptr - txn->uri; |
| 2043 | smp->data.u.str = *temp; |
| 2044 | smp->data.type = SMP_T_STR; |
| 2045 | smp->flags = SMP_F_CONST; |
| 2046 | |
| 2047 | return 1; |
| 2048 | |
| 2049 | } |
| 2050 | |
| 2051 | /* Extracts the path in the HTTP request, the txn->uri should be filled before the call */ |
| 2052 | static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2053 | { |
| 2054 | struct http_txn *txn = smp->strm->txn; |
| 2055 | struct ist path; |
| 2056 | const char *ptr; |
| 2057 | |
| 2058 | if (!txn || !txn->uri) |
| 2059 | return 0; |
| 2060 | |
| 2061 | ptr = txn->uri; |
| 2062 | |
| 2063 | while (*ptr != ' ' && *ptr != '\0') /* find first space */ |
| 2064 | ptr++; |
| 2065 | |
| 2066 | if (!*ptr) |
| 2067 | return 0; |
| 2068 | |
Christopher Faulet | 78337bb | 2018-11-15 14:35:18 +0100 | [diff] [blame] | 2069 | /* skip the first space and find space after URI */ |
| 2070 | path = ist2(++ptr, 0); |
| 2071 | while (*ptr != ' ' && *ptr != '\0') |
| 2072 | ptr++; |
| 2073 | path.len = ptr - path.ptr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2074 | |
Christopher Faulet | 78337bb | 2018-11-15 14:35:18 +0100 | [diff] [blame] | 2075 | path = http_get_path(path); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2076 | if (!path.ptr) |
| 2077 | return 0; |
| 2078 | |
| 2079 | smp->data.u.str.area = path.ptr; |
| 2080 | smp->data.u.str.data = path.len; |
| 2081 | smp->data.type = SMP_T_STR; |
| 2082 | smp->flags = SMP_F_CONST; |
| 2083 | |
| 2084 | return 1; |
| 2085 | } |
| 2086 | |
| 2087 | /* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it |
| 2088 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 2089 | */ |
| 2090 | static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2091 | { |
| 2092 | struct http_txn *txn = smp->strm->txn; |
| 2093 | |
| 2094 | if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST) |
| 2095 | return 0; |
| 2096 | |
| 2097 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 2098 | smp->data.u.str.area = "HTTP/1.1"; |
| 2099 | else |
| 2100 | smp->data.u.str.area = "HTTP/1.0"; |
| 2101 | |
| 2102 | smp->data.u.str.data = 8; |
| 2103 | smp->data.type = SMP_T_STR; |
| 2104 | smp->flags = SMP_F_CONST; |
| 2105 | return 1; |
| 2106 | |
| 2107 | } |
| 2108 | |
| 2109 | /* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it |
| 2110 | * as a string (either "HTTP/1.0" or "HTTP/1.1"). |
| 2111 | */ |
| 2112 | static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2113 | { |
| 2114 | struct http_txn *txn = smp->strm->txn; |
| 2115 | |
| 2116 | if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST) |
| 2117 | return 0; |
| 2118 | |
| 2119 | if (txn->rsp.flags & HTTP_MSGF_VER_11) |
| 2120 | smp->data.u.str.area = "HTTP/1.1"; |
| 2121 | else |
| 2122 | smp->data.u.str.area = "HTTP/1.0"; |
| 2123 | |
| 2124 | smp->data.u.str.data = 8; |
| 2125 | smp->data.type = SMP_T_STR; |
| 2126 | smp->flags = SMP_F_CONST; |
| 2127 | return 1; |
| 2128 | |
| 2129 | } |
| 2130 | |
| 2131 | /* Iterate over all cookies present in a message. The context is stored in |
| 2132 | * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the |
| 2133 | * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on |
| 2134 | * the direction, multiple cookies may be parsed on the same line or not. |
| 2135 | * The cookie name is in args and the name length in args->data.str.len. |
| 2136 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 2137 | * that no iterating is desired, then only last value is fetched if any. |
| 2138 | * The returned sample is of type CSTR. Can be used to parse cookies in other |
| 2139 | * files. |
| 2140 | */ |
| 2141 | static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2142 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2143 | /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */ |
| 2144 | 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] | 2145 | int occ = 0; |
| 2146 | int found = 0; |
| 2147 | |
| 2148 | if (!args || args->type != ARGT_STR) |
| 2149 | return 0; |
| 2150 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2151 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2152 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2153 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2154 | struct http_hdr_ctx *ctx = smp->ctx.a[2]; |
| 2155 | struct ist hdr; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2156 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2157 | if (!ctx) { |
| 2158 | /* first call */ |
| 2159 | ctx = &static_http_hdr_ctx; |
| 2160 | ctx->blk = NULL; |
| 2161 | smp->ctx.a[2] = ctx; |
| 2162 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2163 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2164 | if (!htx) |
| 2165 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2166 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2167 | hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie")); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2168 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2169 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 2170 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 2171 | occ = -1; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2172 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2173 | /* OK so basically here, either we want only one value and it's the |
| 2174 | * last one, or we want to iterate over all of them and we fetch the |
| 2175 | * next one. |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2176 | */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2177 | |
| 2178 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
| 2179 | /* search for the header from the beginning, we must first initialize |
| 2180 | * the search parameters. |
| 2181 | */ |
| 2182 | smp->ctx.a[0] = NULL; |
| 2183 | ctx->blk = NULL; |
| 2184 | } |
| 2185 | |
| 2186 | smp->flags |= SMP_F_VOL_HDR; |
| 2187 | while (1) { |
| 2188 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 2189 | if (!smp->ctx.a[0]) { |
| 2190 | if (!http_find_header(htx, hdr, ctx, 0)) |
| 2191 | goto out; |
| 2192 | |
| 2193 | if (ctx->value.len < args->data.str.data + 1) |
| 2194 | continue; |
| 2195 | |
| 2196 | smp->ctx.a[0] = ctx->value.ptr; |
| 2197 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len; |
| 2198 | } |
| 2199 | |
| 2200 | smp->data.type = SMP_T_STR; |
| 2201 | smp->flags |= SMP_F_CONST; |
| 2202 | smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1], |
| 2203 | args->data.str.area, args->data.str.data, |
| 2204 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 2205 | &smp->data.u.str.area, |
| 2206 | &smp->data.u.str.data); |
| 2207 | if (smp->ctx.a[0]) { |
| 2208 | found = 1; |
| 2209 | if (occ >= 0) { |
| 2210 | /* one value was returned into smp->data.u.str.{str,len} */ |
| 2211 | smp->flags |= SMP_F_NOT_LAST; |
| 2212 | return 1; |
| 2213 | } |
| 2214 | } |
| 2215 | /* if we're looking for last occurrence, let's loop */ |
| 2216 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2217 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2218 | else { |
| 2219 | /* LEGACY version */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2220 | struct hdr_idx *idx; |
| 2221 | struct hdr_ctx *ctx = smp->ctx.a[2]; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2222 | const char *hdr_name; |
| 2223 | int hdr_name_len; |
| 2224 | char *sol; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2225 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2226 | if (!ctx) { |
| 2227 | /* first call */ |
| 2228 | ctx = &static_hdr_ctx; |
| 2229 | ctx->idx = 0; |
| 2230 | smp->ctx.a[2] = ctx; |
| 2231 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2232 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2233 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2234 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2235 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2236 | if (!(chn->flags & CF_ISRESP)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2237 | hdr_name = "Cookie"; |
| 2238 | hdr_name_len = 6; |
| 2239 | } else { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2240 | hdr_name = "Set-Cookie"; |
| 2241 | hdr_name_len = 10; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2242 | } |
| 2243 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2244 | if (!occ && !(smp->opt & SMP_OPT_ITERATE)) |
| 2245 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 2246 | occ = -1; |
| 2247 | |
| 2248 | /* OK so basically here, either we want only one value and it's the |
| 2249 | * last one, or we want to iterate over all of them and we fetch the |
| 2250 | * next one. |
| 2251 | */ |
| 2252 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2253 | sol = ci_head(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2254 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
| 2255 | /* search for the header from the beginning, we must first initialize |
| 2256 | * the search parameters. |
| 2257 | */ |
| 2258 | smp->ctx.a[0] = NULL; |
| 2259 | ctx->idx = 0; |
| 2260 | } |
| 2261 | |
| 2262 | smp->flags |= SMP_F_VOL_HDR; |
| 2263 | |
| 2264 | while (1) { |
| 2265 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 2266 | if (!smp->ctx.a[0]) { |
| 2267 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 2268 | goto out; |
| 2269 | |
| 2270 | if (ctx->vlen < args->data.str.data + 1) |
| 2271 | continue; |
| 2272 | |
| 2273 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 2274 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
| 2275 | } |
| 2276 | |
| 2277 | smp->data.type = SMP_T_STR; |
| 2278 | smp->flags |= SMP_F_CONST; |
| 2279 | smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1], |
| 2280 | args->data.str.area, args->data.str.data, |
| 2281 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 2282 | &smp->data.u.str.area, &smp->data.u.str.data); |
| 2283 | if (smp->ctx.a[0]) { |
| 2284 | found = 1; |
| 2285 | if (occ >= 0) { |
| 2286 | /* one value was returned into smp->data.u.str.{str,len} */ |
| 2287 | smp->flags |= SMP_F_NOT_LAST; |
| 2288 | return 1; |
| 2289 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2290 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2291 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2292 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2293 | } |
| 2294 | /* all cookie headers and values were scanned. If we're looking for the |
| 2295 | * last occurrence, we may return it now. |
| 2296 | */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2297 | out: |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2298 | smp->flags &= ~SMP_F_NOT_LAST; |
| 2299 | return found; |
| 2300 | } |
| 2301 | |
Christopher Faulet | c1f40dd | 2019-05-16 10:07:30 +0200 | [diff] [blame] | 2302 | /* Same than smp_fetch_cookie() but only relies on the sample direction to |
| 2303 | * choose the right channel. So instead of duplicating the code, we just change |
| 2304 | * the keyword and then fallback on smp_fetch_cookie(). |
| 2305 | */ |
| 2306 | static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2307 | { |
| 2308 | kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook"); |
| 2309 | return smp_fetch_cookie(args, smp, kw, private); |
| 2310 | } |
| 2311 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2312 | /* Iterate over all cookies present in a request to count how many occurrences |
| 2313 | * match the name in args and args->data.str.len. If <multi> is non-null, then |
| 2314 | * multiple cookies may be parsed on the same line. The returned sample is of |
| 2315 | * type UINT. Accepts exactly 1 argument of type string. |
| 2316 | */ |
| 2317 | static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2318 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2319 | /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */ |
| 2320 | 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] | 2321 | char *val_beg, *val_end; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2322 | int cnt; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2323 | |
| 2324 | if (!args || args->type != ARGT_STR) |
| 2325 | return 0; |
| 2326 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2327 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2328 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2329 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2330 | struct http_hdr_ctx ctx; |
| 2331 | struct ist hdr; |
| 2332 | |
| 2333 | if (!htx) |
| 2334 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2335 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2336 | hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie")); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2337 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2338 | val_end = val_beg = NULL; |
| 2339 | ctx.blk = NULL; |
| 2340 | cnt = 0; |
| 2341 | while (1) { |
| 2342 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 2343 | if (!val_beg) { |
| 2344 | if (!http_find_header(htx, hdr, &ctx, 0)) |
| 2345 | break; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2346 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2347 | if (ctx.value.len < args->data.str.data + 1) |
| 2348 | continue; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2349 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2350 | val_beg = ctx.value.ptr; |
| 2351 | val_end = val_beg + ctx.value.len; |
| 2352 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2353 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2354 | smp->data.type = SMP_T_STR; |
| 2355 | smp->flags |= SMP_F_CONST; |
| 2356 | while ((val_beg = http_extract_cookie_value(val_beg, val_end, |
| 2357 | args->data.str.area, args->data.str.data, |
| 2358 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 2359 | &smp->data.u.str.area, |
| 2360 | &smp->data.u.str.data))) { |
| 2361 | cnt++; |
| 2362 | } |
| 2363 | } |
| 2364 | } |
| 2365 | else { |
| 2366 | /* LEGACY version */ |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2367 | struct hdr_idx *idx; |
| 2368 | struct hdr_ctx ctx; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2369 | const char *hdr_name; |
| 2370 | int hdr_name_len; |
| 2371 | char *sol; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2372 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2373 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2374 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2375 | idx = &smp->strm->txn->hdr_idx; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2376 | if (!(chn->flags & CF_ISRESP)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2377 | hdr_name = "Cookie"; |
| 2378 | hdr_name_len = 6; |
| 2379 | } else { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2380 | hdr_name = "Set-Cookie"; |
| 2381 | hdr_name_len = 10; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2382 | } |
| 2383 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2384 | sol = ci_head(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2385 | val_end = val_beg = NULL; |
| 2386 | ctx.idx = 0; |
| 2387 | cnt = 0; |
| 2388 | |
| 2389 | while (1) { |
| 2390 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 2391 | if (!val_beg) { |
| 2392 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 2393 | break; |
| 2394 | |
| 2395 | if (ctx.vlen < args->data.str.data + 1) |
| 2396 | continue; |
| 2397 | |
| 2398 | val_beg = ctx.line + ctx.val; |
| 2399 | val_end = val_beg + ctx.vlen; |
| 2400 | } |
| 2401 | |
| 2402 | smp->data.type = SMP_T_STR; |
| 2403 | smp->flags |= SMP_F_CONST; |
| 2404 | while ((val_beg = http_extract_cookie_value(val_beg, val_end, |
| 2405 | args->data.str.area, args->data.str.data, |
| 2406 | (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
| 2407 | &smp->data.u.str.area, &smp->data.u.str.data))) { |
| 2408 | cnt++; |
| 2409 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2410 | } |
| 2411 | } |
| 2412 | |
| 2413 | smp->data.type = SMP_T_SINT; |
| 2414 | smp->data.u.sint = cnt; |
| 2415 | smp->flags |= SMP_F_VOL_HDR; |
| 2416 | return 1; |
| 2417 | } |
| 2418 | |
| 2419 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 2420 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 2421 | */ |
| 2422 | static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2423 | { |
| 2424 | int ret = smp_fetch_cookie(args, smp, kw, private); |
| 2425 | |
| 2426 | if (ret > 0) { |
| 2427 | smp->data.type = SMP_T_SINT; |
| 2428 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 2429 | smp->data.u.str.data); |
| 2430 | } |
| 2431 | |
| 2432 | return ret; |
| 2433 | } |
| 2434 | |
| 2435 | /************************************************************************/ |
| 2436 | /* The code below is dedicated to sample fetches */ |
| 2437 | /************************************************************************/ |
| 2438 | |
| 2439 | /* This scans a URL-encoded query string. It takes an optionally wrapping |
| 2440 | * string whose first contigous chunk has its beginning in ctx->a[0] and end |
| 2441 | * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The |
| 2442 | * pointers are updated for next iteration before leaving. |
| 2443 | */ |
| 2444 | 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) |
| 2445 | { |
| 2446 | const char *vstart, *vend; |
| 2447 | struct buffer *temp; |
| 2448 | const char **chunks = (const char **)smp->ctx.a; |
| 2449 | |
| 2450 | if (!http_find_next_url_param(chunks, name, name_len, |
| 2451 | &vstart, &vend, delim)) |
| 2452 | return 0; |
| 2453 | |
| 2454 | /* Create sample. If the value is contiguous, return the pointer as CONST, |
| 2455 | * if the value is wrapped, copy-it in a buffer. |
| 2456 | */ |
| 2457 | smp->data.type = SMP_T_STR; |
| 2458 | if (chunks[2] && |
| 2459 | vstart >= chunks[0] && vstart <= chunks[1] && |
| 2460 | vend >= chunks[2] && vend <= chunks[3]) { |
| 2461 | /* Wrapped case. */ |
| 2462 | temp = get_trash_chunk(); |
| 2463 | memcpy(temp->area, vstart, chunks[1] - vstart); |
| 2464 | memcpy(temp->area + ( chunks[1] - vstart ), chunks[2], |
| 2465 | vend - chunks[2]); |
| 2466 | smp->data.u.str.area = temp->area; |
| 2467 | smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] ); |
| 2468 | } else { |
| 2469 | /* Contiguous case. */ |
| 2470 | smp->data.u.str.area = (char *)vstart; |
| 2471 | smp->data.u.str.data = vend - vstart; |
| 2472 | smp->flags = SMP_F_VOL_1ST | SMP_F_CONST; |
| 2473 | } |
| 2474 | |
| 2475 | /* Update context, check wrapping. */ |
| 2476 | chunks[0] = vend; |
| 2477 | if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) { |
| 2478 | chunks[1] = chunks[3]; |
| 2479 | chunks[2] = NULL; |
| 2480 | } |
| 2481 | |
| 2482 | if (chunks[0] < chunks[1]) |
| 2483 | smp->flags |= SMP_F_NOT_LAST; |
| 2484 | |
| 2485 | return 1; |
| 2486 | } |
| 2487 | |
| 2488 | /* This function iterates over each parameter of the query string. It uses |
| 2489 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the current |
| 2490 | * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL. |
| 2491 | * An optional parameter name is passed in args[0], otherwise any parameter is |
| 2492 | * considered. It supports an optional delimiter argument for the beginning of |
| 2493 | * the string in args[1], which defaults to "?". |
| 2494 | */ |
| 2495 | static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2496 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2497 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2498 | char delim = '?'; |
| 2499 | const char *name; |
| 2500 | int name_len; |
| 2501 | |
| 2502 | if (!args || |
| 2503 | (args[0].type && args[0].type != ARGT_STR) || |
| 2504 | (args[1].type && args[1].type != ARGT_STR)) |
| 2505 | return 0; |
| 2506 | |
| 2507 | name = ""; |
| 2508 | name_len = 0; |
| 2509 | if (args->type == ARGT_STR) { |
| 2510 | name = args->data.str.area; |
| 2511 | name_len = args->data.str.data; |
| 2512 | } |
| 2513 | |
| 2514 | if (args[1].type) |
| 2515 | delim = *args[1].data.str.area; |
| 2516 | |
| 2517 | if (!smp->ctx.a[0]) { // first call, find the query string |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2518 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2519 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2520 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2521 | struct htx_sl *sl; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2522 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2523 | if (!htx) |
| 2524 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2525 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2526 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2527 | 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] | 2528 | if (!smp->ctx.a[0]) |
| 2529 | return 0; |
| 2530 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2531 | 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] | 2532 | } |
| 2533 | else { |
| 2534 | /* LEGACY version */ |
| 2535 | struct http_msg *msg; |
| 2536 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2537 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2538 | |
| 2539 | msg = &smp->strm->txn->req; |
| 2540 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2541 | 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] | 2542 | msg->sl.rq.u_l, delim); |
| 2543 | if (!smp->ctx.a[0]) |
| 2544 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2545 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2546 | 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] | 2547 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2548 | |
| 2549 | /* Assume that the context is filled with NULL pointer |
| 2550 | * before the first call. |
| 2551 | * smp->ctx.a[2] = NULL; |
| 2552 | * smp->ctx.a[3] = NULL; |
| 2553 | */ |
| 2554 | } |
| 2555 | |
| 2556 | return smp_fetch_param(delim, name, name_len, args, smp, kw, private); |
| 2557 | } |
| 2558 | |
| 2559 | /* This function iterates over each parameter of the body. This requires |
| 2560 | * that the body has been waited for using http-buffer-request. It uses |
| 2561 | * ctx->a[0] and ctx->a[1] to store the beginning and end of the first |
| 2562 | * contigous part of the body, and optionally ctx->a[2..3] to reference the |
| 2563 | * optional second part if the body wraps at the end of the buffer. An optional |
| 2564 | * parameter name is passed in args[0], otherwise any parameter is considered. |
| 2565 | */ |
| 2566 | static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2567 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2568 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2569 | const char *name; |
| 2570 | int name_len; |
| 2571 | |
| 2572 | if (!args || (args[0].type && args[0].type != ARGT_STR)) |
| 2573 | return 0; |
| 2574 | |
| 2575 | name = ""; |
| 2576 | name_len = 0; |
| 2577 | if (args[0].type == ARGT_STR) { |
| 2578 | name = args[0].data.str.area; |
| 2579 | name_len = args[0].data.str.data; |
| 2580 | } |
| 2581 | |
| 2582 | if (!smp->ctx.a[0]) { // first call, find the query string |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2583 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2584 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2585 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2586 | struct buffer *temp; |
| 2587 | int32_t pos; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2588 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2589 | if (!htx) |
| 2590 | return 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2591 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2592 | temp = get_trash_chunk(); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 2593 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2594 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 2595 | enum htx_blk_type type = htx_get_blk_type(blk); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2596 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 2597 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2598 | break; |
| 2599 | if (type == HTX_BLK_DATA) { |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 2600 | 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] | 2601 | return 0; |
| 2602 | } |
| 2603 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2604 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2605 | smp->ctx.a[0] = temp->area; |
| 2606 | smp->ctx.a[1] = temp->area + temp->data; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2607 | |
| 2608 | /* Assume that the context is filled with NULL pointer |
| 2609 | * before the first call. |
| 2610 | * smp->ctx.a[2] = NULL; |
| 2611 | * smp->ctx.a[3] = NULL; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2612 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2613 | } |
| 2614 | else { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2615 | /* LEGACY version */ |
| 2616 | struct http_msg *msg; |
| 2617 | unsigned long len; |
| 2618 | unsigned long block1; |
| 2619 | char *body; |
| 2620 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2621 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2622 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2623 | msg = &smp->strm->txn->req; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2624 | len = http_body_bytes(msg); |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2625 | body = c_ptr(chn, -http_data_rewind(msg)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2626 | |
| 2627 | block1 = len; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2628 | if (block1 > b_wrap(&chn->buf) - body) |
| 2629 | block1 = b_wrap(&chn->buf) - body; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2630 | |
| 2631 | if (block1 == len) { |
| 2632 | /* buffer is not wrapped (or empty) */ |
| 2633 | smp->ctx.a[0] = body; |
| 2634 | smp->ctx.a[1] = body + len; |
| 2635 | |
| 2636 | /* Assume that the context is filled with NULL pointer |
| 2637 | * before the first call. |
| 2638 | * smp->ctx.a[2] = NULL; |
| 2639 | * smp->ctx.a[3] = NULL; |
| 2640 | */ |
| 2641 | } |
| 2642 | else { |
| 2643 | /* buffer is wrapped, we need to defragment it */ |
| 2644 | smp->ctx.a[0] = body; |
| 2645 | smp->ctx.a[1] = body + block1; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2646 | smp->ctx.a[2] = b_orig(&chn->buf); |
| 2647 | smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 ); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2648 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2649 | } |
| 2650 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2651 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2652 | return smp_fetch_param('&', name, name_len, args, smp, kw, private); |
| 2653 | } |
| 2654 | |
| 2655 | /* Return the signed integer value for the specified url parameter (see url_param |
| 2656 | * above). |
| 2657 | */ |
| 2658 | static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2659 | { |
| 2660 | int ret = smp_fetch_url_param(args, smp, kw, private); |
| 2661 | |
| 2662 | if (ret > 0) { |
| 2663 | smp->data.type = SMP_T_SINT; |
| 2664 | smp->data.u.sint = strl2ic(smp->data.u.str.area, |
| 2665 | smp->data.u.str.data); |
| 2666 | } |
| 2667 | |
| 2668 | return ret; |
| 2669 | } |
| 2670 | |
| 2671 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 2672 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 2673 | * This means that '*' will not be added, resulting in exactly the first Host |
| 2674 | * entry. If no Host header is found, then the path is used. The resulting value |
| 2675 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 2676 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 2677 | * high-traffic sites without having to store whole paths. |
| 2678 | * this differs from the base32 functions in that it includes the url parameters |
| 2679 | * as well as the path |
| 2680 | */ |
| 2681 | static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2682 | { |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2683 | struct channel *chn = SMP_REQ_CHN(smp); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2684 | unsigned int hash = 0; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2685 | |
Christopher Faulet | 46575cd | 2019-04-17 11:40:30 +0200 | [diff] [blame] | 2686 | if (smp->px->options2 & PR_O2_USE_HTX) { |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2687 | /* HTX version */ |
Christopher Faulet | 5ec8bcb | 2019-04-17 12:04:12 +0200 | [diff] [blame] | 2688 | struct htx *htx = smp_prefetch_htx(smp, chn, 1); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2689 | struct http_hdr_ctx ctx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2690 | struct htx_sl *sl; |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2691 | struct ist path; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2692 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2693 | if (!htx) |
| 2694 | return 0; |
| 2695 | |
| 2696 | ctx.blk = NULL; |
| 2697 | if (http_find_header(htx, ist("Host"), &ctx, 1)) { |
| 2698 | /* OK we have the header value in ctx.value */ |
| 2699 | while (ctx.value.len--) |
| 2700 | hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash; |
| 2701 | } |
| 2702 | |
| 2703 | /* now retrieve the path */ |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2704 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2705 | path = http_get_path(htx_sl_req_uri(sl)); |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2706 | while (path.len > 0 && *(path.ptr) != '?') { |
| 2707 | path.ptr++; |
| 2708 | path.len--; |
| 2709 | } |
| 2710 | if (path.len && *(path.ptr) == '/') { |
| 2711 | while (path.len--) |
| 2712 | hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash; |
| 2713 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2714 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2715 | else { |
| 2716 | /* LEGACY version */ |
| 2717 | struct http_txn *txn; |
| 2718 | struct hdr_ctx ctx; |
| 2719 | char *ptr, *beg, *end; |
| 2720 | int len; |
| 2721 | |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2722 | CHECK_HTTP_MESSAGE_FIRST(chn); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2723 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2724 | txn = smp->strm->txn; |
| 2725 | ctx.idx = 0; |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2726 | 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] | 2727 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 2728 | ptr = ctx.line + ctx.val; |
| 2729 | len = ctx.vlen; |
| 2730 | while (len--) |
| 2731 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 2732 | } |
| 2733 | |
| 2734 | /* now retrieve the path */ |
Christopher Faulet | 89dc499 | 2019-04-17 12:02:59 +0200 | [diff] [blame] | 2735 | 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] | 2736 | beg = http_txn_get_path(txn); |
| 2737 | if (!beg) |
| 2738 | beg = end; |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2739 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2740 | for (ptr = beg; ptr < end ; ptr++); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2741 | |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2742 | if (beg < ptr && *beg == '/') { |
| 2743 | while (beg < ptr) |
| 2744 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 2745 | } |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2746 | } |
Christopher Faulet | 311c7ea | 2018-10-24 21:41:55 +0200 | [diff] [blame] | 2747 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2748 | hash = full_hash(hash); |
| 2749 | |
| 2750 | smp->data.type = SMP_T_SINT; |
| 2751 | smp->data.u.sint = hash; |
| 2752 | smp->flags = SMP_F_VOL_1ST; |
| 2753 | return 1; |
| 2754 | } |
| 2755 | |
| 2756 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 2757 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 2758 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 2759 | * on the source address length. The URL hash is stored before the address so |
| 2760 | * that in environments where IPv6 is insignificant, truncating the output to |
| 2761 | * 8 bytes would still work. |
| 2762 | */ |
| 2763 | static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2764 | { |
| 2765 | struct buffer *temp; |
| 2766 | struct connection *cli_conn = objt_conn(smp->sess->origin); |
| 2767 | |
| 2768 | if (!cli_conn) |
| 2769 | return 0; |
| 2770 | |
| 2771 | if (!smp_fetch_url32(args, smp, kw, private)) |
| 2772 | return 0; |
| 2773 | |
| 2774 | temp = get_trash_chunk(); |
| 2775 | *(unsigned int *) temp->area = htonl(smp->data.u.sint); |
| 2776 | temp->data += sizeof(unsigned int); |
| 2777 | |
| 2778 | switch (cli_conn->addr.from.ss_family) { |
| 2779 | case AF_INET: |
| 2780 | memcpy(temp->area + temp->data, |
| 2781 | &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, |
| 2782 | 4); |
| 2783 | temp->data += 4; |
| 2784 | break; |
| 2785 | case AF_INET6: |
| 2786 | memcpy(temp->area + temp->data, |
| 2787 | &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, |
| 2788 | 16); |
| 2789 | temp->data += 16; |
| 2790 | break; |
| 2791 | default: |
| 2792 | return 0; |
| 2793 | } |
| 2794 | |
| 2795 | smp->data.u.str = *temp; |
| 2796 | smp->data.type = SMP_T_BIN; |
| 2797 | return 1; |
| 2798 | } |
| 2799 | |
| 2800 | /************************************************************************/ |
| 2801 | /* Other utility functions */ |
| 2802 | /************************************************************************/ |
| 2803 | |
| 2804 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 2805 | * keyword. These keywords support an optional positive or negative occurrence |
| 2806 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 2807 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 2808 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 2809 | * error message in case of error, that the caller is responsible for freeing. |
| 2810 | * The initial location must either be freeable or NULL. |
| 2811 | * Note: this function's pointer is checked from Lua. |
| 2812 | */ |
| 2813 | int val_hdr(struct arg *arg, char **err_msg) |
| 2814 | { |
| 2815 | if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) { |
| 2816 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
| 2817 | return 0; |
| 2818 | } |
| 2819 | return 1; |
| 2820 | } |
| 2821 | |
| 2822 | /************************************************************************/ |
| 2823 | /* All supported sample fetch keywords must be declared here. */ |
| 2824 | /************************************************************************/ |
| 2825 | |
| 2826 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 2827 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 2828 | { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2829 | { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2830 | { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2831 | |
| 2832 | /* capture are allocated and are permanent in the stream */ |
| 2833 | { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2834 | |
| 2835 | /* retrieve these captures from the HTTP logs */ |
| 2836 | { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2837 | { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2838 | { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2839 | |
| 2840 | { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP }, |
| 2841 | { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP }, |
| 2842 | |
| 2843 | /* cookie is valid in both directions (eg: for "stick ...") but cook* |
| 2844 | * are only here to match the ACL's name, are request-only and are used |
| 2845 | * for ACL compatibility only. |
| 2846 | */ |
| 2847 | { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
Christopher Faulet | c1f40dd | 2019-05-16 10:07:30 +0200 | [diff] [blame] | 2848 | { "cookie", smp_fetch_chn_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2849 | { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2850 | { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2851 | |
| 2852 | /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are |
| 2853 | * only here to match the ACL's name, are request-only and are used for |
| 2854 | * ACL compatibility only. |
| 2855 | */ |
Christopher Faulet | c1f40dd | 2019-05-16 10:07:30 +0200 | [diff] [blame] | 2856 | { "hdr", smp_fetch_chn_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV }, |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2857 | { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2858 | { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2859 | { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2860 | |
| 2861 | { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV }, |
| 2862 | { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2863 | { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2864 | { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP }, |
| 2865 | { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2866 | { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2867 | |
| 2868 | /* HTTP protocol on the request path */ |
| 2869 | { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2870 | { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP }, |
| 2871 | |
| 2872 | /* HTTP version on the request path */ |
| 2873 | { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2874 | { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2875 | |
| 2876 | { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2877 | { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2878 | { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2879 | { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2880 | |
| 2881 | { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2882 | { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2883 | |
| 2884 | /* HTTP version on the response path */ |
| 2885 | { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2886 | { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2887 | |
| 2888 | /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */ |
| 2889 | { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2890 | { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2891 | { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2892 | |
| 2893 | { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
| 2894 | { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2895 | { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV }, |
| 2896 | { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2897 | { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2898 | { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2899 | { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2900 | |
| 2901 | /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */ |
| 2902 | { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2903 | { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2904 | { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2905 | |
| 2906 | { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2907 | { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2908 | { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2909 | { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2910 | { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 2911 | { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2912 | { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2913 | |
| 2914 | /* scook is valid only on the response and is used for ACL compatibility */ |
| 2915 | { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, |
| 2916 | { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2917 | { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2918 | { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */ |
| 2919 | |
| 2920 | /* shdr is valid only on the response and is used for ACL compatibility */ |
| 2921 | { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV }, |
| 2922 | { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2923 | { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV }, |
| 2924 | { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV }, |
| 2925 | |
| 2926 | { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP }, |
| 2927 | { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV }, |
| 2928 | { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2929 | { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2930 | { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV }, |
| 2931 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV }, |
| 2932 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2933 | { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2934 | { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV }, |
| 2935 | { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV }, |
| 2936 | { /* END */ }, |
| 2937 | }}; |
| 2938 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2939 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 2940 | |
| 2941 | /* |
| 2942 | * Local variables: |
| 2943 | * c-indent-level: 8 |
| 2944 | * c-basic-offset: 8 |
| 2945 | * End: |
| 2946 | */ |