blob: face0bfeac6e04a1302874ee6838cc4f7cf54939 [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 Faulet315b39c2018-09-21 16:26:19 +0200793 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
794 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
795 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200796
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200797 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200798 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200799 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200800
Christopher Faulet315b39c2018-09-21 16:26:19 +0200801 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
802 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200803 tmp = TX_CON_WANT_CLO;
804
805 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
806 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
807
808 if (!(txn->flags & TX_HDR_CONN_PRS) &&
809 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
810 /* parse the Connection header and possibly clean it */
811 int to_del = 0;
812 if ((msg->flags & HTTP_MSGF_VER_11) ||
813 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200814 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200815 to_del |= 2; /* remove "keep-alive" */
816 if (!(msg->flags & HTTP_MSGF_VER_11))
817 to_del |= 1; /* remove "close" */
818 http_parse_connection_header(txn, msg, to_del);
819 }
820
821 /* check if client or config asks for explicit close in KAL/SCL */
822 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
823 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
824 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
825 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
826 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200827 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200828 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
829}
William Lallemand82fe75c2012-10-23 10:25:10 +0200830
Willy Tarreaud787e662009-07-07 10:14:51 +0200831/* This stream analyser waits for a complete HTTP request. It returns 1 if the
832 * processing can continue on next analysers, or zero if it either needs more
833 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100834 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200835 * when it has nothing left to do, and may remove any analyser when it wants to
836 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100837 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200838int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100839{
Willy Tarreau59234e92008-11-30 23:51:27 +0100840 /*
841 * We will parse the partial (or complete) lines.
842 * We will check the request syntax, and also join multi-line
843 * headers. An index of all the lines will be elaborated while
844 * parsing.
845 *
846 * For the parsing, we use a 28 states FSM.
847 *
848 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200849 * ci_head(req) = beginning of request
850 * ci_head(req) + msg->eoh = end of processed headers / start of current one
851 * ci_tail(req) = end of input data
852 * msg->eol = end of current header or line (LF or CRLF)
853 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200854 *
855 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200856 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200857 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
858 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100859 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100860
Willy Tarreau59234e92008-11-30 23:51:27 +0100861 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200862 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200863 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100864 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200865 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100866
Christopher Faulete0768eb2018-10-03 16:38:02 +0200867 if (IS_HTX_STRM(s))
868 return htx_wait_for_request(s, req, an_bit);
869
Christopher Faulet45073512018-07-20 10:16:29 +0200870 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 +0100871 now_ms, __FUNCTION__,
872 s,
873 req,
874 req->rex, req->wex,
875 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200876 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100877 req->analysers);
878
Willy Tarreau52a0c602009-08-16 22:45:38 +0200879 /* we're speaking HTTP here, so let's speak HTTP to the client */
880 s->srv_error = http_return_srv_error;
881
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300882 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200883 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300884 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
885
Willy Tarreau83e3af02009-12-28 17:39:57 +0100886 /* There's a protected area at the end of the buffer for rewriting
887 * purposes. We don't want to start to parse the request if the
888 * protected area is affected, because we may have to move processed
889 * data later, which is much more complicated.
890 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200891 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200892 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100893 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200894 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100895 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100896 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200897 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200898 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100899 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100900 return 0;
901 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200902 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200903 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200904 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100905 }
906
Willy Tarreauf37954d2018-06-15 18:31:02 +0200907 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100908 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100909 }
910
Willy Tarreau59234e92008-11-30 23:51:27 +0100911 /* 1: we might have to print this header in debug mode */
912 if (unlikely((global.mode & MODE_DEBUG) &&
913 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200914 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100915 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100916
Willy Tarreauf37954d2018-06-15 18:31:02 +0200917 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200918 /* this is a bit complex : in case of error on the request line,
919 * we know that rq.l is still zero, so we display only the part
920 * up to the end of the line (truncated by debug_hdr).
921 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200922 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100923 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100924
Willy Tarreau59234e92008-11-30 23:51:27 +0100925 sol += hdr_idx_first_pos(&txn->hdr_idx);
926 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100927
Willy Tarreau59234e92008-11-30 23:51:27 +0100928 while (cur_idx) {
929 eol = sol + txn->hdr_idx.v[cur_idx].len;
930 debug_hdr("clihdr", s, sol, eol);
931 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
932 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100933 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100934 }
935
Willy Tarreau58f10d72006-12-04 02:26:12 +0100936
Willy Tarreau59234e92008-11-30 23:51:27 +0100937 /*
938 * Now we quickly check if we have found a full valid request.
939 * If not so, we check the FD and buffer states before leaving.
940 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100941 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100942 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200943 * on a keep-alive stream, if we encounter and error, close, t/o,
944 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100945 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200946 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200947 * Last, we may increase some tracked counters' http request errors on
948 * the cases that are deliberately the client's fault. For instance,
949 * a timeout or connection reset is not counted as an error. However
950 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100951 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100952
Willy Tarreau655dce92009-11-08 13:10:58 +0100953 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100954 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100955 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100956 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100957 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200958 stream_inc_http_req_ctr(s);
959 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200960 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100961 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100962 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100963
Willy Tarreau59234e92008-11-30 23:51:27 +0100964 /* 1: Since we are in header mode, if there's no space
965 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200966 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100967 * must terminate it now.
968 */
Willy Tarreau23752332018-06-15 14:54:53 +0200969 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100970 /* FIXME: check if URI is set and return Status
971 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100972 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200973 stream_inc_http_req_ctr(s);
974 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200975 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200976 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200977 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100978 goto return_bad_req;
979 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100980
Willy Tarreau59234e92008-11-30 23:51:27 +0100981 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200982 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200983 if (!(s->flags & SF_ERR_MASK))
984 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100985
Willy Tarreaufcffa692010-01-10 14:21:19 +0100986 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100987 goto failed_keep_alive;
988
Willy Tarreau0f228a02015-05-01 15:37:53 +0200989 if (sess->fe->options & PR_O_IGNORE_PRB)
990 goto failed_keep_alive;
991
Willy Tarreau59234e92008-11-30 23:51:27 +0100992 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200993 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200994 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200995 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200996 }
997
Willy Tarreaudc979f22012-12-04 10:39:01 +0100998 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100999 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001000 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001001 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001002 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001003 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001004 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001005 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001006 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001007 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001008
Willy Tarreaue7dff022015-04-03 01:14:29 +02001009 if (!(s->flags & SF_FINST_MASK))
1010 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001011 return 0;
1012 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001013
Willy Tarreau59234e92008-11-30 23:51:27 +01001014 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001015 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001016 if (!(s->flags & SF_ERR_MASK))
1017 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001018
Willy Tarreaufcffa692010-01-10 14:21:19 +01001019 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001020 goto failed_keep_alive;
1021
Willy Tarreau0f228a02015-05-01 15:37:53 +02001022 if (sess->fe->options & PR_O_IGNORE_PRB)
1023 goto failed_keep_alive;
1024
Willy Tarreau59234e92008-11-30 23:51:27 +01001025 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001026 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001027 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001028 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001029 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001030 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001031 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001032 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001033 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001034 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001035
Willy Tarreau87b09662015-04-03 00:22:06 +02001036 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001037 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001038 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001039 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001040 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001041
Willy Tarreaue7dff022015-04-03 01:14:29 +02001042 if (!(s->flags & SF_FINST_MASK))
1043 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001044 return 0;
1045 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001046
Willy Tarreau59234e92008-11-30 23:51:27 +01001047 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001048 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001049 if (!(s->flags & SF_ERR_MASK))
1050 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001051
Willy Tarreaufcffa692010-01-10 14:21:19 +01001052 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001053 goto failed_keep_alive;
1054
Willy Tarreau0f228a02015-05-01 15:37:53 +02001055 if (sess->fe->options & PR_O_IGNORE_PRB)
1056 goto failed_keep_alive;
1057
Willy Tarreau4076a152009-04-02 15:18:36 +02001058 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001059 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001060 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001061 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001062 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001063 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001064 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001065 stream_inc_http_err_ctr(s);
1066 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001067 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001068 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001069 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001070 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001071
Willy Tarreaue7dff022015-04-03 01:14:29 +02001072 if (!(s->flags & SF_FINST_MASK))
1073 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001074 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001075 }
1076
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001077 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001078 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001079 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001080#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001081 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001082 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001083 /* We need more data, we have to re-enable quick-ack in case we
1084 * previously disabled it, otherwise we might cause the client
1085 * to delay next data.
1086 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001087 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001088 }
1089#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001090
Willy Tarreaufcffa692010-01-10 14:21:19 +01001091 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1092 /* If the client starts to talk, let's fall back to
1093 * request timeout processing.
1094 */
1095 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001096 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001097 }
1098
Willy Tarreau59234e92008-11-30 23:51:27 +01001099 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001100 if (!tick_isset(req->analyse_exp)) {
1101 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1102 (txn->flags & TX_WAIT_NEXT_RQ) &&
1103 tick_isset(s->be->timeout.httpka))
1104 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1105 else
1106 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1107 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001108
Willy Tarreau59234e92008-11-30 23:51:27 +01001109 /* we're not ready yet */
1110 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001111
1112 failed_keep_alive:
1113 /* Here we process low-level errors for keep-alive requests. In
1114 * short, if the request is not the first one and it experiences
1115 * a timeout, read error or shutdown, we just silently close so
1116 * that the client can try again.
1117 */
1118 txn->status = 0;
1119 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001120 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001121 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001122 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001123 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001124 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001125 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001126 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001127
Willy Tarreaud787e662009-07-07 10:14:51 +02001128 /* OK now we have a complete HTTP request with indexed headers. Let's
1129 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001130 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001131 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001132 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001133 * byte after the last LF. msg->sov points to the first byte of data.
1134 * msg->eol cannot be trusted because it may have been left uninitialized
1135 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001136 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001137
Willy Tarreau87b09662015-04-03 00:22:06 +02001138 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001139 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001140
Willy Tarreaub16a5742010-01-10 14:46:16 +01001141 if (txn->flags & TX_WAIT_NEXT_RQ) {
1142 /* kill the pending keep-alive timeout */
1143 txn->flags &= ~TX_WAIT_NEXT_RQ;
1144 req->analyse_exp = TICK_ETERNITY;
1145 }
1146
1147
Willy Tarreaud787e662009-07-07 10:14:51 +02001148 /* Maybe we found in invalid header name while we were configured not
1149 * to block on that, so we have to capture it now.
1150 */
1151 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001152 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001153
Willy Tarreau59234e92008-11-30 23:51:27 +01001154 /*
1155 * 1: identify the method
1156 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001157 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001158
1159 /* we can make use of server redirect on GET and HEAD */
1160 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001161 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001162 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001163 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001164 /* PRI is reserved for the HTTP/2 preface */
1165 msg->err_pos = 0;
1166 goto return_bad_req;
1167 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001168
Willy Tarreau59234e92008-11-30 23:51:27 +01001169 /*
1170 * 2: check if the URI matches the monitor_uri.
1171 * We have to do this for every request which gets in, because
1172 * the monitor-uri is defined by the frontend.
1173 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001174 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1175 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001176 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001177 sess->fe->monitor_uri,
1178 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001179 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001180 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001181 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001182 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001183
Willy Tarreaue7dff022015-04-03 01:14:29 +02001184 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001185 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001186
Willy Tarreau59234e92008-11-30 23:51:27 +01001187 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001188 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001189 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001190
Willy Tarreau59234e92008-11-30 23:51:27 +01001191 ret = acl_pass(ret);
1192 if (cond->pol == ACL_COND_UNLESS)
1193 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001194
Willy Tarreau59234e92008-11-30 23:51:27 +01001195 if (ret) {
1196 /* we fail this request, let's return 503 service unavail */
1197 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001198 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001199 if (!(s->flags & SF_ERR_MASK))
1200 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001201 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001202 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001203 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001204
Willy Tarreau59234e92008-11-30 23:51:27 +01001205 /* nothing to fail, let's reply normaly */
1206 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001207 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001208 if (!(s->flags & SF_ERR_MASK))
1209 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001210 goto return_prx_cond;
1211 }
1212
1213 /*
1214 * 3: Maybe we have to copy the original REQURI for the logs ?
1215 * Note: we cannot log anymore if the request has been
1216 * classified as invalid.
1217 */
1218 if (unlikely(s->logs.logwait & LW_REQ)) {
1219 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001220 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001221 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001222
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001223 if (urilen >= global.tune.requri_len )
1224 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001225 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001226 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001227
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001228 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001229 s->do_log(s);
1230 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001231 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001232 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001233 }
Willy Tarreau06619262006-12-17 08:37:22 +01001234
Willy Tarreau91852eb2015-05-01 13:26:00 +02001235 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1236 * exactly one digit "." one digit. This check may be disabled using
1237 * option accept-invalid-http-request.
1238 */
1239 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1240 if (msg->sl.rq.v_l != 8) {
1241 msg->err_pos = msg->sl.rq.v;
1242 goto return_bad_req;
1243 }
1244
Willy Tarreauf37954d2018-06-15 18:31:02 +02001245 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1246 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1247 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1248 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001249 msg->err_pos = msg->sl.rq.v + 4;
1250 goto return_bad_req;
1251 }
1252 }
Willy Tarreau13317662015-05-01 13:47:08 +02001253 else {
1254 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1255 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1256 goto return_bad_req;
1257 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001258
Willy Tarreau5b154472009-12-21 20:11:07 +01001259 /* ... and check if the request is HTTP/1.1 or above */
1260 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001261 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1262 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1263 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001264 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001265
1266 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001267 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 +01001268
Willy Tarreau88d349d2010-01-25 12:15:43 +01001269 /* if the frontend has "option http-use-proxy-header", we'll check if
1270 * we have what looks like a proxied connection instead of a connection,
1271 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1272 * Note that this is *not* RFC-compliant, however browsers and proxies
1273 * happen to do that despite being non-standard :-(
1274 * We consider that a request not beginning with either '/' or '*' is
1275 * a proxied connection, which covers both "scheme://location" and
1276 * CONNECT ip:port.
1277 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001278 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001279 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001280 txn->flags |= TX_USE_PX_CONN;
1281
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001282 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001283 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001284
Willy Tarreau59234e92008-11-30 23:51:27 +01001285 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001286 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001287 http_capture_headers(ci_head(req), &txn->hdr_idx,
1288 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001289
Willy Tarreau557f1992015-05-01 10:05:17 +02001290 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1291 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001292 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001293 * The length of a message body is determined by one of the following
1294 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001295 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001296 * 1. Any response to a HEAD request and any response with a 1xx
1297 * (Informational), 204 (No Content), or 304 (Not Modified) status
1298 * code is always terminated by the first empty line after the
1299 * header fields, regardless of the header fields present in the
1300 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001301 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001302 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1303 * the connection will become a tunnel immediately after the empty
1304 * line that concludes the header fields. A client MUST ignore any
1305 * Content-Length or Transfer-Encoding header fields received in
1306 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001307 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001308 * 3. If a Transfer-Encoding header field is present and the chunked
1309 * transfer coding (Section 4.1) is the final encoding, the message
1310 * body length is determined by reading and decoding the chunked
1311 * data until the transfer coding indicates the data is complete.
1312 *
1313 * If a Transfer-Encoding header field is present in a response and
1314 * the chunked transfer coding is not the final encoding, the
1315 * message body length is determined by reading the connection until
1316 * it is closed by the server. If a Transfer-Encoding header field
1317 * is present in a request and the chunked transfer coding is not
1318 * the final encoding, the message body length cannot be determined
1319 * reliably; the server MUST respond with the 400 (Bad Request)
1320 * status code and then close the connection.
1321 *
1322 * If a message is received with both a Transfer-Encoding and a
1323 * Content-Length header field, the Transfer-Encoding overrides the
1324 * Content-Length. Such a message might indicate an attempt to
1325 * perform request smuggling (Section 9.5) or response splitting
1326 * (Section 9.4) and ought to be handled as an error. A sender MUST
1327 * remove the received Content-Length field prior to forwarding such
1328 * a message downstream.
1329 *
1330 * 4. If a message is received without Transfer-Encoding and with
1331 * either multiple Content-Length header fields having differing
1332 * field-values or a single Content-Length header field having an
1333 * invalid value, then the message framing is invalid and the
1334 * recipient MUST treat it as an unrecoverable error. If this is a
1335 * request message, the server MUST respond with a 400 (Bad Request)
1336 * status code and then close the connection. If this is a response
1337 * message received by a proxy, the proxy MUST close the connection
1338 * to the server, discard the received response, and send a 502 (Bad
1339 * Gateway) response to the client. If this is a response message
1340 * received by a user agent, the user agent MUST close the
1341 * connection to the server and discard the received response.
1342 *
1343 * 5. If a valid Content-Length header field is present without
1344 * Transfer-Encoding, its decimal value defines the expected message
1345 * body length in octets. If the sender closes the connection or
1346 * the recipient times out before the indicated number of octets are
1347 * received, the recipient MUST consider the message to be
1348 * incomplete and close the connection.
1349 *
1350 * 6. If this is a request message and none of the above are true, then
1351 * the message body length is zero (no message body is present).
1352 *
1353 * 7. Otherwise, this is a response message without a declared message
1354 * body length, so the message body length is determined by the
1355 * number of octets received prior to the server closing the
1356 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001357 */
1358
Willy Tarreau32b47f42009-10-18 20:55:02 +02001359 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001360 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001361 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001362 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001363 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001364 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001365 /* chunked not last, return badreq */
1366 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001367 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001368 }
1369
Willy Tarreau1c913912015-04-30 10:57:51 +02001370 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001371 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001372 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001373 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001374 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1375 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001376 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001377 signed long long cl;
1378
Willy Tarreauad14f752011-09-02 20:33:27 +02001379 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001380 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001381 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001382 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001383
Willy Tarreauad14f752011-09-02 20:33:27 +02001384 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001385 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001386 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001387 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001388
Willy Tarreauad14f752011-09-02 20:33:27 +02001389 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001390 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001391 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001392 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001393
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001394 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001395 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001396 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001397 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001398
Christopher Fauletbe821b92017-03-30 11:21:53 +02001399 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001400 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001401 }
1402
Willy Tarreau34dfc602015-05-01 10:09:49 +02001403 /* even bodyless requests have a known length */
1404 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001405
Willy Tarreau179085c2014-04-28 16:48:56 +02001406 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1407 * only change if both the request and the config reference something else.
1408 * Option httpclose by itself sets tunnel mode where headers are mangled.
1409 * However, if another mode is set, it will affect it (eg: server-close/
1410 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1411 * if FE and BE have the same settings (common). The method consists in
1412 * checking if options changed between the two calls (implying that either
1413 * one is non-null, or one of them is non-null and we are there for the first
1414 * time.
1415 */
1416 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001417 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001418 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001419
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001420 /* we may have to wait for the request's body */
1421 if ((s->be->options & PR_O_WREQ_BODY) &&
1422 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1423 req->analysers |= AN_REQ_HTTP_BODY;
1424
Willy Tarreau83ece462017-12-21 15:13:09 +01001425 /*
1426 * RFC7234#4:
1427 * A cache MUST write through requests with methods
1428 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1429 * the origin server; i.e., a cache is not allowed
1430 * to generate a reply to such a request before
1431 * having forwarded the request and having received
1432 * a corresponding response.
1433 *
1434 * RFC7231#4.2.1:
1435 * Of the request methods defined by this
1436 * specification, the GET, HEAD, OPTIONS, and TRACE
1437 * methods are defined to be safe.
1438 */
1439 if (likely(txn->meth == HTTP_METH_GET ||
1440 txn->meth == HTTP_METH_HEAD ||
1441 txn->meth == HTTP_METH_OPTIONS ||
1442 txn->meth == HTTP_METH_TRACE))
1443 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1444
Willy Tarreaud787e662009-07-07 10:14:51 +02001445 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001446 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001447 req->analyse_exp = TICK_ETERNITY;
1448 return 1;
1449
1450 return_bad_req:
1451 /* We centralize bad requests processing here */
1452 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1453 /* we detected a parsing error. We want to archive this request
1454 * in the dedicated proxy area for later troubleshooting.
1455 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001456 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001457 }
1458
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001459 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001460 txn->req.msg_state = HTTP_MSG_ERROR;
1461 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001462 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001463
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001464 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001465 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001466 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001467
1468 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001469 if (!(s->flags & SF_ERR_MASK))
1470 s->flags |= SF_ERR_PRXCOND;
1471 if (!(s->flags & SF_FINST_MASK))
1472 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001473
Christopher Faulet0184ea72017-01-05 14:06:34 +01001474 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001475 req->analyse_exp = TICK_ETERNITY;
1476 return 0;
1477}
1478
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001479
Willy Tarreau347a35d2013-11-22 17:51:09 +01001480/* This function prepares an applet to handle the stats. It can deal with the
1481 * "100-continue" expectation, check that admin rules are met for POST requests,
1482 * and program a response message if something was unexpected. It cannot fail
1483 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001484 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001485 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001486 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001487 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001488int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001489{
1490 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001491 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001492 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001493 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001494 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001495 struct uri_auth *uri_auth = s->be->uri_auth;
1496 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001497 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001498
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001499 appctx = si_appctx(si);
1500 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1501 appctx->st1 = appctx->st2 = 0;
1502 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1503 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001504 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001505 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001506
Willy Tarreauf37954d2018-06-15 18:31:02 +02001507 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001508 lookup = uri + uri_auth->uri_len;
1509
1510 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1511 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001512 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001513 break;
1514 }
1515 }
1516
1517 if (uri_auth->refresh) {
1518 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1519 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001520 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001521 break;
1522 }
1523 }
1524 }
1525
1526 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1527 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001528 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001529 break;
1530 }
1531 }
1532
Willy Tarreau1e62df92016-01-11 18:57:53 +01001533 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1534 if (memcmp(h, ";typed", 6) == 0) {
1535 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1536 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1537 break;
1538 }
1539 }
1540
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001541 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1542 if (memcmp(h, ";st=", 4) == 0) {
1543 int i;
1544 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001545 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001546 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1547 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001548 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001549 break;
1550 }
1551 }
1552 break;
1553 }
1554 }
1555
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001556 appctx->ctx.stats.scope_str = 0;
1557 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001558 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1559 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1560 int itx = 0;
1561 const char *h2;
1562 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1563 const char *err;
1564
1565 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1566 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001567 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001568 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1569 itx++;
1570 h++;
1571 }
1572
1573 if (itx > STAT_SCOPE_TXT_MAXLEN)
1574 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001575 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001576
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001577 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001578 memcpy(scope_txt, h2, itx);
1579 scope_txt[itx] = '\0';
1580 err = invalid_char(scope_txt);
1581 if (err) {
1582 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001583 appctx->ctx.stats.scope_str = 0;
1584 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001585 }
1586 break;
1587 }
1588 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001589
1590 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001591 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001592 int ret = 1;
1593
1594 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001595 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 +01001596 ret = acl_pass(ret);
1597 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1598 ret = !ret;
1599 }
1600
1601 if (ret) {
1602 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001603 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001604 break;
1605 }
1606 }
1607
1608 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001609 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001610 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001611 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001612 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1613 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001614 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001615 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001616 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001617 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1618 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001619 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001620 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001621 else {
1622 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001623 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001624 }
1625
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001626 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001627 return 1;
1628}
1629
Lukas Tribus67db8df2013-06-23 17:37:13 +02001630/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1631 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1632 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001633void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001634{
1635#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001636 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001637 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1638#endif
1639#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001640 if (from->ss_family == AF_INET6) {
1641 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001642 /* v4-mapped addresses need IP_TOS */
1643 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1644 else
1645 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1646 }
1647#endif
1648}
1649
Willy Tarreau87b09662015-04-03 00:22:06 +02001650int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001651 const char* name, unsigned int name_len,
1652 const char *str, struct my_regex *re,
1653 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001654{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001655 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001656 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001657 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001658 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1659 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001660 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001661
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001662 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001663
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001664 /* Choose the header browsing function. */
1665 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001666 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001667 http_find_hdr_func = http_find_header2;
1668 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001669 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001670 http_find_hdr_func = http_find_full_header2;
1671 break;
1672 default: /* impossible */
1673 return -1;
1674 }
1675
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001676 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1677 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001678 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001679 char *val = ctx.line + ctx.val;
1680 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001681
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001682 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1683 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001684
Willy Tarreau6e27be12018-08-22 04:46:47 +02001685 len = exp_replace(output->area, output->size, val, str, pmatch);
1686 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001687 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001688
Willy Tarreau6e27be12018-08-22 04:46:47 +02001689 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001690
1691 hdr->len += delta;
1692 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001693
1694 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001695 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001696 }
1697
1698 return 0;
1699}
1700
Willy Tarreau87b09662015-04-03 00:22:06 +02001701static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001702 const char* name, unsigned int name_len,
1703 struct list *fmt, struct my_regex *re,
1704 int action)
1705{
Willy Tarreau83061a82018-07-13 11:56:34 +02001706 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001707 int ret = -1;
1708
1709 replace = alloc_trash_chunk();
1710 if (!replace)
1711 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001712
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001713 replace->data = build_logline(s, replace->area, replace->size, fmt);
1714 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001715 goto leave;
1716
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001717 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1718 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001719
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001720 leave:
1721 free_trash_chunk(replace);
1722 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001723}
1724
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001725/*
1726 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1727 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001728 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1729 * line is inserted before the header. If an error occurred <early_hints> is
1730 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001731 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001732static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1733 const char* name, unsigned int name_len,
1734 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001735{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001736 if (!early_hints) {
1737 early_hints = alloc_trash_chunk();
1738 if (!early_hints)
1739 goto fail;
1740 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1741 goto fail;
1742 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001743
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001744 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1745 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001746
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001747 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1748 if (!chunk_memcat(early_hints, "\r\n", 2))
1749 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001750
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001751 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001752
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001753 fail:
1754 free_trash_chunk(early_hints);
1755 return NULL;
1756}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001757
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001758/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1759 * response is added. If an error occurred or if another response was already
1760 * sent, this function does nothing.
1761 */
1762static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1763{
1764 struct channel *chn = s->txn->rsp.chn;
1765 char *cur_ptr = ci_head(chn);
1766 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001767
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001768 /* If a response was already sent, skip early hints */
1769 if (s->txn->status > 0)
1770 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001771
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001772 if (!chunk_memcat(early_hints, "\r\n", 2))
1773 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001774
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001775 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1776 c_adv(chn, ret);
1777 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001778}
1779
Willy Tarreau87b09662015-04-03 00:22:06 +02001780/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001781 * transaction <txn>. Returns the verdict of the first rule that prevents
1782 * further processing of the request (auth, deny, ...), and defaults to
1783 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1784 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001785 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1786 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1787 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001788 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001789enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001790http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001791{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001792 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001793 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001794 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001795 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001796 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001797 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001798 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001799 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001800 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001801 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001802
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001803 /* If "the current_rule_list" match the executed rule list, we are in
1804 * resume condition. If a resume is needed it is always in the action
1805 * and never in the ACL or converters. In this case, we initialise the
1806 * current rule, and go to the action execution point.
1807 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001808 if (s->current_rule) {
1809 rule = s->current_rule;
1810 s->current_rule = NULL;
1811 if (s->current_rule_list == rules)
1812 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001813 }
1814 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001815
Willy Tarreauff011f22011-01-06 17:51:27 +01001816 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001817
Willy Tarreau96257ec2012-12-27 10:46:37 +01001818 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001819 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001820 int ret;
1821
Willy Tarreau192252e2015-04-04 01:47:55 +02001822 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001823 ret = acl_pass(ret);
1824
Willy Tarreauff011f22011-01-06 17:51:27 +01001825 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001826 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001827
1828 if (!ret) /* condition not matched */
1829 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001830 }
1831
Willy Tarreauacc98002015-09-27 23:34:39 +02001832 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001833resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001834 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001835 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001836 rule_ret = HTTP_RULE_RES_STOP;
1837 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001838
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001839 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001840 if (deny_status)
1841 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001842 rule_ret = HTTP_RULE_RES_DENY;
1843 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001844
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001845 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001846 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001847 if (deny_status)
1848 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001849 rule_ret = HTTP_RULE_RES_DENY;
1850 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001851
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001852 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001853 /* Be sure to send any pending HTTP 103 response first */
1854 if (early_hints) {
1855 http_send_early_hints(s, early_hints);
1856 free_trash_chunk(early_hints);
1857 early_hints = NULL;
1858 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001859 /* Auth might be performed on regular http-req rules as well as on stats */
1860 auth_realm = rule->arg.auth.realm;
1861 if (!auth_realm) {
1862 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1863 auth_realm = STATS_DEFAULT_REALM;
1864 else
1865 auth_realm = px->id;
1866 }
1867 /* send 401/407 depending on whether we use a proxy or not. We still
1868 * count one error, because normal browsing won't significantly
1869 * increase the counter but brute force attempts will.
1870 */
1871 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1872 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001873 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001874 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001875 rule_ret = HTTP_RULE_RES_ABRT;
1876 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001877
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001878 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001879 /* Be sure to send any pending HTTP 103 response first */
1880 if (early_hints) {
1881 http_send_early_hints(s, early_hints);
1882 free_trash_chunk(early_hints);
1883 early_hints = NULL;
1884 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001885 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001886 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001887 rule_ret = HTTP_RULE_RES_BADREQ;
1888 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001889
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001890 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001891 s->task->nice = rule->arg.nice;
1892 break;
1893
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001894 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001895 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001896 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001897 break;
1898
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001899 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001900#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001901 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001902 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001903#endif
1904 break;
1905
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001906 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001907 s->logs.level = rule->arg.loglevel;
1908 break;
1909
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001910 case ACT_HTTP_REPLACE_HDR:
1911 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001912 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1913 rule->arg.hdr_add.name_len,
1914 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001915 &rule->arg.hdr_add.re, rule->action)) {
1916 rule_ret = HTTP_RULE_RES_BADREQ;
1917 goto end;
1918 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001919 break;
1920
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001921 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001922 ctx.idx = 0;
1923 /* remove all occurrences of the header */
1924 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001925 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001926 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001927 }
Willy Tarreau85603282015-01-21 20:39:27 +01001928 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001929
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001930 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001931 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001932 /* The scope of the trash buffer must be limited to this function. The
1933 * build_logline() function can execute a lot of other function which
1934 * can use the trash buffer. So for limiting the scope of this global
1935 * buffer, we build first the header value using build_logline, and
1936 * after we store the header name.
1937 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001938 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001939
1940 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001941 if (!replace) {
1942 rule_ret = HTTP_RULE_RES_BADREQ;
1943 goto end;
1944 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001945
Thierry Fournier4b788f72016-06-01 13:35:36 +02001946 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001947 len += build_logline(s, replace->area + len,
1948 replace->size - len,
1949 &rule->arg.hdr_add.fmt);
1950 memcpy(replace->area, rule->arg.hdr_add.name,
1951 rule->arg.hdr_add.name_len);
1952 replace->area[rule->arg.hdr_add.name_len] = ':';
1953 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1954 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001955
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001956 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001957 /* remove all occurrences of the header */
1958 ctx.idx = 0;
1959 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001960 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001961 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1962 }
1963 }
1964
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001965 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001966 static unsigned char rate_limit = 0;
1967
1968 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001969 replace->area[rule->arg.hdr_add.name_len] = 0;
1970 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,
1971 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001972 }
1973
1974 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1975 if (sess->fe != s->be)
1976 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1977 if (sess->listener->counters)
1978 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1979 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001980
1981 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001982 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001983 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001984
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001985 case ACT_HTTP_DEL_ACL:
1986 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001987 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001988 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001989
1990 /* collect reference */
1991 ref = pat_ref_lookup(rule->arg.map.ref);
1992 if (!ref)
1993 continue;
1994
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001995 /* allocate key */
1996 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001997 if (!key) {
1998 rule_ret = HTTP_RULE_RES_BADREQ;
1999 goto end;
2000 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002001
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002002 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002003 key->data = build_logline(s, key->area, key->size,
2004 &rule->arg.map.key);
2005 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002006
2007 /* perform update */
2008 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002009 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002010 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002011 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002012
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002013 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002014 break;
2015 }
2016
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002017 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002018 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002019 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002020
2021 /* collect reference */
2022 ref = pat_ref_lookup(rule->arg.map.ref);
2023 if (!ref)
2024 continue;
2025
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002026 /* allocate key */
2027 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002028 if (!key) {
2029 rule_ret = HTTP_RULE_RES_BADREQ;
2030 goto end;
2031 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002032
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002033 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002034 key->data = build_logline(s, key->area, key->size,
2035 &rule->arg.map.key);
2036 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002037
2038 /* perform update */
2039 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002040 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002041 if (pat_ref_find_elt(ref, key->area) == NULL)
2042 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002043 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002044
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002045 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002046 break;
2047 }
2048
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002049 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002050 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002051 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002052
2053 /* collect reference */
2054 ref = pat_ref_lookup(rule->arg.map.ref);
2055 if (!ref)
2056 continue;
2057
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002058 /* allocate key */
2059 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002060 if (!key) {
2061 rule_ret = HTTP_RULE_RES_BADREQ;
2062 goto end;
2063 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002064
2065 /* allocate value */
2066 value = alloc_trash_chunk();
2067 if (!value) {
2068 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01002069 rule_ret = HTTP_RULE_RES_BADREQ;
2070 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002071 }
2072
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002073 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002074 key->data = build_logline(s, key->area, key->size,
2075 &rule->arg.map.key);
2076 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002077
2078 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002079 value->data = build_logline(s, value->area,
2080 value->size,
2081 &rule->arg.map.value);
2082 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002083
2084 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002085 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002086 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002087 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002088 else
2089 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002090 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002091
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002092 free_trash_chunk(key);
2093 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002094 break;
2095 }
William Lallemand73025dd2014-04-24 14:38:37 +02002096
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002097 case ACT_HTTP_EARLY_HINT:
2098 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2099 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002100 early_hints = http_apply_early_hint_rule(s, early_hints,
2101 rule->arg.early_hint.name,
2102 rule->arg.early_hint.name_len,
2103 &rule->arg.early_hint.fmt);
2104 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01002105 rule_ret = HTTP_RULE_RES_DONE;
2106 goto end;
2107 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002108 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002109 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002110 if ((s->req.flags & CF_READ_ERROR) ||
2111 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2112 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2113 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002114 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002115
Willy Tarreauacc98002015-09-27 23:34:39 +02002116 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002117 case ACT_RET_ERR:
2118 case ACT_RET_CONT:
2119 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002120 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01002121 rule_ret = HTTP_RULE_RES_DONE;
2122 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002123 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002124 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01002125 rule_ret = HTTP_RULE_RES_YIELD;
2126 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002127 }
William Lallemand73025dd2014-04-24 14:38:37 +02002128 break;
2129
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002130 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002131 /* Note: only the first valid tracking parameter of each
2132 * applies.
2133 */
2134
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002135 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002136 struct stktable *t;
2137 struct stksess *ts;
2138 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002139 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002140
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002141 t = rule->arg.trk_ctr.table.t;
2142 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 +02002143
2144 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002145 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002146
2147 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002148 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2149 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2150 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002151 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002152
Emeric Brun819fc6f2017-06-13 19:37:32 +02002153 if (ptr1)
2154 stktable_data_cast(ptr1, http_req_cnt)++;
2155
2156 if (ptr2)
2157 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2158 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2159
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002160 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002161
2162 /* If data was modified, we need to touch to re-schedule sync */
2163 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002164 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002165
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002166 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002167 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002168 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002169 }
2170 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002171 break;
2172
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002173 /* other flags exists, but normaly, they never be matched. */
2174 default:
2175 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002176 }
2177 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002178
Christopher Fauletea827bd2018-11-15 15:34:11 +01002179 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002180 if (early_hints) {
2181 http_send_early_hints(s, early_hints);
2182 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002183 }
2184
Willy Tarreau96257ec2012-12-27 10:46:37 +01002185 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01002186 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002187}
2188
Willy Tarreau71241ab2012-12-27 11:30:54 +01002189
Willy Tarreau51d861a2015-05-22 17:30:48 +02002190/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2191 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2192 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2193 * is returned, the process can continue the evaluation of next rule list. If
2194 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2195 * is returned, it means the operation could not be processed and a server error
2196 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2197 * deny rule. If *YIELD is returned, the caller must call again the function
2198 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002199 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002200enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002201http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002202{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002203 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002204 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002205 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002206 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002207 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002208 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02002209 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002210
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002211 /* If "the current_rule_list" match the executed rule list, we are in
2212 * resume condition. If a resume is needed it is always in the action
2213 * and never in the ACL or converters. In this case, we initialise the
2214 * current rule, and go to the action execution point.
2215 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002216 if (s->current_rule) {
2217 rule = s->current_rule;
2218 s->current_rule = NULL;
2219 if (s->current_rule_list == rules)
2220 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002221 }
2222 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002223
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002224 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002225
2226 /* check optional condition */
2227 if (rule->cond) {
2228 int ret;
2229
Willy Tarreau192252e2015-04-04 01:47:55 +02002230 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002231 ret = acl_pass(ret);
2232
2233 if (rule->cond->pol == ACL_COND_UNLESS)
2234 ret = !ret;
2235
2236 if (!ret) /* condition not matched */
2237 continue;
2238 }
2239
Willy Tarreauacc98002015-09-27 23:34:39 +02002240 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002241resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002242 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002243 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002244 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2245 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002246
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002247 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002248 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002249 rule_ret = HTTP_RULE_RES_STOP;
2250 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002251
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002252 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002253 s->task->nice = rule->arg.nice;
2254 break;
2255
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002256 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002257 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002258 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002259 break;
2260
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002261 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002262#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002263 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002264 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002265#endif
2266 break;
2267
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002268 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002269 s->logs.level = rule->arg.loglevel;
2270 break;
2271
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002272 case ACT_HTTP_REPLACE_HDR:
2273 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002274 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2275 rule->arg.hdr_add.name_len,
2276 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002277 &rule->arg.hdr_add.re, rule->action)) {
2278 rule_ret = HTTP_RULE_RES_BADREQ;
2279 goto end;
2280 }
Sasha Pachev218f0642014-06-16 12:05:59 -06002281 break;
2282
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002283 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002284 ctx.idx = 0;
2285 /* remove all occurrences of the header */
2286 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002287 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002288 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2289 }
Willy Tarreau85603282015-01-21 20:39:27 +01002290 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002291
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002292 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002293 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002294 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002295
2296 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002297 if (!replace) {
2298 rule_ret = HTTP_RULE_RES_BADREQ;
2299 goto end;
2300 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002301
2302 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002303 memcpy(replace->area, rule->arg.hdr_add.name,
2304 rule->arg.hdr_add.name_len);
2305 replace->data = rule->arg.hdr_add.name_len;
2306 replace->area[replace->data++] = ':';
2307 replace->area[replace->data++] = ' ';
2308 replace->data += build_logline(s,
2309 replace->area + replace->data,
2310 replace->size - replace->data,
2311 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002312
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002313 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002314 /* remove all occurrences of the header */
2315 ctx.idx = 0;
2316 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002317 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002318 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2319 }
2320 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002321
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002322 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002323 static unsigned char rate_limit = 0;
2324
2325 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002326 replace->area[rule->arg.hdr_add.name_len] = 0;
2327 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,
2328 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002329 }
2330
2331 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2332 if (sess->fe != s->be)
2333 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2334 if (sess->listener->counters)
2335 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2336 if (objt_server(s->target))
2337 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2338 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002339
2340 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002341 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002342 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002343
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002344 case ACT_HTTP_DEL_ACL:
2345 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002346 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002347 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002348
2349 /* collect reference */
2350 ref = pat_ref_lookup(rule->arg.map.ref);
2351 if (!ref)
2352 continue;
2353
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002354 /* allocate key */
2355 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002356 if (!key) {
2357 rule_ret = HTTP_RULE_RES_BADREQ;
2358 goto end;
2359 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002360
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002361 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002362 key->data = build_logline(s, key->area, key->size,
2363 &rule->arg.map.key);
2364 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002365
2366 /* perform update */
2367 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002368 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002369 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002370 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002371
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002372 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002373 break;
2374 }
2375
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002376 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002377 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002378 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002379
2380 /* collect reference */
2381 ref = pat_ref_lookup(rule->arg.map.ref);
2382 if (!ref)
2383 continue;
2384
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002385 /* allocate key */
2386 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002387 if (!key) {
2388 rule_ret = HTTP_RULE_RES_BADREQ;
2389 goto end;
2390 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002391
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002392 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002393 key->data = build_logline(s, key->area, key->size,
2394 &rule->arg.map.key);
2395 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002396
2397 /* perform update */
2398 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002399 if (pat_ref_find_elt(ref, key->area) == NULL)
2400 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002401
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002402 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002403 break;
2404 }
2405
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002406 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002407 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002408 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002409
2410 /* collect reference */
2411 ref = pat_ref_lookup(rule->arg.map.ref);
2412 if (!ref)
2413 continue;
2414
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002415 /* allocate key */
2416 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002417 if (!key) {
2418 rule_ret = HTTP_RULE_RES_BADREQ;
2419 goto end;
2420 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002421
2422 /* allocate value */
2423 value = alloc_trash_chunk();
2424 if (!value) {
2425 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002426 rule_ret = HTTP_RULE_RES_BADREQ;
2427 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002428 }
2429
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002430 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002431 key->data = build_logline(s, key->area, key->size,
2432 &rule->arg.map.key);
2433 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002434
2435 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002436 value->data = build_logline(s, value->area,
2437 value->size,
2438 &rule->arg.map.value);
2439 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002440
2441 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002442 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002443 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002444 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002445 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002446 else
2447 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002448 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002449 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002450 free_trash_chunk(key);
2451 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002452 break;
2453 }
William Lallemand73025dd2014-04-24 14:38:37 +02002454
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002455 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002456 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002457 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002458 rule_ret = HTTP_RULE_RES_BADREQ;
2459 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002460
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002461 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2462 /* Note: only the first valid tracking parameter of each
2463 * applies.
2464 */
2465
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002466 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002467 struct stktable *t;
2468 struct stksess *ts;
2469 struct stktable_key *key;
2470 void *ptr;
2471
2472 t = rule->arg.trk_ctr.table.t;
2473 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2474
2475 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002476 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002477
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002478 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002479
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002480 /* let's count a new HTTP request as it's the first time we do it */
2481 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2482 if (ptr)
2483 stktable_data_cast(ptr, http_req_cnt)++;
2484
2485 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2486 if (ptr)
2487 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2488 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2489
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002490 /* When the client triggers a 4xx from the server, it's most often due
2491 * to a missing object or permission. These events should be tracked
2492 * because if they happen often, it may indicate a brute force or a
2493 * vulnerability scan. Normally this is done when receiving the response
2494 * but here we're tracking after this ought to have been done so we have
2495 * to do it on purpose.
2496 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002497 if ((unsigned)(txn->status - 400) < 100) {
2498 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2499 if (ptr)
2500 stktable_data_cast(ptr, http_err_cnt)++;
2501
2502 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2503 if (ptr)
2504 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2505 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2506 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002507
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002508 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002509
Emeric Brun0fed0b02017-11-29 16:15:07 +01002510 /* If data was modified, we need to touch to re-schedule sync */
2511 stktable_touch_local(t, ts, 0);
2512
Emeric Brun819fc6f2017-06-13 19:37:32 +02002513 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2514 if (sess->fe != s->be)
2515 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2516
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002517 }
2518 }
2519 break;
2520
Thierry FOURNIER42148732015-09-02 17:17:33 +02002521 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002522 if ((s->req.flags & CF_READ_ERROR) ||
2523 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2524 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2525 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002526 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002527
Willy Tarreauacc98002015-09-27 23:34:39 +02002528 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002529 case ACT_RET_ERR:
2530 case ACT_RET_CONT:
2531 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002532 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002533 rule_ret = HTTP_RULE_RES_STOP;
2534 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002535 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002536 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002537 rule_ret = HTTP_RULE_RES_YIELD;
2538 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002539 }
William Lallemand73025dd2014-04-24 14:38:37 +02002540 break;
2541
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002542 /* other flags exists, but normaly, they never be matched. */
2543 default:
2544 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002545 }
2546 }
2547
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002548 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002549 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002550 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002551}
2552
2553
Willy Tarreau71241ab2012-12-27 11:30:54 +01002554/* Perform an HTTP redirect based on the information in <rule>. The function
2555 * returns non-zero on success, or zero in case of a, irrecoverable error such
2556 * as too large a request to build a valid response.
2557 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002558int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002559{
Willy Tarreaub329a312015-05-22 16:27:37 +02002560 struct http_msg *req = &txn->req;
2561 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002562 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002563 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002564 int ret = 0;
2565
2566 chunk = alloc_trash_chunk();
2567 if (!chunk)
2568 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002569
2570 /* build redirect message */
2571 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002572 case 308:
2573 msg_fmt = HTTP_308;
2574 break;
2575 case 307:
2576 msg_fmt = HTTP_307;
2577 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002578 case 303:
2579 msg_fmt = HTTP_303;
2580 break;
2581 case 301:
2582 msg_fmt = HTTP_301;
2583 break;
2584 case 302:
2585 default:
2586 msg_fmt = HTTP_302;
2587 break;
2588 }
2589
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002590 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2591 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002592
2593 switch(rule->type) {
2594 case REDIRECT_TYPE_SCHEME: {
2595 const char *path;
2596 const char *host;
2597 struct hdr_ctx ctx;
2598 int pathlen;
2599 int hostlen;
2600
2601 host = "";
2602 hostlen = 0;
2603 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002604 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002605 host = ctx.line + ctx.val;
2606 hostlen = ctx.vlen;
2607 }
2608
Willy Tarreau6b952c82018-09-10 17:45:34 +02002609 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002610 /* build message using path */
2611 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002612 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002613 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2614 int qs = 0;
2615 while (qs < pathlen) {
2616 if (path[qs] == '?') {
2617 pathlen = qs;
2618 break;
2619 }
2620 qs++;
2621 }
2622 }
2623 } else {
2624 path = "/";
2625 pathlen = 1;
2626 }
2627
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002628 if (rule->rdr_str) { /* this is an old "redirect" rule */
2629 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002630 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002631 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002632
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002633 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002634 memcpy(chunk->area + chunk->data, rule->rdr_str,
2635 rule->rdr_len);
2636 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002637 }
2638 else {
2639 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002640 chunk->data += build_logline(s,
2641 chunk->area + chunk->data,
2642 chunk->size - chunk->data,
2643 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002644
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002645 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002646 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002647 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002648 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002649 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002650 memcpy(chunk->area + chunk->data, "://", 3);
2651 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002652
2653 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002654 memcpy(chunk->area + chunk->data, host, hostlen);
2655 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002656
2657 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002658 memcpy(chunk->area + chunk->data, path, pathlen);
2659 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002660
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002661 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002662 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002663 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002664 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002665 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002666 chunk->area[chunk->data] = '/';
2667 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002668 }
2669
2670 break;
2671 }
2672 case REDIRECT_TYPE_PREFIX: {
2673 const char *path;
2674 int pathlen;
2675
Willy Tarreau6b952c82018-09-10 17:45:34 +02002676 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002677 /* build message using path */
2678 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002679 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002680 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2681 int qs = 0;
2682 while (qs < pathlen) {
2683 if (path[qs] == '?') {
2684 pathlen = qs;
2685 break;
2686 }
2687 qs++;
2688 }
2689 }
2690 } else {
2691 path = "/";
2692 pathlen = 1;
2693 }
2694
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002695 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002696 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002697 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002698
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002699 /* add prefix. Note that if prefix == "/", we don't want to
2700 * add anything, otherwise it makes it hard for the user to
2701 * configure a self-redirection.
2702 */
2703 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002704 memcpy(chunk->area + chunk->data,
2705 rule->rdr_str, rule->rdr_len);
2706 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002707 }
2708 }
2709 else {
2710 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002711 chunk->data += build_logline(s,
2712 chunk->area + chunk->data,
2713 chunk->size - chunk->data,
2714 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002715
2716 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002717 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002718 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002719 }
2720
2721 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002722 memcpy(chunk->area + chunk->data, path, pathlen);
2723 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002724
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002725 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002726 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002727 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002728 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002729 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002730 chunk->area[chunk->data] = '/';
2731 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002732 }
2733
2734 break;
2735 }
2736 case REDIRECT_TYPE_LOCATION:
2737 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002738 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002739 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002740 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002741
2742 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002743 memcpy(chunk->area + chunk->data, rule->rdr_str,
2744 rule->rdr_len);
2745 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002746 }
2747 else {
2748 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002749 chunk->data += build_logline(s,
2750 chunk->area + chunk->data,
2751 chunk->size - chunk->data,
2752 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002753
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002754 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002755 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002756 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002757 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002758 break;
2759 }
2760
2761 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002762 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2763 chunk->data += 14;
2764 memcpy(chunk->area + chunk->data, rule->cookie_str,
2765 rule->cookie_len);
2766 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002767 }
2768
Willy Tarreau19b14122017-02-28 09:48:11 +01002769 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002770 txn->status = rule->code;
2771 /* let's log the request time */
2772 s->logs.tv_request = now;
2773
Christopher Fauletbe821b92017-03-30 11:21:53 +02002774 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002775 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2776 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2777 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002778 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002779 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002780 memcpy(chunk->area + chunk->data,
2781 "\r\nProxy-Connection: keep-alive", 30);
2782 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002783 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002784 memcpy(chunk->area + chunk->data,
2785 "\r\nConnection: keep-alive", 24);
2786 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002787 }
2788 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002789 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2790 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002791 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002792 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002793 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002794 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002795 req->next -= req->sov;
2796 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002797 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002798 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002799 req->msg_state = HTTP_MSG_CLOSED;
2800 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002801 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002802 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002803 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002804 /* let the server side turn to SI_ST_CLO */
2805 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002806 } else {
2807 /* keep-alive not possible */
2808 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002809 memcpy(chunk->area + chunk->data,
2810 "\r\nProxy-Connection: close\r\n\r\n", 29);
2811 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002812 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002813 memcpy(chunk->area + chunk->data,
2814 "\r\nConnection: close\r\n\r\n", 23);
2815 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002816 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002817 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002818 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002819 }
2820
Willy Tarreaue7dff022015-04-03 01:14:29 +02002821 if (!(s->flags & SF_ERR_MASK))
2822 s->flags |= SF_ERR_LOCAL;
2823 if (!(s->flags & SF_FINST_MASK))
2824 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002825
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002826 ret = 1;
2827 leave:
2828 free_trash_chunk(chunk);
2829 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002830}
2831
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002832/* This stream analyser runs all HTTP request processing which is common to
2833 * frontends and backends, which means blocking ACLs, filters, connection-close,
2834 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002835 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002836 * either needs more data or wants to immediately abort the request (eg: deny,
2837 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002838 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002839int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002840{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002841 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002842 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002843 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002844 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002845 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002846 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002847 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002848 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002849
Christopher Faulete0768eb2018-10-03 16:38:02 +02002850 if (IS_HTX_STRM(s))
2851 return htx_process_req_common(s, req, an_bit, px);
2852
Willy Tarreau655dce92009-11-08 13:10:58 +01002853 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002854 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002855 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002856 }
2857
Christopher Faulet45073512018-07-20 10:16:29 +02002858 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 +02002859 now_ms, __FUNCTION__,
2860 s,
2861 req,
2862 req->rex, req->wex,
2863 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002864 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002865 req->analysers);
2866
Willy Tarreau65410832014-04-28 21:25:43 +02002867 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002868 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002869
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002870 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002871 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002872 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002873
Willy Tarreau0b748332014-04-29 00:13:29 +02002874 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002875 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2876 goto return_prx_yield;
2877
Willy Tarreau0b748332014-04-29 00:13:29 +02002878 case HTTP_RULE_RES_CONT:
2879 case HTTP_RULE_RES_STOP: /* nothing to do */
2880 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002881
Willy Tarreau0b748332014-04-29 00:13:29 +02002882 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2883 if (txn->flags & TX_CLTARPIT)
2884 goto tarpit;
2885 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002886
Willy Tarreau0b748332014-04-29 00:13:29 +02002887 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2888 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002889
Willy Tarreau0b748332014-04-29 00:13:29 +02002890 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002891 goto done;
2892
Willy Tarreau0b748332014-04-29 00:13:29 +02002893 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2894 goto return_bad_req;
2895 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002896 }
2897
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002898 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2899 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002900 struct hdr_ctx ctx;
2901
2902 ctx.idx = 0;
2903 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002904 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002905 if (unlikely(http_header_add_tail2(&txn->req,
2906 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002907 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002908 goto return_bad_req;
2909 }
2910 }
2911
2912 }
2913
Willy Tarreau52542592014-04-28 18:33:26 +02002914 /* OK at this stage, we know that the request was accepted according to
2915 * the http-request rules, we can check for the stats. Note that the
2916 * URI is detected *before* the req* rules in order not to be affected
2917 * by a possible reqrep, while they are processed *after* so that a
2918 * reqdeny can still block them. This clearly needs to change in 1.6!
2919 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002920 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002921 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002922 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002923 txn->status = 500;
2924 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002925 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002926
Willy Tarreaue7dff022015-04-03 01:14:29 +02002927 if (!(s->flags & SF_ERR_MASK))
2928 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002929 goto return_prx_cond;
2930 }
2931
2932 /* parse the whole stats request and extract the relevant information */
2933 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002934 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002935 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002936
Willy Tarreau0b748332014-04-29 00:13:29 +02002937 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2938 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002939
Willy Tarreau0b748332014-04-29 00:13:29 +02002940 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2941 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002942 }
2943
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002944 /* evaluate the req* rules except reqadd */
2945 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002946 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002947 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002948
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002949 if (txn->flags & TX_CLDENY)
2950 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002951
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002952 if (txn->flags & TX_CLTARPIT) {
2953 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002954 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002955 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002956 }
Willy Tarreau06619262006-12-17 08:37:22 +01002957
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002958 /* add request headers from the rule sets in the same order */
2959 list_for_each_entry(wl, &px->req_add, list) {
2960 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002961 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002962 ret = acl_pass(ret);
2963 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2964 ret = !ret;
2965 if (!ret)
2966 continue;
2967 }
2968
Christopher Faulet10079f52018-10-03 15:17:28 +02002969 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002970 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002971 }
2972
Willy Tarreau52542592014-04-28 18:33:26 +02002973
2974 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002975 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2976 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002977 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002978 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002979 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002980
Willy Tarreaue7dff022015-04-03 01:14:29 +02002981 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2982 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2983 if (!(s->flags & SF_FINST_MASK))
2984 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002985
Willy Tarreau70730dd2014-04-24 18:06:27 +02002986 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002987 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2988 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002989 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002990 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002991 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002992
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002993 /* check whether we have some ACLs set to redirect this request */
2994 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002995 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002996 int ret;
2997
Willy Tarreau192252e2015-04-04 01:47:55 +02002998 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002999 ret = acl_pass(ret);
3000 if (rule->cond->pol == ACL_COND_UNLESS)
3001 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003002 if (!ret)
3003 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003004 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003005 if (!http_apply_redirect_rule(rule, s, txn))
3006 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003007 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003008 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003009
Willy Tarreau2be39392010-01-03 17:24:51 +01003010 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3011 * If this happens, then the data will not come immediately, so we must
3012 * send all what we have without waiting. Note that due to the small gain
3013 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003014 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003015 * itself once used.
3016 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003017 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003018
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003019 done: /* done with this analyser, continue with next ones that the calling
3020 * points will have set, if any.
3021 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003022 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003023 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3024 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003025 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003026
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003027 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003028 /* Allow cookie logging
3029 */
3030 if (s->be->cookie_name || sess->fe->capture_name)
3031 manage_client_side_cookies(s, req);
3032
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003033 /* When a connection is tarpitted, we use the tarpit timeout,
3034 * which may be the same as the connect timeout if unspecified.
3035 * If unset, then set it to zero because we really want it to
3036 * eventually expire. We build the tarpit as an analyser.
3037 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003038 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003039
3040 /* wipe the request out so that we can drop the connection early
3041 * if the client closes first.
3042 */
3043 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003044
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003045 txn->status = http_err_codes[deny_status];
3046
Christopher Faulet0184ea72017-01-05 14:06:34 +01003047 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003048 req->analysers |= AN_REQ_HTTP_TARPIT;
3049 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3050 if (!req->analyse_exp)
3051 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003052 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003053 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003054 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003055 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003056 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003057 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003058 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003059
3060 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003061
3062 /* Allow cookie logging
3063 */
3064 if (s->be->cookie_name || sess->fe->capture_name)
3065 manage_client_side_cookies(s, req);
3066
Willy Tarreau0b748332014-04-29 00:13:29 +02003067 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003068 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003069 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003070 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003071 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003072 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003073 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003074 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003075 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003076 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003077 goto return_prx_cond;
3078
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003079 return_bad_req:
3080 /* We centralize bad requests processing here */
3081 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3082 /* we detected a parsing error. We want to archive this request
3083 * in the dedicated proxy area for later troubleshooting.
3084 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003085 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003086 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003087
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003088 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003089 txn->req.msg_state = HTTP_MSG_ERROR;
3090 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003091 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003092
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003093 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003094 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003095 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003096
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003097 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003098 if (!(s->flags & SF_ERR_MASK))
3099 s->flags |= SF_ERR_PRXCOND;
3100 if (!(s->flags & SF_FINST_MASK))
3101 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003102
Christopher Faulet0184ea72017-01-05 14:06:34 +01003103 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003104 req->analyse_exp = TICK_ETERNITY;
3105 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003106
3107 return_prx_yield:
3108 channel_dont_connect(req);
3109 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003110}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003111
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003112/* This function performs all the processing enabled for the current request.
3113 * It returns 1 if the processing can continue on next analysers, or zero if it
3114 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003115 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003116 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003117int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003118{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003119 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003120 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003121 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003122 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003123
Christopher Faulete0768eb2018-10-03 16:38:02 +02003124 if (IS_HTX_STRM(s))
3125 return htx_process_request(s, req, an_bit);
3126
Willy Tarreau655dce92009-11-08 13:10:58 +01003127 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003128 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003129 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003130 return 0;
3131 }
3132
Christopher Faulet45073512018-07-20 10:16:29 +02003133 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 +02003134 now_ms, __FUNCTION__,
3135 s,
3136 req,
3137 req->rex, req->wex,
3138 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003139 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003140 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003141
Willy Tarreau59234e92008-11-30 23:51:27 +01003142 /*
3143 * Right now, we know that we have processed the entire headers
3144 * and that unwanted requests have been filtered out. We can do
3145 * whatever we want with the remaining request. Also, now we
3146 * may have separate values for ->fe, ->be.
3147 */
Willy Tarreau06619262006-12-17 08:37:22 +01003148
Willy Tarreau59234e92008-11-30 23:51:27 +01003149 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003150 * If HTTP PROXY is set we simply get remote server address parsing
3151 * incoming request. Note that this requires that a connection is
3152 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003153 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003154 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003155 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003156 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003157
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003158 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003159 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003160 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003161 txn->req.msg_state = HTTP_MSG_ERROR;
3162 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003163 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003164 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003165
Willy Tarreaue7dff022015-04-03 01:14:29 +02003166 if (!(s->flags & SF_ERR_MASK))
3167 s->flags |= SF_ERR_RESOURCE;
3168 if (!(s->flags & SF_FINST_MASK))
3169 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003170
3171 return 0;
3172 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003173
Willy Tarreau6b952c82018-09-10 17:45:34 +02003174 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003175 if (url2sa(ci_head(req) + msg->sl.rq.u,
3176 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003177 &conn->addr.to, NULL) == -1)
3178 goto return_bad_req;
3179
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003180 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003181 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3182 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003183 * u_l characters by a single "/".
3184 */
3185 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003186 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003187 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3188 int delta;
3189
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003190 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003191 http_msg_move_end(&txn->req, delta);
3192 cur_end += delta;
3193 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3194 goto return_bad_req;
3195 }
3196 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003197 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003198 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3199 int delta;
3200
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003201 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003202 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003203 http_msg_move_end(&txn->req, delta);
3204 cur_end += delta;
3205 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3206 goto return_bad_req;
3207 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003208 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003209
Willy Tarreau59234e92008-11-30 23:51:27 +01003210 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003211 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003212 * Note that doing so might move headers in the request, but
3213 * the fields will stay coherent and the URI will not move.
3214 * This should only be performed in the backend.
3215 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003216 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003217 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003218
William Lallemanda73203e2012-03-12 12:48:57 +01003219 /* add unique-id if "header-unique-id" is specified */
3220
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003221 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003222 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003223 goto return_bad_req;
3224 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003225 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003226 }
William Lallemanda73203e2012-03-12 12:48:57 +01003227
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003228 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003229 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003230 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003231 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003232 goto return_bad_req;
3233 }
3234
Cyril Bontéb21570a2009-11-29 20:04:48 +01003235 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003236 * 9: add X-Forwarded-For if either the frontend or the backend
3237 * asks for it.
3238 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003239 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003240 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003241 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3242 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3243 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003244 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003245 /* The header is set to be added only if none is present
3246 * and we found it, so don't do anything.
3247 */
3248 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003249 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003250 /* Add an X-Forwarded-For header unless the source IP is
3251 * in the 'except' network range.
3252 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003253 if ((!sess->fe->except_mask.s_addr ||
3254 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3255 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003256 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003257 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003258 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003259 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003260 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003261 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003262
3263 /* Note: we rely on the backend to get the header name to be used for
3264 * x-forwarded-for, because the header is really meant for the backends.
3265 * However, if the backend did not specify any option, we have to rely
3266 * on the frontend's header name.
3267 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003268 if (s->be->fwdfor_hdr_len) {
3269 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003270 memcpy(trash.area,
3271 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003272 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003273 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003274 memcpy(trash.area,
3275 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003276 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003277 len += snprintf(trash.area + len,
3278 trash.size - len,
3279 ": %d.%d.%d.%d", pn[0], pn[1],
3280 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003281
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003282 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003283 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003284 }
3285 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003286 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003287 /* FIXME: for the sake of completeness, we should also support
3288 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003289 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003290 int len;
3291 char pn[INET6_ADDRSTRLEN];
3292 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003293 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003294 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003295
Willy Tarreau59234e92008-11-30 23:51:27 +01003296 /* Note: we rely on the backend to get the header name to be used for
3297 * x-forwarded-for, because the header is really meant for the backends.
3298 * However, if the backend did not specify any option, we have to rely
3299 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003300 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003301 if (s->be->fwdfor_hdr_len) {
3302 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003303 memcpy(trash.area, s->be->fwdfor_hdr_name,
3304 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003305 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003306 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003307 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3308 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003309 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003310 len += snprintf(trash.area + len, trash.size - len,
3311 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003312
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003313 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003314 goto return_bad_req;
3315 }
3316 }
3317
3318 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003319 * 10: add X-Original-To if either the frontend or the backend
3320 * asks for it.
3321 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003322 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003323
3324 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003325 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003326 /* Add an X-Original-To header unless the destination IP is
3327 * in the 'except' network range.
3328 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003329 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003330
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003331 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003332 ((!sess->fe->except_mask_to.s_addr ||
3333 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3334 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003335 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003336 (((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 +02003337 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003338 int len;
3339 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003340 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003341
3342 /* Note: we rely on the backend to get the header name to be used for
3343 * x-original-to, because the header is really meant for the backends.
3344 * However, if the backend did not specify any option, we have to rely
3345 * on the frontend's header name.
3346 */
3347 if (s->be->orgto_hdr_len) {
3348 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003349 memcpy(trash.area,
3350 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003351 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003352 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003353 memcpy(trash.area,
3354 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003355 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003356 len += snprintf(trash.area + len,
3357 trash.size - len,
3358 ": %d.%d.%d.%d", pn[0], pn[1],
3359 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003360
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003361 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003362 goto return_bad_req;
3363 }
3364 }
3365 }
3366
Willy Tarreau50fc7772012-11-11 22:19:57 +01003367 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3368 * If an "Upgrade" token is found, the header is left untouched in order not to have
3369 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3370 * "Upgrade" is present in the Connection header.
3371 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003372 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003373 unsigned int want_flags = 0;
3374
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003375 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003376 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003377 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003378 want_flags |= TX_CON_CLO_SET;
3379 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003380 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003381 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003382 want_flags |= TX_CON_KAL_SET;
3383 }
3384
3385 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003386 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003387 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003388
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003389
Willy Tarreau522d6c02009-12-06 18:49:18 +01003390 /* If we have no server assigned yet and we're balancing on url_param
3391 * with a POST request, we may be interested in checking the body for
3392 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003393 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003394 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003395 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003396 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003397 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003398 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003399 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003400
Christopher Fauletbe821b92017-03-30 11:21:53 +02003401 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3402 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003403#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003404 /* We expect some data from the client. Unless we know for sure
3405 * we already have a full request, we have to re-enable quick-ack
3406 * in case we previously disabled it, otherwise we might cause
3407 * the client to delay further data.
3408 */
3409 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3410 cli_conn && conn_ctrl_ready(cli_conn) &&
3411 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003412 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003413 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003414#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003415
Willy Tarreau59234e92008-11-30 23:51:27 +01003416 /*************************************************************
3417 * OK, that's finished for the headers. We have done what we *
3418 * could. Let's switch to the DATA state. *
3419 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003420 req->analyse_exp = TICK_ETERNITY;
3421 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003422
Willy Tarreau59234e92008-11-30 23:51:27 +01003423 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003424 /* OK let's go on with the BODY now */
3425 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003426
Willy Tarreau59234e92008-11-30 23:51:27 +01003427 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003428 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003429 /* we detected a parsing error. We want to archive this request
3430 * in the dedicated proxy area for later troubleshooting.
3431 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003432 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003433 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003434
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003435 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003436 txn->req.msg_state = HTTP_MSG_ERROR;
3437 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003438 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003439 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003440
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003441 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003442 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003443 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003444
Willy Tarreaue7dff022015-04-03 01:14:29 +02003445 if (!(s->flags & SF_ERR_MASK))
3446 s->flags |= SF_ERR_PRXCOND;
3447 if (!(s->flags & SF_FINST_MASK))
3448 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003449 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003450}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003451
Willy Tarreau60b85b02008-11-30 23:28:40 +01003452/* This function is an analyser which processes the HTTP tarpit. It always
3453 * returns zero, at the beginning because it prevents any other processing
3454 * from occurring, and at the end because it terminates the request.
3455 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003456int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003457{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003458 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003459
Christopher Faulete0768eb2018-10-03 16:38:02 +02003460 if (IS_HTX_STRM(s))
3461 return htx_process_tarpit(s, req, an_bit);
3462
Willy Tarreau60b85b02008-11-30 23:28:40 +01003463 /* This connection is being tarpitted. The CLIENT side has
3464 * already set the connect expiration date to the right
3465 * timeout. We just have to check that the client is still
3466 * there and that the timeout has not expired.
3467 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003468 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003469 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003470 !tick_is_expired(req->analyse_exp, now_ms))
3471 return 0;
3472
3473 /* We will set the queue timer to the time spent, just for
3474 * logging purposes. We fake a 500 server error, so that the
3475 * attacker will not suspect his connection has been tarpitted.
3476 * It will not cause trouble to the logs because we can exclude
3477 * the tarpitted connections by filtering on the 'PT' status flags.
3478 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003479 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3480
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003481 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003482 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003483
Christopher Faulet0184ea72017-01-05 14:06:34 +01003484 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003485 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003486
Willy Tarreaue7dff022015-04-03 01:14:29 +02003487 if (!(s->flags & SF_ERR_MASK))
3488 s->flags |= SF_ERR_PRXCOND;
3489 if (!(s->flags & SF_FINST_MASK))
3490 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003491 return 0;
3492}
3493
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003494/* This function is an analyser which waits for the HTTP request body. It waits
3495 * for either the buffer to be full, or the full advertised contents to have
3496 * reached the buffer. It must only be called after the standard HTTP request
3497 * processing has occurred, because it expects the request to be parsed and will
3498 * look for the Expect header. It may send a 100-Continue interim response. It
3499 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3500 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3501 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003502 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003503int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003504{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003505 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003506 struct http_txn *txn = s->txn;
3507 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003508
Christopher Faulete0768eb2018-10-03 16:38:02 +02003509 if (IS_HTX_STRM(s))
3510 return htx_wait_for_request_body(s, req, an_bit);
3511
Willy Tarreaud34af782008-11-30 23:36:37 +01003512 /* We have to parse the HTTP request body to find any required data.
3513 * "balance url_param check_post" should have been the only way to get
3514 * into this. We were brought here after HTTP header analysis, so all
3515 * related structures are ready.
3516 */
3517
Willy Tarreau890988f2014-04-10 11:59:33 +02003518 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3519 /* This is the first call */
3520 if (msg->msg_state < HTTP_MSG_BODY)
3521 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003522
Willy Tarreau890988f2014-04-10 11:59:33 +02003523 if (msg->msg_state < HTTP_MSG_100_SENT) {
3524 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3525 * send an HTTP/1.1 100 Continue intermediate response.
3526 */
3527 if (msg->flags & HTTP_MSGF_VER_11) {
3528 struct hdr_ctx ctx;
3529 ctx.idx = 0;
3530 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003531 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003532 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003533 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003534 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003535 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003536 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003537 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003538 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003539
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003540 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003541 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003542 * must save the body in msg->next because it survives buffer
3543 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003544 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003545 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003546
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003547 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003548 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3549 else
3550 msg->msg_state = HTTP_MSG_DATA;
3551 }
3552
Willy Tarreau890988f2014-04-10 11:59:33 +02003553 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3554 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003555 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003556 goto missing_data;
3557
3558 /* OK we have everything we need now */
3559 goto http_end;
3560 }
3561
3562 /* OK here we're parsing a chunked-encoded message */
3563
Willy Tarreau522d6c02009-12-06 18:49:18 +01003564 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003565 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003566 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003567 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003568 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003569 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003570 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003571
Willy Tarreau115acb92009-12-26 13:56:06 +01003572 if (!ret)
3573 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003574 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003575 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003576 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003577 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003578 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003579 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003580 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003581
3582 msg->chunk_len = chunk;
3583 msg->body_len += chunk;
3584
3585 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003586 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003587 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003588 }
3589
Willy Tarreaud98cf932009-12-27 22:54:55 +01003590 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003591 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3592 * for at least a whole chunk or the whole content length bytes after
3593 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003594 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003595 if (msg->msg_state == HTTP_MSG_TRAILERS)
3596 goto http_end;
3597
Willy Tarreaue115b492015-05-01 23:05:14 +02003598 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003599 goto http_end;
3600
3601 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003602 /* we get here if we need to wait for more data. If the buffer is full,
3603 * we have the maximum we can expect.
3604 */
Willy Tarreau23752332018-06-15 14:54:53 +02003605 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003606 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003607
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003608 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003609 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003610 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003611
Willy Tarreaue7dff022015-04-03 01:14:29 +02003612 if (!(s->flags & SF_ERR_MASK))
3613 s->flags |= SF_ERR_CLITO;
3614 if (!(s->flags & SF_FINST_MASK))
3615 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003616 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003617 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003618
3619 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003620 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003621 /* Not enough data. We'll re-use the http-request
3622 * timeout here. Ideally, we should set the timeout
3623 * relative to the accept() date. We just set the
3624 * request timeout once at the beginning of the
3625 * request.
3626 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003627 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003628 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003629 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003630 return 0;
3631 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003632
3633 http_end:
3634 /* The situation will not evolve, so let's give up on the analysis. */
3635 s->logs.tv_request = now; /* update the request timer to reflect full request */
3636 req->analysers &= ~an_bit;
3637 req->analyse_exp = TICK_ETERNITY;
3638 return 1;
3639
3640 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003641 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003642 txn->req.msg_state = HTTP_MSG_ERROR;
3643 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003644 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003645
Willy Tarreaue7dff022015-04-03 01:14:29 +02003646 if (!(s->flags & SF_ERR_MASK))
3647 s->flags |= SF_ERR_PRXCOND;
3648 if (!(s->flags & SF_FINST_MASK))
3649 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003650
Willy Tarreau522d6c02009-12-06 18:49:18 +01003651 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003652 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003653 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003654 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003655 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003656 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003657}
3658
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003659/* send a server's name with an outgoing request over an established connection.
3660 * Note: this function is designed to be called once the request has been scheduled
3661 * for being forwarded. This is the reason why it rewinds the buffer before
3662 * proceeding.
3663 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003664int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003665
3666 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003667 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003668 char *hdr_name = be->server_id_hdr_name;
3669 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003670 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003671 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003672 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003673
William Lallemandd9e90662012-01-30 17:27:17 +01003674 ctx.idx = 0;
3675
Willy Tarreau211cdec2014-04-17 20:18:08 +02003676 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003677 if (old_o) {
3678 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003679 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003680 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003681 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003682 }
3683
Willy Tarreauf37954d2018-06-15 18:31:02 +02003684 old_i = ci_data(chn);
3685 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 -05003686 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003687 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003688 }
3689
3690 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003691 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003692 memcpy(hdr_val, hdr_name, hdr_name_len);
3693 hdr_val += hdr_name_len;
3694 *hdr_val++ = ':';
3695 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003696 hdr_val += strlcpy2(hdr_val, srv_name,
3697 trash.area + trash.size - hdr_val);
3698 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3699 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003700
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003701 if (old_o) {
3702 /* If this was a forwarded request, we must readjust the amount of
3703 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003704 * variations. Note that the current state is >= HTTP_MSG_BODY,
3705 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003706 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003707 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003708 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003709 txn->req.next -= old_o;
3710 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003711 }
3712
Mark Lamourinec2247f02012-01-04 13:02:01 -05003713 return 0;
3714}
3715
Willy Tarreau610ecce2010-01-04 21:15:02 +01003716/* Terminate current transaction and prepare a new one. This is very tricky
3717 * right now but it works.
3718 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003719void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003720{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003721 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003722 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003723 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003724 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003725 struct connection *srv_conn;
3726 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003727 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003728
Willy Tarreau610ecce2010-01-04 21:15:02 +01003729 /* FIXME: We need a more portable way of releasing a backend's and a
3730 * server's connections. We need a safer way to reinitialize buffer
3731 * flags. We also need a more accurate method for computing per-request
3732 * data.
3733 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003734 cs = objt_cs(s->si[1].end);
3735 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003736
Willy Tarreau4213a112013-12-15 10:25:42 +01003737 /* unless we're doing keep-alive, we want to quickly close the connection
3738 * to the server.
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003739 * XXX cognet: If the connection doesn't have a owner then it may not
3740 * be referenced anywhere, just kill it now, even if it could be reused.
3741 * To be revisited later when revisited later when we handle connection
3742 * pools properly.
Willy Tarreau4213a112013-12-15 10:25:42 +01003743 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003744 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003745 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003746 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3747 si_shutr(&s->si[1]);
3748 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003749 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003750
Willy Tarreaue7dff022015-04-03 01:14:29 +02003751 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003752 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003753 if (unlikely(s->srv_conn))
3754 sess_change_server(s, NULL);
3755 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003756
3757 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003758 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003759
Willy Tarreaueee5b512015-04-03 23:46:31 +02003760 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003761 int n;
3762
Willy Tarreaueee5b512015-04-03 23:46:31 +02003763 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003764 if (n < 1 || n > 5)
3765 n = 0;
3766
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003767 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003768 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003769 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003770 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003771 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003772 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3773 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003774 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003775 }
3776
3777 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003778 s->logs.bytes_in -= ci_data(&s->req);
3779 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003780
Willy Tarreau66425e32018-07-25 06:55:12 +02003781 /* we may need to know the position in the queue */
3782 pendconn_free(s);
3783
Willy Tarreau610ecce2010-01-04 21:15:02 +01003784 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003785 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003786 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003787 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003788 s->do_log(s);
3789 }
3790
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003791 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003792 stream_stop_content_counters(s);
3793 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003794
Willy Tarreau610ecce2010-01-04 21:15:02 +01003795 s->logs.accept_date = date; /* user-visible date for logging */
3796 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003797 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3798 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003799 tv_zero(&s->logs.tv_request);
3800 s->logs.t_queue = -1;
3801 s->logs.t_connect = -1;
3802 s->logs.t_data = -1;
3803 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003804 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3805 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003806
Willy Tarreauf37954d2018-06-15 18:31:02 +02003807 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3808 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003809
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003810 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003811 if (s->flags & SF_CURR_SESS) {
3812 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003813 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003814 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003815 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003816 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003817 }
3818
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003819 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003820
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003821
3822 /* If we're doing keepalive, first call the mux detach() method
3823 * to let it know we want to detach without freing the connection.
3824 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003825 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003826 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003827 !si_conn_ready(&s->si[1]) || !srv_conn->owner)
Willy Tarreau323a2d92015-08-04 19:00:17 +02003828 srv_conn = NULL;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003829 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003830
Willy Tarreau350f4872014-11-28 14:42:25 +01003831 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3832 s->si[1].err_type = SI_ET_NONE;
3833 s->si[1].conn_retries = 0; /* used for logging too */
3834 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003835 s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003836 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
Willy Tarreauede3d882018-10-24 17:17:56 +02003837 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 +02003838 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3839 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3840 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003841
Patrick Hemmere3faf022018-08-22 10:02:00 -04003842 hlua_ctx_destroy(s->hlua);
3843 s->hlua = NULL;
3844
Willy Tarreaueee5b512015-04-03 23:46:31 +02003845 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003846 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003847 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003848
3849 if (prev_status == 401 || prev_status == 407) {
3850 /* In HTTP keep-alive mode, if we receive a 401, we still have
3851 * a chance of being able to send the visitor again to the same
3852 * server over the same connection. This is required by some
3853 * broken protocols such as NTLM, and anyway whenever there is
3854 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003855 * it's better to do it (at least it helps with debugging), at
3856 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003857 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003858 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003859 }
3860
Willy Tarreau53f96852016-02-02 18:50:47 +01003861 /* Never ever allow to reuse a connection from a non-reuse backend */
3862 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3863 srv_conn->flags |= CO_FL_PRIVATE;
3864
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003865 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003866 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003867
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003868 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003869 s->req.flags |= CF_NEVER_WAIT;
3870 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003871 }
3872
Willy Tarreau714ea782015-11-25 20:11:11 +01003873 /* we're removing the analysers, we MUST re-enable events detection.
3874 * We don't enable close on the response channel since it's either
3875 * already closed, or in keep-alive with an idle connection handler.
3876 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003877 channel_auto_read(&s->req);
3878 channel_auto_close(&s->req);
3879 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003880
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003881 /* we're in keep-alive with an idle connection, monitor it if not already done */
3882 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003883 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003884 if (srv) {
3885 if (srv_conn->flags & CO_FL_PRIVATE)
3886 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3887 else if (prev_flags & TX_NOT_FIRST)
3888 /* note: we check the request, not the connection, but
3889 * this is valid for strategies SAFE and AGGR, and in
3890 * case of ALWS, we don't care anyway.
3891 */
3892 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3893 else
3894 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3895 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003896 }
Christopher Faulete6006242017-03-10 11:52:44 +01003897 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3898 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003899}
3900
3901
3902/* This function updates the request state machine according to the response
3903 * state machine and buffer flags. It returns 1 if it changes anything (flag
3904 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3905 * it is only used to find when a request/response couple is complete. Both
3906 * this function and its equivalent should loop until both return zero. It
3907 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3908 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003909int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003910{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003911 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003912 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003913 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003914 unsigned int old_state = txn->req.msg_state;
3915
Christopher Faulet4be98032017-07-18 10:48:24 +02003916 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003917 return 0;
3918
3919 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003920 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003921 * We can shut the read side unless we want to abort_on_close,
3922 * or we have a POST request. The issue with POST requests is
3923 * that some browsers still send a CRLF after the request, and
3924 * this CRLF must be read so that it does not remain in the kernel
3925 * buffers, otherwise a close could cause an RST on some systems
3926 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003927 * Note that if we're using keep-alive on the client side, we'd
3928 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003929 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003930 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003931 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003932 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3933 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003934 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3935 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003936 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003937 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003938
Willy Tarreau40f151a2012-12-20 12:10:09 +01003939 /* if the server closes the connection, we want to immediately react
3940 * and close the socket to save packets and syscalls.
3941 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003942 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003943
Willy Tarreau7f876a12015-11-18 11:59:55 +01003944 /* In any case we've finished parsing the request so we must
3945 * disable Nagle when sending data because 1) we're not going
3946 * to shut this side, and 2) the server is waiting for us to
3947 * send pending data.
3948 */
3949 chn->flags |= CF_NEVER_WAIT;
3950
Willy Tarreau610ecce2010-01-04 21:15:02 +01003951 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3952 goto wait_other_side;
3953
3954 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3955 /* The server has not finished to respond, so we
3956 * don't want to move in order not to upset it.
3957 */
3958 goto wait_other_side;
3959 }
3960
Willy Tarreau610ecce2010-01-04 21:15:02 +01003961 /* When we get here, it means that both the request and the
3962 * response have finished receiving. Depending on the connection
3963 * mode, we'll have to wait for the last bytes to leave in either
3964 * direction, and sometimes for a close to be effective.
3965 */
3966
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003967 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3968 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003969 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3970 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003971 }
3972 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3973 /* Option forceclose is set, or either side wants to close,
3974 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003975 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003976 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003977 *
3978 * However, there is an exception if the response
3979 * length is undefined. In this case, we need to wait
3980 * the close from the server. The response will be
3981 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003982 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003983 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3984 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3985 goto check_channel_flags;
3986
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003987 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3988 channel_shutr_now(chn);
3989 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003990 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003991 }
3992 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003993 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3994 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003995 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003996 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3997 channel_auto_read(chn);
3998 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003999 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004000 }
4001
Christopher Faulet4be98032017-07-18 10:48:24 +02004002 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004003 }
4004
4005 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4006 http_msg_closing:
4007 /* nothing else to forward, just waiting for the output buffer
4008 * to be empty and for the shutw_now to take effect.
4009 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004010 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004011 txn->req.msg_state = HTTP_MSG_CLOSED;
4012 goto http_msg_closed;
4013 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004014 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004015 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004016 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004017 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004018 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004019 }
4020
4021 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4022 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004023 /* if we don't know whether the server will close, we need to hard close */
4024 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4025 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4026
Willy Tarreau3988d932013-12-27 23:03:08 +01004027 /* see above in MSG_DONE why we only do this in these states */
4028 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4029 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004030 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4031 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004032 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033 goto wait_other_side;
4034 }
4035
Christopher Faulet4be98032017-07-18 10:48:24 +02004036 check_channel_flags:
4037 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4038 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4039 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004040 txn->req.msg_state = HTTP_MSG_CLOSING;
4041 goto http_msg_closing;
4042 }
4043
4044
Willy Tarreau610ecce2010-01-04 21:15:02 +01004045 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004046 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004047}
4048
4049
4050/* This function updates the response state machine according to the request
4051 * state machine and buffer flags. It returns 1 if it changes anything (flag
4052 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4053 * it is only used to find when a request/response couple is complete. Both
4054 * this function and its equivalent should loop until both return zero. It
4055 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4056 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004057int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004058{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004059 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004060 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004061 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004062 unsigned int old_state = txn->rsp.msg_state;
4063
Christopher Faulet4be98032017-07-18 10:48:24 +02004064 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004065 return 0;
4066
4067 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4068 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004069 * still monitor the server connection for a possible close
4070 * while the request is being uploaded, so we don't disable
4071 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004072 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004073 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004074
4075 if (txn->req.msg_state == HTTP_MSG_ERROR)
4076 goto wait_other_side;
4077
4078 if (txn->req.msg_state < HTTP_MSG_DONE) {
4079 /* The client seems to still be sending data, probably
4080 * because we got an error response during an upload.
4081 * We have the choice of either breaking the connection
4082 * or letting it pass through. Let's do the later.
4083 */
4084 goto wait_other_side;
4085 }
4086
Willy Tarreau610ecce2010-01-04 21:15:02 +01004087 /* When we get here, it means that both the request and the
4088 * response have finished receiving. Depending on the connection
4089 * mode, we'll have to wait for the last bytes to leave in either
4090 * direction, and sometimes for a close to be effective.
4091 */
4092
4093 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4094 /* Server-close mode : shut read and wait for the request
4095 * side to close its output buffer. The caller will detect
4096 * when we're in DONE and the other is in CLOSED and will
4097 * catch that for the final cleanup.
4098 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004099 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4100 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004101 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004102 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4103 /* Option forceclose is set, or either side wants to close,
4104 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004105 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004106 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004107 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004108 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004109 channel_shutr_now(chn);
4110 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004111 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004112 }
4113 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004114 /* The last possible modes are keep-alive and tunnel. Tunnel will
4115 * need to forward remaining data. Keep-alive will need to monitor
4116 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004117 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004118 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004119 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004120 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4121 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004122 }
4123
Christopher Faulet4be98032017-07-18 10:48:24 +02004124 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004125 }
4126
4127 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4128 http_msg_closing:
4129 /* nothing else to forward, just waiting for the output buffer
4130 * to be empty and for the shutw_now to take effect.
4131 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004132 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004133 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4134 goto http_msg_closed;
4135 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004136 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004137 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004138 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004139 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004140 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004141 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004142 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004143 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004144 }
4145
4146 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4147 http_msg_closed:
4148 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004149 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004150 channel_auto_close(chn);
4151 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004152 goto wait_other_side;
4153 }
4154
Christopher Faulet4be98032017-07-18 10:48:24 +02004155 check_channel_flags:
4156 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4157 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4158 /* if we've just closed an output, let's switch */
4159 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4160 goto http_msg_closing;
4161 }
4162
Willy Tarreau610ecce2010-01-04 21:15:02 +01004163 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004164 /* We force the response to leave immediately if we're waiting for the
4165 * other side, since there is no pending shutdown to push it out.
4166 */
4167 if (!channel_is_empty(chn))
4168 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004169 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004170}
4171
4172
Christopher Faulet894da4c2017-07-18 11:29:07 +02004173/* Resync the request and response state machines. */
4174void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004175{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004176 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004177#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004178 int old_req_state = txn->req.msg_state;
4179 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004180#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004181
Willy Tarreau610ecce2010-01-04 21:15:02 +01004182 http_sync_req_state(s);
4183 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004184 if (!http_sync_res_state(s))
4185 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004186 if (!http_sync_req_state(s))
4187 break;
4188 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004189
Christopher Faulet894da4c2017-07-18 11:29:07 +02004190 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4191 "req->analysers=0x%08x res->analysers=0x%08x\n",
4192 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004193 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4194 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004195 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004196
4197
Willy Tarreau610ecce2010-01-04 21:15:02 +01004198 /* OK, both state machines agree on a compatible state.
4199 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004200 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4201 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004202 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4203 * means we must abort the request.
4204 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4205 * corresponding channel.
4206 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4207 * on the response with server-close mode means we've completed one
4208 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004209 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004210 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4211 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004212 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004213 channel_auto_close(&s->req);
4214 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004215 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004216 channel_auto_close(&s->res);
4217 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004218 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004219 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4220 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004221 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004222 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004223 channel_auto_close(&s->res);
4224 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004225 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004226 channel_abort(&s->req);
4227 channel_auto_close(&s->req);
4228 channel_auto_read(&s->req);
4229 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004230 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004231 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4232 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4233 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4234 s->req.analysers &= AN_REQ_FLT_END;
4235 if (HAS_REQ_DATA_FILTERS(s))
4236 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4237 }
4238 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4239 s->res.analysers &= AN_RES_FLT_END;
4240 if (HAS_RSP_DATA_FILTERS(s))
4241 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4242 }
4243 channel_auto_close(&s->req);
4244 channel_auto_read(&s->req);
4245 channel_auto_close(&s->res);
4246 channel_auto_read(&s->res);
4247 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004248 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4249 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004250 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004251 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4252 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4253 /* server-close/keep-alive: terminate this transaction,
4254 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004255 * a fresh-new transaction, but only once we're sure there's
4256 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004257 * another request. They must not hold any pending output data
4258 * and the response buffer must realigned
4259 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004260 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004261 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004262 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004263 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004264 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004265 else {
4266 s->req.analysers = AN_REQ_FLT_END;
4267 s->res.analysers = AN_RES_FLT_END;
4268 txn->flags |= TX_WAIT_CLEANUP;
4269 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004270 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004271}
4272
Willy Tarreaud98cf932009-12-27 22:54:55 +01004273/* This function is an analyser which forwards request body (including chunk
4274 * sizes if any). It is called as soon as we must forward, even if we forward
4275 * zero byte. The only situation where it must not be called is when we're in
4276 * tunnel mode and we want to forward till the close. It's used both to forward
4277 * remaining data and to resync after end of body. It expects the msg_state to
4278 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004279 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004280 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004281 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004282 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004283int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004284{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004285 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004286 struct http_txn *txn = s->txn;
4287 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004288 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004289
Christopher Faulete0768eb2018-10-03 16:38:02 +02004290 if (IS_HTX_STRM(s))
4291 return htx_request_forward_body(s, req, an_bit);
4292
Christopher Faulet45073512018-07-20 10:16:29 +02004293 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 +02004294 now_ms, __FUNCTION__,
4295 s,
4296 req,
4297 req->rex, req->wex,
4298 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004299 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004300 req->analysers);
4301
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004302 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4303 return 0;
4304
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004305 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004306 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004307 /* Output closed while we were sending data. We must abort and
4308 * wake the other side up.
4309 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004310 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004311 msg->msg_state = HTTP_MSG_ERROR;
4312 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004313 return 1;
4314 }
4315
Willy Tarreaud98cf932009-12-27 22:54:55 +01004316 /* Note that we don't have to send 100-continue back because we don't
4317 * need the data to complete our job, and it's up to the server to
4318 * decide whether to return 100, 417 or anything else in return of
4319 * an "Expect: 100-continue" header.
4320 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004321 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004322 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4323 ? HTTP_MSG_CHUNK_SIZE
4324 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004325
4326 /* TODO/filters: when http-buffer-request option is set or if a
4327 * rule on url_param exists, the first chunk size could be
4328 * already parsed. In that case, msg->next is after the chunk
4329 * size (including the CRLF after the size). So this case should
4330 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004331 }
4332
Willy Tarreau7ba23542014-04-17 21:50:00 +02004333 /* Some post-connect processing might want us to refrain from starting to
4334 * forward data. Currently, the only reason for this is "balance url_param"
4335 * whichs need to parse/process the request after we've enabled forwarding.
4336 */
4337 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004338 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004339 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004340 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004341 channel_dont_close(req); /* don't fail on early shutr */
4342 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004343 }
4344 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4345 }
4346
Willy Tarreau80a92c02014-03-12 10:41:13 +01004347 /* in most states, we should abort in case of early close */
4348 channel_auto_close(req);
4349
Willy Tarreauefdf0942014-04-24 20:08:57 +02004350 if (req->to_forward) {
4351 /* We can't process the buffer's contents yet */
4352 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004353 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004354 }
4355
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004356 if (msg->msg_state < HTTP_MSG_DONE) {
4357 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4358 ? http_msg_forward_chunked_body(s, msg)
4359 : http_msg_forward_body(s, msg));
4360 if (!ret)
4361 goto missing_data_or_waiting;
4362 if (ret < 0)
4363 goto return_bad_req;
4364 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004365
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004366 /* other states, DONE...TUNNEL */
4367 /* we don't want to forward closes on DONE except in tunnel mode. */
4368 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4369 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004370
Christopher Faulet894da4c2017-07-18 11:29:07 +02004371 http_resync_states(s);
4372 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004373 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4374 if (req->flags & CF_SHUTW) {
4375 /* request errors are most likely due to the
4376 * server aborting the transfer. */
4377 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004378 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004379 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004380 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004381 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004382 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004383 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004384 }
4385
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004386 /* If "option abortonclose" is set on the backend, we want to monitor
4387 * the client's connection and forward any shutdown notification to the
4388 * server, which will decide whether to close or to go on processing the
4389 * request. We only do that in tunnel mode, and not in other modes since
4390 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004391 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004392 channel_auto_read(req);
4393 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4394 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4395 s->si[1].flags |= SI_FL_NOLINGER;
4396 channel_auto_close(req);
4397 }
4398 else if (s->txn->meth == HTTP_METH_POST) {
4399 /* POST requests may require to read extra CRLF sent by broken
4400 * browsers and which could cause an RST to be sent upon close
4401 * on some systems (eg: Linux). */
4402 channel_auto_read(req);
4403 }
4404 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004405
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004406 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004407 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004408 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004409 if (!(s->flags & SF_ERR_MASK))
4410 s->flags |= SF_ERR_CLICL;
4411 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004412 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004413 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004414 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004415 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004416 }
4417
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004418 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4419 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004420 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004421 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004422
4423 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004424 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004425
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004426 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004427 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004428 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004429 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004430
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004431 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004432 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004433 * And when content-length is used, we never want to let the possible
4434 * shutdown be forwarded to the other side, as the state machine will
4435 * take care of it once the client responds. It's also important to
4436 * prevent TIME_WAITs from accumulating on the backend side, and for
4437 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004438 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004439 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004440 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004441
Willy Tarreau5c620922011-05-11 19:56:11 +02004442 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004443 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004444 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004445 * modes are already handled by the stream sock layer. We must not do
4446 * this in content-length mode because it could present the MSG_MORE
4447 * flag with the last block of forwarded data, which would cause an
4448 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004449 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004450 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004451 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004452
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004453 return 0;
4454
Willy Tarreaud98cf932009-12-27 22:54:55 +01004455 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004456 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004457 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004458 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004459
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004460 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004461 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004462 txn->req.msg_state = HTTP_MSG_ERROR;
4463 if (txn->status) {
4464 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004465 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004466 } else {
4467 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004468 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004469 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004470 req->analysers &= AN_REQ_FLT_END;
4471 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 +01004472
Willy Tarreaue7dff022015-04-03 01:14:29 +02004473 if (!(s->flags & SF_ERR_MASK))
4474 s->flags |= SF_ERR_PRXCOND;
4475 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004476 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004477 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004478 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004479 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004480 }
4481 return 0;
4482
4483 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004484 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004485 txn->req.msg_state = HTTP_MSG_ERROR;
4486 if (txn->status) {
4487 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004488 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004489 } else {
4490 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004491 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004492 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004493 req->analysers &= AN_REQ_FLT_END;
4494 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 +01004495
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004496 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4497 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004498 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004499 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004500
Willy Tarreaue7dff022015-04-03 01:14:29 +02004501 if (!(s->flags & SF_ERR_MASK))
4502 s->flags |= SF_ERR_SRVCL;
4503 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004504 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004505 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004506 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004507 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004508 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004509 return 0;
4510}
4511
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004512/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4513 * processing can continue on next analysers, or zero if it either needs more
4514 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004515 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004516 * when it has nothing left to do, and may remove any analyser when it wants to
4517 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004518 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004519int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004520{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004521 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004522 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004523 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004524 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004525 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004526 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004527 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004528 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004529
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004530 srv_conn = cs_conn(objt_cs(s->si[1].end));
4531
Christopher Faulete0768eb2018-10-03 16:38:02 +02004532 if (IS_HTX_STRM(s))
4533 return htx_wait_for_response(s, rep, an_bit);
4534
Christopher Faulet45073512018-07-20 10:16:29 +02004535 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 +02004536 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004537 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004538 rep,
4539 rep->rex, rep->wex,
4540 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004541 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004542 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004543
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004544 /*
4545 * Now parse the partial (or complete) lines.
4546 * We will check the response syntax, and also join multi-line
4547 * headers. An index of all the lines will be elaborated while
4548 * parsing.
4549 *
4550 * For the parsing, we use a 28 states FSM.
4551 *
4552 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004553 * ci_head(rep) = beginning of response
4554 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4555 * ci_tail(rep) = end of input data
4556 * msg->eol = end of current header or line (LF or CRLF)
4557 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004558 */
4559
Willy Tarreau628c40c2014-04-24 19:11:26 +02004560 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004561 /* There's a protected area at the end of the buffer for rewriting
4562 * purposes. We don't want to start to parse the request if the
4563 * protected area is affected, because we may have to move processed
4564 * data later, which is much more complicated.
4565 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004566 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004567 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004568 /* some data has still not left the buffer, wake us once that's done */
4569 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4570 goto abort_response;
4571 channel_dont_close(rep);
4572 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004573 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004574 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004575 }
4576
Willy Tarreau188e2302018-06-15 11:11:53 +02004577 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004578 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004579 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004580
Willy Tarreauf37954d2018-06-15 18:31:02 +02004581 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004582 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004583 }
4584
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004585 /* 1: we might have to print this header in debug mode */
4586 if (unlikely((global.mode & MODE_DEBUG) &&
4587 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004588 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004589 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004590
Willy Tarreauf37954d2018-06-15 18:31:02 +02004591 sol = ci_head(rep);
4592 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004593 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004594
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004595 sol += hdr_idx_first_pos(&txn->hdr_idx);
4596 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004597
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004598 while (cur_idx) {
4599 eol = sol + txn->hdr_idx.v[cur_idx].len;
4600 debug_hdr("srvhdr", s, sol, eol);
4601 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4602 cur_idx = txn->hdr_idx.v[cur_idx].next;
4603 }
4604 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004605
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004606 /*
4607 * Now we quickly check if we have found a full valid response.
4608 * If not so, we check the FD and buffer states before leaving.
4609 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004610 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004611 * responses are checked first.
4612 *
4613 * Depending on whether the client is still there or not, we
4614 * may send an error response back or not. Note that normally
4615 * we should only check for HTTP status there, and check I/O
4616 * errors somewhere else.
4617 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004618
Willy Tarreau655dce92009-11-08 13:10:58 +01004619 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004620 /* Invalid response */
4621 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4622 /* we detected a parsing error. We want to archive this response
4623 * in the dedicated proxy area for later troubleshooting.
4624 */
4625 hdr_response_bad:
4626 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004627 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004628
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004629 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004630 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004631 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004632 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004633 }
Willy Tarreau64648412010-03-05 10:41:54 +01004634 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004635 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004636 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004637 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004638 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004639 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004640 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004641
Willy Tarreaue7dff022015-04-03 01:14:29 +02004642 if (!(s->flags & SF_ERR_MASK))
4643 s->flags |= SF_ERR_PRXCOND;
4644 if (!(s->flags & SF_FINST_MASK))
4645 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646
4647 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004648 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004649
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004650 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004651 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004652 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004653 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004654 goto hdr_response_bad;
4655 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004656
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004657 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004658 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004659 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004660 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004661 else if (txn->flags & TX_NOT_FIRST)
4662 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004663
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004664 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004665 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004666 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004667 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004668 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004669
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004670 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004671 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004672 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004673
4674 /* Check to see if the server refused the early data.
4675 * If so, just send a 425
4676 */
4677 if (objt_cs(s->si[1].end)) {
4678 struct connection *conn = objt_cs(s->si[1].end)->conn;
4679
4680 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4681 txn->status = 425;
4682 }
4683
Willy Tarreau350f4872014-11-28 14:42:25 +01004684 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004685 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004686 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004687
Willy Tarreaue7dff022015-04-03 01:14:29 +02004688 if (!(s->flags & SF_ERR_MASK))
4689 s->flags |= SF_ERR_SRVCL;
4690 if (!(s->flags & SF_FINST_MASK))
4691 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004692 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004693 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004694
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004695 /* read timeout : return a 504 to the client. */
4696 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004697 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004698 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004699
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004700 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004701 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004702 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004703 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004704 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004705
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004706 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004707 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004708 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004709 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004710 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004711 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004712
Willy Tarreaue7dff022015-04-03 01:14:29 +02004713 if (!(s->flags & SF_ERR_MASK))
4714 s->flags |= SF_ERR_SRVTO;
4715 if (!(s->flags & SF_FINST_MASK))
4716 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004717 return 0;
4718 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004719
Willy Tarreauf003d372012-11-26 13:35:37 +01004720 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004721 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004722 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4723 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004724 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004725 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004726
Christopher Faulet0184ea72017-01-05 14:06:34 +01004727 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004728 channel_auto_close(rep);
4729
4730 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004731 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004732 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004733
Willy Tarreaue7dff022015-04-03 01:14:29 +02004734 if (!(s->flags & SF_ERR_MASK))
4735 s->flags |= SF_ERR_CLICL;
4736 if (!(s->flags & SF_FINST_MASK))
4737 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004738
Willy Tarreau87b09662015-04-03 00:22:06 +02004739 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004740 return 0;
4741 }
4742
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004743 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004744 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004745 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004746 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004747 else if (txn->flags & TX_NOT_FIRST)
4748 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004749
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004750 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004751 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004752 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004753 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004754 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004755
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004756 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004757 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004758 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004759 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004760 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004761 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004762
Willy Tarreaue7dff022015-04-03 01:14:29 +02004763 if (!(s->flags & SF_ERR_MASK))
4764 s->flags |= SF_ERR_SRVCL;
4765 if (!(s->flags & SF_FINST_MASK))
4766 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004767 return 0;
4768 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004769
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004770 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004771 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004772 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004773 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004774 else if (txn->flags & TX_NOT_FIRST)
4775 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004776
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004777 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004778 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004779 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004780
Willy Tarreaue7dff022015-04-03 01:14:29 +02004781 if (!(s->flags & SF_ERR_MASK))
4782 s->flags |= SF_ERR_CLICL;
4783 if (!(s->flags & SF_FINST_MASK))
4784 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004785
Willy Tarreau87b09662015-04-03 00:22:06 +02004786 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004787 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004788 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004789
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004790 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004791 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004792 return 0;
4793 }
4794
4795 /* More interesting part now : we know that we have a complete
4796 * response which at least looks like HTTP. We have an indicator
4797 * of each header's length, so we can parse them quickly.
4798 */
4799
4800 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004801 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004802
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004803 /*
4804 * 1: get the status code
4805 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004806 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004807 if (n < 1 || n > 5)
4808 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004809 /* when the client triggers a 4xx from the server, it's most often due
4810 * to a missing object or permission. These events should be tracked
4811 * because if they happen often, it may indicate a brute force or a
4812 * vulnerability scan.
4813 */
4814 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004815 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004816
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004817 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004818 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004819
Willy Tarreau91852eb2015-05-01 13:26:00 +02004820 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4821 * exactly one digit "." one digit. This check may be disabled using
4822 * option accept-invalid-http-response.
4823 */
4824 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4825 if (msg->sl.st.v_l != 8) {
4826 msg->err_pos = 0;
4827 goto hdr_response_bad;
4828 }
4829
Willy Tarreauf37954d2018-06-15 18:31:02 +02004830 if (ci_head(rep)[4] != '/' ||
4831 !isdigit((unsigned char)ci_head(rep)[5]) ||
4832 ci_head(rep)[6] != '.' ||
4833 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004834 msg->err_pos = 4;
4835 goto hdr_response_bad;
4836 }
4837 }
4838
Willy Tarreau5b154472009-12-21 20:11:07 +01004839 /* check if the response is HTTP/1.1 or above */
4840 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004841 ((ci_head(rep)[5] > '1') ||
4842 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004843 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004844
4845 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004846 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 +01004847
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004848 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004849 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004850
Willy Tarreauf37954d2018-06-15 18:31:02 +02004851 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004852
Willy Tarreau39650402010-03-15 19:44:39 +01004853 /* Adjust server's health based on status code. Note: status codes 501
4854 * and 505 are triggered on demand by client request, so we must not
4855 * count them as server failures.
4856 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004857 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004858 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004859 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004860 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004861 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004862 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004863
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004864 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004865 * We may be facing a 100-continue response, or any other informational
4866 * 1xx response which is non-final, in which case this is not the right
4867 * response, and we're waiting for the next one. Let's allow this response
4868 * to go to the client and wait for the next one. There's an exception for
4869 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004870 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004871 if (txn->status < 200 &&
4872 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004873 hdr_idx_init(&txn->hdr_idx);
4874 msg->next -= channel_forward(rep, msg->next);
4875 msg->msg_state = HTTP_MSG_RPBEFORE;
4876 txn->status = 0;
4877 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004878 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004879 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004880 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004881
Willy Tarreaua14ad722017-07-07 11:36:32 +02004882 /*
4883 * 2: check for cacheability.
4884 */
4885
4886 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004887 case 200:
4888 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004889 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004890 case 206:
4891 case 300:
4892 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004893 case 404:
4894 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004895 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004896 case 414:
4897 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004898 break;
4899 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004900 /* RFC7231#6.1:
4901 * Responses with status codes that are defined as
4902 * cacheable by default (e.g., 200, 203, 204, 206,
4903 * 300, 301, 404, 405, 410, 414, and 501 in this
4904 * specification) can be reused by a cache with
4905 * heuristic expiration unless otherwise indicated
4906 * by the method definition or explicit cache
4907 * controls [RFC7234]; all other status codes are
4908 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004909 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004910 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004911 break;
4912 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004913
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004914 /*
4915 * 3: we may need to capture headers
4916 */
4917 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004918 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004919 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4920 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004921
Willy Tarreau557f1992015-05-01 10:05:17 +02004922 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4923 * by RFC7230#3.3.3 :
4924 *
4925 * The length of a message body is determined by one of the following
4926 * (in order of precedence):
4927 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004928 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4929 * the connection will become a tunnel immediately after the empty
4930 * line that concludes the header fields. A client MUST ignore
4931 * any Content-Length or Transfer-Encoding header fields received
4932 * in such a message. Any 101 response (Switching Protocols) is
4933 * managed in the same manner.
4934 *
4935 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004936 * (Informational), 204 (No Content), or 304 (Not Modified) status
4937 * code is always terminated by the first empty line after the
4938 * header fields, regardless of the header fields present in the
4939 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004940 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004941 * 3. If a Transfer-Encoding header field is present and the chunked
4942 * transfer coding (Section 4.1) is the final encoding, the message
4943 * body length is determined by reading and decoding the chunked
4944 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004945 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004946 * If a Transfer-Encoding header field is present in a response and
4947 * the chunked transfer coding is not the final encoding, the
4948 * message body length is determined by reading the connection until
4949 * it is closed by the server. If a Transfer-Encoding header field
4950 * is present in a request and the chunked transfer coding is not
4951 * the final encoding, the message body length cannot be determined
4952 * reliably; the server MUST respond with the 400 (Bad Request)
4953 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004954 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004955 * If a message is received with both a Transfer-Encoding and a
4956 * Content-Length header field, the Transfer-Encoding overrides the
4957 * Content-Length. Such a message might indicate an attempt to
4958 * perform request smuggling (Section 9.5) or response splitting
4959 * (Section 9.4) and ought to be handled as an error. A sender MUST
4960 * remove the received Content-Length field prior to forwarding such
4961 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004962 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004963 * 4. If a message is received without Transfer-Encoding and with
4964 * either multiple Content-Length header fields having differing
4965 * field-values or a single Content-Length header field having an
4966 * invalid value, then the message framing is invalid and the
4967 * recipient MUST treat it as an unrecoverable error. If this is a
4968 * request message, the server MUST respond with a 400 (Bad Request)
4969 * status code and then close the connection. If this is a response
4970 * message received by a proxy, the proxy MUST close the connection
4971 * to the server, discard the received response, and send a 502 (Bad
4972 * Gateway) response to the client. If this is a response message
4973 * received by a user agent, the user agent MUST close the
4974 * connection to the server and discard the received response.
4975 *
4976 * 5. If a valid Content-Length header field is present without
4977 * Transfer-Encoding, its decimal value defines the expected message
4978 * body length in octets. If the sender closes the connection or
4979 * the recipient times out before the indicated number of octets are
4980 * received, the recipient MUST consider the message to be
4981 * incomplete and close the connection.
4982 *
4983 * 6. If this is a request message and none of the above are true, then
4984 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004985 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004986 * 7. Otherwise, this is a response message without a declared message
4987 * body length, so the message body length is determined by the
4988 * number of octets received prior to the server closing the
4989 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004990 */
4991
4992 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004993 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004994 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004995 * FIXME: should we parse anyway and return an error on chunked encoding ?
4996 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004997 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4998 txn->status == 101)) {
4999 /* Either we've established an explicit tunnel, or we're
5000 * switching the protocol. In both cases, we're very unlikely
5001 * to understand the next protocols. We have to switch to tunnel
5002 * mode, so that we transfer the request and responses then let
5003 * this protocol pass unmodified. When we later implement specific
5004 * parsers for such protocols, we'll want to check the Upgrade
5005 * header which contains information about that protocol for
5006 * responses with status 101 (eg: see RFC2817 about TLS).
5007 */
5008 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5009 msg->flags |= HTTP_MSGF_XFER_LEN;
5010 goto end;
5011 }
5012
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005013 if (txn->meth == HTTP_METH_HEAD ||
5014 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005015 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005016 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005017 goto skip_content_length;
5018 }
5019
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005020 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005021 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005022 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005023 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005024 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5025 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005026 /* bad transfer-encoding (chunked followed by something else) */
5027 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005028 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005029 break;
5030 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005031 }
5032
Willy Tarreau1c913912015-04-30 10:57:51 +02005033 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005034 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005035 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005036 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005037 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5038 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005039 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005040 signed long long cl;
5041
Willy Tarreauad14f752011-09-02 20:33:27 +02005042 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005043 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005044 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005045 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005046
Willy Tarreauad14f752011-09-02 20:33:27 +02005047 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005048 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005049 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005050 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005051
Willy Tarreauad14f752011-09-02 20:33:27 +02005052 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005053 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005054 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005055 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005056
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005057 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005058 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005059 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005060 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005061
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005062 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005063 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005064 }
5065
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02005066 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
5067 * 407 (Proxy-Authenticate) responses and set the connection to private
5068 */
5069 if (srv_conn && txn->status == 401) {
5070 /* check for Negotiate/NTLM WWW-Authenticate headers */
5071 ctx.idx = 0;
5072 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
5073 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5074 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5075 srv_conn->flags |= CO_FL_PRIVATE;
5076 }
5077 } else if (srv_conn && txn->status == 407) {
5078 /* check for Negotiate/NTLM Proxy-Authenticate headers */
5079 ctx.idx = 0;
5080 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
5081 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5082 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5083 srv_conn->flags |= CO_FL_PRIVATE;
5084 }
5085 }
5086
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005087 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005088 /* Now we have to check if we need to modify the Connection header.
5089 * This is more difficult on the response than it is on the request,
5090 * because we can have two different HTTP versions and we don't know
5091 * how the client will interprete a response. For instance, let's say
5092 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5093 * HTTP/1.1 response without any header. Maybe it will bound itself to
5094 * HTTP/1.0 because it only knows about it, and will consider the lack
5095 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5096 * the lack of header as a keep-alive. Thus we will use two flags
5097 * indicating how a request MAY be understood by the client. In case
5098 * of multiple possibilities, we'll fix the header to be explicit. If
5099 * ambiguous cases such as both close and keepalive are seen, then we
5100 * will fall back to explicit close. Note that we won't take risks with
5101 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005102 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005103 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005104 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005105 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01005106 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005107
Willy Tarreau60466522010-01-18 19:08:45 +01005108 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005109 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01005110 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005111
Willy Tarreau60466522010-01-18 19:08:45 +01005112 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005113 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01005114 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005115 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005116 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005117 }
Willy Tarreau60466522010-01-18 19:08:45 +01005118 else { /* SCL / KAL */
5119 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005120 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005121 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005122 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005123
Willy Tarreau60466522010-01-18 19:08:45 +01005124 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005125 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005126
Willy Tarreau60466522010-01-18 19:08:45 +01005127 /* Some keep-alive responses are converted to Server-close if
5128 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005129 */
Willy Tarreau60466522010-01-18 19:08:45 +01005130 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5131 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005132 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005133 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005134 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005135 }
5136
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005137 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005138 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005139 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005140
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005141 /* end of job, return OK */
5142 rep->analysers &= ~an_bit;
5143 rep->analyse_exp = TICK_ETERNITY;
5144 channel_auto_close(rep);
5145 return 1;
5146
5147 abort_keep_alive:
5148 /* A keep-alive request to the server failed on a network error.
5149 * The client is required to retry. We need to close without returning
5150 * any other information so that the client retries.
5151 */
5152 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005153 rep->analysers &= AN_RES_FLT_END;
5154 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005155 channel_auto_close(rep);
5156 s->logs.logwait = 0;
5157 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005158 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005159 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005160 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005161 return 0;
5162}
5163
5164/* This function performs all the processing enabled for the current response.
5165 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005166 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005167 * other functions. It works like process_request (see indications above).
5168 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005169int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005170{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005171 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005172 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005173 struct http_msg *msg = &txn->rsp;
5174 struct proxy *cur_proxy;
5175 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005176 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005177
Christopher Faulete0768eb2018-10-03 16:38:02 +02005178 if (IS_HTX_STRM(s))
5179 return htx_process_res_common(s, rep, an_bit, px);
5180
Christopher Faulet45073512018-07-20 10:16:29 +02005181 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 +02005182 now_ms, __FUNCTION__,
5183 s,
5184 rep,
5185 rep->rex, rep->wex,
5186 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005187 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005188 rep->analysers);
5189
5190 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5191 return 0;
5192
Willy Tarreau70730dd2014-04-24 18:06:27 +02005193 /* The stats applet needs to adjust the Connection header but we don't
5194 * apply any filter there.
5195 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005196 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5197 rep->analysers &= ~an_bit;
5198 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005199 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005200 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005201
Willy Tarreau58975672014-04-24 21:13:57 +02005202 /*
5203 * We will have to evaluate the filters.
5204 * As opposed to version 1.2, now they will be evaluated in the
5205 * filters order and not in the header order. This means that
5206 * each filter has to be validated among all headers.
5207 *
5208 * Filters are tried with ->be first, then with ->fe if it is
5209 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005210 *
5211 * Maybe we are in resume condiion. In this case I choose the
5212 * "struct proxy" which contains the rule list matching the resume
5213 * pointer. If none of theses "struct proxy" match, I initialise
5214 * the process with the first one.
5215 *
5216 * In fact, I check only correspondance betwwen the current list
5217 * pointer and the ->fe rule list. If it doesn't match, I initialize
5218 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005219 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005220 if (s->current_rule_list == &sess->fe->http_res_rules)
5221 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005222 else
5223 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005224 while (1) {
5225 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005226
Willy Tarreau58975672014-04-24 21:13:57 +02005227 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005228 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005229 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005230
Willy Tarreau51d861a2015-05-22 17:30:48 +02005231 if (ret == HTTP_RULE_RES_BADREQ)
5232 goto return_srv_prx_502;
5233
5234 if (ret == HTTP_RULE_RES_DONE) {
5235 rep->analysers &= ~an_bit;
5236 rep->analyse_exp = TICK_ETERNITY;
5237 return 1;
5238 }
5239 }
5240
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005241 /* we need to be called again. */
5242 if (ret == HTTP_RULE_RES_YIELD) {
5243 channel_dont_close(rep);
5244 return 0;
5245 }
5246
Willy Tarreau58975672014-04-24 21:13:57 +02005247 /* try headers filters */
5248 if (rule_set->rsp_exp != NULL) {
5249 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5250 return_bad_resp:
5251 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005252 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005253 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005254 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005255 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005256 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005257 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005258 txn->status = 502;
5259 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005260 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005261 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005262 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005263 if (!(s->flags & SF_ERR_MASK))
5264 s->flags |= SF_ERR_PRXCOND;
5265 if (!(s->flags & SF_FINST_MASK))
5266 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005267 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005268 }
Willy Tarreau58975672014-04-24 21:13:57 +02005269 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005270
Willy Tarreau58975672014-04-24 21:13:57 +02005271 /* has the response been denied ? */
5272 if (txn->flags & TX_SVDENY) {
5273 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005274 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005275
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005276 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5277 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005278 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005279 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005280
Willy Tarreau58975672014-04-24 21:13:57 +02005281 goto return_srv_prx_502;
5282 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005283
Willy Tarreau58975672014-04-24 21:13:57 +02005284 /* add response headers from the rule sets in the same order */
5285 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005286 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005287 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005288 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005289 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005290 ret = acl_pass(ret);
5291 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5292 ret = !ret;
5293 if (!ret)
5294 continue;
5295 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005296 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005297 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005298 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005299
Willy Tarreau58975672014-04-24 21:13:57 +02005300 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005301 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005302 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005303 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005304 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005305
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005306 /* After this point, this anayzer can't return yield, so we can
5307 * remove the bit corresponding to this analyzer from the list.
5308 *
5309 * Note that the intermediate returns and goto found previously
5310 * reset the analyzers.
5311 */
5312 rep->analysers &= ~an_bit;
5313 rep->analyse_exp = TICK_ETERNITY;
5314
Willy Tarreau58975672014-04-24 21:13:57 +02005315 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005316 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005317 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005318
Willy Tarreau58975672014-04-24 21:13:57 +02005319 /*
5320 * Now check for a server cookie.
5321 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005322 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005323 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005324
Willy Tarreau58975672014-04-24 21:13:57 +02005325 /*
5326 * Check for cache-control or pragma headers if required.
5327 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005328 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005329 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005330
Willy Tarreau58975672014-04-24 21:13:57 +02005331 /*
5332 * Add server cookie in the response if needed
5333 */
5334 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5335 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005336 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005337 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5338 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5339 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5340 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5341 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005342 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005343 /* the server is known, it's not the one the client requested, or the
5344 * cookie's last seen date needs to be refreshed. We have to
5345 * insert a set-cookie here, except if we want to insert only on POST
5346 * requests and this one isn't. Note that servers which don't have cookies
5347 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005348 */
Willy Tarreau58975672014-04-24 21:13:57 +02005349 if (!objt_server(s->target)->cookie) {
5350 chunk_printf(&trash,
5351 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5352 s->be->cookie_name);
5353 }
5354 else {
5355 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005356
Willy Tarreau58975672014-04-24 21:13:57 +02005357 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5358 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005359 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5360 s30tob64((date.tv_sec+3) >> 2,
5361 trash.area + trash.data);
5362 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005363
Willy Tarreau58975672014-04-24 21:13:57 +02005364 if (s->be->cookie_maxlife) {
5365 /* emit first_date, which is either the original one or
5366 * the current date.
5367 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005368 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005369 s30tob64(txn->cookie_first_date ?
5370 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005371 (date.tv_sec+3) >> 2,
5372 trash.area + trash.data);
5373 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005374 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005375 }
Willy Tarreau58975672014-04-24 21:13:57 +02005376 chunk_appendf(&trash, "; path=/");
5377 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005378
Willy Tarreau58975672014-04-24 21:13:57 +02005379 if (s->be->cookie_domain)
5380 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005381
Willy Tarreau58975672014-04-24 21:13:57 +02005382 if (s->be->ck_opts & PR_CK_HTTPONLY)
5383 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005384
Willy Tarreau58975672014-04-24 21:13:57 +02005385 if (s->be->ck_opts & PR_CK_SECURE)
5386 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005387
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005388 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005389 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005390
Willy Tarreau58975672014-04-24 21:13:57 +02005391 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005392 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005393 /* the server did not change, only the date was updated */
5394 txn->flags |= TX_SCK_UPDATED;
5395 else
5396 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005397
Willy Tarreau58975672014-04-24 21:13:57 +02005398 /* Here, we will tell an eventual cache on the client side that we don't
5399 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5400 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5401 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005402 */
Willy Tarreau58975672014-04-24 21:13:57 +02005403 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005404
Willy Tarreau58975672014-04-24 21:13:57 +02005405 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005406
Willy Tarreau58975672014-04-24 21:13:57 +02005407 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5408 "Cache-control: private", 22) < 0))
5409 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005410 }
Willy Tarreau58975672014-04-24 21:13:57 +02005411 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005412
Willy Tarreau58975672014-04-24 21:13:57 +02005413 /*
5414 * Check if result will be cacheable with a cookie.
5415 * We'll block the response if security checks have caught
5416 * nasty things such as a cacheable cookie.
5417 */
5418 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5419 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5420 (s->be->options & PR_O_CHK_CACHE)) {
5421 /* we're in presence of a cacheable response containing
5422 * a set-cookie header. We'll block it as requested by
5423 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005424 */
Willy Tarreau58975672014-04-24 21:13:57 +02005425 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005426 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005427
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005428 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5429 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005430 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005431 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005432
Christopher Faulet767a84b2017-11-24 16:50:31 +01005433 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5434 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005435 send_log(s->be, LOG_ALERT,
5436 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5437 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5438 goto return_srv_prx_502;
5439 }
Willy Tarreau03945942009-12-22 16:50:27 +01005440
Willy Tarreau70730dd2014-04-24 18:06:27 +02005441 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005442 /*
5443 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5444 * If an "Upgrade" token is found, the header is left untouched in order
5445 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005446 * if anything but "Upgrade" is present in the Connection header. We don't
5447 * want to touch any 101 response either since it's switching to another
5448 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005449 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005450 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005451 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005452 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005453
Willy Tarreau58975672014-04-24 21:13:57 +02005454 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5455 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5456 /* we want a keep-alive response here. Keep-alive header
5457 * required if either side is not 1.1.
5458 */
5459 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5460 want_flags |= TX_CON_KAL_SET;
5461 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005462 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005463 /* we want a close response here. Close header required if
5464 * the server is 1.1, regardless of the client.
5465 */
5466 if (msg->flags & HTTP_MSGF_VER_11)
5467 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005468 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005469
Willy Tarreau58975672014-04-24 21:13:57 +02005470 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5471 http_change_connection_header(txn, msg, want_flags);
5472 }
5473
5474 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005475 /* Always enter in the body analyzer */
5476 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5477 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005478
Willy Tarreau58975672014-04-24 21:13:57 +02005479 /* if the user wants to log as soon as possible, without counting
5480 * bytes from the server, then this is the right moment. We have
5481 * to temporarily assign bytes_out to log what we currently have.
5482 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005483 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005484 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5485 s->logs.bytes_out = txn->rsp.eoh;
5486 s->do_log(s);
5487 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005488 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005489 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005490}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005491
Willy Tarreaud98cf932009-12-27 22:54:55 +01005492/* This function is an analyser which forwards response body (including chunk
5493 * sizes if any). It is called as soon as we must forward, even if we forward
5494 * zero byte. The only situation where it must not be called is when we're in
5495 * tunnel mode and we want to forward till the close. It's used both to forward
5496 * remaining data and to resync after end of body. It expects the msg_state to
5497 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005498 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005499 *
5500 * It is capable of compressing response data both in content-length mode and
5501 * in chunked mode. The state machines follows different flows depending on
5502 * whether content-length and chunked modes are used, since there are no
5503 * trailers in content-length :
5504 *
5505 * chk-mode cl-mode
5506 * ,----- BODY -----.
5507 * / \
5508 * V size > 0 V chk-mode
5509 * .--> SIZE -------------> DATA -------------> CRLF
5510 * | | size == 0 | last byte |
5511 * | v final crlf v inspected |
5512 * | TRAILERS -----------> DONE |
5513 * | |
5514 * `----------------------------------------------'
5515 *
5516 * Compression only happens in the DATA state, and must be flushed in final
5517 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5518 * is performed at once on final states for all bytes parsed, or when leaving
5519 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005520 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005521int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005522{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005523 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005524 struct http_txn *txn = s->txn;
5525 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005526 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005527
Christopher Faulete0768eb2018-10-03 16:38:02 +02005528 if (IS_HTX_STRM(s))
5529 return htx_response_forward_body(s, res, an_bit);
5530
Christopher Faulet45073512018-07-20 10:16:29 +02005531 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 +02005532 now_ms, __FUNCTION__,
5533 s,
5534 res,
5535 res->rex, res->wex,
5536 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005537 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005538 res->analysers);
5539
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005540 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5541 return 0;
5542
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005543 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005544 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005545 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005546 /* Output closed while we were sending data. We must abort and
5547 * wake the other side up.
5548 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005549 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005550 msg->msg_state = HTTP_MSG_ERROR;
5551 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005552 return 1;
5553 }
5554
Willy Tarreau4fe41902010-06-07 22:27:41 +02005555 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005556 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005557
Christopher Fauletd7c91962015-04-30 11:48:27 +02005558 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005559 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5560 ? HTTP_MSG_CHUNK_SIZE
5561 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005562 }
5563
Willy Tarreauefdf0942014-04-24 20:08:57 +02005564 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005565 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005566 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005567 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005568 }
5569
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005570 if (msg->msg_state < HTTP_MSG_DONE) {
5571 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5572 ? http_msg_forward_chunked_body(s, msg)
5573 : http_msg_forward_body(s, msg));
5574 if (!ret)
5575 goto missing_data_or_waiting;
5576 if (ret < 0)
5577 goto return_bad_res;
5578 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005579
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005580 /* other states, DONE...TUNNEL */
5581 /* for keep-alive we don't want to forward closes on DONE */
5582 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5583 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5584 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005585
Christopher Faulet894da4c2017-07-18 11:29:07 +02005586 http_resync_states(s);
5587 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005588 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5589 if (res->flags & CF_SHUTW) {
5590 /* response errors are most likely due to the
5591 * client aborting the transfer. */
5592 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005593 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005594 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005595 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005596 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005597 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005598 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005599 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005600 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005601
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005602 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005603 if (res->flags & CF_SHUTW)
5604 goto aborted_xfer;
5605
5606 /* stop waiting for data if the input is closed before the end. If the
5607 * client side was already closed, it means that the client has aborted,
5608 * so we don't want to count this as a server abort. Otherwise it's a
5609 * server abort.
5610 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005611 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005612 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005613 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005614 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005615 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005616 if (!(s->flags & SF_ERR_MASK))
5617 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005618 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005619 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005620 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005621 goto return_bad_res_stats_ok;
5622 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005623 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005624
Willy Tarreau40dba092010-03-04 18:14:51 +01005625 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005626 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005627 goto return_bad_res;
5628
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005629 /* When TE: chunked is used, we need to get there again to parse
5630 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005631 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5632 * or if there are filters registered on the stream, we don't want to
5633 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005634 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005635 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005636 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005637 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5638 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005639 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005640
Willy Tarreau5c620922011-05-11 19:56:11 +02005641 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005642 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005643 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005644 * modes are already handled by the stream sock layer. We must not do
5645 * this in content-length mode because it could present the MSG_MORE
5646 * flag with the last block of forwarded data, which would cause an
5647 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005648 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005649 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005650 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005651
Willy Tarreau87b09662015-04-03 00:22:06 +02005652 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005653 return 0;
5654
Willy Tarreau40dba092010-03-04 18:14:51 +01005655 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005656 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005657 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005658 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005659
5660 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005661 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005662 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005663 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005664 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005665 res->analysers &= AN_RES_FLT_END;
5666 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 +01005667 if (objt_server(s->target))
5668 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005669
Willy Tarreaue7dff022015-04-03 01:14:29 +02005670 if (!(s->flags & SF_ERR_MASK))
5671 s->flags |= SF_ERR_PRXCOND;
5672 if (!(s->flags & SF_FINST_MASK))
5673 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005674 return 0;
5675
5676 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005677 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005678 txn->rsp.msg_state = HTTP_MSG_ERROR;
5679 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005680 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005681 res->analysers &= AN_RES_FLT_END;
5682 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 +01005683
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005684 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5685 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005686 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005687 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005688
Willy Tarreaue7dff022015-04-03 01:14:29 +02005689 if (!(s->flags & SF_ERR_MASK))
5690 s->flags |= SF_ERR_CLICL;
5691 if (!(s->flags & SF_FINST_MASK))
5692 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005693 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005694}
5695
5696
Christopher Faulet10079f52018-10-03 15:17:28 +02005697int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005698{
5699 struct channel *chn = msg->chn;
5700 int ret;
5701
5702 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5703
5704 if (msg->msg_state == HTTP_MSG_ENDING)
5705 goto ending;
5706
5707 /* Neither content-length, nor transfer-encoding was found, so we must
5708 * read the body until the server connection is closed. In that case, we
5709 * eat data as they come. Of course, this happens for response only. */
5710 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005711 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005712 msg->chunk_len += len;
5713 msg->body_len += len;
5714 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005715 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005716 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005717 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005718 msg->next += ret;
5719 msg->chunk_len -= ret;
5720 if (msg->chunk_len) {
5721 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005722 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005723 chn->flags |= CF_WAKE_WRITE;
5724 goto missing_data_or_waiting;
5725 }
5726
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005727 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5728 * always set for a request. */
5729 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5730 /* The server still sending data that should be filtered */
5731 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5732 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005733 msg->msg_state = HTTP_MSG_TUNNEL;
5734 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005735 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005736
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005737 msg->msg_state = HTTP_MSG_ENDING;
5738
5739 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005740 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005741 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005742 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5743 /* default_ret */ msg->next,
5744 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005745 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005746 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005747 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5748 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005749 if (msg->next)
5750 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005751
Christopher Fauletda02e172015-12-04 09:25:05 +01005752 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5753 /* default_ret */ 1,
5754 /* on_error */ goto error,
5755 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005756 if (msg->msg_state == HTTP_MSG_ENDING)
5757 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005758 return 1;
5759
5760 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005761 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005762 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5763 /* default_ret */ msg->next,
5764 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005765 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005766 msg->next -= ret;
5767 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5768 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005769 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005770 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005771 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005772 return 0;
5773 error:
5774 return -1;
5775}
5776
Christopher Faulet10079f52018-10-03 15:17:28 +02005777int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005778{
5779 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005780 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005781 int ret;
5782
5783 /* Here we have the guarantee to be in one of the following state:
5784 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5785 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5786
Christopher Fauletca874b82018-09-20 11:31:01 +02005787 if (msg->msg_state == HTTP_MSG_ENDING)
5788 goto ending;
5789
5790 /* Don't parse chunks if there is no input data */
5791 if (!ci_data(chn))
5792 goto waiting;
5793
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005794 switch_states:
5795 switch (msg->msg_state) {
5796 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005797 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005798 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005799 /* on_error */ goto error);
5800 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005801 msg->chunk_len -= ret;
5802 if (msg->chunk_len) {
5803 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005804 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005805 chn->flags |= CF_WAKE_WRITE;
5806 goto missing_data_or_waiting;
5807 }
5808
5809 /* nothing left to forward for this chunk*/
5810 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5811 /* fall through for HTTP_MSG_CHUNK_CRLF */
5812
5813 case HTTP_MSG_CHUNK_CRLF:
5814 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005815 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005816 if (ret == 0)
5817 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005818 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005819 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005820 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005821 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005822 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005823 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005824 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005825 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5826 /* fall through for HTTP_MSG_CHUNK_SIZE */
5827
5828 case HTTP_MSG_CHUNK_SIZE:
5829 /* read the chunk size and assign it to ->chunk_len,
5830 * then set ->next to point to the body and switch to
5831 * DATA or TRAILERS state.
5832 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005833 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005834 if (ret == 0)
5835 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005836 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005837 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005838 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005839 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005840 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005841 }
5842
5843 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005844 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005845 msg->chunk_len = chunk;
5846 msg->body_len += chunk;
5847
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005848 if (msg->chunk_len) {
5849 msg->msg_state = HTTP_MSG_DATA;
5850 goto switch_states;
5851 }
5852 msg->msg_state = HTTP_MSG_TRAILERS;
5853 /* fall through for HTTP_MSG_TRAILERS */
5854
5855 case HTTP_MSG_TRAILERS:
5856 ret = http_forward_trailers(msg);
5857 if (ret < 0)
5858 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005859 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5860 /* default_ret */ 1,
5861 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005862 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005863 if (!ret)
5864 goto missing_data_or_waiting;
5865 break;
5866
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005867 default:
5868 /* This should no happen in this function */
5869 goto error;
5870 }
5871
5872 msg->msg_state = HTTP_MSG_ENDING;
5873 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005874 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005875 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005876 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005877 /* default_ret */ msg->next,
5878 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005879 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005880 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005881 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5882 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005883 if (msg->next)
5884 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005885
Christopher Fauletda02e172015-12-04 09:25:05 +01005886 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005887 /* default_ret */ 1,
5888 /* on_error */ goto error,
5889 /* on_wait */ goto waiting);
5890 msg->msg_state = HTTP_MSG_DONE;
5891 return 1;
5892
5893 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005894 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005895 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005896 /* default_ret */ msg->next,
5897 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005898 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005899 msg->next -= ret;
5900 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5901 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005902 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005903 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005904 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005905 return 0;
5906
5907 chunk_parsing_error:
5908 if (msg->err_pos >= 0) {
5909 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005910 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005911 msg->msg_state, strm_fe(s));
5912 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005913 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005914 msg, msg->msg_state, s->be);
5915 }
5916 error:
5917 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005918}
5919
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005920
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005921/* Iterate the same filter through all request headers.
5922 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005923 * Since it can manage the switch to another backend, it updates the per-proxy
5924 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005925 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005926int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005927{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005928 char *cur_ptr, *cur_end, *cur_next;
5929 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005930 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005931 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005932 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005933
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005934 last_hdr = 0;
5935
Willy Tarreauf37954d2018-06-15 18:31:02 +02005936 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005937 old_idx = 0;
5938
5939 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005940 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005941 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005942 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005943 (exp->action == ACT_ALLOW ||
5944 exp->action == ACT_DENY ||
5945 exp->action == ACT_TARPIT))
5946 return 0;
5947
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005948 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005949 if (!cur_idx)
5950 break;
5951
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005952 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005953 cur_ptr = cur_next;
5954 cur_end = cur_ptr + cur_hdr->len;
5955 cur_next = cur_end + cur_hdr->cr + 1;
5956
5957 /* Now we have one header between cur_ptr and cur_end,
5958 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005959 */
5960
Willy Tarreau15a53a42015-01-21 13:39:42 +01005961 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005962 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005963 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005964 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005965 last_hdr = 1;
5966 break;
5967
5968 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005969 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005970 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005971 break;
5972
5973 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005974 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005975 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005976 break;
5977
5978 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005979 len = exp_replace(trash.area,
5980 trash.size, cur_ptr,
5981 exp->replace, pmatch);
5982 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005983 return -1;
5984
Willy Tarreau6e27be12018-08-22 04:46:47 +02005985 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5986
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005987 /* FIXME: if the user adds a newline in the replacement, the
5988 * index will not be recalculated for now, and the new line
5989 * will not be counted as a new header.
5990 */
5991
5992 cur_end += delta;
5993 cur_next += delta;
5994 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005995 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005996 break;
5997
5998 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005999 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006000 cur_next += delta;
6001
Willy Tarreaufa355d42009-11-29 18:12:29 +01006002 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006003 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6004 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006005 cur_hdr->len = 0;
6006 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006007 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006008 break;
6009
6010 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006011 }
6012
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006013 /* keep the link from this header to next one in case of later
6014 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006015 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006016 old_idx = cur_idx;
6017 }
6018 return 0;
6019}
6020
6021
6022/* Apply the filter to the request line.
6023 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6024 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006025 * Since it can manage the switch to another backend, it updates the per-proxy
6026 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006027 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006028int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006029{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006030 char *cur_ptr, *cur_end;
6031 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006032 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006033 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006034
Willy Tarreau3d300592007-03-18 18:34:41 +01006035 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006036 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006037 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006038 (exp->action == ACT_ALLOW ||
6039 exp->action == ACT_DENY ||
6040 exp->action == ACT_TARPIT))
6041 return 0;
6042 else if (exp->action == ACT_REMOVE)
6043 return 0;
6044
6045 done = 0;
6046
Willy Tarreauf37954d2018-06-15 18:31:02 +02006047 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006048 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006049
6050 /* Now we have the request line between cur_ptr and cur_end */
6051
Willy Tarreau15a53a42015-01-21 13:39:42 +01006052 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006053 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006054 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006055 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006056 done = 1;
6057 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006058
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006059 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006060 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006061 done = 1;
6062 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006063
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006064 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006065 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006066 done = 1;
6067 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006068
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006069 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006070 len = exp_replace(trash.area, trash.size,
6071 cur_ptr, exp->replace, pmatch);
6072 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006073 return -1;
6074
Willy Tarreau6e27be12018-08-22 04:46:47 +02006075 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006077 /* FIXME: if the user adds a newline in the replacement, the
6078 * index will not be recalculated for now, and the new line
6079 * will not be counted as a new header.
6080 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006081
Willy Tarreaufa355d42009-11-29 18:12:29 +01006082 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006083 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006084 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006085 HTTP_MSG_RQMETH,
6086 cur_ptr, cur_end + 1,
6087 NULL, NULL);
6088 if (unlikely(!cur_end))
6089 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006090
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006091 /* we have a full request and we know that we have either a CR
6092 * or an LF at <ptr>.
6093 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006094 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6095 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006096 /* there is no point trying this regex on headers */
6097 return 1;
6098 }
6099 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006100 return done;
6101}
Willy Tarreau97de6242006-12-27 17:18:38 +01006102
Willy Tarreau58f10d72006-12-04 02:26:12 +01006103
Willy Tarreau58f10d72006-12-04 02:26:12 +01006104
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006105/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006106 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006107 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006108 * unparsable request. Since it can manage the switch to another backend, it
6109 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006110 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006111int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006112{
Willy Tarreau192252e2015-04-04 01:47:55 +02006113 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006114 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006115 struct hdr_exp *exp;
6116
6117 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006118 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006119
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006120 /*
6121 * The interleaving of transformations and verdicts
6122 * makes it difficult to decide to continue or stop
6123 * the evaluation.
6124 */
6125
Willy Tarreau6c123b12010-01-28 20:22:06 +01006126 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6127 break;
6128
Willy Tarreau3d300592007-03-18 18:34:41 +01006129 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006130 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006131 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006132 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006133
6134 /* if this filter had a condition, evaluate it now and skip to
6135 * next filter if the condition does not match.
6136 */
6137 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006138 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006139 ret = acl_pass(ret);
6140 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6141 ret = !ret;
6142
6143 if (!ret)
6144 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006145 }
6146
6147 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006148 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006149 if (unlikely(ret < 0))
6150 return -1;
6151
6152 if (likely(ret == 0)) {
6153 /* The filter did not match the request, it can be
6154 * iterated through all headers.
6155 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006156 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6157 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006158 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006159 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006160 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006161}
6162
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006163
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006164/* Delete a value in a header between delimiters <from> and <next> in buffer
6165 * <buf>. The number of characters displaced is returned, and the pointer to
6166 * the first delimiter is updated if required. The function tries as much as
6167 * possible to respect the following principles :
6168 * - replace <from> delimiter by the <next> one unless <from> points to a
6169 * colon, in which case <next> is simply removed
6170 * - set exactly one space character after the new first delimiter, unless
6171 * there are not enough characters in the block being moved to do so.
6172 * - remove unneeded spaces before the previous delimiter and after the new
6173 * one.
6174 *
6175 * It is the caller's responsibility to ensure that :
6176 * - <from> points to a valid delimiter or the colon ;
6177 * - <next> points to a valid delimiter or the final CR/LF ;
6178 * - there are non-space chars before <from> ;
6179 * - there is a CR/LF at or after <next>.
6180 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006181int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006182{
6183 char *prev = *from;
6184
6185 if (*prev == ':') {
6186 /* We're removing the first value, preserve the colon and add a
6187 * space if possible.
6188 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006189 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006190 next++;
6191 prev++;
6192 if (prev < next)
6193 *prev++ = ' ';
6194
Willy Tarreau2235b262016-11-05 15:50:20 +01006195 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006196 next++;
6197 } else {
6198 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006199 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006200 prev--;
6201 *from = prev;
6202
6203 /* copy the delimiter and if possible a space if we're
6204 * not at the end of the line.
6205 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006206 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006207 *prev++ = *next++;
6208 if (prev + 1 < next)
6209 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006210 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006211 next++;
6212 }
6213 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006214 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006215}
6216
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006217/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006218 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006219 * desirable to call it only when needed. This code is quite complex because
6220 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6221 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006222 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006223void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006224{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006225 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006226 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006227 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006228 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006229 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6230 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006231
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006232 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006233 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006234 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006235
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006236 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006237 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006238 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006239
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006240 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006241 hdr_beg = hdr_next;
6242 hdr_end = hdr_beg + cur_hdr->len;
6243 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006244
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006245 /* We have one full header between hdr_beg and hdr_end, and the
6246 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006247 * "Cookie:" headers.
6248 */
6249
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006250 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006251 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006252 old_idx = cur_idx;
6253 continue;
6254 }
6255
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 del_from = NULL; /* nothing to be deleted */
6257 preserve_hdr = 0; /* assume we may kill the whole header */
6258
Willy Tarreau58f10d72006-12-04 02:26:12 +01006259 /* Now look for cookies. Conforming to RFC2109, we have to support
6260 * attributes whose name begin with a '$', and associate them with
6261 * the right cookie, if we want to delete this cookie.
6262 * So there are 3 cases for each cookie read :
6263 * 1) it's a special attribute, beginning with a '$' : ignore it.
6264 * 2) it's a server id cookie that we *MAY* want to delete : save
6265 * some pointers on it (last semi-colon, beginning of cookie...)
6266 * 3) it's an application cookie : we *MAY* have to delete a previous
6267 * "special" cookie.
6268 * At the end of loop, if a "special" cookie remains, we may have to
6269 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006270 * *MUST* delete it.
6271 *
6272 * Note: RFC2965 is unclear about the processing of spaces around
6273 * the equal sign in the ATTR=VALUE form. A careful inspection of
6274 * the RFC explicitly allows spaces before it, and not within the
6275 * tokens (attrs or values). An inspection of RFC2109 allows that
6276 * too but section 10.1.3 lets one think that spaces may be allowed
6277 * after the equal sign too, resulting in some (rare) buggy
6278 * implementations trying to do that. So let's do what servers do.
6279 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6280 * allowed quoted strings in values, with any possible character
6281 * after a backslash, including control chars and delimitors, which
6282 * causes parsing to become ambiguous. Browsers also allow spaces
6283 * within values even without quotes.
6284 *
6285 * We have to keep multiple pointers in order to support cookie
6286 * removal at the beginning, middle or end of header without
6287 * corrupting the header. All of these headers are valid :
6288 *
6289 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6290 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6291 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6292 * | | | | | | | | |
6293 * | | | | | | | | hdr_end <--+
6294 * | | | | | | | +--> next
6295 * | | | | | | +----> val_end
6296 * | | | | | +-----------> val_beg
6297 * | | | | +--------------> equal
6298 * | | | +----------------> att_end
6299 * | | +---------------------> att_beg
6300 * | +--------------------------> prev
6301 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006302 */
6303
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006304 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6305 /* Iterate through all cookies on this line */
6306
6307 /* find att_beg */
6308 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006309 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006310 att_beg++;
6311
6312 /* find att_end : this is the first character after the last non
6313 * space before the equal. It may be equal to hdr_end.
6314 */
6315 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006316
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006317 while (equal < hdr_end) {
6318 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006319 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006320 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006321 continue;
6322 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006323 }
6324
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006325 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6326 * is between <att_beg> and <equal>, both may be identical.
6327 */
6328
6329 /* look for end of cookie if there is an equal sign */
6330 if (equal < hdr_end && *equal == '=') {
6331 /* look for the beginning of the value */
6332 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006333 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006334 val_beg++;
6335
6336 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006337 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006338
6339 /* make val_end point to the first white space or delimitor after the value */
6340 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006341 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006342 val_end--;
6343 } else {
6344 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006346
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006347 /* We have nothing to do with attributes beginning with '$'. However,
6348 * they will automatically be removed if a header before them is removed,
6349 * since they're supposed to be linked together.
6350 */
6351 if (*att_beg == '$')
6352 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006353
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006354 /* Ignore cookies with no equal sign */
6355 if (equal == next) {
6356 /* This is not our cookie, so we must preserve it. But if we already
6357 * scheduled another cookie for removal, we cannot remove the
6358 * complete header, but we can remove the previous block itself.
6359 */
6360 preserve_hdr = 1;
6361 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006362 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006363 val_end += delta;
6364 next += delta;
6365 hdr_end += delta;
6366 hdr_next += delta;
6367 cur_hdr->len += delta;
6368 http_msg_move_end(&txn->req, delta);
6369 prev = del_from;
6370 del_from = NULL;
6371 }
6372 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006373 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006374
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006375 /* if there are spaces around the equal sign, we need to
6376 * strip them otherwise we'll get trouble for cookie captures,
6377 * or even for rewrites. Since this happens extremely rarely,
6378 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006379 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006380 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6381 int stripped_before = 0;
6382 int stripped_after = 0;
6383
6384 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006385 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006386 equal += stripped_before;
6387 val_beg += stripped_before;
6388 }
6389
6390 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006391 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006392 val_beg += stripped_after;
6393 stripped_before += stripped_after;
6394 }
6395
6396 val_end += stripped_before;
6397 next += stripped_before;
6398 hdr_end += stripped_before;
6399 hdr_next += stripped_before;
6400 cur_hdr->len += stripped_before;
6401 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006402 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006403 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006404
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006405 /* First, let's see if we want to capture this cookie. We check
6406 * that we don't already have a client side cookie, because we
6407 * can only capture one. Also as an optimisation, we ignore
6408 * cookies shorter than the declared name.
6409 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006410 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6411 (val_end - att_beg >= sess->fe->capture_namelen) &&
6412 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006413 int log_len = val_end - att_beg;
6414
Willy Tarreaubafbe012017-11-24 17:34:44 +01006415 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006416 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006417 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006418 if (log_len > sess->fe->capture_len)
6419 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006420 memcpy(txn->cli_cookie, att_beg, log_len);
6421 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006422 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006423 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006424
Willy Tarreaubca99692010-10-06 19:25:55 +02006425 /* Persistence cookies in passive, rewrite or insert mode have the
6426 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006427 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006428 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006429 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006430 * For cookies in prefix mode, the form is :
6431 *
6432 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006433 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006434 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6435 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6436 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006437 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006438
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006439 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6440 * have the server ID between val_beg and delim, and the original cookie between
6441 * delim+1 and val_end. Otherwise, delim==val_end :
6442 *
6443 * Cookie: NAME=SRV; # in all but prefix modes
6444 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6445 * | || || | |+-> next
6446 * | || || | +--> val_end
6447 * | || || +---------> delim
6448 * | || |+------------> val_beg
6449 * | || +-------------> att_end = equal
6450 * | |+-----------------> att_beg
6451 * | +------------------> prev
6452 * +-------------------------> hdr_beg
6453 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006454
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006455 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006456 for (delim = val_beg; delim < val_end; delim++)
6457 if (*delim == COOKIE_DELIM)
6458 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006459 } else {
6460 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006461 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006462 /* Now check if the cookie contains a date field, which would
6463 * appear after a vertical bar ('|') just after the server name
6464 * and before the delimiter.
6465 */
6466 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6467 if (vbar1) {
6468 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006469 * right is the last seen date. It is a base64 encoded
6470 * 30-bit value representing the UNIX date since the
6471 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006472 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006473 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006474 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006475 if (val_end - vbar1 >= 5) {
6476 val = b64tos30(vbar1);
6477 if (val > 0)
6478 txn->cookie_last_date = val << 2;
6479 }
6480 /* look for a second vertical bar */
6481 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6482 if (vbar1 && (val_end - vbar1 > 5)) {
6483 val = b64tos30(vbar1 + 1);
6484 if (val > 0)
6485 txn->cookie_first_date = val << 2;
6486 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006487 }
6488 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006489
Willy Tarreauf64d1412010-10-07 20:06:11 +02006490 /* if the cookie has an expiration date and the proxy wants to check
6491 * it, then we do that now. We first check if the cookie is too old,
6492 * then only if it has expired. We detect strict overflow because the
6493 * time resolution here is not great (4 seconds). Cookies with dates
6494 * in the future are ignored if their offset is beyond one day. This
6495 * allows an admin to fix timezone issues without expiring everyone
6496 * and at the same time avoids keeping unwanted side effects for too
6497 * long.
6498 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006499 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6500 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006501 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006502 txn->flags &= ~TX_CK_MASK;
6503 txn->flags |= TX_CK_OLD;
6504 delim = val_beg; // let's pretend we have not found the cookie
6505 txn->cookie_first_date = 0;
6506 txn->cookie_last_date = 0;
6507 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006508 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6509 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006510 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006511 txn->flags &= ~TX_CK_MASK;
6512 txn->flags |= TX_CK_EXPIRED;
6513 delim = val_beg; // let's pretend we have not found the cookie
6514 txn->cookie_first_date = 0;
6515 txn->cookie_last_date = 0;
6516 }
6517
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006518 /* Here, we'll look for the first running server which supports the cookie.
6519 * This allows to share a same cookie between several servers, for example
6520 * to dedicate backup servers to specific servers only.
6521 * However, to prevent clients from sticking to cookie-less backup server
6522 * when they have incidentely learned an empty cookie, we simply ignore
6523 * empty cookies and mark them as invalid.
6524 * The same behaviour is applied when persistence must be ignored.
6525 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006526 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006527 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006528
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006529 while (srv) {
6530 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6531 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006532 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006533 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006534 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006535 /* we found the server and we can use it */
6536 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006537 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006538 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006539 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006540 break;
6541 } else {
6542 /* we found a server, but it's down,
6543 * mark it as such and go on in case
6544 * another one is available.
6545 */
6546 txn->flags &= ~TX_CK_MASK;
6547 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006548 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006549 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006550 srv = srv->next;
6551 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006552
Willy Tarreauf64d1412010-10-07 20:06:11 +02006553 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006554 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006555 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006556 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006557 txn->flags |= TX_CK_UNUSED;
6558 else
6559 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006560 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006561
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006562 /* depending on the cookie mode, we may have to either :
6563 * - delete the complete cookie if we're in insert+indirect mode, so that
6564 * the server never sees it ;
6565 * - remove the server id from the cookie value, and tag the cookie as an
6566 * application cookie so that it does not get accidentely removed later,
6567 * if we're in cookie prefix mode
6568 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006569 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006570 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006571
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006572 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006573 val_end += delta;
6574 next += delta;
6575 hdr_end += delta;
6576 hdr_next += delta;
6577 cur_hdr->len += delta;
6578 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006579
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006580 del_from = NULL;
6581 preserve_hdr = 1; /* we want to keep this cookie */
6582 }
6583 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006584 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006585 del_from = prev;
6586 }
6587 } else {
6588 /* This is not our cookie, so we must preserve it. But if we already
6589 * scheduled another cookie for removal, we cannot remove the
6590 * complete header, but we can remove the previous block itself.
6591 */
6592 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006593
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006594 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006595 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006596 if (att_beg >= del_from)
6597 att_beg += delta;
6598 if (att_end >= del_from)
6599 att_end += delta;
6600 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006601 val_end += delta;
6602 next += delta;
6603 hdr_end += delta;
6604 hdr_next += delta;
6605 cur_hdr->len += delta;
6606 http_msg_move_end(&txn->req, delta);
6607 prev = del_from;
6608 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006609 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006610 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006611
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006612 /* continue with next cookie on this header line */
6613 att_beg = next;
6614 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006615
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006616 /* There are no more cookies on this line.
6617 * We may still have one (or several) marked for deletion at the
6618 * end of the line. We must do this now in two ways :
6619 * - if some cookies must be preserved, we only delete from the
6620 * mark to the end of line ;
6621 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006622 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006623 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006624 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006625 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006626 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006627 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006628 cur_hdr->len += delta;
6629 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006630 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006631
6632 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006633 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6634 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006635 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006636 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006637 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006638 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006639 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006640 }
6641
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006642 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006643 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006644 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006645}
6646
6647
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648/* Iterate the same filter through all response headers contained in <rtr>.
6649 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6650 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006651int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006652{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006653 char *cur_ptr, *cur_end, *cur_next;
6654 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006655 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006656 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006657 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006658
6659 last_hdr = 0;
6660
Willy Tarreauf37954d2018-06-15 18:31:02 +02006661 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006662 old_idx = 0;
6663
6664 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006665 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006666 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006667 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006668 (exp->action == ACT_ALLOW ||
6669 exp->action == ACT_DENY))
6670 return 0;
6671
6672 cur_idx = txn->hdr_idx.v[old_idx].next;
6673 if (!cur_idx)
6674 break;
6675
6676 cur_hdr = &txn->hdr_idx.v[cur_idx];
6677 cur_ptr = cur_next;
6678 cur_end = cur_ptr + cur_hdr->len;
6679 cur_next = cur_end + cur_hdr->cr + 1;
6680
6681 /* Now we have one header between cur_ptr and cur_end,
6682 * and the next header starts at cur_next.
6683 */
6684
Willy Tarreau15a53a42015-01-21 13:39:42 +01006685 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006686 switch (exp->action) {
6687 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006688 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006689 last_hdr = 1;
6690 break;
6691
6692 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006693 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006694 last_hdr = 1;
6695 break;
6696
6697 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006698 len = exp_replace(trash.area,
6699 trash.size, cur_ptr,
6700 exp->replace, pmatch);
6701 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006702 return -1;
6703
Willy Tarreau6e27be12018-08-22 04:46:47 +02006704 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6705
Willy Tarreaua15645d2007-03-18 16:22:39 +01006706 /* FIXME: if the user adds a newline in the replacement, the
6707 * index will not be recalculated for now, and the new line
6708 * will not be counted as a new header.
6709 */
6710
6711 cur_end += delta;
6712 cur_next += delta;
6713 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006714 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006715 break;
6716
6717 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006718 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006719 cur_next += delta;
6720
Willy Tarreaufa355d42009-11-29 18:12:29 +01006721 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006722 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6723 txn->hdr_idx.used--;
6724 cur_hdr->len = 0;
6725 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006726 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006727 break;
6728
6729 }
6730 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006731
6732 /* keep the link from this header to next one in case of later
6733 * removal of next header.
6734 */
6735 old_idx = cur_idx;
6736 }
6737 return 0;
6738}
6739
6740
6741/* Apply the filter to the status line in the response buffer <rtr>.
6742 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6743 * or -1 if a replacement resulted in an invalid status line.
6744 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006745int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006746{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006747 char *cur_ptr, *cur_end;
6748 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006749 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006750 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006751
Willy Tarreau3d300592007-03-18 18:34:41 +01006752 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006753 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006754 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006755 (exp->action == ACT_ALLOW ||
6756 exp->action == ACT_DENY))
6757 return 0;
6758 else if (exp->action == ACT_REMOVE)
6759 return 0;
6760
6761 done = 0;
6762
Willy Tarreauf37954d2018-06-15 18:31:02 +02006763 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006764 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006765
6766 /* Now we have the status line between cur_ptr and cur_end */
6767
Willy Tarreau15a53a42015-01-21 13:39:42 +01006768 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006769 switch (exp->action) {
6770 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006771 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006772 done = 1;
6773 break;
6774
6775 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006776 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006777 done = 1;
6778 break;
6779
6780 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006781 len = exp_replace(trash.area, trash.size,
6782 cur_ptr, exp->replace, pmatch);
6783 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006784 return -1;
6785
Willy Tarreau6e27be12018-08-22 04:46:47 +02006786 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6787
Willy Tarreaua15645d2007-03-18 16:22:39 +01006788 /* FIXME: if the user adds a newline in the replacement, the
6789 * index will not be recalculated for now, and the new line
6790 * will not be counted as a new header.
6791 */
6792
Willy Tarreaufa355d42009-11-29 18:12:29 +01006793 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006794 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006795 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006796 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006797 cur_ptr, cur_end + 1,
6798 NULL, NULL);
6799 if (unlikely(!cur_end))
6800 return -1;
6801
6802 /* we have a full respnse and we know that we have either a CR
6803 * or an LF at <ptr>.
6804 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006805 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006806 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006807 /* there is no point trying this regex on headers */
6808 return 1;
6809 }
6810 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006811 return done;
6812}
6813
6814
6815
6816/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006817 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006818 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6819 * unparsable response.
6820 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006821int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006822{
Willy Tarreau192252e2015-04-04 01:47:55 +02006823 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006824 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006825 struct hdr_exp *exp;
6826
6827 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006828 int ret;
6829
6830 /*
6831 * The interleaving of transformations and verdicts
6832 * makes it difficult to decide to continue or stop
6833 * the evaluation.
6834 */
6835
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006836 if (txn->flags & TX_SVDENY)
6837 break;
6838
Willy Tarreau3d300592007-03-18 18:34:41 +01006839 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006840 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6841 exp->action == ACT_PASS)) {
6842 exp = exp->next;
6843 continue;
6844 }
6845
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006846 /* if this filter had a condition, evaluate it now and skip to
6847 * next filter if the condition does not match.
6848 */
6849 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006850 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006851 ret = acl_pass(ret);
6852 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6853 ret = !ret;
6854 if (!ret)
6855 continue;
6856 }
6857
Willy Tarreaua15645d2007-03-18 16:22:39 +01006858 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006859 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006860 if (unlikely(ret < 0))
6861 return -1;
6862
6863 if (likely(ret == 0)) {
6864 /* The filter did not match the response, it can be
6865 * iterated through all headers.
6866 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006867 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6868 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006869 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006870 }
6871 return 0;
6872}
6873
6874
Willy Tarreaua15645d2007-03-18 16:22:39 +01006875/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006876 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006877 * desirable to call it only when needed. This function is also used when we
6878 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006879 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006880void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006881{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006882 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006883 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006884 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006885 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006886 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006887 char *hdr_beg, *hdr_end, *hdr_next;
6888 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006889
Willy Tarreaua15645d2007-03-18 16:22:39 +01006890 /* Iterate through the headers.
6891 * we start with the start line.
6892 */
6893 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006894 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006895
6896 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6897 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006898 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006899
6900 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006901 hdr_beg = hdr_next;
6902 hdr_end = hdr_beg + cur_hdr->len;
6903 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006904
Willy Tarreau24581ba2010-08-31 22:39:35 +02006905 /* We have one full header between hdr_beg and hdr_end, and the
6906 * next header starts at hdr_next. We're only interested in
6907 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006908 */
6909
Willy Tarreau24581ba2010-08-31 22:39:35 +02006910 is_cookie2 = 0;
6911 prev = hdr_beg + 10;
6912 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006913 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006914 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6915 if (!val) {
6916 old_idx = cur_idx;
6917 continue;
6918 }
6919 is_cookie2 = 1;
6920 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006921 }
6922
Willy Tarreau24581ba2010-08-31 22:39:35 +02006923 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6924 * <prev> points to the colon.
6925 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006926 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006927
Willy Tarreau24581ba2010-08-31 22:39:35 +02006928 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6929 * check-cache is enabled) and we are not interested in checking
6930 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006931 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006932 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006933 return;
6934
Willy Tarreau24581ba2010-08-31 22:39:35 +02006935 /* OK so now we know we have to process this response cookie.
6936 * The format of the Set-Cookie header is slightly different
6937 * from the format of the Cookie header in that it does not
6938 * support the comma as a cookie delimiter (thus the header
6939 * cannot be folded) because the Expires attribute described in
6940 * the original Netscape's spec may contain an unquoted date
6941 * with a comma inside. We have to live with this because
6942 * many browsers don't support Max-Age and some browsers don't
6943 * support quoted strings. However the Set-Cookie2 header is
6944 * clean.
6945 *
6946 * We have to keep multiple pointers in order to support cookie
6947 * removal at the beginning, middle or end of header without
6948 * corrupting the header (in case of set-cookie2). A special
6949 * pointer, <scav> points to the beginning of the set-cookie-av
6950 * fields after the first semi-colon. The <next> pointer points
6951 * either to the end of line (set-cookie) or next unquoted comma
6952 * (set-cookie2). All of these headers are valid :
6953 *
6954 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6955 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6956 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6957 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6958 * | | | | | | | | | |
6959 * | | | | | | | | +-> next hdr_end <--+
6960 * | | | | | | | +------------> scav
6961 * | | | | | | +--------------> val_end
6962 * | | | | | +--------------------> val_beg
6963 * | | | | +----------------------> equal
6964 * | | | +------------------------> att_end
6965 * | | +----------------------------> att_beg
6966 * | +------------------------------> prev
6967 * +-----------------------------------------> hdr_beg
6968 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006969
Willy Tarreau24581ba2010-08-31 22:39:35 +02006970 for (; prev < hdr_end; prev = next) {
6971 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006972
Willy Tarreau24581ba2010-08-31 22:39:35 +02006973 /* find att_beg */
6974 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006975 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006976 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006977
Willy Tarreau24581ba2010-08-31 22:39:35 +02006978 /* find att_end : this is the first character after the last non
6979 * space before the equal. It may be equal to hdr_end.
6980 */
6981 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006982
Willy Tarreau24581ba2010-08-31 22:39:35 +02006983 while (equal < hdr_end) {
6984 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6985 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006986 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006987 continue;
6988 att_end = equal;
6989 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006990
Willy Tarreau24581ba2010-08-31 22:39:35 +02006991 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6992 * is between <att_beg> and <equal>, both may be identical.
6993 */
6994
6995 /* look for end of cookie if there is an equal sign */
6996 if (equal < hdr_end && *equal == '=') {
6997 /* look for the beginning of the value */
6998 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006999 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007000 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007001
Willy Tarreau24581ba2010-08-31 22:39:35 +02007002 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02007003 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007004
7005 /* make val_end point to the first white space or delimitor after the value */
7006 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007007 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007008 val_end--;
7009 } else {
7010 /* <equal> points to next comma, semi-colon or EOL */
7011 val_beg = val_end = next = equal;
7012 }
7013
7014 if (next < hdr_end) {
7015 /* Set-Cookie2 supports multiple cookies, and <next> points to
7016 * a colon or semi-colon before the end. So skip all attr-value
7017 * pairs and look for the next comma. For Set-Cookie, since
7018 * commas are permitted in values, skip to the end.
7019 */
7020 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02007021 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007022 else
7023 next = hdr_end;
7024 }
7025
7026 /* Now everything is as on the diagram above */
7027
7028 /* Ignore cookies with no equal sign */
7029 if (equal == val_end)
7030 continue;
7031
7032 /* If there are spaces around the equal sign, we need to
7033 * strip them otherwise we'll get trouble for cookie captures,
7034 * or even for rewrites. Since this happens extremely rarely,
7035 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007036 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007037 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7038 int stripped_before = 0;
7039 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007040
Willy Tarreau24581ba2010-08-31 22:39:35 +02007041 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007042 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007043 equal += stripped_before;
7044 val_beg += stripped_before;
7045 }
7046
7047 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007048 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007049 val_beg += stripped_after;
7050 stripped_before += stripped_after;
7051 }
7052
7053 val_end += stripped_before;
7054 next += stripped_before;
7055 hdr_end += stripped_before;
7056 hdr_next += stripped_before;
7057 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007058 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007059 }
7060
7061 /* First, let's see if we want to capture this cookie. We check
7062 * that we don't already have a server side cookie, because we
7063 * can only capture one. Also as an optimisation, we ignore
7064 * cookies shorter than the declared name.
7065 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007066 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007067 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007068 (val_end - att_beg >= sess->fe->capture_namelen) &&
7069 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007070 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007071 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007072 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007073 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007074 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007075 if (log_len > sess->fe->capture_len)
7076 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007077 memcpy(txn->srv_cookie, att_beg, log_len);
7078 txn->srv_cookie[log_len] = 0;
7079 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007080 }
7081
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007082 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007083 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007084 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007085 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7086 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007087 /* assume passive cookie by default */
7088 txn->flags &= ~TX_SCK_MASK;
7089 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007090
7091 /* If the cookie is in insert mode on a known server, we'll delete
7092 * this occurrence because we'll insert another one later.
7093 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007094 * a direct access.
7095 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007096 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007097 /* The "preserve" flag was set, we don't want to touch the
7098 * server's cookie.
7099 */
7100 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007101 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007102 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007103 /* this cookie must be deleted */
7104 if (*prev == ':' && next == hdr_end) {
7105 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007106 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007107 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7108 txn->hdr_idx.used--;
7109 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007110 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007111 hdr_next += delta;
7112 http_msg_move_end(&txn->rsp, delta);
7113 /* note: while both invalid now, <next> and <hdr_end>
7114 * are still equal, so the for() will stop as expected.
7115 */
7116 } else {
7117 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007118 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007119 next = prev;
7120 hdr_end += delta;
7121 hdr_next += delta;
7122 cur_hdr->len += delta;
7123 http_msg_move_end(&txn->rsp, delta);
7124 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007125 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007126 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007127 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007128 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007129 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007130 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007131 * with this server since we know it.
7132 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007133 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007134 next += delta;
7135 hdr_end += delta;
7136 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007137 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007138 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007139
Willy Tarreauf1348312010-10-07 15:54:11 +02007140 txn->flags &= ~TX_SCK_MASK;
7141 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007142 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007143 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007144 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007145 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007146 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007147 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007148 next += delta;
7149 hdr_end += delta;
7150 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007151 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007152 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007153
Willy Tarreau827aee92011-03-10 16:55:02 +01007154 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007155 txn->flags &= ~TX_SCK_MASK;
7156 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007157 }
7158 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007159 /* that's done for this cookie, check the next one on the same
7160 * line when next != hdr_end (only if is_cookie2).
7161 */
7162 }
7163 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007165 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007166}
7167
7168
Willy Tarreaua15645d2007-03-18 16:22:39 +01007169/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007170 * Parses the Cache-Control and Pragma request header fields to determine if
7171 * the request may be served from the cache and/or if it is cacheable. Updates
7172 * s->txn->flags.
7173 */
7174void check_request_for_cacheability(struct stream *s, struct channel *chn)
7175{
7176 struct http_txn *txn = s->txn;
7177 char *p1, *p2;
7178 char *cur_ptr, *cur_end, *cur_next;
7179 int pragma_found;
7180 int cc_found;
7181 int cur_idx;
7182
7183 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7184 return; /* nothing more to do here */
7185
7186 cur_idx = 0;
7187 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007188 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007189
7190 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7191 struct hdr_idx_elem *cur_hdr;
7192 int val;
7193
7194 cur_hdr = &txn->hdr_idx.v[cur_idx];
7195 cur_ptr = cur_next;
7196 cur_end = cur_ptr + cur_hdr->len;
7197 cur_next = cur_end + cur_hdr->cr + 1;
7198
7199 /* We have one full header between cur_ptr and cur_end, and the
7200 * next header starts at cur_next.
7201 */
7202
7203 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7204 if (val) {
7205 if ((cur_end - (cur_ptr + val) >= 8) &&
7206 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7207 pragma_found = 1;
7208 continue;
7209 }
7210 }
7211
William Lallemand8a16fe02018-05-22 11:04:33 +02007212 /* Don't use the cache and don't try to store if we found the
7213 * Authorization header */
7214 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7215 if (val) {
7216 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7217 txn->flags |= TX_CACHE_IGNORE;
7218 continue;
7219 }
7220
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007221 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7222 if (!val)
7223 continue;
7224
7225 /* OK, right now we know we have a cache-control header at cur_ptr */
7226 cc_found = 1;
7227 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7228
7229 if (p1 >= cur_end) /* no more info */
7230 continue;
7231
7232 /* p1 is at the beginning of the value */
7233 p2 = p1;
7234 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7235 p2++;
7236
7237 /* we have a complete value between p1 and p2. We don't check the
7238 * values after max-age, max-stale nor min-fresh, we simply don't
7239 * use the cache when they're specified.
7240 */
7241 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7242 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7243 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7244 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7245 txn->flags |= TX_CACHE_IGNORE;
7246 continue;
7247 }
7248
7249 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7250 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7251 continue;
7252 }
7253 }
7254
7255 /* RFC7234#5.4:
7256 * When the Cache-Control header field is also present and
7257 * understood in a request, Pragma is ignored.
7258 * When the Cache-Control header field is not present in a
7259 * request, caches MUST consider the no-cache request
7260 * pragma-directive as having the same effect as if
7261 * "Cache-Control: no-cache" were present.
7262 */
7263 if (!cc_found && pragma_found)
7264 txn->flags |= TX_CACHE_IGNORE;
7265}
7266
7267/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007268 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007269 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007270void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007271{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007272 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007273 char *p1, *p2;
7274
7275 char *cur_ptr, *cur_end, *cur_next;
7276 int cur_idx;
7277
Willy Tarreau12b32f22017-12-21 16:08:09 +01007278 if (txn->status < 200) {
7279 /* do not try to cache interim responses! */
7280 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007281 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007282 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007283
7284 /* Iterate through the headers.
7285 * we start with the start line.
7286 */
7287 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007288 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007289
7290 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7291 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007292 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007293
7294 cur_hdr = &txn->hdr_idx.v[cur_idx];
7295 cur_ptr = cur_next;
7296 cur_end = cur_ptr + cur_hdr->len;
7297 cur_next = cur_end + cur_hdr->cr + 1;
7298
7299 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007300 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301 */
7302
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007303 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7304 if (val) {
7305 if ((cur_end - (cur_ptr + val) >= 8) &&
7306 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7307 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7308 return;
7309 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007310 }
7311
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007312 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7313 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007314 continue;
7315
7316 /* OK, right now we know we have a cache-control header at cur_ptr */
7317
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007318 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007319
7320 if (p1 >= cur_end) /* no more info */
7321 continue;
7322
7323 /* p1 is at the beginning of the value */
7324 p2 = p1;
7325
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007326 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007327 p2++;
7328
7329 /* we have a complete value between p1 and p2 */
7330 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007331 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7332 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7333 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7334 continue;
7335 }
7336
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337 /* we have something of the form no-cache="set-cookie" */
7338 if ((cur_end - p1 >= 21) &&
7339 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7340 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007341 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007342 continue;
7343 }
7344
7345 /* OK, so we know that either p2 points to the end of string or to a comma */
7346 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007347 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007348 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007349 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350 return;
7351 }
7352
7353 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007354 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007355 continue;
7356 }
7357 }
7358}
7359
Willy Tarreau58f10d72006-12-04 02:26:12 +01007360
Willy Tarreaub2513902006-12-17 14:52:38 +01007361/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007362 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007363 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007364 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007365 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007366 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007367 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007368 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007369 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007370int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007371{
7372 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007373 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007374 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007375
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007376 if (!uri_auth)
7377 return 0;
7378
Cyril Bonté70be45d2010-10-12 00:14:35 +02007379 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007380 return 0;
7381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007382 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007383 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007384 return 0;
7385
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007386 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007387 return 0;
7388
Willy Tarreaub2513902006-12-17 14:52:38 +01007389 return 1;
7390}
7391
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007392/* Append the description of what is present in error snapshot <es> into <out>.
7393 * The description must be small enough to always fit in a trash. The output
7394 * buffer may be the trash so the trash must not be used inside this function.
7395 */
7396void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7397{
7398 chunk_appendf(&trash,
7399 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7400 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7401 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7402 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7403 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7404 es->ctx.http.m_flags, es->ctx.http.m_clen,
7405 es->ctx.http.m_blen, es->ctx.http.b_flags);
7406}
7407
Willy Tarreau4076a152009-04-02 15:18:36 +02007408/*
7409 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007410 * By default it tries to report the error position as msg->err_pos. However if
7411 * this one is not set, it will then report msg->next, which is the last known
7412 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007413 * displays buffers as a contiguous area starting at buf->p. The direction is
7414 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007415 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007416void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007417 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007418 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007419{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007420 union error_snapshot_ctx ctx;
7421 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007422
Willy Tarreauef3ca732018-09-07 15:47:35 +02007423 /* http-specific part now */
7424 ctx.http.sid = s->uniq_id;
7425 ctx.http.state = state;
7426 ctx.http.b_flags = msg->chn->flags;
7427 ctx.http.s_flags = s->flags;
7428 ctx.http.t_flags = s->txn->flags;
7429 ctx.http.m_flags = msg->flags;
7430 ctx.http.m_clen = msg->chunk_len;
7431 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007432
Willy Tarreauef3ca732018-09-07 15:47:35 +02007433 ofs = msg->chn->total - ci_data(msg->chn);
7434 if (ofs < 0)
7435 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007436
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007437 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007438 other_end, s->target,
7439 strm_sess(s), &msg->chn->buf,
7440 ofs, co_data(msg->chn),
7441 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7442 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007443}
Willy Tarreaub2513902006-12-17 14:52:38 +01007444
Willy Tarreau294c4732011-12-16 21:35:50 +01007445/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7446 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7447 * performed over the whole headers. Otherwise it must contain a valid header
7448 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7449 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7450 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7451 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007452 * -1. The value fetch stops at commas, so this function is suited for use with
7453 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007454 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007455 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007456unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007457 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007458 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007459{
Willy Tarreau294c4732011-12-16 21:35:50 +01007460 struct hdr_ctx local_ctx;
7461 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007462 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007463 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007464 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007465
Willy Tarreau294c4732011-12-16 21:35:50 +01007466 if (!ctx) {
7467 local_ctx.idx = 0;
7468 ctx = &local_ctx;
7469 }
7470
Willy Tarreaubce70882009-09-07 11:51:47 +02007471 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007472 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007473 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007474 occ--;
7475 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007476 *vptr = ctx->line + ctx->val;
7477 *vlen = ctx->vlen;
7478 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007479 }
7480 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007481 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007482 }
7483
7484 /* negative occurrence, we scan all the list then walk back */
7485 if (-occ > MAX_HDR_HISTORY)
7486 return 0;
7487
Willy Tarreau294c4732011-12-16 21:35:50 +01007488 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007489 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007490 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7491 len_hist[hist_ptr] = ctx->vlen;
7492 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007493 hist_ptr = 0;
7494 found++;
7495 }
7496 if (-occ > found)
7497 return 0;
7498 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007499 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7500 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7501 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007502 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007503 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007504 if (hist_ptr >= MAX_HDR_HISTORY)
7505 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007506 *vptr = ptr_hist[hist_ptr];
7507 *vlen = len_hist[hist_ptr];
7508 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007509}
7510
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007511/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7512 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7513 * performed over the whole headers. Otherwise it must contain a valid header
7514 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7515 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7516 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7517 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7518 * -1. This function differs from http_get_hdr() in that it only returns full
7519 * line header values and does not stop at commas.
7520 * The return value is 0 if nothing was found, or non-zero otherwise.
7521 */
7522unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7523 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007524 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007525{
7526 struct hdr_ctx local_ctx;
7527 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007528 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007529 unsigned int hist_ptr;
7530 int found;
7531
7532 if (!ctx) {
7533 local_ctx.idx = 0;
7534 ctx = &local_ctx;
7535 }
7536
7537 if (occ >= 0) {
7538 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007539 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007540 occ--;
7541 if (occ <= 0) {
7542 *vptr = ctx->line + ctx->val;
7543 *vlen = ctx->vlen;
7544 return 1;
7545 }
7546 }
7547 return 0;
7548 }
7549
7550 /* negative occurrence, we scan all the list then walk back */
7551 if (-occ > MAX_HDR_HISTORY)
7552 return 0;
7553
7554 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007555 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007556 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7557 len_hist[hist_ptr] = ctx->vlen;
7558 if (++hist_ptr >= MAX_HDR_HISTORY)
7559 hist_ptr = 0;
7560 found++;
7561 }
7562 if (-occ > found)
7563 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007564
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007565 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007566 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7567 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7568 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007569 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007570 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007571 if (hist_ptr >= MAX_HDR_HISTORY)
7572 hist_ptr -= MAX_HDR_HISTORY;
7573 *vptr = ptr_hist[hist_ptr];
7574 *vlen = len_hist[hist_ptr];
7575 return 1;
7576}
7577
Willy Tarreaubaaee002006-06-26 02:48:02 +02007578/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007579 * Print a debug line with a header. Always stop at the first CR or LF char,
7580 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7581 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007582 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007583void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007584{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007585 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007586 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007587
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007588 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007589 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007590 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007591 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 +02007592
7593 for (max = 0; start + max < end; max++)
7594 if (start[max] == '\r' || start[max] == '\n')
7595 break;
7596
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007597 UBOUND(max, trash.size - trash.data - 3);
7598 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7599 trash.area[trash.data++] = '\n';
7600 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007601}
7602
Willy Tarreaueee5b512015-04-03 23:46:31 +02007603
7604/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7605 * The hdr_idx is allocated as well. In case of allocation failure, everything
7606 * allocated is freed and NULL is returned. Otherwise the new transaction is
7607 * assigned to the stream and returned.
7608 */
7609struct http_txn *http_alloc_txn(struct stream *s)
7610{
7611 struct http_txn *txn = s->txn;
7612
7613 if (txn)
7614 return txn;
7615
Willy Tarreaubafbe012017-11-24 17:34:44 +01007616 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007617 if (!txn)
7618 return txn;
7619
7620 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007621 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007622 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007623 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007624 return NULL;
7625 }
7626
7627 s->txn = txn;
7628 return txn;
7629}
7630
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007631void http_txn_reset_req(struct http_txn *txn)
7632{
7633 txn->req.flags = 0;
7634 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7635 txn->req.next = 0;
7636 txn->req.chunk_len = 0LL;
7637 txn->req.body_len = 0LL;
7638 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7639}
7640
7641void http_txn_reset_res(struct http_txn *txn)
7642{
7643 txn->rsp.flags = 0;
7644 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7645 txn->rsp.next = 0;
7646 txn->rsp.chunk_len = 0LL;
7647 txn->rsp.body_len = 0LL;
7648 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7649}
7650
Willy Tarreau0937bc42009-12-22 15:03:09 +01007651/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007652 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007653 * the required fields are properly allocated and that we only need to (re)init
7654 * them. This should be used before processing any new request.
7655 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007656void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007657{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007658 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007659 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007660
7661 txn->flags = 0;
7662 txn->status = -1;
7663
Willy Tarreauf64d1412010-10-07 20:06:11 +02007664 txn->cookie_first_date = 0;
7665 txn->cookie_last_date = 0;
7666
Willy Tarreaueee5b512015-04-03 23:46:31 +02007667 txn->srv_cookie = NULL;
7668 txn->cli_cookie = NULL;
7669 txn->uri = NULL;
7670
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007671 http_txn_reset_req(txn);
7672 http_txn_reset_res(txn);
7673
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007674 txn->req.chn = &s->req;
7675 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007676
7677 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007678
7679 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7680 if (fe->options2 & PR_O2_REQBUG_OK)
7681 txn->req.err_pos = -1; /* let buggy requests pass */
7682
Willy Tarreau0937bc42009-12-22 15:03:09 +01007683 if (txn->hdr_idx.v)
7684 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007685
7686 vars_init(&s->vars_txn, SCOPE_TXN);
7687 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007688}
7689
7690/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007691void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007692{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007693 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007694 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007695
7696 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007697 pool_free(pool_head_requri, txn->uri);
7698 pool_free(pool_head_capture, txn->cli_cookie);
7699 pool_free(pool_head_capture, txn->srv_cookie);
7700 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007701
William Lallemanda73203e2012-03-12 12:48:57 +01007702 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007703 txn->uri = NULL;
7704 txn->srv_cookie = NULL;
7705 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007706
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007707 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007708 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007709 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007710 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007711 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007712 }
7713
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007714 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007715 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007716 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007717 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007718 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007719 }
7720
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007721 if (!LIST_ISEMPTY(&s->vars_txn.head))
7722 vars_prune(&s->vars_txn, s->sess, s);
7723 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7724 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007725}
7726
7727/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007728void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007729{
7730 http_end_txn(s);
7731 http_init_txn(s);
7732
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007733 /* reinitialise the current rule list pointer to NULL. We are sure that
7734 * any rulelist match the NULL pointer.
7735 */
7736 s->current_rule_list = NULL;
7737
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007738 s->be = strm_fe(s);
7739 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007740 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007741 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007742 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007743 /* re-init store persistence */
7744 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007745 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007746
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007747 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007748
Willy Tarreau739cfba2010-01-25 23:11:14 +01007749 /* We must trim any excess data from the response buffer, because we
7750 * may have blocked an invalid response from a server that we don't
7751 * want to accidentely forward once we disable the analysers, nor do
7752 * we want those data to come along with next response. A typical
7753 * example of such data would be from a buggy server responding to
7754 * a HEAD with some data, or sending more than the advertised
7755 * content-length.
7756 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007757 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007758 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007759
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007760 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007761 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007762
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007763 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007764 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007765
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007766 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007767 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007768
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007769 s->req.rex = TICK_ETERNITY;
7770 s->req.wex = TICK_ETERNITY;
7771 s->req.analyse_exp = TICK_ETERNITY;
7772 s->res.rex = TICK_ETERNITY;
7773 s->res.wex = TICK_ETERNITY;
7774 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007775 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007776}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007777
Willy Tarreau79e57332018-10-02 16:01:16 +02007778/* This function executes one of the set-{method,path,query,uri} actions. It
7779 * takes the string from the variable 'replace' with length 'len', then modifies
7780 * the relevant part of the request line accordingly. Then it updates various
7781 * pointers to the next elements which were moved, and the total buffer length.
7782 * It finds the action to be performed in p[2], previously filled by function
7783 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7784 * error, though this can be revisited when this code is finally exploited.
7785 *
7786 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7787 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007788 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007789 * In query string case, the mark question '?' must be set at the start of the
7790 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007791 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007792int http_replace_req_line(int action, const char *replace, int len,
7793 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007794{
Willy Tarreau79e57332018-10-02 16:01:16 +02007795 struct http_txn *txn = s->txn;
7796 char *cur_ptr, *cur_end;
7797 int offset = 0;
7798 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007799
Willy Tarreau79e57332018-10-02 16:01:16 +02007800 switch (action) {
7801 case 0: // method
7802 cur_ptr = ci_head(&s->req);
7803 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007804
Willy Tarreau79e57332018-10-02 16:01:16 +02007805 /* adjust req line offsets and lengths */
7806 delta = len - offset - (cur_end - cur_ptr);
7807 txn->req.sl.rq.m_l += delta;
7808 txn->req.sl.rq.u += delta;
7809 txn->req.sl.rq.v += delta;
7810 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007811
Willy Tarreau79e57332018-10-02 16:01:16 +02007812 case 1: // path
7813 cur_ptr = http_txn_get_path(txn);
7814 if (!cur_ptr)
7815 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007816
Willy Tarreau79e57332018-10-02 16:01:16 +02007817 cur_end = cur_ptr;
7818 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7819 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007820
Willy Tarreau79e57332018-10-02 16:01:16 +02007821 /* adjust req line offsets and lengths */
7822 delta = len - offset - (cur_end - cur_ptr);
7823 txn->req.sl.rq.u_l += delta;
7824 txn->req.sl.rq.v += delta;
7825 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007826
Willy Tarreau79e57332018-10-02 16:01:16 +02007827 case 2: // query
7828 offset = 1;
7829 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7830 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7831 while (cur_ptr < cur_end && *cur_ptr != '?')
7832 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007833
Willy Tarreau79e57332018-10-02 16:01:16 +02007834 /* skip the question mark or indicate that we must insert it
7835 * (but only if the format string is not empty then).
7836 */
7837 if (cur_ptr < cur_end)
7838 cur_ptr++;
7839 else if (len > 1)
7840 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007841
Willy Tarreau79e57332018-10-02 16:01:16 +02007842 /* adjust req line offsets and lengths */
7843 delta = len - offset - (cur_end - cur_ptr);
7844 txn->req.sl.rq.u_l += delta;
7845 txn->req.sl.rq.v += delta;
7846 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007847
Willy Tarreau79e57332018-10-02 16:01:16 +02007848 case 3: // uri
7849 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7850 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007851
Willy Tarreau79e57332018-10-02 16:01:16 +02007852 /* adjust req line offsets and lengths */
7853 delta = len - offset - (cur_end - cur_ptr);
7854 txn->req.sl.rq.u_l += delta;
7855 txn->req.sl.rq.v += delta;
7856 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007857
Willy Tarreau79e57332018-10-02 16:01:16 +02007858 default:
7859 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007860 }
7861
Willy Tarreau79e57332018-10-02 16:01:16 +02007862 /* commit changes and adjust end of message */
7863 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7864 txn->req.sl.rq.l += delta;
7865 txn->hdr_idx.v[0].len += delta;
7866 http_msg_move_end(&txn->req, delta);
7867 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007868}
7869
Willy Tarreau79e57332018-10-02 16:01:16 +02007870/* This function replace the HTTP status code and the associated message. The
7871 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007872 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007873void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007874{
Willy Tarreau79e57332018-10-02 16:01:16 +02007875 struct http_txn *txn = s->txn;
7876 char *cur_ptr, *cur_end;
7877 int delta;
7878 char *res;
7879 int c_l;
7880 const char *msg = reason;
7881 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007882
Willy Tarreau79e57332018-10-02 16:01:16 +02007883 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007884
Willy Tarreau79e57332018-10-02 16:01:16 +02007885 res = ultoa_o(status, trash.area, trash.size);
7886 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007887
Willy Tarreau79e57332018-10-02 16:01:16 +02007888 trash.area[c_l] = ' ';
7889 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007890
Willy Tarreau79e57332018-10-02 16:01:16 +02007891 /* Do we have a custom reason format string? */
7892 if (msg == NULL)
7893 msg = http_get_reason(status);
7894 msg_len = strlen(msg);
7895 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7896 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007897
Willy Tarreau79e57332018-10-02 16:01:16 +02007898 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7899 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007900
Willy Tarreau79e57332018-10-02 16:01:16 +02007901 /* commit changes and adjust message */
7902 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7903 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007904
Willy Tarreau79e57332018-10-02 16:01:16 +02007905 /* adjust res line offsets and lengths */
7906 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7907 txn->rsp.sl.st.c_l = c_l;
7908 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007909
Willy Tarreau79e57332018-10-02 16:01:16 +02007910 delta = trash.data - (cur_end - cur_ptr);
7911 txn->rsp.sl.st.l += delta;
7912 txn->hdr_idx.v[0].len += delta;
7913 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007914}
7915
Willy Tarreau8797c062007-05-07 00:55:35 +02007916__attribute__((constructor))
7917static void __http_protocol_init(void)
7918{
Willy Tarreau8797c062007-05-07 00:55:35 +02007919}
7920
7921
Willy Tarreau58f10d72006-12-04 02:26:12 +01007922/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007923 * Local variables:
7924 * c-indent-level: 8
7925 * c-basic-offset: 8
7926 * End:
7927 */