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