blob: 348116003de7134834aea4164a868231aa10bc01 [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
Willy Tarreau80587432006-12-24 17:47:20 +010090void init_proto_http()
91{
Willy Tarreau332f8bf2007-05-13 21:36:56 +020092 /* memory allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +010093 pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
94 pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +010095}
Willy Tarreaubaaee002006-06-26 02:48:02 +020096
Willy Tarreau53b6c742006-12-17 13:37:46 +010097/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +010098 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +020099 * the last CRLF. <len> bytes are copied, not counting the CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100100 * The header is also automatically added to the index <hdr_idx>, and the end
101 * of headers is automatically adjusted. The number of bytes added is returned
102 * on success, otherwise <0 is returned indicating an error.
103 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100104int http_header_add_tail2(struct http_msg *msg,
105 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100106{
107 int bytes;
108
Willy Tarreau4d893d42018-07-12 15:43:32 +0200109 bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100110 if (!bytes)
111 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100112 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100113 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
114}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200116/* Find the first or next occurrence of header <name> in message buffer <sol>
117 * using headers index <idx>, and return it in the <ctx> structure. This
118 * structure holds everything necessary to use the header and find next
119 * occurrence. If its <idx> member is 0, the header is searched from the
120 * beginning. Otherwise, the next occurrence is returned. The function returns
121 * 1 when it finds a value, and 0 when there is no more. It is very similar to
122 * http_find_header2() except that it is designed to work with full-line headers
123 * whose comma is not a delimiter but is part of the syntax. As a special case,
124 * if ctx->val is NULL when searching for a new values of a header, the current
125 * header is rescanned. This allows rescanning after a header deletion.
126 */
127int http_find_full_header2(const char *name, int len,
128 char *sol, struct hdr_idx *idx,
129 struct hdr_ctx *ctx)
130{
131 char *eol, *sov;
132 int cur_idx, old_idx;
133
134 cur_idx = ctx->idx;
135 if (cur_idx) {
136 /* We have previously returned a header, let's search another one */
137 sol = ctx->line;
138 eol = sol + idx->v[cur_idx].len;
139 goto next_hdr;
140 }
141
142 /* first request for this header */
143 sol += hdr_idx_first_pos(idx);
144 old_idx = 0;
145 cur_idx = hdr_idx_first_idx(idx);
146 while (cur_idx) {
147 eol = sol + idx->v[cur_idx].len;
148
149 if (len == 0) {
150 /* No argument was passed, we want any header.
151 * To achieve this, we simply build a fake request. */
152 while (sol + len < eol && sol[len] != ':')
153 len++;
154 name = sol;
155 }
156
157 if ((len < eol - sol) &&
158 (sol[len] == ':') &&
159 (strncasecmp(sol, name, len) == 0)) {
160 ctx->del = len;
161 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100162 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200163 sov++;
164
165 ctx->line = sol;
166 ctx->prev = old_idx;
167 ctx->idx = cur_idx;
168 ctx->val = sov - sol;
169 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100170 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200171 eol--;
172 ctx->tws++;
173 }
174 ctx->vlen = eol - sov;
175 return 1;
176 }
177 next_hdr:
178 sol = eol + idx->v[cur_idx].cr + 1;
179 old_idx = cur_idx;
180 cur_idx = idx->v[cur_idx].next;
181 }
182 return 0;
183}
184
Willy Tarreauc90dc232015-02-20 13:51:36 +0100185/* Find the first or next header field in message buffer <sol> using headers
186 * index <idx>, and return it in the <ctx> structure. This structure holds
187 * everything necessary to use the header and find next occurrence. If its
188 * <idx> member is 0, the first header is retrieved. Otherwise, the next
189 * occurrence is returned. The function returns 1 when it finds a value, and
190 * 0 when there is no more. It is equivalent to http_find_full_header2() with
191 * no header name.
192 */
193int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
194{
195 char *eol, *sov;
196 int cur_idx, old_idx;
197 int len;
198
199 cur_idx = ctx->idx;
200 if (cur_idx) {
201 /* We have previously returned a header, let's search another one */
202 sol = ctx->line;
203 eol = sol + idx->v[cur_idx].len;
204 goto next_hdr;
205 }
206
207 /* first request for this header */
208 sol += hdr_idx_first_pos(idx);
209 old_idx = 0;
210 cur_idx = hdr_idx_first_idx(idx);
211 while (cur_idx) {
212 eol = sol + idx->v[cur_idx].len;
213
214 len = 0;
215 while (1) {
216 if (len >= eol - sol)
217 goto next_hdr;
218 if (sol[len] == ':')
219 break;
220 len++;
221 }
222
223 ctx->del = len;
224 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100225 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100226 sov++;
227
228 ctx->line = sol;
229 ctx->prev = old_idx;
230 ctx->idx = cur_idx;
231 ctx->val = sov - sol;
232 ctx->tws = 0;
233
Willy Tarreau2235b262016-11-05 15:50:20 +0100234 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100235 eol--;
236 ctx->tws++;
237 }
238 ctx->vlen = eol - sov;
239 return 1;
240
241 next_hdr:
242 sol = eol + idx->v[cur_idx].cr + 1;
243 old_idx = cur_idx;
244 cur_idx = idx->v[cur_idx].next;
245 }
246 return 0;
247}
248
Willy Tarreau33a7e692007-06-10 19:45:56 +0200249/* Find the first or next occurrence of header <name> in message buffer <sol>
250 * using headers index <idx>, and return it in the <ctx> structure. This
251 * structure holds everything necessary to use the header and find next
252 * occurrence. If its <idx> member is 0, the header is searched from the
253 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100254 * 1 when it finds a value, and 0 when there is no more. It is designed to work
255 * with headers defined as comma-separated lists. As a special case, if ctx->val
256 * is NULL when searching for a new values of a header, the current header is
257 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200258 */
259int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100260 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200261 struct hdr_ctx *ctx)
262{
Willy Tarreau68085d82010-01-18 14:54:04 +0100263 char *eol, *sov;
264 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200265
Willy Tarreau68085d82010-01-18 14:54:04 +0100266 cur_idx = ctx->idx;
267 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200268 /* We have previously returned a value, let's search
269 * another one on the same line.
270 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200271 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200272 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100273 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200274 eol = sol + idx->v[cur_idx].len;
275
276 if (sov >= eol)
277 /* no more values in this header */
278 goto next_hdr;
279
Willy Tarreau68085d82010-01-18 14:54:04 +0100280 /* values remaining for this header, skip the comma but save it
281 * for later use (eg: for header deletion).
282 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200283 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100284 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200285 sov++;
286
287 goto return_hdr;
288 }
289
290 /* first request for this header */
291 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100292 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200293 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200294 while (cur_idx) {
295 eol = sol + idx->v[cur_idx].len;
296
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200297 if (len == 0) {
298 /* No argument was passed, we want any header.
299 * To achieve this, we simply build a fake request. */
300 while (sol + len < eol && sol[len] != ':')
301 len++;
302 name = sol;
303 }
304
Willy Tarreau33a7e692007-06-10 19:45:56 +0200305 if ((len < eol - sol) &&
306 (sol[len] == ':') &&
307 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100308 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200309 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100310 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200311 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100312
Willy Tarreau33a7e692007-06-10 19:45:56 +0200313 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100314 ctx->prev = old_idx;
315 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200316 ctx->idx = cur_idx;
317 ctx->val = sov - sol;
318
Willy Tarreauab813a42018-09-10 18:41:28 +0200319 eol = http_find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200320 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100321 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200322 eol--;
323 ctx->tws++;
324 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200325 ctx->vlen = eol - sov;
326 return 1;
327 }
328 next_hdr:
329 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100330 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200331 cur_idx = idx->v[cur_idx].next;
332 }
333 return 0;
334}
335
Willy Tarreau68085d82010-01-18 14:54:04 +0100336/* Remove one value of a header. This only works on a <ctx> returned by one of
337 * the http_find_header functions. The value is removed, as well as surrounding
338 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100339 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100340 * message <msg>. The new index is returned. If it is zero, it means there is
341 * no more header, so any processing may stop. The ctx is always left in a form
342 * that can be handled by http_find_header2() to find next occurrence.
343 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100344int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100345{
346 int cur_idx = ctx->idx;
347 char *sol = ctx->line;
348 struct hdr_idx_elem *hdr;
349 int delta, skip_comma;
350
351 if (!cur_idx)
352 return 0;
353
354 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200355 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100356 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200357 delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100358 http_msg_move_end(msg, delta);
359 idx->used--;
360 hdr->len = 0; /* unused entry */
361 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100362 if (idx->tail == ctx->idx)
363 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100364 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100365 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100366 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200367 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100368 return ctx->idx;
369 }
370
371 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200372 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
373 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100374 */
375
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200376 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200377 delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200378 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100379 NULL, 0);
380 hdr->len += delta;
381 http_msg_move_end(msg, delta);
382 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200383 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100384 return ctx->idx;
385}
386
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100387/* This function handles a server error at the stream interface level. The
388 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100389 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100390 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100391 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200393static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +0200394 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395{
Willy Tarreau2019f952017-03-14 11:07:31 +0100396 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100397 channel_auto_read(si_oc(si));
398 channel_abort(si_oc(si));
399 channel_auto_close(si_oc(si));
400 channel_erase(si_oc(si));
401 channel_auto_close(si_ic(si));
402 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200403 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200404 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200405 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200406 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200407 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200408 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409}
410
Willy Tarreau87b09662015-04-03 00:22:06 +0200411/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100412 * and message.
413 */
414
Willy Tarreau83061a82018-07-13 11:56:34 +0200415struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100416{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200417 const int msgnum = http_get_status_idx(s->txn->status);
418
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200419 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200420 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200421 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200422 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100423 else
424 return &http_err_chunks[msgnum];
425}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200426
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100427void
Willy Tarreau83061a82018-07-13 11:56:34 +0200428http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100429{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200430 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100431 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100432 stream_int_retnclose(&s->si[0], msg);
433}
434
Willy Tarreau21d2af32008-02-14 20:25:24 +0100435/* Parse the URI from the given transaction (which is assumed to be in request
436 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
437 * It is returned otherwise.
438 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200439char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100440{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200441 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100442
Willy Tarreau9d9ccdb2018-10-28 20:13:12 +0100443 if (!txn->req.chn->buf.size)
444 return NULL;
445
Willy Tarreau6b952c82018-09-10 17:45:34 +0200446 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 +0100447
Willy Tarreau6b952c82018-09-10 17:45:34 +0200448 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100449}
450
Willy Tarreau71241ab2012-12-27 11:30:54 +0100451/* Returns a 302 for a redirectable request that reaches a server working in
452 * in redirect mode. This may only be called just after the stream interface
453 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
454 * follow normal proxy processing. NOTE: this function is designed to support
455 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100456 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200457void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100458{
459 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100460 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100461 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200462 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100463
464 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200465 trash.data = strlen(HTTP_302);
466 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100467
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200468 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100469
Willy Tarreauefb453c2008-10-26 20:49:47 +0100470 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200471 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100472 return;
473
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100474 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100475 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200476 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
477 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100478 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100479
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200480 /* 3: add the request URI. Since it was already forwarded, we need
481 * to temporarily rewind the buffer.
482 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200483 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200484 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200485
Willy Tarreau6b952c82018-09-10 17:45:34 +0200486 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200487 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 +0200488
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200489 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200490
Willy Tarreauefb453c2008-10-26 20:49:47 +0100491 if (!path)
492 return;
493
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200494 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100495 return;
496
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200497 memcpy(trash.area + trash.data, path, len);
498 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100499
500 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 memcpy(trash.area + trash.data,
502 "\r\nProxy-Connection: close\r\n\r\n", 29);
503 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100504 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200505 memcpy(trash.area + trash.data,
506 "\r\nConnection: close\r\n\r\n", 23);
507 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100508 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100509
510 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200511 si_shutr(si);
512 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100513 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100514 si->state = SI_ST_CLO;
515
516 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100517 txn->status = 302;
518 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100519
520 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100521 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500522 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100523}
524
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100525/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100526 * that the server side is closed. Note that err_type is actually a
527 * bitmask, where almost only aborts may be cumulated with other
528 * values. We consider that aborted operations are more important
529 * than timeouts or errors due to the fact that nobody else in the
530 * logs might explain incomplete retries. All others should avoid
531 * being cumulated. It should normally not be possible to have multiple
532 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100533 * Note that connection errors appearing on the second request of a keep-alive
534 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100535 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200536void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100537{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100538 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100539
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200540 /* set s->txn->status for http_error_message(s) */
541 s->txn->status = 503;
542
Willy Tarreauefb453c2008-10-26 20:49:47 +0100543 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200544 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100545 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100546 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200547 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100548 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200549 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100550 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200551 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100552 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100553 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200554 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100555 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100556 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200557 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100558 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200559 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100560 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200561 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100562 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200563 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200564 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200565 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100566 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200567 http_error_message(s));
568 else { /* SI_ET_CONN_OTHER and others */
569 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200570 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100571 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200572 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100573}
574
Willy Tarreau42250582007-04-01 01:30:43 +0200575extern const char sess_term_cond[8];
576extern const char sess_fin_state[8];
577extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +0100578struct pool_head *pool_head_http_txn;
579struct pool_head *pool_head_requri;
580struct pool_head *pool_head_capture = NULL;
581struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100582
Willy Tarreau117f59e2007-03-04 18:17:17 +0100583/*
584 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200585 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100586 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200587void http_capture_headers(char *som, struct hdr_idx *idx,
588 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100589{
590 char *eol, *sol, *col, *sov;
591 int cur_idx;
592 struct cap_hdr *h;
593 int len;
594
595 sol = som + hdr_idx_first_pos(idx);
596 cur_idx = hdr_idx_first_idx(idx);
597
598 while (cur_idx) {
599 eol = sol + idx->v[cur_idx].len;
600
601 col = sol;
602 while (col < eol && *col != ':')
603 col++;
604
605 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100606 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100607 sov++;
608
609 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200610 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100611 (strncasecmp(sol, h->name, h->namelen) == 0)) {
612 if (cap[h->index] == NULL)
613 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100614 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100615
616 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100617 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100618 continue;
619 }
620
621 len = eol - sov;
622 if (len > h->len)
623 len = h->len;
624
625 memcpy(cap[h->index], sov, len);
626 cap[h->index][len]=0;
627 }
628 }
629 sol = eol + idx->v[cur_idx].cr + 1;
630 cur_idx = idx->v[cur_idx].next;
631 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100632}
633
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200634/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
635 * conversion succeeded, 0 in case of error. If the request was already 1.X,
636 * nothing is done and 1 is returned.
637 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200638int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200639{
640 int delta;
641 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100642 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200643
644 if (msg->sl.rq.v_l != 0)
645 return 1;
646
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300647 /* RFC 1945 allows only GET for HTTP/0.9 requests */
648 if (txn->meth != HTTP_METH_GET)
649 return 0;
650
Willy Tarreauf37954d2018-06-15 18:31:02 +0200651 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200652
653 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300654 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
655 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200656 }
657 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200658 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100659 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200660 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200661 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200662 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200663 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200664 NULL, NULL);
665 if (unlikely(!cur_end))
666 return 0;
667
668 /* we have a full HTTP/1.0 request now and we know that
669 * we have either a CR or an LF at <ptr>.
670 */
671 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
672 return 1;
673}
674
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100675/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100676 * and "keep-alive" values. If we already know that some headers may safely
677 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100678 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
679 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100680 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100681 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
682 * found, and TX_CON_*_SET is adjusted depending on what is left so only
683 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100684 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100685 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100686void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100687{
Willy Tarreau5b154472009-12-21 20:11:07 +0100688 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100689 const char *hdr_val = "Connection";
690 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100691
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100692 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100693 return;
694
Willy Tarreau88d349d2010-01-25 12:15:43 +0100695 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
696 hdr_val = "Proxy-Connection";
697 hdr_len = 16;
698 }
699
Willy Tarreau5b154472009-12-21 20:11:07 +0100700 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100701 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200702 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100703 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
704 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100705 if (to_del & 2)
706 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100707 else
708 txn->flags |= TX_CON_KAL_SET;
709 }
710 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
711 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100712 if (to_del & 1)
713 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100714 else
715 txn->flags |= TX_CON_CLO_SET;
716 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100717 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
718 txn->flags |= TX_HDR_CONN_UPG;
719 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100720 }
721
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100722 txn->flags |= TX_HDR_CONN_PRS;
723 return;
724}
Willy Tarreau5b154472009-12-21 20:11:07 +0100725
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100726/* Apply desired changes on the Connection: header. Values may be removed and/or
727 * added depending on the <wanted> flags, which are exclusively composed of
728 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
729 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
730 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100731void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100732{
733 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100734 const char *hdr_val = "Connection";
735 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100736
737 ctx.idx = 0;
738
Willy Tarreau88d349d2010-01-25 12:15:43 +0100739
740 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
741 hdr_val = "Proxy-Connection";
742 hdr_len = 16;
743 }
744
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100745 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200746 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100747 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
748 if (wanted & TX_CON_KAL_SET)
749 txn->flags |= TX_CON_KAL_SET;
750 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100751 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100752 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100753 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
754 if (wanted & TX_CON_CLO_SET)
755 txn->flags |= TX_CON_CLO_SET;
756 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100757 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100758 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100759 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100760
761 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
762 return;
763
764 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
765 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100766 hdr_val = "Connection: close";
767 hdr_len = 17;
768 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
769 hdr_val = "Proxy-Connection: close";
770 hdr_len = 23;
771 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100772 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100773 }
774
775 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
776 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100777 hdr_val = "Connection: keep-alive";
778 hdr_len = 22;
779 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
780 hdr_val = "Proxy-Connection: keep-alive";
781 hdr_len = 28;
782 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100783 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100784 }
785 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100786}
787
Willy Tarreau87b09662015-04-03 00:22:06 +0200788void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200789{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200790 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200791 int tmp = TX_CON_WANT_KAL;
792
Christopher Fauletf2824e62018-10-01 12:12:37 +0200793 if (IS_HTX_STRM(s))
Christopher Faulet0f226952018-10-22 09:29:56 +0200794 return htx_adjust_conn_mode(s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200795
Christopher Faulet315b39c2018-09-21 16:26:19 +0200796 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
797 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
798 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200799
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200800 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200801 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200802 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200803
Christopher Faulet315b39c2018-09-21 16:26:19 +0200804 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
805 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200806 tmp = TX_CON_WANT_CLO;
807
808 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
809 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
810
811 if (!(txn->flags & TX_HDR_CONN_PRS) &&
812 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
813 /* parse the Connection header and possibly clean it */
814 int to_del = 0;
815 if ((msg->flags & HTTP_MSGF_VER_11) ||
816 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200817 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200818 to_del |= 2; /* remove "keep-alive" */
819 if (!(msg->flags & HTTP_MSGF_VER_11))
820 to_del |= 1; /* remove "close" */
821 http_parse_connection_header(txn, msg, to_del);
822 }
823
824 /* check if client or config asks for explicit close in KAL/SCL */
825 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
826 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
827 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
828 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
829 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200830 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200831 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
832}
William Lallemand82fe75c2012-10-23 10:25:10 +0200833
Willy Tarreaud787e662009-07-07 10:14:51 +0200834/* This stream analyser waits for a complete HTTP request. It returns 1 if the
835 * processing can continue on next analysers, or zero if it either needs more
836 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100837 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200838 * when it has nothing left to do, and may remove any analyser when it wants to
839 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100840 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200841int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100842{
Willy Tarreau59234e92008-11-30 23:51:27 +0100843 /*
844 * We will parse the partial (or complete) lines.
845 * We will check the request syntax, and also join multi-line
846 * headers. An index of all the lines will be elaborated while
847 * parsing.
848 *
849 * For the parsing, we use a 28 states FSM.
850 *
851 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200852 * ci_head(req) = beginning of request
853 * ci_head(req) + msg->eoh = end of processed headers / start of current one
854 * ci_tail(req) = end of input data
855 * msg->eol = end of current header or line (LF or CRLF)
856 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200857 *
858 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200859 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200860 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
861 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100862 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100863
Willy Tarreau59234e92008-11-30 23:51:27 +0100864 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200865 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200866 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100867 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200868 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100869
Christopher Faulete0768eb2018-10-03 16:38:02 +0200870 if (IS_HTX_STRM(s))
871 return htx_wait_for_request(s, req, an_bit);
872
Christopher Faulet45073512018-07-20 10:16:29 +0200873 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 +0100874 now_ms, __FUNCTION__,
875 s,
876 req,
877 req->rex, req->wex,
878 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200879 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100880 req->analysers);
881
Willy Tarreau52a0c602009-08-16 22:45:38 +0200882 /* we're speaking HTTP here, so let's speak HTTP to the client */
883 s->srv_error = http_return_srv_error;
884
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300885 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200886 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300887 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
888
Willy Tarreau83e3af02009-12-28 17:39:57 +0100889 /* There's a protected area at the end of the buffer for rewriting
890 * purposes. We don't want to start to parse the request if the
891 * protected area is affected, because we may have to move processed
892 * data later, which is much more complicated.
893 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200894 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200895 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100896 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200897 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100898 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100899 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200900 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200901 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100902 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100903 return 0;
904 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200905 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200906 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200907 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100908 }
909
Willy Tarreauf37954d2018-06-15 18:31:02 +0200910 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100911 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100912 }
913
Willy Tarreau59234e92008-11-30 23:51:27 +0100914 /* 1: we might have to print this header in debug mode */
915 if (unlikely((global.mode & MODE_DEBUG) &&
916 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200917 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100918 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100919
Willy Tarreauf37954d2018-06-15 18:31:02 +0200920 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200921 /* this is a bit complex : in case of error on the request line,
922 * we know that rq.l is still zero, so we display only the part
923 * up to the end of the line (truncated by debug_hdr).
924 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200925 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100926 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100927
Willy Tarreau59234e92008-11-30 23:51:27 +0100928 sol += hdr_idx_first_pos(&txn->hdr_idx);
929 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100930
Willy Tarreau59234e92008-11-30 23:51:27 +0100931 while (cur_idx) {
932 eol = sol + txn->hdr_idx.v[cur_idx].len;
933 debug_hdr("clihdr", s, sol, eol);
934 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
935 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100936 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100937 }
938
Willy Tarreau58f10d72006-12-04 02:26:12 +0100939
Willy Tarreau59234e92008-11-30 23:51:27 +0100940 /*
941 * Now we quickly check if we have found a full valid request.
942 * If not so, we check the FD and buffer states before leaving.
943 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100944 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100945 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200946 * on a keep-alive stream, if we encounter and error, close, t/o,
947 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100948 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200949 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200950 * Last, we may increase some tracked counters' http request errors on
951 * the cases that are deliberately the client's fault. For instance,
952 * a timeout or connection reset is not counted as an error. However
953 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100954 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100955
Willy Tarreau655dce92009-11-08 13:10:58 +0100956 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100957 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100958 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100959 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100960 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200961 stream_inc_http_req_ctr(s);
962 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200963 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100964 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100965 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100966
Willy Tarreau59234e92008-11-30 23:51:27 +0100967 /* 1: Since we are in header mode, if there's no space
968 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200969 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100970 * must terminate it now.
971 */
Willy Tarreau23752332018-06-15 14:54:53 +0200972 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100973 /* FIXME: check if URI is set and return Status
974 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100975 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200976 stream_inc_http_req_ctr(s);
977 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200978 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200979 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200980 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100981 goto return_bad_req;
982 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100983
Willy Tarreau59234e92008-11-30 23:51:27 +0100984 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200985 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200986 if (!(s->flags & SF_ERR_MASK))
987 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100988
Willy Tarreaufcffa692010-01-10 14:21:19 +0100989 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100990 goto failed_keep_alive;
991
Willy Tarreau0f228a02015-05-01 15:37:53 +0200992 if (sess->fe->options & PR_O_IGNORE_PRB)
993 goto failed_keep_alive;
994
Willy Tarreau59234e92008-11-30 23:51:27 +0100995 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200996 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200997 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200998 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200999 }
1000
Willy Tarreaudc979f22012-12-04 10:39:01 +01001001 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001002 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001003 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001004 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001005 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001006 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001007 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001008 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001009 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001010 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001011
Willy Tarreaue7dff022015-04-03 01:14:29 +02001012 if (!(s->flags & SF_FINST_MASK))
1013 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001014 return 0;
1015 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001016
Willy Tarreau59234e92008-11-30 23:51:27 +01001017 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001018 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001019 if (!(s->flags & SF_ERR_MASK))
1020 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001021
Willy Tarreaufcffa692010-01-10 14:21:19 +01001022 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001023 goto failed_keep_alive;
1024
Willy Tarreau0f228a02015-05-01 15:37:53 +02001025 if (sess->fe->options & PR_O_IGNORE_PRB)
1026 goto failed_keep_alive;
1027
Willy Tarreau59234e92008-11-30 23:51:27 +01001028 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001029 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001030 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001031 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001032 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001033 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001034 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001035 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001036 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001037 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001038
Willy Tarreau87b09662015-04-03 00:22:06 +02001039 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001040 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001041 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001042 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001043 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001044
Willy Tarreaue7dff022015-04-03 01:14:29 +02001045 if (!(s->flags & SF_FINST_MASK))
1046 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001047 return 0;
1048 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001049
Willy Tarreau59234e92008-11-30 23:51:27 +01001050 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001051 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001052 if (!(s->flags & SF_ERR_MASK))
1053 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001054
Willy Tarreaufcffa692010-01-10 14:21:19 +01001055 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001056 goto failed_keep_alive;
1057
Willy Tarreau0f228a02015-05-01 15:37:53 +02001058 if (sess->fe->options & PR_O_IGNORE_PRB)
1059 goto failed_keep_alive;
1060
Willy Tarreau4076a152009-04-02 15:18:36 +02001061 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001062 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001063 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001064 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001065 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001066 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001067 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001068 stream_inc_http_err_ctr(s);
1069 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001070 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001071 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001072 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001073 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001074
Willy Tarreaue7dff022015-04-03 01:14:29 +02001075 if (!(s->flags & SF_FINST_MASK))
1076 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001077 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001078 }
1079
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001080 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001081 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001082 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001083#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001084 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001085 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001086 /* We need more data, we have to re-enable quick-ack in case we
1087 * previously disabled it, otherwise we might cause the client
1088 * to delay next data.
1089 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001090 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001091 }
1092#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001093
Willy Tarreaufcffa692010-01-10 14:21:19 +01001094 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1095 /* If the client starts to talk, let's fall back to
1096 * request timeout processing.
1097 */
1098 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001099 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001100 }
1101
Willy Tarreau59234e92008-11-30 23:51:27 +01001102 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001103 if (!tick_isset(req->analyse_exp)) {
1104 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1105 (txn->flags & TX_WAIT_NEXT_RQ) &&
1106 tick_isset(s->be->timeout.httpka))
1107 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1108 else
1109 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1110 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001111
Willy Tarreau59234e92008-11-30 23:51:27 +01001112 /* we're not ready yet */
1113 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001114
1115 failed_keep_alive:
1116 /* Here we process low-level errors for keep-alive requests. In
1117 * short, if the request is not the first one and it experiences
1118 * a timeout, read error or shutdown, we just silently close so
1119 * that the client can try again.
1120 */
1121 txn->status = 0;
1122 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001123 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001124 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001125 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001126 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001127 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001128 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001129 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001130
Willy Tarreaud787e662009-07-07 10:14:51 +02001131 /* OK now we have a complete HTTP request with indexed headers. Let's
1132 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001133 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001134 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001135 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001136 * byte after the last LF. msg->sov points to the first byte of data.
1137 * msg->eol cannot be trusted because it may have been left uninitialized
1138 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001139 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001140
Willy Tarreau87b09662015-04-03 00:22:06 +02001141 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001142 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001143
Willy Tarreaub16a5742010-01-10 14:46:16 +01001144 if (txn->flags & TX_WAIT_NEXT_RQ) {
1145 /* kill the pending keep-alive timeout */
1146 txn->flags &= ~TX_WAIT_NEXT_RQ;
1147 req->analyse_exp = TICK_ETERNITY;
1148 }
1149
1150
Willy Tarreaud787e662009-07-07 10:14:51 +02001151 /* Maybe we found in invalid header name while we were configured not
1152 * to block on that, so we have to capture it now.
1153 */
1154 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001155 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001156
Willy Tarreau59234e92008-11-30 23:51:27 +01001157 /*
1158 * 1: identify the method
1159 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001160 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001161
1162 /* we can make use of server redirect on GET and HEAD */
1163 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001164 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001165 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001166 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001167 /* PRI is reserved for the HTTP/2 preface */
1168 msg->err_pos = 0;
1169 goto return_bad_req;
1170 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001171
Willy Tarreau59234e92008-11-30 23:51:27 +01001172 /*
1173 * 2: check if the URI matches the monitor_uri.
1174 * We have to do this for every request which gets in, because
1175 * the monitor-uri is defined by the frontend.
1176 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001177 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1178 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001179 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001180 sess->fe->monitor_uri,
1181 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001182 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001183 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001184 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001185 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001186
Willy Tarreaue7dff022015-04-03 01:14:29 +02001187 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001188 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001189
Willy Tarreau59234e92008-11-30 23:51:27 +01001190 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001191 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001192 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001193
Willy Tarreau59234e92008-11-30 23:51:27 +01001194 ret = acl_pass(ret);
1195 if (cond->pol == ACL_COND_UNLESS)
1196 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001197
Willy Tarreau59234e92008-11-30 23:51:27 +01001198 if (ret) {
1199 /* we fail this request, let's return 503 service unavail */
1200 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001201 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001202 if (!(s->flags & SF_ERR_MASK))
1203 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001204 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001205 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001206 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001207
Willy Tarreau59234e92008-11-30 23:51:27 +01001208 /* nothing to fail, let's reply normaly */
1209 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001210 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001211 if (!(s->flags & SF_ERR_MASK))
1212 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001213 goto return_prx_cond;
1214 }
1215
1216 /*
1217 * 3: Maybe we have to copy the original REQURI for the logs ?
1218 * Note: we cannot log anymore if the request has been
1219 * classified as invalid.
1220 */
1221 if (unlikely(s->logs.logwait & LW_REQ)) {
1222 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001223 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001224 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001225
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001226 if (urilen >= global.tune.requri_len )
1227 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001228 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001229 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001230
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001231 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001232 s->do_log(s);
1233 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001234 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001235 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001236 }
Willy Tarreau06619262006-12-17 08:37:22 +01001237
Willy Tarreau91852eb2015-05-01 13:26:00 +02001238 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1239 * exactly one digit "." one digit. This check may be disabled using
1240 * option accept-invalid-http-request.
1241 */
1242 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1243 if (msg->sl.rq.v_l != 8) {
1244 msg->err_pos = msg->sl.rq.v;
1245 goto return_bad_req;
1246 }
1247
Willy Tarreauf37954d2018-06-15 18:31:02 +02001248 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1249 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1250 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1251 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001252 msg->err_pos = msg->sl.rq.v + 4;
1253 goto return_bad_req;
1254 }
1255 }
Willy Tarreau13317662015-05-01 13:47:08 +02001256 else {
1257 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1258 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1259 goto return_bad_req;
1260 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001261
Willy Tarreau5b154472009-12-21 20:11:07 +01001262 /* ... and check if the request is HTTP/1.1 or above */
1263 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001264 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1265 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1266 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001267 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001268
1269 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001270 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 +01001271
Willy Tarreau88d349d2010-01-25 12:15:43 +01001272 /* if the frontend has "option http-use-proxy-header", we'll check if
1273 * we have what looks like a proxied connection instead of a connection,
1274 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1275 * Note that this is *not* RFC-compliant, however browsers and proxies
1276 * happen to do that despite being non-standard :-(
1277 * We consider that a request not beginning with either '/' or '*' is
1278 * a proxied connection, which covers both "scheme://location" and
1279 * CONNECT ip:port.
1280 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001281 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001282 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001283 txn->flags |= TX_USE_PX_CONN;
1284
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001285 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001286 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001287
Willy Tarreau59234e92008-11-30 23:51:27 +01001288 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001289 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001290 http_capture_headers(ci_head(req), &txn->hdr_idx,
1291 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001292
Willy Tarreau557f1992015-05-01 10:05:17 +02001293 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1294 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001295 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001296 * The length of a message body is determined by one of the following
1297 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001298 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001299 * 1. Any response to a HEAD request and any response with a 1xx
1300 * (Informational), 204 (No Content), or 304 (Not Modified) status
1301 * code is always terminated by the first empty line after the
1302 * header fields, regardless of the header fields present in the
1303 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001304 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001305 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1306 * the connection will become a tunnel immediately after the empty
1307 * line that concludes the header fields. A client MUST ignore any
1308 * Content-Length or Transfer-Encoding header fields received in
1309 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001310 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001311 * 3. If a Transfer-Encoding header field is present and the chunked
1312 * transfer coding (Section 4.1) is the final encoding, the message
1313 * body length is determined by reading and decoding the chunked
1314 * data until the transfer coding indicates the data is complete.
1315 *
1316 * If a Transfer-Encoding header field is present in a response and
1317 * the chunked transfer coding is not the final encoding, the
1318 * message body length is determined by reading the connection until
1319 * it is closed by the server. If a Transfer-Encoding header field
1320 * is present in a request and the chunked transfer coding is not
1321 * the final encoding, the message body length cannot be determined
1322 * reliably; the server MUST respond with the 400 (Bad Request)
1323 * status code and then close the connection.
1324 *
1325 * If a message is received with both a Transfer-Encoding and a
1326 * Content-Length header field, the Transfer-Encoding overrides the
1327 * Content-Length. Such a message might indicate an attempt to
1328 * perform request smuggling (Section 9.5) or response splitting
1329 * (Section 9.4) and ought to be handled as an error. A sender MUST
1330 * remove the received Content-Length field prior to forwarding such
1331 * a message downstream.
1332 *
1333 * 4. If a message is received without Transfer-Encoding and with
1334 * either multiple Content-Length header fields having differing
1335 * field-values or a single Content-Length header field having an
1336 * invalid value, then the message framing is invalid and the
1337 * recipient MUST treat it as an unrecoverable error. If this is a
1338 * request message, the server MUST respond with a 400 (Bad Request)
1339 * status code and then close the connection. If this is a response
1340 * message received by a proxy, the proxy MUST close the connection
1341 * to the server, discard the received response, and send a 502 (Bad
1342 * Gateway) response to the client. If this is a response message
1343 * received by a user agent, the user agent MUST close the
1344 * connection to the server and discard the received response.
1345 *
1346 * 5. If a valid Content-Length header field is present without
1347 * Transfer-Encoding, its decimal value defines the expected message
1348 * body length in octets. If the sender closes the connection or
1349 * the recipient times out before the indicated number of octets are
1350 * received, the recipient MUST consider the message to be
1351 * incomplete and close the connection.
1352 *
1353 * 6. If this is a request message and none of the above are true, then
1354 * the message body length is zero (no message body is present).
1355 *
1356 * 7. Otherwise, this is a response message without a declared message
1357 * body length, so the message body length is determined by the
1358 * number of octets received prior to the server closing the
1359 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001360 */
1361
Willy Tarreau32b47f42009-10-18 20:55:02 +02001362 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001363 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001364 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001365 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001366 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001367 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001368 /* chunked not last, return badreq */
1369 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001370 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001371 }
1372
Willy Tarreau1c913912015-04-30 10:57:51 +02001373 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001374 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001375 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001376 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001377 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1378 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001379 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001380 signed long long cl;
1381
Willy Tarreauad14f752011-09-02 20:33:27 +02001382 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001383 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001384 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001385 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001386
Willy Tarreauad14f752011-09-02 20:33:27 +02001387 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001388 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001389 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001390 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001391
Willy Tarreauad14f752011-09-02 20:33:27 +02001392 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001393 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001394 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001395 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001396
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001397 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001398 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001399 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001400 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001401
Christopher Fauletbe821b92017-03-30 11:21:53 +02001402 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001403 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001404 }
1405
Willy Tarreau34dfc602015-05-01 10:09:49 +02001406 /* even bodyless requests have a known length */
1407 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001408
Willy Tarreau179085c2014-04-28 16:48:56 +02001409 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1410 * only change if both the request and the config reference something else.
1411 * Option httpclose by itself sets tunnel mode where headers are mangled.
1412 * However, if another mode is set, it will affect it (eg: server-close/
1413 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1414 * if FE and BE have the same settings (common). The method consists in
1415 * checking if options changed between the two calls (implying that either
1416 * one is non-null, or one of them is non-null and we are there for the first
1417 * time.
1418 */
1419 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001420 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001421 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001422
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001423 /* we may have to wait for the request's body */
1424 if ((s->be->options & PR_O_WREQ_BODY) &&
1425 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1426 req->analysers |= AN_REQ_HTTP_BODY;
1427
Willy Tarreau83ece462017-12-21 15:13:09 +01001428 /*
1429 * RFC7234#4:
1430 * A cache MUST write through requests with methods
1431 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1432 * the origin server; i.e., a cache is not allowed
1433 * to generate a reply to such a request before
1434 * having forwarded the request and having received
1435 * a corresponding response.
1436 *
1437 * RFC7231#4.2.1:
1438 * Of the request methods defined by this
1439 * specification, the GET, HEAD, OPTIONS, and TRACE
1440 * methods are defined to be safe.
1441 */
1442 if (likely(txn->meth == HTTP_METH_GET ||
1443 txn->meth == HTTP_METH_HEAD ||
1444 txn->meth == HTTP_METH_OPTIONS ||
1445 txn->meth == HTTP_METH_TRACE))
1446 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1447
Willy Tarreaud787e662009-07-07 10:14:51 +02001448 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001449 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001450 req->analyse_exp = TICK_ETERNITY;
1451 return 1;
1452
1453 return_bad_req:
1454 /* We centralize bad requests processing here */
1455 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1456 /* we detected a parsing error. We want to archive this request
1457 * in the dedicated proxy area for later troubleshooting.
1458 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001459 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001460 }
1461
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001462 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001463 txn->req.msg_state = HTTP_MSG_ERROR;
1464 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001465 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001466
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001467 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001468 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001469 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001470
1471 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001472 if (!(s->flags & SF_ERR_MASK))
1473 s->flags |= SF_ERR_PRXCOND;
1474 if (!(s->flags & SF_FINST_MASK))
1475 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001476
Christopher Faulet0184ea72017-01-05 14:06:34 +01001477 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001478 req->analyse_exp = TICK_ETERNITY;
1479 return 0;
1480}
1481
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001482
Willy Tarreau347a35d2013-11-22 17:51:09 +01001483/* This function prepares an applet to handle the stats. It can deal with the
1484 * "100-continue" expectation, check that admin rules are met for POST requests,
1485 * and program a response message if something was unexpected. It cannot fail
1486 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001487 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001488 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001489 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001490 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001491int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001492{
1493 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001494 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001495 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001496 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001497 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001498 struct uri_auth *uri_auth = s->be->uri_auth;
1499 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001500 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001501
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001502 appctx = si_appctx(si);
1503 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1504 appctx->st1 = appctx->st2 = 0;
1505 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1506 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001507 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001508 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001509
Willy Tarreauf37954d2018-06-15 18:31:02 +02001510 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001511 lookup = uri + uri_auth->uri_len;
1512
1513 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1514 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001515 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001516 break;
1517 }
1518 }
1519
1520 if (uri_auth->refresh) {
1521 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1522 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001523 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001524 break;
1525 }
1526 }
1527 }
1528
1529 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1530 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001531 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001532 break;
1533 }
1534 }
1535
Willy Tarreau1e62df92016-01-11 18:57:53 +01001536 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1537 if (memcmp(h, ";typed", 6) == 0) {
1538 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1539 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1540 break;
1541 }
1542 }
1543
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001544 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1545 if (memcmp(h, ";st=", 4) == 0) {
1546 int i;
1547 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001548 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001549 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1550 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001551 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001552 break;
1553 }
1554 }
1555 break;
1556 }
1557 }
1558
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001559 appctx->ctx.stats.scope_str = 0;
1560 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001561 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1562 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1563 int itx = 0;
1564 const char *h2;
1565 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1566 const char *err;
1567
1568 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1569 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001570 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001571 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1572 itx++;
1573 h++;
1574 }
1575
1576 if (itx > STAT_SCOPE_TXT_MAXLEN)
1577 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001578 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001579
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001580 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001581 memcpy(scope_txt, h2, itx);
1582 scope_txt[itx] = '\0';
1583 err = invalid_char(scope_txt);
1584 if (err) {
1585 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001586 appctx->ctx.stats.scope_str = 0;
1587 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001588 }
1589 break;
1590 }
1591 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001592
1593 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001594 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001595 int ret = 1;
1596
1597 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001598 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 +01001599 ret = acl_pass(ret);
1600 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1601 ret = !ret;
1602 }
1603
1604 if (ret) {
1605 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001606 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001607 break;
1608 }
1609 }
1610
1611 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001612 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001613 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001614 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001615 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1616 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001617 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001618 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001619 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001620 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1621 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001622 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001623 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001624 else {
1625 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001626 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001627 }
1628
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001629 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001630 return 1;
1631}
1632
Lukas Tribus67db8df2013-06-23 17:37:13 +02001633/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1634 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1635 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001636void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001637{
1638#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001639 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001640 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1641#endif
1642#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001643 if (from->ss_family == AF_INET6) {
1644 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001645 /* v4-mapped addresses need IP_TOS */
1646 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1647 else
1648 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1649 }
1650#endif
1651}
1652
Willy Tarreau87b09662015-04-03 00:22:06 +02001653int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001654 const char* name, unsigned int name_len,
1655 const char *str, struct my_regex *re,
1656 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001657{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001658 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001659 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001660 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001661 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1662 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001663 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001664
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001665 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001666
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001667 /* Choose the header browsing function. */
1668 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001669 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001670 http_find_hdr_func = http_find_header2;
1671 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001672 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001673 http_find_hdr_func = http_find_full_header2;
1674 break;
1675 default: /* impossible */
1676 return -1;
1677 }
1678
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001679 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1680 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001681 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001682 char *val = ctx.line + ctx.val;
1683 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001684
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001685 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1686 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001687
Willy Tarreau6e27be12018-08-22 04:46:47 +02001688 len = exp_replace(output->area, output->size, val, str, pmatch);
1689 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001690 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001691
Willy Tarreau6e27be12018-08-22 04:46:47 +02001692 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001693
1694 hdr->len += delta;
1695 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001696
1697 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001698 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001699 }
1700
1701 return 0;
1702}
1703
Willy Tarreau87b09662015-04-03 00:22:06 +02001704static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001705 const char* name, unsigned int name_len,
1706 struct list *fmt, struct my_regex *re,
1707 int action)
1708{
Willy Tarreau83061a82018-07-13 11:56:34 +02001709 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001710 int ret = -1;
1711
1712 replace = alloc_trash_chunk();
1713 if (!replace)
1714 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001715
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001716 replace->data = build_logline(s, replace->area, replace->size, fmt);
1717 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001718 goto leave;
1719
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001720 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1721 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001722
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001723 leave:
1724 free_trash_chunk(replace);
1725 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001726}
1727
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001728/*
1729 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1730 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001731 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1732 * line is inserted before the header. If an error occurred <early_hints> is
1733 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001734 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001735static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1736 const char* name, unsigned int name_len,
1737 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001738{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001739 if (!early_hints) {
1740 early_hints = alloc_trash_chunk();
1741 if (!early_hints)
1742 goto fail;
1743 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1744 goto fail;
1745 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001746
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001747 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1748 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001749
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001750 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1751 if (!chunk_memcat(early_hints, "\r\n", 2))
1752 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001753
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001754 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001755
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001756 fail:
1757 free_trash_chunk(early_hints);
1758 return NULL;
1759}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001760
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001761/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1762 * response is added. If an error occurred or if another response was already
1763 * sent, this function does nothing.
1764 */
1765static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1766{
1767 struct channel *chn = s->txn->rsp.chn;
1768 char *cur_ptr = ci_head(chn);
1769 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001770
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001771 /* If a response was already sent, skip early hints */
1772 if (s->txn->status > 0)
1773 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001774
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001775 if (!chunk_memcat(early_hints, "\r\n", 2))
1776 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001777
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001778 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1779 c_adv(chn, ret);
1780 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001781}
1782
Willy Tarreau87b09662015-04-03 00:22:06 +02001783/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001784 * transaction <txn>. Returns the verdict of the first rule that prevents
1785 * further processing of the request (auth, deny, ...), and defaults to
1786 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1787 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001788 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1789 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1790 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001791 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001792enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001793http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001794{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001795 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001796 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001797 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001798 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001799 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001800 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001801 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001802 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001803 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001804 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001805
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001806 /* If "the current_rule_list" match the executed rule list, we are in
1807 * resume condition. If a resume is needed it is always in the action
1808 * and never in the ACL or converters. In this case, we initialise the
1809 * current rule, and go to the action execution point.
1810 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001811 if (s->current_rule) {
1812 rule = s->current_rule;
1813 s->current_rule = NULL;
1814 if (s->current_rule_list == rules)
1815 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001816 }
1817 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001818
Willy Tarreauff011f22011-01-06 17:51:27 +01001819 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001820
Willy Tarreau96257ec2012-12-27 10:46:37 +01001821 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001822 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001823 int ret;
1824
Willy Tarreau192252e2015-04-04 01:47:55 +02001825 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001826 ret = acl_pass(ret);
1827
Willy Tarreauff011f22011-01-06 17:51:27 +01001828 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001829 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001830
1831 if (!ret) /* condition not matched */
1832 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001833 }
1834
Willy Tarreauacc98002015-09-27 23:34:39 +02001835 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001836resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001837 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001838 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001839 rule_ret = HTTP_RULE_RES_STOP;
1840 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001841
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001842 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001843 if (deny_status)
1844 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001845 rule_ret = HTTP_RULE_RES_DENY;
1846 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001847
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001848 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001849 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001850 if (deny_status)
1851 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001852 rule_ret = HTTP_RULE_RES_DENY;
1853 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001854
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001855 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001856 /* Be sure to send any pending HTTP 103 response first */
1857 if (early_hints) {
1858 http_send_early_hints(s, early_hints);
1859 free_trash_chunk(early_hints);
1860 early_hints = NULL;
1861 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001862 /* Auth might be performed on regular http-req rules as well as on stats */
1863 auth_realm = rule->arg.auth.realm;
1864 if (!auth_realm) {
1865 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1866 auth_realm = STATS_DEFAULT_REALM;
1867 else
1868 auth_realm = px->id;
1869 }
1870 /* send 401/407 depending on whether we use a proxy or not. We still
1871 * count one error, because normal browsing won't significantly
1872 * increase the counter but brute force attempts will.
1873 */
1874 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1875 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001876 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001877 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001878 rule_ret = HTTP_RULE_RES_ABRT;
1879 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001880
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001881 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001882 /* Be sure to send any pending HTTP 103 response first */
1883 if (early_hints) {
1884 http_send_early_hints(s, early_hints);
1885 free_trash_chunk(early_hints);
1886 early_hints = NULL;
1887 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001888 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001889 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001890 rule_ret = HTTP_RULE_RES_BADREQ;
1891 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001892
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001893 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001894 s->task->nice = rule->arg.nice;
1895 break;
1896
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001897 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001898 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001899 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001900 break;
1901
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001902 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001903#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001904 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001905 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001906#endif
1907 break;
1908
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001909 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001910 s->logs.level = rule->arg.loglevel;
1911 break;
1912
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001913 case ACT_HTTP_REPLACE_HDR:
1914 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001915 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1916 rule->arg.hdr_add.name_len,
1917 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001918 &rule->arg.hdr_add.re, rule->action)) {
1919 rule_ret = HTTP_RULE_RES_BADREQ;
1920 goto end;
1921 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001922 break;
1923
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001924 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001925 ctx.idx = 0;
1926 /* remove all occurrences of the header */
1927 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001928 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001929 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001930 }
Willy Tarreau85603282015-01-21 20:39:27 +01001931 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001932
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001933 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001934 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001935 /* The scope of the trash buffer must be limited to this function. The
1936 * build_logline() function can execute a lot of other function which
1937 * can use the trash buffer. So for limiting the scope of this global
1938 * buffer, we build first the header value using build_logline, and
1939 * after we store the header name.
1940 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001941 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001942
1943 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001944 if (!replace) {
1945 rule_ret = HTTP_RULE_RES_BADREQ;
1946 goto end;
1947 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001948
Thierry Fournier4b788f72016-06-01 13:35:36 +02001949 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001950 len += build_logline(s, replace->area + len,
1951 replace->size - len,
1952 &rule->arg.hdr_add.fmt);
1953 memcpy(replace->area, rule->arg.hdr_add.name,
1954 rule->arg.hdr_add.name_len);
1955 replace->area[rule->arg.hdr_add.name_len] = ':';
1956 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1957 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001958
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001959 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001960 /* remove all occurrences of the header */
1961 ctx.idx = 0;
1962 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001963 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001964 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1965 }
1966 }
1967
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001968 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001969 static unsigned char rate_limit = 0;
1970
1971 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001972 replace->area[rule->arg.hdr_add.name_len] = 0;
1973 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,
1974 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001975 }
1976
1977 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1978 if (sess->fe != s->be)
1979 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1980 if (sess->listener->counters)
1981 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1982 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001983
1984 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001985 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001986 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001987
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001988 case ACT_HTTP_DEL_ACL:
1989 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001990 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001991 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001992
1993 /* collect reference */
1994 ref = pat_ref_lookup(rule->arg.map.ref);
1995 if (!ref)
1996 continue;
1997
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001998 /* allocate key */
1999 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002000 if (!key) {
2001 rule_ret = HTTP_RULE_RES_BADREQ;
2002 goto end;
2003 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002004
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002005 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002006 key->data = build_logline(s, key->area, key->size,
2007 &rule->arg.map.key);
2008 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002009
2010 /* perform update */
2011 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002012 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002013 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002014 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002015
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002016 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002017 break;
2018 }
2019
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002020 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002021 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002022 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002023
2024 /* collect reference */
2025 ref = pat_ref_lookup(rule->arg.map.ref);
2026 if (!ref)
2027 continue;
2028
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002029 /* allocate key */
2030 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002031 if (!key) {
2032 rule_ret = HTTP_RULE_RES_BADREQ;
2033 goto end;
2034 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002035
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002036 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002037 key->data = build_logline(s, key->area, key->size,
2038 &rule->arg.map.key);
2039 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002040
2041 /* perform update */
2042 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002043 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002044 if (pat_ref_find_elt(ref, key->area) == NULL)
2045 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002046 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002047
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002048 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002049 break;
2050 }
2051
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002052 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002053 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002054 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002055
2056 /* collect reference */
2057 ref = pat_ref_lookup(rule->arg.map.ref);
2058 if (!ref)
2059 continue;
2060
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002061 /* allocate key */
2062 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002063 if (!key) {
2064 rule_ret = HTTP_RULE_RES_BADREQ;
2065 goto end;
2066 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002067
2068 /* allocate value */
2069 value = alloc_trash_chunk();
2070 if (!value) {
2071 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01002072 rule_ret = HTTP_RULE_RES_BADREQ;
2073 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002074 }
2075
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002076 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002077 key->data = build_logline(s, key->area, key->size,
2078 &rule->arg.map.key);
2079 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002080
2081 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002082 value->data = build_logline(s, value->area,
2083 value->size,
2084 &rule->arg.map.value);
2085 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002086
2087 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002088 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002089 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002090 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002091 else
2092 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002093 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002094
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002095 free_trash_chunk(key);
2096 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002097 break;
2098 }
William Lallemand73025dd2014-04-24 14:38:37 +02002099
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002100 case ACT_HTTP_EARLY_HINT:
2101 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2102 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002103 early_hints = http_apply_early_hint_rule(s, early_hints,
2104 rule->arg.early_hint.name,
2105 rule->arg.early_hint.name_len,
2106 &rule->arg.early_hint.fmt);
2107 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01002108 rule_ret = HTTP_RULE_RES_DONE;
2109 goto end;
2110 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002111 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002112 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002113 if ((s->req.flags & CF_READ_ERROR) ||
2114 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2115 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2116 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002117 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002118
Willy Tarreauacc98002015-09-27 23:34:39 +02002119 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002120 case ACT_RET_ERR:
2121 case ACT_RET_CONT:
2122 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002123 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01002124 rule_ret = HTTP_RULE_RES_DONE;
2125 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002126 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002127 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01002128 rule_ret = HTTP_RULE_RES_YIELD;
2129 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002130 }
William Lallemand73025dd2014-04-24 14:38:37 +02002131 break;
2132
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002133 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002134 /* Note: only the first valid tracking parameter of each
2135 * applies.
2136 */
2137
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002138 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002139 struct stktable *t;
2140 struct stksess *ts;
2141 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002142 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002143
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002144 t = rule->arg.trk_ctr.table.t;
2145 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 +02002146
2147 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002148 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002149
2150 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002151 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2152 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2153 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002154 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002155
Emeric Brun819fc6f2017-06-13 19:37:32 +02002156 if (ptr1)
2157 stktable_data_cast(ptr1, http_req_cnt)++;
2158
2159 if (ptr2)
2160 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2161 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2162
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002163 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002164
2165 /* If data was modified, we need to touch to re-schedule sync */
2166 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002167 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002168
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002169 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002170 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002171 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002172 }
2173 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002174 break;
2175
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002176 /* other flags exists, but normaly, they never be matched. */
2177 default:
2178 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002179 }
2180 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002181
Christopher Fauletea827bd2018-11-15 15:34:11 +01002182 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002183 if (early_hints) {
2184 http_send_early_hints(s, early_hints);
2185 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002186 }
2187
Willy Tarreau96257ec2012-12-27 10:46:37 +01002188 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01002189 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002190}
2191
Willy Tarreau71241ab2012-12-27 11:30:54 +01002192
Willy Tarreau51d861a2015-05-22 17:30:48 +02002193/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2194 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2195 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2196 * is returned, the process can continue the evaluation of next rule list. If
2197 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2198 * is returned, it means the operation could not be processed and a server error
2199 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2200 * deny rule. If *YIELD is returned, the caller must call again the function
2201 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002202 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002203enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002204http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002205{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002206 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002207 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002208 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002209 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002210 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002211 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02002212 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002213
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002214 /* If "the current_rule_list" match the executed rule list, we are in
2215 * resume condition. If a resume is needed it is always in the action
2216 * and never in the ACL or converters. In this case, we initialise the
2217 * current rule, and go to the action execution point.
2218 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002219 if (s->current_rule) {
2220 rule = s->current_rule;
2221 s->current_rule = NULL;
2222 if (s->current_rule_list == rules)
2223 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002224 }
2225 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002226
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002227 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002228
2229 /* check optional condition */
2230 if (rule->cond) {
2231 int ret;
2232
Willy Tarreau192252e2015-04-04 01:47:55 +02002233 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002234 ret = acl_pass(ret);
2235
2236 if (rule->cond->pol == ACL_COND_UNLESS)
2237 ret = !ret;
2238
2239 if (!ret) /* condition not matched */
2240 continue;
2241 }
2242
Willy Tarreauacc98002015-09-27 23:34:39 +02002243 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002244resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002245 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002246 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002247 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2248 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002249
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002250 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002251 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002252 rule_ret = HTTP_RULE_RES_STOP;
2253 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002254
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002255 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002256 s->task->nice = rule->arg.nice;
2257 break;
2258
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002259 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002260 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002261 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002262 break;
2263
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002264 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002265#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002266 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002267 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002268#endif
2269 break;
2270
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002271 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002272 s->logs.level = rule->arg.loglevel;
2273 break;
2274
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002275 case ACT_HTTP_REPLACE_HDR:
2276 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002277 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2278 rule->arg.hdr_add.name_len,
2279 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002280 &rule->arg.hdr_add.re, rule->action)) {
2281 rule_ret = HTTP_RULE_RES_BADREQ;
2282 goto end;
2283 }
Sasha Pachev218f0642014-06-16 12:05:59 -06002284 break;
2285
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002286 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002287 ctx.idx = 0;
2288 /* remove all occurrences of the header */
2289 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002290 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002291 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2292 }
Willy Tarreau85603282015-01-21 20:39:27 +01002293 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002294
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002295 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002296 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002297 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002298
2299 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002300 if (!replace) {
2301 rule_ret = HTTP_RULE_RES_BADREQ;
2302 goto end;
2303 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002304
2305 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002306 memcpy(replace->area, rule->arg.hdr_add.name,
2307 rule->arg.hdr_add.name_len);
2308 replace->data = rule->arg.hdr_add.name_len;
2309 replace->area[replace->data++] = ':';
2310 replace->area[replace->data++] = ' ';
2311 replace->data += build_logline(s,
2312 replace->area + replace->data,
2313 replace->size - replace->data,
2314 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002315
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002316 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002317 /* remove all occurrences of the header */
2318 ctx.idx = 0;
2319 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002320 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002321 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2322 }
2323 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002324
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002325 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002326 static unsigned char rate_limit = 0;
2327
2328 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002329 replace->area[rule->arg.hdr_add.name_len] = 0;
2330 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,
2331 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002332 }
2333
2334 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2335 if (sess->fe != s->be)
2336 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2337 if (sess->listener->counters)
2338 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2339 if (objt_server(s->target))
2340 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2341 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002342
2343 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002344 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002345 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002346
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002347 case ACT_HTTP_DEL_ACL:
2348 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002349 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002350 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002351
2352 /* collect reference */
2353 ref = pat_ref_lookup(rule->arg.map.ref);
2354 if (!ref)
2355 continue;
2356
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002357 /* allocate key */
2358 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002359 if (!key) {
2360 rule_ret = HTTP_RULE_RES_BADREQ;
2361 goto end;
2362 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002363
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002364 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002365 key->data = build_logline(s, key->area, key->size,
2366 &rule->arg.map.key);
2367 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002368
2369 /* perform update */
2370 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002371 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002372 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002373 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002374
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002375 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002376 break;
2377 }
2378
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002379 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002380 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002381 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002382
2383 /* collect reference */
2384 ref = pat_ref_lookup(rule->arg.map.ref);
2385 if (!ref)
2386 continue;
2387
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002388 /* allocate key */
2389 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002390 if (!key) {
2391 rule_ret = HTTP_RULE_RES_BADREQ;
2392 goto end;
2393 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002394
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002395 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002396 key->data = build_logline(s, key->area, key->size,
2397 &rule->arg.map.key);
2398 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002399
2400 /* perform update */
2401 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002402 if (pat_ref_find_elt(ref, key->area) == NULL)
2403 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002404
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002405 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002406 break;
2407 }
2408
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002409 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002410 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002411 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002412
2413 /* collect reference */
2414 ref = pat_ref_lookup(rule->arg.map.ref);
2415 if (!ref)
2416 continue;
2417
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002418 /* allocate key */
2419 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002420 if (!key) {
2421 rule_ret = HTTP_RULE_RES_BADREQ;
2422 goto end;
2423 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002424
2425 /* allocate value */
2426 value = alloc_trash_chunk();
2427 if (!value) {
2428 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002429 rule_ret = HTTP_RULE_RES_BADREQ;
2430 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002431 }
2432
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002433 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002434 key->data = build_logline(s, key->area, key->size,
2435 &rule->arg.map.key);
2436 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002437
2438 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002439 value->data = build_logline(s, value->area,
2440 value->size,
2441 &rule->arg.map.value);
2442 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002443
2444 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002445 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002446 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002447 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002448 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002449 else
2450 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002451 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002452 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002453 free_trash_chunk(key);
2454 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002455 break;
2456 }
William Lallemand73025dd2014-04-24 14:38:37 +02002457
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002458 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002459 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002460 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002461 rule_ret = HTTP_RULE_RES_BADREQ;
2462 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002463
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002464 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2465 /* Note: only the first valid tracking parameter of each
2466 * applies.
2467 */
2468
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002469 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002470 struct stktable *t;
2471 struct stksess *ts;
2472 struct stktable_key *key;
2473 void *ptr;
2474
2475 t = rule->arg.trk_ctr.table.t;
2476 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2477
2478 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002479 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002480
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002481 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002482
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002483 /* let's count a new HTTP request as it's the first time we do it */
2484 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2485 if (ptr)
2486 stktable_data_cast(ptr, http_req_cnt)++;
2487
2488 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2489 if (ptr)
2490 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2491 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2492
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002493 /* When the client triggers a 4xx from the server, it's most often due
2494 * to a missing object or permission. These events should be tracked
2495 * because if they happen often, it may indicate a brute force or a
2496 * vulnerability scan. Normally this is done when receiving the response
2497 * but here we're tracking after this ought to have been done so we have
2498 * to do it on purpose.
2499 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002500 if ((unsigned)(txn->status - 400) < 100) {
2501 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2502 if (ptr)
2503 stktable_data_cast(ptr, http_err_cnt)++;
2504
2505 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2506 if (ptr)
2507 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2508 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2509 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002510
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002511 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002512
Emeric Brun0fed0b02017-11-29 16:15:07 +01002513 /* If data was modified, we need to touch to re-schedule sync */
2514 stktable_touch_local(t, ts, 0);
2515
Emeric Brun819fc6f2017-06-13 19:37:32 +02002516 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2517 if (sess->fe != s->be)
2518 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2519
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002520 }
2521 }
2522 break;
2523
Thierry FOURNIER42148732015-09-02 17:17:33 +02002524 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002525 if ((s->req.flags & CF_READ_ERROR) ||
2526 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2527 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2528 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002529 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002530
Willy Tarreauacc98002015-09-27 23:34:39 +02002531 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002532 case ACT_RET_ERR:
2533 case ACT_RET_CONT:
2534 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002535 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002536 rule_ret = HTTP_RULE_RES_STOP;
2537 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002538 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002539 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002540 rule_ret = HTTP_RULE_RES_YIELD;
2541 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002542 }
William Lallemand73025dd2014-04-24 14:38:37 +02002543 break;
2544
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002545 /* other flags exists, but normaly, they never be matched. */
2546 default:
2547 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002548 }
2549 }
2550
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002551 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002552 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002553 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002554}
2555
2556
Willy Tarreau71241ab2012-12-27 11:30:54 +01002557/* Perform an HTTP redirect based on the information in <rule>. The function
2558 * returns non-zero on success, or zero in case of a, irrecoverable error such
2559 * as too large a request to build a valid response.
2560 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002561int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002562{
Willy Tarreaub329a312015-05-22 16:27:37 +02002563 struct http_msg *req = &txn->req;
2564 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002565 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002566 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002567 int ret = 0;
2568
Christopher Fauletf2824e62018-10-01 12:12:37 +02002569 if (IS_HTX_STRM(s))
2570 return htx_apply_redirect_rule(rule, s, txn);
2571
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002572 chunk = alloc_trash_chunk();
2573 if (!chunk)
2574 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002575
2576 /* build redirect message */
2577 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002578 case 308:
2579 msg_fmt = HTTP_308;
2580 break;
2581 case 307:
2582 msg_fmt = HTTP_307;
2583 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002584 case 303:
2585 msg_fmt = HTTP_303;
2586 break;
2587 case 301:
2588 msg_fmt = HTTP_301;
2589 break;
2590 case 302:
2591 default:
2592 msg_fmt = HTTP_302;
2593 break;
2594 }
2595
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002596 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2597 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002598
2599 switch(rule->type) {
2600 case REDIRECT_TYPE_SCHEME: {
2601 const char *path;
2602 const char *host;
2603 struct hdr_ctx ctx;
2604 int pathlen;
2605 int hostlen;
2606
2607 host = "";
2608 hostlen = 0;
2609 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002610 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002611 host = ctx.line + ctx.val;
2612 hostlen = ctx.vlen;
2613 }
2614
Willy Tarreau6b952c82018-09-10 17:45:34 +02002615 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002616 /* build message using path */
2617 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002618 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002619 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2620 int qs = 0;
2621 while (qs < pathlen) {
2622 if (path[qs] == '?') {
2623 pathlen = qs;
2624 break;
2625 }
2626 qs++;
2627 }
2628 }
2629 } else {
2630 path = "/";
2631 pathlen = 1;
2632 }
2633
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002634 if (rule->rdr_str) { /* this is an old "redirect" rule */
2635 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002636 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002637 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002638
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002639 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002640 memcpy(chunk->area + chunk->data, rule->rdr_str,
2641 rule->rdr_len);
2642 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002643 }
2644 else {
2645 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002646 chunk->data += build_logline(s,
2647 chunk->area + chunk->data,
2648 chunk->size - chunk->data,
2649 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002650
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002651 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002652 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002653 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002654 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002655 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002656 memcpy(chunk->area + chunk->data, "://", 3);
2657 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002658
2659 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002660 memcpy(chunk->area + chunk->data, host, hostlen);
2661 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002662
2663 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002664 memcpy(chunk->area + chunk->data, path, pathlen);
2665 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002666
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002667 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002668 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002669 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002670 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002671 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002672 chunk->area[chunk->data] = '/';
2673 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002674 }
2675
2676 break;
2677 }
2678 case REDIRECT_TYPE_PREFIX: {
2679 const char *path;
2680 int pathlen;
2681
Willy Tarreau6b952c82018-09-10 17:45:34 +02002682 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002683 /* build message using path */
2684 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002685 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002686 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2687 int qs = 0;
2688 while (qs < pathlen) {
2689 if (path[qs] == '?') {
2690 pathlen = qs;
2691 break;
2692 }
2693 qs++;
2694 }
2695 }
2696 } else {
2697 path = "/";
2698 pathlen = 1;
2699 }
2700
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002701 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002702 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002703 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002704
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002705 /* add prefix. Note that if prefix == "/", we don't want to
2706 * add anything, otherwise it makes it hard for the user to
2707 * configure a self-redirection.
2708 */
2709 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002710 memcpy(chunk->area + chunk->data,
2711 rule->rdr_str, rule->rdr_len);
2712 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002713 }
2714 }
2715 else {
2716 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002717 chunk->data += build_logline(s,
2718 chunk->area + chunk->data,
2719 chunk->size - chunk->data,
2720 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002721
2722 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002723 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002724 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002725 }
2726
2727 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002728 memcpy(chunk->area + chunk->data, path, pathlen);
2729 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002730
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002731 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002732 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002733 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002734 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002735 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002736 chunk->area[chunk->data] = '/';
2737 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002738 }
2739
2740 break;
2741 }
2742 case REDIRECT_TYPE_LOCATION:
2743 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002744 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002745 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002746 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002747
2748 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002749 memcpy(chunk->area + chunk->data, rule->rdr_str,
2750 rule->rdr_len);
2751 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002752 }
2753 else {
2754 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002755 chunk->data += build_logline(s,
2756 chunk->area + chunk->data,
2757 chunk->size - chunk->data,
2758 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002759
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002760 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002761 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002762 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002763 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002764 break;
2765 }
2766
2767 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002768 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2769 chunk->data += 14;
2770 memcpy(chunk->area + chunk->data, rule->cookie_str,
2771 rule->cookie_len);
2772 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002773 }
2774
Willy Tarreau19b14122017-02-28 09:48:11 +01002775 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002776 txn->status = rule->code;
2777 /* let's log the request time */
2778 s->logs.tv_request = now;
2779
Christopher Fauletbe821b92017-03-30 11:21:53 +02002780 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002781 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2782 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2783 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002784 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002785 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002786 memcpy(chunk->area + chunk->data,
2787 "\r\nProxy-Connection: keep-alive", 30);
2788 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002789 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002790 memcpy(chunk->area + chunk->data,
2791 "\r\nConnection: keep-alive", 24);
2792 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002793 }
2794 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002795 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2796 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002797 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002798 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002799 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002800 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002801 req->next -= req->sov;
2802 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002803 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002804 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002805 req->msg_state = HTTP_MSG_CLOSED;
2806 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002807 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002808 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002809 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002810 /* let the server side turn to SI_ST_CLO */
2811 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002812 } else {
2813 /* keep-alive not possible */
2814 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002815 memcpy(chunk->area + chunk->data,
2816 "\r\nProxy-Connection: close\r\n\r\n", 29);
2817 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002818 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002819 memcpy(chunk->area + chunk->data,
2820 "\r\nConnection: close\r\n\r\n", 23);
2821 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002822 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002823 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002824 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002825 }
2826
Willy Tarreaue7dff022015-04-03 01:14:29 +02002827 if (!(s->flags & SF_ERR_MASK))
2828 s->flags |= SF_ERR_LOCAL;
2829 if (!(s->flags & SF_FINST_MASK))
2830 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002831
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002832 ret = 1;
2833 leave:
2834 free_trash_chunk(chunk);
2835 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002836}
2837
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002838/* This stream analyser runs all HTTP request processing which is common to
2839 * frontends and backends, which means blocking ACLs, filters, connection-close,
2840 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002841 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002842 * either needs more data or wants to immediately abort the request (eg: deny,
2843 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002844 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002845int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002846{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002847 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002848 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002849 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002850 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002851 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002852 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002853 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002854 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002855
Christopher Faulete0768eb2018-10-03 16:38:02 +02002856 if (IS_HTX_STRM(s))
2857 return htx_process_req_common(s, req, an_bit, px);
2858
Willy Tarreau655dce92009-11-08 13:10:58 +01002859 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002860 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002861 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002862 }
2863
Christopher Faulet45073512018-07-20 10:16:29 +02002864 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 +02002865 now_ms, __FUNCTION__,
2866 s,
2867 req,
2868 req->rex, req->wex,
2869 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002870 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002871 req->analysers);
2872
Willy Tarreau65410832014-04-28 21:25:43 +02002873 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002874 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002875
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002876 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002877 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002878 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002879
Willy Tarreau0b748332014-04-29 00:13:29 +02002880 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002881 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2882 goto return_prx_yield;
2883
Willy Tarreau0b748332014-04-29 00:13:29 +02002884 case HTTP_RULE_RES_CONT:
2885 case HTTP_RULE_RES_STOP: /* nothing to do */
2886 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002887
Willy Tarreau0b748332014-04-29 00:13:29 +02002888 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2889 if (txn->flags & TX_CLTARPIT)
2890 goto tarpit;
2891 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002892
Willy Tarreau0b748332014-04-29 00:13:29 +02002893 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2894 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002895
Willy Tarreau0b748332014-04-29 00:13:29 +02002896 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002897 goto done;
2898
Willy Tarreau0b748332014-04-29 00:13:29 +02002899 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2900 goto return_bad_req;
2901 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002902 }
2903
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002904 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2905 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002906 struct hdr_ctx ctx;
2907
2908 ctx.idx = 0;
2909 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002910 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002911 if (unlikely(http_header_add_tail2(&txn->req,
2912 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002913 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002914 goto return_bad_req;
2915 }
2916 }
2917
2918 }
2919
Willy Tarreau52542592014-04-28 18:33:26 +02002920 /* OK at this stage, we know that the request was accepted according to
2921 * the http-request rules, we can check for the stats. Note that the
2922 * URI is detected *before* the req* rules in order not to be affected
2923 * by a possible reqrep, while they are processed *after* so that a
2924 * reqdeny can still block them. This clearly needs to change in 1.6!
2925 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002926 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002927 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002928 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002929 txn->status = 500;
2930 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002931 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002932
Willy Tarreaue7dff022015-04-03 01:14:29 +02002933 if (!(s->flags & SF_ERR_MASK))
2934 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002935 goto return_prx_cond;
2936 }
2937
2938 /* parse the whole stats request and extract the relevant information */
2939 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002940 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002941 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002942
Willy Tarreau0b748332014-04-29 00:13:29 +02002943 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2944 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002945
Willy Tarreau0b748332014-04-29 00:13:29 +02002946 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2947 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002948 }
2949
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002950 /* evaluate the req* rules except reqadd */
2951 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002952 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002953 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002954
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002955 if (txn->flags & TX_CLDENY)
2956 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002957
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002958 if (txn->flags & TX_CLTARPIT) {
2959 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002960 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002961 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002962 }
Willy Tarreau06619262006-12-17 08:37:22 +01002963
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002964 /* add request headers from the rule sets in the same order */
2965 list_for_each_entry(wl, &px->req_add, list) {
2966 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002967 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002968 ret = acl_pass(ret);
2969 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2970 ret = !ret;
2971 if (!ret)
2972 continue;
2973 }
2974
Christopher Faulet10079f52018-10-03 15:17:28 +02002975 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002976 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002977 }
2978
Willy Tarreau52542592014-04-28 18:33:26 +02002979
2980 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002981 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2982 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002983 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002984 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002985 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002986
Willy Tarreaue7dff022015-04-03 01:14:29 +02002987 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2988 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2989 if (!(s->flags & SF_FINST_MASK))
2990 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002991
Willy Tarreau70730dd2014-04-24 18:06:27 +02002992 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002993 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2994 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002995 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002996 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002997 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002998
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002999 /* check whether we have some ACLs set to redirect this request */
3000 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003001 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003002 int ret;
3003
Willy Tarreau192252e2015-04-04 01:47:55 +02003004 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003005 ret = acl_pass(ret);
3006 if (rule->cond->pol == ACL_COND_UNLESS)
3007 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003008 if (!ret)
3009 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003010 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003011 if (!http_apply_redirect_rule(rule, s, txn))
3012 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003013 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003014 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003015
Willy Tarreau2be39392010-01-03 17:24:51 +01003016 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3017 * If this happens, then the data will not come immediately, so we must
3018 * send all what we have without waiting. Note that due to the small gain
3019 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003020 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003021 * itself once used.
3022 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003023 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003024
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003025 done: /* done with this analyser, continue with next ones that the calling
3026 * points will have set, if any.
3027 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003028 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003029 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3030 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003031 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003032
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003033 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003034 /* Allow cookie logging
3035 */
3036 if (s->be->cookie_name || sess->fe->capture_name)
3037 manage_client_side_cookies(s, req);
3038
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003039 /* When a connection is tarpitted, we use the tarpit timeout,
3040 * which may be the same as the connect timeout if unspecified.
3041 * If unset, then set it to zero because we really want it to
3042 * eventually expire. We build the tarpit as an analyser.
3043 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003044 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003045
3046 /* wipe the request out so that we can drop the connection early
3047 * if the client closes first.
3048 */
3049 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003050
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003051 txn->status = http_err_codes[deny_status];
3052
Christopher Faulet0184ea72017-01-05 14:06:34 +01003053 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003054 req->analysers |= AN_REQ_HTTP_TARPIT;
3055 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3056 if (!req->analyse_exp)
3057 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003058 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003059 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003060 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003061 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003062 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003063 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003064 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003065
3066 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003067
3068 /* Allow cookie logging
3069 */
3070 if (s->be->cookie_name || sess->fe->capture_name)
3071 manage_client_side_cookies(s, req);
3072
Willy Tarreau0b748332014-04-29 00:13:29 +02003073 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003074 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003075 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003076 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003077 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003078 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003079 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003080 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003081 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003082 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003083 goto return_prx_cond;
3084
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003085 return_bad_req:
3086 /* We centralize bad requests processing here */
3087 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3088 /* we detected a parsing error. We want to archive this request
3089 * in the dedicated proxy area for later troubleshooting.
3090 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003091 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003092 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003093
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003094 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003095 txn->req.msg_state = HTTP_MSG_ERROR;
3096 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003097 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003098
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003099 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003100 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003101 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003102
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003103 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003104 if (!(s->flags & SF_ERR_MASK))
3105 s->flags |= SF_ERR_PRXCOND;
3106 if (!(s->flags & SF_FINST_MASK))
3107 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003108
Christopher Faulet0184ea72017-01-05 14:06:34 +01003109 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003110 req->analyse_exp = TICK_ETERNITY;
3111 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003112
3113 return_prx_yield:
3114 channel_dont_connect(req);
3115 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003116}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003117
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003118/* This function performs all the processing enabled for the current request.
3119 * It returns 1 if the processing can continue on next analysers, or zero if it
3120 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003121 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003122 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003123int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003124{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003125 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003126 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003127 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003128 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003129
Christopher Faulete0768eb2018-10-03 16:38:02 +02003130 if (IS_HTX_STRM(s))
3131 return htx_process_request(s, req, an_bit);
3132
Willy Tarreau655dce92009-11-08 13:10:58 +01003133 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003134 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003135 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003136 return 0;
3137 }
3138
Christopher Faulet45073512018-07-20 10:16:29 +02003139 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 +02003140 now_ms, __FUNCTION__,
3141 s,
3142 req,
3143 req->rex, req->wex,
3144 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003145 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003146 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003147
Willy Tarreau59234e92008-11-30 23:51:27 +01003148 /*
3149 * Right now, we know that we have processed the entire headers
3150 * and that unwanted requests have been filtered out. We can do
3151 * whatever we want with the remaining request. Also, now we
3152 * may have separate values for ->fe, ->be.
3153 */
Willy Tarreau06619262006-12-17 08:37:22 +01003154
Willy Tarreau59234e92008-11-30 23:51:27 +01003155 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003156 * If HTTP PROXY is set we simply get remote server address parsing
3157 * incoming request. Note that this requires that a connection is
3158 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003159 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003160 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003161 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003162 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003163
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003164 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003165 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003166 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003167 txn->req.msg_state = HTTP_MSG_ERROR;
3168 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003169 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003170 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003171
Willy Tarreaue7dff022015-04-03 01:14:29 +02003172 if (!(s->flags & SF_ERR_MASK))
3173 s->flags |= SF_ERR_RESOURCE;
3174 if (!(s->flags & SF_FINST_MASK))
3175 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003176
3177 return 0;
3178 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003179
Willy Tarreau6b952c82018-09-10 17:45:34 +02003180 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003181 if (url2sa(ci_head(req) + msg->sl.rq.u,
3182 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003183 &conn->addr.to, NULL) == -1)
3184 goto return_bad_req;
3185
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003186 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003187 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3188 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003189 * u_l characters by a single "/".
3190 */
3191 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003192 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003193 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3194 int delta;
3195
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003196 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003197 http_msg_move_end(&txn->req, delta);
3198 cur_end += delta;
3199 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3200 goto return_bad_req;
3201 }
3202 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003203 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003204 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3205 int delta;
3206
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003207 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003208 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003209 http_msg_move_end(&txn->req, delta);
3210 cur_end += delta;
3211 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3212 goto return_bad_req;
3213 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003214 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003215
Willy Tarreau59234e92008-11-30 23:51:27 +01003216 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003217 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003218 * Note that doing so might move headers in the request, but
3219 * the fields will stay coherent and the URI will not move.
3220 * This should only be performed in the backend.
3221 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003222 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003223 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003224
William Lallemanda73203e2012-03-12 12:48:57 +01003225 /* add unique-id if "header-unique-id" is specified */
3226
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003227 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003228 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003229 goto return_bad_req;
3230 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003231 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003232 }
William Lallemanda73203e2012-03-12 12:48:57 +01003233
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003234 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003235 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003236 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003237 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003238 goto return_bad_req;
3239 }
3240
Cyril Bontéb21570a2009-11-29 20:04:48 +01003241 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003242 * 9: add X-Forwarded-For if either the frontend or the backend
3243 * asks for it.
3244 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003245 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003246 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003247 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3248 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3249 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003250 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003251 /* The header is set to be added only if none is present
3252 * and we found it, so don't do anything.
3253 */
3254 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003255 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003256 /* Add an X-Forwarded-For header unless the source IP is
3257 * in the 'except' network range.
3258 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003259 if ((!sess->fe->except_mask.s_addr ||
3260 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3261 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003262 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003263 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003264 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003265 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003266 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003267 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003268
3269 /* Note: we rely on the backend to get the header name to be used for
3270 * x-forwarded-for, because the header is really meant for the backends.
3271 * However, if the backend did not specify any option, we have to rely
3272 * on the frontend's header name.
3273 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003274 if (s->be->fwdfor_hdr_len) {
3275 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003276 memcpy(trash.area,
3277 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003278 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003279 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003280 memcpy(trash.area,
3281 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003282 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003283 len += snprintf(trash.area + len,
3284 trash.size - len,
3285 ": %d.%d.%d.%d", pn[0], pn[1],
3286 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003287
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003288 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003289 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003290 }
3291 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003292 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003293 /* FIXME: for the sake of completeness, we should also support
3294 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003295 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003296 int len;
3297 char pn[INET6_ADDRSTRLEN];
3298 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003299 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003300 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003301
Willy Tarreau59234e92008-11-30 23:51:27 +01003302 /* Note: we rely on the backend to get the header name to be used for
3303 * x-forwarded-for, because the header is really meant for the backends.
3304 * However, if the backend did not specify any option, we have to rely
3305 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003306 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003307 if (s->be->fwdfor_hdr_len) {
3308 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003309 memcpy(trash.area, s->be->fwdfor_hdr_name,
3310 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003311 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003312 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003313 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3314 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003315 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003316 len += snprintf(trash.area + len, trash.size - len,
3317 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003318
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003319 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003320 goto return_bad_req;
3321 }
3322 }
3323
3324 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003325 * 10: add X-Original-To if either the frontend or the backend
3326 * asks for it.
3327 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003328 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003329
3330 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003331 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003332 /* Add an X-Original-To header unless the destination IP is
3333 * in the 'except' network range.
3334 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003335 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003336
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003337 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003338 ((!sess->fe->except_mask_to.s_addr ||
3339 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3340 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003341 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003342 (((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 +02003343 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003344 int len;
3345 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003346 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003347
3348 /* Note: we rely on the backend to get the header name to be used for
3349 * x-original-to, because the header is really meant for the backends.
3350 * However, if the backend did not specify any option, we have to rely
3351 * on the frontend's header name.
3352 */
3353 if (s->be->orgto_hdr_len) {
3354 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003355 memcpy(trash.area,
3356 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003357 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003358 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003359 memcpy(trash.area,
3360 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003361 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003362 len += snprintf(trash.area + len,
3363 trash.size - len,
3364 ": %d.%d.%d.%d", pn[0], pn[1],
3365 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003366
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003367 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003368 goto return_bad_req;
3369 }
3370 }
3371 }
3372
Willy Tarreau50fc7772012-11-11 22:19:57 +01003373 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3374 * If an "Upgrade" token is found, the header is left untouched in order not to have
3375 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3376 * "Upgrade" is present in the Connection header.
3377 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003378 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003379 unsigned int want_flags = 0;
3380
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003381 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003382 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003383 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003384 want_flags |= TX_CON_CLO_SET;
3385 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003386 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003387 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003388 want_flags |= TX_CON_KAL_SET;
3389 }
3390
3391 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003392 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003393 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003394
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003395
Willy Tarreau522d6c02009-12-06 18:49:18 +01003396 /* If we have no server assigned yet and we're balancing on url_param
3397 * with a POST request, we may be interested in checking the body for
3398 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003399 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003400 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003401 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003402 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003403 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003404 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003405 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003406
Christopher Fauletbe821b92017-03-30 11:21:53 +02003407 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3408 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003409#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003410 /* We expect some data from the client. Unless we know for sure
3411 * we already have a full request, we have to re-enable quick-ack
3412 * in case we previously disabled it, otherwise we might cause
3413 * the client to delay further data.
3414 */
3415 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3416 cli_conn && conn_ctrl_ready(cli_conn) &&
3417 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003418 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003419 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003420#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003421
Willy Tarreau59234e92008-11-30 23:51:27 +01003422 /*************************************************************
3423 * OK, that's finished for the headers. We have done what we *
3424 * could. Let's switch to the DATA state. *
3425 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003426 req->analyse_exp = TICK_ETERNITY;
3427 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003428
Willy Tarreau59234e92008-11-30 23:51:27 +01003429 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003430 /* OK let's go on with the BODY now */
3431 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003432
Willy Tarreau59234e92008-11-30 23:51:27 +01003433 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003434 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003435 /* we detected a parsing error. We want to archive this request
3436 * in the dedicated proxy area for later troubleshooting.
3437 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003438 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003439 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003440
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003441 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003442 txn->req.msg_state = HTTP_MSG_ERROR;
3443 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003444 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003445 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003446
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003447 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003448 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003449 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003450
Willy Tarreaue7dff022015-04-03 01:14:29 +02003451 if (!(s->flags & SF_ERR_MASK))
3452 s->flags |= SF_ERR_PRXCOND;
3453 if (!(s->flags & SF_FINST_MASK))
3454 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003455 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003456}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003457
Willy Tarreau60b85b02008-11-30 23:28:40 +01003458/* This function is an analyser which processes the HTTP tarpit. It always
3459 * returns zero, at the beginning because it prevents any other processing
3460 * from occurring, and at the end because it terminates the request.
3461 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003462int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003463{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003464 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003465
Christopher Faulete0768eb2018-10-03 16:38:02 +02003466 if (IS_HTX_STRM(s))
3467 return htx_process_tarpit(s, req, an_bit);
3468
Willy Tarreau60b85b02008-11-30 23:28:40 +01003469 /* This connection is being tarpitted. The CLIENT side has
3470 * already set the connect expiration date to the right
3471 * timeout. We just have to check that the client is still
3472 * there and that the timeout has not expired.
3473 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003474 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003475 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003476 !tick_is_expired(req->analyse_exp, now_ms))
3477 return 0;
3478
3479 /* We will set the queue timer to the time spent, just for
3480 * logging purposes. We fake a 500 server error, so that the
3481 * attacker will not suspect his connection has been tarpitted.
3482 * It will not cause trouble to the logs because we can exclude
3483 * the tarpitted connections by filtering on the 'PT' status flags.
3484 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003485 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3486
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003487 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003488 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003489
Christopher Faulet0184ea72017-01-05 14:06:34 +01003490 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003491 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003492
Willy Tarreaue7dff022015-04-03 01:14:29 +02003493 if (!(s->flags & SF_ERR_MASK))
3494 s->flags |= SF_ERR_PRXCOND;
3495 if (!(s->flags & SF_FINST_MASK))
3496 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003497 return 0;
3498}
3499
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003500/* This function is an analyser which waits for the HTTP request body. It waits
3501 * for either the buffer to be full, or the full advertised contents to have
3502 * reached the buffer. It must only be called after the standard HTTP request
3503 * processing has occurred, because it expects the request to be parsed and will
3504 * look for the Expect header. It may send a 100-Continue interim response. It
3505 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3506 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3507 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003508 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003509int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003510{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003511 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003512 struct http_txn *txn = s->txn;
3513 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003514
Christopher Faulete0768eb2018-10-03 16:38:02 +02003515 if (IS_HTX_STRM(s))
3516 return htx_wait_for_request_body(s, req, an_bit);
3517
Willy Tarreaud34af782008-11-30 23:36:37 +01003518 /* We have to parse the HTTP request body to find any required data.
3519 * "balance url_param check_post" should have been the only way to get
3520 * into this. We were brought here after HTTP header analysis, so all
3521 * related structures are ready.
3522 */
3523
Willy Tarreau890988f2014-04-10 11:59:33 +02003524 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3525 /* This is the first call */
3526 if (msg->msg_state < HTTP_MSG_BODY)
3527 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003528
Willy Tarreau890988f2014-04-10 11:59:33 +02003529 if (msg->msg_state < HTTP_MSG_100_SENT) {
3530 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3531 * send an HTTP/1.1 100 Continue intermediate response.
3532 */
3533 if (msg->flags & HTTP_MSGF_VER_11) {
3534 struct hdr_ctx ctx;
3535 ctx.idx = 0;
3536 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003537 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003538 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003539 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003540 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003541 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003542 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003543 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003544 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003545
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003546 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003547 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003548 * must save the body in msg->next because it survives buffer
3549 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003550 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003551 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003552
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003553 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003554 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3555 else
3556 msg->msg_state = HTTP_MSG_DATA;
3557 }
3558
Willy Tarreau890988f2014-04-10 11:59:33 +02003559 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3560 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003561 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003562 goto missing_data;
3563
3564 /* OK we have everything we need now */
3565 goto http_end;
3566 }
3567
3568 /* OK here we're parsing a chunked-encoded message */
3569
Willy Tarreau522d6c02009-12-06 18:49:18 +01003570 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003571 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003572 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003573 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003574 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003575 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003576 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003577
Willy Tarreau115acb92009-12-26 13:56:06 +01003578 if (!ret)
3579 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003580 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003581 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003582 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003583 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003584 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003585 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003586 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003587
3588 msg->chunk_len = chunk;
3589 msg->body_len += chunk;
3590
3591 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003592 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003593 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003594 }
3595
Willy Tarreaud98cf932009-12-27 22:54:55 +01003596 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003597 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3598 * for at least a whole chunk or the whole content length bytes after
3599 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003600 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003601 if (msg->msg_state == HTTP_MSG_TRAILERS)
3602 goto http_end;
3603
Willy Tarreaue115b492015-05-01 23:05:14 +02003604 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003605 goto http_end;
3606
3607 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003608 /* we get here if we need to wait for more data. If the buffer is full,
3609 * we have the maximum we can expect.
3610 */
Willy Tarreau23752332018-06-15 14:54:53 +02003611 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003612 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003613
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003614 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003615 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003616 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003617
Willy Tarreaue7dff022015-04-03 01:14:29 +02003618 if (!(s->flags & SF_ERR_MASK))
3619 s->flags |= SF_ERR_CLITO;
3620 if (!(s->flags & SF_FINST_MASK))
3621 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003622 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003623 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003624
3625 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003626 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003627 /* Not enough data. We'll re-use the http-request
3628 * timeout here. Ideally, we should set the timeout
3629 * relative to the accept() date. We just set the
3630 * request timeout once at the beginning of the
3631 * request.
3632 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003633 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003634 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003635 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003636 return 0;
3637 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003638
3639 http_end:
3640 /* The situation will not evolve, so let's give up on the analysis. */
3641 s->logs.tv_request = now; /* update the request timer to reflect full request */
3642 req->analysers &= ~an_bit;
3643 req->analyse_exp = TICK_ETERNITY;
3644 return 1;
3645
3646 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003647 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003648 txn->req.msg_state = HTTP_MSG_ERROR;
3649 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003650 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003651
Willy Tarreaue7dff022015-04-03 01:14:29 +02003652 if (!(s->flags & SF_ERR_MASK))
3653 s->flags |= SF_ERR_PRXCOND;
3654 if (!(s->flags & SF_FINST_MASK))
3655 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003656
Willy Tarreau522d6c02009-12-06 18:49:18 +01003657 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003658 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003659 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003660 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003661 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003662 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003663}
3664
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003665/* send a server's name with an outgoing request over an established connection.
3666 * Note: this function is designed to be called once the request has been scheduled
3667 * for being forwarded. This is the reason why it rewinds the buffer before
3668 * proceeding.
3669 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003670int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003671
3672 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003673 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003674 char *hdr_name = be->server_id_hdr_name;
3675 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003676 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003677 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003678 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003679
William Lallemandd9e90662012-01-30 17:27:17 +01003680 ctx.idx = 0;
3681
Willy Tarreau211cdec2014-04-17 20:18:08 +02003682 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003683 if (old_o) {
3684 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003685 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003686 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003687 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003688 }
3689
Willy Tarreauf37954d2018-06-15 18:31:02 +02003690 old_i = ci_data(chn);
3691 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 -05003692 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003693 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003694 }
3695
3696 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003697 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003698 memcpy(hdr_val, hdr_name, hdr_name_len);
3699 hdr_val += hdr_name_len;
3700 *hdr_val++ = ':';
3701 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003702 hdr_val += strlcpy2(hdr_val, srv_name,
3703 trash.area + trash.size - hdr_val);
3704 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3705 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003706
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003707 if (old_o) {
3708 /* If this was a forwarded request, we must readjust the amount of
3709 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003710 * variations. Note that the current state is >= HTTP_MSG_BODY,
3711 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003712 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003713 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003714 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003715 txn->req.next -= old_o;
3716 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003717 }
3718
Mark Lamourinec2247f02012-01-04 13:02:01 -05003719 return 0;
3720}
3721
Willy Tarreau610ecce2010-01-04 21:15:02 +01003722/* Terminate current transaction and prepare a new one. This is very tricky
3723 * right now but it works.
3724 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003725void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003726{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003727 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003728 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003729 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003730 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003731 struct connection *srv_conn;
3732 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003733 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003734
Willy Tarreau610ecce2010-01-04 21:15:02 +01003735 /* FIXME: We need a more portable way of releasing a backend's and a
3736 * server's connections. We need a safer way to reinitialize buffer
3737 * flags. We also need a more accurate method for computing per-request
3738 * data.
3739 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003740 cs = objt_cs(s->si[1].end);
3741 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003742
Willy Tarreau4213a112013-12-15 10:25:42 +01003743 /* unless we're doing keep-alive, we want to quickly close the connection
3744 * to the server.
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003745 * XXX cognet: If the connection doesn't have a owner then it may not
3746 * be referenced anywhere, just kill it now, even if it could be reused.
3747 * To be revisited later when revisited later when we handle connection
3748 * pools properly.
Willy Tarreau4213a112013-12-15 10:25:42 +01003749 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003750 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003751 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003752 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3753 si_shutr(&s->si[1]);
3754 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003755 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003756
Willy Tarreaue7dff022015-04-03 01:14:29 +02003757 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003758 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003759 if (unlikely(s->srv_conn))
3760 sess_change_server(s, NULL);
3761 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003762
3763 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003764 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003765
Willy Tarreaueee5b512015-04-03 23:46:31 +02003766 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003767 int n;
3768
Willy Tarreaueee5b512015-04-03 23:46:31 +02003769 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003770 if (n < 1 || n > 5)
3771 n = 0;
3772
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003773 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003774 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003775 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003776 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003777 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003778 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3779 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003780 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003781 }
3782
3783 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003784 s->logs.bytes_in -= ci_data(&s->req);
3785 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003786
Willy Tarreau66425e32018-07-25 06:55:12 +02003787 /* we may need to know the position in the queue */
3788 pendconn_free(s);
3789
Willy Tarreau610ecce2010-01-04 21:15:02 +01003790 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003791 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003792 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003793 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003794 s->do_log(s);
3795 }
3796
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003797 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003798 stream_stop_content_counters(s);
3799 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003800
Willy Tarreau610ecce2010-01-04 21:15:02 +01003801 s->logs.accept_date = date; /* user-visible date for logging */
3802 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003803 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3804 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003805 tv_zero(&s->logs.tv_request);
3806 s->logs.t_queue = -1;
3807 s->logs.t_connect = -1;
3808 s->logs.t_data = -1;
3809 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003810 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3811 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003812
Willy Tarreauf37954d2018-06-15 18:31:02 +02003813 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3814 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003815
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003816 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003817 if (s->flags & SF_CURR_SESS) {
3818 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003819 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003820 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003821 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003822 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003823 }
3824
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003825 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003826
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003827
3828 /* If we're doing keepalive, first call the mux detach() method
3829 * to let it know we want to detach without freing the connection.
3830 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003831 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003832 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003833 !si_conn_ready(&s->si[1]) || !srv_conn->owner)
Willy Tarreau323a2d92015-08-04 19:00:17 +02003834 srv_conn = NULL;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003835 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003836
Willy Tarreau350f4872014-11-28 14:42:25 +01003837 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3838 s->si[1].err_type = SI_ET_NONE;
3839 s->si[1].conn_retries = 0; /* used for logging too */
3840 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003841 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 +01003842 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);
Willy Tarreauede3d882018-10-24 17:17:56 +02003843 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);
Willy Tarreaue7dff022015-04-03 01:14:29 +02003844 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3845 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3846 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003847
Patrick Hemmere3faf022018-08-22 10:02:00 -04003848 hlua_ctx_destroy(s->hlua);
3849 s->hlua = NULL;
3850
Willy Tarreaueee5b512015-04-03 23:46:31 +02003851 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003852 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003853 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003854
3855 if (prev_status == 401 || prev_status == 407) {
3856 /* In HTTP keep-alive mode, if we receive a 401, we still have
3857 * a chance of being able to send the visitor again to the same
3858 * server over the same connection. This is required by some
3859 * broken protocols such as NTLM, and anyway whenever there is
3860 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003861 * it's better to do it (at least it helps with debugging), at
3862 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003863 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003864 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003865 }
3866
Willy Tarreau53f96852016-02-02 18:50:47 +01003867 /* Never ever allow to reuse a connection from a non-reuse backend */
3868 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3869 srv_conn->flags |= CO_FL_PRIVATE;
3870
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003871 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003872 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003873
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003874 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003875 s->req.flags |= CF_NEVER_WAIT;
3876 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003877 }
3878
Willy Tarreau714ea782015-11-25 20:11:11 +01003879 /* we're removing the analysers, we MUST re-enable events detection.
3880 * We don't enable close on the response channel since it's either
3881 * already closed, or in keep-alive with an idle connection handler.
3882 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003883 channel_auto_read(&s->req);
3884 channel_auto_close(&s->req);
3885 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003886
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003887 /* we're in keep-alive with an idle connection, monitor it if not already done */
3888 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003889 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003890 if (srv) {
3891 if (srv_conn->flags & CO_FL_PRIVATE)
3892 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3893 else if (prev_flags & TX_NOT_FIRST)
3894 /* note: we check the request, not the connection, but
3895 * this is valid for strategies SAFE and AGGR, and in
3896 * case of ALWS, we don't care anyway.
3897 */
3898 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3899 else
3900 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3901 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003902 }
Christopher Faulete6006242017-03-10 11:52:44 +01003903 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3904 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003905}
3906
3907
3908/* This function updates the request state machine according to the response
3909 * state machine and buffer flags. It returns 1 if it changes anything (flag
3910 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3911 * it is only used to find when a request/response couple is complete. Both
3912 * this function and its equivalent should loop until both return zero. It
3913 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3914 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003915int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003916{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003917 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003918 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003919 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003920 unsigned int old_state = txn->req.msg_state;
3921
Christopher Faulet4be98032017-07-18 10:48:24 +02003922 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003923 return 0;
3924
3925 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003926 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003927 * We can shut the read side unless we want to abort_on_close,
3928 * or we have a POST request. The issue with POST requests is
3929 * that some browsers still send a CRLF after the request, and
3930 * this CRLF must be read so that it does not remain in the kernel
3931 * buffers, otherwise a close could cause an RST on some systems
3932 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003933 * Note that if we're using keep-alive on the client side, we'd
3934 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003935 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003936 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003937 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003938 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3939 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003940 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3941 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003942 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003943 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003944
Willy Tarreau40f151a2012-12-20 12:10:09 +01003945 /* if the server closes the connection, we want to immediately react
3946 * and close the socket to save packets and syscalls.
3947 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003948 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003949
Willy Tarreau7f876a12015-11-18 11:59:55 +01003950 /* In any case we've finished parsing the request so we must
3951 * disable Nagle when sending data because 1) we're not going
3952 * to shut this side, and 2) the server is waiting for us to
3953 * send pending data.
3954 */
3955 chn->flags |= CF_NEVER_WAIT;
3956
Willy Tarreau610ecce2010-01-04 21:15:02 +01003957 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3958 goto wait_other_side;
3959
3960 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3961 /* The server has not finished to respond, so we
3962 * don't want to move in order not to upset it.
3963 */
3964 goto wait_other_side;
3965 }
3966
Willy Tarreau610ecce2010-01-04 21:15:02 +01003967 /* When we get here, it means that both the request and the
3968 * response have finished receiving. Depending on the connection
3969 * mode, we'll have to wait for the last bytes to leave in either
3970 * direction, and sometimes for a close to be effective.
3971 */
3972
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003973 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3974 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003975 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3976 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003977 }
3978 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3979 /* Option forceclose is set, or either side wants to close,
3980 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003981 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003982 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003983 *
3984 * However, there is an exception if the response
3985 * length is undefined. In this case, we need to wait
3986 * the close from the server. The response will be
3987 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003988 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003989 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3990 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3991 goto check_channel_flags;
3992
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003993 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3994 channel_shutr_now(chn);
3995 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003996 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003997 }
3998 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003999 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4000 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004001 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004002 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4003 channel_auto_read(chn);
4004 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004005 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004006 }
4007
Christopher Faulet4be98032017-07-18 10:48:24 +02004008 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 }
4010
4011 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4012 http_msg_closing:
4013 /* nothing else to forward, just waiting for the output buffer
4014 * to be empty and for the shutw_now to take effect.
4015 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004016 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004017 txn->req.msg_state = HTTP_MSG_CLOSED;
4018 goto http_msg_closed;
4019 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004020 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004021 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004023 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004024 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004025 }
4026
4027 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4028 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004029 /* if we don't know whether the server will close, we need to hard close */
4030 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4031 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4032
Willy Tarreau3988d932013-12-27 23:03:08 +01004033 /* see above in MSG_DONE why we only do this in these states */
4034 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4035 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004036 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4037 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004038 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004039 goto wait_other_side;
4040 }
4041
Christopher Faulet4be98032017-07-18 10:48:24 +02004042 check_channel_flags:
4043 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4044 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4045 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004046 txn->req.msg_state = HTTP_MSG_CLOSING;
4047 goto http_msg_closing;
4048 }
4049
4050
Willy Tarreau610ecce2010-01-04 21:15:02 +01004051 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004052 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004053}
4054
4055
4056/* This function updates the response state machine according to the request
4057 * state machine and buffer flags. It returns 1 if it changes anything (flag
4058 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4059 * it is only used to find when a request/response couple is complete. Both
4060 * this function and its equivalent should loop until both return zero. It
4061 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4062 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004063int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004064{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004065 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004066 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004067 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004068 unsigned int old_state = txn->rsp.msg_state;
4069
Christopher Faulet4be98032017-07-18 10:48:24 +02004070 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004071 return 0;
4072
4073 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4074 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004075 * still monitor the server connection for a possible close
4076 * while the request is being uploaded, so we don't disable
4077 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004078 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004079 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004080
4081 if (txn->req.msg_state == HTTP_MSG_ERROR)
4082 goto wait_other_side;
4083
4084 if (txn->req.msg_state < HTTP_MSG_DONE) {
4085 /* The client seems to still be sending data, probably
4086 * because we got an error response during an upload.
4087 * We have the choice of either breaking the connection
4088 * or letting it pass through. Let's do the later.
4089 */
4090 goto wait_other_side;
4091 }
4092
Willy Tarreau610ecce2010-01-04 21:15:02 +01004093 /* When we get here, it means that both the request and the
4094 * response have finished receiving. Depending on the connection
4095 * mode, we'll have to wait for the last bytes to leave in either
4096 * direction, and sometimes for a close to be effective.
4097 */
4098
4099 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4100 /* Server-close mode : shut read and wait for the request
4101 * side to close its output buffer. The caller will detect
4102 * when we're in DONE and the other is in CLOSED and will
4103 * catch that for the final cleanup.
4104 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004105 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4106 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004107 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004108 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4109 /* Option forceclose is set, or either side wants to close,
4110 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004111 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004112 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004113 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004114 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004115 channel_shutr_now(chn);
4116 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004117 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004118 }
4119 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004120 /* The last possible modes are keep-alive and tunnel. Tunnel will
4121 * need to forward remaining data. Keep-alive will need to monitor
4122 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004123 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004124 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004125 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004126 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4127 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004128 }
4129
Christopher Faulet4be98032017-07-18 10:48:24 +02004130 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004131 }
4132
4133 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4134 http_msg_closing:
4135 /* nothing else to forward, just waiting for the output buffer
4136 * to be empty and for the shutw_now to take effect.
4137 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004138 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004139 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4140 goto http_msg_closed;
4141 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004142 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004143 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004144 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004145 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004146 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004147 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004148 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004149 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004150 }
4151
4152 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4153 http_msg_closed:
4154 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004155 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004156 channel_auto_close(chn);
4157 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004158 goto wait_other_side;
4159 }
4160
Christopher Faulet4be98032017-07-18 10:48:24 +02004161 check_channel_flags:
4162 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4163 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4164 /* if we've just closed an output, let's switch */
4165 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4166 goto http_msg_closing;
4167 }
4168
Willy Tarreau610ecce2010-01-04 21:15:02 +01004169 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004170 /* We force the response to leave immediately if we're waiting for the
4171 * other side, since there is no pending shutdown to push it out.
4172 */
4173 if (!channel_is_empty(chn))
4174 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004175 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004176}
4177
4178
Christopher Faulet894da4c2017-07-18 11:29:07 +02004179/* Resync the request and response state machines. */
4180void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004181{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004182 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004183#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004184 int old_req_state = txn->req.msg_state;
4185 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004186#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004187
Willy Tarreau610ecce2010-01-04 21:15:02 +01004188 http_sync_req_state(s);
4189 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004190 if (!http_sync_res_state(s))
4191 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004192 if (!http_sync_req_state(s))
4193 break;
4194 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004195
Christopher Faulet894da4c2017-07-18 11:29:07 +02004196 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4197 "req->analysers=0x%08x res->analysers=0x%08x\n",
4198 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004199 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4200 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004201 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004202
4203
Willy Tarreau610ecce2010-01-04 21:15:02 +01004204 /* OK, both state machines agree on a compatible state.
4205 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004206 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4207 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004208 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4209 * means we must abort the request.
4210 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4211 * corresponding channel.
4212 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4213 * on the response with server-close mode means we've completed one
4214 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004215 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004216 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4217 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004218 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004219 channel_auto_close(&s->req);
4220 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004221 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004222 channel_auto_close(&s->res);
4223 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004224 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004225 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4226 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004227 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004228 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004229 channel_auto_close(&s->res);
4230 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004231 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004232 channel_abort(&s->req);
4233 channel_auto_close(&s->req);
4234 channel_auto_read(&s->req);
4235 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004236 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004237 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4238 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4239 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4240 s->req.analysers &= AN_REQ_FLT_END;
4241 if (HAS_REQ_DATA_FILTERS(s))
4242 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4243 }
4244 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4245 s->res.analysers &= AN_RES_FLT_END;
4246 if (HAS_RSP_DATA_FILTERS(s))
4247 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4248 }
4249 channel_auto_close(&s->req);
4250 channel_auto_read(&s->req);
4251 channel_auto_close(&s->res);
4252 channel_auto_read(&s->res);
4253 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004254 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4255 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004256 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004257 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4258 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4259 /* server-close/keep-alive: terminate this transaction,
4260 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004261 * a fresh-new transaction, but only once we're sure there's
4262 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004263 * another request. They must not hold any pending output data
4264 * and the response buffer must realigned
4265 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004266 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004267 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004268 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004269 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004270 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004271 else {
4272 s->req.analysers = AN_REQ_FLT_END;
4273 s->res.analysers = AN_RES_FLT_END;
4274 txn->flags |= TX_WAIT_CLEANUP;
4275 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004276 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004277}
4278
Willy Tarreaud98cf932009-12-27 22:54:55 +01004279/* This function is an analyser which forwards request body (including chunk
4280 * sizes if any). It is called as soon as we must forward, even if we forward
4281 * zero byte. The only situation where it must not be called is when we're in
4282 * tunnel mode and we want to forward till the close. It's used both to forward
4283 * remaining data and to resync after end of body. It expects the msg_state to
4284 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004285 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004286 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004287 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004288 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004289int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004290{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004291 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004292 struct http_txn *txn = s->txn;
4293 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004294 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004295
Christopher Faulete0768eb2018-10-03 16:38:02 +02004296 if (IS_HTX_STRM(s))
4297 return htx_request_forward_body(s, req, an_bit);
4298
Christopher Faulet45073512018-07-20 10:16:29 +02004299 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 +02004300 now_ms, __FUNCTION__,
4301 s,
4302 req,
4303 req->rex, req->wex,
4304 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004305 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004306 req->analysers);
4307
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004308 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4309 return 0;
4310
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004311 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004312 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004313 /* Output closed while we were sending data. We must abort and
4314 * wake the other side up.
4315 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004316 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004317 msg->msg_state = HTTP_MSG_ERROR;
4318 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004319 return 1;
4320 }
4321
Willy Tarreaud98cf932009-12-27 22:54:55 +01004322 /* Note that we don't have to send 100-continue back because we don't
4323 * need the data to complete our job, and it's up to the server to
4324 * decide whether to return 100, 417 or anything else in return of
4325 * an "Expect: 100-continue" header.
4326 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004327 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004328 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4329 ? HTTP_MSG_CHUNK_SIZE
4330 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004331
4332 /* TODO/filters: when http-buffer-request option is set or if a
4333 * rule on url_param exists, the first chunk size could be
4334 * already parsed. In that case, msg->next is after the chunk
4335 * size (including the CRLF after the size). So this case should
4336 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004337 }
4338
Willy Tarreau7ba23542014-04-17 21:50:00 +02004339 /* Some post-connect processing might want us to refrain from starting to
4340 * forward data. Currently, the only reason for this is "balance url_param"
4341 * whichs need to parse/process the request after we've enabled forwarding.
4342 */
4343 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004344 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004345 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004346 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004347 channel_dont_close(req); /* don't fail on early shutr */
4348 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004349 }
4350 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4351 }
4352
Willy Tarreau80a92c02014-03-12 10:41:13 +01004353 /* in most states, we should abort in case of early close */
4354 channel_auto_close(req);
4355
Willy Tarreauefdf0942014-04-24 20:08:57 +02004356 if (req->to_forward) {
4357 /* We can't process the buffer's contents yet */
4358 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004359 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004360 }
4361
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004362 if (msg->msg_state < HTTP_MSG_DONE) {
4363 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4364 ? http_msg_forward_chunked_body(s, msg)
4365 : http_msg_forward_body(s, msg));
4366 if (!ret)
4367 goto missing_data_or_waiting;
4368 if (ret < 0)
4369 goto return_bad_req;
4370 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004371
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004372 /* other states, DONE...TUNNEL */
4373 /* we don't want to forward closes on DONE except in tunnel mode. */
4374 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4375 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004376
Christopher Faulet894da4c2017-07-18 11:29:07 +02004377 http_resync_states(s);
4378 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004379 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4380 if (req->flags & CF_SHUTW) {
4381 /* request errors are most likely due to the
4382 * server aborting the transfer. */
4383 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004384 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004385 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004386 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004387 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004388 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004389 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004390 }
4391
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004392 /* If "option abortonclose" is set on the backend, we want to monitor
4393 * the client's connection and forward any shutdown notification to the
4394 * server, which will decide whether to close or to go on processing the
4395 * request. We only do that in tunnel mode, and not in other modes since
4396 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004397 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004398 channel_auto_read(req);
4399 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4400 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4401 s->si[1].flags |= SI_FL_NOLINGER;
4402 channel_auto_close(req);
4403 }
4404 else if (s->txn->meth == HTTP_METH_POST) {
4405 /* POST requests may require to read extra CRLF sent by broken
4406 * browsers and which could cause an RST to be sent upon close
4407 * on some systems (eg: Linux). */
4408 channel_auto_read(req);
4409 }
4410 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004411
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004412 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004413 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004414 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004415 if (!(s->flags & SF_ERR_MASK))
4416 s->flags |= SF_ERR_CLICL;
4417 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004418 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004419 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004420 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004421 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004422 }
4423
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004424 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4425 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004426 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004427 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004428
4429 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004430 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004431
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004432 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004433 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004434 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004435 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004436
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004437 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004438 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004439 * And when content-length is used, we never want to let the possible
4440 * shutdown be forwarded to the other side, as the state machine will
4441 * take care of it once the client responds. It's also important to
4442 * prevent TIME_WAITs from accumulating on the backend side, and for
4443 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004444 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004445 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004446 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004447
Willy Tarreau5c620922011-05-11 19:56:11 +02004448 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004449 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004450 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004451 * modes are already handled by the stream sock layer. We must not do
4452 * this in content-length mode because it could present the MSG_MORE
4453 * flag with the last block of forwarded data, which would cause an
4454 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004455 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004456 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004457 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004458
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004459 return 0;
4460
Willy Tarreaud98cf932009-12-27 22:54:55 +01004461 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004462 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004463 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004464 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004465
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004466 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004467 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004468 txn->req.msg_state = HTTP_MSG_ERROR;
4469 if (txn->status) {
4470 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004471 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004472 } else {
4473 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004474 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004475 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004476 req->analysers &= AN_REQ_FLT_END;
4477 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 +01004478
Willy Tarreaue7dff022015-04-03 01:14:29 +02004479 if (!(s->flags & SF_ERR_MASK))
4480 s->flags |= SF_ERR_PRXCOND;
4481 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004482 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004483 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004484 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004485 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004486 }
4487 return 0;
4488
4489 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004490 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004491 txn->req.msg_state = HTTP_MSG_ERROR;
4492 if (txn->status) {
4493 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004494 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004495 } else {
4496 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004497 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004498 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004499 req->analysers &= AN_REQ_FLT_END;
4500 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 +01004501
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004502 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4503 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004504 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004505 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004506
Willy Tarreaue7dff022015-04-03 01:14:29 +02004507 if (!(s->flags & SF_ERR_MASK))
4508 s->flags |= SF_ERR_SRVCL;
4509 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004510 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004511 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004512 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004513 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004514 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004515 return 0;
4516}
4517
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004518/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4519 * processing can continue on next analysers, or zero if it either needs more
4520 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004521 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004522 * when it has nothing left to do, and may remove any analyser when it wants to
4523 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004524 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004525int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004526{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004527 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004528 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004529 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004530 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004531 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004532 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004533 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004534 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004535
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004536 srv_conn = cs_conn(objt_cs(s->si[1].end));
4537
Christopher Faulete0768eb2018-10-03 16:38:02 +02004538 if (IS_HTX_STRM(s))
4539 return htx_wait_for_response(s, rep, an_bit);
4540
Christopher Faulet45073512018-07-20 10:16:29 +02004541 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 +02004542 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004543 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004544 rep,
4545 rep->rex, rep->wex,
4546 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004547 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004548 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004549
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004550 /*
4551 * Now parse the partial (or complete) lines.
4552 * We will check the response syntax, and also join multi-line
4553 * headers. An index of all the lines will be elaborated while
4554 * parsing.
4555 *
4556 * For the parsing, we use a 28 states FSM.
4557 *
4558 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004559 * ci_head(rep) = beginning of response
4560 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4561 * ci_tail(rep) = end of input data
4562 * msg->eol = end of current header or line (LF or CRLF)
4563 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004564 */
4565
Willy Tarreau628c40c2014-04-24 19:11:26 +02004566 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004567 /* There's a protected area at the end of the buffer for rewriting
4568 * purposes. We don't want to start to parse the request if the
4569 * protected area is affected, because we may have to move processed
4570 * data later, which is much more complicated.
4571 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004572 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004573 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004574 /* some data has still not left the buffer, wake us once that's done */
4575 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4576 goto abort_response;
4577 channel_dont_close(rep);
4578 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004579 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004580 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004581 }
4582
Willy Tarreau188e2302018-06-15 11:11:53 +02004583 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004584 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004585 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004586
Willy Tarreauf37954d2018-06-15 18:31:02 +02004587 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004588 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004589 }
4590
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004591 /* 1: we might have to print this header in debug mode */
4592 if (unlikely((global.mode & MODE_DEBUG) &&
4593 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004594 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004595 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004596
Willy Tarreauf37954d2018-06-15 18:31:02 +02004597 sol = ci_head(rep);
4598 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004599 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004600
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004601 sol += hdr_idx_first_pos(&txn->hdr_idx);
4602 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004603
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004604 while (cur_idx) {
4605 eol = sol + txn->hdr_idx.v[cur_idx].len;
4606 debug_hdr("srvhdr", s, sol, eol);
4607 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4608 cur_idx = txn->hdr_idx.v[cur_idx].next;
4609 }
4610 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004611
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004612 /*
4613 * Now we quickly check if we have found a full valid response.
4614 * If not so, we check the FD and buffer states before leaving.
4615 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004616 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004617 * responses are checked first.
4618 *
4619 * Depending on whether the client is still there or not, we
4620 * may send an error response back or not. Note that normally
4621 * we should only check for HTTP status there, and check I/O
4622 * errors somewhere else.
4623 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004624
Willy Tarreau655dce92009-11-08 13:10:58 +01004625 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004626 /* Invalid response */
4627 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4628 /* we detected a parsing error. We want to archive this response
4629 * in the dedicated proxy area for later troubleshooting.
4630 */
4631 hdr_response_bad:
4632 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004633 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004634
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004635 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004636 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004637 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004638 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004639 }
Willy Tarreau64648412010-03-05 10:41:54 +01004640 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004641 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004642 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004643 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004644 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004645 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004646 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004647
Willy Tarreaue7dff022015-04-03 01:14:29 +02004648 if (!(s->flags & SF_ERR_MASK))
4649 s->flags |= SF_ERR_PRXCOND;
4650 if (!(s->flags & SF_FINST_MASK))
4651 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004652
4653 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004654 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004655
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004656 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004657 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004658 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004659 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004660 goto hdr_response_bad;
4661 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004662
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004663 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004664 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004665 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004666 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004667 else if (txn->flags & TX_NOT_FIRST)
4668 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004669
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004670 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004671 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004672 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004673 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004674 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004675
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004676 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004677 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004678 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004679
4680 /* Check to see if the server refused the early data.
4681 * If so, just send a 425
4682 */
4683 if (objt_cs(s->si[1].end)) {
4684 struct connection *conn = objt_cs(s->si[1].end)->conn;
4685
4686 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4687 txn->status = 425;
4688 }
4689
Willy Tarreau350f4872014-11-28 14:42:25 +01004690 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004691 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004692 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004693
Willy Tarreaue7dff022015-04-03 01:14:29 +02004694 if (!(s->flags & SF_ERR_MASK))
4695 s->flags |= SF_ERR_SRVCL;
4696 if (!(s->flags & SF_FINST_MASK))
4697 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004698 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004699 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004700
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004701 /* read timeout : return a 504 to the client. */
4702 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004703 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004704 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004705
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004706 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004707 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004708 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004709 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004710 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004711
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004712 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004713 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004714 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004715 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004716 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004717 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004718
Willy Tarreaue7dff022015-04-03 01:14:29 +02004719 if (!(s->flags & SF_ERR_MASK))
4720 s->flags |= SF_ERR_SRVTO;
4721 if (!(s->flags & SF_FINST_MASK))
4722 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004723 return 0;
4724 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004725
Willy Tarreauf003d372012-11-26 13:35:37 +01004726 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004727 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004728 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4729 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004730 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004731 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004732
Christopher Faulet0184ea72017-01-05 14:06:34 +01004733 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004734 channel_auto_close(rep);
4735
4736 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004737 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004738 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004739
Willy Tarreaue7dff022015-04-03 01:14:29 +02004740 if (!(s->flags & SF_ERR_MASK))
4741 s->flags |= SF_ERR_CLICL;
4742 if (!(s->flags & SF_FINST_MASK))
4743 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004744
Willy Tarreau87b09662015-04-03 00:22:06 +02004745 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004746 return 0;
4747 }
4748
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004749 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004750 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004751 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004752 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004753 else if (txn->flags & TX_NOT_FIRST)
4754 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004755
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004756 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004757 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004758 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004759 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004760 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004761
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004762 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004763 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004764 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004765 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004766 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004767 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004768
Willy Tarreaue7dff022015-04-03 01:14:29 +02004769 if (!(s->flags & SF_ERR_MASK))
4770 s->flags |= SF_ERR_SRVCL;
4771 if (!(s->flags & SF_FINST_MASK))
4772 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004773 return 0;
4774 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004775
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004776 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004777 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004778 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004779 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004780 else if (txn->flags & TX_NOT_FIRST)
4781 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004782
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004783 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004784 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004785 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004786
Willy Tarreaue7dff022015-04-03 01:14:29 +02004787 if (!(s->flags & SF_ERR_MASK))
4788 s->flags |= SF_ERR_CLICL;
4789 if (!(s->flags & SF_FINST_MASK))
4790 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004791
Willy Tarreau87b09662015-04-03 00:22:06 +02004792 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004793 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004794 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004795
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004796 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004797 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004798 return 0;
4799 }
4800
4801 /* More interesting part now : we know that we have a complete
4802 * response which at least looks like HTTP. We have an indicator
4803 * of each header's length, so we can parse them quickly.
4804 */
4805
4806 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004807 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004808
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004809 /*
4810 * 1: get the status code
4811 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004812 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004813 if (n < 1 || n > 5)
4814 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004815 /* when the client triggers a 4xx from the server, it's most often due
4816 * to a missing object or permission. These events should be tracked
4817 * because if they happen often, it may indicate a brute force or a
4818 * vulnerability scan.
4819 */
4820 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004821 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004822
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004823 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004824 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004825
Willy Tarreau91852eb2015-05-01 13:26:00 +02004826 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4827 * exactly one digit "." one digit. This check may be disabled using
4828 * option accept-invalid-http-response.
4829 */
4830 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4831 if (msg->sl.st.v_l != 8) {
4832 msg->err_pos = 0;
4833 goto hdr_response_bad;
4834 }
4835
Willy Tarreauf37954d2018-06-15 18:31:02 +02004836 if (ci_head(rep)[4] != '/' ||
4837 !isdigit((unsigned char)ci_head(rep)[5]) ||
4838 ci_head(rep)[6] != '.' ||
4839 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004840 msg->err_pos = 4;
4841 goto hdr_response_bad;
4842 }
4843 }
4844
Willy Tarreau5b154472009-12-21 20:11:07 +01004845 /* check if the response is HTTP/1.1 or above */
4846 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004847 ((ci_head(rep)[5] > '1') ||
4848 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004849 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004850
4851 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004852 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 +01004853
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004854 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004855 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004856
Willy Tarreauf37954d2018-06-15 18:31:02 +02004857 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004858
Willy Tarreau39650402010-03-15 19:44:39 +01004859 /* Adjust server's health based on status code. Note: status codes 501
4860 * and 505 are triggered on demand by client request, so we must not
4861 * count them as server failures.
4862 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004863 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004864 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004865 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004866 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004867 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004868 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004869
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004870 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004871 * We may be facing a 100-continue response, or any other informational
4872 * 1xx response which is non-final, in which case this is not the right
4873 * response, and we're waiting for the next one. Let's allow this response
4874 * to go to the client and wait for the next one. There's an exception for
4875 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004876 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004877 if (txn->status < 200 &&
4878 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004879 hdr_idx_init(&txn->hdr_idx);
4880 msg->next -= channel_forward(rep, msg->next);
4881 msg->msg_state = HTTP_MSG_RPBEFORE;
4882 txn->status = 0;
4883 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004884 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004885 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004886 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004887
Willy Tarreaua14ad722017-07-07 11:36:32 +02004888 /*
4889 * 2: check for cacheability.
4890 */
4891
4892 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004893 case 200:
4894 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004895 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004896 case 206:
4897 case 300:
4898 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004899 case 404:
4900 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004901 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004902 case 414:
4903 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004904 break;
4905 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004906 /* RFC7231#6.1:
4907 * Responses with status codes that are defined as
4908 * cacheable by default (e.g., 200, 203, 204, 206,
4909 * 300, 301, 404, 405, 410, 414, and 501 in this
4910 * specification) can be reused by a cache with
4911 * heuristic expiration unless otherwise indicated
4912 * by the method definition or explicit cache
4913 * controls [RFC7234]; all other status codes are
4914 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004915 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004916 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004917 break;
4918 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004919
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004920 /*
4921 * 3: we may need to capture headers
4922 */
4923 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004924 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004925 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4926 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004927
Willy Tarreau557f1992015-05-01 10:05:17 +02004928 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4929 * by RFC7230#3.3.3 :
4930 *
4931 * The length of a message body is determined by one of the following
4932 * (in order of precedence):
4933 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004934 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4935 * the connection will become a tunnel immediately after the empty
4936 * line that concludes the header fields. A client MUST ignore
4937 * any Content-Length or Transfer-Encoding header fields received
4938 * in such a message. Any 101 response (Switching Protocols) is
4939 * managed in the same manner.
4940 *
4941 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004942 * (Informational), 204 (No Content), or 304 (Not Modified) status
4943 * code is always terminated by the first empty line after the
4944 * header fields, regardless of the header fields present in the
4945 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004946 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004947 * 3. If a Transfer-Encoding header field is present and the chunked
4948 * transfer coding (Section 4.1) is the final encoding, the message
4949 * body length is determined by reading and decoding the chunked
4950 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004951 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004952 * If a Transfer-Encoding header field is present in a response and
4953 * the chunked transfer coding is not the final encoding, the
4954 * message body length is determined by reading the connection until
4955 * it is closed by the server. If a Transfer-Encoding header field
4956 * is present in a request and the chunked transfer coding is not
4957 * the final encoding, the message body length cannot be determined
4958 * reliably; the server MUST respond with the 400 (Bad Request)
4959 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004960 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004961 * If a message is received with both a Transfer-Encoding and a
4962 * Content-Length header field, the Transfer-Encoding overrides the
4963 * Content-Length. Such a message might indicate an attempt to
4964 * perform request smuggling (Section 9.5) or response splitting
4965 * (Section 9.4) and ought to be handled as an error. A sender MUST
4966 * remove the received Content-Length field prior to forwarding such
4967 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004968 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004969 * 4. If a message is received without Transfer-Encoding and with
4970 * either multiple Content-Length header fields having differing
4971 * field-values or a single Content-Length header field having an
4972 * invalid value, then the message framing is invalid and the
4973 * recipient MUST treat it as an unrecoverable error. If this is a
4974 * request message, the server MUST respond with a 400 (Bad Request)
4975 * status code and then close the connection. If this is a response
4976 * message received by a proxy, the proxy MUST close the connection
4977 * to the server, discard the received response, and send a 502 (Bad
4978 * Gateway) response to the client. If this is a response message
4979 * received by a user agent, the user agent MUST close the
4980 * connection to the server and discard the received response.
4981 *
4982 * 5. If a valid Content-Length header field is present without
4983 * Transfer-Encoding, its decimal value defines the expected message
4984 * body length in octets. If the sender closes the connection or
4985 * the recipient times out before the indicated number of octets are
4986 * received, the recipient MUST consider the message to be
4987 * incomplete and close the connection.
4988 *
4989 * 6. If this is a request message and none of the above are true, then
4990 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004991 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004992 * 7. Otherwise, this is a response message without a declared message
4993 * body length, so the message body length is determined by the
4994 * number of octets received prior to the server closing the
4995 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004996 */
4997
4998 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004999 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005000 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005001 * FIXME: should we parse anyway and return an error on chunked encoding ?
5002 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005003 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5004 txn->status == 101)) {
5005 /* Either we've established an explicit tunnel, or we're
5006 * switching the protocol. In both cases, we're very unlikely
5007 * to understand the next protocols. We have to switch to tunnel
5008 * mode, so that we transfer the request and responses then let
5009 * this protocol pass unmodified. When we later implement specific
5010 * parsers for such protocols, we'll want to check the Upgrade
5011 * header which contains information about that protocol for
5012 * responses with status 101 (eg: see RFC2817 about TLS).
5013 */
5014 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5015 msg->flags |= HTTP_MSGF_XFER_LEN;
5016 goto end;
5017 }
5018
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005019 if (txn->meth == HTTP_METH_HEAD ||
5020 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005021 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005022 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005023 goto skip_content_length;
5024 }
5025
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005026 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005027 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005028 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005029 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005030 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5031 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005032 /* bad transfer-encoding (chunked followed by something else) */
5033 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005034 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005035 break;
5036 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005037 }
5038
Willy Tarreau1c913912015-04-30 10:57:51 +02005039 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005040 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005041 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005042 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005043 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5044 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005045 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005046 signed long long cl;
5047
Willy Tarreauad14f752011-09-02 20:33:27 +02005048 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005049 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005050 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005051 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005052
Willy Tarreauad14f752011-09-02 20:33:27 +02005053 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005054 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005055 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005056 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005057
Willy Tarreauad14f752011-09-02 20:33:27 +02005058 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005059 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005060 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005061 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005062
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005063 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005064 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005065 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005066 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005067
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005068 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005069 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005070 }
5071
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02005072 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
5073 * 407 (Proxy-Authenticate) responses and set the connection to private
5074 */
5075 if (srv_conn && txn->status == 401) {
5076 /* check for Negotiate/NTLM WWW-Authenticate headers */
5077 ctx.idx = 0;
5078 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
5079 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5080 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5081 srv_conn->flags |= CO_FL_PRIVATE;
5082 }
5083 } else if (srv_conn && txn->status == 407) {
5084 /* check for Negotiate/NTLM Proxy-Authenticate headers */
5085 ctx.idx = 0;
5086 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
5087 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5088 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5089 srv_conn->flags |= CO_FL_PRIVATE;
5090 }
5091 }
5092
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005093 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005094 /* Now we have to check if we need to modify the Connection header.
5095 * This is more difficult on the response than it is on the request,
5096 * because we can have two different HTTP versions and we don't know
5097 * how the client will interprete a response. For instance, let's say
5098 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5099 * HTTP/1.1 response without any header. Maybe it will bound itself to
5100 * HTTP/1.0 because it only knows about it, and will consider the lack
5101 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5102 * the lack of header as a keep-alive. Thus we will use two flags
5103 * indicating how a request MAY be understood by the client. In case
5104 * of multiple possibilities, we'll fix the header to be explicit. If
5105 * ambiguous cases such as both close and keepalive are seen, then we
5106 * will fall back to explicit close. Note that we won't take risks with
5107 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005108 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005109 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005110 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005111 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01005112 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005113
Willy Tarreau60466522010-01-18 19:08:45 +01005114 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005115 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01005116 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005117
Willy Tarreau60466522010-01-18 19:08:45 +01005118 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005119 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01005120 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005121 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005122 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005123 }
Willy Tarreau60466522010-01-18 19:08:45 +01005124 else { /* SCL / KAL */
5125 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005126 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005127 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005128 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005129
Willy Tarreau60466522010-01-18 19:08:45 +01005130 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005131 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005132
Willy Tarreau60466522010-01-18 19:08:45 +01005133 /* Some keep-alive responses are converted to Server-close if
5134 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005135 */
Willy Tarreau60466522010-01-18 19:08:45 +01005136 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5137 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005138 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005139 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005140 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005141 }
5142
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005143 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005144 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005145 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005146
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005147 /* end of job, return OK */
5148 rep->analysers &= ~an_bit;
5149 rep->analyse_exp = TICK_ETERNITY;
5150 channel_auto_close(rep);
5151 return 1;
5152
5153 abort_keep_alive:
5154 /* A keep-alive request to the server failed on a network error.
5155 * The client is required to retry. We need to close without returning
5156 * any other information so that the client retries.
5157 */
5158 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005159 rep->analysers &= AN_RES_FLT_END;
5160 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005161 channel_auto_close(rep);
5162 s->logs.logwait = 0;
5163 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005164 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005165 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005166 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005167 return 0;
5168}
5169
5170/* This function performs all the processing enabled for the current response.
5171 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005172 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005173 * other functions. It works like process_request (see indications above).
5174 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005175int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005176{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005177 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005178 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005179 struct http_msg *msg = &txn->rsp;
5180 struct proxy *cur_proxy;
5181 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005182 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005183
Christopher Faulete0768eb2018-10-03 16:38:02 +02005184 if (IS_HTX_STRM(s))
5185 return htx_process_res_common(s, rep, an_bit, px);
5186
Christopher Faulet45073512018-07-20 10:16:29 +02005187 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 +02005188 now_ms, __FUNCTION__,
5189 s,
5190 rep,
5191 rep->rex, rep->wex,
5192 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005193 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005194 rep->analysers);
5195
5196 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5197 return 0;
5198
Willy Tarreau70730dd2014-04-24 18:06:27 +02005199 /* The stats applet needs to adjust the Connection header but we don't
5200 * apply any filter there.
5201 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005202 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5203 rep->analysers &= ~an_bit;
5204 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005205 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005206 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005207
Willy Tarreau58975672014-04-24 21:13:57 +02005208 /*
5209 * We will have to evaluate the filters.
5210 * As opposed to version 1.2, now they will be evaluated in the
5211 * filters order and not in the header order. This means that
5212 * each filter has to be validated among all headers.
5213 *
5214 * Filters are tried with ->be first, then with ->fe if it is
5215 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005216 *
5217 * Maybe we are in resume condiion. In this case I choose the
5218 * "struct proxy" which contains the rule list matching the resume
5219 * pointer. If none of theses "struct proxy" match, I initialise
5220 * the process with the first one.
5221 *
5222 * In fact, I check only correspondance betwwen the current list
5223 * pointer and the ->fe rule list. If it doesn't match, I initialize
5224 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005225 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005226 if (s->current_rule_list == &sess->fe->http_res_rules)
5227 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005228 else
5229 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005230 while (1) {
5231 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005232
Willy Tarreau58975672014-04-24 21:13:57 +02005233 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005234 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005235 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005236
Willy Tarreau51d861a2015-05-22 17:30:48 +02005237 if (ret == HTTP_RULE_RES_BADREQ)
5238 goto return_srv_prx_502;
5239
5240 if (ret == HTTP_RULE_RES_DONE) {
5241 rep->analysers &= ~an_bit;
5242 rep->analyse_exp = TICK_ETERNITY;
5243 return 1;
5244 }
5245 }
5246
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005247 /* we need to be called again. */
5248 if (ret == HTTP_RULE_RES_YIELD) {
5249 channel_dont_close(rep);
5250 return 0;
5251 }
5252
Willy Tarreau58975672014-04-24 21:13:57 +02005253 /* try headers filters */
5254 if (rule_set->rsp_exp != NULL) {
5255 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5256 return_bad_resp:
5257 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005258 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005259 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005260 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005261 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005262 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005263 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005264 txn->status = 502;
5265 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005266 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005267 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005268 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005269 if (!(s->flags & SF_ERR_MASK))
5270 s->flags |= SF_ERR_PRXCOND;
5271 if (!(s->flags & SF_FINST_MASK))
5272 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005273 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005274 }
Willy Tarreau58975672014-04-24 21:13:57 +02005275 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005276
Willy Tarreau58975672014-04-24 21:13:57 +02005277 /* has the response been denied ? */
5278 if (txn->flags & TX_SVDENY) {
5279 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005280 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005281
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005282 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5283 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005284 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005285 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005286
Willy Tarreau58975672014-04-24 21:13:57 +02005287 goto return_srv_prx_502;
5288 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005289
Willy Tarreau58975672014-04-24 21:13:57 +02005290 /* add response headers from the rule sets in the same order */
5291 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005292 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005293 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005294 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005295 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005296 ret = acl_pass(ret);
5297 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5298 ret = !ret;
5299 if (!ret)
5300 continue;
5301 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005302 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005303 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005304 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005305
Willy Tarreau58975672014-04-24 21:13:57 +02005306 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005307 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005308 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005309 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005310 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005311
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005312 /* After this point, this anayzer can't return yield, so we can
5313 * remove the bit corresponding to this analyzer from the list.
5314 *
5315 * Note that the intermediate returns and goto found previously
5316 * reset the analyzers.
5317 */
5318 rep->analysers &= ~an_bit;
5319 rep->analyse_exp = TICK_ETERNITY;
5320
Willy Tarreau58975672014-04-24 21:13:57 +02005321 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005322 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005323 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005324
Willy Tarreau58975672014-04-24 21:13:57 +02005325 /*
5326 * Now check for a server cookie.
5327 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005328 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005329 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005330
Willy Tarreau58975672014-04-24 21:13:57 +02005331 /*
5332 * Check for cache-control or pragma headers if required.
5333 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005334 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005335 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005336
Willy Tarreau58975672014-04-24 21:13:57 +02005337 /*
5338 * Add server cookie in the response if needed
5339 */
5340 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5341 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005342 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005343 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5344 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5345 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5346 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5347 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005348 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005349 /* the server is known, it's not the one the client requested, or the
5350 * cookie's last seen date needs to be refreshed. We have to
5351 * insert a set-cookie here, except if we want to insert only on POST
5352 * requests and this one isn't. Note that servers which don't have cookies
5353 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005354 */
Willy Tarreau58975672014-04-24 21:13:57 +02005355 if (!objt_server(s->target)->cookie) {
5356 chunk_printf(&trash,
5357 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5358 s->be->cookie_name);
5359 }
5360 else {
5361 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005362
Willy Tarreau58975672014-04-24 21:13:57 +02005363 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5364 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005365 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5366 s30tob64((date.tv_sec+3) >> 2,
5367 trash.area + trash.data);
5368 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005369
Willy Tarreau58975672014-04-24 21:13:57 +02005370 if (s->be->cookie_maxlife) {
5371 /* emit first_date, which is either the original one or
5372 * the current date.
5373 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005374 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005375 s30tob64(txn->cookie_first_date ?
5376 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005377 (date.tv_sec+3) >> 2,
5378 trash.area + trash.data);
5379 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005380 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005381 }
Willy Tarreau58975672014-04-24 21:13:57 +02005382 chunk_appendf(&trash, "; path=/");
5383 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005384
Willy Tarreau58975672014-04-24 21:13:57 +02005385 if (s->be->cookie_domain)
5386 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005387
Willy Tarreau58975672014-04-24 21:13:57 +02005388 if (s->be->ck_opts & PR_CK_HTTPONLY)
5389 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005390
Willy Tarreau58975672014-04-24 21:13:57 +02005391 if (s->be->ck_opts & PR_CK_SECURE)
5392 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005393
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005394 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005395 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005396
Willy Tarreau58975672014-04-24 21:13:57 +02005397 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005398 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005399 /* the server did not change, only the date was updated */
5400 txn->flags |= TX_SCK_UPDATED;
5401 else
5402 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005403
Willy Tarreau58975672014-04-24 21:13:57 +02005404 /* Here, we will tell an eventual cache on the client side that we don't
5405 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5406 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5407 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005408 */
Willy Tarreau58975672014-04-24 21:13:57 +02005409 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005410
Willy Tarreau58975672014-04-24 21:13:57 +02005411 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005412
Willy Tarreau58975672014-04-24 21:13:57 +02005413 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5414 "Cache-control: private", 22) < 0))
5415 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005416 }
Willy Tarreau58975672014-04-24 21:13:57 +02005417 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005418
Willy Tarreau58975672014-04-24 21:13:57 +02005419 /*
5420 * Check if result will be cacheable with a cookie.
5421 * We'll block the response if security checks have caught
5422 * nasty things such as a cacheable cookie.
5423 */
5424 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5425 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5426 (s->be->options & PR_O_CHK_CACHE)) {
5427 /* we're in presence of a cacheable response containing
5428 * a set-cookie header. We'll block it as requested by
5429 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005430 */
Willy Tarreau58975672014-04-24 21:13:57 +02005431 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005432 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005433
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005434 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5435 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005436 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005437 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005438
Christopher Faulet767a84b2017-11-24 16:50:31 +01005439 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5440 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005441 send_log(s->be, LOG_ALERT,
5442 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5443 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5444 goto return_srv_prx_502;
5445 }
Willy Tarreau03945942009-12-22 16:50:27 +01005446
Willy Tarreau70730dd2014-04-24 18:06:27 +02005447 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005448 /*
5449 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5450 * If an "Upgrade" token is found, the header is left untouched in order
5451 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005452 * if anything but "Upgrade" is present in the Connection header. We don't
5453 * want to touch any 101 response either since it's switching to another
5454 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005455 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005456 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005457 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005458 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005459
Willy Tarreau58975672014-04-24 21:13:57 +02005460 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5461 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5462 /* we want a keep-alive response here. Keep-alive header
5463 * required if either side is not 1.1.
5464 */
5465 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5466 want_flags |= TX_CON_KAL_SET;
5467 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005468 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005469 /* we want a close response here. Close header required if
5470 * the server is 1.1, regardless of the client.
5471 */
5472 if (msg->flags & HTTP_MSGF_VER_11)
5473 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005474 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005475
Willy Tarreau58975672014-04-24 21:13:57 +02005476 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5477 http_change_connection_header(txn, msg, want_flags);
5478 }
5479
5480 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005481 /* Always enter in the body analyzer */
5482 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5483 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005484
Willy Tarreau58975672014-04-24 21:13:57 +02005485 /* if the user wants to log as soon as possible, without counting
5486 * bytes from the server, then this is the right moment. We have
5487 * to temporarily assign bytes_out to log what we currently have.
5488 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005489 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005490 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5491 s->logs.bytes_out = txn->rsp.eoh;
5492 s->do_log(s);
5493 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005494 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005495 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005496}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005497
Willy Tarreaud98cf932009-12-27 22:54:55 +01005498/* This function is an analyser which forwards response body (including chunk
5499 * sizes if any). It is called as soon as we must forward, even if we forward
5500 * zero byte. The only situation where it must not be called is when we're in
5501 * tunnel mode and we want to forward till the close. It's used both to forward
5502 * remaining data and to resync after end of body. It expects the msg_state to
5503 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005504 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005505 *
5506 * It is capable of compressing response data both in content-length mode and
5507 * in chunked mode. The state machines follows different flows depending on
5508 * whether content-length and chunked modes are used, since there are no
5509 * trailers in content-length :
5510 *
5511 * chk-mode cl-mode
5512 * ,----- BODY -----.
5513 * / \
5514 * V size > 0 V chk-mode
5515 * .--> SIZE -------------> DATA -------------> CRLF
5516 * | | size == 0 | last byte |
5517 * | v final crlf v inspected |
5518 * | TRAILERS -----------> DONE |
5519 * | |
5520 * `----------------------------------------------'
5521 *
5522 * Compression only happens in the DATA state, and must be flushed in final
5523 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5524 * is performed at once on final states for all bytes parsed, or when leaving
5525 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005526 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005527int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005528{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005529 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005530 struct http_txn *txn = s->txn;
5531 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005532 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005533
Christopher Faulete0768eb2018-10-03 16:38:02 +02005534 if (IS_HTX_STRM(s))
5535 return htx_response_forward_body(s, res, an_bit);
5536
Christopher Faulet45073512018-07-20 10:16:29 +02005537 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 +02005538 now_ms, __FUNCTION__,
5539 s,
5540 res,
5541 res->rex, res->wex,
5542 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005543 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005544 res->analysers);
5545
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005546 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5547 return 0;
5548
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005549 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005550 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005551 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005552 /* Output closed while we were sending data. We must abort and
5553 * wake the other side up.
5554 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005555 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005556 msg->msg_state = HTTP_MSG_ERROR;
5557 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005558 return 1;
5559 }
5560
Willy Tarreau4fe41902010-06-07 22:27:41 +02005561 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005562 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005563
Christopher Fauletd7c91962015-04-30 11:48:27 +02005564 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005565 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5566 ? HTTP_MSG_CHUNK_SIZE
5567 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005568 }
5569
Willy Tarreauefdf0942014-04-24 20:08:57 +02005570 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005571 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005572 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005573 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005574 }
5575
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005576 if (msg->msg_state < HTTP_MSG_DONE) {
5577 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5578 ? http_msg_forward_chunked_body(s, msg)
5579 : http_msg_forward_body(s, msg));
5580 if (!ret)
5581 goto missing_data_or_waiting;
5582 if (ret < 0)
5583 goto return_bad_res;
5584 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005585
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005586 /* other states, DONE...TUNNEL */
5587 /* for keep-alive we don't want to forward closes on DONE */
5588 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5589 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5590 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005591
Christopher Faulet894da4c2017-07-18 11:29:07 +02005592 http_resync_states(s);
5593 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005594 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5595 if (res->flags & CF_SHUTW) {
5596 /* response errors are most likely due to the
5597 * client aborting the transfer. */
5598 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005599 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005600 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005601 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005602 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005603 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005604 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005605 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005606 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005607
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005608 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005609 if (res->flags & CF_SHUTW)
5610 goto aborted_xfer;
5611
5612 /* stop waiting for data if the input is closed before the end. If the
5613 * client side was already closed, it means that the client has aborted,
5614 * so we don't want to count this as a server abort. Otherwise it's a
5615 * server abort.
5616 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005617 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005618 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005619 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005620 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005621 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005622 if (!(s->flags & SF_ERR_MASK))
5623 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005624 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005625 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005626 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005627 goto return_bad_res_stats_ok;
5628 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005629 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005630
Willy Tarreau40dba092010-03-04 18:14:51 +01005631 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005632 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005633 goto return_bad_res;
5634
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005635 /* When TE: chunked is used, we need to get there again to parse
5636 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005637 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5638 * or if there are filters registered on the stream, we don't want to
5639 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005640 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005641 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005642 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005643 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5644 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005645 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005646
Willy Tarreau5c620922011-05-11 19:56:11 +02005647 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005648 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005649 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005650 * modes are already handled by the stream sock layer. We must not do
5651 * this in content-length mode because it could present the MSG_MORE
5652 * flag with the last block of forwarded data, which would cause an
5653 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005654 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005655 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005656 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005657
Willy Tarreau87b09662015-04-03 00:22:06 +02005658 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005659 return 0;
5660
Willy Tarreau40dba092010-03-04 18:14:51 +01005661 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005662 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 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.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005665
5666 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005667 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005668 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005669 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005670 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005671 res->analysers &= AN_RES_FLT_END;
5672 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 +01005673 if (objt_server(s->target))
5674 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005675
Willy Tarreaue7dff022015-04-03 01:14:29 +02005676 if (!(s->flags & SF_ERR_MASK))
5677 s->flags |= SF_ERR_PRXCOND;
5678 if (!(s->flags & SF_FINST_MASK))
5679 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005680 return 0;
5681
5682 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005683 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005684 txn->rsp.msg_state = HTTP_MSG_ERROR;
5685 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005686 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005687 res->analysers &= AN_RES_FLT_END;
5688 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 +01005689
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005690 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5691 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005692 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005693 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005694
Willy Tarreaue7dff022015-04-03 01:14:29 +02005695 if (!(s->flags & SF_ERR_MASK))
5696 s->flags |= SF_ERR_CLICL;
5697 if (!(s->flags & SF_FINST_MASK))
5698 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005699 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005700}
5701
5702
Christopher Faulet10079f52018-10-03 15:17:28 +02005703int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005704{
5705 struct channel *chn = msg->chn;
5706 int ret;
5707
5708 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5709
5710 if (msg->msg_state == HTTP_MSG_ENDING)
5711 goto ending;
5712
5713 /* Neither content-length, nor transfer-encoding was found, so we must
5714 * read the body until the server connection is closed. In that case, we
5715 * eat data as they come. Of course, this happens for response only. */
5716 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005717 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005718 msg->chunk_len += len;
5719 msg->body_len += len;
5720 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005721 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005722 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005723 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005724 msg->next += ret;
5725 msg->chunk_len -= ret;
5726 if (msg->chunk_len) {
5727 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005728 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005729 chn->flags |= CF_WAKE_WRITE;
5730 goto missing_data_or_waiting;
5731 }
5732
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005733 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5734 * always set for a request. */
5735 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5736 /* The server still sending data that should be filtered */
5737 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5738 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005739 msg->msg_state = HTTP_MSG_TUNNEL;
5740 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005741 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005742
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005743 msg->msg_state = HTTP_MSG_ENDING;
5744
5745 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005746 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005747 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005748 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5749 /* default_ret */ msg->next,
5750 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005751 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005752 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005753 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5754 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005755 if (msg->next)
5756 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005757
Christopher Fauletda02e172015-12-04 09:25:05 +01005758 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5759 /* default_ret */ 1,
5760 /* on_error */ goto error,
5761 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005762 if (msg->msg_state == HTTP_MSG_ENDING)
5763 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005764 return 1;
5765
5766 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005767 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005768 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5769 /* default_ret */ msg->next,
5770 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005771 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005772 msg->next -= ret;
5773 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5774 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005775 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005776 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005777 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005778 return 0;
5779 error:
5780 return -1;
5781}
5782
Christopher Faulet10079f52018-10-03 15:17:28 +02005783int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005784{
5785 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005786 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005787 int ret;
5788
5789 /* Here we have the guarantee to be in one of the following state:
5790 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5791 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5792
Christopher Fauletca874b82018-09-20 11:31:01 +02005793 if (msg->msg_state == HTTP_MSG_ENDING)
5794 goto ending;
5795
5796 /* Don't parse chunks if there is no input data */
5797 if (!ci_data(chn))
5798 goto waiting;
5799
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005800 switch_states:
5801 switch (msg->msg_state) {
5802 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005803 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005804 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005805 /* on_error */ goto error);
5806 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005807 msg->chunk_len -= ret;
5808 if (msg->chunk_len) {
5809 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005810 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005811 chn->flags |= CF_WAKE_WRITE;
5812 goto missing_data_or_waiting;
5813 }
5814
5815 /* nothing left to forward for this chunk*/
5816 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5817 /* fall through for HTTP_MSG_CHUNK_CRLF */
5818
5819 case HTTP_MSG_CHUNK_CRLF:
5820 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005821 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005822 if (ret == 0)
5823 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005824 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005825 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005826 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005827 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005828 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005829 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005830 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005831 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5832 /* fall through for HTTP_MSG_CHUNK_SIZE */
5833
5834 case HTTP_MSG_CHUNK_SIZE:
5835 /* read the chunk size and assign it to ->chunk_len,
5836 * then set ->next to point to the body and switch to
5837 * DATA or TRAILERS state.
5838 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005839 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005840 if (ret == 0)
5841 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005842 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005843 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005844 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005845 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005846 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005847 }
5848
5849 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005850 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005851 msg->chunk_len = chunk;
5852 msg->body_len += chunk;
5853
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005854 if (msg->chunk_len) {
5855 msg->msg_state = HTTP_MSG_DATA;
5856 goto switch_states;
5857 }
5858 msg->msg_state = HTTP_MSG_TRAILERS;
5859 /* fall through for HTTP_MSG_TRAILERS */
5860
5861 case HTTP_MSG_TRAILERS:
5862 ret = http_forward_trailers(msg);
5863 if (ret < 0)
5864 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005865 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5866 /* default_ret */ 1,
5867 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005868 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005869 if (!ret)
5870 goto missing_data_or_waiting;
5871 break;
5872
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005873 default:
5874 /* This should no happen in this function */
5875 goto error;
5876 }
5877
5878 msg->msg_state = HTTP_MSG_ENDING;
5879 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005880 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005881 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005882 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005883 /* default_ret */ msg->next,
5884 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005885 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005886 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005887 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5888 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005889 if (msg->next)
5890 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005891
Christopher Fauletda02e172015-12-04 09:25:05 +01005892 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005893 /* default_ret */ 1,
5894 /* on_error */ goto error,
5895 /* on_wait */ goto waiting);
5896 msg->msg_state = HTTP_MSG_DONE;
5897 return 1;
5898
5899 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005900 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005901 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005902 /* default_ret */ msg->next,
5903 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005904 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005905 msg->next -= ret;
5906 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5907 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005908 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005909 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005910 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005911 return 0;
5912
5913 chunk_parsing_error:
5914 if (msg->err_pos >= 0) {
5915 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005916 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005917 msg->msg_state, strm_fe(s));
5918 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005919 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005920 msg, msg->msg_state, s->be);
5921 }
5922 error:
5923 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005924}
5925
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005926
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005927/* Iterate the same filter through all request headers.
5928 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005929 * Since it can manage the switch to another backend, it updates the per-proxy
5930 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005931 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005932int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005933{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005934 char *cur_ptr, *cur_end, *cur_next;
5935 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005936 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005937 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005938 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005939
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005940 last_hdr = 0;
5941
Willy Tarreauf37954d2018-06-15 18:31:02 +02005942 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005943 old_idx = 0;
5944
5945 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005946 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005947 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005948 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005949 (exp->action == ACT_ALLOW ||
5950 exp->action == ACT_DENY ||
5951 exp->action == ACT_TARPIT))
5952 return 0;
5953
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005954 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005955 if (!cur_idx)
5956 break;
5957
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005958 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005959 cur_ptr = cur_next;
5960 cur_end = cur_ptr + cur_hdr->len;
5961 cur_next = cur_end + cur_hdr->cr + 1;
5962
5963 /* Now we have one header between cur_ptr and cur_end,
5964 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005965 */
5966
Willy Tarreau15a53a42015-01-21 13:39:42 +01005967 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005968 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005969 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005970 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005971 last_hdr = 1;
5972 break;
5973
5974 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005975 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005976 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005977 break;
5978
5979 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005980 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005981 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005982 break;
5983
5984 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005985 len = exp_replace(trash.area,
5986 trash.size, cur_ptr,
5987 exp->replace, pmatch);
5988 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005989 return -1;
5990
Willy Tarreau6e27be12018-08-22 04:46:47 +02005991 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5992
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005993 /* FIXME: if the user adds a newline in the replacement, the
5994 * index will not be recalculated for now, and the new line
5995 * will not be counted as a new header.
5996 */
5997
5998 cur_end += delta;
5999 cur_next += delta;
6000 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006001 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006002 break;
6003
6004 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006005 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006006 cur_next += delta;
6007
Willy Tarreaufa355d42009-11-29 18:12:29 +01006008 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006009 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6010 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006011 cur_hdr->len = 0;
6012 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006013 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006014 break;
6015
6016 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006017 }
6018
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006019 /* keep the link from this header to next one in case of later
6020 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006021 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006022 old_idx = cur_idx;
6023 }
6024 return 0;
6025}
6026
6027
6028/* Apply the filter to the request line.
6029 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6030 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006031 * Since it can manage the switch to another backend, it updates the per-proxy
6032 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006033 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006034int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006035{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006036 char *cur_ptr, *cur_end;
6037 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006038 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006039 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006040
Willy Tarreau3d300592007-03-18 18:34:41 +01006041 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006042 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006043 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006044 (exp->action == ACT_ALLOW ||
6045 exp->action == ACT_DENY ||
6046 exp->action == ACT_TARPIT))
6047 return 0;
6048 else if (exp->action == ACT_REMOVE)
6049 return 0;
6050
6051 done = 0;
6052
Willy Tarreauf37954d2018-06-15 18:31:02 +02006053 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006054 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006055
6056 /* Now we have the request line between cur_ptr and cur_end */
6057
Willy Tarreau15a53a42015-01-21 13:39:42 +01006058 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006059 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006060 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006061 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006062 done = 1;
6063 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006064
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006065 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006066 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006067 done = 1;
6068 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006069
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006070 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006071 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006072 done = 1;
6073 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006074
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006075 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006076 len = exp_replace(trash.area, trash.size,
6077 cur_ptr, exp->replace, pmatch);
6078 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006079 return -1;
6080
Willy Tarreau6e27be12018-08-22 04:46:47 +02006081 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006083 /* FIXME: if the user adds a newline in the replacement, the
6084 * index will not be recalculated for now, and the new line
6085 * will not be counted as a new header.
6086 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006087
Willy Tarreaufa355d42009-11-29 18:12:29 +01006088 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006089 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006090 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006091 HTTP_MSG_RQMETH,
6092 cur_ptr, cur_end + 1,
6093 NULL, NULL);
6094 if (unlikely(!cur_end))
6095 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006096
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006097 /* we have a full request and we know that we have either a CR
6098 * or an LF at <ptr>.
6099 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006100 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6101 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006102 /* there is no point trying this regex on headers */
6103 return 1;
6104 }
6105 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006106 return done;
6107}
Willy Tarreau97de6242006-12-27 17:18:38 +01006108
Willy Tarreau58f10d72006-12-04 02:26:12 +01006109
Willy Tarreau58f10d72006-12-04 02:26:12 +01006110
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006111/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006112 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006113 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006114 * unparsable request. Since it can manage the switch to another backend, it
6115 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006116 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006117int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006118{
Willy Tarreau192252e2015-04-04 01:47:55 +02006119 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006120 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006121 struct hdr_exp *exp;
6122
6123 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006124 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006125
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006126 /*
6127 * The interleaving of transformations and verdicts
6128 * makes it difficult to decide to continue or stop
6129 * the evaluation.
6130 */
6131
Willy Tarreau6c123b12010-01-28 20:22:06 +01006132 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6133 break;
6134
Willy Tarreau3d300592007-03-18 18:34:41 +01006135 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006136 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006137 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006138 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006139
6140 /* if this filter had a condition, evaluate it now and skip to
6141 * next filter if the condition does not match.
6142 */
6143 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006144 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006145 ret = acl_pass(ret);
6146 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6147 ret = !ret;
6148
6149 if (!ret)
6150 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006151 }
6152
6153 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006154 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006155 if (unlikely(ret < 0))
6156 return -1;
6157
6158 if (likely(ret == 0)) {
6159 /* The filter did not match the request, it can be
6160 * iterated through all headers.
6161 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006162 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6163 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006164 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006165 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006166 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006167}
6168
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006169
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006170/* Delete a value in a header between delimiters <from> and <next> in buffer
6171 * <buf>. The number of characters displaced is returned, and the pointer to
6172 * the first delimiter is updated if required. The function tries as much as
6173 * possible to respect the following principles :
6174 * - replace <from> delimiter by the <next> one unless <from> points to a
6175 * colon, in which case <next> is simply removed
6176 * - set exactly one space character after the new first delimiter, unless
6177 * there are not enough characters in the block being moved to do so.
6178 * - remove unneeded spaces before the previous delimiter and after the new
6179 * one.
6180 *
6181 * It is the caller's responsibility to ensure that :
6182 * - <from> points to a valid delimiter or the colon ;
6183 * - <next> points to a valid delimiter or the final CR/LF ;
6184 * - there are non-space chars before <from> ;
6185 * - there is a CR/LF at or after <next>.
6186 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006187int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006188{
6189 char *prev = *from;
6190
6191 if (*prev == ':') {
6192 /* We're removing the first value, preserve the colon and add a
6193 * space if possible.
6194 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006195 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006196 next++;
6197 prev++;
6198 if (prev < next)
6199 *prev++ = ' ';
6200
Willy Tarreau2235b262016-11-05 15:50:20 +01006201 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006202 next++;
6203 } else {
6204 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006205 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006206 prev--;
6207 *from = prev;
6208
6209 /* copy the delimiter and if possible a space if we're
6210 * not at the end of the line.
6211 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006212 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006213 *prev++ = *next++;
6214 if (prev + 1 < next)
6215 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006216 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006217 next++;
6218 }
6219 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006220 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006221}
6222
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006223/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006224 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006225 * desirable to call it only when needed. This code is quite complex because
6226 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6227 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006228 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006229void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006230{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006231 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006232 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006233 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006234 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006235 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6236 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006237
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006238 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006239 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006240 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006241
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006242 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006243 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006244 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006245
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006246 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006247 hdr_beg = hdr_next;
6248 hdr_end = hdr_beg + cur_hdr->len;
6249 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006250
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006251 /* We have one full header between hdr_beg and hdr_end, and the
6252 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006253 * "Cookie:" headers.
6254 */
6255
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006257 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006258 old_idx = cur_idx;
6259 continue;
6260 }
6261
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006262 del_from = NULL; /* nothing to be deleted */
6263 preserve_hdr = 0; /* assume we may kill the whole header */
6264
Willy Tarreau58f10d72006-12-04 02:26:12 +01006265 /* Now look for cookies. Conforming to RFC2109, we have to support
6266 * attributes whose name begin with a '$', and associate them with
6267 * the right cookie, if we want to delete this cookie.
6268 * So there are 3 cases for each cookie read :
6269 * 1) it's a special attribute, beginning with a '$' : ignore it.
6270 * 2) it's a server id cookie that we *MAY* want to delete : save
6271 * some pointers on it (last semi-colon, beginning of cookie...)
6272 * 3) it's an application cookie : we *MAY* have to delete a previous
6273 * "special" cookie.
6274 * At the end of loop, if a "special" cookie remains, we may have to
6275 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006276 * *MUST* delete it.
6277 *
6278 * Note: RFC2965 is unclear about the processing of spaces around
6279 * the equal sign in the ATTR=VALUE form. A careful inspection of
6280 * the RFC explicitly allows spaces before it, and not within the
6281 * tokens (attrs or values). An inspection of RFC2109 allows that
6282 * too but section 10.1.3 lets one think that spaces may be allowed
6283 * after the equal sign too, resulting in some (rare) buggy
6284 * implementations trying to do that. So let's do what servers do.
6285 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6286 * allowed quoted strings in values, with any possible character
6287 * after a backslash, including control chars and delimitors, which
6288 * causes parsing to become ambiguous. Browsers also allow spaces
6289 * within values even without quotes.
6290 *
6291 * We have to keep multiple pointers in order to support cookie
6292 * removal at the beginning, middle or end of header without
6293 * corrupting the header. All of these headers are valid :
6294 *
6295 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6296 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6297 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6298 * | | | | | | | | |
6299 * | | | | | | | | hdr_end <--+
6300 * | | | | | | | +--> next
6301 * | | | | | | +----> val_end
6302 * | | | | | +-----------> val_beg
6303 * | | | | +--------------> equal
6304 * | | | +----------------> att_end
6305 * | | +---------------------> att_beg
6306 * | +--------------------------> prev
6307 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006308 */
6309
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006310 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6311 /* Iterate through all cookies on this line */
6312
6313 /* find att_beg */
6314 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006315 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006316 att_beg++;
6317
6318 /* find att_end : this is the first character after the last non
6319 * space before the equal. It may be equal to hdr_end.
6320 */
6321 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006322
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006323 while (equal < hdr_end) {
6324 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006325 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006326 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006327 continue;
6328 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006329 }
6330
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006331 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6332 * is between <att_beg> and <equal>, both may be identical.
6333 */
6334
6335 /* look for end of cookie if there is an equal sign */
6336 if (equal < hdr_end && *equal == '=') {
6337 /* look for the beginning of the value */
6338 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006339 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006340 val_beg++;
6341
6342 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006343 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006344
6345 /* make val_end point to the first white space or delimitor after the value */
6346 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006347 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006348 val_end--;
6349 } else {
6350 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006351 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006352
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006353 /* We have nothing to do with attributes beginning with '$'. However,
6354 * they will automatically be removed if a header before them is removed,
6355 * since they're supposed to be linked together.
6356 */
6357 if (*att_beg == '$')
6358 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006359
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006360 /* Ignore cookies with no equal sign */
6361 if (equal == next) {
6362 /* This is not our cookie, so we must preserve it. But if we already
6363 * scheduled another cookie for removal, we cannot remove the
6364 * complete header, but we can remove the previous block itself.
6365 */
6366 preserve_hdr = 1;
6367 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006368 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006369 val_end += delta;
6370 next += delta;
6371 hdr_end += delta;
6372 hdr_next += delta;
6373 cur_hdr->len += delta;
6374 http_msg_move_end(&txn->req, delta);
6375 prev = del_from;
6376 del_from = NULL;
6377 }
6378 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006379 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006380
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006381 /* if there are spaces around the equal sign, we need to
6382 * strip them otherwise we'll get trouble for cookie captures,
6383 * or even for rewrites. Since this happens extremely rarely,
6384 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006385 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006386 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6387 int stripped_before = 0;
6388 int stripped_after = 0;
6389
6390 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006391 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006392 equal += stripped_before;
6393 val_beg += stripped_before;
6394 }
6395
6396 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006397 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006398 val_beg += stripped_after;
6399 stripped_before += stripped_after;
6400 }
6401
6402 val_end += stripped_before;
6403 next += stripped_before;
6404 hdr_end += stripped_before;
6405 hdr_next += stripped_before;
6406 cur_hdr->len += stripped_before;
6407 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006408 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006409 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006410
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006411 /* First, let's see if we want to capture this cookie. We check
6412 * that we don't already have a client side cookie, because we
6413 * can only capture one. Also as an optimisation, we ignore
6414 * cookies shorter than the declared name.
6415 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006416 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6417 (val_end - att_beg >= sess->fe->capture_namelen) &&
6418 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006419 int log_len = val_end - att_beg;
6420
Willy Tarreaubafbe012017-11-24 17:34:44 +01006421 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006422 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006423 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006424 if (log_len > sess->fe->capture_len)
6425 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006426 memcpy(txn->cli_cookie, att_beg, log_len);
6427 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006428 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006429 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006430
Willy Tarreaubca99692010-10-06 19:25:55 +02006431 /* Persistence cookies in passive, rewrite or insert mode have the
6432 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006433 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006434 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006435 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006436 * For cookies in prefix mode, the form is :
6437 *
6438 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006439 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006440 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6441 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6442 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006443 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006444
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006445 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6446 * have the server ID between val_beg and delim, and the original cookie between
6447 * delim+1 and val_end. Otherwise, delim==val_end :
6448 *
6449 * Cookie: NAME=SRV; # in all but prefix modes
6450 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6451 * | || || | |+-> next
6452 * | || || | +--> val_end
6453 * | || || +---------> delim
6454 * | || |+------------> val_beg
6455 * | || +-------------> att_end = equal
6456 * | |+-----------------> att_beg
6457 * | +------------------> prev
6458 * +-------------------------> hdr_beg
6459 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006460
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006461 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006462 for (delim = val_beg; delim < val_end; delim++)
6463 if (*delim == COOKIE_DELIM)
6464 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006465 } else {
6466 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006467 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006468 /* Now check if the cookie contains a date field, which would
6469 * appear after a vertical bar ('|') just after the server name
6470 * and before the delimiter.
6471 */
6472 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6473 if (vbar1) {
6474 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006475 * right is the last seen date. It is a base64 encoded
6476 * 30-bit value representing the UNIX date since the
6477 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006478 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006479 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006480 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006481 if (val_end - vbar1 >= 5) {
6482 val = b64tos30(vbar1);
6483 if (val > 0)
6484 txn->cookie_last_date = val << 2;
6485 }
6486 /* look for a second vertical bar */
6487 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6488 if (vbar1 && (val_end - vbar1 > 5)) {
6489 val = b64tos30(vbar1 + 1);
6490 if (val > 0)
6491 txn->cookie_first_date = val << 2;
6492 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006493 }
6494 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006495
Willy Tarreauf64d1412010-10-07 20:06:11 +02006496 /* if the cookie has an expiration date and the proxy wants to check
6497 * it, then we do that now. We first check if the cookie is too old,
6498 * then only if it has expired. We detect strict overflow because the
6499 * time resolution here is not great (4 seconds). Cookies with dates
6500 * in the future are ignored if their offset is beyond one day. This
6501 * allows an admin to fix timezone issues without expiring everyone
6502 * and at the same time avoids keeping unwanted side effects for too
6503 * long.
6504 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006505 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6506 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006507 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006508 txn->flags &= ~TX_CK_MASK;
6509 txn->flags |= TX_CK_OLD;
6510 delim = val_beg; // let's pretend we have not found the cookie
6511 txn->cookie_first_date = 0;
6512 txn->cookie_last_date = 0;
6513 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006514 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6515 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006516 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006517 txn->flags &= ~TX_CK_MASK;
6518 txn->flags |= TX_CK_EXPIRED;
6519 delim = val_beg; // let's pretend we have not found the cookie
6520 txn->cookie_first_date = 0;
6521 txn->cookie_last_date = 0;
6522 }
6523
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006524 /* Here, we'll look for the first running server which supports the cookie.
6525 * This allows to share a same cookie between several servers, for example
6526 * to dedicate backup servers to specific servers only.
6527 * However, to prevent clients from sticking to cookie-less backup server
6528 * when they have incidentely learned an empty cookie, we simply ignore
6529 * empty cookies and mark them as invalid.
6530 * The same behaviour is applied when persistence must be ignored.
6531 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006532 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006533 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006534
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006535 while (srv) {
6536 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6537 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006538 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006539 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006540 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006541 /* we found the server and we can use it */
6542 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006543 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006544 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006545 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006546 break;
6547 } else {
6548 /* we found a server, but it's down,
6549 * mark it as such and go on in case
6550 * another one is available.
6551 */
6552 txn->flags &= ~TX_CK_MASK;
6553 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006554 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006555 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006556 srv = srv->next;
6557 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006558
Willy Tarreauf64d1412010-10-07 20:06:11 +02006559 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006560 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006561 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006562 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006563 txn->flags |= TX_CK_UNUSED;
6564 else
6565 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006566 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006567
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006568 /* depending on the cookie mode, we may have to either :
6569 * - delete the complete cookie if we're in insert+indirect mode, so that
6570 * the server never sees it ;
6571 * - remove the server id from the cookie value, and tag the cookie as an
6572 * application cookie so that it does not get accidentely removed later,
6573 * if we're in cookie prefix mode
6574 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006575 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006576 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006577
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006578 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006579 val_end += delta;
6580 next += delta;
6581 hdr_end += delta;
6582 hdr_next += delta;
6583 cur_hdr->len += delta;
6584 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006585
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006586 del_from = NULL;
6587 preserve_hdr = 1; /* we want to keep this cookie */
6588 }
6589 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006590 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006591 del_from = prev;
6592 }
6593 } else {
6594 /* This is not our cookie, so we must preserve it. But if we already
6595 * scheduled another cookie for removal, we cannot remove the
6596 * complete header, but we can remove the previous block itself.
6597 */
6598 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006599
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006600 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006601 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006602 if (att_beg >= del_from)
6603 att_beg += delta;
6604 if (att_end >= del_from)
6605 att_end += delta;
6606 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006607 val_end += delta;
6608 next += delta;
6609 hdr_end += delta;
6610 hdr_next += delta;
6611 cur_hdr->len += delta;
6612 http_msg_move_end(&txn->req, delta);
6613 prev = del_from;
6614 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006615 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006616 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006617
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006618 /* continue with next cookie on this header line */
6619 att_beg = next;
6620 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006621
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006622 /* There are no more cookies on this line.
6623 * We may still have one (or several) marked for deletion at the
6624 * end of the line. We must do this now in two ways :
6625 * - if some cookies must be preserved, we only delete from the
6626 * mark to the end of line ;
6627 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006628 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006629 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006630 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006631 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006632 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006633 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006634 cur_hdr->len += delta;
6635 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006636 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006637
6638 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006639 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6640 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006641 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006642 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006643 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006644 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006645 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006646 }
6647
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006648 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006649 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006650 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006651}
6652
6653
Willy Tarreaua15645d2007-03-18 16:22:39 +01006654/* Iterate the same filter through all response headers contained in <rtr>.
6655 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6656 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006657int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006658{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006659 char *cur_ptr, *cur_end, *cur_next;
6660 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006661 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006662 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006663 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006664
6665 last_hdr = 0;
6666
Willy Tarreauf37954d2018-06-15 18:31:02 +02006667 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006668 old_idx = 0;
6669
6670 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006671 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006672 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006673 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006674 (exp->action == ACT_ALLOW ||
6675 exp->action == ACT_DENY))
6676 return 0;
6677
6678 cur_idx = txn->hdr_idx.v[old_idx].next;
6679 if (!cur_idx)
6680 break;
6681
6682 cur_hdr = &txn->hdr_idx.v[cur_idx];
6683 cur_ptr = cur_next;
6684 cur_end = cur_ptr + cur_hdr->len;
6685 cur_next = cur_end + cur_hdr->cr + 1;
6686
6687 /* Now we have one header between cur_ptr and cur_end,
6688 * and the next header starts at cur_next.
6689 */
6690
Willy Tarreau15a53a42015-01-21 13:39:42 +01006691 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006692 switch (exp->action) {
6693 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006694 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006695 last_hdr = 1;
6696 break;
6697
6698 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006699 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006700 last_hdr = 1;
6701 break;
6702
6703 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006704 len = exp_replace(trash.area,
6705 trash.size, cur_ptr,
6706 exp->replace, pmatch);
6707 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006708 return -1;
6709
Willy Tarreau6e27be12018-08-22 04:46:47 +02006710 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6711
Willy Tarreaua15645d2007-03-18 16:22:39 +01006712 /* FIXME: if the user adds a newline in the replacement, the
6713 * index will not be recalculated for now, and the new line
6714 * will not be counted as a new header.
6715 */
6716
6717 cur_end += delta;
6718 cur_next += delta;
6719 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006720 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006721 break;
6722
6723 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006724 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006725 cur_next += delta;
6726
Willy Tarreaufa355d42009-11-29 18:12:29 +01006727 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006728 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6729 txn->hdr_idx.used--;
6730 cur_hdr->len = 0;
6731 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006732 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006733 break;
6734
6735 }
6736 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737
6738 /* keep the link from this header to next one in case of later
6739 * removal of next header.
6740 */
6741 old_idx = cur_idx;
6742 }
6743 return 0;
6744}
6745
6746
6747/* Apply the filter to the status line in the response buffer <rtr>.
6748 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6749 * or -1 if a replacement resulted in an invalid status line.
6750 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006751int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006752{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006753 char *cur_ptr, *cur_end;
6754 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006755 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006756 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006757
Willy Tarreau3d300592007-03-18 18:34:41 +01006758 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006759 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006760 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006761 (exp->action == ACT_ALLOW ||
6762 exp->action == ACT_DENY))
6763 return 0;
6764 else if (exp->action == ACT_REMOVE)
6765 return 0;
6766
6767 done = 0;
6768
Willy Tarreauf37954d2018-06-15 18:31:02 +02006769 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006770 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006771
6772 /* Now we have the status line between cur_ptr and cur_end */
6773
Willy Tarreau15a53a42015-01-21 13:39:42 +01006774 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006775 switch (exp->action) {
6776 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006777 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006778 done = 1;
6779 break;
6780
6781 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006782 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006783 done = 1;
6784 break;
6785
6786 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006787 len = exp_replace(trash.area, trash.size,
6788 cur_ptr, exp->replace, pmatch);
6789 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006790 return -1;
6791
Willy Tarreau6e27be12018-08-22 04:46:47 +02006792 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6793
Willy Tarreaua15645d2007-03-18 16:22:39 +01006794 /* FIXME: if the user adds a newline in the replacement, the
6795 * index will not be recalculated for now, and the new line
6796 * will not be counted as a new header.
6797 */
6798
Willy Tarreaufa355d42009-11-29 18:12:29 +01006799 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006800 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006801 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006802 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803 cur_ptr, cur_end + 1,
6804 NULL, NULL);
6805 if (unlikely(!cur_end))
6806 return -1;
6807
6808 /* we have a full respnse and we know that we have either a CR
6809 * or an LF at <ptr>.
6810 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006811 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006812 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006813 /* there is no point trying this regex on headers */
6814 return 1;
6815 }
6816 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006817 return done;
6818}
6819
6820
6821
6822/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006823 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006824 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6825 * unparsable response.
6826 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006827int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006828{
Willy Tarreau192252e2015-04-04 01:47:55 +02006829 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006830 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006831 struct hdr_exp *exp;
6832
6833 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006834 int ret;
6835
6836 /*
6837 * The interleaving of transformations and verdicts
6838 * makes it difficult to decide to continue or stop
6839 * the evaluation.
6840 */
6841
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006842 if (txn->flags & TX_SVDENY)
6843 break;
6844
Willy Tarreau3d300592007-03-18 18:34:41 +01006845 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006846 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6847 exp->action == ACT_PASS)) {
6848 exp = exp->next;
6849 continue;
6850 }
6851
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006852 /* if this filter had a condition, evaluate it now and skip to
6853 * next filter if the condition does not match.
6854 */
6855 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006856 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006857 ret = acl_pass(ret);
6858 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6859 ret = !ret;
6860 if (!ret)
6861 continue;
6862 }
6863
Willy Tarreaua15645d2007-03-18 16:22:39 +01006864 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006865 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006866 if (unlikely(ret < 0))
6867 return -1;
6868
6869 if (likely(ret == 0)) {
6870 /* The filter did not match the response, it can be
6871 * iterated through all headers.
6872 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006873 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6874 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006875 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006876 }
6877 return 0;
6878}
6879
6880
Willy Tarreaua15645d2007-03-18 16:22:39 +01006881/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006882 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006883 * desirable to call it only when needed. This function is also used when we
6884 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006885 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006886void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006887{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006888 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006889 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006890 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006891 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006892 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006893 char *hdr_beg, *hdr_end, *hdr_next;
6894 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006895
Willy Tarreaua15645d2007-03-18 16:22:39 +01006896 /* Iterate through the headers.
6897 * we start with the start line.
6898 */
6899 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006900 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006901
6902 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6903 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006904 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006905
6906 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006907 hdr_beg = hdr_next;
6908 hdr_end = hdr_beg + cur_hdr->len;
6909 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006910
Willy Tarreau24581ba2010-08-31 22:39:35 +02006911 /* We have one full header between hdr_beg and hdr_end, and the
6912 * next header starts at hdr_next. We're only interested in
6913 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006914 */
6915
Willy Tarreau24581ba2010-08-31 22:39:35 +02006916 is_cookie2 = 0;
6917 prev = hdr_beg + 10;
6918 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006919 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006920 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6921 if (!val) {
6922 old_idx = cur_idx;
6923 continue;
6924 }
6925 is_cookie2 = 1;
6926 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006927 }
6928
Willy Tarreau24581ba2010-08-31 22:39:35 +02006929 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6930 * <prev> points to the colon.
6931 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006932 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006933
Willy Tarreau24581ba2010-08-31 22:39:35 +02006934 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6935 * check-cache is enabled) and we are not interested in checking
6936 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006937 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006938 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006939 return;
6940
Willy Tarreau24581ba2010-08-31 22:39:35 +02006941 /* OK so now we know we have to process this response cookie.
6942 * The format of the Set-Cookie header is slightly different
6943 * from the format of the Cookie header in that it does not
6944 * support the comma as a cookie delimiter (thus the header
6945 * cannot be folded) because the Expires attribute described in
6946 * the original Netscape's spec may contain an unquoted date
6947 * with a comma inside. We have to live with this because
6948 * many browsers don't support Max-Age and some browsers don't
6949 * support quoted strings. However the Set-Cookie2 header is
6950 * clean.
6951 *
6952 * We have to keep multiple pointers in order to support cookie
6953 * removal at the beginning, middle or end of header without
6954 * corrupting the header (in case of set-cookie2). A special
6955 * pointer, <scav> points to the beginning of the set-cookie-av
6956 * fields after the first semi-colon. The <next> pointer points
6957 * either to the end of line (set-cookie) or next unquoted comma
6958 * (set-cookie2). All of these headers are valid :
6959 *
6960 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6961 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6962 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6963 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6964 * | | | | | | | | | |
6965 * | | | | | | | | +-> next hdr_end <--+
6966 * | | | | | | | +------------> scav
6967 * | | | | | | +--------------> val_end
6968 * | | | | | +--------------------> val_beg
6969 * | | | | +----------------------> equal
6970 * | | | +------------------------> att_end
6971 * | | +----------------------------> att_beg
6972 * | +------------------------------> prev
6973 * +-----------------------------------------> hdr_beg
6974 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006975
Willy Tarreau24581ba2010-08-31 22:39:35 +02006976 for (; prev < hdr_end; prev = next) {
6977 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006978
Willy Tarreau24581ba2010-08-31 22:39:35 +02006979 /* find att_beg */
6980 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006981 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006982 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006983
Willy Tarreau24581ba2010-08-31 22:39:35 +02006984 /* find att_end : this is the first character after the last non
6985 * space before the equal. It may be equal to hdr_end.
6986 */
6987 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006988
Willy Tarreau24581ba2010-08-31 22:39:35 +02006989 while (equal < hdr_end) {
6990 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6991 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006992 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006993 continue;
6994 att_end = equal;
6995 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006996
Willy Tarreau24581ba2010-08-31 22:39:35 +02006997 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6998 * is between <att_beg> and <equal>, both may be identical.
6999 */
7000
7001 /* look for end of cookie if there is an equal sign */
7002 if (equal < hdr_end && *equal == '=') {
7003 /* look for the beginning of the value */
7004 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007005 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007006 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007007
Willy Tarreau24581ba2010-08-31 22:39:35 +02007008 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02007009 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007010
7011 /* make val_end point to the first white space or delimitor after the value */
7012 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007013 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007014 val_end--;
7015 } else {
7016 /* <equal> points to next comma, semi-colon or EOL */
7017 val_beg = val_end = next = equal;
7018 }
7019
7020 if (next < hdr_end) {
7021 /* Set-Cookie2 supports multiple cookies, and <next> points to
7022 * a colon or semi-colon before the end. So skip all attr-value
7023 * pairs and look for the next comma. For Set-Cookie, since
7024 * commas are permitted in values, skip to the end.
7025 */
7026 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02007027 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007028 else
7029 next = hdr_end;
7030 }
7031
7032 /* Now everything is as on the diagram above */
7033
7034 /* Ignore cookies with no equal sign */
7035 if (equal == val_end)
7036 continue;
7037
7038 /* If there are spaces around the equal sign, we need to
7039 * strip them otherwise we'll get trouble for cookie captures,
7040 * or even for rewrites. Since this happens extremely rarely,
7041 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007042 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007043 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7044 int stripped_before = 0;
7045 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007046
Willy Tarreau24581ba2010-08-31 22:39:35 +02007047 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007048 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007049 equal += stripped_before;
7050 val_beg += stripped_before;
7051 }
7052
7053 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007054 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007055 val_beg += stripped_after;
7056 stripped_before += stripped_after;
7057 }
7058
7059 val_end += stripped_before;
7060 next += stripped_before;
7061 hdr_end += stripped_before;
7062 hdr_next += stripped_before;
7063 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007064 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007065 }
7066
7067 /* First, let's see if we want to capture this cookie. We check
7068 * that we don't already have a server side cookie, because we
7069 * can only capture one. Also as an optimisation, we ignore
7070 * cookies shorter than the declared name.
7071 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007072 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007073 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007074 (val_end - att_beg >= sess->fe->capture_namelen) &&
7075 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007076 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007077 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007078 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007079 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007080 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007081 if (log_len > sess->fe->capture_len)
7082 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007083 memcpy(txn->srv_cookie, att_beg, log_len);
7084 txn->srv_cookie[log_len] = 0;
7085 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007086 }
7087
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007088 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007089 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007090 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007091 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7092 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007093 /* assume passive cookie by default */
7094 txn->flags &= ~TX_SCK_MASK;
7095 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007096
7097 /* If the cookie is in insert mode on a known server, we'll delete
7098 * this occurrence because we'll insert another one later.
7099 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007100 * a direct access.
7101 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007102 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007103 /* The "preserve" flag was set, we don't want to touch the
7104 * server's cookie.
7105 */
7106 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007107 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007108 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007109 /* this cookie must be deleted */
7110 if (*prev == ':' && next == hdr_end) {
7111 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007112 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007113 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7114 txn->hdr_idx.used--;
7115 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007116 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007117 hdr_next += delta;
7118 http_msg_move_end(&txn->rsp, delta);
7119 /* note: while both invalid now, <next> and <hdr_end>
7120 * are still equal, so the for() will stop as expected.
7121 */
7122 } else {
7123 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007124 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007125 next = prev;
7126 hdr_end += delta;
7127 hdr_next += delta;
7128 cur_hdr->len += delta;
7129 http_msg_move_end(&txn->rsp, delta);
7130 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007131 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007132 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007133 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007135 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007136 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007137 * with this server since we know it.
7138 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007139 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007140 next += delta;
7141 hdr_end += delta;
7142 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007144 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007145
Willy Tarreauf1348312010-10-07 15:54:11 +02007146 txn->flags &= ~TX_SCK_MASK;
7147 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007148 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007149 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007150 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007151 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007152 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007153 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007154 next += delta;
7155 hdr_end += delta;
7156 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007157 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007158 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007159
Willy Tarreau827aee92011-03-10 16:55:02 +01007160 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007161 txn->flags &= ~TX_SCK_MASK;
7162 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007163 }
7164 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007165 /* that's done for this cookie, check the next one on the same
7166 * line when next != hdr_end (only if is_cookie2).
7167 */
7168 }
7169 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007170 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007171 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007172}
7173
7174
Willy Tarreaua15645d2007-03-18 16:22:39 +01007175/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007176 * Parses the Cache-Control and Pragma request header fields to determine if
7177 * the request may be served from the cache and/or if it is cacheable. Updates
7178 * s->txn->flags.
7179 */
7180void check_request_for_cacheability(struct stream *s, struct channel *chn)
7181{
7182 struct http_txn *txn = s->txn;
7183 char *p1, *p2;
7184 char *cur_ptr, *cur_end, *cur_next;
7185 int pragma_found;
7186 int cc_found;
7187 int cur_idx;
7188
7189 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7190 return; /* nothing more to do here */
7191
7192 cur_idx = 0;
7193 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007194 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007195
7196 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7197 struct hdr_idx_elem *cur_hdr;
7198 int val;
7199
7200 cur_hdr = &txn->hdr_idx.v[cur_idx];
7201 cur_ptr = cur_next;
7202 cur_end = cur_ptr + cur_hdr->len;
7203 cur_next = cur_end + cur_hdr->cr + 1;
7204
7205 /* We have one full header between cur_ptr and cur_end, and the
7206 * next header starts at cur_next.
7207 */
7208
7209 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7210 if (val) {
7211 if ((cur_end - (cur_ptr + val) >= 8) &&
7212 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7213 pragma_found = 1;
7214 continue;
7215 }
7216 }
7217
William Lallemand8a16fe02018-05-22 11:04:33 +02007218 /* Don't use the cache and don't try to store if we found the
7219 * Authorization header */
7220 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7221 if (val) {
7222 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7223 txn->flags |= TX_CACHE_IGNORE;
7224 continue;
7225 }
7226
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007227 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7228 if (!val)
7229 continue;
7230
7231 /* OK, right now we know we have a cache-control header at cur_ptr */
7232 cc_found = 1;
7233 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7234
7235 if (p1 >= cur_end) /* no more info */
7236 continue;
7237
7238 /* p1 is at the beginning of the value */
7239 p2 = p1;
7240 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7241 p2++;
7242
7243 /* we have a complete value between p1 and p2. We don't check the
7244 * values after max-age, max-stale nor min-fresh, we simply don't
7245 * use the cache when they're specified.
7246 */
7247 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7248 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7249 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7250 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7251 txn->flags |= TX_CACHE_IGNORE;
7252 continue;
7253 }
7254
7255 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7256 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7257 continue;
7258 }
7259 }
7260
7261 /* RFC7234#5.4:
7262 * When the Cache-Control header field is also present and
7263 * understood in a request, Pragma is ignored.
7264 * When the Cache-Control header field is not present in a
7265 * request, caches MUST consider the no-cache request
7266 * pragma-directive as having the same effect as if
7267 * "Cache-Control: no-cache" were present.
7268 */
7269 if (!cc_found && pragma_found)
7270 txn->flags |= TX_CACHE_IGNORE;
7271}
7272
7273/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007274 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007275 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007276void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007277{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007278 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007279 char *p1, *p2;
7280
7281 char *cur_ptr, *cur_end, *cur_next;
7282 int cur_idx;
7283
Willy Tarreau12b32f22017-12-21 16:08:09 +01007284 if (txn->status < 200) {
7285 /* do not try to cache interim responses! */
7286 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007287 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007288 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007289
7290 /* Iterate through the headers.
7291 * we start with the start line.
7292 */
7293 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007294 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007295
7296 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7297 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007298 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007299
7300 cur_hdr = &txn->hdr_idx.v[cur_idx];
7301 cur_ptr = cur_next;
7302 cur_end = cur_ptr + cur_hdr->len;
7303 cur_next = cur_end + cur_hdr->cr + 1;
7304
7305 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007306 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007307 */
7308
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007309 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7310 if (val) {
7311 if ((cur_end - (cur_ptr + val) >= 8) &&
7312 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7313 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7314 return;
7315 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007316 }
7317
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007318 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7319 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007320 continue;
7321
7322 /* OK, right now we know we have a cache-control header at cur_ptr */
7323
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007324 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007325
7326 if (p1 >= cur_end) /* no more info */
7327 continue;
7328
7329 /* p1 is at the beginning of the value */
7330 p2 = p1;
7331
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007332 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007333 p2++;
7334
7335 /* we have a complete value between p1 and p2 */
7336 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007337 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7338 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7339 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7340 continue;
7341 }
7342
Willy Tarreaua15645d2007-03-18 16:22:39 +01007343 /* we have something of the form no-cache="set-cookie" */
7344 if ((cur_end - p1 >= 21) &&
7345 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7346 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007347 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007348 continue;
7349 }
7350
7351 /* OK, so we know that either p2 points to the end of string or to a comma */
7352 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007353 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007354 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007355 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007356 return;
7357 }
7358
7359 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007360 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007361 continue;
7362 }
7363 }
7364}
7365
Willy Tarreau58f10d72006-12-04 02:26:12 +01007366
Willy Tarreaub2513902006-12-17 14:52:38 +01007367/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007368 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007369 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007370 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007371 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007372 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007373 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007374 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007375 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007376int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007377{
7378 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007379 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007380 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007381
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007382 if (!uri_auth)
7383 return 0;
7384
Cyril Bonté70be45d2010-10-12 00:14:35 +02007385 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007386 return 0;
7387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007388 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007389 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007390 return 0;
7391
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007392 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007393 return 0;
7394
Willy Tarreaub2513902006-12-17 14:52:38 +01007395 return 1;
7396}
7397
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007398/* Append the description of what is present in error snapshot <es> into <out>.
7399 * The description must be small enough to always fit in a trash. The output
7400 * buffer may be the trash so the trash must not be used inside this function.
7401 */
7402void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7403{
7404 chunk_appendf(&trash,
7405 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7406 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7407 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7408 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7409 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7410 es->ctx.http.m_flags, es->ctx.http.m_clen,
7411 es->ctx.http.m_blen, es->ctx.http.b_flags);
7412}
7413
Willy Tarreau4076a152009-04-02 15:18:36 +02007414/*
7415 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007416 * By default it tries to report the error position as msg->err_pos. However if
7417 * this one is not set, it will then report msg->next, which is the last known
7418 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007419 * displays buffers as a contiguous area starting at buf->p. The direction is
7420 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007421 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007422void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007423 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007424 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007425{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007426 union error_snapshot_ctx ctx;
7427 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007428
Willy Tarreauef3ca732018-09-07 15:47:35 +02007429 /* http-specific part now */
7430 ctx.http.sid = s->uniq_id;
7431 ctx.http.state = state;
7432 ctx.http.b_flags = msg->chn->flags;
7433 ctx.http.s_flags = s->flags;
7434 ctx.http.t_flags = s->txn->flags;
7435 ctx.http.m_flags = msg->flags;
7436 ctx.http.m_clen = msg->chunk_len;
7437 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007438
Willy Tarreauef3ca732018-09-07 15:47:35 +02007439 ofs = msg->chn->total - ci_data(msg->chn);
7440 if (ofs < 0)
7441 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007442
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007443 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007444 other_end, s->target,
7445 strm_sess(s), &msg->chn->buf,
7446 ofs, co_data(msg->chn),
7447 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7448 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007449}
Willy Tarreaub2513902006-12-17 14:52:38 +01007450
Willy Tarreau294c4732011-12-16 21:35:50 +01007451/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7452 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7453 * performed over the whole headers. Otherwise it must contain a valid header
7454 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7455 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7456 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7457 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007458 * -1. The value fetch stops at commas, so this function is suited for use with
7459 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007460 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007461 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007462unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007463 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007464 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007465{
Willy Tarreau294c4732011-12-16 21:35:50 +01007466 struct hdr_ctx local_ctx;
7467 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007468 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007469 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007470 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007471
Willy Tarreau294c4732011-12-16 21:35:50 +01007472 if (!ctx) {
7473 local_ctx.idx = 0;
7474 ctx = &local_ctx;
7475 }
7476
Willy Tarreaubce70882009-09-07 11:51:47 +02007477 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007478 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007479 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007480 occ--;
7481 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007482 *vptr = ctx->line + ctx->val;
7483 *vlen = ctx->vlen;
7484 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007485 }
7486 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007487 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007488 }
7489
7490 /* negative occurrence, we scan all the list then walk back */
7491 if (-occ > MAX_HDR_HISTORY)
7492 return 0;
7493
Willy Tarreau294c4732011-12-16 21:35:50 +01007494 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007495 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007496 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7497 len_hist[hist_ptr] = ctx->vlen;
7498 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007499 hist_ptr = 0;
7500 found++;
7501 }
7502 if (-occ > found)
7503 return 0;
7504 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007505 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7506 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7507 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007508 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007509 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007510 if (hist_ptr >= MAX_HDR_HISTORY)
7511 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007512 *vptr = ptr_hist[hist_ptr];
7513 *vlen = len_hist[hist_ptr];
7514 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007515}
7516
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007517/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7518 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7519 * performed over the whole headers. Otherwise it must contain a valid header
7520 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7521 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7522 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7523 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7524 * -1. This function differs from http_get_hdr() in that it only returns full
7525 * line header values and does not stop at commas.
7526 * The return value is 0 if nothing was found, or non-zero otherwise.
7527 */
7528unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7529 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007530 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007531{
7532 struct hdr_ctx local_ctx;
7533 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007534 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007535 unsigned int hist_ptr;
7536 int found;
7537
7538 if (!ctx) {
7539 local_ctx.idx = 0;
7540 ctx = &local_ctx;
7541 }
7542
7543 if (occ >= 0) {
7544 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007545 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007546 occ--;
7547 if (occ <= 0) {
7548 *vptr = ctx->line + ctx->val;
7549 *vlen = ctx->vlen;
7550 return 1;
7551 }
7552 }
7553 return 0;
7554 }
7555
7556 /* negative occurrence, we scan all the list then walk back */
7557 if (-occ > MAX_HDR_HISTORY)
7558 return 0;
7559
7560 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007561 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007562 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7563 len_hist[hist_ptr] = ctx->vlen;
7564 if (++hist_ptr >= MAX_HDR_HISTORY)
7565 hist_ptr = 0;
7566 found++;
7567 }
7568 if (-occ > found)
7569 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007570
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007571 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007572 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7573 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7574 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007575 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007576 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007577 if (hist_ptr >= MAX_HDR_HISTORY)
7578 hist_ptr -= MAX_HDR_HISTORY;
7579 *vptr = ptr_hist[hist_ptr];
7580 *vlen = len_hist[hist_ptr];
7581 return 1;
7582}
7583
Willy Tarreaubaaee002006-06-26 02:48:02 +02007584/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007585 * Print a debug line with a header. Always stop at the first CR or LF char,
7586 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7587 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007588 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007589void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007590{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007591 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007592 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007593
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007594 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007595 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007596 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007597 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 +02007598
7599 for (max = 0; start + max < end; max++)
7600 if (start[max] == '\r' || start[max] == '\n')
7601 break;
7602
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007603 UBOUND(max, trash.size - trash.data - 3);
7604 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7605 trash.area[trash.data++] = '\n';
7606 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007607}
7608
Willy Tarreaueee5b512015-04-03 23:46:31 +02007609
7610/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7611 * The hdr_idx is allocated as well. In case of allocation failure, everything
7612 * allocated is freed and NULL is returned. Otherwise the new transaction is
7613 * assigned to the stream and returned.
7614 */
7615struct http_txn *http_alloc_txn(struct stream *s)
7616{
7617 struct http_txn *txn = s->txn;
7618
7619 if (txn)
7620 return txn;
7621
Willy Tarreaubafbe012017-11-24 17:34:44 +01007622 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007623 if (!txn)
7624 return txn;
7625
7626 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007627 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007628 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007629 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007630 return NULL;
7631 }
7632
7633 s->txn = txn;
7634 return txn;
7635}
7636
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007637void http_txn_reset_req(struct http_txn *txn)
7638{
7639 txn->req.flags = 0;
7640 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7641 txn->req.next = 0;
7642 txn->req.chunk_len = 0LL;
7643 txn->req.body_len = 0LL;
7644 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7645}
7646
7647void http_txn_reset_res(struct http_txn *txn)
7648{
7649 txn->rsp.flags = 0;
7650 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7651 txn->rsp.next = 0;
7652 txn->rsp.chunk_len = 0LL;
7653 txn->rsp.body_len = 0LL;
7654 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7655}
7656
Willy Tarreau0937bc42009-12-22 15:03:09 +01007657/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007658 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007659 * the required fields are properly allocated and that we only need to (re)init
7660 * them. This should be used before processing any new request.
7661 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007662void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007663{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007664 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007665 struct proxy *fe = strm_fe(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02007666 struct conn_stream *cs = objt_cs(s->si[0].end);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007667
Christopher Fauletf2824e62018-10-01 12:12:37 +02007668 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
7669 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
7670 : 0);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007671 txn->status = -1;
7672
Willy Tarreauf64d1412010-10-07 20:06:11 +02007673 txn->cookie_first_date = 0;
7674 txn->cookie_last_date = 0;
7675
Willy Tarreaueee5b512015-04-03 23:46:31 +02007676 txn->srv_cookie = NULL;
7677 txn->cli_cookie = NULL;
7678 txn->uri = NULL;
7679
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007680 http_txn_reset_req(txn);
7681 http_txn_reset_res(txn);
7682
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007683 txn->req.chn = &s->req;
7684 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007685
7686 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007687
7688 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7689 if (fe->options2 & PR_O2_REQBUG_OK)
7690 txn->req.err_pos = -1; /* let buggy requests pass */
7691
Willy Tarreau0937bc42009-12-22 15:03:09 +01007692 if (txn->hdr_idx.v)
7693 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007694
7695 vars_init(&s->vars_txn, SCOPE_TXN);
7696 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007697}
7698
7699/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007700void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007701{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007702 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007703 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007704
7705 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007706 pool_free(pool_head_requri, txn->uri);
7707 pool_free(pool_head_capture, txn->cli_cookie);
7708 pool_free(pool_head_capture, txn->srv_cookie);
7709 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007710
William Lallemanda73203e2012-03-12 12:48:57 +01007711 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007712 txn->uri = NULL;
7713 txn->srv_cookie = NULL;
7714 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007715
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007716 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007717 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007718 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007719 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007720 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007721 }
7722
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007723 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007724 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007725 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007726 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007727 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007728 }
7729
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007730 if (!LIST_ISEMPTY(&s->vars_txn.head))
7731 vars_prune(&s->vars_txn, s->sess, s);
7732 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7733 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007734}
7735
7736/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007737void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007738{
7739 http_end_txn(s);
7740 http_init_txn(s);
7741
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007742 /* reinitialise the current rule list pointer to NULL. We are sure that
7743 * any rulelist match the NULL pointer.
7744 */
7745 s->current_rule_list = NULL;
7746
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007747 s->be = strm_fe(s);
7748 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007749 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007750 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007751 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007752 /* re-init store persistence */
7753 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007754 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007755
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007756 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007757
Willy Tarreau739cfba2010-01-25 23:11:14 +01007758 /* We must trim any excess data from the response buffer, because we
7759 * may have blocked an invalid response from a server that we don't
7760 * want to accidentely forward once we disable the analysers, nor do
7761 * we want those data to come along with next response. A typical
7762 * example of such data would be from a buggy server responding to
7763 * a HEAD with some data, or sending more than the advertised
7764 * content-length.
7765 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007766 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007767 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007768
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007769 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007770 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007771
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007772 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007773 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007774
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007775 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007776 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007777
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007778 s->req.rex = TICK_ETERNITY;
7779 s->req.wex = TICK_ETERNITY;
7780 s->req.analyse_exp = TICK_ETERNITY;
7781 s->res.rex = TICK_ETERNITY;
7782 s->res.wex = TICK_ETERNITY;
7783 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007784 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007785}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007786
Willy Tarreau79e57332018-10-02 16:01:16 +02007787/* This function executes one of the set-{method,path,query,uri} actions. It
7788 * takes the string from the variable 'replace' with length 'len', then modifies
7789 * the relevant part of the request line accordingly. Then it updates various
7790 * pointers to the next elements which were moved, and the total buffer length.
7791 * It finds the action to be performed in p[2], previously filled by function
7792 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7793 * error, though this can be revisited when this code is finally exploited.
7794 *
7795 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7796 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007797 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007798 * In query string case, the mark question '?' must be set at the start of the
7799 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007800 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007801int http_replace_req_line(int action, const char *replace, int len,
7802 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007803{
Willy Tarreau79e57332018-10-02 16:01:16 +02007804 struct http_txn *txn = s->txn;
7805 char *cur_ptr, *cur_end;
7806 int offset = 0;
7807 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007808
Willy Tarreau79e57332018-10-02 16:01:16 +02007809 switch (action) {
7810 case 0: // method
7811 cur_ptr = ci_head(&s->req);
7812 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007813
Willy Tarreau79e57332018-10-02 16:01:16 +02007814 /* adjust req line offsets and lengths */
7815 delta = len - offset - (cur_end - cur_ptr);
7816 txn->req.sl.rq.m_l += delta;
7817 txn->req.sl.rq.u += delta;
7818 txn->req.sl.rq.v += delta;
7819 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007820
Willy Tarreau79e57332018-10-02 16:01:16 +02007821 case 1: // path
7822 cur_ptr = http_txn_get_path(txn);
7823 if (!cur_ptr)
7824 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007825
Willy Tarreau79e57332018-10-02 16:01:16 +02007826 cur_end = cur_ptr;
7827 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7828 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007829
Willy Tarreau79e57332018-10-02 16:01:16 +02007830 /* adjust req line offsets and lengths */
7831 delta = len - offset - (cur_end - cur_ptr);
7832 txn->req.sl.rq.u_l += delta;
7833 txn->req.sl.rq.v += delta;
7834 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007835
Willy Tarreau79e57332018-10-02 16:01:16 +02007836 case 2: // query
7837 offset = 1;
7838 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7839 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7840 while (cur_ptr < cur_end && *cur_ptr != '?')
7841 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007842
Willy Tarreau79e57332018-10-02 16:01:16 +02007843 /* skip the question mark or indicate that we must insert it
7844 * (but only if the format string is not empty then).
7845 */
7846 if (cur_ptr < cur_end)
7847 cur_ptr++;
7848 else if (len > 1)
7849 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007850
Willy Tarreau79e57332018-10-02 16:01:16 +02007851 /* adjust req line offsets and lengths */
7852 delta = len - offset - (cur_end - cur_ptr);
7853 txn->req.sl.rq.u_l += delta;
7854 txn->req.sl.rq.v += delta;
7855 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007856
Willy Tarreau79e57332018-10-02 16:01:16 +02007857 case 3: // uri
7858 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7859 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007860
Willy Tarreau79e57332018-10-02 16:01:16 +02007861 /* adjust req line offsets and lengths */
7862 delta = len - offset - (cur_end - cur_ptr);
7863 txn->req.sl.rq.u_l += delta;
7864 txn->req.sl.rq.v += delta;
7865 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007866
Willy Tarreau79e57332018-10-02 16:01:16 +02007867 default:
7868 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007869 }
7870
Willy Tarreau79e57332018-10-02 16:01:16 +02007871 /* commit changes and adjust end of message */
7872 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7873 txn->req.sl.rq.l += delta;
7874 txn->hdr_idx.v[0].len += delta;
7875 http_msg_move_end(&txn->req, delta);
7876 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007877}
7878
Willy Tarreau79e57332018-10-02 16:01:16 +02007879/* This function replace the HTTP status code and the associated message. The
7880 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007881 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007882void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007883{
Willy Tarreau79e57332018-10-02 16:01:16 +02007884 struct http_txn *txn = s->txn;
7885 char *cur_ptr, *cur_end;
7886 int delta;
7887 char *res;
7888 int c_l;
7889 const char *msg = reason;
7890 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007891
Willy Tarreau79e57332018-10-02 16:01:16 +02007892 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007893
Willy Tarreau79e57332018-10-02 16:01:16 +02007894 res = ultoa_o(status, trash.area, trash.size);
7895 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007896
Willy Tarreau79e57332018-10-02 16:01:16 +02007897 trash.area[c_l] = ' ';
7898 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007899
Willy Tarreau79e57332018-10-02 16:01:16 +02007900 /* Do we have a custom reason format string? */
7901 if (msg == NULL)
7902 msg = http_get_reason(status);
7903 msg_len = strlen(msg);
7904 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7905 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007906
Willy Tarreau79e57332018-10-02 16:01:16 +02007907 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7908 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007909
Willy Tarreau79e57332018-10-02 16:01:16 +02007910 /* commit changes and adjust message */
7911 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7912 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007913
Willy Tarreau79e57332018-10-02 16:01:16 +02007914 /* adjust res line offsets and lengths */
7915 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7916 txn->rsp.sl.st.c_l = c_l;
7917 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007918
Willy Tarreau79e57332018-10-02 16:01:16 +02007919 delta = trash.data - (cur_end - cur_ptr);
7920 txn->rsp.sl.st.l += delta;
7921 txn->hdr_idx.v[0].len += delta;
7922 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007923}
7924
Willy Tarreau8797c062007-05-07 00:55:35 +02007925__attribute__((constructor))
7926static void __http_protocol_init(void)
7927{
Willy Tarreau8797c062007-05-07 00:55:35 +02007928}
7929
7930
Willy Tarreau58f10d72006-12-04 02:26:12 +01007931/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007932 * Local variables:
7933 * c-indent-level: 8
7934 * c-basic-offset: 8
7935 * End:
7936 */