blob: 9f2ccc01dee951260ca140b9eeb30fbed55f4afc [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 Tarreaub05e48a2018-09-20 11:12:58 +0200503 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 Tarreaub05e48a2018-09-20 11:12:58 +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
Christopher Fauletca874b82018-09-20 11:31:01 +02005766 if (msg->msg_state == HTTP_MSG_ENDING)
5767 goto ending;
5768
5769 /* Don't parse chunks if there is no input data */
5770 if (!ci_data(chn))
5771 goto waiting;
5772
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005773 switch_states:
5774 switch (msg->msg_state) {
5775 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005776 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005777 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005778 /* on_error */ goto error);
5779 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005780 msg->chunk_len -= ret;
5781 if (msg->chunk_len) {
5782 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005783 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005784 chn->flags |= CF_WAKE_WRITE;
5785 goto missing_data_or_waiting;
5786 }
5787
5788 /* nothing left to forward for this chunk*/
5789 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5790 /* fall through for HTTP_MSG_CHUNK_CRLF */
5791
5792 case HTTP_MSG_CHUNK_CRLF:
5793 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005794 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005795 if (ret == 0)
5796 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005797 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005798 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005799 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005800 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005801 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005802 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005803 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005804 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5805 /* fall through for HTTP_MSG_CHUNK_SIZE */
5806
5807 case HTTP_MSG_CHUNK_SIZE:
5808 /* read the chunk size and assign it to ->chunk_len,
5809 * then set ->next to point to the body and switch to
5810 * DATA or TRAILERS state.
5811 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005812 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005813 if (ret == 0)
5814 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005815 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005816 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005817 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005818 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005819 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005820 }
5821
5822 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005823 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005824 msg->chunk_len = chunk;
5825 msg->body_len += chunk;
5826
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005827 if (msg->chunk_len) {
5828 msg->msg_state = HTTP_MSG_DATA;
5829 goto switch_states;
5830 }
5831 msg->msg_state = HTTP_MSG_TRAILERS;
5832 /* fall through for HTTP_MSG_TRAILERS */
5833
5834 case HTTP_MSG_TRAILERS:
5835 ret = http_forward_trailers(msg);
5836 if (ret < 0)
5837 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005838 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5839 /* default_ret */ 1,
5840 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005841 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005842 if (!ret)
5843 goto missing_data_or_waiting;
5844 break;
5845
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005846 default:
5847 /* This should no happen in this function */
5848 goto error;
5849 }
5850
5851 msg->msg_state = HTTP_MSG_ENDING;
5852 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005853 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005854 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005855 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005856 /* default_ret */ msg->next,
5857 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005858 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005859 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005860 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5861 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005862 if (msg->next)
5863 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005864
Christopher Fauletda02e172015-12-04 09:25:05 +01005865 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005866 /* default_ret */ 1,
5867 /* on_error */ goto error,
5868 /* on_wait */ goto waiting);
5869 msg->msg_state = HTTP_MSG_DONE;
5870 return 1;
5871
5872 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005873 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005874 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005875 /* default_ret */ msg->next,
5876 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005877 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005878 msg->next -= ret;
5879 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5880 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005881 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005882 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005883 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005884 return 0;
5885
5886 chunk_parsing_error:
5887 if (msg->err_pos >= 0) {
5888 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005889 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005890 msg->msg_state, strm_fe(s));
5891 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005892 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005893 msg, msg->msg_state, s->be);
5894 }
5895 error:
5896 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005897}
5898
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005899
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005900/* Iterate the same filter through all request headers.
5901 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005902 * Since it can manage the switch to another backend, it updates the per-proxy
5903 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005904 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005905int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005906{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005907 char *cur_ptr, *cur_end, *cur_next;
5908 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005909 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005910 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005911 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005912
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005913 last_hdr = 0;
5914
Willy Tarreauf37954d2018-06-15 18:31:02 +02005915 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005916 old_idx = 0;
5917
5918 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005919 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005920 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005921 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005922 (exp->action == ACT_ALLOW ||
5923 exp->action == ACT_DENY ||
5924 exp->action == ACT_TARPIT))
5925 return 0;
5926
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005927 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005928 if (!cur_idx)
5929 break;
5930
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005931 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005932 cur_ptr = cur_next;
5933 cur_end = cur_ptr + cur_hdr->len;
5934 cur_next = cur_end + cur_hdr->cr + 1;
5935
5936 /* Now we have one header between cur_ptr and cur_end,
5937 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005938 */
5939
Willy Tarreau15a53a42015-01-21 13:39:42 +01005940 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005941 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005942 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005943 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005944 last_hdr = 1;
5945 break;
5946
5947 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005948 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005949 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005950 break;
5951
5952 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005953 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005954 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005955 break;
5956
5957 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005958 len = exp_replace(trash.area,
5959 trash.size, cur_ptr,
5960 exp->replace, pmatch);
5961 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005962 return -1;
5963
Willy Tarreau6e27be12018-08-22 04:46:47 +02005964 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5965
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005966 /* FIXME: if the user adds a newline in the replacement, the
5967 * index will not be recalculated for now, and the new line
5968 * will not be counted as a new header.
5969 */
5970
5971 cur_end += delta;
5972 cur_next += delta;
5973 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005974 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005975 break;
5976
5977 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005978 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005979 cur_next += delta;
5980
Willy Tarreaufa355d42009-11-29 18:12:29 +01005981 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005982 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5983 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005984 cur_hdr->len = 0;
5985 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005986 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005987 break;
5988
5989 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005990 }
5991
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005992 /* keep the link from this header to next one in case of later
5993 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005994 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005995 old_idx = cur_idx;
5996 }
5997 return 0;
5998}
5999
6000
6001/* Apply the filter to the request line.
6002 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6003 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006004 * Since it can manage the switch to another backend, it updates the per-proxy
6005 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006006 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006007int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006008{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006009 char *cur_ptr, *cur_end;
6010 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006011 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006012 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006013
Willy Tarreau3d300592007-03-18 18:34:41 +01006014 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006015 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006016 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006017 (exp->action == ACT_ALLOW ||
6018 exp->action == ACT_DENY ||
6019 exp->action == ACT_TARPIT))
6020 return 0;
6021 else if (exp->action == ACT_REMOVE)
6022 return 0;
6023
6024 done = 0;
6025
Willy Tarreauf37954d2018-06-15 18:31:02 +02006026 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006027 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006028
6029 /* Now we have the request line between cur_ptr and cur_end */
6030
Willy Tarreau15a53a42015-01-21 13:39:42 +01006031 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006032 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006033 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006034 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006035 done = 1;
6036 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006037
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006038 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006039 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006040 done = 1;
6041 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006042
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006043 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006044 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006045 done = 1;
6046 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006047
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006048 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006049 len = exp_replace(trash.area, trash.size,
6050 cur_ptr, exp->replace, pmatch);
6051 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006052 return -1;
6053
Willy Tarreau6e27be12018-08-22 04:46:47 +02006054 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6055
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006056 /* FIXME: if the user adds a newline in the replacement, the
6057 * index will not be recalculated for now, and the new line
6058 * will not be counted as a new header.
6059 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006060
Willy Tarreaufa355d42009-11-29 18:12:29 +01006061 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006062 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006063 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006064 HTTP_MSG_RQMETH,
6065 cur_ptr, cur_end + 1,
6066 NULL, NULL);
6067 if (unlikely(!cur_end))
6068 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006069
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006070 /* we have a full request and we know that we have either a CR
6071 * or an LF at <ptr>.
6072 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006073 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6074 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006075 /* there is no point trying this regex on headers */
6076 return 1;
6077 }
6078 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006079 return done;
6080}
Willy Tarreau97de6242006-12-27 17:18:38 +01006081
Willy Tarreau58f10d72006-12-04 02:26:12 +01006082
Willy Tarreau58f10d72006-12-04 02:26:12 +01006083
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006084/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006085 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006086 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006087 * unparsable request. Since it can manage the switch to another backend, it
6088 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006089 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006090int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006091{
Willy Tarreau192252e2015-04-04 01:47:55 +02006092 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006093 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006094 struct hdr_exp *exp;
6095
6096 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006097 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006098
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006099 /*
6100 * The interleaving of transformations and verdicts
6101 * makes it difficult to decide to continue or stop
6102 * the evaluation.
6103 */
6104
Willy Tarreau6c123b12010-01-28 20:22:06 +01006105 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6106 break;
6107
Willy Tarreau3d300592007-03-18 18:34:41 +01006108 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006109 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006110 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006111 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006112
6113 /* if this filter had a condition, evaluate it now and skip to
6114 * next filter if the condition does not match.
6115 */
6116 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006117 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006118 ret = acl_pass(ret);
6119 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6120 ret = !ret;
6121
6122 if (!ret)
6123 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006124 }
6125
6126 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006127 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006128 if (unlikely(ret < 0))
6129 return -1;
6130
6131 if (likely(ret == 0)) {
6132 /* The filter did not match the request, it can be
6133 * iterated through all headers.
6134 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006135 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6136 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006137 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006138 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006139 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006140}
6141
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006142
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006143/* Delete a value in a header between delimiters <from> and <next> in buffer
6144 * <buf>. The number of characters displaced is returned, and the pointer to
6145 * the first delimiter is updated if required. The function tries as much as
6146 * possible to respect the following principles :
6147 * - replace <from> delimiter by the <next> one unless <from> points to a
6148 * colon, in which case <next> is simply removed
6149 * - set exactly one space character after the new first delimiter, unless
6150 * there are not enough characters in the block being moved to do so.
6151 * - remove unneeded spaces before the previous delimiter and after the new
6152 * one.
6153 *
6154 * It is the caller's responsibility to ensure that :
6155 * - <from> points to a valid delimiter or the colon ;
6156 * - <next> points to a valid delimiter or the final CR/LF ;
6157 * - there are non-space chars before <from> ;
6158 * - there is a CR/LF at or after <next>.
6159 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006160int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006161{
6162 char *prev = *from;
6163
6164 if (*prev == ':') {
6165 /* We're removing the first value, preserve the colon and add a
6166 * space if possible.
6167 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006168 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006169 next++;
6170 prev++;
6171 if (prev < next)
6172 *prev++ = ' ';
6173
Willy Tarreau2235b262016-11-05 15:50:20 +01006174 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006175 next++;
6176 } else {
6177 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006178 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006179 prev--;
6180 *from = prev;
6181
6182 /* copy the delimiter and if possible a space if we're
6183 * not at the end of the line.
6184 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006185 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006186 *prev++ = *next++;
6187 if (prev + 1 < next)
6188 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006189 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006190 next++;
6191 }
6192 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006193 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006194}
6195
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006196/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006197 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006198 * desirable to call it only when needed. This code is quite complex because
6199 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6200 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006201 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006202void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006203{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006204 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006205 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006206 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006207 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006208 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6209 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006210
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006211 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006212 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006213 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006214
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006215 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006216 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006217 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006218
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006219 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006220 hdr_beg = hdr_next;
6221 hdr_end = hdr_beg + cur_hdr->len;
6222 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006223
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006224 /* We have one full header between hdr_beg and hdr_end, and the
6225 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006226 * "Cookie:" headers.
6227 */
6228
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006229 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006230 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006231 old_idx = cur_idx;
6232 continue;
6233 }
6234
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006235 del_from = NULL; /* nothing to be deleted */
6236 preserve_hdr = 0; /* assume we may kill the whole header */
6237
Willy Tarreau58f10d72006-12-04 02:26:12 +01006238 /* Now look for cookies. Conforming to RFC2109, we have to support
6239 * attributes whose name begin with a '$', and associate them with
6240 * the right cookie, if we want to delete this cookie.
6241 * So there are 3 cases for each cookie read :
6242 * 1) it's a special attribute, beginning with a '$' : ignore it.
6243 * 2) it's a server id cookie that we *MAY* want to delete : save
6244 * some pointers on it (last semi-colon, beginning of cookie...)
6245 * 3) it's an application cookie : we *MAY* have to delete a previous
6246 * "special" cookie.
6247 * At the end of loop, if a "special" cookie remains, we may have to
6248 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006249 * *MUST* delete it.
6250 *
6251 * Note: RFC2965 is unclear about the processing of spaces around
6252 * the equal sign in the ATTR=VALUE form. A careful inspection of
6253 * the RFC explicitly allows spaces before it, and not within the
6254 * tokens (attrs or values). An inspection of RFC2109 allows that
6255 * too but section 10.1.3 lets one think that spaces may be allowed
6256 * after the equal sign too, resulting in some (rare) buggy
6257 * implementations trying to do that. So let's do what servers do.
6258 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6259 * allowed quoted strings in values, with any possible character
6260 * after a backslash, including control chars and delimitors, which
6261 * causes parsing to become ambiguous. Browsers also allow spaces
6262 * within values even without quotes.
6263 *
6264 * We have to keep multiple pointers in order to support cookie
6265 * removal at the beginning, middle or end of header without
6266 * corrupting the header. All of these headers are valid :
6267 *
6268 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6269 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6270 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6271 * | | | | | | | | |
6272 * | | | | | | | | hdr_end <--+
6273 * | | | | | | | +--> next
6274 * | | | | | | +----> val_end
6275 * | | | | | +-----------> val_beg
6276 * | | | | +--------------> equal
6277 * | | | +----------------> att_end
6278 * | | +---------------------> att_beg
6279 * | +--------------------------> prev
6280 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006281 */
6282
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006283 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6284 /* Iterate through all cookies on this line */
6285
6286 /* find att_beg */
6287 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006288 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006289 att_beg++;
6290
6291 /* find att_end : this is the first character after the last non
6292 * space before the equal. It may be equal to hdr_end.
6293 */
6294 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006295
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006296 while (equal < hdr_end) {
6297 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006298 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006299 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006300 continue;
6301 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006302 }
6303
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006304 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6305 * is between <att_beg> and <equal>, both may be identical.
6306 */
6307
6308 /* look for end of cookie if there is an equal sign */
6309 if (equal < hdr_end && *equal == '=') {
6310 /* look for the beginning of the value */
6311 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006312 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006313 val_beg++;
6314
6315 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006316 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006317
6318 /* make val_end point to the first white space or delimitor after the value */
6319 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006320 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006321 val_end--;
6322 } else {
6323 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006324 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006325
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006326 /* We have nothing to do with attributes beginning with '$'. However,
6327 * they will automatically be removed if a header before them is removed,
6328 * since they're supposed to be linked together.
6329 */
6330 if (*att_beg == '$')
6331 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006332
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006333 /* Ignore cookies with no equal sign */
6334 if (equal == next) {
6335 /* This is not our cookie, so we must preserve it. But if we already
6336 * scheduled another cookie for removal, we cannot remove the
6337 * complete header, but we can remove the previous block itself.
6338 */
6339 preserve_hdr = 1;
6340 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006341 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006342 val_end += delta;
6343 next += delta;
6344 hdr_end += delta;
6345 hdr_next += delta;
6346 cur_hdr->len += delta;
6347 http_msg_move_end(&txn->req, delta);
6348 prev = del_from;
6349 del_from = NULL;
6350 }
6351 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006352 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006353
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006354 /* if there are spaces around the equal sign, we need to
6355 * strip them otherwise we'll get trouble for cookie captures,
6356 * or even for rewrites. Since this happens extremely rarely,
6357 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006358 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006359 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6360 int stripped_before = 0;
6361 int stripped_after = 0;
6362
6363 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006364 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006365 equal += stripped_before;
6366 val_beg += stripped_before;
6367 }
6368
6369 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006370 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006371 val_beg += stripped_after;
6372 stripped_before += stripped_after;
6373 }
6374
6375 val_end += stripped_before;
6376 next += stripped_before;
6377 hdr_end += stripped_before;
6378 hdr_next += stripped_before;
6379 cur_hdr->len += stripped_before;
6380 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006381 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006382 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006383
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006384 /* First, let's see if we want to capture this cookie. We check
6385 * that we don't already have a client side cookie, because we
6386 * can only capture one. Also as an optimisation, we ignore
6387 * cookies shorter than the declared name.
6388 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006389 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6390 (val_end - att_beg >= sess->fe->capture_namelen) &&
6391 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006392 int log_len = val_end - att_beg;
6393
Willy Tarreaubafbe012017-11-24 17:34:44 +01006394 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006395 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006396 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006397 if (log_len > sess->fe->capture_len)
6398 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006399 memcpy(txn->cli_cookie, att_beg, log_len);
6400 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006401 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006402 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006403
Willy Tarreaubca99692010-10-06 19:25:55 +02006404 /* Persistence cookies in passive, rewrite or insert mode have the
6405 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006406 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006407 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006408 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006409 * For cookies in prefix mode, the form is :
6410 *
6411 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006412 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006413 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6414 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6415 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006416 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006417
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006418 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6419 * have the server ID between val_beg and delim, and the original cookie between
6420 * delim+1 and val_end. Otherwise, delim==val_end :
6421 *
6422 * Cookie: NAME=SRV; # in all but prefix modes
6423 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6424 * | || || | |+-> next
6425 * | || || | +--> val_end
6426 * | || || +---------> delim
6427 * | || |+------------> val_beg
6428 * | || +-------------> att_end = equal
6429 * | |+-----------------> att_beg
6430 * | +------------------> prev
6431 * +-------------------------> hdr_beg
6432 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006433
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006434 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006435 for (delim = val_beg; delim < val_end; delim++)
6436 if (*delim == COOKIE_DELIM)
6437 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006438 } else {
6439 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006440 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006441 /* Now check if the cookie contains a date field, which would
6442 * appear after a vertical bar ('|') just after the server name
6443 * and before the delimiter.
6444 */
6445 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6446 if (vbar1) {
6447 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006448 * right is the last seen date. It is a base64 encoded
6449 * 30-bit value representing the UNIX date since the
6450 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006451 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006452 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006453 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006454 if (val_end - vbar1 >= 5) {
6455 val = b64tos30(vbar1);
6456 if (val > 0)
6457 txn->cookie_last_date = val << 2;
6458 }
6459 /* look for a second vertical bar */
6460 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6461 if (vbar1 && (val_end - vbar1 > 5)) {
6462 val = b64tos30(vbar1 + 1);
6463 if (val > 0)
6464 txn->cookie_first_date = val << 2;
6465 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006466 }
6467 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006468
Willy Tarreauf64d1412010-10-07 20:06:11 +02006469 /* if the cookie has an expiration date and the proxy wants to check
6470 * it, then we do that now. We first check if the cookie is too old,
6471 * then only if it has expired. We detect strict overflow because the
6472 * time resolution here is not great (4 seconds). Cookies with dates
6473 * in the future are ignored if their offset is beyond one day. This
6474 * allows an admin to fix timezone issues without expiring everyone
6475 * and at the same time avoids keeping unwanted side effects for too
6476 * long.
6477 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006478 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6479 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006480 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006481 txn->flags &= ~TX_CK_MASK;
6482 txn->flags |= TX_CK_OLD;
6483 delim = val_beg; // let's pretend we have not found the cookie
6484 txn->cookie_first_date = 0;
6485 txn->cookie_last_date = 0;
6486 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006487 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6488 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006489 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006490 txn->flags &= ~TX_CK_MASK;
6491 txn->flags |= TX_CK_EXPIRED;
6492 delim = val_beg; // let's pretend we have not found the cookie
6493 txn->cookie_first_date = 0;
6494 txn->cookie_last_date = 0;
6495 }
6496
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006497 /* Here, we'll look for the first running server which supports the cookie.
6498 * This allows to share a same cookie between several servers, for example
6499 * to dedicate backup servers to specific servers only.
6500 * However, to prevent clients from sticking to cookie-less backup server
6501 * when they have incidentely learned an empty cookie, we simply ignore
6502 * empty cookies and mark them as invalid.
6503 * The same behaviour is applied when persistence must be ignored.
6504 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006505 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006506 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006507
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006508 while (srv) {
6509 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6510 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006511 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006512 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006513 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006514 /* we found the server and we can use it */
6515 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006516 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006517 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006518 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006519 break;
6520 } else {
6521 /* we found a server, but it's down,
6522 * mark it as such and go on in case
6523 * another one is available.
6524 */
6525 txn->flags &= ~TX_CK_MASK;
6526 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006527 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006528 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006529 srv = srv->next;
6530 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006531
Willy Tarreauf64d1412010-10-07 20:06:11 +02006532 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006533 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006534 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006535 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006536 txn->flags |= TX_CK_UNUSED;
6537 else
6538 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006539 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006540
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006541 /* depending on the cookie mode, we may have to either :
6542 * - delete the complete cookie if we're in insert+indirect mode, so that
6543 * the server never sees it ;
6544 * - remove the server id from the cookie value, and tag the cookie as an
6545 * application cookie so that it does not get accidentely removed later,
6546 * if we're in cookie prefix mode
6547 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006548 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006549 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006550
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006551 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006552 val_end += delta;
6553 next += delta;
6554 hdr_end += delta;
6555 hdr_next += delta;
6556 cur_hdr->len += delta;
6557 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006558
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006559 del_from = NULL;
6560 preserve_hdr = 1; /* we want to keep this cookie */
6561 }
6562 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006563 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006564 del_from = prev;
6565 }
6566 } else {
6567 /* This is not our cookie, so we must preserve it. But if we already
6568 * scheduled another cookie for removal, we cannot remove the
6569 * complete header, but we can remove the previous block itself.
6570 */
6571 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006572
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006573 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006574 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006575 if (att_beg >= del_from)
6576 att_beg += delta;
6577 if (att_end >= del_from)
6578 att_end += delta;
6579 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006580 val_end += delta;
6581 next += delta;
6582 hdr_end += delta;
6583 hdr_next += delta;
6584 cur_hdr->len += delta;
6585 http_msg_move_end(&txn->req, delta);
6586 prev = del_from;
6587 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006588 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006589 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006590
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006591 /* continue with next cookie on this header line */
6592 att_beg = next;
6593 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006594
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006595 /* There are no more cookies on this line.
6596 * We may still have one (or several) marked for deletion at the
6597 * end of the line. We must do this now in two ways :
6598 * - if some cookies must be preserved, we only delete from the
6599 * mark to the end of line ;
6600 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006601 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006602 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006603 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006604 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006605 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006606 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006607 cur_hdr->len += delta;
6608 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006609 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006610
6611 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006612 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6613 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006614 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006615 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006616 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006617 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006618 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006619 }
6620
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006621 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006622 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006623 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006624}
6625
6626
Willy Tarreaua15645d2007-03-18 16:22:39 +01006627/* Iterate the same filter through all response headers contained in <rtr>.
6628 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6629 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006630int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006631{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006632 char *cur_ptr, *cur_end, *cur_next;
6633 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006634 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006635 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006636 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006637
6638 last_hdr = 0;
6639
Willy Tarreauf37954d2018-06-15 18:31:02 +02006640 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641 old_idx = 0;
6642
6643 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006644 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006645 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006646 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006647 (exp->action == ACT_ALLOW ||
6648 exp->action == ACT_DENY))
6649 return 0;
6650
6651 cur_idx = txn->hdr_idx.v[old_idx].next;
6652 if (!cur_idx)
6653 break;
6654
6655 cur_hdr = &txn->hdr_idx.v[cur_idx];
6656 cur_ptr = cur_next;
6657 cur_end = cur_ptr + cur_hdr->len;
6658 cur_next = cur_end + cur_hdr->cr + 1;
6659
6660 /* Now we have one header between cur_ptr and cur_end,
6661 * and the next header starts at cur_next.
6662 */
6663
Willy Tarreau15a53a42015-01-21 13:39:42 +01006664 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006665 switch (exp->action) {
6666 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006667 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006668 last_hdr = 1;
6669 break;
6670
6671 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006672 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006673 last_hdr = 1;
6674 break;
6675
6676 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006677 len = exp_replace(trash.area,
6678 trash.size, cur_ptr,
6679 exp->replace, pmatch);
6680 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006681 return -1;
6682
Willy Tarreau6e27be12018-08-22 04:46:47 +02006683 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6684
Willy Tarreaua15645d2007-03-18 16:22:39 +01006685 /* FIXME: if the user adds a newline in the replacement, the
6686 * index will not be recalculated for now, and the new line
6687 * will not be counted as a new header.
6688 */
6689
6690 cur_end += delta;
6691 cur_next += delta;
6692 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006693 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006694 break;
6695
6696 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006697 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006698 cur_next += delta;
6699
Willy Tarreaufa355d42009-11-29 18:12:29 +01006700 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6702 txn->hdr_idx.used--;
6703 cur_hdr->len = 0;
6704 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006705 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006706 break;
6707
6708 }
6709 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006710
6711 /* keep the link from this header to next one in case of later
6712 * removal of next header.
6713 */
6714 old_idx = cur_idx;
6715 }
6716 return 0;
6717}
6718
6719
6720/* Apply the filter to the status line in the response buffer <rtr>.
6721 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6722 * or -1 if a replacement resulted in an invalid status line.
6723 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006724int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006725{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006726 char *cur_ptr, *cur_end;
6727 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006728 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006729 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006730
Willy Tarreau3d300592007-03-18 18:34:41 +01006731 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006732 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006733 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006734 (exp->action == ACT_ALLOW ||
6735 exp->action == ACT_DENY))
6736 return 0;
6737 else if (exp->action == ACT_REMOVE)
6738 return 0;
6739
6740 done = 0;
6741
Willy Tarreauf37954d2018-06-15 18:31:02 +02006742 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006743 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006744
6745 /* Now we have the status line between cur_ptr and cur_end */
6746
Willy Tarreau15a53a42015-01-21 13:39:42 +01006747 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006748 switch (exp->action) {
6749 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006750 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006751 done = 1;
6752 break;
6753
6754 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006755 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006756 done = 1;
6757 break;
6758
6759 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006760 len = exp_replace(trash.area, trash.size,
6761 cur_ptr, exp->replace, pmatch);
6762 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006763 return -1;
6764
Willy Tarreau6e27be12018-08-22 04:46:47 +02006765 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6766
Willy Tarreaua15645d2007-03-18 16:22:39 +01006767 /* FIXME: if the user adds a newline in the replacement, the
6768 * index will not be recalculated for now, and the new line
6769 * will not be counted as a new header.
6770 */
6771
Willy Tarreaufa355d42009-11-29 18:12:29 +01006772 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006773 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006774 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006775 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006776 cur_ptr, cur_end + 1,
6777 NULL, NULL);
6778 if (unlikely(!cur_end))
6779 return -1;
6780
6781 /* we have a full respnse and we know that we have either a CR
6782 * or an LF at <ptr>.
6783 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006784 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006785 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006786 /* there is no point trying this regex on headers */
6787 return 1;
6788 }
6789 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790 return done;
6791}
6792
6793
6794
6795/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006796 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006797 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6798 * unparsable response.
6799 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006800int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006801{
Willy Tarreau192252e2015-04-04 01:47:55 +02006802 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006803 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006804 struct hdr_exp *exp;
6805
6806 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006807 int ret;
6808
6809 /*
6810 * The interleaving of transformations and verdicts
6811 * makes it difficult to decide to continue or stop
6812 * the evaluation.
6813 */
6814
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006815 if (txn->flags & TX_SVDENY)
6816 break;
6817
Willy Tarreau3d300592007-03-18 18:34:41 +01006818 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006819 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6820 exp->action == ACT_PASS)) {
6821 exp = exp->next;
6822 continue;
6823 }
6824
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006825 /* if this filter had a condition, evaluate it now and skip to
6826 * next filter if the condition does not match.
6827 */
6828 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006829 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006830 ret = acl_pass(ret);
6831 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6832 ret = !ret;
6833 if (!ret)
6834 continue;
6835 }
6836
Willy Tarreaua15645d2007-03-18 16:22:39 +01006837 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006838 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006839 if (unlikely(ret < 0))
6840 return -1;
6841
6842 if (likely(ret == 0)) {
6843 /* The filter did not match the response, it can be
6844 * iterated through all headers.
6845 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006846 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6847 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006848 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006849 }
6850 return 0;
6851}
6852
6853
Willy Tarreaua15645d2007-03-18 16:22:39 +01006854/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006855 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006856 * desirable to call it only when needed. This function is also used when we
6857 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006858 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006859void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006860{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006861 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006862 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006863 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006864 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006865 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006866 char *hdr_beg, *hdr_end, *hdr_next;
6867 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006868
Willy Tarreaua15645d2007-03-18 16:22:39 +01006869 /* Iterate through the headers.
6870 * we start with the start line.
6871 */
6872 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006873 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006874
6875 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6876 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006877 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006878
6879 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006880 hdr_beg = hdr_next;
6881 hdr_end = hdr_beg + cur_hdr->len;
6882 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006883
Willy Tarreau24581ba2010-08-31 22:39:35 +02006884 /* We have one full header between hdr_beg and hdr_end, and the
6885 * next header starts at hdr_next. We're only interested in
6886 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006887 */
6888
Willy Tarreau24581ba2010-08-31 22:39:35 +02006889 is_cookie2 = 0;
6890 prev = hdr_beg + 10;
6891 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006892 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006893 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6894 if (!val) {
6895 old_idx = cur_idx;
6896 continue;
6897 }
6898 is_cookie2 = 1;
6899 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006900 }
6901
Willy Tarreau24581ba2010-08-31 22:39:35 +02006902 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6903 * <prev> points to the colon.
6904 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006905 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006906
Willy Tarreau24581ba2010-08-31 22:39:35 +02006907 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6908 * check-cache is enabled) and we are not interested in checking
6909 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006910 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006911 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006912 return;
6913
Willy Tarreau24581ba2010-08-31 22:39:35 +02006914 /* OK so now we know we have to process this response cookie.
6915 * The format of the Set-Cookie header is slightly different
6916 * from the format of the Cookie header in that it does not
6917 * support the comma as a cookie delimiter (thus the header
6918 * cannot be folded) because the Expires attribute described in
6919 * the original Netscape's spec may contain an unquoted date
6920 * with a comma inside. We have to live with this because
6921 * many browsers don't support Max-Age and some browsers don't
6922 * support quoted strings. However the Set-Cookie2 header is
6923 * clean.
6924 *
6925 * We have to keep multiple pointers in order to support cookie
6926 * removal at the beginning, middle or end of header without
6927 * corrupting the header (in case of set-cookie2). A special
6928 * pointer, <scav> points to the beginning of the set-cookie-av
6929 * fields after the first semi-colon. The <next> pointer points
6930 * either to the end of line (set-cookie) or next unquoted comma
6931 * (set-cookie2). All of these headers are valid :
6932 *
6933 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6934 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6935 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6936 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6937 * | | | | | | | | | |
6938 * | | | | | | | | +-> next hdr_end <--+
6939 * | | | | | | | +------------> scav
6940 * | | | | | | +--------------> val_end
6941 * | | | | | +--------------------> val_beg
6942 * | | | | +----------------------> equal
6943 * | | | +------------------------> att_end
6944 * | | +----------------------------> att_beg
6945 * | +------------------------------> prev
6946 * +-----------------------------------------> hdr_beg
6947 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006948
Willy Tarreau24581ba2010-08-31 22:39:35 +02006949 for (; prev < hdr_end; prev = next) {
6950 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006951
Willy Tarreau24581ba2010-08-31 22:39:35 +02006952 /* find att_beg */
6953 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006954 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006955 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006956
Willy Tarreau24581ba2010-08-31 22:39:35 +02006957 /* find att_end : this is the first character after the last non
6958 * space before the equal. It may be equal to hdr_end.
6959 */
6960 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006961
Willy Tarreau24581ba2010-08-31 22:39:35 +02006962 while (equal < hdr_end) {
6963 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6964 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006965 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006966 continue;
6967 att_end = equal;
6968 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006969
Willy Tarreau24581ba2010-08-31 22:39:35 +02006970 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6971 * is between <att_beg> and <equal>, both may be identical.
6972 */
6973
6974 /* look for end of cookie if there is an equal sign */
6975 if (equal < hdr_end && *equal == '=') {
6976 /* look for the beginning of the value */
6977 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006978 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006979 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006980
Willy Tarreau24581ba2010-08-31 22:39:35 +02006981 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006982 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006983
6984 /* make val_end point to the first white space or delimitor after the value */
6985 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006986 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006987 val_end--;
6988 } else {
6989 /* <equal> points to next comma, semi-colon or EOL */
6990 val_beg = val_end = next = equal;
6991 }
6992
6993 if (next < hdr_end) {
6994 /* Set-Cookie2 supports multiple cookies, and <next> points to
6995 * a colon or semi-colon before the end. So skip all attr-value
6996 * pairs and look for the next comma. For Set-Cookie, since
6997 * commas are permitted in values, skip to the end.
6998 */
6999 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02007000 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007001 else
7002 next = hdr_end;
7003 }
7004
7005 /* Now everything is as on the diagram above */
7006
7007 /* Ignore cookies with no equal sign */
7008 if (equal == val_end)
7009 continue;
7010
7011 /* If there are spaces around the equal sign, we need to
7012 * strip them otherwise we'll get trouble for cookie captures,
7013 * or even for rewrites. Since this happens extremely rarely,
7014 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007015 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007016 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7017 int stripped_before = 0;
7018 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007019
Willy Tarreau24581ba2010-08-31 22:39:35 +02007020 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007021 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007022 equal += stripped_before;
7023 val_beg += stripped_before;
7024 }
7025
7026 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007027 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007028 val_beg += stripped_after;
7029 stripped_before += stripped_after;
7030 }
7031
7032 val_end += stripped_before;
7033 next += stripped_before;
7034 hdr_end += stripped_before;
7035 hdr_next += stripped_before;
7036 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007037 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007038 }
7039
7040 /* First, let's see if we want to capture this cookie. We check
7041 * that we don't already have a server side cookie, because we
7042 * can only capture one. Also as an optimisation, we ignore
7043 * cookies shorter than the declared name.
7044 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007045 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007046 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007047 (val_end - att_beg >= sess->fe->capture_namelen) &&
7048 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007049 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007050 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007051 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007052 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007053 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007054 if (log_len > sess->fe->capture_len)
7055 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007056 memcpy(txn->srv_cookie, att_beg, log_len);
7057 txn->srv_cookie[log_len] = 0;
7058 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007059 }
7060
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007061 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007062 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007063 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007064 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7065 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007066 /* assume passive cookie by default */
7067 txn->flags &= ~TX_SCK_MASK;
7068 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007069
7070 /* If the cookie is in insert mode on a known server, we'll delete
7071 * this occurrence because we'll insert another one later.
7072 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007073 * a direct access.
7074 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007075 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007076 /* The "preserve" flag was set, we don't want to touch the
7077 * server's cookie.
7078 */
7079 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007080 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007081 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007082 /* this cookie must be deleted */
7083 if (*prev == ':' && next == hdr_end) {
7084 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007085 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007086 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7087 txn->hdr_idx.used--;
7088 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007089 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007090 hdr_next += delta;
7091 http_msg_move_end(&txn->rsp, delta);
7092 /* note: while both invalid now, <next> and <hdr_end>
7093 * are still equal, so the for() will stop as expected.
7094 */
7095 } else {
7096 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007097 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007098 next = prev;
7099 hdr_end += delta;
7100 hdr_next += delta;
7101 cur_hdr->len += delta;
7102 http_msg_move_end(&txn->rsp, delta);
7103 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007104 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007105 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007106 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007107 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007108 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007109 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007110 * with this server since we know it.
7111 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007112 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007113 next += delta;
7114 hdr_end += delta;
7115 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007116 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007117 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007118
Willy Tarreauf1348312010-10-07 15:54:11 +02007119 txn->flags &= ~TX_SCK_MASK;
7120 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007121 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007122 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007123 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007124 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007125 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007126 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007127 next += delta;
7128 hdr_end += delta;
7129 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007130 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007131 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007132
Willy Tarreau827aee92011-03-10 16:55:02 +01007133 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007134 txn->flags &= ~TX_SCK_MASK;
7135 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007136 }
7137 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007138 /* that's done for this cookie, check the next one on the same
7139 * line when next != hdr_end (only if is_cookie2).
7140 */
7141 }
7142 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007144 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007145}
7146
7147
Willy Tarreaua15645d2007-03-18 16:22:39 +01007148/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007149 * Parses the Cache-Control and Pragma request header fields to determine if
7150 * the request may be served from the cache and/or if it is cacheable. Updates
7151 * s->txn->flags.
7152 */
7153void check_request_for_cacheability(struct stream *s, struct channel *chn)
7154{
7155 struct http_txn *txn = s->txn;
7156 char *p1, *p2;
7157 char *cur_ptr, *cur_end, *cur_next;
7158 int pragma_found;
7159 int cc_found;
7160 int cur_idx;
7161
7162 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7163 return; /* nothing more to do here */
7164
7165 cur_idx = 0;
7166 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007167 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007168
7169 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7170 struct hdr_idx_elem *cur_hdr;
7171 int val;
7172
7173 cur_hdr = &txn->hdr_idx.v[cur_idx];
7174 cur_ptr = cur_next;
7175 cur_end = cur_ptr + cur_hdr->len;
7176 cur_next = cur_end + cur_hdr->cr + 1;
7177
7178 /* We have one full header between cur_ptr and cur_end, and the
7179 * next header starts at cur_next.
7180 */
7181
7182 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7183 if (val) {
7184 if ((cur_end - (cur_ptr + val) >= 8) &&
7185 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7186 pragma_found = 1;
7187 continue;
7188 }
7189 }
7190
William Lallemand8a16fe02018-05-22 11:04:33 +02007191 /* Don't use the cache and don't try to store if we found the
7192 * Authorization header */
7193 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7194 if (val) {
7195 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7196 txn->flags |= TX_CACHE_IGNORE;
7197 continue;
7198 }
7199
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007200 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7201 if (!val)
7202 continue;
7203
7204 /* OK, right now we know we have a cache-control header at cur_ptr */
7205 cc_found = 1;
7206 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7207
7208 if (p1 >= cur_end) /* no more info */
7209 continue;
7210
7211 /* p1 is at the beginning of the value */
7212 p2 = p1;
7213 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7214 p2++;
7215
7216 /* we have a complete value between p1 and p2. We don't check the
7217 * values after max-age, max-stale nor min-fresh, we simply don't
7218 * use the cache when they're specified.
7219 */
7220 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7221 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7222 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7223 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7224 txn->flags |= TX_CACHE_IGNORE;
7225 continue;
7226 }
7227
7228 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7229 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7230 continue;
7231 }
7232 }
7233
7234 /* RFC7234#5.4:
7235 * When the Cache-Control header field is also present and
7236 * understood in a request, Pragma is ignored.
7237 * When the Cache-Control header field is not present in a
7238 * request, caches MUST consider the no-cache request
7239 * pragma-directive as having the same effect as if
7240 * "Cache-Control: no-cache" were present.
7241 */
7242 if (!cc_found && pragma_found)
7243 txn->flags |= TX_CACHE_IGNORE;
7244}
7245
7246/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007247 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007248 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007249void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007250{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007251 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007252 char *p1, *p2;
7253
7254 char *cur_ptr, *cur_end, *cur_next;
7255 int cur_idx;
7256
Willy Tarreau12b32f22017-12-21 16:08:09 +01007257 if (txn->status < 200) {
7258 /* do not try to cache interim responses! */
7259 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007260 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007261 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007262
7263 /* Iterate through the headers.
7264 * we start with the start line.
7265 */
7266 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007267 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007268
7269 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7270 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007271 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007272
7273 cur_hdr = &txn->hdr_idx.v[cur_idx];
7274 cur_ptr = cur_next;
7275 cur_end = cur_ptr + cur_hdr->len;
7276 cur_next = cur_end + cur_hdr->cr + 1;
7277
7278 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007279 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007280 */
7281
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007282 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7283 if (val) {
7284 if ((cur_end - (cur_ptr + val) >= 8) &&
7285 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7286 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7287 return;
7288 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007289 }
7290
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007291 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7292 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007293 continue;
7294
7295 /* OK, right now we know we have a cache-control header at cur_ptr */
7296
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007297 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007298
7299 if (p1 >= cur_end) /* no more info */
7300 continue;
7301
7302 /* p1 is at the beginning of the value */
7303 p2 = p1;
7304
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007305 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007306 p2++;
7307
7308 /* we have a complete value between p1 and p2 */
7309 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007310 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7311 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7312 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7313 continue;
7314 }
7315
Willy Tarreaua15645d2007-03-18 16:22:39 +01007316 /* we have something of the form no-cache="set-cookie" */
7317 if ((cur_end - p1 >= 21) &&
7318 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7319 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007320 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007321 continue;
7322 }
7323
7324 /* OK, so we know that either p2 points to the end of string or to a comma */
7325 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007326 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007327 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007328 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007329 return;
7330 }
7331
7332 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007333 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007334 continue;
7335 }
7336 }
7337}
7338
Willy Tarreau58f10d72006-12-04 02:26:12 +01007339
Willy Tarreaub2513902006-12-17 14:52:38 +01007340/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007341 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007342 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007343 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007344 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007345 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007346 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007347 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007348 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007349int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007350{
7351 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007352 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007353 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007354
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007355 if (!uri_auth)
7356 return 0;
7357
Cyril Bonté70be45d2010-10-12 00:14:35 +02007358 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007359 return 0;
7360
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007361 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007362 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007363 return 0;
7364
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007365 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007366 return 0;
7367
Willy Tarreaub2513902006-12-17 14:52:38 +01007368 return 1;
7369}
7370
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007371/* Append the description of what is present in error snapshot <es> into <out>.
7372 * The description must be small enough to always fit in a trash. The output
7373 * buffer may be the trash so the trash must not be used inside this function.
7374 */
7375void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7376{
7377 chunk_appendf(&trash,
7378 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7379 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7380 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7381 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7382 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7383 es->ctx.http.m_flags, es->ctx.http.m_clen,
7384 es->ctx.http.m_blen, es->ctx.http.b_flags);
7385}
7386
Willy Tarreau4076a152009-04-02 15:18:36 +02007387/*
7388 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007389 * By default it tries to report the error position as msg->err_pos. However if
7390 * this one is not set, it will then report msg->next, which is the last known
7391 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007392 * displays buffers as a contiguous area starting at buf->p. The direction is
7393 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007394 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007395void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007396 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007397 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007398{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007399 union error_snapshot_ctx ctx;
7400 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007401
Willy Tarreauef3ca732018-09-07 15:47:35 +02007402 /* http-specific part now */
7403 ctx.http.sid = s->uniq_id;
7404 ctx.http.state = state;
7405 ctx.http.b_flags = msg->chn->flags;
7406 ctx.http.s_flags = s->flags;
7407 ctx.http.t_flags = s->txn->flags;
7408 ctx.http.m_flags = msg->flags;
7409 ctx.http.m_clen = msg->chunk_len;
7410 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007411
Willy Tarreauef3ca732018-09-07 15:47:35 +02007412 ofs = msg->chn->total - ci_data(msg->chn);
7413 if (ofs < 0)
7414 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007415
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007416 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007417 other_end, s->target,
7418 strm_sess(s), &msg->chn->buf,
7419 ofs, co_data(msg->chn),
7420 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7421 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007422}
Willy Tarreaub2513902006-12-17 14:52:38 +01007423
Willy Tarreau294c4732011-12-16 21:35:50 +01007424/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7425 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7426 * performed over the whole headers. Otherwise it must contain a valid header
7427 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7428 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7429 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7430 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007431 * -1. The value fetch stops at commas, so this function is suited for use with
7432 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007433 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007434 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007435unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007436 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007437 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007438{
Willy Tarreau294c4732011-12-16 21:35:50 +01007439 struct hdr_ctx local_ctx;
7440 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007441 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007442 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007443 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007444
Willy Tarreau294c4732011-12-16 21:35:50 +01007445 if (!ctx) {
7446 local_ctx.idx = 0;
7447 ctx = &local_ctx;
7448 }
7449
Willy Tarreaubce70882009-09-07 11:51:47 +02007450 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007451 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007452 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007453 occ--;
7454 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007455 *vptr = ctx->line + ctx->val;
7456 *vlen = ctx->vlen;
7457 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007458 }
7459 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007460 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007461 }
7462
7463 /* negative occurrence, we scan all the list then walk back */
7464 if (-occ > MAX_HDR_HISTORY)
7465 return 0;
7466
Willy Tarreau294c4732011-12-16 21:35:50 +01007467 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007468 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007469 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7470 len_hist[hist_ptr] = ctx->vlen;
7471 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007472 hist_ptr = 0;
7473 found++;
7474 }
7475 if (-occ > found)
7476 return 0;
7477 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007478 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7479 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7480 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007481 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007482 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007483 if (hist_ptr >= MAX_HDR_HISTORY)
7484 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007485 *vptr = ptr_hist[hist_ptr];
7486 *vlen = len_hist[hist_ptr];
7487 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007488}
7489
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007490/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7491 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7492 * performed over the whole headers. Otherwise it must contain a valid header
7493 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7494 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7495 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7496 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7497 * -1. This function differs from http_get_hdr() in that it only returns full
7498 * line header values and does not stop at commas.
7499 * The return value is 0 if nothing was found, or non-zero otherwise.
7500 */
7501unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7502 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007503 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007504{
7505 struct hdr_ctx local_ctx;
7506 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007507 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007508 unsigned int hist_ptr;
7509 int found;
7510
7511 if (!ctx) {
7512 local_ctx.idx = 0;
7513 ctx = &local_ctx;
7514 }
7515
7516 if (occ >= 0) {
7517 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007518 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007519 occ--;
7520 if (occ <= 0) {
7521 *vptr = ctx->line + ctx->val;
7522 *vlen = ctx->vlen;
7523 return 1;
7524 }
7525 }
7526 return 0;
7527 }
7528
7529 /* negative occurrence, we scan all the list then walk back */
7530 if (-occ > MAX_HDR_HISTORY)
7531 return 0;
7532
7533 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007534 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007535 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7536 len_hist[hist_ptr] = ctx->vlen;
7537 if (++hist_ptr >= MAX_HDR_HISTORY)
7538 hist_ptr = 0;
7539 found++;
7540 }
7541 if (-occ > found)
7542 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007543
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007544 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007545 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7546 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7547 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007548 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007549 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007550 if (hist_ptr >= MAX_HDR_HISTORY)
7551 hist_ptr -= MAX_HDR_HISTORY;
7552 *vptr = ptr_hist[hist_ptr];
7553 *vlen = len_hist[hist_ptr];
7554 return 1;
7555}
7556
Willy Tarreaubaaee002006-06-26 02:48:02 +02007557/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007558 * Print a debug line with a header. Always stop at the first CR or LF char,
7559 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7560 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007561 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007562void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007563{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007564 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007565 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007566
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007567 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007568 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007569 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007570 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 +02007571
7572 for (max = 0; start + max < end; max++)
7573 if (start[max] == '\r' || start[max] == '\n')
7574 break;
7575
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007576 UBOUND(max, trash.size - trash.data - 3);
7577 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7578 trash.area[trash.data++] = '\n';
7579 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007580}
7581
Willy Tarreaueee5b512015-04-03 23:46:31 +02007582
7583/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7584 * The hdr_idx is allocated as well. In case of allocation failure, everything
7585 * allocated is freed and NULL is returned. Otherwise the new transaction is
7586 * assigned to the stream and returned.
7587 */
7588struct http_txn *http_alloc_txn(struct stream *s)
7589{
7590 struct http_txn *txn = s->txn;
7591
7592 if (txn)
7593 return txn;
7594
Willy Tarreaubafbe012017-11-24 17:34:44 +01007595 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007596 if (!txn)
7597 return txn;
7598
7599 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007600 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007601 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007602 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007603 return NULL;
7604 }
7605
7606 s->txn = txn;
7607 return txn;
7608}
7609
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007610void http_txn_reset_req(struct http_txn *txn)
7611{
7612 txn->req.flags = 0;
7613 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7614 txn->req.next = 0;
7615 txn->req.chunk_len = 0LL;
7616 txn->req.body_len = 0LL;
7617 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7618}
7619
7620void http_txn_reset_res(struct http_txn *txn)
7621{
7622 txn->rsp.flags = 0;
7623 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7624 txn->rsp.next = 0;
7625 txn->rsp.chunk_len = 0LL;
7626 txn->rsp.body_len = 0LL;
7627 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7628}
7629
Willy Tarreau0937bc42009-12-22 15:03:09 +01007630/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007631 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007632 * the required fields are properly allocated and that we only need to (re)init
7633 * them. This should be used before processing any new request.
7634 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007635void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007636{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007637 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007638 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007639
7640 txn->flags = 0;
7641 txn->status = -1;
7642
Willy Tarreauf64d1412010-10-07 20:06:11 +02007643 txn->cookie_first_date = 0;
7644 txn->cookie_last_date = 0;
7645
Willy Tarreaueee5b512015-04-03 23:46:31 +02007646 txn->srv_cookie = NULL;
7647 txn->cli_cookie = NULL;
7648 txn->uri = NULL;
7649
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007650 http_txn_reset_req(txn);
7651 http_txn_reset_res(txn);
7652
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007653 txn->req.chn = &s->req;
7654 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007655
7656 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007657
7658 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7659 if (fe->options2 & PR_O2_REQBUG_OK)
7660 txn->req.err_pos = -1; /* let buggy requests pass */
7661
Willy Tarreau0937bc42009-12-22 15:03:09 +01007662 if (txn->hdr_idx.v)
7663 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007664
7665 vars_init(&s->vars_txn, SCOPE_TXN);
7666 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007667}
7668
7669/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007670void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007671{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007672 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007673 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007674
7675 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007676 pool_free(pool_head_requri, txn->uri);
7677 pool_free(pool_head_capture, txn->cli_cookie);
7678 pool_free(pool_head_capture, txn->srv_cookie);
7679 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007680
William Lallemanda73203e2012-03-12 12:48:57 +01007681 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007682 txn->uri = NULL;
7683 txn->srv_cookie = NULL;
7684 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007685
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007686 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007687 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007688 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007689 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007690 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007691 }
7692
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007693 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007694 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007695 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007696 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007697 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007698 }
7699
Willy Tarreau6204cd92016-03-10 16:33:04 +01007700 vars_prune(&s->vars_txn, s->sess, s);
7701 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007702}
7703
7704/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007705void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007706{
7707 http_end_txn(s);
7708 http_init_txn(s);
7709
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007710 /* reinitialise the current rule list pointer to NULL. We are sure that
7711 * any rulelist match the NULL pointer.
7712 */
7713 s->current_rule_list = NULL;
7714
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007715 s->be = strm_fe(s);
7716 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007717 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007718 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007719 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007720 /* re-init store persistence */
7721 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007722 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007723
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007724 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007725
Willy Tarreau739cfba2010-01-25 23:11:14 +01007726 /* We must trim any excess data from the response buffer, because we
7727 * may have blocked an invalid response from a server that we don't
7728 * want to accidentely forward once we disable the analysers, nor do
7729 * we want those data to come along with next response. A typical
7730 * example of such data would be from a buggy server responding to
7731 * a HEAD with some data, or sending more than the advertised
7732 * content-length.
7733 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007734 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007735 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007736
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007737 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007738 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007739
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007740 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007741 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007742
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007743 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007744 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007745
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007746 s->req.rex = TICK_ETERNITY;
7747 s->req.wex = TICK_ETERNITY;
7748 s->req.analyse_exp = TICK_ETERNITY;
7749 s->res.rex = TICK_ETERNITY;
7750 s->res.wex = TICK_ETERNITY;
7751 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007752 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007753}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007754
Sasha Pachev218f0642014-06-16 12:05:59 -06007755void free_http_res_rules(struct list *r)
7756{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02007757 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06007758
7759 list_for_each_entry_safe(pr, tr, r, list) {
7760 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02007761 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06007762 free(pr);
7763 }
7764}
7765
7766void free_http_req_rules(struct list *r)
7767{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02007768 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01007769
7770 list_for_each_entry_safe(pr, tr, r, list) {
7771 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007772 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01007773 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01007774
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02007775 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01007776 free(pr);
7777 }
7778}
7779
Willy Tarreaue365c0b2013-06-11 16:06:12 +02007780/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02007781struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01007782{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02007783 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02007784 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007785 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02007786 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01007787
Vincent Bernat02779b62016-04-03 13:48:43 +02007788 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01007789 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007790 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01007791 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01007792 }
7793
Willy Tarreau5c2e1982012-12-24 12:00:25 +01007794 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007795 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01007796 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02007797 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04007798 int code;
7799 int hc;
7800
Jarno Huuskonen800d1762017-03-06 14:56:36 +02007801 if (!strcmp(args[0], "tarpit")) {
7802 rule->action = ACT_HTTP_REQ_TARPIT;
7803 rule->deny_status = HTTP_ERR_500;
7804 }
7805 else {
7806 rule->action = ACT_ACTION_DENY;
7807 rule->deny_status = HTTP_ERR_403;
7808 }
Willy Tarreauff011f22011-01-06 17:51:27 +01007809 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04007810 if (strcmp(args[cur_arg], "deny_status") == 0) {
7811 cur_arg++;
7812 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007813 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
7814 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
CJ Ess108b1dd2015-04-07 12:03:37 -04007815 goto out_err;
7816 }
7817
7818 code = atol(args[cur_arg]);
7819 cur_arg++;
7820 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
7821 if (http_err_codes[hc] == code) {
7822 rule->deny_status = hc;
7823 break;
7824 }
7825 }
7826
7827 if (hc >= HTTP_ERR_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007828 ha_warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
7829 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04007830 }
7831 }
Willy Tarreauff011f22011-01-06 17:51:27 +01007832 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007833 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01007834 cur_arg = 1;
7835
7836 while(*args[cur_arg]) {
7837 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01007838 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01007839 cur_arg+=2;
7840 continue;
7841 } else
7842 break;
7843 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02007844 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007845 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02007846 cur_arg = 1;
7847
7848 if (!*args[cur_arg] ||
7849 (*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 +01007850 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
7851 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02007852 goto out_err;
7853 }
7854 rule->arg.nice = atoi(args[cur_arg]);
7855 if (rule->arg.nice < -1024)
7856 rule->arg.nice = -1024;
7857 else if (rule->arg.nice > 1024)
7858 rule->arg.nice = 1024;
7859 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02007860 } else if (!strcmp(args[0], "set-tos")) {
7861#ifdef IP_TOS
7862 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007863 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02007864 cur_arg = 1;
7865
7866 if (!*args[cur_arg] ||
7867 (*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 +01007868 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
7869 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02007870 goto out_err;
7871 }
7872
7873 rule->arg.tos = strtol(args[cur_arg], &err, 0);
7874 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007875 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
7876 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02007877 goto out_err;
7878 }
7879 cur_arg++;
7880#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01007881 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 +02007882 goto out_err;
7883#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02007884 } else if (!strcmp(args[0], "set-mark")) {
7885#ifdef SO_MARK
7886 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007887 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02007888 cur_arg = 1;
7889
7890 if (!*args[cur_arg] ||
7891 (*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 +01007892 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
7893 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02007894 goto out_err;
7895 }
7896
7897 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
7898 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007899 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
7900 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02007901 goto out_err;
7902 }
7903 cur_arg++;
7904 global.last_checks |= LSTCHK_NETADM;
7905#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01007906 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 +02007907 goto out_err;
7908#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02007909 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007910 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02007911 cur_arg = 1;
7912
7913 if (!*args[cur_arg] ||
7914 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
7915 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01007916 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
7917 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02007918 goto out_err;
7919 }
7920 if (strcmp(args[cur_arg], "silent") == 0)
7921 rule->arg.loglevel = -1;
7922 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
7923 goto bad_log_level;
7924 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01007925 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007926 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01007927 cur_arg = 1;
7928
Willy Tarreau8d1c5162013-04-03 14:13:58 +02007929 if (!*args[cur_arg] || !*args[cur_arg+1] ||
7930 (*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 +01007931 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
7932 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01007933 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01007934 }
7935
7936 rule->arg.hdr_add.name = strdup(args[cur_arg]);
7937 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
7938 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02007939
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01007940 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01007941 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01007942 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 +01007943 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007944 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
7945 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01007946 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01007947 goto out_err;
7948 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01007949 free(proxy->conf.lfs_file);
7950 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
7951 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01007952 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02007953 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007954 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06007955 cur_arg = 1;
7956
7957 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02007958 (*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 +01007959 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
7960 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06007961 goto out_err;
7962 }
7963
7964 rule->arg.hdr_add.name = strdup(args[cur_arg]);
7965 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
7966 LIST_INIT(&rule->arg.hdr_add.fmt);
7967
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02007968 error = NULL;
7969 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007970 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
7971 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02007972 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06007973 goto out_err;
7974 }
7975
7976 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01007977 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01007978 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 +01007979 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007980 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
7981 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01007982 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01007983 goto out_err;
7984 }
Sasha Pachev218f0642014-06-16 12:05:59 -06007985
7986 free(proxy->conf.lfs_file);
7987 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
7988 proxy->conf.lfs_line = proxy->conf.args.line;
7989 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02007990 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02007991 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02007992 cur_arg = 1;
7993
7994 if (!*args[cur_arg] ||
7995 (*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 +01007996 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
7997 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02007998 goto out_err;
7999 }
8000
8001 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8002 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8003
8004 proxy->conf.args.ctx = ARGC_HRQ;
8005 free(proxy->conf.lfs_file);
8006 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8007 proxy->conf.lfs_line = proxy->conf.args.line;
8008 cur_arg += 1;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008009 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Willy Tarreau09448f72014-06-25 18:12:15 +02008010 struct sample_expr *expr;
8011 unsigned int where;
8012 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008013 unsigned int tsc_num;
8014 const char *tsc_num_str;
Willy Tarreau09448f72014-06-25 18:12:15 +02008015
8016 cur_arg = 1;
8017 proxy->conf.args.ctx = ARGC_TRK;
8018
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008019 tsc_num_str = &args[0][8];
8020 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8021 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8022 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8023 free(err);
8024 goto out_err;
8025 }
8026
Willy Tarreau09448f72014-06-25 18:12:15 +02008027 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8028 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008029 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8030 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Willy Tarreau09448f72014-06-25 18:12:15 +02008031 free(err);
8032 goto out_err;
8033 }
8034
8035 where = 0;
8036 if (proxy->cap & PR_CAP_FE)
8037 where |= SMP_VAL_FE_HRQ_HDR;
8038 if (proxy->cap & PR_CAP_BE)
8039 where |= SMP_VAL_BE_HRQ_HDR;
8040
8041 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008042 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8043 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8044 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8045 args[cur_arg-1], sample_src_names(expr->fetch->use));
Willy Tarreau09448f72014-06-25 18:12:15 +02008046 free(expr);
8047 goto out_err;
8048 }
8049
8050 if (strcmp(args[cur_arg], "table") == 0) {
8051 cur_arg++;
8052 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008053 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8054 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008055 free(expr);
8056 goto out_err;
8057 }
8058 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008059 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008060 cur_arg++;
8061 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008062 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008063 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008064 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008065 } else if (strcmp(args[0], "redirect") == 0) {
8066 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008067 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008068
Willy Tarreaube4653b2015-05-28 15:26:58 +02008069 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008070 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8071 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008072 goto out_err;
8073 }
8074
8075 /* this redirect rule might already contain a parsed condition which
8076 * we'll pass to the http-request rule.
8077 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008078 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008079 rule->arg.redir = redir;
8080 rule->cond = redir->cond;
8081 redir->cond = NULL;
8082 cur_arg = 2;
8083 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008084 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8085 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008086 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008087 /*
8088 * '+ 8' for 'add-acl('
8089 * '- 9' for 'add-acl(' + trailing ')'
8090 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008091 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008092
8093 cur_arg = 1;
8094
8095 if (!*args[cur_arg] ||
8096 (*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 +01008097 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8098 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008099 goto out_err;
8100 }
8101
8102 LIST_INIT(&rule->arg.map.key);
8103 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008104 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008105 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008106 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008107 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8108 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008109 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008110 goto out_err;
8111 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008112 free(proxy->conf.lfs_file);
8113 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8114 proxy->conf.lfs_line = proxy->conf.args.line;
8115 cur_arg += 1;
8116 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8117 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008118 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008119 /*
8120 * '+ 8' for 'del-acl('
8121 * '- 9' for 'del-acl(' + trailing ')'
8122 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008123 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008124
8125 cur_arg = 1;
8126
8127 if (!*args[cur_arg] ||
8128 (*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 +01008129 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8130 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008131 goto out_err;
8132 }
8133
8134 LIST_INIT(&rule->arg.map.key);
8135 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008136 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008137 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008138 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008139 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8140 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008141 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008142 goto out_err;
8143 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008144 free(proxy->conf.lfs_file);
8145 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8146 proxy->conf.lfs_line = proxy->conf.args.line;
8147 cur_arg += 1;
8148 } else if (strncmp(args[0], "del-map", 7) == 0) {
8149 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008150 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008151 /*
8152 * '+ 8' for 'del-map('
8153 * '- 9' for 'del-map(' + trailing ')'
8154 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008155 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008156
8157 cur_arg = 1;
8158
8159 if (!*args[cur_arg] ||
8160 (*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 +01008161 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8162 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008163 goto out_err;
8164 }
8165
8166 LIST_INIT(&rule->arg.map.key);
8167 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008168 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008169 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008170 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008171 ha_alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8172 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008173 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008174 goto out_err;
8175 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008176 free(proxy->conf.lfs_file);
8177 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8178 proxy->conf.lfs_line = proxy->conf.args.line;
8179 cur_arg += 1;
8180 } else if (strncmp(args[0], "set-map", 7) == 0) {
8181 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008182 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008183 /*
8184 * '+ 8' for 'set-map('
8185 * '- 9' for 'set-map(' + trailing ')'
8186 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008187 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008188
8189 cur_arg = 1;
8190
8191 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8192 (*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 +01008193 ha_alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8194 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008195 goto out_err;
8196 }
8197
8198 LIST_INIT(&rule->arg.map.key);
8199 LIST_INIT(&rule->arg.map.value);
8200 proxy->conf.args.ctx = ARGC_HRQ;
8201
8202 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008203 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008204 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008205 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008206 ha_alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8207 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008208 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008209 goto out_err;
8210 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008211
8212 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008213 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008214 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 +01008215 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008216 ha_alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8217 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008218 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008219 goto out_err;
8220 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008221 free(proxy->conf.lfs_file);
8222 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8223 proxy->conf.lfs_line = proxy->conf.args.line;
8224
8225 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008226 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8227 char *errmsg = NULL;
8228 cur_arg = 1;
8229 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008230 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008231 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008232 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008233 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8234 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008235 free(errmsg);
8236 goto out_err;
8237 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008238 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008239 action_build_list(&http_req_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008240 ha_alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8241 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8242 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8243 "%s%s, but got '%s'%s.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008244 file, linenum, *trash.area ? ", " : "", trash.area,
8245 args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008246 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008247 }
8248
8249 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8250 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008251 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008252
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008253 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008254 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8255 file, linenum, args[0], errmsg);
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008256 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008257 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008258 }
8259 rule->cond = cond;
8260 }
8261 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008262 ha_alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8263 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8264 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008265 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008266 }
8267
8268 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008269 out_err:
8270 free(rule);
8271 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008272}
8273
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008274/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008275struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008276{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008277 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008278 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008279 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008280 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008281
8282 rule = calloc(1, sizeof(*rule));
8283 if (!rule) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008284 ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008285 goto out_err;
8286 }
8287
8288 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008289 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008290 cur_arg = 1;
8291 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008292 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008293 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008294 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008295 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008296 cur_arg = 1;
8297
8298 if (!*args[cur_arg] ||
8299 (*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 +01008300 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8301 file, linenum, args[0]);
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008302 goto out_err;
8303 }
8304 rule->arg.nice = atoi(args[cur_arg]);
8305 if (rule->arg.nice < -1024)
8306 rule->arg.nice = -1024;
8307 else if (rule->arg.nice > 1024)
8308 rule->arg.nice = 1024;
8309 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008310 } else if (!strcmp(args[0], "set-tos")) {
8311#ifdef IP_TOS
8312 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008313 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008314 cur_arg = 1;
8315
8316 if (!*args[cur_arg] ||
8317 (*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 +01008318 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8319 file, linenum, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008320 goto out_err;
8321 }
8322
8323 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8324 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008325 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8326 file, linenum, err, args[0]);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008327 goto out_err;
8328 }
8329 cur_arg++;
8330#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008331 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 +02008332 goto out_err;
8333#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008334 } else if (!strcmp(args[0], "set-mark")) {
8335#ifdef SO_MARK
8336 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008337 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008338 cur_arg = 1;
8339
8340 if (!*args[cur_arg] ||
8341 (*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 +01008342 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8343 file, linenum, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008344 goto out_err;
8345 }
8346
8347 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8348 if (err && *err != '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008349 ha_alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8350 file, linenum, err, args[0]);
Willy Tarreau51347ed2013-06-11 19:34:13 +02008351 goto out_err;
8352 }
8353 cur_arg++;
8354 global.last_checks |= LSTCHK_NETADM;
8355#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01008356 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 +02008357 goto out_err;
8358#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008359 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008360 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008361 cur_arg = 1;
8362
8363 if (!*args[cur_arg] ||
8364 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8365 bad_log_level:
Christopher Faulet767a84b2017-11-24 16:50:31 +01008366 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8367 file, linenum, args[0]);
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008368 goto out_err;
8369 }
8370 if (strcmp(args[cur_arg], "silent") == 0)
8371 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008372 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008373 goto bad_log_level;
8374 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008375 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008376 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008377 cur_arg = 1;
8378
8379 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8380 (*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 +01008381 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8382 file, linenum, args[0]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008383 goto out_err;
8384 }
8385
8386 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8387 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8388 LIST_INIT(&rule->arg.hdr_add.fmt);
8389
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008390 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008391 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008392 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 +01008393 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008394 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8395 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008396 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008397 goto out_err;
8398 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008399 free(proxy->conf.lfs_file);
8400 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8401 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008402 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008403 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008404 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008405 cur_arg = 1;
8406
8407 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008408 (*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 +01008409 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
8410 file, linenum, args[0]);
Sasha Pachev218f0642014-06-16 12:05:59 -06008411 goto out_err;
8412 }
8413
8414 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8415 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8416 LIST_INIT(&rule->arg.hdr_add.fmt);
8417
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008418 error = NULL;
8419 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008420 ha_alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8421 args[cur_arg + 1], error);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008422 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008423 goto out_err;
8424 }
8425
8426 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008427 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008428 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 +01008429 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008430 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8431 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008432 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008433 goto out_err;
8434 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008435
8436 free(proxy->conf.lfs_file);
8437 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8438 proxy->conf.lfs_line = proxy->conf.args.line;
8439 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008440 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008441 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008442 cur_arg = 1;
8443
8444 if (!*args[cur_arg] ||
8445 (*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 +01008446 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8447 file, linenum, args[0]);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008448 goto out_err;
8449 }
8450
8451 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8452 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8453
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008454 proxy->conf.args.ctx = ARGC_HRS;
8455 free(proxy->conf.lfs_file);
8456 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8457 proxy->conf.lfs_line = proxy->conf.args.line;
8458 cur_arg += 1;
8459 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8460 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008461 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008462 /*
8463 * '+ 8' for 'add-acl('
8464 * '- 9' for 'add-acl(' + trailing ')'
8465 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008466 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008467
8468 cur_arg = 1;
8469
8470 if (!*args[cur_arg] ||
8471 (*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 +01008472 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8473 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008474 goto out_err;
8475 }
8476
8477 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008478 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008479 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008480 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008481 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008482 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8483 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008484 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008485 goto out_err;
8486 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008487 free(proxy->conf.lfs_file);
8488 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8489 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008490
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008491 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008492 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8493 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008494 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008495 /*
8496 * '+ 8' for 'del-acl('
8497 * '- 9' for 'del-acl(' + trailing ')'
8498 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008499 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008500
8501 cur_arg = 1;
8502
8503 if (!*args[cur_arg] ||
8504 (*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 +01008505 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8506 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008507 goto out_err;
8508 }
8509
8510 LIST_INIT(&rule->arg.map.key);
8511 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008512 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008513 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008514 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008515 ha_alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8516 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008517 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008518 goto out_err;
8519 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008520 free(proxy->conf.lfs_file);
8521 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8522 proxy->conf.lfs_line = proxy->conf.args.line;
8523 cur_arg += 1;
8524 } else if (strncmp(args[0], "del-map", 7) == 0) {
8525 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008526 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008527 /*
8528 * '+ 8' for 'del-map('
8529 * '- 9' for 'del-map(' + trailing ')'
8530 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008531 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008532
8533 cur_arg = 1;
8534
8535 if (!*args[cur_arg] ||
8536 (*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 +01008537 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8538 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008539 goto out_err;
8540 }
8541
8542 LIST_INIT(&rule->arg.map.key);
8543 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008544 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008545 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008546 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008547 ha_alert("parsing [%s:%d]: 'http-response %s' %s.\n",
8548 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008549 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008550 goto out_err;
8551 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008552 free(proxy->conf.lfs_file);
8553 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8554 proxy->conf.lfs_line = proxy->conf.args.line;
8555 cur_arg += 1;
8556 } else if (strncmp(args[0], "set-map", 7) == 0) {
8557 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008558 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008559 /*
8560 * '+ 8' for 'set-map('
8561 * '- 9' for 'set-map(' + trailing ')'
8562 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008563 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008564
8565 cur_arg = 1;
8566
8567 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8568 (*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 +01008569 ha_alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8570 file, linenum, args[0]);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008571 goto out_err;
8572 }
8573
8574 LIST_INIT(&rule->arg.map.key);
8575 LIST_INIT(&rule->arg.map.value);
8576
8577 proxy->conf.args.ctx = ARGC_HRS;
8578
8579 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008580 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008581 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008582 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008583 ha_alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
8584 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008585 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008586 goto out_err;
8587 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008588
8589 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008590 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008591 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 +01008592 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008593 ha_alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
8594 file, linenum, args[0], error);
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008595 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008596 goto out_err;
8597 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008598
8599 free(proxy->conf.lfs_file);
8600 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8601 proxy->conf.lfs_line = proxy->conf.args.line;
8602
8603 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008604 } else if (strcmp(args[0], "redirect") == 0) {
8605 struct redirect_rule *redir;
8606 char *errmsg = NULL;
8607
8608 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008609 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8610 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
Willy Tarreau51d861a2015-05-22 17:30:48 +02008611 goto out_err;
8612 }
8613
8614 /* this redirect rule might already contain a parsed condition which
8615 * we'll pass to the http-request rule.
8616 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008617 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008618 rule->arg.redir = redir;
8619 rule->cond = redir->cond;
8620 redir->cond = NULL;
8621 cur_arg = 2;
8622 return rule;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008623 } else if (strncmp(args[0], "track-sc", 8) == 0) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008624 struct sample_expr *expr;
8625 unsigned int where;
8626 char *err = NULL;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008627 unsigned int tsc_num;
8628 const char *tsc_num_str;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008629
8630 cur_arg = 1;
8631 proxy->conf.args.ctx = ARGC_TRK;
8632
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008633 tsc_num_str = &args[0][8];
8634 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), &err) == -1) {
8635 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8636 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8637 free(err);
8638 goto out_err;
8639 }
8640
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008641 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8642 if (!expr) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008643 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8644 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008645 free(err);
8646 goto out_err;
8647 }
8648
8649 where = 0;
8650 if (proxy->cap & PR_CAP_FE)
8651 where |= SMP_VAL_FE_HRS_HDR;
8652 if (proxy->cap & PR_CAP_BE)
8653 where |= SMP_VAL_BE_HRS_HDR;
8654
8655 if (!(expr->fetch->val & where)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008656 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
8657 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8658 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8659 args[cur_arg-1], sample_src_names(expr->fetch->use));
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008660 free(expr);
8661 goto out_err;
8662 }
8663
8664 if (strcmp(args[cur_arg], "table") == 0) {
8665 cur_arg++;
8666 if (!args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008667 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
8668 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008669 free(expr);
8670 goto out_err;
8671 }
8672 /* we copy the table name for now, it will be resolved later */
8673 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
8674 cur_arg++;
8675 }
8676 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +01008677 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +02008678 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02008679 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
8680 char *errmsg = NULL;
8681 cur_arg = 1;
8682 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008683 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008684 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008685 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008686 ha_alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8687 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
William Lallemand73025dd2014-04-24 14:38:37 +02008688 free(errmsg);
8689 goto out_err;
8690 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008691 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008692 action_build_list(&http_res_keywords.list, &trash);
Christopher Faulet767a84b2017-11-24 16:50:31 +01008693 ha_alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
8694 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
8695 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
8696 "%s%s, but got '%s'%s.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008697 file, linenum, *trash.area ? ", " : "", trash.area,
8698 args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008699 goto out_err;
8700 }
8701
8702 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8703 struct acl_cond *cond;
8704 char *errmsg = NULL;
8705
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008706 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008707 ha_alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8708 file, linenum, args[0], errmsg);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008709 free(errmsg);
8710 goto out_err;
8711 }
8712 rule->cond = cond;
8713 }
8714 else if (*args[cur_arg]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008715 ha_alert("parsing [%s:%d]: 'http-response %s' expects"
8716 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8717 file, linenum, args[0], args[cur_arg]);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008718 goto out_err;
8719 }
8720
8721 return rule;
8722 out_err:
8723 free(rule);
8724 return NULL;
8725}
8726
Willy Tarreau4baae242012-12-27 12:00:31 +01008727/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008728 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02008729 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
8730 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01008731 */
8732struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02008733 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01008734{
8735 struct redirect_rule *rule;
8736 int cur_arg;
8737 int type = REDIRECT_TYPE_NONE;
8738 int code = 302;
8739 const char *destination = NULL;
8740 const char *cookie = NULL;
8741 int cookie_set = 0;
8742 unsigned int flags = REDIRECT_FLAG_NONE;
8743 struct acl_cond *cond = NULL;
8744
8745 cur_arg = 0;
8746 while (*(args[cur_arg])) {
8747 if (strcmp(args[cur_arg], "location") == 0) {
8748 if (!*args[cur_arg + 1])
8749 goto missing_arg;
8750
8751 type = REDIRECT_TYPE_LOCATION;
8752 cur_arg++;
8753 destination = args[cur_arg];
8754 }
8755 else if (strcmp(args[cur_arg], "prefix") == 0) {
8756 if (!*args[cur_arg + 1])
8757 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01008758 type = REDIRECT_TYPE_PREFIX;
8759 cur_arg++;
8760 destination = args[cur_arg];
8761 }
8762 else if (strcmp(args[cur_arg], "scheme") == 0) {
8763 if (!*args[cur_arg + 1])
8764 goto missing_arg;
8765
8766 type = REDIRECT_TYPE_SCHEME;
8767 cur_arg++;
8768 destination = args[cur_arg];
8769 }
8770 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8771 if (!*args[cur_arg + 1])
8772 goto missing_arg;
8773
8774 cur_arg++;
8775 cookie = args[cur_arg];
8776 cookie_set = 1;
8777 }
8778 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8779 if (!*args[cur_arg + 1])
8780 goto missing_arg;
8781
8782 cur_arg++;
8783 cookie = args[cur_arg];
8784 cookie_set = 0;
8785 }
8786 else if (strcmp(args[cur_arg], "code") == 0) {
8787 if (!*args[cur_arg + 1])
8788 goto missing_arg;
8789
8790 cur_arg++;
8791 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008792 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008793 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008794 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008795 args[cur_arg - 1], args[cur_arg]);
8796 return NULL;
8797 }
8798 }
8799 else if (!strcmp(args[cur_arg],"drop-query")) {
8800 flags |= REDIRECT_FLAG_DROP_QS;
8801 }
8802 else if (!strcmp(args[cur_arg],"append-slash")) {
8803 flags |= REDIRECT_FLAG_APPEND_SLASH;
8804 }
8805 else if (strcmp(args[cur_arg], "if") == 0 ||
8806 strcmp(args[cur_arg], "unless") == 0) {
Olivier Houchardfbc74e82017-11-24 16:54:05 +01008807 cond = build_acl_cond(file, linenum, &curproxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01008808 if (!cond) {
8809 memprintf(errmsg, "error in condition: %s", *errmsg);
8810 return NULL;
8811 }
8812 break;
8813 }
8814 else {
8815 memprintf(errmsg,
8816 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8817 args[cur_arg]);
8818 return NULL;
8819 }
8820 cur_arg++;
8821 }
8822
8823 if (type == REDIRECT_TYPE_NONE) {
8824 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8825 return NULL;
8826 }
8827
Willy Tarreaube4653b2015-05-28 15:26:58 +02008828 if (dir && type != REDIRECT_TYPE_LOCATION) {
8829 memprintf(errmsg, "response only supports redirect type 'location'");
8830 return NULL;
8831 }
8832
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02008833 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01008834 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008835 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008836
8837 if (!use_fmt) {
8838 /* old-style static redirect rule */
8839 rule->rdr_str = strdup(destination);
8840 rule->rdr_len = strlen(destination);
8841 }
8842 else {
8843 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008844
8845 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8846 * if prefix == "/", we don't want to add anything, otherwise it
8847 * makes it hard for the user to configure a self-redirection.
8848 */
Godbachd9722032014-12-18 15:44:58 +08008849 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008850 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008851 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
8852 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 +01008853 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8854 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008855 return NULL;
8856 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008857 free(curproxy->conf.lfs_file);
8858 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8859 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008860 }
8861 }
8862
Willy Tarreau4baae242012-12-27 12:00:31 +01008863 if (cookie) {
8864 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8865 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8866 */
8867 rule->cookie_len = strlen(cookie);
8868 if (cookie_set) {
8869 rule->cookie_str = malloc(rule->cookie_len + 10);
8870 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8871 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8872 rule->cookie_len += 9;
8873 } else {
8874 rule->cookie_str = malloc(rule->cookie_len + 21);
8875 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8876 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8877 rule->cookie_len += 20;
8878 }
8879 }
8880 rule->type = type;
8881 rule->code = code;
8882 rule->flags = flags;
8883 LIST_INIT(&rule->list);
8884 return rule;
8885
8886 missing_arg:
8887 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8888 return NULL;
8889}
8890
Willy Tarreau8797c062007-05-07 00:55:35 +02008891/************************************************************************/
8892/* The code below is dedicated to ACL parsing and matching */
8893/************************************************************************/
8894
8895
Willy Tarreau14174bc2012-04-16 14:34:04 +02008896/* This function ensures that the prerequisites for an L7 fetch are ready,
8897 * which means that a request or response is ready. If some data is missing,
8898 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008899 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8900 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008901 *
8902 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02008903 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
8904 * decide whether or not an HTTP message is present ;
8905 * 0 if the requested data cannot be fetched or if it is certain that
8906 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008907 * 1 if an HTTP message is ready
8908 */
James Rosewell91a41cb2015-09-18 17:11:16 +01008909int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008910 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008911{
Willy Tarreau192252e2015-04-04 01:47:55 +02008912 struct http_txn *txn;
8913 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008914
Willy Tarreaube508f12016-03-10 11:47:01 +01008915 /* Note: it is possible that <s> is NULL when called before stream
8916 * initialization (eg: tcp-request connection), so this function is the
8917 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008918 */
Willy Tarreau192252e2015-04-04 01:47:55 +02008919 if (!s)
8920 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01008921
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02008922 if (!s->txn) {
8923 if (unlikely(!http_alloc_txn(s)))
8924 return 0; /* not enough memory */
8925 http_init_txn(s);
8926 }
Willy Tarreau192252e2015-04-04 01:47:55 +02008927 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02008928 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008929
8930 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008931 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008932
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008933 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01008934 /* If the buffer does not leave enough free space at the end,
8935 * we must first realign it.
8936 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02008937 if (ci_head(&s->req) > b_orig(&s->req.buf) &&
8938 ci_head(&s->req) + ci_data(&s->req) > b_wrap(&s->req.buf) - global.tune.maxrewrite)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008939 channel_slow_realign(&s->req, trash.area);
Willy Tarreauaae75e32013-03-29 12:31:49 +01008940
Willy Tarreau14174bc2012-04-16 14:34:04 +02008941 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02008942 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02008943 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008944
8945 /* Try to decode HTTP request */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008946 if (likely(msg->next < ci_data(&s->req)))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008947 http_msg_analyzer(msg, &txn->hdr_idx);
8948
8949 /* Still no valid request ? */
8950 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008951 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau23752332018-06-15 14:54:53 +02008952 channel_full(&s->req, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02008953 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008954 }
8955 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008956 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008957 return 0;
8958 }
8959
8960 /* OK we just got a valid HTTP request. We have some minor
8961 * preparation to perform so that further checks can rely
8962 * on HTTP tests.
8963 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008964
8965 /* If the request was parsed but was too large, we must absolutely
8966 * return an error so that it is not processed. At the moment this
8967 * cannot happen, but if the parsers are to change in the future,
8968 * we want this check to be maintained.
8969 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02008970 if (unlikely(ci_head(&s->req) + ci_data(&s->req) >
Willy Tarreauc9fa0482018-07-10 17:43:27 +02008971 b_wrap(&s->req.buf) - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01008972 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008973 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008974 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01008975 return 1;
8976 }
8977
Willy Tarreauf37954d2018-06-15 18:31:02 +02008978 txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008979 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02008980 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008981
Willy Tarreau506d0502013-07-06 13:29:24 +02008982 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
8983 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008984 }
8985
Willy Tarreau506d0502013-07-06 13:29:24 +02008986 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008987 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02008988 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008989
8990 /* otherwise everything's ready for the request */
8991 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008992 else {
8993 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02008994 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
8995 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008996 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02008997 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02008998 }
8999
9000 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009001 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009002 return 1;
9003}
Willy Tarreau8797c062007-05-07 00:55:35 +02009004
Willy Tarreau8797c062007-05-07 00:55:35 +02009005/* 1. Check on METHOD
9006 * We use the pre-parsed method if it is known, and store its number as an
9007 * integer. If it is unknown, we use the pointer and the length.
9008 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009009static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009010{
9011 int len, meth;
9012
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009013 len = strlen(text);
9014 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009015
9016 pattern->val.i = meth;
9017 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009018 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009019 pattern->len = len;
9020 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009021 else {
9022 pattern->ptr.str = NULL;
9023 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009024 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009025 return 1;
9026}
9027
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009028/* This function fetches the method of current HTTP request and stores
9029 * it in the global pattern struct as a chunk. There are two possibilities :
9030 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9031 * in <len> and <ptr> is NULL ;
9032 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9033 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009034 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009035 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009036static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009037smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009038{
9039 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009040 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009041
Willy Tarreau24e32d82012-04-23 23:55:44 +02009042 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009043
Willy Tarreaube508f12016-03-10 11:47:01 +01009044 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009045 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009046 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009047 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009048 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009049 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9050 /* ensure the indexes are not affected */
9051 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009052 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009053 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
9054 smp->data.u.meth.str.area = ci_head(txn->req.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009055 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009056 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009057 return 1;
9058}
9059
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009060/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009061static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009062{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009063 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009064 struct pattern_list *lst;
9065 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009066
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009067 list_for_each_entry(lst, &expr->patterns, list) {
9068 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009069
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009070 /* well-known method */
9071 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009072 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009073 return pattern;
9074 else
9075 continue;
9076 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009077
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009078 /* Other method, we must compare the strings */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009079 if (pattern->len != smp->data.u.meth.str.data)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009080 continue;
9081
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009082 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009083 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.area, smp->data.u.meth.str.data) == 0) ||
9084 (!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 +01009085 return pattern;
9086 }
9087 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009088}
9089
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009090static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009091smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009092{
Willy Tarreaube508f12016-03-10 11:47:01 +01009093 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009094 char *ptr;
9095 int len;
9096
Willy Tarreauc0239e02012-04-16 14:42:55 +02009097 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009098
Willy Tarreaube508f12016-03-10 11:47:01 +01009099 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009100 len = txn->req.sl.rq.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009101 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009102
9103 while ((len-- > 0) && (*ptr++ != '/'));
9104 if (len <= 0)
9105 return 0;
9106
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009107 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009108 smp->data.u.str.area = ptr;
9109 smp->data.u.str.data = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009110
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009111 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009112 return 1;
9113}
9114
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009115static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009116smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009117{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009118 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009119 char *ptr;
9120 int len;
9121
Willy Tarreauc0239e02012-04-16 14:42:55 +02009122 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009123
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009124 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009125 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9126 return 0;
9127
Willy Tarreau8797c062007-05-07 00:55:35 +02009128 len = txn->rsp.sl.st.v_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009129 ptr = ci_head(txn->rsp.chn);
Willy Tarreau8797c062007-05-07 00:55:35 +02009130
9131 while ((len-- > 0) && (*ptr++ != '/'));
9132 if (len <= 0)
9133 return 0;
9134
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009135 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009136 smp->data.u.str.area = ptr;
9137 smp->data.u.str.data = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009138
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009139 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009140 return 1;
9141}
9142
9143/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009144static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009145smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009146{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009147 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009148 char *ptr;
9149 int len;
9150
Willy Tarreauc0239e02012-04-16 14:42:55 +02009151 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009152
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009153 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009154 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9155 return 0;
9156
Willy Tarreau8797c062007-05-07 00:55:35 +02009157 len = txn->rsp.sl.st.c_l;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009158 ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009159
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009160 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009161 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009162 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009163 return 1;
9164}
9165
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009166static int
9167smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9168{
9169 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9170 return 0;
9171
9172 if (!smp->strm->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01009173 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009174 return 0;
9175 smp->strm->unique_id[0] = '\0';
9176 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009177 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009178 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9179
9180 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009181 smp->data.u.str.area = smp->strm->unique_id;
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009182 smp->flags = SMP_F_CONST;
9183 return 1;
9184}
9185
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009186/* Returns a string block containing all headers including the
9187 * empty line wich separes headers from the body. This is useful
9188 * form some headers analysis.
9189 */
9190static int
9191smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9192{
9193 struct http_msg *msg;
9194 struct hdr_idx *idx;
9195 struct http_txn *txn;
9196
9197 CHECK_HTTP_MESSAGE_FIRST();
9198
9199 txn = smp->strm->txn;
9200 idx = &txn->hdr_idx;
9201 msg = &txn->req;
9202
9203 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009204 smp->data.u.str.area = ci_head(msg->chn) + hdr_idx_first_pos(idx);
9205 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Willy Tarreauf37954d2018-06-15 18:31:02 +02009206 (ci_head(msg->chn)[msg->eoh] == '\r');
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009207
9208 return 1;
9209}
9210
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009211/* Returns the header request in a length/value encoded format.
9212 * This is useful for exchanges with the SPOE.
9213 *
9214 * A "length value" is a multibyte code encoding numbers. It uses the
9215 * SPOE format. The encoding is the following:
9216 *
9217 * Each couple "header name" / "header value" is composed
9218 * like this:
9219 * "length value" "header name bytes"
9220 * "length value" "header value bytes"
9221 * When the last header is reached, the header name and the header
9222 * value are empty. Their length are 0
9223 */
9224static int
9225smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9226{
9227 struct http_msg *msg;
Willy Tarreau83061a82018-07-13 11:56:34 +02009228 struct buffer *temp;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009229 struct hdr_idx *idx;
9230 const char *cur_ptr, *cur_next, *p;
9231 int old_idx, cur_idx;
9232 struct hdr_idx_elem *cur_hdr;
9233 const char *hn, *hv;
9234 int hnl, hvl;
9235 int ret;
9236 struct http_txn *txn;
9237 char *buf;
9238 char *end;
9239
9240 CHECK_HTTP_MESSAGE_FIRST();
9241
9242 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009243 buf = temp->area;
9244 end = temp->area + temp->size;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009245
9246 txn = smp->strm->txn;
9247 idx = &txn->hdr_idx;
9248 msg = &txn->req;
9249
9250 /* Build array of headers. */
9251 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009252 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx);
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009253 while (1) {
9254 cur_idx = idx->v[old_idx].next;
9255 if (!cur_idx)
9256 break;
9257 old_idx = cur_idx;
9258
9259 cur_hdr = &idx->v[cur_idx];
9260 cur_ptr = cur_next;
9261 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9262
9263 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9264 * and the next header starts at cur_next. We'll check
9265 * this header in the list as well as against the default
9266 * rule.
9267 */
9268
9269 /* look for ': *'. */
9270 hn = cur_ptr;
9271 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9272 if (p >= cur_ptr+cur_hdr->len)
9273 continue;
9274 hnl = p - hn;
9275 p++;
9276 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9277 p++;
9278 if (p >= cur_ptr + cur_hdr->len)
9279 continue;
9280 hv = p;
9281 hvl = cur_ptr + cur_hdr->len-p;
9282
9283 /* encode the header name. */
9284 ret = encode_varint(hnl, &buf, end);
9285 if (ret == -1)
9286 return 0;
9287 if (buf + hnl > end)
9288 return 0;
9289 memcpy(buf, hn, hnl);
9290 buf += hnl;
9291
9292 /* encode and copy the value. */
9293 ret = encode_varint(hvl, &buf, end);
9294 if (ret == -1)
9295 return 0;
9296 if (buf + hvl > end)
9297 return 0;
9298 memcpy(buf, hv, hvl);
9299 buf += hvl;
9300 }
9301
9302 /* encode the end of the header list with empty
9303 * header name and header value.
9304 */
9305 ret = encode_varint(0, &buf, end);
9306 if (ret == -1)
9307 return 0;
9308 ret = encode_varint(0, &buf, end);
9309 if (ret == -1)
9310 return 0;
9311
9312 /* Initialise sample data which will be filled. */
9313 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009314 smp->data.u.str.area = temp->area;
9315 smp->data.u.str.data = buf - temp->area;
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009316 smp->data.u.str.size = temp->size;
9317
9318 return 1;
9319}
9320
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009321/* returns the longest available part of the body. This requires that the body
9322 * has been waited for using http-buffer-request.
9323 */
9324static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009325smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009326{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009327 struct http_msg *msg;
9328 unsigned long len;
9329 unsigned long block1;
9330 char *body;
Willy Tarreau83061a82018-07-13 11:56:34 +02009331 struct buffer *temp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009332
9333 CHECK_HTTP_MESSAGE_FIRST();
9334
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009335 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009336 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009337 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009338 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009339
9340 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +02009341 body = c_ptr(msg->chn, -http_data_rewind(msg));
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009342
9343 block1 = len;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02009344 if (block1 > b_wrap(&msg->chn->buf) - body)
9345 block1 = b_wrap(&msg->chn->buf) - body;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009346
9347 if (block1 == len) {
9348 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009349 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009350 smp->data.u.str.area = body;
9351 smp->data.u.str.data = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009352 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9353 }
9354 else {
9355 /* buffer is wrapped, we need to defragment it */
9356 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009357 memcpy(temp->area, body, block1);
9358 memcpy(temp->area + block1, b_orig(&msg->chn->buf),
9359 len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009360 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009361 smp->data.u.str.area = temp->area;
9362 smp->data.u.str.data = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009363 smp->flags = SMP_F_VOL_TEST;
9364 }
9365 return 1;
9366}
9367
9368
9369/* returns the available length of the body. This requires that the body
9370 * has been waited for using http-buffer-request.
9371 */
9372static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009373smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009374{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009375 struct http_msg *msg;
9376
9377 CHECK_HTTP_MESSAGE_FIRST();
9378
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009379 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009380 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009381 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009382 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009383
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009384 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009385 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009386
9387 smp->flags = SMP_F_VOL_TEST;
9388 return 1;
9389}
9390
9391
9392/* returns the advertised length of the body, or the advertised size of the
9393 * chunks available in the buffer. This requires that the body has been waited
9394 * for using http-buffer-request.
9395 */
9396static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009397smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009398{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009399 struct http_msg *msg;
9400
9401 CHECK_HTTP_MESSAGE_FIRST();
9402
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009403 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009404 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009405 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009406 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009407
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009408 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009409 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009410
9411 smp->flags = SMP_F_VOL_TEST;
9412 return 1;
9413}
9414
9415
Willy Tarreau8797c062007-05-07 00:55:35 +02009416/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009417static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009418smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009419{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009420 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009421
Willy Tarreauc0239e02012-04-16 14:42:55 +02009422 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009423
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009424 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009425 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009426 smp->data.u.str.data = txn->req.sl.rq.u_l;
9427 smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009428 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009429 return 1;
9430}
9431
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009432static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009433smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009434{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009435 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009436 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009437
Willy Tarreauc0239e02012-04-16 14:42:55 +02009438 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009439
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009440 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009441 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 +02009442 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009443 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009444
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009445 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009446 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009447 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009448 return 1;
9449}
9450
9451static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009452smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009453{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009454 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009455 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009456
Willy Tarreauc0239e02012-04-16 14:42:55 +02009457 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009458
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009459 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009460 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 +02009461 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9462 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009463
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009464 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009465 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009466 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009467 return 1;
9468}
9469
Willy Tarreau185b5c42012-04-26 15:11:51 +02009470/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9471 * Accepts an optional argument of type string containing the header field name,
9472 * and an optional argument of type signed or unsigned integer to request an
9473 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009474 * headers are considered from the first one. It does not stop on commas and
9475 * returns full lines instead (useful for User-Agent or Date for example).
9476 */
9477static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009478smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009479{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009480 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009481 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009482 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009483 int occ = 0;
9484 const char *name_str = NULL;
9485 int name_len = 0;
9486
9487 if (!ctx) {
9488 /* first call */
9489 ctx = &static_hdr_ctx;
9490 ctx->idx = 0;
9491 smp->ctx.a[0] = ctx;
9492 }
9493
9494 if (args) {
9495 if (args[0].type != ARGT_STR)
9496 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009497 name_str = args[0].data.str.area;
9498 name_len = args[0].data.str.data;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009499
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009500 if (args[1].type == ARGT_SINT)
9501 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009502 }
9503
9504 CHECK_HTTP_MESSAGE_FIRST();
9505
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009506 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009507 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 +02009508
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009509 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9510 /* search for header from the beginning */
9511 ctx->idx = 0;
9512
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009513 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009514 /* no explicit occurrence and single fetch => last header by default */
9515 occ = -1;
9516
9517 if (!occ)
9518 /* prepare to report multiple occurrences for ACL fetches */
9519 smp->flags |= SMP_F_NOT_LAST;
9520
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009521 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009522 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009523 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 +02009524 return 1;
9525
9526 smp->flags &= ~SMP_F_NOT_LAST;
9527 return 0;
9528}
9529
9530/* 6. Check on HTTP header count. The number of occurrences is returned.
9531 * Accepts exactly 1 argument of type string. It does not stop on commas and
9532 * returns full lines instead (useful for User-Agent or Date for example).
9533 */
9534static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009535smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009536{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009537 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009538 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009539 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009540 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009541 const char *name = NULL;
9542 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009543
Willy Tarreau601a4d12015-04-01 19:16:09 +02009544 if (args && args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009545 name = args->data.str.area;
9546 len = args->data.str.data;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009547 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009548
9549 CHECK_HTTP_MESSAGE_FIRST();
9550
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009551 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009552 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 +02009553
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009554 ctx.idx = 0;
9555 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009556 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009557 cnt++;
9558
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009559 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009560 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009561 smp->flags = SMP_F_VOL_HDR;
9562 return 1;
9563}
9564
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009565static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009566smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009567{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009568 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009569 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009570 const struct http_msg *msg;
Willy Tarreau83061a82018-07-13 11:56:34 +02009571 struct buffer *temp;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009572 char del = ',';
9573
9574 if (args && args->type == ARGT_STR)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009575 del = *args[0].data.str.area;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009576
9577 CHECK_HTTP_MESSAGE_FIRST();
9578
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009579 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009580 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 +02009581
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009582 temp = get_trash_chunk();
9583
9584 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009585 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009586 if (temp->data)
9587 temp->area[temp->data++] = del;
9588 memcpy(temp->area + temp->data, ctx.line, ctx.del);
9589 temp->data += ctx.del;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009590 }
9591
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009592 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009593 smp->data.u.str.area = temp->area;
9594 smp->data.u.str.data = temp->data;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009595 smp->flags = SMP_F_VOL_HDR;
9596 return 1;
9597}
9598
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009599/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9600 * Accepts an optional argument of type string containing the header field name,
9601 * and an optional argument of type signed or unsigned integer to request an
9602 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009603 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009604 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009605static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009606smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009607{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009608 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009609 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009610 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009611 int occ = 0;
9612 const char *name_str = NULL;
9613 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009614
Willy Tarreaua890d072013-04-02 12:01:06 +02009615 if (!ctx) {
9616 /* first call */
9617 ctx = &static_hdr_ctx;
9618 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009619 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009620 }
9621
Willy Tarreau185b5c42012-04-26 15:11:51 +02009622 if (args) {
9623 if (args[0].type != ARGT_STR)
9624 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009625 name_str = args[0].data.str.area;
9626 name_len = args[0].data.str.data;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009627
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009628 if (args[1].type == ARGT_SINT)
9629 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009630 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009631
Willy Tarreaue333ec92012-04-16 16:26:40 +02009632 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009633
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009634 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009635 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 +02009636
Willy Tarreau185b5c42012-04-26 15:11:51 +02009637 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009638 /* search for header from the beginning */
9639 ctx->idx = 0;
9640
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009641 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +02009642 /* no explicit occurrence and single fetch => last header by default */
9643 occ = -1;
9644
9645 if (!occ)
9646 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009647 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009648
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009649 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009650 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009651 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 +02009652 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009653
Willy Tarreau37406352012-04-23 16:16:37 +02009654 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009655 return 0;
9656}
9657
Willy Tarreauc11416f2007-06-17 16:58:38 +02009658/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009659 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009660 */
9661static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009662smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009663{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009664 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009665 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009666 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009667 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009668 const char *name = NULL;
9669 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02009670
Willy Tarreau601a4d12015-04-01 19:16:09 +02009671 if (args && args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009672 name = args->data.str.area;
9673 len = args->data.str.data;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009674 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009675
Willy Tarreaue333ec92012-04-16 16:26:40 +02009676 CHECK_HTTP_MESSAGE_FIRST();
9677
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009678 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009679 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 +02009680
Willy Tarreau33a7e692007-06-10 19:45:56 +02009681 ctx.idx = 0;
9682 cnt = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009683 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009684 cnt++;
9685
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009686 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009687 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009688 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009689 return 1;
9690}
9691
Willy Tarreau185b5c42012-04-26 15:11:51 +02009692/* Fetch an HTTP header's integer value. The integer value is returned. It
9693 * takes a mandatory argument of type string and an optional one of type int
9694 * to designate a specific occurrence. It returns an unsigned integer, which
9695 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009696 */
9697static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009698smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009699{
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009700 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009701
Willy Tarreauf853c462012-04-23 18:53:56 +02009702 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009703 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009704 smp->data.u.sint = strl2ic(smp->data.u.str.area,
9705 smp->data.u.str.data);
Willy Tarreauf853c462012-04-23 18:53:56 +02009706 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009707
Willy Tarreaud53e2422012-04-16 17:21:11 +02009708 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009709}
9710
Cyril Bonté69fa9922012-10-25 00:01:06 +02009711/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9712 * and an optional one of type int to designate a specific occurrence.
9713 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009714 */
9715static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009716smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +02009717{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009718 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009719
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009720 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009721 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009722 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009723 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009724 } else {
Willy Tarreau83061a82018-07-13 11:56:34 +02009725 struct buffer *temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009726 if (smp->data.u.str.data < temp->size - 1) {
9727 memcpy(temp->area, smp->data.u.str.area,
9728 smp->data.u.str.data);
9729 temp->area[smp->data.u.str.data] = '\0';
9730 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009731 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009732 break;
9733 }
9734 }
9735 }
9736
Willy Tarreaud53e2422012-04-16 17:21:11 +02009737 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009738 if (!(smp->flags & SMP_F_NOT_LAST))
9739 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009740 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009741 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009742}
9743
Willy Tarreau737b0c12007-06-10 21:28:46 +02009744/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9745 * the first '/' after the possible hostname, and ends before the possible '?'.
9746 */
9747static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009748smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009749{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009750 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009751 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009752
Willy Tarreauc0239e02012-04-16 14:42:55 +02009753 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009754
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009755 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009756 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +02009757 ptr = http_txn_get_path(txn);
Willy Tarreau21d2af32008-02-14 20:25:24 +01009758 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009759 return 0;
9760
9761 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009762 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009763 smp->data.u.str.area = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009764
9765 while (ptr < end && *ptr != '?')
9766 ptr++;
9767
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009768 smp->data.u.str.data = ptr - smp->data.u.str.area;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009769 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009770 return 1;
9771}
9772
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009773/* This produces a concatenation of the first occurrence of the Host header
9774 * followed by the path component if it begins with a slash ('/'). This means
9775 * that '*' will not be added, resulting in exactly the first Host entry.
9776 * If no Host header is found, then the path is returned as-is. The returned
9777 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009778 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009779 */
9780static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009781smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009782{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009783 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009784 char *ptr, *end, *beg;
9785 struct hdr_ctx ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02009786 struct buffer *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009787
9788 CHECK_HTTP_MESSAGE_FIRST();
9789
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009790 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009791 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009792 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009793 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009794
9795 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +02009796 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009797 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009798 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009799 smp->data.u.str.area = temp->area;
9800 smp->data.u.str.data = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009801
9802 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009803 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +02009804 beg = http_txn_get_path(txn);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009805 if (!beg)
9806 beg = end;
9807
9808 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9809
9810 if (beg < ptr && *beg == '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009811 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
9812 ptr - beg);
9813 smp->data.u.str.data += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009814 }
9815
9816 smp->flags = SMP_F_VOL_1ST;
9817 return 1;
9818}
9819
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009820/* This produces a 32-bit hash of the concatenation of the first occurrence of
9821 * the Host header followed by the path component if it begins with a slash ('/').
9822 * This means that '*' will not be added, resulting in exactly the first Host
9823 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009824 * is hashed using the path hash followed by a full avalanche hash and provides a
9825 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009826 * high-traffic sites without having to store whole paths.
9827 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +02009828int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009829smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009830{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009831 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009832 struct hdr_ctx ctx;
9833 unsigned int hash = 0;
9834 char *ptr, *beg, *end;
9835 int len;
9836
9837 CHECK_HTTP_MESSAGE_FIRST();
9838
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009839 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009840 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009841 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009842 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9843 ptr = ctx.line + ctx.val;
9844 len = ctx.vlen;
9845 while (len--)
9846 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9847 }
9848
9849 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +02009850 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +02009851 beg = http_txn_get_path(txn);
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009852 if (!beg)
9853 beg = end;
9854
9855 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9856
9857 if (beg < ptr && *beg == '/') {
9858 while (beg < ptr)
9859 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9860 }
9861 hash = full_hash(hash);
9862
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009863 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009864 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009865 smp->flags = SMP_F_VOL_1ST;
9866 return 1;
9867}
9868
Willy Tarreau4a550602012-12-09 14:53:32 +01009869/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009870 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9871 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9872 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009873 * that in environments where IPv6 is insignificant, truncating the output to
9874 * 8 bytes would still work.
9875 */
9876static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009877smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +01009878{
Willy Tarreau83061a82018-07-13 11:56:34 +02009879 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009880 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009881
9882 if (!cli_conn)
9883 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009884
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009885 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +01009886 return 0;
9887
Willy Tarreau47ca5452012-12-23 20:22:19 +01009888 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009889 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
9890 temp->data += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +01009891
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009892 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009893 case AF_INET:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009894 memcpy(temp->area + temp->data,
9895 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
9896 4);
9897 temp->data += 4;
Willy Tarreau4a550602012-12-09 14:53:32 +01009898 break;
9899 case AF_INET6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009900 memcpy(temp->area + temp->data,
9901 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
9902 16);
9903 temp->data += 16;
Willy Tarreau4a550602012-12-09 14:53:32 +01009904 break;
9905 default:
9906 return 0;
9907 }
9908
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009909 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009910 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +01009911 return 1;
9912}
9913
Willy Tarreau49ad95c2015-01-19 15:06:26 +01009914/* Extracts the query string, which comes after the question mark '?'. If no
9915 * question mark is found, nothing is returned. Otherwise it returns a sample
9916 * of type string carrying the whole query string.
9917 */
9918static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009919smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +01009920{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009921 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +01009922 char *ptr, *end;
9923
9924 CHECK_HTTP_MESSAGE_FIRST();
9925
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009926 txn = smp->strm->txn;
Willy Tarreauf37954d2018-06-15 18:31:02 +02009927 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
Willy Tarreau49ad95c2015-01-19 15:06:26 +01009928 end = ptr + txn->req.sl.rq.u_l;
9929
9930 /* look up the '?' */
9931 do {
9932 if (ptr == end)
9933 return 0;
9934 } while (*ptr++ != '?');
9935
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009936 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02009937 smp->data.u.str.area = ptr;
9938 smp->data.u.str.data = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +01009939 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
9940 return 1;
9941}
9942
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009943static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009944smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009945{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009946 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9947 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9948 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009949
Willy Tarreau24e32d82012-04-23 23:55:44 +02009950 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009951
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009952 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009953 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009954 return 1;
9955}
9956
Willy Tarreau7f18e522010-10-22 20:04:13 +02009957/* return a valid test if the current request is the first one on the connection */
9958static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009959smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009960{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009961 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009962 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009963 return 1;
9964}
9965
Willy Tarreau34db1082012-04-19 17:16:54 +02009966/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009967static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009968smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009969{
9970
Willy Tarreau24e32d82012-04-23 23:55:44 +02009971 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009972 return 0;
9973
Willy Tarreauc0239e02012-04-16 14:42:55 +02009974 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009975
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009976 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009977 return 0;
9978
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009979 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009980 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009981 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009982 return 1;
9983}
Willy Tarreau8797c062007-05-07 00:55:35 +02009984
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009985/* Accepts exactly 1 argument of type userlist */
9986static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009987smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009988{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009989 if (!args || args->type != ARGT_USR)
9990 return 0;
9991
9992 CHECK_HTTP_MESSAGE_FIRST();
9993
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009994 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009995 return 0;
9996
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009997 /* if the user does not belong to the userlist or has a wrong password,
9998 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009999 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010000 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010001 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10002 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010003 return 0;
10004
10005 /* pat_match_auth() will need the user list */
10006 smp->ctx.a[0] = args->data.usr;
10007
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010008 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010009 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010010 smp->data.u.str.area = smp->strm->txn->auth.user;
10011 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010012
10013 return 1;
10014}
10015
William Lallemanda43ba4e2014-01-28 18:14:25 +010010016/* Fetch a captured HTTP request header. The index is the position of
10017 * the "capture" option in the configuration file
10018 */
10019static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010020smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010021{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010022 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010023 int idx;
10024
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010025 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010026 return 0;
10027
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010028 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010029
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010030 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 +010010031 return 0;
10032
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010033 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010034 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010035 smp->data.u.str.area = smp->strm->req_cap[idx];
10036 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010037
10038 return 1;
10039}
10040
10041/* Fetch a captured HTTP response header. The index is the position of
10042 * the "capture" option in the configuration file
10043 */
10044static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010045smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010046{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010047 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010048 int idx;
10049
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010050 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010051 return 0;
10052
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010053 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010054
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010055 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 +010010056 return 0;
10057
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010058 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010059 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010060 smp->data.u.str.area = smp->strm->res_cap[idx];
10061 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010062
10063 return 1;
10064}
10065
William Lallemand65ad6e12014-01-31 15:08:02 +010010066/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10067static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010068smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010069{
Willy Tarreau83061a82018-07-13 11:56:34 +020010070 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010071 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010072 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010073
Willy Tarreau15e91e12015-04-04 00:52:09 +020010074 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010075 return 0;
10076
William Lallemand96a77852014-02-05 00:30:02 +010010077 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010078
William Lallemand96a77852014-02-05 00:30:02 +010010079 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10080 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010081
William Lallemand96a77852014-02-05 00:30:02 +010010082 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010083 temp->area = txn->uri;
10084 temp->data = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010085 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010086 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010087 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010088
10089 return 1;
10090
10091}
10092
10093/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10094static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010095smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010096{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010097 struct http_txn *txn = smp->strm->txn;
Willy Tarreau6b952c82018-09-10 17:45:34 +020010098 struct ist path;
10099 const char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010100
Willy Tarreau15e91e12015-04-04 00:52:09 +020010101 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010102 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010103
William Lallemand65ad6e12014-01-31 15:08:02 +010010104 ptr = txn->uri;
10105
10106 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10107 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010108
William Lallemand65ad6e12014-01-31 15:08:02 +010010109 if (!*ptr)
10110 return 0;
10111
10112 ptr++; /* skip the space */
10113
Willy Tarreau6b952c82018-09-10 17:45:34 +020010114 path = http_get_path(ist(ptr));
10115 if (!path.ptr)
William Lallemand65ad6e12014-01-31 15:08:02 +010010116 return 0;
William Lallemand65ad6e12014-01-31 15:08:02 +010010117
Willy Tarreau6b952c82018-09-10 17:45:34 +020010118 smp->data.u.str.area = path.ptr;
10119 smp->data.u.str.data = path.len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010120 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010121 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010122
10123 return 1;
10124}
10125
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010126/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10127 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10128 */
10129static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010130smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010131{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010132 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010133
Willy Tarreau15e91e12015-04-04 00:52:09 +020010134 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010135 return 0;
10136
10137 if (txn->req.flags & HTTP_MSGF_VER_11)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010138 smp->data.u.str.area = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010139 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010140 smp->data.u.str.area = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010141
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010142 smp->data.u.str.data = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010143 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010144 smp->flags = SMP_F_CONST;
10145 return 1;
10146
10147}
10148
10149/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10150 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10151 */
10152static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010153smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010154{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010155 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010156
Willy Tarreau15e91e12015-04-04 00:52:09 +020010157 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010158 return 0;
10159
10160 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010161 smp->data.u.str.area = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010162 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010163 smp->data.u.str.area = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010164
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010165 smp->data.u.str.data = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010166 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010167 smp->flags = SMP_F_CONST;
10168 return 1;
10169
10170}
10171
William Lallemand65ad6e12014-01-31 15:08:02 +010010172
Willy Tarreaue333ec92012-04-16 16:26:40 +020010173/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010174 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010175 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010176 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010177 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010178 * Accepts exactly 1 argument of type string. If the input options indicate
10179 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010180 * The returned sample is of type CSTR. Can be used to parse cookies in other
10181 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010182 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010183int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010184{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010185 struct http_txn *txn;
10186 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010187 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010188 const struct http_msg *msg;
10189 const char *hdr_name;
10190 int hdr_name_len;
10191 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010192 int occ = 0;
10193 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010194
Willy Tarreau24e32d82012-04-23 23:55:44 +020010195 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010196 return 0;
10197
Willy Tarreaua890d072013-04-02 12:01:06 +020010198 if (!ctx) {
10199 /* first call */
10200 ctx = &static_hdr_ctx;
10201 ctx->idx = 0;
10202 smp->ctx.a[2] = ctx;
10203 }
10204
Willy Tarreaue333ec92012-04-16 16:26:40 +020010205 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010206
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010207 txn = smp->strm->txn;
10208 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010209
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010210 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010211 msg = &txn->req;
10212 hdr_name = "Cookie";
10213 hdr_name_len = 6;
10214 } else {
10215 msg = &txn->rsp;
10216 hdr_name = "Set-Cookie";
10217 hdr_name_len = 10;
10218 }
10219
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010220 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010221 /* no explicit occurrence and single fetch => last cookie by default */
10222 occ = -1;
10223
10224 /* OK so basically here, either we want only one value and it's the
10225 * last one, or we want to iterate over all of them and we fetch the
10226 * next one.
10227 */
10228
Willy Tarreauf37954d2018-06-15 18:31:02 +020010229 sol = ci_head(msg->chn);
Willy Tarreau37406352012-04-23 16:16:37 +020010230 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010231 /* search for the header from the beginning, we must first initialize
10232 * the search parameters.
10233 */
Willy Tarreau37406352012-04-23 16:16:37 +020010234 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010235 ctx->idx = 0;
10236 }
10237
Willy Tarreau28376d62012-04-26 21:26:10 +020010238 smp->flags |= SMP_F_VOL_HDR;
10239
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010240 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010241 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10242 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010243 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10244 goto out;
10245
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010246 if (ctx->vlen < args->data.str.data + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010247 continue;
10248
Willy Tarreau37406352012-04-23 16:16:37 +020010249 smp->ctx.a[0] = ctx->line + ctx->val;
10250 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010251 }
10252
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010253 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010254 smp->flags |= SMP_F_CONST;
Willy Tarreauab813a42018-09-10 18:41:28 +020010255 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
10256 args->data.str.area, args->data.str.data,
10257 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
10258 &smp->data.u.str.area, &smp->data.u.str.data);
Willy Tarreau37406352012-04-23 16:16:37 +020010259 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010260 found = 1;
10261 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010262 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010263 smp->flags |= SMP_F_NOT_LAST;
10264 return 1;
10265 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010266 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010267 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010268 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010269 /* all cookie headers and values were scanned. If we're looking for the
10270 * last occurrence, we may return it now.
10271 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010272 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010273 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010274 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010275}
10276
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010277/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010278 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010279 * multiple cookies may be parsed on the same line. The returned sample is of
10280 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010281 */
10282static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010283smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010284{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010285 struct http_txn *txn;
10286 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010287 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010288 const struct http_msg *msg;
10289 const char *hdr_name;
10290 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010291 int cnt;
10292 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010293 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010294
Willy Tarreau24e32d82012-04-23 23:55:44 +020010295 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010296 return 0;
10297
Willy Tarreaue333ec92012-04-16 16:26:40 +020010298 CHECK_HTTP_MESSAGE_FIRST();
10299
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010300 txn = smp->strm->txn;
10301 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010302
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010303 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010304 msg = &txn->req;
10305 hdr_name = "Cookie";
10306 hdr_name_len = 6;
10307 } else {
10308 msg = &txn->rsp;
10309 hdr_name = "Set-Cookie";
10310 hdr_name_len = 10;
10311 }
10312
Willy Tarreauf37954d2018-06-15 18:31:02 +020010313 sol = ci_head(msg->chn);
Willy Tarreau46787ed2012-04-11 17:28:40 +020010314 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010315 ctx.idx = 0;
10316 cnt = 0;
10317
10318 while (1) {
10319 /* Note: val_beg == NULL every time we need to fetch a new header */
10320 if (!val_beg) {
10321 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10322 break;
10323
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010324 if (ctx.vlen < args->data.str.data + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010325 continue;
10326
10327 val_beg = ctx.line + ctx.val;
10328 val_end = val_beg + ctx.vlen;
10329 }
10330
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010331 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010332 smp->flags |= SMP_F_CONST;
Willy Tarreauab813a42018-09-10 18:41:28 +020010333 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
10334 args->data.str.area, args->data.str.data,
10335 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
10336 &smp->data.u.str.area, &smp->data.u.str.data))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010337 cnt++;
10338 }
10339 }
10340
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010341 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010342 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010343 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010344 return 1;
10345}
10346
Willy Tarreau51539362012-05-08 12:46:28 +020010347/* Fetch an cookie's integer value. The integer value is returned. It
10348 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10349 */
10350static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010351smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010352{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010353 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010354
10355 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010356 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010357 smp->data.u.sint = strl2ic(smp->data.u.str.area,
10358 smp->data.u.str.data);
Willy Tarreau51539362012-05-08 12:46:28 +020010359 }
10360
10361 return ret;
10362}
10363
Willy Tarreau8797c062007-05-07 00:55:35 +020010364/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010365/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010366/************************************************************************/
10367
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010368/* This scans a URL-encoded query string. It takes an optionally wrapping
10369 * string whose first contigous chunk has its beginning in ctx->a[0] and end
10370 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
10371 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010372 */
David Cournapeau16023ee2010-12-23 20:55:41 +090010373static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010374smp_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 +090010375{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010376 const char *vstart, *vend;
Willy Tarreau83061a82018-07-13 11:56:34 +020010377 struct buffer *temp;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010378 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020010379
Willy Tarreauab813a42018-09-10 18:41:28 +020010380 if (!http_find_next_url_param(chunks, name, name_len,
10381 &vstart, &vend, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010382 return 0;
10383
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010384 /* Create sample. If the value is contiguous, return the pointer as CONST,
10385 * if the value is wrapped, copy-it in a buffer.
10386 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010387 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010388 if (chunks[2] &&
10389 vstart >= chunks[0] && vstart <= chunks[1] &&
10390 vend >= chunks[2] && vend <= chunks[3]) {
10391 /* Wrapped case. */
10392 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010393 memcpy(temp->area, vstart, chunks[1] - vstart);
10394 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
10395 vend - chunks[2]);
10396 smp->data.u.str.area = temp->area;
10397 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010398 } else {
10399 /* Contiguous case. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010400 smp->data.u.str.area = (char *)vstart;
10401 smp->data.u.str.data = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010402 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10403 }
10404
10405 /* Update context, check wrapping. */
10406 chunks[0] = vend;
10407 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
10408 chunks[1] = chunks[3];
10409 chunks[2] = NULL;
10410 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010411
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010412 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010413 smp->flags |= SMP_F_NOT_LAST;
10414
David Cournapeau16023ee2010-12-23 20:55:41 +090010415 return 1;
10416}
10417
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010418/* This function iterates over each parameter of the query string. It uses
10419 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010420 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
10421 * An optional parameter name is passed in args[0], otherwise any parameter is
10422 * considered. It supports an optional delimiter argument for the beginning of
10423 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010424 */
10425static int
10426smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
10427{
10428 struct http_msg *msg;
10429 char delim = '?';
10430 const char *name;
10431 int name_len;
10432
Dragan Dosen26f77e52015-05-25 10:02:11 +020010433 if (!args ||
10434 (args[0].type && args[0].type != ARGT_STR) ||
10435 (args[1].type && args[1].type != ARGT_STR))
10436 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010437
Dragan Dosen26f77e52015-05-25 10:02:11 +020010438 name = "";
10439 name_len = 0;
10440 if (args->type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010441 name = args->data.str.area;
10442 name_len = args->data.str.data;
Dragan Dosen26f77e52015-05-25 10:02:11 +020010443 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010444
Dragan Dosen26f77e52015-05-25 10:02:11 +020010445 if (args[1].type)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010446 delim = *args[1].data.str.area;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010447
Dragan Dosen26f77e52015-05-25 10:02:11 +020010448 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010449 CHECK_HTTP_MESSAGE_FIRST();
10450
10451 msg = &smp->strm->txn->req;
10452
Willy Tarreauab813a42018-09-10 18:41:28 +020010453 smp->ctx.a[0] = http_find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
10454 msg->sl.rq.u_l, delim);
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010455 if (!smp->ctx.a[0])
10456 return 0;
10457
Willy Tarreauf37954d2018-06-15 18:31:02 +020010458 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010459
10460 /* Assume that the context is filled with NULL pointer
10461 * before the first call.
10462 * smp->ctx.a[2] = NULL;
10463 * smp->ctx.a[3] = NULL;
10464 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010465 }
10466
10467 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
10468}
10469
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010470/* This function iterates over each parameter of the body. This requires
10471 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010472 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
10473 * contigous part of the body, and optionally ctx->a[2..3] to reference the
10474 * optional second part if the body wraps at the end of the buffer. An optional
10475 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010476 */
10477static int
10478smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
10479{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010480 struct http_msg *msg;
10481 unsigned long len;
10482 unsigned long block1;
10483 char *body;
10484 const char *name;
10485 int name_len;
10486
10487 if (!args || (args[0].type && args[0].type != ARGT_STR))
10488 return 0;
10489
10490 name = "";
10491 name_len = 0;
10492 if (args[0].type == ARGT_STR) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010493 name = args[0].data.str.area;
10494 name_len = args[0].data.str.data;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010495 }
10496
10497 if (!smp->ctx.a[0]) { // first call, find the query string
10498 CHECK_HTTP_MESSAGE_FIRST();
10499
10500 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010501 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010502 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010503 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010504
10505 len = http_body_bytes(msg);
Willy Tarreau188e2302018-06-15 11:11:53 +020010506 body = c_ptr(msg->chn, -http_data_rewind(msg));
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010507
10508 block1 = len;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020010509 if (block1 > b_wrap(&msg->chn->buf) - body)
10510 block1 = b_wrap(&msg->chn->buf) - body;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010511
10512 if (block1 == len) {
10513 /* buffer is not wrapped (or empty) */
10514 smp->ctx.a[0] = body;
10515 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010516
10517 /* Assume that the context is filled with NULL pointer
10518 * before the first call.
10519 * smp->ctx.a[2] = NULL;
10520 * smp->ctx.a[3] = NULL;
10521 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010522 }
10523 else {
10524 /* buffer is wrapped, we need to defragment it */
10525 smp->ctx.a[0] = body;
10526 smp->ctx.a[1] = body + block1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +020010527 smp->ctx.a[2] = b_orig(&msg->chn->buf);
10528 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020010529 }
10530 }
10531 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
10532}
10533
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010534/* Return the signed integer value for the specified url parameter (see url_param
10535 * above).
10536 */
10537static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010538smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010539{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010540 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010541
10542 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010543 smp->data.type = SMP_T_SINT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010544 smp->data.u.sint = strl2ic(smp->data.u.str.area,
10545 smp->data.u.str.data);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010546 }
10547
10548 return ret;
10549}
10550
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010551/* This produces a 32-bit hash of the concatenation of the first occurrence of
10552 * the Host header followed by the path component if it begins with a slash ('/').
10553 * This means that '*' will not be added, resulting in exactly the first Host
10554 * entry. If no Host header is found, then the path is used. The resulting value
10555 * is hashed using the url hash followed by a full avalanche hash and provides a
10556 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10557 * high-traffic sites without having to store whole paths.
10558 * this differs from the base32 functions in that it includes the url parameters
10559 * as well as the path
10560 */
10561static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010562smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010563{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010564 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010565 struct hdr_ctx ctx;
10566 unsigned int hash = 0;
10567 char *ptr, *beg, *end;
10568 int len;
10569
10570 CHECK_HTTP_MESSAGE_FIRST();
10571
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010572 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010573 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +020010574 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010575 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10576 ptr = ctx.line + ctx.val;
10577 len = ctx.vlen;
10578 while (len--)
10579 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10580 }
10581
10582 /* now retrieve the path */
Willy Tarreauf37954d2018-06-15 18:31:02 +020010583 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau6b952c82018-09-10 17:45:34 +020010584 beg = http_txn_get_path(txn);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010585 if (!beg)
10586 beg = end;
10587
10588 for (ptr = beg; ptr < end ; ptr++);
10589
10590 if (beg < ptr && *beg == '/') {
10591 while (beg < ptr)
10592 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10593 }
10594 hash = full_hash(hash);
10595
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010596 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010597 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010598 smp->flags = SMP_F_VOL_1ST;
10599 return 1;
10600}
10601
10602/* This concatenates the source address with the 32-bit hash of the Host and
10603 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10604 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10605 * on the source address length. The URL hash is stored before the address so
10606 * that in environments where IPv6 is insignificant, truncating the output to
10607 * 8 bytes would still work.
10608 */
10609static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010610smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010611{
Willy Tarreau83061a82018-07-13 11:56:34 +020010612 struct buffer *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010613 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010614
Dragan Dosendb5af612016-06-16 11:23:01 +020010615 if (!cli_conn)
10616 return 0;
10617
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010618 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010619 return 0;
10620
10621 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010622 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
10623 temp->data += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010624
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010625 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010626 case AF_INET:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010627 memcpy(temp->area + temp->data,
10628 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
10629 4);
10630 temp->data += 4;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010631 break;
10632 case AF_INET6:
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010633 memcpy(temp->area + temp->data,
10634 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
10635 16);
10636 temp->data += 16;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010637 break;
10638 default:
10639 return 0;
10640 }
10641
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010642 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010643 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010644 return 1;
10645}
10646
Willy Tarreau185b5c42012-04-26 15:11:51 +020010647/* This function is used to validate the arguments passed to any "hdr" fetch
10648 * keyword. These keywords support an optional positive or negative occurrence
10649 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10650 * is assumed that the types are already the correct ones. Returns 0 on error,
10651 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10652 * error message in case of error, that the caller is responsible for freeing.
10653 * The initial location must either be freeable or NULL.
10654 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010010655int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020010656{
10657 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010658 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010659 return 0;
10660 }
10661 return 1;
10662}
10663
Willy Tarreau276fae92013-07-25 14:36:01 +020010664/* takes an UINT value on input supposed to represent the time since EPOCH,
10665 * adds an optional offset found in args[0] and emits a string representing
10666 * the date in RFC-1123/5322 format.
10667 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010668static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020010669{
Cyril Bontéf78d8962016-01-22 19:40:28 +010010670 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020010671 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
Willy Tarreau83061a82018-07-13 11:56:34 +020010672 struct buffer *temp;
Willy Tarreau276fae92013-07-25 14:36:01 +020010673 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010674 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010675 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020010676
10677 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010678 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020010679 curr_date += args[0].data.sint;
10680
10681 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020010682 if (!tm)
10683 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020010684
10685 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010686 temp->data = snprintf(temp->area, temp->size - temp->data,
10687 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10688 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon],
10689 1900+tm->tm_year,
10690 tm->tm_hour, tm->tm_min, tm->tm_sec);
Willy Tarreau276fae92013-07-25 14:36:01 +020010691
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010692 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010693 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020010694 return 1;
10695}
10696
Thierry FOURNIERad903512014-04-11 17:51:01 +020010697/* Match language range with language tag. RFC2616 14.4:
10698 *
10699 * A language-range matches a language-tag if it exactly equals
10700 * the tag, or if it exactly equals a prefix of the tag such
10701 * that the first tag character following the prefix is "-".
10702 *
10703 * Return 1 if the strings match, else return 0.
10704 */
10705static inline int language_range_match(const char *range, int range_len,
10706 const char *tag, int tag_len)
10707{
10708 const char *end = range + range_len;
10709 const char *tend = tag + tag_len;
10710 while (range < end) {
10711 if (*range == '-' && tag == tend)
10712 return 1;
10713 if (*range != *tag || tag == tend)
10714 return 0;
10715 range++;
10716 tag++;
10717 }
10718 /* Return true only if the last char of the tag is matched. */
10719 return tag == tend;
10720}
10721
10722/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010723static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020010724{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010725 const char *al = smp->data.u.str.area;
10726 const char *end = al + smp->data.u.str.data;
Thierry FOURNIERad903512014-04-11 17:51:01 +020010727 const char *token;
10728 int toklen;
10729 int qvalue;
10730 const char *str;
10731 const char *w;
10732 int best_q = 0;
10733
10734 /* Set the constant to the sample, because the output of the
10735 * function will be peek in the constant configuration string.
10736 */
10737 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010738 smp->data.u.str.size = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010739 smp->data.u.str.area = "";
10740 smp->data.u.str.data = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020010741
10742 /* Parse the accept language */
10743 while (1) {
10744
10745 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020010746 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020010747 al++;
10748 if (al >= end)
10749 break;
10750
10751 /* Start of the fisrt word. */
10752 token = al;
10753
10754 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020010755 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020010756 al++;
10757 if (al == token)
10758 goto expect_comma;
10759
10760 /* Length of the token. */
10761 toklen = al - token;
10762 qvalue = 1000;
10763
10764 /* Check if the token exists in the list. If the token not exists,
10765 * jump to the next token.
10766 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010767 str = args[0].data.str.area;
Thierry FOURNIERad903512014-04-11 17:51:01 +020010768 w = str;
10769 while (1) {
10770 if (*str == ';' || *str == '\0') {
10771 if (language_range_match(token, toklen, w, str-w))
10772 goto look_for_q;
10773 if (*str == '\0')
10774 goto expect_comma;
10775 w = str + 1;
10776 }
10777 str++;
10778 }
10779 goto expect_comma;
10780
10781look_for_q:
10782
10783 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020010784 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020010785 al++;
10786 if (al >= end)
10787 goto process_value;
10788
10789 /* If ',' is found, process the result */
10790 if (*al == ',')
10791 goto process_value;
10792
10793 /* If the character is different from ';', look
10794 * for the end of the header part in best effort.
10795 */
10796 if (*al != ';')
10797 goto expect_comma;
10798
10799 /* Assumes that the char is ';', now expect "q=". */
10800 al++;
10801
10802 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020010803 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020010804 al++;
10805 if (al >= end)
10806 goto process_value;
10807
10808 /* Expect 'q'. If no 'q', continue in best effort */
10809 if (*al != 'q')
10810 goto process_value;
10811 al++;
10812
10813 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020010814 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020010815 al++;
10816 if (al >= end)
10817 goto process_value;
10818
10819 /* Expect '='. If no '=', continue in best effort */
10820 if (*al != '=')
10821 goto process_value;
10822 al++;
10823
10824 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020010825 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020010826 al++;
10827 if (al >= end)
10828 goto process_value;
10829
10830 /* Parse the q value. */
Willy Tarreauab813a42018-09-10 18:41:28 +020010831 qvalue = http_parse_qvalue(al, &al);
Thierry FOURNIERad903512014-04-11 17:51:01 +020010832
10833process_value:
10834
10835 /* If the new q value is the best q value, then store the associated
10836 * language in the response. If qvalue is the biggest value (1000),
10837 * break the process.
10838 */
10839 if (qvalue > best_q) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010840 smp->data.u.str.area = (char *)w;
10841 smp->data.u.str.data = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020010842 if (qvalue >= 1000)
10843 break;
10844 best_q = qvalue;
10845 }
10846
10847expect_comma:
10848
10849 /* Expect comma or end. If the end is detected, quit the loop. */
10850 while (al < end && *al != ',')
10851 al++;
10852 if (al >= end)
10853 break;
10854
10855 /* Comma is found, jump it and restart the analyzer. */
10856 al++;
10857 }
10858
10859 /* Set default value if required. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010860 if (smp->data.u.str.data == 0 && args[1].type == ARGT_STR) {
10861 smp->data.u.str.area = args[1].data.str.area;
10862 smp->data.u.str.data = args[1].data.str.data;
Thierry FOURNIERad903512014-04-11 17:51:01 +020010863 }
10864
10865 /* Return true only if a matching language was found. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010866 return smp->data.u.str.data != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020010867}
10868
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020010869/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010870static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020010871{
Willy Tarreau9c768fd2018-08-22 05:08:57 +020010872 int len;
10873
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020010874 /* If the constant flag is set or if not size is avalaible at
10875 * the end of the buffer, copy the string in other buffer
10876 * before decoding.
10877 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010878 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.data) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010879 struct buffer *str = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010880 memcpy(str->area, smp->data.u.str.area, smp->data.u.str.data);
10881 smp->data.u.str.area = str->area;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010882 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020010883 smp->flags &= ~SMP_F_CONST;
10884 }
10885
10886 /* Add final \0 required by url_decode(), and convert the input string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010887 smp->data.u.str.area[smp->data.u.str.data] = '\0';
Willy Tarreau9c768fd2018-08-22 05:08:57 +020010888 len = url_decode(smp->data.u.str.area);
10889 if (len < 0)
10890 return 0;
10891 smp->data.u.str.data = len;
10892 return 1;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020010893}
10894
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010895static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
10896{
10897 struct proxy *fe = strm_fe(smp->strm);
10898 int idx, i;
10899 struct cap_hdr *hdr;
10900 int len;
10901
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010902 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010903 return 0;
10904
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010905 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010906
10907 /* Check the availibity of the capture id. */
10908 if (idx > fe->nb_req_cap - 1)
10909 return 0;
10910
10911 /* Look for the original configuration. */
10912 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
10913 hdr != NULL && i != idx ;
10914 i--, hdr = hdr->next);
10915 if (!hdr)
10916 return 0;
10917
10918 /* check for the memory allocation */
10919 if (smp->strm->req_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010010920 smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010921 if (smp->strm->req_cap[hdr->index] == NULL)
10922 return 0;
10923
10924 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010925 len = smp->data.u.str.data;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010926 if (len > hdr->len)
10927 len = hdr->len;
10928
10929 /* Capture input data. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010930 memcpy(smp->strm->req_cap[idx], smp->data.u.str.area, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010931 smp->strm->req_cap[idx][len] = '\0';
10932
10933 return 1;
10934}
10935
10936static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
10937{
10938 struct proxy *fe = strm_fe(smp->strm);
10939 int idx, i;
10940 struct cap_hdr *hdr;
10941 int len;
10942
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010943 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010944 return 0;
10945
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010946 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010947
10948 /* Check the availibity of the capture id. */
10949 if (idx > fe->nb_rsp_cap - 1)
10950 return 0;
10951
10952 /* Look for the original configuration. */
10953 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
10954 hdr != NULL && i != idx ;
10955 i--, hdr = hdr->next);
10956 if (!hdr)
10957 return 0;
10958
10959 /* check for the memory allocation */
10960 if (smp->strm->res_cap[hdr->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010010961 smp->strm->res_cap[hdr->index] = pool_alloc(hdr->pool);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010962 if (smp->strm->res_cap[hdr->index] == NULL)
10963 return 0;
10964
10965 /* Check length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010966 len = smp->data.u.str.data;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010967 if (len > hdr->len)
10968 len = hdr->len;
10969
10970 /* Capture input data. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020010971 memcpy(smp->strm->res_cap[idx], smp->data.u.str.area, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020010972 smp->strm->res_cap[idx][len] = '\0';
10973
10974 return 1;
10975}
10976
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010977/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010010978 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010979 * the relevant part of the request line accordingly. Then it updates various
10980 * pointers to the next elements which were moved, and the total buffer length.
10981 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010010982 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
10983 * error, though this can be revisited when this code is finally exploited.
10984 *
10985 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
10986 * query string and 3 to replace uri.
10987 *
10988 * In query string case, the mark question '?' must be set at the start of the
10989 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010990 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010010991int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020010992 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010993{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020010994 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010995 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010010996 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010010997 int delta;
10998
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010010999 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011000 case 0: // method
Willy Tarreauf37954d2018-06-15 18:31:02 +020011001 cur_ptr = ci_head(&s->req);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011002 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11003
11004 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011005 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011006 txn->req.sl.rq.m_l += delta;
11007 txn->req.sl.rq.u += delta;
11008 txn->req.sl.rq.v += delta;
11009 break;
11010
11011 case 1: // path
Willy Tarreau6b952c82018-09-10 17:45:34 +020011012 cur_ptr = http_txn_get_path(txn);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011013 if (!cur_ptr)
Willy Tarreauf37954d2018-06-15 18:31:02 +020011014 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011015
11016 cur_end = cur_ptr;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011017 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 +010011018 cur_end++;
11019
11020 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011021 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011022 txn->req.sl.rq.u_l += delta;
11023 txn->req.sl.rq.v += delta;
11024 break;
11025
11026 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011027 offset = 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +020011028 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011029 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11030 while (cur_ptr < cur_end && *cur_ptr != '?')
11031 cur_ptr++;
11032
11033 /* skip the question mark or indicate that we must insert it
11034 * (but only if the format string is not empty then).
11035 */
11036 if (cur_ptr < cur_end)
11037 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011038 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011039 offset = 0;
11040
11041 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011042 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011043 txn->req.sl.rq.u_l += delta;
11044 txn->req.sl.rq.v += delta;
11045 break;
11046
11047 case 3: // uri
Willy Tarreauf37954d2018-06-15 18:31:02 +020011048 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011049 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11050
11051 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011052 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011053 txn->req.sl.rq.u_l += delta;
11054 txn->req.sl.rq.v += delta;
11055 break;
11056
11057 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011058 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011059 }
11060
11061 /* commit changes and adjust end of message */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020011062 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011063 txn->req.sl.rq.l += delta;
11064 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011065 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011066 return 0;
11067}
11068
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011069/* This function replace the HTTP status code and the associated message. The
11070 * variable <status> contains the new status code. This function never fails.
11071 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011072void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011073{
11074 struct http_txn *txn = s->txn;
11075 char *cur_ptr, *cur_end;
11076 int delta;
11077 char *res;
11078 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011079 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011080 int msg_len;
11081
11082 chunk_reset(&trash);
11083
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011084 res = ultoa_o(status, trash.area, trash.size);
11085 c_l = res - trash.area;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011086
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011087 trash.area[c_l] = ' ';
11088 trash.data = c_l + 1;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011089
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011090 /* Do we have a custom reason format string? */
11091 if (msg == NULL)
Willy Tarreau04f1e2d2018-09-10 18:04:24 +020011092 msg = http_get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011093 msg_len = strlen(msg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011094 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
11095 trash.data += msg_len;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011096
Willy Tarreauf37954d2018-06-15 18:31:02 +020011097 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
11098 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011099
11100 /* commit changes and adjust message */
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011101 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
11102 trash.data);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011103
11104 /* adjust res line offsets and lengths */
11105 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11106 txn->rsp.sl.st.c_l = c_l;
11107 txn->rsp.sl.st.r_l = msg_len;
11108
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011109 delta = trash.data - (cur_end - cur_ptr);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011110 txn->rsp.sl.st.l += delta;
11111 txn->hdr_idx.v[0].len += delta;
11112 http_msg_move_end(&txn->rsp, delta);
11113}
11114
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011115/* This function executes one of the set-{method,path,query,uri} actions. It
11116 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011117 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011118 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011119 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11120 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011121 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011122enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011123 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011124{
Willy Tarreau83061a82018-07-13 11:56:34 +020011125 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011126 enum act_return ret = ACT_RET_ERR;
11127
11128 replace = alloc_trash_chunk();
11129 if (!replace)
11130 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011131
11132 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011133 if (rule->arg.http.action == 2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011134 replace->area[replace->data++] = '?';
11135 replace->data += build_logline(s, replace->area + replace->data,
11136 replace->size - replace->data,
11137 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011138
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011139 http_replace_req_line(rule->arg.http.action, replace->area,
11140 replace->data, px, s);
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011141
11142 ret = ACT_RET_CONT;
11143
11144leave:
11145 free_trash_chunk(replace);
11146 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011147}
11148
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011149/* This function is just a compliant action wrapper for "set-status". */
11150enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011151 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011152{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011153 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011154 return ACT_RET_CONT;
11155}
11156
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011157/* parse an http-request action among :
11158 * set-method
11159 * set-path
11160 * set-query
11161 * set-uri
11162 *
11163 * All of them accept a single argument of type string representing a log-format.
11164 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11165 * 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 +020011166 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011167 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011168enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11169 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011170{
11171 int cur_arg = *orig_arg;
11172
Thierry FOURNIER42148732015-09-02 17:17:33 +020011173 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011174
11175 switch (args[0][4]) {
11176 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011177 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011178 rule->action_ptr = http_action_set_req_line;
11179 break;
11180 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011181 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011182 rule->action_ptr = http_action_set_req_line;
11183 break;
11184 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011185 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011186 rule->action_ptr = http_action_set_req_line;
11187 break;
11188 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011189 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011190 rule->action_ptr = http_action_set_req_line;
11191 break;
11192 default:
11193 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011194 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011195 }
11196
11197 if (!*args[cur_arg] ||
11198 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11199 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011200 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011201 }
11202
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011203 LIST_INIT(&rule->arg.http.logfmt);
Olivier Houchardfbc74e82017-11-24 16:54:05 +010011204 px->conf.args.ctx = ARGC_HRQ;
11205 if (!parse_logformat_string(args[cur_arg], px, &rule->arg.http.logfmt, LOG_OPT_HTTP,
11206 (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 +010011207 return ACT_RET_PRS_ERR;
11208 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011209
11210 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011211 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011212}
11213
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011214/* parse set-status action:
11215 * This action accepts a single argument of type int representing
11216 * an http status code. It returns ACT_RET_PRS_OK on success,
11217 * ACT_RET_PRS_ERR on error.
11218 */
11219enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
11220 struct act_rule *rule, char **err)
11221{
11222 char *error;
11223
Thierry FOURNIER42148732015-09-02 17:17:33 +020011224 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011225 rule->action_ptr = action_http_set_status;
11226
11227 /* Check if an argument is available */
11228 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011229 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011230 return ACT_RET_PRS_ERR;
11231 }
11232
11233 /* convert status code as integer */
11234 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
11235 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
11236 memprintf(err, "expects an integer status code between 100 and 999");
11237 return ACT_RET_PRS_ERR;
11238 }
11239
11240 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011241
11242 /* set custom reason string */
11243 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
11244 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
11245 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
11246 (*orig_arg)++;
11247 rule->arg.status.reason = strdup(args[*orig_arg]);
11248 (*orig_arg)++;
11249 }
11250
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011251 return ACT_RET_PRS_OK;
11252}
11253
Willy Tarreau53275e82017-11-24 07:52:01 +010011254/* This function executes the "reject" HTTP action. It clears the request and
11255 * response buffer without sending any response. It can be useful as an HTTP
11256 * alternative to the silent-drop action to defend against DoS attacks, and may
11257 * also be used with HTTP/2 to close a connection instead of just a stream.
11258 * The txn status is unchanged, indicating no response was sent. The termination
11259 * flags will indicate "PR". It always returns ACT_RET_STOP.
11260 */
11261enum act_return http_action_reject(struct act_rule *rule, struct proxy *px,
11262 struct session *sess, struct stream *s, int flags)
11263{
11264 channel_abort(&s->req);
11265 channel_abort(&s->res);
11266 s->req.analysers = 0;
11267 s->res.analysers = 0;
11268
11269 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
11270 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
11271 if (sess->listener && sess->listener->counters)
11272 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
11273
11274 if (!(s->flags & SF_ERR_MASK))
11275 s->flags |= SF_ERR_PRXCOND;
11276 if (!(s->flags & SF_FINST_MASK))
11277 s->flags |= SF_FINST_R;
11278
11279 return ACT_RET_CONT;
11280}
11281
11282/* parse the "reject" action:
11283 * This action takes no argument and returns ACT_RET_PRS_OK on success,
11284 * ACT_RET_PRS_ERR on error.
11285 */
11286enum act_parse_ret parse_http_action_reject(const char **args, int *orig_arg, struct proxy *px,
11287 struct act_rule *rule, char **err)
11288{
11289 rule->action = ACT_CUSTOM;
11290 rule->action_ptr = http_action_reject;
11291 return ACT_RET_PRS_OK;
11292}
11293
Willy Tarreaua9083d02015-05-08 15:27:59 +020011294/* This function executes the "capture" action. It executes a fetch expression,
11295 * turns the result into a string and puts it in a capture slot. It always
11296 * returns 1. If an error occurs the action is cancelled, but the rule
11297 * processing continues.
11298 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011299enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011300 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011301{
Willy Tarreaua9083d02015-05-08 15:27:59 +020011302 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011303 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011304 char **cap = s->req_cap;
11305 int len;
11306
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011307 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 +020011308 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011309 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011310
11311 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011312 cap[h->index] = pool_alloc(h->pool);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011313
11314 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011315 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011316
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011317 len = key->data.u.str.data;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011318 if (len > h->len)
11319 len = h->len;
11320
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011321 memcpy(cap[h->index], key->data.u.str.area, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011322 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011323 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011324}
11325
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011326/* This function executes the "capture" action and store the result in a
11327 * capture slot if exists. It executes a fetch expression, turns the result
11328 * into a string and puts it in a capture slot. It always returns 1. If an
11329 * error occurs the action is cancelled, but the rule processing continues.
11330 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011331enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011332 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011333{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011334 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011335 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011336 char **cap = s->req_cap;
11337 struct proxy *fe = strm_fe(s);
11338 int len;
11339 int i;
11340
11341 /* Look for the original configuration. */
11342 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011343 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011344 i--, h = h->next);
11345 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011346 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011347
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011348 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 +020011349 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011350 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011351
11352 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011353 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011354
11355 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011356 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011357
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011358 len = key->data.u.str.data;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011359 if (len > h->len)
11360 len = h->len;
11361
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011362 memcpy(cap[h->index], key->data.u.str.area, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011363 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011364 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011365}
11366
Christopher Faulet29730ba2017-09-18 15:26:32 +020011367/* Check an "http-request capture" action.
11368 *
11369 * The function returns 1 in success case, otherwise, it returns 0 and err is
11370 * filled.
11371 */
11372int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
11373{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010011374 if (rule->action_ptr != http_action_req_capture_by_id)
11375 return 1;
11376
Christopher Faulet29730ba2017-09-18 15:26:32 +020011377 if (rule->arg.capid.idx >= px->nb_req_cap) {
11378 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
11379 rule->arg.capid.idx);
11380 return 0;
11381 }
11382
11383 return 1;
11384}
11385
Willy Tarreaua9083d02015-05-08 15:27:59 +020011386/* parse an "http-request capture" action. It takes a single argument which is
11387 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011388 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011389 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020011390 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011391enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
11392 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011393{
11394 struct sample_expr *expr;
11395 struct cap_hdr *hdr;
11396 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020011397 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011398
11399 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
11400 if (strcmp(args[cur_arg], "if") == 0 ||
11401 strcmp(args[cur_arg], "unless") == 0)
11402 break;
11403
11404 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011405 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011406 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011407 }
11408
Willy Tarreaua9083d02015-05-08 15:27:59 +020011409 cur_arg = *orig_arg;
11410 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
11411 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011412 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011413
11414 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
11415 memprintf(err,
11416 "fetch method '%s' extracts information from '%s', none of which is available here",
11417 args[cur_arg-1], sample_src_names(expr->fetch->use));
11418 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011419 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011420 }
11421
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011422 if (!args[cur_arg] || !*args[cur_arg]) {
11423 memprintf(err, "expects 'len or 'id'");
11424 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011425 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011426 }
11427
Willy Tarreaua9083d02015-05-08 15:27:59 +020011428 if (strcmp(args[cur_arg], "len") == 0) {
11429 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011430
11431 if (!(px->cap & PR_CAP_FE)) {
11432 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011433 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011434 }
11435
Olivier Houchardfbc74e82017-11-24 16:54:05 +010011436 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011437
Willy Tarreaua9083d02015-05-08 15:27:59 +020011438 if (!args[cur_arg]) {
11439 memprintf(err, "missing length value");
11440 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011441 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011442 }
11443 /* we copy the table name for now, it will be resolved later */
11444 len = atoi(args[cur_arg]);
11445 if (len <= 0) {
11446 memprintf(err, "length must be > 0");
11447 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011448 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011449 }
11450 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011451
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011452 if (!len) {
11453 memprintf(err, "a positive 'len' argument is mandatory");
11454 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011455 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011456 }
11457
Vincent Bernat02779b62016-04-03 13:48:43 +020011458 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011459 hdr->next = px->req_cap;
11460 hdr->name = NULL; /* not a header capture */
11461 hdr->namelen = 0;
11462 hdr->len = len;
11463 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
11464 hdr->index = px->nb_req_cap++;
11465
11466 px->req_cap = hdr;
11467 px->to_log |= LW_REQHDR;
11468
Thierry FOURNIER42148732015-09-02 17:17:33 +020011469 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011470 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011471 rule->arg.cap.expr = expr;
11472 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011473 }
11474
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011475 else if (strcmp(args[cur_arg], "id") == 0) {
11476 int id;
11477 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011478
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011479 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011480
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011481 if (!args[cur_arg]) {
11482 memprintf(err, "missing id value");
11483 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011484 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011485 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020011486
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011487 id = strtol(args[cur_arg], &error, 10);
11488 if (*error != '\0') {
11489 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
11490 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011491 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011492 }
11493 cur_arg++;
11494
Olivier Houchardfbc74e82017-11-24 16:54:05 +010011495 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011496
Thierry FOURNIER42148732015-09-02 17:17:33 +020011497 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011498 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020011499 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011500 rule->arg.capid.expr = expr;
11501 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011502 }
11503
11504 else {
11505 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
11506 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011507 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011508 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020011509
11510 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011511 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011512}
11513
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011514/* This function executes the "capture" action and store the result in a
11515 * capture slot if exists. It executes a fetch expression, turns the result
11516 * into a string and puts it in a capture slot. It always returns 1. If an
11517 * error occurs the action is cancelled, but the rule processing continues.
11518 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011519enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011520 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011521{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011522 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011523 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011524 char **cap = s->res_cap;
11525 struct proxy *fe = strm_fe(s);
11526 int len;
11527 int i;
11528
11529 /* Look for the original configuration. */
11530 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011531 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011532 i--, h = h->next);
11533 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011534 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011535
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011536 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 +020011537 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011538 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011539
11540 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +010011541 cap[h->index] = pool_alloc(h->pool);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011542
11543 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011544 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011545
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011546 len = key->data.u.str.data;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011547 if (len > h->len)
11548 len = h->len;
11549
Willy Tarreau843b7cb2018-07-13 10:54:26 +020011550 memcpy(cap[h->index], key->data.u.str.area, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011551 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011552 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011553}
11554
Christopher Faulet29730ba2017-09-18 15:26:32 +020011555/* Check an "http-response capture" action.
11556 *
11557 * The function returns 1 in success case, otherwise, it returns 0 and err is
11558 * filled.
11559 */
11560int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
11561{
Christopher Fauletfd608dd2017-12-04 09:45:15 +010011562 if (rule->action_ptr != http_action_res_capture_by_id)
11563 return 1;
11564
Christopher Faulet29730ba2017-09-18 15:26:32 +020011565 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
11566 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
11567 rule->arg.capid.idx);
11568 return 0;
11569 }
11570
11571 return 1;
11572}
11573
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011574/* parse an "http-response capture" action. It takes a single argument which is
11575 * a sample fetch expression. It stores the expression into arg->act.p[0] and
11576 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011577 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011578 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011579enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
11580 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011581{
11582 struct sample_expr *expr;
11583 int cur_arg;
11584 int id;
11585 char *error;
11586
11587 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
11588 if (strcmp(args[cur_arg], "if") == 0 ||
11589 strcmp(args[cur_arg], "unless") == 0)
11590 break;
11591
11592 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020011593 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011594 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011595 }
11596
11597 cur_arg = *orig_arg;
11598 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
11599 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011600 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011601
11602 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
11603 memprintf(err,
11604 "fetch method '%s' extracts information from '%s', none of which is available here",
11605 args[cur_arg-1], sample_src_names(expr->fetch->use));
11606 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011607 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011608 }
11609
11610 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020011611 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011612 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011613 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011614 }
11615
11616 if (strcmp(args[cur_arg], "id") != 0) {
11617 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
11618 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011619 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011620 }
11621
11622 cur_arg++;
11623
11624 if (!args[cur_arg]) {
11625 memprintf(err, "missing id value");
11626 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011627 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011628 }
11629
11630 id = strtol(args[cur_arg], &error, 10);
11631 if (*error != '\0') {
11632 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
11633 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011634 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011635 }
11636 cur_arg++;
11637
Olivier Houchardfbc74e82017-11-24 16:54:05 +010011638 px->conf.args.ctx = ARGC_CAP;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011639
Thierry FOURNIER42148732015-09-02 17:17:33 +020011640 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011641 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020011642 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011643 rule->arg.capid.expr = expr;
11644 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011645
11646 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011647 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011648}
11649
William Lallemand73025dd2014-04-24 14:38:37 +020011650/*
11651 * Return the struct http_req_action_kw associated to a keyword.
11652 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020011653struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020011654{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020011655 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020011656}
11657
11658/*
11659 * Return the struct http_res_action_kw associated to a keyword.
11660 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020011661struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020011662{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020011663 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020011664}
11665
Willy Tarreau12207b32016-11-22 19:48:51 +010011666
Willy Tarreau4a568972010-05-12 08:08:50 +020011667/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011668/* All supported ACL keywords must be declared here. */
11669/************************************************************************/
11670
11671/* Note: must not be declared <const> as its list will be overwritten.
11672 * Please take care of keeping this list alphabetically sorted.
11673 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011674static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011675 { "base", "base", PAT_MATCH_STR },
11676 { "base_beg", "base", PAT_MATCH_BEG },
11677 { "base_dir", "base", PAT_MATCH_DIR },
11678 { "base_dom", "base", PAT_MATCH_DOM },
11679 { "base_end", "base", PAT_MATCH_END },
11680 { "base_len", "base", PAT_MATCH_LEN },
11681 { "base_reg", "base", PAT_MATCH_REG },
11682 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011683
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011684 { "cook", "req.cook", PAT_MATCH_STR },
11685 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11686 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11687 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11688 { "cook_end", "req.cook", PAT_MATCH_END },
11689 { "cook_len", "req.cook", PAT_MATCH_LEN },
11690 { "cook_reg", "req.cook", PAT_MATCH_REG },
11691 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011692
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011693 { "hdr", "req.hdr", PAT_MATCH_STR },
11694 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11695 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11696 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11697 { "hdr_end", "req.hdr", PAT_MATCH_END },
11698 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11699 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11700 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011701
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011702 /* these two declarations uses strings with list storage (in place
11703 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11704 * and delete functions are relative to the list management. The parse
11705 * and match method are related to the corresponding fetch methods. This
11706 * is very particular ACL declaration mode.
11707 */
11708 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11709 { "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 +020011710
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011711 { "path", "path", PAT_MATCH_STR },
11712 { "path_beg", "path", PAT_MATCH_BEG },
11713 { "path_dir", "path", PAT_MATCH_DIR },
11714 { "path_dom", "path", PAT_MATCH_DOM },
11715 { "path_end", "path", PAT_MATCH_END },
11716 { "path_len", "path", PAT_MATCH_LEN },
11717 { "path_reg", "path", PAT_MATCH_REG },
11718 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011719
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011720 { "req_ver", "req.ver", PAT_MATCH_STR },
11721 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011722
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011723 { "scook", "res.cook", PAT_MATCH_STR },
11724 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11725 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11726 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11727 { "scook_end", "res.cook", PAT_MATCH_END },
11728 { "scook_len", "res.cook", PAT_MATCH_LEN },
11729 { "scook_reg", "res.cook", PAT_MATCH_REG },
11730 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011731
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011732 { "shdr", "res.hdr", PAT_MATCH_STR },
11733 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11734 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11735 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11736 { "shdr_end", "res.hdr", PAT_MATCH_END },
11737 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11738 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11739 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011740
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011741 { "url", "url", PAT_MATCH_STR },
11742 { "url_beg", "url", PAT_MATCH_BEG },
11743 { "url_dir", "url", PAT_MATCH_DIR },
11744 { "url_dom", "url", PAT_MATCH_DOM },
11745 { "url_end", "url", PAT_MATCH_END },
11746 { "url_len", "url", PAT_MATCH_LEN },
11747 { "url_reg", "url", PAT_MATCH_REG },
11748 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011749
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011750 { "urlp", "urlp", PAT_MATCH_STR },
11751 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11752 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11753 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11754 { "urlp_end", "urlp", PAT_MATCH_END },
11755 { "urlp_len", "urlp", PAT_MATCH_LEN },
11756 { "urlp_reg", "urlp", PAT_MATCH_REG },
11757 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011758
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011759 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011760}};
11761
11762/************************************************************************/
11763/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011764/************************************************************************/
11765/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011766static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011767 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011768 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011769 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11770
Willy Tarreau87b09662015-04-03 00:22:06 +020011771 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011772 { "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 +020011773
11774 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011775 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11776 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11777 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011778
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011779 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
11780 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011781
Willy Tarreau409bcde2013-01-08 00:31:00 +010011782 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11783 * are only here to match the ACL's name, are request-only and are used
11784 * for ACL compatibility only.
11785 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011786 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11787 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011788 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
11789 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011790
11791 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11792 * only here to match the ACL's name, are request-only and are used for
11793 * ACL compatibility only.
11794 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011795 { "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 +020011796 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011797 { "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 +020011798 { "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 +010011799
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011800 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011801 { "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 +010011802 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011803 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011804 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011805 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011806
11807 /* HTTP protocol on the request path */
11808 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011809 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011810
11811 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011812 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11813 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011814
Willy Tarreaua5910cc2015-05-02 00:46:08 +020011815 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011816 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
11817 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011818 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020011819
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020011820 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020011821 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11822
Willy Tarreau18ed2562013-01-14 15:56:36 +010011823 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011824 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11825 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011826
Willy Tarreau18ed2562013-01-14 15:56:36 +010011827 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011828 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011829 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
11830 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011831
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011832 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011833 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011834 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011835 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011836 { "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 +010011837 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011838 { "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 +010011839
11840 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011841 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011842 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
11843 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011844
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011845 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011846 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011847 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011848 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011849 { "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 +010011850 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011851 { "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 +010011852
Willy Tarreau409bcde2013-01-08 00:31:00 +010011853 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011854 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011855 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
11856 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011857 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011858
11859 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011860 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011861 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011862 { "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 +020011863 { "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 +010011864
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011865 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020011866 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011867 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011868 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011869 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011870 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011871 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011872 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11873 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011874 { "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 +010011875 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011876}};
11877
Willy Tarreau8797c062007-05-07 00:55:35 +020011878
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011879/************************************************************************/
11880/* All supported converter keywords must be declared here. */
11881/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020011882/* Note: must not be declared <const> as its list will be overwritten */
11883static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011884 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020011885 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011886 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
11887 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011888 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011889 { NULL, NULL, 0, 0, 0 },
11890}};
11891
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011892
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011893/************************************************************************/
11894/* All supported http-request action keywords must be declared here. */
11895/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020011896struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011897 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020011898 { "capture", parse_http_req_capture },
Willy Tarreau53275e82017-11-24 07:52:01 +010011899 { "reject", parse_http_action_reject },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011900 { "set-method", parse_set_req_line },
11901 { "set-path", parse_set_req_line },
11902 { "set-query", parse_set_req_line },
11903 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020011904 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011905 }
11906};
11907
Thierry FOURNIER36481b82015-08-19 09:01:53 +020011908struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011909 .kw = {
11910 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011911 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011912 { NULL, NULL }
11913 }
11914};
11915
Willy Tarreau8797c062007-05-07 00:55:35 +020011916__attribute__((constructor))
11917static void __http_protocol_init(void)
11918{
11919 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011920 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011921 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011922 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011923 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020011924}
11925
11926
Willy Tarreau58f10d72006-12-04 02:26:12 +010011927/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011928 * Local variables:
11929 * c-indent-level: 8
11930 * c-basic-offset: 8
11931 * End:
11932 */