blob: 0b8f2678f17155e840c8f60c2acd241c8db225ce [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
19#include <common/base64.h>
20#include <common/chunk.h>
21#include <common/compat.h>
22#include <common/config.h>
23#include <common/debug.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010024#include <common/h1.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020025#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010026#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010027#include <common/initcall.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020028#include <common/memory.h>
29#include <common/standard.h>
30#include <common/version.h>
31
32#include <types/global.h>
33
34#include <proto/arg.h>
35#include <proto/auth.h>
Willy Tarreau538746a2018-12-11 10:59:20 +010036#include <proto/hdr_idx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020037#include <proto/http_fetch.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020038#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020039#include <proto/log.h>
40#include <proto/obj_type.h>
41#include <proto/proto_http.h>
42#include <proto/sample.h>
43#include <proto/stream.h>
44
45
46/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
47static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Christopher Fauletef453ed2018-10-24 21:39:27 +020048static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
49
Christopher Faulet89dc4992019-04-17 12:02:59 +020050#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
51#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020052
53/*
54 * Returns the data from Authorization header. Function may be called more
55 * than once so data is stored in txn->auth_data. When no header is found
56 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
57 * searching again for something we are unable to find anyway. However, if
58 * the result if valid, the cache is not reused because we would risk to
59 * have the credentials overwritten by another stream in parallel.
60 */
61
Christopher Faulet311c7ea2018-10-24 21:41:55 +020062static int get_http_auth(struct sample *smp)
Willy Tarreau79e57332018-10-02 16:01:16 +020063{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020064 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020065 struct http_txn *txn = s->txn;
66 struct buffer auth_method;
Willy Tarreau79e57332018-10-02 16:01:16 +020067 char *h, *p;
68 int len;
69
70#ifdef DEBUG_AUTH
71 printf("Auth for stream %p: %d\n", s, txn->auth.method);
72#endif
Willy Tarreau79e57332018-10-02 16:01:16 +020073 if (txn->auth.method == HTTP_AUTH_WRONG)
74 return 0;
75
76 txn->auth.method = HTTP_AUTH_WRONG;
77
Christopher Faulet311c7ea2018-10-24 21:41:55 +020078 if (IS_HTX_STRM(s) || (smp->px->mode == PR_MODE_TCP)) {
79 /* HTX version */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010080 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet311c7ea2018-10-24 21:41:55 +020081 struct http_hdr_ctx ctx = { .blk = NULL };
82 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020083
Christopher Faulet311c7ea2018-10-24 21:41:55 +020084 if (txn->flags & TX_USE_PX_CONN)
85 hdr = ist("Proxy-Authorization");
86 else
87 hdr = ist("Authorization");
88
Christopher Faulet311c7ea2018-10-24 21:41:55 +020089 ctx.blk = NULL;
90 if (!http_find_header(htx, hdr, &ctx, 0))
91 return 0;
92
93 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
94 len = p - ctx.value.ptr;
95 if (!p || len <= 0)
96 return 0;
97
98 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
99 return 0;
100
101 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200102 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200103 else {
104 /* LEGACY version */
105 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau79e57332018-10-02 16:01:16 +0200106
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200107 if (txn->flags & TX_USE_PX_CONN) {
108 h = "Proxy-Authorization";
109 len = strlen(h);
110 } else {
111 h = "Authorization";
112 len = strlen(h);
113 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200114
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200115 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
116 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200117
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200118 h = ctx.line + ctx.val;
Willy Tarreau79e57332018-10-02 16:01:16 +0200119
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200120 p = memchr(h, ' ', ctx.vlen);
121 len = p - h;
122 if (!p || len <= 0)
123 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200124
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200125 if (chunk_initlen(&auth_method, h, 0, len) != 1)
126 return 0;
127
128 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
129 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200130
131 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
132 struct buffer *http_auth = get_trash_chunk();
133
134 len = base64dec(txn->auth.method_data.area,
135 txn->auth.method_data.data,
136 http_auth->area, global.tune.bufsize - 1);
137
138 if (len < 0)
139 return 0;
140
141
142 http_auth->area[len] = '\0';
143
144 p = strchr(http_auth->area, ':');
145
146 if (!p)
147 return 0;
148
149 txn->auth.user = http_auth->area;
150 *p = '\0';
151 txn->auth.pass = p+1;
152
153 txn->auth.method = HTTP_AUTH_BASIC;
154 return 1;
155 }
156
157 return 0;
158}
159
160/* This function ensures that the prerequisites for an L7 fetch are ready,
161 * which means that a request or response is ready. If some data is missing,
162 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200163 * to extract data from L7. If <vol> is non-null during a prefetch, another
164 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200165 *
166 * The function returns :
167 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
168 * decide whether or not an HTTP message is present ;
169 * NULL if the requested data cannot be fetched or if it is certain that
170 * we'll never have any HTTP message there ;
171 * The HTX message if ready
172 */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200173struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200174{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200175 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200176 struct http_txn *txn = NULL;
177 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200178 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100179 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200180
181 /* Note: it is possible that <s> is NULL when called before stream
182 * initialization (eg: tcp-request connection), so this function is the
183 * one responsible for guarding against this case for all HTTP users.
184 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200185 if (!s || !chn)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200186 return NULL;
187
188 if (!s->txn) {
189 if (unlikely(!http_alloc_txn(s)))
190 return NULL; /* not enough memory */
191 http_init_txn(s);
192 txn = s->txn;
193 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200194 txn = s->txn;
195 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
196 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200197
Christopher Fauleteca88542019-04-03 10:12:42 +0200198 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200199 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200200
Christopher Faulet89dc4992019-04-17 12:02:59 +0200201 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
202 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200203
Christopher Faulet89dc4992019-04-17 12:02:59 +0200204 if (msg->msg_state < HTTP_MSG_BODY) {
205 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200206 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200207 /* Parsing is done by the mux, just wait */
208 smp->flags |= SMP_F_MAY_CHANGE;
209 return NULL;
210 }
211 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200212 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200213 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200214 /* The start-line was already forwarded, it is too late to fetch anything */
215 return NULL;
216 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200217 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200218 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200219 struct buffer *buf;
220 struct h1m h1m;
221 struct http_hdr hdrs[MAX_HTTP_HDR];
222 union h1_sl h1sl;
223 unsigned int flags = HTX_FL_NONE;
224 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200225
Christopher Faulet89dc4992019-04-17 12:02:59 +0200226 /* no HTTP fetch on the response in TCP mode */
227 if (chn->flags & CF_ISRESP)
228 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200229
Christopher Faulet89dc4992019-04-17 12:02:59 +0200230 /* Now we are working on the request only */
231 buf = &chn->buf;
232 if (b_head(buf) + b_data(buf) > b_wrap(buf))
233 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200234
Christopher Faulet89dc4992019-04-17 12:02:59 +0200235 h1m_init_req(&h1m);
236 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
237 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
238 if (ret <= 0) {
239 /* Invalid or too big*/
240 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200241 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100242
Christopher Faulet89dc4992019-04-17 12:02:59 +0200243 /* wait for a full request */
244 smp->flags |= SMP_F_MAY_CHANGE;
245 return NULL;
246 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100247
Christopher Faulet89dc4992019-04-17 12:02:59 +0200248 /* OK we just got a valid HTTP mesage. We have to convert it
249 * into an HTX message.
250 */
251 if (unlikely(h1sl.rq.v.len == 0)) {
252 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
253 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200254 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200255 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200256 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200257
258 /* Set HTX start-line flags */
259 if (h1m.flags & H1_MF_VER_11)
260 flags |= HTX_SL_F_VER_11;
261 if (h1m.flags & H1_MF_XFER_ENC)
262 flags |= HTX_SL_F_XFER_ENC;
263 flags |= HTX_SL_F_XFER_LEN;
264 if (h1m.flags & H1_MF_CHNK)
265 flags |= HTX_SL_F_CHNK;
266 else if (h1m.flags & H1_MF_CLEN)
267 flags |= HTX_SL_F_CLEN;
268
269 htx = htx_from_buf(get_trash_chunk());
270 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
271 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200272 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200273 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200274 }
275
276 /* OK we just got a valid HTTP message. If not already done by
277 * HTTP analyzers, we have some minor preparation to perform so
278 * that further checks can rely on HTTP tests.
279 */
280 if (sl && msg->msg_state < HTTP_MSG_BODY) {
281 if (!(chn->flags & CF_ISRESP)) {
282 txn->meth = sl->info.req.meth;
283 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
284 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200285 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200286 else
287 txn->status = sl->info.res.status;
288 if (sl->flags & HTX_SL_F_VER_11)
289 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200290 }
291
292 /* everything's OK */
293 smp->data.u.sint = 1;
294 return htx;
295}
296
297/* This function ensures that the prerequisites for an L7 fetch are ready,
298 * which means that a request or response is ready. If some data is missing,
299 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200300 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
301 * another test is made to ensure the required information is not gone.
302 *
303 * The function returns :
304 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
305 * decide whether or not an HTTP message is present ;
306 * 0 if the requested data cannot be fetched or if it is certain that
307 * we'll never have any HTTP message there ;
308 * 1 if an HTTP message is ready
309 */
310int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Christopher Faulet89dc4992019-04-17 12:02:59 +0200311 struct channel *chn, struct sample *smp, int req_vol)
Willy Tarreau79e57332018-10-02 16:01:16 +0200312{
313 struct http_txn *txn;
314 struct http_msg *msg;
315
316 /* Note: it is possible that <s> is NULL when called before stream
317 * initialization (eg: tcp-request connection), so this function is the
318 * one responsible for guarding against this case for all HTTP users.
319 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200320 if (!s || !chn)
Willy Tarreau79e57332018-10-02 16:01:16 +0200321 return 0;
322
323 if (!s->txn) {
324 if (unlikely(!http_alloc_txn(s)))
325 return 0; /* not enough memory */
326 http_init_txn(s);
327 }
328 txn = s->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200329 smp->data.type = SMP_T_BOOL;
330
Christopher Faulet89dc4992019-04-17 12:02:59 +0200331 if (chn->flags & CF_ISRESP) {
332 /* Check for a dependency on a response */
333 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
334 smp->flags |= SMP_F_MAY_CHANGE;
335 return 0;
336 }
337 goto end;
338 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200339
Christopher Faulet89dc4992019-04-17 12:02:59 +0200340 /* Check for a dependency on a request */
341 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200342
Christopher Faulet89dc4992019-04-17 12:02:59 +0200343 if (req_vol && (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
344 return 0; /* data might have moved and indexes changed */
345 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200346
Christopher Faulet89dc4992019-04-17 12:02:59 +0200347 /* If the buffer does not leave enough free space at the end, we must
348 * first realign it.
349 */
350 if (ci_head(chn) > b_orig(&chn->buf) &&
351 ci_head(chn) + ci_data(chn) > b_wrap(&chn->buf) - global.tune.maxrewrite)
352 channel_slow_realign(chn, trash.area);
Willy Tarreau79e57332018-10-02 16:01:16 +0200353
Christopher Faulet89dc4992019-04-17 12:02:59 +0200354 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
355 if (msg->msg_state == HTTP_MSG_ERROR)
356 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200357
Christopher Faulet89dc4992019-04-17 12:02:59 +0200358 /* Try to decode HTTP request */
359 if (likely(msg->next < ci_data(chn)))
360 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau79e57332018-10-02 16:01:16 +0200361
Christopher Faulet89dc4992019-04-17 12:02:59 +0200362 /* Still no valid request ? */
363 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
364 if ((msg->msg_state == HTTP_MSG_ERROR) ||
365 channel_full(chn, global.tune.maxrewrite)) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200366 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200367 }
368 /* wait for final state */
369 smp->flags |= SMP_F_MAY_CHANGE;
370 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200371 }
372
Christopher Faulet89dc4992019-04-17 12:02:59 +0200373 /* OK we just got a valid HTTP message. We have some minor
374 * preparation to perform so that further checks can rely
375 * on HTTP tests.
376 */
377
378 /* If the message was parsed but was too large, we must absolutely
379 * return an error so that it is not processed. At the moment this
380 * cannot happen, but if the parsers are to change in the future,
381 * we want this check to be maintained.
382 */
383 if (unlikely(ci_head(chn) + ci_data(chn) >
384 b_wrap(&chn->buf) - global.tune.maxrewrite)) {
385 msg->err_state = msg->msg_state;
386 msg->msg_state = HTTP_MSG_ERROR;
387 smp->data.u.sint = 1;
388 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200389 }
390
Christopher Faulet89dc4992019-04-17 12:02:59 +0200391 txn->meth = find_http_meth(ci_head(chn), msg->sl.rq.m_l);
392 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
393 s->flags |= SF_REDIRECTABLE;
394
395 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau79e57332018-10-02 16:01:16 +0200396 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200397 }
398
Christopher Faulet89dc4992019-04-17 12:02:59 +0200399 end:
Willy Tarreau79e57332018-10-02 16:01:16 +0200400 /* everything's OK */
401 smp->data.u.sint = 1;
402 return 1;
403}
404
405/* This function fetches the method of current HTTP request and stores
406 * it in the global pattern struct as a chunk. There are two possibilities :
407 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
408 * in <len> and <ptr> is NULL ;
409 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
410 * <len> to its length.
411 * This is intended to be used with pat_match_meth() only.
412 */
413static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
414{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200415 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200416 int meth;
417 struct http_txn *txn;
418
Christopher Faulet46575cd2019-04-17 11:40:30 +0200419 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200420 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200421 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200422
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200423 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +0200424 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200425
426 txn = smp->strm->txn;
427 meth = txn->meth;
428 smp->data.type = SMP_T_METH;
429 smp->data.u.meth.meth = meth;
430 if (meth == HTTP_METH_OTHER) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100431 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200432
Christopher Faulet89dc4992019-04-17 12:02:59 +0200433 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200434 /* ensure the indexes are not affected */
435 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200436 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200437 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200438 smp->flags |= SMP_F_CONST;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100439 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
440 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200441 }
442 smp->flags |= SMP_F_VOL_1ST;
443 }
444 else {
445 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200446 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200447
448 txn = smp->strm->txn;
449 meth = txn->meth;
450 smp->data.type = SMP_T_METH;
451 smp->data.u.meth.meth = meth;
452 if (meth == HTTP_METH_OTHER) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200453 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200454 /* ensure the indexes are not affected */
455 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200456 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200457 smp->flags |= SMP_F_CONST;
458 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
459 smp->data.u.meth.str.area = ci_head(txn->req.chn);
460 }
461 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200462 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200463 return 1;
464}
465
466static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
467{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200468 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200469 struct http_txn *txn;
470 char *ptr;
471 int len;
472
Christopher Faulet46575cd2019-04-17 11:40:30 +0200473 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200474 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200475 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100476 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200477
478 if (!htx)
479 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200480
Christopher Faulet297fbb42019-05-13 14:41:27 +0200481 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100482 len = HTX_SL_REQ_VLEN(sl);
483 ptr = HTX_SL_REQ_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200484 }
485 else {
486 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200487 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200488
489 txn = smp->strm->txn;
490 len = txn->req.sl.rq.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200491 ptr = ci_head(chn) + txn->req.sl.rq.v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200492 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200493
494 while ((len-- > 0) && (*ptr++ != '/'));
495 if (len <= 0)
496 return 0;
497
498 smp->data.type = SMP_T_STR;
499 smp->data.u.str.area = ptr;
500 smp->data.u.str.data = len;
501
502 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
503 return 1;
504}
505
506static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
507{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200508 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200509 struct http_txn *txn;
510 char *ptr;
511 int len;
512
Christopher Faulet46575cd2019-04-17 11:40:30 +0200513 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200514 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200515 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100516 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200517
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200518 if (!htx)
519 return 0;
520
Christopher Faulet297fbb42019-05-13 14:41:27 +0200521 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100522 len = HTX_SL_RES_VLEN(sl);
523 ptr = HTX_SL_RES_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200524 }
525 else {
526 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200527 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200528
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200529 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200530 len = txn->rsp.sl.st.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200531 ptr = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200532 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200533
534 while ((len-- > 0) && (*ptr++ != '/'));
535 if (len <= 0)
536 return 0;
537
538 smp->data.type = SMP_T_STR;
539 smp->data.u.str.area = ptr;
540 smp->data.u.str.data = len;
541
542 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
543 return 1;
544}
545
546/* 3. Check on Status Code. We manipulate integers here. */
547static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
548{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200549 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200550 struct http_txn *txn;
551 char *ptr;
552 int len;
553
Christopher Faulet46575cd2019-04-17 11:40:30 +0200554 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200555 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200556 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100557 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200558
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200559 if (!htx)
560 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200561
Christopher Faulet297fbb42019-05-13 14:41:27 +0200562 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100563 len = HTX_SL_RES_CLEN(sl);
564 ptr = HTX_SL_RES_CPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200565 }
566 else {
567 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200568 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200569
570 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200571 len = txn->rsp.sl.st.c_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200572 ptr = ci_head(chn) + txn->rsp.sl.st.c;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200573 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200574
575 smp->data.type = SMP_T_SINT;
576 smp->data.u.sint = __strl2ui(ptr, len);
577 smp->flags = SMP_F_VOL_1ST;
578 return 1;
579}
580
581static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
582{
583 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
584 return 0;
585
586 if (!smp->strm->unique_id) {
587 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
588 return 0;
589 smp->strm->unique_id[0] = '\0';
590 }
591 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
592 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
593
594 smp->data.type = SMP_T_STR;
595 smp->data.u.str.area = smp->strm->unique_id;
596 smp->flags = SMP_F_CONST;
597 return 1;
598}
599
600/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800601 * empty line which separes headers from the body. This is useful
602 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200603 */
604static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
605{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200606 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200607 struct http_txn *txn;
608
Christopher Faulet46575cd2019-04-17 11:40:30 +0200609 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200610 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200611 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200612 struct buffer *temp;
613 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200614
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200615 if (!htx)
616 return 0;
617 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200618 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200619 struct htx_blk *blk = htx_get_blk(htx, pos);
620 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200621
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200622 if (type == HTX_BLK_HDR) {
623 struct ist n = htx_get_blk_name(htx, blk);
624 struct ist v = htx_get_blk_value(htx, blk);
625
Christopher Fauletc59ff232018-12-03 13:58:44 +0100626 if (!htx_hdr_to_h1(n, v, temp))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200627 return 0;
628 }
629 else if (type == HTX_BLK_EOH) {
630 if (!chunk_memcat(temp, "\r\n", 2))
631 return 0;
632 break;
633 }
634 }
635 smp->data.type = SMP_T_STR;
636 smp->data.u.str = *temp;
637
638 }
639 else {
640 /* LEGACY version */
641 struct http_msg *msg;
642 struct hdr_idx *idx;
643
Christopher Faulet89dc4992019-04-17 12:02:59 +0200644 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200645
646 txn = smp->strm->txn;
647 idx = &txn->hdr_idx;
648 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200649
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200650 smp->data.type = SMP_T_STR;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200651 smp->data.u.str.area = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200652 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Christopher Faulet89dc4992019-04-17 12:02:59 +0200653 (ci_head(chn)[msg->eoh] == '\r');
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200654 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200655 return 1;
656}
657
658/* Returns the header request in a length/value encoded format.
659 * This is useful for exchanges with the SPOE.
660 *
661 * A "length value" is a multibyte code encoding numbers. It uses the
662 * SPOE format. The encoding is the following:
663 *
664 * Each couple "header name" / "header value" is composed
665 * like this:
666 * "length value" "header name bytes"
667 * "length value" "header value bytes"
668 * When the last header is reached, the header name and the header
669 * value are empty. Their length are 0
670 */
671static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
672{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200673 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200674 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200675 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200676
Christopher Faulet46575cd2019-04-17 11:40:30 +0200677 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200678 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200679 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200680 struct buffer *temp;
681 char *p, *end;
682 int32_t pos;
683 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200684
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200685 if (!htx)
686 return 0;
687 temp = get_trash_chunk();
688 p = temp->area;
689 end = temp->area + temp->size;
Christopher Fauleta3f15502019-05-13 15:27:23 +0200690 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200691 struct htx_blk *blk = htx_get_blk(htx, pos);
692 enum htx_blk_type type = htx_get_blk_type(blk);
693 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200694
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200695 if (type == HTX_BLK_HDR) {
696 n = htx_get_blk_name(htx,blk);
697 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200698
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200699 /* encode the header name. */
700 ret = encode_varint(n.len, &p, end);
701 if (ret == -1)
702 return 0;
703 if (p + n.len > end)
704 return 0;
705 memcpy(p, n.ptr, n.len);
706 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200707
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200708 /* encode the header value. */
709 ret = encode_varint(v.len, &p, end);
710 if (ret == -1)
711 return 0;
712 if (p + v.len > end)
713 return 0;
714 memcpy(p, v.ptr, v.len);
715 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200716
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200717 }
718 else if (type == HTX_BLK_EOH) {
719 /* encode the end of the header list with empty
720 * header name and header value.
721 */
722 ret = encode_varint(0, &p, end);
723 if (ret == -1)
724 return 0;
725 ret = encode_varint(0, &p, end);
726 if (ret == -1)
727 return 0;
728 break;
729 }
730 }
731
732 /* Initialise sample data which will be filled. */
733 smp->data.type = SMP_T_BIN;
734 smp->data.u.str.area = temp->area;
735 smp->data.u.str.data = p - temp->area;
736 smp->data.u.str.size = temp->size;
737 }
738 else {
739 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200740 struct hdr_idx *idx;
741 const char *cur_ptr, *cur_next, *p;
742 int old_idx, cur_idx;
743 struct hdr_idx_elem *cur_hdr;
744 const char *hn, *hv;
745 int hnl, hvl;
746 int ret;
747 char *buf;
748 char *end;
749
Christopher Faulet89dc4992019-04-17 12:02:59 +0200750 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200751
752 temp = get_trash_chunk();
753 buf = temp->area;
754 end = temp->area + temp->size;
755
756 txn = smp->strm->txn;
757 idx = &txn->hdr_idx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200758
759 /* Build array of headers. */
760 old_idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200761 cur_next = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200762 while (1) {
763 cur_idx = idx->v[old_idx].next;
764 if (!cur_idx)
765 break;
766 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200767
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200768 cur_hdr = &idx->v[cur_idx];
769 cur_ptr = cur_next;
770 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
771
772 /* Now we have one full header at cur_ptr of len cur_hdr->len,
773 * and the next header starts at cur_next. We'll check
774 * this header in the list as well as against the default
775 * rule.
776 */
777
778 /* look for ': *'. */
779 hn = cur_ptr;
780 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
781 if (p >= cur_ptr+cur_hdr->len)
782 continue;
783 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200784 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200785 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
786 p++;
787 if (p >= cur_ptr + cur_hdr->len)
788 continue;
789 hv = p;
790 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200791
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200792 /* encode the header name. */
793 ret = encode_varint(hnl, &buf, end);
794 if (ret == -1)
795 return 0;
796 if (buf + hnl > end)
797 return 0;
798 memcpy(buf, hn, hnl);
799 buf += hnl;
800
801 /* encode and copy the value. */
802 ret = encode_varint(hvl, &buf, end);
803 if (ret == -1)
804 return 0;
805 if (buf + hvl > end)
806 return 0;
807 memcpy(buf, hv, hvl);
808 buf += hvl;
809 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200810
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200811 /* encode the end of the header list with empty
812 * header name and header value.
813 */
814 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200815 if (ret == -1)
816 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200817 ret = encode_varint(0, &buf, end);
818 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200819 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200820
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200821 /* Initialise sample data which will be filled. */
822 smp->data.type = SMP_T_BIN;
823 smp->data.u.str.area = temp->area;
824 smp->data.u.str.data = buf - temp->area;
825 smp->data.u.str.size = temp->size;
826 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200827 return 1;
828}
829
830/* returns the longest available part of the body. This requires that the body
831 * has been waited for using http-buffer-request.
832 */
833static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
834{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200835 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200836 struct buffer *temp;
837
Christopher Faulet46575cd2019-04-17 11:40:30 +0200838 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200839 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200840 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200841 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200842
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200843 if (!htx)
844 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200845
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200846 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200847 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200848 struct htx_blk *blk = htx_get_blk(htx, pos);
849 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200850
Christopher Faulet54b5e212019-06-04 10:08:28 +0200851 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200852 break;
853 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +0100854 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200855 return 0;
856 }
857 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200858
Willy Tarreau79e57332018-10-02 16:01:16 +0200859 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200860 smp->data.u.str = *temp;
861 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200862 }
863 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200864 /* LEGACY version */
865 struct http_msg *msg;
866 unsigned long len;
867 unsigned long block1;
868 char *body;
869
Christopher Faulet89dc4992019-04-17 12:02:59 +0200870 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200871
Christopher Faulet89dc4992019-04-17 12:02:59 +0200872 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200873 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200874 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200875
876 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200877 if (block1 > b_wrap(&chn->buf) - body)
878 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200879
880 if (block1 == len) {
881 /* buffer is not wrapped (or empty) */
882 smp->data.type = SMP_T_BIN;
883 smp->data.u.str.area = body;
884 smp->data.u.str.data = len;
885 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
886 }
887 else {
888 /* buffer is wrapped, we need to defragment it */
889 temp = get_trash_chunk();
890 memcpy(temp->area, body, block1);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200891 memcpy(temp->area + block1, b_orig(&chn->buf), len - block1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200892 smp->data.type = SMP_T_BIN;
893 smp->data.u.str.area = temp->area;
894 smp->data.u.str.data = len;
895 smp->flags = SMP_F_VOL_TEST;
896 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200897 }
898 return 1;
899}
900
901
902/* returns the available length of the body. This requires that the body
903 * has been waited for using http-buffer-request.
904 */
905static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
906{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200907 struct channel *chn = SMP_REQ_CHN(smp);
908
Christopher Faulet46575cd2019-04-17 11:40:30 +0200909 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200910 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200911 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100912 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100913 unsigned long long len = 0;
914
915 if (!htx)
916 return 0;
917
Christopher Fauleta3f15502019-05-13 15:27:23 +0200918 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +0100919 struct htx_blk *blk = htx_get_blk(htx, pos);
920 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100921
Christopher Faulet54b5e212019-06-04 10:08:28 +0200922 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100923 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100924 if (type == HTX_BLK_DATA)
925 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100926 }
927
928 smp->data.type = SMP_T_SINT;
929 smp->data.u.sint = len;
930
931 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200932 }
933 else {
934 /* LEGACY version */
935 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200936
Christopher Faulet89dc4992019-04-17 12:02:59 +0200937 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200938
Christopher Faulet89dc4992019-04-17 12:02:59 +0200939 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200940 smp->data.type = SMP_T_SINT;
941 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200942
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200943 smp->flags = SMP_F_VOL_TEST;
944 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200945 return 1;
946}
947
948
949/* returns the advertised length of the body, or the advertised size of the
950 * chunks available in the buffer. This requires that the body has been waited
951 * for using http-buffer-request.
952 */
953static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
954{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200955 struct channel *chn = SMP_REQ_CHN(smp);
956
Christopher Faulet46575cd2019-04-17 11:40:30 +0200957 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200958 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200959 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100960 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100961 unsigned long long len = 0;
962
963 if (!htx)
964 return 0;
965
Christopher Fauleta3f15502019-05-13 15:27:23 +0200966 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +0100967 struct htx_blk *blk = htx_get_blk(htx, pos);
968 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100969
Christopher Faulet54b5e212019-06-04 10:08:28 +0200970 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100971 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100972 if (type == HTX_BLK_DATA)
973 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100974 }
975 if (htx->extra != ULLONG_MAX)
976 len += htx->extra;
977
978 smp->data.type = SMP_T_SINT;
979 smp->data.u.sint = len;
980
981 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200982 }
983 else {
984 /* LEGACY version */
985 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200986
Christopher Faulet89dc4992019-04-17 12:02:59 +0200987 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200988
Christopher Faulet89dc4992019-04-17 12:02:59 +0200989 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200990 smp->data.type = SMP_T_SINT;
991 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200992
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200993 smp->flags = SMP_F_VOL_TEST;
994 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200995 return 1;
996}
997
998
999/* 4. Check on URL/URI. A pointer to the URI is stored. */
1000static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
1001{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001002 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001003 struct http_txn *txn;
1004
Christopher Faulet46575cd2019-04-17 11:40:30 +02001005 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001006 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001007 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001008 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001009
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001010 if (!htx)
1011 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001012 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001013 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001014 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
1015 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001016 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1017 }
1018 else {
1019 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001020 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001021
1022 txn = smp->strm->txn;
1023 smp->data.type = SMP_T_STR;
1024 smp->data.u.str.data = txn->req.sl.rq.u_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001025 smp->data.u.str.area = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001026 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1027 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001028 return 1;
1029}
1030
1031static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1032{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001033 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001034 struct http_txn *txn;
1035 struct sockaddr_storage addr;
1036
Christopher Faulet46575cd2019-04-17 11:40:30 +02001037 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001038 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001039 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001040 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001041
1042 if (!htx)
1043 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001044 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001045 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001046 }
1047 else {
1048 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001049 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001050
1051 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001052 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001053 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001054
Willy Tarreau79e57332018-10-02 16:01:16 +02001055 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1056 return 0;
1057
1058 smp->data.type = SMP_T_IPV4;
1059 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1060 smp->flags = 0;
1061 return 1;
1062}
1063
1064static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1065{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001066 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001067 struct http_txn *txn;
1068 struct sockaddr_storage addr;
1069
Christopher Faulet46575cd2019-04-17 11:40:30 +02001070 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001071 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001072 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001073 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001074
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001075 if (!htx)
1076 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001077 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001078 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001079 }
1080 else {
1081 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001082 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001083
1084 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001085 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001086 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001087 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1088 return 0;
1089
1090 smp->data.type = SMP_T_SINT;
1091 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1092 smp->flags = 0;
1093 return 1;
1094}
1095
1096/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1097 * Accepts an optional argument of type string containing the header field name,
1098 * and an optional argument of type signed or unsigned integer to request an
1099 * explicit occurrence of the header. Note that in the event of a missing name,
1100 * headers are considered from the first one. It does not stop on commas and
1101 * returns full lines instead (useful for User-Agent or Date for example).
1102 */
1103static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1104{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001105 /* possible keywords: req.fhdr, res.fhdr */
1106 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001107 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001108
Christopher Faulet46575cd2019-04-17 11:40:30 +02001109 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001110 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001111 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001112 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1113 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001114
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001115 if (!ctx) {
1116 /* first call */
1117 ctx = &static_http_hdr_ctx;
1118 ctx->blk = NULL;
1119 smp->ctx.a[0] = ctx;
1120 }
1121
1122 if (args) {
1123 if (args[0].type != ARGT_STR)
1124 return 0;
1125 name.ptr = args[0].data.str.area;
1126 name.len = args[0].data.str.data;
1127
1128 if (args[1].type == ARGT_SINT)
1129 occ = args[1].data.sint;
1130 }
1131
1132 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001133 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001134
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001135 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1136 /* search for header from the beginning */
1137 ctx->blk = NULL;
1138
1139 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1140 /* no explicit occurrence and single fetch => last header by default */
1141 occ = -1;
1142
1143 if (!occ)
1144 /* prepare to report multiple occurrences for ACL fetches */
1145 smp->flags |= SMP_F_NOT_LAST;
1146
1147 smp->data.type = SMP_T_STR;
1148 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1149 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1150 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001151 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001152 else {
1153 /* LEGACY version */
1154 struct hdr_idx *idx;
1155 struct hdr_ctx *ctx = smp->ctx.a[0];
1156 const struct http_msg *msg;
1157 const char *name_str = NULL;
1158 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001159
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001160 if (!ctx) {
1161 /* first call */
1162 ctx = &static_hdr_ctx;
1163 ctx->idx = 0;
1164 smp->ctx.a[0] = ctx;
1165 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001166
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001167 if (args) {
1168 if (args[0].type != ARGT_STR)
1169 return 0;
1170 name_str = args[0].data.str.area;
1171 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001172
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001173 if (args[1].type == ARGT_SINT)
1174 occ = args[1].data.sint;
1175 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001176
Christopher Faulet89dc4992019-04-17 12:02:59 +02001177 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001178
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001179 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001180 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001181
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001182 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1183 /* search for header from the beginning */
1184 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001185
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001186 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1187 /* no explicit occurrence and single fetch => last header by default */
1188 occ = -1;
1189
1190 if (!occ)
1191 /* prepare to report multiple occurrences for ACL fetches */
1192 smp->flags |= SMP_F_NOT_LAST;
1193
1194 smp->data.type = SMP_T_STR;
1195 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1196 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1197 return 1;
1198 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001199 smp->flags &= ~SMP_F_NOT_LAST;
1200 return 0;
1201}
1202
1203/* 6. Check on HTTP header count. The number of occurrences is returned.
1204 * Accepts exactly 1 argument of type string. It does not stop on commas and
1205 * returns full lines instead (useful for User-Agent or Date for example).
1206 */
1207static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1208{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001209 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
1210 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001211 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001212
Christopher Faulet46575cd2019-04-17 11:40:30 +02001213 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001214 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001215 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001216 struct http_hdr_ctx ctx;
1217 struct ist name;
1218
1219 if (!htx)
1220 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001221
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001222 if (args && args->type == ARGT_STR) {
1223 name.ptr = args->data.str.area;
1224 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001225 } else {
1226 name.ptr = NULL;
1227 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001228 }
1229
1230 ctx.blk = NULL;
1231 cnt = 0;
1232 while (http_find_header(htx, name, &ctx, 1))
1233 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001234 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001235 else {
1236 /* LEGACY version */
1237 struct hdr_idx *idx;
1238 struct hdr_ctx ctx;
1239 const struct http_msg *msg;
1240 const char *name = NULL;
1241 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001242
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001243 if (args && args->type == ARGT_STR) {
1244 name = args->data.str.area;
1245 len = args->data.str.data;
1246 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001247
Christopher Faulet89dc4992019-04-17 12:02:59 +02001248 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001249
1250 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001251 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001252
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001253 ctx.idx = 0;
1254 cnt = 0;
1255 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1256 cnt++;
1257 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001258
1259 smp->data.type = SMP_T_SINT;
1260 smp->data.u.sint = cnt;
1261 smp->flags = SMP_F_VOL_HDR;
1262 return 1;
1263}
1264
1265static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1266{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001267 /* possible keywords: req.hdr_names, res.hdr_names */
1268 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001269 struct buffer *temp;
1270 char del = ',';
1271
Christopher Faulet46575cd2019-04-17 11:40:30 +02001272 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001273 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001274 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001275 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001276
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001277 if (!htx)
1278 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001279
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001280 if (args && args->type == ARGT_STR)
1281 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001282
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001283 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02001284 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001285 struct htx_blk *blk = htx_get_blk(htx, pos);
1286 enum htx_blk_type type = htx_get_blk_type(blk);
1287 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001288
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001289 if (type == HTX_BLK_EOH)
1290 break;
1291 if (type != HTX_BLK_HDR)
1292 continue;
1293 n = htx_get_blk_name(htx, blk);
1294
1295 if (temp->data)
1296 temp->area[temp->data++] = del;
1297 chunk_memcat(temp, n.ptr, n.len);
1298 }
1299 }
1300 else {
1301 /* LEGACY version */
1302 struct hdr_idx *idx;
1303 struct hdr_ctx ctx;
1304 const struct http_msg *msg;
1305
1306 if (args && args->type == ARGT_STR)
1307 del = *args[0].data.str.area;
1308
Christopher Faulet89dc4992019-04-17 12:02:59 +02001309 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001310
1311 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001312 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001313
1314 temp = get_trash_chunk();
1315
1316 ctx.idx = 0;
1317 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1318 if (temp->data)
1319 temp->area[temp->data++] = del;
1320 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1321 temp->data += ctx.del;
1322 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001323 }
1324
1325 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001326 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001327 smp->flags = SMP_F_VOL_HDR;
1328 return 1;
1329}
1330
1331/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1332 * Accepts an optional argument of type string containing the header field name,
1333 * and an optional argument of type signed or unsigned integer to request an
1334 * explicit occurrence of the header. Note that in the event of a missing name,
1335 * headers are considered from the first one.
1336 */
1337static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1338{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001339 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
1340 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001341 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001342
Christopher Faulet46575cd2019-04-17 11:40:30 +02001343 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001344 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001345 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001346 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1347 struct ist name;
1348
1349 if (!ctx) {
1350 /* first call */
1351 ctx = &static_http_hdr_ctx;
1352 ctx->blk = NULL;
1353 smp->ctx.a[0] = ctx;
1354 }
1355
1356 if (args) {
1357 if (args[0].type != ARGT_STR)
1358 return 0;
1359 name.ptr = args[0].data.str.area;
1360 name.len = args[0].data.str.data;
1361
1362 if (args[1].type == ARGT_SINT)
1363 occ = args[1].data.sint;
1364 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001365
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001366 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001367 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001368
1369 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1370 /* search for header from the beginning */
1371 ctx->blk = NULL;
1372
1373 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1374 /* no explicit occurrence and single fetch => last header by default */
1375 occ = -1;
1376
1377 if (!occ)
1378 /* prepare to report multiple occurrences for ACL fetches */
1379 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001380
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001381 smp->data.type = SMP_T_STR;
1382 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1383 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1384 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001385 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001386 else {
1387 /* LEGACY version */
1388 struct hdr_idx *idx;
1389 struct hdr_ctx *ctx = smp->ctx.a[0];
1390 const struct http_msg *msg;
1391 const char *name_str = NULL;
1392 int name_len = 0;
1393
1394 if (!ctx) {
1395 /* first call */
1396 ctx = &static_hdr_ctx;
1397 ctx->idx = 0;
1398 smp->ctx.a[0] = ctx;
1399 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001400
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001401 if (args) {
1402 if (args[0].type != ARGT_STR)
1403 return 0;
1404 name_str = args[0].data.str.area;
1405 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001406
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001407 if (args[1].type == ARGT_SINT)
1408 occ = args[1].data.sint;
1409 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001410
Christopher Faulet89dc4992019-04-17 12:02:59 +02001411 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001412
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001413 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001414 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001415
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001416 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1417 /* search for header from the beginning */
1418 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001419
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001420 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1421 /* no explicit occurrence and single fetch => last header by default */
1422 occ = -1;
1423
1424 if (!occ)
1425 /* prepare to report multiple occurrences for ACL fetches */
1426 smp->flags |= SMP_F_NOT_LAST;
1427
1428 smp->data.type = SMP_T_STR;
1429 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1430 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1431 return 1;
1432 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001433
1434 smp->flags &= ~SMP_F_NOT_LAST;
1435 return 0;
1436}
1437
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001438/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
1439 * the right channel. So instead of duplicating the code, we just change the
1440 * keyword and then fallback on smp_fetch_hdr().
1441 */
1442static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1443{
1444 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
1445 return smp_fetch_hdr(args, smp, kw, private);
1446}
1447
Willy Tarreau79e57332018-10-02 16:01:16 +02001448/* 6. Check on HTTP header count. The number of occurrences is returned.
1449 * Accepts exactly 1 argument of type string.
1450 */
1451static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1452{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001453 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
1454 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001455 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001456
Christopher Faulet46575cd2019-04-17 11:40:30 +02001457 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001458 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001459 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001460 struct http_hdr_ctx ctx;
1461 struct ist name;
1462
1463 if (!htx)
1464 return 0;
1465
1466 if (args && args->type == ARGT_STR) {
1467 name.ptr = args->data.str.area;
1468 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001469 } else {
1470 name.ptr = NULL;
1471 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001472 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001473
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001474 ctx.blk = NULL;
1475 cnt = 0;
1476 while (http_find_header(htx, name, &ctx, 0))
1477 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001478 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001479 else {
1480 /* LEGACY version */
1481 struct hdr_idx *idx;
1482 struct hdr_ctx ctx;
1483 const struct http_msg *msg;
1484 const char *name = NULL;
1485 int len = 0;
1486
1487 if (args && args->type == ARGT_STR) {
1488 name = args->data.str.area;
1489 len = args->data.str.data;
1490 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001491
Christopher Faulet89dc4992019-04-17 12:02:59 +02001492 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001493
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001494 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001495 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001496
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001497 ctx.idx = 0;
1498 cnt = 0;
1499 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1500 cnt++;
1501 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001502
1503 smp->data.type = SMP_T_SINT;
1504 smp->data.u.sint = cnt;
1505 smp->flags = SMP_F_VOL_HDR;
1506 return 1;
1507}
1508
1509/* Fetch an HTTP header's integer value. The integer value is returned. It
1510 * takes a mandatory argument of type string and an optional one of type int
1511 * to designate a specific occurrence. It returns an unsigned integer, which
1512 * may or may not be appropriate for everything.
1513 */
1514static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1515{
1516 int ret = smp_fetch_hdr(args, smp, kw, private);
1517
1518 if (ret > 0) {
1519 smp->data.type = SMP_T_SINT;
1520 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1521 smp->data.u.str.data);
1522 }
1523
1524 return ret;
1525}
1526
1527/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1528 * and an optional one of type int to designate a specific occurrence.
1529 * It returns an IPv4 or IPv6 address.
1530 */
1531static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1532{
1533 int ret;
1534
1535 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1536 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1537 smp->data.type = SMP_T_IPV4;
1538 break;
1539 } else {
1540 struct buffer *temp = get_trash_chunk();
1541 if (smp->data.u.str.data < temp->size - 1) {
1542 memcpy(temp->area, smp->data.u.str.area,
1543 smp->data.u.str.data);
1544 temp->area[smp->data.u.str.data] = '\0';
1545 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1546 smp->data.type = SMP_T_IPV6;
1547 break;
1548 }
1549 }
1550 }
1551
1552 /* if the header doesn't match an IP address, fetch next one */
1553 if (!(smp->flags & SMP_F_NOT_LAST))
1554 return 0;
1555 }
1556 return ret;
1557}
1558
1559/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1560 * the first '/' after the possible hostname, and ends before the possible '?'.
1561 */
1562static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1563{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001564 struct channel *chn = SMP_REQ_CHN(smp);
1565
Christopher Faulet46575cd2019-04-17 11:40:30 +02001566 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001567 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001568 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001569 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001570 struct ist path;
1571 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001572
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001573 if (!htx)
1574 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001575
Christopher Faulet297fbb42019-05-13 14:41:27 +02001576 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001577 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001578 if (!path.ptr)
1579 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001580
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001581 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001582 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001583
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001584 /* OK, we got the '/' ! */
1585 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001586 smp->data.u.str.area = path.ptr;
1587 smp->data.u.str.data = len;
1588 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1589 }
1590 else {
1591 struct http_txn *txn;
1592 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001593
Christopher Faulet89dc4992019-04-17 12:02:59 +02001594 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001595
1596 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001597 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001598 ptr = http_txn_get_path(txn);
1599 if (!ptr)
1600 return 0;
1601
1602 /* OK, we got the '/' ! */
1603 smp->data.type = SMP_T_STR;
1604 smp->data.u.str.area = ptr;
1605
1606 while (ptr < end && *ptr != '?')
1607 ptr++;
1608
1609 smp->data.u.str.data = ptr - smp->data.u.str.area;
1610 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1611 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001612 return 1;
1613}
1614
1615/* This produces a concatenation of the first occurrence of the Host header
1616 * followed by the path component if it begins with a slash ('/'). This means
1617 * that '*' will not be added, resulting in exactly the first Host entry.
1618 * If no Host header is found, then the path is returned as-is. The returned
1619 * value is stored in the trash so it does not need to be marked constant.
1620 * The returned sample is of type string.
1621 */
1622static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1623{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001624 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001625 struct buffer *temp;
1626
Christopher Faulet46575cd2019-04-17 11:40:30 +02001627 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001628 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001629 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001630 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001631 struct http_hdr_ctx ctx;
1632 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001633
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001634 if (!htx)
1635 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001636
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001637 ctx.blk = NULL;
1638 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1639 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001640
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001641 /* OK we have the header value in ctx.value */
1642 temp = get_trash_chunk();
1643 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1644
1645 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001646 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001647 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001648 if (path.ptr) {
1649 size_t len;
1650
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001651 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1652 ;
1653
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001654 if (len && *(path.ptr) == '/')
1655 chunk_memcat(temp, path.ptr, len);
1656 }
1657
1658 smp->data.type = SMP_T_STR;
1659 smp->data.u.str = *temp;
1660 }
1661 else {
1662 /* LEGACY version */
1663 struct http_txn *txn;
1664 char *ptr, *end, *beg;
1665 struct hdr_ctx ctx;
1666
Christopher Faulet89dc4992019-04-17 12:02:59 +02001667 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001668
1669 txn = smp->strm->txn;
1670 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001671 if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001672 return smp_fetch_path(args, smp, kw, private);
1673
1674 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1675 temp = get_trash_chunk();
1676 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1677 smp->data.type = SMP_T_STR;
1678 smp->data.u.str.area = temp->area;
1679 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001680
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001681 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001682 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001683 beg = http_txn_get_path(txn);
1684 if (!beg)
1685 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001686
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001687 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1688
1689 if (beg < ptr && *beg == '/') {
1690 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1691 ptr - beg);
1692 smp->data.u.str.data += ptr - beg;
1693 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001694 }
1695
1696 smp->flags = SMP_F_VOL_1ST;
1697 return 1;
1698}
1699
1700/* This produces a 32-bit hash of the concatenation of the first occurrence of
1701 * the Host header followed by the path component if it begins with a slash ('/').
1702 * This means that '*' will not be added, resulting in exactly the first Host
1703 * entry. If no Host header is found, then the path is used. The resulting value
1704 * is hashed using the path hash followed by a full avalanche hash and provides a
1705 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1706 * high-traffic sites without having to store whole paths.
1707 */
1708static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1709{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001710 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001711 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001712
Christopher Faulet46575cd2019-04-17 11:40:30 +02001713 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001714 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001715 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001716 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001717 struct http_hdr_ctx ctx;
1718 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001719
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001720 if (!htx)
1721 return 0;
1722
1723 ctx.blk = NULL;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001724 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001725 /* OK we have the header value in ctx.value */
1726 while (ctx.value.len--)
1727 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1728 }
1729
1730 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001731 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001732 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001733 if (path.ptr) {
1734 size_t len;
1735
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001736 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1737 ;
1738
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001739 if (len && *(path.ptr) == '/') {
1740 while (len--)
1741 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1742 }
1743 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001744 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001745 else {
1746 /* LEGACY version */
1747 struct http_txn *txn;
1748 struct hdr_ctx ctx;
1749 char *ptr, *beg, *end;
1750 int len;
1751
Christopher Faulet89dc4992019-04-17 12:02:59 +02001752 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001753
1754 txn = smp->strm->txn;
1755 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001756 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001757 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1758 ptr = ctx.line + ctx.val;
1759 len = ctx.vlen;
1760 while (len--)
1761 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1762 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001763
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001764 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001765 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001766 beg = http_txn_get_path(txn);
1767 if (!beg)
1768 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001769
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001770 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001771
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001772 if (beg < ptr && *beg == '/') {
1773 while (beg < ptr)
1774 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1775 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001776 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001777
Willy Tarreau79e57332018-10-02 16:01:16 +02001778 hash = full_hash(hash);
1779
1780 smp->data.type = SMP_T_SINT;
1781 smp->data.u.sint = hash;
1782 smp->flags = SMP_F_VOL_1ST;
1783 return 1;
1784}
1785
1786/* This concatenates the source address with the 32-bit hash of the Host and
1787 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1788 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1789 * on the source address length. The path hash is stored before the address so
1790 * that in environments where IPv6 is insignificant, truncating the output to
1791 * 8 bytes would still work.
1792 */
1793static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1794{
1795 struct buffer *temp;
1796 struct connection *cli_conn = objt_conn(smp->sess->origin);
1797
1798 if (!cli_conn)
1799 return 0;
1800
1801 if (!smp_fetch_base32(args, smp, kw, private))
1802 return 0;
1803
1804 temp = get_trash_chunk();
1805 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1806 temp->data += sizeof(unsigned int);
1807
1808 switch (cli_conn->addr.from.ss_family) {
1809 case AF_INET:
1810 memcpy(temp->area + temp->data,
1811 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1812 4);
1813 temp->data += 4;
1814 break;
1815 case AF_INET6:
1816 memcpy(temp->area + temp->data,
1817 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1818 16);
1819 temp->data += 16;
1820 break;
1821 default:
1822 return 0;
1823 }
1824
1825 smp->data.u.str = *temp;
1826 smp->data.type = SMP_T_BIN;
1827 return 1;
1828}
1829
1830/* Extracts the query string, which comes after the question mark '?'. If no
1831 * question mark is found, nothing is returned. Otherwise it returns a sample
1832 * of type string carrying the whole query string.
1833 */
1834static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1835{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001836 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001837 char *ptr, *end;
1838
Christopher Faulet46575cd2019-04-17 11:40:30 +02001839 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001840 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001841 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001842 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001843
1844 if (!htx)
1845 return 0;
1846
Christopher Faulet297fbb42019-05-13 14:41:27 +02001847 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001848 ptr = HTX_SL_REQ_UPTR(sl);
1849 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001850 }
1851 else {
1852 /* LEGACY version */
1853 struct http_txn *txn;
1854
Christopher Faulet89dc4992019-04-17 12:02:59 +02001855 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001856
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001857 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001858 ptr = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001859 end = ptr + txn->req.sl.rq.u_l;
1860 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001861
1862 /* look up the '?' */
1863 do {
1864 if (ptr == end)
1865 return 0;
1866 } while (*ptr++ != '?');
1867
1868 smp->data.type = SMP_T_STR;
1869 smp->data.u.str.area = ptr;
1870 smp->data.u.str.data = end - ptr;
1871 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1872 return 1;
1873}
1874
1875static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1876{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001877 struct channel *chn = SMP_REQ_CHN(smp);
1878
Christopher Faulet46575cd2019-04-17 11:40:30 +02001879 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001880 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001881 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001882
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001883 if (!htx)
1884 return 0;
1885 }
1886 else {
1887 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001888
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001889 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1890 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1891 */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001892 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001893 }
1894 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001895 smp->data.u.sint = 1;
1896 return 1;
1897}
1898
1899/* return a valid test if the current request is the first one on the connection */
1900static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1901{
1902 smp->data.type = SMP_T_BOOL;
1903 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1904 return 1;
1905}
1906
1907/* Accepts exactly 1 argument of type userlist */
1908static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1909{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001910 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001911
1912 if (!args || args->type != ARGT_USR)
1913 return 0;
1914
Christopher Faulet46575cd2019-04-17 11:40:30 +02001915 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001916 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001917 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001918
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001919 if (!htx)
1920 return 0;
1921 }
1922 else {
1923 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001924 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001925 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001926
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001927 if (!get_http_auth(smp))
1928 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001929 smp->data.type = SMP_T_BOOL;
1930 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001931 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001932 return 1;
1933}
1934
1935/* Accepts exactly 1 argument of type userlist */
1936static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1937{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001938 struct channel *chn = SMP_REQ_CHN(smp);
1939
Willy Tarreau79e57332018-10-02 16:01:16 +02001940 if (!args || args->type != ARGT_USR)
1941 return 0;
1942
Christopher Faulet46575cd2019-04-17 11:40:30 +02001943 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001944 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001945 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001946
1947 if (!htx)
1948 return 0;
1949 }
1950 else {
1951 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001952 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001953 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001954
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001955 if (!get_http_auth(smp))
Willy Tarreau79e57332018-10-02 16:01:16 +02001956 return 0;
1957
1958 /* if the user does not belong to the userlist or has a wrong password,
1959 * report that it unconditionally does not match. Otherwise we return
1960 * a string containing the username.
1961 */
1962 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1963 smp->strm->txn->auth.pass))
1964 return 0;
1965
1966 /* pat_match_auth() will need the user list */
1967 smp->ctx.a[0] = args->data.usr;
1968
1969 smp->data.type = SMP_T_STR;
1970 smp->flags = SMP_F_CONST;
1971 smp->data.u.str.area = smp->strm->txn->auth.user;
1972 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1973
1974 return 1;
1975}
1976
1977/* Fetch a captured HTTP request header. The index is the position of
1978 * the "capture" option in the configuration file
1979 */
1980static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1981{
1982 struct proxy *fe = strm_fe(smp->strm);
1983 int idx;
1984
1985 if (!args || args->type != ARGT_SINT)
1986 return 0;
1987
1988 idx = args->data.sint;
1989
1990 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1991 return 0;
1992
1993 smp->data.type = SMP_T_STR;
1994 smp->flags |= SMP_F_CONST;
1995 smp->data.u.str.area = smp->strm->req_cap[idx];
1996 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1997
1998 return 1;
1999}
2000
2001/* Fetch a captured HTTP response header. The index is the position of
2002 * the "capture" option in the configuration file
2003 */
2004static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2005{
2006 struct proxy *fe = strm_fe(smp->strm);
2007 int idx;
2008
2009 if (!args || args->type != ARGT_SINT)
2010 return 0;
2011
2012 idx = args->data.sint;
2013
2014 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
2015 return 0;
2016
2017 smp->data.type = SMP_T_STR;
2018 smp->flags |= SMP_F_CONST;
2019 smp->data.u.str.area = smp->strm->res_cap[idx];
2020 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2021
2022 return 1;
2023}
2024
2025/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2026static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2027{
2028 struct buffer *temp;
2029 struct http_txn *txn = smp->strm->txn;
2030 char *ptr;
2031
2032 if (!txn || !txn->uri)
2033 return 0;
2034
2035 ptr = txn->uri;
2036
2037 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2038 ptr++;
2039
2040 temp = get_trash_chunk();
2041 temp->area = txn->uri;
2042 temp->data = ptr - txn->uri;
2043 smp->data.u.str = *temp;
2044 smp->data.type = SMP_T_STR;
2045 smp->flags = SMP_F_CONST;
2046
2047 return 1;
2048
2049}
2050
2051/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2052static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2053{
2054 struct http_txn *txn = smp->strm->txn;
2055 struct ist path;
2056 const char *ptr;
2057
2058 if (!txn || !txn->uri)
2059 return 0;
2060
2061 ptr = txn->uri;
2062
2063 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2064 ptr++;
2065
2066 if (!*ptr)
2067 return 0;
2068
Christopher Faulet78337bb2018-11-15 14:35:18 +01002069 /* skip the first space and find space after URI */
2070 path = ist2(++ptr, 0);
2071 while (*ptr != ' ' && *ptr != '\0')
2072 ptr++;
2073 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002074
Christopher Faulet78337bb2018-11-15 14:35:18 +01002075 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002076 if (!path.ptr)
2077 return 0;
2078
2079 smp->data.u.str.area = path.ptr;
2080 smp->data.u.str.data = path.len;
2081 smp->data.type = SMP_T_STR;
2082 smp->flags = SMP_F_CONST;
2083
2084 return 1;
2085}
2086
2087/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2088 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2089 */
2090static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2091{
2092 struct http_txn *txn = smp->strm->txn;
2093
2094 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2095 return 0;
2096
2097 if (txn->req.flags & HTTP_MSGF_VER_11)
2098 smp->data.u.str.area = "HTTP/1.1";
2099 else
2100 smp->data.u.str.area = "HTTP/1.0";
2101
2102 smp->data.u.str.data = 8;
2103 smp->data.type = SMP_T_STR;
2104 smp->flags = SMP_F_CONST;
2105 return 1;
2106
2107}
2108
2109/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2110 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2111 */
2112static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2113{
2114 struct http_txn *txn = smp->strm->txn;
2115
2116 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2117 return 0;
2118
2119 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2120 smp->data.u.str.area = "HTTP/1.1";
2121 else
2122 smp->data.u.str.area = "HTTP/1.0";
2123
2124 smp->data.u.str.data = 8;
2125 smp->data.type = SMP_T_STR;
2126 smp->flags = SMP_F_CONST;
2127 return 1;
2128
2129}
2130
2131/* Iterate over all cookies present in a message. The context is stored in
2132 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2133 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2134 * the direction, multiple cookies may be parsed on the same line or not.
2135 * The cookie name is in args and the name length in args->data.str.len.
2136 * Accepts exactly 1 argument of type string. If the input options indicate
2137 * that no iterating is desired, then only last value is fetched if any.
2138 * The returned sample is of type CSTR. Can be used to parse cookies in other
2139 * files.
2140 */
2141static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2142{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002143 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
2144 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002145 int occ = 0;
2146 int found = 0;
2147
2148 if (!args || args->type != ARGT_STR)
2149 return 0;
2150
Christopher Faulet46575cd2019-04-17 11:40:30 +02002151 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002152 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002153 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002154 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2155 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002156
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002157 if (!ctx) {
2158 /* first call */
2159 ctx = &static_http_hdr_ctx;
2160 ctx->blk = NULL;
2161 smp->ctx.a[2] = ctx;
2162 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002163
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002164 if (!htx)
2165 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002166
Christopher Faulet89dc4992019-04-17 12:02:59 +02002167 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002168
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002169 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2170 /* no explicit occurrence and single fetch => last cookie by default */
2171 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002172
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002173 /* OK so basically here, either we want only one value and it's the
2174 * last one, or we want to iterate over all of them and we fetch the
2175 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002176 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002177
2178 if (!(smp->flags & SMP_F_NOT_LAST)) {
2179 /* search for the header from the beginning, we must first initialize
2180 * the search parameters.
2181 */
2182 smp->ctx.a[0] = NULL;
2183 ctx->blk = NULL;
2184 }
2185
2186 smp->flags |= SMP_F_VOL_HDR;
2187 while (1) {
2188 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2189 if (!smp->ctx.a[0]) {
2190 if (!http_find_header(htx, hdr, ctx, 0))
2191 goto out;
2192
2193 if (ctx->value.len < args->data.str.data + 1)
2194 continue;
2195
2196 smp->ctx.a[0] = ctx->value.ptr;
2197 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2198 }
2199
2200 smp->data.type = SMP_T_STR;
2201 smp->flags |= SMP_F_CONST;
2202 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2203 args->data.str.area, args->data.str.data,
2204 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2205 &smp->data.u.str.area,
2206 &smp->data.u.str.data);
2207 if (smp->ctx.a[0]) {
2208 found = 1;
2209 if (occ >= 0) {
2210 /* one value was returned into smp->data.u.str.{str,len} */
2211 smp->flags |= SMP_F_NOT_LAST;
2212 return 1;
2213 }
2214 }
2215 /* if we're looking for last occurrence, let's loop */
2216 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002217 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002218 else {
2219 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002220 struct hdr_idx *idx;
2221 struct hdr_ctx *ctx = smp->ctx.a[2];
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002222 const char *hdr_name;
2223 int hdr_name_len;
2224 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002225
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002226 if (!ctx) {
2227 /* first call */
2228 ctx = &static_hdr_ctx;
2229 ctx->idx = 0;
2230 smp->ctx.a[2] = ctx;
2231 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002232
Christopher Faulet89dc4992019-04-17 12:02:59 +02002233 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002234
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002235 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002236 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002237 hdr_name = "Cookie";
2238 hdr_name_len = 6;
2239 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002240 hdr_name = "Set-Cookie";
2241 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002242 }
2243
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002244 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2245 /* no explicit occurrence and single fetch => last cookie by default */
2246 occ = -1;
2247
2248 /* OK so basically here, either we want only one value and it's the
2249 * last one, or we want to iterate over all of them and we fetch the
2250 * next one.
2251 */
2252
Christopher Faulet89dc4992019-04-17 12:02:59 +02002253 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002254 if (!(smp->flags & SMP_F_NOT_LAST)) {
2255 /* search for the header from the beginning, we must first initialize
2256 * the search parameters.
2257 */
2258 smp->ctx.a[0] = NULL;
2259 ctx->idx = 0;
2260 }
2261
2262 smp->flags |= SMP_F_VOL_HDR;
2263
2264 while (1) {
2265 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2266 if (!smp->ctx.a[0]) {
2267 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2268 goto out;
2269
2270 if (ctx->vlen < args->data.str.data + 1)
2271 continue;
2272
2273 smp->ctx.a[0] = ctx->line + ctx->val;
2274 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2275 }
2276
2277 smp->data.type = SMP_T_STR;
2278 smp->flags |= SMP_F_CONST;
2279 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2280 args->data.str.area, args->data.str.data,
2281 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2282 &smp->data.u.str.area, &smp->data.u.str.data);
2283 if (smp->ctx.a[0]) {
2284 found = 1;
2285 if (occ >= 0) {
2286 /* one value was returned into smp->data.u.str.{str,len} */
2287 smp->flags |= SMP_F_NOT_LAST;
2288 return 1;
2289 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002290 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002291 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002292 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002293 }
2294 /* all cookie headers and values were scanned. If we're looking for the
2295 * last occurrence, we may return it now.
2296 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002297 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002298 smp->flags &= ~SMP_F_NOT_LAST;
2299 return found;
2300}
2301
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002302/* Same than smp_fetch_cookie() but only relies on the sample direction to
2303 * choose the right channel. So instead of duplicating the code, we just change
2304 * the keyword and then fallback on smp_fetch_cookie().
2305 */
2306static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2307{
2308 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
2309 return smp_fetch_cookie(args, smp, kw, private);
2310}
2311
Willy Tarreau79e57332018-10-02 16:01:16 +02002312/* Iterate over all cookies present in a request to count how many occurrences
2313 * match the name in args and args->data.str.len. If <multi> is non-null, then
2314 * multiple cookies may be parsed on the same line. The returned sample is of
2315 * type UINT. Accepts exactly 1 argument of type string.
2316 */
2317static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2318{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002319 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
2320 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002321 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002322 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002323
2324 if (!args || args->type != ARGT_STR)
2325 return 0;
2326
Christopher Faulet46575cd2019-04-17 11:40:30 +02002327 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002328 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002329 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002330 struct http_hdr_ctx ctx;
2331 struct ist hdr;
2332
2333 if (!htx)
2334 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002335
Christopher Faulet89dc4992019-04-17 12:02:59 +02002336 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002337
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002338 val_end = val_beg = NULL;
2339 ctx.blk = NULL;
2340 cnt = 0;
2341 while (1) {
2342 /* Note: val_beg == NULL every time we need to fetch a new header */
2343 if (!val_beg) {
2344 if (!http_find_header(htx, hdr, &ctx, 0))
2345 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002346
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002347 if (ctx.value.len < args->data.str.data + 1)
2348 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002349
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002350 val_beg = ctx.value.ptr;
2351 val_end = val_beg + ctx.value.len;
2352 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002353
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002354 smp->data.type = SMP_T_STR;
2355 smp->flags |= SMP_F_CONST;
2356 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2357 args->data.str.area, args->data.str.data,
2358 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2359 &smp->data.u.str.area,
2360 &smp->data.u.str.data))) {
2361 cnt++;
2362 }
2363 }
2364 }
2365 else {
2366 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002367 struct hdr_idx *idx;
2368 struct hdr_ctx ctx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002369 const char *hdr_name;
2370 int hdr_name_len;
2371 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002372
Christopher Faulet89dc4992019-04-17 12:02:59 +02002373 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002374
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002375 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002376 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002377 hdr_name = "Cookie";
2378 hdr_name_len = 6;
2379 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002380 hdr_name = "Set-Cookie";
2381 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002382 }
2383
Christopher Faulet89dc4992019-04-17 12:02:59 +02002384 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002385 val_end = val_beg = NULL;
2386 ctx.idx = 0;
2387 cnt = 0;
2388
2389 while (1) {
2390 /* Note: val_beg == NULL every time we need to fetch a new header */
2391 if (!val_beg) {
2392 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2393 break;
2394
2395 if (ctx.vlen < args->data.str.data + 1)
2396 continue;
2397
2398 val_beg = ctx.line + ctx.val;
2399 val_end = val_beg + ctx.vlen;
2400 }
2401
2402 smp->data.type = SMP_T_STR;
2403 smp->flags |= SMP_F_CONST;
2404 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2405 args->data.str.area, args->data.str.data,
2406 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2407 &smp->data.u.str.area, &smp->data.u.str.data))) {
2408 cnt++;
2409 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002410 }
2411 }
2412
2413 smp->data.type = SMP_T_SINT;
2414 smp->data.u.sint = cnt;
2415 smp->flags |= SMP_F_VOL_HDR;
2416 return 1;
2417}
2418
2419/* Fetch an cookie's integer value. The integer value is returned. It
2420 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2421 */
2422static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2423{
2424 int ret = smp_fetch_cookie(args, smp, kw, private);
2425
2426 if (ret > 0) {
2427 smp->data.type = SMP_T_SINT;
2428 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2429 smp->data.u.str.data);
2430 }
2431
2432 return ret;
2433}
2434
2435/************************************************************************/
2436/* The code below is dedicated to sample fetches */
2437/************************************************************************/
2438
2439/* This scans a URL-encoded query string. It takes an optionally wrapping
2440 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2441 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2442 * pointers are updated for next iteration before leaving.
2443 */
2444static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2445{
2446 const char *vstart, *vend;
2447 struct buffer *temp;
2448 const char **chunks = (const char **)smp->ctx.a;
2449
2450 if (!http_find_next_url_param(chunks, name, name_len,
2451 &vstart, &vend, delim))
2452 return 0;
2453
2454 /* Create sample. If the value is contiguous, return the pointer as CONST,
2455 * if the value is wrapped, copy-it in a buffer.
2456 */
2457 smp->data.type = SMP_T_STR;
2458 if (chunks[2] &&
2459 vstart >= chunks[0] && vstart <= chunks[1] &&
2460 vend >= chunks[2] && vend <= chunks[3]) {
2461 /* Wrapped case. */
2462 temp = get_trash_chunk();
2463 memcpy(temp->area, vstart, chunks[1] - vstart);
2464 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2465 vend - chunks[2]);
2466 smp->data.u.str.area = temp->area;
2467 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2468 } else {
2469 /* Contiguous case. */
2470 smp->data.u.str.area = (char *)vstart;
2471 smp->data.u.str.data = vend - vstart;
2472 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2473 }
2474
2475 /* Update context, check wrapping. */
2476 chunks[0] = vend;
2477 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2478 chunks[1] = chunks[3];
2479 chunks[2] = NULL;
2480 }
2481
2482 if (chunks[0] < chunks[1])
2483 smp->flags |= SMP_F_NOT_LAST;
2484
2485 return 1;
2486}
2487
2488/* This function iterates over each parameter of the query string. It uses
2489 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2490 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2491 * An optional parameter name is passed in args[0], otherwise any parameter is
2492 * considered. It supports an optional delimiter argument for the beginning of
2493 * the string in args[1], which defaults to "?".
2494 */
2495static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2496{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002497 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002498 char delim = '?';
2499 const char *name;
2500 int name_len;
2501
2502 if (!args ||
2503 (args[0].type && args[0].type != ARGT_STR) ||
2504 (args[1].type && args[1].type != ARGT_STR))
2505 return 0;
2506
2507 name = "";
2508 name_len = 0;
2509 if (args->type == ARGT_STR) {
2510 name = args->data.str.area;
2511 name_len = args->data.str.data;
2512 }
2513
2514 if (args[1].type)
2515 delim = *args[1].data.str.area;
2516
2517 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002518 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002519 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002520 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002521 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002522
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002523 if (!htx)
2524 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002525
Christopher Faulet297fbb42019-05-13 14:41:27 +02002526 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002527 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002528 if (!smp->ctx.a[0])
2529 return 0;
2530
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002531 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002532 }
2533 else {
2534 /* LEGACY version */
2535 struct http_msg *msg;
2536
Christopher Faulet89dc4992019-04-17 12:02:59 +02002537 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002538
2539 msg = &smp->strm->txn->req;
2540
Christopher Faulet89dc4992019-04-17 12:02:59 +02002541 smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002542 msg->sl.rq.u_l, delim);
2543 if (!smp->ctx.a[0])
2544 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002545
Christopher Faulet89dc4992019-04-17 12:02:59 +02002546 smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002547 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002548
2549 /* Assume that the context is filled with NULL pointer
2550 * before the first call.
2551 * smp->ctx.a[2] = NULL;
2552 * smp->ctx.a[3] = NULL;
2553 */
2554 }
2555
2556 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2557}
2558
2559/* This function iterates over each parameter of the body. This requires
2560 * that the body has been waited for using http-buffer-request. It uses
2561 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2562 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2563 * optional second part if the body wraps at the end of the buffer. An optional
2564 * parameter name is passed in args[0], otherwise any parameter is considered.
2565 */
2566static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2567{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002568 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002569 const char *name;
2570 int name_len;
2571
2572 if (!args || (args[0].type && args[0].type != ARGT_STR))
2573 return 0;
2574
2575 name = "";
2576 name_len = 0;
2577 if (args[0].type == ARGT_STR) {
2578 name = args[0].data.str.area;
2579 name_len = args[0].data.str.data;
2580 }
2581
2582 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002583 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002584 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002585 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002586 struct buffer *temp;
2587 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002588
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002589 if (!htx)
2590 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002591
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002592 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02002593 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002594 struct htx_blk *blk = htx_get_blk(htx, pos);
2595 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002596
Christopher Faulet54b5e212019-06-04 10:08:28 +02002597 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002598 break;
2599 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002600 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002601 return 0;
2602 }
2603 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002604
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002605 smp->ctx.a[0] = temp->area;
2606 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002607
2608 /* Assume that the context is filled with NULL pointer
2609 * before the first call.
2610 * smp->ctx.a[2] = NULL;
2611 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002612 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002613 }
2614 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002615 /* LEGACY version */
2616 struct http_msg *msg;
2617 unsigned long len;
2618 unsigned long block1;
2619 char *body;
2620
Christopher Faulet89dc4992019-04-17 12:02:59 +02002621 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002622
Christopher Faulet89dc4992019-04-17 12:02:59 +02002623 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002624 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +02002625 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002626
2627 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002628 if (block1 > b_wrap(&chn->buf) - body)
2629 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002630
2631 if (block1 == len) {
2632 /* buffer is not wrapped (or empty) */
2633 smp->ctx.a[0] = body;
2634 smp->ctx.a[1] = body + len;
2635
2636 /* Assume that the context is filled with NULL pointer
2637 * before the first call.
2638 * smp->ctx.a[2] = NULL;
2639 * smp->ctx.a[3] = NULL;
2640 */
2641 }
2642 else {
2643 /* buffer is wrapped, we need to defragment it */
2644 smp->ctx.a[0] = body;
2645 smp->ctx.a[1] = body + block1;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002646 smp->ctx.a[2] = b_orig(&chn->buf);
2647 smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 );
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002648 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002649 }
2650 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002651
Willy Tarreau79e57332018-10-02 16:01:16 +02002652 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2653}
2654
2655/* Return the signed integer value for the specified url parameter (see url_param
2656 * above).
2657 */
2658static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2659{
2660 int ret = smp_fetch_url_param(args, smp, kw, private);
2661
2662 if (ret > 0) {
2663 smp->data.type = SMP_T_SINT;
2664 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2665 smp->data.u.str.data);
2666 }
2667
2668 return ret;
2669}
2670
2671/* This produces a 32-bit hash of the concatenation of the first occurrence of
2672 * the Host header followed by the path component if it begins with a slash ('/').
2673 * This means that '*' will not be added, resulting in exactly the first Host
2674 * entry. If no Host header is found, then the path is used. The resulting value
2675 * is hashed using the url hash followed by a full avalanche hash and provides a
2676 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2677 * high-traffic sites without having to store whole paths.
2678 * this differs from the base32 functions in that it includes the url parameters
2679 * as well as the path
2680 */
2681static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2682{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002683 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002684 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002685
Christopher Faulet46575cd2019-04-17 11:40:30 +02002686 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002687 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002688 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002689 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002690 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002691 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002692
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002693 if (!htx)
2694 return 0;
2695
2696 ctx.blk = NULL;
2697 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2698 /* OK we have the header value in ctx.value */
2699 while (ctx.value.len--)
2700 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2701 }
2702
2703 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02002704 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002705 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002706 while (path.len > 0 && *(path.ptr) != '?') {
2707 path.ptr++;
2708 path.len--;
2709 }
2710 if (path.len && *(path.ptr) == '/') {
2711 while (path.len--)
2712 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2713 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002714 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002715 else {
2716 /* LEGACY version */
2717 struct http_txn *txn;
2718 struct hdr_ctx ctx;
2719 char *ptr, *beg, *end;
2720 int len;
2721
Christopher Faulet89dc4992019-04-17 12:02:59 +02002722 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002723
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002724 txn = smp->strm->txn;
2725 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002726 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002727 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2728 ptr = ctx.line + ctx.val;
2729 len = ctx.vlen;
2730 while (len--)
2731 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2732 }
2733
2734 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002735 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002736 beg = http_txn_get_path(txn);
2737 if (!beg)
2738 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002739
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002740 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002741
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002742 if (beg < ptr && *beg == '/') {
2743 while (beg < ptr)
2744 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2745 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002746 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002747
Willy Tarreau79e57332018-10-02 16:01:16 +02002748 hash = full_hash(hash);
2749
2750 smp->data.type = SMP_T_SINT;
2751 smp->data.u.sint = hash;
2752 smp->flags = SMP_F_VOL_1ST;
2753 return 1;
2754}
2755
2756/* This concatenates the source address with the 32-bit hash of the Host and
2757 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2758 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2759 * on the source address length. The URL hash is stored before the address so
2760 * that in environments where IPv6 is insignificant, truncating the output to
2761 * 8 bytes would still work.
2762 */
2763static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2764{
2765 struct buffer *temp;
2766 struct connection *cli_conn = objt_conn(smp->sess->origin);
2767
2768 if (!cli_conn)
2769 return 0;
2770
2771 if (!smp_fetch_url32(args, smp, kw, private))
2772 return 0;
2773
2774 temp = get_trash_chunk();
2775 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2776 temp->data += sizeof(unsigned int);
2777
2778 switch (cli_conn->addr.from.ss_family) {
2779 case AF_INET:
2780 memcpy(temp->area + temp->data,
2781 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2782 4);
2783 temp->data += 4;
2784 break;
2785 case AF_INET6:
2786 memcpy(temp->area + temp->data,
2787 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2788 16);
2789 temp->data += 16;
2790 break;
2791 default:
2792 return 0;
2793 }
2794
2795 smp->data.u.str = *temp;
2796 smp->data.type = SMP_T_BIN;
2797 return 1;
2798}
2799
2800/************************************************************************/
2801/* Other utility functions */
2802/************************************************************************/
2803
2804/* This function is used to validate the arguments passed to any "hdr" fetch
2805 * keyword. These keywords support an optional positive or negative occurrence
2806 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2807 * is assumed that the types are already the correct ones. Returns 0 on error,
2808 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2809 * error message in case of error, that the caller is responsible for freeing.
2810 * The initial location must either be freeable or NULL.
2811 * Note: this function's pointer is checked from Lua.
2812 */
2813int val_hdr(struct arg *arg, char **err_msg)
2814{
2815 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2816 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2817 return 0;
2818 }
2819 return 1;
2820}
2821
2822/************************************************************************/
2823/* All supported sample fetch keywords must be declared here. */
2824/************************************************************************/
2825
2826/* Note: must not be declared <const> as its list will be overwritten */
2827static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2828 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2829 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2830 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2831
2832 /* capture are allocated and are permanent in the stream */
2833 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2834
2835 /* retrieve these captures from the HTTP logs */
2836 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2837 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2838 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2839
2840 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2841 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2842
2843 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2844 * are only here to match the ACL's name, are request-only and are used
2845 * for ACL compatibility only.
2846 */
2847 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002848 { "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 +02002849 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2850 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2851
2852 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2853 * only here to match the ACL's name, are request-only and are used for
2854 * ACL compatibility only.
2855 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002856 { "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 +02002857 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2858 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2859 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2860
2861 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2862 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2863 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2864 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2865 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2866 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2867
2868 /* HTTP protocol on the request path */
2869 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2870 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2871
2872 /* HTTP version on the request path */
2873 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2874 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2875
2876 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2877 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2878 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2879 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2880
2881 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2882 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2883
2884 /* HTTP version on the response path */
2885 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2886 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2887
2888 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2889 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2890 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2891 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2892
2893 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2894 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2895 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2896 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2897 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2898 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2899 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2900
2901 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2902 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2903 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2904 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2905
2906 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2907 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2908 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2909 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2910 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2911 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2912 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2913
2914 /* scook is valid only on the response and is used for ACL compatibility */
2915 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2916 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2917 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2918 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2919
2920 /* shdr is valid only on the response and is used for ACL compatibility */
2921 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2922 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2923 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2924 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2925
2926 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2927 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2928 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2929 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2930 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2931 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2932 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2933 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2934 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2935 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2936 { /* END */ },
2937}};
2938
Willy Tarreau0108d902018-11-25 19:14:37 +01002939INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002940
2941/*
2942 * Local variables:
2943 * c-indent-level: 8
2944 * c-basic-offset: 8
2945 * End:
2946 */