blob: 22eab8b30aa7f0628d114203327669876e2db74d [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 Tarreau87b09662015-04-03 00:22:06 +0200104static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200105
David Carlier7365f7d2016-04-04 11:54:42 +0100106static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
107static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100108
Willy Tarreau80587432006-12-24 17:47:20 +0100109void init_proto_http()
110{
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200111 /* memory allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100112 pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
113 pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100114}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115
Willy Tarreau53b6c742006-12-17 13:37:46 +0100116/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100117 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +0200118 * the last CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100119 * The header is also automatically added to the index <hdr_idx>, and the end
120 * of headers is automatically adjusted. The number of bytes added is returned
121 * on success, otherwise <0 is returned indicating an error.
122 */
Willy Tarreau4d893d42018-07-12 15:43:32 +0200123static 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 +0100124{
Willy Tarreau4d893d42018-07-12 15:43:32 +0200125 return http_header_add_tail2(msg, hdr_idx, text, strlen(text));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100126}
127
128/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100129 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +0200130 * the last CRLF. <len> bytes are copied, not counting the CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100131 * The header is also automatically added to the index <hdr_idx>, and the end
132 * of headers is automatically adjusted. The number of bytes added is returned
133 * on success, otherwise <0 is returned indicating an error.
134 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100135int http_header_add_tail2(struct http_msg *msg,
136 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100137{
138 int bytes;
139
Willy Tarreau4d893d42018-07-12 15:43:32 +0200140 bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100141 if (!bytes)
142 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100143 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100144 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
145}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200146
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200147/* Find the first or next occurrence of header <name> in message buffer <sol>
148 * using headers index <idx>, and return it in the <ctx> structure. This
149 * structure holds everything necessary to use the header and find next
150 * occurrence. If its <idx> member is 0, the header is searched from the
151 * beginning. Otherwise, the next occurrence is returned. The function returns
152 * 1 when it finds a value, and 0 when there is no more. It is very similar to
153 * http_find_header2() except that it is designed to work with full-line headers
154 * whose comma is not a delimiter but is part of the syntax. As a special case,
155 * if ctx->val is NULL when searching for a new values of a header, the current
156 * header is rescanned. This allows rescanning after a header deletion.
157 */
158int http_find_full_header2(const char *name, int len,
159 char *sol, struct hdr_idx *idx,
160 struct hdr_ctx *ctx)
161{
162 char *eol, *sov;
163 int cur_idx, old_idx;
164
165 cur_idx = ctx->idx;
166 if (cur_idx) {
167 /* We have previously returned a header, let's search another one */
168 sol = ctx->line;
169 eol = sol + idx->v[cur_idx].len;
170 goto next_hdr;
171 }
172
173 /* first request for this header */
174 sol += hdr_idx_first_pos(idx);
175 old_idx = 0;
176 cur_idx = hdr_idx_first_idx(idx);
177 while (cur_idx) {
178 eol = sol + idx->v[cur_idx].len;
179
180 if (len == 0) {
181 /* No argument was passed, we want any header.
182 * To achieve this, we simply build a fake request. */
183 while (sol + len < eol && sol[len] != ':')
184 len++;
185 name = sol;
186 }
187
188 if ((len < eol - sol) &&
189 (sol[len] == ':') &&
190 (strncasecmp(sol, name, len) == 0)) {
191 ctx->del = len;
192 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100193 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200194 sov++;
195
196 ctx->line = sol;
197 ctx->prev = old_idx;
198 ctx->idx = cur_idx;
199 ctx->val = sov - sol;
200 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100201 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200202 eol--;
203 ctx->tws++;
204 }
205 ctx->vlen = eol - sov;
206 return 1;
207 }
208 next_hdr:
209 sol = eol + idx->v[cur_idx].cr + 1;
210 old_idx = cur_idx;
211 cur_idx = idx->v[cur_idx].next;
212 }
213 return 0;
214}
215
Willy Tarreauc90dc232015-02-20 13:51:36 +0100216/* Find the first or next header field in message buffer <sol> using headers
217 * index <idx>, and return it in the <ctx> structure. This structure holds
218 * everything necessary to use the header and find next occurrence. If its
219 * <idx> member is 0, the first header is retrieved. Otherwise, the next
220 * occurrence is returned. The function returns 1 when it finds a value, and
221 * 0 when there is no more. It is equivalent to http_find_full_header2() with
222 * no header name.
223 */
224int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
225{
226 char *eol, *sov;
227 int cur_idx, old_idx;
228 int len;
229
230 cur_idx = ctx->idx;
231 if (cur_idx) {
232 /* We have previously returned a header, let's search another one */
233 sol = ctx->line;
234 eol = sol + idx->v[cur_idx].len;
235 goto next_hdr;
236 }
237
238 /* first request for this header */
239 sol += hdr_idx_first_pos(idx);
240 old_idx = 0;
241 cur_idx = hdr_idx_first_idx(idx);
242 while (cur_idx) {
243 eol = sol + idx->v[cur_idx].len;
244
245 len = 0;
246 while (1) {
247 if (len >= eol - sol)
248 goto next_hdr;
249 if (sol[len] == ':')
250 break;
251 len++;
252 }
253
254 ctx->del = len;
255 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100256 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100257 sov++;
258
259 ctx->line = sol;
260 ctx->prev = old_idx;
261 ctx->idx = cur_idx;
262 ctx->val = sov - sol;
263 ctx->tws = 0;
264
Willy Tarreau2235b262016-11-05 15:50:20 +0100265 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100266 eol--;
267 ctx->tws++;
268 }
269 ctx->vlen = eol - sov;
270 return 1;
271
272 next_hdr:
273 sol = eol + idx->v[cur_idx].cr + 1;
274 old_idx = cur_idx;
275 cur_idx = idx->v[cur_idx].next;
276 }
277 return 0;
278}
279
Willy Tarreau33a7e692007-06-10 19:45:56 +0200280/* Find the first or next occurrence of header <name> in message buffer <sol>
281 * using headers index <idx>, and return it in the <ctx> structure. This
282 * structure holds everything necessary to use the header and find next
283 * occurrence. If its <idx> member is 0, the header is searched from the
284 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100285 * 1 when it finds a value, and 0 when there is no more. It is designed to work
286 * with headers defined as comma-separated lists. As a special case, if ctx->val
287 * is NULL when searching for a new values of a header, the current header is
288 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200289 */
290int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100291 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200292 struct hdr_ctx *ctx)
293{
Willy Tarreau68085d82010-01-18 14:54:04 +0100294 char *eol, *sov;
295 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200296
Willy Tarreau68085d82010-01-18 14:54:04 +0100297 cur_idx = ctx->idx;
298 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200299 /* We have previously returned a value, let's search
300 * another one on the same line.
301 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200302 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200303 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100304 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200305 eol = sol + idx->v[cur_idx].len;
306
307 if (sov >= eol)
308 /* no more values in this header */
309 goto next_hdr;
310
Willy Tarreau68085d82010-01-18 14:54:04 +0100311 /* values remaining for this header, skip the comma but save it
312 * for later use (eg: for header deletion).
313 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200314 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100315 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200316 sov++;
317
318 goto return_hdr;
319 }
320
321 /* first request for this header */
322 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100323 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200324 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200325 while (cur_idx) {
326 eol = sol + idx->v[cur_idx].len;
327
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200328 if (len == 0) {
329 /* No argument was passed, we want any header.
330 * To achieve this, we simply build a fake request. */
331 while (sol + len < eol && sol[len] != ':')
332 len++;
333 name = sol;
334 }
335
Willy Tarreau33a7e692007-06-10 19:45:56 +0200336 if ((len < eol - sol) &&
337 (sol[len] == ':') &&
338 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100339 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200340 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100341 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200342 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100343
Willy Tarreau33a7e692007-06-10 19:45:56 +0200344 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100345 ctx->prev = old_idx;
346 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200347 ctx->idx = cur_idx;
348 ctx->val = sov - sol;
349
Willy Tarreauab813a42018-09-10 18:41:28 +0200350 eol = http_find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200351 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100352 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200353 eol--;
354 ctx->tws++;
355 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200356 ctx->vlen = eol - sov;
357 return 1;
358 }
359 next_hdr:
360 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100361 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200362 cur_idx = idx->v[cur_idx].next;
363 }
364 return 0;
365}
366
367int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100368 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200369 struct hdr_ctx *ctx)
370{
371 return http_find_header2(name, strlen(name), sol, idx, ctx);
372}
373
Willy Tarreau68085d82010-01-18 14:54:04 +0100374/* Remove one value of a header. This only works on a <ctx> returned by one of
375 * the http_find_header functions. The value is removed, as well as surrounding
376 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100377 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100378 * message <msg>. The new index is returned. If it is zero, it means there is
379 * no more header, so any processing may stop. The ctx is always left in a form
380 * that can be handled by http_find_header2() to find next occurrence.
381 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100382int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100383{
384 int cur_idx = ctx->idx;
385 char *sol = ctx->line;
386 struct hdr_idx_elem *hdr;
387 int delta, skip_comma;
388
389 if (!cur_idx)
390 return 0;
391
392 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200393 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100394 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200395 delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100396 http_msg_move_end(msg, delta);
397 idx->used--;
398 hdr->len = 0; /* unused entry */
399 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100400 if (idx->tail == ctx->idx)
401 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100402 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100403 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100404 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200405 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100406 return ctx->idx;
407 }
408
409 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200410 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
411 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100412 */
413
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200414 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200415 delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200416 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100417 NULL, 0);
418 hdr->len += delta;
419 http_msg_move_end(msg, delta);
420 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200421 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100422 return ctx->idx;
423}
424
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100425/* This function handles a server error at the stream interface level. The
426 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100427 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100428 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100429 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200430 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200431static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +0200432 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433{
Willy Tarreau2019f952017-03-14 11:07:31 +0100434 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100435 channel_auto_read(si_oc(si));
436 channel_abort(si_oc(si));
437 channel_auto_close(si_oc(si));
438 channel_erase(si_oc(si));
439 channel_auto_close(si_ic(si));
440 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200441 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200442 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200443 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200444 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200445 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200446 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200447}
448
Willy Tarreau87b09662015-04-03 00:22:06 +0200449/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100450 * and message.
451 */
452
Willy Tarreau83061a82018-07-13 11:56:34 +0200453struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100454{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200455 const int msgnum = http_get_status_idx(s->txn->status);
456
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200457 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200458 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200459 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200460 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100461 else
462 return &http_err_chunks[msgnum];
463}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100465void
Willy Tarreau83061a82018-07-13 11:56:34 +0200466http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100467{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200468 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100469 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100470 stream_int_retnclose(&s->si[0], msg);
471}
472
Willy Tarreau21d2af32008-02-14 20:25:24 +0100473/* Parse the URI from the given transaction (which is assumed to be in request
474 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
475 * It is returned otherwise.
476 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200477char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100478{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200479 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100480
Willy Tarreau6b952c82018-09-10 17:45:34 +0200481 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 +0100482
Willy Tarreau6b952c82018-09-10 17:45:34 +0200483 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100484}
485
Willy Tarreau71241ab2012-12-27 11:30:54 +0100486/* Returns a 302 for a redirectable request that reaches a server working in
487 * in redirect mode. This may only be called just after the stream interface
488 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
489 * follow normal proxy processing. NOTE: this function is designed to support
490 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100491 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200492void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100493{
494 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100495 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100496 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200497 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100498
499 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 trash.data = strlen(HTTP_302);
501 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100502
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100503 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100504
Willy Tarreauefb453c2008-10-26 20:49:47 +0100505 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200506 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100507 return;
508
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100509 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100510 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200511 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
512 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100513 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100514
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200515 /* 3: add the request URI. Since it was already forwarded, we need
516 * to temporarily rewind the buffer.
517 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200518 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200519 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200520
Willy Tarreau6b952c82018-09-10 17:45:34 +0200521 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200522 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 +0200523
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200524 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200525
Willy Tarreauefb453c2008-10-26 20:49:47 +0100526 if (!path)
527 return;
528
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200529 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100530 return;
531
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200532 memcpy(trash.area + trash.data, path, len);
533 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100534
535 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200536 memcpy(trash.area + trash.data,
537 "\r\nProxy-Connection: close\r\n\r\n", 29);
538 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100539 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200540 memcpy(trash.area + trash.data,
541 "\r\nConnection: close\r\n\r\n", 23);
542 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100543 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100544
545 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200546 si_shutr(si);
547 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100548 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100549 si->state = SI_ST_CLO;
550
551 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100552 txn->status = 302;
553 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100554
555 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100556 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500557 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100558}
559
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100560/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100561 * that the server side is closed. Note that err_type is actually a
562 * bitmask, where almost only aborts may be cumulated with other
563 * values. We consider that aborted operations are more important
564 * than timeouts or errors due to the fact that nobody else in the
565 * logs might explain incomplete retries. All others should avoid
566 * being cumulated. It should normally not be possible to have multiple
567 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100568 * Note that connection errors appearing on the second request of a keep-alive
569 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100570 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200571void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100572{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100573 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100574
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200575 /* set s->txn->status for http_error_message(s) */
576 s->txn->status = 503;
577
Willy Tarreauefb453c2008-10-26 20:49:47 +0100578 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200579 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100580 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100581 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200582 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100583 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200584 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100585 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200586 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100587 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100588 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200589 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100590 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100591 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200592 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100593 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200594 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100595 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200596 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100597 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200598 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200599 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200600 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100601 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200602 http_error_message(s));
603 else { /* SI_ET_CONN_OTHER and others */
604 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200605 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100606 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200607 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100608}
609
Willy Tarreau42250582007-04-01 01:30:43 +0200610extern const char sess_term_cond[8];
611extern const char sess_fin_state[8];
612extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +0100613struct pool_head *pool_head_http_txn;
614struct pool_head *pool_head_requri;
615struct pool_head *pool_head_capture = NULL;
616struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100617
Willy Tarreau117f59e2007-03-04 18:17:17 +0100618/*
619 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200620 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100621 */
622void capture_headers(char *som, struct hdr_idx *idx,
623 char **cap, struct cap_hdr *cap_hdr)
624{
625 char *eol, *sol, *col, *sov;
626 int cur_idx;
627 struct cap_hdr *h;
628 int len;
629
630 sol = som + hdr_idx_first_pos(idx);
631 cur_idx = hdr_idx_first_idx(idx);
632
633 while (cur_idx) {
634 eol = sol + idx->v[cur_idx].len;
635
636 col = sol;
637 while (col < eol && *col != ':')
638 col++;
639
640 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100641 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100642 sov++;
643
644 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200645 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100646 (strncasecmp(sol, h->name, h->namelen) == 0)) {
647 if (cap[h->index] == NULL)
648 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100649 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100650
651 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100652 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100653 continue;
654 }
655
656 len = eol - sov;
657 if (len > h->len)
658 len = h->len;
659
660 memcpy(cap[h->index], sov, len);
661 cap[h->index][len]=0;
662 }
663 }
664 sol = eol + idx->v[cur_idx].cr + 1;
665 cur_idx = idx->v[cur_idx].next;
666 }
667}
668
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669/*
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100670 * Returns the data from Authorization header. Function may be called more
671 * than once so data is stored in txn->auth_data. When no header is found
672 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +0100673 * searching again for something we are unable to find anyway. However, if
674 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +0200675 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100676 */
677
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100678int
Willy Tarreau87b09662015-04-03 00:22:06 +0200679get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100680{
681
Willy Tarreaueee5b512015-04-03 23:46:31 +0200682 struct http_txn *txn = s->txn;
Willy Tarreau83061a82018-07-13 11:56:34 +0200683 struct buffer auth_method;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100684 struct hdr_ctx ctx;
685 char *h, *p;
686 int len;
687
688#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +0200689 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100690#endif
691
692 if (txn->auth.method == HTTP_AUTH_WRONG)
693 return 0;
694
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100695 txn->auth.method = HTTP_AUTH_WRONG;
696
697 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +0100698
699 if (txn->flags & TX_USE_PX_CONN) {
700 h = "Proxy-Authorization";
701 len = strlen(h);
702 } else {
703 h = "Authorization";
704 len = strlen(h);
705 }
706
Willy Tarreauf37954d2018-06-15 18:31:02 +0200707 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100708 return 0;
709
710 h = ctx.line + ctx.val;
711
712 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +0100713 len = p - h;
714 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100715 return 0;
716
David Carlier7365f7d2016-04-04 11:54:42 +0100717 if (chunk_initlen(&auth_method, h, 0, len) != 1)
718 return 0;
719
Willy Tarreau5c557d12016-03-13 08:17:02 +0100720 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100721
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200722 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200723 struct buffer *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100724
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200725 len = base64dec(txn->auth.method_data.area,
726 txn->auth.method_data.data,
727 http_auth->area, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100728
729 if (len < 0)
730 return 0;
731
732
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200733 http_auth->area[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100734
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200735 p = strchr(http_auth->area, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100736
737 if (!p)
738 return 0;
739
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200740 txn->auth.user = http_auth->area;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100741 *p = '\0';
742 txn->auth.pass = p+1;
743
744 txn->auth.method = HTTP_AUTH_BASIC;
745 return 1;
746 }
747
748 return 0;
749}
750
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100751
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200752/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
753 * conversion succeeded, 0 in case of error. If the request was already 1.X,
754 * nothing is done and 1 is returned.
755 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100756static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200757{
758 int delta;
759 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100760 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200761
762 if (msg->sl.rq.v_l != 0)
763 return 1;
764
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300765 /* RFC 1945 allows only GET for HTTP/0.9 requests */
766 if (txn->meth != HTTP_METH_GET)
767 return 0;
768
Willy Tarreauf37954d2018-06-15 18:31:02 +0200769 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200770
771 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300772 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
773 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200774 }
775 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200776 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100777 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200778 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200779 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200780 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200781 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200782 NULL, NULL);
783 if (unlikely(!cur_end))
784 return 0;
785
786 /* we have a full HTTP/1.0 request now and we know that
787 * we have either a CR or an LF at <ptr>.
788 */
789 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
790 return 1;
791}
792
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100793/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100794 * and "keep-alive" values. If we already know that some headers may safely
795 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100796 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
797 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100798 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100799 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
800 * found, and TX_CON_*_SET is adjusted depending on what is left so only
801 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100802 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100803 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100804void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100805{
Willy Tarreau5b154472009-12-21 20:11:07 +0100806 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100807 const char *hdr_val = "Connection";
808 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100809
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100810 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100811 return;
812
Willy Tarreau88d349d2010-01-25 12:15:43 +0100813 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
814 hdr_val = "Proxy-Connection";
815 hdr_len = 16;
816 }
817
Willy Tarreau5b154472009-12-21 20:11:07 +0100818 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100819 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200820 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100821 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
822 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100823 if (to_del & 2)
824 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100825 else
826 txn->flags |= TX_CON_KAL_SET;
827 }
828 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
829 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100830 if (to_del & 1)
831 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100832 else
833 txn->flags |= TX_CON_CLO_SET;
834 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100835 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
836 txn->flags |= TX_HDR_CONN_UPG;
837 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100838 }
839
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100840 txn->flags |= TX_HDR_CONN_PRS;
841 return;
842}
Willy Tarreau5b154472009-12-21 20:11:07 +0100843
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100844/* Apply desired changes on the Connection: header. Values may be removed and/or
845 * added depending on the <wanted> flags, which are exclusively composed of
846 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
847 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
848 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100849void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100850{
851 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100852 const char *hdr_val = "Connection";
853 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100854
855 ctx.idx = 0;
856
Willy Tarreau88d349d2010-01-25 12:15:43 +0100857
858 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
859 hdr_val = "Proxy-Connection";
860 hdr_len = 16;
861 }
862
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100863 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200864 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100865 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
866 if (wanted & TX_CON_KAL_SET)
867 txn->flags |= TX_CON_KAL_SET;
868 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100869 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100870 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100871 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
872 if (wanted & TX_CON_CLO_SET)
873 txn->flags |= TX_CON_CLO_SET;
874 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100875 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100876 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100877 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100878
879 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
880 return;
881
882 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
883 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100884 hdr_val = "Connection: close";
885 hdr_len = 17;
886 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
887 hdr_val = "Proxy-Connection: close";
888 hdr_len = 23;
889 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100890 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100891 }
892
893 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
894 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100895 hdr_val = "Connection: keep-alive";
896 hdr_len = 22;
897 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
898 hdr_val = "Proxy-Connection: keep-alive";
899 hdr_len = 28;
900 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100901 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100902 }
903 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100904}
905
Willy Tarreau87b09662015-04-03 00:22:06 +0200906void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200907{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200908 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200909 int tmp = TX_CON_WANT_KAL;
910
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200911 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
912 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200913 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
914 tmp = TX_CON_WANT_TUN;
915
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200916 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200917 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
918 tmp = TX_CON_WANT_TUN;
919 }
920
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200921 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200922 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
923 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200924 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200925 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
926 tmp = TX_CON_WANT_CLO;
927 else
928 tmp = TX_CON_WANT_SCL;
929 }
930
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200931 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200932 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
933 tmp = TX_CON_WANT_CLO;
934
935 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
936 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
937
938 if (!(txn->flags & TX_HDR_CONN_PRS) &&
939 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
940 /* parse the Connection header and possibly clean it */
941 int to_del = 0;
942 if ((msg->flags & HTTP_MSGF_VER_11) ||
943 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200944 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200945 to_del |= 2; /* remove "keep-alive" */
946 if (!(msg->flags & HTTP_MSGF_VER_11))
947 to_del |= 1; /* remove "close" */
948 http_parse_connection_header(txn, msg, to_del);
949 }
950
951 /* check if client or config asks for explicit close in KAL/SCL */
952 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
953 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
954 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
955 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
956 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200957 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200958 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
959}
William Lallemand82fe75c2012-10-23 10:25:10 +0200960
Willy Tarreaud787e662009-07-07 10:14:51 +0200961/* This stream analyser waits for a complete HTTP request. It returns 1 if the
962 * processing can continue on next analysers, or zero if it either needs more
963 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100964 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200965 * when it has nothing left to do, and may remove any analyser when it wants to
966 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100967 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200968int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100969{
Willy Tarreau59234e92008-11-30 23:51:27 +0100970 /*
971 * We will parse the partial (or complete) lines.
972 * We will check the request syntax, and also join multi-line
973 * headers. An index of all the lines will be elaborated while
974 * parsing.
975 *
976 * For the parsing, we use a 28 states FSM.
977 *
978 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200979 * ci_head(req) = beginning of request
980 * ci_head(req) + msg->eoh = end of processed headers / start of current one
981 * ci_tail(req) = end of input data
982 * msg->eol = end of current header or line (LF or CRLF)
983 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200984 *
985 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200986 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200987 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
988 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100989 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100990
Willy Tarreau59234e92008-11-30 23:51:27 +0100991 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200992 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200993 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100994 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200995 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100996
Christopher Faulet45073512018-07-20 10:16:29 +0200997 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 +0100998 now_ms, __FUNCTION__,
999 s,
1000 req,
1001 req->rex, req->wex,
1002 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001003 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +01001004 req->analysers);
1005
Willy Tarreau52a0c602009-08-16 22:45:38 +02001006 /* we're speaking HTTP here, so let's speak HTTP to the client */
1007 s->srv_error = http_return_srv_error;
1008
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001009 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +02001010 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -03001011 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1012
Willy Tarreau83e3af02009-12-28 17:39:57 +01001013 /* There's a protected area at the end of the buffer for rewriting
1014 * purposes. We don't want to start to parse the request if the
1015 * protected area is affected, because we may have to move processed
1016 * data later, which is much more complicated.
1017 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02001018 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02001019 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001020 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001021 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001022 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001023 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001024 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001025 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001026 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001027 return 0;
1028 }
Willy Tarreau188e2302018-06-15 11:11:53 +02001029 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001030 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001031 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001032 }
1033
Willy Tarreauf37954d2018-06-15 18:31:02 +02001034 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001035 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001036 }
1037
Willy Tarreau59234e92008-11-30 23:51:27 +01001038 /* 1: we might have to print this header in debug mode */
1039 if (unlikely((global.mode & MODE_DEBUG) &&
1040 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001041 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001042 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001043
Willy Tarreauf37954d2018-06-15 18:31:02 +02001044 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +02001045 /* this is a bit complex : in case of error on the request line,
1046 * we know that rq.l is still zero, so we display only the part
1047 * up to the end of the line (truncated by debug_hdr).
1048 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001049 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +01001050 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001051
Willy Tarreau59234e92008-11-30 23:51:27 +01001052 sol += hdr_idx_first_pos(&txn->hdr_idx);
1053 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001054
Willy Tarreau59234e92008-11-30 23:51:27 +01001055 while (cur_idx) {
1056 eol = sol + txn->hdr_idx.v[cur_idx].len;
1057 debug_hdr("clihdr", s, sol, eol);
1058 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1059 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001060 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001061 }
1062
Willy Tarreau58f10d72006-12-04 02:26:12 +01001063
Willy Tarreau59234e92008-11-30 23:51:27 +01001064 /*
1065 * Now we quickly check if we have found a full valid request.
1066 * If not so, we check the FD and buffer states before leaving.
1067 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001068 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001069 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001070 * on a keep-alive stream, if we encounter and error, close, t/o,
1071 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001072 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001073 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001074 * Last, we may increase some tracked counters' http request errors on
1075 * the cases that are deliberately the client's fault. For instance,
1076 * a timeout or connection reset is not counted as an error. However
1077 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001078 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001079
Willy Tarreau655dce92009-11-08 13:10:58 +01001080 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001081 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001082 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001083 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001084 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001085 stream_inc_http_req_ctr(s);
1086 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001087 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001088 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001089 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001090
Willy Tarreau59234e92008-11-30 23:51:27 +01001091 /* 1: Since we are in header mode, if there's no space
1092 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001093 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001094 * must terminate it now.
1095 */
Willy Tarreau23752332018-06-15 14:54:53 +02001096 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001097 /* FIXME: check if URI is set and return Status
1098 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001099 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001100 stream_inc_http_req_ctr(s);
1101 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001102 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001103 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02001104 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +01001105 goto return_bad_req;
1106 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001107
Willy Tarreau59234e92008-11-30 23:51:27 +01001108 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001109 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001110 if (!(s->flags & SF_ERR_MASK))
1111 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001112
Willy Tarreaufcffa692010-01-10 14:21:19 +01001113 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001114 goto failed_keep_alive;
1115
Willy Tarreau0f228a02015-05-01 15:37:53 +02001116 if (sess->fe->options & PR_O_IGNORE_PRB)
1117 goto failed_keep_alive;
1118
Willy Tarreau59234e92008-11-30 23:51:27 +01001119 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001120 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001121 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001122 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001123 }
1124
Willy Tarreaudc979f22012-12-04 10:39:01 +01001125 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001126 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001127 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001128 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001129 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001130 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001131 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001132 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001133 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001134 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001135
Willy Tarreaue7dff022015-04-03 01:14:29 +02001136 if (!(s->flags & SF_FINST_MASK))
1137 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001138 return 0;
1139 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001140
Willy Tarreau59234e92008-11-30 23:51:27 +01001141 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001142 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001143 if (!(s->flags & SF_ERR_MASK))
1144 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001145
Willy Tarreaufcffa692010-01-10 14:21:19 +01001146 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001147 goto failed_keep_alive;
1148
Willy Tarreau0f228a02015-05-01 15:37:53 +02001149 if (sess->fe->options & PR_O_IGNORE_PRB)
1150 goto failed_keep_alive;
1151
Willy Tarreau59234e92008-11-30 23:51:27 +01001152 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001153 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001154 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001155 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001156 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001157 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001158 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001159 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001160 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001161 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001162
Willy Tarreau87b09662015-04-03 00:22:06 +02001163 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001164 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001165 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001166 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001167 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001168
Willy Tarreaue7dff022015-04-03 01:14:29 +02001169 if (!(s->flags & SF_FINST_MASK))
1170 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001171 return 0;
1172 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001173
Willy Tarreau59234e92008-11-30 23:51:27 +01001174 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001175 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001176 if (!(s->flags & SF_ERR_MASK))
1177 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001178
Willy Tarreaufcffa692010-01-10 14:21:19 +01001179 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001180 goto failed_keep_alive;
1181
Willy Tarreau0f228a02015-05-01 15:37:53 +02001182 if (sess->fe->options & PR_O_IGNORE_PRB)
1183 goto failed_keep_alive;
1184
Willy Tarreau4076a152009-04-02 15:18:36 +02001185 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001186 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001187 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001188 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001189 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001190 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001191 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001192 stream_inc_http_err_ctr(s);
1193 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001194 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001195 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001196 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001197 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001198
Willy Tarreaue7dff022015-04-03 01:14:29 +02001199 if (!(s->flags & SF_FINST_MASK))
1200 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001201 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001202 }
1203
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001204 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001205 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001206 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001207#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001208 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001209 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001210 /* We need more data, we have to re-enable quick-ack in case we
1211 * previously disabled it, otherwise we might cause the client
1212 * to delay next data.
1213 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001214 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001215 }
1216#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001217
Willy Tarreaufcffa692010-01-10 14:21:19 +01001218 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1219 /* If the client starts to talk, let's fall back to
1220 * request timeout processing.
1221 */
1222 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001223 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001224 }
1225
Willy Tarreau59234e92008-11-30 23:51:27 +01001226 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001227 if (!tick_isset(req->analyse_exp)) {
1228 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1229 (txn->flags & TX_WAIT_NEXT_RQ) &&
1230 tick_isset(s->be->timeout.httpka))
1231 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1232 else
1233 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1234 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001235
Willy Tarreau59234e92008-11-30 23:51:27 +01001236 /* we're not ready yet */
1237 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001238
1239 failed_keep_alive:
1240 /* Here we process low-level errors for keep-alive requests. In
1241 * short, if the request is not the first one and it experiences
1242 * a timeout, read error or shutdown, we just silently close so
1243 * that the client can try again.
1244 */
1245 txn->status = 0;
1246 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001247 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001248 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001249 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001250 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001251 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001252 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001253 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001254
Willy Tarreaud787e662009-07-07 10:14:51 +02001255 /* OK now we have a complete HTTP request with indexed headers. Let's
1256 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001257 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001258 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001259 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001260 * byte after the last LF. msg->sov points to the first byte of data.
1261 * msg->eol cannot be trusted because it may have been left uninitialized
1262 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001263 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001264
Willy Tarreau87b09662015-04-03 00:22:06 +02001265 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001266 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001267
Willy Tarreaub16a5742010-01-10 14:46:16 +01001268 if (txn->flags & TX_WAIT_NEXT_RQ) {
1269 /* kill the pending keep-alive timeout */
1270 txn->flags &= ~TX_WAIT_NEXT_RQ;
1271 req->analyse_exp = TICK_ETERNITY;
1272 }
1273
1274
Willy Tarreaud787e662009-07-07 10:14:51 +02001275 /* Maybe we found in invalid header name while we were configured not
1276 * to block on that, so we have to capture it now.
1277 */
1278 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001279 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001280
Willy Tarreau59234e92008-11-30 23:51:27 +01001281 /*
1282 * 1: identify the method
1283 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001284 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001285
1286 /* we can make use of server redirect on GET and HEAD */
1287 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001288 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001289 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001290 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001291 /* PRI is reserved for the HTTP/2 preface */
1292 msg->err_pos = 0;
1293 goto return_bad_req;
1294 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001295
Willy Tarreau59234e92008-11-30 23:51:27 +01001296 /*
1297 * 2: check if the URI matches the monitor_uri.
1298 * We have to do this for every request which gets in, because
1299 * the monitor-uri is defined by the frontend.
1300 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001301 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1302 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001303 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001304 sess->fe->monitor_uri,
1305 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001306 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001307 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001308 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001309 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001310
Willy Tarreaue7dff022015-04-03 01:14:29 +02001311 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001312 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001313
Willy Tarreau59234e92008-11-30 23:51:27 +01001314 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001315 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001316 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001317
Willy Tarreau59234e92008-11-30 23:51:27 +01001318 ret = acl_pass(ret);
1319 if (cond->pol == ACL_COND_UNLESS)
1320 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001321
Willy Tarreau59234e92008-11-30 23:51:27 +01001322 if (ret) {
1323 /* we fail this request, let's return 503 service unavail */
1324 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001325 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001326 if (!(s->flags & SF_ERR_MASK))
1327 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001328 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001329 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001331
Willy Tarreau59234e92008-11-30 23:51:27 +01001332 /* nothing to fail, let's reply normaly */
1333 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001334 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001335 if (!(s->flags & SF_ERR_MASK))
1336 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001337 goto return_prx_cond;
1338 }
1339
1340 /*
1341 * 3: Maybe we have to copy the original REQURI for the logs ?
1342 * Note: we cannot log anymore if the request has been
1343 * classified as invalid.
1344 */
1345 if (unlikely(s->logs.logwait & LW_REQ)) {
1346 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001347 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001348 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001350 if (urilen >= global.tune.requri_len )
1351 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001352 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001353 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001355 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001356 s->do_log(s);
1357 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001358 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001359 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001360 }
Willy Tarreau06619262006-12-17 08:37:22 +01001361
Willy Tarreau91852eb2015-05-01 13:26:00 +02001362 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1363 * exactly one digit "." one digit. This check may be disabled using
1364 * option accept-invalid-http-request.
1365 */
1366 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1367 if (msg->sl.rq.v_l != 8) {
1368 msg->err_pos = msg->sl.rq.v;
1369 goto return_bad_req;
1370 }
1371
Willy Tarreauf37954d2018-06-15 18:31:02 +02001372 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1373 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1374 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1375 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001376 msg->err_pos = msg->sl.rq.v + 4;
1377 goto return_bad_req;
1378 }
1379 }
Willy Tarreau13317662015-05-01 13:47:08 +02001380 else {
1381 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1382 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1383 goto return_bad_req;
1384 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001385
Willy Tarreau5b154472009-12-21 20:11:07 +01001386 /* ... and check if the request is HTTP/1.1 or above */
1387 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001388 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1389 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1390 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001391 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001392
1393 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001394 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 +01001395
Willy Tarreau88d349d2010-01-25 12:15:43 +01001396 /* if the frontend has "option http-use-proxy-header", we'll check if
1397 * we have what looks like a proxied connection instead of a connection,
1398 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1399 * Note that this is *not* RFC-compliant, however browsers and proxies
1400 * happen to do that despite being non-standard :-(
1401 * We consider that a request not beginning with either '/' or '*' is
1402 * a proxied connection, which covers both "scheme://location" and
1403 * CONNECT ip:port.
1404 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001405 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001406 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001407 txn->flags |= TX_USE_PX_CONN;
1408
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001409 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001410 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001411
Willy Tarreau59234e92008-11-30 23:51:27 +01001412 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001413 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02001414 capture_headers(ci_head(req), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001415 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001416
Willy Tarreau557f1992015-05-01 10:05:17 +02001417 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1418 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001419 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001420 * The length of a message body is determined by one of the following
1421 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001422 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001423 * 1. Any response to a HEAD request and any response with a 1xx
1424 * (Informational), 204 (No Content), or 304 (Not Modified) status
1425 * code is always terminated by the first empty line after the
1426 * header fields, regardless of the header fields present in the
1427 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001428 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001429 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1430 * the connection will become a tunnel immediately after the empty
1431 * line that concludes the header fields. A client MUST ignore any
1432 * Content-Length or Transfer-Encoding header fields received in
1433 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001434 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001435 * 3. If a Transfer-Encoding header field is present and the chunked
1436 * transfer coding (Section 4.1) is the final encoding, the message
1437 * body length is determined by reading and decoding the chunked
1438 * data until the transfer coding indicates the data is complete.
1439 *
1440 * If a Transfer-Encoding header field is present in a response and
1441 * the chunked transfer coding is not the final encoding, the
1442 * message body length is determined by reading the connection until
1443 * it is closed by the server. If a Transfer-Encoding header field
1444 * is present in a request and the chunked transfer coding is not
1445 * the final encoding, the message body length cannot be determined
1446 * reliably; the server MUST respond with the 400 (Bad Request)
1447 * status code and then close the connection.
1448 *
1449 * If a message is received with both a Transfer-Encoding and a
1450 * Content-Length header field, the Transfer-Encoding overrides the
1451 * Content-Length. Such a message might indicate an attempt to
1452 * perform request smuggling (Section 9.5) or response splitting
1453 * (Section 9.4) and ought to be handled as an error. A sender MUST
1454 * remove the received Content-Length field prior to forwarding such
1455 * a message downstream.
1456 *
1457 * 4. If a message is received without Transfer-Encoding and with
1458 * either multiple Content-Length header fields having differing
1459 * field-values or a single Content-Length header field having an
1460 * invalid value, then the message framing is invalid and the
1461 * recipient MUST treat it as an unrecoverable error. If this is a
1462 * request message, the server MUST respond with a 400 (Bad Request)
1463 * status code and then close the connection. If this is a response
1464 * message received by a proxy, the proxy MUST close the connection
1465 * to the server, discard the received response, and send a 502 (Bad
1466 * Gateway) response to the client. If this is a response message
1467 * received by a user agent, the user agent MUST close the
1468 * connection to the server and discard the received response.
1469 *
1470 * 5. If a valid Content-Length header field is present without
1471 * Transfer-Encoding, its decimal value defines the expected message
1472 * body length in octets. If the sender closes the connection or
1473 * the recipient times out before the indicated number of octets are
1474 * received, the recipient MUST consider the message to be
1475 * incomplete and close the connection.
1476 *
1477 * 6. If this is a request message and none of the above are true, then
1478 * the message body length is zero (no message body is present).
1479 *
1480 * 7. Otherwise, this is a response message without a declared message
1481 * body length, so the message body length is determined by the
1482 * number of octets received prior to the server closing the
1483 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001484 */
1485
Willy Tarreau32b47f42009-10-18 20:55:02 +02001486 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001487 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001488 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001489 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001490 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001491 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001492 /* chunked not last, return badreq */
1493 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001494 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001495 }
1496
Willy Tarreau1c913912015-04-30 10:57:51 +02001497 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001498 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001499 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001500 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001501 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1502 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001503 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001504 signed long long cl;
1505
Willy Tarreauad14f752011-09-02 20:33:27 +02001506 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001507 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001508 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001509 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001510
Willy Tarreauad14f752011-09-02 20:33:27 +02001511 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001512 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001513 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001514 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001515
Willy Tarreauad14f752011-09-02 20:33:27 +02001516 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001517 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001518 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001519 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001520
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001521 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001522 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001523 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001524 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001525
Christopher Fauletbe821b92017-03-30 11:21:53 +02001526 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001527 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001528 }
1529
Willy Tarreau34dfc602015-05-01 10:09:49 +02001530 /* even bodyless requests have a known length */
1531 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001532
Willy Tarreau179085c2014-04-28 16:48:56 +02001533 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1534 * only change if both the request and the config reference something else.
1535 * Option httpclose by itself sets tunnel mode where headers are mangled.
1536 * However, if another mode is set, it will affect it (eg: server-close/
1537 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1538 * if FE and BE have the same settings (common). The method consists in
1539 * checking if options changed between the two calls (implying that either
1540 * one is non-null, or one of them is non-null and we are there for the first
1541 * time.
1542 */
1543 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001544 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001545 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001546
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001547 /* we may have to wait for the request's body */
1548 if ((s->be->options & PR_O_WREQ_BODY) &&
1549 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1550 req->analysers |= AN_REQ_HTTP_BODY;
1551
Willy Tarreau83ece462017-12-21 15:13:09 +01001552 /*
1553 * RFC7234#4:
1554 * A cache MUST write through requests with methods
1555 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1556 * the origin server; i.e., a cache is not allowed
1557 * to generate a reply to such a request before
1558 * having forwarded the request and having received
1559 * a corresponding response.
1560 *
1561 * RFC7231#4.2.1:
1562 * Of the request methods defined by this
1563 * specification, the GET, HEAD, OPTIONS, and TRACE
1564 * methods are defined to be safe.
1565 */
1566 if (likely(txn->meth == HTTP_METH_GET ||
1567 txn->meth == HTTP_METH_HEAD ||
1568 txn->meth == HTTP_METH_OPTIONS ||
1569 txn->meth == HTTP_METH_TRACE))
1570 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1571
Willy Tarreaud787e662009-07-07 10:14:51 +02001572 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001573 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001574 req->analyse_exp = TICK_ETERNITY;
1575 return 1;
1576
1577 return_bad_req:
1578 /* We centralize bad requests processing here */
1579 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1580 /* we detected a parsing error. We want to archive this request
1581 * in the dedicated proxy area for later troubleshooting.
1582 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001583 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001584 }
1585
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001586 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001587 txn->req.msg_state = HTTP_MSG_ERROR;
1588 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001589 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001590
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001591 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001592 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001593 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001594
1595 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001596 if (!(s->flags & SF_ERR_MASK))
1597 s->flags |= SF_ERR_PRXCOND;
1598 if (!(s->flags & SF_FINST_MASK))
1599 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001600
Christopher Faulet0184ea72017-01-05 14:06:34 +01001601 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001602 req->analyse_exp = TICK_ETERNITY;
1603 return 0;
1604}
1605
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001606
Willy Tarreau347a35d2013-11-22 17:51:09 +01001607/* This function prepares an applet to handle the stats. It can deal with the
1608 * "100-continue" expectation, check that admin rules are met for POST requests,
1609 * and program a response message if something was unexpected. It cannot fail
1610 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001611 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001612 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001613 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001614 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001615int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001616{
1617 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001618 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001619 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001620 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001621 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001622 struct uri_auth *uri_auth = s->be->uri_auth;
1623 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001624 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001625
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001626 appctx = si_appctx(si);
1627 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1628 appctx->st1 = appctx->st2 = 0;
1629 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1630 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001631 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001632 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001633
Willy Tarreauf37954d2018-06-15 18:31:02 +02001634 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001635 lookup = uri + uri_auth->uri_len;
1636
1637 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1638 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001639 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001640 break;
1641 }
1642 }
1643
1644 if (uri_auth->refresh) {
1645 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1646 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001647 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001648 break;
1649 }
1650 }
1651 }
1652
1653 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1654 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001655 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001656 break;
1657 }
1658 }
1659
Willy Tarreau1e62df92016-01-11 18:57:53 +01001660 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1661 if (memcmp(h, ";typed", 6) == 0) {
1662 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1663 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1664 break;
1665 }
1666 }
1667
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001668 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1669 if (memcmp(h, ";st=", 4) == 0) {
1670 int i;
1671 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001672 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001673 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1674 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001675 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001676 break;
1677 }
1678 }
1679 break;
1680 }
1681 }
1682
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001683 appctx->ctx.stats.scope_str = 0;
1684 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001685 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1686 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1687 int itx = 0;
1688 const char *h2;
1689 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1690 const char *err;
1691
1692 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1693 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001694 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001695 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1696 itx++;
1697 h++;
1698 }
1699
1700 if (itx > STAT_SCOPE_TXT_MAXLEN)
1701 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001702 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001703
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001704 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001705 memcpy(scope_txt, h2, itx);
1706 scope_txt[itx] = '\0';
1707 err = invalid_char(scope_txt);
1708 if (err) {
1709 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001710 appctx->ctx.stats.scope_str = 0;
1711 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001712 }
1713 break;
1714 }
1715 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001716
1717 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001718 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001719 int ret = 1;
1720
1721 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001722 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 +01001723 ret = acl_pass(ret);
1724 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1725 ret = !ret;
1726 }
1727
1728 if (ret) {
1729 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001730 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001731 break;
1732 }
1733 }
1734
1735 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001736 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001737 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001738 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001739 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1740 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001741 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001742 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001743 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001744 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1745 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001746 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001747 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001748 else {
1749 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001750 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001751 }
1752
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001753 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001754 return 1;
1755}
1756
Lukas Tribus67db8df2013-06-23 17:37:13 +02001757/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1758 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1759 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001760void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001761{
1762#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001763 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001764 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1765#endif
1766#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001767 if (from->ss_family == AF_INET6) {
1768 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001769 /* v4-mapped addresses need IP_TOS */
1770 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1771 else
1772 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1773 }
1774#endif
1775}
1776
Willy Tarreau87b09662015-04-03 00:22:06 +02001777int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001778 const char* name, unsigned int name_len,
1779 const char *str, struct my_regex *re,
1780 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001781{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001782 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001783 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001784 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001785 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1786 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001787 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001788
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001789 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001790
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001791 /* Choose the header browsing function. */
1792 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001793 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001794 http_find_hdr_func = http_find_header2;
1795 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001796 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001797 http_find_hdr_func = http_find_full_header2;
1798 break;
1799 default: /* impossible */
1800 return -1;
1801 }
1802
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001803 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1804 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001805 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001806 char *val = ctx.line + ctx.val;
1807 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001808
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001809 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1810 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001811
Willy Tarreau6e27be12018-08-22 04:46:47 +02001812 len = exp_replace(output->area, output->size, val, str, pmatch);
1813 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001814 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001815
Willy Tarreau6e27be12018-08-22 04:46:47 +02001816 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001817
1818 hdr->len += delta;
1819 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001820
1821 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001822 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001823 }
1824
1825 return 0;
1826}
1827
Willy Tarreau87b09662015-04-03 00:22:06 +02001828static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001829 const char* name, unsigned int name_len,
1830 struct list *fmt, struct my_regex *re,
1831 int action)
1832{
Willy Tarreau83061a82018-07-13 11:56:34 +02001833 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001834 int ret = -1;
1835
1836 replace = alloc_trash_chunk();
1837 if (!replace)
1838 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001839
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001840 replace->data = build_logline(s, replace->area, replace->size, fmt);
1841 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001842 goto leave;
1843
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001844 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1845 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001846
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001847 leave:
1848 free_trash_chunk(replace);
1849 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001850}
1851
Willy Tarreau87b09662015-04-03 00:22:06 +02001852/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001853 * transaction <txn>. Returns the verdict of the first rule that prevents
1854 * further processing of the request (auth, deny, ...), and defaults to
1855 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1856 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001857 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1858 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1859 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001860 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001861enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001862http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001863{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001864 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001865 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001866 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001867 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001868 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001869 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02001870 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001871 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001872
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001873 /* If "the current_rule_list" match the executed rule list, we are in
1874 * resume condition. If a resume is needed it is always in the action
1875 * and never in the ACL or converters. In this case, we initialise the
1876 * current rule, and go to the action execution point.
1877 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001878 if (s->current_rule) {
1879 rule = s->current_rule;
1880 s->current_rule = NULL;
1881 if (s->current_rule_list == rules)
1882 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001883 }
1884 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001885
Willy Tarreauff011f22011-01-06 17:51:27 +01001886 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001887
Willy Tarreau96257ec2012-12-27 10:46:37 +01001888 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001889 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001890 int ret;
1891
Willy Tarreau192252e2015-04-04 01:47:55 +02001892 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001893 ret = acl_pass(ret);
1894
Willy Tarreauff011f22011-01-06 17:51:27 +01001895 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001896 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001897
1898 if (!ret) /* condition not matched */
1899 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001900 }
1901
Willy Tarreauacc98002015-09-27 23:34:39 +02001902 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001903resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001904 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001905 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02001906 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001907
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001908 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001909 if (deny_status)
1910 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001911 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001912
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001913 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001914 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001915 if (deny_status)
1916 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001917 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001918
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001919 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02001920 /* Auth might be performed on regular http-req rules as well as on stats */
1921 auth_realm = rule->arg.auth.realm;
1922 if (!auth_realm) {
1923 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1924 auth_realm = STATS_DEFAULT_REALM;
1925 else
1926 auth_realm = px->id;
1927 }
1928 /* send 401/407 depending on whether we use a proxy or not. We still
1929 * count one error, because normal browsing won't significantly
1930 * increase the counter but brute force attempts will.
1931 */
1932 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1933 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001934 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001935 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02001936 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001937
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001938 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02001939 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
1940 return HTTP_RULE_RES_BADREQ;
1941 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001942
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001943 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001944 s->task->nice = rule->arg.nice;
1945 break;
1946
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001947 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001948 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001949 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001950 break;
1951
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001952 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001953#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001954 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001955 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001956#endif
1957 break;
1958
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001959 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001960 s->logs.level = rule->arg.loglevel;
1961 break;
1962
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001963 case ACT_HTTP_REPLACE_HDR:
1964 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001965 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1966 rule->arg.hdr_add.name_len,
1967 &rule->arg.hdr_add.fmt,
1968 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06001969 return HTTP_RULE_RES_BADREQ;
1970 break;
1971
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001972 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001973 ctx.idx = 0;
1974 /* remove all occurrences of the header */
1975 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001976 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001977 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001978 }
Willy Tarreau85603282015-01-21 20:39:27 +01001979 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001980
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001981 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001982 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001983 /* The scope of the trash buffer must be limited to this function. The
1984 * build_logline() function can execute a lot of other function which
1985 * can use the trash buffer. So for limiting the scope of this global
1986 * buffer, we build first the header value using build_logline, and
1987 * after we store the header name.
1988 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001989 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001990
1991 replace = alloc_trash_chunk();
1992 if (!replace)
1993 return HTTP_RULE_RES_BADREQ;
1994
Thierry Fournier4b788f72016-06-01 13:35:36 +02001995 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001996 len += build_logline(s, replace->area + len,
1997 replace->size - len,
1998 &rule->arg.hdr_add.fmt);
1999 memcpy(replace->area, rule->arg.hdr_add.name,
2000 rule->arg.hdr_add.name_len);
2001 replace->area[rule->arg.hdr_add.name_len] = ':';
2002 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
2003 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002004
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002005 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002006 /* remove all occurrences of the header */
2007 ctx.idx = 0;
2008 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002009 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002010 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2011 }
2012 }
2013
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002014 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002015 static unsigned char rate_limit = 0;
2016
2017 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002018 replace->area[rule->arg.hdr_add.name_len] = 0;
2019 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,
2020 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002021 }
2022
2023 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2024 if (sess->fe != s->be)
2025 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2026 if (sess->listener->counters)
2027 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2028 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002029
2030 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002031 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002032 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002033
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002034 case ACT_HTTP_DEL_ACL:
2035 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002036 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002037 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002038
2039 /* collect reference */
2040 ref = pat_ref_lookup(rule->arg.map.ref);
2041 if (!ref)
2042 continue;
2043
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002044 /* allocate key */
2045 key = alloc_trash_chunk();
2046 if (!key)
2047 return HTTP_RULE_RES_BADREQ;
2048
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002049 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002050 key->data = build_logline(s, key->area, key->size,
2051 &rule->arg.map.key);
2052 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002053
2054 /* perform update */
2055 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002056 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002057 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002058 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002059
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002060 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002061 break;
2062 }
2063
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002064 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002065 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002066 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002067
2068 /* collect reference */
2069 ref = pat_ref_lookup(rule->arg.map.ref);
2070 if (!ref)
2071 continue;
2072
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002073 /* allocate key */
2074 key = alloc_trash_chunk();
2075 if (!key)
2076 return HTTP_RULE_RES_BADREQ;
2077
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002078 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002079 key->data = build_logline(s, key->area, key->size,
2080 &rule->arg.map.key);
2081 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002082
2083 /* perform update */
2084 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002085 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002086 if (pat_ref_find_elt(ref, key->area) == NULL)
2087 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002088 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002089
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002090 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002091 break;
2092 }
2093
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002094 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002095 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002096 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002097
2098 /* collect reference */
2099 ref = pat_ref_lookup(rule->arg.map.ref);
2100 if (!ref)
2101 continue;
2102
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002103 /* allocate key */
2104 key = alloc_trash_chunk();
2105 if (!key)
2106 return HTTP_RULE_RES_BADREQ;
2107
2108 /* allocate value */
2109 value = alloc_trash_chunk();
2110 if (!value) {
2111 free_trash_chunk(key);
2112 return HTTP_RULE_RES_BADREQ;
2113 }
2114
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002115 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002116 key->data = build_logline(s, key->area, key->size,
2117 &rule->arg.map.key);
2118 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002119
2120 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002121 value->data = build_logline(s, value->area,
2122 value->size,
2123 &rule->arg.map.value);
2124 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002125
2126 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002127 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002128 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002129 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002130 else
2131 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002132 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002133
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002134 free_trash_chunk(key);
2135 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002136 break;
2137 }
William Lallemand73025dd2014-04-24 14:38:37 +02002138
Thierry FOURNIER42148732015-09-02 17:17:33 +02002139 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002140 if ((s->req.flags & CF_READ_ERROR) ||
2141 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2142 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2143 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002144 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002145
Willy Tarreauacc98002015-09-27 23:34:39 +02002146 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002147 case ACT_RET_ERR:
2148 case ACT_RET_CONT:
2149 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002150 case ACT_RET_STOP:
2151 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002152 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002153 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002154 return HTTP_RULE_RES_YIELD;
2155 }
William Lallemand73025dd2014-04-24 14:38:37 +02002156 break;
2157
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002158 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002159 /* Note: only the first valid tracking parameter of each
2160 * applies.
2161 */
2162
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002163 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002164 struct stktable *t;
2165 struct stksess *ts;
2166 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002167 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002168
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002169 t = rule->arg.trk_ctr.table.t;
2170 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 +02002171
2172 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002173 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002174
2175 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002176 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2177 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2178 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002179 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002180
Emeric Brun819fc6f2017-06-13 19:37:32 +02002181 if (ptr1)
2182 stktable_data_cast(ptr1, http_req_cnt)++;
2183
2184 if (ptr2)
2185 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2186 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2187
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002188 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002189
2190 /* If data was modified, we need to touch to re-schedule sync */
2191 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002192 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002193
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002194 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002195 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002196 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002197 }
2198 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002199 break;
2200
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002201 /* other flags exists, but normaly, they never be matched. */
2202 default:
2203 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002204 }
2205 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002206
2207 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002208 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002209}
2210
Willy Tarreau71241ab2012-12-27 11:30:54 +01002211
Willy Tarreau51d861a2015-05-22 17:30:48 +02002212/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2213 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2214 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2215 * is returned, the process can continue the evaluation of next rule list. If
2216 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2217 * is returned, it means the operation could not be processed and a server error
2218 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2219 * deny rule. If *YIELD is returned, the caller must call again the function
2220 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002221 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002222static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002223http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002224{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002225 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002226 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002227 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002228 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002229 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002230 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002231
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002232 /* If "the current_rule_list" match the executed rule list, we are in
2233 * resume condition. If a resume is needed it is always in the action
2234 * and never in the ACL or converters. In this case, we initialise the
2235 * current rule, and go to the action execution point.
2236 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002237 if (s->current_rule) {
2238 rule = s->current_rule;
2239 s->current_rule = NULL;
2240 if (s->current_rule_list == rules)
2241 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002242 }
2243 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002244
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002245 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002246
2247 /* check optional condition */
2248 if (rule->cond) {
2249 int ret;
2250
Willy Tarreau192252e2015-04-04 01:47:55 +02002251 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002252 ret = acl_pass(ret);
2253
2254 if (rule->cond->pol == ACL_COND_UNLESS)
2255 ret = !ret;
2256
2257 if (!ret) /* condition not matched */
2258 continue;
2259 }
2260
Willy Tarreauacc98002015-09-27 23:34:39 +02002261 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002262resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002263 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002264 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002265 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002266
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002267 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002268 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002269 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002270
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002271 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002272 s->task->nice = rule->arg.nice;
2273 break;
2274
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002275 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002276 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002277 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002278 break;
2279
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002280 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002281#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002282 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002283 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002284#endif
2285 break;
2286
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002287 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002288 s->logs.level = rule->arg.loglevel;
2289 break;
2290
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002291 case ACT_HTTP_REPLACE_HDR:
2292 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002293 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2294 rule->arg.hdr_add.name_len,
2295 &rule->arg.hdr_add.fmt,
2296 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002297 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002298 break;
2299
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002300 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002301 ctx.idx = 0;
2302 /* remove all occurrences of the header */
2303 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002304 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002305 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2306 }
Willy Tarreau85603282015-01-21 20:39:27 +01002307 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002308
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002309 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002310 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002311 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002312
2313 replace = alloc_trash_chunk();
2314 if (!replace)
2315 return HTTP_RULE_RES_BADREQ;
2316
2317 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002318 memcpy(replace->area, rule->arg.hdr_add.name,
2319 rule->arg.hdr_add.name_len);
2320 replace->data = rule->arg.hdr_add.name_len;
2321 replace->area[replace->data++] = ':';
2322 replace->area[replace->data++] = ' ';
2323 replace->data += build_logline(s,
2324 replace->area + replace->data,
2325 replace->size - replace->data,
2326 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002327
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002328 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002329 /* remove all occurrences of the header */
2330 ctx.idx = 0;
2331 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002332 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002333 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2334 }
2335 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002336
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002337 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002338 static unsigned char rate_limit = 0;
2339
2340 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002341 replace->area[rule->arg.hdr_add.name_len] = 0;
2342 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,
2343 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002344 }
2345
2346 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2347 if (sess->fe != s->be)
2348 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2349 if (sess->listener->counters)
2350 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2351 if (objt_server(s->target))
2352 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2353 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002354
2355 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002356 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002357 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002358
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002359 case ACT_HTTP_DEL_ACL:
2360 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002361 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002362 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002363
2364 /* collect reference */
2365 ref = pat_ref_lookup(rule->arg.map.ref);
2366 if (!ref)
2367 continue;
2368
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002369 /* allocate key */
2370 key = alloc_trash_chunk();
2371 if (!key)
2372 return HTTP_RULE_RES_BADREQ;
2373
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002374 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002375 key->data = build_logline(s, key->area, key->size,
2376 &rule->arg.map.key);
2377 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002378
2379 /* perform update */
2380 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002381 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002382 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002383 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002384
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002385 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002386 break;
2387 }
2388
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002389 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002390 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002391 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002392
2393 /* collect reference */
2394 ref = pat_ref_lookup(rule->arg.map.ref);
2395 if (!ref)
2396 continue;
2397
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002398 /* allocate key */
2399 key = alloc_trash_chunk();
2400 if (!key)
2401 return HTTP_RULE_RES_BADREQ;
2402
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002403 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002404 key->data = build_logline(s, key->area, key->size,
2405 &rule->arg.map.key);
2406 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002407
2408 /* perform update */
2409 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002410 if (pat_ref_find_elt(ref, key->area) == NULL)
2411 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002412
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002413 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002414 break;
2415 }
2416
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002417 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002418 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002419 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002420
2421 /* collect reference */
2422 ref = pat_ref_lookup(rule->arg.map.ref);
2423 if (!ref)
2424 continue;
2425
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002426 /* allocate key */
2427 key = alloc_trash_chunk();
2428 if (!key)
2429 return HTTP_RULE_RES_BADREQ;
2430
2431 /* allocate value */
2432 value = alloc_trash_chunk();
2433 if (!value) {
2434 free_trash_chunk(key);
2435 return HTTP_RULE_RES_BADREQ;
2436 }
2437
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002438 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002439 key->data = build_logline(s, key->area, key->size,
2440 &rule->arg.map.key);
2441 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002442
2443 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002444 value->data = build_logline(s, value->area,
2445 value->size,
2446 &rule->arg.map.value);
2447 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002448
2449 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002450 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002451 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002452 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002453 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002454 else
2455 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002456 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002457 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002458 free_trash_chunk(key);
2459 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002460 break;
2461 }
William Lallemand73025dd2014-04-24 14:38:37 +02002462
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002463 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02002464 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2465 return HTTP_RULE_RES_BADREQ;
2466 return HTTP_RULE_RES_DONE;
2467
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002468 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2469 /* Note: only the first valid tracking parameter of each
2470 * applies.
2471 */
2472
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002473 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002474 struct stktable *t;
2475 struct stksess *ts;
2476 struct stktable_key *key;
2477 void *ptr;
2478
2479 t = rule->arg.trk_ctr.table.t;
2480 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2481
2482 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002483 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002484
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002485 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002486
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002487 /* let's count a new HTTP request as it's the first time we do it */
2488 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2489 if (ptr)
2490 stktable_data_cast(ptr, http_req_cnt)++;
2491
2492 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2493 if (ptr)
2494 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2495 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2496
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002497 /* When the client triggers a 4xx from the server, it's most often due
2498 * to a missing object or permission. These events should be tracked
2499 * because if they happen often, it may indicate a brute force or a
2500 * vulnerability scan. Normally this is done when receiving the response
2501 * but here we're tracking after this ought to have been done so we have
2502 * to do it on purpose.
2503 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002504 if ((unsigned)(txn->status - 400) < 100) {
2505 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2506 if (ptr)
2507 stktable_data_cast(ptr, http_err_cnt)++;
2508
2509 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2510 if (ptr)
2511 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2512 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2513 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002514
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002515 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002516
Emeric Brun0fed0b02017-11-29 16:15:07 +01002517 /* If data was modified, we need to touch to re-schedule sync */
2518 stktable_touch_local(t, ts, 0);
2519
Emeric Brun819fc6f2017-06-13 19:37:32 +02002520 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2521 if (sess->fe != s->be)
2522 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2523
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002524 }
2525 }
2526 break;
2527
Thierry FOURNIER42148732015-09-02 17:17:33 +02002528 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002529 if ((s->req.flags & CF_READ_ERROR) ||
2530 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2531 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2532 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002533 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002534
Willy Tarreauacc98002015-09-27 23:34:39 +02002535 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002536 case ACT_RET_ERR:
2537 case ACT_RET_CONT:
2538 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002539 case ACT_RET_STOP:
2540 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002541 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002542 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002543 return HTTP_RULE_RES_YIELD;
2544 }
William Lallemand73025dd2014-04-24 14:38:37 +02002545 break;
2546
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002547 /* other flags exists, but normaly, they never be matched. */
2548 default:
2549 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002550 }
2551 }
2552
2553 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002554 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002555}
2556
2557
Willy Tarreau71241ab2012-12-27 11:30:54 +01002558/* Perform an HTTP redirect based on the information in <rule>. The function
2559 * returns non-zero on success, or zero in case of a, irrecoverable error such
2560 * as too large a request to build a valid response.
2561 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002562static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002563{
Willy Tarreaub329a312015-05-22 16:27:37 +02002564 struct http_msg *req = &txn->req;
2565 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002566 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002567 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002568 int ret = 0;
2569
2570 chunk = alloc_trash_chunk();
2571 if (!chunk)
2572 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002573
2574 /* build redirect message */
2575 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002576 case 308:
2577 msg_fmt = HTTP_308;
2578 break;
2579 case 307:
2580 msg_fmt = HTTP_307;
2581 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002582 case 303:
2583 msg_fmt = HTTP_303;
2584 break;
2585 case 301:
2586 msg_fmt = HTTP_301;
2587 break;
2588 case 302:
2589 default:
2590 msg_fmt = HTTP_302;
2591 break;
2592 }
2593
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002594 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2595 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002596
2597 switch(rule->type) {
2598 case REDIRECT_TYPE_SCHEME: {
2599 const char *path;
2600 const char *host;
2601 struct hdr_ctx ctx;
2602 int pathlen;
2603 int hostlen;
2604
2605 host = "";
2606 hostlen = 0;
2607 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002608 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002609 host = ctx.line + ctx.val;
2610 hostlen = ctx.vlen;
2611 }
2612
Willy Tarreau6b952c82018-09-10 17:45:34 +02002613 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002614 /* build message using path */
2615 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002616 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002617 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2618 int qs = 0;
2619 while (qs < pathlen) {
2620 if (path[qs] == '?') {
2621 pathlen = qs;
2622 break;
2623 }
2624 qs++;
2625 }
2626 }
2627 } else {
2628 path = "/";
2629 pathlen = 1;
2630 }
2631
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002632 if (rule->rdr_str) { /* this is an old "redirect" rule */
2633 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002634 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002635 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002636
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002637 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002638 memcpy(chunk->area + chunk->data, rule->rdr_str,
2639 rule->rdr_len);
2640 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002641 }
2642 else {
2643 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002644 chunk->data += build_logline(s,
2645 chunk->area + chunk->data,
2646 chunk->size - chunk->data,
2647 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002648
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002649 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002650 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002651 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002652 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002653 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002654 memcpy(chunk->area + chunk->data, "://", 3);
2655 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002656
2657 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002658 memcpy(chunk->area + chunk->data, host, hostlen);
2659 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002660
2661 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002662 memcpy(chunk->area + chunk->data, path, pathlen);
2663 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002664
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002665 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002666 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002667 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002668 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002669 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002670 chunk->area[chunk->data] = '/';
2671 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002672 }
2673
2674 break;
2675 }
2676 case REDIRECT_TYPE_PREFIX: {
2677 const char *path;
2678 int pathlen;
2679
Willy Tarreau6b952c82018-09-10 17:45:34 +02002680 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002681 /* build message using path */
2682 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002683 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002684 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2685 int qs = 0;
2686 while (qs < pathlen) {
2687 if (path[qs] == '?') {
2688 pathlen = qs;
2689 break;
2690 }
2691 qs++;
2692 }
2693 }
2694 } else {
2695 path = "/";
2696 pathlen = 1;
2697 }
2698
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002699 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002700 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002701 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002702
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002703 /* add prefix. Note that if prefix == "/", we don't want to
2704 * add anything, otherwise it makes it hard for the user to
2705 * configure a self-redirection.
2706 */
2707 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002708 memcpy(chunk->area + chunk->data,
2709 rule->rdr_str, rule->rdr_len);
2710 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002711 }
2712 }
2713 else {
2714 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002715 chunk->data += build_logline(s,
2716 chunk->area + chunk->data,
2717 chunk->size - chunk->data,
2718 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002719
2720 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002721 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002722 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002723 }
2724
2725 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002726 memcpy(chunk->area + chunk->data, path, pathlen);
2727 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002728
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002729 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002730 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002731 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002732 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002733 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002734 chunk->area[chunk->data] = '/';
2735 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002736 }
2737
2738 break;
2739 }
2740 case REDIRECT_TYPE_LOCATION:
2741 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002742 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002743 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002744 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002745
2746 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002747 memcpy(chunk->area + chunk->data, rule->rdr_str,
2748 rule->rdr_len);
2749 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002750 }
2751 else {
2752 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002753 chunk->data += build_logline(s,
2754 chunk->area + chunk->data,
2755 chunk->size - chunk->data,
2756 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002757
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002758 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002759 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002760 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002761 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002762 break;
2763 }
2764
2765 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002766 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2767 chunk->data += 14;
2768 memcpy(chunk->area + chunk->data, rule->cookie_str,
2769 rule->cookie_len);
2770 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002771 }
2772
Willy Tarreau19b14122017-02-28 09:48:11 +01002773 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002774 txn->status = rule->code;
2775 /* let's log the request time */
2776 s->logs.tv_request = now;
2777
Christopher Fauletbe821b92017-03-30 11:21:53 +02002778 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002779 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2780 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2781 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002782 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002783 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002784 memcpy(chunk->area + chunk->data,
2785 "\r\nProxy-Connection: keep-alive", 30);
2786 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002787 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002788 memcpy(chunk->area + chunk->data,
2789 "\r\nConnection: keep-alive", 24);
2790 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002791 }
2792 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002793 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2794 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002795 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002796 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002797 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002798 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002799 req->next -= req->sov;
2800 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002801 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002802 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002803 req->msg_state = HTTP_MSG_CLOSED;
2804 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002805 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002806 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002807 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002808 /* let the server side turn to SI_ST_CLO */
2809 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002810 } else {
2811 /* keep-alive not possible */
2812 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002813 memcpy(chunk->area + chunk->data,
2814 "\r\nProxy-Connection: close\r\n\r\n", 29);
2815 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002816 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002817 memcpy(chunk->area + chunk->data,
2818 "\r\nConnection: close\r\n\r\n", 23);
2819 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002820 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002821 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002822 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002823 }
2824
Willy Tarreaue7dff022015-04-03 01:14:29 +02002825 if (!(s->flags & SF_ERR_MASK))
2826 s->flags |= SF_ERR_LOCAL;
2827 if (!(s->flags & SF_FINST_MASK))
2828 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002829
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002830 ret = 1;
2831 leave:
2832 free_trash_chunk(chunk);
2833 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002834}
2835
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002836/* This stream analyser runs all HTTP request processing which is common to
2837 * frontends and backends, which means blocking ACLs, filters, connection-close,
2838 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002839 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002840 * either needs more data or wants to immediately abort the request (eg: deny,
2841 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002842 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002843int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002844{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002845 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002846 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002847 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002848 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002849 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002850 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002851 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002852 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002853
Willy Tarreau655dce92009-11-08 13:10:58 +01002854 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002855 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002856 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002857 }
2858
Christopher Faulet45073512018-07-20 10:16:29 +02002859 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 +02002860 now_ms, __FUNCTION__,
2861 s,
2862 req,
2863 req->rex, req->wex,
2864 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002865 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002866 req->analysers);
2867
Willy Tarreau65410832014-04-28 21:25:43 +02002868 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002869 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002870
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002871 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002872 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002873 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002874
Willy Tarreau0b748332014-04-29 00:13:29 +02002875 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002876 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2877 goto return_prx_yield;
2878
Willy Tarreau0b748332014-04-29 00:13:29 +02002879 case HTTP_RULE_RES_CONT:
2880 case HTTP_RULE_RES_STOP: /* nothing to do */
2881 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002882
Willy Tarreau0b748332014-04-29 00:13:29 +02002883 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2884 if (txn->flags & TX_CLTARPIT)
2885 goto tarpit;
2886 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002887
Willy Tarreau0b748332014-04-29 00:13:29 +02002888 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2889 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002890
Willy Tarreau0b748332014-04-29 00:13:29 +02002891 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002892 goto done;
2893
Willy Tarreau0b748332014-04-29 00:13:29 +02002894 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2895 goto return_bad_req;
2896 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002897 }
2898
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002899 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2900 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002901 struct hdr_ctx ctx;
2902
2903 ctx.idx = 0;
2904 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002905 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002906 if (unlikely(http_header_add_tail2(&txn->req,
2907 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002908 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002909 goto return_bad_req;
2910 }
2911 }
2912
2913 }
2914
Willy Tarreau52542592014-04-28 18:33:26 +02002915 /* OK at this stage, we know that the request was accepted according to
2916 * the http-request rules, we can check for the stats. Note that the
2917 * URI is detected *before* the req* rules in order not to be affected
2918 * by a possible reqrep, while they are processed *after* so that a
2919 * reqdeny can still block them. This clearly needs to change in 1.6!
2920 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002921 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002922 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002923 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002924 txn->status = 500;
2925 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002926 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002927
Willy Tarreaue7dff022015-04-03 01:14:29 +02002928 if (!(s->flags & SF_ERR_MASK))
2929 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002930 goto return_prx_cond;
2931 }
2932
2933 /* parse the whole stats request and extract the relevant information */
2934 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002935 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002936 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002937
Willy Tarreau0b748332014-04-29 00:13:29 +02002938 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2939 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002940
Willy Tarreau0b748332014-04-29 00:13:29 +02002941 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2942 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002943 }
2944
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002945 /* evaluate the req* rules except reqadd */
2946 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002947 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002948 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002949
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002950 if (txn->flags & TX_CLDENY)
2951 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002952
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002953 if (txn->flags & TX_CLTARPIT) {
2954 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002955 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002956 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002957 }
Willy Tarreau06619262006-12-17 08:37:22 +01002958
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002959 /* add request headers from the rule sets in the same order */
2960 list_for_each_entry(wl, &px->req_add, list) {
2961 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002962 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002963 ret = acl_pass(ret);
2964 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2965 ret = !ret;
2966 if (!ret)
2967 continue;
2968 }
2969
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002970 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002971 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002972 }
2973
Willy Tarreau52542592014-04-28 18:33:26 +02002974
2975 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002976 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2977 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002978 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002979 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002980 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002981
Willy Tarreaue7dff022015-04-03 01:14:29 +02002982 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2983 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2984 if (!(s->flags & SF_FINST_MASK))
2985 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002986
Willy Tarreau70730dd2014-04-24 18:06:27 +02002987 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002988 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2989 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002990 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002991 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002992 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002993
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002994 /* check whether we have some ACLs set to redirect this request */
2995 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002996 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002997 int ret;
2998
Willy Tarreau192252e2015-04-04 01:47:55 +02002999 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003000 ret = acl_pass(ret);
3001 if (rule->cond->pol == ACL_COND_UNLESS)
3002 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003003 if (!ret)
3004 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003005 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003006 if (!http_apply_redirect_rule(rule, s, txn))
3007 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003008 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003009 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003010
Willy Tarreau2be39392010-01-03 17:24:51 +01003011 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3012 * If this happens, then the data will not come immediately, so we must
3013 * send all what we have without waiting. Note that due to the small gain
3014 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003015 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003016 * itself once used.
3017 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003018 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003019
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003020 done: /* done with this analyser, continue with next ones that the calling
3021 * points will have set, if any.
3022 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003023 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003024 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3025 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003026 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003027
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003028 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003029 /* Allow cookie logging
3030 */
3031 if (s->be->cookie_name || sess->fe->capture_name)
3032 manage_client_side_cookies(s, req);
3033
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003034 /* When a connection is tarpitted, we use the tarpit timeout,
3035 * which may be the same as the connect timeout if unspecified.
3036 * If unset, then set it to zero because we really want it to
3037 * eventually expire. We build the tarpit as an analyser.
3038 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003039 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003040
3041 /* wipe the request out so that we can drop the connection early
3042 * if the client closes first.
3043 */
3044 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003045
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003046 txn->status = http_err_codes[deny_status];
3047
Christopher Faulet0184ea72017-01-05 14:06:34 +01003048 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003049 req->analysers |= AN_REQ_HTTP_TARPIT;
3050 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3051 if (!req->analyse_exp)
3052 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003053 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003054 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003055 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003056 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003057 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003058 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003059 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003060
3061 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003062
3063 /* Allow cookie logging
3064 */
3065 if (s->be->cookie_name || sess->fe->capture_name)
3066 manage_client_side_cookies(s, req);
3067
Willy Tarreau0b748332014-04-29 00:13:29 +02003068 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003069 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003070 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003071 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003072 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003073 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003074 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003075 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003076 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003077 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003078 goto return_prx_cond;
3079
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003080 return_bad_req:
3081 /* We centralize bad requests processing here */
3082 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3083 /* we detected a parsing error. We want to archive this request
3084 * in the dedicated proxy area for later troubleshooting.
3085 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003086 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003087 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003088
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003089 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003090 txn->req.msg_state = HTTP_MSG_ERROR;
3091 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003092 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003093
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003094 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003095 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003096 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003097
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003099 if (!(s->flags & SF_ERR_MASK))
3100 s->flags |= SF_ERR_PRXCOND;
3101 if (!(s->flags & SF_FINST_MASK))
3102 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003103
Christopher Faulet0184ea72017-01-05 14:06:34 +01003104 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003105 req->analyse_exp = TICK_ETERNITY;
3106 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003107
3108 return_prx_yield:
3109 channel_dont_connect(req);
3110 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003111}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003112
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003113/* This function performs all the processing enabled for the current request.
3114 * It returns 1 if the processing can continue on next analysers, or zero if it
3115 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003116 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003117 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003118int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003119{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003120 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003121 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003122 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003123 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003124
Willy Tarreau655dce92009-11-08 13:10:58 +01003125 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003126 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003127 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003128 return 0;
3129 }
3130
Christopher Faulet45073512018-07-20 10:16:29 +02003131 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 +02003132 now_ms, __FUNCTION__,
3133 s,
3134 req,
3135 req->rex, req->wex,
3136 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003137 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003138 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003139
Willy Tarreau59234e92008-11-30 23:51:27 +01003140 /*
3141 * Right now, we know that we have processed the entire headers
3142 * and that unwanted requests have been filtered out. We can do
3143 * whatever we want with the remaining request. Also, now we
3144 * may have separate values for ->fe, ->be.
3145 */
Willy Tarreau06619262006-12-17 08:37:22 +01003146
Willy Tarreau59234e92008-11-30 23:51:27 +01003147 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003148 * If HTTP PROXY is set we simply get remote server address parsing
3149 * incoming request. Note that this requires that a connection is
3150 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003151 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003152 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003153 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003154 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003155
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003156 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003157 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003158 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003159 txn->req.msg_state = HTTP_MSG_ERROR;
3160 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003161 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003162 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003163
Willy Tarreaue7dff022015-04-03 01:14:29 +02003164 if (!(s->flags & SF_ERR_MASK))
3165 s->flags |= SF_ERR_RESOURCE;
3166 if (!(s->flags & SF_FINST_MASK))
3167 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003168
3169 return 0;
3170 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003171
Willy Tarreau6b952c82018-09-10 17:45:34 +02003172 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003173 if (url2sa(ci_head(req) + msg->sl.rq.u,
3174 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003175 &conn->addr.to, NULL) == -1)
3176 goto return_bad_req;
3177
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003178 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003179 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3180 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003181 * u_l characters by a single "/".
3182 */
3183 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003184 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003185 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3186 int delta;
3187
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003188 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003189 http_msg_move_end(&txn->req, delta);
3190 cur_end += delta;
3191 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3192 goto return_bad_req;
3193 }
3194 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003195 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003196 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3197 int delta;
3198
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003199 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003200 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003201 http_msg_move_end(&txn->req, delta);
3202 cur_end += delta;
3203 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3204 goto return_bad_req;
3205 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003206 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003207
Willy Tarreau59234e92008-11-30 23:51:27 +01003208 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003209 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003210 * Note that doing so might move headers in the request, but
3211 * the fields will stay coherent and the URI will not move.
3212 * This should only be performed in the backend.
3213 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003214 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003215 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003216
William Lallemanda73203e2012-03-12 12:48:57 +01003217 /* add unique-id if "header-unique-id" is specified */
3218
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003219 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003220 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003221 goto return_bad_req;
3222 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003223 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003224 }
William Lallemanda73203e2012-03-12 12:48:57 +01003225
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003226 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003227 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003228 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003229 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003230 goto return_bad_req;
3231 }
3232
Cyril Bontéb21570a2009-11-29 20:04:48 +01003233 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003234 * 9: add X-Forwarded-For if either the frontend or the backend
3235 * asks for it.
3236 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003237 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003238 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003239 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3240 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3241 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003242 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003243 /* The header is set to be added only if none is present
3244 * and we found it, so don't do anything.
3245 */
3246 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003247 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003248 /* Add an X-Forwarded-For header unless the source IP is
3249 * in the 'except' network range.
3250 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003251 if ((!sess->fe->except_mask.s_addr ||
3252 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3253 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003254 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003255 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003256 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003257 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003258 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003259 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003260
3261 /* Note: we rely on the backend to get the header name to be used for
3262 * x-forwarded-for, because the header is really meant for the backends.
3263 * However, if the backend did not specify any option, we have to rely
3264 * on the frontend's header name.
3265 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003266 if (s->be->fwdfor_hdr_len) {
3267 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003268 memcpy(trash.area,
3269 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003270 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003271 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003272 memcpy(trash.area,
3273 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003274 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003275 len += snprintf(trash.area + len,
3276 trash.size - len,
3277 ": %d.%d.%d.%d", pn[0], pn[1],
3278 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003279
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003280 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003281 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003282 }
3283 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003284 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003285 /* FIXME: for the sake of completeness, we should also support
3286 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003287 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003288 int len;
3289 char pn[INET6_ADDRSTRLEN];
3290 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003291 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003292 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003293
Willy Tarreau59234e92008-11-30 23:51:27 +01003294 /* Note: we rely on the backend to get the header name to be used for
3295 * x-forwarded-for, because the header is really meant for the backends.
3296 * However, if the backend did not specify any option, we have to rely
3297 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003298 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003299 if (s->be->fwdfor_hdr_len) {
3300 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003301 memcpy(trash.area, s->be->fwdfor_hdr_name,
3302 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003303 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003304 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003305 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3306 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003307 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003308 len += snprintf(trash.area + len, trash.size - len,
3309 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003310
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003311 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003312 goto return_bad_req;
3313 }
3314 }
3315
3316 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003317 * 10: add X-Original-To if either the frontend or the backend
3318 * asks for it.
3319 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003320 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003321
3322 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003323 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003324 /* Add an X-Original-To header unless the destination IP is
3325 * in the 'except' network range.
3326 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003327 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003328
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003329 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003330 ((!sess->fe->except_mask_to.s_addr ||
3331 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3332 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003333 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003334 (((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 +02003335 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003336 int len;
3337 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003338 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003339
3340 /* Note: we rely on the backend to get the header name to be used for
3341 * x-original-to, because the header is really meant for the backends.
3342 * However, if the backend did not specify any option, we have to rely
3343 * on the frontend's header name.
3344 */
3345 if (s->be->orgto_hdr_len) {
3346 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003347 memcpy(trash.area,
3348 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003349 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003350 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003351 memcpy(trash.area,
3352 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003353 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003354 len += snprintf(trash.area + len,
3355 trash.size - len,
3356 ": %d.%d.%d.%d", pn[0], pn[1],
3357 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003358
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003359 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003360 goto return_bad_req;
3361 }
3362 }
3363 }
3364
Willy Tarreau50fc7772012-11-11 22:19:57 +01003365 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3366 * If an "Upgrade" token is found, the header is left untouched in order not to have
3367 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3368 * "Upgrade" is present in the Connection header.
3369 */
3370 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3371 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003372 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003373 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003374 unsigned int want_flags = 0;
3375
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003376 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003377 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003378 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003379 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003380 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003381 want_flags |= TX_CON_CLO_SET;
3382 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003383 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003384 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003385 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003386 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003387 want_flags |= TX_CON_KAL_SET;
3388 }
3389
3390 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003391 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003392 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003393
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003394
Willy Tarreau522d6c02009-12-06 18:49:18 +01003395 /* If we have no server assigned yet and we're balancing on url_param
3396 * with a POST request, we may be interested in checking the body for
3397 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003398 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003399 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003400 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003401 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003402 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003403 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003404 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003405
Christopher Fauletbe821b92017-03-30 11:21:53 +02003406 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3407 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003408#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003409 /* We expect some data from the client. Unless we know for sure
3410 * we already have a full request, we have to re-enable quick-ack
3411 * in case we previously disabled it, otherwise we might cause
3412 * the client to delay further data.
3413 */
3414 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3415 cli_conn && conn_ctrl_ready(cli_conn) &&
3416 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003417 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003418 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003419#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003420
Willy Tarreau59234e92008-11-30 23:51:27 +01003421 /*************************************************************
3422 * OK, that's finished for the headers. We have done what we *
3423 * could. Let's switch to the DATA state. *
3424 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003425 req->analyse_exp = TICK_ETERNITY;
3426 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003427
Willy Tarreau59234e92008-11-30 23:51:27 +01003428 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003429 /* OK let's go on with the BODY now */
3430 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003431
Willy Tarreau59234e92008-11-30 23:51:27 +01003432 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003433 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003434 /* we detected a parsing error. We want to archive this request
3435 * in the dedicated proxy area for later troubleshooting.
3436 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003437 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003438 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003439
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003440 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003441 txn->req.msg_state = HTTP_MSG_ERROR;
3442 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003443 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003444 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003445
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003446 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003447 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003448 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003449
Willy Tarreaue7dff022015-04-03 01:14:29 +02003450 if (!(s->flags & SF_ERR_MASK))
3451 s->flags |= SF_ERR_PRXCOND;
3452 if (!(s->flags & SF_FINST_MASK))
3453 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003454 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003455}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003456
Willy Tarreau60b85b02008-11-30 23:28:40 +01003457/* This function is an analyser which processes the HTTP tarpit. It always
3458 * returns zero, at the beginning because it prevents any other processing
3459 * from occurring, and at the end because it terminates the request.
3460 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003461int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003462{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003463 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003464
3465 /* This connection is being tarpitted. The CLIENT side has
3466 * already set the connect expiration date to the right
3467 * timeout. We just have to check that the client is still
3468 * there and that the timeout has not expired.
3469 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003470 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003471 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003472 !tick_is_expired(req->analyse_exp, now_ms))
3473 return 0;
3474
3475 /* We will set the queue timer to the time spent, just for
3476 * logging purposes. We fake a 500 server error, so that the
3477 * attacker will not suspect his connection has been tarpitted.
3478 * It will not cause trouble to the logs because we can exclude
3479 * the tarpitted connections by filtering on the 'PT' status flags.
3480 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003481 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3482
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003483 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003484 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003485
Christopher Faulet0184ea72017-01-05 14:06:34 +01003486 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003487 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003488
Willy Tarreaue7dff022015-04-03 01:14:29 +02003489 if (!(s->flags & SF_ERR_MASK))
3490 s->flags |= SF_ERR_PRXCOND;
3491 if (!(s->flags & SF_FINST_MASK))
3492 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003493 return 0;
3494}
3495
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003496/* This function is an analyser which waits for the HTTP request body. It waits
3497 * for either the buffer to be full, or the full advertised contents to have
3498 * reached the buffer. It must only be called after the standard HTTP request
3499 * processing has occurred, because it expects the request to be parsed and will
3500 * look for the Expect header. It may send a 100-Continue interim response. It
3501 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3502 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3503 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003504 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003505int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003506{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003507 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003508 struct http_txn *txn = s->txn;
3509 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003510
3511 /* We have to parse the HTTP request body to find any required data.
3512 * "balance url_param check_post" should have been the only way to get
3513 * into this. We were brought here after HTTP header analysis, so all
3514 * related structures are ready.
3515 */
3516
Willy Tarreau890988f2014-04-10 11:59:33 +02003517 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3518 /* This is the first call */
3519 if (msg->msg_state < HTTP_MSG_BODY)
3520 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003521
Willy Tarreau890988f2014-04-10 11:59:33 +02003522 if (msg->msg_state < HTTP_MSG_100_SENT) {
3523 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3524 * send an HTTP/1.1 100 Continue intermediate response.
3525 */
3526 if (msg->flags & HTTP_MSGF_VER_11) {
3527 struct hdr_ctx ctx;
3528 ctx.idx = 0;
3529 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003530 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003531 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003532 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003533 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003534 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003535 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003536 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003537 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003538
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003539 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003540 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003541 * must save the body in msg->next because it survives buffer
3542 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003543 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003544 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003545
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003546 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003547 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3548 else
3549 msg->msg_state = HTTP_MSG_DATA;
3550 }
3551
Willy Tarreau890988f2014-04-10 11:59:33 +02003552 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3553 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003554 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003555 goto missing_data;
3556
3557 /* OK we have everything we need now */
3558 goto http_end;
3559 }
3560
3561 /* OK here we're parsing a chunked-encoded message */
3562
Willy Tarreau522d6c02009-12-06 18:49:18 +01003563 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003564 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003565 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003566 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003567 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003568 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003569 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003570
Willy Tarreau115acb92009-12-26 13:56:06 +01003571 if (!ret)
3572 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003573 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003574 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003575 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003576 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003577 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003578 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003579 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003580
3581 msg->chunk_len = chunk;
3582 msg->body_len += chunk;
3583
3584 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003585 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003586 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003587 }
3588
Willy Tarreaud98cf932009-12-27 22:54:55 +01003589 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003590 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3591 * for at least a whole chunk or the whole content length bytes after
3592 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003593 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003594 if (msg->msg_state == HTTP_MSG_TRAILERS)
3595 goto http_end;
3596
Willy Tarreaue115b492015-05-01 23:05:14 +02003597 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003598 goto http_end;
3599
3600 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003601 /* we get here if we need to wait for more data. If the buffer is full,
3602 * we have the maximum we can expect.
3603 */
Willy Tarreau23752332018-06-15 14:54:53 +02003604 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003605 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003606
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003607 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003608 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003609 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003610
Willy Tarreaue7dff022015-04-03 01:14:29 +02003611 if (!(s->flags & SF_ERR_MASK))
3612 s->flags |= SF_ERR_CLITO;
3613 if (!(s->flags & SF_FINST_MASK))
3614 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003615 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003616 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003617
3618 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003619 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003620 /* Not enough data. We'll re-use the http-request
3621 * timeout here. Ideally, we should set the timeout
3622 * relative to the accept() date. We just set the
3623 * request timeout once at the beginning of the
3624 * request.
3625 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003626 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003627 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003628 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003629 return 0;
3630 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003631
3632 http_end:
3633 /* The situation will not evolve, so let's give up on the analysis. */
3634 s->logs.tv_request = now; /* update the request timer to reflect full request */
3635 req->analysers &= ~an_bit;
3636 req->analyse_exp = TICK_ETERNITY;
3637 return 1;
3638
3639 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003640 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003641 txn->req.msg_state = HTTP_MSG_ERROR;
3642 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003643 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003644
Willy Tarreaue7dff022015-04-03 01:14:29 +02003645 if (!(s->flags & SF_ERR_MASK))
3646 s->flags |= SF_ERR_PRXCOND;
3647 if (!(s->flags & SF_FINST_MASK))
3648 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003649
Willy Tarreau522d6c02009-12-06 18:49:18 +01003650 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003651 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003652 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003653 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003654 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003655 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003656}
3657
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003658/* send a server's name with an outgoing request over an established connection.
3659 * Note: this function is designed to be called once the request has been scheduled
3660 * for being forwarded. This is the reason why it rewinds the buffer before
3661 * proceeding.
3662 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003663int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003664
3665 struct hdr_ctx ctx;
3666
Mark Lamourinec2247f02012-01-04 13:02:01 -05003667 char *hdr_name = be->server_id_hdr_name;
3668 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003669 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003670 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003671 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003672
William Lallemandd9e90662012-01-30 17:27:17 +01003673 ctx.idx = 0;
3674
Willy Tarreau211cdec2014-04-17 20:18:08 +02003675 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003676 if (old_o) {
3677 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003678 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003679 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003680 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003681 }
3682
Willy Tarreauf37954d2018-06-15 18:31:02 +02003683 old_i = ci_data(chn);
3684 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 -05003685 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003686 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003687 }
3688
3689 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003690 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003691 memcpy(hdr_val, hdr_name, hdr_name_len);
3692 hdr_val += hdr_name_len;
3693 *hdr_val++ = ':';
3694 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003695 hdr_val += strlcpy2(hdr_val, srv_name,
3696 trash.area + trash.size - hdr_val);
3697 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3698 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003699
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003700 if (old_o) {
3701 /* If this was a forwarded request, we must readjust the amount of
3702 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003703 * variations. Note that the current state is >= HTTP_MSG_BODY,
3704 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003705 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003706 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003707 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003708 txn->req.next -= old_o;
3709 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003710 }
3711
Mark Lamourinec2247f02012-01-04 13:02:01 -05003712 return 0;
3713}
3714
Willy Tarreau610ecce2010-01-04 21:15:02 +01003715/* Terminate current transaction and prepare a new one. This is very tricky
3716 * right now but it works.
3717 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003718void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003719{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003720 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003721 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003722 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003723 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003724 struct connection *srv_conn;
3725 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003726 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003727
Willy Tarreau610ecce2010-01-04 21:15:02 +01003728 /* FIXME: We need a more portable way of releasing a backend's and a
3729 * server's connections. We need a safer way to reinitialize buffer
3730 * flags. We also need a more accurate method for computing per-request
3731 * data.
3732 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003733 /*
3734 * XXX cognet: This is probably wrong, this is killing a whole
3735 * connection, in the new world order, we probably want to just kill
3736 * the stream, this is to be revisited the day we handle multiple
3737 * streams in one server connection.
3738 */
3739 cs = objt_cs(s->si[1].end);
3740 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003741
Willy Tarreau4213a112013-12-15 10:25:42 +01003742 /* unless we're doing keep-alive, we want to quickly close the connection
3743 * to the server.
3744 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003745 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003746 !si_conn_ready(&s->si[1])) {
3747 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3748 si_shutr(&s->si[1]);
3749 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003750 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003751
Willy Tarreaue7dff022015-04-03 01:14:29 +02003752 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003753 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003754 if (unlikely(s->srv_conn))
3755 sess_change_server(s, NULL);
3756 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003757
3758 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003759 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003760
Willy Tarreaueee5b512015-04-03 23:46:31 +02003761 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003762 int n;
3763
Willy Tarreaueee5b512015-04-03 23:46:31 +02003764 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003765 if (n < 1 || n > 5)
3766 n = 0;
3767
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003768 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003769 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003770 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003771 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003772 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003773 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3774 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003775 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003776 }
3777
3778 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003779 s->logs.bytes_in -= ci_data(&s->req);
3780 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003781
Willy Tarreau66425e32018-07-25 06:55:12 +02003782 /* we may need to know the position in the queue */
3783 pendconn_free(s);
3784
Willy Tarreau610ecce2010-01-04 21:15:02 +01003785 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003786 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003787 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003788 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003789 s->do_log(s);
3790 }
3791
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003792 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003793 stream_stop_content_counters(s);
3794 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003795
Willy Tarreau610ecce2010-01-04 21:15:02 +01003796 s->logs.accept_date = date; /* user-visible date for logging */
3797 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003798 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3799 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003800 tv_zero(&s->logs.tv_request);
3801 s->logs.t_queue = -1;
3802 s->logs.t_connect = -1;
3803 s->logs.t_data = -1;
3804 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003805 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3806 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003807
Willy Tarreauf37954d2018-06-15 18:31:02 +02003808 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3809 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003810
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003811 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003812 if (s->flags & SF_CURR_SESS) {
3813 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003814 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003815 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003816 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003817 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003818 }
3819
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003820 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003821
Willy Tarreau4213a112013-12-15 10:25:42 +01003822 /* only release our endpoint if we don't intend to reuse the
3823 * connection.
3824 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003825 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003826 !si_conn_ready(&s->si[1])) {
3827 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02003828 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003829 }
3830
Willy Tarreau350f4872014-11-28 14:42:25 +01003831 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3832 s->si[1].err_type = SI_ET_NONE;
3833 s->si[1].conn_retries = 0; /* used for logging too */
3834 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003835 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 +01003836 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 +01003837 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 +02003838 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3839 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3840 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003841
Patrick Hemmere3faf022018-08-22 10:02:00 -04003842 hlua_ctx_destroy(s->hlua);
3843 s->hlua = NULL;
3844
Willy Tarreaueee5b512015-04-03 23:46:31 +02003845 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003846 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003847 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003848
3849 if (prev_status == 401 || prev_status == 407) {
3850 /* In HTTP keep-alive mode, if we receive a 401, we still have
3851 * a chance of being able to send the visitor again to the same
3852 * server over the same connection. This is required by some
3853 * broken protocols such as NTLM, and anyway whenever there is
3854 * an opportunity for sending the challenge to the proper place,
3855 * it's better to do it (at least it helps with debugging).
3856 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003857 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02003858 if (srv_conn)
3859 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01003860 }
3861
Willy Tarreau53f96852016-02-02 18:50:47 +01003862 /* Never ever allow to reuse a connection from a non-reuse backend */
3863 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3864 srv_conn->flags |= CO_FL_PRIVATE;
3865
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003866 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003867 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003868
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003869 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003870 s->req.flags |= CF_NEVER_WAIT;
3871 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003872 }
3873
Willy Tarreau714ea782015-11-25 20:11:11 +01003874 /* we're removing the analysers, we MUST re-enable events detection.
3875 * We don't enable close on the response channel since it's either
3876 * already closed, or in keep-alive with an idle connection handler.
3877 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003878 channel_auto_read(&s->req);
3879 channel_auto_close(&s->req);
3880 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003881
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003882 /* we're in keep-alive with an idle connection, monitor it if not already done */
3883 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003884 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02003885 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003886 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01003887 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003888 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02003889 else if (prev_flags & TX_NOT_FIRST)
3890 /* note: we check the request, not the connection, but
3891 * this is valid for strategies SAFE and AGGR, and in
3892 * case of ALWS, we don't care anyway.
3893 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003894 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02003895 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003896 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003897 }
Christopher Faulete6006242017-03-10 11:52:44 +01003898 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3899 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003900}
3901
3902
3903/* This function updates the request state machine according to the response
3904 * state machine and buffer flags. It returns 1 if it changes anything (flag
3905 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3906 * it is only used to find when a request/response couple is complete. Both
3907 * this function and its equivalent should loop until both return zero. It
3908 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3909 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003910int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003911{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003912 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003913 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003914 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003915 unsigned int old_state = txn->req.msg_state;
3916
Christopher Faulet4be98032017-07-18 10:48:24 +02003917 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003918 return 0;
3919
3920 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003921 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003922 * We can shut the read side unless we want to abort_on_close,
3923 * or we have a POST request. The issue with POST requests is
3924 * that some browsers still send a CRLF after the request, and
3925 * this CRLF must be read so that it does not remain in the kernel
3926 * buffers, otherwise a close could cause an RST on some systems
3927 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003928 * Note that if we're using keep-alive on the client side, we'd
3929 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003930 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003931 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003932 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003933 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3934 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003935 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3936 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003937 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003938 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003939
Willy Tarreau40f151a2012-12-20 12:10:09 +01003940 /* if the server closes the connection, we want to immediately react
3941 * and close the socket to save packets and syscalls.
3942 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003943 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003944
Willy Tarreau7f876a12015-11-18 11:59:55 +01003945 /* In any case we've finished parsing the request so we must
3946 * disable Nagle when sending data because 1) we're not going
3947 * to shut this side, and 2) the server is waiting for us to
3948 * send pending data.
3949 */
3950 chn->flags |= CF_NEVER_WAIT;
3951
Willy Tarreau610ecce2010-01-04 21:15:02 +01003952 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3953 goto wait_other_side;
3954
3955 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3956 /* The server has not finished to respond, so we
3957 * don't want to move in order not to upset it.
3958 */
3959 goto wait_other_side;
3960 }
3961
Willy Tarreau610ecce2010-01-04 21:15:02 +01003962 /* When we get here, it means that both the request and the
3963 * response have finished receiving. Depending on the connection
3964 * mode, we'll have to wait for the last bytes to leave in either
3965 * direction, and sometimes for a close to be effective.
3966 */
3967
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003968 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3969 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003970 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3971 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003972 }
3973 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3974 /* Option forceclose is set, or either side wants to close,
3975 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003976 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003977 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003978 *
3979 * However, there is an exception if the response
3980 * length is undefined. In this case, we need to wait
3981 * the close from the server. The response will be
3982 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003983 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003984 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3985 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3986 goto check_channel_flags;
3987
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003988 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3989 channel_shutr_now(chn);
3990 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003991 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003992 }
3993 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003994 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3995 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003996 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003997 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3998 channel_auto_read(chn);
3999 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004000 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004001 }
4002
Christopher Faulet4be98032017-07-18 10:48:24 +02004003 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004004 }
4005
4006 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4007 http_msg_closing:
4008 /* nothing else to forward, just waiting for the output buffer
4009 * to be empty and for the shutw_now to take effect.
4010 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004011 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004012 txn->req.msg_state = HTTP_MSG_CLOSED;
4013 goto http_msg_closed;
4014 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004015 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004016 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004017 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004018 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004019 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004020 }
4021
4022 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4023 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004024 /* if we don't know whether the server will close, we need to hard close */
4025 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4026 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4027
Willy Tarreau3988d932013-12-27 23:03:08 +01004028 /* see above in MSG_DONE why we only do this in these states */
4029 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4030 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004031 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4032 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004033 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004034 goto wait_other_side;
4035 }
4036
Christopher Faulet4be98032017-07-18 10:48:24 +02004037 check_channel_flags:
4038 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4039 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4040 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004041 txn->req.msg_state = HTTP_MSG_CLOSING;
4042 goto http_msg_closing;
4043 }
4044
4045
Willy Tarreau610ecce2010-01-04 21:15:02 +01004046 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004047 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004048}
4049
4050
4051/* This function updates the response state machine according to the request
4052 * state machine and buffer flags. It returns 1 if it changes anything (flag
4053 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4054 * it is only used to find when a request/response couple is complete. Both
4055 * this function and its equivalent should loop until both return zero. It
4056 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4057 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004058int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004059{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004060 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004061 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004062 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004063 unsigned int old_state = txn->rsp.msg_state;
4064
Christopher Faulet4be98032017-07-18 10:48:24 +02004065 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004066 return 0;
4067
4068 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4069 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004070 * still monitor the server connection for a possible close
4071 * while the request is being uploaded, so we don't disable
4072 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004073 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004074 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004075
4076 if (txn->req.msg_state == HTTP_MSG_ERROR)
4077 goto wait_other_side;
4078
4079 if (txn->req.msg_state < HTTP_MSG_DONE) {
4080 /* The client seems to still be sending data, probably
4081 * because we got an error response during an upload.
4082 * We have the choice of either breaking the connection
4083 * or letting it pass through. Let's do the later.
4084 */
4085 goto wait_other_side;
4086 }
4087
Willy Tarreau610ecce2010-01-04 21:15:02 +01004088 /* When we get here, it means that both the request and the
4089 * response have finished receiving. Depending on the connection
4090 * mode, we'll have to wait for the last bytes to leave in either
4091 * direction, and sometimes for a close to be effective.
4092 */
4093
4094 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4095 /* Server-close mode : shut read and wait for the request
4096 * side to close its output buffer. The caller will detect
4097 * when we're in DONE and the other is in CLOSED and will
4098 * catch that for the final cleanup.
4099 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004100 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4101 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004102 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004103 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4104 /* Option forceclose is set, or either side wants to close,
4105 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004106 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004107 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004108 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004109 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004110 channel_shutr_now(chn);
4111 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004112 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004113 }
4114 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004115 /* The last possible modes are keep-alive and tunnel. Tunnel will
4116 * need to forward remaining data. Keep-alive will need to monitor
4117 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004118 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004119 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004120 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004121 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4122 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004123 }
4124
Christopher Faulet4be98032017-07-18 10:48:24 +02004125 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004126 }
4127
4128 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4129 http_msg_closing:
4130 /* nothing else to forward, just waiting for the output buffer
4131 * to be empty and for the shutw_now to take effect.
4132 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004133 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004134 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4135 goto http_msg_closed;
4136 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004137 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004138 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004139 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004140 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004141 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004142 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004143 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004144 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004145 }
4146
4147 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4148 http_msg_closed:
4149 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004150 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004151 channel_auto_close(chn);
4152 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004153 goto wait_other_side;
4154 }
4155
Christopher Faulet4be98032017-07-18 10:48:24 +02004156 check_channel_flags:
4157 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4158 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4159 /* if we've just closed an output, let's switch */
4160 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4161 goto http_msg_closing;
4162 }
4163
Willy Tarreau610ecce2010-01-04 21:15:02 +01004164 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004165 /* We force the response to leave immediately if we're waiting for the
4166 * other side, since there is no pending shutdown to push it out.
4167 */
4168 if (!channel_is_empty(chn))
4169 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004170 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004171}
4172
4173
Christopher Faulet894da4c2017-07-18 11:29:07 +02004174/* Resync the request and response state machines. */
4175void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004176{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004177 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004178#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004179 int old_req_state = txn->req.msg_state;
4180 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004181#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004182
Willy Tarreau610ecce2010-01-04 21:15:02 +01004183 http_sync_req_state(s);
4184 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004185 if (!http_sync_res_state(s))
4186 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004187 if (!http_sync_req_state(s))
4188 break;
4189 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004190
Christopher Faulet894da4c2017-07-18 11:29:07 +02004191 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4192 "req->analysers=0x%08x res->analysers=0x%08x\n",
4193 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004194 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4195 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004196 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004197
4198
Willy Tarreau610ecce2010-01-04 21:15:02 +01004199 /* OK, both state machines agree on a compatible state.
4200 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004201 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4202 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004203 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4204 * means we must abort the request.
4205 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4206 * corresponding channel.
4207 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4208 * on the response with server-close mode means we've completed one
4209 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004210 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004211 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4212 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004213 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004214 channel_auto_close(&s->req);
4215 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004216 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004217 channel_auto_close(&s->res);
4218 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004219 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004220 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4221 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004222 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004223 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004224 channel_auto_close(&s->res);
4225 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004226 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004227 channel_abort(&s->req);
4228 channel_auto_close(&s->req);
4229 channel_auto_read(&s->req);
4230 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004231 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004232 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4233 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4234 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4235 s->req.analysers &= AN_REQ_FLT_END;
4236 if (HAS_REQ_DATA_FILTERS(s))
4237 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4238 }
4239 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4240 s->res.analysers &= AN_RES_FLT_END;
4241 if (HAS_RSP_DATA_FILTERS(s))
4242 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4243 }
4244 channel_auto_close(&s->req);
4245 channel_auto_read(&s->req);
4246 channel_auto_close(&s->res);
4247 channel_auto_read(&s->res);
4248 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004249 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4250 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004251 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004252 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4253 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4254 /* server-close/keep-alive: terminate this transaction,
4255 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004256 * a fresh-new transaction, but only once we're sure there's
4257 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004258 * another request. They must not hold any pending output data
4259 * and the response buffer must realigned
4260 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004261 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004262 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004263 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004264 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004265 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004266 else {
4267 s->req.analysers = AN_REQ_FLT_END;
4268 s->res.analysers = AN_RES_FLT_END;
4269 txn->flags |= TX_WAIT_CLEANUP;
4270 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004271 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004272}
4273
Willy Tarreaud98cf932009-12-27 22:54:55 +01004274/* This function is an analyser which forwards request body (including chunk
4275 * sizes if any). It is called as soon as we must forward, even if we forward
4276 * zero byte. The only situation where it must not be called is when we're in
4277 * tunnel mode and we want to forward till the close. It's used both to forward
4278 * remaining data and to resync after end of body. It expects the msg_state to
4279 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004280 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004281 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004282 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004283 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004284int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004285{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004286 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004287 struct http_txn *txn = s->txn;
4288 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004289 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004290
Christopher Faulet45073512018-07-20 10:16:29 +02004291 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 +02004292 now_ms, __FUNCTION__,
4293 s,
4294 req,
4295 req->rex, req->wex,
4296 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004297 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004298 req->analysers);
4299
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004300 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4301 return 0;
4302
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004303 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004304 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004305 /* Output closed while we were sending data. We must abort and
4306 * wake the other side up.
4307 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004308 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004309 msg->msg_state = HTTP_MSG_ERROR;
4310 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004311 return 1;
4312 }
4313
Willy Tarreaud98cf932009-12-27 22:54:55 +01004314 /* Note that we don't have to send 100-continue back because we don't
4315 * need the data to complete our job, and it's up to the server to
4316 * decide whether to return 100, 417 or anything else in return of
4317 * an "Expect: 100-continue" header.
4318 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004319 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004320 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4321 ? HTTP_MSG_CHUNK_SIZE
4322 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004323
4324 /* TODO/filters: when http-buffer-request option is set or if a
4325 * rule on url_param exists, the first chunk size could be
4326 * already parsed. In that case, msg->next is after the chunk
4327 * size (including the CRLF after the size). So this case should
4328 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004329 }
4330
Willy Tarreau7ba23542014-04-17 21:50:00 +02004331 /* Some post-connect processing might want us to refrain from starting to
4332 * forward data. Currently, the only reason for this is "balance url_param"
4333 * whichs need to parse/process the request after we've enabled forwarding.
4334 */
4335 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004336 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004337 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004338 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004339 channel_dont_close(req); /* don't fail on early shutr */
4340 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004341 }
4342 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4343 }
4344
Willy Tarreau80a92c02014-03-12 10:41:13 +01004345 /* in most states, we should abort in case of early close */
4346 channel_auto_close(req);
4347
Willy Tarreauefdf0942014-04-24 20:08:57 +02004348 if (req->to_forward) {
4349 /* We can't process the buffer's contents yet */
4350 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004351 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004352 }
4353
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004354 if (msg->msg_state < HTTP_MSG_DONE) {
4355 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4356 ? http_msg_forward_chunked_body(s, msg)
4357 : http_msg_forward_body(s, msg));
4358 if (!ret)
4359 goto missing_data_or_waiting;
4360 if (ret < 0)
4361 goto return_bad_req;
4362 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004363
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004364 /* other states, DONE...TUNNEL */
4365 /* we don't want to forward closes on DONE except in tunnel mode. */
4366 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4367 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004368
Christopher Faulet894da4c2017-07-18 11:29:07 +02004369 http_resync_states(s);
4370 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004371 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4372 if (req->flags & CF_SHUTW) {
4373 /* request errors are most likely due to the
4374 * server aborting the transfer. */
4375 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004376 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004377 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004378 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004379 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004380 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004381 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004382 }
4383
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004384 /* If "option abortonclose" is set on the backend, we want to monitor
4385 * the client's connection and forward any shutdown notification to the
4386 * server, which will decide whether to close or to go on processing the
4387 * request. We only do that in tunnel mode, and not in other modes since
4388 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004389 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004390 channel_auto_read(req);
4391 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4392 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4393 s->si[1].flags |= SI_FL_NOLINGER;
4394 channel_auto_close(req);
4395 }
4396 else if (s->txn->meth == HTTP_METH_POST) {
4397 /* POST requests may require to read extra CRLF sent by broken
4398 * browsers and which could cause an RST to be sent upon close
4399 * on some systems (eg: Linux). */
4400 channel_auto_read(req);
4401 }
4402 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004403
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004404 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004405 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004406 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004407 if (!(s->flags & SF_ERR_MASK))
4408 s->flags |= SF_ERR_CLICL;
4409 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004410 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004411 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004412 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004413 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004414 }
4415
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004416 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4417 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004418 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004419 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004420
4421 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004422 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004423
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004424 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004425 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004426 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004427 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004428
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004429 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004430 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004431 * And when content-length is used, we never want to let the possible
4432 * shutdown be forwarded to the other side, as the state machine will
4433 * take care of it once the client responds. It's also important to
4434 * prevent TIME_WAITs from accumulating on the backend side, and for
4435 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004436 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004437 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004438 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004439
Willy Tarreau5c620922011-05-11 19:56:11 +02004440 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004441 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004442 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004443 * modes are already handled by the stream sock layer. We must not do
4444 * this in content-length mode because it could present the MSG_MORE
4445 * flag with the last block of forwarded data, which would cause an
4446 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004447 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004448 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004449 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004450
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004451 return 0;
4452
Willy Tarreaud98cf932009-12-27 22:54:55 +01004453 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004454 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004455 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004456 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004457
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004458 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004459 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004460 txn->req.msg_state = HTTP_MSG_ERROR;
4461 if (txn->status) {
4462 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004463 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004464 } else {
4465 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004466 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004467 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004468 req->analysers &= AN_REQ_FLT_END;
4469 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 +01004470
Willy Tarreaue7dff022015-04-03 01:14:29 +02004471 if (!(s->flags & SF_ERR_MASK))
4472 s->flags |= SF_ERR_PRXCOND;
4473 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004474 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004475 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004476 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004477 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004478 }
4479 return 0;
4480
4481 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004482 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004483 txn->req.msg_state = HTTP_MSG_ERROR;
4484 if (txn->status) {
4485 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004486 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004487 } else {
4488 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004489 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004490 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004491 req->analysers &= AN_REQ_FLT_END;
4492 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 +01004493
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004494 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4495 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004496 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004497 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004498
Willy Tarreaue7dff022015-04-03 01:14:29 +02004499 if (!(s->flags & SF_ERR_MASK))
4500 s->flags |= SF_ERR_SRVCL;
4501 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004502 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004503 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004504 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004505 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004506 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004507 return 0;
4508}
4509
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004510/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4511 * processing can continue on next analysers, or zero if it either needs more
4512 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004513 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004514 * when it has nothing left to do, and may remove any analyser when it wants to
4515 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004516 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004517int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004518{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004519 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004520 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004521 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004522 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004523 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004524 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004525 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004526
Christopher Faulet45073512018-07-20 10:16:29 +02004527 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 +02004528 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004529 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004530 rep,
4531 rep->rex, rep->wex,
4532 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004533 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004534 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004535
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004536 /*
4537 * Now parse the partial (or complete) lines.
4538 * We will check the response syntax, and also join multi-line
4539 * headers. An index of all the lines will be elaborated while
4540 * parsing.
4541 *
4542 * For the parsing, we use a 28 states FSM.
4543 *
4544 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004545 * ci_head(rep) = beginning of response
4546 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4547 * ci_tail(rep) = end of input data
4548 * msg->eol = end of current header or line (LF or CRLF)
4549 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004550 */
4551
Willy Tarreau628c40c2014-04-24 19:11:26 +02004552 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004553 /* There's a protected area at the end of the buffer for rewriting
4554 * purposes. We don't want to start to parse the request if the
4555 * protected area is affected, because we may have to move processed
4556 * data later, which is much more complicated.
4557 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004558 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004559 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004560 /* some data has still not left the buffer, wake us once that's done */
4561 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4562 goto abort_response;
4563 channel_dont_close(rep);
4564 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004565 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004566 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004567 }
4568
Willy Tarreau188e2302018-06-15 11:11:53 +02004569 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004570 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004571 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004572
Willy Tarreauf37954d2018-06-15 18:31:02 +02004573 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004574 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004575 }
4576
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004577 /* 1: we might have to print this header in debug mode */
4578 if (unlikely((global.mode & MODE_DEBUG) &&
4579 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004580 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004581 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004582
Willy Tarreauf37954d2018-06-15 18:31:02 +02004583 sol = ci_head(rep);
4584 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004585 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004586
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004587 sol += hdr_idx_first_pos(&txn->hdr_idx);
4588 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004589
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004590 while (cur_idx) {
4591 eol = sol + txn->hdr_idx.v[cur_idx].len;
4592 debug_hdr("srvhdr", s, sol, eol);
4593 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4594 cur_idx = txn->hdr_idx.v[cur_idx].next;
4595 }
4596 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004597
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004598 /*
4599 * Now we quickly check if we have found a full valid response.
4600 * If not so, we check the FD and buffer states before leaving.
4601 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004602 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004603 * responses are checked first.
4604 *
4605 * Depending on whether the client is still there or not, we
4606 * may send an error response back or not. Note that normally
4607 * we should only check for HTTP status there, and check I/O
4608 * errors somewhere else.
4609 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004610
Willy Tarreau655dce92009-11-08 13:10:58 +01004611 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004612 /* Invalid response */
4613 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4614 /* we detected a parsing error. We want to archive this response
4615 * in the dedicated proxy area for later troubleshooting.
4616 */
4617 hdr_response_bad:
4618 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004619 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004620
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004621 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004622 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004623 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004624 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004625 }
Willy Tarreau64648412010-03-05 10:41:54 +01004626 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004627 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004628 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004630 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004631 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004632 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004633
Willy Tarreaue7dff022015-04-03 01:14:29 +02004634 if (!(s->flags & SF_ERR_MASK))
4635 s->flags |= SF_ERR_PRXCOND;
4636 if (!(s->flags & SF_FINST_MASK))
4637 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004638
4639 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004640 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004641
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004642 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004643 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004644 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004645 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646 goto hdr_response_bad;
4647 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004648
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004649 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004650 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004651 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004652 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004653 else if (txn->flags & TX_NOT_FIRST)
4654 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004655
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004656 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004657 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004658 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004659 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004660 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004661
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004662 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004663 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004664 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004665
4666 /* Check to see if the server refused the early data.
4667 * If so, just send a 425
4668 */
4669 if (objt_cs(s->si[1].end)) {
4670 struct connection *conn = objt_cs(s->si[1].end)->conn;
4671
4672 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4673 txn->status = 425;
4674 }
4675
Willy Tarreau350f4872014-11-28 14:42:25 +01004676 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004677 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004678 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004679
Willy Tarreaue7dff022015-04-03 01:14:29 +02004680 if (!(s->flags & SF_ERR_MASK))
4681 s->flags |= SF_ERR_SRVCL;
4682 if (!(s->flags & SF_FINST_MASK))
4683 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004684 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004685 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004686
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004687 /* read timeout : return a 504 to the client. */
4688 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004689 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004690 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004691
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004692 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004693 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004694 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004695 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004696 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004697
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004698 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004699 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004700 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004701 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004702 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004703 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004704
Willy Tarreaue7dff022015-04-03 01:14:29 +02004705 if (!(s->flags & SF_ERR_MASK))
4706 s->flags |= SF_ERR_SRVTO;
4707 if (!(s->flags & SF_FINST_MASK))
4708 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004709 return 0;
4710 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004711
Willy Tarreauf003d372012-11-26 13:35:37 +01004712 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004713 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004714 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4715 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004716 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004717 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004718
Christopher Faulet0184ea72017-01-05 14:06:34 +01004719 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004720 channel_auto_close(rep);
4721
4722 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004723 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004724 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004725
Willy Tarreaue7dff022015-04-03 01:14:29 +02004726 if (!(s->flags & SF_ERR_MASK))
4727 s->flags |= SF_ERR_CLICL;
4728 if (!(s->flags & SF_FINST_MASK))
4729 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004730
Willy Tarreau87b09662015-04-03 00:22:06 +02004731 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004732 return 0;
4733 }
4734
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004735 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004736 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004737 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004738 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004739 else if (txn->flags & TX_NOT_FIRST)
4740 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004741
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004742 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004743 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004744 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004745 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004746 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004747
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004748 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004749 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004750 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004751 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004752 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004753 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004754
Willy Tarreaue7dff022015-04-03 01:14:29 +02004755 if (!(s->flags & SF_ERR_MASK))
4756 s->flags |= SF_ERR_SRVCL;
4757 if (!(s->flags & SF_FINST_MASK))
4758 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004759 return 0;
4760 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004761
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004762 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004763 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004764 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004765 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004766 else if (txn->flags & TX_NOT_FIRST)
4767 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004768
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004769 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004770 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004771 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004772
Willy Tarreaue7dff022015-04-03 01:14:29 +02004773 if (!(s->flags & SF_ERR_MASK))
4774 s->flags |= SF_ERR_CLICL;
4775 if (!(s->flags & SF_FINST_MASK))
4776 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004777
Willy Tarreau87b09662015-04-03 00:22:06 +02004778 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004779 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004780 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004781
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004782 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004783 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004784 return 0;
4785 }
4786
4787 /* More interesting part now : we know that we have a complete
4788 * response which at least looks like HTTP. We have an indicator
4789 * of each header's length, so we can parse them quickly.
4790 */
4791
4792 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004793 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004794
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004795 /*
4796 * 1: get the status code
4797 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004798 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004799 if (n < 1 || n > 5)
4800 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004801 /* when the client triggers a 4xx from the server, it's most often due
4802 * to a missing object or permission. These events should be tracked
4803 * because if they happen often, it may indicate a brute force or a
4804 * vulnerability scan.
4805 */
4806 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004807 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004808
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004809 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004810 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004811
Willy Tarreau91852eb2015-05-01 13:26:00 +02004812 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4813 * exactly one digit "." one digit. This check may be disabled using
4814 * option accept-invalid-http-response.
4815 */
4816 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4817 if (msg->sl.st.v_l != 8) {
4818 msg->err_pos = 0;
4819 goto hdr_response_bad;
4820 }
4821
Willy Tarreauf37954d2018-06-15 18:31:02 +02004822 if (ci_head(rep)[4] != '/' ||
4823 !isdigit((unsigned char)ci_head(rep)[5]) ||
4824 ci_head(rep)[6] != '.' ||
4825 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004826 msg->err_pos = 4;
4827 goto hdr_response_bad;
4828 }
4829 }
4830
Willy Tarreau5b154472009-12-21 20:11:07 +01004831 /* check if the response is HTTP/1.1 or above */
4832 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004833 ((ci_head(rep)[5] > '1') ||
4834 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004835 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004836
4837 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004838 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 +01004839
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004840 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004841 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004842
Willy Tarreauf37954d2018-06-15 18:31:02 +02004843 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004844
Willy Tarreau39650402010-03-15 19:44:39 +01004845 /* Adjust server's health based on status code. Note: status codes 501
4846 * and 505 are triggered on demand by client request, so we must not
4847 * count them as server failures.
4848 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004849 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004850 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004851 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004852 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004853 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004854 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004855
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004856 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004857 * We may be facing a 100-continue response, or any other informational
4858 * 1xx response which is non-final, in which case this is not the right
4859 * response, and we're waiting for the next one. Let's allow this response
4860 * to go to the client and wait for the next one. There's an exception for
4861 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004862 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004863 if (txn->status < 200 &&
4864 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004865 hdr_idx_init(&txn->hdr_idx);
4866 msg->next -= channel_forward(rep, msg->next);
4867 msg->msg_state = HTTP_MSG_RPBEFORE;
4868 txn->status = 0;
4869 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004870 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004871 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004872 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004873
Willy Tarreaua14ad722017-07-07 11:36:32 +02004874 /*
4875 * 2: check for cacheability.
4876 */
4877
4878 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004879 case 200:
4880 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004881 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004882 case 206:
4883 case 300:
4884 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004885 case 404:
4886 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004887 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004888 case 414:
4889 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004890 break;
4891 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004892 /* RFC7231#6.1:
4893 * Responses with status codes that are defined as
4894 * cacheable by default (e.g., 200, 203, 204, 206,
4895 * 300, 301, 404, 405, 410, 414, and 501 in this
4896 * specification) can be reused by a cache with
4897 * heuristic expiration unless otherwise indicated
4898 * by the method definition or explicit cache
4899 * controls [RFC7234]; all other status codes are
4900 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004901 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004902 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004903 break;
4904 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004905
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004906 /*
4907 * 3: we may need to capture headers
4908 */
4909 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004910 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02004911 capture_headers(ci_head(rep), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004912 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004913
Willy Tarreau557f1992015-05-01 10:05:17 +02004914 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4915 * by RFC7230#3.3.3 :
4916 *
4917 * The length of a message body is determined by one of the following
4918 * (in order of precedence):
4919 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004920 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4921 * the connection will become a tunnel immediately after the empty
4922 * line that concludes the header fields. A client MUST ignore
4923 * any Content-Length or Transfer-Encoding header fields received
4924 * in such a message. Any 101 response (Switching Protocols) is
4925 * managed in the same manner.
4926 *
4927 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004928 * (Informational), 204 (No Content), or 304 (Not Modified) status
4929 * code is always terminated by the first empty line after the
4930 * header fields, regardless of the header fields present in the
4931 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004932 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004933 * 3. If a Transfer-Encoding header field is present and the chunked
4934 * transfer coding (Section 4.1) is the final encoding, the message
4935 * body length is determined by reading and decoding the chunked
4936 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004937 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004938 * If a Transfer-Encoding header field is present in a response and
4939 * the chunked transfer coding is not the final encoding, the
4940 * message body length is determined by reading the connection until
4941 * it is closed by the server. If a Transfer-Encoding header field
4942 * is present in a request and the chunked transfer coding is not
4943 * the final encoding, the message body length cannot be determined
4944 * reliably; the server MUST respond with the 400 (Bad Request)
4945 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004946 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004947 * If a message is received with both a Transfer-Encoding and a
4948 * Content-Length header field, the Transfer-Encoding overrides the
4949 * Content-Length. Such a message might indicate an attempt to
4950 * perform request smuggling (Section 9.5) or response splitting
4951 * (Section 9.4) and ought to be handled as an error. A sender MUST
4952 * remove the received Content-Length field prior to forwarding such
4953 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004954 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004955 * 4. If a message is received without Transfer-Encoding and with
4956 * either multiple Content-Length header fields having differing
4957 * field-values or a single Content-Length header field having an
4958 * invalid value, then the message framing is invalid and the
4959 * recipient MUST treat it as an unrecoverable error. If this is a
4960 * request message, the server MUST respond with a 400 (Bad Request)
4961 * status code and then close the connection. If this is a response
4962 * message received by a proxy, the proxy MUST close the connection
4963 * to the server, discard the received response, and send a 502 (Bad
4964 * Gateway) response to the client. If this is a response message
4965 * received by a user agent, the user agent MUST close the
4966 * connection to the server and discard the received response.
4967 *
4968 * 5. If a valid Content-Length header field is present without
4969 * Transfer-Encoding, its decimal value defines the expected message
4970 * body length in octets. If the sender closes the connection or
4971 * the recipient times out before the indicated number of octets are
4972 * received, the recipient MUST consider the message to be
4973 * incomplete and close the connection.
4974 *
4975 * 6. If this is a request message and none of the above are true, then
4976 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004977 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004978 * 7. Otherwise, this is a response message without a declared message
4979 * body length, so the message body length is determined by the
4980 * number of octets received prior to the server closing the
4981 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004982 */
4983
4984 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004985 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004986 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004987 * FIXME: should we parse anyway and return an error on chunked encoding ?
4988 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004989 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4990 txn->status == 101)) {
4991 /* Either we've established an explicit tunnel, or we're
4992 * switching the protocol. In both cases, we're very unlikely
4993 * to understand the next protocols. We have to switch to tunnel
4994 * mode, so that we transfer the request and responses then let
4995 * this protocol pass unmodified. When we later implement specific
4996 * parsers for such protocols, we'll want to check the Upgrade
4997 * header which contains information about that protocol for
4998 * responses with status 101 (eg: see RFC2817 about TLS).
4999 */
5000 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5001 msg->flags |= HTTP_MSGF_XFER_LEN;
5002 goto end;
5003 }
5004
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005005 if (txn->meth == HTTP_METH_HEAD ||
5006 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005007 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005008 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005009 goto skip_content_length;
5010 }
5011
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005012 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005013 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005014 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005015 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005016 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5017 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005018 /* bad transfer-encoding (chunked followed by something else) */
5019 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005020 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005021 break;
5022 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005023 }
5024
Willy Tarreau1c913912015-04-30 10:57:51 +02005025 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005026 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005027 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005028 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005029 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5030 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005031 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005032 signed long long cl;
5033
Willy Tarreauad14f752011-09-02 20:33:27 +02005034 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005035 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005036 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005037 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005038
Willy Tarreauad14f752011-09-02 20:33:27 +02005039 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005040 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005041 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005042 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005043
Willy Tarreauad14f752011-09-02 20:33:27 +02005044 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005045 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005046 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005047 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005048
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005049 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005050 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005051 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005052 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005053
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005054 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005055 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005056 }
5057
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005058 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005059 /* Now we have to check if we need to modify the Connection header.
5060 * This is more difficult on the response than it is on the request,
5061 * because we can have two different HTTP versions and we don't know
5062 * how the client will interprete a response. For instance, let's say
5063 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5064 * HTTP/1.1 response without any header. Maybe it will bound itself to
5065 * HTTP/1.0 because it only knows about it, and will consider the lack
5066 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5067 * the lack of header as a keep-alive. Thus we will use two flags
5068 * indicating how a request MAY be understood by the client. In case
5069 * of multiple possibilities, we'll fix the header to be explicit. If
5070 * ambiguous cases such as both close and keepalive are seen, then we
5071 * will fall back to explicit close. Note that we won't take risks with
5072 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005073 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005074 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005075 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5076 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5077 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5078 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005079 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005080
Willy Tarreau70dffda2014-01-30 03:07:23 +01005081 /* this situation happens when combining pretend-keepalive with httpclose. */
5082 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005083 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005084 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005085 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5086
Willy Tarreau60466522010-01-18 19:08:45 +01005087 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005088 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005089 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5090 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005091
Willy Tarreau60466522010-01-18 19:08:45 +01005092 /* now adjust header transformations depending on current state */
5093 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5094 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5095 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005096 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005097 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005098 }
Willy Tarreau60466522010-01-18 19:08:45 +01005099 else { /* SCL / KAL */
5100 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005101 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005102 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005103 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005104
Willy Tarreau60466522010-01-18 19:08:45 +01005105 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005106 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005107
Willy Tarreau60466522010-01-18 19:08:45 +01005108 /* Some keep-alive responses are converted to Server-close if
5109 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005110 */
Willy Tarreau60466522010-01-18 19:08:45 +01005111 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5112 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005113 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005114 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005115 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005116 }
5117
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005118 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005119 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005120 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005121
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005122 /* end of job, return OK */
5123 rep->analysers &= ~an_bit;
5124 rep->analyse_exp = TICK_ETERNITY;
5125 channel_auto_close(rep);
5126 return 1;
5127
5128 abort_keep_alive:
5129 /* A keep-alive request to the server failed on a network error.
5130 * The client is required to retry. We need to close without returning
5131 * any other information so that the client retries.
5132 */
5133 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005134 rep->analysers &= AN_RES_FLT_END;
5135 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005136 channel_auto_close(rep);
5137 s->logs.logwait = 0;
5138 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005139 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005140 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005141 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005142 return 0;
5143}
5144
5145/* This function performs all the processing enabled for the current response.
5146 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005147 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005148 * other functions. It works like process_request (see indications above).
5149 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005150int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005151{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005152 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005153 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005154 struct http_msg *msg = &txn->rsp;
5155 struct proxy *cur_proxy;
5156 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005157 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005158
Christopher Faulet45073512018-07-20 10:16:29 +02005159 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 +02005160 now_ms, __FUNCTION__,
5161 s,
5162 rep,
5163 rep->rex, rep->wex,
5164 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005165 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005166 rep->analysers);
5167
5168 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5169 return 0;
5170
Willy Tarreau70730dd2014-04-24 18:06:27 +02005171 /* The stats applet needs to adjust the Connection header but we don't
5172 * apply any filter there.
5173 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005174 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5175 rep->analysers &= ~an_bit;
5176 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005177 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005178 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005179
Willy Tarreau58975672014-04-24 21:13:57 +02005180 /*
5181 * We will have to evaluate the filters.
5182 * As opposed to version 1.2, now they will be evaluated in the
5183 * filters order and not in the header order. This means that
5184 * each filter has to be validated among all headers.
5185 *
5186 * Filters are tried with ->be first, then with ->fe if it is
5187 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005188 *
5189 * Maybe we are in resume condiion. In this case I choose the
5190 * "struct proxy" which contains the rule list matching the resume
5191 * pointer. If none of theses "struct proxy" match, I initialise
5192 * the process with the first one.
5193 *
5194 * In fact, I check only correspondance betwwen the current list
5195 * pointer and the ->fe rule list. If it doesn't match, I initialize
5196 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005197 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005198 if (s->current_rule_list == &sess->fe->http_res_rules)
5199 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005200 else
5201 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005202 while (1) {
5203 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005204
Willy Tarreau58975672014-04-24 21:13:57 +02005205 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005206 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005207 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005208
Willy Tarreau51d861a2015-05-22 17:30:48 +02005209 if (ret == HTTP_RULE_RES_BADREQ)
5210 goto return_srv_prx_502;
5211
5212 if (ret == HTTP_RULE_RES_DONE) {
5213 rep->analysers &= ~an_bit;
5214 rep->analyse_exp = TICK_ETERNITY;
5215 return 1;
5216 }
5217 }
5218
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005219 /* we need to be called again. */
5220 if (ret == HTTP_RULE_RES_YIELD) {
5221 channel_dont_close(rep);
5222 return 0;
5223 }
5224
Willy Tarreau58975672014-04-24 21:13:57 +02005225 /* try headers filters */
5226 if (rule_set->rsp_exp != NULL) {
5227 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5228 return_bad_resp:
5229 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005230 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005231 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005232 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005233 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005234 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005235 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005236 txn->status = 502;
5237 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005238 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005239 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005240 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005241 if (!(s->flags & SF_ERR_MASK))
5242 s->flags |= SF_ERR_PRXCOND;
5243 if (!(s->flags & SF_FINST_MASK))
5244 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005245 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005246 }
Willy Tarreau58975672014-04-24 21:13:57 +02005247 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005248
Willy Tarreau58975672014-04-24 21:13:57 +02005249 /* has the response been denied ? */
5250 if (txn->flags & TX_SVDENY) {
5251 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005252 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005253
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005254 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5255 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005256 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005257 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005258
Willy Tarreau58975672014-04-24 21:13:57 +02005259 goto return_srv_prx_502;
5260 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005261
Willy Tarreau58975672014-04-24 21:13:57 +02005262 /* add response headers from the rule sets in the same order */
5263 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005264 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005265 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005266 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005267 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005268 ret = acl_pass(ret);
5269 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5270 ret = !ret;
5271 if (!ret)
5272 continue;
5273 }
5274 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5275 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005276 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005277
Willy Tarreau58975672014-04-24 21:13:57 +02005278 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005279 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005280 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005281 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005282 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005283
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005284 /* After this point, this anayzer can't return yield, so we can
5285 * remove the bit corresponding to this analyzer from the list.
5286 *
5287 * Note that the intermediate returns and goto found previously
5288 * reset the analyzers.
5289 */
5290 rep->analysers &= ~an_bit;
5291 rep->analyse_exp = TICK_ETERNITY;
5292
Willy Tarreau58975672014-04-24 21:13:57 +02005293 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005294 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005295 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005296
Willy Tarreau58975672014-04-24 21:13:57 +02005297 /*
5298 * Now check for a server cookie.
5299 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005300 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005301 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005302
Willy Tarreau58975672014-04-24 21:13:57 +02005303 /*
5304 * Check for cache-control or pragma headers if required.
5305 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005306 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005307 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005308
Willy Tarreau58975672014-04-24 21:13:57 +02005309 /*
5310 * Add server cookie in the response if needed
5311 */
5312 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5313 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005314 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005315 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5316 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5317 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5318 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5319 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005320 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005321 /* the server is known, it's not the one the client requested, or the
5322 * cookie's last seen date needs to be refreshed. We have to
5323 * insert a set-cookie here, except if we want to insert only on POST
5324 * requests and this one isn't. Note that servers which don't have cookies
5325 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005326 */
Willy Tarreau58975672014-04-24 21:13:57 +02005327 if (!objt_server(s->target)->cookie) {
5328 chunk_printf(&trash,
5329 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5330 s->be->cookie_name);
5331 }
5332 else {
5333 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005334
Willy Tarreau58975672014-04-24 21:13:57 +02005335 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5336 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005337 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5338 s30tob64((date.tv_sec+3) >> 2,
5339 trash.area + trash.data);
5340 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005341
Willy Tarreau58975672014-04-24 21:13:57 +02005342 if (s->be->cookie_maxlife) {
5343 /* emit first_date, which is either the original one or
5344 * the current date.
5345 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005346 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005347 s30tob64(txn->cookie_first_date ?
5348 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005349 (date.tv_sec+3) >> 2,
5350 trash.area + trash.data);
5351 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005352 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005353 }
Willy Tarreau58975672014-04-24 21:13:57 +02005354 chunk_appendf(&trash, "; path=/");
5355 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005356
Willy Tarreau58975672014-04-24 21:13:57 +02005357 if (s->be->cookie_domain)
5358 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005359
Willy Tarreau58975672014-04-24 21:13:57 +02005360 if (s->be->ck_opts & PR_CK_HTTPONLY)
5361 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005362
Willy Tarreau58975672014-04-24 21:13:57 +02005363 if (s->be->ck_opts & PR_CK_SECURE)
5364 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005365
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005366 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005367 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005368
Willy Tarreau58975672014-04-24 21:13:57 +02005369 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005370 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005371 /* the server did not change, only the date was updated */
5372 txn->flags |= TX_SCK_UPDATED;
5373 else
5374 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005375
Willy Tarreau58975672014-04-24 21:13:57 +02005376 /* Here, we will tell an eventual cache on the client side that we don't
5377 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5378 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5379 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005380 */
Willy Tarreau58975672014-04-24 21:13:57 +02005381 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005382
Willy Tarreau58975672014-04-24 21:13:57 +02005383 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005384
Willy Tarreau58975672014-04-24 21:13:57 +02005385 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5386 "Cache-control: private", 22) < 0))
5387 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005388 }
Willy Tarreau58975672014-04-24 21:13:57 +02005389 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005390
Willy Tarreau58975672014-04-24 21:13:57 +02005391 /*
5392 * Check if result will be cacheable with a cookie.
5393 * We'll block the response if security checks have caught
5394 * nasty things such as a cacheable cookie.
5395 */
5396 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5397 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5398 (s->be->options & PR_O_CHK_CACHE)) {
5399 /* we're in presence of a cacheable response containing
5400 * a set-cookie header. We'll block it as requested by
5401 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005402 */
Willy Tarreau58975672014-04-24 21:13:57 +02005403 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005404 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005405
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005406 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5407 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005408 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005409 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005410
Christopher Faulet767a84b2017-11-24 16:50:31 +01005411 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5412 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005413 send_log(s->be, LOG_ALERT,
5414 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5415 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5416 goto return_srv_prx_502;
5417 }
Willy Tarreau03945942009-12-22 16:50:27 +01005418
Willy Tarreau70730dd2014-04-24 18:06:27 +02005419 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005420 /*
5421 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5422 * If an "Upgrade" token is found, the header is left untouched in order
5423 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005424 * if anything but "Upgrade" is present in the Connection header. We don't
5425 * want to touch any 101 response either since it's switching to another
5426 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005427 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005428 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005429 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005430 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005431 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5432 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005433
Willy Tarreau58975672014-04-24 21:13:57 +02005434 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5435 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5436 /* we want a keep-alive response here. Keep-alive header
5437 * required if either side is not 1.1.
5438 */
5439 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5440 want_flags |= TX_CON_KAL_SET;
5441 }
5442 else {
5443 /* we want a close response here. Close header required if
5444 * the server is 1.1, regardless of the client.
5445 */
5446 if (msg->flags & HTTP_MSGF_VER_11)
5447 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005448 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005449
Willy Tarreau58975672014-04-24 21:13:57 +02005450 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5451 http_change_connection_header(txn, msg, want_flags);
5452 }
5453
5454 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005455 /* Always enter in the body analyzer */
5456 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5457 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005458
Willy Tarreau58975672014-04-24 21:13:57 +02005459 /* if the user wants to log as soon as possible, without counting
5460 * bytes from the server, then this is the right moment. We have
5461 * to temporarily assign bytes_out to log what we currently have.
5462 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005463 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005464 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5465 s->logs.bytes_out = txn->rsp.eoh;
5466 s->do_log(s);
5467 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005468 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005469 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005470}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005471
Willy Tarreaud98cf932009-12-27 22:54:55 +01005472/* This function is an analyser which forwards response body (including chunk
5473 * sizes if any). It is called as soon as we must forward, even if we forward
5474 * zero byte. The only situation where it must not be called is when we're in
5475 * tunnel mode and we want to forward till the close. It's used both to forward
5476 * remaining data and to resync after end of body. It expects the msg_state to
5477 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005478 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005479 *
5480 * It is capable of compressing response data both in content-length mode and
5481 * in chunked mode. The state machines follows different flows depending on
5482 * whether content-length and chunked modes are used, since there are no
5483 * trailers in content-length :
5484 *
5485 * chk-mode cl-mode
5486 * ,----- BODY -----.
5487 * / \
5488 * V size > 0 V chk-mode
5489 * .--> SIZE -------------> DATA -------------> CRLF
5490 * | | size == 0 | last byte |
5491 * | v final crlf v inspected |
5492 * | TRAILERS -----------> DONE |
5493 * | |
5494 * `----------------------------------------------'
5495 *
5496 * Compression only happens in the DATA state, and must be flushed in final
5497 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5498 * is performed at once on final states for all bytes parsed, or when leaving
5499 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005500 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005501int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005502{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005503 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005504 struct http_txn *txn = s->txn;
5505 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005506 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005507
Christopher Faulet45073512018-07-20 10:16:29 +02005508 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 +02005509 now_ms, __FUNCTION__,
5510 s,
5511 res,
5512 res->rex, res->wex,
5513 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005514 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005515 res->analysers);
5516
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005517 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5518 return 0;
5519
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005520 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005521 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005522 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005523 /* Output closed while we were sending data. We must abort and
5524 * wake the other side up.
5525 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005526 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005527 msg->msg_state = HTTP_MSG_ERROR;
5528 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005529 return 1;
5530 }
5531
Willy Tarreau4fe41902010-06-07 22:27:41 +02005532 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005533 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005534
Christopher Fauletd7c91962015-04-30 11:48:27 +02005535 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005536 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5537 ? HTTP_MSG_CHUNK_SIZE
5538 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005539 }
5540
Willy Tarreauefdf0942014-04-24 20:08:57 +02005541 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005542 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005543 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005544 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005545 }
5546
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005547 if (msg->msg_state < HTTP_MSG_DONE) {
5548 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5549 ? http_msg_forward_chunked_body(s, msg)
5550 : http_msg_forward_body(s, msg));
5551 if (!ret)
5552 goto missing_data_or_waiting;
5553 if (ret < 0)
5554 goto return_bad_res;
5555 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005556
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005557 /* other states, DONE...TUNNEL */
5558 /* for keep-alive we don't want to forward closes on DONE */
5559 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5560 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5561 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005562
Christopher Faulet894da4c2017-07-18 11:29:07 +02005563 http_resync_states(s);
5564 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005565 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5566 if (res->flags & CF_SHUTW) {
5567 /* response errors are most likely due to the
5568 * client aborting the transfer. */
5569 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005570 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005571 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005572 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005573 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005574 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005575 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005576 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005577 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005578
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005579 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005580 if (res->flags & CF_SHUTW)
5581 goto aborted_xfer;
5582
5583 /* stop waiting for data if the input is closed before the end. If the
5584 * client side was already closed, it means that the client has aborted,
5585 * so we don't want to count this as a server abort. Otherwise it's a
5586 * server abort.
5587 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005588 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005589 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005590 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005591 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005592 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005593 if (!(s->flags & SF_ERR_MASK))
5594 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005595 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005596 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005597 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005598 goto return_bad_res_stats_ok;
5599 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005600 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005601
Willy Tarreau40dba092010-03-04 18:14:51 +01005602 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005603 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005604 goto return_bad_res;
5605
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005606 /* When TE: chunked is used, we need to get there again to parse
5607 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005608 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5609 * or if there are filters registered on the stream, we don't want to
5610 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005611 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005612 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005613 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005614 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5615 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005616 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005617
Willy Tarreau5c620922011-05-11 19:56:11 +02005618 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005619 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005620 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005621 * modes are already handled by the stream sock layer. We must not do
5622 * this in content-length mode because it could present the MSG_MORE
5623 * flag with the last block of forwarded data, which would cause an
5624 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005625 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005626 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005627 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005628
Willy Tarreau87b09662015-04-03 00:22:06 +02005629 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005630 return 0;
5631
Willy Tarreau40dba092010-03-04 18:14:51 +01005632 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005633 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005634 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005635 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005636
5637 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005638 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005639 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005640 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005641 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005642 res->analysers &= AN_RES_FLT_END;
5643 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 +01005644 if (objt_server(s->target))
5645 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005646
Willy Tarreaue7dff022015-04-03 01:14:29 +02005647 if (!(s->flags & SF_ERR_MASK))
5648 s->flags |= SF_ERR_PRXCOND;
5649 if (!(s->flags & SF_FINST_MASK))
5650 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005651 return 0;
5652
5653 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005654 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005655 txn->rsp.msg_state = HTTP_MSG_ERROR;
5656 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005657 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005658 res->analysers &= AN_RES_FLT_END;
5659 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 +01005660
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005661 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5662 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005663 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005664 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005665
Willy Tarreaue7dff022015-04-03 01:14:29 +02005666 if (!(s->flags & SF_ERR_MASK))
5667 s->flags |= SF_ERR_CLICL;
5668 if (!(s->flags & SF_FINST_MASK))
5669 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005670 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005671}
5672
5673
5674static inline int
5675http_msg_forward_body(struct stream *s, struct http_msg *msg)
5676{
5677 struct channel *chn = msg->chn;
5678 int ret;
5679
5680 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5681
5682 if (msg->msg_state == HTTP_MSG_ENDING)
5683 goto ending;
5684
5685 /* Neither content-length, nor transfer-encoding was found, so we must
5686 * read the body until the server connection is closed. In that case, we
5687 * eat data as they come. Of course, this happens for response only. */
5688 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005689 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005690 msg->chunk_len += len;
5691 msg->body_len += len;
5692 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005693 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005694 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005695 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005696 msg->next += ret;
5697 msg->chunk_len -= ret;
5698 if (msg->chunk_len) {
5699 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005700 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005701 chn->flags |= CF_WAKE_WRITE;
5702 goto missing_data_or_waiting;
5703 }
5704
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005705 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5706 * always set for a request. */
5707 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5708 /* The server still sending data that should be filtered */
5709 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5710 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005711 msg->msg_state = HTTP_MSG_TUNNEL;
5712 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005713 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005714
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005715 msg->msg_state = HTTP_MSG_ENDING;
5716
5717 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005718 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005719 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005720 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5721 /* default_ret */ msg->next,
5722 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005723 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005724 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005725 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5726 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005727 if (msg->next)
5728 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005729
Christopher Fauletda02e172015-12-04 09:25:05 +01005730 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5731 /* default_ret */ 1,
5732 /* on_error */ goto error,
5733 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005734 if (msg->msg_state == HTTP_MSG_ENDING)
5735 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005736 return 1;
5737
5738 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005739 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005740 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5741 /* default_ret */ msg->next,
5742 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005743 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005744 msg->next -= ret;
5745 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5746 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005747 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005748 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005749 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005750 return 0;
5751 error:
5752 return -1;
5753}
5754
5755static inline int
5756http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
5757{
5758 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005759 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005760 int ret;
5761
5762 /* Here we have the guarantee to be in one of the following state:
5763 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5764 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5765
5766 switch_states:
5767 switch (msg->msg_state) {
5768 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005769 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005770 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005771 /* on_error */ goto error);
5772 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005773 msg->chunk_len -= ret;
5774 if (msg->chunk_len) {
5775 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005776 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005777 chn->flags |= CF_WAKE_WRITE;
5778 goto missing_data_or_waiting;
5779 }
5780
5781 /* nothing left to forward for this chunk*/
5782 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5783 /* fall through for HTTP_MSG_CHUNK_CRLF */
5784
5785 case HTTP_MSG_CHUNK_CRLF:
5786 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005787 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005788 if (ret == 0)
5789 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005790 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005791 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005792 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005793 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005794 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005795 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005796 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005797 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5798 /* fall through for HTTP_MSG_CHUNK_SIZE */
5799
5800 case HTTP_MSG_CHUNK_SIZE:
5801 /* read the chunk size and assign it to ->chunk_len,
5802 * then set ->next to point to the body and switch to
5803 * DATA or TRAILERS state.
5804 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005805 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005806 if (ret == 0)
5807 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005808 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005809 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005810 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005811 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005812 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005813 }
5814
5815 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005816 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005817 msg->chunk_len = chunk;
5818 msg->body_len += chunk;
5819
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005820 if (msg->chunk_len) {
5821 msg->msg_state = HTTP_MSG_DATA;
5822 goto switch_states;
5823 }
5824 msg->msg_state = HTTP_MSG_TRAILERS;
5825 /* fall through for HTTP_MSG_TRAILERS */
5826
5827 case HTTP_MSG_TRAILERS:
5828 ret = http_forward_trailers(msg);
5829 if (ret < 0)
5830 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005831 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5832 /* default_ret */ 1,
5833 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005834 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005835 if (!ret)
5836 goto missing_data_or_waiting;
5837 break;
5838
5839 case HTTP_MSG_ENDING:
5840 goto ending;
5841
5842 default:
5843 /* This should no happen in this function */
5844 goto error;
5845 }
5846
5847 msg->msg_state = HTTP_MSG_ENDING;
5848 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005849 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005850 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005851 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005852 /* default_ret */ msg->next,
5853 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005854 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005855 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005856 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5857 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005858 if (msg->next)
5859 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005860
Christopher Fauletda02e172015-12-04 09:25:05 +01005861 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005862 /* default_ret */ 1,
5863 /* on_error */ goto error,
5864 /* on_wait */ goto waiting);
5865 msg->msg_state = HTTP_MSG_DONE;
5866 return 1;
5867
5868 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005869 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005870 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005871 /* default_ret */ msg->next,
5872 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005873 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005874 msg->next -= ret;
5875 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5876 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005877 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005878 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005879 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005880 return 0;
5881
5882 chunk_parsing_error:
5883 if (msg->err_pos >= 0) {
5884 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005885 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005886 msg->msg_state, strm_fe(s));
5887 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005888 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005889 msg, msg->msg_state, s->be);
5890 }
5891 error:
5892 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005893}
5894
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005895
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005896/* Iterate the same filter through all request headers.
5897 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005898 * Since it can manage the switch to another backend, it updates the per-proxy
5899 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005900 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005901int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005902{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005903 char *cur_ptr, *cur_end, *cur_next;
5904 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005905 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005906 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005907 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005908
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005909 last_hdr = 0;
5910
Willy Tarreauf37954d2018-06-15 18:31:02 +02005911 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005912 old_idx = 0;
5913
5914 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005915 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005916 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005917 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005918 (exp->action == ACT_ALLOW ||
5919 exp->action == ACT_DENY ||
5920 exp->action == ACT_TARPIT))
5921 return 0;
5922
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005923 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005924 if (!cur_idx)
5925 break;
5926
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005927 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005928 cur_ptr = cur_next;
5929 cur_end = cur_ptr + cur_hdr->len;
5930 cur_next = cur_end + cur_hdr->cr + 1;
5931
5932 /* Now we have one header between cur_ptr and cur_end,
5933 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005934 */
5935
Willy Tarreau15a53a42015-01-21 13:39:42 +01005936 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005937 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005938 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005939 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005940 last_hdr = 1;
5941 break;
5942
5943 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005944 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005945 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005946 break;
5947
5948 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005949 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005950 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005951 break;
5952
5953 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005954 len = exp_replace(trash.area,
5955 trash.size, cur_ptr,
5956 exp->replace, pmatch);
5957 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005958 return -1;
5959
Willy Tarreau6e27be12018-08-22 04:46:47 +02005960 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5961
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005962 /* FIXME: if the user adds a newline in the replacement, the
5963 * index will not be recalculated for now, and the new line
5964 * will not be counted as a new header.
5965 */
5966
5967 cur_end += delta;
5968 cur_next += delta;
5969 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005970 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005971 break;
5972
5973 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005974 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005975 cur_next += delta;
5976
Willy Tarreaufa355d42009-11-29 18:12:29 +01005977 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005978 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5979 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005980 cur_hdr->len = 0;
5981 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005982 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005983 break;
5984
5985 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005986 }
5987
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005988 /* keep the link from this header to next one in case of later
5989 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005990 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005991 old_idx = cur_idx;
5992 }
5993 return 0;
5994}
5995
5996
5997/* Apply the filter to the request line.
5998 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5999 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006000 * Since it can manage the switch to another backend, it updates the per-proxy
6001 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006002 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006003int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006004{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006005 char *cur_ptr, *cur_end;
6006 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006007 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006008 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006009
Willy Tarreau3d300592007-03-18 18:34:41 +01006010 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006011 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006012 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006013 (exp->action == ACT_ALLOW ||
6014 exp->action == ACT_DENY ||
6015 exp->action == ACT_TARPIT))
6016 return 0;
6017 else if (exp->action == ACT_REMOVE)
6018 return 0;
6019
6020 done = 0;
6021
Willy Tarreauf37954d2018-06-15 18:31:02 +02006022 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006023 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006024
6025 /* Now we have the request line between cur_ptr and cur_end */
6026
Willy Tarreau15a53a42015-01-21 13:39:42 +01006027 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006028 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006029 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006030 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006031 done = 1;
6032 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006033
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006034 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006035 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006036 done = 1;
6037 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006038
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006039 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006040 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006041 done = 1;
6042 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006043
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006044 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006045 len = exp_replace(trash.area, trash.size,
6046 cur_ptr, exp->replace, pmatch);
6047 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006048 return -1;
6049
Willy Tarreau6e27be12018-08-22 04:46:47 +02006050 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6051
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006052 /* FIXME: if the user adds a newline in the replacement, the
6053 * index will not be recalculated for now, and the new line
6054 * will not be counted as a new header.
6055 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006056
Willy Tarreaufa355d42009-11-29 18:12:29 +01006057 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006058 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006059 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006060 HTTP_MSG_RQMETH,
6061 cur_ptr, cur_end + 1,
6062 NULL, NULL);
6063 if (unlikely(!cur_end))
6064 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006065
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006066 /* we have a full request and we know that we have either a CR
6067 * or an LF at <ptr>.
6068 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006069 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6070 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006071 /* there is no point trying this regex on headers */
6072 return 1;
6073 }
6074 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006075 return done;
6076}
Willy Tarreau97de6242006-12-27 17:18:38 +01006077
Willy Tarreau58f10d72006-12-04 02:26:12 +01006078
Willy Tarreau58f10d72006-12-04 02:26:12 +01006079
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006080/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006081 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006082 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006083 * unparsable request. Since it can manage the switch to another backend, it
6084 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006085 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006086int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006087{
Willy Tarreau192252e2015-04-04 01:47:55 +02006088 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006089 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006090 struct hdr_exp *exp;
6091
6092 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006093 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006094
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006095 /*
6096 * The interleaving of transformations and verdicts
6097 * makes it difficult to decide to continue or stop
6098 * the evaluation.
6099 */
6100
Willy Tarreau6c123b12010-01-28 20:22:06 +01006101 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6102 break;
6103
Willy Tarreau3d300592007-03-18 18:34:41 +01006104 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006105 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006106 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006107 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006108
6109 /* if this filter had a condition, evaluate it now and skip to
6110 * next filter if the condition does not match.
6111 */
6112 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006113 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006114 ret = acl_pass(ret);
6115 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6116 ret = !ret;
6117
6118 if (!ret)
6119 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006120 }
6121
6122 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006123 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006124 if (unlikely(ret < 0))
6125 return -1;
6126
6127 if (likely(ret == 0)) {
6128 /* The filter did not match the request, it can be
6129 * iterated through all headers.
6130 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006131 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6132 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006133 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006134 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006135 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006136}
6137
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006138
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006139/* Delete a value in a header between delimiters <from> and <next> in buffer
6140 * <buf>. The number of characters displaced is returned, and the pointer to
6141 * the first delimiter is updated if required. The function tries as much as
6142 * possible to respect the following principles :
6143 * - replace <from> delimiter by the <next> one unless <from> points to a
6144 * colon, in which case <next> is simply removed
6145 * - set exactly one space character after the new first delimiter, unless
6146 * there are not enough characters in the block being moved to do so.
6147 * - remove unneeded spaces before the previous delimiter and after the new
6148 * one.
6149 *
6150 * It is the caller's responsibility to ensure that :
6151 * - <from> points to a valid delimiter or the colon ;
6152 * - <next> points to a valid delimiter or the final CR/LF ;
6153 * - there are non-space chars before <from> ;
6154 * - there is a CR/LF at or after <next>.
6155 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006156int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006157{
6158 char *prev = *from;
6159
6160 if (*prev == ':') {
6161 /* We're removing the first value, preserve the colon and add a
6162 * space if possible.
6163 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006164 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006165 next++;
6166 prev++;
6167 if (prev < next)
6168 *prev++ = ' ';
6169
Willy Tarreau2235b262016-11-05 15:50:20 +01006170 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006171 next++;
6172 } else {
6173 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006174 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006175 prev--;
6176 *from = prev;
6177
6178 /* copy the delimiter and if possible a space if we're
6179 * not at the end of the line.
6180 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006181 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006182 *prev++ = *next++;
6183 if (prev + 1 < next)
6184 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006185 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006186 next++;
6187 }
6188 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006189 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006190}
6191
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006192/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006193 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006194 * desirable to call it only when needed. This code is quite complex because
6195 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6196 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006197 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006198void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006199{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006200 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006201 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006202 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006203 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006204 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6205 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006206
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006207 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006208 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006209 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006210
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006211 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006212 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006213 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006214
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006215 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006216 hdr_beg = hdr_next;
6217 hdr_end = hdr_beg + cur_hdr->len;
6218 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006219
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006220 /* We have one full header between hdr_beg and hdr_end, and the
6221 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006222 * "Cookie:" headers.
6223 */
6224
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006225 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006226 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006227 old_idx = cur_idx;
6228 continue;
6229 }
6230
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006231 del_from = NULL; /* nothing to be deleted */
6232 preserve_hdr = 0; /* assume we may kill the whole header */
6233
Willy Tarreau58f10d72006-12-04 02:26:12 +01006234 /* Now look for cookies. Conforming to RFC2109, we have to support
6235 * attributes whose name begin with a '$', and associate them with
6236 * the right cookie, if we want to delete this cookie.
6237 * So there are 3 cases for each cookie read :
6238 * 1) it's a special attribute, beginning with a '$' : ignore it.
6239 * 2) it's a server id cookie that we *MAY* want to delete : save
6240 * some pointers on it (last semi-colon, beginning of cookie...)
6241 * 3) it's an application cookie : we *MAY* have to delete a previous
6242 * "special" cookie.
6243 * At the end of loop, if a "special" cookie remains, we may have to
6244 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006245 * *MUST* delete it.
6246 *
6247 * Note: RFC2965 is unclear about the processing of spaces around
6248 * the equal sign in the ATTR=VALUE form. A careful inspection of
6249 * the RFC explicitly allows spaces before it, and not within the
6250 * tokens (attrs or values). An inspection of RFC2109 allows that
6251 * too but section 10.1.3 lets one think that spaces may be allowed
6252 * after the equal sign too, resulting in some (rare) buggy
6253 * implementations trying to do that. So let's do what servers do.
6254 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6255 * allowed quoted strings in values, with any possible character
6256 * after a backslash, including control chars and delimitors, which
6257 * causes parsing to become ambiguous. Browsers also allow spaces
6258 * within values even without quotes.
6259 *
6260 * We have to keep multiple pointers in order to support cookie
6261 * removal at the beginning, middle or end of header without
6262 * corrupting the header. All of these headers are valid :
6263 *
6264 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6265 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6266 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6267 * | | | | | | | | |
6268 * | | | | | | | | hdr_end <--+
6269 * | | | | | | | +--> next
6270 * | | | | | | +----> val_end
6271 * | | | | | +-----------> val_beg
6272 * | | | | +--------------> equal
6273 * | | | +----------------> att_end
6274 * | | +---------------------> att_beg
6275 * | +--------------------------> prev
6276 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006277 */
6278
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006279 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6280 /* Iterate through all cookies on this line */
6281
6282 /* find att_beg */
6283 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006284 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006285 att_beg++;
6286
6287 /* find att_end : this is the first character after the last non
6288 * space before the equal. It may be equal to hdr_end.
6289 */
6290 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006291
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006292 while (equal < hdr_end) {
6293 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006294 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006295 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006296 continue;
6297 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006298 }
6299
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006300 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6301 * is between <att_beg> and <equal>, both may be identical.
6302 */
6303
6304 /* look for end of cookie if there is an equal sign */
6305 if (equal < hdr_end && *equal == '=') {
6306 /* look for the beginning of the value */
6307 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006308 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006309 val_beg++;
6310
6311 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006312 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006313
6314 /* make val_end point to the first white space or delimitor after the value */
6315 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006316 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006317 val_end--;
6318 } else {
6319 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006320 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006321
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006322 /* We have nothing to do with attributes beginning with '$'. However,
6323 * they will automatically be removed if a header before them is removed,
6324 * since they're supposed to be linked together.
6325 */
6326 if (*att_beg == '$')
6327 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006328
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006329 /* Ignore cookies with no equal sign */
6330 if (equal == next) {
6331 /* This is not our cookie, so we must preserve it. But if we already
6332 * scheduled another cookie for removal, we cannot remove the
6333 * complete header, but we can remove the previous block itself.
6334 */
6335 preserve_hdr = 1;
6336 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006337 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006338 val_end += delta;
6339 next += delta;
6340 hdr_end += delta;
6341 hdr_next += delta;
6342 cur_hdr->len += delta;
6343 http_msg_move_end(&txn->req, delta);
6344 prev = del_from;
6345 del_from = NULL;
6346 }
6347 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006348 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006349
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006350 /* if there are spaces around the equal sign, we need to
6351 * strip them otherwise we'll get trouble for cookie captures,
6352 * or even for rewrites. Since this happens extremely rarely,
6353 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006354 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006355 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6356 int stripped_before = 0;
6357 int stripped_after = 0;
6358
6359 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006360 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006361 equal += stripped_before;
6362 val_beg += stripped_before;
6363 }
6364
6365 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006366 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006367 val_beg += stripped_after;
6368 stripped_before += stripped_after;
6369 }
6370
6371 val_end += stripped_before;
6372 next += stripped_before;
6373 hdr_end += stripped_before;
6374 hdr_next += stripped_before;
6375 cur_hdr->len += stripped_before;
6376 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006377 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006378 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006379
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006380 /* First, let's see if we want to capture this cookie. We check
6381 * that we don't already have a client side cookie, because we
6382 * can only capture one. Also as an optimisation, we ignore
6383 * cookies shorter than the declared name.
6384 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006385 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6386 (val_end - att_beg >= sess->fe->capture_namelen) &&
6387 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006388 int log_len = val_end - att_beg;
6389
Willy Tarreaubafbe012017-11-24 17:34:44 +01006390 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006391 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006392 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006393 if (log_len > sess->fe->capture_len)
6394 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006395 memcpy(txn->cli_cookie, att_beg, log_len);
6396 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006397 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006398 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006399
Willy Tarreaubca99692010-10-06 19:25:55 +02006400 /* Persistence cookies in passive, rewrite or insert mode have the
6401 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006402 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006403 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006404 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006405 * For cookies in prefix mode, the form is :
6406 *
6407 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006408 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006409 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6410 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6411 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006412 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006413
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006414 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6415 * have the server ID between val_beg and delim, and the original cookie between
6416 * delim+1 and val_end. Otherwise, delim==val_end :
6417 *
6418 * Cookie: NAME=SRV; # in all but prefix modes
6419 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6420 * | || || | |+-> next
6421 * | || || | +--> val_end
6422 * | || || +---------> delim
6423 * | || |+------------> val_beg
6424 * | || +-------------> att_end = equal
6425 * | |+-----------------> att_beg
6426 * | +------------------> prev
6427 * +-------------------------> hdr_beg
6428 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006429
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006430 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006431 for (delim = val_beg; delim < val_end; delim++)
6432 if (*delim == COOKIE_DELIM)
6433 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006434 } else {
6435 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006436 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006437 /* Now check if the cookie contains a date field, which would
6438 * appear after a vertical bar ('|') just after the server name
6439 * and before the delimiter.
6440 */
6441 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6442 if (vbar1) {
6443 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006444 * right is the last seen date. It is a base64 encoded
6445 * 30-bit value representing the UNIX date since the
6446 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006447 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006448 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006449 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006450 if (val_end - vbar1 >= 5) {
6451 val = b64tos30(vbar1);
6452 if (val > 0)
6453 txn->cookie_last_date = val << 2;
6454 }
6455 /* look for a second vertical bar */
6456 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6457 if (vbar1 && (val_end - vbar1 > 5)) {
6458 val = b64tos30(vbar1 + 1);
6459 if (val > 0)
6460 txn->cookie_first_date = val << 2;
6461 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006462 }
6463 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006464
Willy Tarreauf64d1412010-10-07 20:06:11 +02006465 /* if the cookie has an expiration date and the proxy wants to check
6466 * it, then we do that now. We first check if the cookie is too old,
6467 * then only if it has expired. We detect strict overflow because the
6468 * time resolution here is not great (4 seconds). Cookies with dates
6469 * in the future are ignored if their offset is beyond one day. This
6470 * allows an admin to fix timezone issues without expiring everyone
6471 * and at the same time avoids keeping unwanted side effects for too
6472 * long.
6473 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006474 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6475 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006476 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006477 txn->flags &= ~TX_CK_MASK;
6478 txn->flags |= TX_CK_OLD;
6479 delim = val_beg; // let's pretend we have not found the cookie
6480 txn->cookie_first_date = 0;
6481 txn->cookie_last_date = 0;
6482 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006483 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6484 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006485 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006486 txn->flags &= ~TX_CK_MASK;
6487 txn->flags |= TX_CK_EXPIRED;
6488 delim = val_beg; // let's pretend we have not found the cookie
6489 txn->cookie_first_date = 0;
6490 txn->cookie_last_date = 0;
6491 }
6492
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006493 /* Here, we'll look for the first running server which supports the cookie.
6494 * This allows to share a same cookie between several servers, for example
6495 * to dedicate backup servers to specific servers only.
6496 * However, to prevent clients from sticking to cookie-less backup server
6497 * when they have incidentely learned an empty cookie, we simply ignore
6498 * empty cookies and mark them as invalid.
6499 * The same behaviour is applied when persistence must be ignored.
6500 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006501 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006502 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006503
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006504 while (srv) {
6505 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6506 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006507 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006508 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006509 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006510 /* we found the server and we can use it */
6511 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006512 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006513 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006514 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006515 break;
6516 } else {
6517 /* we found a server, but it's down,
6518 * mark it as such and go on in case
6519 * another one is available.
6520 */
6521 txn->flags &= ~TX_CK_MASK;
6522 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006523 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006524 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006525 srv = srv->next;
6526 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006527
Willy Tarreauf64d1412010-10-07 20:06:11 +02006528 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006529 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006530 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006531 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006532 txn->flags |= TX_CK_UNUSED;
6533 else
6534 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006535 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006536
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006537 /* depending on the cookie mode, we may have to either :
6538 * - delete the complete cookie if we're in insert+indirect mode, so that
6539 * the server never sees it ;
6540 * - remove the server id from the cookie value, and tag the cookie as an
6541 * application cookie so that it does not get accidentely removed later,
6542 * if we're in cookie prefix mode
6543 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006544 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006545 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006546
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006547 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006548 val_end += delta;
6549 next += delta;
6550 hdr_end += delta;
6551 hdr_next += delta;
6552 cur_hdr->len += delta;
6553 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006554
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006555 del_from = NULL;
6556 preserve_hdr = 1; /* we want to keep this cookie */
6557 }
6558 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006559 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006560 del_from = prev;
6561 }
6562 } else {
6563 /* This is not our cookie, so we must preserve it. But if we already
6564 * scheduled another cookie for removal, we cannot remove the
6565 * complete header, but we can remove the previous block itself.
6566 */
6567 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006568
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006569 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006570 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006571 if (att_beg >= del_from)
6572 att_beg += delta;
6573 if (att_end >= del_from)
6574 att_end += delta;
6575 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006576 val_end += delta;
6577 next += delta;
6578 hdr_end += delta;
6579 hdr_next += delta;
6580 cur_hdr->len += delta;
6581 http_msg_move_end(&txn->req, delta);
6582 prev = del_from;
6583 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006584 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006585 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006586
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006587 /* continue with next cookie on this header line */
6588 att_beg = next;
6589 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006590
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006591 /* There are no more cookies on this line.
6592 * We may still have one (or several) marked for deletion at the
6593 * end of the line. We must do this now in two ways :
6594 * - if some cookies must be preserved, we only delete from the
6595 * mark to the end of line ;
6596 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006597 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006598 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006599 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006600 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006601 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006602 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006603 cur_hdr->len += delta;
6604 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006605 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006606
6607 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006608 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6609 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006610 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006611 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006612 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006613 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006614 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006615 }
6616
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006617 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006618 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006619 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006620}
6621
6622
Willy Tarreaua15645d2007-03-18 16:22:39 +01006623/* Iterate the same filter through all response headers contained in <rtr>.
6624 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6625 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006626int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006627{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628 char *cur_ptr, *cur_end, *cur_next;
6629 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006630 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006631 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006632 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006633
6634 last_hdr = 0;
6635
Willy Tarreauf37954d2018-06-15 18:31:02 +02006636 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006637 old_idx = 0;
6638
6639 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006640 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006642 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006643 (exp->action == ACT_ALLOW ||
6644 exp->action == ACT_DENY))
6645 return 0;
6646
6647 cur_idx = txn->hdr_idx.v[old_idx].next;
6648 if (!cur_idx)
6649 break;
6650
6651 cur_hdr = &txn->hdr_idx.v[cur_idx];
6652 cur_ptr = cur_next;
6653 cur_end = cur_ptr + cur_hdr->len;
6654 cur_next = cur_end + cur_hdr->cr + 1;
6655
6656 /* Now we have one header between cur_ptr and cur_end,
6657 * and the next header starts at cur_next.
6658 */
6659
Willy Tarreau15a53a42015-01-21 13:39:42 +01006660 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006661 switch (exp->action) {
6662 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006663 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006664 last_hdr = 1;
6665 break;
6666
6667 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006668 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006669 last_hdr = 1;
6670 break;
6671
6672 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006673 len = exp_replace(trash.area,
6674 trash.size, cur_ptr,
6675 exp->replace, pmatch);
6676 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006677 return -1;
6678
Willy Tarreau6e27be12018-08-22 04:46:47 +02006679 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6680
Willy Tarreaua15645d2007-03-18 16:22:39 +01006681 /* FIXME: if the user adds a newline in the replacement, the
6682 * index will not be recalculated for now, and the new line
6683 * will not be counted as a new header.
6684 */
6685
6686 cur_end += delta;
6687 cur_next += delta;
6688 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006689 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006690 break;
6691
6692 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006693 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006694 cur_next += delta;
6695
Willy Tarreaufa355d42009-11-29 18:12:29 +01006696 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006697 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6698 txn->hdr_idx.used--;
6699 cur_hdr->len = 0;
6700 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006701 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006702 break;
6703
6704 }
6705 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006706
6707 /* keep the link from this header to next one in case of later
6708 * removal of next header.
6709 */
6710 old_idx = cur_idx;
6711 }
6712 return 0;
6713}
6714
6715
6716/* Apply the filter to the status line in the response buffer <rtr>.
6717 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6718 * or -1 if a replacement resulted in an invalid status line.
6719 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006720int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006721{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006722 char *cur_ptr, *cur_end;
6723 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006724 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006725 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006726
Willy Tarreau3d300592007-03-18 18:34:41 +01006727 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006728 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006729 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006730 (exp->action == ACT_ALLOW ||
6731 exp->action == ACT_DENY))
6732 return 0;
6733 else if (exp->action == ACT_REMOVE)
6734 return 0;
6735
6736 done = 0;
6737
Willy Tarreauf37954d2018-06-15 18:31:02 +02006738 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006739 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006740
6741 /* Now we have the status line between cur_ptr and cur_end */
6742
Willy Tarreau15a53a42015-01-21 13:39:42 +01006743 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006744 switch (exp->action) {
6745 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006746 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006747 done = 1;
6748 break;
6749
6750 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006751 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006752 done = 1;
6753 break;
6754
6755 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006756 len = exp_replace(trash.area, trash.size,
6757 cur_ptr, exp->replace, pmatch);
6758 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006759 return -1;
6760
Willy Tarreau6e27be12018-08-22 04:46:47 +02006761 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6762
Willy Tarreaua15645d2007-03-18 16:22:39 +01006763 /* FIXME: if the user adds a newline in the replacement, the
6764 * index will not be recalculated for now, and the new line
6765 * will not be counted as a new header.
6766 */
6767
Willy Tarreaufa355d42009-11-29 18:12:29 +01006768 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006769 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006770 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006771 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006772 cur_ptr, cur_end + 1,
6773 NULL, NULL);
6774 if (unlikely(!cur_end))
6775 return -1;
6776
6777 /* we have a full respnse and we know that we have either a CR
6778 * or an LF at <ptr>.
6779 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006780 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006781 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006782 /* there is no point trying this regex on headers */
6783 return 1;
6784 }
6785 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006786 return done;
6787}
6788
6789
6790
6791/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006792 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006793 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6794 * unparsable response.
6795 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006796int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006797{
Willy Tarreau192252e2015-04-04 01:47:55 +02006798 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006799 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006800 struct hdr_exp *exp;
6801
6802 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803 int ret;
6804
6805 /*
6806 * The interleaving of transformations and verdicts
6807 * makes it difficult to decide to continue or stop
6808 * the evaluation.
6809 */
6810
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006811 if (txn->flags & TX_SVDENY)
6812 break;
6813
Willy Tarreau3d300592007-03-18 18:34:41 +01006814 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006815 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6816 exp->action == ACT_PASS)) {
6817 exp = exp->next;
6818 continue;
6819 }
6820
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006821 /* if this filter had a condition, evaluate it now and skip to
6822 * next filter if the condition does not match.
6823 */
6824 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006825 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006826 ret = acl_pass(ret);
6827 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6828 ret = !ret;
6829 if (!ret)
6830 continue;
6831 }
6832
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006834 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006835 if (unlikely(ret < 0))
6836 return -1;
6837
6838 if (likely(ret == 0)) {
6839 /* The filter did not match the response, it can be
6840 * iterated through all headers.
6841 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006842 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6843 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006844 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006845 }
6846 return 0;
6847}
6848
6849
Willy Tarreaua15645d2007-03-18 16:22:39 +01006850/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006851 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006852 * desirable to call it only when needed. This function is also used when we
6853 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006854 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006855void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006856{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006857 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006858 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006859 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006860 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006861 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006862 char *hdr_beg, *hdr_end, *hdr_next;
6863 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006864
Willy Tarreaua15645d2007-03-18 16:22:39 +01006865 /* Iterate through the headers.
6866 * we start with the start line.
6867 */
6868 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006869 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006870
6871 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6872 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006873 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006874
6875 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006876 hdr_beg = hdr_next;
6877 hdr_end = hdr_beg + cur_hdr->len;
6878 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006879
Willy Tarreau24581ba2010-08-31 22:39:35 +02006880 /* We have one full header between hdr_beg and hdr_end, and the
6881 * next header starts at hdr_next. We're only interested in
6882 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006883 */
6884
Willy Tarreau24581ba2010-08-31 22:39:35 +02006885 is_cookie2 = 0;
6886 prev = hdr_beg + 10;
6887 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006888 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006889 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6890 if (!val) {
6891 old_idx = cur_idx;
6892 continue;
6893 }
6894 is_cookie2 = 1;
6895 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006896 }
6897
Willy Tarreau24581ba2010-08-31 22:39:35 +02006898 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6899 * <prev> points to the colon.
6900 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006901 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006902
Willy Tarreau24581ba2010-08-31 22:39:35 +02006903 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6904 * check-cache is enabled) and we are not interested in checking
6905 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006906 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006907 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006908 return;
6909
Willy Tarreau24581ba2010-08-31 22:39:35 +02006910 /* OK so now we know we have to process this response cookie.
6911 * The format of the Set-Cookie header is slightly different
6912 * from the format of the Cookie header in that it does not
6913 * support the comma as a cookie delimiter (thus the header
6914 * cannot be folded) because the Expires attribute described in
6915 * the original Netscape's spec may contain an unquoted date
6916 * with a comma inside. We have to live with this because
6917 * many browsers don't support Max-Age and some browsers don't
6918 * support quoted strings. However the Set-Cookie2 header is
6919 * clean.
6920 *
6921 * We have to keep multiple pointers in order to support cookie
6922 * removal at the beginning, middle or end of header without
6923 * corrupting the header (in case of set-cookie2). A special
6924 * pointer, <scav> points to the beginning of the set-cookie-av
6925 * fields after the first semi-colon. The <next> pointer points
6926 * either to the end of line (set-cookie) or next unquoted comma
6927 * (set-cookie2). All of these headers are valid :
6928 *
6929 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6930 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6931 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6932 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6933 * | | | | | | | | | |
6934 * | | | | | | | | +-> next hdr_end <--+
6935 * | | | | | | | +------------> scav
6936 * | | | | | | +--------------> val_end
6937 * | | | | | +--------------------> val_beg
6938 * | | | | +----------------------> equal
6939 * | | | +------------------------> att_end
6940 * | | +----------------------------> att_beg
6941 * | +------------------------------> prev
6942 * +-----------------------------------------> hdr_beg
6943 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006944
Willy Tarreau24581ba2010-08-31 22:39:35 +02006945 for (; prev < hdr_end; prev = next) {
6946 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006947
Willy Tarreau24581ba2010-08-31 22:39:35 +02006948 /* find att_beg */
6949 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006950 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006951 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006952
Willy Tarreau24581ba2010-08-31 22:39:35 +02006953 /* find att_end : this is the first character after the last non
6954 * space before the equal. It may be equal to hdr_end.
6955 */
6956 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006957
Willy Tarreau24581ba2010-08-31 22:39:35 +02006958 while (equal < hdr_end) {
6959 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6960 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006961 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006962 continue;
6963 att_end = equal;
6964 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006965
Willy Tarreau24581ba2010-08-31 22:39:35 +02006966 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6967 * is between <att_beg> and <equal>, both may be identical.
6968 */
6969
6970 /* look for end of cookie if there is an equal sign */
6971 if (equal < hdr_end && *equal == '=') {
6972 /* look for the beginning of the value */
6973 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006974 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006975 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006976
Willy Tarreau24581ba2010-08-31 22:39:35 +02006977 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006978 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006979
6980 /* make val_end point to the first white space or delimitor after the value */
6981 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006982 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006983 val_end--;
6984 } else {
6985 /* <equal> points to next comma, semi-colon or EOL */
6986 val_beg = val_end = next = equal;
6987 }
6988
6989 if (next < hdr_end) {
6990 /* Set-Cookie2 supports multiple cookies, and <next> points to
6991 * a colon or semi-colon before the end. So skip all attr-value
6992 * pairs and look for the next comma. For Set-Cookie, since
6993 * commas are permitted in values, skip to the end.
6994 */
6995 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006996 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006997 else
6998 next = hdr_end;
6999 }
7000
7001 /* Now everything is as on the diagram above */
7002
7003 /* Ignore cookies with no equal sign */
7004 if (equal == val_end)
7005 continue;
7006
7007 /* If there are spaces around the equal sign, we need to
7008 * strip them otherwise we'll get trouble for cookie captures,
7009 * or even for rewrites. Since this happens extremely rarely,
7010 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007011 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007012 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7013 int stripped_before = 0;
7014 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007015
Willy Tarreau24581ba2010-08-31 22:39:35 +02007016 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007017 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007018 equal += stripped_before;
7019 val_beg += stripped_before;
7020 }
7021
7022 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007023 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007024 val_beg += stripped_after;
7025 stripped_before += stripped_after;
7026 }
7027
7028 val_end += stripped_before;
7029 next += stripped_before;
7030 hdr_end += stripped_before;
7031 hdr_next += stripped_before;
7032 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007033 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007034 }
7035
7036 /* First, let's see if we want to capture this cookie. We check
7037 * that we don't already have a server side cookie, because we
7038 * can only capture one. Also as an optimisation, we ignore
7039 * cookies shorter than the declared name.
7040 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007041 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007042 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007043 (val_end - att_beg >= sess->fe->capture_namelen) &&
7044 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007045 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007046 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007047 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007048 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007049 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007050 if (log_len > sess->fe->capture_len)
7051 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007052 memcpy(txn->srv_cookie, att_beg, log_len);
7053 txn->srv_cookie[log_len] = 0;
7054 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007055 }
7056
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007057 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007058 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007059 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007060 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7061 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007062 /* assume passive cookie by default */
7063 txn->flags &= ~TX_SCK_MASK;
7064 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007065
7066 /* If the cookie is in insert mode on a known server, we'll delete
7067 * this occurrence because we'll insert another one later.
7068 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007069 * a direct access.
7070 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007071 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007072 /* The "preserve" flag was set, we don't want to touch the
7073 * server's cookie.
7074 */
7075 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007076 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007077 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007078 /* this cookie must be deleted */
7079 if (*prev == ':' && next == hdr_end) {
7080 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007081 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007082 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7083 txn->hdr_idx.used--;
7084 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007085 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007086 hdr_next += delta;
7087 http_msg_move_end(&txn->rsp, delta);
7088 /* note: while both invalid now, <next> and <hdr_end>
7089 * are still equal, so the for() will stop as expected.
7090 */
7091 } else {
7092 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007093 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007094 next = prev;
7095 hdr_end += delta;
7096 hdr_next += delta;
7097 cur_hdr->len += delta;
7098 http_msg_move_end(&txn->rsp, delta);
7099 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007100 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007101 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007102 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007103 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007104 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007105 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007106 * with this server since we know it.
7107 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007108 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007109 next += delta;
7110 hdr_end += delta;
7111 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007112 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007113 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007114
Willy Tarreauf1348312010-10-07 15:54:11 +02007115 txn->flags &= ~TX_SCK_MASK;
7116 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007117 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007118 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007119 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007120 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007121 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007122 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007123 next += delta;
7124 hdr_end += delta;
7125 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007126 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007127 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007128
Willy Tarreau827aee92011-03-10 16:55:02 +01007129 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007130 txn->flags &= ~TX_SCK_MASK;
7131 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007132 }
7133 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007134 /* that's done for this cookie, check the next one on the same
7135 * line when next != hdr_end (only if is_cookie2).
7136 */
7137 }
7138 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007139 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007140 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007141}
7142
7143
Willy Tarreaua15645d2007-03-18 16:22:39 +01007144/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007145 * Parses the Cache-Control and Pragma request header fields to determine if
7146 * the request may be served from the cache and/or if it is cacheable. Updates
7147 * s->txn->flags.
7148 */
7149void check_request_for_cacheability(struct stream *s, struct channel *chn)
7150{
7151 struct http_txn *txn = s->txn;
7152 char *p1, *p2;
7153 char *cur_ptr, *cur_end, *cur_next;
7154 int pragma_found;
7155 int cc_found;
7156 int cur_idx;
7157
7158 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7159 return; /* nothing more to do here */
7160
7161 cur_idx = 0;
7162 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007163 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007164
7165 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7166 struct hdr_idx_elem *cur_hdr;
7167 int val;
7168
7169 cur_hdr = &txn->hdr_idx.v[cur_idx];
7170 cur_ptr = cur_next;
7171 cur_end = cur_ptr + cur_hdr->len;
7172 cur_next = cur_end + cur_hdr->cr + 1;
7173
7174 /* We have one full header between cur_ptr and cur_end, and the
7175 * next header starts at cur_next.
7176 */
7177
7178 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7179 if (val) {
7180 if ((cur_end - (cur_ptr + val) >= 8) &&
7181 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7182 pragma_found = 1;
7183 continue;
7184 }
7185 }
7186
William Lallemand8a16fe02018-05-22 11:04:33 +02007187 /* Don't use the cache and don't try to store if we found the
7188 * Authorization header */
7189 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7190 if (val) {
7191 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7192 txn->flags |= TX_CACHE_IGNORE;
7193 continue;
7194 }
7195
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007196 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7197 if (!val)
7198 continue;
7199
7200 /* OK, right now we know we have a cache-control header at cur_ptr */
7201 cc_found = 1;
7202 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7203
7204 if (p1 >= cur_end) /* no more info */
7205 continue;
7206
7207 /* p1 is at the beginning of the value */
7208 p2 = p1;
7209 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7210 p2++;
7211
7212 /* we have a complete value between p1 and p2. We don't check the
7213 * values after max-age, max-stale nor min-fresh, we simply don't
7214 * use the cache when they're specified.
7215 */
7216 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7217 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7218 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7219 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7220 txn->flags |= TX_CACHE_IGNORE;
7221 continue;
7222 }
7223
7224 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7225 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7226 continue;
7227 }
7228 }
7229
7230 /* RFC7234#5.4:
7231 * When the Cache-Control header field is also present and
7232 * understood in a request, Pragma is ignored.
7233 * When the Cache-Control header field is not present in a
7234 * request, caches MUST consider the no-cache request
7235 * pragma-directive as having the same effect as if
7236 * "Cache-Control: no-cache" were present.
7237 */
7238 if (!cc_found && pragma_found)
7239 txn->flags |= TX_CACHE_IGNORE;
7240}
7241
7242/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007243 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007244 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007245void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007246{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007247 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007248 char *p1, *p2;
7249
7250 char *cur_ptr, *cur_end, *cur_next;
7251 int cur_idx;
7252
Willy Tarreau12b32f22017-12-21 16:08:09 +01007253 if (txn->status < 200) {
7254 /* do not try to cache interim responses! */
7255 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007256 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007257 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007258
7259 /* Iterate through the headers.
7260 * we start with the start line.
7261 */
7262 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007263 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007264
7265 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7266 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007267 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007268
7269 cur_hdr = &txn->hdr_idx.v[cur_idx];
7270 cur_ptr = cur_next;
7271 cur_end = cur_ptr + cur_hdr->len;
7272 cur_next = cur_end + cur_hdr->cr + 1;
7273
7274 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007275 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007276 */
7277
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007278 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7279 if (val) {
7280 if ((cur_end - (cur_ptr + val) >= 8) &&
7281 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7282 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7283 return;
7284 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007285 }
7286
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007287 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7288 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007289 continue;
7290
7291 /* OK, right now we know we have a cache-control header at cur_ptr */
7292
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007293 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294
7295 if (p1 >= cur_end) /* no more info */
7296 continue;
7297
7298 /* p1 is at the beginning of the value */
7299 p2 = p1;
7300
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007301 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007302 p2++;
7303
7304 /* we have a complete value between p1 and p2 */
7305 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007306 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7307 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7308 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7309 continue;
7310 }
7311
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312 /* we have something of the form no-cache="set-cookie" */
7313 if ((cur_end - p1 >= 21) &&
7314 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7315 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007316 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007317 continue;
7318 }
7319
7320 /* OK, so we know that either p2 points to the end of string or to a comma */
7321 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007322 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007323 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007324 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007325 return;
7326 }
7327
7328 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007329 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007330 continue;
7331 }
7332 }
7333}
7334
Willy Tarreau58f10d72006-12-04 02:26:12 +01007335
Willy Tarreaub2513902006-12-17 14:52:38 +01007336/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007337 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007338 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007339 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007340 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007341 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007342 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007343 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007344 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007345int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007346{
7347 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007348 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007349 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007350
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007351 if (!uri_auth)
7352 return 0;
7353
Cyril Bonté70be45d2010-10-12 00:14:35 +02007354 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007355 return 0;
7356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007357 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007358 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007359 return 0;
7360
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007361 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007362 return 0;
7363
Willy Tarreaub2513902006-12-17 14:52:38 +01007364 return 1;
7365}
7366
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007367/* Append the description of what is present in error snapshot <es> into <out>.
7368 * The description must be small enough to always fit in a trash. The output
7369 * buffer may be the trash so the trash must not be used inside this function.
7370 */
7371void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7372{
7373 chunk_appendf(&trash,
7374 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7375 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7376 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7377 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7378 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7379 es->ctx.http.m_flags, es->ctx.http.m_clen,
7380 es->ctx.http.m_blen, es->ctx.http.b_flags);
7381}
7382
Willy Tarreau4076a152009-04-02 15:18:36 +02007383/*
7384 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007385 * By default it tries to report the error position as msg->err_pos. However if
7386 * this one is not set, it will then report msg->next, which is the last known
7387 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007388 * displays buffers as a contiguous area starting at buf->p. The direction is
7389 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007390 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007391void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007392 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007393 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007394{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007395 union error_snapshot_ctx ctx;
7396 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007397
Willy Tarreauef3ca732018-09-07 15:47:35 +02007398 /* http-specific part now */
7399 ctx.http.sid = s->uniq_id;
7400 ctx.http.state = state;
7401 ctx.http.b_flags = msg->chn->flags;
7402 ctx.http.s_flags = s->flags;
7403 ctx.http.t_flags = s->txn->flags;
7404 ctx.http.m_flags = msg->flags;
7405 ctx.http.m_clen = msg->chunk_len;
7406 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007407
Willy Tarreauef3ca732018-09-07 15:47:35 +02007408 ofs = msg->chn->total - ci_data(msg->chn);
7409 if (ofs < 0)
7410 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007411
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007412 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007413 other_end, s->target,
7414 strm_sess(s), &msg->chn->buf,
7415 ofs, co_data(msg->chn),
7416 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7417 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007418}
Willy Tarreaub2513902006-12-17 14:52:38 +01007419
Willy Tarreau294c4732011-12-16 21:35:50 +01007420/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7421 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7422 * performed over the whole headers. Otherwise it must contain a valid header
7423 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7424 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7425 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7426 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007427 * -1. The value fetch stops at commas, so this function is suited for use with
7428 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007429 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007430 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007431unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007432 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007433 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007434{
Willy Tarreau294c4732011-12-16 21:35:50 +01007435 struct hdr_ctx local_ctx;
7436 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007437 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007438 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007439 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007440
Willy Tarreau294c4732011-12-16 21:35:50 +01007441 if (!ctx) {
7442 local_ctx.idx = 0;
7443 ctx = &local_ctx;
7444 }
7445
Willy Tarreaubce70882009-09-07 11:51:47 +02007446 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007447 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007448 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007449 occ--;
7450 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007451 *vptr = ctx->line + ctx->val;
7452 *vlen = ctx->vlen;
7453 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007454 }
7455 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007456 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007457 }
7458
7459 /* negative occurrence, we scan all the list then walk back */
7460 if (-occ > MAX_HDR_HISTORY)
7461 return 0;
7462
Willy Tarreau294c4732011-12-16 21:35:50 +01007463 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007464 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007465 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7466 len_hist[hist_ptr] = ctx->vlen;
7467 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007468 hist_ptr = 0;
7469 found++;
7470 }
7471 if (-occ > found)
7472 return 0;
7473 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007474 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7475 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7476 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007477 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007478 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007479 if (hist_ptr >= MAX_HDR_HISTORY)
7480 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007481 *vptr = ptr_hist[hist_ptr];
7482 *vlen = len_hist[hist_ptr];
7483 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007484}
7485
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007486/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7487 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7488 * performed over the whole headers. Otherwise it must contain a valid header
7489 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7490 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7491 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7492 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7493 * -1. This function differs from http_get_hdr() in that it only returns full
7494 * line header values and does not stop at commas.
7495 * The return value is 0 if nothing was found, or non-zero otherwise.
7496 */
7497unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7498 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007499 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007500{
7501 struct hdr_ctx local_ctx;
7502 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007503 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007504 unsigned int hist_ptr;
7505 int found;
7506
7507 if (!ctx) {
7508 local_ctx.idx = 0;
7509 ctx = &local_ctx;
7510 }
7511
7512 if (occ >= 0) {
7513 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007514 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007515 occ--;
7516 if (occ <= 0) {
7517 *vptr = ctx->line + ctx->val;
7518 *vlen = ctx->vlen;
7519 return 1;
7520 }
7521 }
7522 return 0;
7523 }
7524
7525 /* negative occurrence, we scan all the list then walk back */
7526 if (-occ > MAX_HDR_HISTORY)
7527 return 0;
7528
7529 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007530 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007531 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7532 len_hist[hist_ptr] = ctx->vlen;
7533 if (++hist_ptr >= MAX_HDR_HISTORY)
7534 hist_ptr = 0;
7535 found++;
7536 }
7537 if (-occ > found)
7538 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007539
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007540 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007541 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7542 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7543 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007544 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007545 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007546 if (hist_ptr >= MAX_HDR_HISTORY)
7547 hist_ptr -= MAX_HDR_HISTORY;
7548 *vptr = ptr_hist[hist_ptr];
7549 *vlen = len_hist[hist_ptr];
7550 return 1;
7551}
7552
Willy Tarreaubaaee002006-06-26 02:48:02 +02007553/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007554 * Print a debug line with a header. Always stop at the first CR or LF char,
7555 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7556 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007557 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007558void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007559{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007560 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007561 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007562
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007563 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007564 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007565 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007566 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 +02007567
7568 for (max = 0; start + max < end; max++)
7569 if (start[max] == '\r' || start[max] == '\n')
7570 break;
7571
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007572 UBOUND(max, trash.size - trash.data - 3);
7573 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7574 trash.area[trash.data++] = '\n';
7575 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007576}
7577
Willy Tarreaueee5b512015-04-03 23:46:31 +02007578
7579/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7580 * The hdr_idx is allocated as well. In case of allocation failure, everything
7581 * allocated is freed and NULL is returned. Otherwise the new transaction is
7582 * assigned to the stream and returned.
7583 */
7584struct http_txn *http_alloc_txn(struct stream *s)
7585{
7586 struct http_txn *txn = s->txn;
7587
7588 if (txn)
7589 return txn;
7590
Willy Tarreaubafbe012017-11-24 17:34:44 +01007591 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007592 if (!txn)
7593 return txn;
7594
7595 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007596 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007597 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007598 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007599 return NULL;
7600 }
7601
7602 s->txn = txn;
7603 return txn;
7604}
7605
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007606void http_txn_reset_req(struct http_txn *txn)
7607{
7608 txn->req.flags = 0;
7609 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7610 txn->req.next = 0;
7611 txn->req.chunk_len = 0LL;
7612 txn->req.body_len = 0LL;
7613 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7614}
7615
7616void http_txn_reset_res(struct http_txn *txn)
7617{
7618 txn->rsp.flags = 0;
7619 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7620 txn->rsp.next = 0;
7621 txn->rsp.chunk_len = 0LL;
7622 txn->rsp.body_len = 0LL;
7623 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7624}
7625
Willy Tarreau0937bc42009-12-22 15:03:09 +01007626/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007627 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007628 * the required fields are properly allocated and that we only need to (re)init
7629 * them. This should be used before processing any new request.
7630 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007631void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007632{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007633 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007634 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007635
7636 txn->flags = 0;
7637 txn->status = -1;
7638
Willy Tarreauf64d1412010-10-07 20:06:11 +02007639 txn->cookie_first_date = 0;
7640 txn->cookie_last_date = 0;
7641
Willy Tarreaueee5b512015-04-03 23:46:31 +02007642 txn->srv_cookie = NULL;
7643 txn->cli_cookie = NULL;
7644 txn->uri = NULL;
7645
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007646 http_txn_reset_req(txn);
7647 http_txn_reset_res(txn);
7648
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007649 txn->req.chn = &s->req;
7650 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007651
7652 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007653
7654 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7655 if (fe->options2 & PR_O2_REQBUG_OK)
7656 txn->req.err_pos = -1; /* let buggy requests pass */
7657
Willy Tarreau0937bc42009-12-22 15:03:09 +01007658 if (txn->hdr_idx.v)
7659 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007660
7661 vars_init(&s->vars_txn, SCOPE_TXN);
7662 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007663}
7664
7665/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007666void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007667{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007668 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007669 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007670
7671 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007672 pool_free(pool_head_requri, txn->uri);
7673 pool_free(pool_head_capture, txn->cli_cookie);
7674 pool_free(pool_head_capture, txn->srv_cookie);
7675 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007676
William Lallemanda73203e2012-03-12 12:48:57 +01007677 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007678 txn->uri = NULL;
7679 txn->srv_cookie = NULL;
7680 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007681
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007682 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007683 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007684 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007685 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007686 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007687 }
7688
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007689 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007690 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007691 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007692 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007693 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007694 }
7695
Willy Tarreau6204cd92016-03-10 16:33:04 +01007696 vars_prune(&s->vars_txn, s->sess, s);
7697 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007698}
7699
7700/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007701void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007702{
7703 http_end_txn(s);
7704 http_init_txn(s);
7705
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007706 /* reinitialise the current rule list pointer to NULL. We are sure that
7707 * any rulelist match the NULL pointer.
7708 */
7709 s->current_rule_list = NULL;
7710
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007711 s->be = strm_fe(s);
7712 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007713 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007714 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007715 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007716 /* re-init store persistence */
7717 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007718 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007719
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007720 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007721
Willy Tarreau739cfba2010-01-25 23:11:14 +01007722 /* We must trim any excess data from the response buffer, because we
7723 * may have blocked an invalid response from a server that we don't
7724 * want to accidentely forward once we disable the analysers, nor do
7725 * we want those data to come along with next response. A typical
7726 * example of such data would be from a buggy server responding to
7727 * a HEAD with some data, or sending more than the advertised
7728 * content-length.
7729 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007730 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007731 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007732
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007733 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007734 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007735
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007736 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007737 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007738
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007739 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007740 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007741
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007742 s->req.rex = TICK_ETERNITY;
7743 s->req.wex = TICK_ETERNITY;
7744 s->req.analyse_exp = TICK_ETERNITY;
7745 s->res.rex = TICK_ETERNITY;
7746 s->res.wex = TICK_ETERNITY;
7747 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007748 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007749}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007750
Sasha Pachev218f0642014-06-16 12:05:59 -06007751void free_http_res_rules(struct list *r)
7752{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02007753 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06007754
7755 list_for_each_entry_safe(pr, tr, r, list) {
7756 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02007757 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06007758 free(pr);
7759 }
7760}
7761
7762void free_http_req_rules(struct list *r)
7763{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02007764 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01007765
7766 list_for_each_entry_safe(pr, tr, r, list) {
7767 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007768 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01007769 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01007770
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02007771 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01007772 free(pr);
7773 }
7774}
7775
Willy Tarreaue365c0b2013-06-11 16:06:12 +02007776/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02007777struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01007778{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02007779 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02007780 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007781 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02007782 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01007783
Vincent Bernat02779b62016-04-03 13:48:43 +02007784 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01007785 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007786 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01007787 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01007788 }
7789
Willy Tarreau5c2e1982012-12-24 12:00:25 +01007790 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007791 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01007792 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02007793 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04007794 int code;
7795 int hc;
7796
Jarno Huuskonen800d1762017-03-06 14:56:36 +02007797 if (!strcmp(args[0], "tarpit")) {
7798 rule->action = ACT_HTTP_REQ_TARPIT;
7799 rule->deny_status = HTTP_ERR_500;
7800 }
7801 else {
7802 rule->action = ACT_ACTION_DENY;
7803 rule->deny_status = HTTP_ERR_403;
7804 }
Willy Tarreauff011f22011-01-06 17:51:27 +01007805 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04007806 if (strcmp(args[cur_arg], "deny_status") == 0) {
7807 cur_arg++;
7808 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007809 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
7810 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04007811 goto out_err;
7812 }
7813
7814 code = atol(args[cur_arg]);
7815 cur_arg++;
7816 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
7817 if (http_err_codes[hc] == code) {
7818 rule->deny_status = hc;
7819 break;
7820 }
7821 }
7822
7823 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007824 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
7825 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04007826 }
7827 }
Willy Tarreauff011f22011-01-06 17:51:27 +01007828 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007829 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01007830 cur_arg = 1;
7831
7832 while(*args[cur_arg]) {
7833 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01007834 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01007835 cur_arg+=2;
7836 continue;
7837 } else
7838 break;
7839 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02007840 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007841 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02007842 cur_arg = 1;
7843
7844 if (!*args[cur_arg] ||
7845 (*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 +01007846 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
7847 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02007848 goto out_err;
7849 }
7850 rule->arg.nice = atoi(args[cur_arg]);
7851 if (rule->arg.nice < -1024)
7852 rule->arg.nice = -1024;
7853 else if (rule->arg.nice > 1024)
7854 rule->arg.nice = 1024;
7855 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02007856 } else if (!strcmp(args[0], "set-tos")) {
7857#ifdef IP_TOS
7858 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007859 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02007860 cur_arg = 1;
7861
7862 if (!*args[cur_arg] ||
7863 (*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 +01007864 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
7865 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02007866 goto out_err;
7867 }
7868
7869 rule->arg.tos = strtol(args[cur_arg], &err, 0);
7870 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007871 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
7872 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02007873 goto out_err;
7874 }
7875 cur_arg++;
7876#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01007877 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 +02007878 goto out_err;
7879#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02007880 } else if (!strcmp(args[0], "set-mark")) {
7881#ifdef SO_MARK
7882 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007883 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02007884 cur_arg = 1;
7885
7886 if (!*args[cur_arg] ||
7887 (*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 +01007888 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
7889 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02007890 goto out_err;
7891 }
7892
7893 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
7894 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007895 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
7896 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02007897 goto out_err;
7898 }
7899 cur_arg++;
7900 global.last_checks |= LSTCHK_NETADM;
7901#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01007902 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 +02007903 goto out_err;
7904#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02007905 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007906 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02007907 cur_arg = 1;
7908
7909 if (!*args[cur_arg] ||
7910 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
7911 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01007912 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
7913 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02007914 goto out_err;
7915 }
7916 if (strcmp(args[cur_arg], "silent") == 0)
7917 rule->arg.loglevel = -1;
7918 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
7919 goto bad_log_level;
7920 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01007921 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007922 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01007923 cur_arg = 1;
7924
Willy Tarreau8d1c5162013-04-03 14:13:58 +02007925 if (!*args[cur_arg] || !*args[cur_arg+1] ||
7926 (*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 +01007927 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
7928 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01007929 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01007930 }
7931
7932 rule->arg.hdr_add.name = strdup(args[cur_arg]);
7933 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
7934 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02007935
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01007936 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01007937 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01007938 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 +01007939 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007940 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
7941 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01007942 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01007943 goto out_err;
7944 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01007945 free(proxy->conf.lfs_file);
7946 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
7947 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01007948 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02007949 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007950 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06007951 cur_arg = 1;
7952
7953 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02007954 (*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 +01007955 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
7956 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06007957 goto out_err;
7958 }
7959
7960 rule->arg.hdr_add.name = strdup(args[cur_arg]);
7961 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
7962 LIST_INIT(&rule->arg.hdr_add.fmt);
7963
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02007964 error = NULL;
7965 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007966 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
7967 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02007968 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06007969 goto out_err;
7970 }
7971
7972 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01007973 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01007974 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 +01007975 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007976 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
7977 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01007978 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01007979 goto out_err;
7980 }
Sasha Pachev218f0642014-06-16 12:05:59 -06007981
7982 free(proxy->conf.lfs_file);
7983 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
7984 proxy->conf.lfs_line = proxy->conf.args.line;
7985 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02007986 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007987 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02007988 cur_arg = 1;
7989
7990 if (!*args[cur_arg] ||
7991 (*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 +01007992 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
7993 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02007994 goto out_err;
7995 }
7996
7997 rule->arg.hdr_add.name = strdup(args[cur_arg]);
7998 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
7999
8000 proxy->conf.args.ctx = ARGC_HRQ;
8001 free(proxy->conf.lfs_file);
8002 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8003 proxy->conf.lfs_line = proxy->conf.args.line;
8004 cur_arg += 1;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008005 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Willy Tarreau09448f72014-06-25 18:12:15 +02008006 struct sample_expr *expr;
8007 unsigned int where;
8008 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008009 unsigned int tsc_num;
8010 const char *tsc_num_str;
Willy Tarreau09448f72014-06-25 18:12:15 +02008011
8012 cur_arg = 1;
8013 proxy->conf.args.ctx = ARGC_TRK;
8014
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008015 tsc_num_str = &args[0][8];
8016 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8017 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8018 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8019 free(err);
8020 goto out_err;
8021 }
8022
Willy Tarreau09448f72014-06-25 18:12:15 +02008023 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8024 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008025 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8026 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008027 free(err);
8028 goto out_err;
8029 }
8030
8031 where = 0;
8032 if (proxy->cap & PR_CAP_FE)
8033 where |= SMP_VAL_FE_HRQ_HDR;
8034 if (proxy->cap & PR_CAP_BE)
8035 where |= SMP_VAL_BE_HRQ_HDR;
8036
8037 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008038 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8039 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8040 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8041 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008042 free(expr);
8043 goto out_err;
8044 }
8045
8046 if (strcmp(args[cur_arg], "table") == 0) {
8047 cur_arg++;
8048 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008049 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8050 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008051 free(expr);
8052 goto out_err;
8053 }
8054 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008055 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008056 cur_arg++;
8057 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008058 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008059 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008060 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008061 } else if (strcmp(args[0], "redirect") == 0) {
8062 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008063 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008064
Willy Tarreaube4653b2015-05-28 15:26:58 +02008065 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008066 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8067 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008068 goto out_err;
8069 }
8070
8071 /* this redirect rule might already contain a parsed condition which
8072 * we'll pass to the http-request rule.
8073 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008074 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008075 rule->arg.redir = redir;
8076 rule->cond = redir->cond;
8077 redir->cond = NULL;
8078 cur_arg = 2;
8079 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008080 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8081 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008082 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008083 /*
8084 * '+ 8' for 'add-acl('
8085 * '- 9' for 'add-acl(' + trailing ')'
8086 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008087 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008088
8089 cur_arg = 1;
8090
8091 if (!*args[cur_arg] ||
8092 (*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 +01008093 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8094 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008095 goto out_err;
8096 }
8097
8098 LIST_INIT(&rule->arg.map.key);
8099 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008100 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008101 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008102 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008103 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8104 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008105 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008106 goto out_err;
8107 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008108 free(proxy->conf.lfs_file);
8109 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8110 proxy->conf.lfs_line = proxy->conf.args.line;
8111 cur_arg += 1;
8112 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8113 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008114 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008115 /*
8116 * '+ 8' for 'del-acl('
8117 * '- 9' for 'del-acl(' + trailing ')'
8118 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008119 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008120
8121 cur_arg = 1;
8122
8123 if (!*args[cur_arg] ||
8124 (*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 +01008125 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8126 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008127 goto out_err;
8128 }
8129
8130 LIST_INIT(&rule->arg.map.key);
8131 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008132 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008133 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008134 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008135 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8136 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008137 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008138 goto out_err;
8139 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008140 free(proxy->conf.lfs_file);
8141 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8142 proxy->conf.lfs_line = proxy->conf.args.line;
8143 cur_arg += 1;
8144 } else if (strncmp(args[0], "del-map", 7) == 0) {
8145 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008146 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008147 /*
8148 * '+ 8' for 'del-map('
8149 * '- 9' for 'del-map(' + trailing ')'
8150 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008151 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008152
8153 cur_arg = 1;
8154
8155 if (!*args[cur_arg] ||
8156 (*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 +01008157 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8158 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008159 goto out_err;
8160 }
8161
8162 LIST_INIT(&rule->arg.map.key);
8163 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008164 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008165 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008166 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008167 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8168 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008169 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008170 goto out_err;
8171 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008172 free(proxy->conf.lfs_file);
8173 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8174 proxy->conf.lfs_line = proxy->conf.args.line;
8175 cur_arg += 1;
8176 } else if (strncmp(args[0], "set-map", 7) == 0) {
8177 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008178 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008179 /*
8180 * '+ 8' for 'set-map('
8181 * '- 9' for 'set-map(' + trailing ')'
8182 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008183 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008184
8185 cur_arg = 1;
8186
8187 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8188 (*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 +01008189 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8190 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008191 goto out_err;
8192 }
8193
8194 LIST_INIT(&rule->arg.map.key);
8195 LIST_INIT(&rule->arg.map.value);
8196 proxy->conf.args.ctx = ARGC_HRQ;
8197
8198 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008199 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008200 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008201 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008202 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8203 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008204 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008205 goto out_err;
8206 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008207
8208 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008209 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008210 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 +01008211 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008212 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8213 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008214 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008215 goto out_err;
8216 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008217 free(proxy->conf.lfs_file);
8218 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8219 proxy->conf.lfs_line = proxy->conf.args.line;
8220
8221 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008222 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8223 char *errmsg = NULL;
8224 cur_arg = 1;
8225 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008226 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008227 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008228 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008229 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8230 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008231 free(errmsg);
8232 goto out_err;
8233 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008234 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008235 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008236 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8237 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8238 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8239 "%s%s, but got '%s'%s.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008240 file, linenum, *trash.area ? ", " : "", trash.area,
8241 args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008242 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008243 }
8244
8245 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8246 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008247 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008248
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008249 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008250 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8251 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008252 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008253 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008254 }
8255 rule->cond = cond;
8256 }
8257 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008258 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8259 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8260 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008261 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008262 }
8263
8264 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008265 out_err:
8266 free(rule);
8267 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008268}
8269
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008270/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008271struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008272{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008273 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008274 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008275 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008276 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008277
8278 rule = calloc(1, sizeof(*rule));
8279 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008280 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008281 goto out_err;
8282 }
8283
8284 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008285 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008286 cur_arg = 1;
8287 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008288 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008289 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008290 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008291 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008292 cur_arg = 1;
8293
8294 if (!*args[cur_arg] ||
8295 (*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 +01008296 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8297 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008298 goto out_err;
8299 }
8300 rule->arg.nice = atoi(args[cur_arg]);
8301 if (rule->arg.nice < -1024)
8302 rule->arg.nice = -1024;
8303 else if (rule->arg.nice > 1024)
8304 rule->arg.nice = 1024;
8305 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008306 } else if (!strcmp(args[0], "set-tos")) {
8307#ifdef IP_TOS
8308 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008309 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008310 cur_arg = 1;
8311
8312 if (!*args[cur_arg] ||
8313 (*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 +01008314 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8315 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008316 goto out_err;
8317 }
8318
8319 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8320 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008321 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8322 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008323 goto out_err;
8324 }
8325 cur_arg++;
8326#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008327 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 +02008328 goto out_err;
8329#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008330 } else if (!strcmp(args[0], "set-mark")) {
8331#ifdef SO_MARK
8332 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008333 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008334 cur_arg = 1;
8335
8336 if (!*args[cur_arg] ||
8337 (*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 +01008338 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8339 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008340 goto out_err;
8341 }
8342
8343 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8344 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008345 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8346 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008347 goto out_err;
8348 }
8349 cur_arg++;
8350 global.last_checks |= LSTCHK_NETADM;
8351#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008352 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 +02008353 goto out_err;
8354#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008355 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008356 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008357 cur_arg = 1;
8358
8359 if (!*args[cur_arg] ||
8360 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8361 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008362 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8363 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008364 goto out_err;
8365 }
8366 if (strcmp(args[cur_arg], "silent") == 0)
8367 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008368 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008369 goto bad_log_level;
8370 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008371 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008372 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008373 cur_arg = 1;
8374
8375 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8376 (*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 +01008377 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8378 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008379 goto out_err;
8380 }
8381
8382 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8383 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8384 LIST_INIT(&rule->arg.hdr_add.fmt);
8385
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008386 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008387 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008388 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 +01008389 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008390 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8391 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008392 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008393 goto out_err;
8394 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008395 free(proxy->conf.lfs_file);
8396 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8397 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008398 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008399 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008400 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008401 cur_arg = 1;
8402
8403 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008404 (*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 +01008405 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8406 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008407 goto out_err;
8408 }
8409
8410 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8411 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8412 LIST_INIT(&rule->arg.hdr_add.fmt);
8413
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008414 error = NULL;
8415 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008416 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8417 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008418 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008419 goto out_err;
8420 }
8421
8422 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008423 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008424 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 +01008425 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008426 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8427 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008428 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008429 goto out_err;
8430 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008431
8432 free(proxy->conf.lfs_file);
8433 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8434 proxy->conf.lfs_line = proxy->conf.args.line;
8435 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008436 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008437 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008438 cur_arg = 1;
8439
8440 if (!*args[cur_arg] ||
8441 (*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 +01008442 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8443 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008444 goto out_err;
8445 }
8446
8447 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8448 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8449
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008450 proxy->conf.args.ctx = ARGC_HRS;
8451 free(proxy->conf.lfs_file);
8452 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8453 proxy->conf.lfs_line = proxy->conf.args.line;
8454 cur_arg += 1;
8455 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8456 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008457 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008458 /*
8459 * '+ 8' for 'add-acl('
8460 * '- 9' for 'add-acl(' + trailing ')'
8461 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008462 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008463
8464 cur_arg = 1;
8465
8466 if (!*args[cur_arg] ||
8467 (*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 +01008468 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8469 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008470 goto out_err;
8471 }
8472
8473 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008474 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008475 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008476 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008477 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008478 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8479 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008480 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008481 goto out_err;
8482 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008483 free(proxy->conf.lfs_file);
8484 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8485 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008486
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008487 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008488 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8489 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008490 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008491 /*
8492 * '+ 8' for 'del-acl('
8493 * '- 9' for 'del-acl(' + trailing ')'
8494 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008495 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008496
8497 cur_arg = 1;
8498
8499 if (!*args[cur_arg] ||
8500 (*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 +01008501 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8502 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008503 goto out_err;
8504 }
8505
8506 LIST_INIT(&rule->arg.map.key);
8507 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008508 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008509 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008510 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008511 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8512 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008513 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008514 goto out_err;
8515 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008516 free(proxy->conf.lfs_file);
8517 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8518 proxy->conf.lfs_line = proxy->conf.args.line;
8519 cur_arg += 1;
8520 } else if (strncmp(args[0], "del-map", 7) == 0) {
8521 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008522 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008523 /*
8524 * '+ 8' for 'del-map('
8525 * '- 9' for 'del-map(' + trailing ')'
8526 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008527 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008528
8529 cur_arg = 1;
8530
8531 if (!*args[cur_arg] ||
8532 (*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 +01008533 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8534 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008535 goto out_err;
8536 }
8537
8538 LIST_INIT(&rule->arg.map.key);
8539 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008540 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008541 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008542 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008543 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
8544 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008545 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008546 goto out_err;
8547 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008548 free(proxy->conf.lfs_file);
8549 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8550 proxy->conf.lfs_line = proxy->conf.args.line;
8551 cur_arg += 1;
8552 } else if (strncmp(args[0], "set-map", 7) == 0) {
8553 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008554 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008555 /*
8556 * '+ 8' for 'set-map('
8557 * '- 9' for 'set-map(' + trailing ')'
8558 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008559 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008560
8561 cur_arg = 1;
8562
8563 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8564 (*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 +01008565 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8566 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008567 goto out_err;
8568 }
8569
8570 LIST_INIT(&rule->arg.map.key);
8571 LIST_INIT(&rule->arg.map.value);
8572
8573 proxy->conf.args.ctx = ARGC_HRS;
8574
8575 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008576 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008577 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008578 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008579 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
8580 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008581 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008582 goto out_err;
8583 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008584
8585 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008586 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008587 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 +01008588 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008589 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
8590 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008591 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008592 goto out_err;
8593 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008594
8595 free(proxy->conf.lfs_file);
8596 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8597 proxy->conf.lfs_line = proxy->conf.args.line;
8598
8599 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008600 } else if (strcmp(args[0], "redirect") == 0) {
8601 struct redirect_rule *redir;
8602 char *errmsg = NULL;
8603
8604 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008605 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8606 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02008607 goto out_err;
8608 }
8609
8610 /* this redirect rule might already contain a parsed condition which
8611 * we'll pass to the http-request rule.
8612 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008613 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008614 rule->arg.redir = redir;
8615 rule->cond = redir->cond;
8616 redir->cond = NULL;
8617 cur_arg = 2;
8618 return rule;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008619 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008620 struct sample_expr *expr;
8621 unsigned int where;
8622 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008623 unsigned int tsc_num;
8624 const char *tsc_num_str;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008625
8626 cur_arg = 1;
8627 proxy->conf.args.ctx = ARGC_TRK;
8628
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008629 tsc_num_str = &args[0][8];
8630 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8631 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8632 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8633 free(err);
8634 goto out_err;
8635 }
8636
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008637 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8638 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008639 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8640 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008641 free(err);
8642 goto out_err;
8643 }
8644
8645 where = 0;
8646 if (proxy->cap & PR_CAP_FE)
8647 where |= SMP_VAL_FE_HRS_HDR;
8648 if (proxy->cap & PR_CAP_BE)
8649 where |= SMP_VAL_BE_HRS_HDR;
8650
8651 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008652 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
8653 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8654 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8655 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008656 free(expr);
8657 goto out_err;
8658 }
8659
8660 if (strcmp(args[cur_arg], "table") == 0) {
8661 cur_arg++;
8662 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008663 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
8664 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008665 free(expr);
8666 goto out_err;
8667 }
8668 /* we copy the table name for now, it will be resolved later */
8669 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
8670 cur_arg++;
8671 }
8672 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008673 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008674 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02008675 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
8676 char *errmsg = NULL;
8677 cur_arg = 1;
8678 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008679 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008680 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008681 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008682 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8683 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008684 free(errmsg);
8685 goto out_err;
8686 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008687 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008688 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008689 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
8690 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8691 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8692 "%s%s, but got '%s'%s.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008693 file, linenum, *trash.area ? ", " : "", trash.area,
8694 args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008695 goto out_err;
8696 }
8697
8698 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8699 struct acl_cond *cond;
8700 char *errmsg = NULL;
8701
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008702 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008703 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8704 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008705 free(errmsg);
8706 goto out_err;
8707 }
8708 rule->cond = cond;
8709 }
8710 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008711 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
8712 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8713 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008714 goto out_err;
8715 }
8716
8717 return rule;
8718 out_err:
8719 free(rule);
8720 return NULL;
8721}
8722
Willy Tarreau4baae242012-12-27 12:00:31 +01008723/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008724 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02008725 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
8726 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01008727 */
8728struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02008729 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01008730{
8731 struct redirect_rule *rule;
8732 int cur_arg;
8733 int type = REDIRECT_TYPE_NONE;
8734 int code = 302;
8735 const char *destination = NULL;
8736 const char *cookie = NULL;
8737 int cookie_set = 0;
8738 unsigned int flags = REDIRECT_FLAG_NONE;
8739 struct acl_cond *cond = NULL;
8740
8741 cur_arg = 0;
8742 while (*(args[cur_arg])) {
8743 if (strcmp(args[cur_arg], "location") == 0) {
8744 if (!*args[cur_arg + 1])
8745 goto missing_arg;
8746
8747 type = REDIRECT_TYPE_LOCATION;
8748 cur_arg++;
8749 destination = args[cur_arg];
8750 }
8751 else if (strcmp(args[cur_arg], "prefix") == 0) {
8752 if (!*args[cur_arg + 1])
8753 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01008754 type = REDIRECT_TYPE_PREFIX;
8755 cur_arg++;
8756 destination = args[cur_arg];
8757 }
8758 else if (strcmp(args[cur_arg], "scheme") == 0) {
8759 if (!*args[cur_arg + 1])
8760 goto missing_arg;
8761
8762 type = REDIRECT_TYPE_SCHEME;
8763 cur_arg++;
8764 destination = args[cur_arg];
8765 }
8766 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8767 if (!*args[cur_arg + 1])
8768 goto missing_arg;
8769
8770 cur_arg++;
8771 cookie = args[cur_arg];
8772 cookie_set = 1;
8773 }
8774 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8775 if (!*args[cur_arg + 1])
8776 goto missing_arg;
8777
8778 cur_arg++;
8779 cookie = args[cur_arg];
8780 cookie_set = 0;
8781 }
8782 else if (strcmp(args[cur_arg], "code") == 0) {
8783 if (!*args[cur_arg + 1])
8784 goto missing_arg;
8785
8786 cur_arg++;
8787 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008788 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008789 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008790 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008791 args[cur_arg - 1], args[cur_arg]);
8792 return NULL;
8793 }
8794 }
8795 else if (!strcmp(args[cur_arg],"drop-query")) {
8796 flags |= REDIRECT_FLAG_DROP_QS;
8797 }
8798 else if (!strcmp(args[cur_arg],"append-slash")) {
8799 flags |= REDIRECT_FLAG_APPEND_SLASH;
8800 }
8801 else if (strcmp(args[cur_arg], "if") == 0 ||
8802 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01008803 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01008804 if (!cond) {
8805 memprintf(errmsg, "error in condition: %s", *errmsg);
8806 return NULL;
8807 }
8808 break;
8809 }
8810 else {
8811 memprintf(errmsg,
8812 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8813 args[cur_arg]);
8814 return NULL;
8815 }
8816 cur_arg++;
8817 }
8818
8819 if (type == REDIRECT_TYPE_NONE) {
8820 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8821 return NULL;
8822 }
8823
Willy Tarreaube4653b2015-05-28 15:26:58 +02008824 if (dir && type != REDIRECT_TYPE_LOCATION) {
8825 memprintf(errmsg, "response only supports redirect type 'location'");
8826 return NULL;
8827 }
8828
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02008829 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01008830 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008831 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008832
8833 if (!use_fmt) {
8834 /* old-style static redirect rule */
8835 rule->rdr_str = strdup(destination);
8836 rule->rdr_len = strlen(destination);
8837 }
8838 else {
8839 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008840
8841 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8842 * if prefix == "/", we don't want to add anything, otherwise it
8843 * makes it hard for the user to configure a self-redirection.
8844 */
Godbachd9722032014-12-18 15:44:58 +08008845 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008846 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008847 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
8848 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 +01008849 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8850 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008851 return NULL;
8852 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008853 free(curproxy->conf.lfs_file);
8854 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8855 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008856 }
8857 }
8858
Willy Tarreau4baae242012-12-27 12:00:31 +01008859 if (cookie) {
8860 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8861 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8862 */
8863 rule->cookie_len = strlen(cookie);
8864 if (cookie_set) {
8865 rule->cookie_str = malloc(rule->cookie_len + 10);
8866 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8867 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8868 rule->cookie_len += 9;
8869 } else {
8870 rule->cookie_str = malloc(rule->cookie_len + 21);
8871 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8872 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8873 rule->cookie_len += 20;
8874 }
8875 }
8876 rule->type = type;
8877 rule->code = code;
8878 rule->flags = flags;
8879 LIST_INIT(&rule->list);
8880 return rule;
8881
8882 missing_arg:
8883 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8884 return NULL;
8885}
8886
Willy Tarreau8797c062007-05-07 00:55:35 +02008887/************************************************************************/
8888/* The code below is dedicated to ACL parsing and matching */
8889/************************************************************************/
8890
8891
Willy Tarreau14174bc2012-04-16 14:34:04 +02008892/* This function ensures that the prerequisites for an L7 fetch are ready,
8893 * which means that a request or response is ready. If some data is missing,
8894 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008895 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8896 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008897 *
8898 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008899 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8900 * decide whether or not an HTTP message is present ;
8901 * 0 if the requested data cannot be fetched or if it is certain that
8902 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008903 * 1 if an HTTP message is ready
8904 */
James Rosewell91a41cb2015-09-18 17:11:16 +01008905int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008906 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008907{
Willy Tarreau192252e2015-04-04 01:47:55 +02008908 struct http_txn *txn;
8909 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008910
Willy Tarreaube508f12016-03-10 11:47:01 +01008911 /* Note: it is possible that <s> is NULL when called before stream
8912 * initialization (eg: tcp-request connection), so this function is the
8913 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008914 */
Willy Tarreau192252e2015-04-04 01:47:55 +02008915 if (!s)
8916 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01008917
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02008918 if (!s->txn) {
8919 if (unlikely(!http_alloc_txn(s)))
8920 return 0; /* not enough memory */
8921 http_init_txn(s);
8922 }
Willy Tarreau192252e2015-04-04 01:47:55 +02008923 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02008924 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008925
8926 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008927 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008928
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008929 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01008930 /* If the buffer does not leave enough free space at the end,
8931 * we must first realign it.
8932 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02008933 if (ci_head(&s->req) > b_orig(&s->req.buf) &&
8934 ci_head(&s->req) + ci_data(&s->req) > b_wrap(&s->req.buf) - global.tune.maxrewrite)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008935 channel_slow_realign(&s->req, trash.area);
Willy Tarreauaae75e32013-03-29 12:31:49 +01008936
Willy Tarreau14174bc2012-04-16 14:34:04 +02008937 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008938 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008939 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008940
8941 /* Try to decode HTTP request */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008942 if (likely(msg->next < ci_data(&s->req)))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008943 http_msg_analyzer(msg, &txn->hdr_idx);
8944
8945 /* Still no valid request ? */
8946 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008947 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau23752332018-06-15 14:54:53 +02008948 channel_full(&s->req, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008949 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008950 }
8951 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008952 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008953 return 0;
8954 }
8955
8956 /* OK we just got a valid HTTP request. We have some minor
8957 * preparation to perform so that further checks can rely
8958 * on HTTP tests.
8959 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008960
8961 /* If the request was parsed but was too large, we must absolutely
8962 * return an error so that it is not processed. At the moment this
8963 * cannot happen, but if the parsers are to change in the future,
8964 * we want this check to be maintained.
8965 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008966 if (unlikely(ci_head(&s->req) + ci_data(&s->req) >
Willy Tarreauc9fa0482018-07-10 17:43:27 +02008967 b_wrap(&s->req.buf) - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01008968 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008969 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008970 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008971 return 1;
8972 }
8973
Willy Tarreauf37954d2018-06-15 18:31:02 +02008974 txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008975 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02008976 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008977
Willy Tarreau506d0502013-07-06 13:29:24 +02008978 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8979 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008980 }
8981
Willy Tarreau506d0502013-07-06 13:29:24 +02008982 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008983 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008984 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008985
8986 /* otherwise everything's ready for the request */
8987 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008988 else {
8989 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008990 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8991 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008992 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008993 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008994 }
8995
8996 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008997 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008998 return 1;
8999}
Willy Tarreau8797c062007-05-07 00:55:35 +02009000
Willy Tarreau8797c062007-05-07 00:55:35 +02009001/* 1. Check on METHOD
9002 * We use the pre-parsed method if it is known, and store its number as an
9003 * integer. If it is unknown, we use the pointer and the length.
9004 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009005static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009006{
9007 int len, meth;
9008
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009009 len = strlen(text);
9010 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009011
9012 pattern->val.i = meth;
9013 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009014 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009015 pattern->len = len;
9016 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009017 else {
9018 pattern->ptr.str = NULL;
9019 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009020 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009021 return 1;
9022}
9023
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009024/* This function fetches the method of current HTTP request and stores
9025 * it in the global pattern struct as a chunk. There are two possibilities :
9026 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9027 * in <len> and <ptr> is NULL ;
9028 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9029 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009030 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009031 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009032static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009033smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009034{
9035 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009036 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009037
Willy Tarreau24e32d82012-04-23 23:55:44 +02009038 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009039
Willy Tarreaube508f12016-03-10 11:47:01 +01009040 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009041 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009042 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009043 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009044 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009045 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9046 /* ensure the indexes are not affected */
9047 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009048 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009049 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
9050 smp->data.u.meth.str.area = ci_head(txn->req.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009051 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009052 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009053 return 1;
9054}
9055
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009056/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009057static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009058{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009059 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009060 struct pattern_list *lst;
9061 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009062
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009063 list_for_each_entry(lst, &expr->patterns, list) {
9064 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009065
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009066 /* well-known method */
9067 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009068 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009069 return pattern;
9070 else
9071 continue;
9072 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009073
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009074 /* Other method, we must compare the strings */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009075 if (pattern->len != smp->data.u.meth.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009076 continue;
9077
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009078 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009079 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.area, smp->data.u.meth.str.data) == 0) ||
9080 (!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 +01009081 return pattern;
9082 }
9083 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009084}
9085
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009086static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009087smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009088{
Willy Tarreaube508f12016-03-10 11:47:01 +01009089 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009090 char *ptr;
9091 int len;
9092
Willy Tarreauc0239e02012-04-16 14:42:55 +02009093 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009094
Willy Tarreaube508f12016-03-10 11:47:01 +01009095 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009096 len = txn->req.sl.rq.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009097 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009098
9099 while ((len-- > 0) && (*ptr++ != '/'));
9100 if (len <= 0)
9101 return 0;
9102
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009103 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009104 smp->data.u.str.area = ptr;
9105 smp->data.u.str.data = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009106
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009107 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009108 return 1;
9109}
9110
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009111static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009112smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009113{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009114 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009115 char *ptr;
9116 int len;
9117
Willy Tarreauc0239e02012-04-16 14:42:55 +02009118 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009119
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009120 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009121 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9122 return 0;
9123
Willy Tarreau8797c062007-05-07 00:55:35 +02009124 len = txn->rsp.sl.st.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009125 ptr = ci_head(txn->rsp.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009126
9127 while ((len-- > 0) && (*ptr++ != '/'));
9128 if (len <= 0)
9129 return 0;
9130
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009131 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009132 smp->data.u.str.area = ptr;
9133 smp->data.u.str.data = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009134
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009135 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009136 return 1;
9137}
9138
9139/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009140static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009141smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009142{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009143 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009144 char *ptr;
9145 int len;
9146
Willy Tarreauc0239e02012-04-16 14:42:55 +02009147 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009148
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009149 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009150 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9151 return 0;
9152
Willy Tarreau8797c062007-05-07 00:55:35 +02009153 len = txn->rsp.sl.st.c_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009154 ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009155
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009156 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009157 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009158 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009159 return 1;
9160}
9161
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009162static int
9163smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9164{
9165 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9166 return 0;
9167
9168 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009169 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009170 return 0;
9171 smp->strm->unique_id[0] = '\0';
9172 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009173 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009174 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9175
9176 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009177 smp->data.u.str.area = smp->strm->unique_id;
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009178 smp->flags = SMP_F_CONST;
9179 return 1;
9180}
9181
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009182/* Returns a string block containing all headers including the
9183 * empty line wich separes headers from the body. This is useful
9184 * form some headers analysis.
9185 */
9186static int
9187smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9188{
9189 struct http_msg *msg;
9190 struct hdr_idx *idx;
9191 struct http_txn *txn;
9192
9193 CHECK_HTTP_MESSAGE_FIRST();
9194
9195 txn = smp->strm->txn;
9196 idx = &txn->hdr_idx;
9197 msg = &txn->req;
9198
9199 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009200 smp->data.u.str.area = ci_head(msg->chn) + hdr_idx_first_pos(idx);
9201 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Willy Tarreauf37954d2018-06-15 18:31:02 +02009202 (ci_head(msg->chn)[msg->eoh] == '\r');
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009203
9204 return 1;
9205}
9206
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009207/* Returns the header request in a length/value encoded format.
9208 * This is useful for exchanges with the SPOE.
9209 *
9210 * A "length value" is a multibyte code encoding numbers. It uses the
9211 * SPOE format. The encoding is the following:
9212 *
9213 * Each couple "header name" / "header value" is composed
9214 * like this:
9215 * "length value" "header name bytes"
9216 * "length value" "header value bytes"
9217 * When the last header is reached, the header name and the header
9218 * value are empty. Their length are 0
9219 */
9220static int
9221smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9222{
9223 struct http_msg *msg;
Willy Tarreau83061a82018-07-13 11:56:34 +02009224 struct buffer *temp;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009225 struct hdr_idx *idx;
9226 const char *cur_ptr, *cur_next, *p;
9227 int old_idx, cur_idx;
9228 struct hdr_idx_elem *cur_hdr;
9229 const char *hn, *hv;
9230 int hnl, hvl;
9231 int ret;
9232 struct http_txn *txn;
9233 char *buf;
9234 char *end;
9235
9236 CHECK_HTTP_MESSAGE_FIRST();
9237
9238 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009239 buf = temp->area;
9240 end = temp->area + temp->size;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009241
9242 txn = smp->strm->txn;
9243 idx = &txn->hdr_idx;
9244 msg = &txn->req;
9245
9246 /* Build array of headers. */
9247 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009248 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx);
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009249 while (1) {
9250 cur_idx = idx->v[old_idx].next;
9251 if (!cur_idx)
9252 break;
9253 old_idx = cur_idx;
9254
9255 cur_hdr = &idx->v[cur_idx];
9256 cur_ptr = cur_next;
9257 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9258
9259 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9260 * and the next header starts at cur_next. We'll check
9261 * this header in the list as well as against the default
9262 * rule.
9263 */
9264
9265 /* look for ': *'. */
9266 hn = cur_ptr;
9267 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9268 if (p >= cur_ptr+cur_hdr->len)
9269 continue;
9270 hnl = p - hn;
9271 p++;
9272 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9273 p++;
9274 if (p >= cur_ptr + cur_hdr->len)
9275 continue;
9276 hv = p;
9277 hvl = cur_ptr + cur_hdr->len-p;
9278
9279 /* encode the header name. */
9280 ret = encode_varint(hnl, &buf, end);
9281 if (ret == -1)
9282 return 0;
9283 if (buf + hnl > end)
9284 return 0;
9285 memcpy(buf, hn, hnl);
9286 buf += hnl;
9287
9288 /* encode and copy the value. */
9289 ret = encode_varint(hvl, &buf, end);
9290 if (ret == -1)
9291 return 0;
9292 if (buf + hvl > end)
9293 return 0;
9294 memcpy(buf, hv, hvl);
9295 buf += hvl;
9296 }
9297
9298 /* encode the end of the header list with empty
9299 * header name and header value.
9300 */
9301 ret = encode_varint(0, &buf, end);
9302 if (ret == -1)
9303 return 0;
9304 ret = encode_varint(0, &buf, end);
9305 if (ret == -1)
9306 return 0;
9307
9308 /* Initialise sample data which will be filled. */
9309 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009310 smp->data.u.str.area = temp->area;
9311 smp->data.u.str.data = buf - temp->area;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009312 smp->data.u.str.size = temp->size;
9313
9314 return 1;
9315}
9316
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009317/* returns the longest available part of the body. This requires that the body
9318 * has been waited for using http-buffer-request.
9319 */
9320static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009321smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009322{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009323 struct http_msg *msg;
9324 unsigned long len;
9325 unsigned long block1;
9326 char *body;
Willy Tarreau83061a82018-07-13 11:56:34 +02009327 struct buffer *temp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009328
9329 CHECK_HTTP_MESSAGE_FIRST();
9330
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009331 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009332 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009333 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009334 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009335
9336 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +02009337 body = c_ptr(msg->chn, -http_data_rewind(msg));
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009338
9339 block1 = len;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009340 if (block1 > b_wrap(&msg->chn->buf) - body)
9341 block1 = b_wrap(&msg->chn->buf) - body;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009342
9343 if (block1 == len) {
9344 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009345 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009346 smp->data.u.str.area = body;
9347 smp->data.u.str.data = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009348 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9349 }
9350 else {
9351 /* buffer is wrapped, we need to defragment it */
9352 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009353 memcpy(temp->area, body, block1);
9354 memcpy(temp->area + block1, b_orig(&msg->chn->buf),
9355 len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009356 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009357 smp->data.u.str.area = temp->area;
9358 smp->data.u.str.data = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009359 smp->flags = SMP_F_VOL_TEST;
9360 }
9361 return 1;
9362}
9363
9364
9365/* returns the available length of the body. This requires that the body
9366 * has been waited for using http-buffer-request.
9367 */
9368static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009369smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009370{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009371 struct http_msg *msg;
9372
9373 CHECK_HTTP_MESSAGE_FIRST();
9374
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009375 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009376 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009377 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009378 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009379
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009380 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009381 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009382
9383 smp->flags = SMP_F_VOL_TEST;
9384 return 1;
9385}
9386
9387
9388/* returns the advertised length of the body, or the advertised size of the
9389 * chunks available in the buffer. This requires that the body has been waited
9390 * for using http-buffer-request.
9391 */
9392static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009393smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009394{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009395 struct http_msg *msg;
9396
9397 CHECK_HTTP_MESSAGE_FIRST();
9398
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009399 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009400 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009401 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009402 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009403
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009404 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009405 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009406
9407 smp->flags = SMP_F_VOL_TEST;
9408 return 1;
9409}
9410
9411
Willy Tarreau8797c062007-05-07 00:55:35 +02009412/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009413static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009414smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009415{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009416 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009417
Willy Tarreauc0239e02012-04-16 14:42:55 +02009418 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009419
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009420 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009421 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009422 smp->data.u.str.data = txn->req.sl.rq.u_l;
9423 smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009424 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009425 return 1;
9426}
9427
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009428static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009429smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009430{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009431 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009432 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009433
Willy Tarreauc0239e02012-04-16 14:42:55 +02009434 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009435
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009436 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009437 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 +02009438 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009439 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009440
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009441 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009442 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009443 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009444 return 1;
9445}
9446
9447static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009448smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009449{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009450 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009451 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009452
Willy Tarreauc0239e02012-04-16 14:42:55 +02009453 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009454
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009455 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009456 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 +02009457 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9458 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009459
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009460 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009461 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009462 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009463 return 1;
9464}
9465
Willy Tarreau185b5c42012-04-26 15:11:51 +02009466/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9467 * Accepts an optional argument of type string containing the header field name,
9468 * and an optional argument of type signed or unsigned integer to request an
9469 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009470 * headers are considered from the first one. It does not stop on commas and
9471 * returns full lines instead (useful for User-Agent or Date for example).
9472 */
9473static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009474smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009475{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009476 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009477 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009478 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009479 int occ = 0;
9480 const char *name_str = NULL;
9481 int name_len = 0;
9482
9483 if (!ctx) {
9484 /* first call */
9485 ctx = &static_hdr_ctx;
9486 ctx->idx = 0;
9487 smp->ctx.a[0] = ctx;
9488 }
9489
9490 if (args) {
9491 if (args[0].type != ARGT_STR)
9492 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009493 name_str = args[0].data.str.area;
9494 name_len = args[0].data.str.data;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009495
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009496 if (args[1].type == ARGT_SINT)
9497 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009498 }
9499
9500 CHECK_HTTP_MESSAGE_FIRST();
9501
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009502 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009503 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 +02009504
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009505 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9506 /* search for header from the beginning */
9507 ctx->idx = 0;
9508
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009509 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009510 /* no explicit occurrence and single fetch => last header by default */
9511 occ = -1;
9512
9513 if (!occ)
9514 /* prepare to report multiple occurrences for ACL fetches */
9515 smp->flags |= SMP_F_NOT_LAST;
9516
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009517 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009518 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009519 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 +02009520 return 1;
9521
9522 smp->flags &= ~SMP_F_NOT_LAST;
9523 return 0;
9524}
9525
9526/* 6. Check on HTTP header count. The number of occurrences is returned.
9527 * Accepts exactly 1 argument of type string. It does not stop on commas and
9528 * returns full lines instead (useful for User-Agent or Date for example).
9529 */
9530static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009531smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009532{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009533 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009534 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009535 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009536 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009537 const char *name = NULL;
9538 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009539
Willy Tarreau601a4d12015-04-01 19:16:09 +02009540 if (args && args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009541 name = args->data.str.area;
9542 len = args->data.str.data;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009543 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009544
9545 CHECK_HTTP_MESSAGE_FIRST();
9546
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009547 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009548 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 +02009549
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009550 ctx.idx = 0;
9551 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009552 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009553 cnt++;
9554
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009555 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009556 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009557 smp->flags = SMP_F_VOL_HDR;
9558 return 1;
9559}
9560
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009561static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009562smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009563{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009564 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009565 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009566 const struct http_msg *msg;
Willy Tarreau83061a82018-07-13 11:56:34 +02009567 struct buffer *temp;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009568 char del = ',';
9569
9570 if (args && args->type == ARGT_STR)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009571 del = *args[0].data.str.area;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009572
9573 CHECK_HTTP_MESSAGE_FIRST();
9574
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009575 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009576 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 +02009577
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009578 temp = get_trash_chunk();
9579
9580 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009581 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009582 if (temp->data)
9583 temp->area[temp->data++] = del;
9584 memcpy(temp->area + temp->data, ctx.line, ctx.del);
9585 temp->data += ctx.del;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009586 }
9587
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009588 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009589 smp->data.u.str.area = temp->area;
9590 smp->data.u.str.data = temp->data;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009591 smp->flags = SMP_F_VOL_HDR;
9592 return 1;
9593}
9594
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009595/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9596 * Accepts an optional argument of type string containing the header field name,
9597 * and an optional argument of type signed or unsigned integer to request an
9598 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009599 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009600 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009601static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009602smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009603{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009604 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009605 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009606 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009607 int occ = 0;
9608 const char *name_str = NULL;
9609 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009610
Willy Tarreaua890d072013-04-02 12:01:06 +02009611 if (!ctx) {
9612 /* first call */
9613 ctx = &static_hdr_ctx;
9614 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009615 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009616 }
9617
Willy Tarreau185b5c42012-04-26 15:11:51 +02009618 if (args) {
9619 if (args[0].type != ARGT_STR)
9620 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009621 name_str = args[0].data.str.area;
9622 name_len = args[0].data.str.data;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009623
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009624 if (args[1].type == ARGT_SINT)
9625 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009626 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009627
Willy Tarreaue333ec92012-04-16 16:26:40 +02009628 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009629
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009630 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009631 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 +02009632
Willy Tarreau185b5c42012-04-26 15:11:51 +02009633 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009634 /* search for header from the beginning */
9635 ctx->idx = 0;
9636
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009637 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +02009638 /* no explicit occurrence and single fetch => last header by default */
9639 occ = -1;
9640
9641 if (!occ)
9642 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009643 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009644
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009645 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009646 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009647 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 +02009648 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009649
Willy Tarreau37406352012-04-23 16:16:37 +02009650 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009651 return 0;
9652}
9653
Willy Tarreauc11416f2007-06-17 16:58:38 +02009654/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009655 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009656 */
9657static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009658smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009659{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009660 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009661 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009662 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009663 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009664 const char *name = NULL;
9665 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02009666
Willy Tarreau601a4d12015-04-01 19:16:09 +02009667 if (args && args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009668 name = args->data.str.area;
9669 len = args->data.str.data;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009670 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009671
Willy Tarreaue333ec92012-04-16 16:26:40 +02009672 CHECK_HTTP_MESSAGE_FIRST();
9673
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009674 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009675 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 +02009676
Willy Tarreau33a7e692007-06-10 19:45:56 +02009677 ctx.idx = 0;
9678 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009679 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009680 cnt++;
9681
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009682 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009683 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009684 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009685 return 1;
9686}
9687
Willy Tarreau185b5c42012-04-26 15:11:51 +02009688/* Fetch an HTTP header's integer value. The integer value is returned. It
9689 * takes a mandatory argument of type string and an optional one of type int
9690 * to designate a specific occurrence. It returns an unsigned integer, which
9691 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009692 */
9693static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009694smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009695{
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009696 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009697
Willy Tarreauf853c462012-04-23 18:53:56 +02009698 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009699 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009700 smp->data.u.sint = strl2ic(smp->data.u.str.area,
9701 smp->data.u.str.data);
Willy Tarreauf853c462012-04-23 18:53:56 +02009702 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009703
Willy Tarreaud53e2422012-04-16 17:21:11 +02009704 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009705}
9706
Cyril Bonté69fa9922012-10-25 00:01:06 +02009707/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9708 * and an optional one of type int to designate a specific occurrence.
9709 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009710 */
9711static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009712smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +02009713{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009714 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009715
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009716 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009717 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009718 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009719 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009720 } else {
Willy Tarreau83061a82018-07-13 11:56:34 +02009721 struct buffer *temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009722 if (smp->data.u.str.data < temp->size - 1) {
9723 memcpy(temp->area, smp->data.u.str.area,
9724 smp->data.u.str.data);
9725 temp->area[smp->data.u.str.data] = '\0';
9726 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009727 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009728 break;
9729 }
9730 }
9731 }
9732
Willy Tarreaud53e2422012-04-16 17:21:11 +02009733 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009734 if (!(smp->flags & SMP_F_NOT_LAST))
9735 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009736 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009737 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009738}
9739
Willy Tarreau737b0c12007-06-10 21:28:46 +02009740/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9741 * the first '/' after the possible hostname, and ends before the possible '?'.
9742 */
9743static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009744smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009745{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009746 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009747 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009748
Willy Tarreauc0239e02012-04-16 14:42:55 +02009749 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009750
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009751 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009752 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +02009753 ptr = http_txn_get_path(txn);
Willy Tarreau21d2af32008-02-14 20:25:24 +01009754 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009755 return 0;
9756
9757 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009758 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009759 smp->data.u.str.area = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009760
9761 while (ptr < end && *ptr != '?')
9762 ptr++;
9763
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009764 smp->data.u.str.data = ptr - smp->data.u.str.area;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009765 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009766 return 1;
9767}
9768
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009769/* This produces a concatenation of the first occurrence of the Host header
9770 * followed by the path component if it begins with a slash ('/'). This means
9771 * that '*' will not be added, resulting in exactly the first Host entry.
9772 * If no Host header is found, then the path is returned as-is. The returned
9773 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009774 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009775 */
9776static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009777smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009778{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009779 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009780 char *ptr, *end, *beg;
9781 struct hdr_ctx ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02009782 struct buffer *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009783
9784 CHECK_HTTP_MESSAGE_FIRST();
9785
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009786 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009787 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009788 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009789 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009790
9791 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +02009792 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009793 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009794 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009795 smp->data.u.str.area = temp->area;
9796 smp->data.u.str.data = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009797
9798 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009799 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +02009800 beg = http_txn_get_path(txn);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009801 if (!beg)
9802 beg = end;
9803
9804 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9805
9806 if (beg < ptr && *beg == '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009807 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
9808 ptr - beg);
9809 smp->data.u.str.data += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009810 }
9811
9812 smp->flags = SMP_F_VOL_1ST;
9813 return 1;
9814}
9815
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009816/* This produces a 32-bit hash of the concatenation of the first occurrence of
9817 * the Host header followed by the path component if it begins with a slash ('/').
9818 * This means that '*' will not be added, resulting in exactly the first Host
9819 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009820 * is hashed using the path hash followed by a full avalanche hash and provides a
9821 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009822 * high-traffic sites without having to store whole paths.
9823 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +02009824int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009825smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009826{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009827 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009828 struct hdr_ctx ctx;
9829 unsigned int hash = 0;
9830 char *ptr, *beg, *end;
9831 int len;
9832
9833 CHECK_HTTP_MESSAGE_FIRST();
9834
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009835 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009836 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009837 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009838 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9839 ptr = ctx.line + ctx.val;
9840 len = ctx.vlen;
9841 while (len--)
9842 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9843 }
9844
9845 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009846 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +02009847 beg = http_txn_get_path(txn);
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009848 if (!beg)
9849 beg = end;
9850
9851 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9852
9853 if (beg < ptr && *beg == '/') {
9854 while (beg < ptr)
9855 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9856 }
9857 hash = full_hash(hash);
9858
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009859 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009860 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009861 smp->flags = SMP_F_VOL_1ST;
9862 return 1;
9863}
9864
Willy Tarreau4a550602012-12-09 14:53:32 +01009865/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009866 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9867 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9868 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009869 * that in environments where IPv6 is insignificant, truncating the output to
9870 * 8 bytes would still work.
9871 */
9872static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009873smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +01009874{
Willy Tarreau83061a82018-07-13 11:56:34 +02009875 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009876 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009877
9878 if (!cli_conn)
9879 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009880
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009881 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +01009882 return 0;
9883
Willy Tarreau47ca5452012-12-23 20:22:19 +01009884 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009885 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
9886 temp->data += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +01009887
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009888 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009889 case AF_INET:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009890 memcpy(temp->area + temp->data,
9891 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
9892 4);
9893 temp->data += 4;
Willy Tarreau4a550602012-12-09 14:53:32 +01009894 break;
9895 case AF_INET6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009896 memcpy(temp->area + temp->data,
9897 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
9898 16);
9899 temp->data += 16;
Willy Tarreau4a550602012-12-09 14:53:32 +01009900 break;
9901 default:
9902 return 0;
9903 }
9904
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009905 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009906 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +01009907 return 1;
9908}
9909
Willy Tarreau49ad95c2015-01-19 15:06:26 +01009910/* Extracts the query string, which comes after the question mark '?'. If no
9911 * question mark is found, nothing is returned. Otherwise it returns a sample
9912 * of type string carrying the whole query string.
9913 */
9914static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009915smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +01009916{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009917 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +01009918 char *ptr, *end;
9919
9920 CHECK_HTTP_MESSAGE_FIRST();
9921
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009922 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009923 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Willy Tarreau49ad95c2015-01-19 15:06:26 +01009924 end = ptr + txn->req.sl.rq.u_l;
9925
9926 /* look up the '?' */
9927 do {
9928 if (ptr == end)
9929 return 0;
9930 } while (*ptr++ != '?');
9931
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009932 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009933 smp->data.u.str.area = ptr;
9934 smp->data.u.str.data = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +01009935 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
9936 return 1;
9937}
9938
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009939static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009940smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009941{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009942 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9943 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9944 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009945
Willy Tarreau24e32d82012-04-23 23:55:44 +02009946 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009947
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009948 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009949 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009950 return 1;
9951}
9952
Willy Tarreau7f18e522010-10-22 20:04:13 +02009953/* return a valid test if the current request is the first one on the connection */
9954static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009955smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009956{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009957 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009958 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009959 return 1;
9960}
9961
Willy Tarreau34db1082012-04-19 17:16:54 +02009962/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009963static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009964smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009965{
9966
Willy Tarreau24e32d82012-04-23 23:55:44 +02009967 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009968 return 0;
9969
Willy Tarreauc0239e02012-04-16 14:42:55 +02009970 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009971
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009972 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009973 return 0;
9974
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009975 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009976 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009977 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009978 return 1;
9979}
Willy Tarreau8797c062007-05-07 00:55:35 +02009980
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009981/* Accepts exactly 1 argument of type userlist */
9982static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009983smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009984{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009985 if (!args || args->type != ARGT_USR)
9986 return 0;
9987
9988 CHECK_HTTP_MESSAGE_FIRST();
9989
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009990 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009991 return 0;
9992
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009993 /* if the user does not belong to the userlist or has a wrong password,
9994 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009995 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009996 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009997 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
9998 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009999 return 0;
10000
10001 /* pat_match_auth() will need the user list */
10002 smp->ctx.a[0] = args->data.usr;
10003
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010004 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010005 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010006 smp->data.u.str.area = smp->strm->txn->auth.user;
10007 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010008
10009 return 1;
10010}
10011
William Lallemanda43ba4e2014-01-28 18:14:25 +010010012/* Fetch a captured HTTP request header. The index is the position of
10013 * the "capture" option in the configuration file
10014 */
10015static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010016smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010017{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010018 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010019 int idx;
10020
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010021 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010022 return 0;
10023
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010024 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010025
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010026 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 +010010027 return 0;
10028
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010029 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010030 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010031 smp->data.u.str.area = smp->strm->req_cap[idx];
10032 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010033
10034 return 1;
10035}
10036
10037/* Fetch a captured HTTP response header. The index is the position of
10038 * the "capture" option in the configuration file
10039 */
10040static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010041smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010042{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010043 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010044 int idx;
10045
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010046 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010047 return 0;
10048
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010049 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010050
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010051 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 +010010052 return 0;
10053
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010054 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010055 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010056 smp->data.u.str.area = smp->strm->res_cap[idx];
10057 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010058
10059 return 1;
10060}
10061
William Lallemand65ad6e12014-01-31 15:08:02 +010010062/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10063static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010064smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010065{
Willy Tarreau83061a82018-07-13 11:56:34 +020010066 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010067 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010068 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010069
Willy Tarreau15e91e12015-04-04 00:52:09 +020010070 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010071 return 0;
10072
William Lallemand96a77852014-02-05 00:30:02 +010010073 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010074
William Lallemand96a77852014-02-05 00:30:02 +010010075 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10076 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010077
William Lallemand96a77852014-02-05 00:30:02 +010010078 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010079 temp->area = txn->uri;
10080 temp->data = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010081 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010082 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010083 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010084
10085 return 1;
10086
10087}
10088
10089/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10090static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010091smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010092{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010093 struct http_txn *txn = smp->strm->txn;
Willy Tarreau6b952c82018-09-10 17:45:34 +020010094 struct ist path;
10095 const char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010096
Willy Tarreau15e91e12015-04-04 00:52:09 +020010097 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010098 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010099
William Lallemand65ad6e12014-01-31 15:08:02 +010010100 ptr = txn->uri;
10101
10102 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10103 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010104
William Lallemand65ad6e12014-01-31 15:08:02 +010010105 if (!*ptr)
10106 return 0;
10107
10108 ptr++; /* skip the space */
10109
Willy Tarreau6b952c82018-09-10 17:45:34 +020010110 path = http_get_path(ist(ptr));
10111 if (!path.ptr)
William Lallemand65ad6e12014-01-31 15:08:02 +010010112 return 0;
William Lallemand65ad6e12014-01-31 15:08:02 +010010113
Willy Tarreau6b952c82018-09-10 17:45:34 +020010114 smp->data.u.str.area = path.ptr;
10115 smp->data.u.str.data = path.len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010116 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010117 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010118
10119 return 1;
10120}
10121
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010122/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10123 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10124 */
10125static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010126smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010127{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010128 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010129
Willy Tarreau15e91e12015-04-04 00:52:09 +020010130 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010131 return 0;
10132
10133 if (txn->req.flags & HTTP_MSGF_VER_11)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010134 smp->data.u.str.area = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010135 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010136 smp->data.u.str.area = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010137
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010138 smp->data.u.str.data = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010139 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010140 smp->flags = SMP_F_CONST;
10141 return 1;
10142
10143}
10144
10145/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10146 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10147 */
10148static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010149smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010150{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010151 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010152
Willy Tarreau15e91e12015-04-04 00:52:09 +020010153 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010154 return 0;
10155
10156 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010157 smp->data.u.str.area = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010158 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010159 smp->data.u.str.area = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010160
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010161 smp->data.u.str.data = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010162 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010163 smp->flags = SMP_F_CONST;
10164 return 1;
10165
10166}
10167
William Lallemand65ad6e12014-01-31 15:08:02 +010010168
Willy Tarreaue333ec92012-04-16 16:26:40 +020010169/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010170 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010171 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010172 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010173 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010174 * Accepts exactly 1 argument of type string. If the input options indicate
10175 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010176 * The returned sample is of type CSTR. Can be used to parse cookies in other
10177 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010178 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010179int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010180{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010181 struct http_txn *txn;
10182 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010183 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010184 const struct http_msg *msg;
10185 const char *hdr_name;
10186 int hdr_name_len;
10187 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010188 int occ = 0;
10189 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010190
Willy Tarreau24e32d82012-04-23 23:55:44 +020010191 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010192 return 0;
10193
Willy Tarreaua890d072013-04-02 12:01:06 +020010194 if (!ctx) {
10195 /* first call */
10196 ctx = &static_hdr_ctx;
10197 ctx->idx = 0;
10198 smp->ctx.a[2] = ctx;
10199 }
10200
Willy Tarreaue333ec92012-04-16 16:26:40 +020010201 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010202
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010203 txn = smp->strm->txn;
10204 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010205
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010206 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010207 msg = &txn->req;
10208 hdr_name = "Cookie";
10209 hdr_name_len = 6;
10210 } else {
10211 msg = &txn->rsp;
10212 hdr_name = "Set-Cookie";
10213 hdr_name_len = 10;
10214 }
10215
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010216 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010217 /* no explicit occurrence and single fetch => last cookie by default */
10218 occ = -1;
10219
10220 /* OK so basically here, either we want only one value and it's the
10221 * last one, or we want to iterate over all of them and we fetch the
10222 * next one.
10223 */
10224
Willy Tarreauf37954d2018-06-15 18:31:02 +020010225 sol = ci_head(msg->chn);
Willy Tarreau37406352012-04-23 16:16:37 +020010226 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010227 /* search for the header from the beginning, we must first initialize
10228 * the search parameters.
10229 */
Willy Tarreau37406352012-04-23 16:16:37 +020010230 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010231 ctx->idx = 0;
10232 }
10233
Willy Tarreau28376d62012-04-26 21:26:10 +020010234 smp->flags |= SMP_F_VOL_HDR;
10235
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010236 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010237 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10238 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010239 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10240 goto out;
10241
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010242 if (ctx->vlen < args->data.str.data + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010243 continue;
10244
Willy Tarreau37406352012-04-23 16:16:37 +020010245 smp->ctx.a[0] = ctx->line + ctx->val;
10246 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010247 }
10248
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010249 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010250 smp->flags |= SMP_F_CONST;
Willy Tarreauab813a42018-09-10 18:41:28 +020010251 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
10252 args->data.str.area, args->data.str.data,
10253 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
10254 &smp->data.u.str.area, &smp->data.u.str.data);
Willy Tarreau37406352012-04-23 16:16:37 +020010255 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010256 found = 1;
10257 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010258 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010259 smp->flags |= SMP_F_NOT_LAST;
10260 return 1;
10261 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010262 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010263 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010264 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010265 /* all cookie headers and values were scanned. If we're looking for the
10266 * last occurrence, we may return it now.
10267 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010268 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010269 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010270 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010271}
10272
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010273/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010274 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010275 * multiple cookies may be parsed on the same line. The returned sample is of
10276 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010277 */
10278static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010279smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010280{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010281 struct http_txn *txn;
10282 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010283 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010284 const struct http_msg *msg;
10285 const char *hdr_name;
10286 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010287 int cnt;
10288 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010289 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010290
Willy Tarreau24e32d82012-04-23 23:55:44 +020010291 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010292 return 0;
10293
Willy Tarreaue333ec92012-04-16 16:26:40 +020010294 CHECK_HTTP_MESSAGE_FIRST();
10295
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010296 txn = smp->strm->txn;
10297 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010298
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010299 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010300 msg = &txn->req;
10301 hdr_name = "Cookie";
10302 hdr_name_len = 6;
10303 } else {
10304 msg = &txn->rsp;
10305 hdr_name = "Set-Cookie";
10306 hdr_name_len = 10;
10307 }
10308
Willy Tarreauf37954d2018-06-15 18:31:02 +020010309 sol = ci_head(msg->chn);
Willy Tarreau46787ed2012-04-11 17:28:40 +020010310 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010311 ctx.idx = 0;
10312 cnt = 0;
10313
10314 while (1) {
10315 /* Note: val_beg == NULL every time we need to fetch a new header */
10316 if (!val_beg) {
10317 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10318 break;
10319
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010320 if (ctx.vlen < args->data.str.data + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010321 continue;
10322
10323 val_beg = ctx.line + ctx.val;
10324 val_end = val_beg + ctx.vlen;
10325 }
10326
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010327 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010328 smp->flags |= SMP_F_CONST;
Willy Tarreauab813a42018-09-10 18:41:28 +020010329 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
10330 args->data.str.area, args->data.str.data,
10331 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
10332 &smp->data.u.str.area, &smp->data.u.str.data))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010333 cnt++;
10334 }
10335 }
10336
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010337 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010338 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010339 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010340 return 1;
10341}
10342
Willy Tarreau51539362012-05-08 12:46:28 +020010343/* Fetch an cookie's integer value. The integer value is returned. It
10344 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10345 */
10346static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010347smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010348{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010349 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010350
10351 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010352 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010353 smp->data.u.sint = strl2ic(smp->data.u.str.area,
10354 smp->data.u.str.data);
Willy Tarreau51539362012-05-08 12:46:28 +020010355 }
10356
10357 return ret;
10358}
10359
Willy Tarreau8797c062007-05-07 00:55:35 +020010360/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010361/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010362/************************************************************************/
10363
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010364/* This scans a URL-encoded query string. It takes an optionally wrapping
10365 * string whose first contigous chunk has its beginning in ctx->a[0] and end
10366 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
10367 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010368 */
David Cournapeau16023ee2010-12-23 20:55:41 +090010369static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010370smp_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 +090010371{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010372 const char *vstart, *vend;
Willy Tarreau83061a82018-07-13 11:56:34 +020010373 struct buffer *temp;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010374 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020010375
Willy Tarreauab813a42018-09-10 18:41:28 +020010376 if (!http_find_next_url_param(chunks, name, name_len,
10377 &vstart, &vend, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010378 return 0;
10379
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010380 /* Create sample. If the value is contiguous, return the pointer as CONST,
10381 * if the value is wrapped, copy-it in a buffer.
10382 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010383 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010384 if (chunks[2] &&
10385 vstart >= chunks[0] && vstart <= chunks[1] &&
10386 vend >= chunks[2] && vend <= chunks[3]) {
10387 /* Wrapped case. */
10388 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010389 memcpy(temp->area, vstart, chunks[1] - vstart);
10390 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
10391 vend - chunks[2]);
10392 smp->data.u.str.area = temp->area;
10393 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010394 } else {
10395 /* Contiguous case. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010396 smp->data.u.str.area = (char *)vstart;
10397 smp->data.u.str.data = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010398 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10399 }
10400
10401 /* Update context, check wrapping. */
10402 chunks[0] = vend;
10403 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
10404 chunks[1] = chunks[3];
10405 chunks[2] = NULL;
10406 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010407
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010408 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010409 smp->flags |= SMP_F_NOT_LAST;
10410
David Cournapeau16023ee2010-12-23 20:55:41 +090010411 return 1;
10412}
10413
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010414/* This function iterates over each parameter of the query string. It uses
10415 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010416 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
10417 * An optional parameter name is passed in args[0], otherwise any parameter is
10418 * considered. It supports an optional delimiter argument for the beginning of
10419 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010420 */
10421static int
10422smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
10423{
10424 struct http_msg *msg;
10425 char delim = '?';
10426 const char *name;
10427 int name_len;
10428
Dragan Dosen26f77e52015-05-25 10:02:11 +020010429 if (!args ||
10430 (args[0].type && args[0].type != ARGT_STR) ||
10431 (args[1].type && args[1].type != ARGT_STR))
10432 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010433
Dragan Dosen26f77e52015-05-25 10:02:11 +020010434 name = "";
10435 name_len = 0;
10436 if (args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010437 name = args->data.str.area;
10438 name_len = args->data.str.data;
Dragan Dosen26f77e52015-05-25 10:02:11 +020010439 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010440
Dragan Dosen26f77e52015-05-25 10:02:11 +020010441 if (args[1].type)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010442 delim = *args[1].data.str.area;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010443
Dragan Dosen26f77e52015-05-25 10:02:11 +020010444 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010445 CHECK_HTTP_MESSAGE_FIRST();
10446
10447 msg = &smp->strm->txn->req;
10448
Willy Tarreauab813a42018-09-10 18:41:28 +020010449 smp->ctx.a[0] = http_find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
10450 msg->sl.rq.u_l, delim);
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010451 if (!smp->ctx.a[0])
10452 return 0;
10453
Willy Tarreauf37954d2018-06-15 18:31:02 +020010454 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010455
10456 /* Assume that the context is filled with NULL pointer
10457 * before the first call.
10458 * smp->ctx.a[2] = NULL;
10459 * smp->ctx.a[3] = NULL;
10460 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010461 }
10462
10463 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
10464}
10465
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010466/* This function iterates over each parameter of the body. This requires
10467 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010468 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
10469 * contigous part of the body, and optionally ctx->a[2..3] to reference the
10470 * optional second part if the body wraps at the end of the buffer. An optional
10471 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010472 */
10473static int
10474smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
10475{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010476 struct http_msg *msg;
10477 unsigned long len;
10478 unsigned long block1;
10479 char *body;
10480 const char *name;
10481 int name_len;
10482
10483 if (!args || (args[0].type && args[0].type != ARGT_STR))
10484 return 0;
10485
10486 name = "";
10487 name_len = 0;
10488 if (args[0].type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010489 name = args[0].data.str.area;
10490 name_len = args[0].data.str.data;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010491 }
10492
10493 if (!smp->ctx.a[0]) { // first call, find the query string
10494 CHECK_HTTP_MESSAGE_FIRST();
10495
10496 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010497 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010498 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010499 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010500
10501 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +020010502 body = c_ptr(msg->chn, -http_data_rewind(msg));
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010503
10504 block1 = len;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020010505 if (block1 > b_wrap(&msg->chn->buf) - body)
10506 block1 = b_wrap(&msg->chn->buf) - body;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010507
10508 if (block1 == len) {
10509 /* buffer is not wrapped (or empty) */
10510 smp->ctx.a[0] = body;
10511 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010512
10513 /* Assume that the context is filled with NULL pointer
10514 * before the first call.
10515 * smp->ctx.a[2] = NULL;
10516 * smp->ctx.a[3] = NULL;
10517 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010518 }
10519 else {
10520 /* buffer is wrapped, we need to defragment it */
10521 smp->ctx.a[0] = body;
10522 smp->ctx.a[1] = body + block1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020010523 smp->ctx.a[2] = b_orig(&msg->chn->buf);
10524 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010525 }
10526 }
10527 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
10528}
10529
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010530/* Return the signed integer value for the specified url parameter (see url_param
10531 * above).
10532 */
10533static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010534smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010535{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010536 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010537
10538 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010539 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010540 smp->data.u.sint = strl2ic(smp->data.u.str.area,
10541 smp->data.u.str.data);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010542 }
10543
10544 return ret;
10545}
10546
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010547/* This produces a 32-bit hash of the concatenation of the first occurrence of
10548 * the Host header followed by the path component if it begins with a slash ('/').
10549 * This means that '*' will not be added, resulting in exactly the first Host
10550 * entry. If no Host header is found, then the path is used. The resulting value
10551 * is hashed using the url hash followed by a full avalanche hash and provides a
10552 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10553 * high-traffic sites without having to store whole paths.
10554 * this differs from the base32 functions in that it includes the url parameters
10555 * as well as the path
10556 */
10557static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010558smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010559{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010560 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010561 struct hdr_ctx ctx;
10562 unsigned int hash = 0;
10563 char *ptr, *beg, *end;
10564 int len;
10565
10566 CHECK_HTTP_MESSAGE_FIRST();
10567
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010568 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010569 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010570 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010571 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10572 ptr = ctx.line + ctx.val;
10573 len = ctx.vlen;
10574 while (len--)
10575 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10576 }
10577
10578 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020010579 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +020010580 beg = http_txn_get_path(txn);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010581 if (!beg)
10582 beg = end;
10583
10584 for (ptr = beg; ptr < end ; ptr++);
10585
10586 if (beg < ptr && *beg == '/') {
10587 while (beg < ptr)
10588 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10589 }
10590 hash = full_hash(hash);
10591
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010592 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010593 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010594 smp->flags = SMP_F_VOL_1ST;
10595 return 1;
10596}
10597
10598/* This concatenates the source address with the 32-bit hash of the Host and
10599 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10600 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10601 * on the source address length. The URL hash is stored before the address so
10602 * that in environments where IPv6 is insignificant, truncating the output to
10603 * 8 bytes would still work.
10604 */
10605static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010606smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010607{
Willy Tarreau83061a82018-07-13 11:56:34 +020010608 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010609 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010610
Dragan Dosendb5af612016-06-16 11:23:01 +020010611 if (!cli_conn)
10612 return 0;
10613
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010614 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010615 return 0;
10616
10617 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010618 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
10619 temp->data += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010620
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010621 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010622 case AF_INET:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010623 memcpy(temp->area + temp->data,
10624 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
10625 4);
10626 temp->data += 4;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010627 break;
10628 case AF_INET6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010629 memcpy(temp->area + temp->data,
10630 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
10631 16);
10632 temp->data += 16;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010633 break;
10634 default:
10635 return 0;
10636 }
10637
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010638 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010639 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010640 return 1;
10641}
10642
Willy Tarreau185b5c42012-04-26 15:11:51 +020010643/* This function is used to validate the arguments passed to any "hdr" fetch
10644 * keyword. These keywords support an optional positive or negative occurrence
10645 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10646 * is assumed that the types are already the correct ones. Returns 0 on error,
10647 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10648 * error message in case of error, that the caller is responsible for freeing.
10649 * The initial location must either be freeable or NULL.
10650 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010010651int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020010652{
10653 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010654 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010655 return 0;
10656 }
10657 return 1;
10658}
10659
Willy Tarreau276fae92013-07-25 14:36:01 +020010660/* takes an UINT value on input supposed to represent the time since EPOCH,
10661 * adds an optional offset found in args[0] and emits a string representing
10662 * the date in RFC-1123/5322 format.
10663 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010664static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020010665{
Cyril Bontéf78d8962016-01-22 19:40:28 +010010666 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020010667 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
Willy Tarreau83061a82018-07-13 11:56:34 +020010668 struct buffer *temp;
Willy Tarreau276fae92013-07-25 14:36:01 +020010669 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010670 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010671 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020010672
10673 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010674 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020010675 curr_date += args[0].data.sint;
10676
10677 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020010678 if (!tm)
10679 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020010680
10681 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010682 temp->data = snprintf(temp->area, temp->size - temp->data,
10683 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10684 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon],
10685 1900+tm->tm_year,
10686 tm->tm_hour, tm->tm_min, tm->tm_sec);
Willy Tarreau276fae92013-07-25 14:36:01 +020010687
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010688 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010689 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020010690 return 1;
10691}
10692
Thierry FOURNIERad903512014-04-11 17:51:01 +020010693/* Match language range with language tag. RFC2616 14.4:
10694 *
10695 * A language-range matches a language-tag if it exactly equals
10696 * the tag, or if it exactly equals a prefix of the tag such
10697 * that the first tag character following the prefix is "-".
10698 *
10699 * Return 1 if the strings match, else return 0.
10700 */
10701static inline int language_range_match(const char *range, int range_len,
10702 const char *tag, int tag_len)
10703{
10704 const char *end = range + range_len;
10705 const char *tend = tag + tag_len;
10706 while (range < end) {
10707 if (*range == '-' && tag == tend)
10708 return 1;
10709 if (*range != *tag || tag == tend)
10710 return 0;
10711 range++;
10712 tag++;
10713 }
10714 /* Return true only if the last char of the tag is matched. */
10715 return tag == tend;
10716}
10717
10718/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010719static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020010720{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010721 const char *al = smp->data.u.str.area;
10722 const char *end = al + smp->data.u.str.data;
Thierry FOURNIERad903512014-04-11 17:51:01 +020010723 const char *token;
10724 int toklen;
10725 int qvalue;
10726 const char *str;
10727 const char *w;
10728 int best_q = 0;
10729
10730 /* Set the constant to the sample, because the output of the
10731 * function will be peek in the constant configuration string.
10732 */
10733 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010734 smp->data.u.str.size = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010735 smp->data.u.str.area = "";
10736 smp->data.u.str.data = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020010737
10738 /* Parse the accept language */
10739 while (1) {
10740
10741 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020010742 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020010743 al++;
10744 if (al >= end)
10745 break;
10746
10747 /* Start of the fisrt word. */
10748 token = al;
10749
10750 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020010751 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020010752 al++;
10753 if (al == token)
10754 goto expect_comma;
10755
10756 /* Length of the token. */
10757 toklen = al - token;
10758 qvalue = 1000;
10759
10760 /* Check if the token exists in the list. If the token not exists,
10761 * jump to the next token.
10762 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010763 str = args[0].data.str.area;
Thierry FOURNIERad903512014-04-11 17:51:01 +020010764 w = str;
10765 while (1) {
10766 if (*str == ';' || *str == '\0') {
10767 if (language_range_match(token, toklen, w, str-w))
10768 goto look_for_q;
10769 if (*str == '\0')
10770 goto expect_comma;
10771 w = str + 1;
10772 }
10773 str++;
10774 }
10775 goto expect_comma;
10776
10777look_for_q:
10778
10779 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020010780 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020010781 al++;
10782 if (al >= end)
10783 goto process_value;
10784
10785 /* If ',' is found, process the result */
10786 if (*al == ',')
10787 goto process_value;
10788
10789 /* If the character is different from ';', look
10790 * for the end of the header part in best effort.
10791 */
10792 if (*al != ';')
10793 goto expect_comma;
10794
10795 /* Assumes that the char is ';', now expect "q=". */
10796 al++;
10797
10798 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020010799 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020010800 al++;
10801 if (al >= end)
10802 goto process_value;
10803
10804 /* Expect 'q'. If no 'q', continue in best effort */
10805 if (*al != 'q')
10806 goto process_value;
10807 al++;
10808
10809 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020010810 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020010811 al++;
10812 if (al >= end)
10813 goto process_value;
10814
10815 /* Expect '='. If no '=', continue in best effort */
10816 if (*al != '=')
10817 goto process_value;
10818 al++;
10819
10820 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020010821 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020010822 al++;
10823 if (al >= end)
10824 goto process_value;
10825
10826 /* Parse the q value. */
Willy Tarreauab813a42018-09-10 18:41:28 +020010827 qvalue = http_parse_qvalue(al, &al);
Thierry FOURNIERad903512014-04-11 17:51:01 +020010828
10829process_value:
10830
10831 /* If the new q value is the best q value, then store the associated
10832 * language in the response. If qvalue is the biggest value (1000),
10833 * break the process.
10834 */
10835 if (qvalue > best_q) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010836 smp->data.u.str.area = (char *)w;
10837 smp->data.u.str.data = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020010838 if (qvalue >= 1000)
10839 break;
10840 best_q = qvalue;
10841 }
10842
10843expect_comma:
10844
10845 /* Expect comma or end. If the end is detected, quit the loop. */
10846 while (al < end && *al != ',')
10847 al++;
10848 if (al >= end)
10849 break;
10850
10851 /* Comma is found, jump it and restart the analyzer. */
10852 al++;
10853 }
10854
10855 /* Set default value if required. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010856 if (smp->data.u.str.data == 0 && args[1].type == ARGT_STR) {
10857 smp->data.u.str.area = args[1].data.str.area;
10858 smp->data.u.str.data = args[1].data.str.data;
Thierry FOURNIERad903512014-04-11 17:51:01 +020010859 }
10860
10861 /* Return true only if a matching language was found. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010862 return smp->data.u.str.data != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020010863}
10864
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020010865/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010866static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020010867{
Willy Tarreau9c768fd2018-08-22 05:08:57 +020010868 int len;
10869
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020010870 /* If the constant flag is set or if not size is avalaible at
10871 * the end of the buffer, copy the string in other buffer
10872 * before decoding.
10873 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010874 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.data) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010875 struct buffer *str = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010876 memcpy(str->area, smp->data.u.str.area, smp->data.u.str.data);
10877 smp->data.u.str.area = str->area;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010878 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020010879 smp->flags &= ~SMP_F_CONST;
10880 }
10881
10882 /* Add final \0 required by url_decode(), and convert the input string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010883 smp->data.u.str.area[smp->data.u.str.data] = '\0';
Willy Tarreau9c768fd2018-08-22 05:08:57 +020010884 len = url_decode(smp->data.u.str.area);
10885 if (len < 0)
10886 return 0;
10887 smp->data.u.str.data = len;
10888 return 1;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020010889}
10890
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010891static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
10892{
10893 struct proxy *fe = strm_fe(smp->strm);
10894 int idx, i;
10895 struct cap_hdr *hdr;
10896 int len;
10897
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010898 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010899 return 0;
10900
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010901 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010902
10903 /* Check the availibity of the capture id. */
10904 if (idx > fe->nb_req_cap - 1)
10905 return 0;
10906
10907 /* Look for the original configuration. */
10908 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
10909 hdr != NULL && i != idx ;
10910 i--, hdr = hdr->next);
10911 if (!hdr)
10912 return 0;
10913
10914 /* check for the memory allocation */
10915 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010010916 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010917 if (smp->strm->req_cap[hdr->index] == NULL)
10918 return 0;
10919
10920 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010921 len = smp->data.u.str.data;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010922 if (len > hdr->len)
10923 len = hdr->len;
10924
10925 /* Capture input data. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010926 memcpy(smp->strm->req_cap[idx], smp->data.u.str.area, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010927 smp->strm->req_cap[idx][len] = '\0';
10928
10929 return 1;
10930}
10931
10932static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
10933{
10934 struct proxy *fe = strm_fe(smp->strm);
10935 int idx, i;
10936 struct cap_hdr *hdr;
10937 int len;
10938
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010939 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010940 return 0;
10941
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010942 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010943
10944 /* Check the availibity of the capture id. */
10945 if (idx > fe->nb_rsp_cap - 1)
10946 return 0;
10947
10948 /* Look for the original configuration. */
10949 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
10950 hdr != NULL && i != idx ;
10951 i--, hdr = hdr->next);
10952 if (!hdr)
10953 return 0;
10954
10955 /* check for the memory allocation */
10956 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010010957 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010958 if (smp->strm->res_cap[hdr->index] == NULL)
10959 return 0;
10960
10961 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010962 len = smp->data.u.str.data;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010963 if (len > hdr->len)
10964 len = hdr->len;
10965
10966 /* Capture input data. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010967 memcpy(smp->strm->res_cap[idx], smp->data.u.str.area, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010968 smp->strm->res_cap[idx][len] = '\0';
10969
10970 return 1;
10971}
10972
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010973/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010010974 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010975 * the relevant part of the request line accordingly. Then it updates various
10976 * pointers to the next elements which were moved, and the total buffer length.
10977 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010010978 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
10979 * error, though this can be revisited when this code is finally exploited.
10980 *
10981 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
10982 * query string and 3 to replace uri.
10983 *
10984 * In query string case, the mark question '?' must be set at the start of the
10985 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010986 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010010987int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020010988 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010989{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020010990 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010991 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010010992 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010993 int delta;
10994
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010010995 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010996 case 0: // method
Willy Tarreauf37954d2018-06-15 18:31:02 +020010997 cur_ptr = ci_head(&s->req);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010998 cur_end = cur_ptr + txn->req.sl.rq.m_l;
10999
11000 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011001 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011002 txn->req.sl.rq.m_l += delta;
11003 txn->req.sl.rq.u += delta;
11004 txn->req.sl.rq.v += delta;
11005 break;
11006
11007 case 1: // path
Willy Tarreau6b952c82018-09-10 17:45:34 +020011008 cur_ptr = http_txn_get_path(txn);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011009 if (!cur_ptr)
Willy Tarreauf37954d2018-06-15 18:31:02 +020011010 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011011
11012 cur_end = cur_ptr;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011013 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 +010011014 cur_end++;
11015
11016 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011017 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011018 txn->req.sl.rq.u_l += delta;
11019 txn->req.sl.rq.v += delta;
11020 break;
11021
11022 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011023 offset = 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011024 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011025 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11026 while (cur_ptr < cur_end && *cur_ptr != '?')
11027 cur_ptr++;
11028
11029 /* skip the question mark or indicate that we must insert it
11030 * (but only if the format string is not empty then).
11031 */
11032 if (cur_ptr < cur_end)
11033 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011034 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011035 offset = 0;
11036
11037 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011038 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011039 txn->req.sl.rq.u_l += delta;
11040 txn->req.sl.rq.v += delta;
11041 break;
11042
11043 case 3: // uri
Willy Tarreauf37954d2018-06-15 18:31:02 +020011044 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011045 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11046
11047 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011048 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011049 txn->req.sl.rq.u_l += delta;
11050 txn->req.sl.rq.v += delta;
11051 break;
11052
11053 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011054 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011055 }
11056
11057 /* commit changes and adjust end of message */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020011058 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011059 txn->req.sl.rq.l += delta;
11060 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011061 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011062 return 0;
11063}
11064
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011065/* This function replace the HTTP status code and the associated message. The
11066 * variable <status> contains the new status code. This function never fails.
11067 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011068void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011069{
11070 struct http_txn *txn = s->txn;
11071 char *cur_ptr, *cur_end;
11072 int delta;
11073 char *res;
11074 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011075 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011076 int msg_len;
11077
11078 chunk_reset(&trash);
11079
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011080 res = ultoa_o(status, trash.area, trash.size);
11081 c_l = res - trash.area;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011082
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011083 trash.area[c_l] = ' ';
11084 trash.data = c_l + 1;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011085
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011086 /* Do we have a custom reason format string? */
11087 if (msg == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +020011088 msg = http_get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011089 msg_len = strlen(msg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011090 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
11091 trash.data += msg_len;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011092
Willy Tarreauf37954d2018-06-15 18:31:02 +020011093 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
11094 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011095
11096 /* commit changes and adjust message */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011097 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
11098 trash.data);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011099
11100 /* adjust res line offsets and lengths */
11101 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11102 txn->rsp.sl.st.c_l = c_l;
11103 txn->rsp.sl.st.r_l = msg_len;
11104
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011105 delta = trash.data - (cur_end - cur_ptr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011106 txn->rsp.sl.st.l += delta;
11107 txn->hdr_idx.v[0].len += delta;
11108 http_msg_move_end(&txn->rsp, delta);
11109}
11110
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011111/* This function executes one of the set-{method,path,query,uri} actions. It
11112 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011113 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011114 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011115 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11116 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011117 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011118enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011119 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011120{
Willy Tarreau83061a82018-07-13 11:56:34 +020011121 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011122 enum act_return ret = ACT_RET_ERR;
11123
11124 replace = alloc_trash_chunk();
11125 if (!replace)
11126 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011127
11128 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011129 if (rule->arg.http.action == 2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011130 replace->area[replace->data++] = '?';
11131 replace->data += build_logline(s, replace->area + replace->data,
11132 replace->size - replace->data,
11133 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011134
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011135 http_replace_req_line(rule->arg.http.action, replace->area,
11136 replace->data, px, s);
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011137
11138 ret = ACT_RET_CONT;
11139
11140leave:
11141 free_trash_chunk(replace);
11142 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011143}
11144
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011145/* This function is just a compliant action wrapper for "set-status". */
11146enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011147 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011148{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011149 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011150 return ACT_RET_CONT;
11151}
11152
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011153/* parse an http-request action among :
11154 * set-method
11155 * set-path
11156 * set-query
11157 * set-uri
11158 *
11159 * All of them accept a single argument of type string representing a log-format.
11160 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11161 * 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 +020011162 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011163 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011164enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11165 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011166{
11167 int cur_arg = *orig_arg;
11168
Thierry FOURNIER42148732015-09-02 17:17:33 +020011169 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011170
11171 switch (args[0][4]) {
11172 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011173 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011174 rule->action_ptr = http_action_set_req_line;
11175 break;
11176 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011177 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011178 rule->action_ptr = http_action_set_req_line;
11179 break;
11180 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011181 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011182 rule->action_ptr = http_action_set_req_line;
11183 break;
11184 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011185 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011186 rule->action_ptr = http_action_set_req_line;
11187 break;
11188 default:
11189 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011190 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011191 }
11192
11193 if (!*args[cur_arg] ||
11194 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11195 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011196 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011197 }
11198
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011199 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010011200 px->conf.args.ctx = ARGC_HRQ;
11201 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
11202 (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 +010011203 return ACT_RET_PRS_ERR;
11204 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011205
11206 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011207 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011208}
11209
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011210/* parse set-status action:
11211 * This action accepts a single argument of type int representing
11212 * an http status code. It returns ACT_RET_PRS_OK on success,
11213 * ACT_RET_PRS_ERR on error.
11214 */
11215enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
11216 struct act_rule *rule, char **err)
11217{
11218 char *error;
11219
Thierry FOURNIER42148732015-09-02 17:17:33 +020011220 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011221 rule->action_ptr = action_http_set_status;
11222
11223 /* Check if an argument is available */
11224 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011225 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011226 return ACT_RET_PRS_ERR;
11227 }
11228
11229 /* convert status code as integer */
11230 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
11231 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
11232 memprintf(err, "expects an integer status code between 100 and 999");
11233 return ACT_RET_PRS_ERR;
11234 }
11235
11236 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011237
11238 /* set custom reason string */
11239 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
11240 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
11241 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
11242 (*orig_arg)++;
11243 rule->arg.status.reason = strdup(args[*orig_arg]);
11244 (*orig_arg)++;
11245 }
11246
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011247 return ACT_RET_PRS_OK;
11248}
11249
Willy Tarreau53275e82017-11-24 07:52:01 +010011250/* This function executes the "reject" HTTP action. It clears the request and
11251 * response buffer without sending any response. It can be useful as an HTTP
11252 * alternative to the silent-drop action to defend against DoS attacks, and may
11253 * also be used with HTTP/2 to close a connection instead of just a stream.
11254 * The txn status is unchanged, indicating no response was sent. The termination
11255 * flags will indicate "PR". It always returns ACT_RET_STOP.
11256 */
11257enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
11258 struct session *sess, struct stream *s, int flags)
11259{
11260 channel_abort(&s->req);
11261 channel_abort(&s->res);
11262 s->req.analysers = 0;
11263 s->res.analysers = 0;
11264
11265 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
11266 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
11267 if (sess->listener && sess->listener->counters)
11268 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
11269
11270 if (!(s->flags & SF_ERR_MASK))
11271 s->flags |= SF_ERR_PRXCOND;
11272 if (!(s->flags & SF_FINST_MASK))
11273 s->flags |= SF_FINST_R;
11274
11275 return ACT_RET_CONT;
11276}
11277
11278/* parse the "reject" action:
11279 * This action takes no argument and returns ACT_RET_PRS_OK on success,
11280 * ACT_RET_PRS_ERR on error.
11281 */
11282enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
11283 struct act_rule *rule, char **err)
11284{
11285 rule->action = ACT_CUSTOM;
11286 rule->action_ptr = http_action_reject;
11287 return ACT_RET_PRS_OK;
11288}
11289
Willy Tarreaua9083d02015-05-08 15:27:59 +020011290/* This function executes the "capture" action. It executes a fetch expression,
11291 * turns the result into a string and puts it in a capture slot. It always
11292 * returns 1. If an error occurs the action is cancelled, but the rule
11293 * processing continues.
11294 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011295enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011296 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011297{
Willy Tarreaua9083d02015-05-08 15:27:59 +020011298 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011299 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011300 char **cap = s->req_cap;
11301 int len;
11302
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011303 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 +020011304 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011305 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011306
11307 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011308 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011309
11310 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011311 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011312
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011313 len = key->data.u.str.data;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011314 if (len > h->len)
11315 len = h->len;
11316
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011317 memcpy(cap[h->index], key->data.u.str.area, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011318 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011319 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011320}
11321
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011322/* This function executes the "capture" action and store the result in a
11323 * capture slot if exists. It executes a fetch expression, turns the result
11324 * into a string and puts it in a capture slot. It always returns 1. If an
11325 * error occurs the action is cancelled, but the rule processing continues.
11326 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011327enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011328 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011329{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011330 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011331 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011332 char **cap = s->req_cap;
11333 struct proxy *fe = strm_fe(s);
11334 int len;
11335 int i;
11336
11337 /* Look for the original configuration. */
11338 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011339 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011340 i--, h = h->next);
11341 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011342 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011343
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011344 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 +020011345 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011346 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011347
11348 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011349 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011350
11351 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011352 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011353
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011354 len = key->data.u.str.data;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011355 if (len > h->len)
11356 len = h->len;
11357
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011358 memcpy(cap[h->index], key->data.u.str.area, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011359 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011360 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011361}
11362
Christopher Faulet29730ba2017-09-18 15:26:32 +020011363/* Check an "http-request capture" action.
11364 *
11365 * The function returns 1 in success case, otherwise, it returns 0 and err is
11366 * filled.
11367 */
11368int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
11369{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010011370 if (rule->action_ptr != http_action_req_capture_by_id)
11371 return 1;
11372
Christopher Faulet29730ba2017-09-18 15:26:32 +020011373 if (rule->arg.capid.idx >= px->nb_req_cap) {
11374 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
11375 rule->arg.capid.idx);
11376 return 0;
11377 }
11378
11379 return 1;
11380}
11381
Willy Tarreaua9083d02015-05-08 15:27:59 +020011382/* parse an "http-request capture" action. It takes a single argument which is
11383 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011384 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011385 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020011386 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011387enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
11388 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011389{
11390 struct sample_expr *expr;
11391 struct cap_hdr *hdr;
11392 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020011393 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011394
11395 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
11396 if (strcmp(args[cur_arg], "if") == 0 ||
11397 strcmp(args[cur_arg], "unless") == 0)
11398 break;
11399
11400 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011401 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011402 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011403 }
11404
Willy Tarreaua9083d02015-05-08 15:27:59 +020011405 cur_arg = *orig_arg;
11406 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
11407 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011408 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011409
11410 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
11411 memprintf(err,
11412 "fetch method '%s' extracts information from '%s', none of which is available here",
11413 args[cur_arg-1], sample_src_names(expr->fetch->use));
11414 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011415 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011416 }
11417
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011418 if (!args[cur_arg] || !*args[cur_arg]) {
11419 memprintf(err, "expects 'len or 'id'");
11420 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011421 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011422 }
11423
Willy Tarreaua9083d02015-05-08 15:27:59 +020011424 if (strcmp(args[cur_arg], "len") == 0) {
11425 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011426
11427 if (!(px->cap & PR_CAP_FE)) {
11428 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011429 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011430 }
11431
Olivier Houchardfbc74e82017-11-24 16:54:05 +010011432 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011433
Willy Tarreaua9083d02015-05-08 15:27:59 +020011434 if (!args[cur_arg]) {
11435 memprintf(err, "missing length value");
11436 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011437 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011438 }
11439 /* we copy the table name for now, it will be resolved later */
11440 len = atoi(args[cur_arg]);
11441 if (len <= 0) {
11442 memprintf(err, "length must be > 0");
11443 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011444 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011445 }
11446 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011447
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011448 if (!len) {
11449 memprintf(err, "a positive 'len' argument is mandatory");
11450 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011451 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011452 }
11453
Vincent Bernat02779b62016-04-03 13:48:43 +020011454 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011455 hdr->next = px->req_cap;
11456 hdr->name = NULL; /* not a header capture */
11457 hdr->namelen = 0;
11458 hdr->len = len;
11459 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
11460 hdr->index = px->nb_req_cap++;
11461
11462 px->req_cap = hdr;
11463 px->to_log |= LW_REQHDR;
11464
Thierry FOURNIER42148732015-09-02 17:17:33 +020011465 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011466 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011467 rule->arg.cap.expr = expr;
11468 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011469 }
11470
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011471 else if (strcmp(args[cur_arg], "id") == 0) {
11472 int id;
11473 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011474
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011475 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011476
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011477 if (!args[cur_arg]) {
11478 memprintf(err, "missing id value");
11479 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011480 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011481 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020011482
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011483 id = strtol(args[cur_arg], &error, 10);
11484 if (*error != '\0') {
11485 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
11486 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011487 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011488 }
11489 cur_arg++;
11490
Olivier Houchardfbc74e82017-11-24 16:54:05 +010011491 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011492
Thierry FOURNIER42148732015-09-02 17:17:33 +020011493 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011494 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020011495 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011496 rule->arg.capid.expr = expr;
11497 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011498 }
11499
11500 else {
11501 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
11502 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011503 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011504 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020011505
11506 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011507 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011508}
11509
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011510/* This function executes the "capture" action and store the result in a
11511 * capture slot if exists. It executes a fetch expression, turns the result
11512 * into a string and puts it in a capture slot. It always returns 1. If an
11513 * error occurs the action is cancelled, but the rule processing continues.
11514 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011515enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011516 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011517{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011518 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011519 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011520 char **cap = s->res_cap;
11521 struct proxy *fe = strm_fe(s);
11522 int len;
11523 int i;
11524
11525 /* Look for the original configuration. */
11526 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011527 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011528 i--, h = h->next);
11529 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011530 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011531
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011532 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 +020011533 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011534 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011535
11536 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011537 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011538
11539 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011540 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011541
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011542 len = key->data.u.str.data;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011543 if (len > h->len)
11544 len = h->len;
11545
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011546 memcpy(cap[h->index], key->data.u.str.area, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011547 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011548 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011549}
11550
Christopher Faulet29730ba2017-09-18 15:26:32 +020011551/* Check an "http-response capture" action.
11552 *
11553 * The function returns 1 in success case, otherwise, it returns 0 and err is
11554 * filled.
11555 */
11556int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
11557{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010011558 if (rule->action_ptr != http_action_res_capture_by_id)
11559 return 1;
11560
Christopher Faulet29730ba2017-09-18 15:26:32 +020011561 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
11562 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
11563 rule->arg.capid.idx);
11564 return 0;
11565 }
11566
11567 return 1;
11568}
11569
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011570/* parse an "http-response capture" action. It takes a single argument which is
11571 * a sample fetch expression. It stores the expression into arg->act.p[0] and
11572 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011573 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011574 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011575enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
11576 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011577{
11578 struct sample_expr *expr;
11579 int cur_arg;
11580 int id;
11581 char *error;
11582
11583 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
11584 if (strcmp(args[cur_arg], "if") == 0 ||
11585 strcmp(args[cur_arg], "unless") == 0)
11586 break;
11587
11588 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020011589 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011590 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011591 }
11592
11593 cur_arg = *orig_arg;
11594 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
11595 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011596 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011597
11598 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
11599 memprintf(err,
11600 "fetch method '%s' extracts information from '%s', none of which is available here",
11601 args[cur_arg-1], sample_src_names(expr->fetch->use));
11602 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011603 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011604 }
11605
11606 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020011607 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011608 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011609 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011610 }
11611
11612 if (strcmp(args[cur_arg], "id") != 0) {
11613 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
11614 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011615 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011616 }
11617
11618 cur_arg++;
11619
11620 if (!args[cur_arg]) {
11621 memprintf(err, "missing id value");
11622 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011623 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011624 }
11625
11626 id = strtol(args[cur_arg], &error, 10);
11627 if (*error != '\0') {
11628 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
11629 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011630 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011631 }
11632 cur_arg++;
11633
Olivier Houchardfbc74e82017-11-24 16:54:05 +010011634 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011635
Thierry FOURNIER42148732015-09-02 17:17:33 +020011636 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011637 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020011638 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011639 rule->arg.capid.expr = expr;
11640 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011641
11642 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011643 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011644}
11645
William Lallemand73025dd2014-04-24 14:38:37 +020011646/*
11647 * Return the struct http_req_action_kw associated to a keyword.
11648 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020011649struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020011650{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020011651 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020011652}
11653
11654/*
11655 * Return the struct http_res_action_kw associated to a keyword.
11656 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020011657struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020011658{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020011659 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020011660}
11661
Willy Tarreau12207b32016-11-22 19:48:51 +010011662
Willy Tarreau4a568972010-05-12 08:08:50 +020011663/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011664/* All supported ACL keywords must be declared here. */
11665/************************************************************************/
11666
11667/* Note: must not be declared <const> as its list will be overwritten.
11668 * Please take care of keeping this list alphabetically sorted.
11669 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011670static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011671 { "base", "base", PAT_MATCH_STR },
11672 { "base_beg", "base", PAT_MATCH_BEG },
11673 { "base_dir", "base", PAT_MATCH_DIR },
11674 { "base_dom", "base", PAT_MATCH_DOM },
11675 { "base_end", "base", PAT_MATCH_END },
11676 { "base_len", "base", PAT_MATCH_LEN },
11677 { "base_reg", "base", PAT_MATCH_REG },
11678 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011679
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011680 { "cook", "req.cook", PAT_MATCH_STR },
11681 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11682 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11683 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11684 { "cook_end", "req.cook", PAT_MATCH_END },
11685 { "cook_len", "req.cook", PAT_MATCH_LEN },
11686 { "cook_reg", "req.cook", PAT_MATCH_REG },
11687 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011688
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011689 { "hdr", "req.hdr", PAT_MATCH_STR },
11690 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11691 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11692 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11693 { "hdr_end", "req.hdr", PAT_MATCH_END },
11694 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11695 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11696 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011697
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011698 /* these two declarations uses strings with list storage (in place
11699 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11700 * and delete functions are relative to the list management. The parse
11701 * and match method are related to the corresponding fetch methods. This
11702 * is very particular ACL declaration mode.
11703 */
11704 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11705 { "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 +020011706
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011707 { "path", "path", PAT_MATCH_STR },
11708 { "path_beg", "path", PAT_MATCH_BEG },
11709 { "path_dir", "path", PAT_MATCH_DIR },
11710 { "path_dom", "path", PAT_MATCH_DOM },
11711 { "path_end", "path", PAT_MATCH_END },
11712 { "path_len", "path", PAT_MATCH_LEN },
11713 { "path_reg", "path", PAT_MATCH_REG },
11714 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011715
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011716 { "req_ver", "req.ver", PAT_MATCH_STR },
11717 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011718
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011719 { "scook", "res.cook", PAT_MATCH_STR },
11720 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11721 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11722 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11723 { "scook_end", "res.cook", PAT_MATCH_END },
11724 { "scook_len", "res.cook", PAT_MATCH_LEN },
11725 { "scook_reg", "res.cook", PAT_MATCH_REG },
11726 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011727
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011728 { "shdr", "res.hdr", PAT_MATCH_STR },
11729 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11730 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11731 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11732 { "shdr_end", "res.hdr", PAT_MATCH_END },
11733 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11734 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11735 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011736
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011737 { "url", "url", PAT_MATCH_STR },
11738 { "url_beg", "url", PAT_MATCH_BEG },
11739 { "url_dir", "url", PAT_MATCH_DIR },
11740 { "url_dom", "url", PAT_MATCH_DOM },
11741 { "url_end", "url", PAT_MATCH_END },
11742 { "url_len", "url", PAT_MATCH_LEN },
11743 { "url_reg", "url", PAT_MATCH_REG },
11744 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011745
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011746 { "urlp", "urlp", PAT_MATCH_STR },
11747 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11748 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11749 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11750 { "urlp_end", "urlp", PAT_MATCH_END },
11751 { "urlp_len", "urlp", PAT_MATCH_LEN },
11752 { "urlp_reg", "urlp", PAT_MATCH_REG },
11753 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011754
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011755 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011756}};
11757
11758/************************************************************************/
11759/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011760/************************************************************************/
11761/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011762static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011763 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011764 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011765 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11766
Willy Tarreau87b09662015-04-03 00:22:06 +020011767 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011768 { "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 +020011769
11770 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011771 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11772 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11773 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011774
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011775 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
11776 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011777
Willy Tarreau409bcde2013-01-08 00:31:00 +010011778 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11779 * are only here to match the ACL's name, are request-only and are used
11780 * for ACL compatibility only.
11781 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011782 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11783 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011784 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
11785 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011786
11787 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11788 * only here to match the ACL's name, are request-only and are used for
11789 * ACL compatibility only.
11790 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011791 { "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 +020011792 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011793 { "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 +020011794 { "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 +010011795
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011796 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011797 { "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 +010011798 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011799 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011800 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011801 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011802
11803 /* HTTP protocol on the request path */
11804 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011805 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011806
11807 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011808 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11809 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011810
Willy Tarreaua5910cc2015-05-02 00:46:08 +020011811 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011812 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
11813 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011814 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020011815
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020011816 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020011817 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11818
Willy Tarreau18ed2562013-01-14 15:56:36 +010011819 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011820 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11821 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011822
Willy Tarreau18ed2562013-01-14 15:56:36 +010011823 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011824 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011825 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
11826 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011827
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011828 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011829 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011830 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011831 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011832 { "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 +010011833 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011834 { "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 +010011835
11836 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011837 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011838 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
11839 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011840
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011841 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011842 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011843 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011844 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011845 { "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 +010011846 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011847 { "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 +010011848
Willy Tarreau409bcde2013-01-08 00:31:00 +010011849 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011850 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011851 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
11852 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011853 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011854
11855 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011856 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011857 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011858 { "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 +020011859 { "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 +010011860
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011861 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020011862 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011863 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011864 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011865 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011866 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011867 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011868 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11869 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011870 { "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 +010011871 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011872}};
11873
Willy Tarreau8797c062007-05-07 00:55:35 +020011874
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011875/************************************************************************/
11876/* All supported converter keywords must be declared here. */
11877/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020011878/* Note: must not be declared <const> as its list will be overwritten */
11879static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011880 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020011881 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011882 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
11883 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011884 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011885 { NULL, NULL, 0, 0, 0 },
11886}};
11887
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011888
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011889/************************************************************************/
11890/* All supported http-request action keywords must be declared here. */
11891/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020011892struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011893 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020011894 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010011895 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011896 { "set-method", parse_set_req_line },
11897 { "set-path", parse_set_req_line },
11898 { "set-query", parse_set_req_line },
11899 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020011900 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011901 }
11902};
11903
Thierry FOURNIER36481b82015-08-19 09:01:53 +020011904struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011905 .kw = {
11906 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011907 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011908 { NULL, NULL }
11909 }
11910};
11911
Willy Tarreau8797c062007-05-07 00:55:35 +020011912__attribute__((constructor))
11913static void __http_protocol_init(void)
11914{
11915 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011916 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011917 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011918 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011919 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020011920}
11921
11922
Willy Tarreau58f10d72006-12-04 02:26:12 +010011923/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011924 * Local variables:
11925 * c-indent-level: 8
11926 * c-basic-offset: 8
11927 * End:
11928 */