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