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