blob: 04db5622fb9a59ee2a1f51ffe60c4aa9fa1d14e9 [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
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 Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/api.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020020#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020021#include <haproxy/auth.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020022#include <haproxy/base64.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020023#include <haproxy/channel.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020024#include <haproxy/chunk.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020025#include <haproxy/connection.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020026#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020027#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020028#include <haproxy/h1_htx.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020029#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020030#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020031#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020032#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020033#include <haproxy/htx.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020034#include <haproxy/obj_type.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020035#include <haproxy/pool.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020036#include <haproxy/sample.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020038#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020039#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020040
Willy Tarreau79e57332018-10-02 16:01:16 +020041
42/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Fauletef453ed2018-10-24 21:39:27 +020043static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
Richard Russo458eafb2019-07-31 11:45:56 -070044/* this is used to convert raw connection buffers to htx */
45static THREAD_LOCAL struct buffer static_raw_htx_chunk;
46static THREAD_LOCAL char *static_raw_htx_buf;
Christopher Fauletef453ed2018-10-24 21:39:27 +020047
Christopher Faulet89dc4992019-04-17 12:02:59 +020048#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
49#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020050
Richard Russo458eafb2019-07-31 11:45:56 -070051/* This function returns the static htx chunk, where raw connections get
52 * converted to HTX as needed for samplxsing.
53 */
54struct buffer *get_raw_htx_chunk(void)
55{
56 chunk_reset(&static_raw_htx_chunk);
57 return &static_raw_htx_chunk;
58}
59
60static int alloc_raw_htx_chunk_per_thread()
61{
62 static_raw_htx_buf = malloc(global.tune.bufsize);
63 if (!static_raw_htx_buf)
64 return 0;
65 chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize);
66 return 1;
67}
68
69static void free_raw_htx_chunk_per_thread()
70{
71 free(static_raw_htx_buf);
72 static_raw_htx_buf = NULL;
73}
74
75REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
76REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
77
Willy Tarreau79e57332018-10-02 16:01:16 +020078/*
79 * Returns the data from Authorization header. Function may be called more
80 * than once so data is stored in txn->auth_data. When no header is found
81 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
82 * searching again for something we are unable to find anyway. However, if
83 * the result if valid, the cache is not reused because we would risk to
84 * have the credentials overwritten by another stream in parallel.
Willy Tarreaueae83722020-04-29 11:52:51 +020085 * The caller is responsible for passing a sample with a valid stream/txn,
86 * and a valid htx.
Willy Tarreau79e57332018-10-02 16:01:16 +020087 */
88
Christopher Fauletcd761952019-07-15 13:58:29 +020089static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020090{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020091 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020092 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020093 struct http_hdr_ctx ctx = { .blk = NULL };
94 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020095 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020096 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +020097 int len;
98
99#ifdef DEBUG_AUTH
100 printf("Auth for stream %p: %d\n", s, txn->auth.method);
101#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200102 if (txn->auth.method == HTTP_AUTH_WRONG)
103 return 0;
104
105 txn->auth.method = HTTP_AUTH_WRONG;
106
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200107 if (txn->flags & TX_USE_PX_CONN)
108 hdr = ist("Proxy-Authorization");
109 else
110 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +0200111
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200112 ctx.blk = NULL;
113 if (!http_find_header(htx, hdr, &ctx, 0))
114 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200115
Willy Tarreau17254932020-09-02 07:08:47 +0200116 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
117 if (!p || p == ctx.value.ptr) /* if no space was found or if the space is the first character */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200118 return 0;
Willy Tarreau17254932020-09-02 07:08:47 +0200119 len = p - ctx.value.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +0200120
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200121 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
122 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200123
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200124 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200125
126 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
127 struct buffer *http_auth = get_trash_chunk();
128
129 len = base64dec(txn->auth.method_data.area,
130 txn->auth.method_data.data,
131 http_auth->area, global.tune.bufsize - 1);
132
133 if (len < 0)
134 return 0;
135
136
137 http_auth->area[len] = '\0';
138
139 p = strchr(http_auth->area, ':');
140
141 if (!p)
142 return 0;
143
144 txn->auth.user = http_auth->area;
145 *p = '\0';
146 txn->auth.pass = p+1;
147
148 txn->auth.method = HTTP_AUTH_BASIC;
149 return 1;
150 }
151
152 return 0;
153}
154
155/* This function ensures that the prerequisites for an L7 fetch are ready,
156 * which means that a request or response is ready. If some data is missing,
157 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200158 * to extract data from L7. If <vol> is non-null during a prefetch, another
159 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200160 *
161 * The function returns :
162 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
163 * decide whether or not an HTTP message is present ;
164 * NULL if the requested data cannot be fetched or if it is certain that
Willy Tarreaueae83722020-04-29 11:52:51 +0200165 * we'll never have any HTTP message there; this includes null strm or chn.
Willy Tarreaua6d98792020-08-12 14:04:52 +0200166 * NULL if the sample's direction does not match the channel's (i.e. the
167 * function was asked to work on the wrong channel)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200168 * The HTX message if ready
169 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200170struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, struct check *check, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200171{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200172 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200173 struct http_txn *txn = NULL;
174 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200175 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100176 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200177
Willy Tarreaua6d98792020-08-12 14:04:52 +0200178 if (chn &&
179 (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ && (chn->flags & CF_ISRESP)) ||
180 ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES && !(chn->flags & CF_ISRESP))))
181 return 0;
182
Christopher Fauletef453ed2018-10-24 21:39:27 +0200183 /* Note: it is possible that <s> is NULL when called before stream
184 * initialization (eg: tcp-request connection), so this function is the
185 * one responsible for guarding against this case for all HTTP users.
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200186 *
187 * In the health check context, the stream and the channel must be NULL
188 * and <check> must be set. In this case, only the input buffer,
189 * corresponding to the response, is considered. It is the caller
190 * responsibility to provide <check>.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200191 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200192 BUG_ON(check && (s || chn));
193 if (!s || !chn) {
194 if (check) {
195 htx = htxbuf(&check->bi);
196
197 /* Analyse not yet started */
198 if (htx_is_empty(htx) || htx->first == -1)
199 return NULL;
200
201 sl = http_get_stline(htx);
202 if (vol && !sl) {
203 /* The start-line was already forwarded, it is too late to fetch anything */
204 return NULL;
205 }
206 goto end;
207 }
208
Christopher Fauletef453ed2018-10-24 21:39:27 +0200209 return NULL;
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200210 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200211
212 if (!s->txn) {
213 if (unlikely(!http_alloc_txn(s)))
214 return NULL; /* not enough memory */
215 http_init_txn(s);
216 txn = s->txn;
217 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200218 txn = s->txn;
219 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200220
Christopher Fauleteca88542019-04-03 10:12:42 +0200221 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200222 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200223
Christopher Faulet89dc4992019-04-17 12:02:59 +0200224 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
225 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200226
Christopher Faulet89dc4992019-04-17 12:02:59 +0200227 if (msg->msg_state < HTTP_MSG_BODY) {
228 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200229 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200230 /* Parsing is done by the mux, just wait */
231 smp->flags |= SMP_F_MAY_CHANGE;
232 return NULL;
233 }
234 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200235 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200236 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200237 /* The start-line was already forwarded, it is too late to fetch anything */
238 return NULL;
239 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200240 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200241 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200242 struct buffer *buf;
243 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200244 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200245 union h1_sl h1sl;
246 unsigned int flags = HTX_FL_NONE;
247 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200248
Christopher Faulet89dc4992019-04-17 12:02:59 +0200249 /* no HTTP fetch on the response in TCP mode */
250 if (chn->flags & CF_ISRESP)
251 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200252
Christopher Faulet89dc4992019-04-17 12:02:59 +0200253 /* Now we are working on the request only */
254 buf = &chn->buf;
255 if (b_head(buf) + b_data(buf) > b_wrap(buf))
256 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200257
Christopher Faulet89dc4992019-04-17 12:02:59 +0200258 h1m_init_req(&h1m);
259 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
260 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
261 if (ret <= 0) {
262 /* Invalid or too big*/
263 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200264 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100265
Christopher Faulet89dc4992019-04-17 12:02:59 +0200266 /* wait for a full request */
267 smp->flags |= SMP_F_MAY_CHANGE;
268 return NULL;
269 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100270
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500271 /* OK we just got a valid HTTP message. We have to convert it
Christopher Faulet89dc4992019-04-17 12:02:59 +0200272 * into an HTX message.
273 */
274 if (unlikely(h1sl.rq.v.len == 0)) {
275 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
276 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200277 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200278 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200279 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200280
281 /* Set HTX start-line flags */
282 if (h1m.flags & H1_MF_VER_11)
283 flags |= HTX_SL_F_VER_11;
284 if (h1m.flags & H1_MF_XFER_ENC)
285 flags |= HTX_SL_F_XFER_ENC;
286 flags |= HTX_SL_F_XFER_LEN;
287 if (h1m.flags & H1_MF_CHNK)
288 flags |= HTX_SL_F_CHNK;
289 else if (h1m.flags & H1_MF_CLEN)
290 flags |= HTX_SL_F_CLEN;
291
Richard Russo458eafb2019-07-31 11:45:56 -0700292 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200293 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
294 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200295 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200296 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200297 }
298
299 /* OK we just got a valid HTTP message. If not already done by
300 * HTTP analyzers, we have some minor preparation to perform so
301 * that further checks can rely on HTTP tests.
302 */
303 if (sl && msg->msg_state < HTTP_MSG_BODY) {
304 if (!(chn->flags & CF_ISRESP)) {
305 txn->meth = sl->info.req.meth;
306 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
307 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200308 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200309 else
310 txn->status = sl->info.res.status;
311 if (sl->flags & HTX_SL_F_VER_11)
312 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200313 }
314
315 /* everything's OK */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200316 end:
Christopher Fauletef453ed2018-10-24 21:39:27 +0200317 return htx;
318}
319
Willy Tarreau79e57332018-10-02 16:01:16 +0200320/* This function fetches the method of current HTTP request and stores
321 * it in the global pattern struct as a chunk. There are two possibilities :
322 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
323 * in <len> and <ptr> is NULL ;
324 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
325 * <len> to its length.
326 * This is intended to be used with pat_match_meth() only.
327 */
328static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
329{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200330 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200331 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200332 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200333
Willy Tarreaua6d98792020-08-12 14:04:52 +0200334 txn = smp->strm->txn;
335 if (!txn)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200336 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200337
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200338 meth = txn->meth;
339 smp->data.type = SMP_T_METH;
340 smp->data.u.meth.meth = meth;
341 if (meth == HTTP_METH_OTHER) {
Willy Tarreaua6d98792020-08-12 14:04:52 +0200342 struct htx *htx;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200343 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200344
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200345 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
346 /* ensure the indexes are not affected */
347 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200348 }
Willy Tarreaua6d98792020-08-12 14:04:52 +0200349
Christopher Faulet1edd9272021-04-15 09:28:02 +0200350 htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreaua6d98792020-08-12 14:04:52 +0200351 if (!htx)
352 return 0;
353
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200354 sl = http_get_stline(htx);
355 smp->flags |= SMP_F_CONST;
356 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
357 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200358 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200359 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200360 return 1;
361}
362
363static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
364{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200365 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200366 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200367 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200368 char *ptr;
369 int len;
370
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200371 if (!htx)
372 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200373
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200374 sl = http_get_stline(htx);
375 len = HTX_SL_REQ_VLEN(sl);
376 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200377
378 while ((len-- > 0) && (*ptr++ != '/'));
379 if (len <= 0)
380 return 0;
381
382 smp->data.type = SMP_T_STR;
383 smp->data.u.str.area = ptr;
384 smp->data.u.str.data = len;
385
386 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
387 return 1;
388}
389
390static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
391{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200392 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200393 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200394 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200395 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200396 char *ptr;
397 int len;
398
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200399 if (!htx)
400 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200401
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200402 sl = http_get_stline(htx);
403 len = HTX_SL_RES_VLEN(sl);
404 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200405
406 while ((len-- > 0) && (*ptr++ != '/'));
407 if (len <= 0)
408 return 0;
409
410 smp->data.type = SMP_T_STR;
411 smp->data.u.str.area = ptr;
412 smp->data.u.str.data = len;
413
414 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
415 return 1;
416}
417
418/* 3. Check on Status Code. We manipulate integers here. */
419static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
420{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200421 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200422 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200423 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200424 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200425 char *ptr;
426 int len;
427
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200428 if (!htx)
429 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200430
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200431 sl = http_get_stline(htx);
432 len = HTX_SL_RES_CLEN(sl);
433 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200434
435 smp->data.type = SMP_T_SINT;
436 smp->data.u.sint = __strl2ui(ptr, len);
437 smp->flags = SMP_F_VOL_1ST;
438 return 1;
439}
440
441static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
442{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100443 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100444
Willy Tarreau79e57332018-10-02 16:01:16 +0200445 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
446 return 0;
447
Willy Tarreaua1062a42020-04-29 11:50:38 +0200448 if (!smp->strm)
449 return 0;
450
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100451 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
452 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100453 return 0;
454
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100455 smp->data.u.str.area = smp->strm->unique_id.ptr;
456 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100457 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200458 smp->flags = SMP_F_CONST;
459 return 1;
460}
461
462/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800463 * empty line which separes headers from the body. This is useful
464 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200465 */
466static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
467{
Christopher Faulete596d182020-05-05 17:46:34 +0200468 /* possible keywords: req.hdrs, res.hdrs */
469 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200470 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200471 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200472 struct buffer *temp;
473 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200474
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200475 if (!htx)
476 return 0;
477 temp = get_trash_chunk();
478 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
479 struct htx_blk *blk = htx_get_blk(htx, pos);
480 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200481
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200482 if (type == HTX_BLK_HDR) {
483 struct ist n = htx_get_blk_name(htx, blk);
484 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200485
Christopher Faulet53a899b2019-10-08 16:38:42 +0200486 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200487 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200488 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200489 else if (type == HTX_BLK_EOH) {
490 if (!chunk_memcat(temp, "\r\n", 2))
491 return 0;
492 break;
493 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200494 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200495 smp->data.type = SMP_T_STR;
496 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200497 return 1;
498}
499
500/* Returns the header request in a length/value encoded format.
501 * This is useful for exchanges with the SPOE.
502 *
503 * A "length value" is a multibyte code encoding numbers. It uses the
504 * SPOE format. The encoding is the following:
505 *
506 * Each couple "header name" / "header value" is composed
507 * like this:
508 * "length value" "header name bytes"
509 * "length value" "header value bytes"
510 * When the last header is reached, the header name and the header
511 * value are empty. Their length are 0
512 */
513static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
514{
Christopher Faulete596d182020-05-05 17:46:34 +0200515 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
516 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200517 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200518 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200519 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200520 char *p, *end;
521 int32_t pos;
522 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200523
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200524 if (!htx)
525 return 0;
526 temp = get_trash_chunk();
527 p = temp->area;
528 end = temp->area + temp->size;
529 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
530 struct htx_blk *blk = htx_get_blk(htx, pos);
531 enum htx_blk_type type = htx_get_blk_type(blk);
532 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200533
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200534 if (type == HTX_BLK_HDR) {
535 n = htx_get_blk_name(htx,blk);
536 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200537
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200538 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200539 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200540 if (ret == -1)
541 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200542 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200543 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200544 memcpy(p, n.ptr, n.len);
545 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200546
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200547 /* encode the header value. */
548 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200549 if (ret == -1)
550 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200551 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200552 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200553 memcpy(p, v.ptr, v.len);
554 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200555
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200556 }
557 else if (type == HTX_BLK_EOH) {
558 /* encode the end of the header list with empty
559 * header name and header value.
560 */
561 ret = encode_varint(0, &p, end);
562 if (ret == -1)
563 return 0;
564 ret = encode_varint(0, &p, end);
565 if (ret == -1)
566 return 0;
567 break;
568 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200569 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200570
571 /* Initialise sample data which will be filled. */
572 smp->data.type = SMP_T_BIN;
573 smp->data.u.str.area = temp->area;
574 smp->data.u.str.data = p - temp->area;
575 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200576 return 1;
577}
578
579/* returns the longest available part of the body. This requires that the body
580 * has been waited for using http-buffer-request.
581 */
582static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
583{
Christopher Faulete596d182020-05-05 17:46:34 +0200584 /* possible keywords: req.body, res.body */
585 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200586 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200587 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200588 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200589 int32_t pos;
Christopher Faulet4b490b82020-11-25 08:08:08 +0100590 int finished = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200591
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200592 if (!htx)
593 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200594
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200595 temp = get_trash_chunk();
596 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
597 struct htx_blk *blk = htx_get_blk(htx, pos);
598 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200599
Christopher Faulet4b490b82020-11-25 08:08:08 +0100600 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) {
601 finished = 1;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200602 break;
Christopher Faulet4b490b82020-11-25 08:08:08 +0100603 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200604 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200605 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200606 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200607 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200608 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200609
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200610 smp->data.type = SMP_T_BIN;
611 smp->data.u.str = *temp;
612 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200613
Christopher Faulet4b490b82020-11-25 08:08:08 +0100614 if (!finished && (check || (chn && !channel_full(chn, global.tune.maxrewrite) &&
615 !(chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)))))
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200616 smp->flags |= SMP_F_MAY_CHANGE;
617
Willy Tarreau79e57332018-10-02 16:01:16 +0200618 return 1;
619}
620
621
622/* returns the available length of the body. This requires that the body
623 * has been waited for using http-buffer-request.
624 */
625static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
626{
Christopher Faulete596d182020-05-05 17:46:34 +0200627 /* possible keywords: req.body_len, res.body_len */
628 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200629 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200630 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200631 int32_t pos;
632 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100633
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200634 if (!htx)
635 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100636
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200637 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
638 struct htx_blk *blk = htx_get_blk(htx, pos);
639 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100640
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200641 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
642 break;
643 if (type == HTX_BLK_DATA)
644 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200645 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200646
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200647 smp->data.type = SMP_T_SINT;
648 smp->data.u.sint = len;
649 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200650 return 1;
651}
652
653
654/* returns the advertised length of the body, or the advertised size of the
655 * chunks available in the buffer. This requires that the body has been waited
656 * for using http-buffer-request.
657 */
658static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
659{
Christopher Faulete596d182020-05-05 17:46:34 +0200660 /* possible keywords: req.body_size, res.body_size */
661 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200662 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200663 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200664 int32_t pos;
665 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200666
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200667 if (!htx)
668 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100669
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200670 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
671 struct htx_blk *blk = htx_get_blk(htx, pos);
672 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100673
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200674 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
675 break;
676 if (type == HTX_BLK_DATA)
677 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200678 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200679 if (htx->extra != ULLONG_MAX)
680 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200681
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200682 smp->data.type = SMP_T_SINT;
683 smp->data.u.sint = len;
684 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200685 return 1;
686}
687
688
689/* 4. Check on URL/URI. A pointer to the URI is stored. */
690static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
691{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200692 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200693 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200694 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200695
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200696 if (!htx)
697 return 0;
698 sl = http_get_stline(htx);
699 smp->data.type = SMP_T_STR;
700 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
701 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
702 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200703 return 1;
704}
705
706static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
707{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200708 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200709 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200710 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200711 struct sockaddr_storage addr;
712
Amaury Denoyelle6e5795d2021-05-10 11:23:34 +0200713 memset(&addr, 0, sizeof(addr));
714
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200715 if (!htx)
716 return 0;
717 sl = http_get_stline(htx);
Amaury Denoyelle6e5795d2021-05-10 11:23:34 +0200718 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
719 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200720
Willy Tarreau79e57332018-10-02 16:01:16 +0200721 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
722 return 0;
723
724 smp->data.type = SMP_T_IPV4;
725 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
726 smp->flags = 0;
727 return 1;
728}
729
730static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
731{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200732 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200733 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200734 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200735 struct sockaddr_storage addr;
736
Amaury Denoyelle6e5795d2021-05-10 11:23:34 +0200737 memset(&addr, 0, sizeof(addr));
738
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200739 if (!htx)
740 return 0;
741 sl = http_get_stline(htx);
Amaury Denoyelle6e5795d2021-05-10 11:23:34 +0200742 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
743 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200744
Willy Tarreau79e57332018-10-02 16:01:16 +0200745 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
746 return 0;
747
748 smp->data.type = SMP_T_SINT;
749 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
750 smp->flags = 0;
751 return 1;
752}
753
754/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
755 * Accepts an optional argument of type string containing the header field name,
756 * and an optional argument of type signed or unsigned integer to request an
757 * explicit occurrence of the header. Note that in the event of a missing name,
758 * headers are considered from the first one. It does not stop on commas and
759 * returns full lines instead (useful for User-Agent or Date for example).
760 */
761static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
762{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200763 /* possible keywords: req.fhdr, res.fhdr */
764 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200765 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200766 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200767 struct http_hdr_ctx *ctx = smp->ctx.a[0];
768 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200769 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200770
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200771 if (!ctx) {
772 /* first call */
773 ctx = &static_http_hdr_ctx;
774 ctx->blk = NULL;
775 smp->ctx.a[0] = ctx;
776 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200777
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200778 if (args) {
779 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200780 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200781 name.ptr = args[0].data.str.area;
782 name.len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +0200783
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200784 if (args[1].type == ARGT_SINT)
785 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200786 }
787
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200788 if (!htx)
789 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200790
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200791 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
792 /* search for header from the beginning */
793 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200794
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200795 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
796 /* no explicit occurrence and single fetch => last header by default */
797 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200798
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200799 if (!occ)
800 /* prepare to report multiple occurrences for ACL fetches */
801 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200802
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200803 smp->data.type = SMP_T_STR;
804 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
805 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
806 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200807 smp->flags &= ~SMP_F_NOT_LAST;
808 return 0;
809}
810
811/* 6. Check on HTTP header count. The number of occurrences is returned.
812 * Accepts exactly 1 argument of type string. It does not stop on commas and
813 * returns full lines instead (useful for User-Agent or Date for example).
814 */
815static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
816{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200817 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
818 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200819 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200820 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200821 struct http_hdr_ctx ctx;
822 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200823 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200824
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200825 if (!htx)
826 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200827
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200828 if (args && args->type == ARGT_STR) {
829 name.ptr = args->data.str.area;
830 name.len = args->data.str.data;
831 } else {
832 name.ptr = NULL;
833 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200834 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200835
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200836 ctx.blk = NULL;
837 cnt = 0;
838 while (http_find_header(htx, name, &ctx, 1))
839 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200840 smp->data.type = SMP_T_SINT;
841 smp->data.u.sint = cnt;
842 smp->flags = SMP_F_VOL_HDR;
843 return 1;
844}
845
846static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
847{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200848 /* possible keywords: req.hdr_names, res.hdr_names */
849 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200850 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200851 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200852 struct buffer *temp;
853 char del = ',';
854
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200855 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200856
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200857 if (!htx)
858 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200859
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200860 if (args && args->type == ARGT_STR)
861 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200862
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200863 temp = get_trash_chunk();
864 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
865 struct htx_blk *blk = htx_get_blk(htx, pos);
866 enum htx_blk_type type = htx_get_blk_type(blk);
867 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200868
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200869 if (type == HTX_BLK_EOH)
870 break;
871 if (type != HTX_BLK_HDR)
872 continue;
873 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200874
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200875 if (temp->data)
876 temp->area[temp->data++] = del;
877 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200878 }
879
880 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200881 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200882 smp->flags = SMP_F_VOL_HDR;
883 return 1;
884}
885
886/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
887 * Accepts an optional argument of type string containing the header field name,
888 * and an optional argument of type signed or unsigned integer to request an
889 * explicit occurrence of the header. Note that in the event of a missing name,
890 * headers are considered from the first one.
891 */
892static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
893{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200894 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
895 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200896 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200897 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200898 struct http_hdr_ctx *ctx = smp->ctx.a[0];
899 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200900 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200901
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200902 if (!ctx) {
903 /* first call */
904 ctx = &static_http_hdr_ctx;
905 ctx->blk = NULL;
906 smp->ctx.a[0] = ctx;
907 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200908
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200909 if (args) {
910 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200911 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200912 name.ptr = args[0].data.str.area;
913 name.len = args[0].data.str.data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200914
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200915 if (args[1].type == ARGT_SINT)
916 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200917 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200918
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200919 if (!htx)
920 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200921
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200922 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
923 /* search for header from the beginning */
924 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200925
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200926 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
927 /* no explicit occurrence and single fetch => last header by default */
928 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200929
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200930 if (!occ)
931 /* prepare to report multiple occurrences for ACL fetches */
932 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200933
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200934 smp->data.type = SMP_T_STR;
935 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
936 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
937 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200938
939 smp->flags &= ~SMP_F_NOT_LAST;
940 return 0;
941}
942
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200943/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
944 * the right channel. So instead of duplicating the code, we just change the
945 * keyword and then fallback on smp_fetch_hdr().
946 */
947static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
948{
949 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
950 return smp_fetch_hdr(args, smp, kw, private);
951}
952
Willy Tarreau79e57332018-10-02 16:01:16 +0200953/* 6. Check on HTTP header count. The number of occurrences is returned.
954 * Accepts exactly 1 argument of type string.
955 */
956static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
957{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200958 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
959 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200960 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200961 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200962 struct http_hdr_ctx ctx;
963 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200964 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200965
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200966 if (!htx)
967 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200968
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200969 if (args && args->type == ARGT_STR) {
970 name.ptr = args->data.str.area;
971 name.len = args->data.str.data;
972 } else {
973 name.ptr = NULL;
974 name.len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200975 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200976
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200977 ctx.blk = NULL;
978 cnt = 0;
979 while (http_find_header(htx, name, &ctx, 0))
980 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200981
982 smp->data.type = SMP_T_SINT;
983 smp->data.u.sint = cnt;
984 smp->flags = SMP_F_VOL_HDR;
985 return 1;
986}
987
988/* Fetch an HTTP header's integer value. The integer value is returned. It
989 * takes a mandatory argument of type string and an optional one of type int
990 * to designate a specific occurrence. It returns an unsigned integer, which
991 * may or may not be appropriate for everything.
992 */
993static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
994{
995 int ret = smp_fetch_hdr(args, smp, kw, private);
996
997 if (ret > 0) {
998 smp->data.type = SMP_T_SINT;
999 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1000 smp->data.u.str.data);
1001 }
1002
1003 return ret;
1004}
1005
1006/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1007 * and an optional one of type int to designate a specific occurrence.
Willy Tarreau7e2e49e2021-03-25 14:12:29 +01001008 * It returns an IPv4 or IPv6 address. Addresses surrounded by invalid chars
1009 * are rejected. However IPv4 addresses may be followed with a colon and a
1010 * valid port number.
Willy Tarreau79e57332018-10-02 16:01:16 +02001011 */
1012static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1013{
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001014 struct buffer *temp = get_trash_chunk();
Willy Tarreau7e2e49e2021-03-25 14:12:29 +01001015 int ret, len;
1016 int port;
Willy Tarreau79e57332018-10-02 16:01:16 +02001017
1018 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001019 if (smp->data.u.str.data < temp->size - 1) {
1020 memcpy(temp->area, smp->data.u.str.area,
1021 smp->data.u.str.data);
1022 temp->area[smp->data.u.str.data] = '\0';
Willy Tarreau7e2e49e2021-03-25 14:12:29 +01001023 len = url2ipv4((char *) temp->area, &smp->data.u.ipv4);
Willy Tarreau48b6abf2021-03-31 11:41:36 +02001024 if (len > 0 && len == smp->data.u.str.data) {
Willy Tarreau7e2e49e2021-03-25 14:12:29 +01001025 /* plain IPv4 address */
1026 smp->data.type = SMP_T_IPV4;
1027 break;
1028 } else if (len > 0 && temp->area[len] == ':' &&
1029 strl2irc(temp->area + len + 1, smp->data.u.str.data - len - 1, &port) == 0 &&
1030 port >= 0 && port <= 65535) {
1031 /* IPv4 address suffixed with ':' followed by a valid port number */
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001032 smp->data.type = SMP_T_IPV4;
1033 break;
1034 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1035 smp->data.type = SMP_T_IPV6;
1036 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001037 }
1038 }
1039
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001040 /* if the header doesn't match an IP address, fetch next one */
1041 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001042 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001043 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001044 return ret;
1045}
Willy Tarreau79e57332018-10-02 16:01:16 +02001046
Christopher Faulete720c322020-09-02 17:25:18 +02001047/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the
1048 * first '/' after the possible hostname. It ends before the possible '?' except
1049 * for 'pathq' keyword.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001050 */
1051static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1052{
1053 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001054 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001055 struct htx_sl *sl;
1056 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001057
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001058 if (!htx)
1059 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001060
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001061 sl = http_get_stline(htx);
Christopher Faulete720c322020-09-02 17:25:18 +02001062 path = http_get_path(htx_sl_req_uri(sl));
1063
1064 if (kw[0] == 'p' && kw[4] == 'q') // pathq
1065 path = http_get_path(htx_sl_req_uri(sl));
1066 else
1067 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
1068
Tim Duesterhused526372020-03-05 17:56:33 +01001069 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001070 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001071
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001072 /* OK, we got the '/' ! */
1073 smp->data.type = SMP_T_STR;
1074 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001075 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001076 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001077 return 1;
1078}
1079
1080/* This produces a concatenation of the first occurrence of the Host header
1081 * followed by the path component if it begins with a slash ('/'). This means
1082 * that '*' will not be added, resulting in exactly the first Host entry.
1083 * If no Host header is found, then the path is returned as-is. The returned
1084 * value is stored in the trash so it does not need to be marked constant.
1085 * The returned sample is of type string.
1086 */
1087static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1088{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001089 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001090 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001091 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001092 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001093 struct http_hdr_ctx ctx;
1094 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001095
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001096 if (!htx)
1097 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001098
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001099 ctx.blk = NULL;
1100 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1101 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001102
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001103 /* OK we have the header value in ctx.value */
1104 temp = get_trash_chunk();
1105 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001106
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001107 /* now retrieve the path */
1108 sl = http_get_stline(htx);
1109 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001110 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001111 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001112
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001113 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1114 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001115
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001116 if (len && *(path.ptr) == '/')
1117 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001118 }
1119
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001120 smp->data.type = SMP_T_STR;
1121 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001122 smp->flags = SMP_F_VOL_1ST;
1123 return 1;
1124}
1125
1126/* This produces a 32-bit hash of the concatenation of the first occurrence of
1127 * the Host header followed by the path component if it begins with a slash ('/').
1128 * This means that '*' will not be added, resulting in exactly the first Host
1129 * entry. If no Host header is found, then the path is used. The resulting value
1130 * is hashed using the path hash followed by a full avalanche hash and provides a
1131 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1132 * high-traffic sites without having to store whole paths.
1133 */
1134static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1135{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001136 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001137 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001138 struct htx_sl *sl;
1139 struct http_hdr_ctx ctx;
1140 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001141 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001142
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001143 if (!htx)
1144 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001145
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001146 ctx.blk = NULL;
1147 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1148 /* OK we have the header value in ctx.value */
1149 while (ctx.value.len--)
1150 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001151 }
1152
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001153 /* now retrieve the path */
1154 sl = http_get_stline(htx);
1155 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001156 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001157 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001158
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001159 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1160 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001161
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001162 if (len && *(path.ptr) == '/') {
1163 while (len--)
1164 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001165 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001166 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001167
Willy Tarreau79e57332018-10-02 16:01:16 +02001168 hash = full_hash(hash);
1169
1170 smp->data.type = SMP_T_SINT;
1171 smp->data.u.sint = hash;
1172 smp->flags = SMP_F_VOL_1ST;
1173 return 1;
1174}
1175
1176/* This concatenates the source address with the 32-bit hash of the Host and
1177 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1178 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1179 * on the source address length. The path hash is stored before the address so
1180 * that in environments where IPv6 is insignificant, truncating the output to
1181 * 8 bytes would still work.
1182 */
1183static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1184{
1185 struct buffer *temp;
1186 struct connection *cli_conn = objt_conn(smp->sess->origin);
1187
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001188 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001189 return 0;
1190
1191 if (!smp_fetch_base32(args, smp, kw, private))
1192 return 0;
1193
1194 temp = get_trash_chunk();
1195 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1196 temp->data += sizeof(unsigned int);
1197
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001198 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001199 case AF_INET:
1200 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001201 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001202 4);
1203 temp->data += 4;
1204 break;
1205 case AF_INET6:
1206 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001207 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001208 16);
1209 temp->data += 16;
1210 break;
1211 default:
1212 return 0;
1213 }
1214
1215 smp->data.u.str = *temp;
1216 smp->data.type = SMP_T_BIN;
1217 return 1;
1218}
1219
1220/* Extracts the query string, which comes after the question mark '?'. If no
1221 * question mark is found, nothing is returned. Otherwise it returns a sample
1222 * of type string carrying the whole query string.
1223 */
1224static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1225{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001226 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001227 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001228 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001229 char *ptr, *end;
1230
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001231 if (!htx)
1232 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001233
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001234 sl = http_get_stline(htx);
1235 ptr = HTX_SL_REQ_UPTR(sl);
1236 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001237
1238 /* look up the '?' */
1239 do {
1240 if (ptr == end)
1241 return 0;
1242 } while (*ptr++ != '?');
1243
1244 smp->data.type = SMP_T_STR;
1245 smp->data.u.str.area = ptr;
1246 smp->data.u.str.data = end - ptr;
1247 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1248 return 1;
1249}
1250
1251static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1252{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001253 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001254 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001255
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001256 if (!htx)
1257 return 0;
1258 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001259 smp->data.u.sint = 1;
1260 return 1;
1261}
1262
1263/* return a valid test if the current request is the first one on the connection */
1264static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1265{
Willy Tarreau79512b62020-04-29 11:52:13 +02001266 if (!smp->strm)
1267 return 0;
1268
Willy Tarreau79e57332018-10-02 16:01:16 +02001269 smp->data.type = SMP_T_BOOL;
1270 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1271 return 1;
1272}
1273
Christopher Fauleta4063562019-08-02 11:51:37 +02001274/* Fetch the authentication method if there is an Authorization header. It
1275 * relies on get_http_auth()
1276 */
1277static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1278{
1279 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001280 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001281 struct http_txn *txn;
1282
1283 if (!htx)
1284 return 0;
1285
1286 txn = smp->strm->txn;
1287 if (!get_http_auth(smp, htx))
1288 return 0;
1289
1290 switch (txn->auth.method) {
1291 case HTTP_AUTH_BASIC:
1292 smp->data.u.str.area = "Basic";
1293 smp->data.u.str.data = 5;
1294 break;
1295 case HTTP_AUTH_DIGEST:
1296 /* Unexpected because not supported */
1297 smp->data.u.str.area = "Digest";
1298 smp->data.u.str.data = 6;
1299 break;
1300 default:
1301 return 0;
1302 }
1303
1304 smp->data.type = SMP_T_STR;
1305 smp->flags = SMP_F_CONST;
1306 return 1;
1307}
1308
1309/* Fetch the user supplied if there is an Authorization header. It relies on
1310 * get_http_auth()
1311 */
1312static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1313{
1314 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001315 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001316 struct http_txn *txn;
1317
1318 if (!htx)
1319 return 0;
1320
1321 txn = smp->strm->txn;
1322 if (!get_http_auth(smp, htx))
1323 return 0;
1324
1325 smp->data.type = SMP_T_STR;
1326 smp->data.u.str.area = txn->auth.user;
1327 smp->data.u.str.data = strlen(txn->auth.user);
1328 smp->flags = SMP_F_CONST;
1329 return 1;
1330}
1331
1332/* Fetch the password supplied if there is an Authorization header. It relies on
1333 * get_http_auth()
1334 */
1335static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1336{
1337 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001338 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001339 struct http_txn *txn;
1340
1341 if (!htx)
1342 return 0;
1343
1344 txn = smp->strm->txn;
1345 if (!get_http_auth(smp, htx))
1346 return 0;
1347
1348 smp->data.type = SMP_T_STR;
1349 smp->data.u.str.area = txn->auth.pass;
1350 smp->data.u.str.data = strlen(txn->auth.pass);
1351 smp->flags = SMP_F_CONST;
1352 return 1;
1353}
1354
Willy Tarreau79e57332018-10-02 16:01:16 +02001355/* Accepts exactly 1 argument of type userlist */
1356static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1357{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001358 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001359 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001360
1361 if (!args || args->type != ARGT_USR)
1362 return 0;
1363
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001364 if (!htx)
1365 return 0;
1366 if (!get_http_auth(smp, htx))
1367 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001368
1369 smp->data.type = SMP_T_BOOL;
1370 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001371 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001372 return 1;
1373}
1374
1375/* Accepts exactly 1 argument of type userlist */
1376static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1377{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001378 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001379 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001380
Willy Tarreau79e57332018-10-02 16:01:16 +02001381 if (!args || args->type != ARGT_USR)
1382 return 0;
1383
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001384 if (!htx)
1385 return 0;
1386 if (!get_http_auth(smp, htx))
1387 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001388
Willy Tarreau79e57332018-10-02 16:01:16 +02001389 /* if the user does not belong to the userlist or has a wrong password,
1390 * report that it unconditionally does not match. Otherwise we return
1391 * a string containing the username.
1392 */
1393 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1394 smp->strm->txn->auth.pass))
1395 return 0;
1396
1397 /* pat_match_auth() will need the user list */
1398 smp->ctx.a[0] = args->data.usr;
1399
1400 smp->data.type = SMP_T_STR;
1401 smp->flags = SMP_F_CONST;
1402 smp->data.u.str.area = smp->strm->txn->auth.user;
1403 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1404
1405 return 1;
1406}
1407
1408/* Fetch a captured HTTP request header. The index is the position of
1409 * the "capture" option in the configuration file
1410 */
1411static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1412{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001413 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001414 int idx;
1415
1416 if (!args || args->type != ARGT_SINT)
1417 return 0;
1418
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001419 if (!smp->strm)
1420 return 0;
1421
1422 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001423 idx = args->data.sint;
1424
1425 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1426 return 0;
1427
1428 smp->data.type = SMP_T_STR;
1429 smp->flags |= SMP_F_CONST;
1430 smp->data.u.str.area = smp->strm->req_cap[idx];
1431 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1432
1433 return 1;
1434}
1435
1436/* Fetch a captured HTTP response header. The index is the position of
1437 * the "capture" option in the configuration file
1438 */
1439static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1440{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001441 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001442 int idx;
1443
1444 if (!args || args->type != ARGT_SINT)
1445 return 0;
1446
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001447 if (!smp->strm)
1448 return 0;
1449
1450 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001451 idx = args->data.sint;
1452
1453 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1454 return 0;
1455
1456 smp->data.type = SMP_T_STR;
1457 smp->flags |= SMP_F_CONST;
1458 smp->data.u.str.area = smp->strm->res_cap[idx];
1459 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1460
1461 return 1;
1462}
1463
1464/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1465static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1466{
1467 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001468 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001469 char *ptr;
1470
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001471 if (!smp->strm)
1472 return 0;
1473
1474 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001475 if (!txn || !txn->uri)
1476 return 0;
1477
1478 ptr = txn->uri;
1479
1480 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1481 ptr++;
1482
1483 temp = get_trash_chunk();
1484 temp->area = txn->uri;
1485 temp->data = ptr - txn->uri;
1486 smp->data.u.str = *temp;
1487 smp->data.type = SMP_T_STR;
1488 smp->flags = SMP_F_CONST;
1489
1490 return 1;
1491
1492}
1493
1494/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1495static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1496{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001497 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001498 struct ist path;
1499 const char *ptr;
1500
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001501 if (!smp->strm)
1502 return 0;
1503
1504 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001505 if (!txn || !txn->uri)
1506 return 0;
1507
1508 ptr = txn->uri;
1509
1510 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1511 ptr++;
1512
1513 if (!*ptr)
1514 return 0;
1515
Christopher Faulet78337bb2018-11-15 14:35:18 +01001516 /* skip the first space and find space after URI */
1517 path = ist2(++ptr, 0);
1518 while (*ptr != ' ' && *ptr != '\0')
1519 ptr++;
1520 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001521
Christopher Faulet78337bb2018-11-15 14:35:18 +01001522 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001523 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001524 return 0;
1525
1526 smp->data.u.str.area = path.ptr;
1527 smp->data.u.str.data = path.len;
1528 smp->data.type = SMP_T_STR;
1529 smp->flags = SMP_F_CONST;
1530
1531 return 1;
1532}
1533
1534/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1535 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1536 */
1537static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1538{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001539 struct http_txn *txn;
1540
1541 if (!smp->strm)
1542 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001543
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001544 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001545 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001546 return 0;
1547
1548 if (txn->req.flags & HTTP_MSGF_VER_11)
1549 smp->data.u.str.area = "HTTP/1.1";
1550 else
1551 smp->data.u.str.area = "HTTP/1.0";
1552
1553 smp->data.u.str.data = 8;
1554 smp->data.type = SMP_T_STR;
1555 smp->flags = SMP_F_CONST;
1556 return 1;
1557
1558}
1559
1560/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1561 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1562 */
1563static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1564{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001565 struct http_txn *txn;
1566
1567 if (!smp->strm)
1568 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001569
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001570 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001571 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001572 return 0;
1573
1574 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1575 smp->data.u.str.area = "HTTP/1.1";
1576 else
1577 smp->data.u.str.area = "HTTP/1.0";
1578
1579 smp->data.u.str.data = 8;
1580 smp->data.type = SMP_T_STR;
1581 smp->flags = SMP_F_CONST;
1582 return 1;
1583
1584}
1585
1586/* Iterate over all cookies present in a message. The context is stored in
1587 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1588 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1589 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdeb8cac3412020-11-13 09:38:06 +00001590 * If provided, the searched cookie name is in args, in args->data.str. If
1591 * the input options indicate that no iterating is desired, then only last
1592 * value is fetched if any. If no cookie name is provided, the first cookie
1593 * value found is fetched. The returned sample is of type CSTR. Can be used
1594 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02001595 */
1596static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1597{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001598 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1599 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001600 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001601 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001602 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1603 struct ist hdr;
Christopher Faulet934630a2020-11-13 13:41:04 +01001604 char *cook = NULL;
1605 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001606 int found = 0;
1607
Christopher Faulet934630a2020-11-13 13:41:04 +01001608 if (args && args->type == ARGT_STR) {
1609 cook = args->data.str.area;
1610 cook_l = args->data.str.data;
1611 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001612
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001613 if (!ctx) {
1614 /* first call */
1615 ctx = &static_http_hdr_ctx;
1616 ctx->blk = NULL;
1617 smp->ctx.a[2] = ctx;
1618 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001619
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001620 if (!htx)
1621 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001622
Christopher Faulet16032ab2020-04-30 11:30:00 +02001623 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001624
Maciej Zdeb8cac3412020-11-13 09:38:06 +00001625 /* OK so basically here, either we want only one value or we want to
1626 * iterate over all of them and we fetch the next one. In this last case
1627 * SMP_OPT_ITERATE option is set.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001628 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001629
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001630 if (!(smp->flags & SMP_F_NOT_LAST)) {
1631 /* search for the header from the beginning, we must first initialize
1632 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001633 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001634 smp->ctx.a[0] = NULL;
1635 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001636 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001637
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001638 smp->flags |= SMP_F_VOL_HDR;
1639 while (1) {
1640 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1641 if (!smp->ctx.a[0]) {
1642 if (!http_find_header(htx, hdr, ctx, 0))
1643 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001644
Christopher Faulet934630a2020-11-13 13:41:04 +01001645 if (ctx->value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001646 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001647
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001648 smp->ctx.a[0] = ctx->value.ptr;
1649 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001650 }
1651
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001652 smp->data.type = SMP_T_STR;
1653 smp->flags |= SMP_F_CONST;
1654 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet934630a2020-11-13 13:41:04 +01001655 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001656 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1657 &smp->data.u.str.area,
1658 &smp->data.u.str.data);
1659 if (smp->ctx.a[0]) {
1660 found = 1;
Maciej Zdeb8cac3412020-11-13 09:38:06 +00001661 if (smp->opt & SMP_OPT_ITERATE) {
1662 /* iterate on cookie value */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001663 smp->flags |= SMP_F_NOT_LAST;
1664 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001665 }
Maciej Zdeb8cac3412020-11-13 09:38:06 +00001666 if (args->data.str.data == 0) {
1667 /* No cookie name, first occurrence returned */
1668 break;
1669 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001670 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001671 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001672 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001673
Willy Tarreau79e57332018-10-02 16:01:16 +02001674 /* all cookie headers and values were scanned. If we're looking for the
1675 * last occurrence, we may return it now.
1676 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001677 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001678 smp->flags &= ~SMP_F_NOT_LAST;
1679 return found;
1680}
1681
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001682/* Same than smp_fetch_cookie() but only relies on the sample direction to
1683 * choose the right channel. So instead of duplicating the code, we just change
1684 * the keyword and then fallback on smp_fetch_cookie().
1685 */
1686static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1687{
1688 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1689 return smp_fetch_cookie(args, smp, kw, private);
1690}
1691
Willy Tarreau79e57332018-10-02 16:01:16 +02001692/* Iterate over all cookies present in a request to count how many occurrences
1693 * match the name in args and args->data.str.len. If <multi> is non-null, then
1694 * multiple cookies may be parsed on the same line. The returned sample is of
1695 * type UINT. Accepts exactly 1 argument of type string.
1696 */
1697static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1698{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001699 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1700 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001701 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001702 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001703 struct http_hdr_ctx ctx;
1704 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001705 char *val_beg, *val_end;
Christopher Faulet934630a2020-11-13 13:41:04 +01001706 char *cook = NULL;
1707 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001708 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001709
Christopher Faulet934630a2020-11-13 13:41:04 +01001710 if (args && args->type == ARGT_STR){
1711 cook = args->data.str.area;
1712 cook_l = args->data.str.data;
1713 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001714
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001715 if (!htx)
1716 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001717
Christopher Faulet16032ab2020-04-30 11:30:00 +02001718 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001719
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001720 val_end = val_beg = NULL;
1721 ctx.blk = NULL;
1722 cnt = 0;
1723 while (1) {
1724 /* Note: val_beg == NULL every time we need to fetch a new header */
1725 if (!val_beg) {
1726 if (!http_find_header(htx, hdr, &ctx, 0))
1727 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001728
Christopher Faulet934630a2020-11-13 13:41:04 +01001729 if (ctx.value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001730 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001731
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001732 val_beg = ctx.value.ptr;
1733 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001734 }
1735
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001736 smp->data.type = SMP_T_STR;
1737 smp->flags |= SMP_F_CONST;
1738 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet934630a2020-11-13 13:41:04 +01001739 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001740 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1741 &smp->data.u.str.area,
1742 &smp->data.u.str.data))) {
1743 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001744 }
1745 }
1746
1747 smp->data.type = SMP_T_SINT;
1748 smp->data.u.sint = cnt;
1749 smp->flags |= SMP_F_VOL_HDR;
1750 return 1;
1751}
1752
1753/* Fetch an cookie's integer value. The integer value is returned. It
1754 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1755 */
1756static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1757{
1758 int ret = smp_fetch_cookie(args, smp, kw, private);
1759
1760 if (ret > 0) {
1761 smp->data.type = SMP_T_SINT;
1762 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1763 smp->data.u.str.data);
1764 }
1765
1766 return ret;
1767}
1768
1769/************************************************************************/
1770/* The code below is dedicated to sample fetches */
1771/************************************************************************/
1772
1773/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001774 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001775 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1776 * pointers are updated for next iteration before leaving.
1777 */
1778static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1779{
1780 const char *vstart, *vend;
1781 struct buffer *temp;
1782 const char **chunks = (const char **)smp->ctx.a;
1783
1784 if (!http_find_next_url_param(chunks, name, name_len,
1785 &vstart, &vend, delim))
1786 return 0;
1787
1788 /* Create sample. If the value is contiguous, return the pointer as CONST,
1789 * if the value is wrapped, copy-it in a buffer.
1790 */
1791 smp->data.type = SMP_T_STR;
1792 if (chunks[2] &&
1793 vstart >= chunks[0] && vstart <= chunks[1] &&
1794 vend >= chunks[2] && vend <= chunks[3]) {
1795 /* Wrapped case. */
1796 temp = get_trash_chunk();
1797 memcpy(temp->area, vstart, chunks[1] - vstart);
1798 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1799 vend - chunks[2]);
1800 smp->data.u.str.area = temp->area;
1801 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1802 } else {
1803 /* Contiguous case. */
1804 smp->data.u.str.area = (char *)vstart;
1805 smp->data.u.str.data = vend - vstart;
1806 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1807 }
1808
1809 /* Update context, check wrapping. */
1810 chunks[0] = vend;
1811 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1812 chunks[1] = chunks[3];
1813 chunks[2] = NULL;
1814 }
1815
1816 if (chunks[0] < chunks[1])
1817 smp->flags |= SMP_F_NOT_LAST;
1818
1819 return 1;
1820}
1821
1822/* This function iterates over each parameter of the query string. It uses
1823 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1824 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1825 * An optional parameter name is passed in args[0], otherwise any parameter is
1826 * considered. It supports an optional delimiter argument for the beginning of
1827 * the string in args[1], which defaults to "?".
1828 */
1829static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1830{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001831 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001832 char delim = '?';
1833 const char *name;
1834 int name_len;
1835
1836 if (!args ||
1837 (args[0].type && args[0].type != ARGT_STR) ||
1838 (args[1].type && args[1].type != ARGT_STR))
1839 return 0;
1840
1841 name = "";
1842 name_len = 0;
1843 if (args->type == ARGT_STR) {
1844 name = args->data.str.area;
1845 name_len = args->data.str.data;
1846 }
1847
1848 if (args[1].type)
1849 delim = *args[1].data.str.area;
1850
1851 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001852 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001853 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001854
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001855 if (!htx)
1856 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001857
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001858 sl = http_get_stline(htx);
1859 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1860 if (!smp->ctx.a[0])
1861 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001862
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001863 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001864
1865 /* Assume that the context is filled with NULL pointer
1866 * before the first call.
1867 * smp->ctx.a[2] = NULL;
1868 * smp->ctx.a[3] = NULL;
1869 */
1870 }
1871
1872 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1873}
1874
1875/* This function iterates over each parameter of the body. This requires
1876 * that the body has been waited for using http-buffer-request. It uses
1877 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001878 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001879 * optional second part if the body wraps at the end of the buffer. An optional
1880 * parameter name is passed in args[0], otherwise any parameter is considered.
1881 */
1882static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1883{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001884 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001885 const char *name;
1886 int name_len;
1887
1888 if (!args || (args[0].type && args[0].type != ARGT_STR))
1889 return 0;
1890
1891 name = "";
1892 name_len = 0;
1893 if (args[0].type == ARGT_STR) {
1894 name = args[0].data.str.area;
1895 name_len = args[0].data.str.data;
1896 }
1897
1898 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001899 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001900 struct buffer *temp;
1901 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001902
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001903 if (!htx)
1904 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001905
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001906 temp = get_trash_chunk();
1907 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1908 struct htx_blk *blk = htx_get_blk(htx, pos);
1909 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001910
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001911 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1912 break;
1913 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001914 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001915 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001916 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001917 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001918
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001919 smp->ctx.a[0] = temp->area;
1920 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001921
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001922 /* Assume that the context is filled with NULL pointer
1923 * before the first call.
1924 * smp->ctx.a[2] = NULL;
1925 * smp->ctx.a[3] = NULL;
1926 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001927
Willy Tarreau79e57332018-10-02 16:01:16 +02001928 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001929
Willy Tarreau79e57332018-10-02 16:01:16 +02001930 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1931}
1932
1933/* Return the signed integer value for the specified url parameter (see url_param
1934 * above).
1935 */
1936static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1937{
1938 int ret = smp_fetch_url_param(args, smp, kw, private);
1939
1940 if (ret > 0) {
1941 smp->data.type = SMP_T_SINT;
1942 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1943 smp->data.u.str.data);
1944 }
1945
1946 return ret;
1947}
1948
1949/* This produces a 32-bit hash of the concatenation of the first occurrence of
1950 * the Host header followed by the path component if it begins with a slash ('/').
1951 * This means that '*' will not be added, resulting in exactly the first Host
1952 * entry. If no Host header is found, then the path is used. The resulting value
1953 * is hashed using the url hash followed by a full avalanche hash and provides a
1954 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1955 * high-traffic sites without having to store whole paths.
1956 * this differs from the base32 functions in that it includes the url parameters
1957 * as well as the path
1958 */
1959static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1960{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001961 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001962 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001963 struct http_hdr_ctx ctx;
1964 struct htx_sl *sl;
1965 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001966 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001967
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001968 if (!htx)
1969 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001970
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001971 ctx.blk = NULL;
1972 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1973 /* OK we have the header value in ctx.value */
1974 while (ctx.value.len--)
1975 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001976 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001977
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001978 /* now retrieve the path */
1979 sl = http_get_stline(htx);
1980 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001981 if (path.len && *(path.ptr) == '/') {
1982 while (path.len--)
1983 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001984 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001985
Willy Tarreau79e57332018-10-02 16:01:16 +02001986 hash = full_hash(hash);
1987
1988 smp->data.type = SMP_T_SINT;
1989 smp->data.u.sint = hash;
1990 smp->flags = SMP_F_VOL_1ST;
1991 return 1;
1992}
1993
1994/* This concatenates the source address with the 32-bit hash of the Host and
1995 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1996 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1997 * on the source address length. The URL hash is stored before the address so
1998 * that in environments where IPv6 is insignificant, truncating the output to
1999 * 8 bytes would still work.
2000 */
2001static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2002{
2003 struct buffer *temp;
2004 struct connection *cli_conn = objt_conn(smp->sess->origin);
2005
Willy Tarreaucd7ca792019-07-17 16:57:03 +02002006 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02002007 return 0;
2008
2009 if (!smp_fetch_url32(args, smp, kw, private))
2010 return 0;
2011
2012 temp = get_trash_chunk();
2013 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2014 temp->data += sizeof(unsigned int);
2015
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002016 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02002017 case AF_INET:
2018 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002019 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002020 4);
2021 temp->data += 4;
2022 break;
2023 case AF_INET6:
2024 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002025 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002026 16);
2027 temp->data += 16;
2028 break;
2029 default:
2030 return 0;
2031 }
2032
2033 smp->data.u.str = *temp;
2034 smp->data.type = SMP_T_BIN;
2035 return 1;
2036}
2037
2038/************************************************************************/
2039/* Other utility functions */
2040/************************************************************************/
2041
2042/* This function is used to validate the arguments passed to any "hdr" fetch
2043 * keyword. These keywords support an optional positive or negative occurrence
2044 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2045 * is assumed that the types are already the correct ones. Returns 0 on error,
2046 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2047 * error message in case of error, that the caller is responsible for freeing.
2048 * The initial location must either be freeable or NULL.
2049 * Note: this function's pointer is checked from Lua.
2050 */
2051int val_hdr(struct arg *arg, char **err_msg)
2052{
2053 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2054 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2055 return 0;
2056 }
2057 return 1;
2058}
2059
2060/************************************************************************/
2061/* All supported sample fetch keywords must be declared here. */
2062/************************************************************************/
2063
2064/* Note: must not be declared <const> as its list will be overwritten */
2065static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2066 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2067 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2068 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2069
2070 /* capture are allocated and are permanent in the stream */
2071 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2072
2073 /* retrieve these captures from the HTTP logs */
2074 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2075 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2076 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2077
2078 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2079 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2080
2081 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2082 * are only here to match the ACL's name, are request-only and are used
2083 * for ACL compatibility only.
2084 */
2085 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002086 { "cookie", smp_fetch_chn_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002087 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2088 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2089
2090 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2091 * only here to match the ACL's name, are request-only and are used for
2092 * ACL compatibility only.
2093 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002094 { "hdr", smp_fetch_chn_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002095 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2096 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2097 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2098
Christopher Fauleta4063562019-08-02 11:51:37 +02002099 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2100 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2101 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002102 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2103 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2104 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2105 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2106 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Faulete720c322020-09-02 17:25:18 +02002107 { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002108 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2109
2110 /* HTTP protocol on the request path */
2111 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2112 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2113
2114 /* HTTP version on the request path */
2115 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2116 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2117
2118 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2119 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2120 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2121 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2122
2123 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2124 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2125
2126 /* HTTP version on the response path */
2127 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2128 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2129
Christopher Faulete596d182020-05-05 17:46:34 +02002130 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2131 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2132 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2133
2134 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2135 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2136
Willy Tarreau79e57332018-10-02 16:01:16 +02002137 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2138 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2139 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2140 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2141
2142 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2143 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2144 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2145 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2146 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2147 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2148 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2149
2150 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2151 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2152 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2153 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2154
2155 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2156 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2157 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2158 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2159 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2160 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2161 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2162
2163 /* scook is valid only on the response and is used for ACL compatibility */
2164 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2165 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2166 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2167 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2168
2169 /* shdr is valid only on the response and is used for ACL compatibility */
2170 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2171 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2172 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2173 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2174
2175 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2176 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2177 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2178 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2179 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2180 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2181 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2182 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2183 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2184 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Christopher Faulet16032ab2020-04-30 11:30:00 +02002185
Willy Tarreau79e57332018-10-02 16:01:16 +02002186 { /* END */ },
2187}};
2188
Willy Tarreau0108d902018-11-25 19:14:37 +01002189INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002190
2191/*
2192 * Local variables:
2193 * c-indent-level: 8
2194 * c-basic-offset: 8
2195 * End:
2196 */