blob: 9678d512b537ae6587c3fdb85399248b9e4cad7b [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 Faulet45073512018-07-20 10:16:29 +0200867 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 +0100868 now_ms, __FUNCTION__,
869 s,
870 req,
871 req->rex, req->wex,
872 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200873 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100874 req->analysers);
875
Willy Tarreau52a0c602009-08-16 22:45:38 +0200876 /* we're speaking HTTP here, so let's speak HTTP to the client */
877 s->srv_error = http_return_srv_error;
878
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300879 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200880 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300881 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
882
Willy Tarreau83e3af02009-12-28 17:39:57 +0100883 /* There's a protected area at the end of the buffer for rewriting
884 * purposes. We don't want to start to parse the request if the
885 * protected area is affected, because we may have to move processed
886 * data later, which is much more complicated.
887 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200888 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200889 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100890 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200891 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100892 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100893 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200894 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200895 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100896 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100897 return 0;
898 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200899 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200900 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200901 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100902 }
903
Willy Tarreauf37954d2018-06-15 18:31:02 +0200904 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100905 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100906 }
907
Willy Tarreau59234e92008-11-30 23:51:27 +0100908 /* 1: we might have to print this header in debug mode */
909 if (unlikely((global.mode & MODE_DEBUG) &&
910 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200911 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100912 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100913
Willy Tarreauf37954d2018-06-15 18:31:02 +0200914 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200915 /* this is a bit complex : in case of error on the request line,
916 * we know that rq.l is still zero, so we display only the part
917 * up to the end of the line (truncated by debug_hdr).
918 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200919 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100920 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100921
Willy Tarreau59234e92008-11-30 23:51:27 +0100922 sol += hdr_idx_first_pos(&txn->hdr_idx);
923 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100924
Willy Tarreau59234e92008-11-30 23:51:27 +0100925 while (cur_idx) {
926 eol = sol + txn->hdr_idx.v[cur_idx].len;
927 debug_hdr("clihdr", s, sol, eol);
928 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
929 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100930 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100931 }
932
Willy Tarreau58f10d72006-12-04 02:26:12 +0100933
Willy Tarreau59234e92008-11-30 23:51:27 +0100934 /*
935 * Now we quickly check if we have found a full valid request.
936 * If not so, we check the FD and buffer states before leaving.
937 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100938 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100939 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200940 * on a keep-alive stream, if we encounter and error, close, t/o,
941 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100942 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200943 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200944 * Last, we may increase some tracked counters' http request errors on
945 * the cases that are deliberately the client's fault. For instance,
946 * a timeout or connection reset is not counted as an error. However
947 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100948 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100949
Willy Tarreau655dce92009-11-08 13:10:58 +0100950 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100951 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100952 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100953 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100954 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200955 stream_inc_http_req_ctr(s);
956 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200957 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100958 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100959 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100960
Willy Tarreau59234e92008-11-30 23:51:27 +0100961 /* 1: Since we are in header mode, if there's no space
962 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200963 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100964 * must terminate it now.
965 */
Willy Tarreau23752332018-06-15 14:54:53 +0200966 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100967 /* FIXME: check if URI is set and return Status
968 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100969 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200970 stream_inc_http_req_ctr(s);
971 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200972 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200973 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200974 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100975 goto return_bad_req;
976 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100977
Willy Tarreau59234e92008-11-30 23:51:27 +0100978 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200979 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200980 if (!(s->flags & SF_ERR_MASK))
981 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100982
Willy Tarreaufcffa692010-01-10 14:21:19 +0100983 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100984 goto failed_keep_alive;
985
Willy Tarreau0f228a02015-05-01 15:37:53 +0200986 if (sess->fe->options & PR_O_IGNORE_PRB)
987 goto failed_keep_alive;
988
Willy Tarreau59234e92008-11-30 23:51:27 +0100989 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200990 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200991 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200992 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200993 }
994
Willy Tarreaudc979f22012-12-04 10:39:01 +0100995 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100996 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100997 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100998 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +0100999 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001000 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001001 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001002 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001003 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001004 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001005
Willy Tarreaue7dff022015-04-03 01:14:29 +02001006 if (!(s->flags & SF_FINST_MASK))
1007 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001008 return 0;
1009 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001010
Willy Tarreau59234e92008-11-30 23:51:27 +01001011 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001012 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001013 if (!(s->flags & SF_ERR_MASK))
1014 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001015
Willy Tarreaufcffa692010-01-10 14:21:19 +01001016 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001017 goto failed_keep_alive;
1018
Willy Tarreau0f228a02015-05-01 15:37:53 +02001019 if (sess->fe->options & PR_O_IGNORE_PRB)
1020 goto failed_keep_alive;
1021
Willy Tarreau59234e92008-11-30 23:51:27 +01001022 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001023 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001024 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001025 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001026 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001027 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001028 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001029 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001030 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001031 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001032
Willy Tarreau87b09662015-04-03 00:22:06 +02001033 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001034 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001035 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001036 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001037 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001038
Willy Tarreaue7dff022015-04-03 01:14:29 +02001039 if (!(s->flags & SF_FINST_MASK))
1040 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001041 return 0;
1042 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001043
Willy Tarreau59234e92008-11-30 23:51:27 +01001044 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001045 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001046 if (!(s->flags & SF_ERR_MASK))
1047 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001048
Willy Tarreaufcffa692010-01-10 14:21:19 +01001049 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001050 goto failed_keep_alive;
1051
Willy Tarreau0f228a02015-05-01 15:37:53 +02001052 if (sess->fe->options & PR_O_IGNORE_PRB)
1053 goto failed_keep_alive;
1054
Willy Tarreau4076a152009-04-02 15:18:36 +02001055 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001056 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001057 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001058 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001059 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001060 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001061 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001062 stream_inc_http_err_ctr(s);
1063 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001064 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001065 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001066 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001067 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001068
Willy Tarreaue7dff022015-04-03 01:14:29 +02001069 if (!(s->flags & SF_FINST_MASK))
1070 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001071 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001072 }
1073
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001074 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001075 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001076 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001077#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001078 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001079 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001080 /* We need more data, we have to re-enable quick-ack in case we
1081 * previously disabled it, otherwise we might cause the client
1082 * to delay next data.
1083 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001084 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001085 }
1086#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001087
Willy Tarreaufcffa692010-01-10 14:21:19 +01001088 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1089 /* If the client starts to talk, let's fall back to
1090 * request timeout processing.
1091 */
1092 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001093 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001094 }
1095
Willy Tarreau59234e92008-11-30 23:51:27 +01001096 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001097 if (!tick_isset(req->analyse_exp)) {
1098 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1099 (txn->flags & TX_WAIT_NEXT_RQ) &&
1100 tick_isset(s->be->timeout.httpka))
1101 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1102 else
1103 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1104 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001105
Willy Tarreau59234e92008-11-30 23:51:27 +01001106 /* we're not ready yet */
1107 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001108
1109 failed_keep_alive:
1110 /* Here we process low-level errors for keep-alive requests. In
1111 * short, if the request is not the first one and it experiences
1112 * a timeout, read error or shutdown, we just silently close so
1113 * that the client can try again.
1114 */
1115 txn->status = 0;
1116 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001117 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001118 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001119 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001120 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001121 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001122 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001123 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001124
Willy Tarreaud787e662009-07-07 10:14:51 +02001125 /* OK now we have a complete HTTP request with indexed headers. Let's
1126 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001127 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001128 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001129 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001130 * byte after the last LF. msg->sov points to the first byte of data.
1131 * msg->eol cannot be trusted because it may have been left uninitialized
1132 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001133 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001134
Willy Tarreau87b09662015-04-03 00:22:06 +02001135 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001136 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001137
Willy Tarreaub16a5742010-01-10 14:46:16 +01001138 if (txn->flags & TX_WAIT_NEXT_RQ) {
1139 /* kill the pending keep-alive timeout */
1140 txn->flags &= ~TX_WAIT_NEXT_RQ;
1141 req->analyse_exp = TICK_ETERNITY;
1142 }
1143
1144
Willy Tarreaud787e662009-07-07 10:14:51 +02001145 /* Maybe we found in invalid header name while we were configured not
1146 * to block on that, so we have to capture it now.
1147 */
1148 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001149 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001150
Willy Tarreau59234e92008-11-30 23:51:27 +01001151 /*
1152 * 1: identify the method
1153 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001154 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001155
1156 /* we can make use of server redirect on GET and HEAD */
1157 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001158 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001159 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001160 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001161 /* PRI is reserved for the HTTP/2 preface */
1162 msg->err_pos = 0;
1163 goto return_bad_req;
1164 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001165
Willy Tarreau59234e92008-11-30 23:51:27 +01001166 /*
1167 * 2: check if the URI matches the monitor_uri.
1168 * We have to do this for every request which gets in, because
1169 * the monitor-uri is defined by the frontend.
1170 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001171 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1172 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001173 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001174 sess->fe->monitor_uri,
1175 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001176 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001177 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001178 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001179 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001180
Willy Tarreaue7dff022015-04-03 01:14:29 +02001181 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001182 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001183
Willy Tarreau59234e92008-11-30 23:51:27 +01001184 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001185 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001186 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001187
Willy Tarreau59234e92008-11-30 23:51:27 +01001188 ret = acl_pass(ret);
1189 if (cond->pol == ACL_COND_UNLESS)
1190 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001191
Willy Tarreau59234e92008-11-30 23:51:27 +01001192 if (ret) {
1193 /* we fail this request, let's return 503 service unavail */
1194 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001195 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001196 if (!(s->flags & SF_ERR_MASK))
1197 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001198 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001199 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001200 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001201
Willy Tarreau59234e92008-11-30 23:51:27 +01001202 /* nothing to fail, let's reply normaly */
1203 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001204 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001205 if (!(s->flags & SF_ERR_MASK))
1206 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001207 goto return_prx_cond;
1208 }
1209
1210 /*
1211 * 3: Maybe we have to copy the original REQURI for the logs ?
1212 * Note: we cannot log anymore if the request has been
1213 * classified as invalid.
1214 */
1215 if (unlikely(s->logs.logwait & LW_REQ)) {
1216 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001217 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001218 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001219
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001220 if (urilen >= global.tune.requri_len )
1221 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001222 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001223 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001224
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001225 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001226 s->do_log(s);
1227 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001228 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001230 }
Willy Tarreau06619262006-12-17 08:37:22 +01001231
Willy Tarreau91852eb2015-05-01 13:26:00 +02001232 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1233 * exactly one digit "." one digit. This check may be disabled using
1234 * option accept-invalid-http-request.
1235 */
1236 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1237 if (msg->sl.rq.v_l != 8) {
1238 msg->err_pos = msg->sl.rq.v;
1239 goto return_bad_req;
1240 }
1241
Willy Tarreauf37954d2018-06-15 18:31:02 +02001242 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1243 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1244 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1245 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001246 msg->err_pos = msg->sl.rq.v + 4;
1247 goto return_bad_req;
1248 }
1249 }
Willy Tarreau13317662015-05-01 13:47:08 +02001250 else {
1251 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1252 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1253 goto return_bad_req;
1254 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001255
Willy Tarreau5b154472009-12-21 20:11:07 +01001256 /* ... and check if the request is HTTP/1.1 or above */
1257 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001258 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1259 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1260 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001261 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001262
1263 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001264 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 +01001265
Willy Tarreau88d349d2010-01-25 12:15:43 +01001266 /* if the frontend has "option http-use-proxy-header", we'll check if
1267 * we have what looks like a proxied connection instead of a connection,
1268 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1269 * Note that this is *not* RFC-compliant, however browsers and proxies
1270 * happen to do that despite being non-standard :-(
1271 * We consider that a request not beginning with either '/' or '*' is
1272 * a proxied connection, which covers both "scheme://location" and
1273 * CONNECT ip:port.
1274 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001275 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001276 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001277 txn->flags |= TX_USE_PX_CONN;
1278
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001279 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001280 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001281
Willy Tarreau59234e92008-11-30 23:51:27 +01001282 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001283 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001284 http_capture_headers(ci_head(req), &txn->hdr_idx,
1285 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001286
Willy Tarreau557f1992015-05-01 10:05:17 +02001287 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1288 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001289 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001290 * The length of a message body is determined by one of the following
1291 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001292 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001293 * 1. Any response to a HEAD request and any response with a 1xx
1294 * (Informational), 204 (No Content), or 304 (Not Modified) status
1295 * code is always terminated by the first empty line after the
1296 * header fields, regardless of the header fields present in the
1297 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001298 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001299 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1300 * the connection will become a tunnel immediately after the empty
1301 * line that concludes the header fields. A client MUST ignore any
1302 * Content-Length or Transfer-Encoding header fields received in
1303 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001304 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001305 * 3. If a Transfer-Encoding header field is present and the chunked
1306 * transfer coding (Section 4.1) is the final encoding, the message
1307 * body length is determined by reading and decoding the chunked
1308 * data until the transfer coding indicates the data is complete.
1309 *
1310 * If a Transfer-Encoding header field is present in a response and
1311 * the chunked transfer coding is not the final encoding, the
1312 * message body length is determined by reading the connection until
1313 * it is closed by the server. If a Transfer-Encoding header field
1314 * is present in a request and the chunked transfer coding is not
1315 * the final encoding, the message body length cannot be determined
1316 * reliably; the server MUST respond with the 400 (Bad Request)
1317 * status code and then close the connection.
1318 *
1319 * If a message is received with both a Transfer-Encoding and a
1320 * Content-Length header field, the Transfer-Encoding overrides the
1321 * Content-Length. Such a message might indicate an attempt to
1322 * perform request smuggling (Section 9.5) or response splitting
1323 * (Section 9.4) and ought to be handled as an error. A sender MUST
1324 * remove the received Content-Length field prior to forwarding such
1325 * a message downstream.
1326 *
1327 * 4. If a message is received without Transfer-Encoding and with
1328 * either multiple Content-Length header fields having differing
1329 * field-values or a single Content-Length header field having an
1330 * invalid value, then the message framing is invalid and the
1331 * recipient MUST treat it as an unrecoverable error. If this is a
1332 * request message, the server MUST respond with a 400 (Bad Request)
1333 * status code and then close the connection. If this is a response
1334 * message received by a proxy, the proxy MUST close the connection
1335 * to the server, discard the received response, and send a 502 (Bad
1336 * Gateway) response to the client. If this is a response message
1337 * received by a user agent, the user agent MUST close the
1338 * connection to the server and discard the received response.
1339 *
1340 * 5. If a valid Content-Length header field is present without
1341 * Transfer-Encoding, its decimal value defines the expected message
1342 * body length in octets. If the sender closes the connection or
1343 * the recipient times out before the indicated number of octets are
1344 * received, the recipient MUST consider the message to be
1345 * incomplete and close the connection.
1346 *
1347 * 6. If this is a request message and none of the above are true, then
1348 * the message body length is zero (no message body is present).
1349 *
1350 * 7. Otherwise, this is a response message without a declared message
1351 * body length, so the message body length is determined by the
1352 * number of octets received prior to the server closing the
1353 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001354 */
1355
Willy Tarreau32b47f42009-10-18 20:55:02 +02001356 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001357 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001358 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001359 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001360 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001361 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001362 /* chunked not last, return badreq */
1363 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001364 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001365 }
1366
Willy Tarreau1c913912015-04-30 10:57:51 +02001367 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001368 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001369 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001370 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001371 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1372 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001373 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001374 signed long long cl;
1375
Willy Tarreauad14f752011-09-02 20:33:27 +02001376 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001377 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001378 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001379 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001380
Willy Tarreauad14f752011-09-02 20:33:27 +02001381 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001382 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001383 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001384 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001385
Willy Tarreauad14f752011-09-02 20:33:27 +02001386 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001387 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001388 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001389 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001390
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001391 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001392 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001393 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001394 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001395
Christopher Fauletbe821b92017-03-30 11:21:53 +02001396 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001397 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001398 }
1399
Willy Tarreau34dfc602015-05-01 10:09:49 +02001400 /* even bodyless requests have a known length */
1401 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001402
Willy Tarreau179085c2014-04-28 16:48:56 +02001403 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1404 * only change if both the request and the config reference something else.
1405 * Option httpclose by itself sets tunnel mode where headers are mangled.
1406 * However, if another mode is set, it will affect it (eg: server-close/
1407 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1408 * if FE and BE have the same settings (common). The method consists in
1409 * checking if options changed between the two calls (implying that either
1410 * one is non-null, or one of them is non-null and we are there for the first
1411 * time.
1412 */
1413 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001414 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001415 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001416
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001417 /* we may have to wait for the request's body */
1418 if ((s->be->options & PR_O_WREQ_BODY) &&
1419 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1420 req->analysers |= AN_REQ_HTTP_BODY;
1421
Willy Tarreau83ece462017-12-21 15:13:09 +01001422 /*
1423 * RFC7234#4:
1424 * A cache MUST write through requests with methods
1425 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1426 * the origin server; i.e., a cache is not allowed
1427 * to generate a reply to such a request before
1428 * having forwarded the request and having received
1429 * a corresponding response.
1430 *
1431 * RFC7231#4.2.1:
1432 * Of the request methods defined by this
1433 * specification, the GET, HEAD, OPTIONS, and TRACE
1434 * methods are defined to be safe.
1435 */
1436 if (likely(txn->meth == HTTP_METH_GET ||
1437 txn->meth == HTTP_METH_HEAD ||
1438 txn->meth == HTTP_METH_OPTIONS ||
1439 txn->meth == HTTP_METH_TRACE))
1440 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1441
Willy Tarreaud787e662009-07-07 10:14:51 +02001442 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001443 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001444 req->analyse_exp = TICK_ETERNITY;
1445 return 1;
1446
1447 return_bad_req:
1448 /* We centralize bad requests processing here */
1449 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1450 /* we detected a parsing error. We want to archive this request
1451 * in the dedicated proxy area for later troubleshooting.
1452 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001453 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001454 }
1455
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001456 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001457 txn->req.msg_state = HTTP_MSG_ERROR;
1458 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001459 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001460
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001461 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001462 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001463 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001464
1465 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001466 if (!(s->flags & SF_ERR_MASK))
1467 s->flags |= SF_ERR_PRXCOND;
1468 if (!(s->flags & SF_FINST_MASK))
1469 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001470
Christopher Faulet0184ea72017-01-05 14:06:34 +01001471 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001472 req->analyse_exp = TICK_ETERNITY;
1473 return 0;
1474}
1475
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001476
Willy Tarreau347a35d2013-11-22 17:51:09 +01001477/* This function prepares an applet to handle the stats. It can deal with the
1478 * "100-continue" expectation, check that admin rules are met for POST requests,
1479 * and program a response message if something was unexpected. It cannot fail
1480 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001481 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001482 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001483 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001484 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001485int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001486{
1487 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001488 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001489 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001490 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001491 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001492 struct uri_auth *uri_auth = s->be->uri_auth;
1493 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001494 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001495
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001496 appctx = si_appctx(si);
1497 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1498 appctx->st1 = appctx->st2 = 0;
1499 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1500 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001501 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001502 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001503
Willy Tarreauf37954d2018-06-15 18:31:02 +02001504 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001505 lookup = uri + uri_auth->uri_len;
1506
1507 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1508 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001509 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001510 break;
1511 }
1512 }
1513
1514 if (uri_auth->refresh) {
1515 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1516 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001517 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001518 break;
1519 }
1520 }
1521 }
1522
1523 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1524 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001525 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001526 break;
1527 }
1528 }
1529
Willy Tarreau1e62df92016-01-11 18:57:53 +01001530 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1531 if (memcmp(h, ";typed", 6) == 0) {
1532 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1533 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1534 break;
1535 }
1536 }
1537
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001538 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1539 if (memcmp(h, ";st=", 4) == 0) {
1540 int i;
1541 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001542 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001543 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1544 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001545 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001546 break;
1547 }
1548 }
1549 break;
1550 }
1551 }
1552
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001553 appctx->ctx.stats.scope_str = 0;
1554 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001555 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1556 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1557 int itx = 0;
1558 const char *h2;
1559 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1560 const char *err;
1561
1562 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1563 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001564 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001565 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1566 itx++;
1567 h++;
1568 }
1569
1570 if (itx > STAT_SCOPE_TXT_MAXLEN)
1571 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001572 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001573
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001574 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001575 memcpy(scope_txt, h2, itx);
1576 scope_txt[itx] = '\0';
1577 err = invalid_char(scope_txt);
1578 if (err) {
1579 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001580 appctx->ctx.stats.scope_str = 0;
1581 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001582 }
1583 break;
1584 }
1585 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001586
1587 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001588 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001589 int ret = 1;
1590
1591 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001592 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 +01001593 ret = acl_pass(ret);
1594 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1595 ret = !ret;
1596 }
1597
1598 if (ret) {
1599 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001600 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001601 break;
1602 }
1603 }
1604
1605 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001606 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001607 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001608 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001609 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1610 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001611 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001612 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001613 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001614 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1615 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001616 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001617 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001618 else {
1619 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001620 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001621 }
1622
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001623 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001624 return 1;
1625}
1626
Lukas Tribus67db8df2013-06-23 17:37:13 +02001627/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1628 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1629 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001630void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001631{
1632#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001633 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001634 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1635#endif
1636#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001637 if (from->ss_family == AF_INET6) {
1638 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001639 /* v4-mapped addresses need IP_TOS */
1640 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1641 else
1642 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1643 }
1644#endif
1645}
1646
Willy Tarreau87b09662015-04-03 00:22:06 +02001647int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001648 const char* name, unsigned int name_len,
1649 const char *str, struct my_regex *re,
1650 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001651{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001652 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001653 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001654 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001655 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1656 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001657 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001658
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001659 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001660
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001661 /* Choose the header browsing function. */
1662 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001663 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001664 http_find_hdr_func = http_find_header2;
1665 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001666 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001667 http_find_hdr_func = http_find_full_header2;
1668 break;
1669 default: /* impossible */
1670 return -1;
1671 }
1672
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001673 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1674 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001675 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001676 char *val = ctx.line + ctx.val;
1677 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001678
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001679 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1680 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001681
Willy Tarreau6e27be12018-08-22 04:46:47 +02001682 len = exp_replace(output->area, output->size, val, str, pmatch);
1683 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001684 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001685
Willy Tarreau6e27be12018-08-22 04:46:47 +02001686 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001687
1688 hdr->len += delta;
1689 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001690
1691 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001692 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001693 }
1694
1695 return 0;
1696}
1697
Willy Tarreau87b09662015-04-03 00:22:06 +02001698static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001699 const char* name, unsigned int name_len,
1700 struct list *fmt, struct my_regex *re,
1701 int action)
1702{
Willy Tarreau83061a82018-07-13 11:56:34 +02001703 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001704 int ret = -1;
1705
1706 replace = alloc_trash_chunk();
1707 if (!replace)
1708 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001709
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001710 replace->data = build_logline(s, replace->area, replace->size, fmt);
1711 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001712 goto leave;
1713
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001714 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1715 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001716
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001717 leave:
1718 free_trash_chunk(replace);
1719 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001720}
1721
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001722/*
1723 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1724 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001725 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1726 * line is inserted before the header. If an error occurred <early_hints> is
1727 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001728 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001729static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1730 const char* name, unsigned int name_len,
1731 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001732{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001733 if (!early_hints) {
1734 early_hints = alloc_trash_chunk();
1735 if (!early_hints)
1736 goto fail;
1737 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1738 goto fail;
1739 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001740
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001741 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1742 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001743
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001744 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1745 if (!chunk_memcat(early_hints, "\r\n", 2))
1746 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001747
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001748 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001749
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001750 fail:
1751 free_trash_chunk(early_hints);
1752 return NULL;
1753}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001754
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001755/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1756 * response is added. If an error occurred or if another response was already
1757 * sent, this function does nothing.
1758 */
1759static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1760{
1761 struct channel *chn = s->txn->rsp.chn;
1762 char *cur_ptr = ci_head(chn);
1763 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001764
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001765 /* If a response was already sent, skip early hints */
1766 if (s->txn->status > 0)
1767 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001768
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001769 if (!chunk_memcat(early_hints, "\r\n", 2))
1770 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001771
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001772 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1773 c_adv(chn, ret);
1774 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001775}
1776
Willy Tarreau87b09662015-04-03 00:22:06 +02001777/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001778 * transaction <txn>. Returns the verdict of the first rule that prevents
1779 * further processing of the request (auth, deny, ...), and defaults to
1780 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1781 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001782 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1783 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1784 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001785 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001786enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001787http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001788{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001789 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001790 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001791 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001792 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001793 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001794 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001795 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001796 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001797 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001798 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001799
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001800 /* If "the current_rule_list" match the executed rule list, we are in
1801 * resume condition. If a resume is needed it is always in the action
1802 * and never in the ACL or converters. In this case, we initialise the
1803 * current rule, and go to the action execution point.
1804 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001805 if (s->current_rule) {
1806 rule = s->current_rule;
1807 s->current_rule = NULL;
1808 if (s->current_rule_list == rules)
1809 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001810 }
1811 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001812
Willy Tarreauff011f22011-01-06 17:51:27 +01001813 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001814
Willy Tarreau96257ec2012-12-27 10:46:37 +01001815 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001816 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001817 int ret;
1818
Willy Tarreau192252e2015-04-04 01:47:55 +02001819 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001820 ret = acl_pass(ret);
1821
Willy Tarreauff011f22011-01-06 17:51:27 +01001822 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001823 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001824
1825 if (!ret) /* condition not matched */
1826 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001827 }
1828
Willy Tarreauacc98002015-09-27 23:34:39 +02001829 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001830resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001831 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001832 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001833 rule_ret = HTTP_RULE_RES_STOP;
1834 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001835
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001836 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001837 if (deny_status)
1838 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001839 rule_ret = HTTP_RULE_RES_DENY;
1840 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001841
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001842 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001843 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001844 if (deny_status)
1845 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001846 rule_ret = HTTP_RULE_RES_DENY;
1847 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001848
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001849 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001850 /* Be sure to send any pending HTTP 103 response first */
1851 if (early_hints) {
1852 http_send_early_hints(s, early_hints);
1853 free_trash_chunk(early_hints);
1854 early_hints = NULL;
1855 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001856 /* Auth might be performed on regular http-req rules as well as on stats */
1857 auth_realm = rule->arg.auth.realm;
1858 if (!auth_realm) {
1859 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1860 auth_realm = STATS_DEFAULT_REALM;
1861 else
1862 auth_realm = px->id;
1863 }
1864 /* send 401/407 depending on whether we use a proxy or not. We still
1865 * count one error, because normal browsing won't significantly
1866 * increase the counter but brute force attempts will.
1867 */
1868 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1869 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001870 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001871 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001872 rule_ret = HTTP_RULE_RES_ABRT;
1873 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001874
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001875 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001876 /* Be sure to send any pending HTTP 103 response first */
1877 if (early_hints) {
1878 http_send_early_hints(s, early_hints);
1879 free_trash_chunk(early_hints);
1880 early_hints = NULL;
1881 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001882 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001883 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001884 rule_ret = HTTP_RULE_RES_BADREQ;
1885 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001886
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001887 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001888 s->task->nice = rule->arg.nice;
1889 break;
1890
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001891 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001892 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001893 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001894 break;
1895
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001896 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001897#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001898 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001899 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001900#endif
1901 break;
1902
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001903 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001904 s->logs.level = rule->arg.loglevel;
1905 break;
1906
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001907 case ACT_HTTP_REPLACE_HDR:
1908 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001909 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1910 rule->arg.hdr_add.name_len,
1911 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001912 &rule->arg.hdr_add.re, rule->action)) {
1913 rule_ret = HTTP_RULE_RES_BADREQ;
1914 goto end;
1915 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001916 break;
1917
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001918 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001919 ctx.idx = 0;
1920 /* remove all occurrences of the header */
1921 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001922 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001923 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001924 }
Willy Tarreau85603282015-01-21 20:39:27 +01001925 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001926
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001927 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001928 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001929 /* The scope of the trash buffer must be limited to this function. The
1930 * build_logline() function can execute a lot of other function which
1931 * can use the trash buffer. So for limiting the scope of this global
1932 * buffer, we build first the header value using build_logline, and
1933 * after we store the header name.
1934 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001935 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001936
1937 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001938 if (!replace) {
1939 rule_ret = HTTP_RULE_RES_BADREQ;
1940 goto end;
1941 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001942
Thierry Fournier4b788f72016-06-01 13:35:36 +02001943 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001944 len += build_logline(s, replace->area + len,
1945 replace->size - len,
1946 &rule->arg.hdr_add.fmt);
1947 memcpy(replace->area, rule->arg.hdr_add.name,
1948 rule->arg.hdr_add.name_len);
1949 replace->area[rule->arg.hdr_add.name_len] = ':';
1950 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1951 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001952
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001953 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001954 /* remove all occurrences of the header */
1955 ctx.idx = 0;
1956 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001957 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001958 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1959 }
1960 }
1961
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001962 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001963 static unsigned char rate_limit = 0;
1964
1965 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001966 replace->area[rule->arg.hdr_add.name_len] = 0;
1967 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,
1968 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001969 }
1970
1971 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1972 if (sess->fe != s->be)
1973 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1974 if (sess->listener->counters)
1975 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1976 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001977
1978 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001979 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001980 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001981
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001982 case ACT_HTTP_DEL_ACL:
1983 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001984 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001985 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001986
1987 /* collect reference */
1988 ref = pat_ref_lookup(rule->arg.map.ref);
1989 if (!ref)
1990 continue;
1991
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001992 /* allocate key */
1993 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001994 if (!key) {
1995 rule_ret = HTTP_RULE_RES_BADREQ;
1996 goto end;
1997 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001998
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001999 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002000 key->data = build_logline(s, key->area, key->size,
2001 &rule->arg.map.key);
2002 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002003
2004 /* perform update */
2005 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002006 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002007 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002008 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002009
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002010 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002011 break;
2012 }
2013
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002014 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002015 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002016 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002017
2018 /* collect reference */
2019 ref = pat_ref_lookup(rule->arg.map.ref);
2020 if (!ref)
2021 continue;
2022
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002023 /* allocate key */
2024 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002025 if (!key) {
2026 rule_ret = HTTP_RULE_RES_BADREQ;
2027 goto end;
2028 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002029
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002030 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002031 key->data = build_logline(s, key->area, key->size,
2032 &rule->arg.map.key);
2033 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002034
2035 /* perform update */
2036 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002037 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002038 if (pat_ref_find_elt(ref, key->area) == NULL)
2039 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002040 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002041
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002042 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002043 break;
2044 }
2045
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002046 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002047 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002048 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002049
2050 /* collect reference */
2051 ref = pat_ref_lookup(rule->arg.map.ref);
2052 if (!ref)
2053 continue;
2054
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002055 /* allocate key */
2056 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002057 if (!key) {
2058 rule_ret = HTTP_RULE_RES_BADREQ;
2059 goto end;
2060 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002061
2062 /* allocate value */
2063 value = alloc_trash_chunk();
2064 if (!value) {
2065 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01002066 rule_ret = HTTP_RULE_RES_BADREQ;
2067 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002068 }
2069
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002070 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002071 key->data = build_logline(s, key->area, key->size,
2072 &rule->arg.map.key);
2073 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002074
2075 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002076 value->data = build_logline(s, value->area,
2077 value->size,
2078 &rule->arg.map.value);
2079 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002080
2081 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002082 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002083 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002084 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002085 else
2086 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002087 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002088
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002089 free_trash_chunk(key);
2090 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002091 break;
2092 }
William Lallemand73025dd2014-04-24 14:38:37 +02002093
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002094 case ACT_HTTP_EARLY_HINT:
2095 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2096 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002097 early_hints = http_apply_early_hint_rule(s, early_hints,
2098 rule->arg.early_hint.name,
2099 rule->arg.early_hint.name_len,
2100 &rule->arg.early_hint.fmt);
2101 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01002102 rule_ret = HTTP_RULE_RES_DONE;
2103 goto end;
2104 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002105 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002106 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002107 if ((s->req.flags & CF_READ_ERROR) ||
2108 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2109 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2110 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002111 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002112
Willy Tarreauacc98002015-09-27 23:34:39 +02002113 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002114 case ACT_RET_ERR:
2115 case ACT_RET_CONT:
2116 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002117 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01002118 rule_ret = HTTP_RULE_RES_DONE;
2119 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002120 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002121 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01002122 rule_ret = HTTP_RULE_RES_YIELD;
2123 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002124 }
William Lallemand73025dd2014-04-24 14:38:37 +02002125 break;
2126
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002127 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002128 /* Note: only the first valid tracking parameter of each
2129 * applies.
2130 */
2131
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002132 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002133 struct stktable *t;
2134 struct stksess *ts;
2135 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002136 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002137
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002138 t = rule->arg.trk_ctr.table.t;
2139 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 +02002140
2141 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002142 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002143
2144 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002145 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2146 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2147 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002148 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002149
Emeric Brun819fc6f2017-06-13 19:37:32 +02002150 if (ptr1)
2151 stktable_data_cast(ptr1, http_req_cnt)++;
2152
2153 if (ptr2)
2154 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2155 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2156
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002157 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002158
2159 /* If data was modified, we need to touch to re-schedule sync */
2160 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002161 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002162
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002163 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002164 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002165 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002166 }
2167 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002168 break;
2169
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002170 /* other flags exists, but normaly, they never be matched. */
2171 default:
2172 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002173 }
2174 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002175
Christopher Fauletea827bd2018-11-15 15:34:11 +01002176 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002177 if (early_hints) {
2178 http_send_early_hints(s, early_hints);
2179 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002180 }
2181
Willy Tarreau96257ec2012-12-27 10:46:37 +01002182 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01002183 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002184}
2185
Willy Tarreau71241ab2012-12-27 11:30:54 +01002186
Willy Tarreau51d861a2015-05-22 17:30:48 +02002187/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2188 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2189 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2190 * is returned, the process can continue the evaluation of next rule list. If
2191 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2192 * is returned, it means the operation could not be processed and a server error
2193 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2194 * deny rule. If *YIELD is returned, the caller must call again the function
2195 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002196 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002197enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002198http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002199{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002200 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002201 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002202 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002203 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002204 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002205 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02002206 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002207
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002208 /* If "the current_rule_list" match the executed rule list, we are in
2209 * resume condition. If a resume is needed it is always in the action
2210 * and never in the ACL or converters. In this case, we initialise the
2211 * current rule, and go to the action execution point.
2212 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002213 if (s->current_rule) {
2214 rule = s->current_rule;
2215 s->current_rule = NULL;
2216 if (s->current_rule_list == rules)
2217 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002218 }
2219 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002220
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002221 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002222
2223 /* check optional condition */
2224 if (rule->cond) {
2225 int ret;
2226
Willy Tarreau192252e2015-04-04 01:47:55 +02002227 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002228 ret = acl_pass(ret);
2229
2230 if (rule->cond->pol == ACL_COND_UNLESS)
2231 ret = !ret;
2232
2233 if (!ret) /* condition not matched */
2234 continue;
2235 }
2236
Willy Tarreauacc98002015-09-27 23:34:39 +02002237 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002238resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002239 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002240 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002241 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2242 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002243
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002244 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002245 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002246 rule_ret = HTTP_RULE_RES_STOP;
2247 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002248
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002249 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002250 s->task->nice = rule->arg.nice;
2251 break;
2252
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002253 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002254 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002255 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002256 break;
2257
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002258 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002259#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002260 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002261 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002262#endif
2263 break;
2264
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002265 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002266 s->logs.level = rule->arg.loglevel;
2267 break;
2268
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002269 case ACT_HTTP_REPLACE_HDR:
2270 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002271 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2272 rule->arg.hdr_add.name_len,
2273 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002274 &rule->arg.hdr_add.re, rule->action)) {
2275 rule_ret = HTTP_RULE_RES_BADREQ;
2276 goto end;
2277 }
Sasha Pachev218f0642014-06-16 12:05:59 -06002278 break;
2279
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002280 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002281 ctx.idx = 0;
2282 /* remove all occurrences of the header */
2283 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002284 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002285 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2286 }
Willy Tarreau85603282015-01-21 20:39:27 +01002287 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002288
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002289 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002290 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002291 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002292
2293 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002294 if (!replace) {
2295 rule_ret = HTTP_RULE_RES_BADREQ;
2296 goto end;
2297 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002298
2299 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002300 memcpy(replace->area, rule->arg.hdr_add.name,
2301 rule->arg.hdr_add.name_len);
2302 replace->data = rule->arg.hdr_add.name_len;
2303 replace->area[replace->data++] = ':';
2304 replace->area[replace->data++] = ' ';
2305 replace->data += build_logline(s,
2306 replace->area + replace->data,
2307 replace->size - replace->data,
2308 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002309
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002310 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002311 /* remove all occurrences of the header */
2312 ctx.idx = 0;
2313 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002314 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002315 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2316 }
2317 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002318
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002319 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002320 static unsigned char rate_limit = 0;
2321
2322 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002323 replace->area[rule->arg.hdr_add.name_len] = 0;
2324 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,
2325 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002326 }
2327
2328 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2329 if (sess->fe != s->be)
2330 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2331 if (sess->listener->counters)
2332 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2333 if (objt_server(s->target))
2334 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2335 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002336
2337 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002338 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002339 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002340
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002341 case ACT_HTTP_DEL_ACL:
2342 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002343 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002344 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002345
2346 /* collect reference */
2347 ref = pat_ref_lookup(rule->arg.map.ref);
2348 if (!ref)
2349 continue;
2350
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002351 /* allocate key */
2352 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002353 if (!key) {
2354 rule_ret = HTTP_RULE_RES_BADREQ;
2355 goto end;
2356 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002357
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002358 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002359 key->data = build_logline(s, key->area, key->size,
2360 &rule->arg.map.key);
2361 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002362
2363 /* perform update */
2364 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002365 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002366 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002367 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002368
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002369 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002370 break;
2371 }
2372
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002373 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002374 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002375 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002376
2377 /* collect reference */
2378 ref = pat_ref_lookup(rule->arg.map.ref);
2379 if (!ref)
2380 continue;
2381
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002382 /* allocate key */
2383 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002384 if (!key) {
2385 rule_ret = HTTP_RULE_RES_BADREQ;
2386 goto end;
2387 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002388
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002389 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002390 key->data = build_logline(s, key->area, key->size,
2391 &rule->arg.map.key);
2392 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002393
2394 /* perform update */
2395 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002396 if (pat_ref_find_elt(ref, key->area) == NULL)
2397 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002398
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002399 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002400 break;
2401 }
2402
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002403 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002404 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002405 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002406
2407 /* collect reference */
2408 ref = pat_ref_lookup(rule->arg.map.ref);
2409 if (!ref)
2410 continue;
2411
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002412 /* allocate key */
2413 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002414 if (!key) {
2415 rule_ret = HTTP_RULE_RES_BADREQ;
2416 goto end;
2417 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002418
2419 /* allocate value */
2420 value = alloc_trash_chunk();
2421 if (!value) {
2422 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002423 rule_ret = HTTP_RULE_RES_BADREQ;
2424 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002425 }
2426
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002427 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002428 key->data = build_logline(s, key->area, key->size,
2429 &rule->arg.map.key);
2430 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002431
2432 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002433 value->data = build_logline(s, value->area,
2434 value->size,
2435 &rule->arg.map.value);
2436 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002437
2438 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002439 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002440 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002441 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002442 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002443 else
2444 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002445 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002446 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002447 free_trash_chunk(key);
2448 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002449 break;
2450 }
William Lallemand73025dd2014-04-24 14:38:37 +02002451
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002452 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002453 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002454 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002455 rule_ret = HTTP_RULE_RES_BADREQ;
2456 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002457
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002458 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2459 /* Note: only the first valid tracking parameter of each
2460 * applies.
2461 */
2462
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002463 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002464 struct stktable *t;
2465 struct stksess *ts;
2466 struct stktable_key *key;
2467 void *ptr;
2468
2469 t = rule->arg.trk_ctr.table.t;
2470 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2471
2472 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002473 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002474
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002475 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002476
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002477 /* let's count a new HTTP request as it's the first time we do it */
2478 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2479 if (ptr)
2480 stktable_data_cast(ptr, http_req_cnt)++;
2481
2482 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2483 if (ptr)
2484 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2485 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2486
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002487 /* When the client triggers a 4xx from the server, it's most often due
2488 * to a missing object or permission. These events should be tracked
2489 * because if they happen often, it may indicate a brute force or a
2490 * vulnerability scan. Normally this is done when receiving the response
2491 * but here we're tracking after this ought to have been done so we have
2492 * to do it on purpose.
2493 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002494 if ((unsigned)(txn->status - 400) < 100) {
2495 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2496 if (ptr)
2497 stktable_data_cast(ptr, http_err_cnt)++;
2498
2499 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2500 if (ptr)
2501 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2502 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2503 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002504
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002505 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002506
Emeric Brun0fed0b02017-11-29 16:15:07 +01002507 /* If data was modified, we need to touch to re-schedule sync */
2508 stktable_touch_local(t, ts, 0);
2509
Emeric Brun819fc6f2017-06-13 19:37:32 +02002510 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2511 if (sess->fe != s->be)
2512 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2513
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002514 }
2515 }
2516 break;
2517
Thierry FOURNIER42148732015-09-02 17:17:33 +02002518 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002519 if ((s->req.flags & CF_READ_ERROR) ||
2520 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2521 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2522 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002523 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002524
Willy Tarreauacc98002015-09-27 23:34:39 +02002525 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002526 case ACT_RET_ERR:
2527 case ACT_RET_CONT:
2528 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002529 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002530 rule_ret = HTTP_RULE_RES_STOP;
2531 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002532 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002533 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002534 rule_ret = HTTP_RULE_RES_YIELD;
2535 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002536 }
William Lallemand73025dd2014-04-24 14:38:37 +02002537 break;
2538
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002539 /* other flags exists, but normaly, they never be matched. */
2540 default:
2541 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002542 }
2543 }
2544
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002545 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002546 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002547 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002548}
2549
2550
Willy Tarreau71241ab2012-12-27 11:30:54 +01002551/* Perform an HTTP redirect based on the information in <rule>. The function
2552 * returns non-zero on success, or zero in case of a, irrecoverable error such
2553 * as too large a request to build a valid response.
2554 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002555int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002556{
Willy Tarreaub329a312015-05-22 16:27:37 +02002557 struct http_msg *req = &txn->req;
2558 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002559 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002560 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002561 int ret = 0;
2562
2563 chunk = alloc_trash_chunk();
2564 if (!chunk)
2565 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002566
2567 /* build redirect message */
2568 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002569 case 308:
2570 msg_fmt = HTTP_308;
2571 break;
2572 case 307:
2573 msg_fmt = HTTP_307;
2574 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002575 case 303:
2576 msg_fmt = HTTP_303;
2577 break;
2578 case 301:
2579 msg_fmt = HTTP_301;
2580 break;
2581 case 302:
2582 default:
2583 msg_fmt = HTTP_302;
2584 break;
2585 }
2586
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002587 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2588 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002589
2590 switch(rule->type) {
2591 case REDIRECT_TYPE_SCHEME: {
2592 const char *path;
2593 const char *host;
2594 struct hdr_ctx ctx;
2595 int pathlen;
2596 int hostlen;
2597
2598 host = "";
2599 hostlen = 0;
2600 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002601 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002602 host = ctx.line + ctx.val;
2603 hostlen = ctx.vlen;
2604 }
2605
Willy Tarreau6b952c82018-09-10 17:45:34 +02002606 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002607 /* build message using path */
2608 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002609 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002610 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2611 int qs = 0;
2612 while (qs < pathlen) {
2613 if (path[qs] == '?') {
2614 pathlen = qs;
2615 break;
2616 }
2617 qs++;
2618 }
2619 }
2620 } else {
2621 path = "/";
2622 pathlen = 1;
2623 }
2624
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002625 if (rule->rdr_str) { /* this is an old "redirect" rule */
2626 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002627 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002628 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002629
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002630 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002631 memcpy(chunk->area + chunk->data, rule->rdr_str,
2632 rule->rdr_len);
2633 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002634 }
2635 else {
2636 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002637 chunk->data += build_logline(s,
2638 chunk->area + chunk->data,
2639 chunk->size - chunk->data,
2640 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002641
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002642 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002643 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002644 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002645 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002646 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002647 memcpy(chunk->area + chunk->data, "://", 3);
2648 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002649
2650 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002651 memcpy(chunk->area + chunk->data, host, hostlen);
2652 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002653
2654 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002655 memcpy(chunk->area + chunk->data, path, pathlen);
2656 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002657
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002658 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002659 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002660 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002661 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002662 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002663 chunk->area[chunk->data] = '/';
2664 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002665 }
2666
2667 break;
2668 }
2669 case REDIRECT_TYPE_PREFIX: {
2670 const char *path;
2671 int pathlen;
2672
Willy Tarreau6b952c82018-09-10 17:45:34 +02002673 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002674 /* build message using path */
2675 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002676 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002677 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2678 int qs = 0;
2679 while (qs < pathlen) {
2680 if (path[qs] == '?') {
2681 pathlen = qs;
2682 break;
2683 }
2684 qs++;
2685 }
2686 }
2687 } else {
2688 path = "/";
2689 pathlen = 1;
2690 }
2691
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002692 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002693 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002694 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002695
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002696 /* add prefix. Note that if prefix == "/", we don't want to
2697 * add anything, otherwise it makes it hard for the user to
2698 * configure a self-redirection.
2699 */
2700 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002701 memcpy(chunk->area + chunk->data,
2702 rule->rdr_str, rule->rdr_len);
2703 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002704 }
2705 }
2706 else {
2707 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002708 chunk->data += build_logline(s,
2709 chunk->area + chunk->data,
2710 chunk->size - chunk->data,
2711 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002712
2713 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002714 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002715 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002716 }
2717
2718 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002719 memcpy(chunk->area + chunk->data, path, pathlen);
2720 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002721
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002722 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002723 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002724 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002725 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002726 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002727 chunk->area[chunk->data] = '/';
2728 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002729 }
2730
2731 break;
2732 }
2733 case REDIRECT_TYPE_LOCATION:
2734 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002735 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002736 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002737 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002738
2739 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002740 memcpy(chunk->area + chunk->data, rule->rdr_str,
2741 rule->rdr_len);
2742 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002743 }
2744 else {
2745 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002746 chunk->data += build_logline(s,
2747 chunk->area + chunk->data,
2748 chunk->size - chunk->data,
2749 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002750
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002751 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002752 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002753 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002754 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002755 break;
2756 }
2757
2758 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002759 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2760 chunk->data += 14;
2761 memcpy(chunk->area + chunk->data, rule->cookie_str,
2762 rule->cookie_len);
2763 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002764 }
2765
Willy Tarreau19b14122017-02-28 09:48:11 +01002766 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002767 txn->status = rule->code;
2768 /* let's log the request time */
2769 s->logs.tv_request = now;
2770
Christopher Fauletbe821b92017-03-30 11:21:53 +02002771 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002772 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2773 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2774 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002775 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002776 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002777 memcpy(chunk->area + chunk->data,
2778 "\r\nProxy-Connection: keep-alive", 30);
2779 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002780 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002781 memcpy(chunk->area + chunk->data,
2782 "\r\nConnection: keep-alive", 24);
2783 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002784 }
2785 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002786 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2787 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002788 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002789 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002790 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002791 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002792 req->next -= req->sov;
2793 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002794 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002795 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002796 req->msg_state = HTTP_MSG_CLOSED;
2797 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002798 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002799 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002800 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002801 /* let the server side turn to SI_ST_CLO */
2802 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002803 } else {
2804 /* keep-alive not possible */
2805 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002806 memcpy(chunk->area + chunk->data,
2807 "\r\nProxy-Connection: close\r\n\r\n", 29);
2808 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002809 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002810 memcpy(chunk->area + chunk->data,
2811 "\r\nConnection: close\r\n\r\n", 23);
2812 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002813 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002814 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002815 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002816 }
2817
Willy Tarreaue7dff022015-04-03 01:14:29 +02002818 if (!(s->flags & SF_ERR_MASK))
2819 s->flags |= SF_ERR_LOCAL;
2820 if (!(s->flags & SF_FINST_MASK))
2821 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002822
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002823 ret = 1;
2824 leave:
2825 free_trash_chunk(chunk);
2826 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002827}
2828
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002829/* This stream analyser runs all HTTP request processing which is common to
2830 * frontends and backends, which means blocking ACLs, filters, connection-close,
2831 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002832 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002833 * either needs more data or wants to immediately abort the request (eg: deny,
2834 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002835 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002836int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002837{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002838 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002839 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002840 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002841 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002842 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002843 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002844 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002845 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002846
Willy Tarreau655dce92009-11-08 13:10:58 +01002847 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002848 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002849 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002850 }
2851
Christopher Faulet45073512018-07-20 10:16:29 +02002852 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 +02002853 now_ms, __FUNCTION__,
2854 s,
2855 req,
2856 req->rex, req->wex,
2857 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002858 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002859 req->analysers);
2860
Willy Tarreau65410832014-04-28 21:25:43 +02002861 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002862 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002863
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002864 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002865 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002866 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002867
Willy Tarreau0b748332014-04-29 00:13:29 +02002868 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002869 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2870 goto return_prx_yield;
2871
Willy Tarreau0b748332014-04-29 00:13:29 +02002872 case HTTP_RULE_RES_CONT:
2873 case HTTP_RULE_RES_STOP: /* nothing to do */
2874 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002875
Willy Tarreau0b748332014-04-29 00:13:29 +02002876 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2877 if (txn->flags & TX_CLTARPIT)
2878 goto tarpit;
2879 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002880
Willy Tarreau0b748332014-04-29 00:13:29 +02002881 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2882 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002883
Willy Tarreau0b748332014-04-29 00:13:29 +02002884 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002885 goto done;
2886
Willy Tarreau0b748332014-04-29 00:13:29 +02002887 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2888 goto return_bad_req;
2889 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002890 }
2891
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002892 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2893 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002894 struct hdr_ctx ctx;
2895
2896 ctx.idx = 0;
2897 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002898 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002899 if (unlikely(http_header_add_tail2(&txn->req,
2900 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002901 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002902 goto return_bad_req;
2903 }
2904 }
2905
2906 }
2907
Willy Tarreau52542592014-04-28 18:33:26 +02002908 /* OK at this stage, we know that the request was accepted according to
2909 * the http-request rules, we can check for the stats. Note that the
2910 * URI is detected *before* the req* rules in order not to be affected
2911 * by a possible reqrep, while they are processed *after* so that a
2912 * reqdeny can still block them. This clearly needs to change in 1.6!
2913 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002914 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002915 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002916 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002917 txn->status = 500;
2918 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002919 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002920
Willy Tarreaue7dff022015-04-03 01:14:29 +02002921 if (!(s->flags & SF_ERR_MASK))
2922 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002923 goto return_prx_cond;
2924 }
2925
2926 /* parse the whole stats request and extract the relevant information */
2927 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002928 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002929 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002930
Willy Tarreau0b748332014-04-29 00:13:29 +02002931 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2932 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002933
Willy Tarreau0b748332014-04-29 00:13:29 +02002934 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2935 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002936 }
2937
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002938 /* evaluate the req* rules except reqadd */
2939 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002940 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002941 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002942
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002943 if (txn->flags & TX_CLDENY)
2944 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002945
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002946 if (txn->flags & TX_CLTARPIT) {
2947 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002948 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002949 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002950 }
Willy Tarreau06619262006-12-17 08:37:22 +01002951
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002952 /* add request headers from the rule sets in the same order */
2953 list_for_each_entry(wl, &px->req_add, list) {
2954 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002955 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002956 ret = acl_pass(ret);
2957 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2958 ret = !ret;
2959 if (!ret)
2960 continue;
2961 }
2962
Christopher Faulet10079f52018-10-03 15:17:28 +02002963 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002964 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002965 }
2966
Willy Tarreau52542592014-04-28 18:33:26 +02002967
2968 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002969 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2970 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002971 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002972 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002973 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002974
Willy Tarreaue7dff022015-04-03 01:14:29 +02002975 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2976 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2977 if (!(s->flags & SF_FINST_MASK))
2978 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002979
Willy Tarreau70730dd2014-04-24 18:06:27 +02002980 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002981 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2982 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002983 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002984 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002985 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002986
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002987 /* check whether we have some ACLs set to redirect this request */
2988 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002989 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002990 int ret;
2991
Willy Tarreau192252e2015-04-04 01:47:55 +02002992 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002993 ret = acl_pass(ret);
2994 if (rule->cond->pol == ACL_COND_UNLESS)
2995 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002996 if (!ret)
2997 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002998 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002999 if (!http_apply_redirect_rule(rule, s, txn))
3000 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003001 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003002 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003003
Willy Tarreau2be39392010-01-03 17:24:51 +01003004 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3005 * If this happens, then the data will not come immediately, so we must
3006 * send all what we have without waiting. Note that due to the small gain
3007 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003008 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003009 * itself once used.
3010 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003011 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003012
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003013 done: /* done with this analyser, continue with next ones that the calling
3014 * points will have set, if any.
3015 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003016 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003017 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3018 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003019 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003020
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003021 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003022 /* Allow cookie logging
3023 */
3024 if (s->be->cookie_name || sess->fe->capture_name)
3025 manage_client_side_cookies(s, req);
3026
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003027 /* When a connection is tarpitted, we use the tarpit timeout,
3028 * which may be the same as the connect timeout if unspecified.
3029 * If unset, then set it to zero because we really want it to
3030 * eventually expire. We build the tarpit as an analyser.
3031 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003032 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003033
3034 /* wipe the request out so that we can drop the connection early
3035 * if the client closes first.
3036 */
3037 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003038
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003039 txn->status = http_err_codes[deny_status];
3040
Christopher Faulet0184ea72017-01-05 14:06:34 +01003041 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003042 req->analysers |= AN_REQ_HTTP_TARPIT;
3043 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3044 if (!req->analyse_exp)
3045 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003046 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003047 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003048 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003049 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003050 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003051 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003052 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003053
3054 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003055
3056 /* Allow cookie logging
3057 */
3058 if (s->be->cookie_name || sess->fe->capture_name)
3059 manage_client_side_cookies(s, req);
3060
Willy Tarreau0b748332014-04-29 00:13:29 +02003061 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003062 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003063 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003064 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003065 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003066 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003067 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003068 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003069 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003070 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003071 goto return_prx_cond;
3072
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003073 return_bad_req:
3074 /* We centralize bad requests processing here */
3075 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3076 /* we detected a parsing error. We want to archive this request
3077 * in the dedicated proxy area for later troubleshooting.
3078 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003079 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003080 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003081
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003082 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003083 txn->req.msg_state = HTTP_MSG_ERROR;
3084 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003085 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003086
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003087 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003088 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003089 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003090
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003091 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003092 if (!(s->flags & SF_ERR_MASK))
3093 s->flags |= SF_ERR_PRXCOND;
3094 if (!(s->flags & SF_FINST_MASK))
3095 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003096
Christopher Faulet0184ea72017-01-05 14:06:34 +01003097 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098 req->analyse_exp = TICK_ETERNITY;
3099 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003100
3101 return_prx_yield:
3102 channel_dont_connect(req);
3103 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003104}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003105
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003106/* This function performs all the processing enabled for the current request.
3107 * It returns 1 if the processing can continue on next analysers, or zero if it
3108 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003109 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003110 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003111int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003112{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003113 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003114 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003115 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003116 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003117
Willy Tarreau655dce92009-11-08 13:10:58 +01003118 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003119 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003120 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003121 return 0;
3122 }
3123
Christopher Faulet45073512018-07-20 10:16:29 +02003124 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 +02003125 now_ms, __FUNCTION__,
3126 s,
3127 req,
3128 req->rex, req->wex,
3129 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003130 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003131 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003132
Willy Tarreau59234e92008-11-30 23:51:27 +01003133 /*
3134 * Right now, we know that we have processed the entire headers
3135 * and that unwanted requests have been filtered out. We can do
3136 * whatever we want with the remaining request. Also, now we
3137 * may have separate values for ->fe, ->be.
3138 */
Willy Tarreau06619262006-12-17 08:37:22 +01003139
Willy Tarreau59234e92008-11-30 23:51:27 +01003140 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003141 * If HTTP PROXY is set we simply get remote server address parsing
3142 * incoming request. Note that this requires that a connection is
3143 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003144 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003145 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003146 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003147 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003148
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003149 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003150 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003151 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003152 txn->req.msg_state = HTTP_MSG_ERROR;
3153 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003154 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003155 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003156
Willy Tarreaue7dff022015-04-03 01:14:29 +02003157 if (!(s->flags & SF_ERR_MASK))
3158 s->flags |= SF_ERR_RESOURCE;
3159 if (!(s->flags & SF_FINST_MASK))
3160 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003161
3162 return 0;
3163 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003164
Willy Tarreau6b952c82018-09-10 17:45:34 +02003165 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003166 if (url2sa(ci_head(req) + msg->sl.rq.u,
3167 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003168 &conn->addr.to, NULL) == -1)
3169 goto return_bad_req;
3170
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003171 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003172 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3173 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003174 * u_l characters by a single "/".
3175 */
3176 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003177 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003178 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3179 int delta;
3180
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003181 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003182 http_msg_move_end(&txn->req, delta);
3183 cur_end += delta;
3184 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3185 goto return_bad_req;
3186 }
3187 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003188 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003189 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3190 int delta;
3191
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003192 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003193 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003194 http_msg_move_end(&txn->req, delta);
3195 cur_end += delta;
3196 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3197 goto return_bad_req;
3198 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003199 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003200
Willy Tarreau59234e92008-11-30 23:51:27 +01003201 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003202 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003203 * Note that doing so might move headers in the request, but
3204 * the fields will stay coherent and the URI will not move.
3205 * This should only be performed in the backend.
3206 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003207 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003208 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003209
William Lallemanda73203e2012-03-12 12:48:57 +01003210 /* add unique-id if "header-unique-id" is specified */
3211
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003212 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003213 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003214 goto return_bad_req;
3215 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003216 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003217 }
William Lallemanda73203e2012-03-12 12:48:57 +01003218
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003219 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003220 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003221 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003222 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003223 goto return_bad_req;
3224 }
3225
Cyril Bontéb21570a2009-11-29 20:04:48 +01003226 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003227 * 9: add X-Forwarded-For if either the frontend or the backend
3228 * asks for it.
3229 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003230 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003231 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003232 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3233 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3234 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003235 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003236 /* The header is set to be added only if none is present
3237 * and we found it, so don't do anything.
3238 */
3239 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003240 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003241 /* Add an X-Forwarded-For header unless the source IP is
3242 * in the 'except' network range.
3243 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003244 if ((!sess->fe->except_mask.s_addr ||
3245 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3246 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003247 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003248 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003249 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003250 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003251 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003252 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003253
3254 /* Note: we rely on the backend to get the header name to be used for
3255 * x-forwarded-for, because the header is really meant for the backends.
3256 * However, if the backend did not specify any option, we have to rely
3257 * on the frontend's header name.
3258 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003259 if (s->be->fwdfor_hdr_len) {
3260 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003261 memcpy(trash.area,
3262 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003263 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003264 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003265 memcpy(trash.area,
3266 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003267 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003268 len += snprintf(trash.area + len,
3269 trash.size - len,
3270 ": %d.%d.%d.%d", pn[0], pn[1],
3271 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003272
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003273 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003274 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003275 }
3276 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003277 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003278 /* FIXME: for the sake of completeness, we should also support
3279 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003280 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003281 int len;
3282 char pn[INET6_ADDRSTRLEN];
3283 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003284 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003285 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003286
Willy Tarreau59234e92008-11-30 23:51:27 +01003287 /* Note: we rely on the backend to get the header name to be used for
3288 * x-forwarded-for, because the header is really meant for the backends.
3289 * However, if the backend did not specify any option, we have to rely
3290 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003291 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003292 if (s->be->fwdfor_hdr_len) {
3293 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003294 memcpy(trash.area, s->be->fwdfor_hdr_name,
3295 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003296 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003297 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003298 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3299 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003300 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003301 len += snprintf(trash.area + len, trash.size - len,
3302 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003303
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003304 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003305 goto return_bad_req;
3306 }
3307 }
3308
3309 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003310 * 10: add X-Original-To if either the frontend or the backend
3311 * asks for it.
3312 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003313 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003314
3315 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003316 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003317 /* Add an X-Original-To header unless the destination IP is
3318 * in the 'except' network range.
3319 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003320 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003321
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003322 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003323 ((!sess->fe->except_mask_to.s_addr ||
3324 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3325 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003326 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003327 (((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 +02003328 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003329 int len;
3330 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003331 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003332
3333 /* Note: we rely on the backend to get the header name to be used for
3334 * x-original-to, because the header is really meant for the backends.
3335 * However, if the backend did not specify any option, we have to rely
3336 * on the frontend's header name.
3337 */
3338 if (s->be->orgto_hdr_len) {
3339 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003340 memcpy(trash.area,
3341 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003342 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003343 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003344 memcpy(trash.area,
3345 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003346 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003347 len += snprintf(trash.area + len,
3348 trash.size - len,
3349 ": %d.%d.%d.%d", pn[0], pn[1],
3350 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003351
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003352 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003353 goto return_bad_req;
3354 }
3355 }
3356 }
3357
Willy Tarreau50fc7772012-11-11 22:19:57 +01003358 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3359 * If an "Upgrade" token is found, the header is left untouched in order not to have
3360 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3361 * "Upgrade" is present in the Connection header.
3362 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003363 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003364 unsigned int want_flags = 0;
3365
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003366 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003367 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003368 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003369 want_flags |= TX_CON_CLO_SET;
3370 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003371 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003372 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003373 want_flags |= TX_CON_KAL_SET;
3374 }
3375
3376 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003377 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003378 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003379
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003380
Willy Tarreau522d6c02009-12-06 18:49:18 +01003381 /* If we have no server assigned yet and we're balancing on url_param
3382 * with a POST request, we may be interested in checking the body for
3383 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003384 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003385 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003386 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003387 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003388 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003389 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003390 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003391
Christopher Fauletbe821b92017-03-30 11:21:53 +02003392 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3393 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003394#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003395 /* We expect some data from the client. Unless we know for sure
3396 * we already have a full request, we have to re-enable quick-ack
3397 * in case we previously disabled it, otherwise we might cause
3398 * the client to delay further data.
3399 */
3400 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3401 cli_conn && conn_ctrl_ready(cli_conn) &&
3402 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003403 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003404 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003405#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003406
Willy Tarreau59234e92008-11-30 23:51:27 +01003407 /*************************************************************
3408 * OK, that's finished for the headers. We have done what we *
3409 * could. Let's switch to the DATA state. *
3410 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003411 req->analyse_exp = TICK_ETERNITY;
3412 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003413
Willy Tarreau59234e92008-11-30 23:51:27 +01003414 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003415 /* OK let's go on with the BODY now */
3416 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003417
Willy Tarreau59234e92008-11-30 23:51:27 +01003418 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003419 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003420 /* we detected a parsing error. We want to archive this request
3421 * in the dedicated proxy area for later troubleshooting.
3422 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003423 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003424 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003425
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003426 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003427 txn->req.msg_state = HTTP_MSG_ERROR;
3428 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003429 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003430 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003431
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003432 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003433 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003434 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003435
Willy Tarreaue7dff022015-04-03 01:14:29 +02003436 if (!(s->flags & SF_ERR_MASK))
3437 s->flags |= SF_ERR_PRXCOND;
3438 if (!(s->flags & SF_FINST_MASK))
3439 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003440 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003441}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003442
Willy Tarreau60b85b02008-11-30 23:28:40 +01003443/* This function is an analyser which processes the HTTP tarpit. It always
3444 * returns zero, at the beginning because it prevents any other processing
3445 * from occurring, and at the end because it terminates the request.
3446 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003447int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003448{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003449 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003450
3451 /* This connection is being tarpitted. The CLIENT side has
3452 * already set the connect expiration date to the right
3453 * timeout. We just have to check that the client is still
3454 * there and that the timeout has not expired.
3455 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003456 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003457 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003458 !tick_is_expired(req->analyse_exp, now_ms))
3459 return 0;
3460
3461 /* We will set the queue timer to the time spent, just for
3462 * logging purposes. We fake a 500 server error, so that the
3463 * attacker will not suspect his connection has been tarpitted.
3464 * It will not cause trouble to the logs because we can exclude
3465 * the tarpitted connections by filtering on the 'PT' status flags.
3466 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003467 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3468
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003469 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003470 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003471
Christopher Faulet0184ea72017-01-05 14:06:34 +01003472 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003473 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003474
Willy Tarreaue7dff022015-04-03 01:14:29 +02003475 if (!(s->flags & SF_ERR_MASK))
3476 s->flags |= SF_ERR_PRXCOND;
3477 if (!(s->flags & SF_FINST_MASK))
3478 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003479 return 0;
3480}
3481
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003482/* This function is an analyser which waits for the HTTP request body. It waits
3483 * for either the buffer to be full, or the full advertised contents to have
3484 * reached the buffer. It must only be called after the standard HTTP request
3485 * processing has occurred, because it expects the request to be parsed and will
3486 * look for the Expect header. It may send a 100-Continue interim response. It
3487 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3488 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3489 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003490 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003491int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003492{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003493 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003494 struct http_txn *txn = s->txn;
3495 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003496
3497 /* We have to parse the HTTP request body to find any required data.
3498 * "balance url_param check_post" should have been the only way to get
3499 * into this. We were brought here after HTTP header analysis, so all
3500 * related structures are ready.
3501 */
3502
Willy Tarreau890988f2014-04-10 11:59:33 +02003503 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3504 /* This is the first call */
3505 if (msg->msg_state < HTTP_MSG_BODY)
3506 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003507
Willy Tarreau890988f2014-04-10 11:59:33 +02003508 if (msg->msg_state < HTTP_MSG_100_SENT) {
3509 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3510 * send an HTTP/1.1 100 Continue intermediate response.
3511 */
3512 if (msg->flags & HTTP_MSGF_VER_11) {
3513 struct hdr_ctx ctx;
3514 ctx.idx = 0;
3515 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003516 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003517 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003518 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003519 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003520 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003521 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003522 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003523 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003524
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003525 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003526 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003527 * must save the body in msg->next because it survives buffer
3528 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003529 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003530 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003531
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003532 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003533 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3534 else
3535 msg->msg_state = HTTP_MSG_DATA;
3536 }
3537
Willy Tarreau890988f2014-04-10 11:59:33 +02003538 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3539 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003540 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003541 goto missing_data;
3542
3543 /* OK we have everything we need now */
3544 goto http_end;
3545 }
3546
3547 /* OK here we're parsing a chunked-encoded message */
3548
Willy Tarreau522d6c02009-12-06 18:49:18 +01003549 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003550 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003551 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003552 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003553 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003554 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003555 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003556
Willy Tarreau115acb92009-12-26 13:56:06 +01003557 if (!ret)
3558 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003559 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003560 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003561 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003562 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003563 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003564 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003565 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003566
3567 msg->chunk_len = chunk;
3568 msg->body_len += chunk;
3569
3570 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003571 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003572 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003573 }
3574
Willy Tarreaud98cf932009-12-27 22:54:55 +01003575 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003576 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3577 * for at least a whole chunk or the whole content length bytes after
3578 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003579 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003580 if (msg->msg_state == HTTP_MSG_TRAILERS)
3581 goto http_end;
3582
Willy Tarreaue115b492015-05-01 23:05:14 +02003583 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003584 goto http_end;
3585
3586 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003587 /* we get here if we need to wait for more data. If the buffer is full,
3588 * we have the maximum we can expect.
3589 */
Willy Tarreau23752332018-06-15 14:54:53 +02003590 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003591 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003592
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003593 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003594 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003595 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003596
Willy Tarreaue7dff022015-04-03 01:14:29 +02003597 if (!(s->flags & SF_ERR_MASK))
3598 s->flags |= SF_ERR_CLITO;
3599 if (!(s->flags & SF_FINST_MASK))
3600 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003601 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003602 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003603
3604 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003605 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003606 /* Not enough data. We'll re-use the http-request
3607 * timeout here. Ideally, we should set the timeout
3608 * relative to the accept() date. We just set the
3609 * request timeout once at the beginning of the
3610 * request.
3611 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003612 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003613 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003614 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003615 return 0;
3616 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003617
3618 http_end:
3619 /* The situation will not evolve, so let's give up on the analysis. */
3620 s->logs.tv_request = now; /* update the request timer to reflect full request */
3621 req->analysers &= ~an_bit;
3622 req->analyse_exp = TICK_ETERNITY;
3623 return 1;
3624
3625 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003626 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003627 txn->req.msg_state = HTTP_MSG_ERROR;
3628 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003629 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003630
Willy Tarreaue7dff022015-04-03 01:14:29 +02003631 if (!(s->flags & SF_ERR_MASK))
3632 s->flags |= SF_ERR_PRXCOND;
3633 if (!(s->flags & SF_FINST_MASK))
3634 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003635
Willy Tarreau522d6c02009-12-06 18:49:18 +01003636 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003637 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003638 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003639 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003640 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003641 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003642}
3643
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003644/* send a server's name with an outgoing request over an established connection.
3645 * Note: this function is designed to be called once the request has been scheduled
3646 * for being forwarded. This is the reason why it rewinds the buffer before
3647 * proceeding.
3648 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003649int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003650
3651 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003652 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003653 char *hdr_name = be->server_id_hdr_name;
3654 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003655 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003656 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003657 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003658
William Lallemandd9e90662012-01-30 17:27:17 +01003659 ctx.idx = 0;
3660
Willy Tarreau211cdec2014-04-17 20:18:08 +02003661 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003662 if (old_o) {
3663 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003664 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003665 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003666 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003667 }
3668
Willy Tarreauf37954d2018-06-15 18:31:02 +02003669 old_i = ci_data(chn);
3670 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 -05003671 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003672 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003673 }
3674
3675 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003676 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003677 memcpy(hdr_val, hdr_name, hdr_name_len);
3678 hdr_val += hdr_name_len;
3679 *hdr_val++ = ':';
3680 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003681 hdr_val += strlcpy2(hdr_val, srv_name,
3682 trash.area + trash.size - hdr_val);
3683 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3684 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003685
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003686 if (old_o) {
3687 /* If this was a forwarded request, we must readjust the amount of
3688 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003689 * variations. Note that the current state is >= HTTP_MSG_BODY,
3690 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003691 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003692 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003693 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003694 txn->req.next -= old_o;
3695 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003696 }
3697
Mark Lamourinec2247f02012-01-04 13:02:01 -05003698 return 0;
3699}
3700
Willy Tarreau610ecce2010-01-04 21:15:02 +01003701/* Terminate current transaction and prepare a new one. This is very tricky
3702 * right now but it works.
3703 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003704void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003705{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003706 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003707 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003708 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003709 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003710 struct connection *srv_conn;
3711 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003712 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003713
Willy Tarreau610ecce2010-01-04 21:15:02 +01003714 /* FIXME: We need a more portable way of releasing a backend's and a
3715 * server's connections. We need a safer way to reinitialize buffer
3716 * flags. We also need a more accurate method for computing per-request
3717 * data.
3718 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003719 cs = objt_cs(s->si[1].end);
3720 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003721
Willy Tarreau4213a112013-12-15 10:25:42 +01003722 /* unless we're doing keep-alive, we want to quickly close the connection
3723 * to the server.
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003724 * XXX cognet: If the connection doesn't have a owner then it may not
3725 * be referenced anywhere, just kill it now, even if it could be reused.
3726 * To be revisited later when revisited later when we handle connection
3727 * pools properly.
Willy Tarreau4213a112013-12-15 10:25:42 +01003728 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003729 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003730 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003731 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3732 si_shutr(&s->si[1]);
3733 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003734 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003735
Willy Tarreaue7dff022015-04-03 01:14:29 +02003736 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003737 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003738 if (unlikely(s->srv_conn))
3739 sess_change_server(s, NULL);
3740 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003741
3742 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003743 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003744
Willy Tarreaueee5b512015-04-03 23:46:31 +02003745 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003746 int n;
3747
Willy Tarreaueee5b512015-04-03 23:46:31 +02003748 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003749 if (n < 1 || n > 5)
3750 n = 0;
3751
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003752 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003753 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003754 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003755 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003756 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003757 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3758 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003759 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003760 }
3761
3762 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003763 s->logs.bytes_in -= ci_data(&s->req);
3764 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003765
Willy Tarreau66425e32018-07-25 06:55:12 +02003766 /* we may need to know the position in the queue */
3767 pendconn_free(s);
3768
Willy Tarreau610ecce2010-01-04 21:15:02 +01003769 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003770 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003771 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003772 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003773 s->do_log(s);
3774 }
3775
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003776 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003777 stream_stop_content_counters(s);
3778 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003779
Willy Tarreau610ecce2010-01-04 21:15:02 +01003780 s->logs.accept_date = date; /* user-visible date for logging */
3781 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003782 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3783 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003784 tv_zero(&s->logs.tv_request);
3785 s->logs.t_queue = -1;
3786 s->logs.t_connect = -1;
3787 s->logs.t_data = -1;
3788 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003789 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3790 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003791
Willy Tarreauf37954d2018-06-15 18:31:02 +02003792 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3793 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003794
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003795 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003796 if (s->flags & SF_CURR_SESS) {
3797 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003798 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003799 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003800 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003801 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003802 }
3803
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003804 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003805
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003806
3807 /* If we're doing keepalive, first call the mux detach() method
3808 * to let it know we want to detach without freing the connection.
3809 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003810 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003811 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003812 !si_conn_ready(&s->si[1]) || !srv_conn->owner)
Willy Tarreau323a2d92015-08-04 19:00:17 +02003813 srv_conn = NULL;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003814 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003815
Willy Tarreau350f4872014-11-28 14:42:25 +01003816 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3817 s->si[1].err_type = SI_ET_NONE;
3818 s->si[1].conn_retries = 0; /* used for logging too */
3819 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003820 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 +01003821 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 +02003822 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 +02003823 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3824 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3825 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003826
Patrick Hemmere3faf022018-08-22 10:02:00 -04003827 hlua_ctx_destroy(s->hlua);
3828 s->hlua = NULL;
3829
Willy Tarreaueee5b512015-04-03 23:46:31 +02003830 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003831 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003832 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003833
3834 if (prev_status == 401 || prev_status == 407) {
3835 /* In HTTP keep-alive mode, if we receive a 401, we still have
3836 * a chance of being able to send the visitor again to the same
3837 * server over the same connection. This is required by some
3838 * broken protocols such as NTLM, and anyway whenever there is
3839 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003840 * it's better to do it (at least it helps with debugging), at
3841 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003842 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003843 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003844 }
3845
Willy Tarreau53f96852016-02-02 18:50:47 +01003846 /* Never ever allow to reuse a connection from a non-reuse backend */
3847 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3848 srv_conn->flags |= CO_FL_PRIVATE;
3849
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003850 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003851 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003852
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003853 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003854 s->req.flags |= CF_NEVER_WAIT;
3855 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003856 }
3857
Willy Tarreau714ea782015-11-25 20:11:11 +01003858 /* we're removing the analysers, we MUST re-enable events detection.
3859 * We don't enable close on the response channel since it's either
3860 * already closed, or in keep-alive with an idle connection handler.
3861 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003862 channel_auto_read(&s->req);
3863 channel_auto_close(&s->req);
3864 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003865
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003866 /* we're in keep-alive with an idle connection, monitor it if not already done */
3867 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003868 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003869 if (srv) {
3870 if (srv_conn->flags & CO_FL_PRIVATE)
3871 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3872 else if (prev_flags & TX_NOT_FIRST)
3873 /* note: we check the request, not the connection, but
3874 * this is valid for strategies SAFE and AGGR, and in
3875 * case of ALWS, we don't care anyway.
3876 */
3877 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3878 else
3879 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3880 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003881 }
Christopher Faulete6006242017-03-10 11:52:44 +01003882 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3883 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003884}
3885
3886
3887/* This function updates the request state machine according to the response
3888 * state machine and buffer flags. It returns 1 if it changes anything (flag
3889 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3890 * it is only used to find when a request/response couple is complete. Both
3891 * this function and its equivalent should loop until both return zero. It
3892 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3893 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003894int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003895{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003896 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003897 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003898 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003899 unsigned int old_state = txn->req.msg_state;
3900
Christopher Faulet4be98032017-07-18 10:48:24 +02003901 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003902 return 0;
3903
3904 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003905 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003906 * We can shut the read side unless we want to abort_on_close,
3907 * or we have a POST request. The issue with POST requests is
3908 * that some browsers still send a CRLF after the request, and
3909 * this CRLF must be read so that it does not remain in the kernel
3910 * buffers, otherwise a close could cause an RST on some systems
3911 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003912 * Note that if we're using keep-alive on the client side, we'd
3913 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003914 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003915 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003916 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003917 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3918 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003919 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3920 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003921 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003922 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003923
Willy Tarreau40f151a2012-12-20 12:10:09 +01003924 /* if the server closes the connection, we want to immediately react
3925 * and close the socket to save packets and syscalls.
3926 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003927 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003928
Willy Tarreau7f876a12015-11-18 11:59:55 +01003929 /* In any case we've finished parsing the request so we must
3930 * disable Nagle when sending data because 1) we're not going
3931 * to shut this side, and 2) the server is waiting for us to
3932 * send pending data.
3933 */
3934 chn->flags |= CF_NEVER_WAIT;
3935
Willy Tarreau610ecce2010-01-04 21:15:02 +01003936 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3937 goto wait_other_side;
3938
3939 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3940 /* The server has not finished to respond, so we
3941 * don't want to move in order not to upset it.
3942 */
3943 goto wait_other_side;
3944 }
3945
Willy Tarreau610ecce2010-01-04 21:15:02 +01003946 /* When we get here, it means that both the request and the
3947 * response have finished receiving. Depending on the connection
3948 * mode, we'll have to wait for the last bytes to leave in either
3949 * direction, and sometimes for a close to be effective.
3950 */
3951
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003952 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3953 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003954 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3955 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003956 }
3957 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3958 /* Option forceclose is set, or either side wants to close,
3959 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003960 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003961 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003962 *
3963 * However, there is an exception if the response
3964 * length is undefined. In this case, we need to wait
3965 * the close from the server. The response will be
3966 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003967 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003968 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3969 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3970 goto check_channel_flags;
3971
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003972 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3973 channel_shutr_now(chn);
3974 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003975 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003976 }
3977 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003978 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3979 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003980 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003981 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3982 channel_auto_read(chn);
3983 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003984 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003985 }
3986
Christopher Faulet4be98032017-07-18 10:48:24 +02003987 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003988 }
3989
3990 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3991 http_msg_closing:
3992 /* nothing else to forward, just waiting for the output buffer
3993 * to be empty and for the shutw_now to take effect.
3994 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003995 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003996 txn->req.msg_state = HTTP_MSG_CLOSED;
3997 goto http_msg_closed;
3998 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003999 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004000 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004001 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004002 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004003 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004004 }
4005
4006 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4007 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004008 /* if we don't know whether the server will close, we need to hard close */
4009 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4010 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4011
Willy Tarreau3988d932013-12-27 23:03:08 +01004012 /* see above in MSG_DONE why we only do this in these states */
4013 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4014 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004015 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4016 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004017 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004018 goto wait_other_side;
4019 }
4020
Christopher Faulet4be98032017-07-18 10:48:24 +02004021 check_channel_flags:
4022 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4023 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4024 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004025 txn->req.msg_state = HTTP_MSG_CLOSING;
4026 goto http_msg_closing;
4027 }
4028
4029
Willy Tarreau610ecce2010-01-04 21:15:02 +01004030 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004031 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004032}
4033
4034
4035/* This function updates the response state machine according to the request
4036 * state machine and buffer flags. It returns 1 if it changes anything (flag
4037 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4038 * it is only used to find when a request/response couple is complete. Both
4039 * this function and its equivalent should loop until both return zero. It
4040 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4041 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004042int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004043{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004044 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004045 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004046 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004047 unsigned int old_state = txn->rsp.msg_state;
4048
Christopher Faulet4be98032017-07-18 10:48:24 +02004049 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004050 return 0;
4051
4052 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4053 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004054 * still monitor the server connection for a possible close
4055 * while the request is being uploaded, so we don't disable
4056 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004057 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004058 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004059
4060 if (txn->req.msg_state == HTTP_MSG_ERROR)
4061 goto wait_other_side;
4062
4063 if (txn->req.msg_state < HTTP_MSG_DONE) {
4064 /* The client seems to still be sending data, probably
4065 * because we got an error response during an upload.
4066 * We have the choice of either breaking the connection
4067 * or letting it pass through. Let's do the later.
4068 */
4069 goto wait_other_side;
4070 }
4071
Willy Tarreau610ecce2010-01-04 21:15:02 +01004072 /* When we get here, it means that both the request and the
4073 * response have finished receiving. Depending on the connection
4074 * mode, we'll have to wait for the last bytes to leave in either
4075 * direction, and sometimes for a close to be effective.
4076 */
4077
4078 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4079 /* Server-close mode : shut read and wait for the request
4080 * side to close its output buffer. The caller will detect
4081 * when we're in DONE and the other is in CLOSED and will
4082 * catch that for the final cleanup.
4083 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004084 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4085 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004086 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004087 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4088 /* Option forceclose is set, or either side wants to close,
4089 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004090 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004091 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004092 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004093 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004094 channel_shutr_now(chn);
4095 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004096 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004097 }
4098 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004099 /* The last possible modes are keep-alive and tunnel. Tunnel will
4100 * need to forward remaining data. Keep-alive will need to monitor
4101 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004102 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004103 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004104 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004105 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4106 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004107 }
4108
Christopher Faulet4be98032017-07-18 10:48:24 +02004109 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004110 }
4111
4112 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4113 http_msg_closing:
4114 /* nothing else to forward, just waiting for the output buffer
4115 * to be empty and for the shutw_now to take effect.
4116 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004117 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004118 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4119 goto http_msg_closed;
4120 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004121 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004122 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004123 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004124 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004125 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004126 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004127 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004128 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004129 }
4130
4131 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4132 http_msg_closed:
4133 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004134 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004135 channel_auto_close(chn);
4136 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004137 goto wait_other_side;
4138 }
4139
Christopher Faulet4be98032017-07-18 10:48:24 +02004140 check_channel_flags:
4141 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4142 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4143 /* if we've just closed an output, let's switch */
4144 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4145 goto http_msg_closing;
4146 }
4147
Willy Tarreau610ecce2010-01-04 21:15:02 +01004148 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004149 /* We force the response to leave immediately if we're waiting for the
4150 * other side, since there is no pending shutdown to push it out.
4151 */
4152 if (!channel_is_empty(chn))
4153 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004154 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004155}
4156
4157
Christopher Faulet894da4c2017-07-18 11:29:07 +02004158/* Resync the request and response state machines. */
4159void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004160{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004161 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004162#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004163 int old_req_state = txn->req.msg_state;
4164 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004165#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004166
Willy Tarreau610ecce2010-01-04 21:15:02 +01004167 http_sync_req_state(s);
4168 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004169 if (!http_sync_res_state(s))
4170 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004171 if (!http_sync_req_state(s))
4172 break;
4173 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004174
Christopher Faulet894da4c2017-07-18 11:29:07 +02004175 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4176 "req->analysers=0x%08x res->analysers=0x%08x\n",
4177 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004178 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4179 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004180 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004181
4182
Willy Tarreau610ecce2010-01-04 21:15:02 +01004183 /* OK, both state machines agree on a compatible state.
4184 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004185 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4186 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004187 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4188 * means we must abort the request.
4189 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4190 * corresponding channel.
4191 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4192 * on the response with server-close mode means we've completed one
4193 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004194 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004195 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4196 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004197 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004198 channel_auto_close(&s->req);
4199 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004200 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004201 channel_auto_close(&s->res);
4202 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004203 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004204 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4205 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004206 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004207 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004208 channel_auto_close(&s->res);
4209 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004210 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004211 channel_abort(&s->req);
4212 channel_auto_close(&s->req);
4213 channel_auto_read(&s->req);
4214 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004215 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004216 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4217 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4218 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4219 s->req.analysers &= AN_REQ_FLT_END;
4220 if (HAS_REQ_DATA_FILTERS(s))
4221 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4222 }
4223 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4224 s->res.analysers &= AN_RES_FLT_END;
4225 if (HAS_RSP_DATA_FILTERS(s))
4226 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4227 }
4228 channel_auto_close(&s->req);
4229 channel_auto_read(&s->req);
4230 channel_auto_close(&s->res);
4231 channel_auto_read(&s->res);
4232 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004233 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4234 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004235 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004236 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4237 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4238 /* server-close/keep-alive: terminate this transaction,
4239 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004240 * a fresh-new transaction, but only once we're sure there's
4241 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004242 * another request. They must not hold any pending output data
4243 * and the response buffer must realigned
4244 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004245 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004246 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004247 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004248 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004249 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004250 else {
4251 s->req.analysers = AN_REQ_FLT_END;
4252 s->res.analysers = AN_RES_FLT_END;
4253 txn->flags |= TX_WAIT_CLEANUP;
4254 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004255 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004256}
4257
Willy Tarreaud98cf932009-12-27 22:54:55 +01004258/* This function is an analyser which forwards request body (including chunk
4259 * sizes if any). It is called as soon as we must forward, even if we forward
4260 * zero byte. The only situation where it must not be called is when we're in
4261 * tunnel mode and we want to forward till the close. It's used both to forward
4262 * remaining data and to resync after end of body. It expects the msg_state to
4263 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004264 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004265 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004266 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004267 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004268int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004269{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004270 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004271 struct http_txn *txn = s->txn;
4272 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004273 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004274
Christopher Faulet45073512018-07-20 10:16:29 +02004275 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 +02004276 now_ms, __FUNCTION__,
4277 s,
4278 req,
4279 req->rex, req->wex,
4280 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004281 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004282 req->analysers);
4283
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004284 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4285 return 0;
4286
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004287 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004288 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004289 /* Output closed while we were sending data. We must abort and
4290 * wake the other side up.
4291 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004292 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004293 msg->msg_state = HTTP_MSG_ERROR;
4294 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004295 return 1;
4296 }
4297
Willy Tarreaud98cf932009-12-27 22:54:55 +01004298 /* Note that we don't have to send 100-continue back because we don't
4299 * need the data to complete our job, and it's up to the server to
4300 * decide whether to return 100, 417 or anything else in return of
4301 * an "Expect: 100-continue" header.
4302 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004303 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004304 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4305 ? HTTP_MSG_CHUNK_SIZE
4306 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004307
4308 /* TODO/filters: when http-buffer-request option is set or if a
4309 * rule on url_param exists, the first chunk size could be
4310 * already parsed. In that case, msg->next is after the chunk
4311 * size (including the CRLF after the size). So this case should
4312 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004313 }
4314
Willy Tarreau7ba23542014-04-17 21:50:00 +02004315 /* Some post-connect processing might want us to refrain from starting to
4316 * forward data. Currently, the only reason for this is "balance url_param"
4317 * whichs need to parse/process the request after we've enabled forwarding.
4318 */
4319 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004320 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004321 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004322 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004323 channel_dont_close(req); /* don't fail on early shutr */
4324 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004325 }
4326 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4327 }
4328
Willy Tarreau80a92c02014-03-12 10:41:13 +01004329 /* in most states, we should abort in case of early close */
4330 channel_auto_close(req);
4331
Willy Tarreauefdf0942014-04-24 20:08:57 +02004332 if (req->to_forward) {
4333 /* We can't process the buffer's contents yet */
4334 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004335 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004336 }
4337
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004338 if (msg->msg_state < HTTP_MSG_DONE) {
4339 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4340 ? http_msg_forward_chunked_body(s, msg)
4341 : http_msg_forward_body(s, msg));
4342 if (!ret)
4343 goto missing_data_or_waiting;
4344 if (ret < 0)
4345 goto return_bad_req;
4346 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004347
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004348 /* other states, DONE...TUNNEL */
4349 /* we don't want to forward closes on DONE except in tunnel mode. */
4350 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4351 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004352
Christopher Faulet894da4c2017-07-18 11:29:07 +02004353 http_resync_states(s);
4354 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004355 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4356 if (req->flags & CF_SHUTW) {
4357 /* request errors are most likely due to the
4358 * server aborting the transfer. */
4359 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004360 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004361 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004362 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004363 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004364 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004365 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004366 }
4367
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004368 /* If "option abortonclose" is set on the backend, we want to monitor
4369 * the client's connection and forward any shutdown notification to the
4370 * server, which will decide whether to close or to go on processing the
4371 * request. We only do that in tunnel mode, and not in other modes since
4372 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004373 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004374 channel_auto_read(req);
4375 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4376 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4377 s->si[1].flags |= SI_FL_NOLINGER;
4378 channel_auto_close(req);
4379 }
4380 else if (s->txn->meth == HTTP_METH_POST) {
4381 /* POST requests may require to read extra CRLF sent by broken
4382 * browsers and which could cause an RST to be sent upon close
4383 * on some systems (eg: Linux). */
4384 channel_auto_read(req);
4385 }
4386 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004387
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004388 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004389 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004390 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004391 if (!(s->flags & SF_ERR_MASK))
4392 s->flags |= SF_ERR_CLICL;
4393 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004394 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004395 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004396 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004397 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004398 }
4399
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004400 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4401 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004402 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004403 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004404
4405 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004406 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004407
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004408 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004409 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004410 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004411 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004412
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004413 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004414 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004415 * And when content-length is used, we never want to let the possible
4416 * shutdown be forwarded to the other side, as the state machine will
4417 * take care of it once the client responds. It's also important to
4418 * prevent TIME_WAITs from accumulating on the backend side, and for
4419 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004420 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004421 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004422 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004423
Willy Tarreau5c620922011-05-11 19:56:11 +02004424 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004425 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004426 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004427 * modes are already handled by the stream sock layer. We must not do
4428 * this in content-length mode because it could present the MSG_MORE
4429 * flag with the last block of forwarded data, which would cause an
4430 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004431 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004432 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004433 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004434
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004435 return 0;
4436
Willy Tarreaud98cf932009-12-27 22:54:55 +01004437 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004438 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004439 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004440 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004441
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004442 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004443 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004444 txn->req.msg_state = HTTP_MSG_ERROR;
4445 if (txn->status) {
4446 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004447 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004448 } else {
4449 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004450 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004451 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004452 req->analysers &= AN_REQ_FLT_END;
4453 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 +01004454
Willy Tarreaue7dff022015-04-03 01:14:29 +02004455 if (!(s->flags & SF_ERR_MASK))
4456 s->flags |= SF_ERR_PRXCOND;
4457 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004458 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004459 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004460 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004461 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004462 }
4463 return 0;
4464
4465 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004466 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004467 txn->req.msg_state = HTTP_MSG_ERROR;
4468 if (txn->status) {
4469 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004470 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004471 } else {
4472 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004473 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004474 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004475 req->analysers &= AN_REQ_FLT_END;
4476 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 +01004477
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004478 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4479 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004480 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004481 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004482
Willy Tarreaue7dff022015-04-03 01:14:29 +02004483 if (!(s->flags & SF_ERR_MASK))
4484 s->flags |= SF_ERR_SRVCL;
4485 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004486 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004487 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004488 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004489 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004490 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004491 return 0;
4492}
4493
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004494/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4495 * processing can continue on next analysers, or zero if it either needs more
4496 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004497 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004498 * when it has nothing left to do, and may remove any analyser when it wants to
4499 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004500 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004501int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004502{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004503 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004504 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004505 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004506 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004507 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004508 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004509 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004510 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004511
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004512 srv_conn = cs_conn(objt_cs(s->si[1].end));
4513
Christopher Faulet45073512018-07-20 10:16:29 +02004514 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 +02004515 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004516 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004517 rep,
4518 rep->rex, rep->wex,
4519 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004520 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004521 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004522
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004523 /*
4524 * Now parse the partial (or complete) lines.
4525 * We will check the response syntax, and also join multi-line
4526 * headers. An index of all the lines will be elaborated while
4527 * parsing.
4528 *
4529 * For the parsing, we use a 28 states FSM.
4530 *
4531 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004532 * ci_head(rep) = beginning of response
4533 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4534 * ci_tail(rep) = end of input data
4535 * msg->eol = end of current header or line (LF or CRLF)
4536 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004537 */
4538
Willy Tarreau628c40c2014-04-24 19:11:26 +02004539 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004540 /* There's a protected area at the end of the buffer for rewriting
4541 * purposes. We don't want to start to parse the request if the
4542 * protected area is affected, because we may have to move processed
4543 * data later, which is much more complicated.
4544 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004545 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004546 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004547 /* some data has still not left the buffer, wake us once that's done */
4548 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4549 goto abort_response;
4550 channel_dont_close(rep);
4551 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004552 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004553 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004554 }
4555
Willy Tarreau188e2302018-06-15 11:11:53 +02004556 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004557 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004558 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004559
Willy Tarreauf37954d2018-06-15 18:31:02 +02004560 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004561 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004562 }
4563
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004564 /* 1: we might have to print this header in debug mode */
4565 if (unlikely((global.mode & MODE_DEBUG) &&
4566 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004567 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004568 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004569
Willy Tarreauf37954d2018-06-15 18:31:02 +02004570 sol = ci_head(rep);
4571 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004572 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004573
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004574 sol += hdr_idx_first_pos(&txn->hdr_idx);
4575 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004576
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004577 while (cur_idx) {
4578 eol = sol + txn->hdr_idx.v[cur_idx].len;
4579 debug_hdr("srvhdr", s, sol, eol);
4580 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4581 cur_idx = txn->hdr_idx.v[cur_idx].next;
4582 }
4583 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004584
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004585 /*
4586 * Now we quickly check if we have found a full valid response.
4587 * If not so, we check the FD and buffer states before leaving.
4588 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004589 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004590 * responses are checked first.
4591 *
4592 * Depending on whether the client is still there or not, we
4593 * may send an error response back or not. Note that normally
4594 * we should only check for HTTP status there, and check I/O
4595 * errors somewhere else.
4596 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004597
Willy Tarreau655dce92009-11-08 13:10:58 +01004598 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004599 /* Invalid response */
4600 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4601 /* we detected a parsing error. We want to archive this response
4602 * in the dedicated proxy area for later troubleshooting.
4603 */
4604 hdr_response_bad:
4605 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004606 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004607
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004608 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004609 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004610 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004611 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004612 }
Willy Tarreau64648412010-03-05 10:41:54 +01004613 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004614 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004615 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004616 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004617 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004618 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004619 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004620
Willy Tarreaue7dff022015-04-03 01:14:29 +02004621 if (!(s->flags & SF_ERR_MASK))
4622 s->flags |= SF_ERR_PRXCOND;
4623 if (!(s->flags & SF_FINST_MASK))
4624 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004625
4626 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004627 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004628
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004630 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004631 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004632 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004633 goto hdr_response_bad;
4634 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004635
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004636 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004637 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004638 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004639 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004640 else if (txn->flags & TX_NOT_FIRST)
4641 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004642
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004643 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004644 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004645 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004646 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004647 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004648
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004649 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004650 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004651 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004652
4653 /* Check to see if the server refused the early data.
4654 * If so, just send a 425
4655 */
4656 if (objt_cs(s->si[1].end)) {
4657 struct connection *conn = objt_cs(s->si[1].end)->conn;
4658
4659 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4660 txn->status = 425;
4661 }
4662
Willy Tarreau350f4872014-11-28 14:42:25 +01004663 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004664 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004665 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004666
Willy Tarreaue7dff022015-04-03 01:14:29 +02004667 if (!(s->flags & SF_ERR_MASK))
4668 s->flags |= SF_ERR_SRVCL;
4669 if (!(s->flags & SF_FINST_MASK))
4670 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004671 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004672 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004673
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004674 /* read timeout : return a 504 to the client. */
4675 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004676 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004677 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004678
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004679 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004680 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004681 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004682 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004683 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004684
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004685 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004686 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004687 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004688 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004689 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004690 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004691
Willy Tarreaue7dff022015-04-03 01:14:29 +02004692 if (!(s->flags & SF_ERR_MASK))
4693 s->flags |= SF_ERR_SRVTO;
4694 if (!(s->flags & SF_FINST_MASK))
4695 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004696 return 0;
4697 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004698
Willy Tarreauf003d372012-11-26 13:35:37 +01004699 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004700 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004701 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4702 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004703 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004704 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004705
Christopher Faulet0184ea72017-01-05 14:06:34 +01004706 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004707 channel_auto_close(rep);
4708
4709 txn->status = 400;
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 Tarreauf003d372012-11-26 13:35:37 +01004712
Willy Tarreaue7dff022015-04-03 01:14:29 +02004713 if (!(s->flags & SF_ERR_MASK))
4714 s->flags |= SF_ERR_CLICL;
4715 if (!(s->flags & SF_FINST_MASK))
4716 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004717
Willy Tarreau87b09662015-04-03 00:22:06 +02004718 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004719 return 0;
4720 }
4721
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004722 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004723 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004724 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004725 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004726 else if (txn->flags & TX_NOT_FIRST)
4727 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004728
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004729 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004730 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004731 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004732 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004733 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004734
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004735 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004736 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004737 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004738 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004739 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004740 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004741
Willy Tarreaue7dff022015-04-03 01:14:29 +02004742 if (!(s->flags & SF_ERR_MASK))
4743 s->flags |= SF_ERR_SRVCL;
4744 if (!(s->flags & SF_FINST_MASK))
4745 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004746 return 0;
4747 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004748
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004749 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004750 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004751 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004752 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004753 else if (txn->flags & TX_NOT_FIRST)
4754 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004755
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004756 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004757 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004758 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004759
Willy Tarreaue7dff022015-04-03 01:14:29 +02004760 if (!(s->flags & SF_ERR_MASK))
4761 s->flags |= SF_ERR_CLICL;
4762 if (!(s->flags & SF_FINST_MASK))
4763 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004764
Willy Tarreau87b09662015-04-03 00:22:06 +02004765 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004766 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004767 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004768
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004769 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004770 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004771 return 0;
4772 }
4773
4774 /* More interesting part now : we know that we have a complete
4775 * response which at least looks like HTTP. We have an indicator
4776 * of each header's length, so we can parse them quickly.
4777 */
4778
4779 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004780 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004781
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004782 /*
4783 * 1: get the status code
4784 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004785 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004786 if (n < 1 || n > 5)
4787 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004788 /* when the client triggers a 4xx from the server, it's most often due
4789 * to a missing object or permission. These events should be tracked
4790 * because if they happen often, it may indicate a brute force or a
4791 * vulnerability scan.
4792 */
4793 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004794 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004795
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004796 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004797 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004798
Willy Tarreau91852eb2015-05-01 13:26:00 +02004799 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4800 * exactly one digit "." one digit. This check may be disabled using
4801 * option accept-invalid-http-response.
4802 */
4803 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4804 if (msg->sl.st.v_l != 8) {
4805 msg->err_pos = 0;
4806 goto hdr_response_bad;
4807 }
4808
Willy Tarreauf37954d2018-06-15 18:31:02 +02004809 if (ci_head(rep)[4] != '/' ||
4810 !isdigit((unsigned char)ci_head(rep)[5]) ||
4811 ci_head(rep)[6] != '.' ||
4812 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004813 msg->err_pos = 4;
4814 goto hdr_response_bad;
4815 }
4816 }
4817
Willy Tarreau5b154472009-12-21 20:11:07 +01004818 /* check if the response is HTTP/1.1 or above */
4819 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004820 ((ci_head(rep)[5] > '1') ||
4821 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004822 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004823
4824 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004825 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 +01004826
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004827 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004828 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004829
Willy Tarreauf37954d2018-06-15 18:31:02 +02004830 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004831
Willy Tarreau39650402010-03-15 19:44:39 +01004832 /* Adjust server's health based on status code. Note: status codes 501
4833 * and 505 are triggered on demand by client request, so we must not
4834 * count them as server failures.
4835 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004836 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004837 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004838 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004839 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004840 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004841 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004842
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004843 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004844 * We may be facing a 100-continue response, or any other informational
4845 * 1xx response which is non-final, in which case this is not the right
4846 * response, and we're waiting for the next one. Let's allow this response
4847 * to go to the client and wait for the next one. There's an exception for
4848 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004849 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004850 if (txn->status < 200 &&
4851 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004852 hdr_idx_init(&txn->hdr_idx);
4853 msg->next -= channel_forward(rep, msg->next);
4854 msg->msg_state = HTTP_MSG_RPBEFORE;
4855 txn->status = 0;
4856 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004857 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004858 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004859 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004860
Willy Tarreaua14ad722017-07-07 11:36:32 +02004861 /*
4862 * 2: check for cacheability.
4863 */
4864
4865 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004866 case 200:
4867 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004868 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004869 case 206:
4870 case 300:
4871 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004872 case 404:
4873 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004874 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004875 case 414:
4876 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004877 break;
4878 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004879 /* RFC7231#6.1:
4880 * Responses with status codes that are defined as
4881 * cacheable by default (e.g., 200, 203, 204, 206,
4882 * 300, 301, 404, 405, 410, 414, and 501 in this
4883 * specification) can be reused by a cache with
4884 * heuristic expiration unless otherwise indicated
4885 * by the method definition or explicit cache
4886 * controls [RFC7234]; all other status codes are
4887 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004888 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004889 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004890 break;
4891 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004892
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004893 /*
4894 * 3: we may need to capture headers
4895 */
4896 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004897 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004898 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4899 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004900
Willy Tarreau557f1992015-05-01 10:05:17 +02004901 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4902 * by RFC7230#3.3.3 :
4903 *
4904 * The length of a message body is determined by one of the following
4905 * (in order of precedence):
4906 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004907 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4908 * the connection will become a tunnel immediately after the empty
4909 * line that concludes the header fields. A client MUST ignore
4910 * any Content-Length or Transfer-Encoding header fields received
4911 * in such a message. Any 101 response (Switching Protocols) is
4912 * managed in the same manner.
4913 *
4914 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004915 * (Informational), 204 (No Content), or 304 (Not Modified) status
4916 * code is always terminated by the first empty line after the
4917 * header fields, regardless of the header fields present in the
4918 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004919 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004920 * 3. If a Transfer-Encoding header field is present and the chunked
4921 * transfer coding (Section 4.1) is the final encoding, the message
4922 * body length is determined by reading and decoding the chunked
4923 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004924 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004925 * If a Transfer-Encoding header field is present in a response and
4926 * the chunked transfer coding is not the final encoding, the
4927 * message body length is determined by reading the connection until
4928 * it is closed by the server. If a Transfer-Encoding header field
4929 * is present in a request and the chunked transfer coding is not
4930 * the final encoding, the message body length cannot be determined
4931 * reliably; the server MUST respond with the 400 (Bad Request)
4932 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004933 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004934 * If a message is received with both a Transfer-Encoding and a
4935 * Content-Length header field, the Transfer-Encoding overrides the
4936 * Content-Length. Such a message might indicate an attempt to
4937 * perform request smuggling (Section 9.5) or response splitting
4938 * (Section 9.4) and ought to be handled as an error. A sender MUST
4939 * remove the received Content-Length field prior to forwarding such
4940 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004941 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004942 * 4. If a message is received without Transfer-Encoding and with
4943 * either multiple Content-Length header fields having differing
4944 * field-values or a single Content-Length header field having an
4945 * invalid value, then the message framing is invalid and the
4946 * recipient MUST treat it as an unrecoverable error. If this is a
4947 * request message, the server MUST respond with a 400 (Bad Request)
4948 * status code and then close the connection. If this is a response
4949 * message received by a proxy, the proxy MUST close the connection
4950 * to the server, discard the received response, and send a 502 (Bad
4951 * Gateway) response to the client. If this is a response message
4952 * received by a user agent, the user agent MUST close the
4953 * connection to the server and discard the received response.
4954 *
4955 * 5. If a valid Content-Length header field is present without
4956 * Transfer-Encoding, its decimal value defines the expected message
4957 * body length in octets. If the sender closes the connection or
4958 * the recipient times out before the indicated number of octets are
4959 * received, the recipient MUST consider the message to be
4960 * incomplete and close the connection.
4961 *
4962 * 6. If this is a request message and none of the above are true, then
4963 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004964 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004965 * 7. Otherwise, this is a response message without a declared message
4966 * body length, so the message body length is determined by the
4967 * number of octets received prior to the server closing the
4968 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004969 */
4970
4971 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004972 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004973 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004974 * FIXME: should we parse anyway and return an error on chunked encoding ?
4975 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004976 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4977 txn->status == 101)) {
4978 /* Either we've established an explicit tunnel, or we're
4979 * switching the protocol. In both cases, we're very unlikely
4980 * to understand the next protocols. We have to switch to tunnel
4981 * mode, so that we transfer the request and responses then let
4982 * this protocol pass unmodified. When we later implement specific
4983 * parsers for such protocols, we'll want to check the Upgrade
4984 * header which contains information about that protocol for
4985 * responses with status 101 (eg: see RFC2817 about TLS).
4986 */
4987 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4988 msg->flags |= HTTP_MSGF_XFER_LEN;
4989 goto end;
4990 }
4991
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004992 if (txn->meth == HTTP_METH_HEAD ||
4993 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004994 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004995 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004996 goto skip_content_length;
4997 }
4998
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004999 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005000 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005001 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005002 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005003 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5004 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005005 /* bad transfer-encoding (chunked followed by something else) */
5006 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005007 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005008 break;
5009 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005010 }
5011
Willy Tarreau1c913912015-04-30 10:57:51 +02005012 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005013 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005014 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005015 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005016 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5017 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005018 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005019 signed long long cl;
5020
Willy Tarreauad14f752011-09-02 20:33:27 +02005021 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005022 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005023 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005024 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005025
Willy Tarreauad14f752011-09-02 20:33:27 +02005026 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005027 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005028 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005029 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005030
Willy Tarreauad14f752011-09-02 20:33:27 +02005031 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005032 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005033 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005034 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005035
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005036 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005037 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005038 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005039 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005040
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005041 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005042 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005043 }
5044
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02005045 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
5046 * 407 (Proxy-Authenticate) responses and set the connection to private
5047 */
5048 if (srv_conn && txn->status == 401) {
5049 /* check for Negotiate/NTLM WWW-Authenticate headers */
5050 ctx.idx = 0;
5051 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
5052 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5053 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5054 srv_conn->flags |= CO_FL_PRIVATE;
5055 }
5056 } else if (srv_conn && txn->status == 407) {
5057 /* check for Negotiate/NTLM Proxy-Authenticate headers */
5058 ctx.idx = 0;
5059 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
5060 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5061 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5062 srv_conn->flags |= CO_FL_PRIVATE;
5063 }
5064 }
5065
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005066 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005067 /* Now we have to check if we need to modify the Connection header.
5068 * This is more difficult on the response than it is on the request,
5069 * because we can have two different HTTP versions and we don't know
5070 * how the client will interprete a response. For instance, let's say
5071 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5072 * HTTP/1.1 response without any header. Maybe it will bound itself to
5073 * HTTP/1.0 because it only knows about it, and will consider the lack
5074 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5075 * the lack of header as a keep-alive. Thus we will use two flags
5076 * indicating how a request MAY be understood by the client. In case
5077 * of multiple possibilities, we'll fix the header to be explicit. If
5078 * ambiguous cases such as both close and keepalive are seen, then we
5079 * will fall back to explicit close. Note that we won't take risks with
5080 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005081 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005082 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005083 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005084 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01005085 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005086
Willy Tarreau60466522010-01-18 19:08:45 +01005087 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005088 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01005089 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005090
Willy Tarreau60466522010-01-18 19:08:45 +01005091 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005092 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01005093 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005094 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005095 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005096 }
Willy Tarreau60466522010-01-18 19:08:45 +01005097 else { /* SCL / KAL */
5098 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005099 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005100 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005101 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005102
Willy Tarreau60466522010-01-18 19:08:45 +01005103 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005104 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005105
Willy Tarreau60466522010-01-18 19:08:45 +01005106 /* Some keep-alive responses are converted to Server-close if
5107 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005108 */
Willy Tarreau60466522010-01-18 19:08:45 +01005109 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5110 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005111 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005112 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005113 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005114 }
5115
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005116 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005117 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005118 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005119
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005120 /* end of job, return OK */
5121 rep->analysers &= ~an_bit;
5122 rep->analyse_exp = TICK_ETERNITY;
5123 channel_auto_close(rep);
5124 return 1;
5125
5126 abort_keep_alive:
5127 /* A keep-alive request to the server failed on a network error.
5128 * The client is required to retry. We need to close without returning
5129 * any other information so that the client retries.
5130 */
5131 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005132 rep->analysers &= AN_RES_FLT_END;
5133 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005134 channel_auto_close(rep);
5135 s->logs.logwait = 0;
5136 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005137 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005138 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005139 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005140 return 0;
5141}
5142
5143/* This function performs all the processing enabled for the current response.
5144 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005145 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005146 * other functions. It works like process_request (see indications above).
5147 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005148int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005149{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005150 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005151 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005152 struct http_msg *msg = &txn->rsp;
5153 struct proxy *cur_proxy;
5154 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005155 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005156
Christopher Faulet45073512018-07-20 10:16:29 +02005157 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 +02005158 now_ms, __FUNCTION__,
5159 s,
5160 rep,
5161 rep->rex, rep->wex,
5162 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005163 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005164 rep->analysers);
5165
5166 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5167 return 0;
5168
Willy Tarreau70730dd2014-04-24 18:06:27 +02005169 /* The stats applet needs to adjust the Connection header but we don't
5170 * apply any filter there.
5171 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005172 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5173 rep->analysers &= ~an_bit;
5174 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005175 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005176 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005177
Willy Tarreau58975672014-04-24 21:13:57 +02005178 /*
5179 * We will have to evaluate the filters.
5180 * As opposed to version 1.2, now they will be evaluated in the
5181 * filters order and not in the header order. This means that
5182 * each filter has to be validated among all headers.
5183 *
5184 * Filters are tried with ->be first, then with ->fe if it is
5185 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005186 *
5187 * Maybe we are in resume condiion. In this case I choose the
5188 * "struct proxy" which contains the rule list matching the resume
5189 * pointer. If none of theses "struct proxy" match, I initialise
5190 * the process with the first one.
5191 *
5192 * In fact, I check only correspondance betwwen the current list
5193 * pointer and the ->fe rule list. If it doesn't match, I initialize
5194 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005195 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005196 if (s->current_rule_list == &sess->fe->http_res_rules)
5197 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005198 else
5199 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005200 while (1) {
5201 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005202
Willy Tarreau58975672014-04-24 21:13:57 +02005203 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005204 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005205 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005206
Willy Tarreau51d861a2015-05-22 17:30:48 +02005207 if (ret == HTTP_RULE_RES_BADREQ)
5208 goto return_srv_prx_502;
5209
5210 if (ret == HTTP_RULE_RES_DONE) {
5211 rep->analysers &= ~an_bit;
5212 rep->analyse_exp = TICK_ETERNITY;
5213 return 1;
5214 }
5215 }
5216
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005217 /* we need to be called again. */
5218 if (ret == HTTP_RULE_RES_YIELD) {
5219 channel_dont_close(rep);
5220 return 0;
5221 }
5222
Willy Tarreau58975672014-04-24 21:13:57 +02005223 /* try headers filters */
5224 if (rule_set->rsp_exp != NULL) {
5225 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5226 return_bad_resp:
5227 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005228 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005229 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005230 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005231 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005232 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005233 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005234 txn->status = 502;
5235 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005236 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005237 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005238 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005239 if (!(s->flags & SF_ERR_MASK))
5240 s->flags |= SF_ERR_PRXCOND;
5241 if (!(s->flags & SF_FINST_MASK))
5242 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005243 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005244 }
Willy Tarreau58975672014-04-24 21:13:57 +02005245 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005246
Willy Tarreau58975672014-04-24 21:13:57 +02005247 /* has the response been denied ? */
5248 if (txn->flags & TX_SVDENY) {
5249 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005250 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005251
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005252 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5253 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005254 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005255 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005256
Willy Tarreau58975672014-04-24 21:13:57 +02005257 goto return_srv_prx_502;
5258 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005259
Willy Tarreau58975672014-04-24 21:13:57 +02005260 /* add response headers from the rule sets in the same order */
5261 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005262 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005263 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005264 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005265 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005266 ret = acl_pass(ret);
5267 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5268 ret = !ret;
5269 if (!ret)
5270 continue;
5271 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005272 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005273 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005274 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005275
Willy Tarreau58975672014-04-24 21:13:57 +02005276 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005277 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005278 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005279 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005280 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005281
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005282 /* After this point, this anayzer can't return yield, so we can
5283 * remove the bit corresponding to this analyzer from the list.
5284 *
5285 * Note that the intermediate returns and goto found previously
5286 * reset the analyzers.
5287 */
5288 rep->analysers &= ~an_bit;
5289 rep->analyse_exp = TICK_ETERNITY;
5290
Willy Tarreau58975672014-04-24 21:13:57 +02005291 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005292 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005293 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005294
Willy Tarreau58975672014-04-24 21:13:57 +02005295 /*
5296 * Now check for a server cookie.
5297 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005298 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005299 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005300
Willy Tarreau58975672014-04-24 21:13:57 +02005301 /*
5302 * Check for cache-control or pragma headers if required.
5303 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005304 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005305 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005306
Willy Tarreau58975672014-04-24 21:13:57 +02005307 /*
5308 * Add server cookie in the response if needed
5309 */
5310 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5311 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005312 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005313 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5314 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5315 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5316 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5317 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005318 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005319 /* the server is known, it's not the one the client requested, or the
5320 * cookie's last seen date needs to be refreshed. We have to
5321 * insert a set-cookie here, except if we want to insert only on POST
5322 * requests and this one isn't. Note that servers which don't have cookies
5323 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005324 */
Willy Tarreau58975672014-04-24 21:13:57 +02005325 if (!objt_server(s->target)->cookie) {
5326 chunk_printf(&trash,
5327 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5328 s->be->cookie_name);
5329 }
5330 else {
5331 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005332
Willy Tarreau58975672014-04-24 21:13:57 +02005333 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5334 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005335 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5336 s30tob64((date.tv_sec+3) >> 2,
5337 trash.area + trash.data);
5338 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005339
Willy Tarreau58975672014-04-24 21:13:57 +02005340 if (s->be->cookie_maxlife) {
5341 /* emit first_date, which is either the original one or
5342 * the current date.
5343 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005344 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005345 s30tob64(txn->cookie_first_date ?
5346 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005347 (date.tv_sec+3) >> 2,
5348 trash.area + trash.data);
5349 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005350 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005351 }
Willy Tarreau58975672014-04-24 21:13:57 +02005352 chunk_appendf(&trash, "; path=/");
5353 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005354
Willy Tarreau58975672014-04-24 21:13:57 +02005355 if (s->be->cookie_domain)
5356 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005357
Willy Tarreau58975672014-04-24 21:13:57 +02005358 if (s->be->ck_opts & PR_CK_HTTPONLY)
5359 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005360
Willy Tarreau58975672014-04-24 21:13:57 +02005361 if (s->be->ck_opts & PR_CK_SECURE)
5362 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005363
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005364 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005365 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005366
Willy Tarreau58975672014-04-24 21:13:57 +02005367 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005368 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005369 /* the server did not change, only the date was updated */
5370 txn->flags |= TX_SCK_UPDATED;
5371 else
5372 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005373
Willy Tarreau58975672014-04-24 21:13:57 +02005374 /* Here, we will tell an eventual cache on the client side that we don't
5375 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5376 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5377 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005378 */
Willy Tarreau58975672014-04-24 21:13:57 +02005379 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005380
Willy Tarreau58975672014-04-24 21:13:57 +02005381 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005382
Willy Tarreau58975672014-04-24 21:13:57 +02005383 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5384 "Cache-control: private", 22) < 0))
5385 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005386 }
Willy Tarreau58975672014-04-24 21:13:57 +02005387 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005388
Willy Tarreau58975672014-04-24 21:13:57 +02005389 /*
5390 * Check if result will be cacheable with a cookie.
5391 * We'll block the response if security checks have caught
5392 * nasty things such as a cacheable cookie.
5393 */
5394 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5395 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5396 (s->be->options & PR_O_CHK_CACHE)) {
5397 /* we're in presence of a cacheable response containing
5398 * a set-cookie header. We'll block it as requested by
5399 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005400 */
Willy Tarreau58975672014-04-24 21:13:57 +02005401 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005402 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005403
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005404 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5405 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005406 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005407 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005408
Christopher Faulet767a84b2017-11-24 16:50:31 +01005409 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5410 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005411 send_log(s->be, LOG_ALERT,
5412 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5413 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5414 goto return_srv_prx_502;
5415 }
Willy Tarreau03945942009-12-22 16:50:27 +01005416
Willy Tarreau70730dd2014-04-24 18:06:27 +02005417 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005418 /*
5419 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5420 * If an "Upgrade" token is found, the header is left untouched in order
5421 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005422 * if anything but "Upgrade" is present in the Connection header. We don't
5423 * want to touch any 101 response either since it's switching to another
5424 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005425 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005426 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005427 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005428 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005429
Willy Tarreau58975672014-04-24 21:13:57 +02005430 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5431 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5432 /* we want a keep-alive response here. Keep-alive header
5433 * required if either side is not 1.1.
5434 */
5435 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5436 want_flags |= TX_CON_KAL_SET;
5437 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005438 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005439 /* we want a close response here. Close header required if
5440 * the server is 1.1, regardless of the client.
5441 */
5442 if (msg->flags & HTTP_MSGF_VER_11)
5443 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005444 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005445
Willy Tarreau58975672014-04-24 21:13:57 +02005446 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5447 http_change_connection_header(txn, msg, want_flags);
5448 }
5449
5450 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005451 /* Always enter in the body analyzer */
5452 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5453 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005454
Willy Tarreau58975672014-04-24 21:13:57 +02005455 /* if the user wants to log as soon as possible, without counting
5456 * bytes from the server, then this is the right moment. We have
5457 * to temporarily assign bytes_out to log what we currently have.
5458 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005459 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005460 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5461 s->logs.bytes_out = txn->rsp.eoh;
5462 s->do_log(s);
5463 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005464 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005465 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005466}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005467
Willy Tarreaud98cf932009-12-27 22:54:55 +01005468/* This function is an analyser which forwards response body (including chunk
5469 * sizes if any). It is called as soon as we must forward, even if we forward
5470 * zero byte. The only situation where it must not be called is when we're in
5471 * tunnel mode and we want to forward till the close. It's used both to forward
5472 * remaining data and to resync after end of body. It expects the msg_state to
5473 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005474 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005475 *
5476 * It is capable of compressing response data both in content-length mode and
5477 * in chunked mode. The state machines follows different flows depending on
5478 * whether content-length and chunked modes are used, since there are no
5479 * trailers in content-length :
5480 *
5481 * chk-mode cl-mode
5482 * ,----- BODY -----.
5483 * / \
5484 * V size > 0 V chk-mode
5485 * .--> SIZE -------------> DATA -------------> CRLF
5486 * | | size == 0 | last byte |
5487 * | v final crlf v inspected |
5488 * | TRAILERS -----------> DONE |
5489 * | |
5490 * `----------------------------------------------'
5491 *
5492 * Compression only happens in the DATA state, and must be flushed in final
5493 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5494 * is performed at once on final states for all bytes parsed, or when leaving
5495 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005496 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005497int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005498{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005499 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005500 struct http_txn *txn = s->txn;
5501 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005502 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005503
Christopher Faulet45073512018-07-20 10:16:29 +02005504 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 +02005505 now_ms, __FUNCTION__,
5506 s,
5507 res,
5508 res->rex, res->wex,
5509 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005510 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005511 res->analysers);
5512
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005513 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5514 return 0;
5515
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005516 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005517 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005518 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005519 /* Output closed while we were sending data. We must abort and
5520 * wake the other side up.
5521 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005522 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005523 msg->msg_state = HTTP_MSG_ERROR;
5524 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005525 return 1;
5526 }
5527
Willy Tarreau4fe41902010-06-07 22:27:41 +02005528 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005529 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005530
Christopher Fauletd7c91962015-04-30 11:48:27 +02005531 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005532 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5533 ? HTTP_MSG_CHUNK_SIZE
5534 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005535 }
5536
Willy Tarreauefdf0942014-04-24 20:08:57 +02005537 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005538 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005539 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005540 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005541 }
5542
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005543 if (msg->msg_state < HTTP_MSG_DONE) {
5544 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5545 ? http_msg_forward_chunked_body(s, msg)
5546 : http_msg_forward_body(s, msg));
5547 if (!ret)
5548 goto missing_data_or_waiting;
5549 if (ret < 0)
5550 goto return_bad_res;
5551 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005552
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005553 /* other states, DONE...TUNNEL */
5554 /* for keep-alive we don't want to forward closes on DONE */
5555 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5556 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5557 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005558
Christopher Faulet894da4c2017-07-18 11:29:07 +02005559 http_resync_states(s);
5560 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005561 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5562 if (res->flags & CF_SHUTW) {
5563 /* response errors are most likely due to the
5564 * client aborting the transfer. */
5565 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005566 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005567 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005568 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005569 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005570 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005571 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005572 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005573 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005574
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005575 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005576 if (res->flags & CF_SHUTW)
5577 goto aborted_xfer;
5578
5579 /* stop waiting for data if the input is closed before the end. If the
5580 * client side was already closed, it means that the client has aborted,
5581 * so we don't want to count this as a server abort. Otherwise it's a
5582 * server abort.
5583 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005584 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005585 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005586 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005587 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005588 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005589 if (!(s->flags & SF_ERR_MASK))
5590 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005591 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005592 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005593 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005594 goto return_bad_res_stats_ok;
5595 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005596 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005597
Willy Tarreau40dba092010-03-04 18:14:51 +01005598 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005599 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005600 goto return_bad_res;
5601
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005602 /* When TE: chunked is used, we need to get there again to parse
5603 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005604 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5605 * or if there are filters registered on the stream, we don't want to
5606 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005607 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005608 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005609 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005610 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5611 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005612 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005613
Willy Tarreau5c620922011-05-11 19:56:11 +02005614 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005615 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005616 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005617 * modes are already handled by the stream sock layer. We must not do
5618 * this in content-length mode because it could present the MSG_MORE
5619 * flag with the last block of forwarded data, which would cause an
5620 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005621 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005622 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005623 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005624
Willy Tarreau87b09662015-04-03 00:22:06 +02005625 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005626 return 0;
5627
Willy Tarreau40dba092010-03-04 18:14:51 +01005628 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005629 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005630 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005631 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005632
5633 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005634 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005635 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005636 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005637 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005638 res->analysers &= AN_RES_FLT_END;
5639 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 +01005640 if (objt_server(s->target))
5641 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005642
Willy Tarreaue7dff022015-04-03 01:14:29 +02005643 if (!(s->flags & SF_ERR_MASK))
5644 s->flags |= SF_ERR_PRXCOND;
5645 if (!(s->flags & SF_FINST_MASK))
5646 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005647 return 0;
5648
5649 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005650 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005651 txn->rsp.msg_state = HTTP_MSG_ERROR;
5652 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005653 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005654 res->analysers &= AN_RES_FLT_END;
5655 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 +01005656
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005657 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5658 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005659 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005660 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005661
Willy Tarreaue7dff022015-04-03 01:14:29 +02005662 if (!(s->flags & SF_ERR_MASK))
5663 s->flags |= SF_ERR_CLICL;
5664 if (!(s->flags & SF_FINST_MASK))
5665 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005666 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005667}
5668
5669
Christopher Faulet10079f52018-10-03 15:17:28 +02005670int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005671{
5672 struct channel *chn = msg->chn;
5673 int ret;
5674
5675 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5676
5677 if (msg->msg_state == HTTP_MSG_ENDING)
5678 goto ending;
5679
5680 /* Neither content-length, nor transfer-encoding was found, so we must
5681 * read the body until the server connection is closed. In that case, we
5682 * eat data as they come. Of course, this happens for response only. */
5683 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005684 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005685 msg->chunk_len += len;
5686 msg->body_len += len;
5687 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005688 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005689 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005690 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005691 msg->next += ret;
5692 msg->chunk_len -= ret;
5693 if (msg->chunk_len) {
5694 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005695 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005696 chn->flags |= CF_WAKE_WRITE;
5697 goto missing_data_or_waiting;
5698 }
5699
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005700 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5701 * always set for a request. */
5702 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5703 /* The server still sending data that should be filtered */
5704 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5705 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005706 msg->msg_state = HTTP_MSG_TUNNEL;
5707 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005708 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005709
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005710 msg->msg_state = HTTP_MSG_ENDING;
5711
5712 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005713 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005714 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005715 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5716 /* default_ret */ msg->next,
5717 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005718 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005719 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005720 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5721 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005722 if (msg->next)
5723 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005724
Christopher Fauletda02e172015-12-04 09:25:05 +01005725 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5726 /* default_ret */ 1,
5727 /* on_error */ goto error,
5728 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005729 if (msg->msg_state == HTTP_MSG_ENDING)
5730 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005731 return 1;
5732
5733 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005734 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005735 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5736 /* default_ret */ msg->next,
5737 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005738 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005739 msg->next -= ret;
5740 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5741 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005742 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005743 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005744 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005745 return 0;
5746 error:
5747 return -1;
5748}
5749
Christopher Faulet10079f52018-10-03 15:17:28 +02005750int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005751{
5752 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005753 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005754 int ret;
5755
5756 /* Here we have the guarantee to be in one of the following state:
5757 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5758 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5759
Christopher Fauletca874b82018-09-20 11:31:01 +02005760 if (msg->msg_state == HTTP_MSG_ENDING)
5761 goto ending;
5762
5763 /* Don't parse chunks if there is no input data */
5764 if (!ci_data(chn))
5765 goto waiting;
5766
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005767 switch_states:
5768 switch (msg->msg_state) {
5769 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005770 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005771 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005772 /* on_error */ goto error);
5773 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005774 msg->chunk_len -= ret;
5775 if (msg->chunk_len) {
5776 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005777 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005778 chn->flags |= CF_WAKE_WRITE;
5779 goto missing_data_or_waiting;
5780 }
5781
5782 /* nothing left to forward for this chunk*/
5783 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5784 /* fall through for HTTP_MSG_CHUNK_CRLF */
5785
5786 case HTTP_MSG_CHUNK_CRLF:
5787 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005788 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005789 if (ret == 0)
5790 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005791 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005792 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005793 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005794 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005795 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005796 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005797 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005798 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5799 /* fall through for HTTP_MSG_CHUNK_SIZE */
5800
5801 case HTTP_MSG_CHUNK_SIZE:
5802 /* read the chunk size and assign it to ->chunk_len,
5803 * then set ->next to point to the body and switch to
5804 * DATA or TRAILERS state.
5805 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005806 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005807 if (ret == 0)
5808 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005809 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005810 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005811 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005812 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005813 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005814 }
5815
5816 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005817 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005818 msg->chunk_len = chunk;
5819 msg->body_len += chunk;
5820
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005821 if (msg->chunk_len) {
5822 msg->msg_state = HTTP_MSG_DATA;
5823 goto switch_states;
5824 }
5825 msg->msg_state = HTTP_MSG_TRAILERS;
5826 /* fall through for HTTP_MSG_TRAILERS */
5827
5828 case HTTP_MSG_TRAILERS:
5829 ret = http_forward_trailers(msg);
5830 if (ret < 0)
5831 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005832 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5833 /* default_ret */ 1,
5834 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005835 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005836 if (!ret)
5837 goto missing_data_or_waiting;
5838 break;
5839
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005840 default:
5841 /* This should no happen in this function */
5842 goto error;
5843 }
5844
5845 msg->msg_state = HTTP_MSG_ENDING;
5846 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005847 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005848 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005849 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005850 /* default_ret */ msg->next,
5851 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005852 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005853 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005854 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5855 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005856 if (msg->next)
5857 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005858
Christopher Fauletda02e172015-12-04 09:25:05 +01005859 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005860 /* default_ret */ 1,
5861 /* on_error */ goto error,
5862 /* on_wait */ goto waiting);
5863 msg->msg_state = HTTP_MSG_DONE;
5864 return 1;
5865
5866 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005867 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005868 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005869 /* default_ret */ msg->next,
5870 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005871 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005872 msg->next -= ret;
5873 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5874 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005875 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005876 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005877 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005878 return 0;
5879
5880 chunk_parsing_error:
5881 if (msg->err_pos >= 0) {
5882 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005883 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005884 msg->msg_state, strm_fe(s));
5885 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005886 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005887 msg, msg->msg_state, s->be);
5888 }
5889 error:
5890 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005891}
5892
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005893
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005894/* Iterate the same filter through all request headers.
5895 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005896 * Since it can manage the switch to another backend, it updates the per-proxy
5897 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005898 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005899int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005900{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005901 char *cur_ptr, *cur_end, *cur_next;
5902 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005903 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005904 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005905 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005906
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005907 last_hdr = 0;
5908
Willy Tarreauf37954d2018-06-15 18:31:02 +02005909 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005910 old_idx = 0;
5911
5912 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005913 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005914 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005915 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005916 (exp->action == ACT_ALLOW ||
5917 exp->action == ACT_DENY ||
5918 exp->action == ACT_TARPIT))
5919 return 0;
5920
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005921 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005922 if (!cur_idx)
5923 break;
5924
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005925 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005926 cur_ptr = cur_next;
5927 cur_end = cur_ptr + cur_hdr->len;
5928 cur_next = cur_end + cur_hdr->cr + 1;
5929
5930 /* Now we have one header between cur_ptr and cur_end,
5931 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005932 */
5933
Willy Tarreau15a53a42015-01-21 13:39:42 +01005934 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005935 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005936 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005937 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005938 last_hdr = 1;
5939 break;
5940
5941 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005942 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005943 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005944 break;
5945
5946 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005947 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005948 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005949 break;
5950
5951 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005952 len = exp_replace(trash.area,
5953 trash.size, cur_ptr,
5954 exp->replace, pmatch);
5955 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005956 return -1;
5957
Willy Tarreau6e27be12018-08-22 04:46:47 +02005958 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5959
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005960 /* FIXME: if the user adds a newline in the replacement, the
5961 * index will not be recalculated for now, and the new line
5962 * will not be counted as a new header.
5963 */
5964
5965 cur_end += delta;
5966 cur_next += delta;
5967 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005968 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005969 break;
5970
5971 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005972 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005973 cur_next += delta;
5974
Willy Tarreaufa355d42009-11-29 18:12:29 +01005975 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005976 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5977 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005978 cur_hdr->len = 0;
5979 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005980 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005981 break;
5982
5983 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005984 }
5985
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005986 /* keep the link from this header to next one in case of later
5987 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005988 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005989 old_idx = cur_idx;
5990 }
5991 return 0;
5992}
5993
5994
5995/* Apply the filter to the request line.
5996 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5997 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005998 * Since it can manage the switch to another backend, it updates the per-proxy
5999 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006000 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006001int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006002{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006003 char *cur_ptr, *cur_end;
6004 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006005 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006006 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006007
Willy Tarreau3d300592007-03-18 18:34:41 +01006008 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006009 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006010 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006011 (exp->action == ACT_ALLOW ||
6012 exp->action == ACT_DENY ||
6013 exp->action == ACT_TARPIT))
6014 return 0;
6015 else if (exp->action == ACT_REMOVE)
6016 return 0;
6017
6018 done = 0;
6019
Willy Tarreauf37954d2018-06-15 18:31:02 +02006020 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006021 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006022
6023 /* Now we have the request line between cur_ptr and cur_end */
6024
Willy Tarreau15a53a42015-01-21 13:39:42 +01006025 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006026 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006027 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006028 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006029 done = 1;
6030 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006031
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006032 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006033 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006034 done = 1;
6035 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006036
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006037 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006038 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006039 done = 1;
6040 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006041
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006042 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006043 len = exp_replace(trash.area, trash.size,
6044 cur_ptr, exp->replace, pmatch);
6045 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006046 return -1;
6047
Willy Tarreau6e27be12018-08-22 04:46:47 +02006048 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6049
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006050 /* FIXME: if the user adds a newline in the replacement, the
6051 * index will not be recalculated for now, and the new line
6052 * will not be counted as a new header.
6053 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006054
Willy Tarreaufa355d42009-11-29 18:12:29 +01006055 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006056 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006057 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006058 HTTP_MSG_RQMETH,
6059 cur_ptr, cur_end + 1,
6060 NULL, NULL);
6061 if (unlikely(!cur_end))
6062 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006063
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006064 /* we have a full request and we know that we have either a CR
6065 * or an LF at <ptr>.
6066 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006067 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6068 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006069 /* there is no point trying this regex on headers */
6070 return 1;
6071 }
6072 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006073 return done;
6074}
Willy Tarreau97de6242006-12-27 17:18:38 +01006075
Willy Tarreau58f10d72006-12-04 02:26:12 +01006076
Willy Tarreau58f10d72006-12-04 02:26:12 +01006077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006078/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006079 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006080 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006081 * unparsable request. Since it can manage the switch to another backend, it
6082 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006083 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006084int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006085{
Willy Tarreau192252e2015-04-04 01:47:55 +02006086 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006087 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006088 struct hdr_exp *exp;
6089
6090 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006091 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006093 /*
6094 * The interleaving of transformations and verdicts
6095 * makes it difficult to decide to continue or stop
6096 * the evaluation.
6097 */
6098
Willy Tarreau6c123b12010-01-28 20:22:06 +01006099 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6100 break;
6101
Willy Tarreau3d300592007-03-18 18:34:41 +01006102 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006103 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006104 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006105 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006106
6107 /* if this filter had a condition, evaluate it now and skip to
6108 * next filter if the condition does not match.
6109 */
6110 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006111 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006112 ret = acl_pass(ret);
6113 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6114 ret = !ret;
6115
6116 if (!ret)
6117 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006118 }
6119
6120 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006121 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006122 if (unlikely(ret < 0))
6123 return -1;
6124
6125 if (likely(ret == 0)) {
6126 /* The filter did not match the request, it can be
6127 * iterated through all headers.
6128 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006129 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6130 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006131 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006132 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006133 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006134}
6135
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006136
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006137/* Delete a value in a header between delimiters <from> and <next> in buffer
6138 * <buf>. The number of characters displaced is returned, and the pointer to
6139 * the first delimiter is updated if required. The function tries as much as
6140 * possible to respect the following principles :
6141 * - replace <from> delimiter by the <next> one unless <from> points to a
6142 * colon, in which case <next> is simply removed
6143 * - set exactly one space character after the new first delimiter, unless
6144 * there are not enough characters in the block being moved to do so.
6145 * - remove unneeded spaces before the previous delimiter and after the new
6146 * one.
6147 *
6148 * It is the caller's responsibility to ensure that :
6149 * - <from> points to a valid delimiter or the colon ;
6150 * - <next> points to a valid delimiter or the final CR/LF ;
6151 * - there are non-space chars before <from> ;
6152 * - there is a CR/LF at or after <next>.
6153 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006154int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006155{
6156 char *prev = *from;
6157
6158 if (*prev == ':') {
6159 /* We're removing the first value, preserve the colon and add a
6160 * space if possible.
6161 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006162 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006163 next++;
6164 prev++;
6165 if (prev < next)
6166 *prev++ = ' ';
6167
Willy Tarreau2235b262016-11-05 15:50:20 +01006168 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006169 next++;
6170 } else {
6171 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006172 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006173 prev--;
6174 *from = prev;
6175
6176 /* copy the delimiter and if possible a space if we're
6177 * not at the end of the line.
6178 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006179 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006180 *prev++ = *next++;
6181 if (prev + 1 < next)
6182 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006183 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006184 next++;
6185 }
6186 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006187 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006188}
6189
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006190/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006191 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006192 * desirable to call it only when needed. This code is quite complex because
6193 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6194 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006195 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006196void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006197{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006198 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006199 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006200 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006201 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006202 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6203 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006204
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006205 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006206 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006207 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006208
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006209 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006210 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006211 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006212
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006213 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006214 hdr_beg = hdr_next;
6215 hdr_end = hdr_beg + cur_hdr->len;
6216 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006217
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006218 /* We have one full header between hdr_beg and hdr_end, and the
6219 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006220 * "Cookie:" headers.
6221 */
6222
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006223 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006224 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006225 old_idx = cur_idx;
6226 continue;
6227 }
6228
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006229 del_from = NULL; /* nothing to be deleted */
6230 preserve_hdr = 0; /* assume we may kill the whole header */
6231
Willy Tarreau58f10d72006-12-04 02:26:12 +01006232 /* Now look for cookies. Conforming to RFC2109, we have to support
6233 * attributes whose name begin with a '$', and associate them with
6234 * the right cookie, if we want to delete this cookie.
6235 * So there are 3 cases for each cookie read :
6236 * 1) it's a special attribute, beginning with a '$' : ignore it.
6237 * 2) it's a server id cookie that we *MAY* want to delete : save
6238 * some pointers on it (last semi-colon, beginning of cookie...)
6239 * 3) it's an application cookie : we *MAY* have to delete a previous
6240 * "special" cookie.
6241 * At the end of loop, if a "special" cookie remains, we may have to
6242 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006243 * *MUST* delete it.
6244 *
6245 * Note: RFC2965 is unclear about the processing of spaces around
6246 * the equal sign in the ATTR=VALUE form. A careful inspection of
6247 * the RFC explicitly allows spaces before it, and not within the
6248 * tokens (attrs or values). An inspection of RFC2109 allows that
6249 * too but section 10.1.3 lets one think that spaces may be allowed
6250 * after the equal sign too, resulting in some (rare) buggy
6251 * implementations trying to do that. So let's do what servers do.
6252 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6253 * allowed quoted strings in values, with any possible character
6254 * after a backslash, including control chars and delimitors, which
6255 * causes parsing to become ambiguous. Browsers also allow spaces
6256 * within values even without quotes.
6257 *
6258 * We have to keep multiple pointers in order to support cookie
6259 * removal at the beginning, middle or end of header without
6260 * corrupting the header. All of these headers are valid :
6261 *
6262 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6263 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6264 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6265 * | | | | | | | | |
6266 * | | | | | | | | hdr_end <--+
6267 * | | | | | | | +--> next
6268 * | | | | | | +----> val_end
6269 * | | | | | +-----------> val_beg
6270 * | | | | +--------------> equal
6271 * | | | +----------------> att_end
6272 * | | +---------------------> att_beg
6273 * | +--------------------------> prev
6274 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006275 */
6276
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006277 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6278 /* Iterate through all cookies on this line */
6279
6280 /* find att_beg */
6281 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006282 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006283 att_beg++;
6284
6285 /* find att_end : this is the first character after the last non
6286 * space before the equal. It may be equal to hdr_end.
6287 */
6288 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006289
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006290 while (equal < hdr_end) {
6291 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006292 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006293 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006294 continue;
6295 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006296 }
6297
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006298 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6299 * is between <att_beg> and <equal>, both may be identical.
6300 */
6301
6302 /* look for end of cookie if there is an equal sign */
6303 if (equal < hdr_end && *equal == '=') {
6304 /* look for the beginning of the value */
6305 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006306 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006307 val_beg++;
6308
6309 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006310 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006311
6312 /* make val_end point to the first white space or delimitor after the value */
6313 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006314 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006315 val_end--;
6316 } else {
6317 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006318 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006319
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006320 /* We have nothing to do with attributes beginning with '$'. However,
6321 * they will automatically be removed if a header before them is removed,
6322 * since they're supposed to be linked together.
6323 */
6324 if (*att_beg == '$')
6325 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006326
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006327 /* Ignore cookies with no equal sign */
6328 if (equal == next) {
6329 /* This is not our cookie, so we must preserve it. But if we already
6330 * scheduled another cookie for removal, we cannot remove the
6331 * complete header, but we can remove the previous block itself.
6332 */
6333 preserve_hdr = 1;
6334 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006335 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006336 val_end += delta;
6337 next += delta;
6338 hdr_end += delta;
6339 hdr_next += delta;
6340 cur_hdr->len += delta;
6341 http_msg_move_end(&txn->req, delta);
6342 prev = del_from;
6343 del_from = NULL;
6344 }
6345 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006346 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006347
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006348 /* if there are spaces around the equal sign, we need to
6349 * strip them otherwise we'll get trouble for cookie captures,
6350 * or even for rewrites. Since this happens extremely rarely,
6351 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006352 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006353 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6354 int stripped_before = 0;
6355 int stripped_after = 0;
6356
6357 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006358 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006359 equal += stripped_before;
6360 val_beg += stripped_before;
6361 }
6362
6363 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006364 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006365 val_beg += stripped_after;
6366 stripped_before += stripped_after;
6367 }
6368
6369 val_end += stripped_before;
6370 next += stripped_before;
6371 hdr_end += stripped_before;
6372 hdr_next += stripped_before;
6373 cur_hdr->len += stripped_before;
6374 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006375 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006376 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006377
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006378 /* First, let's see if we want to capture this cookie. We check
6379 * that we don't already have a client side cookie, because we
6380 * can only capture one. Also as an optimisation, we ignore
6381 * cookies shorter than the declared name.
6382 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006383 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6384 (val_end - att_beg >= sess->fe->capture_namelen) &&
6385 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006386 int log_len = val_end - att_beg;
6387
Willy Tarreaubafbe012017-11-24 17:34:44 +01006388 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006389 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006390 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006391 if (log_len > sess->fe->capture_len)
6392 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006393 memcpy(txn->cli_cookie, att_beg, log_len);
6394 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006395 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006396 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006397
Willy Tarreaubca99692010-10-06 19:25:55 +02006398 /* Persistence cookies in passive, rewrite or insert mode have the
6399 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006400 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006401 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006402 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006403 * For cookies in prefix mode, the form is :
6404 *
6405 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006406 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006407 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6408 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6409 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006410 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006411
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006412 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6413 * have the server ID between val_beg and delim, and the original cookie between
6414 * delim+1 and val_end. Otherwise, delim==val_end :
6415 *
6416 * Cookie: NAME=SRV; # in all but prefix modes
6417 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6418 * | || || | |+-> next
6419 * | || || | +--> val_end
6420 * | || || +---------> delim
6421 * | || |+------------> val_beg
6422 * | || +-------------> att_end = equal
6423 * | |+-----------------> att_beg
6424 * | +------------------> prev
6425 * +-------------------------> hdr_beg
6426 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006427
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006428 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006429 for (delim = val_beg; delim < val_end; delim++)
6430 if (*delim == COOKIE_DELIM)
6431 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006432 } else {
6433 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006434 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006435 /* Now check if the cookie contains a date field, which would
6436 * appear after a vertical bar ('|') just after the server name
6437 * and before the delimiter.
6438 */
6439 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6440 if (vbar1) {
6441 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006442 * right is the last seen date. It is a base64 encoded
6443 * 30-bit value representing the UNIX date since the
6444 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006445 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006446 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006447 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006448 if (val_end - vbar1 >= 5) {
6449 val = b64tos30(vbar1);
6450 if (val > 0)
6451 txn->cookie_last_date = val << 2;
6452 }
6453 /* look for a second vertical bar */
6454 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6455 if (vbar1 && (val_end - vbar1 > 5)) {
6456 val = b64tos30(vbar1 + 1);
6457 if (val > 0)
6458 txn->cookie_first_date = val << 2;
6459 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006460 }
6461 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006462
Willy Tarreauf64d1412010-10-07 20:06:11 +02006463 /* if the cookie has an expiration date and the proxy wants to check
6464 * it, then we do that now. We first check if the cookie is too old,
6465 * then only if it has expired. We detect strict overflow because the
6466 * time resolution here is not great (4 seconds). Cookies with dates
6467 * in the future are ignored if their offset is beyond one day. This
6468 * allows an admin to fix timezone issues without expiring everyone
6469 * and at the same time avoids keeping unwanted side effects for too
6470 * long.
6471 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006472 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6473 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006474 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006475 txn->flags &= ~TX_CK_MASK;
6476 txn->flags |= TX_CK_OLD;
6477 delim = val_beg; // let's pretend we have not found the cookie
6478 txn->cookie_first_date = 0;
6479 txn->cookie_last_date = 0;
6480 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006481 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6482 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006483 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006484 txn->flags &= ~TX_CK_MASK;
6485 txn->flags |= TX_CK_EXPIRED;
6486 delim = val_beg; // let's pretend we have not found the cookie
6487 txn->cookie_first_date = 0;
6488 txn->cookie_last_date = 0;
6489 }
6490
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006491 /* Here, we'll look for the first running server which supports the cookie.
6492 * This allows to share a same cookie between several servers, for example
6493 * to dedicate backup servers to specific servers only.
6494 * However, to prevent clients from sticking to cookie-less backup server
6495 * when they have incidentely learned an empty cookie, we simply ignore
6496 * empty cookies and mark them as invalid.
6497 * The same behaviour is applied when persistence must be ignored.
6498 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006499 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006500 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006501
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006502 while (srv) {
6503 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6504 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006505 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006506 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006507 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006508 /* we found the server and we can use it */
6509 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006510 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006511 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006512 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006513 break;
6514 } else {
6515 /* we found a server, but it's down,
6516 * mark it as such and go on in case
6517 * another one is available.
6518 */
6519 txn->flags &= ~TX_CK_MASK;
6520 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006521 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006522 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006523 srv = srv->next;
6524 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006525
Willy Tarreauf64d1412010-10-07 20:06:11 +02006526 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006527 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006528 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006529 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006530 txn->flags |= TX_CK_UNUSED;
6531 else
6532 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006533 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006534
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006535 /* depending on the cookie mode, we may have to either :
6536 * - delete the complete cookie if we're in insert+indirect mode, so that
6537 * the server never sees it ;
6538 * - remove the server id from the cookie value, and tag the cookie as an
6539 * application cookie so that it does not get accidentely removed later,
6540 * if we're in cookie prefix mode
6541 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006542 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006543 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006544
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006545 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006546 val_end += delta;
6547 next += delta;
6548 hdr_end += delta;
6549 hdr_next += delta;
6550 cur_hdr->len += delta;
6551 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006552
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006553 del_from = NULL;
6554 preserve_hdr = 1; /* we want to keep this cookie */
6555 }
6556 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006557 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006558 del_from = prev;
6559 }
6560 } else {
6561 /* This is not our cookie, so we must preserve it. But if we already
6562 * scheduled another cookie for removal, we cannot remove the
6563 * complete header, but we can remove the previous block itself.
6564 */
6565 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006566
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006567 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006568 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006569 if (att_beg >= del_from)
6570 att_beg += delta;
6571 if (att_end >= del_from)
6572 att_end += delta;
6573 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006574 val_end += delta;
6575 next += delta;
6576 hdr_end += delta;
6577 hdr_next += delta;
6578 cur_hdr->len += delta;
6579 http_msg_move_end(&txn->req, delta);
6580 prev = del_from;
6581 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006582 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006583 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006584
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006585 /* continue with next cookie on this header line */
6586 att_beg = next;
6587 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006588
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006589 /* There are no more cookies on this line.
6590 * We may still have one (or several) marked for deletion at the
6591 * end of the line. We must do this now in two ways :
6592 * - if some cookies must be preserved, we only delete from the
6593 * mark to the end of line ;
6594 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006595 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006596 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006597 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006598 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006599 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006600 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006601 cur_hdr->len += delta;
6602 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006603 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006604
6605 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006606 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6607 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006608 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006609 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006610 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006611 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006612 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006613 }
6614
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006615 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006616 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006617 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006618}
6619
6620
Willy Tarreaua15645d2007-03-18 16:22:39 +01006621/* Iterate the same filter through all response headers contained in <rtr>.
6622 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6623 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006624int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006625{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006626 char *cur_ptr, *cur_end, *cur_next;
6627 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006628 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006630 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006631
6632 last_hdr = 0;
6633
Willy Tarreauf37954d2018-06-15 18:31:02 +02006634 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006635 old_idx = 0;
6636
6637 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006638 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006639 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006640 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641 (exp->action == ACT_ALLOW ||
6642 exp->action == ACT_DENY))
6643 return 0;
6644
6645 cur_idx = txn->hdr_idx.v[old_idx].next;
6646 if (!cur_idx)
6647 break;
6648
6649 cur_hdr = &txn->hdr_idx.v[cur_idx];
6650 cur_ptr = cur_next;
6651 cur_end = cur_ptr + cur_hdr->len;
6652 cur_next = cur_end + cur_hdr->cr + 1;
6653
6654 /* Now we have one header between cur_ptr and cur_end,
6655 * and the next header starts at cur_next.
6656 */
6657
Willy Tarreau15a53a42015-01-21 13:39:42 +01006658 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006659 switch (exp->action) {
6660 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006661 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006662 last_hdr = 1;
6663 break;
6664
6665 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006666 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006667 last_hdr = 1;
6668 break;
6669
6670 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006671 len = exp_replace(trash.area,
6672 trash.size, cur_ptr,
6673 exp->replace, pmatch);
6674 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006675 return -1;
6676
Willy Tarreau6e27be12018-08-22 04:46:47 +02006677 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6678
Willy Tarreaua15645d2007-03-18 16:22:39 +01006679 /* FIXME: if the user adds a newline in the replacement, the
6680 * index will not be recalculated for now, and the new line
6681 * will not be counted as a new header.
6682 */
6683
6684 cur_end += delta;
6685 cur_next += delta;
6686 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006687 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006688 break;
6689
6690 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006691 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006692 cur_next += delta;
6693
Willy Tarreaufa355d42009-11-29 18:12:29 +01006694 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006695 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6696 txn->hdr_idx.used--;
6697 cur_hdr->len = 0;
6698 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006699 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006700 break;
6701
6702 }
6703 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006704
6705 /* keep the link from this header to next one in case of later
6706 * removal of next header.
6707 */
6708 old_idx = cur_idx;
6709 }
6710 return 0;
6711}
6712
6713
6714/* Apply the filter to the status line in the response buffer <rtr>.
6715 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6716 * or -1 if a replacement resulted in an invalid status line.
6717 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006718int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006719{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006720 char *cur_ptr, *cur_end;
6721 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006722 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006723 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006724
Willy Tarreau3d300592007-03-18 18:34:41 +01006725 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006726 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006727 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006728 (exp->action == ACT_ALLOW ||
6729 exp->action == ACT_DENY))
6730 return 0;
6731 else if (exp->action == ACT_REMOVE)
6732 return 0;
6733
6734 done = 0;
6735
Willy Tarreauf37954d2018-06-15 18:31:02 +02006736 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006737 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006738
6739 /* Now we have the status line between cur_ptr and cur_end */
6740
Willy Tarreau15a53a42015-01-21 13:39:42 +01006741 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006742 switch (exp->action) {
6743 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006744 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006745 done = 1;
6746 break;
6747
6748 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006749 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006750 done = 1;
6751 break;
6752
6753 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006754 len = exp_replace(trash.area, trash.size,
6755 cur_ptr, exp->replace, pmatch);
6756 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006757 return -1;
6758
Willy Tarreau6e27be12018-08-22 04:46:47 +02006759 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6760
Willy Tarreaua15645d2007-03-18 16:22:39 +01006761 /* FIXME: if the user adds a newline in the replacement, the
6762 * index will not be recalculated for now, and the new line
6763 * will not be counted as a new header.
6764 */
6765
Willy Tarreaufa355d42009-11-29 18:12:29 +01006766 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006767 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006768 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006769 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006770 cur_ptr, cur_end + 1,
6771 NULL, NULL);
6772 if (unlikely(!cur_end))
6773 return -1;
6774
6775 /* we have a full respnse and we know that we have either a CR
6776 * or an LF at <ptr>.
6777 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006778 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006779 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006780 /* there is no point trying this regex on headers */
6781 return 1;
6782 }
6783 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006784 return done;
6785}
6786
6787
6788
6789/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006790 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006791 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6792 * unparsable response.
6793 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006794int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006795{
Willy Tarreau192252e2015-04-04 01:47:55 +02006796 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006797 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006798 struct hdr_exp *exp;
6799
6800 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006801 int ret;
6802
6803 /*
6804 * The interleaving of transformations and verdicts
6805 * makes it difficult to decide to continue or stop
6806 * the evaluation.
6807 */
6808
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006809 if (txn->flags & TX_SVDENY)
6810 break;
6811
Willy Tarreau3d300592007-03-18 18:34:41 +01006812 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006813 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6814 exp->action == ACT_PASS)) {
6815 exp = exp->next;
6816 continue;
6817 }
6818
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006819 /* if this filter had a condition, evaluate it now and skip to
6820 * next filter if the condition does not match.
6821 */
6822 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006823 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006824 ret = acl_pass(ret);
6825 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6826 ret = !ret;
6827 if (!ret)
6828 continue;
6829 }
6830
Willy Tarreaua15645d2007-03-18 16:22:39 +01006831 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006832 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833 if (unlikely(ret < 0))
6834 return -1;
6835
6836 if (likely(ret == 0)) {
6837 /* The filter did not match the response, it can be
6838 * iterated through all headers.
6839 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006840 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6841 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006842 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006843 }
6844 return 0;
6845}
6846
6847
Willy Tarreaua15645d2007-03-18 16:22:39 +01006848/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006849 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006850 * desirable to call it only when needed. This function is also used when we
6851 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006852 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006853void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006854{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006855 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006856 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006857 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006858 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006859 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006860 char *hdr_beg, *hdr_end, *hdr_next;
6861 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006862
Willy Tarreaua15645d2007-03-18 16:22:39 +01006863 /* Iterate through the headers.
6864 * we start with the start line.
6865 */
6866 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006867 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006868
6869 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6870 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006871 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006872
6873 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006874 hdr_beg = hdr_next;
6875 hdr_end = hdr_beg + cur_hdr->len;
6876 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006877
Willy Tarreau24581ba2010-08-31 22:39:35 +02006878 /* We have one full header between hdr_beg and hdr_end, and the
6879 * next header starts at hdr_next. We're only interested in
6880 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006881 */
6882
Willy Tarreau24581ba2010-08-31 22:39:35 +02006883 is_cookie2 = 0;
6884 prev = hdr_beg + 10;
6885 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006886 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006887 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6888 if (!val) {
6889 old_idx = cur_idx;
6890 continue;
6891 }
6892 is_cookie2 = 1;
6893 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006894 }
6895
Willy Tarreau24581ba2010-08-31 22:39:35 +02006896 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6897 * <prev> points to the colon.
6898 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006899 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006900
Willy Tarreau24581ba2010-08-31 22:39:35 +02006901 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6902 * check-cache is enabled) and we are not interested in checking
6903 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006904 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006905 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006906 return;
6907
Willy Tarreau24581ba2010-08-31 22:39:35 +02006908 /* OK so now we know we have to process this response cookie.
6909 * The format of the Set-Cookie header is slightly different
6910 * from the format of the Cookie header in that it does not
6911 * support the comma as a cookie delimiter (thus the header
6912 * cannot be folded) because the Expires attribute described in
6913 * the original Netscape's spec may contain an unquoted date
6914 * with a comma inside. We have to live with this because
6915 * many browsers don't support Max-Age and some browsers don't
6916 * support quoted strings. However the Set-Cookie2 header is
6917 * clean.
6918 *
6919 * We have to keep multiple pointers in order to support cookie
6920 * removal at the beginning, middle or end of header without
6921 * corrupting the header (in case of set-cookie2). A special
6922 * pointer, <scav> points to the beginning of the set-cookie-av
6923 * fields after the first semi-colon. The <next> pointer points
6924 * either to the end of line (set-cookie) or next unquoted comma
6925 * (set-cookie2). All of these headers are valid :
6926 *
6927 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6928 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6929 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6930 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6931 * | | | | | | | | | |
6932 * | | | | | | | | +-> next hdr_end <--+
6933 * | | | | | | | +------------> scav
6934 * | | | | | | +--------------> val_end
6935 * | | | | | +--------------------> val_beg
6936 * | | | | +----------------------> equal
6937 * | | | +------------------------> att_end
6938 * | | +----------------------------> att_beg
6939 * | +------------------------------> prev
6940 * +-----------------------------------------> hdr_beg
6941 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006942
Willy Tarreau24581ba2010-08-31 22:39:35 +02006943 for (; prev < hdr_end; prev = next) {
6944 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006945
Willy Tarreau24581ba2010-08-31 22:39:35 +02006946 /* find att_beg */
6947 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006948 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006949 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006950
Willy Tarreau24581ba2010-08-31 22:39:35 +02006951 /* find att_end : this is the first character after the last non
6952 * space before the equal. It may be equal to hdr_end.
6953 */
6954 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006955
Willy Tarreau24581ba2010-08-31 22:39:35 +02006956 while (equal < hdr_end) {
6957 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6958 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006959 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006960 continue;
6961 att_end = equal;
6962 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006963
Willy Tarreau24581ba2010-08-31 22:39:35 +02006964 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6965 * is between <att_beg> and <equal>, both may be identical.
6966 */
6967
6968 /* look for end of cookie if there is an equal sign */
6969 if (equal < hdr_end && *equal == '=') {
6970 /* look for the beginning of the value */
6971 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006972 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006973 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006974
Willy Tarreau24581ba2010-08-31 22:39:35 +02006975 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006976 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006977
6978 /* make val_end point to the first white space or delimitor after the value */
6979 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006980 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006981 val_end--;
6982 } else {
6983 /* <equal> points to next comma, semi-colon or EOL */
6984 val_beg = val_end = next = equal;
6985 }
6986
6987 if (next < hdr_end) {
6988 /* Set-Cookie2 supports multiple cookies, and <next> points to
6989 * a colon or semi-colon before the end. So skip all attr-value
6990 * pairs and look for the next comma. For Set-Cookie, since
6991 * commas are permitted in values, skip to the end.
6992 */
6993 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006994 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006995 else
6996 next = hdr_end;
6997 }
6998
6999 /* Now everything is as on the diagram above */
7000
7001 /* Ignore cookies with no equal sign */
7002 if (equal == val_end)
7003 continue;
7004
7005 /* If there are spaces around the equal sign, we need to
7006 * strip them otherwise we'll get trouble for cookie captures,
7007 * or even for rewrites. Since this happens extremely rarely,
7008 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007009 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007010 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7011 int stripped_before = 0;
7012 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007013
Willy Tarreau24581ba2010-08-31 22:39:35 +02007014 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007015 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007016 equal += stripped_before;
7017 val_beg += stripped_before;
7018 }
7019
7020 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007021 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007022 val_beg += stripped_after;
7023 stripped_before += stripped_after;
7024 }
7025
7026 val_end += stripped_before;
7027 next += stripped_before;
7028 hdr_end += stripped_before;
7029 hdr_next += stripped_before;
7030 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007031 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007032 }
7033
7034 /* First, let's see if we want to capture this cookie. We check
7035 * that we don't already have a server side cookie, because we
7036 * can only capture one. Also as an optimisation, we ignore
7037 * cookies shorter than the declared name.
7038 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007039 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007040 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007041 (val_end - att_beg >= sess->fe->capture_namelen) &&
7042 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007043 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007044 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007045 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007046 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007047 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007048 if (log_len > sess->fe->capture_len)
7049 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007050 memcpy(txn->srv_cookie, att_beg, log_len);
7051 txn->srv_cookie[log_len] = 0;
7052 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007053 }
7054
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007055 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007056 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007057 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007058 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7059 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007060 /* assume passive cookie by default */
7061 txn->flags &= ~TX_SCK_MASK;
7062 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007063
7064 /* If the cookie is in insert mode on a known server, we'll delete
7065 * this occurrence because we'll insert another one later.
7066 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007067 * a direct access.
7068 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007069 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007070 /* The "preserve" flag was set, we don't want to touch the
7071 * server's cookie.
7072 */
7073 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007074 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007075 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007076 /* this cookie must be deleted */
7077 if (*prev == ':' && next == hdr_end) {
7078 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007079 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007080 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7081 txn->hdr_idx.used--;
7082 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007083 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007084 hdr_next += delta;
7085 http_msg_move_end(&txn->rsp, delta);
7086 /* note: while both invalid now, <next> and <hdr_end>
7087 * are still equal, so the for() will stop as expected.
7088 */
7089 } else {
7090 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007091 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007092 next = prev;
7093 hdr_end += delta;
7094 hdr_next += delta;
7095 cur_hdr->len += delta;
7096 http_msg_move_end(&txn->rsp, delta);
7097 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007098 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007099 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007100 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007101 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007102 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007103 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007104 * with this server since we know it.
7105 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007106 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007107 next += delta;
7108 hdr_end += delta;
7109 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007110 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007111 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007112
Willy Tarreauf1348312010-10-07 15:54:11 +02007113 txn->flags &= ~TX_SCK_MASK;
7114 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007115 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007116 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007117 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007118 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007119 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007120 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007121 next += delta;
7122 hdr_end += delta;
7123 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007124 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007125 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007126
Willy Tarreau827aee92011-03-10 16:55:02 +01007127 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007128 txn->flags &= ~TX_SCK_MASK;
7129 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007130 }
7131 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007132 /* that's done for this cookie, check the next one on the same
7133 * line when next != hdr_end (only if is_cookie2).
7134 */
7135 }
7136 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007137 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007138 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007139}
7140
7141
Willy Tarreaua15645d2007-03-18 16:22:39 +01007142/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007143 * Parses the Cache-Control and Pragma request header fields to determine if
7144 * the request may be served from the cache and/or if it is cacheable. Updates
7145 * s->txn->flags.
7146 */
7147void check_request_for_cacheability(struct stream *s, struct channel *chn)
7148{
7149 struct http_txn *txn = s->txn;
7150 char *p1, *p2;
7151 char *cur_ptr, *cur_end, *cur_next;
7152 int pragma_found;
7153 int cc_found;
7154 int cur_idx;
7155
7156 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7157 return; /* nothing more to do here */
7158
7159 cur_idx = 0;
7160 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007161 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007162
7163 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7164 struct hdr_idx_elem *cur_hdr;
7165 int val;
7166
7167 cur_hdr = &txn->hdr_idx.v[cur_idx];
7168 cur_ptr = cur_next;
7169 cur_end = cur_ptr + cur_hdr->len;
7170 cur_next = cur_end + cur_hdr->cr + 1;
7171
7172 /* We have one full header between cur_ptr and cur_end, and the
7173 * next header starts at cur_next.
7174 */
7175
7176 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7177 if (val) {
7178 if ((cur_end - (cur_ptr + val) >= 8) &&
7179 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7180 pragma_found = 1;
7181 continue;
7182 }
7183 }
7184
William Lallemand8a16fe02018-05-22 11:04:33 +02007185 /* Don't use the cache and don't try to store if we found the
7186 * Authorization header */
7187 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7188 if (val) {
7189 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7190 txn->flags |= TX_CACHE_IGNORE;
7191 continue;
7192 }
7193
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007194 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7195 if (!val)
7196 continue;
7197
7198 /* OK, right now we know we have a cache-control header at cur_ptr */
7199 cc_found = 1;
7200 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7201
7202 if (p1 >= cur_end) /* no more info */
7203 continue;
7204
7205 /* p1 is at the beginning of the value */
7206 p2 = p1;
7207 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7208 p2++;
7209
7210 /* we have a complete value between p1 and p2. We don't check the
7211 * values after max-age, max-stale nor min-fresh, we simply don't
7212 * use the cache when they're specified.
7213 */
7214 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7215 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7216 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7217 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7218 txn->flags |= TX_CACHE_IGNORE;
7219 continue;
7220 }
7221
7222 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7223 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7224 continue;
7225 }
7226 }
7227
7228 /* RFC7234#5.4:
7229 * When the Cache-Control header field is also present and
7230 * understood in a request, Pragma is ignored.
7231 * When the Cache-Control header field is not present in a
7232 * request, caches MUST consider the no-cache request
7233 * pragma-directive as having the same effect as if
7234 * "Cache-Control: no-cache" were present.
7235 */
7236 if (!cc_found && pragma_found)
7237 txn->flags |= TX_CACHE_IGNORE;
7238}
7239
7240/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007241 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007243void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007244{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007245 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007246 char *p1, *p2;
7247
7248 char *cur_ptr, *cur_end, *cur_next;
7249 int cur_idx;
7250
Willy Tarreau12b32f22017-12-21 16:08:09 +01007251 if (txn->status < 200) {
7252 /* do not try to cache interim responses! */
7253 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007254 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007255 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007256
7257 /* Iterate through the headers.
7258 * we start with the start line.
7259 */
7260 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007261 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007262
7263 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7264 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007265 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007266
7267 cur_hdr = &txn->hdr_idx.v[cur_idx];
7268 cur_ptr = cur_next;
7269 cur_end = cur_ptr + cur_hdr->len;
7270 cur_next = cur_end + cur_hdr->cr + 1;
7271
7272 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007273 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007274 */
7275
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007276 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7277 if (val) {
7278 if ((cur_end - (cur_ptr + val) >= 8) &&
7279 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7280 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7281 return;
7282 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007283 }
7284
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007285 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7286 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007287 continue;
7288
7289 /* OK, right now we know we have a cache-control header at cur_ptr */
7290
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007291 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007292
7293 if (p1 >= cur_end) /* no more info */
7294 continue;
7295
7296 /* p1 is at the beginning of the value */
7297 p2 = p1;
7298
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007299 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007300 p2++;
7301
7302 /* we have a complete value between p1 and p2 */
7303 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007304 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7305 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7306 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7307 continue;
7308 }
7309
Willy Tarreaua15645d2007-03-18 16:22:39 +01007310 /* we have something of the form no-cache="set-cookie" */
7311 if ((cur_end - p1 >= 21) &&
7312 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7313 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007314 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007315 continue;
7316 }
7317
7318 /* OK, so we know that either p2 points to the end of string or to a comma */
7319 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007320 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007321 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007322 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007323 return;
7324 }
7325
7326 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007327 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007328 continue;
7329 }
7330 }
7331}
7332
Willy Tarreau58f10d72006-12-04 02:26:12 +01007333
Willy Tarreaub2513902006-12-17 14:52:38 +01007334/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007335 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007336 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007337 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007338 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007339 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007340 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007341 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007342 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007343int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007344{
7345 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007346 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007347 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007348
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007349 if (!uri_auth)
7350 return 0;
7351
Cyril Bonté70be45d2010-10-12 00:14:35 +02007352 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007353 return 0;
7354
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007355 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007356 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007357 return 0;
7358
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007359 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007360 return 0;
7361
Willy Tarreaub2513902006-12-17 14:52:38 +01007362 return 1;
7363}
7364
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007365/* Append the description of what is present in error snapshot <es> into <out>.
7366 * The description must be small enough to always fit in a trash. The output
7367 * buffer may be the trash so the trash must not be used inside this function.
7368 */
7369void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7370{
7371 chunk_appendf(&trash,
7372 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7373 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7374 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7375 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7376 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7377 es->ctx.http.m_flags, es->ctx.http.m_clen,
7378 es->ctx.http.m_blen, es->ctx.http.b_flags);
7379}
7380
Willy Tarreau4076a152009-04-02 15:18:36 +02007381/*
7382 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007383 * By default it tries to report the error position as msg->err_pos. However if
7384 * this one is not set, it will then report msg->next, which is the last known
7385 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007386 * displays buffers as a contiguous area starting at buf->p. The direction is
7387 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007388 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007389void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007390 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007391 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007392{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007393 union error_snapshot_ctx ctx;
7394 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007395
Willy Tarreauef3ca732018-09-07 15:47:35 +02007396 /* http-specific part now */
7397 ctx.http.sid = s->uniq_id;
7398 ctx.http.state = state;
7399 ctx.http.b_flags = msg->chn->flags;
7400 ctx.http.s_flags = s->flags;
7401 ctx.http.t_flags = s->txn->flags;
7402 ctx.http.m_flags = msg->flags;
7403 ctx.http.m_clen = msg->chunk_len;
7404 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007405
Willy Tarreauef3ca732018-09-07 15:47:35 +02007406 ofs = msg->chn->total - ci_data(msg->chn);
7407 if (ofs < 0)
7408 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007409
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007410 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007411 other_end, s->target,
7412 strm_sess(s), &msg->chn->buf,
7413 ofs, co_data(msg->chn),
7414 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7415 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007416}
Willy Tarreaub2513902006-12-17 14:52:38 +01007417
Willy Tarreau294c4732011-12-16 21:35:50 +01007418/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7419 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7420 * performed over the whole headers. Otherwise it must contain a valid header
7421 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7422 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7423 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7424 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007425 * -1. The value fetch stops at commas, so this function is suited for use with
7426 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007427 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007428 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007429unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007430 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007431 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007432{
Willy Tarreau294c4732011-12-16 21:35:50 +01007433 struct hdr_ctx local_ctx;
7434 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007435 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007436 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007437 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007438
Willy Tarreau294c4732011-12-16 21:35:50 +01007439 if (!ctx) {
7440 local_ctx.idx = 0;
7441 ctx = &local_ctx;
7442 }
7443
Willy Tarreaubce70882009-09-07 11:51:47 +02007444 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007445 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007446 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007447 occ--;
7448 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007449 *vptr = ctx->line + ctx->val;
7450 *vlen = ctx->vlen;
7451 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007452 }
7453 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007454 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007455 }
7456
7457 /* negative occurrence, we scan all the list then walk back */
7458 if (-occ > MAX_HDR_HISTORY)
7459 return 0;
7460
Willy Tarreau294c4732011-12-16 21:35:50 +01007461 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007462 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007463 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7464 len_hist[hist_ptr] = ctx->vlen;
7465 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007466 hist_ptr = 0;
7467 found++;
7468 }
7469 if (-occ > found)
7470 return 0;
7471 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007472 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7473 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7474 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007475 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007476 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007477 if (hist_ptr >= MAX_HDR_HISTORY)
7478 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007479 *vptr = ptr_hist[hist_ptr];
7480 *vlen = len_hist[hist_ptr];
7481 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007482}
7483
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007484/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7485 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7486 * performed over the whole headers. Otherwise it must contain a valid header
7487 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7488 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7489 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7490 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7491 * -1. This function differs from http_get_hdr() in that it only returns full
7492 * line header values and does not stop at commas.
7493 * The return value is 0 if nothing was found, or non-zero otherwise.
7494 */
7495unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7496 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007497 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007498{
7499 struct hdr_ctx local_ctx;
7500 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007501 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007502 unsigned int hist_ptr;
7503 int found;
7504
7505 if (!ctx) {
7506 local_ctx.idx = 0;
7507 ctx = &local_ctx;
7508 }
7509
7510 if (occ >= 0) {
7511 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007512 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007513 occ--;
7514 if (occ <= 0) {
7515 *vptr = ctx->line + ctx->val;
7516 *vlen = ctx->vlen;
7517 return 1;
7518 }
7519 }
7520 return 0;
7521 }
7522
7523 /* negative occurrence, we scan all the list then walk back */
7524 if (-occ > MAX_HDR_HISTORY)
7525 return 0;
7526
7527 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007528 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007529 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7530 len_hist[hist_ptr] = ctx->vlen;
7531 if (++hist_ptr >= MAX_HDR_HISTORY)
7532 hist_ptr = 0;
7533 found++;
7534 }
7535 if (-occ > found)
7536 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007537
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007538 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007539 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7540 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7541 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007542 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007543 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007544 if (hist_ptr >= MAX_HDR_HISTORY)
7545 hist_ptr -= MAX_HDR_HISTORY;
7546 *vptr = ptr_hist[hist_ptr];
7547 *vlen = len_hist[hist_ptr];
7548 return 1;
7549}
7550
Willy Tarreaubaaee002006-06-26 02:48:02 +02007551/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007552 * Print a debug line with a header. Always stop at the first CR or LF char,
7553 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7554 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007555 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007556void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007557{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007558 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007559 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007560
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007561 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007562 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007563 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007564 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 +02007565
7566 for (max = 0; start + max < end; max++)
7567 if (start[max] == '\r' || start[max] == '\n')
7568 break;
7569
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007570 UBOUND(max, trash.size - trash.data - 3);
7571 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7572 trash.area[trash.data++] = '\n';
7573 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007574}
7575
Willy Tarreaueee5b512015-04-03 23:46:31 +02007576
7577/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7578 * The hdr_idx is allocated as well. In case of allocation failure, everything
7579 * allocated is freed and NULL is returned. Otherwise the new transaction is
7580 * assigned to the stream and returned.
7581 */
7582struct http_txn *http_alloc_txn(struct stream *s)
7583{
7584 struct http_txn *txn = s->txn;
7585
7586 if (txn)
7587 return txn;
7588
Willy Tarreaubafbe012017-11-24 17:34:44 +01007589 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007590 if (!txn)
7591 return txn;
7592
7593 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007594 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007595 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007596 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007597 return NULL;
7598 }
7599
7600 s->txn = txn;
7601 return txn;
7602}
7603
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007604void http_txn_reset_req(struct http_txn *txn)
7605{
7606 txn->req.flags = 0;
7607 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7608 txn->req.next = 0;
7609 txn->req.chunk_len = 0LL;
7610 txn->req.body_len = 0LL;
7611 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7612}
7613
7614void http_txn_reset_res(struct http_txn *txn)
7615{
7616 txn->rsp.flags = 0;
7617 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7618 txn->rsp.next = 0;
7619 txn->rsp.chunk_len = 0LL;
7620 txn->rsp.body_len = 0LL;
7621 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7622}
7623
Willy Tarreau0937bc42009-12-22 15:03:09 +01007624/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007625 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007626 * the required fields are properly allocated and that we only need to (re)init
7627 * them. This should be used before processing any new request.
7628 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007629void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007630{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007631 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007632 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007633
7634 txn->flags = 0;
7635 txn->status = -1;
7636
Willy Tarreauf64d1412010-10-07 20:06:11 +02007637 txn->cookie_first_date = 0;
7638 txn->cookie_last_date = 0;
7639
Willy Tarreaueee5b512015-04-03 23:46:31 +02007640 txn->srv_cookie = NULL;
7641 txn->cli_cookie = NULL;
7642 txn->uri = NULL;
7643
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007644 http_txn_reset_req(txn);
7645 http_txn_reset_res(txn);
7646
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007647 txn->req.chn = &s->req;
7648 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007649
7650 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007651
7652 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7653 if (fe->options2 & PR_O2_REQBUG_OK)
7654 txn->req.err_pos = -1; /* let buggy requests pass */
7655
Willy Tarreau0937bc42009-12-22 15:03:09 +01007656 if (txn->hdr_idx.v)
7657 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007658
7659 vars_init(&s->vars_txn, SCOPE_TXN);
7660 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007661}
7662
7663/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007664void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007665{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007666 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007667 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007668
7669 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007670 pool_free(pool_head_requri, txn->uri);
7671 pool_free(pool_head_capture, txn->cli_cookie);
7672 pool_free(pool_head_capture, txn->srv_cookie);
7673 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007674
William Lallemanda73203e2012-03-12 12:48:57 +01007675 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007676 txn->uri = NULL;
7677 txn->srv_cookie = NULL;
7678 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007679
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007680 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007681 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007682 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007683 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007684 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007685 }
7686
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007687 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007688 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007689 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007690 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007691 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007692 }
7693
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007694 if (!LIST_ISEMPTY(&s->vars_txn.head))
7695 vars_prune(&s->vars_txn, s->sess, s);
7696 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7697 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007698}
7699
7700/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007701void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007702{
7703 http_end_txn(s);
7704 http_init_txn(s);
7705
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007706 /* reinitialise the current rule list pointer to NULL. We are sure that
7707 * any rulelist match the NULL pointer.
7708 */
7709 s->current_rule_list = NULL;
7710
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007711 s->be = strm_fe(s);
7712 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007713 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007714 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007715 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007716 /* re-init store persistence */
7717 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007718 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007719
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007720 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007721
Willy Tarreau739cfba2010-01-25 23:11:14 +01007722 /* We must trim any excess data from the response buffer, because we
7723 * may have blocked an invalid response from a server that we don't
7724 * want to accidentely forward once we disable the analysers, nor do
7725 * we want those data to come along with next response. A typical
7726 * example of such data would be from a buggy server responding to
7727 * a HEAD with some data, or sending more than the advertised
7728 * content-length.
7729 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007730 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007731 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007732
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007733 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007734 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007735
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007736 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007737 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007738
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007739 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007740 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007741
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007742 s->req.rex = TICK_ETERNITY;
7743 s->req.wex = TICK_ETERNITY;
7744 s->req.analyse_exp = TICK_ETERNITY;
7745 s->res.rex = TICK_ETERNITY;
7746 s->res.wex = TICK_ETERNITY;
7747 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007748 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007749}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007750
Willy Tarreau79e57332018-10-02 16:01:16 +02007751/* This function executes one of the set-{method,path,query,uri} actions. It
7752 * takes the string from the variable 'replace' with length 'len', then modifies
7753 * the relevant part of the request line accordingly. Then it updates various
7754 * pointers to the next elements which were moved, and the total buffer length.
7755 * It finds the action to be performed in p[2], previously filled by function
7756 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7757 * error, though this can be revisited when this code is finally exploited.
7758 *
7759 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7760 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007761 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007762 * In query string case, the mark question '?' must be set at the start of the
7763 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007764 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007765int http_replace_req_line(int action, const char *replace, int len,
7766 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007767{
Willy Tarreau79e57332018-10-02 16:01:16 +02007768 struct http_txn *txn = s->txn;
7769 char *cur_ptr, *cur_end;
7770 int offset = 0;
7771 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007772
Willy Tarreau79e57332018-10-02 16:01:16 +02007773 switch (action) {
7774 case 0: // method
7775 cur_ptr = ci_head(&s->req);
7776 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007777
Willy Tarreau79e57332018-10-02 16:01:16 +02007778 /* adjust req line offsets and lengths */
7779 delta = len - offset - (cur_end - cur_ptr);
7780 txn->req.sl.rq.m_l += delta;
7781 txn->req.sl.rq.u += delta;
7782 txn->req.sl.rq.v += delta;
7783 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007784
Willy Tarreau79e57332018-10-02 16:01:16 +02007785 case 1: // path
7786 cur_ptr = http_txn_get_path(txn);
7787 if (!cur_ptr)
7788 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007789
Willy Tarreau79e57332018-10-02 16:01:16 +02007790 cur_end = cur_ptr;
7791 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7792 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007793
Willy Tarreau79e57332018-10-02 16:01:16 +02007794 /* adjust req line offsets and lengths */
7795 delta = len - offset - (cur_end - cur_ptr);
7796 txn->req.sl.rq.u_l += delta;
7797 txn->req.sl.rq.v += delta;
7798 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007799
Willy Tarreau79e57332018-10-02 16:01:16 +02007800 case 2: // query
7801 offset = 1;
7802 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7803 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7804 while (cur_ptr < cur_end && *cur_ptr != '?')
7805 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007806
Willy Tarreau79e57332018-10-02 16:01:16 +02007807 /* skip the question mark or indicate that we must insert it
7808 * (but only if the format string is not empty then).
7809 */
7810 if (cur_ptr < cur_end)
7811 cur_ptr++;
7812 else if (len > 1)
7813 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007814
Willy Tarreau79e57332018-10-02 16:01:16 +02007815 /* adjust req line offsets and lengths */
7816 delta = len - offset - (cur_end - cur_ptr);
7817 txn->req.sl.rq.u_l += delta;
7818 txn->req.sl.rq.v += delta;
7819 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007820
Willy Tarreau79e57332018-10-02 16:01:16 +02007821 case 3: // uri
7822 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7823 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007824
Willy Tarreau79e57332018-10-02 16:01:16 +02007825 /* adjust req line offsets and lengths */
7826 delta = len - offset - (cur_end - cur_ptr);
7827 txn->req.sl.rq.u_l += delta;
7828 txn->req.sl.rq.v += delta;
7829 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007830
Willy Tarreau79e57332018-10-02 16:01:16 +02007831 default:
7832 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007833 }
7834
Willy Tarreau79e57332018-10-02 16:01:16 +02007835 /* commit changes and adjust end of message */
7836 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7837 txn->req.sl.rq.l += delta;
7838 txn->hdr_idx.v[0].len += delta;
7839 http_msg_move_end(&txn->req, delta);
7840 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007841}
7842
Willy Tarreau79e57332018-10-02 16:01:16 +02007843/* This function replace the HTTP status code and the associated message. The
7844 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007845 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007846void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007847{
Willy Tarreau79e57332018-10-02 16:01:16 +02007848 struct http_txn *txn = s->txn;
7849 char *cur_ptr, *cur_end;
7850 int delta;
7851 char *res;
7852 int c_l;
7853 const char *msg = reason;
7854 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007855
Willy Tarreau79e57332018-10-02 16:01:16 +02007856 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007857
Willy Tarreau79e57332018-10-02 16:01:16 +02007858 res = ultoa_o(status, trash.area, trash.size);
7859 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007860
Willy Tarreau79e57332018-10-02 16:01:16 +02007861 trash.area[c_l] = ' ';
7862 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007863
Willy Tarreau79e57332018-10-02 16:01:16 +02007864 /* Do we have a custom reason format string? */
7865 if (msg == NULL)
7866 msg = http_get_reason(status);
7867 msg_len = strlen(msg);
7868 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7869 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007870
Willy Tarreau79e57332018-10-02 16:01:16 +02007871 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7872 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007873
Willy Tarreau79e57332018-10-02 16:01:16 +02007874 /* commit changes and adjust message */
7875 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7876 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007877
Willy Tarreau79e57332018-10-02 16:01:16 +02007878 /* adjust res line offsets and lengths */
7879 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7880 txn->rsp.sl.st.c_l = c_l;
7881 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007882
Willy Tarreau79e57332018-10-02 16:01:16 +02007883 delta = trash.data - (cur_end - cur_ptr);
7884 txn->rsp.sl.st.l += delta;
7885 txn->hdr_idx.v[0].len += delta;
7886 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007887}
7888
Willy Tarreau8797c062007-05-07 00:55:35 +02007889__attribute__((constructor))
7890static void __http_protocol_init(void)
7891{
Willy Tarreau8797c062007-05-07 00:55:35 +02007892}
7893
7894
Willy Tarreau58f10d72006-12-04 02:26:12 +01007895/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007896 * Local variables:
7897 * c-indent-level: 8
7898 * c-basic-offset: 8
7899 * End:
7900 */