blob: c5d35437f2bd03780d5d2bd18c9898679d991f9d [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.
1725 * If <early_hint> is false the HTTP 103 response first line is inserted before
1726 * the header.
1727 */
1728static int http_apply_early_hint_rule(struct stream* s, struct channel *resp, int early_hint,
1729 const char* name, unsigned int name_len, struct list *fmt)
1730{
1731 int ret;
1732 size_t data;
1733 struct buffer *chunk;
1734 char *cur_ptr = ci_head(resp);
1735
1736 ret = 0;
1737 data = co_data(resp);
1738
1739 chunk = alloc_trash_chunk();
1740 if (!chunk)
1741 goto leave;
1742
1743 if (!early_hint && !chunk_memcat(chunk, HTTP_103.ptr, HTTP_103.len))
1744 goto leave;
1745
1746 if (!chunk_memcat(chunk, name, name_len) || !chunk_memcat(chunk, ": ", 2))
1747 goto leave;
1748
1749 chunk->data += build_logline(s, chunk->area + chunk->data, chunk->size - chunk->data, fmt);
1750 if (!chunk_memcat(chunk, "\r\n", 2))
1751 goto leave;
1752
1753 ret = b_rep_blk(&resp->buf, cur_ptr, cur_ptr, chunk->area, chunk->data);
1754 c_adv(resp, ret);
1755
1756 leave:
1757 if (!ret)
1758 co_set_data(resp, data);
1759 free_trash_chunk(chunk);
1760
1761 return ret;
1762}
1763
Willy Tarreau87b09662015-04-03 00:22:06 +02001764/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001765 * transaction <txn>. Returns the verdict of the first rule that prevents
1766 * further processing of the request (auth, deny, ...), and defaults to
1767 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1768 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001769 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1770 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1771 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001772 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001773enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001774http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001775{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001776 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001777 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001778 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001779 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001780 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001781 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02001782 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001783 int len;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001784 int early_hint = 0;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001785
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001786 /* If "the current_rule_list" match the executed rule list, we are in
1787 * resume condition. If a resume is needed it is always in the action
1788 * and never in the ACL or converters. In this case, we initialise the
1789 * current rule, and go to the action execution point.
1790 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001791 if (s->current_rule) {
1792 rule = s->current_rule;
1793 s->current_rule = NULL;
1794 if (s->current_rule_list == rules)
1795 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001796 }
1797 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001798
Willy Tarreauff011f22011-01-06 17:51:27 +01001799 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001800
Willy Tarreau96257ec2012-12-27 10:46:37 +01001801 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001802 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001803 int ret;
1804
Willy Tarreau192252e2015-04-04 01:47:55 +02001805 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001806 ret = acl_pass(ret);
1807
Willy Tarreauff011f22011-01-06 17:51:27 +01001808 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001809 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001810
1811 if (!ret) /* condition not matched */
1812 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001813 }
1814
Willy Tarreauacc98002015-09-27 23:34:39 +02001815 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001816resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001817 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001818 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02001819 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001820
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001821 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001822 if (deny_status)
1823 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001824 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001825
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001826 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001827 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001828 if (deny_status)
1829 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001830 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001831
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001832 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02001833 /* Auth might be performed on regular http-req rules as well as on stats */
1834 auth_realm = rule->arg.auth.realm;
1835 if (!auth_realm) {
1836 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1837 auth_realm = STATS_DEFAULT_REALM;
1838 else
1839 auth_realm = px->id;
1840 }
1841 /* send 401/407 depending on whether we use a proxy or not. We still
1842 * count one error, because normal browsing won't significantly
1843 * increase the counter but brute force attempts will.
1844 */
1845 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1846 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001847 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001848 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02001849 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001850
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001851 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02001852 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
1853 return HTTP_RULE_RES_BADREQ;
1854 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001855
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001856 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001857 s->task->nice = rule->arg.nice;
1858 break;
1859
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001860 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001861 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001862 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001863 break;
1864
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001865 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001866#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001867 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001868 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001869#endif
1870 break;
1871
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001872 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001873 s->logs.level = rule->arg.loglevel;
1874 break;
1875
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001876 case ACT_HTTP_REPLACE_HDR:
1877 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001878 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1879 rule->arg.hdr_add.name_len,
1880 &rule->arg.hdr_add.fmt,
1881 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06001882 return HTTP_RULE_RES_BADREQ;
1883 break;
1884
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001885 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001886 ctx.idx = 0;
1887 /* remove all occurrences of the header */
1888 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001889 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001890 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001891 }
Willy Tarreau85603282015-01-21 20:39:27 +01001892 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001893
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001894 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001895 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001896 /* The scope of the trash buffer must be limited to this function. The
1897 * build_logline() function can execute a lot of other function which
1898 * can use the trash buffer. So for limiting the scope of this global
1899 * buffer, we build first the header value using build_logline, and
1900 * after we store the header name.
1901 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001902 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001903
1904 replace = alloc_trash_chunk();
1905 if (!replace)
1906 return HTTP_RULE_RES_BADREQ;
1907
Thierry Fournier4b788f72016-06-01 13:35:36 +02001908 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001909 len += build_logline(s, replace->area + len,
1910 replace->size - len,
1911 &rule->arg.hdr_add.fmt);
1912 memcpy(replace->area, rule->arg.hdr_add.name,
1913 rule->arg.hdr_add.name_len);
1914 replace->area[rule->arg.hdr_add.name_len] = ':';
1915 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1916 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001917
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001918 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001919 /* remove all occurrences of the header */
1920 ctx.idx = 0;
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 Tarreau85603282015-01-21 20:39:27 +01001923 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1924 }
1925 }
1926
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001927 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001928 static unsigned char rate_limit = 0;
1929
1930 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001931 replace->area[rule->arg.hdr_add.name_len] = 0;
1932 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,
1933 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001934 }
1935
1936 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1937 if (sess->fe != s->be)
1938 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1939 if (sess->listener->counters)
1940 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1941 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001942
1943 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001944 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001945 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001946
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001947 case ACT_HTTP_DEL_ACL:
1948 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001949 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001950 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001951
1952 /* collect reference */
1953 ref = pat_ref_lookup(rule->arg.map.ref);
1954 if (!ref)
1955 continue;
1956
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001957 /* allocate key */
1958 key = alloc_trash_chunk();
1959 if (!key)
1960 return HTTP_RULE_RES_BADREQ;
1961
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001962 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001963 key->data = build_logline(s, key->area, key->size,
1964 &rule->arg.map.key);
1965 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001966
1967 /* perform update */
1968 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001969 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001970 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001971 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001972
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001973 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001974 break;
1975 }
1976
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001977 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001978 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001979 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001980
1981 /* collect reference */
1982 ref = pat_ref_lookup(rule->arg.map.ref);
1983 if (!ref)
1984 continue;
1985
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001986 /* allocate key */
1987 key = alloc_trash_chunk();
1988 if (!key)
1989 return HTTP_RULE_RES_BADREQ;
1990
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001991 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001992 key->data = build_logline(s, key->area, key->size,
1993 &rule->arg.map.key);
1994 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001995
1996 /* perform update */
1997 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001998 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001999 if (pat_ref_find_elt(ref, key->area) == NULL)
2000 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002001 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002002
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002003 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002004 break;
2005 }
2006
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002007 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002008 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002009 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002010
2011 /* collect reference */
2012 ref = pat_ref_lookup(rule->arg.map.ref);
2013 if (!ref)
2014 continue;
2015
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002016 /* allocate key */
2017 key = alloc_trash_chunk();
2018 if (!key)
2019 return HTTP_RULE_RES_BADREQ;
2020
2021 /* allocate value */
2022 value = alloc_trash_chunk();
2023 if (!value) {
2024 free_trash_chunk(key);
2025 return HTTP_RULE_RES_BADREQ;
2026 }
2027
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002028 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002029 key->data = build_logline(s, key->area, key->size,
2030 &rule->arg.map.key);
2031 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002032
2033 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002034 value->data = build_logline(s, value->area,
2035 value->size,
2036 &rule->arg.map.value);
2037 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002038
2039 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002040 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002041 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002042 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002043 else
2044 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002045 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002046
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002047 free_trash_chunk(key);
2048 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002049 break;
2050 }
William Lallemand73025dd2014-04-24 14:38:37 +02002051
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002052 case ACT_HTTP_EARLY_HINT:
2053 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2054 break;
2055
2056 if (!http_apply_early_hint_rule(s, txn->rsp.chn, early_hint,
2057 rule->arg.early_hint.name,
2058 rule->arg.early_hint.name_len,
2059 &rule->arg.early_hint.fmt))
2060 return HTTP_RULE_RES_DONE;
2061 early_hint = 1;
2062 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002063 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002064 if ((s->req.flags & CF_READ_ERROR) ||
2065 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2066 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2067 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002068 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002069
Willy Tarreauacc98002015-09-27 23:34:39 +02002070 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002071 case ACT_RET_ERR:
2072 case ACT_RET_CONT:
2073 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002074 case ACT_RET_STOP:
2075 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002076 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002077 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002078 return HTTP_RULE_RES_YIELD;
2079 }
William Lallemand73025dd2014-04-24 14:38:37 +02002080 break;
2081
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002082 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002083 /* Note: only the first valid tracking parameter of each
2084 * applies.
2085 */
2086
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002087 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002088 struct stktable *t;
2089 struct stksess *ts;
2090 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002091 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002092
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002093 t = rule->arg.trk_ctr.table.t;
2094 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 +02002095
2096 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002097 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002098
2099 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002100 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2101 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2102 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002103 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002104
Emeric Brun819fc6f2017-06-13 19:37:32 +02002105 if (ptr1)
2106 stktable_data_cast(ptr1, http_req_cnt)++;
2107
2108 if (ptr2)
2109 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2110 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2111
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002112 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002113
2114 /* If data was modified, we need to touch to re-schedule sync */
2115 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002116 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002117
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002118 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002119 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002120 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002121 }
2122 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002123 break;
2124
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002125 /* other flags exists, but normaly, they never be matched. */
2126 default:
2127 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002128 }
2129 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002130
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002131 if (early_hint) {
2132 struct channel *chn = s->txn->rsp.chn;
2133 char *cur_ptr = ci_head(chn);
2134
2135 /* Add an empty line after Early Hint informational response headers */
2136 b_rep_blk(&chn->buf, cur_ptr, cur_ptr, "\r\n", 2);
2137 c_adv(chn, 2);
2138 }
2139
Willy Tarreau96257ec2012-12-27 10:46:37 +01002140 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002141 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002142}
2143
Willy Tarreau71241ab2012-12-27 11:30:54 +01002144
Willy Tarreau51d861a2015-05-22 17:30:48 +02002145/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2146 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2147 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2148 * is returned, the process can continue the evaluation of next rule list. If
2149 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2150 * is returned, it means the operation could not be processed and a server error
2151 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2152 * deny rule. If *YIELD is returned, the caller must call again the function
2153 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002154 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002155enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002156http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002157{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002158 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002159 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002160 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002161 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002162 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002163 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002164
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002165 /* If "the current_rule_list" match the executed rule list, we are in
2166 * resume condition. If a resume is needed it is always in the action
2167 * and never in the ACL or converters. In this case, we initialise the
2168 * current rule, and go to the action execution point.
2169 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002170 if (s->current_rule) {
2171 rule = s->current_rule;
2172 s->current_rule = NULL;
2173 if (s->current_rule_list == rules)
2174 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002175 }
2176 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002177
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002178 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002179
2180 /* check optional condition */
2181 if (rule->cond) {
2182 int ret;
2183
Willy Tarreau192252e2015-04-04 01:47:55 +02002184 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002185 ret = acl_pass(ret);
2186
2187 if (rule->cond->pol == ACL_COND_UNLESS)
2188 ret = !ret;
2189
2190 if (!ret) /* condition not matched */
2191 continue;
2192 }
2193
Willy Tarreauacc98002015-09-27 23:34:39 +02002194 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002195resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002196 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002197 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002198 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002199
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002200 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002201 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002202 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002203
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002204 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002205 s->task->nice = rule->arg.nice;
2206 break;
2207
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002208 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002209 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002210 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002211 break;
2212
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002213 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002214#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002215 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002216 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002217#endif
2218 break;
2219
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002220 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002221 s->logs.level = rule->arg.loglevel;
2222 break;
2223
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002224 case ACT_HTTP_REPLACE_HDR:
2225 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002226 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2227 rule->arg.hdr_add.name_len,
2228 &rule->arg.hdr_add.fmt,
2229 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002230 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002231 break;
2232
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002233 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002234 ctx.idx = 0;
2235 /* remove all occurrences of the header */
2236 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002237 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002238 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2239 }
Willy Tarreau85603282015-01-21 20:39:27 +01002240 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002241
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002242 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002243 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002244 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002245
2246 replace = alloc_trash_chunk();
2247 if (!replace)
2248 return HTTP_RULE_RES_BADREQ;
2249
2250 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002251 memcpy(replace->area, rule->arg.hdr_add.name,
2252 rule->arg.hdr_add.name_len);
2253 replace->data = rule->arg.hdr_add.name_len;
2254 replace->area[replace->data++] = ':';
2255 replace->area[replace->data++] = ' ';
2256 replace->data += build_logline(s,
2257 replace->area + replace->data,
2258 replace->size - replace->data,
2259 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002260
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002261 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002262 /* remove all occurrences of the header */
2263 ctx.idx = 0;
2264 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002265 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002266 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2267 }
2268 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002269
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002270 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002271 static unsigned char rate_limit = 0;
2272
2273 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002274 replace->area[rule->arg.hdr_add.name_len] = 0;
2275 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,
2276 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002277 }
2278
2279 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2280 if (sess->fe != s->be)
2281 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2282 if (sess->listener->counters)
2283 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2284 if (objt_server(s->target))
2285 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2286 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002287
2288 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002289 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002290 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002291
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002292 case ACT_HTTP_DEL_ACL:
2293 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002294 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002295 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002296
2297 /* collect reference */
2298 ref = pat_ref_lookup(rule->arg.map.ref);
2299 if (!ref)
2300 continue;
2301
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002302 /* allocate key */
2303 key = alloc_trash_chunk();
2304 if (!key)
2305 return HTTP_RULE_RES_BADREQ;
2306
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002307 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002308 key->data = build_logline(s, key->area, key->size,
2309 &rule->arg.map.key);
2310 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002311
2312 /* perform update */
2313 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002314 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002315 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002316 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002317
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002318 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002319 break;
2320 }
2321
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002322 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002323 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002324 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002325
2326 /* collect reference */
2327 ref = pat_ref_lookup(rule->arg.map.ref);
2328 if (!ref)
2329 continue;
2330
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002331 /* allocate key */
2332 key = alloc_trash_chunk();
2333 if (!key)
2334 return HTTP_RULE_RES_BADREQ;
2335
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002336 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002337 key->data = build_logline(s, key->area, key->size,
2338 &rule->arg.map.key);
2339 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002340
2341 /* perform update */
2342 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002343 if (pat_ref_find_elt(ref, key->area) == NULL)
2344 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002345
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002346 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002347 break;
2348 }
2349
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002350 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002351 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002352 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002353
2354 /* collect reference */
2355 ref = pat_ref_lookup(rule->arg.map.ref);
2356 if (!ref)
2357 continue;
2358
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002359 /* allocate key */
2360 key = alloc_trash_chunk();
2361 if (!key)
2362 return HTTP_RULE_RES_BADREQ;
2363
2364 /* allocate value */
2365 value = alloc_trash_chunk();
2366 if (!value) {
2367 free_trash_chunk(key);
2368 return HTTP_RULE_RES_BADREQ;
2369 }
2370
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002371 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002372 key->data = build_logline(s, key->area, key->size,
2373 &rule->arg.map.key);
2374 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002375
2376 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002377 value->data = build_logline(s, value->area,
2378 value->size,
2379 &rule->arg.map.value);
2380 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002381
2382 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002383 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002384 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002385 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002386 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002387 else
2388 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002389 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002390 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002391 free_trash_chunk(key);
2392 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002393 break;
2394 }
William Lallemand73025dd2014-04-24 14:38:37 +02002395
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002396 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02002397 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2398 return HTTP_RULE_RES_BADREQ;
2399 return HTTP_RULE_RES_DONE;
2400
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002401 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2402 /* Note: only the first valid tracking parameter of each
2403 * applies.
2404 */
2405
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002406 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002407 struct stktable *t;
2408 struct stksess *ts;
2409 struct stktable_key *key;
2410 void *ptr;
2411
2412 t = rule->arg.trk_ctr.table.t;
2413 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2414
2415 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002416 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002417
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002418 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002419
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002420 /* let's count a new HTTP request as it's the first time we do it */
2421 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2422 if (ptr)
2423 stktable_data_cast(ptr, http_req_cnt)++;
2424
2425 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2426 if (ptr)
2427 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2428 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2429
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002430 /* When the client triggers a 4xx from the server, it's most often due
2431 * to a missing object or permission. These events should be tracked
2432 * because if they happen often, it may indicate a brute force or a
2433 * vulnerability scan. Normally this is done when receiving the response
2434 * but here we're tracking after this ought to have been done so we have
2435 * to do it on purpose.
2436 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002437 if ((unsigned)(txn->status - 400) < 100) {
2438 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2439 if (ptr)
2440 stktable_data_cast(ptr, http_err_cnt)++;
2441
2442 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2443 if (ptr)
2444 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2445 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2446 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002447
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002448 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002449
Emeric Brun0fed0b02017-11-29 16:15:07 +01002450 /* If data was modified, we need to touch to re-schedule sync */
2451 stktable_touch_local(t, ts, 0);
2452
Emeric Brun819fc6f2017-06-13 19:37:32 +02002453 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2454 if (sess->fe != s->be)
2455 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2456
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002457 }
2458 }
2459 break;
2460
Thierry FOURNIER42148732015-09-02 17:17:33 +02002461 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002462 if ((s->req.flags & CF_READ_ERROR) ||
2463 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2464 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2465 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002466 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002467
Willy Tarreauacc98002015-09-27 23:34:39 +02002468 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002469 case ACT_RET_ERR:
2470 case ACT_RET_CONT:
2471 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002472 case ACT_RET_STOP:
2473 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002474 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002475 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002476 return HTTP_RULE_RES_YIELD;
2477 }
William Lallemand73025dd2014-04-24 14:38:37 +02002478 break;
2479
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002480 /* other flags exists, but normaly, they never be matched. */
2481 default:
2482 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002483 }
2484 }
2485
2486 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002487 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002488}
2489
2490
Willy Tarreau71241ab2012-12-27 11:30:54 +01002491/* Perform an HTTP redirect based on the information in <rule>. The function
2492 * returns non-zero on success, or zero in case of a, irrecoverable error such
2493 * as too large a request to build a valid response.
2494 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002495int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002496{
Willy Tarreaub329a312015-05-22 16:27:37 +02002497 struct http_msg *req = &txn->req;
2498 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002499 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002500 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002501 int ret = 0;
2502
2503 chunk = alloc_trash_chunk();
2504 if (!chunk)
2505 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002506
2507 /* build redirect message */
2508 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002509 case 308:
2510 msg_fmt = HTTP_308;
2511 break;
2512 case 307:
2513 msg_fmt = HTTP_307;
2514 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002515 case 303:
2516 msg_fmt = HTTP_303;
2517 break;
2518 case 301:
2519 msg_fmt = HTTP_301;
2520 break;
2521 case 302:
2522 default:
2523 msg_fmt = HTTP_302;
2524 break;
2525 }
2526
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002527 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2528 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002529
2530 switch(rule->type) {
2531 case REDIRECT_TYPE_SCHEME: {
2532 const char *path;
2533 const char *host;
2534 struct hdr_ctx ctx;
2535 int pathlen;
2536 int hostlen;
2537
2538 host = "";
2539 hostlen = 0;
2540 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002541 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002542 host = ctx.line + ctx.val;
2543 hostlen = ctx.vlen;
2544 }
2545
Willy Tarreau6b952c82018-09-10 17:45:34 +02002546 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002547 /* build message using path */
2548 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002549 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002550 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2551 int qs = 0;
2552 while (qs < pathlen) {
2553 if (path[qs] == '?') {
2554 pathlen = qs;
2555 break;
2556 }
2557 qs++;
2558 }
2559 }
2560 } else {
2561 path = "/";
2562 pathlen = 1;
2563 }
2564
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002565 if (rule->rdr_str) { /* this is an old "redirect" rule */
2566 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002567 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002568 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002569
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002570 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002571 memcpy(chunk->area + chunk->data, rule->rdr_str,
2572 rule->rdr_len);
2573 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002574 }
2575 else {
2576 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002577 chunk->data += build_logline(s,
2578 chunk->area + chunk->data,
2579 chunk->size - chunk->data,
2580 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002581
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002582 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002583 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002584 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002585 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002586 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002587 memcpy(chunk->area + chunk->data, "://", 3);
2588 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002589
2590 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002591 memcpy(chunk->area + chunk->data, host, hostlen);
2592 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002593
2594 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002595 memcpy(chunk->area + chunk->data, path, pathlen);
2596 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002597
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002598 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002599 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002600 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002601 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002602 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002603 chunk->area[chunk->data] = '/';
2604 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002605 }
2606
2607 break;
2608 }
2609 case REDIRECT_TYPE_PREFIX: {
2610 const char *path;
2611 int pathlen;
2612
Willy Tarreau6b952c82018-09-10 17:45:34 +02002613 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002614 /* build message using path */
2615 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002616 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002617 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2618 int qs = 0;
2619 while (qs < pathlen) {
2620 if (path[qs] == '?') {
2621 pathlen = qs;
2622 break;
2623 }
2624 qs++;
2625 }
2626 }
2627 } else {
2628 path = "/";
2629 pathlen = 1;
2630 }
2631
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002632 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002633 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002634 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002635
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002636 /* add prefix. Note that if prefix == "/", we don't want to
2637 * add anything, otherwise it makes it hard for the user to
2638 * configure a self-redirection.
2639 */
2640 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002641 memcpy(chunk->area + chunk->data,
2642 rule->rdr_str, rule->rdr_len);
2643 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002644 }
2645 }
2646 else {
2647 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002648 chunk->data += build_logline(s,
2649 chunk->area + chunk->data,
2650 chunk->size - chunk->data,
2651 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002652
2653 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002654 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002655 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002656 }
2657
2658 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002659 memcpy(chunk->area + chunk->data, path, pathlen);
2660 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002661
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002662 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002663 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002664 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002665 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002666 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002667 chunk->area[chunk->data] = '/';
2668 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002669 }
2670
2671 break;
2672 }
2673 case REDIRECT_TYPE_LOCATION:
2674 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002675 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002676 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002677 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002678
2679 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002680 memcpy(chunk->area + chunk->data, rule->rdr_str,
2681 rule->rdr_len);
2682 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002683 }
2684 else {
2685 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002686 chunk->data += build_logline(s,
2687 chunk->area + chunk->data,
2688 chunk->size - chunk->data,
2689 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002690
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002691 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002692 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002693 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002694 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002695 break;
2696 }
2697
2698 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002699 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2700 chunk->data += 14;
2701 memcpy(chunk->area + chunk->data, rule->cookie_str,
2702 rule->cookie_len);
2703 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002704 }
2705
Willy Tarreau19b14122017-02-28 09:48:11 +01002706 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002707 txn->status = rule->code;
2708 /* let's log the request time */
2709 s->logs.tv_request = now;
2710
Christopher Fauletbe821b92017-03-30 11:21:53 +02002711 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002712 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2713 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2714 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002715 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002716 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002717 memcpy(chunk->area + chunk->data,
2718 "\r\nProxy-Connection: keep-alive", 30);
2719 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002720 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002721 memcpy(chunk->area + chunk->data,
2722 "\r\nConnection: keep-alive", 24);
2723 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002724 }
2725 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002726 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2727 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002728 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002729 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002730 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002731 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002732 req->next -= req->sov;
2733 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002734 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002735 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002736 req->msg_state = HTTP_MSG_CLOSED;
2737 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002738 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002739 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002740 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002741 /* let the server side turn to SI_ST_CLO */
2742 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002743 } else {
2744 /* keep-alive not possible */
2745 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002746 memcpy(chunk->area + chunk->data,
2747 "\r\nProxy-Connection: close\r\n\r\n", 29);
2748 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002749 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002750 memcpy(chunk->area + chunk->data,
2751 "\r\nConnection: close\r\n\r\n", 23);
2752 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002753 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002754 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002755 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002756 }
2757
Willy Tarreaue7dff022015-04-03 01:14:29 +02002758 if (!(s->flags & SF_ERR_MASK))
2759 s->flags |= SF_ERR_LOCAL;
2760 if (!(s->flags & SF_FINST_MASK))
2761 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002762
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002763 ret = 1;
2764 leave:
2765 free_trash_chunk(chunk);
2766 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002767}
2768
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002769/* This stream analyser runs all HTTP request processing which is common to
2770 * frontends and backends, which means blocking ACLs, filters, connection-close,
2771 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002772 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002773 * either needs more data or wants to immediately abort the request (eg: deny,
2774 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002775 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002776int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002777{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002778 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002779 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002780 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002781 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002782 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002783 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002784 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002785 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002786
Willy Tarreau655dce92009-11-08 13:10:58 +01002787 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002788 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002789 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002790 }
2791
Christopher Faulet45073512018-07-20 10:16:29 +02002792 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 +02002793 now_ms, __FUNCTION__,
2794 s,
2795 req,
2796 req->rex, req->wex,
2797 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002798 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002799 req->analysers);
2800
Willy Tarreau65410832014-04-28 21:25:43 +02002801 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002802 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002803
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002804 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002805 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002806 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002807
Willy Tarreau0b748332014-04-29 00:13:29 +02002808 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002809 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2810 goto return_prx_yield;
2811
Willy Tarreau0b748332014-04-29 00:13:29 +02002812 case HTTP_RULE_RES_CONT:
2813 case HTTP_RULE_RES_STOP: /* nothing to do */
2814 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002815
Willy Tarreau0b748332014-04-29 00:13:29 +02002816 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2817 if (txn->flags & TX_CLTARPIT)
2818 goto tarpit;
2819 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002820
Willy Tarreau0b748332014-04-29 00:13:29 +02002821 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2822 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002823
Willy Tarreau0b748332014-04-29 00:13:29 +02002824 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002825 goto done;
2826
Willy Tarreau0b748332014-04-29 00:13:29 +02002827 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2828 goto return_bad_req;
2829 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002830 }
2831
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002832 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2833 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002834 struct hdr_ctx ctx;
2835
2836 ctx.idx = 0;
2837 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002838 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002839 if (unlikely(http_header_add_tail2(&txn->req,
2840 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002841 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002842 goto return_bad_req;
2843 }
2844 }
2845
2846 }
2847
Willy Tarreau52542592014-04-28 18:33:26 +02002848 /* OK at this stage, we know that the request was accepted according to
2849 * the http-request rules, we can check for the stats. Note that the
2850 * URI is detected *before* the req* rules in order not to be affected
2851 * by a possible reqrep, while they are processed *after* so that a
2852 * reqdeny can still block them. This clearly needs to change in 1.6!
2853 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002854 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002855 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002856 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002857 txn->status = 500;
2858 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002859 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002860
Willy Tarreaue7dff022015-04-03 01:14:29 +02002861 if (!(s->flags & SF_ERR_MASK))
2862 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002863 goto return_prx_cond;
2864 }
2865
2866 /* parse the whole stats request and extract the relevant information */
2867 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002868 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002869 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002870
Willy Tarreau0b748332014-04-29 00:13:29 +02002871 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2872 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002873
Willy Tarreau0b748332014-04-29 00:13:29 +02002874 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2875 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002876 }
2877
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002878 /* evaluate the req* rules except reqadd */
2879 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002880 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002881 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002882
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002883 if (txn->flags & TX_CLDENY)
2884 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002885
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002886 if (txn->flags & TX_CLTARPIT) {
2887 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002888 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002889 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002890 }
Willy Tarreau06619262006-12-17 08:37:22 +01002891
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002892 /* add request headers from the rule sets in the same order */
2893 list_for_each_entry(wl, &px->req_add, list) {
2894 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002895 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002896 ret = acl_pass(ret);
2897 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2898 ret = !ret;
2899 if (!ret)
2900 continue;
2901 }
2902
Christopher Faulet10079f52018-10-03 15:17:28 +02002903 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002904 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002905 }
2906
Willy Tarreau52542592014-04-28 18:33:26 +02002907
2908 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002909 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2910 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002911 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002912 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002913 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002914
Willy Tarreaue7dff022015-04-03 01:14:29 +02002915 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2916 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2917 if (!(s->flags & SF_FINST_MASK))
2918 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002919
Willy Tarreau70730dd2014-04-24 18:06:27 +02002920 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002921 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2922 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002923 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002924 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002925 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002926
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002927 /* check whether we have some ACLs set to redirect this request */
2928 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002929 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002930 int ret;
2931
Willy Tarreau192252e2015-04-04 01:47:55 +02002932 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002933 ret = acl_pass(ret);
2934 if (rule->cond->pol == ACL_COND_UNLESS)
2935 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002936 if (!ret)
2937 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002938 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002939 if (!http_apply_redirect_rule(rule, s, txn))
2940 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002941 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002942 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002943
Willy Tarreau2be39392010-01-03 17:24:51 +01002944 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2945 * If this happens, then the data will not come immediately, so we must
2946 * send all what we have without waiting. Note that due to the small gain
2947 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002948 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002949 * itself once used.
2950 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002951 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002952
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002953 done: /* done with this analyser, continue with next ones that the calling
2954 * points will have set, if any.
2955 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002956 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002957 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
2958 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002959 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002960
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002961 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02002962 /* Allow cookie logging
2963 */
2964 if (s->be->cookie_name || sess->fe->capture_name)
2965 manage_client_side_cookies(s, req);
2966
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002967 /* When a connection is tarpitted, we use the tarpit timeout,
2968 * which may be the same as the connect timeout if unspecified.
2969 * If unset, then set it to zero because we really want it to
2970 * eventually expire. We build the tarpit as an analyser.
2971 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002972 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002973
2974 /* wipe the request out so that we can drop the connection early
2975 * if the client closes first.
2976 */
2977 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002978
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002979 txn->status = http_err_codes[deny_status];
2980
Christopher Faulet0184ea72017-01-05 14:06:34 +01002981 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002982 req->analysers |= AN_REQ_HTTP_TARPIT;
2983 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2984 if (!req->analyse_exp)
2985 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02002986 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002987 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002988 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002989 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002990 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002991 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002992 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002993
2994 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002995
2996 /* Allow cookie logging
2997 */
2998 if (s->be->cookie_name || sess->fe->capture_name)
2999 manage_client_side_cookies(s, req);
3000
Willy Tarreau0b748332014-04-29 00:13:29 +02003001 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003002 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003003 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003004 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003005 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003006 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003007 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003008 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003009 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003010 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003011 goto return_prx_cond;
3012
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003013 return_bad_req:
3014 /* We centralize bad requests processing here */
3015 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3016 /* we detected a parsing error. We want to archive this request
3017 * in the dedicated proxy area for later troubleshooting.
3018 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003019 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003020 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003021
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003022 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003023 txn->req.msg_state = HTTP_MSG_ERROR;
3024 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003025 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003026
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003027 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003028 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003029 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003030
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003031 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003032 if (!(s->flags & SF_ERR_MASK))
3033 s->flags |= SF_ERR_PRXCOND;
3034 if (!(s->flags & SF_FINST_MASK))
3035 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003036
Christopher Faulet0184ea72017-01-05 14:06:34 +01003037 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003038 req->analyse_exp = TICK_ETERNITY;
3039 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003040
3041 return_prx_yield:
3042 channel_dont_connect(req);
3043 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003044}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003045
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003046/* This function performs all the processing enabled for the current request.
3047 * It returns 1 if the processing can continue on next analysers, or zero if it
3048 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003049 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003050 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003051int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003052{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003053 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003054 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003055 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003056 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003057
Willy Tarreau655dce92009-11-08 13:10:58 +01003058 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003059 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003060 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003061 return 0;
3062 }
3063
Christopher Faulet45073512018-07-20 10:16:29 +02003064 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 +02003065 now_ms, __FUNCTION__,
3066 s,
3067 req,
3068 req->rex, req->wex,
3069 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003070 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003071 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003072
Willy Tarreau59234e92008-11-30 23:51:27 +01003073 /*
3074 * Right now, we know that we have processed the entire headers
3075 * and that unwanted requests have been filtered out. We can do
3076 * whatever we want with the remaining request. Also, now we
3077 * may have separate values for ->fe, ->be.
3078 */
Willy Tarreau06619262006-12-17 08:37:22 +01003079
Willy Tarreau59234e92008-11-30 23:51:27 +01003080 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003081 * If HTTP PROXY is set we simply get remote server address parsing
3082 * incoming request. Note that this requires that a connection is
3083 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003084 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003085 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003086 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003087 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003088
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003089 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003090 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003091 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003092 txn->req.msg_state = HTTP_MSG_ERROR;
3093 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003094 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003095 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003096
Willy Tarreaue7dff022015-04-03 01:14:29 +02003097 if (!(s->flags & SF_ERR_MASK))
3098 s->flags |= SF_ERR_RESOURCE;
3099 if (!(s->flags & SF_FINST_MASK))
3100 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003101
3102 return 0;
3103 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003104
Willy Tarreau6b952c82018-09-10 17:45:34 +02003105 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003106 if (url2sa(ci_head(req) + msg->sl.rq.u,
3107 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003108 &conn->addr.to, NULL) == -1)
3109 goto return_bad_req;
3110
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003111 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003112 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3113 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003114 * u_l characters by a single "/".
3115 */
3116 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003117 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003118 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3119 int delta;
3120
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003121 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003122 http_msg_move_end(&txn->req, delta);
3123 cur_end += delta;
3124 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3125 goto return_bad_req;
3126 }
3127 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003128 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003129 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3130 int delta;
3131
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003132 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003133 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003134 http_msg_move_end(&txn->req, delta);
3135 cur_end += delta;
3136 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3137 goto return_bad_req;
3138 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003139 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003140
Willy Tarreau59234e92008-11-30 23:51:27 +01003141 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003142 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003143 * Note that doing so might move headers in the request, but
3144 * the fields will stay coherent and the URI will not move.
3145 * This should only be performed in the backend.
3146 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003147 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003148 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003149
William Lallemanda73203e2012-03-12 12:48:57 +01003150 /* add unique-id if "header-unique-id" is specified */
3151
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003152 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003153 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003154 goto return_bad_req;
3155 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003156 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003157 }
William Lallemanda73203e2012-03-12 12:48:57 +01003158
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003159 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003160 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003161 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003162 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003163 goto return_bad_req;
3164 }
3165
Cyril Bontéb21570a2009-11-29 20:04:48 +01003166 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003167 * 9: add X-Forwarded-For if either the frontend or the backend
3168 * asks for it.
3169 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003170 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003171 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003172 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3173 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3174 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003175 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003176 /* The header is set to be added only if none is present
3177 * and we found it, so don't do anything.
3178 */
3179 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003180 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003181 /* Add an X-Forwarded-For header unless the source IP is
3182 * in the 'except' network range.
3183 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003184 if ((!sess->fe->except_mask.s_addr ||
3185 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3186 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003187 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003188 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003189 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003190 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003191 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003192 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003193
3194 /* Note: we rely on the backend to get the header name to be used for
3195 * x-forwarded-for, because the header is really meant for the backends.
3196 * However, if the backend did not specify any option, we have to rely
3197 * on the frontend's header name.
3198 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003199 if (s->be->fwdfor_hdr_len) {
3200 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003201 memcpy(trash.area,
3202 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003203 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003204 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003205 memcpy(trash.area,
3206 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003207 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003208 len += snprintf(trash.area + len,
3209 trash.size - len,
3210 ": %d.%d.%d.%d", pn[0], pn[1],
3211 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003212
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003213 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003214 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003215 }
3216 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003217 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003218 /* FIXME: for the sake of completeness, we should also support
3219 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003220 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003221 int len;
3222 char pn[INET6_ADDRSTRLEN];
3223 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003224 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003225 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003226
Willy Tarreau59234e92008-11-30 23:51:27 +01003227 /* Note: we rely on the backend to get the header name to be used for
3228 * x-forwarded-for, because the header is really meant for the backends.
3229 * However, if the backend did not specify any option, we have to rely
3230 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003231 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003232 if (s->be->fwdfor_hdr_len) {
3233 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003234 memcpy(trash.area, s->be->fwdfor_hdr_name,
3235 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003236 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003237 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003238 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3239 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003240 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003241 len += snprintf(trash.area + len, trash.size - len,
3242 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003243
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003244 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003245 goto return_bad_req;
3246 }
3247 }
3248
3249 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003250 * 10: add X-Original-To if either the frontend or the backend
3251 * asks for it.
3252 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003253 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003254
3255 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003256 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003257 /* Add an X-Original-To header unless the destination IP is
3258 * in the 'except' network range.
3259 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003260 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003261
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003262 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003263 ((!sess->fe->except_mask_to.s_addr ||
3264 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3265 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003266 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003267 (((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 +02003268 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003269 int len;
3270 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003271 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003272
3273 /* Note: we rely on the backend to get the header name to be used for
3274 * x-original-to, because the header is really meant for the backends.
3275 * However, if the backend did not specify any option, we have to rely
3276 * on the frontend's header name.
3277 */
3278 if (s->be->orgto_hdr_len) {
3279 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003280 memcpy(trash.area,
3281 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003282 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003283 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003284 memcpy(trash.area,
3285 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003286 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003287 len += snprintf(trash.area + len,
3288 trash.size - len,
3289 ": %d.%d.%d.%d", pn[0], pn[1],
3290 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003291
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003292 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003293 goto return_bad_req;
3294 }
3295 }
3296 }
3297
Willy Tarreau50fc7772012-11-11 22:19:57 +01003298 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3299 * If an "Upgrade" token is found, the header is left untouched in order not to have
3300 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3301 * "Upgrade" is present in the Connection header.
3302 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003303 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003304 unsigned int want_flags = 0;
3305
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003306 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003307 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003308 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003309 want_flags |= TX_CON_CLO_SET;
3310 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003311 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003312 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003313 want_flags |= TX_CON_KAL_SET;
3314 }
3315
3316 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003317 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003318 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003319
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003320
Willy Tarreau522d6c02009-12-06 18:49:18 +01003321 /* If we have no server assigned yet and we're balancing on url_param
3322 * with a POST request, we may be interested in checking the body for
3323 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003324 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003325 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003326 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003327 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003328 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003329 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003330 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003331
Christopher Fauletbe821b92017-03-30 11:21:53 +02003332 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3333 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003334#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003335 /* We expect some data from the client. Unless we know for sure
3336 * we already have a full request, we have to re-enable quick-ack
3337 * in case we previously disabled it, otherwise we might cause
3338 * the client to delay further data.
3339 */
3340 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3341 cli_conn && conn_ctrl_ready(cli_conn) &&
3342 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003343 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003344 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003345#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003346
Willy Tarreau59234e92008-11-30 23:51:27 +01003347 /*************************************************************
3348 * OK, that's finished for the headers. We have done what we *
3349 * could. Let's switch to the DATA state. *
3350 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003351 req->analyse_exp = TICK_ETERNITY;
3352 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003353
Willy Tarreau59234e92008-11-30 23:51:27 +01003354 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003355 /* OK let's go on with the BODY now */
3356 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003357
Willy Tarreau59234e92008-11-30 23:51:27 +01003358 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003359 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003360 /* we detected a parsing error. We want to archive this request
3361 * in the dedicated proxy area for later troubleshooting.
3362 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003363 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003364 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003365
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003366 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003367 txn->req.msg_state = HTTP_MSG_ERROR;
3368 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003369 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003370 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003371
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003372 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003373 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003374 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003375
Willy Tarreaue7dff022015-04-03 01:14:29 +02003376 if (!(s->flags & SF_ERR_MASK))
3377 s->flags |= SF_ERR_PRXCOND;
3378 if (!(s->flags & SF_FINST_MASK))
3379 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003380 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003381}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003382
Willy Tarreau60b85b02008-11-30 23:28:40 +01003383/* This function is an analyser which processes the HTTP tarpit. It always
3384 * returns zero, at the beginning because it prevents any other processing
3385 * from occurring, and at the end because it terminates the request.
3386 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003387int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003388{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003389 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003390
3391 /* This connection is being tarpitted. The CLIENT side has
3392 * already set the connect expiration date to the right
3393 * timeout. We just have to check that the client is still
3394 * there and that the timeout has not expired.
3395 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003396 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003397 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003398 !tick_is_expired(req->analyse_exp, now_ms))
3399 return 0;
3400
3401 /* We will set the queue timer to the time spent, just for
3402 * logging purposes. We fake a 500 server error, so that the
3403 * attacker will not suspect his connection has been tarpitted.
3404 * It will not cause trouble to the logs because we can exclude
3405 * the tarpitted connections by filtering on the 'PT' status flags.
3406 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003407 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3408
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003409 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003410 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003411
Christopher Faulet0184ea72017-01-05 14:06:34 +01003412 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003413 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003414
Willy Tarreaue7dff022015-04-03 01:14:29 +02003415 if (!(s->flags & SF_ERR_MASK))
3416 s->flags |= SF_ERR_PRXCOND;
3417 if (!(s->flags & SF_FINST_MASK))
3418 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003419 return 0;
3420}
3421
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003422/* This function is an analyser which waits for the HTTP request body. It waits
3423 * for either the buffer to be full, or the full advertised contents to have
3424 * reached the buffer. It must only be called after the standard HTTP request
3425 * processing has occurred, because it expects the request to be parsed and will
3426 * look for the Expect header. It may send a 100-Continue interim response. It
3427 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3428 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3429 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003430 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003431int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003432{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003433 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003434 struct http_txn *txn = s->txn;
3435 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003436
3437 /* We have to parse the HTTP request body to find any required data.
3438 * "balance url_param check_post" should have been the only way to get
3439 * into this. We were brought here after HTTP header analysis, so all
3440 * related structures are ready.
3441 */
3442
Willy Tarreau890988f2014-04-10 11:59:33 +02003443 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3444 /* This is the first call */
3445 if (msg->msg_state < HTTP_MSG_BODY)
3446 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003447
Willy Tarreau890988f2014-04-10 11:59:33 +02003448 if (msg->msg_state < HTTP_MSG_100_SENT) {
3449 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3450 * send an HTTP/1.1 100 Continue intermediate response.
3451 */
3452 if (msg->flags & HTTP_MSGF_VER_11) {
3453 struct hdr_ctx ctx;
3454 ctx.idx = 0;
3455 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003456 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003457 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003458 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003459 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003460 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003461 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003462 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003463 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003464
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003465 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003466 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003467 * must save the body in msg->next because it survives buffer
3468 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003469 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003470 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003471
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003472 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003473 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3474 else
3475 msg->msg_state = HTTP_MSG_DATA;
3476 }
3477
Willy Tarreau890988f2014-04-10 11:59:33 +02003478 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3479 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003480 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003481 goto missing_data;
3482
3483 /* OK we have everything we need now */
3484 goto http_end;
3485 }
3486
3487 /* OK here we're parsing a chunked-encoded message */
3488
Willy Tarreau522d6c02009-12-06 18:49:18 +01003489 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003490 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003491 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003492 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003493 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003494 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003495 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003496
Willy Tarreau115acb92009-12-26 13:56:06 +01003497 if (!ret)
3498 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003499 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003500 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003501 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003502 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003503 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003504 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003505 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003506
3507 msg->chunk_len = chunk;
3508 msg->body_len += chunk;
3509
3510 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003511 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003512 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003513 }
3514
Willy Tarreaud98cf932009-12-27 22:54:55 +01003515 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003516 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3517 * for at least a whole chunk or the whole content length bytes after
3518 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003519 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003520 if (msg->msg_state == HTTP_MSG_TRAILERS)
3521 goto http_end;
3522
Willy Tarreaue115b492015-05-01 23:05:14 +02003523 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003524 goto http_end;
3525
3526 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003527 /* we get here if we need to wait for more data. If the buffer is full,
3528 * we have the maximum we can expect.
3529 */
Willy Tarreau23752332018-06-15 14:54:53 +02003530 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003531 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003532
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003533 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003534 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003535 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003536
Willy Tarreaue7dff022015-04-03 01:14:29 +02003537 if (!(s->flags & SF_ERR_MASK))
3538 s->flags |= SF_ERR_CLITO;
3539 if (!(s->flags & SF_FINST_MASK))
3540 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003541 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003542 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003543
3544 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003545 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003546 /* Not enough data. We'll re-use the http-request
3547 * timeout here. Ideally, we should set the timeout
3548 * relative to the accept() date. We just set the
3549 * request timeout once at the beginning of the
3550 * request.
3551 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003552 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003553 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003554 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003555 return 0;
3556 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003557
3558 http_end:
3559 /* The situation will not evolve, so let's give up on the analysis. */
3560 s->logs.tv_request = now; /* update the request timer to reflect full request */
3561 req->analysers &= ~an_bit;
3562 req->analyse_exp = TICK_ETERNITY;
3563 return 1;
3564
3565 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003566 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003567 txn->req.msg_state = HTTP_MSG_ERROR;
3568 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003569 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003570
Willy Tarreaue7dff022015-04-03 01:14:29 +02003571 if (!(s->flags & SF_ERR_MASK))
3572 s->flags |= SF_ERR_PRXCOND;
3573 if (!(s->flags & SF_FINST_MASK))
3574 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003575
Willy Tarreau522d6c02009-12-06 18:49:18 +01003576 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003577 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003578 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003579 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003580 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003581 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003582}
3583
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003584/* send a server's name with an outgoing request over an established connection.
3585 * Note: this function is designed to be called once the request has been scheduled
3586 * for being forwarded. This is the reason why it rewinds the buffer before
3587 * proceeding.
3588 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003589int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003590
3591 struct hdr_ctx ctx;
3592
Mark Lamourinec2247f02012-01-04 13:02:01 -05003593 char *hdr_name = be->server_id_hdr_name;
3594 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003595 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003596 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003597 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003598
William Lallemandd9e90662012-01-30 17:27:17 +01003599 ctx.idx = 0;
3600
Willy Tarreau211cdec2014-04-17 20:18:08 +02003601 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003602 if (old_o) {
3603 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003604 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003605 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003606 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003607 }
3608
Willy Tarreauf37954d2018-06-15 18:31:02 +02003609 old_i = ci_data(chn);
3610 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 -05003611 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003612 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003613 }
3614
3615 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003616 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003617 memcpy(hdr_val, hdr_name, hdr_name_len);
3618 hdr_val += hdr_name_len;
3619 *hdr_val++ = ':';
3620 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003621 hdr_val += strlcpy2(hdr_val, srv_name,
3622 trash.area + trash.size - hdr_val);
3623 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3624 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003625
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003626 if (old_o) {
3627 /* If this was a forwarded request, we must readjust the amount of
3628 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003629 * variations. Note that the current state is >= HTTP_MSG_BODY,
3630 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003631 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003632 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003633 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003634 txn->req.next -= old_o;
3635 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003636 }
3637
Mark Lamourinec2247f02012-01-04 13:02:01 -05003638 return 0;
3639}
3640
Willy Tarreau610ecce2010-01-04 21:15:02 +01003641/* Terminate current transaction and prepare a new one. This is very tricky
3642 * right now but it works.
3643 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003644void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003645{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003646 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003647 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003648 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003649 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003650 struct connection *srv_conn;
3651 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003652 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003653
Willy Tarreau610ecce2010-01-04 21:15:02 +01003654 /* FIXME: We need a more portable way of releasing a backend's and a
3655 * server's connections. We need a safer way to reinitialize buffer
3656 * flags. We also need a more accurate method for computing per-request
3657 * data.
3658 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003659 /*
3660 * XXX cognet: This is probably wrong, this is killing a whole
3661 * connection, in the new world order, we probably want to just kill
3662 * the stream, this is to be revisited the day we handle multiple
3663 * streams in one server connection.
3664 */
3665 cs = objt_cs(s->si[1].end);
3666 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003667
Willy Tarreau4213a112013-12-15 10:25:42 +01003668 /* unless we're doing keep-alive, we want to quickly close the connection
3669 * to the server.
3670 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003671 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003672 !si_conn_ready(&s->si[1])) {
3673 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3674 si_shutr(&s->si[1]);
3675 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003676 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003677
Willy Tarreaue7dff022015-04-03 01:14:29 +02003678 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003679 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003680 if (unlikely(s->srv_conn))
3681 sess_change_server(s, NULL);
3682 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003683
3684 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003685 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003686
Willy Tarreaueee5b512015-04-03 23:46:31 +02003687 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003688 int n;
3689
Willy Tarreaueee5b512015-04-03 23:46:31 +02003690 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003691 if (n < 1 || n > 5)
3692 n = 0;
3693
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003694 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003695 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003696 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003697 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003698 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003699 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3700 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003701 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003702 }
3703
3704 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003705 s->logs.bytes_in -= ci_data(&s->req);
3706 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003707
Willy Tarreau66425e32018-07-25 06:55:12 +02003708 /* we may need to know the position in the queue */
3709 pendconn_free(s);
3710
Willy Tarreau610ecce2010-01-04 21:15:02 +01003711 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003712 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003713 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003714 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003715 s->do_log(s);
3716 }
3717
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003718 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003719 stream_stop_content_counters(s);
3720 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003721
Willy Tarreau610ecce2010-01-04 21:15:02 +01003722 s->logs.accept_date = date; /* user-visible date for logging */
3723 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003724 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3725 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003726 tv_zero(&s->logs.tv_request);
3727 s->logs.t_queue = -1;
3728 s->logs.t_connect = -1;
3729 s->logs.t_data = -1;
3730 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003731 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3732 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003733
Willy Tarreauf37954d2018-06-15 18:31:02 +02003734 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3735 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003736
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003737 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003738 if (s->flags & SF_CURR_SESS) {
3739 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003740 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003741 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003742 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003743 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003744 }
3745
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003746 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003747
Willy Tarreau4213a112013-12-15 10:25:42 +01003748 /* only release our endpoint if we don't intend to reuse the
3749 * connection.
3750 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003751 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003752 !si_conn_ready(&s->si[1])) {
3753 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02003754 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003755 }
3756
Willy Tarreau350f4872014-11-28 14:42:25 +01003757 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3758 s->si[1].err_type = SI_ET_NONE;
3759 s->si[1].conn_retries = 0; /* used for logging too */
3760 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003761 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 +01003762 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 +02003763 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 +02003764 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3765 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3766 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003767
Patrick Hemmere3faf022018-08-22 10:02:00 -04003768 hlua_ctx_destroy(s->hlua);
3769 s->hlua = NULL;
3770
Willy Tarreaueee5b512015-04-03 23:46:31 +02003771 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003772 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003773 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003774
3775 if (prev_status == 401 || prev_status == 407) {
3776 /* In HTTP keep-alive mode, if we receive a 401, we still have
3777 * a chance of being able to send the visitor again to the same
3778 * server over the same connection. This is required by some
3779 * broken protocols such as NTLM, and anyway whenever there is
3780 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003781 * it's better to do it (at least it helps with debugging), at
3782 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003783 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003784 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003785 }
3786
Willy Tarreau53f96852016-02-02 18:50:47 +01003787 /* Never ever allow to reuse a connection from a non-reuse backend */
3788 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3789 srv_conn->flags |= CO_FL_PRIVATE;
3790
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003791 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003792 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003793
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003794 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003795 s->req.flags |= CF_NEVER_WAIT;
3796 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003797 }
3798
Willy Tarreau714ea782015-11-25 20:11:11 +01003799 /* we're removing the analysers, we MUST re-enable events detection.
3800 * We don't enable close on the response channel since it's either
3801 * already closed, or in keep-alive with an idle connection handler.
3802 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003803 channel_auto_read(&s->req);
3804 channel_auto_close(&s->req);
3805 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003806
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003807 /* we're in keep-alive with an idle connection, monitor it if not already done */
3808 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003809 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02003810 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003811 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01003812 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003813 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02003814 else if (prev_flags & TX_NOT_FIRST)
3815 /* note: we check the request, not the connection, but
3816 * this is valid for strategies SAFE and AGGR, and in
3817 * case of ALWS, we don't care anyway.
3818 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003819 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02003820 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003821 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003822 }
Christopher Faulete6006242017-03-10 11:52:44 +01003823 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3824 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003825}
3826
3827
3828/* This function updates the request state machine according to the response
3829 * state machine and buffer flags. It returns 1 if it changes anything (flag
3830 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3831 * it is only used to find when a request/response couple is complete. Both
3832 * this function and its equivalent should loop until both return zero. It
3833 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3834 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003835int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003836{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003837 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003838 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003839 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003840 unsigned int old_state = txn->req.msg_state;
3841
Christopher Faulet4be98032017-07-18 10:48:24 +02003842 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003843 return 0;
3844
3845 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003846 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003847 * We can shut the read side unless we want to abort_on_close,
3848 * or we have a POST request. The issue with POST requests is
3849 * that some browsers still send a CRLF after the request, and
3850 * this CRLF must be read so that it does not remain in the kernel
3851 * buffers, otherwise a close could cause an RST on some systems
3852 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003853 * Note that if we're using keep-alive on the client side, we'd
3854 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003855 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003856 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003857 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003858 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3859 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003860 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3861 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003862 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003863 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003864
Willy Tarreau40f151a2012-12-20 12:10:09 +01003865 /* if the server closes the connection, we want to immediately react
3866 * and close the socket to save packets and syscalls.
3867 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003868 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003869
Willy Tarreau7f876a12015-11-18 11:59:55 +01003870 /* In any case we've finished parsing the request so we must
3871 * disable Nagle when sending data because 1) we're not going
3872 * to shut this side, and 2) the server is waiting for us to
3873 * send pending data.
3874 */
3875 chn->flags |= CF_NEVER_WAIT;
3876
Willy Tarreau610ecce2010-01-04 21:15:02 +01003877 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3878 goto wait_other_side;
3879
3880 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3881 /* The server has not finished to respond, so we
3882 * don't want to move in order not to upset it.
3883 */
3884 goto wait_other_side;
3885 }
3886
Willy Tarreau610ecce2010-01-04 21:15:02 +01003887 /* When we get here, it means that both the request and the
3888 * response have finished receiving. Depending on the connection
3889 * mode, we'll have to wait for the last bytes to leave in either
3890 * direction, and sometimes for a close to be effective.
3891 */
3892
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003893 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3894 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003895 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3896 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003897 }
3898 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3899 /* Option forceclose is set, or either side wants to close,
3900 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003901 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003902 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003903 *
3904 * However, there is an exception if the response
3905 * length is undefined. In this case, we need to wait
3906 * the close from the server. The response will be
3907 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003908 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003909 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3910 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3911 goto check_channel_flags;
3912
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003913 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3914 channel_shutr_now(chn);
3915 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003916 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003917 }
3918 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003919 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3920 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003921 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003922 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3923 channel_auto_read(chn);
3924 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003925 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003926 }
3927
Christopher Faulet4be98032017-07-18 10:48:24 +02003928 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003929 }
3930
3931 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3932 http_msg_closing:
3933 /* nothing else to forward, just waiting for the output buffer
3934 * to be empty and for the shutw_now to take effect.
3935 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003936 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003937 txn->req.msg_state = HTTP_MSG_CLOSED;
3938 goto http_msg_closed;
3939 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003940 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003941 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003942 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003943 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003944 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003945 }
3946
3947 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3948 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01003949 /* if we don't know whether the server will close, we need to hard close */
3950 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
3951 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3952
Willy Tarreau3988d932013-12-27 23:03:08 +01003953 /* see above in MSG_DONE why we only do this in these states */
3954 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3955 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003956 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3957 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01003958 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003959 goto wait_other_side;
3960 }
3961
Christopher Faulet4be98032017-07-18 10:48:24 +02003962 check_channel_flags:
3963 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3964 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3965 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02003966 txn->req.msg_state = HTTP_MSG_CLOSING;
3967 goto http_msg_closing;
3968 }
3969
3970
Willy Tarreau610ecce2010-01-04 21:15:02 +01003971 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003972 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003973}
3974
3975
3976/* This function updates the response state machine according to the request
3977 * state machine and buffer flags. It returns 1 if it changes anything (flag
3978 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3979 * it is only used to find when a request/response couple is complete. Both
3980 * this function and its equivalent should loop until both return zero. It
3981 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3982 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003983int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003984{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003985 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003986 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003987 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003988 unsigned int old_state = txn->rsp.msg_state;
3989
Christopher Faulet4be98032017-07-18 10:48:24 +02003990 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003991 return 0;
3992
3993 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3994 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003995 * still monitor the server connection for a possible close
3996 * while the request is being uploaded, so we don't disable
3997 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003998 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003999 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004000
4001 if (txn->req.msg_state == HTTP_MSG_ERROR)
4002 goto wait_other_side;
4003
4004 if (txn->req.msg_state < HTTP_MSG_DONE) {
4005 /* The client seems to still be sending data, probably
4006 * because we got an error response during an upload.
4007 * We have the choice of either breaking the connection
4008 * or letting it pass through. Let's do the later.
4009 */
4010 goto wait_other_side;
4011 }
4012
Willy Tarreau610ecce2010-01-04 21:15:02 +01004013 /* When we get here, it means that both the request and the
4014 * response have finished receiving. Depending on the connection
4015 * mode, we'll have to wait for the last bytes to leave in either
4016 * direction, and sometimes for a close to be effective.
4017 */
4018
4019 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4020 /* Server-close mode : shut read and wait for the request
4021 * side to close its output buffer. The caller will detect
4022 * when we're in DONE and the other is in CLOSED and will
4023 * catch that for the final cleanup.
4024 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004025 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4026 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004027 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004028 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4029 /* Option forceclose is set, or either side wants to close,
4030 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004031 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004032 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004034 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004035 channel_shutr_now(chn);
4036 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004037 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004038 }
4039 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004040 /* The last possible modes are keep-alive and tunnel. Tunnel will
4041 * need to forward remaining data. Keep-alive will need to monitor
4042 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004043 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004044 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004045 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004046 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4047 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004048 }
4049
Christopher Faulet4be98032017-07-18 10:48:24 +02004050 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004051 }
4052
4053 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4054 http_msg_closing:
4055 /* nothing else to forward, just waiting for the output buffer
4056 * to be empty and for the shutw_now to take effect.
4057 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004058 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004059 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4060 goto http_msg_closed;
4061 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004062 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004063 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004064 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004065 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004066 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004067 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004068 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004069 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004070 }
4071
4072 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4073 http_msg_closed:
4074 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004075 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004076 channel_auto_close(chn);
4077 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004078 goto wait_other_side;
4079 }
4080
Christopher Faulet4be98032017-07-18 10:48:24 +02004081 check_channel_flags:
4082 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4083 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4084 /* if we've just closed an output, let's switch */
4085 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4086 goto http_msg_closing;
4087 }
4088
Willy Tarreau610ecce2010-01-04 21:15:02 +01004089 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004090 /* We force the response to leave immediately if we're waiting for the
4091 * other side, since there is no pending shutdown to push it out.
4092 */
4093 if (!channel_is_empty(chn))
4094 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004095 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004096}
4097
4098
Christopher Faulet894da4c2017-07-18 11:29:07 +02004099/* Resync the request and response state machines. */
4100void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004101{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004102 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004103#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004104 int old_req_state = txn->req.msg_state;
4105 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004106#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004107
Willy Tarreau610ecce2010-01-04 21:15:02 +01004108 http_sync_req_state(s);
4109 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004110 if (!http_sync_res_state(s))
4111 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004112 if (!http_sync_req_state(s))
4113 break;
4114 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004115
Christopher Faulet894da4c2017-07-18 11:29:07 +02004116 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4117 "req->analysers=0x%08x res->analysers=0x%08x\n",
4118 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004119 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4120 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004121 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004122
4123
Willy Tarreau610ecce2010-01-04 21:15:02 +01004124 /* OK, both state machines agree on a compatible state.
4125 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004126 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4127 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004128 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4129 * means we must abort the request.
4130 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4131 * corresponding channel.
4132 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4133 * on the response with server-close mode means we've completed one
4134 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004135 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004136 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4137 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004138 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004139 channel_auto_close(&s->req);
4140 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004141 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004142 channel_auto_close(&s->res);
4143 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004144 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004145 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4146 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004147 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004148 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004149 channel_auto_close(&s->res);
4150 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004151 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004152 channel_abort(&s->req);
4153 channel_auto_close(&s->req);
4154 channel_auto_read(&s->req);
4155 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004156 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004157 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4158 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4159 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4160 s->req.analysers &= AN_REQ_FLT_END;
4161 if (HAS_REQ_DATA_FILTERS(s))
4162 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4163 }
4164 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4165 s->res.analysers &= AN_RES_FLT_END;
4166 if (HAS_RSP_DATA_FILTERS(s))
4167 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4168 }
4169 channel_auto_close(&s->req);
4170 channel_auto_read(&s->req);
4171 channel_auto_close(&s->res);
4172 channel_auto_read(&s->res);
4173 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004174 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4175 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004176 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004177 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4178 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4179 /* server-close/keep-alive: terminate this transaction,
4180 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004181 * a fresh-new transaction, but only once we're sure there's
4182 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004183 * another request. They must not hold any pending output data
4184 * and the response buffer must realigned
4185 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004186 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004187 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004188 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004189 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004190 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004191 else {
4192 s->req.analysers = AN_REQ_FLT_END;
4193 s->res.analysers = AN_RES_FLT_END;
4194 txn->flags |= TX_WAIT_CLEANUP;
4195 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004196 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004197}
4198
Willy Tarreaud98cf932009-12-27 22:54:55 +01004199/* This function is an analyser which forwards request body (including chunk
4200 * sizes if any). It is called as soon as we must forward, even if we forward
4201 * zero byte. The only situation where it must not be called is when we're in
4202 * tunnel mode and we want to forward till the close. It's used both to forward
4203 * remaining data and to resync after end of body. It expects the msg_state to
4204 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004205 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004206 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004207 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004208 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004209int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004210{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004211 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004212 struct http_txn *txn = s->txn;
4213 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004214 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004215
Christopher Faulet45073512018-07-20 10:16:29 +02004216 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 +02004217 now_ms, __FUNCTION__,
4218 s,
4219 req,
4220 req->rex, req->wex,
4221 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004222 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004223 req->analysers);
4224
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004225 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4226 return 0;
4227
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004228 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004229 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004230 /* Output closed while we were sending data. We must abort and
4231 * wake the other side up.
4232 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004233 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004234 msg->msg_state = HTTP_MSG_ERROR;
4235 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004236 return 1;
4237 }
4238
Willy Tarreaud98cf932009-12-27 22:54:55 +01004239 /* Note that we don't have to send 100-continue back because we don't
4240 * need the data to complete our job, and it's up to the server to
4241 * decide whether to return 100, 417 or anything else in return of
4242 * an "Expect: 100-continue" header.
4243 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004244 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004245 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4246 ? HTTP_MSG_CHUNK_SIZE
4247 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004248
4249 /* TODO/filters: when http-buffer-request option is set or if a
4250 * rule on url_param exists, the first chunk size could be
4251 * already parsed. In that case, msg->next is after the chunk
4252 * size (including the CRLF after the size). So this case should
4253 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004254 }
4255
Willy Tarreau7ba23542014-04-17 21:50:00 +02004256 /* Some post-connect processing might want us to refrain from starting to
4257 * forward data. Currently, the only reason for this is "balance url_param"
4258 * whichs need to parse/process the request after we've enabled forwarding.
4259 */
4260 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004261 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004262 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004263 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004264 channel_dont_close(req); /* don't fail on early shutr */
4265 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004266 }
4267 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4268 }
4269
Willy Tarreau80a92c02014-03-12 10:41:13 +01004270 /* in most states, we should abort in case of early close */
4271 channel_auto_close(req);
4272
Willy Tarreauefdf0942014-04-24 20:08:57 +02004273 if (req->to_forward) {
4274 /* We can't process the buffer's contents yet */
4275 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004276 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004277 }
4278
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004279 if (msg->msg_state < HTTP_MSG_DONE) {
4280 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4281 ? http_msg_forward_chunked_body(s, msg)
4282 : http_msg_forward_body(s, msg));
4283 if (!ret)
4284 goto missing_data_or_waiting;
4285 if (ret < 0)
4286 goto return_bad_req;
4287 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004288
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004289 /* other states, DONE...TUNNEL */
4290 /* we don't want to forward closes on DONE except in tunnel mode. */
4291 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4292 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004293
Christopher Faulet894da4c2017-07-18 11:29:07 +02004294 http_resync_states(s);
4295 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004296 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4297 if (req->flags & CF_SHUTW) {
4298 /* request errors are most likely due to the
4299 * server aborting the transfer. */
4300 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004301 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004302 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004303 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004304 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004305 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004306 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004307 }
4308
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004309 /* If "option abortonclose" is set on the backend, we want to monitor
4310 * the client's connection and forward any shutdown notification to the
4311 * server, which will decide whether to close or to go on processing the
4312 * request. We only do that in tunnel mode, and not in other modes since
4313 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004314 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004315 channel_auto_read(req);
4316 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4317 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4318 s->si[1].flags |= SI_FL_NOLINGER;
4319 channel_auto_close(req);
4320 }
4321 else if (s->txn->meth == HTTP_METH_POST) {
4322 /* POST requests may require to read extra CRLF sent by broken
4323 * browsers and which could cause an RST to be sent upon close
4324 * on some systems (eg: Linux). */
4325 channel_auto_read(req);
4326 }
4327 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004328
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004329 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004330 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004331 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004332 if (!(s->flags & SF_ERR_MASK))
4333 s->flags |= SF_ERR_CLICL;
4334 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004335 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004336 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004337 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004338 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004339 }
4340
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004341 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4342 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004343 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004344 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004345
4346 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004347 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004348
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004349 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004350 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004351 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004352 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004353
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004354 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004355 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004356 * And when content-length is used, we never want to let the possible
4357 * shutdown be forwarded to the other side, as the state machine will
4358 * take care of it once the client responds. It's also important to
4359 * prevent TIME_WAITs from accumulating on the backend side, and for
4360 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004361 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004362 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004363 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004364
Willy Tarreau5c620922011-05-11 19:56:11 +02004365 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004366 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004367 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004368 * modes are already handled by the stream sock layer. We must not do
4369 * this in content-length mode because it could present the MSG_MORE
4370 * flag with the last block of forwarded data, which would cause an
4371 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004372 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004373 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004374 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004375
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004376 return 0;
4377
Willy Tarreaud98cf932009-12-27 22:54:55 +01004378 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004379 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004380 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004381 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004382
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004383 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004384 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004385 txn->req.msg_state = HTTP_MSG_ERROR;
4386 if (txn->status) {
4387 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004388 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004389 } else {
4390 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004391 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004392 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004393 req->analysers &= AN_REQ_FLT_END;
4394 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 +01004395
Willy Tarreaue7dff022015-04-03 01:14:29 +02004396 if (!(s->flags & SF_ERR_MASK))
4397 s->flags |= SF_ERR_PRXCOND;
4398 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004399 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004400 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004401 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004402 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004403 }
4404 return 0;
4405
4406 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004407 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004408 txn->req.msg_state = HTTP_MSG_ERROR;
4409 if (txn->status) {
4410 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004411 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004412 } else {
4413 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004414 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004415 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004416 req->analysers &= AN_REQ_FLT_END;
4417 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 +01004418
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004419 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4420 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004421 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004422 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004423
Willy Tarreaue7dff022015-04-03 01:14:29 +02004424 if (!(s->flags & SF_ERR_MASK))
4425 s->flags |= SF_ERR_SRVCL;
4426 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004427 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004428 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004429 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004430 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004431 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004432 return 0;
4433}
4434
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004435/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4436 * processing can continue on next analysers, or zero if it either needs more
4437 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004438 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004439 * when it has nothing left to do, and may remove any analyser when it wants to
4440 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004441 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004442int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004443{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004444 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004445 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004446 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004447 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004448 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004449 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004450 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004451 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004452
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004453 srv_conn = cs_conn(objt_cs(s->si[1].end));
4454
Christopher Faulet45073512018-07-20 10:16:29 +02004455 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 +02004456 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004457 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004458 rep,
4459 rep->rex, rep->wex,
4460 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004461 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004462 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004463
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004464 /*
4465 * Now parse the partial (or complete) lines.
4466 * We will check the response syntax, and also join multi-line
4467 * headers. An index of all the lines will be elaborated while
4468 * parsing.
4469 *
4470 * For the parsing, we use a 28 states FSM.
4471 *
4472 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004473 * ci_head(rep) = beginning of response
4474 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4475 * ci_tail(rep) = end of input data
4476 * msg->eol = end of current header or line (LF or CRLF)
4477 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004478 */
4479
Willy Tarreau628c40c2014-04-24 19:11:26 +02004480 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004481 /* There's a protected area at the end of the buffer for rewriting
4482 * purposes. We don't want to start to parse the request if the
4483 * protected area is affected, because we may have to move processed
4484 * data later, which is much more complicated.
4485 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004486 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004487 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004488 /* some data has still not left the buffer, wake us once that's done */
4489 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4490 goto abort_response;
4491 channel_dont_close(rep);
4492 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004493 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004494 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004495 }
4496
Willy Tarreau188e2302018-06-15 11:11:53 +02004497 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004498 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004499 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004500
Willy Tarreauf37954d2018-06-15 18:31:02 +02004501 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004502 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004503 }
4504
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004505 /* 1: we might have to print this header in debug mode */
4506 if (unlikely((global.mode & MODE_DEBUG) &&
4507 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004508 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004509 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004510
Willy Tarreauf37954d2018-06-15 18:31:02 +02004511 sol = ci_head(rep);
4512 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004513 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004514
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004515 sol += hdr_idx_first_pos(&txn->hdr_idx);
4516 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004517
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004518 while (cur_idx) {
4519 eol = sol + txn->hdr_idx.v[cur_idx].len;
4520 debug_hdr("srvhdr", s, sol, eol);
4521 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4522 cur_idx = txn->hdr_idx.v[cur_idx].next;
4523 }
4524 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004525
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004526 /*
4527 * Now we quickly check if we have found a full valid response.
4528 * If not so, we check the FD and buffer states before leaving.
4529 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004530 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004531 * responses are checked first.
4532 *
4533 * Depending on whether the client is still there or not, we
4534 * may send an error response back or not. Note that normally
4535 * we should only check for HTTP status there, and check I/O
4536 * errors somewhere else.
4537 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004538
Willy Tarreau655dce92009-11-08 13:10:58 +01004539 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004540 /* Invalid response */
4541 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4542 /* we detected a parsing error. We want to archive this response
4543 * in the dedicated proxy area for later troubleshooting.
4544 */
4545 hdr_response_bad:
4546 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004547 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004548
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004549 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004550 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004551 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004552 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004553 }
Willy Tarreau64648412010-03-05 10:41:54 +01004554 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004555 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004556 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004557 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004558 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004559 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004560 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004561
Willy Tarreaue7dff022015-04-03 01:14:29 +02004562 if (!(s->flags & SF_ERR_MASK))
4563 s->flags |= SF_ERR_PRXCOND;
4564 if (!(s->flags & SF_FINST_MASK))
4565 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004566
4567 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004568 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004569
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004570 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004571 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004572 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004573 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004574 goto hdr_response_bad;
4575 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004576
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004577 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004578 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004579 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004580 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004581 else if (txn->flags & TX_NOT_FIRST)
4582 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004583
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004584 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004585 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004586 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004587 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004588 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004589
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004590 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004591 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004592 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004593
4594 /* Check to see if the server refused the early data.
4595 * If so, just send a 425
4596 */
4597 if (objt_cs(s->si[1].end)) {
4598 struct connection *conn = objt_cs(s->si[1].end)->conn;
4599
4600 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4601 txn->status = 425;
4602 }
4603
Willy Tarreau350f4872014-11-28 14:42:25 +01004604 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004605 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004606 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004607
Willy Tarreaue7dff022015-04-03 01:14:29 +02004608 if (!(s->flags & SF_ERR_MASK))
4609 s->flags |= SF_ERR_SRVCL;
4610 if (!(s->flags & SF_FINST_MASK))
4611 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004612 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004613 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004614
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004615 /* read timeout : return a 504 to the client. */
4616 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004617 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004618 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004619
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004620 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004621 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004622 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004623 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004624 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004625
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004626 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004627 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004628 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004629 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004630 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004631 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004632
Willy Tarreaue7dff022015-04-03 01:14:29 +02004633 if (!(s->flags & SF_ERR_MASK))
4634 s->flags |= SF_ERR_SRVTO;
4635 if (!(s->flags & SF_FINST_MASK))
4636 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004637 return 0;
4638 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004639
Willy Tarreauf003d372012-11-26 13:35:37 +01004640 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004641 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004642 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4643 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004644 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004645 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004646
Christopher Faulet0184ea72017-01-05 14:06:34 +01004647 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004648 channel_auto_close(rep);
4649
4650 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004651 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004652 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004653
Willy Tarreaue7dff022015-04-03 01:14:29 +02004654 if (!(s->flags & SF_ERR_MASK))
4655 s->flags |= SF_ERR_CLICL;
4656 if (!(s->flags & SF_FINST_MASK))
4657 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004658
Willy Tarreau87b09662015-04-03 00:22:06 +02004659 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004660 return 0;
4661 }
4662
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004663 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004664 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004665 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004666 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004667 else if (txn->flags & TX_NOT_FIRST)
4668 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004669
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004670 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004671 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004672 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004673 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004674 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004675
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004676 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004677 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004678 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004679 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004680 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004681 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004682
Willy Tarreaue7dff022015-04-03 01:14:29 +02004683 if (!(s->flags & SF_ERR_MASK))
4684 s->flags |= SF_ERR_SRVCL;
4685 if (!(s->flags & SF_FINST_MASK))
4686 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004687 return 0;
4688 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004689
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004690 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004691 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004692 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004693 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004694 else if (txn->flags & TX_NOT_FIRST)
4695 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004696
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004697 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004698 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004699 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004700
Willy Tarreaue7dff022015-04-03 01:14:29 +02004701 if (!(s->flags & SF_ERR_MASK))
4702 s->flags |= SF_ERR_CLICL;
4703 if (!(s->flags & SF_FINST_MASK))
4704 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004705
Willy Tarreau87b09662015-04-03 00:22:06 +02004706 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004707 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004708 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004709
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004710 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004711 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004712 return 0;
4713 }
4714
4715 /* More interesting part now : we know that we have a complete
4716 * response which at least looks like HTTP. We have an indicator
4717 * of each header's length, so we can parse them quickly.
4718 */
4719
4720 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004721 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004722
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004723 /*
4724 * 1: get the status code
4725 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004726 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004727 if (n < 1 || n > 5)
4728 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004729 /* when the client triggers a 4xx from the server, it's most often due
4730 * to a missing object or permission. These events should be tracked
4731 * because if they happen often, it may indicate a brute force or a
4732 * vulnerability scan.
4733 */
4734 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004735 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004736
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004737 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004738 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004739
Willy Tarreau91852eb2015-05-01 13:26:00 +02004740 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4741 * exactly one digit "." one digit. This check may be disabled using
4742 * option accept-invalid-http-response.
4743 */
4744 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4745 if (msg->sl.st.v_l != 8) {
4746 msg->err_pos = 0;
4747 goto hdr_response_bad;
4748 }
4749
Willy Tarreauf37954d2018-06-15 18:31:02 +02004750 if (ci_head(rep)[4] != '/' ||
4751 !isdigit((unsigned char)ci_head(rep)[5]) ||
4752 ci_head(rep)[6] != '.' ||
4753 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004754 msg->err_pos = 4;
4755 goto hdr_response_bad;
4756 }
4757 }
4758
Willy Tarreau5b154472009-12-21 20:11:07 +01004759 /* check if the response is HTTP/1.1 or above */
4760 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004761 ((ci_head(rep)[5] > '1') ||
4762 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004763 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004764
4765 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004766 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 +01004767
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004768 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004769 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004770
Willy Tarreauf37954d2018-06-15 18:31:02 +02004771 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004772
Willy Tarreau39650402010-03-15 19:44:39 +01004773 /* Adjust server's health based on status code. Note: status codes 501
4774 * and 505 are triggered on demand by client request, so we must not
4775 * count them as server failures.
4776 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004777 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004778 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004779 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004780 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004781 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004782 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004783
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004784 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004785 * We may be facing a 100-continue response, or any other informational
4786 * 1xx response which is non-final, in which case this is not the right
4787 * response, and we're waiting for the next one. Let's allow this response
4788 * to go to the client and wait for the next one. There's an exception for
4789 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004790 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004791 if (txn->status < 200 &&
4792 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004793 hdr_idx_init(&txn->hdr_idx);
4794 msg->next -= channel_forward(rep, msg->next);
4795 msg->msg_state = HTTP_MSG_RPBEFORE;
4796 txn->status = 0;
4797 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004798 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004799 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004800 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004801
Willy Tarreaua14ad722017-07-07 11:36:32 +02004802 /*
4803 * 2: check for cacheability.
4804 */
4805
4806 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004807 case 200:
4808 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004809 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004810 case 206:
4811 case 300:
4812 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004813 case 404:
4814 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004815 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004816 case 414:
4817 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004818 break;
4819 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004820 /* RFC7231#6.1:
4821 * Responses with status codes that are defined as
4822 * cacheable by default (e.g., 200, 203, 204, 206,
4823 * 300, 301, 404, 405, 410, 414, and 501 in this
4824 * specification) can be reused by a cache with
4825 * heuristic expiration unless otherwise indicated
4826 * by the method definition or explicit cache
4827 * controls [RFC7234]; all other status codes are
4828 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004829 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004830 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004831 break;
4832 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004833
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004834 /*
4835 * 3: we may need to capture headers
4836 */
4837 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004838 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004839 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4840 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004841
Willy Tarreau557f1992015-05-01 10:05:17 +02004842 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4843 * by RFC7230#3.3.3 :
4844 *
4845 * The length of a message body is determined by one of the following
4846 * (in order of precedence):
4847 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004848 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4849 * the connection will become a tunnel immediately after the empty
4850 * line that concludes the header fields. A client MUST ignore
4851 * any Content-Length or Transfer-Encoding header fields received
4852 * in such a message. Any 101 response (Switching Protocols) is
4853 * managed in the same manner.
4854 *
4855 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004856 * (Informational), 204 (No Content), or 304 (Not Modified) status
4857 * code is always terminated by the first empty line after the
4858 * header fields, regardless of the header fields present in the
4859 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004860 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004861 * 3. If a Transfer-Encoding header field is present and the chunked
4862 * transfer coding (Section 4.1) is the final encoding, the message
4863 * body length is determined by reading and decoding the chunked
4864 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004865 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004866 * If a Transfer-Encoding header field is present in a response and
4867 * the chunked transfer coding is not the final encoding, the
4868 * message body length is determined by reading the connection until
4869 * it is closed by the server. If a Transfer-Encoding header field
4870 * is present in a request and the chunked transfer coding is not
4871 * the final encoding, the message body length cannot be determined
4872 * reliably; the server MUST respond with the 400 (Bad Request)
4873 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004874 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004875 * If a message is received with both a Transfer-Encoding and a
4876 * Content-Length header field, the Transfer-Encoding overrides the
4877 * Content-Length. Such a message might indicate an attempt to
4878 * perform request smuggling (Section 9.5) or response splitting
4879 * (Section 9.4) and ought to be handled as an error. A sender MUST
4880 * remove the received Content-Length field prior to forwarding such
4881 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004882 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004883 * 4. If a message is received without Transfer-Encoding and with
4884 * either multiple Content-Length header fields having differing
4885 * field-values or a single Content-Length header field having an
4886 * invalid value, then the message framing is invalid and the
4887 * recipient MUST treat it as an unrecoverable error. If this is a
4888 * request message, the server MUST respond with a 400 (Bad Request)
4889 * status code and then close the connection. If this is a response
4890 * message received by a proxy, the proxy MUST close the connection
4891 * to the server, discard the received response, and send a 502 (Bad
4892 * Gateway) response to the client. If this is a response message
4893 * received by a user agent, the user agent MUST close the
4894 * connection to the server and discard the received response.
4895 *
4896 * 5. If a valid Content-Length header field is present without
4897 * Transfer-Encoding, its decimal value defines the expected message
4898 * body length in octets. If the sender closes the connection or
4899 * the recipient times out before the indicated number of octets are
4900 * received, the recipient MUST consider the message to be
4901 * incomplete and close the connection.
4902 *
4903 * 6. If this is a request message and none of the above are true, then
4904 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004905 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004906 * 7. Otherwise, this is a response message without a declared message
4907 * body length, so the message body length is determined by the
4908 * number of octets received prior to the server closing the
4909 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004910 */
4911
4912 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004913 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004914 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004915 * FIXME: should we parse anyway and return an error on chunked encoding ?
4916 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004917 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4918 txn->status == 101)) {
4919 /* Either we've established an explicit tunnel, or we're
4920 * switching the protocol. In both cases, we're very unlikely
4921 * to understand the next protocols. We have to switch to tunnel
4922 * mode, so that we transfer the request and responses then let
4923 * this protocol pass unmodified. When we later implement specific
4924 * parsers for such protocols, we'll want to check the Upgrade
4925 * header which contains information about that protocol for
4926 * responses with status 101 (eg: see RFC2817 about TLS).
4927 */
4928 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4929 msg->flags |= HTTP_MSGF_XFER_LEN;
4930 goto end;
4931 }
4932
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004933 if (txn->meth == HTTP_METH_HEAD ||
4934 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004935 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004936 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004937 goto skip_content_length;
4938 }
4939
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004940 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004941 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004942 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004943 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004944 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4945 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004946 /* bad transfer-encoding (chunked followed by something else) */
4947 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004948 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004949 break;
4950 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004951 }
4952
Willy Tarreau1c913912015-04-30 10:57:51 +02004953 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004954 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02004955 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004956 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02004957 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4958 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02004959 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004960 signed long long cl;
4961
Willy Tarreauad14f752011-09-02 20:33:27 +02004962 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004963 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004964 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004965 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004966
Willy Tarreauad14f752011-09-02 20:33:27 +02004967 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004968 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004969 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004970 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004971
Willy Tarreauad14f752011-09-02 20:33:27 +02004972 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004973 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004974 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004975 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004976
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004977 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004978 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004979 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004980 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004981
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004982 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004983 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004984 }
4985
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004986 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
4987 * 407 (Proxy-Authenticate) responses and set the connection to private
4988 */
4989 if (srv_conn && txn->status == 401) {
4990 /* check for Negotiate/NTLM WWW-Authenticate headers */
4991 ctx.idx = 0;
4992 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
4993 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4994 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4995 srv_conn->flags |= CO_FL_PRIVATE;
4996 }
4997 } else if (srv_conn && txn->status == 407) {
4998 /* check for Negotiate/NTLM Proxy-Authenticate headers */
4999 ctx.idx = 0;
5000 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
5001 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5002 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5003 srv_conn->flags |= CO_FL_PRIVATE;
5004 }
5005 }
5006
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005007 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005008 /* Now we have to check if we need to modify the Connection header.
5009 * This is more difficult on the response than it is on the request,
5010 * because we can have two different HTTP versions and we don't know
5011 * how the client will interprete a response. For instance, let's say
5012 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5013 * HTTP/1.1 response without any header. Maybe it will bound itself to
5014 * HTTP/1.0 because it only knows about it, and will consider the lack
5015 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5016 * the lack of header as a keep-alive. Thus we will use two flags
5017 * indicating how a request MAY be understood by the client. In case
5018 * of multiple possibilities, we'll fix the header to be explicit. If
5019 * ambiguous cases such as both close and keepalive are seen, then we
5020 * will fall back to explicit close. Note that we won't take risks with
5021 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005022 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005023 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005024 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005025 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01005026 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005027
Willy Tarreau60466522010-01-18 19:08:45 +01005028 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005029 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01005030 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005031
Willy Tarreau60466522010-01-18 19:08:45 +01005032 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005033 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01005034 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005035 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005036 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005037 }
Willy Tarreau60466522010-01-18 19:08:45 +01005038 else { /* SCL / KAL */
5039 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005040 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005041 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005042 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005043
Willy Tarreau60466522010-01-18 19:08:45 +01005044 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005045 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005046
Willy Tarreau60466522010-01-18 19:08:45 +01005047 /* Some keep-alive responses are converted to Server-close if
5048 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005049 */
Willy Tarreau60466522010-01-18 19:08:45 +01005050 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5051 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005052 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005053 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005054 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005055 }
5056
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005057 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005058 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005059 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005060
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005061 /* end of job, return OK */
5062 rep->analysers &= ~an_bit;
5063 rep->analyse_exp = TICK_ETERNITY;
5064 channel_auto_close(rep);
5065 return 1;
5066
5067 abort_keep_alive:
5068 /* A keep-alive request to the server failed on a network error.
5069 * The client is required to retry. We need to close without returning
5070 * any other information so that the client retries.
5071 */
5072 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005073 rep->analysers &= AN_RES_FLT_END;
5074 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005075 channel_auto_close(rep);
5076 s->logs.logwait = 0;
5077 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005078 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005079 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005080 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005081 return 0;
5082}
5083
5084/* This function performs all the processing enabled for the current response.
5085 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005086 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005087 * other functions. It works like process_request (see indications above).
5088 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005089int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005090{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005091 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005092 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005093 struct http_msg *msg = &txn->rsp;
5094 struct proxy *cur_proxy;
5095 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005096 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005097
Christopher Faulet45073512018-07-20 10:16:29 +02005098 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 +02005099 now_ms, __FUNCTION__,
5100 s,
5101 rep,
5102 rep->rex, rep->wex,
5103 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005104 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005105 rep->analysers);
5106
5107 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5108 return 0;
5109
Willy Tarreau70730dd2014-04-24 18:06:27 +02005110 /* The stats applet needs to adjust the Connection header but we don't
5111 * apply any filter there.
5112 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005113 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5114 rep->analysers &= ~an_bit;
5115 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005116 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005117 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005118
Willy Tarreau58975672014-04-24 21:13:57 +02005119 /*
5120 * We will have to evaluate the filters.
5121 * As opposed to version 1.2, now they will be evaluated in the
5122 * filters order and not in the header order. This means that
5123 * each filter has to be validated among all headers.
5124 *
5125 * Filters are tried with ->be first, then with ->fe if it is
5126 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005127 *
5128 * Maybe we are in resume condiion. In this case I choose the
5129 * "struct proxy" which contains the rule list matching the resume
5130 * pointer. If none of theses "struct proxy" match, I initialise
5131 * the process with the first one.
5132 *
5133 * In fact, I check only correspondance betwwen the current list
5134 * pointer and the ->fe rule list. If it doesn't match, I initialize
5135 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005136 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005137 if (s->current_rule_list == &sess->fe->http_res_rules)
5138 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005139 else
5140 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005141 while (1) {
5142 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005143
Willy Tarreau58975672014-04-24 21:13:57 +02005144 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005145 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005146 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005147
Willy Tarreau51d861a2015-05-22 17:30:48 +02005148 if (ret == HTTP_RULE_RES_BADREQ)
5149 goto return_srv_prx_502;
5150
5151 if (ret == HTTP_RULE_RES_DONE) {
5152 rep->analysers &= ~an_bit;
5153 rep->analyse_exp = TICK_ETERNITY;
5154 return 1;
5155 }
5156 }
5157
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005158 /* we need to be called again. */
5159 if (ret == HTTP_RULE_RES_YIELD) {
5160 channel_dont_close(rep);
5161 return 0;
5162 }
5163
Willy Tarreau58975672014-04-24 21:13:57 +02005164 /* try headers filters */
5165 if (rule_set->rsp_exp != NULL) {
5166 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5167 return_bad_resp:
5168 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005169 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005170 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005171 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005172 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005173 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005174 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005175 txn->status = 502;
5176 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005177 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005178 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005179 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005180 if (!(s->flags & SF_ERR_MASK))
5181 s->flags |= SF_ERR_PRXCOND;
5182 if (!(s->flags & SF_FINST_MASK))
5183 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005184 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005185 }
Willy Tarreau58975672014-04-24 21:13:57 +02005186 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005187
Willy Tarreau58975672014-04-24 21:13:57 +02005188 /* has the response been denied ? */
5189 if (txn->flags & TX_SVDENY) {
5190 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005191 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005192
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005193 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5194 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005195 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005196 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005197
Willy Tarreau58975672014-04-24 21:13:57 +02005198 goto return_srv_prx_502;
5199 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005200
Willy Tarreau58975672014-04-24 21:13:57 +02005201 /* add response headers from the rule sets in the same order */
5202 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005203 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005204 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005205 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005206 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005207 ret = acl_pass(ret);
5208 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5209 ret = !ret;
5210 if (!ret)
5211 continue;
5212 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005213 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005214 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005215 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005216
Willy Tarreau58975672014-04-24 21:13:57 +02005217 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005218 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005219 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005220 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005221 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005222
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005223 /* After this point, this anayzer can't return yield, so we can
5224 * remove the bit corresponding to this analyzer from the list.
5225 *
5226 * Note that the intermediate returns and goto found previously
5227 * reset the analyzers.
5228 */
5229 rep->analysers &= ~an_bit;
5230 rep->analyse_exp = TICK_ETERNITY;
5231
Willy Tarreau58975672014-04-24 21:13:57 +02005232 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005233 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005234 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005235
Willy Tarreau58975672014-04-24 21:13:57 +02005236 /*
5237 * Now check for a server cookie.
5238 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005239 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005240 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005241
Willy Tarreau58975672014-04-24 21:13:57 +02005242 /*
5243 * Check for cache-control or pragma headers if required.
5244 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005245 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005246 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005247
Willy Tarreau58975672014-04-24 21:13:57 +02005248 /*
5249 * Add server cookie in the response if needed
5250 */
5251 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5252 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005253 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005254 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5255 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5256 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5257 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5258 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005259 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005260 /* the server is known, it's not the one the client requested, or the
5261 * cookie's last seen date needs to be refreshed. We have to
5262 * insert a set-cookie here, except if we want to insert only on POST
5263 * requests and this one isn't. Note that servers which don't have cookies
5264 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005265 */
Willy Tarreau58975672014-04-24 21:13:57 +02005266 if (!objt_server(s->target)->cookie) {
5267 chunk_printf(&trash,
5268 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5269 s->be->cookie_name);
5270 }
5271 else {
5272 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005273
Willy Tarreau58975672014-04-24 21:13:57 +02005274 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5275 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005276 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5277 s30tob64((date.tv_sec+3) >> 2,
5278 trash.area + trash.data);
5279 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005280
Willy Tarreau58975672014-04-24 21:13:57 +02005281 if (s->be->cookie_maxlife) {
5282 /* emit first_date, which is either the original one or
5283 * the current date.
5284 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005285 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005286 s30tob64(txn->cookie_first_date ?
5287 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005288 (date.tv_sec+3) >> 2,
5289 trash.area + trash.data);
5290 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005291 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005292 }
Willy Tarreau58975672014-04-24 21:13:57 +02005293 chunk_appendf(&trash, "; path=/");
5294 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005295
Willy Tarreau58975672014-04-24 21:13:57 +02005296 if (s->be->cookie_domain)
5297 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005298
Willy Tarreau58975672014-04-24 21:13:57 +02005299 if (s->be->ck_opts & PR_CK_HTTPONLY)
5300 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005301
Willy Tarreau58975672014-04-24 21:13:57 +02005302 if (s->be->ck_opts & PR_CK_SECURE)
5303 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005304
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005305 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005306 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005307
Willy Tarreau58975672014-04-24 21:13:57 +02005308 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005309 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005310 /* the server did not change, only the date was updated */
5311 txn->flags |= TX_SCK_UPDATED;
5312 else
5313 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005314
Willy Tarreau58975672014-04-24 21:13:57 +02005315 /* Here, we will tell an eventual cache on the client side that we don't
5316 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5317 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5318 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005319 */
Willy Tarreau58975672014-04-24 21:13:57 +02005320 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005321
Willy Tarreau58975672014-04-24 21:13:57 +02005322 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005323
Willy Tarreau58975672014-04-24 21:13:57 +02005324 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5325 "Cache-control: private", 22) < 0))
5326 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005327 }
Willy Tarreau58975672014-04-24 21:13:57 +02005328 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005329
Willy Tarreau58975672014-04-24 21:13:57 +02005330 /*
5331 * Check if result will be cacheable with a cookie.
5332 * We'll block the response if security checks have caught
5333 * nasty things such as a cacheable cookie.
5334 */
5335 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5336 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5337 (s->be->options & PR_O_CHK_CACHE)) {
5338 /* we're in presence of a cacheable response containing
5339 * a set-cookie header. We'll block it as requested by
5340 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005341 */
Willy Tarreau58975672014-04-24 21:13:57 +02005342 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005343 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005344
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005345 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5346 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005347 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005348 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005349
Christopher Faulet767a84b2017-11-24 16:50:31 +01005350 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5351 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005352 send_log(s->be, LOG_ALERT,
5353 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5354 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5355 goto return_srv_prx_502;
5356 }
Willy Tarreau03945942009-12-22 16:50:27 +01005357
Willy Tarreau70730dd2014-04-24 18:06:27 +02005358 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005359 /*
5360 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5361 * If an "Upgrade" token is found, the header is left untouched in order
5362 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005363 * if anything but "Upgrade" is present in the Connection header. We don't
5364 * want to touch any 101 response either since it's switching to another
5365 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005366 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005367 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005368 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005369 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005370
Willy Tarreau58975672014-04-24 21:13:57 +02005371 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5372 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5373 /* we want a keep-alive response here. Keep-alive header
5374 * required if either side is not 1.1.
5375 */
5376 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5377 want_flags |= TX_CON_KAL_SET;
5378 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005379 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005380 /* we want a close response here. Close header required if
5381 * the server is 1.1, regardless of the client.
5382 */
5383 if (msg->flags & HTTP_MSGF_VER_11)
5384 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005385 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005386
Willy Tarreau58975672014-04-24 21:13:57 +02005387 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5388 http_change_connection_header(txn, msg, want_flags);
5389 }
5390
5391 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005392 /* Always enter in the body analyzer */
5393 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5394 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005395
Willy Tarreau58975672014-04-24 21:13:57 +02005396 /* if the user wants to log as soon as possible, without counting
5397 * bytes from the server, then this is the right moment. We have
5398 * to temporarily assign bytes_out to log what we currently have.
5399 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005400 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005401 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5402 s->logs.bytes_out = txn->rsp.eoh;
5403 s->do_log(s);
5404 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005405 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005406 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005407}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005408
Willy Tarreaud98cf932009-12-27 22:54:55 +01005409/* This function is an analyser which forwards response body (including chunk
5410 * sizes if any). It is called as soon as we must forward, even if we forward
5411 * zero byte. The only situation where it must not be called is when we're in
5412 * tunnel mode and we want to forward till the close. It's used both to forward
5413 * remaining data and to resync after end of body. It expects the msg_state to
5414 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005415 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005416 *
5417 * It is capable of compressing response data both in content-length mode and
5418 * in chunked mode. The state machines follows different flows depending on
5419 * whether content-length and chunked modes are used, since there are no
5420 * trailers in content-length :
5421 *
5422 * chk-mode cl-mode
5423 * ,----- BODY -----.
5424 * / \
5425 * V size > 0 V chk-mode
5426 * .--> SIZE -------------> DATA -------------> CRLF
5427 * | | size == 0 | last byte |
5428 * | v final crlf v inspected |
5429 * | TRAILERS -----------> DONE |
5430 * | |
5431 * `----------------------------------------------'
5432 *
5433 * Compression only happens in the DATA state, and must be flushed in final
5434 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5435 * is performed at once on final states for all bytes parsed, or when leaving
5436 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005437 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005438int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005439{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005440 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005441 struct http_txn *txn = s->txn;
5442 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005443 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005444
Christopher Faulet45073512018-07-20 10:16:29 +02005445 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 +02005446 now_ms, __FUNCTION__,
5447 s,
5448 res,
5449 res->rex, res->wex,
5450 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005451 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005452 res->analysers);
5453
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005454 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5455 return 0;
5456
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005457 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005458 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005459 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005460 /* Output closed while we were sending data. We must abort and
5461 * wake the other side up.
5462 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005463 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005464 msg->msg_state = HTTP_MSG_ERROR;
5465 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005466 return 1;
5467 }
5468
Willy Tarreau4fe41902010-06-07 22:27:41 +02005469 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005470 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005471
Christopher Fauletd7c91962015-04-30 11:48:27 +02005472 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005473 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5474 ? HTTP_MSG_CHUNK_SIZE
5475 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005476 }
5477
Willy Tarreauefdf0942014-04-24 20:08:57 +02005478 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005479 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005480 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005481 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005482 }
5483
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005484 if (msg->msg_state < HTTP_MSG_DONE) {
5485 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5486 ? http_msg_forward_chunked_body(s, msg)
5487 : http_msg_forward_body(s, msg));
5488 if (!ret)
5489 goto missing_data_or_waiting;
5490 if (ret < 0)
5491 goto return_bad_res;
5492 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005493
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005494 /* other states, DONE...TUNNEL */
5495 /* for keep-alive we don't want to forward closes on DONE */
5496 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5497 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5498 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005499
Christopher Faulet894da4c2017-07-18 11:29:07 +02005500 http_resync_states(s);
5501 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005502 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5503 if (res->flags & CF_SHUTW) {
5504 /* response errors are most likely due to the
5505 * client aborting the transfer. */
5506 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005507 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005508 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005509 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005510 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005511 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005512 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005513 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005514 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005515
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005516 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005517 if (res->flags & CF_SHUTW)
5518 goto aborted_xfer;
5519
5520 /* stop waiting for data if the input is closed before the end. If the
5521 * client side was already closed, it means that the client has aborted,
5522 * so we don't want to count this as a server abort. Otherwise it's a
5523 * server abort.
5524 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005525 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005526 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005527 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005528 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005529 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005530 if (!(s->flags & SF_ERR_MASK))
5531 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005532 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005533 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005534 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005535 goto return_bad_res_stats_ok;
5536 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005537 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005538
Willy Tarreau40dba092010-03-04 18:14:51 +01005539 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005540 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005541 goto return_bad_res;
5542
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005543 /* When TE: chunked is used, we need to get there again to parse
5544 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005545 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5546 * or if there are filters registered on the stream, we don't want to
5547 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005548 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005549 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005550 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005551 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5552 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005553 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005554
Willy Tarreau5c620922011-05-11 19:56:11 +02005555 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005556 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005557 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005558 * modes are already handled by the stream sock layer. We must not do
5559 * this in content-length mode because it could present the MSG_MORE
5560 * flag with the last block of forwarded data, which would cause an
5561 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005562 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005563 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005564 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005565
Willy Tarreau87b09662015-04-03 00:22:06 +02005566 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005567 return 0;
5568
Willy Tarreau40dba092010-03-04 18:14:51 +01005569 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005570 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005571 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005572 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005573
5574 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005575 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005576 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005577 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005578 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005579 res->analysers &= AN_RES_FLT_END;
5580 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 +01005581 if (objt_server(s->target))
5582 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005583
Willy Tarreaue7dff022015-04-03 01:14:29 +02005584 if (!(s->flags & SF_ERR_MASK))
5585 s->flags |= SF_ERR_PRXCOND;
5586 if (!(s->flags & SF_FINST_MASK))
5587 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005588 return 0;
5589
5590 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005591 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005592 txn->rsp.msg_state = HTTP_MSG_ERROR;
5593 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005594 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005595 res->analysers &= AN_RES_FLT_END;
5596 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 +01005597
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005598 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5599 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005600 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005601 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005602
Willy Tarreaue7dff022015-04-03 01:14:29 +02005603 if (!(s->flags & SF_ERR_MASK))
5604 s->flags |= SF_ERR_CLICL;
5605 if (!(s->flags & SF_FINST_MASK))
5606 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005607 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005608}
5609
5610
Christopher Faulet10079f52018-10-03 15:17:28 +02005611int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005612{
5613 struct channel *chn = msg->chn;
5614 int ret;
5615
5616 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5617
5618 if (msg->msg_state == HTTP_MSG_ENDING)
5619 goto ending;
5620
5621 /* Neither content-length, nor transfer-encoding was found, so we must
5622 * read the body until the server connection is closed. In that case, we
5623 * eat data as they come. Of course, this happens for response only. */
5624 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005625 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005626 msg->chunk_len += len;
5627 msg->body_len += len;
5628 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005629 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005630 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005631 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005632 msg->next += ret;
5633 msg->chunk_len -= ret;
5634 if (msg->chunk_len) {
5635 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005636 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005637 chn->flags |= CF_WAKE_WRITE;
5638 goto missing_data_or_waiting;
5639 }
5640
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005641 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5642 * always set for a request. */
5643 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5644 /* The server still sending data that should be filtered */
5645 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5646 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005647 msg->msg_state = HTTP_MSG_TUNNEL;
5648 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005649 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005650
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005651 msg->msg_state = HTTP_MSG_ENDING;
5652
5653 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005654 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005655 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005656 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5657 /* default_ret */ msg->next,
5658 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005659 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005660 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005661 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5662 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005663 if (msg->next)
5664 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005665
Christopher Fauletda02e172015-12-04 09:25:05 +01005666 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5667 /* default_ret */ 1,
5668 /* on_error */ goto error,
5669 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005670 if (msg->msg_state == HTTP_MSG_ENDING)
5671 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005672 return 1;
5673
5674 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005675 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005676 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5677 /* default_ret */ msg->next,
5678 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005679 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005680 msg->next -= ret;
5681 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5682 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005683 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005684 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005685 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005686 return 0;
5687 error:
5688 return -1;
5689}
5690
Christopher Faulet10079f52018-10-03 15:17:28 +02005691int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005692{
5693 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005694 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005695 int ret;
5696
5697 /* Here we have the guarantee to be in one of the following state:
5698 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5699 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5700
Christopher Fauletca874b82018-09-20 11:31:01 +02005701 if (msg->msg_state == HTTP_MSG_ENDING)
5702 goto ending;
5703
5704 /* Don't parse chunks if there is no input data */
5705 if (!ci_data(chn))
5706 goto waiting;
5707
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005708 switch_states:
5709 switch (msg->msg_state) {
5710 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005711 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005712 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005713 /* on_error */ goto error);
5714 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005715 msg->chunk_len -= ret;
5716 if (msg->chunk_len) {
5717 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005718 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005719 chn->flags |= CF_WAKE_WRITE;
5720 goto missing_data_or_waiting;
5721 }
5722
5723 /* nothing left to forward for this chunk*/
5724 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5725 /* fall through for HTTP_MSG_CHUNK_CRLF */
5726
5727 case HTTP_MSG_CHUNK_CRLF:
5728 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005729 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005730 if (ret == 0)
5731 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005732 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005733 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005734 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005735 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005736 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005737 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005738 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005739 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5740 /* fall through for HTTP_MSG_CHUNK_SIZE */
5741
5742 case HTTP_MSG_CHUNK_SIZE:
5743 /* read the chunk size and assign it to ->chunk_len,
5744 * then set ->next to point to the body and switch to
5745 * DATA or TRAILERS state.
5746 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005747 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005748 if (ret == 0)
5749 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005750 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005751 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005752 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005753 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005754 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005755 }
5756
5757 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005758 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005759 msg->chunk_len = chunk;
5760 msg->body_len += chunk;
5761
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005762 if (msg->chunk_len) {
5763 msg->msg_state = HTTP_MSG_DATA;
5764 goto switch_states;
5765 }
5766 msg->msg_state = HTTP_MSG_TRAILERS;
5767 /* fall through for HTTP_MSG_TRAILERS */
5768
5769 case HTTP_MSG_TRAILERS:
5770 ret = http_forward_trailers(msg);
5771 if (ret < 0)
5772 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005773 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5774 /* default_ret */ 1,
5775 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005776 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005777 if (!ret)
5778 goto missing_data_or_waiting;
5779 break;
5780
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005781 default:
5782 /* This should no happen in this function */
5783 goto error;
5784 }
5785
5786 msg->msg_state = HTTP_MSG_ENDING;
5787 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005788 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005789 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005790 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005791 /* default_ret */ msg->next,
5792 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005793 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005794 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005795 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5796 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005797 if (msg->next)
5798 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005799
Christopher Fauletda02e172015-12-04 09:25:05 +01005800 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005801 /* default_ret */ 1,
5802 /* on_error */ goto error,
5803 /* on_wait */ goto waiting);
5804 msg->msg_state = HTTP_MSG_DONE;
5805 return 1;
5806
5807 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005808 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005809 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005810 /* default_ret */ msg->next,
5811 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005812 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005813 msg->next -= ret;
5814 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5815 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005816 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005817 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005818 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005819 return 0;
5820
5821 chunk_parsing_error:
5822 if (msg->err_pos >= 0) {
5823 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005824 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005825 msg->msg_state, strm_fe(s));
5826 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005827 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005828 msg, msg->msg_state, s->be);
5829 }
5830 error:
5831 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005832}
5833
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005834
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005835/* Iterate the same filter through all request headers.
5836 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005837 * Since it can manage the switch to another backend, it updates the per-proxy
5838 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005839 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005840int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005841{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005842 char *cur_ptr, *cur_end, *cur_next;
5843 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005844 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005845 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005846 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005847
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005848 last_hdr = 0;
5849
Willy Tarreauf37954d2018-06-15 18:31:02 +02005850 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005851 old_idx = 0;
5852
5853 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005854 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005855 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005856 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005857 (exp->action == ACT_ALLOW ||
5858 exp->action == ACT_DENY ||
5859 exp->action == ACT_TARPIT))
5860 return 0;
5861
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005862 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005863 if (!cur_idx)
5864 break;
5865
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005866 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005867 cur_ptr = cur_next;
5868 cur_end = cur_ptr + cur_hdr->len;
5869 cur_next = cur_end + cur_hdr->cr + 1;
5870
5871 /* Now we have one header between cur_ptr and cur_end,
5872 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005873 */
5874
Willy Tarreau15a53a42015-01-21 13:39:42 +01005875 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005876 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005877 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005878 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005879 last_hdr = 1;
5880 break;
5881
5882 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005883 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005884 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005885 break;
5886
5887 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005888 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005889 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005890 break;
5891
5892 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005893 len = exp_replace(trash.area,
5894 trash.size, cur_ptr,
5895 exp->replace, pmatch);
5896 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005897 return -1;
5898
Willy Tarreau6e27be12018-08-22 04:46:47 +02005899 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5900
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005901 /* FIXME: if the user adds a newline in the replacement, the
5902 * index will not be recalculated for now, and the new line
5903 * will not be counted as a new header.
5904 */
5905
5906 cur_end += delta;
5907 cur_next += delta;
5908 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005909 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005910 break;
5911
5912 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005913 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005914 cur_next += delta;
5915
Willy Tarreaufa355d42009-11-29 18:12:29 +01005916 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005917 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5918 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005919 cur_hdr->len = 0;
5920 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005921 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005922 break;
5923
5924 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005925 }
5926
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005927 /* keep the link from this header to next one in case of later
5928 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005929 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005930 old_idx = cur_idx;
5931 }
5932 return 0;
5933}
5934
5935
5936/* Apply the filter to the request line.
5937 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5938 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005939 * Since it can manage the switch to another backend, it updates the per-proxy
5940 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005941 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005942int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005943{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005944 char *cur_ptr, *cur_end;
5945 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005946 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005947 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005948
Willy Tarreau3d300592007-03-18 18:34:41 +01005949 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005950 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005951 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005952 (exp->action == ACT_ALLOW ||
5953 exp->action == ACT_DENY ||
5954 exp->action == ACT_TARPIT))
5955 return 0;
5956 else if (exp->action == ACT_REMOVE)
5957 return 0;
5958
5959 done = 0;
5960
Willy Tarreauf37954d2018-06-15 18:31:02 +02005961 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005962 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005963
5964 /* Now we have the request line between cur_ptr and cur_end */
5965
Willy Tarreau15a53a42015-01-21 13:39:42 +01005966 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005967 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005968 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005969 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005970 done = 1;
5971 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005972
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005973 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005974 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005975 done = 1;
5976 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005977
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005978 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005979 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005980 done = 1;
5981 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005982
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005983 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005984 len = exp_replace(trash.area, trash.size,
5985 cur_ptr, exp->replace, pmatch);
5986 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005987 return -1;
5988
Willy Tarreau6e27be12018-08-22 04:46:47 +02005989 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5990
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005991 /* FIXME: if the user adds a newline in the replacement, the
5992 * index will not be recalculated for now, and the new line
5993 * will not be counted as a new header.
5994 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005995
Willy Tarreaufa355d42009-11-29 18:12:29 +01005996 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005997 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005998 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005999 HTTP_MSG_RQMETH,
6000 cur_ptr, cur_end + 1,
6001 NULL, NULL);
6002 if (unlikely(!cur_end))
6003 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006004
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006005 /* we have a full request and we know that we have either a CR
6006 * or an LF at <ptr>.
6007 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006008 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6009 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006010 /* there is no point trying this regex on headers */
6011 return 1;
6012 }
6013 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006014 return done;
6015}
Willy Tarreau97de6242006-12-27 17:18:38 +01006016
Willy Tarreau58f10d72006-12-04 02:26:12 +01006017
Willy Tarreau58f10d72006-12-04 02:26:12 +01006018
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006019/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006020 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006021 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006022 * unparsable request. Since it can manage the switch to another backend, it
6023 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006024 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006025int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006026{
Willy Tarreau192252e2015-04-04 01:47:55 +02006027 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006028 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006029 struct hdr_exp *exp;
6030
6031 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006032 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006033
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006034 /*
6035 * The interleaving of transformations and verdicts
6036 * makes it difficult to decide to continue or stop
6037 * the evaluation.
6038 */
6039
Willy Tarreau6c123b12010-01-28 20:22:06 +01006040 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6041 break;
6042
Willy Tarreau3d300592007-03-18 18:34:41 +01006043 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006044 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006045 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006046 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006047
6048 /* if this filter had a condition, evaluate it now and skip to
6049 * next filter if the condition does not match.
6050 */
6051 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006052 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006053 ret = acl_pass(ret);
6054 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6055 ret = !ret;
6056
6057 if (!ret)
6058 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006059 }
6060
6061 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006062 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006063 if (unlikely(ret < 0))
6064 return -1;
6065
6066 if (likely(ret == 0)) {
6067 /* The filter did not match the request, it can be
6068 * iterated through all headers.
6069 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006070 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6071 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006072 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006073 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006074 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006075}
6076
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006077
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006078/* Delete a value in a header between delimiters <from> and <next> in buffer
6079 * <buf>. The number of characters displaced is returned, and the pointer to
6080 * the first delimiter is updated if required. The function tries as much as
6081 * possible to respect the following principles :
6082 * - replace <from> delimiter by the <next> one unless <from> points to a
6083 * colon, in which case <next> is simply removed
6084 * - set exactly one space character after the new first delimiter, unless
6085 * there are not enough characters in the block being moved to do so.
6086 * - remove unneeded spaces before the previous delimiter and after the new
6087 * one.
6088 *
6089 * It is the caller's responsibility to ensure that :
6090 * - <from> points to a valid delimiter or the colon ;
6091 * - <next> points to a valid delimiter or the final CR/LF ;
6092 * - there are non-space chars before <from> ;
6093 * - there is a CR/LF at or after <next>.
6094 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006095int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006096{
6097 char *prev = *from;
6098
6099 if (*prev == ':') {
6100 /* We're removing the first value, preserve the colon and add a
6101 * space if possible.
6102 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006103 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006104 next++;
6105 prev++;
6106 if (prev < next)
6107 *prev++ = ' ';
6108
Willy Tarreau2235b262016-11-05 15:50:20 +01006109 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006110 next++;
6111 } else {
6112 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006113 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006114 prev--;
6115 *from = prev;
6116
6117 /* copy the delimiter and if possible a space if we're
6118 * not at the end of the line.
6119 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006120 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006121 *prev++ = *next++;
6122 if (prev + 1 < next)
6123 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006124 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006125 next++;
6126 }
6127 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006128 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006129}
6130
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006131/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006132 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006133 * desirable to call it only when needed. This code is quite complex because
6134 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6135 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006136 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006137void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006138{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006139 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006140 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006141 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006142 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006143 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6144 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006145
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006146 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006147 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006148 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006149
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006150 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006151 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006152 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006153
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006154 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006155 hdr_beg = hdr_next;
6156 hdr_end = hdr_beg + cur_hdr->len;
6157 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006158
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006159 /* We have one full header between hdr_beg and hdr_end, and the
6160 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006161 * "Cookie:" headers.
6162 */
6163
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006164 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006165 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006166 old_idx = cur_idx;
6167 continue;
6168 }
6169
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006170 del_from = NULL; /* nothing to be deleted */
6171 preserve_hdr = 0; /* assume we may kill the whole header */
6172
Willy Tarreau58f10d72006-12-04 02:26:12 +01006173 /* Now look for cookies. Conforming to RFC2109, we have to support
6174 * attributes whose name begin with a '$', and associate them with
6175 * the right cookie, if we want to delete this cookie.
6176 * So there are 3 cases for each cookie read :
6177 * 1) it's a special attribute, beginning with a '$' : ignore it.
6178 * 2) it's a server id cookie that we *MAY* want to delete : save
6179 * some pointers on it (last semi-colon, beginning of cookie...)
6180 * 3) it's an application cookie : we *MAY* have to delete a previous
6181 * "special" cookie.
6182 * At the end of loop, if a "special" cookie remains, we may have to
6183 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006184 * *MUST* delete it.
6185 *
6186 * Note: RFC2965 is unclear about the processing of spaces around
6187 * the equal sign in the ATTR=VALUE form. A careful inspection of
6188 * the RFC explicitly allows spaces before it, and not within the
6189 * tokens (attrs or values). An inspection of RFC2109 allows that
6190 * too but section 10.1.3 lets one think that spaces may be allowed
6191 * after the equal sign too, resulting in some (rare) buggy
6192 * implementations trying to do that. So let's do what servers do.
6193 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6194 * allowed quoted strings in values, with any possible character
6195 * after a backslash, including control chars and delimitors, which
6196 * causes parsing to become ambiguous. Browsers also allow spaces
6197 * within values even without quotes.
6198 *
6199 * We have to keep multiple pointers in order to support cookie
6200 * removal at the beginning, middle or end of header without
6201 * corrupting the header. All of these headers are valid :
6202 *
6203 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6204 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6205 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6206 * | | | | | | | | |
6207 * | | | | | | | | hdr_end <--+
6208 * | | | | | | | +--> next
6209 * | | | | | | +----> val_end
6210 * | | | | | +-----------> val_beg
6211 * | | | | +--------------> equal
6212 * | | | +----------------> att_end
6213 * | | +---------------------> att_beg
6214 * | +--------------------------> prev
6215 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006216 */
6217
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006218 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6219 /* Iterate through all cookies on this line */
6220
6221 /* find att_beg */
6222 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006223 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006224 att_beg++;
6225
6226 /* find att_end : this is the first character after the last non
6227 * space before the equal. It may be equal to hdr_end.
6228 */
6229 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006230
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006231 while (equal < hdr_end) {
6232 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006233 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006234 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006235 continue;
6236 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006237 }
6238
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006239 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6240 * is between <att_beg> and <equal>, both may be identical.
6241 */
6242
6243 /* look for end of cookie if there is an equal sign */
6244 if (equal < hdr_end && *equal == '=') {
6245 /* look for the beginning of the value */
6246 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006247 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006248 val_beg++;
6249
6250 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006251 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006252
6253 /* make val_end point to the first white space or delimitor after the value */
6254 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006255 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 val_end--;
6257 } else {
6258 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006259 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006260
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006261 /* We have nothing to do with attributes beginning with '$'. However,
6262 * they will automatically be removed if a header before them is removed,
6263 * since they're supposed to be linked together.
6264 */
6265 if (*att_beg == '$')
6266 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006267
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006268 /* Ignore cookies with no equal sign */
6269 if (equal == next) {
6270 /* This is not our cookie, so we must preserve it. But if we already
6271 * scheduled another cookie for removal, we cannot remove the
6272 * complete header, but we can remove the previous block itself.
6273 */
6274 preserve_hdr = 1;
6275 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006276 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006277 val_end += delta;
6278 next += delta;
6279 hdr_end += delta;
6280 hdr_next += delta;
6281 cur_hdr->len += delta;
6282 http_msg_move_end(&txn->req, delta);
6283 prev = del_from;
6284 del_from = NULL;
6285 }
6286 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006287 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006288
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006289 /* if there are spaces around the equal sign, we need to
6290 * strip them otherwise we'll get trouble for cookie captures,
6291 * or even for rewrites. Since this happens extremely rarely,
6292 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006293 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006294 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6295 int stripped_before = 0;
6296 int stripped_after = 0;
6297
6298 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006299 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006300 equal += stripped_before;
6301 val_beg += stripped_before;
6302 }
6303
6304 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006305 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006306 val_beg += stripped_after;
6307 stripped_before += stripped_after;
6308 }
6309
6310 val_end += stripped_before;
6311 next += stripped_before;
6312 hdr_end += stripped_before;
6313 hdr_next += stripped_before;
6314 cur_hdr->len += stripped_before;
6315 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006316 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006317 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006318
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006319 /* First, let's see if we want to capture this cookie. We check
6320 * that we don't already have a client side cookie, because we
6321 * can only capture one. Also as an optimisation, we ignore
6322 * cookies shorter than the declared name.
6323 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006324 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6325 (val_end - att_beg >= sess->fe->capture_namelen) &&
6326 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006327 int log_len = val_end - att_beg;
6328
Willy Tarreaubafbe012017-11-24 17:34:44 +01006329 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006330 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006331 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006332 if (log_len > sess->fe->capture_len)
6333 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006334 memcpy(txn->cli_cookie, att_beg, log_len);
6335 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006336 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006337 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006338
Willy Tarreaubca99692010-10-06 19:25:55 +02006339 /* Persistence cookies in passive, rewrite or insert mode have the
6340 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006341 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006342 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006343 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006344 * For cookies in prefix mode, the form is :
6345 *
6346 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006347 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006348 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6349 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6350 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006351 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006352
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006353 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6354 * have the server ID between val_beg and delim, and the original cookie between
6355 * delim+1 and val_end. Otherwise, delim==val_end :
6356 *
6357 * Cookie: NAME=SRV; # in all but prefix modes
6358 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6359 * | || || | |+-> next
6360 * | || || | +--> val_end
6361 * | || || +---------> delim
6362 * | || |+------------> val_beg
6363 * | || +-------------> att_end = equal
6364 * | |+-----------------> att_beg
6365 * | +------------------> prev
6366 * +-------------------------> hdr_beg
6367 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006368
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006369 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006370 for (delim = val_beg; delim < val_end; delim++)
6371 if (*delim == COOKIE_DELIM)
6372 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006373 } else {
6374 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006375 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006376 /* Now check if the cookie contains a date field, which would
6377 * appear after a vertical bar ('|') just after the server name
6378 * and before the delimiter.
6379 */
6380 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6381 if (vbar1) {
6382 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006383 * right is the last seen date. It is a base64 encoded
6384 * 30-bit value representing the UNIX date since the
6385 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006386 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006387 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006388 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006389 if (val_end - vbar1 >= 5) {
6390 val = b64tos30(vbar1);
6391 if (val > 0)
6392 txn->cookie_last_date = val << 2;
6393 }
6394 /* look for a second vertical bar */
6395 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6396 if (vbar1 && (val_end - vbar1 > 5)) {
6397 val = b64tos30(vbar1 + 1);
6398 if (val > 0)
6399 txn->cookie_first_date = val << 2;
6400 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006401 }
6402 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006403
Willy Tarreauf64d1412010-10-07 20:06:11 +02006404 /* if the cookie has an expiration date and the proxy wants to check
6405 * it, then we do that now. We first check if the cookie is too old,
6406 * then only if it has expired. We detect strict overflow because the
6407 * time resolution here is not great (4 seconds). Cookies with dates
6408 * in the future are ignored if their offset is beyond one day. This
6409 * allows an admin to fix timezone issues without expiring everyone
6410 * and at the same time avoids keeping unwanted side effects for too
6411 * long.
6412 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006413 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6414 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006415 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006416 txn->flags &= ~TX_CK_MASK;
6417 txn->flags |= TX_CK_OLD;
6418 delim = val_beg; // let's pretend we have not found the cookie
6419 txn->cookie_first_date = 0;
6420 txn->cookie_last_date = 0;
6421 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006422 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6423 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006424 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006425 txn->flags &= ~TX_CK_MASK;
6426 txn->flags |= TX_CK_EXPIRED;
6427 delim = val_beg; // let's pretend we have not found the cookie
6428 txn->cookie_first_date = 0;
6429 txn->cookie_last_date = 0;
6430 }
6431
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006432 /* Here, we'll look for the first running server which supports the cookie.
6433 * This allows to share a same cookie between several servers, for example
6434 * to dedicate backup servers to specific servers only.
6435 * However, to prevent clients from sticking to cookie-less backup server
6436 * when they have incidentely learned an empty cookie, we simply ignore
6437 * empty cookies and mark them as invalid.
6438 * The same behaviour is applied when persistence must be ignored.
6439 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006440 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006441 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006442
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006443 while (srv) {
6444 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6445 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006446 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006447 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006448 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006449 /* we found the server and we can use it */
6450 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006451 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006452 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006453 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006454 break;
6455 } else {
6456 /* we found a server, but it's down,
6457 * mark it as such and go on in case
6458 * another one is available.
6459 */
6460 txn->flags &= ~TX_CK_MASK;
6461 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006462 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006463 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006464 srv = srv->next;
6465 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006466
Willy Tarreauf64d1412010-10-07 20:06:11 +02006467 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006468 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006469 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006470 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006471 txn->flags |= TX_CK_UNUSED;
6472 else
6473 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006474 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006475
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006476 /* depending on the cookie mode, we may have to either :
6477 * - delete the complete cookie if we're in insert+indirect mode, so that
6478 * the server never sees it ;
6479 * - remove the server id from the cookie value, and tag the cookie as an
6480 * application cookie so that it does not get accidentely removed later,
6481 * if we're in cookie prefix mode
6482 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006483 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006484 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006485
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006486 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006487 val_end += delta;
6488 next += delta;
6489 hdr_end += delta;
6490 hdr_next += delta;
6491 cur_hdr->len += delta;
6492 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006493
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006494 del_from = NULL;
6495 preserve_hdr = 1; /* we want to keep this cookie */
6496 }
6497 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006498 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006499 del_from = prev;
6500 }
6501 } else {
6502 /* This is not our cookie, so we must preserve it. But if we already
6503 * scheduled another cookie for removal, we cannot remove the
6504 * complete header, but we can remove the previous block itself.
6505 */
6506 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006507
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006508 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006509 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006510 if (att_beg >= del_from)
6511 att_beg += delta;
6512 if (att_end >= del_from)
6513 att_end += delta;
6514 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006515 val_end += delta;
6516 next += delta;
6517 hdr_end += delta;
6518 hdr_next += delta;
6519 cur_hdr->len += delta;
6520 http_msg_move_end(&txn->req, delta);
6521 prev = del_from;
6522 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006523 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006524 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006525
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006526 /* continue with next cookie on this header line */
6527 att_beg = next;
6528 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006529
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006530 /* There are no more cookies on this line.
6531 * We may still have one (or several) marked for deletion at the
6532 * end of the line. We must do this now in two ways :
6533 * - if some cookies must be preserved, we only delete from the
6534 * mark to the end of line ;
6535 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006536 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006537 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006538 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006539 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006540 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006541 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006542 cur_hdr->len += delta;
6543 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006544 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006545
6546 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006547 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6548 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006549 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006550 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006551 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006552 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006553 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006554 }
6555
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006556 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006557 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006558 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006559}
6560
6561
Willy Tarreaua15645d2007-03-18 16:22:39 +01006562/* Iterate the same filter through all response headers contained in <rtr>.
6563 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6564 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006565int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006566{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006567 char *cur_ptr, *cur_end, *cur_next;
6568 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006569 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006570 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006571 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006572
6573 last_hdr = 0;
6574
Willy Tarreauf37954d2018-06-15 18:31:02 +02006575 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006576 old_idx = 0;
6577
6578 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006579 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006580 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006581 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006582 (exp->action == ACT_ALLOW ||
6583 exp->action == ACT_DENY))
6584 return 0;
6585
6586 cur_idx = txn->hdr_idx.v[old_idx].next;
6587 if (!cur_idx)
6588 break;
6589
6590 cur_hdr = &txn->hdr_idx.v[cur_idx];
6591 cur_ptr = cur_next;
6592 cur_end = cur_ptr + cur_hdr->len;
6593 cur_next = cur_end + cur_hdr->cr + 1;
6594
6595 /* Now we have one header between cur_ptr and cur_end,
6596 * and the next header starts at cur_next.
6597 */
6598
Willy Tarreau15a53a42015-01-21 13:39:42 +01006599 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006600 switch (exp->action) {
6601 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006602 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006603 last_hdr = 1;
6604 break;
6605
6606 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006607 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006608 last_hdr = 1;
6609 break;
6610
6611 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006612 len = exp_replace(trash.area,
6613 trash.size, cur_ptr,
6614 exp->replace, pmatch);
6615 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006616 return -1;
6617
Willy Tarreau6e27be12018-08-22 04:46:47 +02006618 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6619
Willy Tarreaua15645d2007-03-18 16:22:39 +01006620 /* FIXME: if the user adds a newline in the replacement, the
6621 * index will not be recalculated for now, and the new line
6622 * will not be counted as a new header.
6623 */
6624
6625 cur_end += delta;
6626 cur_next += delta;
6627 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006628 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629 break;
6630
6631 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006632 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006633 cur_next += delta;
6634
Willy Tarreaufa355d42009-11-29 18:12:29 +01006635 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006636 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6637 txn->hdr_idx.used--;
6638 cur_hdr->len = 0;
6639 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006640 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641 break;
6642
6643 }
6644 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006645
6646 /* keep the link from this header to next one in case of later
6647 * removal of next header.
6648 */
6649 old_idx = cur_idx;
6650 }
6651 return 0;
6652}
6653
6654
6655/* Apply the filter to the status line in the response buffer <rtr>.
6656 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6657 * or -1 if a replacement resulted in an invalid status line.
6658 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006659int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006660{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006661 char *cur_ptr, *cur_end;
6662 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006663 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006664 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006665
Willy Tarreau3d300592007-03-18 18:34:41 +01006666 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006667 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006668 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006669 (exp->action == ACT_ALLOW ||
6670 exp->action == ACT_DENY))
6671 return 0;
6672 else if (exp->action == ACT_REMOVE)
6673 return 0;
6674
6675 done = 0;
6676
Willy Tarreauf37954d2018-06-15 18:31:02 +02006677 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006678 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006679
6680 /* Now we have the status line between cur_ptr and cur_end */
6681
Willy Tarreau15a53a42015-01-21 13:39:42 +01006682 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006683 switch (exp->action) {
6684 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006685 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006686 done = 1;
6687 break;
6688
6689 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006690 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006691 done = 1;
6692 break;
6693
6694 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006695 len = exp_replace(trash.area, trash.size,
6696 cur_ptr, exp->replace, pmatch);
6697 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006698 return -1;
6699
Willy Tarreau6e27be12018-08-22 04:46:47 +02006700 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6701
Willy Tarreaua15645d2007-03-18 16:22:39 +01006702 /* FIXME: if the user adds a newline in the replacement, the
6703 * index will not be recalculated for now, and the new line
6704 * will not be counted as a new header.
6705 */
6706
Willy Tarreaufa355d42009-11-29 18:12:29 +01006707 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006709 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006710 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006711 cur_ptr, cur_end + 1,
6712 NULL, NULL);
6713 if (unlikely(!cur_end))
6714 return -1;
6715
6716 /* we have a full respnse and we know that we have either a CR
6717 * or an LF at <ptr>.
6718 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006719 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006720 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006721 /* there is no point trying this regex on headers */
6722 return 1;
6723 }
6724 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006725 return done;
6726}
6727
6728
6729
6730/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006731 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006732 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6733 * unparsable response.
6734 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006735int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006736{
Willy Tarreau192252e2015-04-04 01:47:55 +02006737 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006738 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006739 struct hdr_exp *exp;
6740
6741 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006742 int ret;
6743
6744 /*
6745 * The interleaving of transformations and verdicts
6746 * makes it difficult to decide to continue or stop
6747 * the evaluation.
6748 */
6749
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006750 if (txn->flags & TX_SVDENY)
6751 break;
6752
Willy Tarreau3d300592007-03-18 18:34:41 +01006753 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006754 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6755 exp->action == ACT_PASS)) {
6756 exp = exp->next;
6757 continue;
6758 }
6759
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006760 /* if this filter had a condition, evaluate it now and skip to
6761 * next filter if the condition does not match.
6762 */
6763 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006764 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006765 ret = acl_pass(ret);
6766 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6767 ret = !ret;
6768 if (!ret)
6769 continue;
6770 }
6771
Willy Tarreaua15645d2007-03-18 16:22:39 +01006772 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006773 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006774 if (unlikely(ret < 0))
6775 return -1;
6776
6777 if (likely(ret == 0)) {
6778 /* The filter did not match the response, it can be
6779 * iterated through all headers.
6780 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006781 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6782 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006783 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006784 }
6785 return 0;
6786}
6787
6788
Willy Tarreaua15645d2007-03-18 16:22:39 +01006789/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006790 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006791 * desirable to call it only when needed. This function is also used when we
6792 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006793 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006794void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006795{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006796 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006797 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006798 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006799 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006800 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006801 char *hdr_beg, *hdr_end, *hdr_next;
6802 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803
Willy Tarreaua15645d2007-03-18 16:22:39 +01006804 /* Iterate through the headers.
6805 * we start with the start line.
6806 */
6807 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006808 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006809
6810 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6811 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006812 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006813
6814 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006815 hdr_beg = hdr_next;
6816 hdr_end = hdr_beg + cur_hdr->len;
6817 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006818
Willy Tarreau24581ba2010-08-31 22:39:35 +02006819 /* We have one full header between hdr_beg and hdr_end, and the
6820 * next header starts at hdr_next. We're only interested in
6821 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006822 */
6823
Willy Tarreau24581ba2010-08-31 22:39:35 +02006824 is_cookie2 = 0;
6825 prev = hdr_beg + 10;
6826 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006827 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006828 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6829 if (!val) {
6830 old_idx = cur_idx;
6831 continue;
6832 }
6833 is_cookie2 = 1;
6834 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006835 }
6836
Willy Tarreau24581ba2010-08-31 22:39:35 +02006837 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6838 * <prev> points to the colon.
6839 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006840 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841
Willy Tarreau24581ba2010-08-31 22:39:35 +02006842 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6843 * check-cache is enabled) and we are not interested in checking
6844 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006845 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006846 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006847 return;
6848
Willy Tarreau24581ba2010-08-31 22:39:35 +02006849 /* OK so now we know we have to process this response cookie.
6850 * The format of the Set-Cookie header is slightly different
6851 * from the format of the Cookie header in that it does not
6852 * support the comma as a cookie delimiter (thus the header
6853 * cannot be folded) because the Expires attribute described in
6854 * the original Netscape's spec may contain an unquoted date
6855 * with a comma inside. We have to live with this because
6856 * many browsers don't support Max-Age and some browsers don't
6857 * support quoted strings. However the Set-Cookie2 header is
6858 * clean.
6859 *
6860 * We have to keep multiple pointers in order to support cookie
6861 * removal at the beginning, middle or end of header without
6862 * corrupting the header (in case of set-cookie2). A special
6863 * pointer, <scav> points to the beginning of the set-cookie-av
6864 * fields after the first semi-colon. The <next> pointer points
6865 * either to the end of line (set-cookie) or next unquoted comma
6866 * (set-cookie2). All of these headers are valid :
6867 *
6868 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6869 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6870 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6871 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6872 * | | | | | | | | | |
6873 * | | | | | | | | +-> next hdr_end <--+
6874 * | | | | | | | +------------> scav
6875 * | | | | | | +--------------> val_end
6876 * | | | | | +--------------------> val_beg
6877 * | | | | +----------------------> equal
6878 * | | | +------------------------> att_end
6879 * | | +----------------------------> att_beg
6880 * | +------------------------------> prev
6881 * +-----------------------------------------> hdr_beg
6882 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006883
Willy Tarreau24581ba2010-08-31 22:39:35 +02006884 for (; prev < hdr_end; prev = next) {
6885 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006886
Willy Tarreau24581ba2010-08-31 22:39:35 +02006887 /* find att_beg */
6888 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006889 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006890 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006891
Willy Tarreau24581ba2010-08-31 22:39:35 +02006892 /* find att_end : this is the first character after the last non
6893 * space before the equal. It may be equal to hdr_end.
6894 */
6895 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006896
Willy Tarreau24581ba2010-08-31 22:39:35 +02006897 while (equal < hdr_end) {
6898 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6899 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006900 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006901 continue;
6902 att_end = equal;
6903 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006904
Willy Tarreau24581ba2010-08-31 22:39:35 +02006905 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6906 * is between <att_beg> and <equal>, both may be identical.
6907 */
6908
6909 /* look for end of cookie if there is an equal sign */
6910 if (equal < hdr_end && *equal == '=') {
6911 /* look for the beginning of the value */
6912 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006913 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006914 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006915
Willy Tarreau24581ba2010-08-31 22:39:35 +02006916 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006917 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006918
6919 /* make val_end point to the first white space or delimitor after the value */
6920 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006921 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006922 val_end--;
6923 } else {
6924 /* <equal> points to next comma, semi-colon or EOL */
6925 val_beg = val_end = next = equal;
6926 }
6927
6928 if (next < hdr_end) {
6929 /* Set-Cookie2 supports multiple cookies, and <next> points to
6930 * a colon or semi-colon before the end. So skip all attr-value
6931 * pairs and look for the next comma. For Set-Cookie, since
6932 * commas are permitted in values, skip to the end.
6933 */
6934 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006935 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006936 else
6937 next = hdr_end;
6938 }
6939
6940 /* Now everything is as on the diagram above */
6941
6942 /* Ignore cookies with no equal sign */
6943 if (equal == val_end)
6944 continue;
6945
6946 /* If there are spaces around the equal sign, we need to
6947 * strip them otherwise we'll get trouble for cookie captures,
6948 * or even for rewrites. Since this happens extremely rarely,
6949 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006950 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006951 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6952 int stripped_before = 0;
6953 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006954
Willy Tarreau24581ba2010-08-31 22:39:35 +02006955 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006956 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006957 equal += stripped_before;
6958 val_beg += stripped_before;
6959 }
6960
6961 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006962 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006963 val_beg += stripped_after;
6964 stripped_before += stripped_after;
6965 }
6966
6967 val_end += stripped_before;
6968 next += stripped_before;
6969 hdr_end += stripped_before;
6970 hdr_next += stripped_before;
6971 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006972 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006973 }
6974
6975 /* First, let's see if we want to capture this cookie. We check
6976 * that we don't already have a server side cookie, because we
6977 * can only capture one. Also as an optimisation, we ignore
6978 * cookies shorter than the declared name.
6979 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006980 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006981 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006982 (val_end - att_beg >= sess->fe->capture_namelen) &&
6983 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006984 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006985 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006986 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01006987 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006988 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006989 if (log_len > sess->fe->capture_len)
6990 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01006991 memcpy(txn->srv_cookie, att_beg, log_len);
6992 txn->srv_cookie[log_len] = 0;
6993 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994 }
6995
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006996 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006997 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006998 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006999 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7000 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007001 /* assume passive cookie by default */
7002 txn->flags &= ~TX_SCK_MASK;
7003 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007004
7005 /* If the cookie is in insert mode on a known server, we'll delete
7006 * this occurrence because we'll insert another one later.
7007 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007008 * a direct access.
7009 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007010 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007011 /* The "preserve" flag was set, we don't want to touch the
7012 * server's cookie.
7013 */
7014 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007015 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007016 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007017 /* this cookie must be deleted */
7018 if (*prev == ':' && next == hdr_end) {
7019 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007020 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007021 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7022 txn->hdr_idx.used--;
7023 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007024 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007025 hdr_next += delta;
7026 http_msg_move_end(&txn->rsp, delta);
7027 /* note: while both invalid now, <next> and <hdr_end>
7028 * are still equal, so the for() will stop as expected.
7029 */
7030 } else {
7031 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007032 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007033 next = prev;
7034 hdr_end += delta;
7035 hdr_next += delta;
7036 cur_hdr->len += delta;
7037 http_msg_move_end(&txn->rsp, delta);
7038 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007039 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007040 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007041 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007042 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007043 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007044 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007045 * with this server since we know it.
7046 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007047 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007048 next += delta;
7049 hdr_end += delta;
7050 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007051 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007052 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007053
Willy Tarreauf1348312010-10-07 15:54:11 +02007054 txn->flags &= ~TX_SCK_MASK;
7055 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007056 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007057 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007058 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007059 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007060 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007061 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007062 next += delta;
7063 hdr_end += delta;
7064 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007065 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007066 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007067
Willy Tarreau827aee92011-03-10 16:55:02 +01007068 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007069 txn->flags &= ~TX_SCK_MASK;
7070 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007071 }
7072 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007073 /* that's done for this cookie, check the next one on the same
7074 * line when next != hdr_end (only if is_cookie2).
7075 */
7076 }
7077 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007078 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007079 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007080}
7081
7082
Willy Tarreaua15645d2007-03-18 16:22:39 +01007083/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007084 * Parses the Cache-Control and Pragma request header fields to determine if
7085 * the request may be served from the cache and/or if it is cacheable. Updates
7086 * s->txn->flags.
7087 */
7088void check_request_for_cacheability(struct stream *s, struct channel *chn)
7089{
7090 struct http_txn *txn = s->txn;
7091 char *p1, *p2;
7092 char *cur_ptr, *cur_end, *cur_next;
7093 int pragma_found;
7094 int cc_found;
7095 int cur_idx;
7096
7097 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7098 return; /* nothing more to do here */
7099
7100 cur_idx = 0;
7101 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007102 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007103
7104 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7105 struct hdr_idx_elem *cur_hdr;
7106 int val;
7107
7108 cur_hdr = &txn->hdr_idx.v[cur_idx];
7109 cur_ptr = cur_next;
7110 cur_end = cur_ptr + cur_hdr->len;
7111 cur_next = cur_end + cur_hdr->cr + 1;
7112
7113 /* We have one full header between cur_ptr and cur_end, and the
7114 * next header starts at cur_next.
7115 */
7116
7117 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7118 if (val) {
7119 if ((cur_end - (cur_ptr + val) >= 8) &&
7120 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7121 pragma_found = 1;
7122 continue;
7123 }
7124 }
7125
William Lallemand8a16fe02018-05-22 11:04:33 +02007126 /* Don't use the cache and don't try to store if we found the
7127 * Authorization header */
7128 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7129 if (val) {
7130 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7131 txn->flags |= TX_CACHE_IGNORE;
7132 continue;
7133 }
7134
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007135 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7136 if (!val)
7137 continue;
7138
7139 /* OK, right now we know we have a cache-control header at cur_ptr */
7140 cc_found = 1;
7141 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7142
7143 if (p1 >= cur_end) /* no more info */
7144 continue;
7145
7146 /* p1 is at the beginning of the value */
7147 p2 = p1;
7148 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7149 p2++;
7150
7151 /* we have a complete value between p1 and p2. We don't check the
7152 * values after max-age, max-stale nor min-fresh, we simply don't
7153 * use the cache when they're specified.
7154 */
7155 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7156 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7157 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7158 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7159 txn->flags |= TX_CACHE_IGNORE;
7160 continue;
7161 }
7162
7163 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7164 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7165 continue;
7166 }
7167 }
7168
7169 /* RFC7234#5.4:
7170 * When the Cache-Control header field is also present and
7171 * understood in a request, Pragma is ignored.
7172 * When the Cache-Control header field is not present in a
7173 * request, caches MUST consider the no-cache request
7174 * pragma-directive as having the same effect as if
7175 * "Cache-Control: no-cache" were present.
7176 */
7177 if (!cc_found && pragma_found)
7178 txn->flags |= TX_CACHE_IGNORE;
7179}
7180
7181/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007182 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007183 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007184void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007186 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007187 char *p1, *p2;
7188
7189 char *cur_ptr, *cur_end, *cur_next;
7190 int cur_idx;
7191
Willy Tarreau12b32f22017-12-21 16:08:09 +01007192 if (txn->status < 200) {
7193 /* do not try to cache interim responses! */
7194 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007195 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007196 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007197
7198 /* Iterate through the headers.
7199 * we start with the start line.
7200 */
7201 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007202 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203
7204 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7205 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007206 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007207
7208 cur_hdr = &txn->hdr_idx.v[cur_idx];
7209 cur_ptr = cur_next;
7210 cur_end = cur_ptr + cur_hdr->len;
7211 cur_next = cur_end + cur_hdr->cr + 1;
7212
7213 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007214 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007215 */
7216
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007217 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7218 if (val) {
7219 if ((cur_end - (cur_ptr + val) >= 8) &&
7220 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7221 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7222 return;
7223 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007224 }
7225
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007226 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7227 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007228 continue;
7229
7230 /* OK, right now we know we have a cache-control header at cur_ptr */
7231
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007232 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233
7234 if (p1 >= cur_end) /* no more info */
7235 continue;
7236
7237 /* p1 is at the beginning of the value */
7238 p2 = p1;
7239
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007240 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007241 p2++;
7242
7243 /* we have a complete value between p1 and p2 */
7244 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007245 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7246 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7247 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7248 continue;
7249 }
7250
Willy Tarreaua15645d2007-03-18 16:22:39 +01007251 /* we have something of the form no-cache="set-cookie" */
7252 if ((cur_end - p1 >= 21) &&
7253 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7254 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007255 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007256 continue;
7257 }
7258
7259 /* OK, so we know that either p2 points to the end of string or to a comma */
7260 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007261 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007262 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007263 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007264 return;
7265 }
7266
7267 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007268 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007269 continue;
7270 }
7271 }
7272}
7273
Willy Tarreau58f10d72006-12-04 02:26:12 +01007274
Willy Tarreaub2513902006-12-17 14:52:38 +01007275/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007276 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007277 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007278 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007279 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007280 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007281 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007282 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007283 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007284int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007285{
7286 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007287 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007288 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007289
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007290 if (!uri_auth)
7291 return 0;
7292
Cyril Bonté70be45d2010-10-12 00:14:35 +02007293 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007294 return 0;
7295
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007296 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007297 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007298 return 0;
7299
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007300 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007301 return 0;
7302
Willy Tarreaub2513902006-12-17 14:52:38 +01007303 return 1;
7304}
7305
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007306/* Append the description of what is present in error snapshot <es> into <out>.
7307 * The description must be small enough to always fit in a trash. The output
7308 * buffer may be the trash so the trash must not be used inside this function.
7309 */
7310void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7311{
7312 chunk_appendf(&trash,
7313 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7314 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7315 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7316 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7317 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7318 es->ctx.http.m_flags, es->ctx.http.m_clen,
7319 es->ctx.http.m_blen, es->ctx.http.b_flags);
7320}
7321
Willy Tarreau4076a152009-04-02 15:18:36 +02007322/*
7323 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007324 * By default it tries to report the error position as msg->err_pos. However if
7325 * this one is not set, it will then report msg->next, which is the last known
7326 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007327 * displays buffers as a contiguous area starting at buf->p. The direction is
7328 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007329 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007330void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007331 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007332 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007333{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007334 union error_snapshot_ctx ctx;
7335 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007336
Willy Tarreauef3ca732018-09-07 15:47:35 +02007337 /* http-specific part now */
7338 ctx.http.sid = s->uniq_id;
7339 ctx.http.state = state;
7340 ctx.http.b_flags = msg->chn->flags;
7341 ctx.http.s_flags = s->flags;
7342 ctx.http.t_flags = s->txn->flags;
7343 ctx.http.m_flags = msg->flags;
7344 ctx.http.m_clen = msg->chunk_len;
7345 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007346
Willy Tarreauef3ca732018-09-07 15:47:35 +02007347 ofs = msg->chn->total - ci_data(msg->chn);
7348 if (ofs < 0)
7349 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007350
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007351 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007352 other_end, s->target,
7353 strm_sess(s), &msg->chn->buf,
7354 ofs, co_data(msg->chn),
7355 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7356 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007357}
Willy Tarreaub2513902006-12-17 14:52:38 +01007358
Willy Tarreau294c4732011-12-16 21:35:50 +01007359/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7360 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7361 * performed over the whole headers. Otherwise it must contain a valid header
7362 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7363 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7364 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7365 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007366 * -1. The value fetch stops at commas, so this function is suited for use with
7367 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007368 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007369 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007370unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007371 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007372 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007373{
Willy Tarreau294c4732011-12-16 21:35:50 +01007374 struct hdr_ctx local_ctx;
7375 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007376 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007377 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007378 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007379
Willy Tarreau294c4732011-12-16 21:35:50 +01007380 if (!ctx) {
7381 local_ctx.idx = 0;
7382 ctx = &local_ctx;
7383 }
7384
Willy Tarreaubce70882009-09-07 11:51:47 +02007385 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007386 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007387 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007388 occ--;
7389 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007390 *vptr = ctx->line + ctx->val;
7391 *vlen = ctx->vlen;
7392 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007393 }
7394 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007395 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007396 }
7397
7398 /* negative occurrence, we scan all the list then walk back */
7399 if (-occ > MAX_HDR_HISTORY)
7400 return 0;
7401
Willy Tarreau294c4732011-12-16 21:35:50 +01007402 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007403 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007404 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7405 len_hist[hist_ptr] = ctx->vlen;
7406 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007407 hist_ptr = 0;
7408 found++;
7409 }
7410 if (-occ > found)
7411 return 0;
7412 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007413 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7414 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7415 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007416 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007417 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007418 if (hist_ptr >= MAX_HDR_HISTORY)
7419 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007420 *vptr = ptr_hist[hist_ptr];
7421 *vlen = len_hist[hist_ptr];
7422 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007423}
7424
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007425/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7426 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7427 * performed over the whole headers. Otherwise it must contain a valid header
7428 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7429 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7430 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7431 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7432 * -1. This function differs from http_get_hdr() in that it only returns full
7433 * line header values and does not stop at commas.
7434 * The return value is 0 if nothing was found, or non-zero otherwise.
7435 */
7436unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7437 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007438 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007439{
7440 struct hdr_ctx local_ctx;
7441 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007442 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007443 unsigned int hist_ptr;
7444 int found;
7445
7446 if (!ctx) {
7447 local_ctx.idx = 0;
7448 ctx = &local_ctx;
7449 }
7450
7451 if (occ >= 0) {
7452 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007453 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007454 occ--;
7455 if (occ <= 0) {
7456 *vptr = ctx->line + ctx->val;
7457 *vlen = ctx->vlen;
7458 return 1;
7459 }
7460 }
7461 return 0;
7462 }
7463
7464 /* negative occurrence, we scan all the list then walk back */
7465 if (-occ > MAX_HDR_HISTORY)
7466 return 0;
7467
7468 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007469 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007470 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7471 len_hist[hist_ptr] = ctx->vlen;
7472 if (++hist_ptr >= MAX_HDR_HISTORY)
7473 hist_ptr = 0;
7474 found++;
7475 }
7476 if (-occ > found)
7477 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007478
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007479 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007480 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7481 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7482 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007483 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007484 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007485 if (hist_ptr >= MAX_HDR_HISTORY)
7486 hist_ptr -= MAX_HDR_HISTORY;
7487 *vptr = ptr_hist[hist_ptr];
7488 *vlen = len_hist[hist_ptr];
7489 return 1;
7490}
7491
Willy Tarreaubaaee002006-06-26 02:48:02 +02007492/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007493 * Print a debug line with a header. Always stop at the first CR or LF char,
7494 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7495 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007496 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007497void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007498{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007499 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007500 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007501
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007502 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007503 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007504 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007505 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 +02007506
7507 for (max = 0; start + max < end; max++)
7508 if (start[max] == '\r' || start[max] == '\n')
7509 break;
7510
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007511 UBOUND(max, trash.size - trash.data - 3);
7512 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7513 trash.area[trash.data++] = '\n';
7514 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007515}
7516
Willy Tarreaueee5b512015-04-03 23:46:31 +02007517
7518/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7519 * The hdr_idx is allocated as well. In case of allocation failure, everything
7520 * allocated is freed and NULL is returned. Otherwise the new transaction is
7521 * assigned to the stream and returned.
7522 */
7523struct http_txn *http_alloc_txn(struct stream *s)
7524{
7525 struct http_txn *txn = s->txn;
7526
7527 if (txn)
7528 return txn;
7529
Willy Tarreaubafbe012017-11-24 17:34:44 +01007530 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007531 if (!txn)
7532 return txn;
7533
7534 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007535 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007536 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007537 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007538 return NULL;
7539 }
7540
7541 s->txn = txn;
7542 return txn;
7543}
7544
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007545void http_txn_reset_req(struct http_txn *txn)
7546{
7547 txn->req.flags = 0;
7548 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7549 txn->req.next = 0;
7550 txn->req.chunk_len = 0LL;
7551 txn->req.body_len = 0LL;
7552 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7553}
7554
7555void http_txn_reset_res(struct http_txn *txn)
7556{
7557 txn->rsp.flags = 0;
7558 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7559 txn->rsp.next = 0;
7560 txn->rsp.chunk_len = 0LL;
7561 txn->rsp.body_len = 0LL;
7562 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7563}
7564
Willy Tarreau0937bc42009-12-22 15:03:09 +01007565/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007566 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007567 * the required fields are properly allocated and that we only need to (re)init
7568 * them. This should be used before processing any new request.
7569 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007570void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007571{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007572 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007573 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007574
7575 txn->flags = 0;
7576 txn->status = -1;
7577
Willy Tarreauf64d1412010-10-07 20:06:11 +02007578 txn->cookie_first_date = 0;
7579 txn->cookie_last_date = 0;
7580
Willy Tarreaueee5b512015-04-03 23:46:31 +02007581 txn->srv_cookie = NULL;
7582 txn->cli_cookie = NULL;
7583 txn->uri = NULL;
7584
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007585 http_txn_reset_req(txn);
7586 http_txn_reset_res(txn);
7587
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007588 txn->req.chn = &s->req;
7589 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007590
7591 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007592
7593 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7594 if (fe->options2 & PR_O2_REQBUG_OK)
7595 txn->req.err_pos = -1; /* let buggy requests pass */
7596
Willy Tarreau0937bc42009-12-22 15:03:09 +01007597 if (txn->hdr_idx.v)
7598 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007599
7600 vars_init(&s->vars_txn, SCOPE_TXN);
7601 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007602}
7603
7604/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007605void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007606{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007607 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007608 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007609
7610 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007611 pool_free(pool_head_requri, txn->uri);
7612 pool_free(pool_head_capture, txn->cli_cookie);
7613 pool_free(pool_head_capture, txn->srv_cookie);
7614 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007615
William Lallemanda73203e2012-03-12 12:48:57 +01007616 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007617 txn->uri = NULL;
7618 txn->srv_cookie = NULL;
7619 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007620
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007621 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007622 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007623 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007624 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007625 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007626 }
7627
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007628 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007629 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007630 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007631 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007632 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007633 }
7634
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007635 if (!LIST_ISEMPTY(&s->vars_txn.head))
7636 vars_prune(&s->vars_txn, s->sess, s);
7637 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7638 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007639}
7640
7641/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007642void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007643{
7644 http_end_txn(s);
7645 http_init_txn(s);
7646
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007647 /* reinitialise the current rule list pointer to NULL. We are sure that
7648 * any rulelist match the NULL pointer.
7649 */
7650 s->current_rule_list = NULL;
7651
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007652 s->be = strm_fe(s);
7653 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007654 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007655 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007656 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007657 /* re-init store persistence */
7658 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007659 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007660
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007661 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007662
Willy Tarreau739cfba2010-01-25 23:11:14 +01007663 /* We must trim any excess data from the response buffer, because we
7664 * may have blocked an invalid response from a server that we don't
7665 * want to accidentely forward once we disable the analysers, nor do
7666 * we want those data to come along with next response. A typical
7667 * example of such data would be from a buggy server responding to
7668 * a HEAD with some data, or sending more than the advertised
7669 * content-length.
7670 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007671 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007672 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007673
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007674 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007675 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007676
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007677 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007678 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007679
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007680 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007681 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007682
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007683 s->req.rex = TICK_ETERNITY;
7684 s->req.wex = TICK_ETERNITY;
7685 s->req.analyse_exp = TICK_ETERNITY;
7686 s->res.rex = TICK_ETERNITY;
7687 s->res.wex = TICK_ETERNITY;
7688 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007689 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007690}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007691
Willy Tarreau79e57332018-10-02 16:01:16 +02007692/* This function executes one of the set-{method,path,query,uri} actions. It
7693 * takes the string from the variable 'replace' with length 'len', then modifies
7694 * the relevant part of the request line accordingly. Then it updates various
7695 * pointers to the next elements which were moved, and the total buffer length.
7696 * It finds the action to be performed in p[2], previously filled by function
7697 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7698 * error, though this can be revisited when this code is finally exploited.
7699 *
7700 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7701 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007702 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007703 * In query string case, the mark question '?' must be set at the start of the
7704 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007705 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007706int http_replace_req_line(int action, const char *replace, int len,
7707 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007708{
Willy Tarreau79e57332018-10-02 16:01:16 +02007709 struct http_txn *txn = s->txn;
7710 char *cur_ptr, *cur_end;
7711 int offset = 0;
7712 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007713
Willy Tarreau79e57332018-10-02 16:01:16 +02007714 switch (action) {
7715 case 0: // method
7716 cur_ptr = ci_head(&s->req);
7717 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007718
Willy Tarreau79e57332018-10-02 16:01:16 +02007719 /* adjust req line offsets and lengths */
7720 delta = len - offset - (cur_end - cur_ptr);
7721 txn->req.sl.rq.m_l += delta;
7722 txn->req.sl.rq.u += delta;
7723 txn->req.sl.rq.v += delta;
7724 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007725
Willy Tarreau79e57332018-10-02 16:01:16 +02007726 case 1: // path
7727 cur_ptr = http_txn_get_path(txn);
7728 if (!cur_ptr)
7729 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007730
Willy Tarreau79e57332018-10-02 16:01:16 +02007731 cur_end = cur_ptr;
7732 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7733 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007734
Willy Tarreau79e57332018-10-02 16:01:16 +02007735 /* adjust req line offsets and lengths */
7736 delta = len - offset - (cur_end - cur_ptr);
7737 txn->req.sl.rq.u_l += delta;
7738 txn->req.sl.rq.v += delta;
7739 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007740
Willy Tarreau79e57332018-10-02 16:01:16 +02007741 case 2: // query
7742 offset = 1;
7743 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7744 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7745 while (cur_ptr < cur_end && *cur_ptr != '?')
7746 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007747
Willy Tarreau79e57332018-10-02 16:01:16 +02007748 /* skip the question mark or indicate that we must insert it
7749 * (but only if the format string is not empty then).
7750 */
7751 if (cur_ptr < cur_end)
7752 cur_ptr++;
7753 else if (len > 1)
7754 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007755
Willy Tarreau79e57332018-10-02 16:01:16 +02007756 /* adjust req line offsets and lengths */
7757 delta = len - offset - (cur_end - cur_ptr);
7758 txn->req.sl.rq.u_l += delta;
7759 txn->req.sl.rq.v += delta;
7760 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007761
Willy Tarreau79e57332018-10-02 16:01:16 +02007762 case 3: // uri
7763 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7764 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007765
Willy Tarreau79e57332018-10-02 16:01:16 +02007766 /* adjust req line offsets and lengths */
7767 delta = len - offset - (cur_end - cur_ptr);
7768 txn->req.sl.rq.u_l += delta;
7769 txn->req.sl.rq.v += delta;
7770 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007771
Willy Tarreau79e57332018-10-02 16:01:16 +02007772 default:
7773 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007774 }
7775
Willy Tarreau79e57332018-10-02 16:01:16 +02007776 /* commit changes and adjust end of message */
7777 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7778 txn->req.sl.rq.l += delta;
7779 txn->hdr_idx.v[0].len += delta;
7780 http_msg_move_end(&txn->req, delta);
7781 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007782}
7783
Willy Tarreau79e57332018-10-02 16:01:16 +02007784/* This function replace the HTTP status code and the associated message. The
7785 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007786 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007787void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007788{
Willy Tarreau79e57332018-10-02 16:01:16 +02007789 struct http_txn *txn = s->txn;
7790 char *cur_ptr, *cur_end;
7791 int delta;
7792 char *res;
7793 int c_l;
7794 const char *msg = reason;
7795 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007796
Willy Tarreau79e57332018-10-02 16:01:16 +02007797 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007798
Willy Tarreau79e57332018-10-02 16:01:16 +02007799 res = ultoa_o(status, trash.area, trash.size);
7800 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007801
Willy Tarreau79e57332018-10-02 16:01:16 +02007802 trash.area[c_l] = ' ';
7803 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007804
Willy Tarreau79e57332018-10-02 16:01:16 +02007805 /* Do we have a custom reason format string? */
7806 if (msg == NULL)
7807 msg = http_get_reason(status);
7808 msg_len = strlen(msg);
7809 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7810 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007811
Willy Tarreau79e57332018-10-02 16:01:16 +02007812 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7813 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007814
Willy Tarreau79e57332018-10-02 16:01:16 +02007815 /* commit changes and adjust message */
7816 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7817 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007818
Willy Tarreau79e57332018-10-02 16:01:16 +02007819 /* adjust res line offsets and lengths */
7820 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7821 txn->rsp.sl.st.c_l = c_l;
7822 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007823
Willy Tarreau79e57332018-10-02 16:01:16 +02007824 delta = trash.data - (cur_end - cur_ptr);
7825 txn->rsp.sl.st.l += delta;
7826 txn->hdr_idx.v[0].len += delta;
7827 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007828}
7829
Willy Tarreau8797c062007-05-07 00:55:35 +02007830__attribute__((constructor))
7831static void __http_protocol_init(void)
7832{
Willy Tarreau8797c062007-05-07 00:55:35 +02007833}
7834
7835
Willy Tarreau58f10d72006-12-04 02:26:12 +01007836/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007837 * Local variables:
7838 * c-indent-level: 8
7839 * c-basic-offset: 8
7840 * End:
7841 */