blob: 8e899527b9acc2fc6fb8eff7b0387ebdb2ee523d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Frédéric Lécaillea41d5312018-01-29 12:05:07 +010029#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010043#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020044#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <types/global.h>
William Lallemand71bd11a2017-11-20 19:13:14 +010046#include <types/cache.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreau8797c062007-05-07 00:55:35 +020049#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020050#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020051#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010052#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020054#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010055#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010056#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020057#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010058#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020060#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020061#include <proto/frontend.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020062#include <proto/h1.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010064#include <proto/hdr_idx.h>
Patrick Hemmere3faf022018-08-22 10:02:00 -040065#include <proto/hlua.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010066#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020067#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010069#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020070#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020071#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010072#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020073#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010074#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020075#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020076#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020077#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020078
Cyril Bonté19979e12012-04-04 12:57:21 +020079/* status codes available for the stats admin page (strictly 4 chars length) */
80const char *stat_status_codes[STAT_STATUS_SIZE] = {
81 [STAT_STATUS_DENY] = "DENY",
82 [STAT_STATUS_DONE] = "DONE",
83 [STAT_STATUS_ERRP] = "ERRP",
84 [STAT_STATUS_EXCD] = "EXCD",
85 [STAT_STATUS_NONE] = "NONE",
86 [STAT_STATUS_PART] = "PART",
87 [STAT_STATUS_UNKN] = "UNKN",
88};
89
90
William Lallemand73025dd2014-04-24 14:38:37 +020091/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020092struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +020093 .list = LIST_HEAD_INIT(http_req_keywords.list)
94};
95
96/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020097struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +020098 .list = LIST_HEAD_INIT(http_res_keywords.list)
99};
100
Willy Tarreaua890d072013-04-02 12:01:06 +0200101/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100102static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +0200103
Willy Tarreau42250582007-04-01 01:30:43 +0200104#define FD_SETS_ARE_BITFIELDS
105#ifdef FD_SETS_ARE_BITFIELDS
106/*
107 * This map is used with all the FD_* macros to check whether a particular bit
108 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
109 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
110 * byte should be encoded. Be careful to always pass bytes from 0 to 255
111 * exclusively to the macros.
112 */
113fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
114fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100115fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200116
117#else
118#error "Check if your OS uses bitfields for fd_sets"
119#endif
120
Willy Tarreau87b09662015-04-03 00:22:06 +0200121static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200122
David Carlier7365f7d2016-04-04 11:54:42 +0100123static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
124static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100125
Willy Tarreau80587432006-12-24 17:47:20 +0100126void init_proto_http()
127{
Willy Tarreau42250582007-04-01 01:30:43 +0200128 int i;
129 char *tmp;
Willy Tarreau42250582007-04-01 01:30:43 +0200130
131 /* initialize the log header encoding map : '{|}"#' should be encoded with
132 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
133 * URL encoding only requires '"', '#' to be encoded as well as non-
134 * printable characters above.
135 */
136 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
137 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100138 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200139 for (i = 0; i < 32; i++) {
140 FD_SET(i, hdr_encode_map);
141 FD_SET(i, url_encode_map);
142 }
143 for (i = 127; i < 256; i++) {
144 FD_SET(i, hdr_encode_map);
145 FD_SET(i, url_encode_map);
146 }
147
148 tmp = "\"#{|}";
149 while (*tmp) {
150 FD_SET(*tmp, hdr_encode_map);
151 tmp++;
152 }
153
154 tmp = "\"#";
155 while (*tmp) {
156 FD_SET(*tmp, url_encode_map);
157 tmp++;
158 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200159
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100160 /* initialize the http header encoding map. The draft httpbis define the
161 * header content as:
162 *
163 * HTTP-message = start-line
164 * *( header-field CRLF )
165 * CRLF
166 * [ message-body ]
167 * header-field = field-name ":" OWS field-value OWS
168 * field-value = *( field-content / obs-fold )
169 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
170 * obs-fold = CRLF 1*( SP / HTAB )
171 * field-vchar = VCHAR / obs-text
172 * VCHAR = %x21-7E
173 * obs-text = %x80-FF
174 *
175 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
176 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
177 * "obs-fold" is volontary forgotten because haproxy remove this.
178 */
179 memset(http_encode_map, 0, sizeof(http_encode_map));
180 for (i = 0x00; i <= 0x08; i++)
181 FD_SET(i, http_encode_map);
182 for (i = 0x0a; i <= 0x1f; i++)
183 FD_SET(i, http_encode_map);
184 FD_SET(0x7f, http_encode_map);
185
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200186 /* memory allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100187 pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
188 pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100189}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200190
Willy Tarreau53b6c742006-12-17 13:37:46 +0100191/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100192 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +0200193 * the last CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100194 * The header is also automatically added to the index <hdr_idx>, and the end
195 * of headers is automatically adjusted. The number of bytes added is returned
196 * on success, otherwise <0 is returned indicating an error.
197 */
Willy Tarreau4d893d42018-07-12 15:43:32 +0200198static inline int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100199{
Willy Tarreau4d893d42018-07-12 15:43:32 +0200200 return http_header_add_tail2(msg, hdr_idx, text, strlen(text));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100201}
202
203/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100204 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +0200205 * the last CRLF. <len> bytes are copied, not counting the CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100206 * The header is also automatically added to the index <hdr_idx>, and the end
207 * of headers is automatically adjusted. The number of bytes added is returned
208 * on success, otherwise <0 is returned indicating an error.
209 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100210int http_header_add_tail2(struct http_msg *msg,
211 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100212{
213 int bytes;
214
Willy Tarreau4d893d42018-07-12 15:43:32 +0200215 bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100216 if (!bytes)
217 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100218 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100219 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
220}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200221
222/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100223 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
224 * If so, returns the position of the first non-space character relative to
225 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
226 * to return a pointer to the place after the first space. Returns 0 if the
227 * header name does not match. Checks are case-insensitive.
228 */
229int http_header_match2(const char *hdr, const char *end,
230 const char *name, int len)
231{
232 const char *val;
233
234 if (hdr + len >= end)
235 return 0;
236 if (hdr[len] != ':')
237 return 0;
238 if (strncasecmp(hdr, name, len) != 0)
239 return 0;
240 val = hdr + len + 1;
241 while (val < end && HTTP_IS_SPHT(*val))
242 val++;
243 if ((val >= end) && (len + 2 <= end - hdr))
244 return len + 2; /* we may replace starting from second space */
245 return val - hdr;
246}
247
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200248/* Find the first or next occurrence of header <name> in message buffer <sol>
249 * using headers index <idx>, and return it in the <ctx> structure. This
250 * structure holds everything necessary to use the header and find next
251 * occurrence. If its <idx> member is 0, the header is searched from the
252 * beginning. Otherwise, the next occurrence is returned. The function returns
253 * 1 when it finds a value, and 0 when there is no more. It is very similar to
254 * http_find_header2() except that it is designed to work with full-line headers
255 * whose comma is not a delimiter but is part of the syntax. As a special case,
256 * if ctx->val is NULL when searching for a new values of a header, the current
257 * header is rescanned. This allows rescanning after a header deletion.
258 */
259int http_find_full_header2(const char *name, int len,
260 char *sol, struct hdr_idx *idx,
261 struct hdr_ctx *ctx)
262{
263 char *eol, *sov;
264 int cur_idx, old_idx;
265
266 cur_idx = ctx->idx;
267 if (cur_idx) {
268 /* We have previously returned a header, let's search another one */
269 sol = ctx->line;
270 eol = sol + idx->v[cur_idx].len;
271 goto next_hdr;
272 }
273
274 /* first request for this header */
275 sol += hdr_idx_first_pos(idx);
276 old_idx = 0;
277 cur_idx = hdr_idx_first_idx(idx);
278 while (cur_idx) {
279 eol = sol + idx->v[cur_idx].len;
280
281 if (len == 0) {
282 /* No argument was passed, we want any header.
283 * To achieve this, we simply build a fake request. */
284 while (sol + len < eol && sol[len] != ':')
285 len++;
286 name = sol;
287 }
288
289 if ((len < eol - sol) &&
290 (sol[len] == ':') &&
291 (strncasecmp(sol, name, len) == 0)) {
292 ctx->del = len;
293 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100294 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200295 sov++;
296
297 ctx->line = sol;
298 ctx->prev = old_idx;
299 ctx->idx = cur_idx;
300 ctx->val = sov - sol;
301 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100302 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200303 eol--;
304 ctx->tws++;
305 }
306 ctx->vlen = eol - sov;
307 return 1;
308 }
309 next_hdr:
310 sol = eol + idx->v[cur_idx].cr + 1;
311 old_idx = cur_idx;
312 cur_idx = idx->v[cur_idx].next;
313 }
314 return 0;
315}
316
Willy Tarreauc90dc232015-02-20 13:51:36 +0100317/* Find the first or next header field in message buffer <sol> using headers
318 * index <idx>, and return it in the <ctx> structure. This structure holds
319 * everything necessary to use the header and find next occurrence. If its
320 * <idx> member is 0, the first header is retrieved. Otherwise, the next
321 * occurrence is returned. The function returns 1 when it finds a value, and
322 * 0 when there is no more. It is equivalent to http_find_full_header2() with
323 * no header name.
324 */
325int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
326{
327 char *eol, *sov;
328 int cur_idx, old_idx;
329 int len;
330
331 cur_idx = ctx->idx;
332 if (cur_idx) {
333 /* We have previously returned a header, let's search another one */
334 sol = ctx->line;
335 eol = sol + idx->v[cur_idx].len;
336 goto next_hdr;
337 }
338
339 /* first request for this header */
340 sol += hdr_idx_first_pos(idx);
341 old_idx = 0;
342 cur_idx = hdr_idx_first_idx(idx);
343 while (cur_idx) {
344 eol = sol + idx->v[cur_idx].len;
345
346 len = 0;
347 while (1) {
348 if (len >= eol - sol)
349 goto next_hdr;
350 if (sol[len] == ':')
351 break;
352 len++;
353 }
354
355 ctx->del = len;
356 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100357 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100358 sov++;
359
360 ctx->line = sol;
361 ctx->prev = old_idx;
362 ctx->idx = cur_idx;
363 ctx->val = sov - sol;
364 ctx->tws = 0;
365
Willy Tarreau2235b262016-11-05 15:50:20 +0100366 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100367 eol--;
368 ctx->tws++;
369 }
370 ctx->vlen = eol - sov;
371 return 1;
372
373 next_hdr:
374 sol = eol + idx->v[cur_idx].cr + 1;
375 old_idx = cur_idx;
376 cur_idx = idx->v[cur_idx].next;
377 }
378 return 0;
379}
380
Lukas Tribus23953682017-04-28 13:24:30 +0000381/* Find the end of the header value contained between <s> and <e>. See RFC7230,
382 * par 3.2 for more information. Note that it requires a valid header to return
Willy Tarreau68085d82010-01-18 14:54:04 +0100383 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200384 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100385char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200386{
387 int quoted, qdpair;
388
389 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100390
391#if defined(__x86_64__) || \
392 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
393 defined(__ARM_ARCH_7A__)
394 /* speedup: skip everything not a comma nor a double quote */
395 for (; s <= e - sizeof(int); s += sizeof(int)) {
396 unsigned int c = *(int *)s; // comma
397 unsigned int q = c; // quote
398
399 c ^= 0x2c2c2c2c; // contains one zero on a comma
400 q ^= 0x22222222; // contains one zero on a quote
401
402 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
403 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
404
405 if ((c | q) & 0x80808080)
406 break; // found a comma or a quote
407 }
408#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200409 for (; s < e; s++) {
410 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200411 else if (quoted) {
412 if (*s == '\\') qdpair = 1;
413 else if (*s == '"') quoted = 0;
414 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200415 else if (*s == '"') quoted = 1;
416 else if (*s == ',') return s;
417 }
418 return s;
419}
420
421/* Find the first or next occurrence of header <name> in message buffer <sol>
422 * using headers index <idx>, and return it in the <ctx> structure. This
423 * structure holds everything necessary to use the header and find next
424 * occurrence. If its <idx> member is 0, the header is searched from the
425 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100426 * 1 when it finds a value, and 0 when there is no more. It is designed to work
427 * with headers defined as comma-separated lists. As a special case, if ctx->val
428 * is NULL when searching for a new values of a header, the current header is
429 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200430 */
431int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100432 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200433 struct hdr_ctx *ctx)
434{
Willy Tarreau68085d82010-01-18 14:54:04 +0100435 char *eol, *sov;
436 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200437
Willy Tarreau68085d82010-01-18 14:54:04 +0100438 cur_idx = ctx->idx;
439 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200440 /* We have previously returned a value, let's search
441 * another one on the same line.
442 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200443 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200444 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100445 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200446 eol = sol + idx->v[cur_idx].len;
447
448 if (sov >= eol)
449 /* no more values in this header */
450 goto next_hdr;
451
Willy Tarreau68085d82010-01-18 14:54:04 +0100452 /* values remaining for this header, skip the comma but save it
453 * for later use (eg: for header deletion).
454 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200455 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100456 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200457 sov++;
458
459 goto return_hdr;
460 }
461
462 /* first request for this header */
463 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100464 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200465 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200466 while (cur_idx) {
467 eol = sol + idx->v[cur_idx].len;
468
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200469 if (len == 0) {
470 /* No argument was passed, we want any header.
471 * To achieve this, we simply build a fake request. */
472 while (sol + len < eol && sol[len] != ':')
473 len++;
474 name = sol;
475 }
476
Willy Tarreau33a7e692007-06-10 19:45:56 +0200477 if ((len < eol - sol) &&
478 (sol[len] == ':') &&
479 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100480 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200481 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100482 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200483 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100484
Willy Tarreau33a7e692007-06-10 19:45:56 +0200485 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100486 ctx->prev = old_idx;
487 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200488 ctx->idx = cur_idx;
489 ctx->val = sov - sol;
490
491 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200492 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100493 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200494 eol--;
495 ctx->tws++;
496 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200497 ctx->vlen = eol - sov;
498 return 1;
499 }
500 next_hdr:
501 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200503 cur_idx = idx->v[cur_idx].next;
504 }
505 return 0;
506}
507
508int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100509 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 struct hdr_ctx *ctx)
511{
512 return http_find_header2(name, strlen(name), sol, idx, ctx);
513}
514
Willy Tarreau68085d82010-01-18 14:54:04 +0100515/* Remove one value of a header. This only works on a <ctx> returned by one of
516 * the http_find_header functions. The value is removed, as well as surrounding
517 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100518 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100519 * message <msg>. The new index is returned. If it is zero, it means there is
520 * no more header, so any processing may stop. The ctx is always left in a form
521 * that can be handled by http_find_header2() to find next occurrence.
522 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100523int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100524{
525 int cur_idx = ctx->idx;
526 char *sol = ctx->line;
527 struct hdr_idx_elem *hdr;
528 int delta, skip_comma;
529
530 if (!cur_idx)
531 return 0;
532
533 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200534 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100535 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200536 delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100537 http_msg_move_end(msg, delta);
538 idx->used--;
539 hdr->len = 0; /* unused entry */
540 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100541 if (idx->tail == ctx->idx)
542 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100543 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100544 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100545 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200546 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100547 return ctx->idx;
548 }
549
550 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200551 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
552 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100553 */
554
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200555 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200556 delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200557 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100558 NULL, 0);
559 hdr->len += delta;
560 http_msg_move_end(msg, delta);
561 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200562 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100563 return ctx->idx;
564}
565
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100566/* This function handles a server error at the stream interface level. The
567 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100568 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100569 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100570 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200572static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +0200573 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200574{
Willy Tarreau2019f952017-03-14 11:07:31 +0100575 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100576 channel_auto_read(si_oc(si));
577 channel_abort(si_oc(si));
578 channel_auto_close(si_oc(si));
579 channel_erase(si_oc(si));
580 channel_auto_close(si_ic(si));
581 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200582 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200583 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200584 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200585 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200586 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200587 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200588}
589
Willy Tarreau87b09662015-04-03 00:22:06 +0200590/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100591 * and message.
592 */
593
Willy Tarreau83061a82018-07-13 11:56:34 +0200594struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100595{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200596 const int msgnum = http_get_status_idx(s->txn->status);
597
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200598 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200599 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200600 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200601 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100602 else
603 return &http_err_chunks[msgnum];
604}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200605
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100606void
Willy Tarreau83061a82018-07-13 11:56:34 +0200607http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100608{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200609 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100610 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100611 stream_int_retnclose(&s->si[0], msg);
612}
613
Willy Tarreau21d2af32008-02-14 20:25:24 +0100614/* Parse the URI from the given transaction (which is assumed to be in request
615 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
616 * It is returned otherwise.
617 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200618char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100619{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200620 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100621
Willy Tarreau6b952c82018-09-10 17:45:34 +0200622 ret = http_get_path(ist2(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l));
William Lallemand65ad6e12014-01-31 15:08:02 +0100623
Willy Tarreau6b952c82018-09-10 17:45:34 +0200624 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100625}
626
Willy Tarreau71241ab2012-12-27 11:30:54 +0100627/* Returns a 302 for a redirectable request that reaches a server working in
628 * in redirect mode. This may only be called just after the stream interface
629 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
630 * follow normal proxy processing. NOTE: this function is designed to support
631 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100632 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200633void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100634{
635 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100636 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100637 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200638 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100639
640 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200641 trash.data = strlen(HTTP_302);
642 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100643
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100644 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100645
Willy Tarreauefb453c2008-10-26 20:49:47 +0100646 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200647 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100648 return;
649
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100650 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100651 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200652 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
653 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100654 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100655
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200656 /* 3: add the request URI. Since it was already forwarded, we need
657 * to temporarily rewind the buffer.
658 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200659 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200660 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200661
Willy Tarreau6b952c82018-09-10 17:45:34 +0200662 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200663 len = b_dist(&s->req.buf, path, c_ptr(&s->req, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200664
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200665 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200666
Willy Tarreauefb453c2008-10-26 20:49:47 +0100667 if (!path)
668 return;
669
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200670 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100671 return;
672
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200673 memcpy(trash.area + trash.data, path, len);
674 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100675
676 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200677 memcpy(trash.area + trash.data,
678 "\r\nProxy-Connection: close\r\n\r\n", 29);
679 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100680 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200681 memcpy(trash.area + trash.data,
682 "\r\nConnection: close\r\n\r\n", 23);
683 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100684 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100685
686 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200687 si_shutr(si);
688 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100689 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100690 si->state = SI_ST_CLO;
691
692 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100693 txn->status = 302;
694 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100695
696 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100697 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500698 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100699}
700
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100701/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100702 * that the server side is closed. Note that err_type is actually a
703 * bitmask, where almost only aborts may be cumulated with other
704 * values. We consider that aborted operations are more important
705 * than timeouts or errors due to the fact that nobody else in the
706 * logs might explain incomplete retries. All others should avoid
707 * being cumulated. It should normally not be possible to have multiple
708 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100709 * Note that connection errors appearing on the second request of a keep-alive
710 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100711 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200712void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100713{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100714 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100715
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200716 /* set s->txn->status for http_error_message(s) */
717 s->txn->status = 503;
718
Willy Tarreauefb453c2008-10-26 20:49:47 +0100719 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200720 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100721 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100722 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200723 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100724 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200725 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100726 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200727 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100728 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100729 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200730 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100731 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100732 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200733 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100734 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200735 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100736 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200737 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100738 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200739 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200740 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200741 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100742 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200743 http_error_message(s));
744 else { /* SI_ET_CONN_OTHER and others */
745 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200746 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100747 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200748 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100749}
750
Willy Tarreau42250582007-04-01 01:30:43 +0200751extern const char sess_term_cond[8];
752extern const char sess_fin_state[8];
753extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +0100754struct pool_head *pool_head_http_txn;
755struct pool_head *pool_head_requri;
756struct pool_head *pool_head_capture = NULL;
757struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100758
Willy Tarreau117f59e2007-03-04 18:17:17 +0100759/*
760 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200761 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100762 */
763void capture_headers(char *som, struct hdr_idx *idx,
764 char **cap, struct cap_hdr *cap_hdr)
765{
766 char *eol, *sol, *col, *sov;
767 int cur_idx;
768 struct cap_hdr *h;
769 int len;
770
771 sol = som + hdr_idx_first_pos(idx);
772 cur_idx = hdr_idx_first_idx(idx);
773
774 while (cur_idx) {
775 eol = sol + idx->v[cur_idx].len;
776
777 col = sol;
778 while (col < eol && *col != ':')
779 col++;
780
781 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100782 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100783 sov++;
784
785 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200786 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100787 (strncasecmp(sol, h->name, h->namelen) == 0)) {
788 if (cap[h->index] == NULL)
789 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100790 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100791
792 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100793 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100794 continue;
795 }
796
797 len = eol - sov;
798 if (len > h->len)
799 len = h->len;
800
801 memcpy(cap[h->index], sov, len);
802 cap[h->index][len]=0;
803 }
804 }
805 sol = eol + idx->v[cur_idx].cr + 1;
806 cur_idx = idx->v[cur_idx].next;
807 }
808}
809
Willy Tarreaubaaee002006-06-26 02:48:02 +0200810/*
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100811 * Returns the data from Authorization header. Function may be called more
812 * than once so data is stored in txn->auth_data. When no header is found
813 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +0100814 * searching again for something we are unable to find anyway. However, if
815 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +0200816 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100817 */
818
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100819int
Willy Tarreau87b09662015-04-03 00:22:06 +0200820get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100821{
822
Willy Tarreaueee5b512015-04-03 23:46:31 +0200823 struct http_txn *txn = s->txn;
Willy Tarreau83061a82018-07-13 11:56:34 +0200824 struct buffer auth_method;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100825 struct hdr_ctx ctx;
826 char *h, *p;
827 int len;
828
829#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +0200830 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100831#endif
832
833 if (txn->auth.method == HTTP_AUTH_WRONG)
834 return 0;
835
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100836 txn->auth.method = HTTP_AUTH_WRONG;
837
838 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +0100839
840 if (txn->flags & TX_USE_PX_CONN) {
841 h = "Proxy-Authorization";
842 len = strlen(h);
843 } else {
844 h = "Authorization";
845 len = strlen(h);
846 }
847
Willy Tarreauf37954d2018-06-15 18:31:02 +0200848 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100849 return 0;
850
851 h = ctx.line + ctx.val;
852
853 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +0100854 len = p - h;
855 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100856 return 0;
857
David Carlier7365f7d2016-04-04 11:54:42 +0100858 if (chunk_initlen(&auth_method, h, 0, len) != 1)
859 return 0;
860
Willy Tarreau5c557d12016-03-13 08:17:02 +0100861 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100862
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200863 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200864 struct buffer *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100865
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200866 len = base64dec(txn->auth.method_data.area,
867 txn->auth.method_data.data,
868 http_auth->area, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100869
870 if (len < 0)
871 return 0;
872
873
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200874 http_auth->area[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100875
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200876 p = strchr(http_auth->area, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100877
878 if (!p)
879 return 0;
880
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200881 txn->auth.user = http_auth->area;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100882 *p = '\0';
883 txn->auth.pass = p+1;
884
885 txn->auth.method = HTTP_AUTH_BASIC;
886 return 1;
887 }
888
889 return 0;
890}
891
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100892
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200893/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
894 * conversion succeeded, 0 in case of error. If the request was already 1.X,
895 * nothing is done and 1 is returned.
896 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100897static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200898{
899 int delta;
900 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100901 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200902
903 if (msg->sl.rq.v_l != 0)
904 return 1;
905
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300906 /* RFC 1945 allows only GET for HTTP/0.9 requests */
907 if (txn->meth != HTTP_METH_GET)
908 return 0;
909
Willy Tarreauf37954d2018-06-15 18:31:02 +0200910 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200911
912 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300913 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
914 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200915 }
916 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200917 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100918 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200919 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200920 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200921 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200922 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200923 NULL, NULL);
924 if (unlikely(!cur_end))
925 return 0;
926
927 /* we have a full HTTP/1.0 request now and we know that
928 * we have either a CR or an LF at <ptr>.
929 */
930 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
931 return 1;
932}
933
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100934/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100935 * and "keep-alive" values. If we already know that some headers may safely
936 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100937 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
938 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100939 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100940 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
941 * found, and TX_CON_*_SET is adjusted depending on what is left so only
942 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100943 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100944 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100945void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100946{
Willy Tarreau5b154472009-12-21 20:11:07 +0100947 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100948 const char *hdr_val = "Connection";
949 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100950
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100951 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100952 return;
953
Willy Tarreau88d349d2010-01-25 12:15:43 +0100954 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
955 hdr_val = "Proxy-Connection";
956 hdr_len = 16;
957 }
958
Willy Tarreau5b154472009-12-21 20:11:07 +0100959 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100960 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200961 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100962 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
963 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100964 if (to_del & 2)
965 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100966 else
967 txn->flags |= TX_CON_KAL_SET;
968 }
969 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
970 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100971 if (to_del & 1)
972 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100973 else
974 txn->flags |= TX_CON_CLO_SET;
975 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100976 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
977 txn->flags |= TX_HDR_CONN_UPG;
978 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100979 }
980
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100981 txn->flags |= TX_HDR_CONN_PRS;
982 return;
983}
Willy Tarreau5b154472009-12-21 20:11:07 +0100984
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100985/* Apply desired changes on the Connection: header. Values may be removed and/or
986 * added depending on the <wanted> flags, which are exclusively composed of
987 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
988 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
989 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100990void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100991{
992 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100993 const char *hdr_val = "Connection";
994 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100995
996 ctx.idx = 0;
997
Willy Tarreau88d349d2010-01-25 12:15:43 +0100998
999 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1000 hdr_val = "Proxy-Connection";
1001 hdr_len = 16;
1002 }
1003
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001004 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +02001005 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001006 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1007 if (wanted & TX_CON_KAL_SET)
1008 txn->flags |= TX_CON_KAL_SET;
1009 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001010 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001011 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001012 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1013 if (wanted & TX_CON_CLO_SET)
1014 txn->flags |= TX_CON_CLO_SET;
1015 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001016 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001017 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001018 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001019
1020 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1021 return;
1022
1023 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1024 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001025 hdr_val = "Connection: close";
1026 hdr_len = 17;
1027 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1028 hdr_val = "Proxy-Connection: close";
1029 hdr_len = 23;
1030 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001031 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001032 }
1033
1034 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1035 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001036 hdr_val = "Connection: keep-alive";
1037 hdr_len = 22;
1038 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1039 hdr_val = "Proxy-Connection: keep-alive";
1040 hdr_len = 28;
1041 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001042 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001043 }
1044 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001045}
1046
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001047/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
1048 * value is larger than 1000, it is bound to 1000. The parser consumes up to
1049 * 1 digit, one dot and 3 digits and stops on the first invalid character.
1050 * Unparsable qvalues return 1000 as "q=1.000".
1051 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02001052int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001053{
1054 int q = 1000;
1055
Willy Tarreau506c69a2014-07-08 00:59:48 +02001056 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001057 goto out;
1058 q = (*qvalue++ - '0') * 1000;
1059
1060 if (*qvalue++ != '.')
1061 goto out;
1062
Willy Tarreau506c69a2014-07-08 00:59:48 +02001063 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001064 goto out;
1065 q += (*qvalue++ - '0') * 100;
1066
Willy Tarreau506c69a2014-07-08 00:59:48 +02001067 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001068 goto out;
1069 q += (*qvalue++ - '0') * 10;
1070
Willy Tarreau506c69a2014-07-08 00:59:48 +02001071 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001072 goto out;
1073 q += (*qvalue++ - '0') * 1;
1074 out:
1075 if (q > 1000)
1076 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02001077 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02001078 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001079 return q;
1080}
William Lallemand82fe75c2012-10-23 10:25:10 +02001081
Willy Tarreau87b09662015-04-03 00:22:06 +02001082void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001083{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001084 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001085 int tmp = TX_CON_WANT_KAL;
1086
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001087 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
1088 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001089 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
1090 tmp = TX_CON_WANT_TUN;
1091
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001092 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001093 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1094 tmp = TX_CON_WANT_TUN;
1095 }
1096
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001097 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001098 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
1099 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001100 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001101 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1102 tmp = TX_CON_WANT_CLO;
1103 else
1104 tmp = TX_CON_WANT_SCL;
1105 }
1106
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001107 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001108 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
1109 tmp = TX_CON_WANT_CLO;
1110
1111 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
1112 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
1113
1114 if (!(txn->flags & TX_HDR_CONN_PRS) &&
1115 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
1116 /* parse the Connection header and possibly clean it */
1117 int to_del = 0;
1118 if ((msg->flags & HTTP_MSGF_VER_11) ||
1119 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001120 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001121 to_del |= 2; /* remove "keep-alive" */
1122 if (!(msg->flags & HTTP_MSGF_VER_11))
1123 to_del |= 1; /* remove "close" */
1124 http_parse_connection_header(txn, msg, to_del);
1125 }
1126
1127 /* check if client or config asks for explicit close in KAL/SCL */
1128 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
1129 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
1130 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
1131 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
1132 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001133 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001134 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1135}
William Lallemand82fe75c2012-10-23 10:25:10 +02001136
Willy Tarreaud787e662009-07-07 10:14:51 +02001137/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1138 * processing can continue on next analysers, or zero if it either needs more
1139 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001140 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02001141 * when it has nothing left to do, and may remove any analyser when it wants to
1142 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001143 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001144int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001145{
Willy Tarreau59234e92008-11-30 23:51:27 +01001146 /*
1147 * We will parse the partial (or complete) lines.
1148 * We will check the request syntax, and also join multi-line
1149 * headers. An index of all the lines will be elaborated while
1150 * parsing.
1151 *
1152 * For the parsing, we use a 28 states FSM.
1153 *
1154 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02001155 * ci_head(req) = beginning of request
1156 * ci_head(req) + msg->eoh = end of processed headers / start of current one
1157 * ci_tail(req) = end of input data
1158 * msg->eol = end of current header or line (LF or CRLF)
1159 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001160 *
1161 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02001162 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02001163 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1164 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001165 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001166
Willy Tarreau59234e92008-11-30 23:51:27 +01001167 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001168 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001169 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01001170 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001171 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001172
Christopher Faulet45073512018-07-20 10:16:29 +02001173 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01001174 now_ms, __FUNCTION__,
1175 s,
1176 req,
1177 req->rex, req->wex,
1178 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001179 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +01001180 req->analysers);
1181
Willy Tarreau52a0c602009-08-16 22:45:38 +02001182 /* we're speaking HTTP here, so let's speak HTTP to the client */
1183 s->srv_error = http_return_srv_error;
1184
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001185 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +02001186 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001187 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1188
Willy Tarreau83e3af02009-12-28 17:39:57 +01001189 /* There's a protected area at the end of the buffer for rewriting
1190 * purposes. We don't want to start to parse the request if the
1191 * protected area is affected, because we may have to move processed
1192 * data later, which is much more complicated.
1193 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02001194 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02001195 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001196 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001197 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001198 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001199 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001200 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001201 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001202 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001203 return 0;
1204 }
Willy Tarreau188e2302018-06-15 11:11:53 +02001205 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001206 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001207 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001208 }
1209
Willy Tarreauf37954d2018-06-15 18:31:02 +02001210 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001211 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001212 }
1213
Willy Tarreau59234e92008-11-30 23:51:27 +01001214 /* 1: we might have to print this header in debug mode */
1215 if (unlikely((global.mode & MODE_DEBUG) &&
1216 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001217 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001218 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001219
Willy Tarreauf37954d2018-06-15 18:31:02 +02001220 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +02001221 /* this is a bit complex : in case of error on the request line,
1222 * we know that rq.l is still zero, so we display only the part
1223 * up to the end of the line (truncated by debug_hdr).
1224 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001225 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +01001226 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001227
Willy Tarreau59234e92008-11-30 23:51:27 +01001228 sol += hdr_idx_first_pos(&txn->hdr_idx);
1229 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001230
Willy Tarreau59234e92008-11-30 23:51:27 +01001231 while (cur_idx) {
1232 eol = sol + txn->hdr_idx.v[cur_idx].len;
1233 debug_hdr("clihdr", s, sol, eol);
1234 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1235 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001237 }
1238
Willy Tarreau58f10d72006-12-04 02:26:12 +01001239
Willy Tarreau59234e92008-11-30 23:51:27 +01001240 /*
1241 * Now we quickly check if we have found a full valid request.
1242 * If not so, we check the FD and buffer states before leaving.
1243 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001244 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001245 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001246 * on a keep-alive stream, if we encounter and error, close, t/o,
1247 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001248 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001249 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001250 * Last, we may increase some tracked counters' http request errors on
1251 * the cases that are deliberately the client's fault. For instance,
1252 * a timeout or connection reset is not counted as an error. However
1253 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001254 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001255
Willy Tarreau655dce92009-11-08 13:10:58 +01001256 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001257 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001258 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001259 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001260 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001261 stream_inc_http_req_ctr(s);
1262 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001263 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001264 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001265 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001266
Willy Tarreau59234e92008-11-30 23:51:27 +01001267 /* 1: Since we are in header mode, if there's no space
1268 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001269 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001270 * must terminate it now.
1271 */
Willy Tarreau23752332018-06-15 14:54:53 +02001272 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001273 /* FIXME: check if URI is set and return Status
1274 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001275 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001276 stream_inc_http_req_ctr(s);
1277 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001278 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001279 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02001280 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +01001281 goto return_bad_req;
1282 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283
Willy Tarreau59234e92008-11-30 23:51:27 +01001284 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001285 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001286 if (!(s->flags & SF_ERR_MASK))
1287 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001288
Willy Tarreaufcffa692010-01-10 14:21:19 +01001289 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001290 goto failed_keep_alive;
1291
Willy Tarreau0f228a02015-05-01 15:37:53 +02001292 if (sess->fe->options & PR_O_IGNORE_PRB)
1293 goto failed_keep_alive;
1294
Willy Tarreau59234e92008-11-30 23:51:27 +01001295 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001296 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001297 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001298 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001299 }
1300
Willy Tarreaudc979f22012-12-04 10:39:01 +01001301 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001302 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001303 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001304 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001305 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001306 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001307 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001308 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001309 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001310 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001311
Willy Tarreaue7dff022015-04-03 01:14:29 +02001312 if (!(s->flags & SF_FINST_MASK))
1313 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001314 return 0;
1315 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001316
Willy Tarreau59234e92008-11-30 23:51:27 +01001317 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001318 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001319 if (!(s->flags & SF_ERR_MASK))
1320 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001321
Willy Tarreaufcffa692010-01-10 14:21:19 +01001322 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001323 goto failed_keep_alive;
1324
Willy Tarreau0f228a02015-05-01 15:37:53 +02001325 if (sess->fe->options & PR_O_IGNORE_PRB)
1326 goto failed_keep_alive;
1327
Willy Tarreau59234e92008-11-30 23:51:27 +01001328 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001329 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001330 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001331 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001332 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001333 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001334 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001335 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001336 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001337 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001338
Willy Tarreau87b09662015-04-03 00:22:06 +02001339 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001340 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001341 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001342 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001343 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001344
Willy Tarreaue7dff022015-04-03 01:14:29 +02001345 if (!(s->flags & SF_FINST_MASK))
1346 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001347 return 0;
1348 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001349
Willy Tarreau59234e92008-11-30 23:51:27 +01001350 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001351 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001352 if (!(s->flags & SF_ERR_MASK))
1353 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001354
Willy Tarreaufcffa692010-01-10 14:21:19 +01001355 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001356 goto failed_keep_alive;
1357
Willy Tarreau0f228a02015-05-01 15:37:53 +02001358 if (sess->fe->options & PR_O_IGNORE_PRB)
1359 goto failed_keep_alive;
1360
Willy Tarreau4076a152009-04-02 15:18:36 +02001361 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001362 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001363 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001364 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001365 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001366 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001367 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001368 stream_inc_http_err_ctr(s);
1369 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001370 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001371 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001372 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001373 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001374
Willy Tarreaue7dff022015-04-03 01:14:29 +02001375 if (!(s->flags & SF_FINST_MASK))
1376 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001377 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001378 }
1379
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001380 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001381 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001382 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001383#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001384 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001385 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001386 /* We need more data, we have to re-enable quick-ack in case we
1387 * previously disabled it, otherwise we might cause the client
1388 * to delay next data.
1389 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001390 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001391 }
1392#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001393
Willy Tarreaufcffa692010-01-10 14:21:19 +01001394 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1395 /* If the client starts to talk, let's fall back to
1396 * request timeout processing.
1397 */
1398 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001399 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001400 }
1401
Willy Tarreau59234e92008-11-30 23:51:27 +01001402 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001403 if (!tick_isset(req->analyse_exp)) {
1404 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1405 (txn->flags & TX_WAIT_NEXT_RQ) &&
1406 tick_isset(s->be->timeout.httpka))
1407 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1408 else
1409 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1410 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001411
Willy Tarreau59234e92008-11-30 23:51:27 +01001412 /* we're not ready yet */
1413 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001414
1415 failed_keep_alive:
1416 /* Here we process low-level errors for keep-alive requests. In
1417 * short, if the request is not the first one and it experiences
1418 * a timeout, read error or shutdown, we just silently close so
1419 * that the client can try again.
1420 */
1421 txn->status = 0;
1422 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001423 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001424 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001425 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001426 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001427 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001428 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001429 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001430
Willy Tarreaud787e662009-07-07 10:14:51 +02001431 /* OK now we have a complete HTTP request with indexed headers. Let's
1432 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001433 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001434 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001435 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001436 * byte after the last LF. msg->sov points to the first byte of data.
1437 * msg->eol cannot be trusted because it may have been left uninitialized
1438 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001439 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001440
Willy Tarreau87b09662015-04-03 00:22:06 +02001441 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001442 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001443
Willy Tarreaub16a5742010-01-10 14:46:16 +01001444 if (txn->flags & TX_WAIT_NEXT_RQ) {
1445 /* kill the pending keep-alive timeout */
1446 txn->flags &= ~TX_WAIT_NEXT_RQ;
1447 req->analyse_exp = TICK_ETERNITY;
1448 }
1449
1450
Willy Tarreaud787e662009-07-07 10:14:51 +02001451 /* Maybe we found in invalid header name while we were configured not
1452 * to block on that, so we have to capture it now.
1453 */
1454 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001455 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001456
Willy Tarreau59234e92008-11-30 23:51:27 +01001457 /*
1458 * 1: identify the method
1459 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001460 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001461
1462 /* we can make use of server redirect on GET and HEAD */
1463 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001464 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001465 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001466 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001467 /* PRI is reserved for the HTTP/2 preface */
1468 msg->err_pos = 0;
1469 goto return_bad_req;
1470 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001471
Willy Tarreau59234e92008-11-30 23:51:27 +01001472 /*
1473 * 2: check if the URI matches the monitor_uri.
1474 * We have to do this for every request which gets in, because
1475 * the monitor-uri is defined by the frontend.
1476 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001477 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1478 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001479 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001480 sess->fe->monitor_uri,
1481 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001482 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001483 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001484 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001485 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001486
Willy Tarreaue7dff022015-04-03 01:14:29 +02001487 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001488 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001489
Willy Tarreau59234e92008-11-30 23:51:27 +01001490 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001491 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001492 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001493
Willy Tarreau59234e92008-11-30 23:51:27 +01001494 ret = acl_pass(ret);
1495 if (cond->pol == ACL_COND_UNLESS)
1496 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001497
Willy Tarreau59234e92008-11-30 23:51:27 +01001498 if (ret) {
1499 /* we fail this request, let's return 503 service unavail */
1500 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001501 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001502 if (!(s->flags & SF_ERR_MASK))
1503 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001504 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001505 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001507
Willy Tarreau59234e92008-11-30 23:51:27 +01001508 /* nothing to fail, let's reply normaly */
1509 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001510 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001511 if (!(s->flags & SF_ERR_MASK))
1512 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001513 goto return_prx_cond;
1514 }
1515
1516 /*
1517 * 3: Maybe we have to copy the original REQURI for the logs ?
1518 * Note: we cannot log anymore if the request has been
1519 * classified as invalid.
1520 */
1521 if (unlikely(s->logs.logwait & LW_REQ)) {
1522 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001523 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001524 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001526 if (urilen >= global.tune.requri_len )
1527 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001528 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001529 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001531 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001532 s->do_log(s);
1533 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001534 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001536 }
Willy Tarreau06619262006-12-17 08:37:22 +01001537
Willy Tarreau91852eb2015-05-01 13:26:00 +02001538 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1539 * exactly one digit "." one digit. This check may be disabled using
1540 * option accept-invalid-http-request.
1541 */
1542 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1543 if (msg->sl.rq.v_l != 8) {
1544 msg->err_pos = msg->sl.rq.v;
1545 goto return_bad_req;
1546 }
1547
Willy Tarreauf37954d2018-06-15 18:31:02 +02001548 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1549 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1550 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1551 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001552 msg->err_pos = msg->sl.rq.v + 4;
1553 goto return_bad_req;
1554 }
1555 }
Willy Tarreau13317662015-05-01 13:47:08 +02001556 else {
1557 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1558 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1559 goto return_bad_req;
1560 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001561
Willy Tarreau5b154472009-12-21 20:11:07 +01001562 /* ... and check if the request is HTTP/1.1 or above */
1563 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001564 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1565 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1566 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001567 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001568
1569 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001570 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01001571
Willy Tarreau88d349d2010-01-25 12:15:43 +01001572 /* if the frontend has "option http-use-proxy-header", we'll check if
1573 * we have what looks like a proxied connection instead of a connection,
1574 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1575 * Note that this is *not* RFC-compliant, however browsers and proxies
1576 * happen to do that despite being non-standard :-(
1577 * We consider that a request not beginning with either '/' or '*' is
1578 * a proxied connection, which covers both "scheme://location" and
1579 * CONNECT ip:port.
1580 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001581 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001582 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001583 txn->flags |= TX_USE_PX_CONN;
1584
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001585 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001586 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001587
Willy Tarreau59234e92008-11-30 23:51:27 +01001588 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001589 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02001590 capture_headers(ci_head(req), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001591 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001592
Willy Tarreau557f1992015-05-01 10:05:17 +02001593 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1594 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001595 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001596 * The length of a message body is determined by one of the following
1597 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001598 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001599 * 1. Any response to a HEAD request and any response with a 1xx
1600 * (Informational), 204 (No Content), or 304 (Not Modified) status
1601 * code is always terminated by the first empty line after the
1602 * header fields, regardless of the header fields present in the
1603 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001604 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001605 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1606 * the connection will become a tunnel immediately after the empty
1607 * line that concludes the header fields. A client MUST ignore any
1608 * Content-Length or Transfer-Encoding header fields received in
1609 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001610 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001611 * 3. If a Transfer-Encoding header field is present and the chunked
1612 * transfer coding (Section 4.1) is the final encoding, the message
1613 * body length is determined by reading and decoding the chunked
1614 * data until the transfer coding indicates the data is complete.
1615 *
1616 * If a Transfer-Encoding header field is present in a response and
1617 * the chunked transfer coding is not the final encoding, the
1618 * message body length is determined by reading the connection until
1619 * it is closed by the server. If a Transfer-Encoding header field
1620 * is present in a request and the chunked transfer coding is not
1621 * the final encoding, the message body length cannot be determined
1622 * reliably; the server MUST respond with the 400 (Bad Request)
1623 * status code and then close the connection.
1624 *
1625 * If a message is received with both a Transfer-Encoding and a
1626 * Content-Length header field, the Transfer-Encoding overrides the
1627 * Content-Length. Such a message might indicate an attempt to
1628 * perform request smuggling (Section 9.5) or response splitting
1629 * (Section 9.4) and ought to be handled as an error. A sender MUST
1630 * remove the received Content-Length field prior to forwarding such
1631 * a message downstream.
1632 *
1633 * 4. If a message is received without Transfer-Encoding and with
1634 * either multiple Content-Length header fields having differing
1635 * field-values or a single Content-Length header field having an
1636 * invalid value, then the message framing is invalid and the
1637 * recipient MUST treat it as an unrecoverable error. If this is a
1638 * request message, the server MUST respond with a 400 (Bad Request)
1639 * status code and then close the connection. If this is a response
1640 * message received by a proxy, the proxy MUST close the connection
1641 * to the server, discard the received response, and send a 502 (Bad
1642 * Gateway) response to the client. If this is a response message
1643 * received by a user agent, the user agent MUST close the
1644 * connection to the server and discard the received response.
1645 *
1646 * 5. If a valid Content-Length header field is present without
1647 * Transfer-Encoding, its decimal value defines the expected message
1648 * body length in octets. If the sender closes the connection or
1649 * the recipient times out before the indicated number of octets are
1650 * received, the recipient MUST consider the message to be
1651 * incomplete and close the connection.
1652 *
1653 * 6. If this is a request message and none of the above are true, then
1654 * the message body length is zero (no message body is present).
1655 *
1656 * 7. Otherwise, this is a response message without a declared message
1657 * body length, so the message body length is determined by the
1658 * number of octets received prior to the server closing the
1659 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001660 */
1661
Willy Tarreau32b47f42009-10-18 20:55:02 +02001662 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001663 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001664 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001665 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001666 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001667 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001668 /* chunked not last, return badreq */
1669 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001670 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001671 }
1672
Willy Tarreau1c913912015-04-30 10:57:51 +02001673 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001674 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001675 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001676 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001677 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1678 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001679 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001680 signed long long cl;
1681
Willy Tarreauad14f752011-09-02 20:33:27 +02001682 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001683 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001684 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001685 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001686
Willy Tarreauad14f752011-09-02 20:33:27 +02001687 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001688 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001689 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001690 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001691
Willy Tarreauad14f752011-09-02 20:33:27 +02001692 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001693 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001694 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001695 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001696
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001697 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001698 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001699 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001700 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001701
Christopher Fauletbe821b92017-03-30 11:21:53 +02001702 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001703 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001704 }
1705
Willy Tarreau34dfc602015-05-01 10:09:49 +02001706 /* even bodyless requests have a known length */
1707 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001708
Willy Tarreau179085c2014-04-28 16:48:56 +02001709 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1710 * only change if both the request and the config reference something else.
1711 * Option httpclose by itself sets tunnel mode where headers are mangled.
1712 * However, if another mode is set, it will affect it (eg: server-close/
1713 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1714 * if FE and BE have the same settings (common). The method consists in
1715 * checking if options changed between the two calls (implying that either
1716 * one is non-null, or one of them is non-null and we are there for the first
1717 * time.
1718 */
1719 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001720 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001721 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001722
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001723 /* we may have to wait for the request's body */
1724 if ((s->be->options & PR_O_WREQ_BODY) &&
1725 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1726 req->analysers |= AN_REQ_HTTP_BODY;
1727
Willy Tarreau83ece462017-12-21 15:13:09 +01001728 /*
1729 * RFC7234#4:
1730 * A cache MUST write through requests with methods
1731 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1732 * the origin server; i.e., a cache is not allowed
1733 * to generate a reply to such a request before
1734 * having forwarded the request and having received
1735 * a corresponding response.
1736 *
1737 * RFC7231#4.2.1:
1738 * Of the request methods defined by this
1739 * specification, the GET, HEAD, OPTIONS, and TRACE
1740 * methods are defined to be safe.
1741 */
1742 if (likely(txn->meth == HTTP_METH_GET ||
1743 txn->meth == HTTP_METH_HEAD ||
1744 txn->meth == HTTP_METH_OPTIONS ||
1745 txn->meth == HTTP_METH_TRACE))
1746 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1747
Willy Tarreaud787e662009-07-07 10:14:51 +02001748 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001749 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001750 req->analyse_exp = TICK_ETERNITY;
1751 return 1;
1752
1753 return_bad_req:
1754 /* We centralize bad requests processing here */
1755 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1756 /* we detected a parsing error. We want to archive this request
1757 * in the dedicated proxy area for later troubleshooting.
1758 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001759 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001760 }
1761
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001762 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001763 txn->req.msg_state = HTTP_MSG_ERROR;
1764 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001765 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001766
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001767 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001768 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001769 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001770
1771 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001772 if (!(s->flags & SF_ERR_MASK))
1773 s->flags |= SF_ERR_PRXCOND;
1774 if (!(s->flags & SF_FINST_MASK))
1775 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001776
Christopher Faulet0184ea72017-01-05 14:06:34 +01001777 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001778 req->analyse_exp = TICK_ETERNITY;
1779 return 0;
1780}
1781
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001782
Willy Tarreau347a35d2013-11-22 17:51:09 +01001783/* This function prepares an applet to handle the stats. It can deal with the
1784 * "100-continue" expectation, check that admin rules are met for POST requests,
1785 * and program a response message if something was unexpected. It cannot fail
1786 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001787 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001788 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001789 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001790 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001791int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001792{
1793 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001794 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001795 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001796 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001797 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001798 struct uri_auth *uri_auth = s->be->uri_auth;
1799 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001800 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001801
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001802 appctx = si_appctx(si);
1803 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1804 appctx->st1 = appctx->st2 = 0;
1805 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1806 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001807 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001808 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001809
Willy Tarreauf37954d2018-06-15 18:31:02 +02001810 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001811 lookup = uri + uri_auth->uri_len;
1812
1813 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1814 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001815 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001816 break;
1817 }
1818 }
1819
1820 if (uri_auth->refresh) {
1821 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1822 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001823 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001824 break;
1825 }
1826 }
1827 }
1828
1829 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1830 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001831 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001832 break;
1833 }
1834 }
1835
Willy Tarreau1e62df92016-01-11 18:57:53 +01001836 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1837 if (memcmp(h, ";typed", 6) == 0) {
1838 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1839 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1840 break;
1841 }
1842 }
1843
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001844 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1845 if (memcmp(h, ";st=", 4) == 0) {
1846 int i;
1847 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001848 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001849 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1850 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001851 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001852 break;
1853 }
1854 }
1855 break;
1856 }
1857 }
1858
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001859 appctx->ctx.stats.scope_str = 0;
1860 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001861 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1862 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1863 int itx = 0;
1864 const char *h2;
1865 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1866 const char *err;
1867
1868 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1869 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001870 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001871 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1872 itx++;
1873 h++;
1874 }
1875
1876 if (itx > STAT_SCOPE_TXT_MAXLEN)
1877 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001878 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001879
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001880 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001881 memcpy(scope_txt, h2, itx);
1882 scope_txt[itx] = '\0';
1883 err = invalid_char(scope_txt);
1884 if (err) {
1885 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001886 appctx->ctx.stats.scope_str = 0;
1887 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001888 }
1889 break;
1890 }
1891 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001892
1893 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001894 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001895 int ret = 1;
1896
1897 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001898 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001899 ret = acl_pass(ret);
1900 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1901 ret = !ret;
1902 }
1903
1904 if (ret) {
1905 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001906 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001907 break;
1908 }
1909 }
1910
1911 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001912 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001913 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001914 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001915 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1916 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001917 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001918 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001919 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001920 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1921 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001922 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001923 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001924 else {
1925 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001926 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001927 }
1928
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001929 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001930 return 1;
1931}
1932
Lukas Tribus67db8df2013-06-23 17:37:13 +02001933/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1934 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1935 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001936void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001937{
1938#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001939 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001940 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1941#endif
1942#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001943 if (from->ss_family == AF_INET6) {
1944 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001945 /* v4-mapped addresses need IP_TOS */
1946 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1947 else
1948 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1949 }
1950#endif
1951}
1952
Willy Tarreau87b09662015-04-03 00:22:06 +02001953int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001954 const char* name, unsigned int name_len,
1955 const char *str, struct my_regex *re,
1956 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001957{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001958 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001959 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001960 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001961 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1962 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001963 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001964
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001965 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001966
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001967 /* Choose the header browsing function. */
1968 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001969 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001970 http_find_hdr_func = http_find_header2;
1971 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001972 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001973 http_find_hdr_func = http_find_full_header2;
1974 break;
1975 default: /* impossible */
1976 return -1;
1977 }
1978
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001979 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1980 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001981 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001982 char *val = ctx.line + ctx.val;
1983 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001984
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001985 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1986 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001987
Willy Tarreau6e27be12018-08-22 04:46:47 +02001988 len = exp_replace(output->area, output->size, val, str, pmatch);
1989 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001990 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001991
Willy Tarreau6e27be12018-08-22 04:46:47 +02001992 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001993
1994 hdr->len += delta;
1995 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001996
1997 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001998 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001999 }
2000
2001 return 0;
2002}
2003
Willy Tarreau87b09662015-04-03 00:22:06 +02002004static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002005 const char* name, unsigned int name_len,
2006 struct list *fmt, struct my_regex *re,
2007 int action)
2008{
Willy Tarreau83061a82018-07-13 11:56:34 +02002009 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002010 int ret = -1;
2011
2012 replace = alloc_trash_chunk();
2013 if (!replace)
2014 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002015
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002016 replace->data = build_logline(s, replace->area, replace->size, fmt);
2017 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002018 goto leave;
2019
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002020 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
2021 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002022
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002023 leave:
2024 free_trash_chunk(replace);
2025 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002026}
2027
Willy Tarreau87b09662015-04-03 00:22:06 +02002028/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002029 * transaction <txn>. Returns the verdict of the first rule that prevents
2030 * further processing of the request (auth, deny, ...), and defaults to
2031 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2032 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002033 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2034 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2035 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002036 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002037enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002038http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002039{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002040 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002041 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002042 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002043 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002044 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002045 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002046 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002047 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002048
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002049 /* If "the current_rule_list" match the executed rule list, we are in
2050 * resume condition. If a resume is needed it is always in the action
2051 * and never in the ACL or converters. In this case, we initialise the
2052 * current rule, and go to the action execution point.
2053 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002054 if (s->current_rule) {
2055 rule = s->current_rule;
2056 s->current_rule = NULL;
2057 if (s->current_rule_list == rules)
2058 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002059 }
2060 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002061
Willy Tarreauff011f22011-01-06 17:51:27 +01002062 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002063
Willy Tarreau96257ec2012-12-27 10:46:37 +01002064 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002065 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002066 int ret;
2067
Willy Tarreau192252e2015-04-04 01:47:55 +02002068 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002069 ret = acl_pass(ret);
2070
Willy Tarreauff011f22011-01-06 17:51:27 +01002071 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002072 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002073
2074 if (!ret) /* condition not matched */
2075 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002076 }
2077
Willy Tarreauacc98002015-09-27 23:34:39 +02002078 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002079resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002080 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002081 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002082 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002083
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002084 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002085 if (deny_status)
2086 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002087 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002088
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002089 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002090 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002091 if (deny_status)
2092 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002093 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002094
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002095 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002096 /* Auth might be performed on regular http-req rules as well as on stats */
2097 auth_realm = rule->arg.auth.realm;
2098 if (!auth_realm) {
2099 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2100 auth_realm = STATS_DEFAULT_REALM;
2101 else
2102 auth_realm = px->id;
2103 }
2104 /* send 401/407 depending on whether we use a proxy or not. We still
2105 * count one error, because normal browsing won't significantly
2106 * increase the counter but brute force attempts will.
2107 */
2108 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2109 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002110 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002111 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002112 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002113
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002114 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002115 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2116 return HTTP_RULE_RES_BADREQ;
2117 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002118
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002119 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002120 s->task->nice = rule->arg.nice;
2121 break;
2122
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002123 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002124 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002125 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002126 break;
2127
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002128 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002129#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002130 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002131 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002132#endif
2133 break;
2134
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002135 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002136 s->logs.level = rule->arg.loglevel;
2137 break;
2138
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002139 case ACT_HTTP_REPLACE_HDR:
2140 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002141 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2142 rule->arg.hdr_add.name_len,
2143 &rule->arg.hdr_add.fmt,
2144 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002145 return HTTP_RULE_RES_BADREQ;
2146 break;
2147
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002148 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002149 ctx.idx = 0;
2150 /* remove all occurrences of the header */
2151 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002152 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002153 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002154 }
Willy Tarreau85603282015-01-21 20:39:27 +01002155 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002156
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002157 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002158 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002159 /* The scope of the trash buffer must be limited to this function. The
2160 * build_logline() function can execute a lot of other function which
2161 * can use the trash buffer. So for limiting the scope of this global
2162 * buffer, we build first the header value using build_logline, and
2163 * after we store the header name.
2164 */
Willy Tarreau83061a82018-07-13 11:56:34 +02002165 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002166
2167 replace = alloc_trash_chunk();
2168 if (!replace)
2169 return HTTP_RULE_RES_BADREQ;
2170
Thierry Fournier4b788f72016-06-01 13:35:36 +02002171 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002172 len += build_logline(s, replace->area + len,
2173 replace->size - len,
2174 &rule->arg.hdr_add.fmt);
2175 memcpy(replace->area, rule->arg.hdr_add.name,
2176 rule->arg.hdr_add.name_len);
2177 replace->area[rule->arg.hdr_add.name_len] = ':';
2178 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
2179 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002180
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002181 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002182 /* remove all occurrences of the header */
2183 ctx.idx = 0;
2184 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002185 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002186 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2187 }
2188 }
2189
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002190 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002191 static unsigned char rate_limit = 0;
2192
2193 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002194 replace->area[rule->arg.hdr_add.name_len] = 0;
2195 send_log(px, LOG_WARNING, "Proxy %s failed to add or set the request header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id,
2196 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002197 }
2198
2199 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2200 if (sess->fe != s->be)
2201 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2202 if (sess->listener->counters)
2203 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2204 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002205
2206 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002207 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002208 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002209
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002210 case ACT_HTTP_DEL_ACL:
2211 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002212 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002213 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002214
2215 /* collect reference */
2216 ref = pat_ref_lookup(rule->arg.map.ref);
2217 if (!ref)
2218 continue;
2219
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002220 /* allocate key */
2221 key = alloc_trash_chunk();
2222 if (!key)
2223 return HTTP_RULE_RES_BADREQ;
2224
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002225 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002226 key->data = build_logline(s, key->area, key->size,
2227 &rule->arg.map.key);
2228 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002229
2230 /* perform update */
2231 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002232 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002233 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002234 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002235
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002236 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002237 break;
2238 }
2239
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002240 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002241 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002242 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002243
2244 /* collect reference */
2245 ref = pat_ref_lookup(rule->arg.map.ref);
2246 if (!ref)
2247 continue;
2248
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002249 /* allocate key */
2250 key = alloc_trash_chunk();
2251 if (!key)
2252 return HTTP_RULE_RES_BADREQ;
2253
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002254 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002255 key->data = build_logline(s, key->area, key->size,
2256 &rule->arg.map.key);
2257 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002258
2259 /* perform update */
2260 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002261 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002262 if (pat_ref_find_elt(ref, key->area) == NULL)
2263 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002264 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002265
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002266 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002267 break;
2268 }
2269
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002270 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002271 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002272 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002273
2274 /* collect reference */
2275 ref = pat_ref_lookup(rule->arg.map.ref);
2276 if (!ref)
2277 continue;
2278
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002279 /* allocate key */
2280 key = alloc_trash_chunk();
2281 if (!key)
2282 return HTTP_RULE_RES_BADREQ;
2283
2284 /* allocate value */
2285 value = alloc_trash_chunk();
2286 if (!value) {
2287 free_trash_chunk(key);
2288 return HTTP_RULE_RES_BADREQ;
2289 }
2290
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002291 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002292 key->data = build_logline(s, key->area, key->size,
2293 &rule->arg.map.key);
2294 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002295
2296 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002297 value->data = build_logline(s, value->area,
2298 value->size,
2299 &rule->arg.map.value);
2300 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002301
2302 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002303 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002304 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002305 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002306 else
2307 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002308 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002309
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002310 free_trash_chunk(key);
2311 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002312 break;
2313 }
William Lallemand73025dd2014-04-24 14:38:37 +02002314
Thierry FOURNIER42148732015-09-02 17:17:33 +02002315 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002316 if ((s->req.flags & CF_READ_ERROR) ||
2317 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2318 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2319 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002320 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002321
Willy Tarreauacc98002015-09-27 23:34:39 +02002322 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002323 case ACT_RET_ERR:
2324 case ACT_RET_CONT:
2325 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002326 case ACT_RET_STOP:
2327 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002328 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002329 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002330 return HTTP_RULE_RES_YIELD;
2331 }
William Lallemand73025dd2014-04-24 14:38:37 +02002332 break;
2333
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002334 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002335 /* Note: only the first valid tracking parameter of each
2336 * applies.
2337 */
2338
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002339 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002340 struct stktable *t;
2341 struct stksess *ts;
2342 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002343 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002344
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002345 t = rule->arg.trk_ctr.table.t;
2346 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
Willy Tarreau09448f72014-06-25 18:12:15 +02002347
2348 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002349 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002350
2351 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002352 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2353 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2354 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002355 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002356
Emeric Brun819fc6f2017-06-13 19:37:32 +02002357 if (ptr1)
2358 stktable_data_cast(ptr1, http_req_cnt)++;
2359
2360 if (ptr2)
2361 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2362 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2363
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002364 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002365
2366 /* If data was modified, we need to touch to re-schedule sync */
2367 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002368 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002369
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002370 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002371 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002372 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002373 }
2374 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002375 break;
2376
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002377 /* other flags exists, but normaly, they never be matched. */
2378 default:
2379 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002380 }
2381 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002382
2383 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002384 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002385}
2386
Willy Tarreau71241ab2012-12-27 11:30:54 +01002387
Willy Tarreau51d861a2015-05-22 17:30:48 +02002388/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2389 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2390 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2391 * is returned, the process can continue the evaluation of next rule list. If
2392 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2393 * is returned, it means the operation could not be processed and a server error
2394 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2395 * deny rule. If *YIELD is returned, the caller must call again the function
2396 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002397 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002398static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002399http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002400{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002401 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002402 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002403 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002404 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002405 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002406 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002407
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002408 /* If "the current_rule_list" match the executed rule list, we are in
2409 * resume condition. If a resume is needed it is always in the action
2410 * and never in the ACL or converters. In this case, we initialise the
2411 * current rule, and go to the action execution point.
2412 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002413 if (s->current_rule) {
2414 rule = s->current_rule;
2415 s->current_rule = NULL;
2416 if (s->current_rule_list == rules)
2417 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002418 }
2419 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002420
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002421 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002422
2423 /* check optional condition */
2424 if (rule->cond) {
2425 int ret;
2426
Willy Tarreau192252e2015-04-04 01:47:55 +02002427 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002428 ret = acl_pass(ret);
2429
2430 if (rule->cond->pol == ACL_COND_UNLESS)
2431 ret = !ret;
2432
2433 if (!ret) /* condition not matched */
2434 continue;
2435 }
2436
Willy Tarreauacc98002015-09-27 23:34:39 +02002437 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002438resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002439 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002440 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002441 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002442
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002443 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002444 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002445 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002446
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002447 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002448 s->task->nice = rule->arg.nice;
2449 break;
2450
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002451 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002452 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002453 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002454 break;
2455
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002456 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002457#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002458 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002459 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002460#endif
2461 break;
2462
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002463 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002464 s->logs.level = rule->arg.loglevel;
2465 break;
2466
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002467 case ACT_HTTP_REPLACE_HDR:
2468 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002469 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2470 rule->arg.hdr_add.name_len,
2471 &rule->arg.hdr_add.fmt,
2472 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002473 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002474 break;
2475
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002476 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002477 ctx.idx = 0;
2478 /* remove all occurrences of the header */
2479 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002480 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002481 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2482 }
Willy Tarreau85603282015-01-21 20:39:27 +01002483 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002484
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002485 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002486 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002487 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002488
2489 replace = alloc_trash_chunk();
2490 if (!replace)
2491 return HTTP_RULE_RES_BADREQ;
2492
2493 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002494 memcpy(replace->area, rule->arg.hdr_add.name,
2495 rule->arg.hdr_add.name_len);
2496 replace->data = rule->arg.hdr_add.name_len;
2497 replace->area[replace->data++] = ':';
2498 replace->area[replace->data++] = ' ';
2499 replace->data += build_logline(s,
2500 replace->area + replace->data,
2501 replace->size - replace->data,
2502 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002503
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002504 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002505 /* remove all occurrences of the header */
2506 ctx.idx = 0;
2507 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002508 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002509 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2510 }
2511 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002512
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002513 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002514 static unsigned char rate_limit = 0;
2515
2516 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002517 replace->area[rule->arg.hdr_add.name_len] = 0;
2518 send_log(px, LOG_WARNING, "Proxy %s failed to add or set the response header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id,
2519 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002520 }
2521
2522 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2523 if (sess->fe != s->be)
2524 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2525 if (sess->listener->counters)
2526 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2527 if (objt_server(s->target))
2528 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2529 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002530
2531 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002532 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002533 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002534
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002535 case ACT_HTTP_DEL_ACL:
2536 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002537 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002538 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002539
2540 /* collect reference */
2541 ref = pat_ref_lookup(rule->arg.map.ref);
2542 if (!ref)
2543 continue;
2544
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002545 /* allocate key */
2546 key = alloc_trash_chunk();
2547 if (!key)
2548 return HTTP_RULE_RES_BADREQ;
2549
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002550 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002551 key->data = build_logline(s, key->area, key->size,
2552 &rule->arg.map.key);
2553 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002554
2555 /* perform update */
2556 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002557 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002558 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002559 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002560
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002561 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002562 break;
2563 }
2564
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002565 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002566 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002567 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002568
2569 /* collect reference */
2570 ref = pat_ref_lookup(rule->arg.map.ref);
2571 if (!ref)
2572 continue;
2573
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002574 /* allocate key */
2575 key = alloc_trash_chunk();
2576 if (!key)
2577 return HTTP_RULE_RES_BADREQ;
2578
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002579 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002580 key->data = build_logline(s, key->area, key->size,
2581 &rule->arg.map.key);
2582 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002583
2584 /* perform update */
2585 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002586 if (pat_ref_find_elt(ref, key->area) == NULL)
2587 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002588
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002589 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002590 break;
2591 }
2592
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002593 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002594 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002595 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002596
2597 /* collect reference */
2598 ref = pat_ref_lookup(rule->arg.map.ref);
2599 if (!ref)
2600 continue;
2601
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002602 /* allocate key */
2603 key = alloc_trash_chunk();
2604 if (!key)
2605 return HTTP_RULE_RES_BADREQ;
2606
2607 /* allocate value */
2608 value = alloc_trash_chunk();
2609 if (!value) {
2610 free_trash_chunk(key);
2611 return HTTP_RULE_RES_BADREQ;
2612 }
2613
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002614 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002615 key->data = build_logline(s, key->area, key->size,
2616 &rule->arg.map.key);
2617 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002618
2619 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002620 value->data = build_logline(s, value->area,
2621 value->size,
2622 &rule->arg.map.value);
2623 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002624
2625 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002626 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002627 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002628 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002629 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002630 else
2631 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002632 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002633 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002634 free_trash_chunk(key);
2635 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002636 break;
2637 }
William Lallemand73025dd2014-04-24 14:38:37 +02002638
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002639 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02002640 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2641 return HTTP_RULE_RES_BADREQ;
2642 return HTTP_RULE_RES_DONE;
2643
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002644 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2645 /* Note: only the first valid tracking parameter of each
2646 * applies.
2647 */
2648
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002649 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002650 struct stktable *t;
2651 struct stksess *ts;
2652 struct stktable_key *key;
2653 void *ptr;
2654
2655 t = rule->arg.trk_ctr.table.t;
2656 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2657
2658 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002659 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002660
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002661 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002662
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002663 /* let's count a new HTTP request as it's the first time we do it */
2664 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2665 if (ptr)
2666 stktable_data_cast(ptr, http_req_cnt)++;
2667
2668 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2669 if (ptr)
2670 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2671 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2672
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002673 /* When the client triggers a 4xx from the server, it's most often due
2674 * to a missing object or permission. These events should be tracked
2675 * because if they happen often, it may indicate a brute force or a
2676 * vulnerability scan. Normally this is done when receiving the response
2677 * but here we're tracking after this ought to have been done so we have
2678 * to do it on purpose.
2679 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002680 if ((unsigned)(txn->status - 400) < 100) {
2681 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2682 if (ptr)
2683 stktable_data_cast(ptr, http_err_cnt)++;
2684
2685 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2686 if (ptr)
2687 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2688 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2689 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002690
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002691 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002692
Emeric Brun0fed0b02017-11-29 16:15:07 +01002693 /* If data was modified, we need to touch to re-schedule sync */
2694 stktable_touch_local(t, ts, 0);
2695
Emeric Brun819fc6f2017-06-13 19:37:32 +02002696 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2697 if (sess->fe != s->be)
2698 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2699
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002700 }
2701 }
2702 break;
2703
Thierry FOURNIER42148732015-09-02 17:17:33 +02002704 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002705 if ((s->req.flags & CF_READ_ERROR) ||
2706 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2707 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2708 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002709 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002710
Willy Tarreauacc98002015-09-27 23:34:39 +02002711 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002712 case ACT_RET_ERR:
2713 case ACT_RET_CONT:
2714 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002715 case ACT_RET_STOP:
2716 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002717 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002718 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002719 return HTTP_RULE_RES_YIELD;
2720 }
William Lallemand73025dd2014-04-24 14:38:37 +02002721 break;
2722
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002723 /* other flags exists, but normaly, they never be matched. */
2724 default:
2725 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002726 }
2727 }
2728
2729 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002730 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002731}
2732
2733
Willy Tarreau71241ab2012-12-27 11:30:54 +01002734/* Perform an HTTP redirect based on the information in <rule>. The function
2735 * returns non-zero on success, or zero in case of a, irrecoverable error such
2736 * as too large a request to build a valid response.
2737 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002738static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002739{
Willy Tarreaub329a312015-05-22 16:27:37 +02002740 struct http_msg *req = &txn->req;
2741 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002742 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002743 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002744 int ret = 0;
2745
2746 chunk = alloc_trash_chunk();
2747 if (!chunk)
2748 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002749
2750 /* build redirect message */
2751 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002752 case 308:
2753 msg_fmt = HTTP_308;
2754 break;
2755 case 307:
2756 msg_fmt = HTTP_307;
2757 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002758 case 303:
2759 msg_fmt = HTTP_303;
2760 break;
2761 case 301:
2762 msg_fmt = HTTP_301;
2763 break;
2764 case 302:
2765 default:
2766 msg_fmt = HTTP_302;
2767 break;
2768 }
2769
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002770 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2771 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002772
2773 switch(rule->type) {
2774 case REDIRECT_TYPE_SCHEME: {
2775 const char *path;
2776 const char *host;
2777 struct hdr_ctx ctx;
2778 int pathlen;
2779 int hostlen;
2780
2781 host = "";
2782 hostlen = 0;
2783 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002784 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002785 host = ctx.line + ctx.val;
2786 hostlen = ctx.vlen;
2787 }
2788
Willy Tarreau6b952c82018-09-10 17:45:34 +02002789 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002790 /* build message using path */
2791 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002792 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002793 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2794 int qs = 0;
2795 while (qs < pathlen) {
2796 if (path[qs] == '?') {
2797 pathlen = qs;
2798 break;
2799 }
2800 qs++;
2801 }
2802 }
2803 } else {
2804 path = "/";
2805 pathlen = 1;
2806 }
2807
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002808 if (rule->rdr_str) { /* this is an old "redirect" rule */
2809 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002810 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002811 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002812
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002813 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002814 memcpy(chunk->area + chunk->data, rule->rdr_str,
2815 rule->rdr_len);
2816 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002817 }
2818 else {
2819 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002820 chunk->data += build_logline(s,
2821 chunk->area + chunk->data,
2822 chunk->size - chunk->data,
2823 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002824
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002825 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002826 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002827 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002828 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002829 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002830 memcpy(chunk->area + chunk->data, "://", 3);
2831 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002832
2833 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002834 memcpy(chunk->area + chunk->data, host, hostlen);
2835 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002836
2837 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002838 memcpy(chunk->area + chunk->data, path, pathlen);
2839 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002840
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002841 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002842 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002843 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002844 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002845 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002846 chunk->area[chunk->data] = '/';
2847 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002848 }
2849
2850 break;
2851 }
2852 case REDIRECT_TYPE_PREFIX: {
2853 const char *path;
2854 int pathlen;
2855
Willy Tarreau6b952c82018-09-10 17:45:34 +02002856 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002857 /* build message using path */
2858 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002859 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002860 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2861 int qs = 0;
2862 while (qs < pathlen) {
2863 if (path[qs] == '?') {
2864 pathlen = qs;
2865 break;
2866 }
2867 qs++;
2868 }
2869 }
2870 } else {
2871 path = "/";
2872 pathlen = 1;
2873 }
2874
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002875 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002876 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002877 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002878
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002879 /* add prefix. Note that if prefix == "/", we don't want to
2880 * add anything, otherwise it makes it hard for the user to
2881 * configure a self-redirection.
2882 */
2883 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002884 memcpy(chunk->area + chunk->data,
2885 rule->rdr_str, rule->rdr_len);
2886 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002887 }
2888 }
2889 else {
2890 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002891 chunk->data += build_logline(s,
2892 chunk->area + chunk->data,
2893 chunk->size - chunk->data,
2894 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002895
2896 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002897 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002898 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002899 }
2900
2901 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002902 memcpy(chunk->area + chunk->data, path, pathlen);
2903 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002904
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002905 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002906 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002907 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002908 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002909 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002910 chunk->area[chunk->data] = '/';
2911 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002912 }
2913
2914 break;
2915 }
2916 case REDIRECT_TYPE_LOCATION:
2917 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002918 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002919 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002920 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002921
2922 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002923 memcpy(chunk->area + chunk->data, rule->rdr_str,
2924 rule->rdr_len);
2925 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002926 }
2927 else {
2928 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002929 chunk->data += build_logline(s,
2930 chunk->area + chunk->data,
2931 chunk->size - chunk->data,
2932 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002933
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002934 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002935 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002936 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002937 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002938 break;
2939 }
2940
2941 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002942 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2943 chunk->data += 14;
2944 memcpy(chunk->area + chunk->data, rule->cookie_str,
2945 rule->cookie_len);
2946 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002947 }
2948
Willy Tarreau19b14122017-02-28 09:48:11 +01002949 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002950 txn->status = rule->code;
2951 /* let's log the request time */
2952 s->logs.tv_request = now;
2953
Christopher Fauletbe821b92017-03-30 11:21:53 +02002954 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002955 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2956 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2957 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002958 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002959 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002960 memcpy(chunk->area + chunk->data,
2961 "\r\nProxy-Connection: keep-alive", 30);
2962 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002963 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002964 memcpy(chunk->area + chunk->data,
2965 "\r\nConnection: keep-alive", 24);
2966 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002967 }
2968 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002969 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2970 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002971 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002972 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002973 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002974 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002975 req->next -= req->sov;
2976 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002977 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002978 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002979 req->msg_state = HTTP_MSG_CLOSED;
2980 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002981 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002982 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002983 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002984 /* let the server side turn to SI_ST_CLO */
2985 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002986 } else {
2987 /* keep-alive not possible */
2988 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002989 memcpy(chunk->area + chunk->data,
2990 "\r\nProxy-Connection: close\r\n\r\n", 29);
2991 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002992 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002993 memcpy(chunk->area + chunk->data,
2994 "\r\nConnection: close\r\n\r\n", 23);
2995 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002996 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002997 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002998 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002999 }
3000
Willy Tarreaue7dff022015-04-03 01:14:29 +02003001 if (!(s->flags & SF_ERR_MASK))
3002 s->flags |= SF_ERR_LOCAL;
3003 if (!(s->flags & SF_FINST_MASK))
3004 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003005
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003006 ret = 1;
3007 leave:
3008 free_trash_chunk(chunk);
3009 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003010}
3011
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003012/* This stream analyser runs all HTTP request processing which is common to
3013 * frontends and backends, which means blocking ACLs, filters, connection-close,
3014 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003015 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003016 * either needs more data or wants to immediately abort the request (eg: deny,
3017 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003018 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003019int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003020{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003021 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003022 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003023 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003024 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003025 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003026 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003027 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003028 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003029
Willy Tarreau655dce92009-11-08 13:10:58 +01003030 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003031 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003032 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003033 }
3034
Christopher Faulet45073512018-07-20 10:16:29 +02003035 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02003036 now_ms, __FUNCTION__,
3037 s,
3038 req,
3039 req->rex, req->wex,
3040 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003041 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02003042 req->analysers);
3043
Willy Tarreau65410832014-04-28 21:25:43 +02003044 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003045 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003046
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003047 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003048 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003049 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003050
Willy Tarreau0b748332014-04-29 00:13:29 +02003051 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003052 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3053 goto return_prx_yield;
3054
Willy Tarreau0b748332014-04-29 00:13:29 +02003055 case HTTP_RULE_RES_CONT:
3056 case HTTP_RULE_RES_STOP: /* nothing to do */
3057 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003058
Willy Tarreau0b748332014-04-29 00:13:29 +02003059 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3060 if (txn->flags & TX_CLTARPIT)
3061 goto tarpit;
3062 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003063
Willy Tarreau0b748332014-04-29 00:13:29 +02003064 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3065 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003066
Willy Tarreau0b748332014-04-29 00:13:29 +02003067 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003068 goto done;
3069
Willy Tarreau0b748332014-04-29 00:13:29 +02003070 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3071 goto return_bad_req;
3072 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003073 }
3074
Olivier Houchard25ae45a2017-11-29 19:51:19 +01003075 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
3076 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003077 struct hdr_ctx ctx;
3078
3079 ctx.idx = 0;
3080 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02003081 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003082 if (unlikely(http_header_add_tail2(&txn->req,
3083 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02003084 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02003085 goto return_bad_req;
3086 }
3087 }
3088
3089 }
3090
Willy Tarreau52542592014-04-28 18:33:26 +02003091 /* OK at this stage, we know that the request was accepted according to
3092 * the http-request rules, we can check for the stats. Note that the
3093 * URI is detected *before* the req* rules in order not to be affected
3094 * by a possible reqrep, while they are processed *after* so that a
3095 * reqdeny can still block them. This clearly needs to change in 1.6!
3096 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003097 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003098 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003099 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003100 txn->status = 500;
3101 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003102 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003103
Willy Tarreaue7dff022015-04-03 01:14:29 +02003104 if (!(s->flags & SF_ERR_MASK))
3105 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003106 goto return_prx_cond;
3107 }
3108
3109 /* parse the whole stats request and extract the relevant information */
3110 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003111 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003112 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003113
Willy Tarreau0b748332014-04-29 00:13:29 +02003114 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3115 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003116
Willy Tarreau0b748332014-04-29 00:13:29 +02003117 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3118 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003119 }
3120
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003121 /* evaluate the req* rules except reqadd */
3122 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003123 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003124 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003125
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003126 if (txn->flags & TX_CLDENY)
3127 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003128
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003129 if (txn->flags & TX_CLTARPIT) {
3130 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003131 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003132 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003133 }
Willy Tarreau06619262006-12-17 08:37:22 +01003134
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003135 /* add request headers from the rule sets in the same order */
3136 list_for_each_entry(wl, &px->req_add, list) {
3137 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003138 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003139 ret = acl_pass(ret);
3140 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3141 ret = !ret;
3142 if (!ret)
3143 continue;
3144 }
3145
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003146 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003147 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003148 }
3149
Willy Tarreau52542592014-04-28 18:33:26 +02003150
3151 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01003152 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
3153 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003154 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003155 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003156 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003157
Willy Tarreaue7dff022015-04-03 01:14:29 +02003158 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3159 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3160 if (!(s->flags & SF_FINST_MASK))
3161 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003162
Willy Tarreau70730dd2014-04-24 18:06:27 +02003163 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003164 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3165 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003166 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003167 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003168 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003169
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003170 /* check whether we have some ACLs set to redirect this request */
3171 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003172 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003173 int ret;
3174
Willy Tarreau192252e2015-04-04 01:47:55 +02003175 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003176 ret = acl_pass(ret);
3177 if (rule->cond->pol == ACL_COND_UNLESS)
3178 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003179 if (!ret)
3180 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003181 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003182 if (!http_apply_redirect_rule(rule, s, txn))
3183 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003184 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003185 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003186
Willy Tarreau2be39392010-01-03 17:24:51 +01003187 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3188 * If this happens, then the data will not come immediately, so we must
3189 * send all what we have without waiting. Note that due to the small gain
3190 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003191 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003192 * itself once used.
3193 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003194 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003195
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003196 done: /* done with this analyser, continue with next ones that the calling
3197 * points will have set, if any.
3198 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003199 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003200 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3201 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003202 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003203
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003204 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003205 /* Allow cookie logging
3206 */
3207 if (s->be->cookie_name || sess->fe->capture_name)
3208 manage_client_side_cookies(s, req);
3209
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003210 /* When a connection is tarpitted, we use the tarpit timeout,
3211 * which may be the same as the connect timeout if unspecified.
3212 * If unset, then set it to zero because we really want it to
3213 * eventually expire. We build the tarpit as an analyser.
3214 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003215 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003216
3217 /* wipe the request out so that we can drop the connection early
3218 * if the client closes first.
3219 */
3220 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003221
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003222 txn->status = http_err_codes[deny_status];
3223
Christopher Faulet0184ea72017-01-05 14:06:34 +01003224 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003225 req->analysers |= AN_REQ_HTTP_TARPIT;
3226 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3227 if (!req->analyse_exp)
3228 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003229 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003230 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003231 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003232 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003233 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003234 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003235 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003236
3237 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003238
3239 /* Allow cookie logging
3240 */
3241 if (s->be->cookie_name || sess->fe->capture_name)
3242 manage_client_side_cookies(s, req);
3243
Willy Tarreau0b748332014-04-29 00:13:29 +02003244 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003245 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003246 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003247 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003248 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003249 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003250 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003251 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003252 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003253 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003254 goto return_prx_cond;
3255
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003256 return_bad_req:
3257 /* We centralize bad requests processing here */
3258 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3259 /* we detected a parsing error. We want to archive this request
3260 * in the dedicated proxy area for later troubleshooting.
3261 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003262 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003263 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003264
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003265 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003266 txn->req.msg_state = HTTP_MSG_ERROR;
3267 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003268 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003269
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003270 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003271 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003272 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003273
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003274 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003275 if (!(s->flags & SF_ERR_MASK))
3276 s->flags |= SF_ERR_PRXCOND;
3277 if (!(s->flags & SF_FINST_MASK))
3278 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003279
Christopher Faulet0184ea72017-01-05 14:06:34 +01003280 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003281 req->analyse_exp = TICK_ETERNITY;
3282 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003283
3284 return_prx_yield:
3285 channel_dont_connect(req);
3286 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003287}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003288
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003289/* This function performs all the processing enabled for the current request.
3290 * It returns 1 if the processing can continue on next analysers, or zero if it
3291 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003292 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003293 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003294int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003295{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003296 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003297 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003298 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003299 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003300
Willy Tarreau655dce92009-11-08 13:10:58 +01003301 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003302 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003303 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003304 return 0;
3305 }
3306
Christopher Faulet45073512018-07-20 10:16:29 +02003307 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003308 now_ms, __FUNCTION__,
3309 s,
3310 req,
3311 req->rex, req->wex,
3312 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003313 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003314 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003315
Willy Tarreau59234e92008-11-30 23:51:27 +01003316 /*
3317 * Right now, we know that we have processed the entire headers
3318 * and that unwanted requests have been filtered out. We can do
3319 * whatever we want with the remaining request. Also, now we
3320 * may have separate values for ->fe, ->be.
3321 */
Willy Tarreau06619262006-12-17 08:37:22 +01003322
Willy Tarreau59234e92008-11-30 23:51:27 +01003323 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003324 * If HTTP PROXY is set we simply get remote server address parsing
3325 * incoming request. Note that this requires that a connection is
3326 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003327 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003328 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003329 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003330 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003331
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003332 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003333 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003334 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003335 txn->req.msg_state = HTTP_MSG_ERROR;
3336 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003337 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003338 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003339
Willy Tarreaue7dff022015-04-03 01:14:29 +02003340 if (!(s->flags & SF_ERR_MASK))
3341 s->flags |= SF_ERR_RESOURCE;
3342 if (!(s->flags & SF_FINST_MASK))
3343 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003344
3345 return 0;
3346 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003347
Willy Tarreau6b952c82018-09-10 17:45:34 +02003348 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003349 if (url2sa(ci_head(req) + msg->sl.rq.u,
3350 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003351 &conn->addr.to, NULL) == -1)
3352 goto return_bad_req;
3353
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003354 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003355 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3356 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003357 * u_l characters by a single "/".
3358 */
3359 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003360 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003361 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3362 int delta;
3363
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003364 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003365 http_msg_move_end(&txn->req, delta);
3366 cur_end += delta;
3367 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3368 goto return_bad_req;
3369 }
3370 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003371 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003372 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3373 int delta;
3374
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003375 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003376 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003377 http_msg_move_end(&txn->req, delta);
3378 cur_end += delta;
3379 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3380 goto return_bad_req;
3381 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003382 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003383
Willy Tarreau59234e92008-11-30 23:51:27 +01003384 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003385 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003386 * Note that doing so might move headers in the request, but
3387 * the fields will stay coherent and the URI will not move.
3388 * This should only be performed in the backend.
3389 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003390 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003391 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003392
William Lallemanda73203e2012-03-12 12:48:57 +01003393 /* add unique-id if "header-unique-id" is specified */
3394
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003395 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003396 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003397 goto return_bad_req;
3398 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003399 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003400 }
William Lallemanda73203e2012-03-12 12:48:57 +01003401
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003402 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003403 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003404 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003405 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003406 goto return_bad_req;
3407 }
3408
Cyril Bontéb21570a2009-11-29 20:04:48 +01003409 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003410 * 9: add X-Forwarded-For if either the frontend or the backend
3411 * asks for it.
3412 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003413 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003414 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003415 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3416 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3417 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003418 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003419 /* The header is set to be added only if none is present
3420 * and we found it, so don't do anything.
3421 */
3422 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003423 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003424 /* Add an X-Forwarded-For header unless the source IP is
3425 * in the 'except' network range.
3426 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003427 if ((!sess->fe->except_mask.s_addr ||
3428 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3429 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003430 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003431 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003432 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003433 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003434 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003435 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003436
3437 /* Note: we rely on the backend to get the header name to be used for
3438 * x-forwarded-for, because the header is really meant for the backends.
3439 * However, if the backend did not specify any option, we have to rely
3440 * on the frontend's header name.
3441 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003442 if (s->be->fwdfor_hdr_len) {
3443 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003444 memcpy(trash.area,
3445 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003446 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003447 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003448 memcpy(trash.area,
3449 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003450 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003451 len += snprintf(trash.area + len,
3452 trash.size - len,
3453 ": %d.%d.%d.%d", pn[0], pn[1],
3454 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003455
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003456 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003457 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003458 }
3459 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003460 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003461 /* FIXME: for the sake of completeness, we should also support
3462 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003463 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003464 int len;
3465 char pn[INET6_ADDRSTRLEN];
3466 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003467 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003468 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003469
Willy Tarreau59234e92008-11-30 23:51:27 +01003470 /* Note: we rely on the backend to get the header name to be used for
3471 * x-forwarded-for, because the header is really meant for the backends.
3472 * However, if the backend did not specify any option, we have to rely
3473 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003474 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003475 if (s->be->fwdfor_hdr_len) {
3476 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003477 memcpy(trash.area, s->be->fwdfor_hdr_name,
3478 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003479 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003480 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003481 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3482 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003483 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003484 len += snprintf(trash.area + len, trash.size - len,
3485 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003486
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003487 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003488 goto return_bad_req;
3489 }
3490 }
3491
3492 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003493 * 10: add X-Original-To if either the frontend or the backend
3494 * asks for it.
3495 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003496 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003497
3498 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003499 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003500 /* Add an X-Original-To header unless the destination IP is
3501 * in the 'except' network range.
3502 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003503 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003504
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003505 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003506 ((!sess->fe->except_mask_to.s_addr ||
3507 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3508 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003509 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003510 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003511 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003512 int len;
3513 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003514 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003515
3516 /* Note: we rely on the backend to get the header name to be used for
3517 * x-original-to, because the header is really meant for the backends.
3518 * However, if the backend did not specify any option, we have to rely
3519 * on the frontend's header name.
3520 */
3521 if (s->be->orgto_hdr_len) {
3522 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003523 memcpy(trash.area,
3524 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003525 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003526 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003527 memcpy(trash.area,
3528 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003529 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003530 len += snprintf(trash.area + len,
3531 trash.size - len,
3532 ": %d.%d.%d.%d", pn[0], pn[1],
3533 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003534
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003535 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003536 goto return_bad_req;
3537 }
3538 }
3539 }
3540
Willy Tarreau50fc7772012-11-11 22:19:57 +01003541 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3542 * If an "Upgrade" token is found, the header is left untouched in order not to have
3543 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3544 * "Upgrade" is present in the Connection header.
3545 */
3546 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3547 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003548 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003549 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003550 unsigned int want_flags = 0;
3551
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003552 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003553 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003554 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003555 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003556 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003557 want_flags |= TX_CON_CLO_SET;
3558 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003559 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003560 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003561 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003562 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003563 want_flags |= TX_CON_KAL_SET;
3564 }
3565
3566 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003567 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003568 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003569
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003570
Willy Tarreau522d6c02009-12-06 18:49:18 +01003571 /* If we have no server assigned yet and we're balancing on url_param
3572 * with a POST request, we may be interested in checking the body for
3573 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003574 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003575 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003576 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003577 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003578 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003579 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003580 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003581
Christopher Fauletbe821b92017-03-30 11:21:53 +02003582 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3583 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003584#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003585 /* We expect some data from the client. Unless we know for sure
3586 * we already have a full request, we have to re-enable quick-ack
3587 * in case we previously disabled it, otherwise we might cause
3588 * the client to delay further data.
3589 */
3590 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3591 cli_conn && conn_ctrl_ready(cli_conn) &&
3592 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003593 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003594 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003595#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003596
Willy Tarreau59234e92008-11-30 23:51:27 +01003597 /*************************************************************
3598 * OK, that's finished for the headers. We have done what we *
3599 * could. Let's switch to the DATA state. *
3600 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003601 req->analyse_exp = TICK_ETERNITY;
3602 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003603
Willy Tarreau59234e92008-11-30 23:51:27 +01003604 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003605 /* OK let's go on with the BODY now */
3606 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003607
Willy Tarreau59234e92008-11-30 23:51:27 +01003608 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003609 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003610 /* we detected a parsing error. We want to archive this request
3611 * in the dedicated proxy area for later troubleshooting.
3612 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003613 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003614 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003615
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003616 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003617 txn->req.msg_state = HTTP_MSG_ERROR;
3618 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003619 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003620 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003621
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003622 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003623 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003624 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003625
Willy Tarreaue7dff022015-04-03 01:14:29 +02003626 if (!(s->flags & SF_ERR_MASK))
3627 s->flags |= SF_ERR_PRXCOND;
3628 if (!(s->flags & SF_FINST_MASK))
3629 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003630 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003631}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003632
Willy Tarreau60b85b02008-11-30 23:28:40 +01003633/* This function is an analyser which processes the HTTP tarpit. It always
3634 * returns zero, at the beginning because it prevents any other processing
3635 * from occurring, and at the end because it terminates the request.
3636 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003637int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003638{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003639 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003640
3641 /* This connection is being tarpitted. The CLIENT side has
3642 * already set the connect expiration date to the right
3643 * timeout. We just have to check that the client is still
3644 * there and that the timeout has not expired.
3645 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003646 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003647 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003648 !tick_is_expired(req->analyse_exp, now_ms))
3649 return 0;
3650
3651 /* We will set the queue timer to the time spent, just for
3652 * logging purposes. We fake a 500 server error, so that the
3653 * attacker will not suspect his connection has been tarpitted.
3654 * It will not cause trouble to the logs because we can exclude
3655 * the tarpitted connections by filtering on the 'PT' status flags.
3656 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003657 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3658
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003659 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003660 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003661
Christopher Faulet0184ea72017-01-05 14:06:34 +01003662 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003663 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003664
Willy Tarreaue7dff022015-04-03 01:14:29 +02003665 if (!(s->flags & SF_ERR_MASK))
3666 s->flags |= SF_ERR_PRXCOND;
3667 if (!(s->flags & SF_FINST_MASK))
3668 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003669 return 0;
3670}
3671
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003672/* This function is an analyser which waits for the HTTP request body. It waits
3673 * for either the buffer to be full, or the full advertised contents to have
3674 * reached the buffer. It must only be called after the standard HTTP request
3675 * processing has occurred, because it expects the request to be parsed and will
3676 * look for the Expect header. It may send a 100-Continue interim response. It
3677 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3678 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3679 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003680 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003681int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003682{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003683 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003684 struct http_txn *txn = s->txn;
3685 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003686
3687 /* We have to parse the HTTP request body to find any required data.
3688 * "balance url_param check_post" should have been the only way to get
3689 * into this. We were brought here after HTTP header analysis, so all
3690 * related structures are ready.
3691 */
3692
Willy Tarreau890988f2014-04-10 11:59:33 +02003693 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3694 /* This is the first call */
3695 if (msg->msg_state < HTTP_MSG_BODY)
3696 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003697
Willy Tarreau890988f2014-04-10 11:59:33 +02003698 if (msg->msg_state < HTTP_MSG_100_SENT) {
3699 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3700 * send an HTTP/1.1 100 Continue intermediate response.
3701 */
3702 if (msg->flags & HTTP_MSGF_VER_11) {
3703 struct hdr_ctx ctx;
3704 ctx.idx = 0;
3705 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003706 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003707 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003708 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003709 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003710 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003711 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003712 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003713 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003714
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003715 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003716 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003717 * must save the body in msg->next because it survives buffer
3718 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003719 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003720 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003721
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003722 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003723 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3724 else
3725 msg->msg_state = HTTP_MSG_DATA;
3726 }
3727
Willy Tarreau890988f2014-04-10 11:59:33 +02003728 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3729 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003730 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003731 goto missing_data;
3732
3733 /* OK we have everything we need now */
3734 goto http_end;
3735 }
3736
3737 /* OK here we're parsing a chunked-encoded message */
3738
Willy Tarreau522d6c02009-12-06 18:49:18 +01003739 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003740 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003741 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003742 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003743 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003744 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003745 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003746
Willy Tarreau115acb92009-12-26 13:56:06 +01003747 if (!ret)
3748 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003749 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003750 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003751 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003752 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003753 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003754 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003755 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003756
3757 msg->chunk_len = chunk;
3758 msg->body_len += chunk;
3759
3760 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003761 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003762 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003763 }
3764
Willy Tarreaud98cf932009-12-27 22:54:55 +01003765 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003766 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3767 * for at least a whole chunk or the whole content length bytes after
3768 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003769 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003770 if (msg->msg_state == HTTP_MSG_TRAILERS)
3771 goto http_end;
3772
Willy Tarreaue115b492015-05-01 23:05:14 +02003773 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003774 goto http_end;
3775
3776 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003777 /* we get here if we need to wait for more data. If the buffer is full,
3778 * we have the maximum we can expect.
3779 */
Willy Tarreau23752332018-06-15 14:54:53 +02003780 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003781 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003782
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003783 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003784 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003785 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003786
Willy Tarreaue7dff022015-04-03 01:14:29 +02003787 if (!(s->flags & SF_ERR_MASK))
3788 s->flags |= SF_ERR_CLITO;
3789 if (!(s->flags & SF_FINST_MASK))
3790 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003791 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003792 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003793
3794 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003795 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003796 /* Not enough data. We'll re-use the http-request
3797 * timeout here. Ideally, we should set the timeout
3798 * relative to the accept() date. We just set the
3799 * request timeout once at the beginning of the
3800 * request.
3801 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003802 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003803 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003804 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003805 return 0;
3806 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003807
3808 http_end:
3809 /* The situation will not evolve, so let's give up on the analysis. */
3810 s->logs.tv_request = now; /* update the request timer to reflect full request */
3811 req->analysers &= ~an_bit;
3812 req->analyse_exp = TICK_ETERNITY;
3813 return 1;
3814
3815 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003816 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003817 txn->req.msg_state = HTTP_MSG_ERROR;
3818 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003819 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003820
Willy Tarreaue7dff022015-04-03 01:14:29 +02003821 if (!(s->flags & SF_ERR_MASK))
3822 s->flags |= SF_ERR_PRXCOND;
3823 if (!(s->flags & SF_FINST_MASK))
3824 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003825
Willy Tarreau522d6c02009-12-06 18:49:18 +01003826 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003827 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003828 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003829 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003830 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003831 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003832}
3833
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003834/* send a server's name with an outgoing request over an established connection.
3835 * Note: this function is designed to be called once the request has been scheduled
3836 * for being forwarded. This is the reason why it rewinds the buffer before
3837 * proceeding.
3838 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003839int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003840
3841 struct hdr_ctx ctx;
3842
Mark Lamourinec2247f02012-01-04 13:02:01 -05003843 char *hdr_name = be->server_id_hdr_name;
3844 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003845 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003846 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003847 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003848
William Lallemandd9e90662012-01-30 17:27:17 +01003849 ctx.idx = 0;
3850
Willy Tarreau211cdec2014-04-17 20:18:08 +02003851 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003852 if (old_o) {
3853 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003854 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003855 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003856 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003857 }
3858
Willy Tarreauf37954d2018-06-15 18:31:02 +02003859 old_i = ci_data(chn);
3860 while (http_find_header2(hdr_name, hdr_name_len, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003861 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003862 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003863 }
3864
3865 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003866 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003867 memcpy(hdr_val, hdr_name, hdr_name_len);
3868 hdr_val += hdr_name_len;
3869 *hdr_val++ = ':';
3870 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003871 hdr_val += strlcpy2(hdr_val, srv_name,
3872 trash.area + trash.size - hdr_val);
3873 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3874 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003875
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003876 if (old_o) {
3877 /* If this was a forwarded request, we must readjust the amount of
3878 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003879 * variations. Note that the current state is >= HTTP_MSG_BODY,
3880 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003881 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003882 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003883 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003884 txn->req.next -= old_o;
3885 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003886 }
3887
Mark Lamourinec2247f02012-01-04 13:02:01 -05003888 return 0;
3889}
3890
Willy Tarreau610ecce2010-01-04 21:15:02 +01003891/* Terminate current transaction and prepare a new one. This is very tricky
3892 * right now but it works.
3893 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003894void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003895{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003896 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003897 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003898 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003899 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003900 struct connection *srv_conn;
3901 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003902 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003903
Willy Tarreau610ecce2010-01-04 21:15:02 +01003904 /* FIXME: We need a more portable way of releasing a backend's and a
3905 * server's connections. We need a safer way to reinitialize buffer
3906 * flags. We also need a more accurate method for computing per-request
3907 * data.
3908 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003909 /*
3910 * XXX cognet: This is probably wrong, this is killing a whole
3911 * connection, in the new world order, we probably want to just kill
3912 * the stream, this is to be revisited the day we handle multiple
3913 * streams in one server connection.
3914 */
3915 cs = objt_cs(s->si[1].end);
3916 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003917
Willy Tarreau4213a112013-12-15 10:25:42 +01003918 /* unless we're doing keep-alive, we want to quickly close the connection
3919 * to the server.
3920 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003921 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003922 !si_conn_ready(&s->si[1])) {
3923 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3924 si_shutr(&s->si[1]);
3925 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003926 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003927
Willy Tarreaue7dff022015-04-03 01:14:29 +02003928 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003929 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003930 if (unlikely(s->srv_conn))
3931 sess_change_server(s, NULL);
3932 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003933
3934 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003935 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003936
Willy Tarreaueee5b512015-04-03 23:46:31 +02003937 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003938 int n;
3939
Willy Tarreaueee5b512015-04-03 23:46:31 +02003940 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003941 if (n < 1 || n > 5)
3942 n = 0;
3943
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003944 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003945 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003946 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003947 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003948 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003949 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3950 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003951 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003952 }
3953
3954 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003955 s->logs.bytes_in -= ci_data(&s->req);
3956 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003957
Willy Tarreau66425e32018-07-25 06:55:12 +02003958 /* we may need to know the position in the queue */
3959 pendconn_free(s);
3960
Willy Tarreau610ecce2010-01-04 21:15:02 +01003961 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003962 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003963 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003964 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003965 s->do_log(s);
3966 }
3967
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003968 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003969 stream_stop_content_counters(s);
3970 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003971
Willy Tarreau610ecce2010-01-04 21:15:02 +01003972 s->logs.accept_date = date; /* user-visible date for logging */
3973 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003974 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3975 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003976 tv_zero(&s->logs.tv_request);
3977 s->logs.t_queue = -1;
3978 s->logs.t_connect = -1;
3979 s->logs.t_data = -1;
3980 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003981 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3982 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003983
Willy Tarreauf37954d2018-06-15 18:31:02 +02003984 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3985 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003986
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003987 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003988 if (s->flags & SF_CURR_SESS) {
3989 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003990 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003991 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003992 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003993 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003994 }
3995
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003996 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003997
Willy Tarreau4213a112013-12-15 10:25:42 +01003998 /* only release our endpoint if we don't intend to reuse the
3999 * connection.
4000 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004001 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004002 !si_conn_ready(&s->si[1])) {
4003 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004004 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004005 }
4006
Willy Tarreau350f4872014-11-28 14:42:25 +01004007 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4008 s->si[1].err_type = SI_ET_NONE;
4009 s->si[1].conn_retries = 0; /* used for logging too */
4010 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004011 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004012 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
Christopher Fauletc5a9d5b2017-11-09 09:36:43 +01004013 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA|CF_WRITE_EVENT);
Willy Tarreaue7dff022015-04-03 01:14:29 +02004014 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4015 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4016 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004017
Patrick Hemmere3faf022018-08-22 10:02:00 -04004018 hlua_ctx_destroy(s->hlua);
4019 s->hlua = NULL;
4020
Willy Tarreaueee5b512015-04-03 23:46:31 +02004021 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004023 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004024
4025 if (prev_status == 401 || prev_status == 407) {
4026 /* In HTTP keep-alive mode, if we receive a 401, we still have
4027 * a chance of being able to send the visitor again to the same
4028 * server over the same connection. This is required by some
4029 * broken protocols such as NTLM, and anyway whenever there is
4030 * an opportunity for sending the challenge to the proper place,
4031 * it's better to do it (at least it helps with debugging).
4032 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004033 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004034 if (srv_conn)
4035 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004036 }
4037
Willy Tarreau53f96852016-02-02 18:50:47 +01004038 /* Never ever allow to reuse a connection from a non-reuse backend */
4039 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4040 srv_conn->flags |= CO_FL_PRIVATE;
4041
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004042 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004043 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004044
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004045 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004046 s->req.flags |= CF_NEVER_WAIT;
4047 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004048 }
4049
Willy Tarreau714ea782015-11-25 20:11:11 +01004050 /* we're removing the analysers, we MUST re-enable events detection.
4051 * We don't enable close on the response channel since it's either
4052 * already closed, or in keep-alive with an idle connection handler.
4053 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004054 channel_auto_read(&s->req);
4055 channel_auto_close(&s->req);
4056 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004057
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004058 /* we're in keep-alive with an idle connection, monitor it if not already done */
4059 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004060 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004061 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004062 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004063 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004064 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004065 else if (prev_flags & TX_NOT_FIRST)
4066 /* note: we check the request, not the connection, but
4067 * this is valid for strategies SAFE and AGGR, and in
4068 * case of ALWS, we don't care anyway.
4069 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004070 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004071 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02004072 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004073 }
Christopher Faulete6006242017-03-10 11:52:44 +01004074 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4075 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004076}
4077
4078
4079/* This function updates the request state machine according to the response
4080 * state machine and buffer flags. It returns 1 if it changes anything (flag
4081 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4082 * it is only used to find when a request/response couple is complete. Both
4083 * this function and its equivalent should loop until both return zero. It
4084 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4085 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004086int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004087{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004088 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004089 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004090 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004091 unsigned int old_state = txn->req.msg_state;
4092
Christopher Faulet4be98032017-07-18 10:48:24 +02004093 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004094 return 0;
4095
4096 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004097 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004098 * We can shut the read side unless we want to abort_on_close,
4099 * or we have a POST request. The issue with POST requests is
4100 * that some browsers still send a CRLF after the request, and
4101 * this CRLF must be read so that it does not remain in the kernel
4102 * buffers, otherwise a close could cause an RST on some systems
4103 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004104 * Note that if we're using keep-alive on the client side, we'd
4105 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004106 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004107 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004108 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004109 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4110 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004111 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4112 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01004113 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004114 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004115
Willy Tarreau40f151a2012-12-20 12:10:09 +01004116 /* if the server closes the connection, we want to immediately react
4117 * and close the socket to save packets and syscalls.
4118 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004119 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004120
Willy Tarreau7f876a12015-11-18 11:59:55 +01004121 /* In any case we've finished parsing the request so we must
4122 * disable Nagle when sending data because 1) we're not going
4123 * to shut this side, and 2) the server is waiting for us to
4124 * send pending data.
4125 */
4126 chn->flags |= CF_NEVER_WAIT;
4127
Willy Tarreau610ecce2010-01-04 21:15:02 +01004128 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4129 goto wait_other_side;
4130
4131 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4132 /* The server has not finished to respond, so we
4133 * don't want to move in order not to upset it.
4134 */
4135 goto wait_other_side;
4136 }
4137
Willy Tarreau610ecce2010-01-04 21:15:02 +01004138 /* When we get here, it means that both the request and the
4139 * response have finished receiving. Depending on the connection
4140 * mode, we'll have to wait for the last bytes to leave in either
4141 * direction, and sometimes for a close to be effective.
4142 */
4143
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004144 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4145 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004146 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4147 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004148 }
4149 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4150 /* Option forceclose is set, or either side wants to close,
4151 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004152 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004153 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004154 *
4155 * However, there is an exception if the response
4156 * length is undefined. In this case, we need to wait
4157 * the close from the server. The response will be
4158 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004159 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004160 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4161 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4162 goto check_channel_flags;
4163
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004164 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4165 channel_shutr_now(chn);
4166 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004167 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004168 }
4169 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004170 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4171 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004172 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004173 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4174 channel_auto_read(chn);
4175 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004176 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004177 }
4178
Christopher Faulet4be98032017-07-18 10:48:24 +02004179 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004180 }
4181
4182 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4183 http_msg_closing:
4184 /* nothing else to forward, just waiting for the output buffer
4185 * to be empty and for the shutw_now to take effect.
4186 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004187 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004188 txn->req.msg_state = HTTP_MSG_CLOSED;
4189 goto http_msg_closed;
4190 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004191 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004192 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004193 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004194 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004195 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004196 }
4197
4198 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4199 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004200 /* if we don't know whether the server will close, we need to hard close */
4201 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4202 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4203
Willy Tarreau3988d932013-12-27 23:03:08 +01004204 /* see above in MSG_DONE why we only do this in these states */
4205 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4206 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004207 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4208 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004209 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004210 goto wait_other_side;
4211 }
4212
Christopher Faulet4be98032017-07-18 10:48:24 +02004213 check_channel_flags:
4214 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4215 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4216 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004217 txn->req.msg_state = HTTP_MSG_CLOSING;
4218 goto http_msg_closing;
4219 }
4220
4221
Willy Tarreau610ecce2010-01-04 21:15:02 +01004222 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004223 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004224}
4225
4226
4227/* This function updates the response state machine according to the request
4228 * state machine and buffer flags. It returns 1 if it changes anything (flag
4229 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4230 * it is only used to find when a request/response couple is complete. Both
4231 * this function and its equivalent should loop until both return zero. It
4232 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4233 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004234int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004235{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004236 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004237 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004238 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004239 unsigned int old_state = txn->rsp.msg_state;
4240
Christopher Faulet4be98032017-07-18 10:48:24 +02004241 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004242 return 0;
4243
4244 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4245 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004246 * still monitor the server connection for a possible close
4247 * while the request is being uploaded, so we don't disable
4248 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004249 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004250 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004251
4252 if (txn->req.msg_state == HTTP_MSG_ERROR)
4253 goto wait_other_side;
4254
4255 if (txn->req.msg_state < HTTP_MSG_DONE) {
4256 /* The client seems to still be sending data, probably
4257 * because we got an error response during an upload.
4258 * We have the choice of either breaking the connection
4259 * or letting it pass through. Let's do the later.
4260 */
4261 goto wait_other_side;
4262 }
4263
Willy Tarreau610ecce2010-01-04 21:15:02 +01004264 /* When we get here, it means that both the request and the
4265 * response have finished receiving. Depending on the connection
4266 * mode, we'll have to wait for the last bytes to leave in either
4267 * direction, and sometimes for a close to be effective.
4268 */
4269
4270 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4271 /* Server-close mode : shut read and wait for the request
4272 * side to close its output buffer. The caller will detect
4273 * when we're in DONE and the other is in CLOSED and will
4274 * catch that for the final cleanup.
4275 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004276 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4277 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004278 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004279 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4280 /* Option forceclose is set, or either side wants to close,
4281 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004282 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004283 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004284 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004285 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004286 channel_shutr_now(chn);
4287 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004288 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004289 }
4290 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004291 /* The last possible modes are keep-alive and tunnel. Tunnel will
4292 * need to forward remaining data. Keep-alive will need to monitor
4293 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004294 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004295 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004296 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004297 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4298 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004299 }
4300
Christopher Faulet4be98032017-07-18 10:48:24 +02004301 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004302 }
4303
4304 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4305 http_msg_closing:
4306 /* nothing else to forward, just waiting for the output buffer
4307 * to be empty and for the shutw_now to take effect.
4308 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004309 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004310 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4311 goto http_msg_closed;
4312 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004313 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004314 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004315 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004316 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004317 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004318 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004319 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004320 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004321 }
4322
4323 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4324 http_msg_closed:
4325 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004326 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004327 channel_auto_close(chn);
4328 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004329 goto wait_other_side;
4330 }
4331
Christopher Faulet4be98032017-07-18 10:48:24 +02004332 check_channel_flags:
4333 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4334 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4335 /* if we've just closed an output, let's switch */
4336 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4337 goto http_msg_closing;
4338 }
4339
Willy Tarreau610ecce2010-01-04 21:15:02 +01004340 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004341 /* We force the response to leave immediately if we're waiting for the
4342 * other side, since there is no pending shutdown to push it out.
4343 */
4344 if (!channel_is_empty(chn))
4345 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004346 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004347}
4348
4349
Christopher Faulet894da4c2017-07-18 11:29:07 +02004350/* Resync the request and response state machines. */
4351void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004352{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004353 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004354#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004355 int old_req_state = txn->req.msg_state;
4356 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004357#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004358
Willy Tarreau610ecce2010-01-04 21:15:02 +01004359 http_sync_req_state(s);
4360 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004361 if (!http_sync_res_state(s))
4362 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004363 if (!http_sync_req_state(s))
4364 break;
4365 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004366
Christopher Faulet894da4c2017-07-18 11:29:07 +02004367 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4368 "req->analysers=0x%08x res->analysers=0x%08x\n",
4369 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004370 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4371 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004372 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004373
4374
Willy Tarreau610ecce2010-01-04 21:15:02 +01004375 /* OK, both state machines agree on a compatible state.
4376 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004377 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4378 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004379 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4380 * means we must abort the request.
4381 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4382 * corresponding channel.
4383 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4384 * on the response with server-close mode means we've completed one
4385 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004386 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004387 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4388 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004389 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004390 channel_auto_close(&s->req);
4391 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004392 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004393 channel_auto_close(&s->res);
4394 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004395 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004396 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4397 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004398 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004399 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004400 channel_auto_close(&s->res);
4401 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004402 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004403 channel_abort(&s->req);
4404 channel_auto_close(&s->req);
4405 channel_auto_read(&s->req);
4406 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004407 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004408 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4409 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4410 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4411 s->req.analysers &= AN_REQ_FLT_END;
4412 if (HAS_REQ_DATA_FILTERS(s))
4413 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4414 }
4415 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4416 s->res.analysers &= AN_RES_FLT_END;
4417 if (HAS_RSP_DATA_FILTERS(s))
4418 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4419 }
4420 channel_auto_close(&s->req);
4421 channel_auto_read(&s->req);
4422 channel_auto_close(&s->res);
4423 channel_auto_read(&s->res);
4424 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004425 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4426 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004427 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004428 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4429 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4430 /* server-close/keep-alive: terminate this transaction,
4431 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004432 * a fresh-new transaction, but only once we're sure there's
4433 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004434 * another request. They must not hold any pending output data
4435 * and the response buffer must realigned
4436 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004437 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004438 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004439 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004440 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004441 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004442 else {
4443 s->req.analysers = AN_REQ_FLT_END;
4444 s->res.analysers = AN_RES_FLT_END;
4445 txn->flags |= TX_WAIT_CLEANUP;
4446 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004447 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004448}
4449
Willy Tarreaud98cf932009-12-27 22:54:55 +01004450/* This function is an analyser which forwards request body (including chunk
4451 * sizes if any). It is called as soon as we must forward, even if we forward
4452 * zero byte. The only situation where it must not be called is when we're in
4453 * tunnel mode and we want to forward till the close. It's used both to forward
4454 * remaining data and to resync after end of body. It expects the msg_state to
4455 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004456 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004457 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004458 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004459 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004460int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004461{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004462 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004463 struct http_txn *txn = s->txn;
4464 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004465 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004466
Christopher Faulet45073512018-07-20 10:16:29 +02004467 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Christopher Faulet814d2702017-03-30 11:33:44 +02004468 now_ms, __FUNCTION__,
4469 s,
4470 req,
4471 req->rex, req->wex,
4472 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004473 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004474 req->analysers);
4475
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004476 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4477 return 0;
4478
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004479 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004480 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004481 /* Output closed while we were sending data. We must abort and
4482 * wake the other side up.
4483 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004484 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004485 msg->msg_state = HTTP_MSG_ERROR;
4486 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004487 return 1;
4488 }
4489
Willy Tarreaud98cf932009-12-27 22:54:55 +01004490 /* Note that we don't have to send 100-continue back because we don't
4491 * need the data to complete our job, and it's up to the server to
4492 * decide whether to return 100, 417 or anything else in return of
4493 * an "Expect: 100-continue" header.
4494 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004495 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004496 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4497 ? HTTP_MSG_CHUNK_SIZE
4498 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004499
4500 /* TODO/filters: when http-buffer-request option is set or if a
4501 * rule on url_param exists, the first chunk size could be
4502 * already parsed. In that case, msg->next is after the chunk
4503 * size (including the CRLF after the size). So this case should
4504 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004505 }
4506
Willy Tarreau7ba23542014-04-17 21:50:00 +02004507 /* Some post-connect processing might want us to refrain from starting to
4508 * forward data. Currently, the only reason for this is "balance url_param"
4509 * whichs need to parse/process the request after we've enabled forwarding.
4510 */
4511 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004512 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004513 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004514 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004515 channel_dont_close(req); /* don't fail on early shutr */
4516 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004517 }
4518 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4519 }
4520
Willy Tarreau80a92c02014-03-12 10:41:13 +01004521 /* in most states, we should abort in case of early close */
4522 channel_auto_close(req);
4523
Willy Tarreauefdf0942014-04-24 20:08:57 +02004524 if (req->to_forward) {
4525 /* We can't process the buffer's contents yet */
4526 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004527 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004528 }
4529
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004530 if (msg->msg_state < HTTP_MSG_DONE) {
4531 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4532 ? http_msg_forward_chunked_body(s, msg)
4533 : http_msg_forward_body(s, msg));
4534 if (!ret)
4535 goto missing_data_or_waiting;
4536 if (ret < 0)
4537 goto return_bad_req;
4538 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004539
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004540 /* other states, DONE...TUNNEL */
4541 /* we don't want to forward closes on DONE except in tunnel mode. */
4542 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4543 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004544
Christopher Faulet894da4c2017-07-18 11:29:07 +02004545 http_resync_states(s);
4546 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004547 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4548 if (req->flags & CF_SHUTW) {
4549 /* request errors are most likely due to the
4550 * server aborting the transfer. */
4551 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004552 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004553 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004554 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004555 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004556 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004557 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004558 }
4559
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004560 /* If "option abortonclose" is set on the backend, we want to monitor
4561 * the client's connection and forward any shutdown notification to the
4562 * server, which will decide whether to close or to go on processing the
4563 * request. We only do that in tunnel mode, and not in other modes since
4564 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004565 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004566 channel_auto_read(req);
4567 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4568 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4569 s->si[1].flags |= SI_FL_NOLINGER;
4570 channel_auto_close(req);
4571 }
4572 else if (s->txn->meth == HTTP_METH_POST) {
4573 /* POST requests may require to read extra CRLF sent by broken
4574 * browsers and which could cause an RST to be sent upon close
4575 * on some systems (eg: Linux). */
4576 channel_auto_read(req);
4577 }
4578 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004579
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004580 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004581 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004582 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004583 if (!(s->flags & SF_ERR_MASK))
4584 s->flags |= SF_ERR_CLICL;
4585 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004586 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004587 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004588 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004589 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004590 }
4591
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004592 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4593 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004594 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004595 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004596
4597 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004598 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004599
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004600 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004601 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004602 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004603 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004604
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004605 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004606 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004607 * And when content-length is used, we never want to let the possible
4608 * shutdown be forwarded to the other side, as the state machine will
4609 * take care of it once the client responds. It's also important to
4610 * prevent TIME_WAITs from accumulating on the backend side, and for
4611 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004612 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004613 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004614 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004615
Willy Tarreau5c620922011-05-11 19:56:11 +02004616 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004617 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004618 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004619 * modes are already handled by the stream sock layer. We must not do
4620 * this in content-length mode because it could present the MSG_MORE
4621 * flag with the last block of forwarded data, which would cause an
4622 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004623 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004624 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004625 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004626
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004627 return 0;
4628
Willy Tarreaud98cf932009-12-27 22:54:55 +01004629 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004630 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004631 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004632 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004633
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004634 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004635 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004636 txn->req.msg_state = HTTP_MSG_ERROR;
4637 if (txn->status) {
4638 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004639 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004640 } else {
4641 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004642 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004643 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004644 req->analysers &= AN_REQ_FLT_END;
4645 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004646
Willy Tarreaue7dff022015-04-03 01:14:29 +02004647 if (!(s->flags & SF_ERR_MASK))
4648 s->flags |= SF_ERR_PRXCOND;
4649 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004650 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004651 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004652 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004653 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004654 }
4655 return 0;
4656
4657 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004658 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004659 txn->req.msg_state = HTTP_MSG_ERROR;
4660 if (txn->status) {
4661 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004662 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004663 } else {
4664 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004665 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004666 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004667 req->analysers &= AN_REQ_FLT_END;
4668 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004669
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004670 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4671 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004672 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004673 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004674
Willy Tarreaue7dff022015-04-03 01:14:29 +02004675 if (!(s->flags & SF_ERR_MASK))
4676 s->flags |= SF_ERR_SRVCL;
4677 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004678 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004679 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004680 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004681 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004682 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004683 return 0;
4684}
4685
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004686/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4687 * processing can continue on next analysers, or zero if it either needs more
4688 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004689 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004690 * when it has nothing left to do, and may remove any analyser when it wants to
4691 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004692 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004693int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004694{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004695 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004696 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004697 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004698 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004699 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004700 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004701 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004702
Christopher Faulet45073512018-07-20 10:16:29 +02004703 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004704 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004705 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004706 rep,
4707 rep->rex, rep->wex,
4708 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004709 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004710 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004711
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004712 /*
4713 * Now parse the partial (or complete) lines.
4714 * We will check the response syntax, and also join multi-line
4715 * headers. An index of all the lines will be elaborated while
4716 * parsing.
4717 *
4718 * For the parsing, we use a 28 states FSM.
4719 *
4720 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004721 * ci_head(rep) = beginning of response
4722 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4723 * ci_tail(rep) = end of input data
4724 * msg->eol = end of current header or line (LF or CRLF)
4725 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004726 */
4727
Willy Tarreau628c40c2014-04-24 19:11:26 +02004728 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004729 /* There's a protected area at the end of the buffer for rewriting
4730 * purposes. We don't want to start to parse the request if the
4731 * protected area is affected, because we may have to move processed
4732 * data later, which is much more complicated.
4733 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004734 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004735 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004736 /* some data has still not left the buffer, wake us once that's done */
4737 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4738 goto abort_response;
4739 channel_dont_close(rep);
4740 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004741 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004742 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004743 }
4744
Willy Tarreau188e2302018-06-15 11:11:53 +02004745 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004746 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004747 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004748
Willy Tarreauf37954d2018-06-15 18:31:02 +02004749 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004750 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004751 }
4752
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004753 /* 1: we might have to print this header in debug mode */
4754 if (unlikely((global.mode & MODE_DEBUG) &&
4755 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004756 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004757 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004758
Willy Tarreauf37954d2018-06-15 18:31:02 +02004759 sol = ci_head(rep);
4760 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004761 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004762
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004763 sol += hdr_idx_first_pos(&txn->hdr_idx);
4764 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004765
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004766 while (cur_idx) {
4767 eol = sol + txn->hdr_idx.v[cur_idx].len;
4768 debug_hdr("srvhdr", s, sol, eol);
4769 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4770 cur_idx = txn->hdr_idx.v[cur_idx].next;
4771 }
4772 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004773
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004774 /*
4775 * Now we quickly check if we have found a full valid response.
4776 * If not so, we check the FD and buffer states before leaving.
4777 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004778 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004779 * responses are checked first.
4780 *
4781 * Depending on whether the client is still there or not, we
4782 * may send an error response back or not. Note that normally
4783 * we should only check for HTTP status there, and check I/O
4784 * errors somewhere else.
4785 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004786
Willy Tarreau655dce92009-11-08 13:10:58 +01004787 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004788 /* Invalid response */
4789 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4790 /* we detected a parsing error. We want to archive this response
4791 * in the dedicated proxy area for later troubleshooting.
4792 */
4793 hdr_response_bad:
4794 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004795 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004796
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004797 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004798 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004799 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004800 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004801 }
Willy Tarreau64648412010-03-05 10:41:54 +01004802 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004803 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004804 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004805 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004806 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004807 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004808 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004809
Willy Tarreaue7dff022015-04-03 01:14:29 +02004810 if (!(s->flags & SF_ERR_MASK))
4811 s->flags |= SF_ERR_PRXCOND;
4812 if (!(s->flags & SF_FINST_MASK))
4813 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004814
4815 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004816 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004817
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004818 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004819 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004820 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004821 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004822 goto hdr_response_bad;
4823 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004824
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004825 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004826 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004827 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004828 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004829 else if (txn->flags & TX_NOT_FIRST)
4830 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004831
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004832 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004833 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004834 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004835 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004836 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004837
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004838 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004839 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004840 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004841
4842 /* Check to see if the server refused the early data.
4843 * If so, just send a 425
4844 */
4845 if (objt_cs(s->si[1].end)) {
4846 struct connection *conn = objt_cs(s->si[1].end)->conn;
4847
4848 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4849 txn->status = 425;
4850 }
4851
Willy Tarreau350f4872014-11-28 14:42:25 +01004852 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004853 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004854 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004855
Willy Tarreaue7dff022015-04-03 01:14:29 +02004856 if (!(s->flags & SF_ERR_MASK))
4857 s->flags |= SF_ERR_SRVCL;
4858 if (!(s->flags & SF_FINST_MASK))
4859 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004860 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004861 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004862
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004863 /* read timeout : return a 504 to the client. */
4864 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004865 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004866 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004867
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004868 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004869 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004870 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004871 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004872 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004873
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004874 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004875 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004876 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004877 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004878 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004879 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004880
Willy Tarreaue7dff022015-04-03 01:14:29 +02004881 if (!(s->flags & SF_ERR_MASK))
4882 s->flags |= SF_ERR_SRVTO;
4883 if (!(s->flags & SF_FINST_MASK))
4884 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004885 return 0;
4886 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004887
Willy Tarreauf003d372012-11-26 13:35:37 +01004888 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004889 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004890 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4891 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004892 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004893 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004894
Christopher Faulet0184ea72017-01-05 14:06:34 +01004895 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004896 channel_auto_close(rep);
4897
4898 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004899 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004900 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004901
Willy Tarreaue7dff022015-04-03 01:14:29 +02004902 if (!(s->flags & SF_ERR_MASK))
4903 s->flags |= SF_ERR_CLICL;
4904 if (!(s->flags & SF_FINST_MASK))
4905 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004906
Willy Tarreau87b09662015-04-03 00:22:06 +02004907 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004908 return 0;
4909 }
4910
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004911 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004912 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004913 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004914 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004915 else if (txn->flags & TX_NOT_FIRST)
4916 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004917
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004918 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004919 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004920 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004921 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004922 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004923
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004924 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004925 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004926 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004927 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004928 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004929 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004930
Willy Tarreaue7dff022015-04-03 01:14:29 +02004931 if (!(s->flags & SF_ERR_MASK))
4932 s->flags |= SF_ERR_SRVCL;
4933 if (!(s->flags & SF_FINST_MASK))
4934 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004935 return 0;
4936 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004937
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004938 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004939 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004940 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004941 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004942 else if (txn->flags & TX_NOT_FIRST)
4943 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004944
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004945 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004946 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004947 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004948
Willy Tarreaue7dff022015-04-03 01:14:29 +02004949 if (!(s->flags & SF_ERR_MASK))
4950 s->flags |= SF_ERR_CLICL;
4951 if (!(s->flags & SF_FINST_MASK))
4952 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004953
Willy Tarreau87b09662015-04-03 00:22:06 +02004954 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004955 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004956 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004957
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004958 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004959 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004960 return 0;
4961 }
4962
4963 /* More interesting part now : we know that we have a complete
4964 * response which at least looks like HTTP. We have an indicator
4965 * of each header's length, so we can parse them quickly.
4966 */
4967
4968 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004969 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004970
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004971 /*
4972 * 1: get the status code
4973 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004974 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004975 if (n < 1 || n > 5)
4976 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004977 /* when the client triggers a 4xx from the server, it's most often due
4978 * to a missing object or permission. These events should be tracked
4979 * because if they happen often, it may indicate a brute force or a
4980 * vulnerability scan.
4981 */
4982 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004983 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004984
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004985 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004986 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004987
Willy Tarreau91852eb2015-05-01 13:26:00 +02004988 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4989 * exactly one digit "." one digit. This check may be disabled using
4990 * option accept-invalid-http-response.
4991 */
4992 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4993 if (msg->sl.st.v_l != 8) {
4994 msg->err_pos = 0;
4995 goto hdr_response_bad;
4996 }
4997
Willy Tarreauf37954d2018-06-15 18:31:02 +02004998 if (ci_head(rep)[4] != '/' ||
4999 !isdigit((unsigned char)ci_head(rep)[5]) ||
5000 ci_head(rep)[6] != '.' ||
5001 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02005002 msg->err_pos = 4;
5003 goto hdr_response_bad;
5004 }
5005 }
5006
Willy Tarreau5b154472009-12-21 20:11:07 +01005007 /* check if the response is HTTP/1.1 or above */
5008 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02005009 ((ci_head(rep)[5] > '1') ||
5010 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005011 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005012
5013 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005014 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01005015
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005016 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005017 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005018
Willy Tarreauf37954d2018-06-15 18:31:02 +02005019 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005020
Willy Tarreau39650402010-03-15 19:44:39 +01005021 /* Adjust server's health based on status code. Note: status codes 501
5022 * and 505 are triggered on demand by client request, so we must not
5023 * count them as server failures.
5024 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005025 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005026 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005027 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005028 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005029 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005030 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005031
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005032 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005033 * We may be facing a 100-continue response, or any other informational
5034 * 1xx response which is non-final, in which case this is not the right
5035 * response, and we're waiting for the next one. Let's allow this response
5036 * to go to the client and wait for the next one. There's an exception for
5037 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005038 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005039 if (txn->status < 200 &&
5040 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005041 hdr_idx_init(&txn->hdr_idx);
5042 msg->next -= channel_forward(rep, msg->next);
5043 msg->msg_state = HTTP_MSG_RPBEFORE;
5044 txn->status = 0;
5045 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005046 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005047 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005048 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005049
Willy Tarreaua14ad722017-07-07 11:36:32 +02005050 /*
5051 * 2: check for cacheability.
5052 */
5053
5054 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005055 case 200:
5056 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005057 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005058 case 206:
5059 case 300:
5060 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005061 case 404:
5062 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005063 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005064 case 414:
5065 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01005066 break;
5067 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01005068 /* RFC7231#6.1:
5069 * Responses with status codes that are defined as
5070 * cacheable by default (e.g., 200, 203, 204, 206,
5071 * 300, 301, 404, 405, 410, 414, and 501 in this
5072 * specification) can be reused by a cache with
5073 * heuristic expiration unless otherwise indicated
5074 * by the method definition or explicit cache
5075 * controls [RFC7234]; all other status codes are
5076 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005077 */
Willy Tarreau83ece462017-12-21 15:13:09 +01005078 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005079 break;
5080 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005081
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005082 /*
5083 * 3: we may need to capture headers
5084 */
5085 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005086 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02005087 capture_headers(ci_head(rep), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005088 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005089
Willy Tarreau557f1992015-05-01 10:05:17 +02005090 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5091 * by RFC7230#3.3.3 :
5092 *
5093 * The length of a message body is determined by one of the following
5094 * (in order of precedence):
5095 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005096 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5097 * the connection will become a tunnel immediately after the empty
5098 * line that concludes the header fields. A client MUST ignore
5099 * any Content-Length or Transfer-Encoding header fields received
5100 * in such a message. Any 101 response (Switching Protocols) is
5101 * managed in the same manner.
5102 *
5103 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005104 * (Informational), 204 (No Content), or 304 (Not Modified) status
5105 * code is always terminated by the first empty line after the
5106 * header fields, regardless of the header fields present in the
5107 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005108 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005109 * 3. If a Transfer-Encoding header field is present and the chunked
5110 * transfer coding (Section 4.1) is the final encoding, the message
5111 * body length is determined by reading and decoding the chunked
5112 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005113 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005114 * If a Transfer-Encoding header field is present in a response and
5115 * the chunked transfer coding is not the final encoding, the
5116 * message body length is determined by reading the connection until
5117 * it is closed by the server. If a Transfer-Encoding header field
5118 * is present in a request and the chunked transfer coding is not
5119 * the final encoding, the message body length cannot be determined
5120 * reliably; the server MUST respond with the 400 (Bad Request)
5121 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005122 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005123 * If a message is received with both a Transfer-Encoding and a
5124 * Content-Length header field, the Transfer-Encoding overrides the
5125 * Content-Length. Such a message might indicate an attempt to
5126 * perform request smuggling (Section 9.5) or response splitting
5127 * (Section 9.4) and ought to be handled as an error. A sender MUST
5128 * remove the received Content-Length field prior to forwarding such
5129 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005130 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005131 * 4. If a message is received without Transfer-Encoding and with
5132 * either multiple Content-Length header fields having differing
5133 * field-values or a single Content-Length header field having an
5134 * invalid value, then the message framing is invalid and the
5135 * recipient MUST treat it as an unrecoverable error. If this is a
5136 * request message, the server MUST respond with a 400 (Bad Request)
5137 * status code and then close the connection. If this is a response
5138 * message received by a proxy, the proxy MUST close the connection
5139 * to the server, discard the received response, and send a 502 (Bad
5140 * Gateway) response to the client. If this is a response message
5141 * received by a user agent, the user agent MUST close the
5142 * connection to the server and discard the received response.
5143 *
5144 * 5. If a valid Content-Length header field is present without
5145 * Transfer-Encoding, its decimal value defines the expected message
5146 * body length in octets. If the sender closes the connection or
5147 * the recipient times out before the indicated number of octets are
5148 * received, the recipient MUST consider the message to be
5149 * incomplete and close the connection.
5150 *
5151 * 6. If this is a request message and none of the above are true, then
5152 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005153 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005154 * 7. Otherwise, this is a response message without a declared message
5155 * body length, so the message body length is determined by the
5156 * number of octets received prior to the server closing the
5157 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005158 */
5159
5160 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005161 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005162 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005163 * FIXME: should we parse anyway and return an error on chunked encoding ?
5164 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005165 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5166 txn->status == 101)) {
5167 /* Either we've established an explicit tunnel, or we're
5168 * switching the protocol. In both cases, we're very unlikely
5169 * to understand the next protocols. We have to switch to tunnel
5170 * mode, so that we transfer the request and responses then let
5171 * this protocol pass unmodified. When we later implement specific
5172 * parsers for such protocols, we'll want to check the Upgrade
5173 * header which contains information about that protocol for
5174 * responses with status 101 (eg: see RFC2817 about TLS).
5175 */
5176 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5177 msg->flags |= HTTP_MSGF_XFER_LEN;
5178 goto end;
5179 }
5180
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005181 if (txn->meth == HTTP_METH_HEAD ||
5182 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005183 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005184 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005185 goto skip_content_length;
5186 }
5187
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005188 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005189 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005190 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005191 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005192 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5193 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005194 /* bad transfer-encoding (chunked followed by something else) */
5195 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005196 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005197 break;
5198 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005199 }
5200
Willy Tarreau1c913912015-04-30 10:57:51 +02005201 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005202 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005203 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005204 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005205 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5206 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005207 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005208 signed long long cl;
5209
Willy Tarreauad14f752011-09-02 20:33:27 +02005210 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005211 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005212 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005213 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005214
Willy Tarreauad14f752011-09-02 20:33:27 +02005215 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005216 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005217 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005218 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005219
Willy Tarreauad14f752011-09-02 20:33:27 +02005220 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005221 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005222 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005223 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005224
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005225 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005226 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005227 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005228 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005229
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005230 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005231 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005232 }
5233
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005234 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005235 /* Now we have to check if we need to modify the Connection header.
5236 * This is more difficult on the response than it is on the request,
5237 * because we can have two different HTTP versions and we don't know
5238 * how the client will interprete a response. For instance, let's say
5239 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5240 * HTTP/1.1 response without any header. Maybe it will bound itself to
5241 * HTTP/1.0 because it only knows about it, and will consider the lack
5242 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5243 * the lack of header as a keep-alive. Thus we will use two flags
5244 * indicating how a request MAY be understood by the client. In case
5245 * of multiple possibilities, we'll fix the header to be explicit. If
5246 * ambiguous cases such as both close and keepalive are seen, then we
5247 * will fall back to explicit close. Note that we won't take risks with
5248 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005249 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005250 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005251 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5252 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5253 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5254 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005255 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005256
Willy Tarreau70dffda2014-01-30 03:07:23 +01005257 /* this situation happens when combining pretend-keepalive with httpclose. */
5258 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005259 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005260 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005261 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5262
Willy Tarreau60466522010-01-18 19:08:45 +01005263 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005264 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005265 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5266 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005267
Willy Tarreau60466522010-01-18 19:08:45 +01005268 /* now adjust header transformations depending on current state */
5269 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5270 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5271 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005272 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005273 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005274 }
Willy Tarreau60466522010-01-18 19:08:45 +01005275 else { /* SCL / KAL */
5276 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005277 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005278 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005279 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005280
Willy Tarreau60466522010-01-18 19:08:45 +01005281 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005282 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005283
Willy Tarreau60466522010-01-18 19:08:45 +01005284 /* Some keep-alive responses are converted to Server-close if
5285 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005286 */
Willy Tarreau60466522010-01-18 19:08:45 +01005287 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5288 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005289 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005290 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005291 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005292 }
5293
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005294 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005295 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005296 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005297
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005298 /* end of job, return OK */
5299 rep->analysers &= ~an_bit;
5300 rep->analyse_exp = TICK_ETERNITY;
5301 channel_auto_close(rep);
5302 return 1;
5303
5304 abort_keep_alive:
5305 /* A keep-alive request to the server failed on a network error.
5306 * The client is required to retry. We need to close without returning
5307 * any other information so that the client retries.
5308 */
5309 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005310 rep->analysers &= AN_RES_FLT_END;
5311 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005312 channel_auto_close(rep);
5313 s->logs.logwait = 0;
5314 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005315 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005316 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005317 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005318 return 0;
5319}
5320
5321/* This function performs all the processing enabled for the current response.
5322 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005323 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005324 * other functions. It works like process_request (see indications above).
5325 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005326int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005327{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005328 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005329 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005330 struct http_msg *msg = &txn->rsp;
5331 struct proxy *cur_proxy;
5332 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005333 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005334
Christopher Faulet45073512018-07-20 10:16:29 +02005335 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005336 now_ms, __FUNCTION__,
5337 s,
5338 rep,
5339 rep->rex, rep->wex,
5340 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005341 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005342 rep->analysers);
5343
5344 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5345 return 0;
5346
Willy Tarreau70730dd2014-04-24 18:06:27 +02005347 /* The stats applet needs to adjust the Connection header but we don't
5348 * apply any filter there.
5349 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005350 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5351 rep->analysers &= ~an_bit;
5352 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005353 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005354 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005355
Willy Tarreau58975672014-04-24 21:13:57 +02005356 /*
5357 * We will have to evaluate the filters.
5358 * As opposed to version 1.2, now they will be evaluated in the
5359 * filters order and not in the header order. This means that
5360 * each filter has to be validated among all headers.
5361 *
5362 * Filters are tried with ->be first, then with ->fe if it is
5363 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005364 *
5365 * Maybe we are in resume condiion. In this case I choose the
5366 * "struct proxy" which contains the rule list matching the resume
5367 * pointer. If none of theses "struct proxy" match, I initialise
5368 * the process with the first one.
5369 *
5370 * In fact, I check only correspondance betwwen the current list
5371 * pointer and the ->fe rule list. If it doesn't match, I initialize
5372 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005373 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005374 if (s->current_rule_list == &sess->fe->http_res_rules)
5375 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005376 else
5377 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005378 while (1) {
5379 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005380
Willy Tarreau58975672014-04-24 21:13:57 +02005381 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005382 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005383 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005384
Willy Tarreau51d861a2015-05-22 17:30:48 +02005385 if (ret == HTTP_RULE_RES_BADREQ)
5386 goto return_srv_prx_502;
5387
5388 if (ret == HTTP_RULE_RES_DONE) {
5389 rep->analysers &= ~an_bit;
5390 rep->analyse_exp = TICK_ETERNITY;
5391 return 1;
5392 }
5393 }
5394
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005395 /* we need to be called again. */
5396 if (ret == HTTP_RULE_RES_YIELD) {
5397 channel_dont_close(rep);
5398 return 0;
5399 }
5400
Willy Tarreau58975672014-04-24 21:13:57 +02005401 /* try headers filters */
5402 if (rule_set->rsp_exp != NULL) {
5403 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5404 return_bad_resp:
5405 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005406 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005407 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005408 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005409 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005410 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005411 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005412 txn->status = 502;
5413 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005414 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005415 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005416 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005417 if (!(s->flags & SF_ERR_MASK))
5418 s->flags |= SF_ERR_PRXCOND;
5419 if (!(s->flags & SF_FINST_MASK))
5420 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005421 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005422 }
Willy Tarreau58975672014-04-24 21:13:57 +02005423 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005424
Willy Tarreau58975672014-04-24 21:13:57 +02005425 /* has the response been denied ? */
5426 if (txn->flags & TX_SVDENY) {
5427 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005428 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005429
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005430 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5431 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005432 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005433 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005434
Willy Tarreau58975672014-04-24 21:13:57 +02005435 goto return_srv_prx_502;
5436 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005437
Willy Tarreau58975672014-04-24 21:13:57 +02005438 /* add response headers from the rule sets in the same order */
5439 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005440 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005441 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005442 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005443 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005444 ret = acl_pass(ret);
5445 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5446 ret = !ret;
5447 if (!ret)
5448 continue;
5449 }
5450 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5451 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005452 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005453
Willy Tarreau58975672014-04-24 21:13:57 +02005454 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005455 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005456 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005457 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005458 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005459
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005460 /* After this point, this anayzer can't return yield, so we can
5461 * remove the bit corresponding to this analyzer from the list.
5462 *
5463 * Note that the intermediate returns and goto found previously
5464 * reset the analyzers.
5465 */
5466 rep->analysers &= ~an_bit;
5467 rep->analyse_exp = TICK_ETERNITY;
5468
Willy Tarreau58975672014-04-24 21:13:57 +02005469 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005470 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005471 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005472
Willy Tarreau58975672014-04-24 21:13:57 +02005473 /*
5474 * Now check for a server cookie.
5475 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005476 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005477 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005478
Willy Tarreau58975672014-04-24 21:13:57 +02005479 /*
5480 * Check for cache-control or pragma headers if required.
5481 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005482 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005483 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005484
Willy Tarreau58975672014-04-24 21:13:57 +02005485 /*
5486 * Add server cookie in the response if needed
5487 */
5488 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5489 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005490 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005491 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5492 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5493 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5494 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5495 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005496 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005497 /* the server is known, it's not the one the client requested, or the
5498 * cookie's last seen date needs to be refreshed. We have to
5499 * insert a set-cookie here, except if we want to insert only on POST
5500 * requests and this one isn't. Note that servers which don't have cookies
5501 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005502 */
Willy Tarreau58975672014-04-24 21:13:57 +02005503 if (!objt_server(s->target)->cookie) {
5504 chunk_printf(&trash,
5505 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5506 s->be->cookie_name);
5507 }
5508 else {
5509 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005510
Willy Tarreau58975672014-04-24 21:13:57 +02005511 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5512 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005513 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5514 s30tob64((date.tv_sec+3) >> 2,
5515 trash.area + trash.data);
5516 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005517
Willy Tarreau58975672014-04-24 21:13:57 +02005518 if (s->be->cookie_maxlife) {
5519 /* emit first_date, which is either the original one or
5520 * the current date.
5521 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005522 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005523 s30tob64(txn->cookie_first_date ?
5524 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005525 (date.tv_sec+3) >> 2,
5526 trash.area + trash.data);
5527 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005528 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005529 }
Willy Tarreau58975672014-04-24 21:13:57 +02005530 chunk_appendf(&trash, "; path=/");
5531 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005532
Willy Tarreau58975672014-04-24 21:13:57 +02005533 if (s->be->cookie_domain)
5534 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005535
Willy Tarreau58975672014-04-24 21:13:57 +02005536 if (s->be->ck_opts & PR_CK_HTTPONLY)
5537 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005538
Willy Tarreau58975672014-04-24 21:13:57 +02005539 if (s->be->ck_opts & PR_CK_SECURE)
5540 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005541
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005542 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005543 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005544
Willy Tarreau58975672014-04-24 21:13:57 +02005545 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005546 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005547 /* the server did not change, only the date was updated */
5548 txn->flags |= TX_SCK_UPDATED;
5549 else
5550 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005551
Willy Tarreau58975672014-04-24 21:13:57 +02005552 /* Here, we will tell an eventual cache on the client side that we don't
5553 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5554 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5555 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005556 */
Willy Tarreau58975672014-04-24 21:13:57 +02005557 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005558
Willy Tarreau58975672014-04-24 21:13:57 +02005559 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005560
Willy Tarreau58975672014-04-24 21:13:57 +02005561 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5562 "Cache-control: private", 22) < 0))
5563 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005564 }
Willy Tarreau58975672014-04-24 21:13:57 +02005565 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005566
Willy Tarreau58975672014-04-24 21:13:57 +02005567 /*
5568 * Check if result will be cacheable with a cookie.
5569 * We'll block the response if security checks have caught
5570 * nasty things such as a cacheable cookie.
5571 */
5572 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5573 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5574 (s->be->options & PR_O_CHK_CACHE)) {
5575 /* we're in presence of a cacheable response containing
5576 * a set-cookie header. We'll block it as requested by
5577 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005578 */
Willy Tarreau58975672014-04-24 21:13:57 +02005579 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005580 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005581
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005582 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5583 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005584 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005585 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005586
Christopher Faulet767a84b2017-11-24 16:50:31 +01005587 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5588 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005589 send_log(s->be, LOG_ALERT,
5590 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5591 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5592 goto return_srv_prx_502;
5593 }
Willy Tarreau03945942009-12-22 16:50:27 +01005594
Willy Tarreau70730dd2014-04-24 18:06:27 +02005595 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005596 /*
5597 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5598 * If an "Upgrade" token is found, the header is left untouched in order
5599 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005600 * if anything but "Upgrade" is present in the Connection header. We don't
5601 * want to touch any 101 response either since it's switching to another
5602 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005603 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005604 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005605 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005606 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005607 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5608 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005609
Willy Tarreau58975672014-04-24 21:13:57 +02005610 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5611 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5612 /* we want a keep-alive response here. Keep-alive header
5613 * required if either side is not 1.1.
5614 */
5615 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5616 want_flags |= TX_CON_KAL_SET;
5617 }
5618 else {
5619 /* we want a close response here. Close header required if
5620 * the server is 1.1, regardless of the client.
5621 */
5622 if (msg->flags & HTTP_MSGF_VER_11)
5623 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005624 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005625
Willy Tarreau58975672014-04-24 21:13:57 +02005626 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5627 http_change_connection_header(txn, msg, want_flags);
5628 }
5629
5630 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005631 /* Always enter in the body analyzer */
5632 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5633 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005634
Willy Tarreau58975672014-04-24 21:13:57 +02005635 /* if the user wants to log as soon as possible, without counting
5636 * bytes from the server, then this is the right moment. We have
5637 * to temporarily assign bytes_out to log what we currently have.
5638 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005639 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005640 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5641 s->logs.bytes_out = txn->rsp.eoh;
5642 s->do_log(s);
5643 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005644 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005645 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005646}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005647
Willy Tarreaud98cf932009-12-27 22:54:55 +01005648/* This function is an analyser which forwards response body (including chunk
5649 * sizes if any). It is called as soon as we must forward, even if we forward
5650 * zero byte. The only situation where it must not be called is when we're in
5651 * tunnel mode and we want to forward till the close. It's used both to forward
5652 * remaining data and to resync after end of body. It expects the msg_state to
5653 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005654 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005655 *
5656 * It is capable of compressing response data both in content-length mode and
5657 * in chunked mode. The state machines follows different flows depending on
5658 * whether content-length and chunked modes are used, since there are no
5659 * trailers in content-length :
5660 *
5661 * chk-mode cl-mode
5662 * ,----- BODY -----.
5663 * / \
5664 * V size > 0 V chk-mode
5665 * .--> SIZE -------------> DATA -------------> CRLF
5666 * | | size == 0 | last byte |
5667 * | v final crlf v inspected |
5668 * | TRAILERS -----------> DONE |
5669 * | |
5670 * `----------------------------------------------'
5671 *
5672 * Compression only happens in the DATA state, and must be flushed in final
5673 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5674 * is performed at once on final states for all bytes parsed, or when leaving
5675 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005676 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005677int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005678{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005679 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005680 struct http_txn *txn = s->txn;
5681 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005682 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005683
Christopher Faulet45073512018-07-20 10:16:29 +02005684 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Christopher Faulet814d2702017-03-30 11:33:44 +02005685 now_ms, __FUNCTION__,
5686 s,
5687 res,
5688 res->rex, res->wex,
5689 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005690 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005691 res->analysers);
5692
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005693 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5694 return 0;
5695
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005696 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005697 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005698 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005699 /* Output closed while we were sending data. We must abort and
5700 * wake the other side up.
5701 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005702 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005703 msg->msg_state = HTTP_MSG_ERROR;
5704 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005705 return 1;
5706 }
5707
Willy Tarreau4fe41902010-06-07 22:27:41 +02005708 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005709 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005710
Christopher Fauletd7c91962015-04-30 11:48:27 +02005711 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005712 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5713 ? HTTP_MSG_CHUNK_SIZE
5714 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005715 }
5716
Willy Tarreauefdf0942014-04-24 20:08:57 +02005717 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005718 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005719 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005720 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005721 }
5722
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005723 if (msg->msg_state < HTTP_MSG_DONE) {
5724 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5725 ? http_msg_forward_chunked_body(s, msg)
5726 : http_msg_forward_body(s, msg));
5727 if (!ret)
5728 goto missing_data_or_waiting;
5729 if (ret < 0)
5730 goto return_bad_res;
5731 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005732
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005733 /* other states, DONE...TUNNEL */
5734 /* for keep-alive we don't want to forward closes on DONE */
5735 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5736 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5737 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005738
Christopher Faulet894da4c2017-07-18 11:29:07 +02005739 http_resync_states(s);
5740 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005741 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5742 if (res->flags & CF_SHUTW) {
5743 /* response errors are most likely due to the
5744 * client aborting the transfer. */
5745 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005746 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005747 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005748 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005749 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005750 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005751 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005752 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005753 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005754
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005755 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005756 if (res->flags & CF_SHUTW)
5757 goto aborted_xfer;
5758
5759 /* stop waiting for data if the input is closed before the end. If the
5760 * client side was already closed, it means that the client has aborted,
5761 * so we don't want to count this as a server abort. Otherwise it's a
5762 * server abort.
5763 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005764 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005765 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005766 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005767 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005768 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005769 if (!(s->flags & SF_ERR_MASK))
5770 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005771 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005772 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005773 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005774 goto return_bad_res_stats_ok;
5775 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005776 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005777
Willy Tarreau40dba092010-03-04 18:14:51 +01005778 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005779 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005780 goto return_bad_res;
5781
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005782 /* When TE: chunked is used, we need to get there again to parse
5783 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005784 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5785 * or if there are filters registered on the stream, we don't want to
5786 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005787 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005788 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005789 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005790 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5791 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005792 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005793
Willy Tarreau5c620922011-05-11 19:56:11 +02005794 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005795 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005796 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005797 * modes are already handled by the stream sock layer. We must not do
5798 * this in content-length mode because it could present the MSG_MORE
5799 * flag with the last block of forwarded data, which would cause an
5800 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005801 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005802 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005803 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005804
Willy Tarreau87b09662015-04-03 00:22:06 +02005805 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005806 return 0;
5807
Willy Tarreau40dba092010-03-04 18:14:51 +01005808 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005809 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005810 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005811 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005812
5813 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005814 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005815 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005816 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005817 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005818 res->analysers &= AN_RES_FLT_END;
5819 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005820 if (objt_server(s->target))
5821 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005822
Willy Tarreaue7dff022015-04-03 01:14:29 +02005823 if (!(s->flags & SF_ERR_MASK))
5824 s->flags |= SF_ERR_PRXCOND;
5825 if (!(s->flags & SF_FINST_MASK))
5826 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005827 return 0;
5828
5829 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005830 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005831 txn->rsp.msg_state = HTTP_MSG_ERROR;
5832 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005833 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005834 res->analysers &= AN_RES_FLT_END;
5835 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005836
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005837 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5838 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005839 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005840 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005841
Willy Tarreaue7dff022015-04-03 01:14:29 +02005842 if (!(s->flags & SF_ERR_MASK))
5843 s->flags |= SF_ERR_CLICL;
5844 if (!(s->flags & SF_FINST_MASK))
5845 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005846 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005847}
5848
5849
5850static inline int
5851http_msg_forward_body(struct stream *s, struct http_msg *msg)
5852{
5853 struct channel *chn = msg->chn;
5854 int ret;
5855
5856 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5857
5858 if (msg->msg_state == HTTP_MSG_ENDING)
5859 goto ending;
5860
5861 /* Neither content-length, nor transfer-encoding was found, so we must
5862 * read the body until the server connection is closed. In that case, we
5863 * eat data as they come. Of course, this happens for response only. */
5864 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005865 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005866 msg->chunk_len += len;
5867 msg->body_len += len;
5868 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005869 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005870 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005871 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005872 msg->next += ret;
5873 msg->chunk_len -= ret;
5874 if (msg->chunk_len) {
5875 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005876 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005877 chn->flags |= CF_WAKE_WRITE;
5878 goto missing_data_or_waiting;
5879 }
5880
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005881 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5882 * always set for a request. */
5883 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5884 /* The server still sending data that should be filtered */
5885 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5886 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005887 msg->msg_state = HTTP_MSG_TUNNEL;
5888 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005889 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005890
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005891 msg->msg_state = HTTP_MSG_ENDING;
5892
5893 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005894 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005895 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005896 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5897 /* default_ret */ msg->next,
5898 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005899 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005900 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005901 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5902 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005903 if (msg->next)
5904 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005905
Christopher Fauletda02e172015-12-04 09:25:05 +01005906 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5907 /* default_ret */ 1,
5908 /* on_error */ goto error,
5909 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005910 if (msg->msg_state == HTTP_MSG_ENDING)
5911 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005912 return 1;
5913
5914 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005915 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005916 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5917 /* default_ret */ msg->next,
5918 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005919 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005920 msg->next -= ret;
5921 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5922 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005923 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005924 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005925 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005926 return 0;
5927 error:
5928 return -1;
5929}
5930
5931static inline int
5932http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
5933{
5934 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005935 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005936 int ret;
5937
5938 /* Here we have the guarantee to be in one of the following state:
5939 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5940 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5941
5942 switch_states:
5943 switch (msg->msg_state) {
5944 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005945 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005946 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005947 /* on_error */ goto error);
5948 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005949 msg->chunk_len -= ret;
5950 if (msg->chunk_len) {
5951 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005952 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005953 chn->flags |= CF_WAKE_WRITE;
5954 goto missing_data_or_waiting;
5955 }
5956
5957 /* nothing left to forward for this chunk*/
5958 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5959 /* fall through for HTTP_MSG_CHUNK_CRLF */
5960
5961 case HTTP_MSG_CHUNK_CRLF:
5962 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005963 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005964 if (ret == 0)
5965 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005966 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005967 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005968 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005969 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005970 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005971 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005972 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005973 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5974 /* fall through for HTTP_MSG_CHUNK_SIZE */
5975
5976 case HTTP_MSG_CHUNK_SIZE:
5977 /* read the chunk size and assign it to ->chunk_len,
5978 * then set ->next to point to the body and switch to
5979 * DATA or TRAILERS state.
5980 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005981 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005982 if (ret == 0)
5983 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005984 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005985 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005986 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005987 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005988 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005989 }
5990
5991 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005992 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005993 msg->chunk_len = chunk;
5994 msg->body_len += chunk;
5995
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005996 if (msg->chunk_len) {
5997 msg->msg_state = HTTP_MSG_DATA;
5998 goto switch_states;
5999 }
6000 msg->msg_state = HTTP_MSG_TRAILERS;
6001 /* fall through for HTTP_MSG_TRAILERS */
6002
6003 case HTTP_MSG_TRAILERS:
6004 ret = http_forward_trailers(msg);
6005 if (ret < 0)
6006 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006007 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6008 /* default_ret */ 1,
6009 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006010 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006011 if (!ret)
6012 goto missing_data_or_waiting;
6013 break;
6014
6015 case HTTP_MSG_ENDING:
6016 goto ending;
6017
6018 default:
6019 /* This should no happen in this function */
6020 goto error;
6021 }
6022
6023 msg->msg_state = HTTP_MSG_ENDING;
6024 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006025 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006026 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006027 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006028 /* default_ret */ msg->next,
6029 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006030 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006031 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006032 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6033 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006034 if (msg->next)
6035 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006036
Christopher Fauletda02e172015-12-04 09:25:05 +01006037 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006038 /* default_ret */ 1,
6039 /* on_error */ goto error,
6040 /* on_wait */ goto waiting);
6041 msg->msg_state = HTTP_MSG_DONE;
6042 return 1;
6043
6044 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006045 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006046 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006047 /* default_ret */ msg->next,
6048 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02006049 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006050 msg->next -= ret;
6051 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6052 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006053 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006054 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006055 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006056 return 0;
6057
6058 chunk_parsing_error:
6059 if (msg->err_pos >= 0) {
6060 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02006061 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006062 msg->msg_state, strm_fe(s));
6063 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02006064 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006065 msg, msg->msg_state, s->be);
6066 }
6067 error:
6068 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006069}
6070
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006072/* Iterate the same filter through all request headers.
6073 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006074 * Since it can manage the switch to another backend, it updates the per-proxy
6075 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006076 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006077int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006078{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006079 char *cur_ptr, *cur_end, *cur_next;
6080 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006081 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006082 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006083 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006084
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006085 last_hdr = 0;
6086
Willy Tarreauf37954d2018-06-15 18:31:02 +02006087 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006088 old_idx = 0;
6089
6090 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006091 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006092 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006093 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006094 (exp->action == ACT_ALLOW ||
6095 exp->action == ACT_DENY ||
6096 exp->action == ACT_TARPIT))
6097 return 0;
6098
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006099 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006100 if (!cur_idx)
6101 break;
6102
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006103 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006104 cur_ptr = cur_next;
6105 cur_end = cur_ptr + cur_hdr->len;
6106 cur_next = cur_end + cur_hdr->cr + 1;
6107
6108 /* Now we have one header between cur_ptr and cur_end,
6109 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006110 */
6111
Willy Tarreau15a53a42015-01-21 13:39:42 +01006112 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006113 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006114 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006115 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006116 last_hdr = 1;
6117 break;
6118
6119 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006120 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006121 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006122 break;
6123
6124 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006125 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006126 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006127 break;
6128
6129 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006130 len = exp_replace(trash.area,
6131 trash.size, cur_ptr,
6132 exp->replace, pmatch);
6133 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006134 return -1;
6135
Willy Tarreau6e27be12018-08-22 04:46:47 +02006136 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6137
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006138 /* FIXME: if the user adds a newline in the replacement, the
6139 * index will not be recalculated for now, and the new line
6140 * will not be counted as a new header.
6141 */
6142
6143 cur_end += delta;
6144 cur_next += delta;
6145 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006146 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006147 break;
6148
6149 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006150 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006151 cur_next += delta;
6152
Willy Tarreaufa355d42009-11-29 18:12:29 +01006153 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006154 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6155 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006156 cur_hdr->len = 0;
6157 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006158 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006159 break;
6160
6161 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006162 }
6163
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006164 /* keep the link from this header to next one in case of later
6165 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006166 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006167 old_idx = cur_idx;
6168 }
6169 return 0;
6170}
6171
6172
6173/* Apply the filter to the request line.
6174 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6175 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006176 * Since it can manage the switch to another backend, it updates the per-proxy
6177 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006178 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006179int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006180{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006181 char *cur_ptr, *cur_end;
6182 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006183 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006184 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006185
Willy Tarreau3d300592007-03-18 18:34:41 +01006186 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006187 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006188 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006189 (exp->action == ACT_ALLOW ||
6190 exp->action == ACT_DENY ||
6191 exp->action == ACT_TARPIT))
6192 return 0;
6193 else if (exp->action == ACT_REMOVE)
6194 return 0;
6195
6196 done = 0;
6197
Willy Tarreauf37954d2018-06-15 18:31:02 +02006198 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006199 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006200
6201 /* Now we have the request line between cur_ptr and cur_end */
6202
Willy Tarreau15a53a42015-01-21 13:39:42 +01006203 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006204 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006205 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006206 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006207 done = 1;
6208 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006209
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006210 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006211 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006212 done = 1;
6213 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006214
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006215 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006216 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006217 done = 1;
6218 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006219
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006220 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006221 len = exp_replace(trash.area, trash.size,
6222 cur_ptr, exp->replace, pmatch);
6223 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006224 return -1;
6225
Willy Tarreau6e27be12018-08-22 04:46:47 +02006226 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6227
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006228 /* FIXME: if the user adds a newline in the replacement, the
6229 * index will not be recalculated for now, and the new line
6230 * will not be counted as a new header.
6231 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006232
Willy Tarreaufa355d42009-11-29 18:12:29 +01006233 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006234 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006235 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006236 HTTP_MSG_RQMETH,
6237 cur_ptr, cur_end + 1,
6238 NULL, NULL);
6239 if (unlikely(!cur_end))
6240 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006241
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006242 /* we have a full request and we know that we have either a CR
6243 * or an LF at <ptr>.
6244 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006245 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6246 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006247 /* there is no point trying this regex on headers */
6248 return 1;
6249 }
6250 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006251 return done;
6252}
Willy Tarreau97de6242006-12-27 17:18:38 +01006253
Willy Tarreau58f10d72006-12-04 02:26:12 +01006254
Willy Tarreau58f10d72006-12-04 02:26:12 +01006255
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006256/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006257 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006258 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006259 * unparsable request. Since it can manage the switch to another backend, it
6260 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006261 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006262int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006263{
Willy Tarreau192252e2015-04-04 01:47:55 +02006264 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006265 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006266 struct hdr_exp *exp;
6267
6268 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006269 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006271 /*
6272 * The interleaving of transformations and verdicts
6273 * makes it difficult to decide to continue or stop
6274 * the evaluation.
6275 */
6276
Willy Tarreau6c123b12010-01-28 20:22:06 +01006277 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6278 break;
6279
Willy Tarreau3d300592007-03-18 18:34:41 +01006280 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006281 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006282 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006283 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006284
6285 /* if this filter had a condition, evaluate it now and skip to
6286 * next filter if the condition does not match.
6287 */
6288 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006289 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006290 ret = acl_pass(ret);
6291 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6292 ret = !ret;
6293
6294 if (!ret)
6295 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006296 }
6297
6298 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006299 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006300 if (unlikely(ret < 0))
6301 return -1;
6302
6303 if (likely(ret == 0)) {
6304 /* The filter did not match the request, it can be
6305 * iterated through all headers.
6306 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006307 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6308 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006309 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006310 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006311 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006312}
6313
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006314
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006315/* Find the end of a cookie value contained between <s> and <e>. It works the
6316 * same way as with headers above except that the semi-colon also ends a token.
6317 * See RFC2965 for more information. Note that it requires a valid header to
6318 * return a valid result.
6319 */
6320char *find_cookie_value_end(char *s, const char *e)
6321{
6322 int quoted, qdpair;
6323
6324 quoted = qdpair = 0;
6325 for (; s < e; s++) {
6326 if (qdpair) qdpair = 0;
6327 else if (quoted) {
6328 if (*s == '\\') qdpair = 1;
6329 else if (*s == '"') quoted = 0;
6330 }
6331 else if (*s == '"') quoted = 1;
6332 else if (*s == ',' || *s == ';') return s;
6333 }
6334 return s;
6335}
6336
6337/* Delete a value in a header between delimiters <from> and <next> in buffer
6338 * <buf>. The number of characters displaced is returned, and the pointer to
6339 * the first delimiter is updated if required. The function tries as much as
6340 * possible to respect the following principles :
6341 * - replace <from> delimiter by the <next> one unless <from> points to a
6342 * colon, in which case <next> is simply removed
6343 * - set exactly one space character after the new first delimiter, unless
6344 * there are not enough characters in the block being moved to do so.
6345 * - remove unneeded spaces before the previous delimiter and after the new
6346 * one.
6347 *
6348 * It is the caller's responsibility to ensure that :
6349 * - <from> points to a valid delimiter or the colon ;
6350 * - <next> points to a valid delimiter or the final CR/LF ;
6351 * - there are non-space chars before <from> ;
6352 * - there is a CR/LF at or after <next>.
6353 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006354int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006355{
6356 char *prev = *from;
6357
6358 if (*prev == ':') {
6359 /* We're removing the first value, preserve the colon and add a
6360 * space if possible.
6361 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006362 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006363 next++;
6364 prev++;
6365 if (prev < next)
6366 *prev++ = ' ';
6367
Willy Tarreau2235b262016-11-05 15:50:20 +01006368 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006369 next++;
6370 } else {
6371 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006372 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006373 prev--;
6374 *from = prev;
6375
6376 /* copy the delimiter and if possible a space if we're
6377 * not at the end of the line.
6378 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006379 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006380 *prev++ = *next++;
6381 if (prev + 1 < next)
6382 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006383 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006384 next++;
6385 }
6386 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006387 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006388}
6389
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006390/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006391 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006392 * desirable to call it only when needed. This code is quite complex because
6393 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6394 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006395 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006396void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006397{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006398 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006399 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006400 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006401 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006402 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6403 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006404
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006405 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006406 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006407 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006408
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006409 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006410 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006411 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006412
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006413 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006414 hdr_beg = hdr_next;
6415 hdr_end = hdr_beg + cur_hdr->len;
6416 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006417
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006418 /* We have one full header between hdr_beg and hdr_end, and the
6419 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006420 * "Cookie:" headers.
6421 */
6422
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006423 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006424 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006425 old_idx = cur_idx;
6426 continue;
6427 }
6428
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006429 del_from = NULL; /* nothing to be deleted */
6430 preserve_hdr = 0; /* assume we may kill the whole header */
6431
Willy Tarreau58f10d72006-12-04 02:26:12 +01006432 /* Now look for cookies. Conforming to RFC2109, we have to support
6433 * attributes whose name begin with a '$', and associate them with
6434 * the right cookie, if we want to delete this cookie.
6435 * So there are 3 cases for each cookie read :
6436 * 1) it's a special attribute, beginning with a '$' : ignore it.
6437 * 2) it's a server id cookie that we *MAY* want to delete : save
6438 * some pointers on it (last semi-colon, beginning of cookie...)
6439 * 3) it's an application cookie : we *MAY* have to delete a previous
6440 * "special" cookie.
6441 * At the end of loop, if a "special" cookie remains, we may have to
6442 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006443 * *MUST* delete it.
6444 *
6445 * Note: RFC2965 is unclear about the processing of spaces around
6446 * the equal sign in the ATTR=VALUE form. A careful inspection of
6447 * the RFC explicitly allows spaces before it, and not within the
6448 * tokens (attrs or values). An inspection of RFC2109 allows that
6449 * too but section 10.1.3 lets one think that spaces may be allowed
6450 * after the equal sign too, resulting in some (rare) buggy
6451 * implementations trying to do that. So let's do what servers do.
6452 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6453 * allowed quoted strings in values, with any possible character
6454 * after a backslash, including control chars and delimitors, which
6455 * causes parsing to become ambiguous. Browsers also allow spaces
6456 * within values even without quotes.
6457 *
6458 * We have to keep multiple pointers in order to support cookie
6459 * removal at the beginning, middle or end of header without
6460 * corrupting the header. All of these headers are valid :
6461 *
6462 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6463 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6464 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6465 * | | | | | | | | |
6466 * | | | | | | | | hdr_end <--+
6467 * | | | | | | | +--> next
6468 * | | | | | | +----> val_end
6469 * | | | | | +-----------> val_beg
6470 * | | | | +--------------> equal
6471 * | | | +----------------> att_end
6472 * | | +---------------------> att_beg
6473 * | +--------------------------> prev
6474 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006475 */
6476
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006477 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6478 /* Iterate through all cookies on this line */
6479
6480 /* find att_beg */
6481 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006482 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006483 att_beg++;
6484
6485 /* find att_end : this is the first character after the last non
6486 * space before the equal. It may be equal to hdr_end.
6487 */
6488 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006489
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006490 while (equal < hdr_end) {
6491 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006492 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006493 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006494 continue;
6495 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006496 }
6497
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006498 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6499 * is between <att_beg> and <equal>, both may be identical.
6500 */
6501
6502 /* look for end of cookie if there is an equal sign */
6503 if (equal < hdr_end && *equal == '=') {
6504 /* look for the beginning of the value */
6505 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006506 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006507 val_beg++;
6508
6509 /* find the end of the value, respecting quotes */
6510 next = find_cookie_value_end(val_beg, hdr_end);
6511
6512 /* make val_end point to the first white space or delimitor after the value */
6513 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006514 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006515 val_end--;
6516 } else {
6517 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006518 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006519
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006520 /* We have nothing to do with attributes beginning with '$'. However,
6521 * they will automatically be removed if a header before them is removed,
6522 * since they're supposed to be linked together.
6523 */
6524 if (*att_beg == '$')
6525 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006526
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006527 /* Ignore cookies with no equal sign */
6528 if (equal == next) {
6529 /* This is not our cookie, so we must preserve it. But if we already
6530 * scheduled another cookie for removal, we cannot remove the
6531 * complete header, but we can remove the previous block itself.
6532 */
6533 preserve_hdr = 1;
6534 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006535 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006536 val_end += delta;
6537 next += delta;
6538 hdr_end += delta;
6539 hdr_next += delta;
6540 cur_hdr->len += delta;
6541 http_msg_move_end(&txn->req, delta);
6542 prev = del_from;
6543 del_from = NULL;
6544 }
6545 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006546 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006547
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006548 /* if there are spaces around the equal sign, we need to
6549 * strip them otherwise we'll get trouble for cookie captures,
6550 * or even for rewrites. Since this happens extremely rarely,
6551 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006552 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006553 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6554 int stripped_before = 0;
6555 int stripped_after = 0;
6556
6557 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006558 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006559 equal += stripped_before;
6560 val_beg += stripped_before;
6561 }
6562
6563 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006564 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006565 val_beg += stripped_after;
6566 stripped_before += stripped_after;
6567 }
6568
6569 val_end += stripped_before;
6570 next += stripped_before;
6571 hdr_end += stripped_before;
6572 hdr_next += stripped_before;
6573 cur_hdr->len += stripped_before;
6574 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006575 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006576 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006577
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006578 /* First, let's see if we want to capture this cookie. We check
6579 * that we don't already have a client side cookie, because we
6580 * can only capture one. Also as an optimisation, we ignore
6581 * cookies shorter than the declared name.
6582 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006583 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6584 (val_end - att_beg >= sess->fe->capture_namelen) &&
6585 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006586 int log_len = val_end - att_beg;
6587
Willy Tarreaubafbe012017-11-24 17:34:44 +01006588 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006589 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006590 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006591 if (log_len > sess->fe->capture_len)
6592 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006593 memcpy(txn->cli_cookie, att_beg, log_len);
6594 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006595 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006596 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006597
Willy Tarreaubca99692010-10-06 19:25:55 +02006598 /* Persistence cookies in passive, rewrite or insert mode have the
6599 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006600 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006601 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006602 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006603 * For cookies in prefix mode, the form is :
6604 *
6605 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006606 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006607 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6608 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6609 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006610 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006611
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006612 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6613 * have the server ID between val_beg and delim, and the original cookie between
6614 * delim+1 and val_end. Otherwise, delim==val_end :
6615 *
6616 * Cookie: NAME=SRV; # in all but prefix modes
6617 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6618 * | || || | |+-> next
6619 * | || || | +--> val_end
6620 * | || || +---------> delim
6621 * | || |+------------> val_beg
6622 * | || +-------------> att_end = equal
6623 * | |+-----------------> att_beg
6624 * | +------------------> prev
6625 * +-------------------------> hdr_beg
6626 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006627
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006628 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006629 for (delim = val_beg; delim < val_end; delim++)
6630 if (*delim == COOKIE_DELIM)
6631 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006632 } else {
6633 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006634 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006635 /* Now check if the cookie contains a date field, which would
6636 * appear after a vertical bar ('|') just after the server name
6637 * and before the delimiter.
6638 */
6639 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6640 if (vbar1) {
6641 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006642 * right is the last seen date. It is a base64 encoded
6643 * 30-bit value representing the UNIX date since the
6644 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006645 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006646 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006647 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006648 if (val_end - vbar1 >= 5) {
6649 val = b64tos30(vbar1);
6650 if (val > 0)
6651 txn->cookie_last_date = val << 2;
6652 }
6653 /* look for a second vertical bar */
6654 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6655 if (vbar1 && (val_end - vbar1 > 5)) {
6656 val = b64tos30(vbar1 + 1);
6657 if (val > 0)
6658 txn->cookie_first_date = val << 2;
6659 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006660 }
6661 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006662
Willy Tarreauf64d1412010-10-07 20:06:11 +02006663 /* if the cookie has an expiration date and the proxy wants to check
6664 * it, then we do that now. We first check if the cookie is too old,
6665 * then only if it has expired. We detect strict overflow because the
6666 * time resolution here is not great (4 seconds). Cookies with dates
6667 * in the future are ignored if their offset is beyond one day. This
6668 * allows an admin to fix timezone issues without expiring everyone
6669 * and at the same time avoids keeping unwanted side effects for too
6670 * long.
6671 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006672 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6673 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006674 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006675 txn->flags &= ~TX_CK_MASK;
6676 txn->flags |= TX_CK_OLD;
6677 delim = val_beg; // let's pretend we have not found the cookie
6678 txn->cookie_first_date = 0;
6679 txn->cookie_last_date = 0;
6680 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006681 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6682 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006683 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006684 txn->flags &= ~TX_CK_MASK;
6685 txn->flags |= TX_CK_EXPIRED;
6686 delim = val_beg; // let's pretend we have not found the cookie
6687 txn->cookie_first_date = 0;
6688 txn->cookie_last_date = 0;
6689 }
6690
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006691 /* Here, we'll look for the first running server which supports the cookie.
6692 * This allows to share a same cookie between several servers, for example
6693 * to dedicate backup servers to specific servers only.
6694 * However, to prevent clients from sticking to cookie-less backup server
6695 * when they have incidentely learned an empty cookie, we simply ignore
6696 * empty cookies and mark them as invalid.
6697 * The same behaviour is applied when persistence must be ignored.
6698 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006699 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006700 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006701
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006702 while (srv) {
6703 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6704 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006705 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006706 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006707 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006708 /* we found the server and we can use it */
6709 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006710 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006711 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006712 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006713 break;
6714 } else {
6715 /* we found a server, but it's down,
6716 * mark it as such and go on in case
6717 * another one is available.
6718 */
6719 txn->flags &= ~TX_CK_MASK;
6720 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006721 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006722 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006723 srv = srv->next;
6724 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006725
Willy Tarreauf64d1412010-10-07 20:06:11 +02006726 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006727 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006728 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006729 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006730 txn->flags |= TX_CK_UNUSED;
6731 else
6732 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006733 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006734
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006735 /* depending on the cookie mode, we may have to either :
6736 * - delete the complete cookie if we're in insert+indirect mode, so that
6737 * the server never sees it ;
6738 * - remove the server id from the cookie value, and tag the cookie as an
6739 * application cookie so that it does not get accidentely removed later,
6740 * if we're in cookie prefix mode
6741 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006742 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006743 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006744
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006745 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006746 val_end += delta;
6747 next += delta;
6748 hdr_end += delta;
6749 hdr_next += delta;
6750 cur_hdr->len += delta;
6751 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006752
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006753 del_from = NULL;
6754 preserve_hdr = 1; /* we want to keep this cookie */
6755 }
6756 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006757 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006758 del_from = prev;
6759 }
6760 } else {
6761 /* This is not our cookie, so we must preserve it. But if we already
6762 * scheduled another cookie for removal, we cannot remove the
6763 * complete header, but we can remove the previous block itself.
6764 */
6765 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006766
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006767 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006768 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006769 if (att_beg >= del_from)
6770 att_beg += delta;
6771 if (att_end >= del_from)
6772 att_end += delta;
6773 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006774 val_end += delta;
6775 next += delta;
6776 hdr_end += delta;
6777 hdr_next += delta;
6778 cur_hdr->len += delta;
6779 http_msg_move_end(&txn->req, delta);
6780 prev = del_from;
6781 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006782 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006783 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006784
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006785 /* continue with next cookie on this header line */
6786 att_beg = next;
6787 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006788
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006789 /* There are no more cookies on this line.
6790 * We may still have one (or several) marked for deletion at the
6791 * end of the line. We must do this now in two ways :
6792 * - if some cookies must be preserved, we only delete from the
6793 * mark to the end of line ;
6794 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006795 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006796 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006797 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006798 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006799 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006800 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006801 cur_hdr->len += delta;
6802 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006803 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006804
6805 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006806 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6807 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006808 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006809 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006810 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006811 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006812 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006813 }
6814
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006815 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006816 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006817 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006818}
6819
6820
Willy Tarreaua15645d2007-03-18 16:22:39 +01006821/* Iterate the same filter through all response headers contained in <rtr>.
6822 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6823 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006824int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006825{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006826 char *cur_ptr, *cur_end, *cur_next;
6827 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006828 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006829 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006830 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006831
6832 last_hdr = 0;
6833
Willy Tarreauf37954d2018-06-15 18:31:02 +02006834 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006835 old_idx = 0;
6836
6837 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006838 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006839 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006840 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841 (exp->action == ACT_ALLOW ||
6842 exp->action == ACT_DENY))
6843 return 0;
6844
6845 cur_idx = txn->hdr_idx.v[old_idx].next;
6846 if (!cur_idx)
6847 break;
6848
6849 cur_hdr = &txn->hdr_idx.v[cur_idx];
6850 cur_ptr = cur_next;
6851 cur_end = cur_ptr + cur_hdr->len;
6852 cur_next = cur_end + cur_hdr->cr + 1;
6853
6854 /* Now we have one header between cur_ptr and cur_end,
6855 * and the next header starts at cur_next.
6856 */
6857
Willy Tarreau15a53a42015-01-21 13:39:42 +01006858 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006859 switch (exp->action) {
6860 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006861 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006862 last_hdr = 1;
6863 break;
6864
6865 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006866 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006867 last_hdr = 1;
6868 break;
6869
6870 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006871 len = exp_replace(trash.area,
6872 trash.size, cur_ptr,
6873 exp->replace, pmatch);
6874 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006875 return -1;
6876
Willy Tarreau6e27be12018-08-22 04:46:47 +02006877 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6878
Willy Tarreaua15645d2007-03-18 16:22:39 +01006879 /* FIXME: if the user adds a newline in the replacement, the
6880 * index will not be recalculated for now, and the new line
6881 * will not be counted as a new header.
6882 */
6883
6884 cur_end += delta;
6885 cur_next += delta;
6886 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006887 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006888 break;
6889
6890 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006891 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006892 cur_next += delta;
6893
Willy Tarreaufa355d42009-11-29 18:12:29 +01006894 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006895 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6896 txn->hdr_idx.used--;
6897 cur_hdr->len = 0;
6898 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006899 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006900 break;
6901
6902 }
6903 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006904
6905 /* keep the link from this header to next one in case of later
6906 * removal of next header.
6907 */
6908 old_idx = cur_idx;
6909 }
6910 return 0;
6911}
6912
6913
6914/* Apply the filter to the status line in the response buffer <rtr>.
6915 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6916 * or -1 if a replacement resulted in an invalid status line.
6917 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006918int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006919{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006920 char *cur_ptr, *cur_end;
6921 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006922 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006923 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006924
Willy Tarreau3d300592007-03-18 18:34:41 +01006925 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006926 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006927 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006928 (exp->action == ACT_ALLOW ||
6929 exp->action == ACT_DENY))
6930 return 0;
6931 else if (exp->action == ACT_REMOVE)
6932 return 0;
6933
6934 done = 0;
6935
Willy Tarreauf37954d2018-06-15 18:31:02 +02006936 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006937 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006938
6939 /* Now we have the status line between cur_ptr and cur_end */
6940
Willy Tarreau15a53a42015-01-21 13:39:42 +01006941 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006942 switch (exp->action) {
6943 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006944 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006945 done = 1;
6946 break;
6947
6948 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006949 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006950 done = 1;
6951 break;
6952
6953 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006954 len = exp_replace(trash.area, trash.size,
6955 cur_ptr, exp->replace, pmatch);
6956 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006957 return -1;
6958
Willy Tarreau6e27be12018-08-22 04:46:47 +02006959 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6960
Willy Tarreaua15645d2007-03-18 16:22:39 +01006961 /* FIXME: if the user adds a newline in the replacement, the
6962 * index will not be recalculated for now, and the new line
6963 * will not be counted as a new header.
6964 */
6965
Willy Tarreaufa355d42009-11-29 18:12:29 +01006966 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006967 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006968 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006969 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006970 cur_ptr, cur_end + 1,
6971 NULL, NULL);
6972 if (unlikely(!cur_end))
6973 return -1;
6974
6975 /* we have a full respnse and we know that we have either a CR
6976 * or an LF at <ptr>.
6977 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006978 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006979 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006980 /* there is no point trying this regex on headers */
6981 return 1;
6982 }
6983 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006984 return done;
6985}
6986
6987
6988
6989/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006990 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006991 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6992 * unparsable response.
6993 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006994int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006995{
Willy Tarreau192252e2015-04-04 01:47:55 +02006996 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006997 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006998 struct hdr_exp *exp;
6999
7000 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007001 int ret;
7002
7003 /*
7004 * The interleaving of transformations and verdicts
7005 * makes it difficult to decide to continue or stop
7006 * the evaluation.
7007 */
7008
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007009 if (txn->flags & TX_SVDENY)
7010 break;
7011
Willy Tarreau3d300592007-03-18 18:34:41 +01007012 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007013 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7014 exp->action == ACT_PASS)) {
7015 exp = exp->next;
7016 continue;
7017 }
7018
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007019 /* if this filter had a condition, evaluate it now and skip to
7020 * next filter if the condition does not match.
7021 */
7022 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007023 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007024 ret = acl_pass(ret);
7025 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7026 ret = !ret;
7027 if (!ret)
7028 continue;
7029 }
7030
Willy Tarreaua15645d2007-03-18 16:22:39 +01007031 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007032 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007033 if (unlikely(ret < 0))
7034 return -1;
7035
7036 if (likely(ret == 0)) {
7037 /* The filter did not match the response, it can be
7038 * iterated through all headers.
7039 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007040 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7041 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007042 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007043 }
7044 return 0;
7045}
7046
7047
Willy Tarreaua15645d2007-03-18 16:22:39 +01007048/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007049 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007050 * desirable to call it only when needed. This function is also used when we
7051 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007052 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007053void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007054{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007055 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007056 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007057 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007058 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007059 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007060 char *hdr_beg, *hdr_end, *hdr_next;
7061 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007062
Willy Tarreaua15645d2007-03-18 16:22:39 +01007063 /* Iterate through the headers.
7064 * we start with the start line.
7065 */
7066 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007067 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007068
7069 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7070 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007071 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007072
7073 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007074 hdr_beg = hdr_next;
7075 hdr_end = hdr_beg + cur_hdr->len;
7076 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007077
Willy Tarreau24581ba2010-08-31 22:39:35 +02007078 /* We have one full header between hdr_beg and hdr_end, and the
7079 * next header starts at hdr_next. We're only interested in
7080 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007081 */
7082
Willy Tarreau24581ba2010-08-31 22:39:35 +02007083 is_cookie2 = 0;
7084 prev = hdr_beg + 10;
7085 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007086 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007087 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7088 if (!val) {
7089 old_idx = cur_idx;
7090 continue;
7091 }
7092 is_cookie2 = 1;
7093 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007094 }
7095
Willy Tarreau24581ba2010-08-31 22:39:35 +02007096 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7097 * <prev> points to the colon.
7098 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007099 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007100
Willy Tarreau24581ba2010-08-31 22:39:35 +02007101 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7102 * check-cache is enabled) and we are not interested in checking
7103 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007104 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007105 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007106 return;
7107
Willy Tarreau24581ba2010-08-31 22:39:35 +02007108 /* OK so now we know we have to process this response cookie.
7109 * The format of the Set-Cookie header is slightly different
7110 * from the format of the Cookie header in that it does not
7111 * support the comma as a cookie delimiter (thus the header
7112 * cannot be folded) because the Expires attribute described in
7113 * the original Netscape's spec may contain an unquoted date
7114 * with a comma inside. We have to live with this because
7115 * many browsers don't support Max-Age and some browsers don't
7116 * support quoted strings. However the Set-Cookie2 header is
7117 * clean.
7118 *
7119 * We have to keep multiple pointers in order to support cookie
7120 * removal at the beginning, middle or end of header without
7121 * corrupting the header (in case of set-cookie2). A special
7122 * pointer, <scav> points to the beginning of the set-cookie-av
7123 * fields after the first semi-colon. The <next> pointer points
7124 * either to the end of line (set-cookie) or next unquoted comma
7125 * (set-cookie2). All of these headers are valid :
7126 *
7127 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7128 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7129 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7130 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7131 * | | | | | | | | | |
7132 * | | | | | | | | +-> next hdr_end <--+
7133 * | | | | | | | +------------> scav
7134 * | | | | | | +--------------> val_end
7135 * | | | | | +--------------------> val_beg
7136 * | | | | +----------------------> equal
7137 * | | | +------------------------> att_end
7138 * | | +----------------------------> att_beg
7139 * | +------------------------------> prev
7140 * +-----------------------------------------> hdr_beg
7141 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007142
Willy Tarreau24581ba2010-08-31 22:39:35 +02007143 for (; prev < hdr_end; prev = next) {
7144 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007145
Willy Tarreau24581ba2010-08-31 22:39:35 +02007146 /* find att_beg */
7147 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007148 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007149 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007150
Willy Tarreau24581ba2010-08-31 22:39:35 +02007151 /* find att_end : this is the first character after the last non
7152 * space before the equal. It may be equal to hdr_end.
7153 */
7154 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007155
Willy Tarreau24581ba2010-08-31 22:39:35 +02007156 while (equal < hdr_end) {
7157 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7158 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007159 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007160 continue;
7161 att_end = equal;
7162 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007163
Willy Tarreau24581ba2010-08-31 22:39:35 +02007164 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7165 * is between <att_beg> and <equal>, both may be identical.
7166 */
7167
7168 /* look for end of cookie if there is an equal sign */
7169 if (equal < hdr_end && *equal == '=') {
7170 /* look for the beginning of the value */
7171 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007172 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007173 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007174
Willy Tarreau24581ba2010-08-31 22:39:35 +02007175 /* find the end of the value, respecting quotes */
7176 next = find_cookie_value_end(val_beg, hdr_end);
7177
7178 /* make val_end point to the first white space or delimitor after the value */
7179 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007180 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007181 val_end--;
7182 } else {
7183 /* <equal> points to next comma, semi-colon or EOL */
7184 val_beg = val_end = next = equal;
7185 }
7186
7187 if (next < hdr_end) {
7188 /* Set-Cookie2 supports multiple cookies, and <next> points to
7189 * a colon or semi-colon before the end. So skip all attr-value
7190 * pairs and look for the next comma. For Set-Cookie, since
7191 * commas are permitted in values, skip to the end.
7192 */
7193 if (is_cookie2)
7194 next = find_hdr_value_end(next, hdr_end);
7195 else
7196 next = hdr_end;
7197 }
7198
7199 /* Now everything is as on the diagram above */
7200
7201 /* Ignore cookies with no equal sign */
7202 if (equal == val_end)
7203 continue;
7204
7205 /* If there are spaces around the equal sign, we need to
7206 * strip them otherwise we'll get trouble for cookie captures,
7207 * or even for rewrites. Since this happens extremely rarely,
7208 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007209 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007210 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7211 int stripped_before = 0;
7212 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007213
Willy Tarreau24581ba2010-08-31 22:39:35 +02007214 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007215 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007216 equal += stripped_before;
7217 val_beg += stripped_before;
7218 }
7219
7220 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007221 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007222 val_beg += stripped_after;
7223 stripped_before += stripped_after;
7224 }
7225
7226 val_end += stripped_before;
7227 next += stripped_before;
7228 hdr_end += stripped_before;
7229 hdr_next += stripped_before;
7230 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007231 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007232 }
7233
7234 /* First, let's see if we want to capture this cookie. We check
7235 * that we don't already have a server side cookie, because we
7236 * can only capture one. Also as an optimisation, we ignore
7237 * cookies shorter than the declared name.
7238 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007239 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007240 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007241 (val_end - att_beg >= sess->fe->capture_namelen) &&
7242 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007243 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007244 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007245 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007246 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007247 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007248 if (log_len > sess->fe->capture_len)
7249 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007250 memcpy(txn->srv_cookie, att_beg, log_len);
7251 txn->srv_cookie[log_len] = 0;
7252 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007253 }
7254
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007255 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007256 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007257 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007258 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7259 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007260 /* assume passive cookie by default */
7261 txn->flags &= ~TX_SCK_MASK;
7262 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007263
7264 /* If the cookie is in insert mode on a known server, we'll delete
7265 * this occurrence because we'll insert another one later.
7266 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007267 * a direct access.
7268 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007269 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007270 /* The "preserve" flag was set, we don't want to touch the
7271 * server's cookie.
7272 */
7273 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007274 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007275 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007276 /* this cookie must be deleted */
7277 if (*prev == ':' && next == hdr_end) {
7278 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007279 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007280 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7281 txn->hdr_idx.used--;
7282 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007283 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007284 hdr_next += delta;
7285 http_msg_move_end(&txn->rsp, delta);
7286 /* note: while both invalid now, <next> and <hdr_end>
7287 * are still equal, so the for() will stop as expected.
7288 */
7289 } else {
7290 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007291 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007292 next = prev;
7293 hdr_end += delta;
7294 hdr_next += delta;
7295 cur_hdr->len += delta;
7296 http_msg_move_end(&txn->rsp, delta);
7297 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007298 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007299 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007300 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007302 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007303 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007304 * with this server since we know it.
7305 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007306 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007307 next += delta;
7308 hdr_end += delta;
7309 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007310 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007311 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312
Willy Tarreauf1348312010-10-07 15:54:11 +02007313 txn->flags &= ~TX_SCK_MASK;
7314 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007315 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007316 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007317 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007318 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007319 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007320 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007321 next += delta;
7322 hdr_end += delta;
7323 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007324 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007325 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007326
Willy Tarreau827aee92011-03-10 16:55:02 +01007327 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007328 txn->flags &= ~TX_SCK_MASK;
7329 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007330 }
7331 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007332 /* that's done for this cookie, check the next one on the same
7333 * line when next != hdr_end (only if is_cookie2).
7334 */
7335 }
7336 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007338 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339}
7340
7341
Willy Tarreaua15645d2007-03-18 16:22:39 +01007342/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007343 * Parses the Cache-Control and Pragma request header fields to determine if
7344 * the request may be served from the cache and/or if it is cacheable. Updates
7345 * s->txn->flags.
7346 */
7347void check_request_for_cacheability(struct stream *s, struct channel *chn)
7348{
7349 struct http_txn *txn = s->txn;
7350 char *p1, *p2;
7351 char *cur_ptr, *cur_end, *cur_next;
7352 int pragma_found;
7353 int cc_found;
7354 int cur_idx;
7355
7356 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7357 return; /* nothing more to do here */
7358
7359 cur_idx = 0;
7360 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007361 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007362
7363 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7364 struct hdr_idx_elem *cur_hdr;
7365 int val;
7366
7367 cur_hdr = &txn->hdr_idx.v[cur_idx];
7368 cur_ptr = cur_next;
7369 cur_end = cur_ptr + cur_hdr->len;
7370 cur_next = cur_end + cur_hdr->cr + 1;
7371
7372 /* We have one full header between cur_ptr and cur_end, and the
7373 * next header starts at cur_next.
7374 */
7375
7376 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7377 if (val) {
7378 if ((cur_end - (cur_ptr + val) >= 8) &&
7379 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7380 pragma_found = 1;
7381 continue;
7382 }
7383 }
7384
William Lallemand8a16fe02018-05-22 11:04:33 +02007385 /* Don't use the cache and don't try to store if we found the
7386 * Authorization header */
7387 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7388 if (val) {
7389 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7390 txn->flags |= TX_CACHE_IGNORE;
7391 continue;
7392 }
7393
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007394 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7395 if (!val)
7396 continue;
7397
7398 /* OK, right now we know we have a cache-control header at cur_ptr */
7399 cc_found = 1;
7400 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7401
7402 if (p1 >= cur_end) /* no more info */
7403 continue;
7404
7405 /* p1 is at the beginning of the value */
7406 p2 = p1;
7407 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7408 p2++;
7409
7410 /* we have a complete value between p1 and p2. We don't check the
7411 * values after max-age, max-stale nor min-fresh, we simply don't
7412 * use the cache when they're specified.
7413 */
7414 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7415 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7416 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7417 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7418 txn->flags |= TX_CACHE_IGNORE;
7419 continue;
7420 }
7421
7422 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7423 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7424 continue;
7425 }
7426 }
7427
7428 /* RFC7234#5.4:
7429 * When the Cache-Control header field is also present and
7430 * understood in a request, Pragma is ignored.
7431 * When the Cache-Control header field is not present in a
7432 * request, caches MUST consider the no-cache request
7433 * pragma-directive as having the same effect as if
7434 * "Cache-Control: no-cache" were present.
7435 */
7436 if (!cc_found && pragma_found)
7437 txn->flags |= TX_CACHE_IGNORE;
7438}
7439
7440/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007441 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007442 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007443void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007444{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007445 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007446 char *p1, *p2;
7447
7448 char *cur_ptr, *cur_end, *cur_next;
7449 int cur_idx;
7450
Willy Tarreau12b32f22017-12-21 16:08:09 +01007451 if (txn->status < 200) {
7452 /* do not try to cache interim responses! */
7453 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007454 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007455 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007456
7457 /* Iterate through the headers.
7458 * we start with the start line.
7459 */
7460 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007461 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007462
7463 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7464 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007465 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007466
7467 cur_hdr = &txn->hdr_idx.v[cur_idx];
7468 cur_ptr = cur_next;
7469 cur_end = cur_ptr + cur_hdr->len;
7470 cur_next = cur_end + cur_hdr->cr + 1;
7471
7472 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007473 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007474 */
7475
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007476 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7477 if (val) {
7478 if ((cur_end - (cur_ptr + val) >= 8) &&
7479 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7480 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7481 return;
7482 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007483 }
7484
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007485 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7486 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007487 continue;
7488
7489 /* OK, right now we know we have a cache-control header at cur_ptr */
7490
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007491 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007492
7493 if (p1 >= cur_end) /* no more info */
7494 continue;
7495
7496 /* p1 is at the beginning of the value */
7497 p2 = p1;
7498
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007499 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007500 p2++;
7501
7502 /* we have a complete value between p1 and p2 */
7503 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007504 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7505 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7506 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7507 continue;
7508 }
7509
Willy Tarreaua15645d2007-03-18 16:22:39 +01007510 /* we have something of the form no-cache="set-cookie" */
7511 if ((cur_end - p1 >= 21) &&
7512 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7513 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007514 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007515 continue;
7516 }
7517
7518 /* OK, so we know that either p2 points to the end of string or to a comma */
7519 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007520 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007521 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007522 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007523 return;
7524 }
7525
7526 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007527 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007528 continue;
7529 }
7530 }
7531}
7532
Willy Tarreau58f10d72006-12-04 02:26:12 +01007533
Willy Tarreaub2513902006-12-17 14:52:38 +01007534/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007535 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007536 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007537 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007538 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007539 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007540 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007541 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007542 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007543int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007544{
7545 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007546 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007547 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007548
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007549 if (!uri_auth)
7550 return 0;
7551
Cyril Bonté70be45d2010-10-12 00:14:35 +02007552 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007553 return 0;
7554
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007555 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007556 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007557 return 0;
7558
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007559 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007560 return 0;
7561
Willy Tarreaub2513902006-12-17 14:52:38 +01007562 return 1;
7563}
7564
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007565/* Append the description of what is present in error snapshot <es> into <out>.
7566 * The description must be small enough to always fit in a trash. The output
7567 * buffer may be the trash so the trash must not be used inside this function.
7568 */
7569void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7570{
7571 chunk_appendf(&trash,
7572 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7573 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7574 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7575 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7576 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7577 es->ctx.http.m_flags, es->ctx.http.m_clen,
7578 es->ctx.http.m_blen, es->ctx.http.b_flags);
7579}
7580
Willy Tarreau4076a152009-04-02 15:18:36 +02007581/*
7582 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007583 * By default it tries to report the error position as msg->err_pos. However if
7584 * this one is not set, it will then report msg->next, which is the last known
7585 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007586 * displays buffers as a contiguous area starting at buf->p. The direction is
7587 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007588 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007589void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007590 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007591 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007592{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007593 union error_snapshot_ctx ctx;
7594 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007595
Willy Tarreauef3ca732018-09-07 15:47:35 +02007596 /* http-specific part now */
7597 ctx.http.sid = s->uniq_id;
7598 ctx.http.state = state;
7599 ctx.http.b_flags = msg->chn->flags;
7600 ctx.http.s_flags = s->flags;
7601 ctx.http.t_flags = s->txn->flags;
7602 ctx.http.m_flags = msg->flags;
7603 ctx.http.m_clen = msg->chunk_len;
7604 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007605
Willy Tarreauef3ca732018-09-07 15:47:35 +02007606 ofs = msg->chn->total - ci_data(msg->chn);
7607 if (ofs < 0)
7608 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007609
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007610 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007611 other_end, s->target,
7612 strm_sess(s), &msg->chn->buf,
7613 ofs, co_data(msg->chn),
7614 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7615 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007616}
Willy Tarreaub2513902006-12-17 14:52:38 +01007617
Willy Tarreau294c4732011-12-16 21:35:50 +01007618/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7619 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7620 * performed over the whole headers. Otherwise it must contain a valid header
7621 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7622 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7623 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7624 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007625 * -1. The value fetch stops at commas, so this function is suited for use with
7626 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007627 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007628 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007629unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007630 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007631 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007632{
Willy Tarreau294c4732011-12-16 21:35:50 +01007633 struct hdr_ctx local_ctx;
7634 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007635 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007636 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007637 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007638
Willy Tarreau294c4732011-12-16 21:35:50 +01007639 if (!ctx) {
7640 local_ctx.idx = 0;
7641 ctx = &local_ctx;
7642 }
7643
Willy Tarreaubce70882009-09-07 11:51:47 +02007644 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007645 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007646 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007647 occ--;
7648 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007649 *vptr = ctx->line + ctx->val;
7650 *vlen = ctx->vlen;
7651 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007652 }
7653 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007654 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007655 }
7656
7657 /* negative occurrence, we scan all the list then walk back */
7658 if (-occ > MAX_HDR_HISTORY)
7659 return 0;
7660
Willy Tarreau294c4732011-12-16 21:35:50 +01007661 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007662 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007663 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7664 len_hist[hist_ptr] = ctx->vlen;
7665 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007666 hist_ptr = 0;
7667 found++;
7668 }
7669 if (-occ > found)
7670 return 0;
7671 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007672 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7673 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7674 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007675 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007676 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007677 if (hist_ptr >= MAX_HDR_HISTORY)
7678 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007679 *vptr = ptr_hist[hist_ptr];
7680 *vlen = len_hist[hist_ptr];
7681 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007682}
7683
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007684/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7685 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7686 * performed over the whole headers. Otherwise it must contain a valid header
7687 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7688 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7689 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7690 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7691 * -1. This function differs from http_get_hdr() in that it only returns full
7692 * line header values and does not stop at commas.
7693 * The return value is 0 if nothing was found, or non-zero otherwise.
7694 */
7695unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7696 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007697 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007698{
7699 struct hdr_ctx local_ctx;
7700 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007701 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007702 unsigned int hist_ptr;
7703 int found;
7704
7705 if (!ctx) {
7706 local_ctx.idx = 0;
7707 ctx = &local_ctx;
7708 }
7709
7710 if (occ >= 0) {
7711 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007712 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007713 occ--;
7714 if (occ <= 0) {
7715 *vptr = ctx->line + ctx->val;
7716 *vlen = ctx->vlen;
7717 return 1;
7718 }
7719 }
7720 return 0;
7721 }
7722
7723 /* negative occurrence, we scan all the list then walk back */
7724 if (-occ > MAX_HDR_HISTORY)
7725 return 0;
7726
7727 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007728 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007729 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7730 len_hist[hist_ptr] = ctx->vlen;
7731 if (++hist_ptr >= MAX_HDR_HISTORY)
7732 hist_ptr = 0;
7733 found++;
7734 }
7735 if (-occ > found)
7736 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007737
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007738 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007739 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7740 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7741 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007742 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007743 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007744 if (hist_ptr >= MAX_HDR_HISTORY)
7745 hist_ptr -= MAX_HDR_HISTORY;
7746 *vptr = ptr_hist[hist_ptr];
7747 *vlen = len_hist[hist_ptr];
7748 return 1;
7749}
7750
Willy Tarreaubaaee002006-06-26 02:48:02 +02007751/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007752 * Print a debug line with a header. Always stop at the first CR or LF char,
7753 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7754 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007755 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007756void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007757{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007758 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007759 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007760
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007761 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007762 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007763 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007764 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007765
7766 for (max = 0; start + max < end; max++)
7767 if (start[max] == '\r' || start[max] == '\n')
7768 break;
7769
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007770 UBOUND(max, trash.size - trash.data - 3);
7771 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7772 trash.area[trash.data++] = '\n';
7773 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007774}
7775
Willy Tarreaueee5b512015-04-03 23:46:31 +02007776
7777/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7778 * The hdr_idx is allocated as well. In case of allocation failure, everything
7779 * allocated is freed and NULL is returned. Otherwise the new transaction is
7780 * assigned to the stream and returned.
7781 */
7782struct http_txn *http_alloc_txn(struct stream *s)
7783{
7784 struct http_txn *txn = s->txn;
7785
7786 if (txn)
7787 return txn;
7788
Willy Tarreaubafbe012017-11-24 17:34:44 +01007789 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007790 if (!txn)
7791 return txn;
7792
7793 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007794 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007795 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007796 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007797 return NULL;
7798 }
7799
7800 s->txn = txn;
7801 return txn;
7802}
7803
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007804void http_txn_reset_req(struct http_txn *txn)
7805{
7806 txn->req.flags = 0;
7807 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7808 txn->req.next = 0;
7809 txn->req.chunk_len = 0LL;
7810 txn->req.body_len = 0LL;
7811 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7812}
7813
7814void http_txn_reset_res(struct http_txn *txn)
7815{
7816 txn->rsp.flags = 0;
7817 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7818 txn->rsp.next = 0;
7819 txn->rsp.chunk_len = 0LL;
7820 txn->rsp.body_len = 0LL;
7821 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7822}
7823
Willy Tarreau0937bc42009-12-22 15:03:09 +01007824/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007825 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007826 * the required fields are properly allocated and that we only need to (re)init
7827 * them. This should be used before processing any new request.
7828 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007829void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007830{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007831 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007832 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007833
7834 txn->flags = 0;
7835 txn->status = -1;
7836
Willy Tarreauf64d1412010-10-07 20:06:11 +02007837 txn->cookie_first_date = 0;
7838 txn->cookie_last_date = 0;
7839
Willy Tarreaueee5b512015-04-03 23:46:31 +02007840 txn->srv_cookie = NULL;
7841 txn->cli_cookie = NULL;
7842 txn->uri = NULL;
7843
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007844 http_txn_reset_req(txn);
7845 http_txn_reset_res(txn);
7846
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007847 txn->req.chn = &s->req;
7848 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007849
7850 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007851
7852 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7853 if (fe->options2 & PR_O2_REQBUG_OK)
7854 txn->req.err_pos = -1; /* let buggy requests pass */
7855
Willy Tarreau0937bc42009-12-22 15:03:09 +01007856 if (txn->hdr_idx.v)
7857 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007858
7859 vars_init(&s->vars_txn, SCOPE_TXN);
7860 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007861}
7862
7863/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007864void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007865{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007866 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007867 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007868
7869 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007870 pool_free(pool_head_requri, txn->uri);
7871 pool_free(pool_head_capture, txn->cli_cookie);
7872 pool_free(pool_head_capture, txn->srv_cookie);
7873 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007874
William Lallemanda73203e2012-03-12 12:48:57 +01007875 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007876 txn->uri = NULL;
7877 txn->srv_cookie = NULL;
7878 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007879
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007880 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007881 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007882 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007883 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007884 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007885 }
7886
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007887 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007888 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007889 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007890 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007891 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007892 }
7893
Willy Tarreau6204cd92016-03-10 16:33:04 +01007894 vars_prune(&s->vars_txn, s->sess, s);
7895 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007896}
7897
7898/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007899void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007900{
7901 http_end_txn(s);
7902 http_init_txn(s);
7903
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007904 /* reinitialise the current rule list pointer to NULL. We are sure that
7905 * any rulelist match the NULL pointer.
7906 */
7907 s->current_rule_list = NULL;
7908
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007909 s->be = strm_fe(s);
7910 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007911 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007912 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007913 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007914 /* re-init store persistence */
7915 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007916 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007917
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007918 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007919
Willy Tarreau739cfba2010-01-25 23:11:14 +01007920 /* We must trim any excess data from the response buffer, because we
7921 * may have blocked an invalid response from a server that we don't
7922 * want to accidentely forward once we disable the analysers, nor do
7923 * we want those data to come along with next response. A typical
7924 * example of such data would be from a buggy server responding to
7925 * a HEAD with some data, or sending more than the advertised
7926 * content-length.
7927 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007928 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007929 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007930
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007931 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007932 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007933
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007934 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007935 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007936
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007937 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007938 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007939
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007940 s->req.rex = TICK_ETERNITY;
7941 s->req.wex = TICK_ETERNITY;
7942 s->req.analyse_exp = TICK_ETERNITY;
7943 s->res.rex = TICK_ETERNITY;
7944 s->res.wex = TICK_ETERNITY;
7945 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007946 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007947}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007948
Sasha Pachev218f0642014-06-16 12:05:59 -06007949void free_http_res_rules(struct list *r)
7950{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02007951 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06007952
7953 list_for_each_entry_safe(pr, tr, r, list) {
7954 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02007955 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06007956 free(pr);
7957 }
7958}
7959
7960void free_http_req_rules(struct list *r)
7961{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02007962 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01007963
7964 list_for_each_entry_safe(pr, tr, r, list) {
7965 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007966 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01007967 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01007968
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02007969 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01007970 free(pr);
7971 }
7972}
7973
Willy Tarreaue365c0b2013-06-11 16:06:12 +02007974/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02007975struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01007976{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02007977 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02007978 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007979 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02007980 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01007981
Vincent Bernat02779b62016-04-03 13:48:43 +02007982 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01007983 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007984 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01007985 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01007986 }
7987
Willy Tarreau5c2e1982012-12-24 12:00:25 +01007988 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007989 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01007990 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02007991 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04007992 int code;
7993 int hc;
7994
Jarno Huuskonen800d1762017-03-06 14:56:36 +02007995 if (!strcmp(args[0], "tarpit")) {
7996 rule->action = ACT_HTTP_REQ_TARPIT;
7997 rule->deny_status = HTTP_ERR_500;
7998 }
7999 else {
8000 rule->action = ACT_ACTION_DENY;
8001 rule->deny_status = HTTP_ERR_403;
8002 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008003 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008004 if (strcmp(args[cur_arg], "deny_status") == 0) {
8005 cur_arg++;
8006 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008007 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8008 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008009 goto out_err;
8010 }
8011
8012 code = atol(args[cur_arg]);
8013 cur_arg++;
8014 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8015 if (http_err_codes[hc] == code) {
8016 rule->deny_status = hc;
8017 break;
8018 }
8019 }
8020
8021 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008022 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8023 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008024 }
8025 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008026 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008027 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008028 cur_arg = 1;
8029
8030 while(*args[cur_arg]) {
8031 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008032 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008033 cur_arg+=2;
8034 continue;
8035 } else
8036 break;
8037 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008038 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008039 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008040 cur_arg = 1;
8041
8042 if (!*args[cur_arg] ||
8043 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008044 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8045 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008046 goto out_err;
8047 }
8048 rule->arg.nice = atoi(args[cur_arg]);
8049 if (rule->arg.nice < -1024)
8050 rule->arg.nice = -1024;
8051 else if (rule->arg.nice > 1024)
8052 rule->arg.nice = 1024;
8053 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008054 } else if (!strcmp(args[0], "set-tos")) {
8055#ifdef IP_TOS
8056 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008057 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008058 cur_arg = 1;
8059
8060 if (!*args[cur_arg] ||
8061 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008062 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8063 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008064 goto out_err;
8065 }
8066
8067 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8068 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008069 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8070 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008071 goto out_err;
8072 }
8073 cur_arg++;
8074#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008075 ha_alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008076 goto out_err;
8077#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008078 } else if (!strcmp(args[0], "set-mark")) {
8079#ifdef SO_MARK
8080 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008081 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008082 cur_arg = 1;
8083
8084 if (!*args[cur_arg] ||
8085 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008086 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8087 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008088 goto out_err;
8089 }
8090
8091 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8092 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008093 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8094 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008095 goto out_err;
8096 }
8097 cur_arg++;
8098 global.last_checks |= LSTCHK_NETADM;
8099#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008100 ha_alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008101 goto out_err;
8102#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008103 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008104 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008105 cur_arg = 1;
8106
8107 if (!*args[cur_arg] ||
8108 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8109 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008110 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8111 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008112 goto out_err;
8113 }
8114 if (strcmp(args[cur_arg], "silent") == 0)
8115 rule->arg.loglevel = -1;
8116 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8117 goto bad_log_level;
8118 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008119 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008120 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008121 cur_arg = 1;
8122
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008123 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8124 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008125 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8126 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008127 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008128 }
8129
8130 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8131 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8132 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008133
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008134 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008135 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008136 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008137 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008138 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8139 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008140 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008141 goto out_err;
8142 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008143 free(proxy->conf.lfs_file);
8144 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8145 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008146 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008147 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008148 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008149 cur_arg = 1;
8150
8151 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008152 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008153 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8154 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008155 goto out_err;
8156 }
8157
8158 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8159 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8160 LIST_INIT(&rule->arg.hdr_add.fmt);
8161
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008162 error = NULL;
8163 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008164 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8165 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008166 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008167 goto out_err;
8168 }
8169
8170 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008171 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008172 if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008173 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008174 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8175 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008176 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008177 goto out_err;
8178 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008179
8180 free(proxy->conf.lfs_file);
8181 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8182 proxy->conf.lfs_line = proxy->conf.args.line;
8183 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008184 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008185 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008186 cur_arg = 1;
8187
8188 if (!*args[cur_arg] ||
8189 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008190 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8191 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008192 goto out_err;
8193 }
8194
8195 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8196 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8197
8198 proxy->conf.args.ctx = ARGC_HRQ;
8199 free(proxy->conf.lfs_file);
8200 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8201 proxy->conf.lfs_line = proxy->conf.args.line;
8202 cur_arg += 1;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008203 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Willy Tarreau09448f72014-06-25 18:12:15 +02008204 struct sample_expr *expr;
8205 unsigned int where;
8206 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008207 unsigned int tsc_num;
8208 const char *tsc_num_str;
Willy Tarreau09448f72014-06-25 18:12:15 +02008209
8210 cur_arg = 1;
8211 proxy->conf.args.ctx = ARGC_TRK;
8212
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008213 tsc_num_str = &args[0][8];
8214 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8215 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8216 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8217 free(err);
8218 goto out_err;
8219 }
8220
Willy Tarreau09448f72014-06-25 18:12:15 +02008221 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8222 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008223 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8224 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008225 free(err);
8226 goto out_err;
8227 }
8228
8229 where = 0;
8230 if (proxy->cap & PR_CAP_FE)
8231 where |= SMP_VAL_FE_HRQ_HDR;
8232 if (proxy->cap & PR_CAP_BE)
8233 where |= SMP_VAL_BE_HRQ_HDR;
8234
8235 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008236 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8237 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8238 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8239 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008240 free(expr);
8241 goto out_err;
8242 }
8243
8244 if (strcmp(args[cur_arg], "table") == 0) {
8245 cur_arg++;
8246 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008247 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8248 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008249 free(expr);
8250 goto out_err;
8251 }
8252 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008253 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008254 cur_arg++;
8255 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008256 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008257 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008258 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008259 } else if (strcmp(args[0], "redirect") == 0) {
8260 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008261 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008262
Willy Tarreaube4653b2015-05-28 15:26:58 +02008263 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008264 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8265 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008266 goto out_err;
8267 }
8268
8269 /* this redirect rule might already contain a parsed condition which
8270 * we'll pass to the http-request rule.
8271 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008272 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008273 rule->arg.redir = redir;
8274 rule->cond = redir->cond;
8275 redir->cond = NULL;
8276 cur_arg = 2;
8277 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008278 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8279 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008280 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008281 /*
8282 * '+ 8' for 'add-acl('
8283 * '- 9' for 'add-acl(' + trailing ')'
8284 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008285 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008286
8287 cur_arg = 1;
8288
8289 if (!*args[cur_arg] ||
8290 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008291 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8292 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008293 goto out_err;
8294 }
8295
8296 LIST_INIT(&rule->arg.map.key);
8297 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008298 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008299 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008300 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008301 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8302 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008303 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008304 goto out_err;
8305 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008306 free(proxy->conf.lfs_file);
8307 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8308 proxy->conf.lfs_line = proxy->conf.args.line;
8309 cur_arg += 1;
8310 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8311 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008312 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008313 /*
8314 * '+ 8' for 'del-acl('
8315 * '- 9' for 'del-acl(' + trailing ')'
8316 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008317 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008318
8319 cur_arg = 1;
8320
8321 if (!*args[cur_arg] ||
8322 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008323 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8324 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008325 goto out_err;
8326 }
8327
8328 LIST_INIT(&rule->arg.map.key);
8329 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008330 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008331 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008332 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008333 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8334 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008335 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008336 goto out_err;
8337 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008338 free(proxy->conf.lfs_file);
8339 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8340 proxy->conf.lfs_line = proxy->conf.args.line;
8341 cur_arg += 1;
8342 } else if (strncmp(args[0], "del-map", 7) == 0) {
8343 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008344 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008345 /*
8346 * '+ 8' for 'del-map('
8347 * '- 9' for 'del-map(' + trailing ')'
8348 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008349 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008350
8351 cur_arg = 1;
8352
8353 if (!*args[cur_arg] ||
8354 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008355 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8356 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008357 goto out_err;
8358 }
8359
8360 LIST_INIT(&rule->arg.map.key);
8361 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008362 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008363 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008364 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008365 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8366 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008367 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008368 goto out_err;
8369 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008370 free(proxy->conf.lfs_file);
8371 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8372 proxy->conf.lfs_line = proxy->conf.args.line;
8373 cur_arg += 1;
8374 } else if (strncmp(args[0], "set-map", 7) == 0) {
8375 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008376 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008377 /*
8378 * '+ 8' for 'set-map('
8379 * '- 9' for 'set-map(' + trailing ')'
8380 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008381 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008382
8383 cur_arg = 1;
8384
8385 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8386 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008387 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8388 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008389 goto out_err;
8390 }
8391
8392 LIST_INIT(&rule->arg.map.key);
8393 LIST_INIT(&rule->arg.map.value);
8394 proxy->conf.args.ctx = ARGC_HRQ;
8395
8396 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008397 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008398 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008399 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008400 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8401 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008402 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008403 goto out_err;
8404 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008405
8406 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008407 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008408 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008409 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008410 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8411 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008412 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008413 goto out_err;
8414 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008415 free(proxy->conf.lfs_file);
8416 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8417 proxy->conf.lfs_line = proxy->conf.args.line;
8418
8419 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008420 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8421 char *errmsg = NULL;
8422 cur_arg = 1;
8423 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008424 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008425 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008426 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008427 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8428 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008429 free(errmsg);
8430 goto out_err;
8431 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008432 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008433 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008434 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8435 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8436 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8437 "%s%s, but got '%s'%s.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008438 file, linenum, *trash.area ? ", " : "", trash.area,
8439 args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008440 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008441 }
8442
8443 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8444 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008445 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008446
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008447 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008448 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8449 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008450 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008451 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008452 }
8453 rule->cond = cond;
8454 }
8455 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008456 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8457 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8458 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008459 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008460 }
8461
8462 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008463 out_err:
8464 free(rule);
8465 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008466}
8467
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008468/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008469struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008470{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008471 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008472 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008473 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008474 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008475
8476 rule = calloc(1, sizeof(*rule));
8477 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008478 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008479 goto out_err;
8480 }
8481
8482 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008483 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008484 cur_arg = 1;
8485 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008486 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008487 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008488 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008489 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008490 cur_arg = 1;
8491
8492 if (!*args[cur_arg] ||
8493 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008494 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8495 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008496 goto out_err;
8497 }
8498 rule->arg.nice = atoi(args[cur_arg]);
8499 if (rule->arg.nice < -1024)
8500 rule->arg.nice = -1024;
8501 else if (rule->arg.nice > 1024)
8502 rule->arg.nice = 1024;
8503 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008504 } else if (!strcmp(args[0], "set-tos")) {
8505#ifdef IP_TOS
8506 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008507 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008508 cur_arg = 1;
8509
8510 if (!*args[cur_arg] ||
8511 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008512 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8513 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008514 goto out_err;
8515 }
8516
8517 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8518 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008519 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8520 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008521 goto out_err;
8522 }
8523 cur_arg++;
8524#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008525 ha_alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008526 goto out_err;
8527#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008528 } else if (!strcmp(args[0], "set-mark")) {
8529#ifdef SO_MARK
8530 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008531 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008532 cur_arg = 1;
8533
8534 if (!*args[cur_arg] ||
8535 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008536 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8537 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008538 goto out_err;
8539 }
8540
8541 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8542 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008543 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8544 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008545 goto out_err;
8546 }
8547 cur_arg++;
8548 global.last_checks |= LSTCHK_NETADM;
8549#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008550 ha_alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008551 goto out_err;
8552#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008553 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008554 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008555 cur_arg = 1;
8556
8557 if (!*args[cur_arg] ||
8558 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8559 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008560 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8561 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008562 goto out_err;
8563 }
8564 if (strcmp(args[cur_arg], "silent") == 0)
8565 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008566 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008567 goto bad_log_level;
8568 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008569 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008570 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008571 cur_arg = 1;
8572
8573 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8574 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008575 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8576 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008577 goto out_err;
8578 }
8579
8580 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8581 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8582 LIST_INIT(&rule->arg.hdr_add.fmt);
8583
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008584 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008585 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008586 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008587 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008588 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8589 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008590 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008591 goto out_err;
8592 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008593 free(proxy->conf.lfs_file);
8594 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8595 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008596 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008597 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008598 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008599 cur_arg = 1;
8600
8601 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008602 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008603 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8604 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008605 goto out_err;
8606 }
8607
8608 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8609 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8610 LIST_INIT(&rule->arg.hdr_add.fmt);
8611
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008612 error = NULL;
8613 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008614 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8615 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008616 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008617 goto out_err;
8618 }
8619
8620 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008621 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008622 if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008623 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008624 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8625 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008626 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008627 goto out_err;
8628 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008629
8630 free(proxy->conf.lfs_file);
8631 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8632 proxy->conf.lfs_line = proxy->conf.args.line;
8633 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008634 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008635 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008636 cur_arg = 1;
8637
8638 if (!*args[cur_arg] ||
8639 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008640 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8641 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008642 goto out_err;
8643 }
8644
8645 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8646 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8647
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008648 proxy->conf.args.ctx = ARGC_HRS;
8649 free(proxy->conf.lfs_file);
8650 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8651 proxy->conf.lfs_line = proxy->conf.args.line;
8652 cur_arg += 1;
8653 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8654 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008655 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008656 /*
8657 * '+ 8' for 'add-acl('
8658 * '- 9' for 'add-acl(' + trailing ')'
8659 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008660 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008661
8662 cur_arg = 1;
8663
8664 if (!*args[cur_arg] ||
8665 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008666 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8667 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008668 goto out_err;
8669 }
8670
8671 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008672 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008673 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008674 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008675 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008676 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8677 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008678 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008679 goto out_err;
8680 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008681 free(proxy->conf.lfs_file);
8682 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8683 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008684
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008685 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008686 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8687 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008688 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008689 /*
8690 * '+ 8' for 'del-acl('
8691 * '- 9' for 'del-acl(' + trailing ')'
8692 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008693 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008694
8695 cur_arg = 1;
8696
8697 if (!*args[cur_arg] ||
8698 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008699 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8700 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008701 goto out_err;
8702 }
8703
8704 LIST_INIT(&rule->arg.map.key);
8705 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008706 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008707 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008708 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008709 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8710 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008711 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008712 goto out_err;
8713 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008714 free(proxy->conf.lfs_file);
8715 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8716 proxy->conf.lfs_line = proxy->conf.args.line;
8717 cur_arg += 1;
8718 } else if (strncmp(args[0], "del-map", 7) == 0) {
8719 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008720 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008721 /*
8722 * '+ 8' for 'del-map('
8723 * '- 9' for 'del-map(' + trailing ')'
8724 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008725 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008726
8727 cur_arg = 1;
8728
8729 if (!*args[cur_arg] ||
8730 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008731 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8732 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008733 goto out_err;
8734 }
8735
8736 LIST_INIT(&rule->arg.map.key);
8737 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008738 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008739 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008740 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008741 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
8742 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008743 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008744 goto out_err;
8745 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008746 free(proxy->conf.lfs_file);
8747 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8748 proxy->conf.lfs_line = proxy->conf.args.line;
8749 cur_arg += 1;
8750 } else if (strncmp(args[0], "set-map", 7) == 0) {
8751 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008752 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008753 /*
8754 * '+ 8' for 'set-map('
8755 * '- 9' for 'set-map(' + trailing ')'
8756 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008757 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008758
8759 cur_arg = 1;
8760
8761 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8762 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008763 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8764 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008765 goto out_err;
8766 }
8767
8768 LIST_INIT(&rule->arg.map.key);
8769 LIST_INIT(&rule->arg.map.value);
8770
8771 proxy->conf.args.ctx = ARGC_HRS;
8772
8773 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008774 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008775 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008776 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008777 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
8778 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008779 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008780 goto out_err;
8781 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008782
8783 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008784 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008785 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008786 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008787 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
8788 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008789 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008790 goto out_err;
8791 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008792
8793 free(proxy->conf.lfs_file);
8794 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8795 proxy->conf.lfs_line = proxy->conf.args.line;
8796
8797 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008798 } else if (strcmp(args[0], "redirect") == 0) {
8799 struct redirect_rule *redir;
8800 char *errmsg = NULL;
8801
8802 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008803 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8804 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02008805 goto out_err;
8806 }
8807
8808 /* this redirect rule might already contain a parsed condition which
8809 * we'll pass to the http-request rule.
8810 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008811 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008812 rule->arg.redir = redir;
8813 rule->cond = redir->cond;
8814 redir->cond = NULL;
8815 cur_arg = 2;
8816 return rule;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008817 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008818 struct sample_expr *expr;
8819 unsigned int where;
8820 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008821 unsigned int tsc_num;
8822 const char *tsc_num_str;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008823
8824 cur_arg = 1;
8825 proxy->conf.args.ctx = ARGC_TRK;
8826
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008827 tsc_num_str = &args[0][8];
8828 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8829 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8830 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8831 free(err);
8832 goto out_err;
8833 }
8834
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008835 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8836 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008837 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8838 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008839 free(err);
8840 goto out_err;
8841 }
8842
8843 where = 0;
8844 if (proxy->cap & PR_CAP_FE)
8845 where |= SMP_VAL_FE_HRS_HDR;
8846 if (proxy->cap & PR_CAP_BE)
8847 where |= SMP_VAL_BE_HRS_HDR;
8848
8849 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008850 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
8851 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8852 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8853 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008854 free(expr);
8855 goto out_err;
8856 }
8857
8858 if (strcmp(args[cur_arg], "table") == 0) {
8859 cur_arg++;
8860 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008861 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
8862 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008863 free(expr);
8864 goto out_err;
8865 }
8866 /* we copy the table name for now, it will be resolved later */
8867 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
8868 cur_arg++;
8869 }
8870 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008871 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008872 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02008873 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
8874 char *errmsg = NULL;
8875 cur_arg = 1;
8876 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008877 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008878 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008879 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008880 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8881 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008882 free(errmsg);
8883 goto out_err;
8884 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008885 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008886 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008887 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
8888 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8889 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8890 "%s%s, but got '%s'%s.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008891 file, linenum, *trash.area ? ", " : "", trash.area,
8892 args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008893 goto out_err;
8894 }
8895
8896 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8897 struct acl_cond *cond;
8898 char *errmsg = NULL;
8899
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008900 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008901 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8902 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008903 free(errmsg);
8904 goto out_err;
8905 }
8906 rule->cond = cond;
8907 }
8908 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008909 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
8910 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8911 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008912 goto out_err;
8913 }
8914
8915 return rule;
8916 out_err:
8917 free(rule);
8918 return NULL;
8919}
8920
Willy Tarreau4baae242012-12-27 12:00:31 +01008921/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008922 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02008923 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
8924 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01008925 */
8926struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02008927 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01008928{
8929 struct redirect_rule *rule;
8930 int cur_arg;
8931 int type = REDIRECT_TYPE_NONE;
8932 int code = 302;
8933 const char *destination = NULL;
8934 const char *cookie = NULL;
8935 int cookie_set = 0;
8936 unsigned int flags = REDIRECT_FLAG_NONE;
8937 struct acl_cond *cond = NULL;
8938
8939 cur_arg = 0;
8940 while (*(args[cur_arg])) {
8941 if (strcmp(args[cur_arg], "location") == 0) {
8942 if (!*args[cur_arg + 1])
8943 goto missing_arg;
8944
8945 type = REDIRECT_TYPE_LOCATION;
8946 cur_arg++;
8947 destination = args[cur_arg];
8948 }
8949 else if (strcmp(args[cur_arg], "prefix") == 0) {
8950 if (!*args[cur_arg + 1])
8951 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01008952 type = REDIRECT_TYPE_PREFIX;
8953 cur_arg++;
8954 destination = args[cur_arg];
8955 }
8956 else if (strcmp(args[cur_arg], "scheme") == 0) {
8957 if (!*args[cur_arg + 1])
8958 goto missing_arg;
8959
8960 type = REDIRECT_TYPE_SCHEME;
8961 cur_arg++;
8962 destination = args[cur_arg];
8963 }
8964 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8965 if (!*args[cur_arg + 1])
8966 goto missing_arg;
8967
8968 cur_arg++;
8969 cookie = args[cur_arg];
8970 cookie_set = 1;
8971 }
8972 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8973 if (!*args[cur_arg + 1])
8974 goto missing_arg;
8975
8976 cur_arg++;
8977 cookie = args[cur_arg];
8978 cookie_set = 0;
8979 }
8980 else if (strcmp(args[cur_arg], "code") == 0) {
8981 if (!*args[cur_arg + 1])
8982 goto missing_arg;
8983
8984 cur_arg++;
8985 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008986 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008987 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008988 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008989 args[cur_arg - 1], args[cur_arg]);
8990 return NULL;
8991 }
8992 }
8993 else if (!strcmp(args[cur_arg],"drop-query")) {
8994 flags |= REDIRECT_FLAG_DROP_QS;
8995 }
8996 else if (!strcmp(args[cur_arg],"append-slash")) {
8997 flags |= REDIRECT_FLAG_APPEND_SLASH;
8998 }
8999 else if (strcmp(args[cur_arg], "if") == 0 ||
9000 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01009001 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009002 if (!cond) {
9003 memprintf(errmsg, "error in condition: %s", *errmsg);
9004 return NULL;
9005 }
9006 break;
9007 }
9008 else {
9009 memprintf(errmsg,
9010 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9011 args[cur_arg]);
9012 return NULL;
9013 }
9014 cur_arg++;
9015 }
9016
9017 if (type == REDIRECT_TYPE_NONE) {
9018 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9019 return NULL;
9020 }
9021
Willy Tarreaube4653b2015-05-28 15:26:58 +02009022 if (dir && type != REDIRECT_TYPE_LOCATION) {
9023 memprintf(errmsg, "response only supports redirect type 'location'");
9024 return NULL;
9025 }
9026
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009027 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009028 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009029 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009030
9031 if (!use_fmt) {
9032 /* old-style static redirect rule */
9033 rule->rdr_str = strdup(destination);
9034 rule->rdr_len = strlen(destination);
9035 }
9036 else {
9037 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009038
9039 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9040 * if prefix == "/", we don't want to add anything, otherwise it
9041 * makes it hard for the user to configure a self-redirection.
9042 */
Godbachd9722032014-12-18 15:44:58 +08009043 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009044 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009045 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9046 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009047 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9048 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009049 return NULL;
9050 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009051 free(curproxy->conf.lfs_file);
9052 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9053 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009054 }
9055 }
9056
Willy Tarreau4baae242012-12-27 12:00:31 +01009057 if (cookie) {
9058 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9059 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9060 */
9061 rule->cookie_len = strlen(cookie);
9062 if (cookie_set) {
9063 rule->cookie_str = malloc(rule->cookie_len + 10);
9064 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9065 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9066 rule->cookie_len += 9;
9067 } else {
9068 rule->cookie_str = malloc(rule->cookie_len + 21);
9069 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9070 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9071 rule->cookie_len += 20;
9072 }
9073 }
9074 rule->type = type;
9075 rule->code = code;
9076 rule->flags = flags;
9077 LIST_INIT(&rule->list);
9078 return rule;
9079
9080 missing_arg:
9081 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9082 return NULL;
9083}
9084
Willy Tarreau8797c062007-05-07 00:55:35 +02009085/************************************************************************/
9086/* The code below is dedicated to ACL parsing and matching */
9087/************************************************************************/
9088
9089
Willy Tarreau14174bc2012-04-16 14:34:04 +02009090/* This function ensures that the prerequisites for an L7 fetch are ready,
9091 * which means that a request or response is ready. If some data is missing,
9092 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009093 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9094 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009095 *
9096 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009097 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9098 * decide whether or not an HTTP message is present ;
9099 * 0 if the requested data cannot be fetched or if it is certain that
9100 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009101 * 1 if an HTTP message is ready
9102 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009103int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009104 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009105{
Willy Tarreau192252e2015-04-04 01:47:55 +02009106 struct http_txn *txn;
9107 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009108
Willy Tarreaube508f12016-03-10 11:47:01 +01009109 /* Note: it is possible that <s> is NULL when called before stream
9110 * initialization (eg: tcp-request connection), so this function is the
9111 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009112 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009113 if (!s)
9114 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009115
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009116 if (!s->txn) {
9117 if (unlikely(!http_alloc_txn(s)))
9118 return 0; /* not enough memory */
9119 http_init_txn(s);
9120 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009121 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009122 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009123
9124 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009125 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009126
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009127 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009128 /* If the buffer does not leave enough free space at the end,
9129 * we must first realign it.
9130 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009131 if (ci_head(&s->req) > b_orig(&s->req.buf) &&
9132 ci_head(&s->req) + ci_data(&s->req) > b_wrap(&s->req.buf) - global.tune.maxrewrite)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009133 channel_slow_realign(&s->req, trash.area);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009134
Willy Tarreau14174bc2012-04-16 14:34:04 +02009135 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009136 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009137 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009138
9139 /* Try to decode HTTP request */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009140 if (likely(msg->next < ci_data(&s->req)))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009141 http_msg_analyzer(msg, &txn->hdr_idx);
9142
9143 /* Still no valid request ? */
9144 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009145 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau23752332018-06-15 14:54:53 +02009146 channel_full(&s->req, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009147 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009148 }
9149 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009150 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009151 return 0;
9152 }
9153
9154 /* OK we just got a valid HTTP request. We have some minor
9155 * preparation to perform so that further checks can rely
9156 * on HTTP tests.
9157 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009158
9159 /* If the request was parsed but was too large, we must absolutely
9160 * return an error so that it is not processed. At the moment this
9161 * cannot happen, but if the parsers are to change in the future,
9162 * we want this check to be maintained.
9163 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009164 if (unlikely(ci_head(&s->req) + ci_data(&s->req) >
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009165 b_wrap(&s->req.buf) - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009166 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009167 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009168 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009169 return 1;
9170 }
9171
Willy Tarreauf37954d2018-06-15 18:31:02 +02009172 txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009173 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009174 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009175
Willy Tarreau506d0502013-07-06 13:29:24 +02009176 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9177 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009178 }
9179
Willy Tarreau506d0502013-07-06 13:29:24 +02009180 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009181 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009182 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009183
9184 /* otherwise everything's ready for the request */
9185 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009186 else {
9187 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009188 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9189 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009190 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009191 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009192 }
9193
9194 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009195 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009196 return 1;
9197}
Willy Tarreau8797c062007-05-07 00:55:35 +02009198
Willy Tarreau8797c062007-05-07 00:55:35 +02009199/* 1. Check on METHOD
9200 * We use the pre-parsed method if it is known, and store its number as an
9201 * integer. If it is unknown, we use the pointer and the length.
9202 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009203static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009204{
9205 int len, meth;
9206
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009207 len = strlen(text);
9208 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009209
9210 pattern->val.i = meth;
9211 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009212 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009213 pattern->len = len;
9214 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009215 else {
9216 pattern->ptr.str = NULL;
9217 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009218 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009219 return 1;
9220}
9221
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009222/* This function fetches the method of current HTTP request and stores
9223 * it in the global pattern struct as a chunk. There are two possibilities :
9224 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9225 * in <len> and <ptr> is NULL ;
9226 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9227 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009228 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009229 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009230static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009231smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009232{
9233 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009234 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009235
Willy Tarreau24e32d82012-04-23 23:55:44 +02009236 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009237
Willy Tarreaube508f12016-03-10 11:47:01 +01009238 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009239 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009240 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009241 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009242 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009243 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9244 /* ensure the indexes are not affected */
9245 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009246 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009247 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
9248 smp->data.u.meth.str.area = ci_head(txn->req.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009249 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009250 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009251 return 1;
9252}
9253
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009254/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009255static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009256{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009257 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009258 struct pattern_list *lst;
9259 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009260
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009261 list_for_each_entry(lst, &expr->patterns, list) {
9262 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009263
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009264 /* well-known method */
9265 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009266 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009267 return pattern;
9268 else
9269 continue;
9270 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009271
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009272 /* Other method, we must compare the strings */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009273 if (pattern->len != smp->data.u.meth.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009274 continue;
9275
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009276 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009277 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.area, smp->data.u.meth.str.data) == 0) ||
9278 (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.area, smp->data.u.meth.str.data) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009279 return pattern;
9280 }
9281 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009282}
9283
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009284static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009285smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009286{
Willy Tarreaube508f12016-03-10 11:47:01 +01009287 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009288 char *ptr;
9289 int len;
9290
Willy Tarreauc0239e02012-04-16 14:42:55 +02009291 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009292
Willy Tarreaube508f12016-03-10 11:47:01 +01009293 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009294 len = txn->req.sl.rq.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009295 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009296
9297 while ((len-- > 0) && (*ptr++ != '/'));
9298 if (len <= 0)
9299 return 0;
9300
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009301 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009302 smp->data.u.str.area = ptr;
9303 smp->data.u.str.data = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009304
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009305 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009306 return 1;
9307}
9308
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009309static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009310smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009311{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009312 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009313 char *ptr;
9314 int len;
9315
Willy Tarreauc0239e02012-04-16 14:42:55 +02009316 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009317
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009318 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009319 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9320 return 0;
9321
Willy Tarreau8797c062007-05-07 00:55:35 +02009322 len = txn->rsp.sl.st.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009323 ptr = ci_head(txn->rsp.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009324
9325 while ((len-- > 0) && (*ptr++ != '/'));
9326 if (len <= 0)
9327 return 0;
9328
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009329 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009330 smp->data.u.str.area = ptr;
9331 smp->data.u.str.data = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009332
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009333 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009334 return 1;
9335}
9336
9337/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009338static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009339smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009340{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009341 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009342 char *ptr;
9343 int len;
9344
Willy Tarreauc0239e02012-04-16 14:42:55 +02009345 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009346
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009347 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009348 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9349 return 0;
9350
Willy Tarreau8797c062007-05-07 00:55:35 +02009351 len = txn->rsp.sl.st.c_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009352 ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009353
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009354 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009355 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009356 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009357 return 1;
9358}
9359
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009360static int
9361smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9362{
9363 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9364 return 0;
9365
9366 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009367 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009368 return 0;
9369 smp->strm->unique_id[0] = '\0';
9370 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009371 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009372 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9373
9374 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009375 smp->data.u.str.area = smp->strm->unique_id;
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009376 smp->flags = SMP_F_CONST;
9377 return 1;
9378}
9379
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009380/* Returns a string block containing all headers including the
9381 * empty line wich separes headers from the body. This is useful
9382 * form some headers analysis.
9383 */
9384static int
9385smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9386{
9387 struct http_msg *msg;
9388 struct hdr_idx *idx;
9389 struct http_txn *txn;
9390
9391 CHECK_HTTP_MESSAGE_FIRST();
9392
9393 txn = smp->strm->txn;
9394 idx = &txn->hdr_idx;
9395 msg = &txn->req;
9396
9397 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009398 smp->data.u.str.area = ci_head(msg->chn) + hdr_idx_first_pos(idx);
9399 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Willy Tarreauf37954d2018-06-15 18:31:02 +02009400 (ci_head(msg->chn)[msg->eoh] == '\r');
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009401
9402 return 1;
9403}
9404
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009405/* Returns the header request in a length/value encoded format.
9406 * This is useful for exchanges with the SPOE.
9407 *
9408 * A "length value" is a multibyte code encoding numbers. It uses the
9409 * SPOE format. The encoding is the following:
9410 *
9411 * Each couple "header name" / "header value" is composed
9412 * like this:
9413 * "length value" "header name bytes"
9414 * "length value" "header value bytes"
9415 * When the last header is reached, the header name and the header
9416 * value are empty. Their length are 0
9417 */
9418static int
9419smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9420{
9421 struct http_msg *msg;
Willy Tarreau83061a82018-07-13 11:56:34 +02009422 struct buffer *temp;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009423 struct hdr_idx *idx;
9424 const char *cur_ptr, *cur_next, *p;
9425 int old_idx, cur_idx;
9426 struct hdr_idx_elem *cur_hdr;
9427 const char *hn, *hv;
9428 int hnl, hvl;
9429 int ret;
9430 struct http_txn *txn;
9431 char *buf;
9432 char *end;
9433
9434 CHECK_HTTP_MESSAGE_FIRST();
9435
9436 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009437 buf = temp->area;
9438 end = temp->area + temp->size;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009439
9440 txn = smp->strm->txn;
9441 idx = &txn->hdr_idx;
9442 msg = &txn->req;
9443
9444 /* Build array of headers. */
9445 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009446 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx);
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009447 while (1) {
9448 cur_idx = idx->v[old_idx].next;
9449 if (!cur_idx)
9450 break;
9451 old_idx = cur_idx;
9452
9453 cur_hdr = &idx->v[cur_idx];
9454 cur_ptr = cur_next;
9455 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9456
9457 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9458 * and the next header starts at cur_next. We'll check
9459 * this header in the list as well as against the default
9460 * rule.
9461 */
9462
9463 /* look for ': *'. */
9464 hn = cur_ptr;
9465 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9466 if (p >= cur_ptr+cur_hdr->len)
9467 continue;
9468 hnl = p - hn;
9469 p++;
9470 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9471 p++;
9472 if (p >= cur_ptr + cur_hdr->len)
9473 continue;
9474 hv = p;
9475 hvl = cur_ptr + cur_hdr->len-p;
9476
9477 /* encode the header name. */
9478 ret = encode_varint(hnl, &buf, end);
9479 if (ret == -1)
9480 return 0;
9481 if (buf + hnl > end)
9482 return 0;
9483 memcpy(buf, hn, hnl);
9484 buf += hnl;
9485
9486 /* encode and copy the value. */
9487 ret = encode_varint(hvl, &buf, end);
9488 if (ret == -1)
9489 return 0;
9490 if (buf + hvl > end)
9491 return 0;
9492 memcpy(buf, hv, hvl);
9493 buf += hvl;
9494 }
9495
9496 /* encode the end of the header list with empty
9497 * header name and header value.
9498 */
9499 ret = encode_varint(0, &buf, end);
9500 if (ret == -1)
9501 return 0;
9502 ret = encode_varint(0, &buf, end);
9503 if (ret == -1)
9504 return 0;
9505
9506 /* Initialise sample data which will be filled. */
9507 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009508 smp->data.u.str.area = temp->area;
9509 smp->data.u.str.data = buf - temp->area;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009510 smp->data.u.str.size = temp->size;
9511
9512 return 1;
9513}
9514
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009515/* returns the longest available part of the body. This requires that the body
9516 * has been waited for using http-buffer-request.
9517 */
9518static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009519smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009520{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009521 struct http_msg *msg;
9522 unsigned long len;
9523 unsigned long block1;
9524 char *body;
Willy Tarreau83061a82018-07-13 11:56:34 +02009525 struct buffer *temp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009526
9527 CHECK_HTTP_MESSAGE_FIRST();
9528
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009529 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009530 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009531 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009532 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009533
9534 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +02009535 body = c_ptr(msg->chn, -http_data_rewind(msg));
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009536
9537 block1 = len;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009538 if (block1 > b_wrap(&msg->chn->buf) - body)
9539 block1 = b_wrap(&msg->chn->buf) - body;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009540
9541 if (block1 == len) {
9542 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009543 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009544 smp->data.u.str.area = body;
9545 smp->data.u.str.data = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009546 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9547 }
9548 else {
9549 /* buffer is wrapped, we need to defragment it */
9550 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009551 memcpy(temp->area, body, block1);
9552 memcpy(temp->area + block1, b_orig(&msg->chn->buf),
9553 len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009554 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009555 smp->data.u.str.area = temp->area;
9556 smp->data.u.str.data = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009557 smp->flags = SMP_F_VOL_TEST;
9558 }
9559 return 1;
9560}
9561
9562
9563/* returns the available length of the body. This requires that the body
9564 * has been waited for using http-buffer-request.
9565 */
9566static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009567smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009568{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009569 struct http_msg *msg;
9570
9571 CHECK_HTTP_MESSAGE_FIRST();
9572
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009573 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009574 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009575 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009576 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009577
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009578 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009579 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009580
9581 smp->flags = SMP_F_VOL_TEST;
9582 return 1;
9583}
9584
9585
9586/* returns the advertised length of the body, or the advertised size of the
9587 * chunks available in the buffer. This requires that the body has been waited
9588 * for using http-buffer-request.
9589 */
9590static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009591smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009592{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009593 struct http_msg *msg;
9594
9595 CHECK_HTTP_MESSAGE_FIRST();
9596
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009597 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009598 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009599 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009600 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009601
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009602 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009603 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009604
9605 smp->flags = SMP_F_VOL_TEST;
9606 return 1;
9607}
9608
9609
Willy Tarreau8797c062007-05-07 00:55:35 +02009610/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009611static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009612smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009613{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009614 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009615
Willy Tarreauc0239e02012-04-16 14:42:55 +02009616 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009617
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009618 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009619 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009620 smp->data.u.str.data = txn->req.sl.rq.u_l;
9621 smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009622 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009623 return 1;
9624}
9625
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009626static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009627smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009628{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009629 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009630 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009631
Willy Tarreauc0239e02012-04-16 14:42:55 +02009632 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009633
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009634 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009635 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009636 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009637 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009638
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009639 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009640 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009641 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009642 return 1;
9643}
9644
9645static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009646smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009647{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009648 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009649 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009650
Willy Tarreauc0239e02012-04-16 14:42:55 +02009651 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009652
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009653 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009654 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009655 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9656 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009657
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009658 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009659 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009660 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009661 return 1;
9662}
9663
Willy Tarreau185b5c42012-04-26 15:11:51 +02009664/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9665 * Accepts an optional argument of type string containing the header field name,
9666 * and an optional argument of type signed or unsigned integer to request an
9667 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009668 * headers are considered from the first one. It does not stop on commas and
9669 * returns full lines instead (useful for User-Agent or Date for example).
9670 */
9671static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009672smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009673{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009674 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009675 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009676 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009677 int occ = 0;
9678 const char *name_str = NULL;
9679 int name_len = 0;
9680
9681 if (!ctx) {
9682 /* first call */
9683 ctx = &static_hdr_ctx;
9684 ctx->idx = 0;
9685 smp->ctx.a[0] = ctx;
9686 }
9687
9688 if (args) {
9689 if (args[0].type != ARGT_STR)
9690 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009691 name_str = args[0].data.str.area;
9692 name_len = args[0].data.str.data;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009693
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009694 if (args[1].type == ARGT_SINT)
9695 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009696 }
9697
9698 CHECK_HTTP_MESSAGE_FIRST();
9699
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009700 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009701 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009702
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009703 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9704 /* search for header from the beginning */
9705 ctx->idx = 0;
9706
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009707 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009708 /* no explicit occurrence and single fetch => last header by default */
9709 occ = -1;
9710
9711 if (!occ)
9712 /* prepare to report multiple occurrences for ACL fetches */
9713 smp->flags |= SMP_F_NOT_LAST;
9714
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009715 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009716 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009717 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009718 return 1;
9719
9720 smp->flags &= ~SMP_F_NOT_LAST;
9721 return 0;
9722}
9723
9724/* 6. Check on HTTP header count. The number of occurrences is returned.
9725 * Accepts exactly 1 argument of type string. It does not stop on commas and
9726 * returns full lines instead (useful for User-Agent or Date for example).
9727 */
9728static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009729smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009730{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009731 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009732 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009733 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009734 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009735 const char *name = NULL;
9736 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009737
Willy Tarreau601a4d12015-04-01 19:16:09 +02009738 if (args && args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009739 name = args->data.str.area;
9740 len = args->data.str.data;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009741 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009742
9743 CHECK_HTTP_MESSAGE_FIRST();
9744
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009745 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009746 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009747
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009748 ctx.idx = 0;
9749 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009750 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009751 cnt++;
9752
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009753 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009754 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009755 smp->flags = SMP_F_VOL_HDR;
9756 return 1;
9757}
9758
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009759static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009760smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009761{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009762 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009763 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009764 const struct http_msg *msg;
Willy Tarreau83061a82018-07-13 11:56:34 +02009765 struct buffer *temp;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009766 char del = ',';
9767
9768 if (args && args->type == ARGT_STR)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009769 del = *args[0].data.str.area;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009770
9771 CHECK_HTTP_MESSAGE_FIRST();
9772
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009773 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009774 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009775
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009776 temp = get_trash_chunk();
9777
9778 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009779 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009780 if (temp->data)
9781 temp->area[temp->data++] = del;
9782 memcpy(temp->area + temp->data, ctx.line, ctx.del);
9783 temp->data += ctx.del;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009784 }
9785
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009786 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009787 smp->data.u.str.area = temp->area;
9788 smp->data.u.str.data = temp->data;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009789 smp->flags = SMP_F_VOL_HDR;
9790 return 1;
9791}
9792
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009793/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9794 * Accepts an optional argument of type string containing the header field name,
9795 * and an optional argument of type signed or unsigned integer to request an
9796 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009797 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009798 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009799static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009800smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009801{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009802 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009803 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009804 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009805 int occ = 0;
9806 const char *name_str = NULL;
9807 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009808
Willy Tarreaua890d072013-04-02 12:01:06 +02009809 if (!ctx) {
9810 /* first call */
9811 ctx = &static_hdr_ctx;
9812 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009813 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009814 }
9815
Willy Tarreau185b5c42012-04-26 15:11:51 +02009816 if (args) {
9817 if (args[0].type != ARGT_STR)
9818 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009819 name_str = args[0].data.str.area;
9820 name_len = args[0].data.str.data;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009821
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009822 if (args[1].type == ARGT_SINT)
9823 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009824 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009825
Willy Tarreaue333ec92012-04-16 16:26:40 +02009826 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009827
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009828 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009829 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009830
Willy Tarreau185b5c42012-04-26 15:11:51 +02009831 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009832 /* search for header from the beginning */
9833 ctx->idx = 0;
9834
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009835 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +02009836 /* no explicit occurrence and single fetch => last header by default */
9837 occ = -1;
9838
9839 if (!occ)
9840 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009841 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009842
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009843 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009844 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009845 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009846 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009847
Willy Tarreau37406352012-04-23 16:16:37 +02009848 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009849 return 0;
9850}
9851
Willy Tarreauc11416f2007-06-17 16:58:38 +02009852/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009853 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009854 */
9855static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009856smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009857{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009858 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009859 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009860 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009861 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009862 const char *name = NULL;
9863 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02009864
Willy Tarreau601a4d12015-04-01 19:16:09 +02009865 if (args && args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009866 name = args->data.str.area;
9867 len = args->data.str.data;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009868 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009869
Willy Tarreaue333ec92012-04-16 16:26:40 +02009870 CHECK_HTTP_MESSAGE_FIRST();
9871
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009872 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009873 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009874
Willy Tarreau33a7e692007-06-10 19:45:56 +02009875 ctx.idx = 0;
9876 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009877 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009878 cnt++;
9879
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009880 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009881 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009882 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009883 return 1;
9884}
9885
Willy Tarreau185b5c42012-04-26 15:11:51 +02009886/* Fetch an HTTP header's integer value. The integer value is returned. It
9887 * takes a mandatory argument of type string and an optional one of type int
9888 * to designate a specific occurrence. It returns an unsigned integer, which
9889 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009890 */
9891static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009892smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009893{
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009894 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009895
Willy Tarreauf853c462012-04-23 18:53:56 +02009896 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009897 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009898 smp->data.u.sint = strl2ic(smp->data.u.str.area,
9899 smp->data.u.str.data);
Willy Tarreauf853c462012-04-23 18:53:56 +02009900 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009901
Willy Tarreaud53e2422012-04-16 17:21:11 +02009902 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009903}
9904
Cyril Bonté69fa9922012-10-25 00:01:06 +02009905/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9906 * and an optional one of type int to designate a specific occurrence.
9907 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009908 */
9909static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009910smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +02009911{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009912 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009913
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009914 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009915 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009916 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009917 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009918 } else {
Willy Tarreau83061a82018-07-13 11:56:34 +02009919 struct buffer *temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009920 if (smp->data.u.str.data < temp->size - 1) {
9921 memcpy(temp->area, smp->data.u.str.area,
9922 smp->data.u.str.data);
9923 temp->area[smp->data.u.str.data] = '\0';
9924 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009925 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009926 break;
9927 }
9928 }
9929 }
9930
Willy Tarreaud53e2422012-04-16 17:21:11 +02009931 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009932 if (!(smp->flags & SMP_F_NOT_LAST))
9933 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009934 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009935 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009936}
9937
Willy Tarreau737b0c12007-06-10 21:28:46 +02009938/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9939 * the first '/' after the possible hostname, and ends before the possible '?'.
9940 */
9941static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009942smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009943{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009944 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009945 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009946
Willy Tarreauc0239e02012-04-16 14:42:55 +02009947 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009948
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009949 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009950 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +02009951 ptr = http_txn_get_path(txn);
Willy Tarreau21d2af32008-02-14 20:25:24 +01009952 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009953 return 0;
9954
9955 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009956 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009957 smp->data.u.str.area = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009958
9959 while (ptr < end && *ptr != '?')
9960 ptr++;
9961
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009962 smp->data.u.str.data = ptr - smp->data.u.str.area;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009963 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009964 return 1;
9965}
9966
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009967/* This produces a concatenation of the first occurrence of the Host header
9968 * followed by the path component if it begins with a slash ('/'). This means
9969 * that '*' will not be added, resulting in exactly the first Host entry.
9970 * If no Host header is found, then the path is returned as-is. The returned
9971 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009972 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009973 */
9974static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009975smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009976{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009977 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009978 char *ptr, *end, *beg;
9979 struct hdr_ctx ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02009980 struct buffer *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009981
9982 CHECK_HTTP_MESSAGE_FIRST();
9983
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009984 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009985 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009986 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009987 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009988
9989 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +02009990 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009991 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009992 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009993 smp->data.u.str.area = temp->area;
9994 smp->data.u.str.data = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009995
9996 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009997 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +02009998 beg = http_txn_get_path(txn);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009999 if (!beg)
10000 beg = end;
10001
10002 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10003
10004 if (beg < ptr && *beg == '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010005 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
10006 ptr - beg);
10007 smp->data.u.str.data += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010008 }
10009
10010 smp->flags = SMP_F_VOL_1ST;
10011 return 1;
10012}
10013
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010014/* This produces a 32-bit hash of the concatenation of the first occurrence of
10015 * the Host header followed by the path component if it begins with a slash ('/').
10016 * This means that '*' will not be added, resulting in exactly the first Host
10017 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010018 * is hashed using the path hash followed by a full avalanche hash and provides a
10019 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010020 * high-traffic sites without having to store whole paths.
10021 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010022int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010023smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010024{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010025 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010026 struct hdr_ctx ctx;
10027 unsigned int hash = 0;
10028 char *ptr, *beg, *end;
10029 int len;
10030
10031 CHECK_HTTP_MESSAGE_FIRST();
10032
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010033 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010034 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010035 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010036 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10037 ptr = ctx.line + ctx.val;
10038 len = ctx.vlen;
10039 while (len--)
10040 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10041 }
10042
10043 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020010044 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +020010045 beg = http_txn_get_path(txn);
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010046 if (!beg)
10047 beg = end;
10048
10049 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10050
10051 if (beg < ptr && *beg == '/') {
10052 while (beg < ptr)
10053 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10054 }
10055 hash = full_hash(hash);
10056
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010057 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010058 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010059 smp->flags = SMP_F_VOL_1ST;
10060 return 1;
10061}
10062
Willy Tarreau4a550602012-12-09 14:53:32 +010010063/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010064 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10065 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10066 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010067 * that in environments where IPv6 is insignificant, truncating the output to
10068 * 8 bytes would still work.
10069 */
10070static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010071smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010072{
Willy Tarreau83061a82018-07-13 11:56:34 +020010073 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010074 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010075
10076 if (!cli_conn)
10077 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010078
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010079 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010080 return 0;
10081
Willy Tarreau47ca5452012-12-23 20:22:19 +010010082 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010083 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
10084 temp->data += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010085
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010086 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010087 case AF_INET:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010088 memcpy(temp->area + temp->data,
10089 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
10090 4);
10091 temp->data += 4;
Willy Tarreau4a550602012-12-09 14:53:32 +010010092 break;
10093 case AF_INET6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010094 memcpy(temp->area + temp->data,
10095 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
10096 16);
10097 temp->data += 16;
Willy Tarreau4a550602012-12-09 14:53:32 +010010098 break;
10099 default:
10100 return 0;
10101 }
10102
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010103 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010104 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010105 return 1;
10106}
10107
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010108/* Extracts the query string, which comes after the question mark '?'. If no
10109 * question mark is found, nothing is returned. Otherwise it returns a sample
10110 * of type string carrying the whole query string.
10111 */
10112static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010113smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010114{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010115 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010116 char *ptr, *end;
10117
10118 CHECK_HTTP_MESSAGE_FIRST();
10119
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010120 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010121 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010122 end = ptr + txn->req.sl.rq.u_l;
10123
10124 /* look up the '?' */
10125 do {
10126 if (ptr == end)
10127 return 0;
10128 } while (*ptr++ != '?');
10129
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010130 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010131 smp->data.u.str.area = ptr;
10132 smp->data.u.str.data = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010133 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10134 return 1;
10135}
10136
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010137static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010138smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010139{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010140 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10141 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10142 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010143
Willy Tarreau24e32d82012-04-23 23:55:44 +020010144 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010145
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010146 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010147 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010148 return 1;
10149}
10150
Willy Tarreau7f18e522010-10-22 20:04:13 +020010151/* return a valid test if the current request is the first one on the connection */
10152static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010153smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010154{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010155 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010156 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010157 return 1;
10158}
10159
Willy Tarreau34db1082012-04-19 17:16:54 +020010160/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010161static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010162smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010163{
10164
Willy Tarreau24e32d82012-04-23 23:55:44 +020010165 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010166 return 0;
10167
Willy Tarreauc0239e02012-04-16 14:42:55 +020010168 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010169
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010170 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010171 return 0;
10172
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010173 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010174 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010175 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010176 return 1;
10177}
Willy Tarreau8797c062007-05-07 00:55:35 +020010178
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010179/* Accepts exactly 1 argument of type userlist */
10180static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010181smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010182{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010183 if (!args || args->type != ARGT_USR)
10184 return 0;
10185
10186 CHECK_HTTP_MESSAGE_FIRST();
10187
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010188 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010189 return 0;
10190
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010191 /* if the user does not belong to the userlist or has a wrong password,
10192 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010193 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010194 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010195 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10196 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010197 return 0;
10198
10199 /* pat_match_auth() will need the user list */
10200 smp->ctx.a[0] = args->data.usr;
10201
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010202 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010203 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010204 smp->data.u.str.area = smp->strm->txn->auth.user;
10205 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010206
10207 return 1;
10208}
10209
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010210/* Try to find the next occurrence of a cookie name in a cookie header value.
10211 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10212 * the cookie value is returned into *value and *value_l, and the function
10213 * returns a pointer to the next pointer to search from if the value was found.
10214 * Otherwise if the cookie was not found, NULL is returned and neither value
10215 * nor value_l are touched. The input <hdr> string should first point to the
10216 * header's value, and the <hdr_end> pointer must point to the first character
10217 * not part of the value. <list> must be non-zero if value may represent a list
10218 * of values (cookie headers). This makes it faster to abort parsing when no
10219 * list is expected.
10220 */
David Carlier4686f792015-09-25 14:10:50 +010010221char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010222extract_cookie_value(char *hdr, const char *hdr_end,
10223 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010224 char **value, size_t *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010225{
10226 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10227 char *next;
10228
10229 /* we search at least a cookie name followed by an equal, and more
10230 * generally something like this :
10231 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10232 */
10233 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10234 /* Iterate through all cookies on this line */
10235
Willy Tarreau2235b262016-11-05 15:50:20 +010010236 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010237 att_beg++;
10238
10239 /* find att_end : this is the first character after the last non
10240 * space before the equal. It may be equal to hdr_end.
10241 */
10242 equal = att_end = att_beg;
10243
10244 while (equal < hdr_end) {
10245 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10246 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010247 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010248 continue;
10249 att_end = equal;
10250 }
10251
10252 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10253 * is between <att_beg> and <equal>, both may be identical.
10254 */
10255
10256 /* look for end of cookie if there is an equal sign */
10257 if (equal < hdr_end && *equal == '=') {
10258 /* look for the beginning of the value */
10259 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010260 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010261 val_beg++;
10262
10263 /* find the end of the value, respecting quotes */
10264 next = find_cookie_value_end(val_beg, hdr_end);
10265
10266 /* make val_end point to the first white space or delimitor after the value */
10267 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010268 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010269 val_end--;
10270 } else {
10271 val_beg = val_end = next = equal;
10272 }
10273
10274 /* We have nothing to do with attributes beginning with '$'. However,
10275 * they will automatically be removed if a header before them is removed,
10276 * since they're supposed to be linked together.
10277 */
10278 if (*att_beg == '$')
10279 continue;
10280
10281 /* Ignore cookies with no equal sign */
10282 if (equal == next)
10283 continue;
10284
10285 /* Now we have the cookie name between att_beg and att_end, and
10286 * its value between val_beg and val_end.
10287 */
10288
10289 if (att_end - att_beg == cookie_name_l &&
10290 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10291 /* let's return this value and indicate where to go on from */
10292 *value = val_beg;
10293 *value_l = val_end - val_beg;
10294 return next + 1;
10295 }
10296
10297 /* Set-Cookie headers only have the name in the first attr=value part */
10298 if (!list)
10299 break;
10300 }
10301
10302 return NULL;
10303}
10304
William Lallemanda43ba4e2014-01-28 18:14:25 +010010305/* Fetch a captured HTTP request header. The index is the position of
10306 * the "capture" option in the configuration file
10307 */
10308static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010309smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010310{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010311 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010312 int idx;
10313
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010314 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010315 return 0;
10316
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010317 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010318
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010319 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010320 return 0;
10321
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010322 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010323 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010324 smp->data.u.str.area = smp->strm->req_cap[idx];
10325 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010326
10327 return 1;
10328}
10329
10330/* Fetch a captured HTTP response header. The index is the position of
10331 * the "capture" option in the configuration file
10332 */
10333static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010334smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010335{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010336 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010337 int idx;
10338
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010339 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010340 return 0;
10341
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010342 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010343
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010344 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010345 return 0;
10346
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010347 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010348 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010349 smp->data.u.str.area = smp->strm->res_cap[idx];
10350 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010351
10352 return 1;
10353}
10354
William Lallemand65ad6e12014-01-31 15:08:02 +010010355/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10356static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010357smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010358{
Willy Tarreau83061a82018-07-13 11:56:34 +020010359 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010360 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010361 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010362
Willy Tarreau15e91e12015-04-04 00:52:09 +020010363 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010364 return 0;
10365
William Lallemand96a77852014-02-05 00:30:02 +010010366 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010367
William Lallemand96a77852014-02-05 00:30:02 +010010368 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10369 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010370
William Lallemand96a77852014-02-05 00:30:02 +010010371 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010372 temp->area = txn->uri;
10373 temp->data = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010374 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010375 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010376 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010377
10378 return 1;
10379
10380}
10381
10382/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10383static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010384smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010385{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010386 struct http_txn *txn = smp->strm->txn;
Willy Tarreau6b952c82018-09-10 17:45:34 +020010387 struct ist path;
10388 const char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010389
Willy Tarreau15e91e12015-04-04 00:52:09 +020010390 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010391 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010392
William Lallemand65ad6e12014-01-31 15:08:02 +010010393 ptr = txn->uri;
10394
10395 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10396 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010397
William Lallemand65ad6e12014-01-31 15:08:02 +010010398 if (!*ptr)
10399 return 0;
10400
10401 ptr++; /* skip the space */
10402
Willy Tarreau6b952c82018-09-10 17:45:34 +020010403 path = http_get_path(ist(ptr));
10404 if (!path.ptr)
William Lallemand65ad6e12014-01-31 15:08:02 +010010405 return 0;
William Lallemand65ad6e12014-01-31 15:08:02 +010010406
Willy Tarreau6b952c82018-09-10 17:45:34 +020010407 smp->data.u.str.area = path.ptr;
10408 smp->data.u.str.data = path.len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010409 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010410 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010411
10412 return 1;
10413}
10414
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010415/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10416 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10417 */
10418static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010419smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010420{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010421 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010422
Willy Tarreau15e91e12015-04-04 00:52:09 +020010423 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010424 return 0;
10425
10426 if (txn->req.flags & HTTP_MSGF_VER_11)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010427 smp->data.u.str.area = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010428 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010429 smp->data.u.str.area = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010430
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010431 smp->data.u.str.data = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010432 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010433 smp->flags = SMP_F_CONST;
10434 return 1;
10435
10436}
10437
10438/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10439 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10440 */
10441static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010442smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010443{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010444 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010445
Willy Tarreau15e91e12015-04-04 00:52:09 +020010446 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010447 return 0;
10448
10449 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010450 smp->data.u.str.area = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010451 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010452 smp->data.u.str.area = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010453
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010454 smp->data.u.str.data = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010455 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010456 smp->flags = SMP_F_CONST;
10457 return 1;
10458
10459}
10460
William Lallemand65ad6e12014-01-31 15:08:02 +010010461
Willy Tarreaue333ec92012-04-16 16:26:40 +020010462/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010463 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010464 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010465 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010466 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010467 * Accepts exactly 1 argument of type string. If the input options indicate
10468 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010469 * The returned sample is of type CSTR. Can be used to parse cookies in other
10470 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010471 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010472int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010473{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010474 struct http_txn *txn;
10475 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010476 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010477 const struct http_msg *msg;
10478 const char *hdr_name;
10479 int hdr_name_len;
10480 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010481 int occ = 0;
10482 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010483
Willy Tarreau24e32d82012-04-23 23:55:44 +020010484 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010485 return 0;
10486
Willy Tarreaua890d072013-04-02 12:01:06 +020010487 if (!ctx) {
10488 /* first call */
10489 ctx = &static_hdr_ctx;
10490 ctx->idx = 0;
10491 smp->ctx.a[2] = ctx;
10492 }
10493
Willy Tarreaue333ec92012-04-16 16:26:40 +020010494 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010495
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010496 txn = smp->strm->txn;
10497 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010498
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010499 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010500 msg = &txn->req;
10501 hdr_name = "Cookie";
10502 hdr_name_len = 6;
10503 } else {
10504 msg = &txn->rsp;
10505 hdr_name = "Set-Cookie";
10506 hdr_name_len = 10;
10507 }
10508
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010509 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010510 /* no explicit occurrence and single fetch => last cookie by default */
10511 occ = -1;
10512
10513 /* OK so basically here, either we want only one value and it's the
10514 * last one, or we want to iterate over all of them and we fetch the
10515 * next one.
10516 */
10517
Willy Tarreauf37954d2018-06-15 18:31:02 +020010518 sol = ci_head(msg->chn);
Willy Tarreau37406352012-04-23 16:16:37 +020010519 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010520 /* search for the header from the beginning, we must first initialize
10521 * the search parameters.
10522 */
Willy Tarreau37406352012-04-23 16:16:37 +020010523 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010524 ctx->idx = 0;
10525 }
10526
Willy Tarreau28376d62012-04-26 21:26:10 +020010527 smp->flags |= SMP_F_VOL_HDR;
10528
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010529 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010530 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10531 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010532 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10533 goto out;
10534
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010535 if (ctx->vlen < args->data.str.data + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010536 continue;
10537
Willy Tarreau37406352012-04-23 16:16:37 +020010538 smp->ctx.a[0] = ctx->line + ctx->val;
10539 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010540 }
10541
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010542 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010543 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010544 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010545 args->data.str.area,
10546 args->data.str.data,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010547 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010548 &smp->data.u.str.area,
10549 &smp->data.u.str.data);
Willy Tarreau37406352012-04-23 16:16:37 +020010550 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010551 found = 1;
10552 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010553 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010554 smp->flags |= SMP_F_NOT_LAST;
10555 return 1;
10556 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010557 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010558 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010559 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010560 /* all cookie headers and values were scanned. If we're looking for the
10561 * last occurrence, we may return it now.
10562 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010563 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010564 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010565 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010566}
10567
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010568/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010569 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010570 * multiple cookies may be parsed on the same line. The returned sample is of
10571 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010572 */
10573static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010574smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010575{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010576 struct http_txn *txn;
10577 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010578 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010579 const struct http_msg *msg;
10580 const char *hdr_name;
10581 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010582 int cnt;
10583 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010584 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010585
Willy Tarreau24e32d82012-04-23 23:55:44 +020010586 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010587 return 0;
10588
Willy Tarreaue333ec92012-04-16 16:26:40 +020010589 CHECK_HTTP_MESSAGE_FIRST();
10590
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010591 txn = smp->strm->txn;
10592 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010593
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010594 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010595 msg = &txn->req;
10596 hdr_name = "Cookie";
10597 hdr_name_len = 6;
10598 } else {
10599 msg = &txn->rsp;
10600 hdr_name = "Set-Cookie";
10601 hdr_name_len = 10;
10602 }
10603
Willy Tarreauf37954d2018-06-15 18:31:02 +020010604 sol = ci_head(msg->chn);
Willy Tarreau46787ed2012-04-11 17:28:40 +020010605 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010606 ctx.idx = 0;
10607 cnt = 0;
10608
10609 while (1) {
10610 /* Note: val_beg == NULL every time we need to fetch a new header */
10611 if (!val_beg) {
10612 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10613 break;
10614
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010615 if (ctx.vlen < args->data.str.data + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010616 continue;
10617
10618 val_beg = ctx.line + ctx.val;
10619 val_end = val_beg + ctx.vlen;
10620 }
10621
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010622 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010623 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010624 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010625 args->data.str.area, args->data.str.data,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010626 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010627 &smp->data.u.str.area,
10628 &smp->data.u.str.data))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010629 cnt++;
10630 }
10631 }
10632
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010633 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010634 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010635 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010636 return 1;
10637}
10638
Willy Tarreau51539362012-05-08 12:46:28 +020010639/* Fetch an cookie's integer value. The integer value is returned. It
10640 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10641 */
10642static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010643smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010644{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010645 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010646
10647 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010648 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010649 smp->data.u.sint = strl2ic(smp->data.u.str.area,
10650 smp->data.u.str.data);
Willy Tarreau51539362012-05-08 12:46:28 +020010651 }
10652
10653 return ret;
10654}
10655
Willy Tarreau8797c062007-05-07 00:55:35 +020010656/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010657/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010658/************************************************************************/
10659
David Cournapeau16023ee2010-12-23 20:55:41 +090010660/*
10661 * Given a path string and its length, find the position of beginning of the
10662 * query string. Returns NULL if no query string is found in the path.
10663 *
10664 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10665 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010666 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090010667 */
bedis4c75cca2012-10-05 08:38:24 +020010668static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010669{
10670 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010671
bedis4c75cca2012-10-05 08:38:24 +020010672 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010673 return p ? p + 1 : NULL;
10674}
10675
bedis4c75cca2012-10-05 08:38:24 +020010676static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010677{
bedis4c75cca2012-10-05 08:38:24 +020010678 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010679}
10680
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010681/* after increasing a pointer value, it can exceed the first buffer
10682 * size. This function transform the value of <ptr> according with
10683 * the expected position. <chunks> is an array of the one or two
10684 * avalaible chunks. The first value is the start of the first chunk,
10685 * the second value if the end+1 of the first chunks. The third value
10686 * is NULL or the start of the second chunk and the fourth value is
10687 * the end+1 of the second chunk. The function returns 1 if does a
10688 * wrap, else returns 0.
10689 */
10690static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
10691{
10692 if (*ptr < chunks[1])
10693 return 0;
10694 if (!chunks[2])
10695 return 0;
10696 *ptr = chunks[2] + ( *ptr - chunks[1] );
10697 return 1;
10698}
10699
David Cournapeau16023ee2010-12-23 20:55:41 +090010700/*
10701 * Given a url parameter, find the starting position of the first occurence,
10702 * or NULL if the parameter is not found.
10703 *
10704 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10705 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010706 *
Willy Tarreauf6625822015-12-27 14:51:01 +010010707 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010708 * or the second chunk. The caller must be check the position before using the
10709 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090010710 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010711static const char *
10712find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010713 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010714 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010715{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010716 const char *pos, *last, *equal;
10717 const char **bufs = chunks;
10718 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090010719
David Cournapeau16023ee2010-12-23 20:55:41 +090010720
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010721 pos = bufs[0];
10722 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010010723 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010724 /* Check the equal. */
10725 equal = pos + url_param_name_l;
10726 if (fix_pointer_if_wrap(chunks, &equal)) {
10727 if (equal >= chunks[3])
10728 return NULL;
10729 } else {
10730 if (equal >= chunks[1])
10731 return NULL;
10732 }
10733 if (*equal == '=') {
10734 if (pos + url_param_name_l > last) {
10735 /* process wrap case, we detect a wrap. In this case, the
10736 * comparison is performed in two parts.
10737 */
10738
10739 /* This is the end, we dont have any other chunk. */
10740 if (bufs != chunks || !bufs[2])
10741 return NULL;
10742
10743 /* Compute the length of each part of the comparison. */
10744 l1 = last - pos;
10745 l2 = url_param_name_l - l1;
10746
10747 /* The second buffer is too short to contain the compared string. */
10748 if (bufs[2] + l2 > bufs[3])
10749 return NULL;
10750
10751 if (memcmp(pos, url_param_name, l1) == 0 &&
10752 memcmp(bufs[2], url_param_name+l1, l2) == 0)
10753 return pos;
10754
10755 /* Perform wrapping and jump the string who fail the comparison. */
10756 bufs += 2;
10757 pos = bufs[0] + l2;
10758 last = bufs[1];
10759
10760 } else {
10761 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010762 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10763 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010764 pos += url_param_name_l + 1;
10765 if (fix_pointer_if_wrap(chunks, &pos))
10766 last = bufs[2];
10767 }
10768 }
10769
10770 while (1) {
10771 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010772 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010773 pos++;
10774 if (pos < last)
10775 break;
10776 /* process buffer wrapping. */
10777 if (bufs != chunks || !bufs[2])
10778 return NULL;
10779 bufs += 2;
10780 pos = bufs[0];
10781 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090010782 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010783 pos++;
10784 }
10785 return NULL;
10786}
10787
10788/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010789 * Given a url parameter name and a query string, find the next value.
10790 * An empty url_param_name matches the first available parameter.
10791 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
10792 * respectively provide a pointer to the value and its end.
10793 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090010794 */
10795static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010796find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010797 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010798 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010799{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010800 const char *arg_start, *qs_end;
10801 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090010802
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010803 arg_start = chunks[0];
10804 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010805 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010806 /* Looks for an argument name. */
10807 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010808 url_param_name, url_param_name_l,
10809 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010810 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010811 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010812 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010813 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010814 if (!arg_start)
10815 return 0;
10816
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010817 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010818 while (1) {
10819 /* looks for the first argument. */
10820 value_start = memchr(arg_start, '=', qs_end - arg_start);
10821 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010822 /* Check for wrapping. */
10823 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010010824 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010825 chunks[2]) {
10826 arg_start = chunks[2];
10827 qs_end = chunks[3];
10828 continue;
10829 }
10830 return 0;
10831 }
10832 break;
10833 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010834 value_start++;
10835 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010836 else {
10837 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010838 value_start = arg_start + url_param_name_l + 1;
10839
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010840 /* Check for pointer wrapping. */
10841 if (fix_pointer_if_wrap(chunks, &value_start)) {
10842 /* Update the end pointer. */
10843 qs_end = chunks[3];
10844
10845 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010846 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010847 return 0;
10848 }
10849 }
10850
David Cournapeau16023ee2010-12-23 20:55:41 +090010851 value_end = value_start;
10852
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010853 while (1) {
10854 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
10855 value_end++;
10856 if (value_end < qs_end)
10857 break;
10858 /* process buffer wrapping. */
10859 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010010860 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010861 chunks[2]) {
10862 value_end = chunks[2];
10863 qs_end = chunks[3];
10864 continue;
10865 }
10866 break;
10867 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010868
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010869 *vstart = value_start;
10870 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010871 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090010872}
10873
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010874/* This scans a URL-encoded query string. It takes an optionally wrapping
10875 * string whose first contigous chunk has its beginning in ctx->a[0] and end
10876 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
10877 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010878 */
David Cournapeau16023ee2010-12-23 20:55:41 +090010879static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010880smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090010881{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010882 const char *vstart, *vend;
Willy Tarreau83061a82018-07-13 11:56:34 +020010883 struct buffer *temp;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010884 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020010885
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010886 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020010887 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010888 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020010889 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010890 return 0;
10891
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010892 /* Create sample. If the value is contiguous, return the pointer as CONST,
10893 * if the value is wrapped, copy-it in a buffer.
10894 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010895 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010896 if (chunks[2] &&
10897 vstart >= chunks[0] && vstart <= chunks[1] &&
10898 vend >= chunks[2] && vend <= chunks[3]) {
10899 /* Wrapped case. */
10900 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010901 memcpy(temp->area, vstart, chunks[1] - vstart);
10902 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
10903 vend - chunks[2]);
10904 smp->data.u.str.area = temp->area;
10905 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010906 } else {
10907 /* Contiguous case. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010908 smp->data.u.str.area = (char *)vstart;
10909 smp->data.u.str.data = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010910 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10911 }
10912
10913 /* Update context, check wrapping. */
10914 chunks[0] = vend;
10915 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
10916 chunks[1] = chunks[3];
10917 chunks[2] = NULL;
10918 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010919
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010920 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010921 smp->flags |= SMP_F_NOT_LAST;
10922
David Cournapeau16023ee2010-12-23 20:55:41 +090010923 return 1;
10924}
10925
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010926/* This function iterates over each parameter of the query string. It uses
10927 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010928 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
10929 * An optional parameter name is passed in args[0], otherwise any parameter is
10930 * considered. It supports an optional delimiter argument for the beginning of
10931 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010932 */
10933static int
10934smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
10935{
10936 struct http_msg *msg;
10937 char delim = '?';
10938 const char *name;
10939 int name_len;
10940
Dragan Dosen26f77e52015-05-25 10:02:11 +020010941 if (!args ||
10942 (args[0].type && args[0].type != ARGT_STR) ||
10943 (args[1].type && args[1].type != ARGT_STR))
10944 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010945
Dragan Dosen26f77e52015-05-25 10:02:11 +020010946 name = "";
10947 name_len = 0;
10948 if (args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010949 name = args->data.str.area;
10950 name_len = args->data.str.data;
Dragan Dosen26f77e52015-05-25 10:02:11 +020010951 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010952
Dragan Dosen26f77e52015-05-25 10:02:11 +020010953 if (args[1].type)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010954 delim = *args[1].data.str.area;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010955
Dragan Dosen26f77e52015-05-25 10:02:11 +020010956 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010957 CHECK_HTTP_MESSAGE_FIRST();
10958
10959 msg = &smp->strm->txn->req;
10960
Willy Tarreauf37954d2018-06-15 18:31:02 +020010961 smp->ctx.a[0] = find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010962 msg->sl.rq.u_l, delim);
10963 if (!smp->ctx.a[0])
10964 return 0;
10965
Willy Tarreauf37954d2018-06-15 18:31:02 +020010966 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010967
10968 /* Assume that the context is filled with NULL pointer
10969 * before the first call.
10970 * smp->ctx.a[2] = NULL;
10971 * smp->ctx.a[3] = NULL;
10972 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010973 }
10974
10975 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
10976}
10977
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010978/* This function iterates over each parameter of the body. This requires
10979 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010980 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
10981 * contigous part of the body, and optionally ctx->a[2..3] to reference the
10982 * optional second part if the body wraps at the end of the buffer. An optional
10983 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010984 */
10985static int
10986smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
10987{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010988 struct http_msg *msg;
10989 unsigned long len;
10990 unsigned long block1;
10991 char *body;
10992 const char *name;
10993 int name_len;
10994
10995 if (!args || (args[0].type && args[0].type != ARGT_STR))
10996 return 0;
10997
10998 name = "";
10999 name_len = 0;
11000 if (args[0].type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011001 name = args[0].data.str.area;
11002 name_len = args[0].data.str.data;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011003 }
11004
11005 if (!smp->ctx.a[0]) { // first call, find the query string
11006 CHECK_HTTP_MESSAGE_FIRST();
11007
11008 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011009 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011010 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011011 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011012
11013 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +020011014 body = c_ptr(msg->chn, -http_data_rewind(msg));
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011015
11016 block1 = len;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020011017 if (block1 > b_wrap(&msg->chn->buf) - body)
11018 block1 = b_wrap(&msg->chn->buf) - body;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011019
11020 if (block1 == len) {
11021 /* buffer is not wrapped (or empty) */
11022 smp->ctx.a[0] = body;
11023 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011024
11025 /* Assume that the context is filled with NULL pointer
11026 * before the first call.
11027 * smp->ctx.a[2] = NULL;
11028 * smp->ctx.a[3] = NULL;
11029 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011030 }
11031 else {
11032 /* buffer is wrapped, we need to defragment it */
11033 smp->ctx.a[0] = body;
11034 smp->ctx.a[1] = body + block1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020011035 smp->ctx.a[2] = b_orig(&msg->chn->buf);
11036 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011037 }
11038 }
11039 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11040}
11041
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011042/* Return the signed integer value for the specified url parameter (see url_param
11043 * above).
11044 */
11045static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011046smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011047{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011048 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011049
11050 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011051 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011052 smp->data.u.sint = strl2ic(smp->data.u.str.area,
11053 smp->data.u.str.data);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011054 }
11055
11056 return ret;
11057}
11058
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011059/* This produces a 32-bit hash of the concatenation of the first occurrence of
11060 * the Host header followed by the path component if it begins with a slash ('/').
11061 * This means that '*' will not be added, resulting in exactly the first Host
11062 * entry. If no Host header is found, then the path is used. The resulting value
11063 * is hashed using the url hash followed by a full avalanche hash and provides a
11064 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11065 * high-traffic sites without having to store whole paths.
11066 * this differs from the base32 functions in that it includes the url parameters
11067 * as well as the path
11068 */
11069static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011070smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011071{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011072 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011073 struct hdr_ctx ctx;
11074 unsigned int hash = 0;
11075 char *ptr, *beg, *end;
11076 int len;
11077
11078 CHECK_HTTP_MESSAGE_FIRST();
11079
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011080 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011081 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011082 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011083 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11084 ptr = ctx.line + ctx.val;
11085 len = ctx.vlen;
11086 while (len--)
11087 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11088 }
11089
11090 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020011091 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +020011092 beg = http_txn_get_path(txn);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011093 if (!beg)
11094 beg = end;
11095
11096 for (ptr = beg; ptr < end ; ptr++);
11097
11098 if (beg < ptr && *beg == '/') {
11099 while (beg < ptr)
11100 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11101 }
11102 hash = full_hash(hash);
11103
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011104 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011105 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011106 smp->flags = SMP_F_VOL_1ST;
11107 return 1;
11108}
11109
11110/* This concatenates the source address with the 32-bit hash of the Host and
11111 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11112 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11113 * on the source address length. The URL hash is stored before the address so
11114 * that in environments where IPv6 is insignificant, truncating the output to
11115 * 8 bytes would still work.
11116 */
11117static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011118smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011119{
Willy Tarreau83061a82018-07-13 11:56:34 +020011120 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011121 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011122
Dragan Dosendb5af612016-06-16 11:23:01 +020011123 if (!cli_conn)
11124 return 0;
11125
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011126 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011127 return 0;
11128
11129 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011130 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
11131 temp->data += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011132
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011133 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011134 case AF_INET:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011135 memcpy(temp->area + temp->data,
11136 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
11137 4);
11138 temp->data += 4;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011139 break;
11140 case AF_INET6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011141 memcpy(temp->area + temp->data,
11142 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
11143 16);
11144 temp->data += 16;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011145 break;
11146 default:
11147 return 0;
11148 }
11149
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011150 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011151 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011152 return 1;
11153}
11154
Willy Tarreau185b5c42012-04-26 15:11:51 +020011155/* This function is used to validate the arguments passed to any "hdr" fetch
11156 * keyword. These keywords support an optional positive or negative occurrence
11157 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11158 * is assumed that the types are already the correct ones. Returns 0 on error,
11159 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11160 * error message in case of error, that the caller is responsible for freeing.
11161 * The initial location must either be freeable or NULL.
11162 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011163int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011164{
11165 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011166 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011167 return 0;
11168 }
11169 return 1;
11170}
11171
Willy Tarreau276fae92013-07-25 14:36:01 +020011172/* takes an UINT value on input supposed to represent the time since EPOCH,
11173 * adds an optional offset found in args[0] and emits a string representing
11174 * the date in RFC-1123/5322 format.
11175 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011176static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011177{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011178 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011179 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
Willy Tarreau83061a82018-07-13 11:56:34 +020011180 struct buffer *temp;
Willy Tarreau276fae92013-07-25 14:36:01 +020011181 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011182 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011183 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011184
11185 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011186 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011187 curr_date += args[0].data.sint;
11188
11189 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011190 if (!tm)
11191 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011192
11193 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011194 temp->data = snprintf(temp->area, temp->size - temp->data,
11195 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11196 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon],
11197 1900+tm->tm_year,
11198 tm->tm_hour, tm->tm_min, tm->tm_sec);
Willy Tarreau276fae92013-07-25 14:36:01 +020011199
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011200 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011201 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011202 return 1;
11203}
11204
Thierry FOURNIERad903512014-04-11 17:51:01 +020011205/* Match language range with language tag. RFC2616 14.4:
11206 *
11207 * A language-range matches a language-tag if it exactly equals
11208 * the tag, or if it exactly equals a prefix of the tag such
11209 * that the first tag character following the prefix is "-".
11210 *
11211 * Return 1 if the strings match, else return 0.
11212 */
11213static inline int language_range_match(const char *range, int range_len,
11214 const char *tag, int tag_len)
11215{
11216 const char *end = range + range_len;
11217 const char *tend = tag + tag_len;
11218 while (range < end) {
11219 if (*range == '-' && tag == tend)
11220 return 1;
11221 if (*range != *tag || tag == tend)
11222 return 0;
11223 range++;
11224 tag++;
11225 }
11226 /* Return true only if the last char of the tag is matched. */
11227 return tag == tend;
11228}
11229
11230/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011231static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011232{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011233 const char *al = smp->data.u.str.area;
11234 const char *end = al + smp->data.u.str.data;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011235 const char *token;
11236 int toklen;
11237 int qvalue;
11238 const char *str;
11239 const char *w;
11240 int best_q = 0;
11241
11242 /* Set the constant to the sample, because the output of the
11243 * function will be peek in the constant configuration string.
11244 */
11245 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011246 smp->data.u.str.size = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011247 smp->data.u.str.area = "";
11248 smp->data.u.str.data = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011249
11250 /* Parse the accept language */
11251 while (1) {
11252
11253 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011254 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011255 al++;
11256 if (al >= end)
11257 break;
11258
11259 /* Start of the fisrt word. */
11260 token = al;
11261
11262 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011263 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011264 al++;
11265 if (al == token)
11266 goto expect_comma;
11267
11268 /* Length of the token. */
11269 toklen = al - token;
11270 qvalue = 1000;
11271
11272 /* Check if the token exists in the list. If the token not exists,
11273 * jump to the next token.
11274 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011275 str = args[0].data.str.area;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011276 w = str;
11277 while (1) {
11278 if (*str == ';' || *str == '\0') {
11279 if (language_range_match(token, toklen, w, str-w))
11280 goto look_for_q;
11281 if (*str == '\0')
11282 goto expect_comma;
11283 w = str + 1;
11284 }
11285 str++;
11286 }
11287 goto expect_comma;
11288
11289look_for_q:
11290
11291 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011292 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011293 al++;
11294 if (al >= end)
11295 goto process_value;
11296
11297 /* If ',' is found, process the result */
11298 if (*al == ',')
11299 goto process_value;
11300
11301 /* If the character is different from ';', look
11302 * for the end of the header part in best effort.
11303 */
11304 if (*al != ';')
11305 goto expect_comma;
11306
11307 /* Assumes that the char is ';', now expect "q=". */
11308 al++;
11309
11310 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011311 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011312 al++;
11313 if (al >= end)
11314 goto process_value;
11315
11316 /* Expect 'q'. If no 'q', continue in best effort */
11317 if (*al != 'q')
11318 goto process_value;
11319 al++;
11320
11321 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011322 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011323 al++;
11324 if (al >= end)
11325 goto process_value;
11326
11327 /* Expect '='. If no '=', continue in best effort */
11328 if (*al != '=')
11329 goto process_value;
11330 al++;
11331
11332 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011333 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011334 al++;
11335 if (al >= end)
11336 goto process_value;
11337
11338 /* Parse the q value. */
11339 qvalue = parse_qvalue(al, &al);
11340
11341process_value:
11342
11343 /* If the new q value is the best q value, then store the associated
11344 * language in the response. If qvalue is the biggest value (1000),
11345 * break the process.
11346 */
11347 if (qvalue > best_q) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011348 smp->data.u.str.area = (char *)w;
11349 smp->data.u.str.data = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011350 if (qvalue >= 1000)
11351 break;
11352 best_q = qvalue;
11353 }
11354
11355expect_comma:
11356
11357 /* Expect comma or end. If the end is detected, quit the loop. */
11358 while (al < end && *al != ',')
11359 al++;
11360 if (al >= end)
11361 break;
11362
11363 /* Comma is found, jump it and restart the analyzer. */
11364 al++;
11365 }
11366
11367 /* Set default value if required. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011368 if (smp->data.u.str.data == 0 && args[1].type == ARGT_STR) {
11369 smp->data.u.str.area = args[1].data.str.area;
11370 smp->data.u.str.data = args[1].data.str.data;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011371 }
11372
11373 /* Return true only if a matching language was found. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011374 return smp->data.u.str.data != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011375}
11376
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011377/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011378static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011379{
Willy Tarreau9c768fd2018-08-22 05:08:57 +020011380 int len;
11381
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011382 /* If the constant flag is set or if not size is avalaible at
11383 * the end of the buffer, copy the string in other buffer
11384 * before decoding.
11385 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011386 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.data) {
Willy Tarreau83061a82018-07-13 11:56:34 +020011387 struct buffer *str = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011388 memcpy(str->area, smp->data.u.str.area, smp->data.u.str.data);
11389 smp->data.u.str.area = str->area;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011390 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011391 smp->flags &= ~SMP_F_CONST;
11392 }
11393
11394 /* Add final \0 required by url_decode(), and convert the input string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011395 smp->data.u.str.area[smp->data.u.str.data] = '\0';
Willy Tarreau9c768fd2018-08-22 05:08:57 +020011396 len = url_decode(smp->data.u.str.area);
11397 if (len < 0)
11398 return 0;
11399 smp->data.u.str.data = len;
11400 return 1;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011401}
11402
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011403static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11404{
11405 struct proxy *fe = strm_fe(smp->strm);
11406 int idx, i;
11407 struct cap_hdr *hdr;
11408 int len;
11409
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011410 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011411 return 0;
11412
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011413 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011414
11415 /* Check the availibity of the capture id. */
11416 if (idx > fe->nb_req_cap - 1)
11417 return 0;
11418
11419 /* Look for the original configuration. */
11420 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11421 hdr != NULL && i != idx ;
11422 i--, hdr = hdr->next);
11423 if (!hdr)
11424 return 0;
11425
11426 /* check for the memory allocation */
11427 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011428 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011429 if (smp->strm->req_cap[hdr->index] == NULL)
11430 return 0;
11431
11432 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011433 len = smp->data.u.str.data;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011434 if (len > hdr->len)
11435 len = hdr->len;
11436
11437 /* Capture input data. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011438 memcpy(smp->strm->req_cap[idx], smp->data.u.str.area, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011439 smp->strm->req_cap[idx][len] = '\0';
11440
11441 return 1;
11442}
11443
11444static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11445{
11446 struct proxy *fe = strm_fe(smp->strm);
11447 int idx, i;
11448 struct cap_hdr *hdr;
11449 int len;
11450
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011451 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011452 return 0;
11453
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011454 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011455
11456 /* Check the availibity of the capture id. */
11457 if (idx > fe->nb_rsp_cap - 1)
11458 return 0;
11459
11460 /* Look for the original configuration. */
11461 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11462 hdr != NULL && i != idx ;
11463 i--, hdr = hdr->next);
11464 if (!hdr)
11465 return 0;
11466
11467 /* check for the memory allocation */
11468 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011469 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011470 if (smp->strm->res_cap[hdr->index] == NULL)
11471 return 0;
11472
11473 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011474 len = smp->data.u.str.data;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011475 if (len > hdr->len)
11476 len = hdr->len;
11477
11478 /* Capture input data. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011479 memcpy(smp->strm->res_cap[idx], smp->data.u.str.area, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011480 smp->strm->res_cap[idx][len] = '\0';
11481
11482 return 1;
11483}
11484
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011485/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011486 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011487 * the relevant part of the request line accordingly. Then it updates various
11488 * pointers to the next elements which were moved, and the total buffer length.
11489 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011490 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11491 * error, though this can be revisited when this code is finally exploited.
11492 *
11493 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11494 * query string and 3 to replace uri.
11495 *
11496 * In query string case, the mark question '?' must be set at the start of the
11497 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011498 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011499int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011500 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011501{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011502 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011503 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011504 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011505 int delta;
11506
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011507 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011508 case 0: // method
Willy Tarreauf37954d2018-06-15 18:31:02 +020011509 cur_ptr = ci_head(&s->req);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011510 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11511
11512 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011513 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011514 txn->req.sl.rq.m_l += delta;
11515 txn->req.sl.rq.u += delta;
11516 txn->req.sl.rq.v += delta;
11517 break;
11518
11519 case 1: // path
Willy Tarreau6b952c82018-09-10 17:45:34 +020011520 cur_ptr = http_txn_get_path(txn);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011521 if (!cur_ptr)
Willy Tarreauf37954d2018-06-15 18:31:02 +020011522 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011523
11524 cur_end = cur_ptr;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011525 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011526 cur_end++;
11527
11528 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011529 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011530 txn->req.sl.rq.u_l += delta;
11531 txn->req.sl.rq.v += delta;
11532 break;
11533
11534 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011535 offset = 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011536 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011537 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11538 while (cur_ptr < cur_end && *cur_ptr != '?')
11539 cur_ptr++;
11540
11541 /* skip the question mark or indicate that we must insert it
11542 * (but only if the format string is not empty then).
11543 */
11544 if (cur_ptr < cur_end)
11545 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011546 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011547 offset = 0;
11548
11549 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011550 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011551 txn->req.sl.rq.u_l += delta;
11552 txn->req.sl.rq.v += delta;
11553 break;
11554
11555 case 3: // uri
Willy Tarreauf37954d2018-06-15 18:31:02 +020011556 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011557 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11558
11559 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011560 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011561 txn->req.sl.rq.u_l += delta;
11562 txn->req.sl.rq.v += delta;
11563 break;
11564
11565 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011566 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011567 }
11568
11569 /* commit changes and adjust end of message */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020011570 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011571 txn->req.sl.rq.l += delta;
11572 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011573 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011574 return 0;
11575}
11576
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011577/* This function replace the HTTP status code and the associated message. The
11578 * variable <status> contains the new status code. This function never fails.
11579 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011580void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011581{
11582 struct http_txn *txn = s->txn;
11583 char *cur_ptr, *cur_end;
11584 int delta;
11585 char *res;
11586 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011587 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011588 int msg_len;
11589
11590 chunk_reset(&trash);
11591
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011592 res = ultoa_o(status, trash.area, trash.size);
11593 c_l = res - trash.area;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011594
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011595 trash.area[c_l] = ' ';
11596 trash.data = c_l + 1;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011597
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011598 /* Do we have a custom reason format string? */
11599 if (msg == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +020011600 msg = http_get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011601 msg_len = strlen(msg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011602 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
11603 trash.data += msg_len;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011604
Willy Tarreauf37954d2018-06-15 18:31:02 +020011605 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
11606 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011607
11608 /* commit changes and adjust message */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011609 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
11610 trash.data);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011611
11612 /* adjust res line offsets and lengths */
11613 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11614 txn->rsp.sl.st.c_l = c_l;
11615 txn->rsp.sl.st.r_l = msg_len;
11616
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011617 delta = trash.data - (cur_end - cur_ptr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011618 txn->rsp.sl.st.l += delta;
11619 txn->hdr_idx.v[0].len += delta;
11620 http_msg_move_end(&txn->rsp, delta);
11621}
11622
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011623/* This function executes one of the set-{method,path,query,uri} actions. It
11624 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011625 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011626 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011627 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11628 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011629 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011630enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011631 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011632{
Willy Tarreau83061a82018-07-13 11:56:34 +020011633 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011634 enum act_return ret = ACT_RET_ERR;
11635
11636 replace = alloc_trash_chunk();
11637 if (!replace)
11638 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011639
11640 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011641 if (rule->arg.http.action == 2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011642 replace->area[replace->data++] = '?';
11643 replace->data += build_logline(s, replace->area + replace->data,
11644 replace->size - replace->data,
11645 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011646
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011647 http_replace_req_line(rule->arg.http.action, replace->area,
11648 replace->data, px, s);
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011649
11650 ret = ACT_RET_CONT;
11651
11652leave:
11653 free_trash_chunk(replace);
11654 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011655}
11656
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011657/* This function is just a compliant action wrapper for "set-status". */
11658enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011659 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011660{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011661 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011662 return ACT_RET_CONT;
11663}
11664
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011665/* parse an http-request action among :
11666 * set-method
11667 * set-path
11668 * set-query
11669 * set-uri
11670 *
11671 * All of them accept a single argument of type string representing a log-format.
11672 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11673 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011674 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011675 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011676enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11677 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011678{
11679 int cur_arg = *orig_arg;
11680
Thierry FOURNIER42148732015-09-02 17:17:33 +020011681 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011682
11683 switch (args[0][4]) {
11684 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011685 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011686 rule->action_ptr = http_action_set_req_line;
11687 break;
11688 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011689 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011690 rule->action_ptr = http_action_set_req_line;
11691 break;
11692 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011693 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011694 rule->action_ptr = http_action_set_req_line;
11695 break;
11696 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011697 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011698 rule->action_ptr = http_action_set_req_line;
11699 break;
11700 default:
11701 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011702 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011703 }
11704
11705 if (!*args[cur_arg] ||
11706 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11707 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011708 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011709 }
11710
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011711 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010011712 px->conf.args.ctx = ARGC_HRQ;
11713 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
11714 (px->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010011715 return ACT_RET_PRS_ERR;
11716 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011717
11718 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011719 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011720}
11721
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011722/* parse set-status action:
11723 * This action accepts a single argument of type int representing
11724 * an http status code. It returns ACT_RET_PRS_OK on success,
11725 * ACT_RET_PRS_ERR on error.
11726 */
11727enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
11728 struct act_rule *rule, char **err)
11729{
11730 char *error;
11731
Thierry FOURNIER42148732015-09-02 17:17:33 +020011732 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011733 rule->action_ptr = action_http_set_status;
11734
11735 /* Check if an argument is available */
11736 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011737 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011738 return ACT_RET_PRS_ERR;
11739 }
11740
11741 /* convert status code as integer */
11742 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
11743 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
11744 memprintf(err, "expects an integer status code between 100 and 999");
11745 return ACT_RET_PRS_ERR;
11746 }
11747
11748 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011749
11750 /* set custom reason string */
11751 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
11752 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
11753 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
11754 (*orig_arg)++;
11755 rule->arg.status.reason = strdup(args[*orig_arg]);
11756 (*orig_arg)++;
11757 }
11758
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011759 return ACT_RET_PRS_OK;
11760}
11761
Willy Tarreau53275e82017-11-24 07:52:01 +010011762/* This function executes the "reject" HTTP action. It clears the request and
11763 * response buffer without sending any response. It can be useful as an HTTP
11764 * alternative to the silent-drop action to defend against DoS attacks, and may
11765 * also be used with HTTP/2 to close a connection instead of just a stream.
11766 * The txn status is unchanged, indicating no response was sent. The termination
11767 * flags will indicate "PR". It always returns ACT_RET_STOP.
11768 */
11769enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
11770 struct session *sess, struct stream *s, int flags)
11771{
11772 channel_abort(&s->req);
11773 channel_abort(&s->res);
11774 s->req.analysers = 0;
11775 s->res.analysers = 0;
11776
11777 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
11778 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
11779 if (sess->listener && sess->listener->counters)
11780 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
11781
11782 if (!(s->flags & SF_ERR_MASK))
11783 s->flags |= SF_ERR_PRXCOND;
11784 if (!(s->flags & SF_FINST_MASK))
11785 s->flags |= SF_FINST_R;
11786
11787 return ACT_RET_CONT;
11788}
11789
11790/* parse the "reject" action:
11791 * This action takes no argument and returns ACT_RET_PRS_OK on success,
11792 * ACT_RET_PRS_ERR on error.
11793 */
11794enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
11795 struct act_rule *rule, char **err)
11796{
11797 rule->action = ACT_CUSTOM;
11798 rule->action_ptr = http_action_reject;
11799 return ACT_RET_PRS_OK;
11800}
11801
Willy Tarreaua9083d02015-05-08 15:27:59 +020011802/* This function executes the "capture" action. It executes a fetch expression,
11803 * turns the result into a string and puts it in a capture slot. It always
11804 * returns 1. If an error occurs the action is cancelled, but the rule
11805 * processing continues.
11806 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011807enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011808 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011809{
Willy Tarreaua9083d02015-05-08 15:27:59 +020011810 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011811 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011812 char **cap = s->req_cap;
11813 int len;
11814
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011815 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011816 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011817 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011818
11819 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011820 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011821
11822 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011823 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011824
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011825 len = key->data.u.str.data;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011826 if (len > h->len)
11827 len = h->len;
11828
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011829 memcpy(cap[h->index], key->data.u.str.area, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011830 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011831 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011832}
11833
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011834/* This function executes the "capture" action and store the result in a
11835 * capture slot if exists. It executes a fetch expression, turns the result
11836 * into a string and puts it in a capture slot. It always returns 1. If an
11837 * error occurs the action is cancelled, but the rule processing continues.
11838 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011839enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011840 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011841{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011842 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011843 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011844 char **cap = s->req_cap;
11845 struct proxy *fe = strm_fe(s);
11846 int len;
11847 int i;
11848
11849 /* Look for the original configuration. */
11850 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011851 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011852 i--, h = h->next);
11853 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011854 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011855
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011856 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011857 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011858 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011859
11860 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011861 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011862
11863 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011864 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011865
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011866 len = key->data.u.str.data;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011867 if (len > h->len)
11868 len = h->len;
11869
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011870 memcpy(cap[h->index], key->data.u.str.area, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011871 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011872 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011873}
11874
Christopher Faulet29730ba2017-09-18 15:26:32 +020011875/* Check an "http-request capture" action.
11876 *
11877 * The function returns 1 in success case, otherwise, it returns 0 and err is
11878 * filled.
11879 */
11880int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
11881{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010011882 if (rule->action_ptr != http_action_req_capture_by_id)
11883 return 1;
11884
Christopher Faulet29730ba2017-09-18 15:26:32 +020011885 if (rule->arg.capid.idx >= px->nb_req_cap) {
11886 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
11887 rule->arg.capid.idx);
11888 return 0;
11889 }
11890
11891 return 1;
11892}
11893
Willy Tarreaua9083d02015-05-08 15:27:59 +020011894/* parse an "http-request capture" action. It takes a single argument which is
11895 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011896 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011897 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020011898 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011899enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
11900 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011901{
11902 struct sample_expr *expr;
11903 struct cap_hdr *hdr;
11904 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020011905 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011906
11907 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
11908 if (strcmp(args[cur_arg], "if") == 0 ||
11909 strcmp(args[cur_arg], "unless") == 0)
11910 break;
11911
11912 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011913 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011914 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011915 }
11916
Willy Tarreaua9083d02015-05-08 15:27:59 +020011917 cur_arg = *orig_arg;
11918 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
11919 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011920 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011921
11922 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
11923 memprintf(err,
11924 "fetch method '%s' extracts information from '%s', none of which is available here",
11925 args[cur_arg-1], sample_src_names(expr->fetch->use));
11926 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011927 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011928 }
11929
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011930 if (!args[cur_arg] || !*args[cur_arg]) {
11931 memprintf(err, "expects 'len or 'id'");
11932 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011933 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011934 }
11935
Willy Tarreaua9083d02015-05-08 15:27:59 +020011936 if (strcmp(args[cur_arg], "len") == 0) {
11937 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011938
11939 if (!(px->cap & PR_CAP_FE)) {
11940 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011941 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011942 }
11943
Olivier Houchardfbc74e82017-11-24 16:54:05 +010011944 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011945
Willy Tarreaua9083d02015-05-08 15:27:59 +020011946 if (!args[cur_arg]) {
11947 memprintf(err, "missing length value");
11948 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011949 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011950 }
11951 /* we copy the table name for now, it will be resolved later */
11952 len = atoi(args[cur_arg]);
11953 if (len <= 0) {
11954 memprintf(err, "length must be > 0");
11955 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011956 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011957 }
11958 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011959
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011960 if (!len) {
11961 memprintf(err, "a positive 'len' argument is mandatory");
11962 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011963 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011964 }
11965
Vincent Bernat02779b62016-04-03 13:48:43 +020011966 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011967 hdr->next = px->req_cap;
11968 hdr->name = NULL; /* not a header capture */
11969 hdr->namelen = 0;
11970 hdr->len = len;
11971 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
11972 hdr->index = px->nb_req_cap++;
11973
11974 px->req_cap = hdr;
11975 px->to_log |= LW_REQHDR;
11976
Thierry FOURNIER42148732015-09-02 17:17:33 +020011977 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011978 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011979 rule->arg.cap.expr = expr;
11980 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011981 }
11982
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011983 else if (strcmp(args[cur_arg], "id") == 0) {
11984 int id;
11985 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011986
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011987 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011988
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011989 if (!args[cur_arg]) {
11990 memprintf(err, "missing id value");
11991 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011992 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011993 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020011994
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011995 id = strtol(args[cur_arg], &error, 10);
11996 if (*error != '\0') {
11997 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
11998 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011999 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012000 }
12001 cur_arg++;
12002
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012003 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012004
Thierry FOURNIER42148732015-09-02 17:17:33 +020012005 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012006 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012007 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012008 rule->arg.capid.expr = expr;
12009 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012010 }
12011
12012 else {
12013 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12014 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012015 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012016 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012017
12018 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012019 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012020}
12021
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012022/* This function executes the "capture" action and store the result in a
12023 * capture slot if exists. It executes a fetch expression, turns the result
12024 * into a string and puts it in a capture slot. It always returns 1. If an
12025 * error occurs the action is cancelled, but the rule processing continues.
12026 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012027enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012028 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012029{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012030 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012031 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012032 char **cap = s->res_cap;
12033 struct proxy *fe = strm_fe(s);
12034 int len;
12035 int i;
12036
12037 /* Look for the original configuration. */
12038 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012039 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012040 i--, h = h->next);
12041 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012042 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012043
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012044 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012045 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012046 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012047
12048 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010012049 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012050
12051 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012052 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012053
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012054 len = key->data.u.str.data;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012055 if (len > h->len)
12056 len = h->len;
12057
Willy Tarreau843b7cb2018-07-13 10:54:26 +020012058 memcpy(cap[h->index], key->data.u.str.area, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012059 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012060 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012061}
12062
Christopher Faulet29730ba2017-09-18 15:26:32 +020012063/* Check an "http-response capture" action.
12064 *
12065 * The function returns 1 in success case, otherwise, it returns 0 and err is
12066 * filled.
12067 */
12068int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12069{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010012070 if (rule->action_ptr != http_action_res_capture_by_id)
12071 return 1;
12072
Christopher Faulet29730ba2017-09-18 15:26:32 +020012073 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12074 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12075 rule->arg.capid.idx);
12076 return 0;
12077 }
12078
12079 return 1;
12080}
12081
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012082/* parse an "http-response capture" action. It takes a single argument which is
12083 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12084 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012085 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012086 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012087enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12088 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012089{
12090 struct sample_expr *expr;
12091 int cur_arg;
12092 int id;
12093 char *error;
12094
12095 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12096 if (strcmp(args[cur_arg], "if") == 0 ||
12097 strcmp(args[cur_arg], "unless") == 0)
12098 break;
12099
12100 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012101 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012102 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012103 }
12104
12105 cur_arg = *orig_arg;
12106 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12107 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012108 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012109
12110 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12111 memprintf(err,
12112 "fetch method '%s' extracts information from '%s', none of which is available here",
12113 args[cur_arg-1], sample_src_names(expr->fetch->use));
12114 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012115 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012116 }
12117
12118 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012119 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012120 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012121 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012122 }
12123
12124 if (strcmp(args[cur_arg], "id") != 0) {
12125 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12126 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012127 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012128 }
12129
12130 cur_arg++;
12131
12132 if (!args[cur_arg]) {
12133 memprintf(err, "missing id value");
12134 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012135 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012136 }
12137
12138 id = strtol(args[cur_arg], &error, 10);
12139 if (*error != '\0') {
12140 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12141 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012142 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012143 }
12144 cur_arg++;
12145
Olivier Houchardfbc74e82017-11-24 16:54:05 +010012146 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012147
Thierry FOURNIER42148732015-09-02 17:17:33 +020012148 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012149 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012150 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012151 rule->arg.capid.expr = expr;
12152 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012153
12154 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012155 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012156}
12157
William Lallemand73025dd2014-04-24 14:38:37 +020012158/*
12159 * Return the struct http_req_action_kw associated to a keyword.
12160 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012161struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012162{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012163 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012164}
12165
12166/*
12167 * Return the struct http_res_action_kw associated to a keyword.
12168 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012169struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012170{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012171 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012172}
12173
Willy Tarreau12207b32016-11-22 19:48:51 +010012174
Willy Tarreau4a568972010-05-12 08:08:50 +020012175/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012176/* All supported ACL keywords must be declared here. */
12177/************************************************************************/
12178
12179/* Note: must not be declared <const> as its list will be overwritten.
12180 * Please take care of keeping this list alphabetically sorted.
12181 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012182static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012183 { "base", "base", PAT_MATCH_STR },
12184 { "base_beg", "base", PAT_MATCH_BEG },
12185 { "base_dir", "base", PAT_MATCH_DIR },
12186 { "base_dom", "base", PAT_MATCH_DOM },
12187 { "base_end", "base", PAT_MATCH_END },
12188 { "base_len", "base", PAT_MATCH_LEN },
12189 { "base_reg", "base", PAT_MATCH_REG },
12190 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012191
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012192 { "cook", "req.cook", PAT_MATCH_STR },
12193 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12194 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12195 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12196 { "cook_end", "req.cook", PAT_MATCH_END },
12197 { "cook_len", "req.cook", PAT_MATCH_LEN },
12198 { "cook_reg", "req.cook", PAT_MATCH_REG },
12199 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012200
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012201 { "hdr", "req.hdr", PAT_MATCH_STR },
12202 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12203 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12204 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12205 { "hdr_end", "req.hdr", PAT_MATCH_END },
12206 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12207 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12208 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012209
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012210 /* these two declarations uses strings with list storage (in place
12211 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12212 * and delete functions are relative to the list management. The parse
12213 * and match method are related to the corresponding fetch methods. This
12214 * is very particular ACL declaration mode.
12215 */
12216 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12217 { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012218
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012219 { "path", "path", PAT_MATCH_STR },
12220 { "path_beg", "path", PAT_MATCH_BEG },
12221 { "path_dir", "path", PAT_MATCH_DIR },
12222 { "path_dom", "path", PAT_MATCH_DOM },
12223 { "path_end", "path", PAT_MATCH_END },
12224 { "path_len", "path", PAT_MATCH_LEN },
12225 { "path_reg", "path", PAT_MATCH_REG },
12226 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012227
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012228 { "req_ver", "req.ver", PAT_MATCH_STR },
12229 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012230
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012231 { "scook", "res.cook", PAT_MATCH_STR },
12232 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12233 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12234 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12235 { "scook_end", "res.cook", PAT_MATCH_END },
12236 { "scook_len", "res.cook", PAT_MATCH_LEN },
12237 { "scook_reg", "res.cook", PAT_MATCH_REG },
12238 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012239
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012240 { "shdr", "res.hdr", PAT_MATCH_STR },
12241 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12242 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12243 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12244 { "shdr_end", "res.hdr", PAT_MATCH_END },
12245 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12246 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12247 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012248
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012249 { "url", "url", PAT_MATCH_STR },
12250 { "url_beg", "url", PAT_MATCH_BEG },
12251 { "url_dir", "url", PAT_MATCH_DIR },
12252 { "url_dom", "url", PAT_MATCH_DOM },
12253 { "url_end", "url", PAT_MATCH_END },
12254 { "url_len", "url", PAT_MATCH_LEN },
12255 { "url_reg", "url", PAT_MATCH_REG },
12256 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012257
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012258 { "urlp", "urlp", PAT_MATCH_STR },
12259 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12260 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12261 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12262 { "urlp_end", "urlp", PAT_MATCH_END },
12263 { "urlp_len", "urlp", PAT_MATCH_LEN },
12264 { "urlp_reg", "urlp", PAT_MATCH_REG },
12265 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012266
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012267 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012268}};
12269
12270/************************************************************************/
12271/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012272/************************************************************************/
12273/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012274static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012275 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012276 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012277 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12278
Willy Tarreau87b09662015-04-03 00:22:06 +020012279 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012280 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012281
12282 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012283 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12284 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12285 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012286
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012287 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12288 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012289
Willy Tarreau409bcde2013-01-08 00:31:00 +010012290 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12291 * are only here to match the ACL's name, are request-only and are used
12292 * for ACL compatibility only.
12293 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012294 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12295 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012296 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12297 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012298
12299 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12300 * only here to match the ACL's name, are request-only and are used for
12301 * ACL compatibility only.
12302 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012303 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012304 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012305 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012306 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012307
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012308 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012309 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012310 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012311 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012312 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012313 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012314
12315 /* HTTP protocol on the request path */
12316 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012317 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012318
12319 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012320 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12321 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012322
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012323 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012324 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12325 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012326 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012327
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012328 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012329 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12330
Willy Tarreau18ed2562013-01-14 15:56:36 +010012331 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012332 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12333 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012334
Willy Tarreau18ed2562013-01-14 15:56:36 +010012335 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012336 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012337 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12338 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012339
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012340 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012341 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012342 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012343 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012344 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010012345 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012346 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012347
12348 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012349 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012350 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12351 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012352
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012353 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012354 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012355 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012356 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012357 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010012358 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012359 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012360
Willy Tarreau409bcde2013-01-08 00:31:00 +010012361 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012362 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012363 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12364 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012365 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012366
12367 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012368 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012369 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012370 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012371 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012372
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012373 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012374 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012375 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012376 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012377 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012378 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012379 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012380 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12381 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012382 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012383 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012384}};
12385
Willy Tarreau8797c062007-05-07 00:55:35 +020012386
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012387/************************************************************************/
12388/* All supported converter keywords must be declared here. */
12389/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012390/* Note: must not be declared <const> as its list will be overwritten */
12391static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012392 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012393 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012394 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12395 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012396 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012397 { NULL, NULL, 0, 0, 0 },
12398}};
12399
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012400
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012401/************************************************************************/
12402/* All supported http-request action keywords must be declared here. */
12403/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012404struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012405 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012406 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010012407 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012408 { "set-method", parse_set_req_line },
12409 { "set-path", parse_set_req_line },
12410 { "set-query", parse_set_req_line },
12411 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012412 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012413 }
12414};
12415
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012416struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012417 .kw = {
12418 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012419 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012420 { NULL, NULL }
12421 }
12422};
12423
Willy Tarreau8797c062007-05-07 00:55:35 +020012424__attribute__((constructor))
12425static void __http_protocol_init(void)
12426{
12427 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012428 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012429 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012430 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012431 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020012432}
12433
12434
Willy Tarreau58f10d72006-12-04 02:26:12 +010012435/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012436 * Local variables:
12437 * c-indent-level: 8
12438 * c-basic-offset: 8
12439 * End:
12440 */