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