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