blob: 0ce03b3c15504d1c665eabb9be7aa300404ca09d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Frédéric Lécaillea41d5312018-01-29 12:05:07 +010029#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010043#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020044#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <types/global.h>
William Lallemand71bd11a2017-11-20 19:13:14 +010046#include <types/cache.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreau8797c062007-05-07 00:55:35 +020049#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020050#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020051#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010052#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020054#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010055#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010056#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020057#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010058#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020060#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020061#include <proto/frontend.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020062#include <proto/h1.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010064#include <proto/hdr_idx.h>
Patrick Hemmere3faf022018-08-22 10:02:00 -040065#include <proto/hlua.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010066#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020067#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010069#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020070#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020071#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010072#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020073#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010074#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020075#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020076#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020077#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020078
Cyril Bonté19979e12012-04-04 12:57:21 +020079/* status codes available for the stats admin page (strictly 4 chars length) */
80const char *stat_status_codes[STAT_STATUS_SIZE] = {
81 [STAT_STATUS_DENY] = "DENY",
82 [STAT_STATUS_DONE] = "DONE",
83 [STAT_STATUS_ERRP] = "ERRP",
84 [STAT_STATUS_EXCD] = "EXCD",
85 [STAT_STATUS_NONE] = "NONE",
86 [STAT_STATUS_PART] = "PART",
87 [STAT_STATUS_UNKN] = "UNKN",
88};
89
Willy Tarreau80587432006-12-24 17:47:20 +010090void init_proto_http()
91{
Willy Tarreau332f8bf2007-05-13 21:36:56 +020092 /* memory allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +010093 pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
94 pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +010095}
Willy Tarreaubaaee002006-06-26 02:48:02 +020096
Willy Tarreau53b6c742006-12-17 13:37:46 +010097/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +010098 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +020099 * the last CRLF. <len> bytes are copied, not counting the CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100100 * The header is also automatically added to the index <hdr_idx>, and the end
101 * of headers is automatically adjusted. The number of bytes added is returned
102 * on success, otherwise <0 is returned indicating an error.
103 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100104int http_header_add_tail2(struct http_msg *msg,
105 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100106{
107 int bytes;
108
Willy Tarreau4d893d42018-07-12 15:43:32 +0200109 bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100110 if (!bytes)
111 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100112 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100113 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
114}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200116/* Find the first or next occurrence of header <name> in message buffer <sol>
117 * using headers index <idx>, and return it in the <ctx> structure. This
118 * structure holds everything necessary to use the header and find next
119 * occurrence. If its <idx> member is 0, the header is searched from the
120 * beginning. Otherwise, the next occurrence is returned. The function returns
121 * 1 when it finds a value, and 0 when there is no more. It is very similar to
122 * http_find_header2() except that it is designed to work with full-line headers
123 * whose comma is not a delimiter but is part of the syntax. As a special case,
124 * if ctx->val is NULL when searching for a new values of a header, the current
125 * header is rescanned. This allows rescanning after a header deletion.
126 */
127int http_find_full_header2(const char *name, int len,
128 char *sol, struct hdr_idx *idx,
129 struct hdr_ctx *ctx)
130{
131 char *eol, *sov;
132 int cur_idx, old_idx;
133
134 cur_idx = ctx->idx;
135 if (cur_idx) {
136 /* We have previously returned a header, let's search another one */
137 sol = ctx->line;
138 eol = sol + idx->v[cur_idx].len;
139 goto next_hdr;
140 }
141
142 /* first request for this header */
143 sol += hdr_idx_first_pos(idx);
144 old_idx = 0;
145 cur_idx = hdr_idx_first_idx(idx);
146 while (cur_idx) {
147 eol = sol + idx->v[cur_idx].len;
148
149 if (len == 0) {
150 /* No argument was passed, we want any header.
151 * To achieve this, we simply build a fake request. */
152 while (sol + len < eol && sol[len] != ':')
153 len++;
154 name = sol;
155 }
156
157 if ((len < eol - sol) &&
158 (sol[len] == ':') &&
159 (strncasecmp(sol, name, len) == 0)) {
160 ctx->del = len;
161 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100162 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200163 sov++;
164
165 ctx->line = sol;
166 ctx->prev = old_idx;
167 ctx->idx = cur_idx;
168 ctx->val = sov - sol;
169 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100170 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200171 eol--;
172 ctx->tws++;
173 }
174 ctx->vlen = eol - sov;
175 return 1;
176 }
177 next_hdr:
178 sol = eol + idx->v[cur_idx].cr + 1;
179 old_idx = cur_idx;
180 cur_idx = idx->v[cur_idx].next;
181 }
182 return 0;
183}
184
Willy Tarreauc90dc232015-02-20 13:51:36 +0100185/* Find the first or next header field in message buffer <sol> using headers
186 * index <idx>, and return it in the <ctx> structure. This structure holds
187 * everything necessary to use the header and find next occurrence. If its
188 * <idx> member is 0, the first header is retrieved. Otherwise, the next
189 * occurrence is returned. The function returns 1 when it finds a value, and
190 * 0 when there is no more. It is equivalent to http_find_full_header2() with
191 * no header name.
192 */
193int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
194{
195 char *eol, *sov;
196 int cur_idx, old_idx;
197 int len;
198
199 cur_idx = ctx->idx;
200 if (cur_idx) {
201 /* We have previously returned a header, let's search another one */
202 sol = ctx->line;
203 eol = sol + idx->v[cur_idx].len;
204 goto next_hdr;
205 }
206
207 /* first request for this header */
208 sol += hdr_idx_first_pos(idx);
209 old_idx = 0;
210 cur_idx = hdr_idx_first_idx(idx);
211 while (cur_idx) {
212 eol = sol + idx->v[cur_idx].len;
213
214 len = 0;
215 while (1) {
216 if (len >= eol - sol)
217 goto next_hdr;
218 if (sol[len] == ':')
219 break;
220 len++;
221 }
222
223 ctx->del = len;
224 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100225 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100226 sov++;
227
228 ctx->line = sol;
229 ctx->prev = old_idx;
230 ctx->idx = cur_idx;
231 ctx->val = sov - sol;
232 ctx->tws = 0;
233
Willy Tarreau2235b262016-11-05 15:50:20 +0100234 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100235 eol--;
236 ctx->tws++;
237 }
238 ctx->vlen = eol - sov;
239 return 1;
240
241 next_hdr:
242 sol = eol + idx->v[cur_idx].cr + 1;
243 old_idx = cur_idx;
244 cur_idx = idx->v[cur_idx].next;
245 }
246 return 0;
247}
248
Willy Tarreau33a7e692007-06-10 19:45:56 +0200249/* Find the first or next occurrence of header <name> in message buffer <sol>
250 * using headers index <idx>, and return it in the <ctx> structure. This
251 * structure holds everything necessary to use the header and find next
252 * occurrence. If its <idx> member is 0, the header is searched from the
253 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100254 * 1 when it finds a value, and 0 when there is no more. It is designed to work
255 * with headers defined as comma-separated lists. As a special case, if ctx->val
256 * is NULL when searching for a new values of a header, the current header is
257 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200258 */
259int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100260 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200261 struct hdr_ctx *ctx)
262{
Willy Tarreau68085d82010-01-18 14:54:04 +0100263 char *eol, *sov;
264 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200265
Willy Tarreau68085d82010-01-18 14:54:04 +0100266 cur_idx = ctx->idx;
267 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200268 /* We have previously returned a value, let's search
269 * another one on the same line.
270 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200271 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200272 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100273 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200274 eol = sol + idx->v[cur_idx].len;
275
276 if (sov >= eol)
277 /* no more values in this header */
278 goto next_hdr;
279
Willy Tarreau68085d82010-01-18 14:54:04 +0100280 /* values remaining for this header, skip the comma but save it
281 * for later use (eg: for header deletion).
282 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200283 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100284 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200285 sov++;
286
287 goto return_hdr;
288 }
289
290 /* first request for this header */
291 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100292 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200293 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200294 while (cur_idx) {
295 eol = sol + idx->v[cur_idx].len;
296
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200297 if (len == 0) {
298 /* No argument was passed, we want any header.
299 * To achieve this, we simply build a fake request. */
300 while (sol + len < eol && sol[len] != ':')
301 len++;
302 name = sol;
303 }
304
Willy Tarreau33a7e692007-06-10 19:45:56 +0200305 if ((len < eol - sol) &&
306 (sol[len] == ':') &&
307 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100308 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200309 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100310 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200311 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100312
Willy Tarreau33a7e692007-06-10 19:45:56 +0200313 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100314 ctx->prev = old_idx;
315 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200316 ctx->idx = cur_idx;
317 ctx->val = sov - sol;
318
Willy Tarreauab813a42018-09-10 18:41:28 +0200319 eol = http_find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200320 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100321 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200322 eol--;
323 ctx->tws++;
324 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200325 ctx->vlen = eol - sov;
326 return 1;
327 }
328 next_hdr:
329 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100330 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200331 cur_idx = idx->v[cur_idx].next;
332 }
333 return 0;
334}
335
Willy Tarreau68085d82010-01-18 14:54:04 +0100336/* Remove one value of a header. This only works on a <ctx> returned by one of
337 * the http_find_header functions. The value is removed, as well as surrounding
338 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100339 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100340 * message <msg>. The new index is returned. If it is zero, it means there is
341 * no more header, so any processing may stop. The ctx is always left in a form
342 * that can be handled by http_find_header2() to find next occurrence.
343 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100344int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100345{
346 int cur_idx = ctx->idx;
347 char *sol = ctx->line;
348 struct hdr_idx_elem *hdr;
349 int delta, skip_comma;
350
351 if (!cur_idx)
352 return 0;
353
354 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200355 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100356 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200357 delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100358 http_msg_move_end(msg, delta);
359 idx->used--;
360 hdr->len = 0; /* unused entry */
361 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100362 if (idx->tail == ctx->idx)
363 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100364 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100365 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100366 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200367 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100368 return ctx->idx;
369 }
370
371 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200372 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
373 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100374 */
375
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200376 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200377 delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200378 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100379 NULL, 0);
380 hdr->len += delta;
381 http_msg_move_end(msg, delta);
382 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200383 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100384 return ctx->idx;
385}
386
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100387/* This function handles a server error at the stream interface level. The
388 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100389 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100390 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100391 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200393static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +0200394 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395{
Willy Tarreau2019f952017-03-14 11:07:31 +0100396 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100397 channel_auto_read(si_oc(si));
398 channel_abort(si_oc(si));
399 channel_auto_close(si_oc(si));
400 channel_erase(si_oc(si));
401 channel_auto_close(si_ic(si));
402 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200403 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200404 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200405 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200406 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200407 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200408 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409}
410
Willy Tarreau87b09662015-04-03 00:22:06 +0200411/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100412 * and message.
413 */
414
Willy Tarreau83061a82018-07-13 11:56:34 +0200415struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100416{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200417 const int msgnum = http_get_status_idx(s->txn->status);
418
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200419 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200420 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200421 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200422 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100423 else
424 return &http_err_chunks[msgnum];
425}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200426
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100427void
Willy Tarreau83061a82018-07-13 11:56:34 +0200428http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100429{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200430 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100431 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100432 stream_int_retnclose(&s->si[0], msg);
433}
434
Willy Tarreau21d2af32008-02-14 20:25:24 +0100435/* Parse the URI from the given transaction (which is assumed to be in request
436 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
437 * It is returned otherwise.
438 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200439char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100440{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200441 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100442
Willy Tarreau9d9ccdb2018-10-28 20:13:12 +0100443 if (!txn->req.chn->buf.size)
444 return NULL;
445
Willy Tarreau6b952c82018-09-10 17:45:34 +0200446 ret = http_get_path(ist2(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l));
William Lallemand65ad6e12014-01-31 15:08:02 +0100447
Willy Tarreau6b952c82018-09-10 17:45:34 +0200448 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100449}
450
Willy Tarreau71241ab2012-12-27 11:30:54 +0100451/* Returns a 302 for a redirectable request that reaches a server working in
452 * in redirect mode. This may only be called just after the stream interface
453 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
454 * follow normal proxy processing. NOTE: this function is designed to support
455 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100456 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200457void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100458{
459 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100460 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100461 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200462 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100463
464 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200465 trash.data = strlen(HTTP_302);
466 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100467
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200468 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100469
Willy Tarreauefb453c2008-10-26 20:49:47 +0100470 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200471 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100472 return;
473
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100474 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100475 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200476 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
477 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100478 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100479
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200480 /* 3: add the request URI. Since it was already forwarded, we need
481 * to temporarily rewind the buffer.
482 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200483 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200484 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200485
Willy Tarreau6b952c82018-09-10 17:45:34 +0200486 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200487 len = b_dist(&s->req.buf, path, c_ptr(&s->req, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200488
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200489 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200490
Willy Tarreauefb453c2008-10-26 20:49:47 +0100491 if (!path)
492 return;
493
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200494 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100495 return;
496
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200497 memcpy(trash.area + trash.data, path, len);
498 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100499
500 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 memcpy(trash.area + trash.data,
502 "\r\nProxy-Connection: close\r\n\r\n", 29);
503 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100504 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200505 memcpy(trash.area + trash.data,
506 "\r\nConnection: close\r\n\r\n", 23);
507 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100508 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100509
510 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200511 si_shutr(si);
512 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100513 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100514 si->state = SI_ST_CLO;
515
516 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100517 txn->status = 302;
518 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100519
520 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100521 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500522 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100523}
524
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100525/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100526 * that the server side is closed. Note that err_type is actually a
527 * bitmask, where almost only aborts may be cumulated with other
528 * values. We consider that aborted operations are more important
529 * than timeouts or errors due to the fact that nobody else in the
530 * logs might explain incomplete retries. All others should avoid
531 * being cumulated. It should normally not be possible to have multiple
532 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100533 * Note that connection errors appearing on the second request of a keep-alive
534 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100535 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200536void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100537{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100538 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100539
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200540 /* set s->txn->status for http_error_message(s) */
541 s->txn->status = 503;
542
Willy Tarreauefb453c2008-10-26 20:49:47 +0100543 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200544 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100545 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100546 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200547 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100548 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200549 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100550 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200551 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100552 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100553 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200554 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100555 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100556 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200557 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100558 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200559 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100560 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200561 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100562 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200563 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200564 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200565 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100566 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200567 http_error_message(s));
568 else { /* SI_ET_CONN_OTHER and others */
569 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200570 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100571 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200572 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100573}
574
Willy Tarreau42250582007-04-01 01:30:43 +0200575extern const char sess_term_cond[8];
576extern const char sess_fin_state[8];
577extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +0100578struct pool_head *pool_head_http_txn;
579struct pool_head *pool_head_requri;
580struct pool_head *pool_head_capture = NULL;
581struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100582
Willy Tarreau117f59e2007-03-04 18:17:17 +0100583/*
584 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200585 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100586 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200587void http_capture_headers(char *som, struct hdr_idx *idx,
588 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100589{
590 char *eol, *sol, *col, *sov;
591 int cur_idx;
592 struct cap_hdr *h;
593 int len;
594
595 sol = som + hdr_idx_first_pos(idx);
596 cur_idx = hdr_idx_first_idx(idx);
597
598 while (cur_idx) {
599 eol = sol + idx->v[cur_idx].len;
600
601 col = sol;
602 while (col < eol && *col != ':')
603 col++;
604
605 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100606 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100607 sov++;
608
609 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200610 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100611 (strncasecmp(sol, h->name, h->namelen) == 0)) {
612 if (cap[h->index] == NULL)
613 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100614 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100615
616 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100617 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100618 continue;
619 }
620
621 len = eol - sov;
622 if (len > h->len)
623 len = h->len;
624
625 memcpy(cap[h->index], sov, len);
626 cap[h->index][len]=0;
627 }
628 }
629 sol = eol + idx->v[cur_idx].cr + 1;
630 cur_idx = idx->v[cur_idx].next;
631 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100632}
633
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200634/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
635 * conversion succeeded, 0 in case of error. If the request was already 1.X,
636 * nothing is done and 1 is returned.
637 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200638int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200639{
640 int delta;
641 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100642 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200643
644 if (msg->sl.rq.v_l != 0)
645 return 1;
646
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300647 /* RFC 1945 allows only GET for HTTP/0.9 requests */
648 if (txn->meth != HTTP_METH_GET)
649 return 0;
650
Willy Tarreauf37954d2018-06-15 18:31:02 +0200651 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200652
653 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300654 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
655 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200656 }
657 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200658 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100659 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200660 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200661 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200662 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200663 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200664 NULL, NULL);
665 if (unlikely(!cur_end))
666 return 0;
667
668 /* we have a full HTTP/1.0 request now and we know that
669 * we have either a CR or an LF at <ptr>.
670 */
671 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
672 return 1;
673}
674
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100675/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100676 * and "keep-alive" values. If we already know that some headers may safely
677 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100678 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
679 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100680 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100681 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
682 * found, and TX_CON_*_SET is adjusted depending on what is left so only
683 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100684 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100685 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100686void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100687{
Willy Tarreau5b154472009-12-21 20:11:07 +0100688 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100689 const char *hdr_val = "Connection";
690 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100691
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100692 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100693 return;
694
Willy Tarreau88d349d2010-01-25 12:15:43 +0100695 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
696 hdr_val = "Proxy-Connection";
697 hdr_len = 16;
698 }
699
Willy Tarreau5b154472009-12-21 20:11:07 +0100700 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100701 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200702 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100703 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
704 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100705 if (to_del & 2)
706 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100707 else
708 txn->flags |= TX_CON_KAL_SET;
709 }
710 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
711 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100712 if (to_del & 1)
713 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100714 else
715 txn->flags |= TX_CON_CLO_SET;
716 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100717 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
718 txn->flags |= TX_HDR_CONN_UPG;
719 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100720 }
721
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100722 txn->flags |= TX_HDR_CONN_PRS;
723 return;
724}
Willy Tarreau5b154472009-12-21 20:11:07 +0100725
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100726/* Apply desired changes on the Connection: header. Values may be removed and/or
727 * added depending on the <wanted> flags, which are exclusively composed of
728 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
729 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
730 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100731void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100732{
733 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100734 const char *hdr_val = "Connection";
735 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100736
737 ctx.idx = 0;
738
Willy Tarreau88d349d2010-01-25 12:15:43 +0100739
740 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
741 hdr_val = "Proxy-Connection";
742 hdr_len = 16;
743 }
744
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100745 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200746 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100747 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
748 if (wanted & TX_CON_KAL_SET)
749 txn->flags |= TX_CON_KAL_SET;
750 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100751 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100752 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100753 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
754 if (wanted & TX_CON_CLO_SET)
755 txn->flags |= TX_CON_CLO_SET;
756 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100757 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100758 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100759 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100760
761 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
762 return;
763
764 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
765 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100766 hdr_val = "Connection: close";
767 hdr_len = 17;
768 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
769 hdr_val = "Proxy-Connection: close";
770 hdr_len = 23;
771 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100772 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100773 }
774
775 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
776 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100777 hdr_val = "Connection: keep-alive";
778 hdr_len = 22;
779 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
780 hdr_val = "Proxy-Connection: keep-alive";
781 hdr_len = 28;
782 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100783 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100784 }
785 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100786}
787
Willy Tarreau87b09662015-04-03 00:22:06 +0200788void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200789{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200790 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200791 int tmp = TX_CON_WANT_KAL;
792
Christopher Faulet315b39c2018-09-21 16:26:19 +0200793 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
794 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
795 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200796
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200797 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200798 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200799 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200800
Christopher Faulet315b39c2018-09-21 16:26:19 +0200801 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
802 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200803 tmp = TX_CON_WANT_CLO;
804
805 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
806 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
807
808 if (!(txn->flags & TX_HDR_CONN_PRS) &&
809 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
810 /* parse the Connection header and possibly clean it */
811 int to_del = 0;
812 if ((msg->flags & HTTP_MSGF_VER_11) ||
813 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200814 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200815 to_del |= 2; /* remove "keep-alive" */
816 if (!(msg->flags & HTTP_MSGF_VER_11))
817 to_del |= 1; /* remove "close" */
818 http_parse_connection_header(txn, msg, to_del);
819 }
820
821 /* check if client or config asks for explicit close in KAL/SCL */
822 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
823 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
824 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
825 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
826 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200827 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200828 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
829}
William Lallemand82fe75c2012-10-23 10:25:10 +0200830
Willy Tarreaud787e662009-07-07 10:14:51 +0200831/* This stream analyser waits for a complete HTTP request. It returns 1 if the
832 * processing can continue on next analysers, or zero if it either needs more
833 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100834 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200835 * when it has nothing left to do, and may remove any analyser when it wants to
836 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100837 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200838int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100839{
Willy Tarreau59234e92008-11-30 23:51:27 +0100840 /*
841 * We will parse the partial (or complete) lines.
842 * We will check the request syntax, and also join multi-line
843 * headers. An index of all the lines will be elaborated while
844 * parsing.
845 *
846 * For the parsing, we use a 28 states FSM.
847 *
848 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200849 * ci_head(req) = beginning of request
850 * ci_head(req) + msg->eoh = end of processed headers / start of current one
851 * ci_tail(req) = end of input data
852 * msg->eol = end of current header or line (LF or CRLF)
853 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200854 *
855 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200856 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200857 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
858 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100859 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100860
Willy Tarreau59234e92008-11-30 23:51:27 +0100861 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200862 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200863 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100864 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200865 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100866
Christopher Faulet45073512018-07-20 10:16:29 +0200867 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +0100868 now_ms, __FUNCTION__,
869 s,
870 req,
871 req->rex, req->wex,
872 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200873 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100874 req->analysers);
875
Willy Tarreau52a0c602009-08-16 22:45:38 +0200876 /* we're speaking HTTP here, so let's speak HTTP to the client */
877 s->srv_error = http_return_srv_error;
878
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300879 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200880 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300881 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
882
Willy Tarreau83e3af02009-12-28 17:39:57 +0100883 /* There's a protected area at the end of the buffer for rewriting
884 * purposes. We don't want to start to parse the request if the
885 * protected area is affected, because we may have to move processed
886 * data later, which is much more complicated.
887 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200888 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200889 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100890 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200891 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100892 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100893 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200894 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200895 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100896 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100897 return 0;
898 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200899 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200900 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200901 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100902 }
903
Willy Tarreauf37954d2018-06-15 18:31:02 +0200904 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100905 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100906 }
907
Willy Tarreau59234e92008-11-30 23:51:27 +0100908 /* 1: we might have to print this header in debug mode */
909 if (unlikely((global.mode & MODE_DEBUG) &&
910 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200911 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100912 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100913
Willy Tarreauf37954d2018-06-15 18:31:02 +0200914 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200915 /* this is a bit complex : in case of error on the request line,
916 * we know that rq.l is still zero, so we display only the part
917 * up to the end of the line (truncated by debug_hdr).
918 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200919 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100920 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100921
Willy Tarreau59234e92008-11-30 23:51:27 +0100922 sol += hdr_idx_first_pos(&txn->hdr_idx);
923 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100924
Willy Tarreau59234e92008-11-30 23:51:27 +0100925 while (cur_idx) {
926 eol = sol + txn->hdr_idx.v[cur_idx].len;
927 debug_hdr("clihdr", s, sol, eol);
928 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
929 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100930 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100931 }
932
Willy Tarreau58f10d72006-12-04 02:26:12 +0100933
Willy Tarreau59234e92008-11-30 23:51:27 +0100934 /*
935 * Now we quickly check if we have found a full valid request.
936 * If not so, we check the FD and buffer states before leaving.
937 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100938 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100939 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200940 * on a keep-alive stream, if we encounter and error, close, t/o,
941 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100942 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200943 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200944 * Last, we may increase some tracked counters' http request errors on
945 * the cases that are deliberately the client's fault. For instance,
946 * a timeout or connection reset is not counted as an error. However
947 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100948 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100949
Willy Tarreau655dce92009-11-08 13:10:58 +0100950 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100951 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100952 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100953 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100954 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200955 stream_inc_http_req_ctr(s);
956 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200957 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100958 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100959 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100960
Willy Tarreau59234e92008-11-30 23:51:27 +0100961 /* 1: Since we are in header mode, if there's no space
962 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200963 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100964 * must terminate it now.
965 */
Willy Tarreau23752332018-06-15 14:54:53 +0200966 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100967 /* FIXME: check if URI is set and return Status
968 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100969 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200970 stream_inc_http_req_ctr(s);
971 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200972 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200973 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200974 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100975 goto return_bad_req;
976 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100977
Willy Tarreau59234e92008-11-30 23:51:27 +0100978 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200979 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200980 if (!(s->flags & SF_ERR_MASK))
981 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100982
Willy Tarreaufcffa692010-01-10 14:21:19 +0100983 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100984 goto failed_keep_alive;
985
Willy Tarreau0f228a02015-05-01 15:37:53 +0200986 if (sess->fe->options & PR_O_IGNORE_PRB)
987 goto failed_keep_alive;
988
Willy Tarreau59234e92008-11-30 23:51:27 +0100989 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200990 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200991 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200992 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200993 }
994
Willy Tarreaudc979f22012-12-04 10:39:01 +0100995 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100996 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100997 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100998 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +0100999 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001000 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001001 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001002 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001003 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001004 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001005
Willy Tarreaue7dff022015-04-03 01:14:29 +02001006 if (!(s->flags & SF_FINST_MASK))
1007 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001008 return 0;
1009 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001010
Willy Tarreau59234e92008-11-30 23:51:27 +01001011 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001012 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001013 if (!(s->flags & SF_ERR_MASK))
1014 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001015
Willy Tarreaufcffa692010-01-10 14:21:19 +01001016 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001017 goto failed_keep_alive;
1018
Willy Tarreau0f228a02015-05-01 15:37:53 +02001019 if (sess->fe->options & PR_O_IGNORE_PRB)
1020 goto failed_keep_alive;
1021
Willy Tarreau59234e92008-11-30 23:51:27 +01001022 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001023 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001024 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001025 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001026 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001027 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001028 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001029 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001030 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001031 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001032
Willy Tarreau87b09662015-04-03 00:22:06 +02001033 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001034 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001035 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001036 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001037 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001038
Willy Tarreaue7dff022015-04-03 01:14:29 +02001039 if (!(s->flags & SF_FINST_MASK))
1040 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001041 return 0;
1042 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001043
Willy Tarreau59234e92008-11-30 23:51:27 +01001044 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001045 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001046 if (!(s->flags & SF_ERR_MASK))
1047 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001048
Willy Tarreaufcffa692010-01-10 14:21:19 +01001049 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001050 goto failed_keep_alive;
1051
Willy Tarreau0f228a02015-05-01 15:37:53 +02001052 if (sess->fe->options & PR_O_IGNORE_PRB)
1053 goto failed_keep_alive;
1054
Willy Tarreau4076a152009-04-02 15:18:36 +02001055 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001056 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001057 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001058 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001059 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001060 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001061 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001062 stream_inc_http_err_ctr(s);
1063 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001064 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001065 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001066 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001067 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001068
Willy Tarreaue7dff022015-04-03 01:14:29 +02001069 if (!(s->flags & SF_FINST_MASK))
1070 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001071 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001072 }
1073
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001074 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001075 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001076 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001077#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001078 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001079 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001080 /* We need more data, we have to re-enable quick-ack in case we
1081 * previously disabled it, otherwise we might cause the client
1082 * to delay next data.
1083 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001084 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001085 }
1086#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001087
Willy Tarreaufcffa692010-01-10 14:21:19 +01001088 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1089 /* If the client starts to talk, let's fall back to
1090 * request timeout processing.
1091 */
1092 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001093 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001094 }
1095
Willy Tarreau59234e92008-11-30 23:51:27 +01001096 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001097 if (!tick_isset(req->analyse_exp)) {
1098 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1099 (txn->flags & TX_WAIT_NEXT_RQ) &&
1100 tick_isset(s->be->timeout.httpka))
1101 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1102 else
1103 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1104 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001105
Willy Tarreau59234e92008-11-30 23:51:27 +01001106 /* we're not ready yet */
1107 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001108
1109 failed_keep_alive:
1110 /* Here we process low-level errors for keep-alive requests. In
1111 * short, if the request is not the first one and it experiences
1112 * a timeout, read error or shutdown, we just silently close so
1113 * that the client can try again.
1114 */
1115 txn->status = 0;
1116 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001117 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001118 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001119 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001120 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001121 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001122 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001123 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001124
Willy Tarreaud787e662009-07-07 10:14:51 +02001125 /* OK now we have a complete HTTP request with indexed headers. Let's
1126 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001127 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001128 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001129 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001130 * byte after the last LF. msg->sov points to the first byte of data.
1131 * msg->eol cannot be trusted because it may have been left uninitialized
1132 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001133 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001134
Willy Tarreau87b09662015-04-03 00:22:06 +02001135 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001136 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001137
Willy Tarreaub16a5742010-01-10 14:46:16 +01001138 if (txn->flags & TX_WAIT_NEXT_RQ) {
1139 /* kill the pending keep-alive timeout */
1140 txn->flags &= ~TX_WAIT_NEXT_RQ;
1141 req->analyse_exp = TICK_ETERNITY;
1142 }
1143
1144
Willy Tarreaud787e662009-07-07 10:14:51 +02001145 /* Maybe we found in invalid header name while we were configured not
1146 * to block on that, so we have to capture it now.
1147 */
1148 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001149 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001150
Willy Tarreau59234e92008-11-30 23:51:27 +01001151 /*
1152 * 1: identify the method
1153 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001154 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001155
1156 /* we can make use of server redirect on GET and HEAD */
1157 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001158 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001159 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001160 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001161 /* PRI is reserved for the HTTP/2 preface */
1162 msg->err_pos = 0;
1163 goto return_bad_req;
1164 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001165
Willy Tarreau59234e92008-11-30 23:51:27 +01001166 /*
1167 * 2: check if the URI matches the monitor_uri.
1168 * We have to do this for every request which gets in, because
1169 * the monitor-uri is defined by the frontend.
1170 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001171 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1172 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001173 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001174 sess->fe->monitor_uri,
1175 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001176 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001177 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001178 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001179 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001180
Willy Tarreaue7dff022015-04-03 01:14:29 +02001181 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001182 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001183
Willy Tarreau59234e92008-11-30 23:51:27 +01001184 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001185 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001186 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001187
Willy Tarreau59234e92008-11-30 23:51:27 +01001188 ret = acl_pass(ret);
1189 if (cond->pol == ACL_COND_UNLESS)
1190 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001191
Willy Tarreau59234e92008-11-30 23:51:27 +01001192 if (ret) {
1193 /* we fail this request, let's return 503 service unavail */
1194 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001195 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001196 if (!(s->flags & SF_ERR_MASK))
1197 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001198 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001199 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001200 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001201
Willy Tarreau59234e92008-11-30 23:51:27 +01001202 /* nothing to fail, let's reply normaly */
1203 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001204 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001205 if (!(s->flags & SF_ERR_MASK))
1206 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001207 goto return_prx_cond;
1208 }
1209
1210 /*
1211 * 3: Maybe we have to copy the original REQURI for the logs ?
1212 * Note: we cannot log anymore if the request has been
1213 * classified as invalid.
1214 */
1215 if (unlikely(s->logs.logwait & LW_REQ)) {
1216 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001217 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001218 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001219
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001220 if (urilen >= global.tune.requri_len )
1221 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001222 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001223 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001224
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001225 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001226 s->do_log(s);
1227 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001228 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001230 }
Willy Tarreau06619262006-12-17 08:37:22 +01001231
Willy Tarreau91852eb2015-05-01 13:26:00 +02001232 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1233 * exactly one digit "." one digit. This check may be disabled using
1234 * option accept-invalid-http-request.
1235 */
1236 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1237 if (msg->sl.rq.v_l != 8) {
1238 msg->err_pos = msg->sl.rq.v;
1239 goto return_bad_req;
1240 }
1241
Willy Tarreauf37954d2018-06-15 18:31:02 +02001242 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1243 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1244 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1245 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001246 msg->err_pos = msg->sl.rq.v + 4;
1247 goto return_bad_req;
1248 }
1249 }
Willy Tarreau13317662015-05-01 13:47:08 +02001250 else {
1251 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1252 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1253 goto return_bad_req;
1254 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001255
Willy Tarreau5b154472009-12-21 20:11:07 +01001256 /* ... and check if the request is HTTP/1.1 or above */
1257 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001258 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1259 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1260 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001261 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001262
1263 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001264 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01001265
Willy Tarreau88d349d2010-01-25 12:15:43 +01001266 /* if the frontend has "option http-use-proxy-header", we'll check if
1267 * we have what looks like a proxied connection instead of a connection,
1268 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1269 * Note that this is *not* RFC-compliant, however browsers and proxies
1270 * happen to do that despite being non-standard :-(
1271 * We consider that a request not beginning with either '/' or '*' is
1272 * a proxied connection, which covers both "scheme://location" and
1273 * CONNECT ip:port.
1274 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001275 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001276 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001277 txn->flags |= TX_USE_PX_CONN;
1278
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001279 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001280 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001281
Willy Tarreau59234e92008-11-30 23:51:27 +01001282 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001283 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001284 http_capture_headers(ci_head(req), &txn->hdr_idx,
1285 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001286
Willy Tarreau557f1992015-05-01 10:05:17 +02001287 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1288 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001289 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001290 * The length of a message body is determined by one of the following
1291 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001292 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001293 * 1. Any response to a HEAD request and any response with a 1xx
1294 * (Informational), 204 (No Content), or 304 (Not Modified) status
1295 * code is always terminated by the first empty line after the
1296 * header fields, regardless of the header fields present in the
1297 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001298 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001299 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1300 * the connection will become a tunnel immediately after the empty
1301 * line that concludes the header fields. A client MUST ignore any
1302 * Content-Length or Transfer-Encoding header fields received in
1303 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001304 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001305 * 3. If a Transfer-Encoding header field is present and the chunked
1306 * transfer coding (Section 4.1) is the final encoding, the message
1307 * body length is determined by reading and decoding the chunked
1308 * data until the transfer coding indicates the data is complete.
1309 *
1310 * If a Transfer-Encoding header field is present in a response and
1311 * the chunked transfer coding is not the final encoding, the
1312 * message body length is determined by reading the connection until
1313 * it is closed by the server. If a Transfer-Encoding header field
1314 * is present in a request and the chunked transfer coding is not
1315 * the final encoding, the message body length cannot be determined
1316 * reliably; the server MUST respond with the 400 (Bad Request)
1317 * status code and then close the connection.
1318 *
1319 * If a message is received with both a Transfer-Encoding and a
1320 * Content-Length header field, the Transfer-Encoding overrides the
1321 * Content-Length. Such a message might indicate an attempt to
1322 * perform request smuggling (Section 9.5) or response splitting
1323 * (Section 9.4) and ought to be handled as an error. A sender MUST
1324 * remove the received Content-Length field prior to forwarding such
1325 * a message downstream.
1326 *
1327 * 4. If a message is received without Transfer-Encoding and with
1328 * either multiple Content-Length header fields having differing
1329 * field-values or a single Content-Length header field having an
1330 * invalid value, then the message framing is invalid and the
1331 * recipient MUST treat it as an unrecoverable error. If this is a
1332 * request message, the server MUST respond with a 400 (Bad Request)
1333 * status code and then close the connection. If this is a response
1334 * message received by a proxy, the proxy MUST close the connection
1335 * to the server, discard the received response, and send a 502 (Bad
1336 * Gateway) response to the client. If this is a response message
1337 * received by a user agent, the user agent MUST close the
1338 * connection to the server and discard the received response.
1339 *
1340 * 5. If a valid Content-Length header field is present without
1341 * Transfer-Encoding, its decimal value defines the expected message
1342 * body length in octets. If the sender closes the connection or
1343 * the recipient times out before the indicated number of octets are
1344 * received, the recipient MUST consider the message to be
1345 * incomplete and close the connection.
1346 *
1347 * 6. If this is a request message and none of the above are true, then
1348 * the message body length is zero (no message body is present).
1349 *
1350 * 7. Otherwise, this is a response message without a declared message
1351 * body length, so the message body length is determined by the
1352 * number of octets received prior to the server closing the
1353 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001354 */
1355
Willy Tarreau32b47f42009-10-18 20:55:02 +02001356 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001357 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001358 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001359 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001360 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001361 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001362 /* chunked not last, return badreq */
1363 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001364 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001365 }
1366
Willy Tarreau1c913912015-04-30 10:57:51 +02001367 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001368 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001369 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001370 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001371 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1372 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001373 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001374 signed long long cl;
1375
Willy Tarreauad14f752011-09-02 20:33:27 +02001376 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001377 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001378 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001379 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001380
Willy Tarreauad14f752011-09-02 20:33:27 +02001381 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001382 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001383 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001384 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001385
Willy Tarreauad14f752011-09-02 20:33:27 +02001386 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001387 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001388 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001389 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001390
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001391 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001392 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001393 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001394 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001395
Christopher Fauletbe821b92017-03-30 11:21:53 +02001396 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001397 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001398 }
1399
Willy Tarreau34dfc602015-05-01 10:09:49 +02001400 /* even bodyless requests have a known length */
1401 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001402
Willy Tarreau179085c2014-04-28 16:48:56 +02001403 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1404 * only change if both the request and the config reference something else.
1405 * Option httpclose by itself sets tunnel mode where headers are mangled.
1406 * However, if another mode is set, it will affect it (eg: server-close/
1407 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1408 * if FE and BE have the same settings (common). The method consists in
1409 * checking if options changed between the two calls (implying that either
1410 * one is non-null, or one of them is non-null and we are there for the first
1411 * time.
1412 */
1413 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001414 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001415 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001416
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001417 /* we may have to wait for the request's body */
1418 if ((s->be->options & PR_O_WREQ_BODY) &&
1419 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1420 req->analysers |= AN_REQ_HTTP_BODY;
1421
Willy Tarreau83ece462017-12-21 15:13:09 +01001422 /*
1423 * RFC7234#4:
1424 * A cache MUST write through requests with methods
1425 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1426 * the origin server; i.e., a cache is not allowed
1427 * to generate a reply to such a request before
1428 * having forwarded the request and having received
1429 * a corresponding response.
1430 *
1431 * RFC7231#4.2.1:
1432 * Of the request methods defined by this
1433 * specification, the GET, HEAD, OPTIONS, and TRACE
1434 * methods are defined to be safe.
1435 */
1436 if (likely(txn->meth == HTTP_METH_GET ||
1437 txn->meth == HTTP_METH_HEAD ||
1438 txn->meth == HTTP_METH_OPTIONS ||
1439 txn->meth == HTTP_METH_TRACE))
1440 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1441
Willy Tarreaud787e662009-07-07 10:14:51 +02001442 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001443 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001444 req->analyse_exp = TICK_ETERNITY;
1445 return 1;
1446
1447 return_bad_req:
1448 /* We centralize bad requests processing here */
1449 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1450 /* we detected a parsing error. We want to archive this request
1451 * in the dedicated proxy area for later troubleshooting.
1452 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001453 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001454 }
1455
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001456 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001457 txn->req.msg_state = HTTP_MSG_ERROR;
1458 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001459 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001460
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001461 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001462 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001463 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001464
1465 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001466 if (!(s->flags & SF_ERR_MASK))
1467 s->flags |= SF_ERR_PRXCOND;
1468 if (!(s->flags & SF_FINST_MASK))
1469 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001470
Christopher Faulet0184ea72017-01-05 14:06:34 +01001471 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001472 req->analyse_exp = TICK_ETERNITY;
1473 return 0;
1474}
1475
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001476
Willy Tarreau347a35d2013-11-22 17:51:09 +01001477/* This function prepares an applet to handle the stats. It can deal with the
1478 * "100-continue" expectation, check that admin rules are met for POST requests,
1479 * and program a response message if something was unexpected. It cannot fail
1480 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001481 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001482 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001483 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001484 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001485int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001486{
1487 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001488 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001489 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001490 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001491 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001492 struct uri_auth *uri_auth = s->be->uri_auth;
1493 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001494 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001495
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001496 appctx = si_appctx(si);
1497 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1498 appctx->st1 = appctx->st2 = 0;
1499 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1500 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001501 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001502 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001503
Willy Tarreauf37954d2018-06-15 18:31:02 +02001504 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001505 lookup = uri + uri_auth->uri_len;
1506
1507 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1508 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001509 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001510 break;
1511 }
1512 }
1513
1514 if (uri_auth->refresh) {
1515 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1516 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001517 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001518 break;
1519 }
1520 }
1521 }
1522
1523 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1524 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001525 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001526 break;
1527 }
1528 }
1529
Willy Tarreau1e62df92016-01-11 18:57:53 +01001530 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1531 if (memcmp(h, ";typed", 6) == 0) {
1532 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1533 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1534 break;
1535 }
1536 }
1537
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001538 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1539 if (memcmp(h, ";st=", 4) == 0) {
1540 int i;
1541 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001542 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001543 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1544 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001545 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001546 break;
1547 }
1548 }
1549 break;
1550 }
1551 }
1552
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001553 appctx->ctx.stats.scope_str = 0;
1554 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001555 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1556 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1557 int itx = 0;
1558 const char *h2;
1559 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1560 const char *err;
1561
1562 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1563 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001564 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001565 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1566 itx++;
1567 h++;
1568 }
1569
1570 if (itx > STAT_SCOPE_TXT_MAXLEN)
1571 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001572 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001573
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001574 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001575 memcpy(scope_txt, h2, itx);
1576 scope_txt[itx] = '\0';
1577 err = invalid_char(scope_txt);
1578 if (err) {
1579 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001580 appctx->ctx.stats.scope_str = 0;
1581 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001582 }
1583 break;
1584 }
1585 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001586
1587 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001588 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001589 int ret = 1;
1590
1591 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001592 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001593 ret = acl_pass(ret);
1594 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1595 ret = !ret;
1596 }
1597
1598 if (ret) {
1599 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001600 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001601 break;
1602 }
1603 }
1604
1605 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001606 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001607 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001608 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001609 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1610 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001611 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001612 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001613 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001614 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1615 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001616 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001617 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001618 else {
1619 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001620 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001621 }
1622
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001623 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001624 return 1;
1625}
1626
Lukas Tribus67db8df2013-06-23 17:37:13 +02001627/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1628 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1629 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001630void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001631{
1632#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001633 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001634 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1635#endif
1636#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001637 if (from->ss_family == AF_INET6) {
1638 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001639 /* v4-mapped addresses need IP_TOS */
1640 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1641 else
1642 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1643 }
1644#endif
1645}
1646
Willy Tarreau87b09662015-04-03 00:22:06 +02001647int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001648 const char* name, unsigned int name_len,
1649 const char *str, struct my_regex *re,
1650 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001651{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001652 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001653 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001654 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001655 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1656 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001657 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001658
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001659 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001660
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001661 /* Choose the header browsing function. */
1662 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001663 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001664 http_find_hdr_func = http_find_header2;
1665 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001666 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001667 http_find_hdr_func = http_find_full_header2;
1668 break;
1669 default: /* impossible */
1670 return -1;
1671 }
1672
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001673 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1674 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001675 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001676 char *val = ctx.line + ctx.val;
1677 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001678
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001679 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1680 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001681
Willy Tarreau6e27be12018-08-22 04:46:47 +02001682 len = exp_replace(output->area, output->size, val, str, pmatch);
1683 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001684 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001685
Willy Tarreau6e27be12018-08-22 04:46:47 +02001686 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001687
1688 hdr->len += delta;
1689 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001690
1691 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001692 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001693 }
1694
1695 return 0;
1696}
1697
Willy Tarreau87b09662015-04-03 00:22:06 +02001698static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001699 const char* name, unsigned int name_len,
1700 struct list *fmt, struct my_regex *re,
1701 int action)
1702{
Willy Tarreau83061a82018-07-13 11:56:34 +02001703 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001704 int ret = -1;
1705
1706 replace = alloc_trash_chunk();
1707 if (!replace)
1708 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001709
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001710 replace->data = build_logline(s, replace->area, replace->size, fmt);
1711 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001712 goto leave;
1713
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001714 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1715 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001716
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001717 leave:
1718 free_trash_chunk(replace);
1719 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001720}
1721
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001722/*
1723 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1724 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001725 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1726 * line is inserted before the header. If an error occurred <early_hints> is
1727 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001728 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001729static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1730 const char* name, unsigned int name_len,
1731 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001732{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001733 if (!early_hints) {
1734 early_hints = alloc_trash_chunk();
1735 if (!early_hints)
1736 goto fail;
1737 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1738 goto fail;
1739 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001740
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001741 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1742 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001743
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001744 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1745 if (!chunk_memcat(early_hints, "\r\n", 2))
1746 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001747
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001748 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001749
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001750 fail:
1751 free_trash_chunk(early_hints);
1752 return NULL;
1753}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001754
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001755/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1756 * response is added. If an error occurred or if another response was already
1757 * sent, this function does nothing.
1758 */
1759static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1760{
1761 struct channel *chn = s->txn->rsp.chn;
1762 char *cur_ptr = ci_head(chn);
1763 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001764
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001765 /* If a response was already sent, skip early hints */
1766 if (s->txn->status > 0)
1767 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001768
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001769 if (!chunk_memcat(early_hints, "\r\n", 2))
1770 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001771
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001772 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1773 c_adv(chn, ret);
1774 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001775}
1776
Willy Tarreau87b09662015-04-03 00:22:06 +02001777/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001778 * transaction <txn>. Returns the verdict of the first rule that prevents
1779 * further processing of the request (auth, deny, ...), and defaults to
1780 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1781 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001782 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1783 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1784 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001785 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001786enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001787http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001788{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001789 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001790 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001791 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001792 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001793 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001794 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001795 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001796 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001797 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001798 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001799
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001800 /* If "the current_rule_list" match the executed rule list, we are in
1801 * resume condition. If a resume is needed it is always in the action
1802 * and never in the ACL or converters. In this case, we initialise the
1803 * current rule, and go to the action execution point.
1804 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001805 if (s->current_rule) {
1806 rule = s->current_rule;
1807 s->current_rule = NULL;
1808 if (s->current_rule_list == rules)
1809 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001810 }
1811 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001812
Willy Tarreauff011f22011-01-06 17:51:27 +01001813 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001814
Willy Tarreau96257ec2012-12-27 10:46:37 +01001815 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001816 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001817 int ret;
1818
Willy Tarreau192252e2015-04-04 01:47:55 +02001819 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001820 ret = acl_pass(ret);
1821
Willy Tarreauff011f22011-01-06 17:51:27 +01001822 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001823 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001824
1825 if (!ret) /* condition not matched */
1826 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001827 }
1828
Willy Tarreauacc98002015-09-27 23:34:39 +02001829 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001830resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001831 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001832 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001833 rule_ret = HTTP_RULE_RES_STOP;
1834 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001835
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001836 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001837 if (deny_status)
1838 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001839 rule_ret = HTTP_RULE_RES_DENY;
1840 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001841
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001842 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001843 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001844 if (deny_status)
1845 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001846 rule_ret = HTTP_RULE_RES_DENY;
1847 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001848
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001849 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001850 /* Be sure to send any pending HTTP 103 response first */
1851 if (early_hints) {
1852 http_send_early_hints(s, early_hints);
1853 free_trash_chunk(early_hints);
1854 early_hints = NULL;
1855 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001856 /* Auth might be performed on regular http-req rules as well as on stats */
1857 auth_realm = rule->arg.auth.realm;
1858 if (!auth_realm) {
1859 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1860 auth_realm = STATS_DEFAULT_REALM;
1861 else
1862 auth_realm = px->id;
1863 }
1864 /* send 401/407 depending on whether we use a proxy or not. We still
1865 * count one error, because normal browsing won't significantly
1866 * increase the counter but brute force attempts will.
1867 */
1868 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1869 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001870 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001871 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001872 rule_ret = HTTP_RULE_RES_ABRT;
1873 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001874
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001875 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001876 /* Be sure to send any pending HTTP 103 response first */
1877 if (early_hints) {
1878 http_send_early_hints(s, early_hints);
1879 free_trash_chunk(early_hints);
1880 early_hints = NULL;
1881 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001882 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001883 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001884 rule_ret = HTTP_RULE_RES_BADREQ;
1885 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001886
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001887 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001888 s->task->nice = rule->arg.nice;
1889 break;
1890
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001891 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001892 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001893 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001894 break;
1895
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001896 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001897#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001898 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001899 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001900#endif
1901 break;
1902
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001903 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001904 s->logs.level = rule->arg.loglevel;
1905 break;
1906
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001907 case ACT_HTTP_REPLACE_HDR:
1908 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001909 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1910 rule->arg.hdr_add.name_len,
1911 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001912 &rule->arg.hdr_add.re, rule->action)) {
1913 rule_ret = HTTP_RULE_RES_BADREQ;
1914 goto end;
1915 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001916 break;
1917
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001918 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001919 ctx.idx = 0;
1920 /* remove all occurrences of the header */
1921 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001922 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001923 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001924 }
Willy Tarreau85603282015-01-21 20:39:27 +01001925 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001926
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001927 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001928 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001929 /* The scope of the trash buffer must be limited to this function. The
1930 * build_logline() function can execute a lot of other function which
1931 * can use the trash buffer. So for limiting the scope of this global
1932 * buffer, we build first the header value using build_logline, and
1933 * after we store the header name.
1934 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001935 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001936
1937 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001938 if (!replace) {
1939 rule_ret = HTTP_RULE_RES_BADREQ;
1940 goto end;
1941 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001942
Thierry Fournier4b788f72016-06-01 13:35:36 +02001943 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001944 len += build_logline(s, replace->area + len,
1945 replace->size - len,
1946 &rule->arg.hdr_add.fmt);
1947 memcpy(replace->area, rule->arg.hdr_add.name,
1948 rule->arg.hdr_add.name_len);
1949 replace->area[rule->arg.hdr_add.name_len] = ':';
1950 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1951 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001952
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001953 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001954 /* remove all occurrences of the header */
1955 ctx.idx = 0;
1956 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001957 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001958 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1959 }
1960 }
1961
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001962 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001963 static unsigned char rate_limit = 0;
1964
1965 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001966 replace->area[rule->arg.hdr_add.name_len] = 0;
1967 send_log(px, LOG_WARNING, "Proxy %s failed to add or set the request header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id,
1968 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001969 }
1970
1971 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1972 if (sess->fe != s->be)
1973 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1974 if (sess->listener->counters)
1975 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1976 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001977
1978 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001979 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001980 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001981
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001982 case ACT_HTTP_DEL_ACL:
1983 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001984 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001985 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001986
1987 /* collect reference */
1988 ref = pat_ref_lookup(rule->arg.map.ref);
1989 if (!ref)
1990 continue;
1991
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001992 /* allocate key */
1993 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001994 if (!key) {
1995 rule_ret = HTTP_RULE_RES_BADREQ;
1996 goto end;
1997 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001998
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001999 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002000 key->data = build_logline(s, key->area, key->size,
2001 &rule->arg.map.key);
2002 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002003
2004 /* perform update */
2005 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002006 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002007 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002008 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002009
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002010 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002011 break;
2012 }
2013
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002014 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002015 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002016 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002017
2018 /* collect reference */
2019 ref = pat_ref_lookup(rule->arg.map.ref);
2020 if (!ref)
2021 continue;
2022
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002023 /* allocate key */
2024 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002025 if (!key) {
2026 rule_ret = HTTP_RULE_RES_BADREQ;
2027 goto end;
2028 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002029
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002030 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002031 key->data = build_logline(s, key->area, key->size,
2032 &rule->arg.map.key);
2033 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002034
2035 /* perform update */
2036 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002037 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002038 if (pat_ref_find_elt(ref, key->area) == NULL)
2039 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002040 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002041
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002042 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002043 break;
2044 }
2045
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002046 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002047 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002048 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002049
2050 /* collect reference */
2051 ref = pat_ref_lookup(rule->arg.map.ref);
2052 if (!ref)
2053 continue;
2054
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002055 /* allocate key */
2056 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002057 if (!key) {
2058 rule_ret = HTTP_RULE_RES_BADREQ;
2059 goto end;
2060 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002061
2062 /* allocate value */
2063 value = alloc_trash_chunk();
2064 if (!value) {
2065 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01002066 rule_ret = HTTP_RULE_RES_BADREQ;
2067 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002068 }
2069
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002070 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002071 key->data = build_logline(s, key->area, key->size,
2072 &rule->arg.map.key);
2073 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002074
2075 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002076 value->data = build_logline(s, value->area,
2077 value->size,
2078 &rule->arg.map.value);
2079 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002080
2081 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002082 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002083 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002084 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002085 else
2086 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002087 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002088
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002089 free_trash_chunk(key);
2090 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002091 break;
2092 }
William Lallemand73025dd2014-04-24 14:38:37 +02002093
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002094 case ACT_HTTP_EARLY_HINT:
2095 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2096 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002097 early_hints = http_apply_early_hint_rule(s, early_hints,
2098 rule->arg.early_hint.name,
2099 rule->arg.early_hint.name_len,
2100 &rule->arg.early_hint.fmt);
2101 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01002102 rule_ret = HTTP_RULE_RES_DONE;
2103 goto end;
2104 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002105 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002106 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002107 if ((s->req.flags & CF_READ_ERROR) ||
2108 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2109 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2110 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002111 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002112
Willy Tarreauacc98002015-09-27 23:34:39 +02002113 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002114 case ACT_RET_ERR:
2115 case ACT_RET_CONT:
2116 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002117 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01002118 rule_ret = HTTP_RULE_RES_DONE;
2119 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002120 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002121 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01002122 rule_ret = HTTP_RULE_RES_YIELD;
2123 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002124 }
William Lallemand73025dd2014-04-24 14:38:37 +02002125 break;
2126
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002127 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002128 /* Note: only the first valid tracking parameter of each
2129 * applies.
2130 */
2131
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002132 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002133 struct stktable *t;
2134 struct stksess *ts;
2135 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002136 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002137
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002138 t = rule->arg.trk_ctr.table.t;
2139 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
Willy Tarreau09448f72014-06-25 18:12:15 +02002140
2141 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002142 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002143
2144 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002145 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2146 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2147 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002148 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002149
Emeric Brun819fc6f2017-06-13 19:37:32 +02002150 if (ptr1)
2151 stktable_data_cast(ptr1, http_req_cnt)++;
2152
2153 if (ptr2)
2154 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2155 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2156
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002157 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002158
2159 /* If data was modified, we need to touch to re-schedule sync */
2160 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002161 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002162
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002163 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002164 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002165 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002166 }
2167 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002168 break;
2169
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002170 /* other flags exists, but normaly, they never be matched. */
2171 default:
2172 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002173 }
2174 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002175
Christopher Fauletea827bd2018-11-15 15:34:11 +01002176 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002177 if (early_hints) {
2178 http_send_early_hints(s, early_hints);
2179 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002180 }
2181
Willy Tarreau96257ec2012-12-27 10:46:37 +01002182 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01002183 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002184}
2185
Willy Tarreau71241ab2012-12-27 11:30:54 +01002186
Willy Tarreau51d861a2015-05-22 17:30:48 +02002187/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2188 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2189 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2190 * is returned, the process can continue the evaluation of next rule list. If
2191 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2192 * is returned, it means the operation could not be processed and a server error
2193 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2194 * deny rule. If *YIELD is returned, the caller must call again the function
2195 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002196 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002197enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002198http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002199{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002200 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002201 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002202 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002203 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002204 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002205 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02002206 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002207
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002208 /* If "the current_rule_list" match the executed rule list, we are in
2209 * resume condition. If a resume is needed it is always in the action
2210 * and never in the ACL or converters. In this case, we initialise the
2211 * current rule, and go to the action execution point.
2212 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002213 if (s->current_rule) {
2214 rule = s->current_rule;
2215 s->current_rule = NULL;
2216 if (s->current_rule_list == rules)
2217 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002218 }
2219 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002220
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002221 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002222
2223 /* check optional condition */
2224 if (rule->cond) {
2225 int ret;
2226
Willy Tarreau192252e2015-04-04 01:47:55 +02002227 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002228 ret = acl_pass(ret);
2229
2230 if (rule->cond->pol == ACL_COND_UNLESS)
2231 ret = !ret;
2232
2233 if (!ret) /* condition not matched */
2234 continue;
2235 }
2236
Willy Tarreauacc98002015-09-27 23:34:39 +02002237 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002238resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002239 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002240 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002241 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2242 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002243
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002244 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002245 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002246 rule_ret = HTTP_RULE_RES_STOP;
2247 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002248
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002249 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002250 s->task->nice = rule->arg.nice;
2251 break;
2252
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002253 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002254 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002255 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002256 break;
2257
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002258 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002259#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002260 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002261 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002262#endif
2263 break;
2264
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002265 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002266 s->logs.level = rule->arg.loglevel;
2267 break;
2268
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002269 case ACT_HTTP_REPLACE_HDR:
2270 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002271 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2272 rule->arg.hdr_add.name_len,
2273 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002274 &rule->arg.hdr_add.re, rule->action)) {
2275 rule_ret = HTTP_RULE_RES_BADREQ;
2276 goto end;
2277 }
Sasha Pachev218f0642014-06-16 12:05:59 -06002278 break;
2279
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002280 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002281 ctx.idx = 0;
2282 /* remove all occurrences of the header */
2283 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002284 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002285 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2286 }
Willy Tarreau85603282015-01-21 20:39:27 +01002287 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002288
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002289 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002290 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002291 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002292
2293 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002294 if (!replace) {
2295 rule_ret = HTTP_RULE_RES_BADREQ;
2296 goto end;
2297 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002298
2299 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002300 memcpy(replace->area, rule->arg.hdr_add.name,
2301 rule->arg.hdr_add.name_len);
2302 replace->data = rule->arg.hdr_add.name_len;
2303 replace->area[replace->data++] = ':';
2304 replace->area[replace->data++] = ' ';
2305 replace->data += build_logline(s,
2306 replace->area + replace->data,
2307 replace->size - replace->data,
2308 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002309
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002310 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002311 /* remove all occurrences of the header */
2312 ctx.idx = 0;
2313 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002314 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002315 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2316 }
2317 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002318
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002319 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002320 static unsigned char rate_limit = 0;
2321
2322 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002323 replace->area[rule->arg.hdr_add.name_len] = 0;
2324 send_log(px, LOG_WARNING, "Proxy %s failed to add or set the response header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id,
2325 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002326 }
2327
2328 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2329 if (sess->fe != s->be)
2330 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2331 if (sess->listener->counters)
2332 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2333 if (objt_server(s->target))
2334 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2335 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002336
2337 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002338 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002339 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002340
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002341 case ACT_HTTP_DEL_ACL:
2342 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002343 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002344 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002345
2346 /* collect reference */
2347 ref = pat_ref_lookup(rule->arg.map.ref);
2348 if (!ref)
2349 continue;
2350
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002351 /* allocate key */
2352 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002353 if (!key) {
2354 rule_ret = HTTP_RULE_RES_BADREQ;
2355 goto end;
2356 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002357
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002358 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002359 key->data = build_logline(s, key->area, key->size,
2360 &rule->arg.map.key);
2361 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002362
2363 /* perform update */
2364 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002365 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002366 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002367 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002368
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002369 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002370 break;
2371 }
2372
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002373 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002374 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002375 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002376
2377 /* collect reference */
2378 ref = pat_ref_lookup(rule->arg.map.ref);
2379 if (!ref)
2380 continue;
2381
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002382 /* allocate key */
2383 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002384 if (!key) {
2385 rule_ret = HTTP_RULE_RES_BADREQ;
2386 goto end;
2387 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002388
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002389 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002390 key->data = build_logline(s, key->area, key->size,
2391 &rule->arg.map.key);
2392 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002393
2394 /* perform update */
2395 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002396 if (pat_ref_find_elt(ref, key->area) == NULL)
2397 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002398
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002399 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002400 break;
2401 }
2402
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002403 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002404 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002405 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002406
2407 /* collect reference */
2408 ref = pat_ref_lookup(rule->arg.map.ref);
2409 if (!ref)
2410 continue;
2411
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002412 /* allocate key */
2413 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002414 if (!key) {
2415 rule_ret = HTTP_RULE_RES_BADREQ;
2416 goto end;
2417 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002418
2419 /* allocate value */
2420 value = alloc_trash_chunk();
2421 if (!value) {
2422 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002423 rule_ret = HTTP_RULE_RES_BADREQ;
2424 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002425 }
2426
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002427 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002428 key->data = build_logline(s, key->area, key->size,
2429 &rule->arg.map.key);
2430 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002431
2432 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002433 value->data = build_logline(s, value->area,
2434 value->size,
2435 &rule->arg.map.value);
2436 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002437
2438 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002439 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002440 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002441 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002442 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002443 else
2444 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002445 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002446 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002447 free_trash_chunk(key);
2448 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002449 break;
2450 }
William Lallemand73025dd2014-04-24 14:38:37 +02002451
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002452 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002453 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002454 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002455 rule_ret = HTTP_RULE_RES_BADREQ;
2456 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002457
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002458 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2459 /* Note: only the first valid tracking parameter of each
2460 * applies.
2461 */
2462
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002463 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002464 struct stktable *t;
2465 struct stksess *ts;
2466 struct stktable_key *key;
2467 void *ptr;
2468
2469 t = rule->arg.trk_ctr.table.t;
2470 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2471
2472 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002473 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002474
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002475 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002476
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002477 /* let's count a new HTTP request as it's the first time we do it */
2478 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2479 if (ptr)
2480 stktable_data_cast(ptr, http_req_cnt)++;
2481
2482 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2483 if (ptr)
2484 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2485 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2486
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002487 /* When the client triggers a 4xx from the server, it's most often due
2488 * to a missing object or permission. These events should be tracked
2489 * because if they happen often, it may indicate a brute force or a
2490 * vulnerability scan. Normally this is done when receiving the response
2491 * but here we're tracking after this ought to have been done so we have
2492 * to do it on purpose.
2493 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002494 if ((unsigned)(txn->status - 400) < 100) {
2495 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2496 if (ptr)
2497 stktable_data_cast(ptr, http_err_cnt)++;
2498
2499 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2500 if (ptr)
2501 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2502 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2503 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002504
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002505 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002506
Emeric Brun0fed0b02017-11-29 16:15:07 +01002507 /* If data was modified, we need to touch to re-schedule sync */
2508 stktable_touch_local(t, ts, 0);
2509
Emeric Brun819fc6f2017-06-13 19:37:32 +02002510 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2511 if (sess->fe != s->be)
2512 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2513
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002514 }
2515 }
2516 break;
2517
Thierry FOURNIER42148732015-09-02 17:17:33 +02002518 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002519 if ((s->req.flags & CF_READ_ERROR) ||
2520 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2521 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2522 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002523 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002524
Willy Tarreauacc98002015-09-27 23:34:39 +02002525 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002526 case ACT_RET_ERR:
2527 case ACT_RET_CONT:
2528 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002529 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002530 rule_ret = HTTP_RULE_RES_STOP;
2531 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002532 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002533 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002534 rule_ret = HTTP_RULE_RES_YIELD;
2535 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002536 }
William Lallemand73025dd2014-04-24 14:38:37 +02002537 break;
2538
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002539 /* other flags exists, but normaly, they never be matched. */
2540 default:
2541 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002542 }
2543 }
2544
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002545 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002546 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002547 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002548}
2549
2550
Willy Tarreau71241ab2012-12-27 11:30:54 +01002551/* Perform an HTTP redirect based on the information in <rule>. The function
2552 * returns non-zero on success, or zero in case of a, irrecoverable error such
2553 * as too large a request to build a valid response.
2554 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002555int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002556{
Willy Tarreaub329a312015-05-22 16:27:37 +02002557 struct http_msg *req = &txn->req;
2558 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002559 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002560 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002561 int ret = 0;
2562
2563 chunk = alloc_trash_chunk();
2564 if (!chunk)
2565 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002566
2567 /* build redirect message */
2568 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002569 case 308:
2570 msg_fmt = HTTP_308;
2571 break;
2572 case 307:
2573 msg_fmt = HTTP_307;
2574 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002575 case 303:
2576 msg_fmt = HTTP_303;
2577 break;
2578 case 301:
2579 msg_fmt = HTTP_301;
2580 break;
2581 case 302:
2582 default:
2583 msg_fmt = HTTP_302;
2584 break;
2585 }
2586
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002587 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2588 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002589
2590 switch(rule->type) {
2591 case REDIRECT_TYPE_SCHEME: {
2592 const char *path;
2593 const char *host;
2594 struct hdr_ctx ctx;
2595 int pathlen;
2596 int hostlen;
2597
2598 host = "";
2599 hostlen = 0;
2600 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002601 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002602 host = ctx.line + ctx.val;
2603 hostlen = ctx.vlen;
2604 }
2605
Willy Tarreau6b952c82018-09-10 17:45:34 +02002606 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002607 /* build message using path */
2608 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002609 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002610 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2611 int qs = 0;
2612 while (qs < pathlen) {
2613 if (path[qs] == '?') {
2614 pathlen = qs;
2615 break;
2616 }
2617 qs++;
2618 }
2619 }
2620 } else {
2621 path = "/";
2622 pathlen = 1;
2623 }
2624
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002625 if (rule->rdr_str) { /* this is an old "redirect" rule */
2626 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002627 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002628 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002629
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002630 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002631 memcpy(chunk->area + chunk->data, rule->rdr_str,
2632 rule->rdr_len);
2633 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002634 }
2635 else {
2636 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002637 chunk->data += build_logline(s,
2638 chunk->area + chunk->data,
2639 chunk->size - chunk->data,
2640 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002641
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002642 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002643 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002644 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002645 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002646 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002647 memcpy(chunk->area + chunk->data, "://", 3);
2648 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002649
2650 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002651 memcpy(chunk->area + chunk->data, host, hostlen);
2652 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002653
2654 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002655 memcpy(chunk->area + chunk->data, path, pathlen);
2656 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002657
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002658 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002659 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002660 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002661 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002662 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002663 chunk->area[chunk->data] = '/';
2664 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002665 }
2666
2667 break;
2668 }
2669 case REDIRECT_TYPE_PREFIX: {
2670 const char *path;
2671 int pathlen;
2672
Willy Tarreau6b952c82018-09-10 17:45:34 +02002673 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002674 /* build message using path */
2675 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002676 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002677 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2678 int qs = 0;
2679 while (qs < pathlen) {
2680 if (path[qs] == '?') {
2681 pathlen = qs;
2682 break;
2683 }
2684 qs++;
2685 }
2686 }
2687 } else {
2688 path = "/";
2689 pathlen = 1;
2690 }
2691
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002692 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002693 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002694 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002695
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002696 /* add prefix. Note that if prefix == "/", we don't want to
2697 * add anything, otherwise it makes it hard for the user to
2698 * configure a self-redirection.
2699 */
2700 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002701 memcpy(chunk->area + chunk->data,
2702 rule->rdr_str, rule->rdr_len);
2703 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002704 }
2705 }
2706 else {
2707 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002708 chunk->data += build_logline(s,
2709 chunk->area + chunk->data,
2710 chunk->size - chunk->data,
2711 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002712
2713 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002714 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002715 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002716 }
2717
2718 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002719 memcpy(chunk->area + chunk->data, path, pathlen);
2720 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002721
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002722 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002723 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002724 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002725 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002726 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002727 chunk->area[chunk->data] = '/';
2728 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002729 }
2730
2731 break;
2732 }
2733 case REDIRECT_TYPE_LOCATION:
2734 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002735 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002736 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002737 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002738
2739 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002740 memcpy(chunk->area + chunk->data, rule->rdr_str,
2741 rule->rdr_len);
2742 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002743 }
2744 else {
2745 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002746 chunk->data += build_logline(s,
2747 chunk->area + chunk->data,
2748 chunk->size - chunk->data,
2749 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002750
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002751 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002752 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002753 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002754 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002755 break;
2756 }
2757
2758 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002759 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2760 chunk->data += 14;
2761 memcpy(chunk->area + chunk->data, rule->cookie_str,
2762 rule->cookie_len);
2763 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002764 }
2765
Willy Tarreau19b14122017-02-28 09:48:11 +01002766 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002767 txn->status = rule->code;
2768 /* let's log the request time */
2769 s->logs.tv_request = now;
2770
Christopher Fauletbe821b92017-03-30 11:21:53 +02002771 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002772 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2773 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2774 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002775 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002776 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002777 memcpy(chunk->area + chunk->data,
2778 "\r\nProxy-Connection: keep-alive", 30);
2779 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002780 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002781 memcpy(chunk->area + chunk->data,
2782 "\r\nConnection: keep-alive", 24);
2783 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002784 }
2785 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002786 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2787 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002788 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002789 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002790 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002791 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002792 req->next -= req->sov;
2793 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002794 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002795 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002796 req->msg_state = HTTP_MSG_CLOSED;
2797 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002798 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002799 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002800 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002801 /* let the server side turn to SI_ST_CLO */
2802 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002803 } else {
2804 /* keep-alive not possible */
2805 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002806 memcpy(chunk->area + chunk->data,
2807 "\r\nProxy-Connection: close\r\n\r\n", 29);
2808 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002809 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002810 memcpy(chunk->area + chunk->data,
2811 "\r\nConnection: close\r\n\r\n", 23);
2812 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002813 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002814 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002815 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002816 }
2817
Willy Tarreaue7dff022015-04-03 01:14:29 +02002818 if (!(s->flags & SF_ERR_MASK))
2819 s->flags |= SF_ERR_LOCAL;
2820 if (!(s->flags & SF_FINST_MASK))
2821 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002822
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002823 ret = 1;
2824 leave:
2825 free_trash_chunk(chunk);
2826 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002827}
2828
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002829/* This stream analyser runs all HTTP request processing which is common to
2830 * frontends and backends, which means blocking ACLs, filters, connection-close,
2831 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002832 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002833 * either needs more data or wants to immediately abort the request (eg: deny,
2834 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002835 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002836int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002837{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002838 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002839 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002840 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002841 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002842 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002843 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002844 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002845 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002846
Willy Tarreau655dce92009-11-08 13:10:58 +01002847 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002848 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002849 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002850 }
2851
Christopher Faulet45073512018-07-20 10:16:29 +02002852 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02002853 now_ms, __FUNCTION__,
2854 s,
2855 req,
2856 req->rex, req->wex,
2857 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002858 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002859 req->analysers);
2860
Willy Tarreau65410832014-04-28 21:25:43 +02002861 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002862 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002863
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002864 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002865 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002866 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002867
Willy Tarreau0b748332014-04-29 00:13:29 +02002868 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002869 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2870 goto return_prx_yield;
2871
Willy Tarreau0b748332014-04-29 00:13:29 +02002872 case HTTP_RULE_RES_CONT:
2873 case HTTP_RULE_RES_STOP: /* nothing to do */
2874 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002875
Willy Tarreau0b748332014-04-29 00:13:29 +02002876 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2877 if (txn->flags & TX_CLTARPIT)
2878 goto tarpit;
2879 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002880
Willy Tarreau0b748332014-04-29 00:13:29 +02002881 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2882 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002883
Willy Tarreau0b748332014-04-29 00:13:29 +02002884 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002885 goto done;
2886
Willy Tarreau0b748332014-04-29 00:13:29 +02002887 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2888 goto return_bad_req;
2889 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002890 }
2891
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002892 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2893 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002894 struct hdr_ctx ctx;
2895
2896 ctx.idx = 0;
2897 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002898 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002899 if (unlikely(http_header_add_tail2(&txn->req,
2900 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002901 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002902 goto return_bad_req;
2903 }
2904 }
2905
2906 }
2907
Willy Tarreau52542592014-04-28 18:33:26 +02002908 /* OK at this stage, we know that the request was accepted according to
2909 * the http-request rules, we can check for the stats. Note that the
2910 * URI is detected *before* the req* rules in order not to be affected
2911 * by a possible reqrep, while they are processed *after* so that a
2912 * reqdeny can still block them. This clearly needs to change in 1.6!
2913 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002914 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002915 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002916 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002917 txn->status = 500;
2918 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002919 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002920
Willy Tarreaue7dff022015-04-03 01:14:29 +02002921 if (!(s->flags & SF_ERR_MASK))
2922 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002923 goto return_prx_cond;
2924 }
2925
2926 /* parse the whole stats request and extract the relevant information */
2927 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002928 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002929 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002930
Willy Tarreau0b748332014-04-29 00:13:29 +02002931 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2932 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002933
Willy Tarreau0b748332014-04-29 00:13:29 +02002934 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2935 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002936 }
2937
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002938 /* evaluate the req* rules except reqadd */
2939 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002940 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002941 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002942
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002943 if (txn->flags & TX_CLDENY)
2944 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002945
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002946 if (txn->flags & TX_CLTARPIT) {
2947 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002948 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002949 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002950 }
Willy Tarreau06619262006-12-17 08:37:22 +01002951
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002952 /* add request headers from the rule sets in the same order */
2953 list_for_each_entry(wl, &px->req_add, list) {
2954 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002955 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002956 ret = acl_pass(ret);
2957 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2958 ret = !ret;
2959 if (!ret)
2960 continue;
2961 }
2962
Christopher Faulet10079f52018-10-03 15:17:28 +02002963 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002964 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002965 }
2966
Willy Tarreau52542592014-04-28 18:33:26 +02002967
2968 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002969 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2970 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002971 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002972 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002973 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002974
Willy Tarreaue7dff022015-04-03 01:14:29 +02002975 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2976 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2977 if (!(s->flags & SF_FINST_MASK))
2978 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002979
Willy Tarreau70730dd2014-04-24 18:06:27 +02002980 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002981 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2982 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002983 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002984 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002985 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002986
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002987 /* check whether we have some ACLs set to redirect this request */
2988 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002989 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002990 int ret;
2991
Willy Tarreau192252e2015-04-04 01:47:55 +02002992 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002993 ret = acl_pass(ret);
2994 if (rule->cond->pol == ACL_COND_UNLESS)
2995 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002996 if (!ret)
2997 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002998 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002999 if (!http_apply_redirect_rule(rule, s, txn))
3000 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003001 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003002 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003003
Willy Tarreau2be39392010-01-03 17:24:51 +01003004 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3005 * If this happens, then the data will not come immediately, so we must
3006 * send all what we have without waiting. Note that due to the small gain
3007 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003008 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003009 * itself once used.
3010 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003011 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003012
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003013 done: /* done with this analyser, continue with next ones that the calling
3014 * points will have set, if any.
3015 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003016 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003017 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3018 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003019 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003020
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003021 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003022 /* Allow cookie logging
3023 */
3024 if (s->be->cookie_name || sess->fe->capture_name)
3025 manage_client_side_cookies(s, req);
3026
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003027 /* When a connection is tarpitted, we use the tarpit timeout,
3028 * which may be the same as the connect timeout if unspecified.
3029 * If unset, then set it to zero because we really want it to
3030 * eventually expire. We build the tarpit as an analyser.
3031 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003032 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003033
3034 /* wipe the request out so that we can drop the connection early
3035 * if the client closes first.
3036 */
3037 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003038
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003039 txn->status = http_err_codes[deny_status];
3040
Christopher Faulet0184ea72017-01-05 14:06:34 +01003041 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003042 req->analysers |= AN_REQ_HTTP_TARPIT;
3043 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3044 if (!req->analyse_exp)
3045 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003046 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003047 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003048 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003049 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003050 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003051 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003052 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003053
3054 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003055
3056 /* Allow cookie logging
3057 */
3058 if (s->be->cookie_name || sess->fe->capture_name)
3059 manage_client_side_cookies(s, req);
3060
Willy Tarreau0b748332014-04-29 00:13:29 +02003061 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003062 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003063 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003064 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003065 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003066 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003067 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003068 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003069 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003070 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003071 goto return_prx_cond;
3072
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003073 return_bad_req:
3074 /* We centralize bad requests processing here */
3075 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3076 /* we detected a parsing error. We want to archive this request
3077 * in the dedicated proxy area for later troubleshooting.
3078 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003079 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003080 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003081
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003082 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003083 txn->req.msg_state = HTTP_MSG_ERROR;
3084 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003085 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003086
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003087 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003088 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003089 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003090
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003091 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003092 if (!(s->flags & SF_ERR_MASK))
3093 s->flags |= SF_ERR_PRXCOND;
3094 if (!(s->flags & SF_FINST_MASK))
3095 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003096
Christopher Faulet0184ea72017-01-05 14:06:34 +01003097 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098 req->analyse_exp = TICK_ETERNITY;
3099 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003100
3101 return_prx_yield:
3102 channel_dont_connect(req);
3103 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003104}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003105
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003106/* This function performs all the processing enabled for the current request.
3107 * It returns 1 if the processing can continue on next analysers, or zero if it
3108 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003109 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003110 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003111int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003112{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003113 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003114 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003115 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003116 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003117
Willy Tarreau655dce92009-11-08 13:10:58 +01003118 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003119 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003120 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003121 return 0;
3122 }
3123
Christopher Faulet45073512018-07-20 10:16:29 +02003124 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003125 now_ms, __FUNCTION__,
3126 s,
3127 req,
3128 req->rex, req->wex,
3129 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003130 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003131 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003132
Willy Tarreau59234e92008-11-30 23:51:27 +01003133 /*
3134 * Right now, we know that we have processed the entire headers
3135 * and that unwanted requests have been filtered out. We can do
3136 * whatever we want with the remaining request. Also, now we
3137 * may have separate values for ->fe, ->be.
3138 */
Willy Tarreau06619262006-12-17 08:37:22 +01003139
Willy Tarreau59234e92008-11-30 23:51:27 +01003140 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003141 * If HTTP PROXY is set we simply get remote server address parsing
3142 * incoming request. Note that this requires that a connection is
3143 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003144 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003145 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003146 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003147 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003148
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003149 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003150 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003151 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003152 txn->req.msg_state = HTTP_MSG_ERROR;
3153 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003154 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003155 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003156
Willy Tarreaue7dff022015-04-03 01:14:29 +02003157 if (!(s->flags & SF_ERR_MASK))
3158 s->flags |= SF_ERR_RESOURCE;
3159 if (!(s->flags & SF_FINST_MASK))
3160 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003161
3162 return 0;
3163 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003164
Willy Tarreau6b952c82018-09-10 17:45:34 +02003165 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003166 if (url2sa(ci_head(req) + msg->sl.rq.u,
3167 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003168 &conn->addr.to, NULL) == -1)
3169 goto return_bad_req;
3170
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003171 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003172 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3173 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003174 * u_l characters by a single "/".
3175 */
3176 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003177 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003178 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3179 int delta;
3180
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003181 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003182 http_msg_move_end(&txn->req, delta);
3183 cur_end += delta;
3184 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3185 goto return_bad_req;
3186 }
3187 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003188 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003189 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3190 int delta;
3191
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003192 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003193 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003194 http_msg_move_end(&txn->req, delta);
3195 cur_end += delta;
3196 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3197 goto return_bad_req;
3198 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003199 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003200
Willy Tarreau59234e92008-11-30 23:51:27 +01003201 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003202 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003203 * Note that doing so might move headers in the request, but
3204 * the fields will stay coherent and the URI will not move.
3205 * This should only be performed in the backend.
3206 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003207 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003208 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003209
William Lallemanda73203e2012-03-12 12:48:57 +01003210 /* add unique-id if "header-unique-id" is specified */
3211
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003212 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003213 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003214 goto return_bad_req;
3215 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003216 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003217 }
William Lallemanda73203e2012-03-12 12:48:57 +01003218
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003219 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003220 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003221 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003222 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003223 goto return_bad_req;
3224 }
3225
Cyril Bontéb21570a2009-11-29 20:04:48 +01003226 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003227 * 9: add X-Forwarded-For if either the frontend or the backend
3228 * asks for it.
3229 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003230 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003231 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003232 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3233 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3234 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003235 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003236 /* The header is set to be added only if none is present
3237 * and we found it, so don't do anything.
3238 */
3239 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003240 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003241 /* Add an X-Forwarded-For header unless the source IP is
3242 * in the 'except' network range.
3243 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003244 if ((!sess->fe->except_mask.s_addr ||
3245 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3246 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003247 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003248 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003249 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003250 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003251 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003252 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003253
3254 /* Note: we rely on the backend to get the header name to be used for
3255 * x-forwarded-for, because the header is really meant for the backends.
3256 * However, if the backend did not specify any option, we have to rely
3257 * on the frontend's header name.
3258 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003259 if (s->be->fwdfor_hdr_len) {
3260 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003261 memcpy(trash.area,
3262 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003263 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003264 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003265 memcpy(trash.area,
3266 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003267 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003268 len += snprintf(trash.area + len,
3269 trash.size - len,
3270 ": %d.%d.%d.%d", pn[0], pn[1],
3271 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003272
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003273 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003274 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003275 }
3276 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003277 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003278 /* FIXME: for the sake of completeness, we should also support
3279 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003280 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003281 int len;
3282 char pn[INET6_ADDRSTRLEN];
3283 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003284 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003285 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003286
Willy Tarreau59234e92008-11-30 23:51:27 +01003287 /* Note: we rely on the backend to get the header name to be used for
3288 * x-forwarded-for, because the header is really meant for the backends.
3289 * However, if the backend did not specify any option, we have to rely
3290 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003291 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003292 if (s->be->fwdfor_hdr_len) {
3293 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003294 memcpy(trash.area, s->be->fwdfor_hdr_name,
3295 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003296 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003297 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003298 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3299 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003300 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003301 len += snprintf(trash.area + len, trash.size - len,
3302 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003303
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003304 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003305 goto return_bad_req;
3306 }
3307 }
3308
3309 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003310 * 10: add X-Original-To if either the frontend or the backend
3311 * asks for it.
3312 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003313 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003314
3315 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003316 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003317 /* Add an X-Original-To header unless the destination IP is
3318 * in the 'except' network range.
3319 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003320 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003321
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003322 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003323 ((!sess->fe->except_mask_to.s_addr ||
3324 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3325 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003326 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003327 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003328 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003329 int len;
3330 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003331 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003332
3333 /* Note: we rely on the backend to get the header name to be used for
3334 * x-original-to, because the header is really meant for the backends.
3335 * However, if the backend did not specify any option, we have to rely
3336 * on the frontend's header name.
3337 */
3338 if (s->be->orgto_hdr_len) {
3339 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003340 memcpy(trash.area,
3341 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003342 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003343 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003344 memcpy(trash.area,
3345 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003346 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003347 len += snprintf(trash.area + len,
3348 trash.size - len,
3349 ": %d.%d.%d.%d", pn[0], pn[1],
3350 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003351
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003352 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003353 goto return_bad_req;
3354 }
3355 }
3356 }
3357
Willy Tarreau50fc7772012-11-11 22:19:57 +01003358 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3359 * If an "Upgrade" token is found, the header is left untouched in order not to have
3360 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3361 * "Upgrade" is present in the Connection header.
3362 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003363 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003364 unsigned int want_flags = 0;
3365
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003366 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003367 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003368 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003369 want_flags |= TX_CON_CLO_SET;
3370 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003371 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003372 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003373 want_flags |= TX_CON_KAL_SET;
3374 }
3375
3376 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003377 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003378 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003379
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003380
Willy Tarreau522d6c02009-12-06 18:49:18 +01003381 /* If we have no server assigned yet and we're balancing on url_param
3382 * with a POST request, we may be interested in checking the body for
3383 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003384 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003385 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003386 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003387 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003388 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003389 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003390 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003391
Christopher Fauletbe821b92017-03-30 11:21:53 +02003392 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3393 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003394#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003395 /* We expect some data from the client. Unless we know for sure
3396 * we already have a full request, we have to re-enable quick-ack
3397 * in case we previously disabled it, otherwise we might cause
3398 * the client to delay further data.
3399 */
3400 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3401 cli_conn && conn_ctrl_ready(cli_conn) &&
3402 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003403 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003404 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003405#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003406
Willy Tarreau59234e92008-11-30 23:51:27 +01003407 /*************************************************************
3408 * OK, that's finished for the headers. We have done what we *
3409 * could. Let's switch to the DATA state. *
3410 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003411 req->analyse_exp = TICK_ETERNITY;
3412 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003413
Willy Tarreau59234e92008-11-30 23:51:27 +01003414 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003415 /* OK let's go on with the BODY now */
3416 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003417
Willy Tarreau59234e92008-11-30 23:51:27 +01003418 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003419 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003420 /* we detected a parsing error. We want to archive this request
3421 * in the dedicated proxy area for later troubleshooting.
3422 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003423 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003424 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003425
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003426 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003427 txn->req.msg_state = HTTP_MSG_ERROR;
3428 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003429 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003430 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003431
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003432 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003433 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003434 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003435
Willy Tarreaue7dff022015-04-03 01:14:29 +02003436 if (!(s->flags & SF_ERR_MASK))
3437 s->flags |= SF_ERR_PRXCOND;
3438 if (!(s->flags & SF_FINST_MASK))
3439 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003440 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003441}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003442
Willy Tarreau60b85b02008-11-30 23:28:40 +01003443/* This function is an analyser which processes the HTTP tarpit. It always
3444 * returns zero, at the beginning because it prevents any other processing
3445 * from occurring, and at the end because it terminates the request.
3446 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003447int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003448{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003449 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003450
3451 /* This connection is being tarpitted. The CLIENT side has
3452 * already set the connect expiration date to the right
3453 * timeout. We just have to check that the client is still
3454 * there and that the timeout has not expired.
3455 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003456 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003457 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003458 !tick_is_expired(req->analyse_exp, now_ms))
3459 return 0;
3460
3461 /* We will set the queue timer to the time spent, just for
3462 * logging purposes. We fake a 500 server error, so that the
3463 * attacker will not suspect his connection has been tarpitted.
3464 * It will not cause trouble to the logs because we can exclude
3465 * the tarpitted connections by filtering on the 'PT' status flags.
3466 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003467 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3468
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003469 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003470 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003471
Christopher Faulet0184ea72017-01-05 14:06:34 +01003472 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003473 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003474
Willy Tarreaue7dff022015-04-03 01:14:29 +02003475 if (!(s->flags & SF_ERR_MASK))
3476 s->flags |= SF_ERR_PRXCOND;
3477 if (!(s->flags & SF_FINST_MASK))
3478 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003479 return 0;
3480}
3481
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003482/* This function is an analyser which waits for the HTTP request body. It waits
3483 * for either the buffer to be full, or the full advertised contents to have
3484 * reached the buffer. It must only be called after the standard HTTP request
3485 * processing has occurred, because it expects the request to be parsed and will
3486 * look for the Expect header. It may send a 100-Continue interim response. It
3487 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3488 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3489 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003490 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003491int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003492{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003493 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003494 struct http_txn *txn = s->txn;
3495 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003496
3497 /* We have to parse the HTTP request body to find any required data.
3498 * "balance url_param check_post" should have been the only way to get
3499 * into this. We were brought here after HTTP header analysis, so all
3500 * related structures are ready.
3501 */
3502
Willy Tarreau890988f2014-04-10 11:59:33 +02003503 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3504 /* This is the first call */
3505 if (msg->msg_state < HTTP_MSG_BODY)
3506 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003507
Willy Tarreau890988f2014-04-10 11:59:33 +02003508 if (msg->msg_state < HTTP_MSG_100_SENT) {
3509 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3510 * send an HTTP/1.1 100 Continue intermediate response.
3511 */
3512 if (msg->flags & HTTP_MSGF_VER_11) {
3513 struct hdr_ctx ctx;
3514 ctx.idx = 0;
3515 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003516 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003517 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003518 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003519 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003520 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003521 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003522 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003523 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003524
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003525 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003526 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003527 * must save the body in msg->next because it survives buffer
3528 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003529 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003530 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003531
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003532 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003533 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3534 else
3535 msg->msg_state = HTTP_MSG_DATA;
3536 }
3537
Willy Tarreau890988f2014-04-10 11:59:33 +02003538 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3539 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003540 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003541 goto missing_data;
3542
3543 /* OK we have everything we need now */
3544 goto http_end;
3545 }
3546
3547 /* OK here we're parsing a chunked-encoded message */
3548
Willy Tarreau522d6c02009-12-06 18:49:18 +01003549 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003550 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003551 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003552 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003553 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003554 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003555 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003556
Willy Tarreau115acb92009-12-26 13:56:06 +01003557 if (!ret)
3558 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003559 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003560 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003561 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003562 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003563 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003564 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003565 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003566
3567 msg->chunk_len = chunk;
3568 msg->body_len += chunk;
3569
3570 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003571 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003572 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003573 }
3574
Willy Tarreaud98cf932009-12-27 22:54:55 +01003575 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003576 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3577 * for at least a whole chunk or the whole content length bytes after
3578 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003579 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003580 if (msg->msg_state == HTTP_MSG_TRAILERS)
3581 goto http_end;
3582
Willy Tarreaue115b492015-05-01 23:05:14 +02003583 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003584 goto http_end;
3585
3586 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003587 /* we get here if we need to wait for more data. If the buffer is full,
3588 * we have the maximum we can expect.
3589 */
Willy Tarreau23752332018-06-15 14:54:53 +02003590 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003591 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003592
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003593 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003594 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003595 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003596
Willy Tarreaue7dff022015-04-03 01:14:29 +02003597 if (!(s->flags & SF_ERR_MASK))
3598 s->flags |= SF_ERR_CLITO;
3599 if (!(s->flags & SF_FINST_MASK))
3600 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003601 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003602 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003603
3604 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003605 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003606 /* Not enough data. We'll re-use the http-request
3607 * timeout here. Ideally, we should set the timeout
3608 * relative to the accept() date. We just set the
3609 * request timeout once at the beginning of the
3610 * request.
3611 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003612 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003613 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003614 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003615 return 0;
3616 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003617
3618 http_end:
3619 /* The situation will not evolve, so let's give up on the analysis. */
3620 s->logs.tv_request = now; /* update the request timer to reflect full request */
3621 req->analysers &= ~an_bit;
3622 req->analyse_exp = TICK_ETERNITY;
3623 return 1;
3624
3625 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003626 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003627 txn->req.msg_state = HTTP_MSG_ERROR;
3628 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003629 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003630
Willy Tarreaue7dff022015-04-03 01:14:29 +02003631 if (!(s->flags & SF_ERR_MASK))
3632 s->flags |= SF_ERR_PRXCOND;
3633 if (!(s->flags & SF_FINST_MASK))
3634 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003635
Willy Tarreau522d6c02009-12-06 18:49:18 +01003636 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003637 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003638 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003639 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003640 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003641 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003642}
3643
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003644/* send a server's name with an outgoing request over an established connection.
3645 * Note: this function is designed to be called once the request has been scheduled
3646 * for being forwarded. This is the reason why it rewinds the buffer before
3647 * proceeding.
3648 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003649int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003650
3651 struct hdr_ctx ctx;
3652
Mark Lamourinec2247f02012-01-04 13:02:01 -05003653 char *hdr_name = be->server_id_hdr_name;
3654 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003655 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003656 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003657 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003658
William Lallemandd9e90662012-01-30 17:27:17 +01003659 ctx.idx = 0;
3660
Willy Tarreau211cdec2014-04-17 20:18:08 +02003661 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003662 if (old_o) {
3663 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003664 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003665 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003666 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003667 }
3668
Willy Tarreauf37954d2018-06-15 18:31:02 +02003669 old_i = ci_data(chn);
3670 while (http_find_header2(hdr_name, hdr_name_len, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003671 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003672 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003673 }
3674
3675 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003676 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003677 memcpy(hdr_val, hdr_name, hdr_name_len);
3678 hdr_val += hdr_name_len;
3679 *hdr_val++ = ':';
3680 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003681 hdr_val += strlcpy2(hdr_val, srv_name,
3682 trash.area + trash.size - hdr_val);
3683 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3684 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003685
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003686 if (old_o) {
3687 /* If this was a forwarded request, we must readjust the amount of
3688 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003689 * variations. Note that the current state is >= HTTP_MSG_BODY,
3690 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003691 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003692 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003693 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003694 txn->req.next -= old_o;
3695 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003696 }
3697
Mark Lamourinec2247f02012-01-04 13:02:01 -05003698 return 0;
3699}
3700
Willy Tarreau610ecce2010-01-04 21:15:02 +01003701/* Terminate current transaction and prepare a new one. This is very tricky
3702 * right now but it works.
3703 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003704void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003705{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003706 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003707 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003708 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003709 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003710 struct connection *srv_conn;
3711 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003712 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003713
Willy Tarreau610ecce2010-01-04 21:15:02 +01003714 /* FIXME: We need a more portable way of releasing a backend's and a
3715 * server's connections. We need a safer way to reinitialize buffer
3716 * flags. We also need a more accurate method for computing per-request
3717 * data.
3718 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003719 /*
3720 * XXX cognet: This is probably wrong, this is killing a whole
3721 * connection, in the new world order, we probably want to just kill
3722 * the stream, this is to be revisited the day we handle multiple
3723 * streams in one server connection.
3724 */
3725 cs = objt_cs(s->si[1].end);
3726 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003727
Willy Tarreau4213a112013-12-15 10:25:42 +01003728 /* unless we're doing keep-alive, we want to quickly close the connection
3729 * to the server.
3730 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003731 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003732 !si_conn_ready(&s->si[1])) {
3733 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3734 si_shutr(&s->si[1]);
3735 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003736 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003737
Willy Tarreaue7dff022015-04-03 01:14:29 +02003738 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003739 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003740 if (unlikely(s->srv_conn))
3741 sess_change_server(s, NULL);
3742 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003743
3744 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003745 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003746
Willy Tarreaueee5b512015-04-03 23:46:31 +02003747 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003748 int n;
3749
Willy Tarreaueee5b512015-04-03 23:46:31 +02003750 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003751 if (n < 1 || n > 5)
3752 n = 0;
3753
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003754 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003755 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003756 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003757 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003758 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003759 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3760 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003761 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003762 }
3763
3764 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003765 s->logs.bytes_in -= ci_data(&s->req);
3766 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003767
Willy Tarreau66425e32018-07-25 06:55:12 +02003768 /* we may need to know the position in the queue */
3769 pendconn_free(s);
3770
Willy Tarreau610ecce2010-01-04 21:15:02 +01003771 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003772 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003773 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003774 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003775 s->do_log(s);
3776 }
3777
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003778 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003779 stream_stop_content_counters(s);
3780 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003781
Willy Tarreau610ecce2010-01-04 21:15:02 +01003782 s->logs.accept_date = date; /* user-visible date for logging */
3783 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003784 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3785 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003786 tv_zero(&s->logs.tv_request);
3787 s->logs.t_queue = -1;
3788 s->logs.t_connect = -1;
3789 s->logs.t_data = -1;
3790 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003791 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3792 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003793
Willy Tarreauf37954d2018-06-15 18:31:02 +02003794 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3795 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003796
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003797 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003798 if (s->flags & SF_CURR_SESS) {
3799 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003800 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003801 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003802 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003803 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003804 }
3805
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003806 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003807
Willy Tarreau4213a112013-12-15 10:25:42 +01003808 /* only release our endpoint if we don't intend to reuse the
3809 * connection.
3810 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003811 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003812 !si_conn_ready(&s->si[1])) {
3813 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02003814 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003815 }
3816
Willy Tarreau350f4872014-11-28 14:42:25 +01003817 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3818 s->si[1].err_type = SI_ET_NONE;
3819 s->si[1].conn_retries = 0; /* used for logging too */
3820 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003821 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 +01003822 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 +02003823 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 +02003824 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3825 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3826 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003827
Patrick Hemmere3faf022018-08-22 10:02:00 -04003828 hlua_ctx_destroy(s->hlua);
3829 s->hlua = NULL;
3830
Willy Tarreaueee5b512015-04-03 23:46:31 +02003831 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003832 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003833 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003834
3835 if (prev_status == 401 || prev_status == 407) {
3836 /* In HTTP keep-alive mode, if we receive a 401, we still have
3837 * a chance of being able to send the visitor again to the same
3838 * server over the same connection. This is required by some
3839 * broken protocols such as NTLM, and anyway whenever there is
3840 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003841 * it's better to do it (at least it helps with debugging), at
3842 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003843 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003844 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003845 }
3846
Willy Tarreau53f96852016-02-02 18:50:47 +01003847 /* Never ever allow to reuse a connection from a non-reuse backend */
3848 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3849 srv_conn->flags |= CO_FL_PRIVATE;
3850
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003851 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003852 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003853
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003854 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003855 s->req.flags |= CF_NEVER_WAIT;
3856 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003857 }
3858
Willy Tarreau714ea782015-11-25 20:11:11 +01003859 /* we're removing the analysers, we MUST re-enable events detection.
3860 * We don't enable close on the response channel since it's either
3861 * already closed, or in keep-alive with an idle connection handler.
3862 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003863 channel_auto_read(&s->req);
3864 channel_auto_close(&s->req);
3865 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003866
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003867 /* we're in keep-alive with an idle connection, monitor it if not already done */
3868 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003869 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02003870 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003871 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01003872 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003873 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02003874 else if (prev_flags & TX_NOT_FIRST)
3875 /* note: we check the request, not the connection, but
3876 * this is valid for strategies SAFE and AGGR, and in
3877 * case of ALWS, we don't care anyway.
3878 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003879 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02003880 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003881 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003882 }
Christopher Faulete6006242017-03-10 11:52:44 +01003883 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3884 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003885}
3886
3887
3888/* This function updates the request state machine according to the response
3889 * state machine and buffer flags. It returns 1 if it changes anything (flag
3890 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3891 * it is only used to find when a request/response couple is complete. Both
3892 * this function and its equivalent should loop until both return zero. It
3893 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3894 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003895int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003896{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003897 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003898 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003899 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003900 unsigned int old_state = txn->req.msg_state;
3901
Christopher Faulet4be98032017-07-18 10:48:24 +02003902 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003903 return 0;
3904
3905 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003906 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003907 * We can shut the read side unless we want to abort_on_close,
3908 * or we have a POST request. The issue with POST requests is
3909 * that some browsers still send a CRLF after the request, and
3910 * this CRLF must be read so that it does not remain in the kernel
3911 * buffers, otherwise a close could cause an RST on some systems
3912 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003913 * Note that if we're using keep-alive on the client side, we'd
3914 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003915 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003916 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003917 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003918 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3919 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003920 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3921 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003922 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003923 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003924
Willy Tarreau40f151a2012-12-20 12:10:09 +01003925 /* if the server closes the connection, we want to immediately react
3926 * and close the socket to save packets and syscalls.
3927 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003928 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003929
Willy Tarreau7f876a12015-11-18 11:59:55 +01003930 /* In any case we've finished parsing the request so we must
3931 * disable Nagle when sending data because 1) we're not going
3932 * to shut this side, and 2) the server is waiting for us to
3933 * send pending data.
3934 */
3935 chn->flags |= CF_NEVER_WAIT;
3936
Willy Tarreau610ecce2010-01-04 21:15:02 +01003937 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3938 goto wait_other_side;
3939
3940 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3941 /* The server has not finished to respond, so we
3942 * don't want to move in order not to upset it.
3943 */
3944 goto wait_other_side;
3945 }
3946
Willy Tarreau610ecce2010-01-04 21:15:02 +01003947 /* When we get here, it means that both the request and the
3948 * response have finished receiving. Depending on the connection
3949 * mode, we'll have to wait for the last bytes to leave in either
3950 * direction, and sometimes for a close to be effective.
3951 */
3952
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003953 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3954 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003955 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3956 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003957 }
3958 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3959 /* Option forceclose is set, or either side wants to close,
3960 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003961 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003962 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003963 *
3964 * However, there is an exception if the response
3965 * length is undefined. In this case, we need to wait
3966 * the close from the server. The response will be
3967 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003968 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003969 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3970 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3971 goto check_channel_flags;
3972
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003973 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3974 channel_shutr_now(chn);
3975 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003976 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003977 }
3978 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003979 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3980 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003981 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003982 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3983 channel_auto_read(chn);
3984 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003985 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003986 }
3987
Christopher Faulet4be98032017-07-18 10:48:24 +02003988 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003989 }
3990
3991 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3992 http_msg_closing:
3993 /* nothing else to forward, just waiting for the output buffer
3994 * to be empty and for the shutw_now to take effect.
3995 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003996 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003997 txn->req.msg_state = HTTP_MSG_CLOSED;
3998 goto http_msg_closed;
3999 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004000 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004001 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004002 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004003 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004004 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004005 }
4006
4007 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4008 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004009 /* if we don't know whether the server will close, we need to hard close */
4010 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4011 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4012
Willy Tarreau3988d932013-12-27 23:03:08 +01004013 /* see above in MSG_DONE why we only do this in these states */
4014 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4015 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004016 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4017 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004018 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004019 goto wait_other_side;
4020 }
4021
Christopher Faulet4be98032017-07-18 10:48:24 +02004022 check_channel_flags:
4023 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4024 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4025 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004026 txn->req.msg_state = HTTP_MSG_CLOSING;
4027 goto http_msg_closing;
4028 }
4029
4030
Willy Tarreau610ecce2010-01-04 21:15:02 +01004031 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004032 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033}
4034
4035
4036/* This function updates the response state machine according to the request
4037 * state machine and buffer flags. It returns 1 if it changes anything (flag
4038 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4039 * it is only used to find when a request/response couple is complete. Both
4040 * this function and its equivalent should loop until both return zero. It
4041 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4042 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004043int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004044{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004045 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004046 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004047 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004048 unsigned int old_state = txn->rsp.msg_state;
4049
Christopher Faulet4be98032017-07-18 10:48:24 +02004050 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004051 return 0;
4052
4053 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4054 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004055 * still monitor the server connection for a possible close
4056 * while the request is being uploaded, so we don't disable
4057 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004058 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004059 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004060
4061 if (txn->req.msg_state == HTTP_MSG_ERROR)
4062 goto wait_other_side;
4063
4064 if (txn->req.msg_state < HTTP_MSG_DONE) {
4065 /* The client seems to still be sending data, probably
4066 * because we got an error response during an upload.
4067 * We have the choice of either breaking the connection
4068 * or letting it pass through. Let's do the later.
4069 */
4070 goto wait_other_side;
4071 }
4072
Willy Tarreau610ecce2010-01-04 21:15:02 +01004073 /* When we get here, it means that both the request and the
4074 * response have finished receiving. Depending on the connection
4075 * mode, we'll have to wait for the last bytes to leave in either
4076 * direction, and sometimes for a close to be effective.
4077 */
4078
4079 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4080 /* Server-close mode : shut read and wait for the request
4081 * side to close its output buffer. The caller will detect
4082 * when we're in DONE and the other is in CLOSED and will
4083 * catch that for the final cleanup.
4084 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004085 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4086 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004087 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004088 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4089 /* Option forceclose is set, or either side wants to close,
4090 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004091 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004092 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004093 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004094 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004095 channel_shutr_now(chn);
4096 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004097 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004098 }
4099 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004100 /* The last possible modes are keep-alive and tunnel. Tunnel will
4101 * need to forward remaining data. Keep-alive will need to monitor
4102 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004103 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004104 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004105 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004106 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4107 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004108 }
4109
Christopher Faulet4be98032017-07-18 10:48:24 +02004110 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004111 }
4112
4113 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4114 http_msg_closing:
4115 /* nothing else to forward, just waiting for the output buffer
4116 * to be empty and for the shutw_now to take effect.
4117 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004118 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004119 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4120 goto http_msg_closed;
4121 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004122 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004123 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004124 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004125 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004126 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004127 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004128 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004129 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004130 }
4131
4132 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4133 http_msg_closed:
4134 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004135 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004136 channel_auto_close(chn);
4137 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004138 goto wait_other_side;
4139 }
4140
Christopher Faulet4be98032017-07-18 10:48:24 +02004141 check_channel_flags:
4142 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4143 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4144 /* if we've just closed an output, let's switch */
4145 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4146 goto http_msg_closing;
4147 }
4148
Willy Tarreau610ecce2010-01-04 21:15:02 +01004149 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004150 /* We force the response to leave immediately if we're waiting for the
4151 * other side, since there is no pending shutdown to push it out.
4152 */
4153 if (!channel_is_empty(chn))
4154 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004155 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004156}
4157
4158
Christopher Faulet894da4c2017-07-18 11:29:07 +02004159/* Resync the request and response state machines. */
4160void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004161{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004162 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004163#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004164 int old_req_state = txn->req.msg_state;
4165 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004166#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004167
Willy Tarreau610ecce2010-01-04 21:15:02 +01004168 http_sync_req_state(s);
4169 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004170 if (!http_sync_res_state(s))
4171 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004172 if (!http_sync_req_state(s))
4173 break;
4174 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004175
Christopher Faulet894da4c2017-07-18 11:29:07 +02004176 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4177 "req->analysers=0x%08x res->analysers=0x%08x\n",
4178 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004179 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4180 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004181 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004182
4183
Willy Tarreau610ecce2010-01-04 21:15:02 +01004184 /* OK, both state machines agree on a compatible state.
4185 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004186 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4187 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004188 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4189 * means we must abort the request.
4190 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4191 * corresponding channel.
4192 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4193 * on the response with server-close mode means we've completed one
4194 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004195 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004196 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4197 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004198 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004199 channel_auto_close(&s->req);
4200 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004201 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004202 channel_auto_close(&s->res);
4203 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004204 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004205 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4206 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004207 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004208 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004209 channel_auto_close(&s->res);
4210 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004211 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004212 channel_abort(&s->req);
4213 channel_auto_close(&s->req);
4214 channel_auto_read(&s->req);
4215 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004216 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004217 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4218 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4219 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4220 s->req.analysers &= AN_REQ_FLT_END;
4221 if (HAS_REQ_DATA_FILTERS(s))
4222 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4223 }
4224 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4225 s->res.analysers &= AN_RES_FLT_END;
4226 if (HAS_RSP_DATA_FILTERS(s))
4227 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4228 }
4229 channel_auto_close(&s->req);
4230 channel_auto_read(&s->req);
4231 channel_auto_close(&s->res);
4232 channel_auto_read(&s->res);
4233 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004234 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4235 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004236 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004237 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4238 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4239 /* server-close/keep-alive: terminate this transaction,
4240 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004241 * a fresh-new transaction, but only once we're sure there's
4242 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004243 * another request. They must not hold any pending output data
4244 * and the response buffer must realigned
4245 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004246 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004247 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004248 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004249 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004250 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004251 else {
4252 s->req.analysers = AN_REQ_FLT_END;
4253 s->res.analysers = AN_RES_FLT_END;
4254 txn->flags |= TX_WAIT_CLEANUP;
4255 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004256 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004257}
4258
Willy Tarreaud98cf932009-12-27 22:54:55 +01004259/* This function is an analyser which forwards request body (including chunk
4260 * sizes if any). It is called as soon as we must forward, even if we forward
4261 * zero byte. The only situation where it must not be called is when we're in
4262 * tunnel mode and we want to forward till the close. It's used both to forward
4263 * remaining data and to resync after end of body. It expects the msg_state to
4264 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004265 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004266 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004267 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004268 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004269int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004270{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004271 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004272 struct http_txn *txn = s->txn;
4273 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004274 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004275
Christopher Faulet45073512018-07-20 10:16:29 +02004276 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 +02004277 now_ms, __FUNCTION__,
4278 s,
4279 req,
4280 req->rex, req->wex,
4281 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004282 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004283 req->analysers);
4284
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004285 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4286 return 0;
4287
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004288 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004289 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004290 /* Output closed while we were sending data. We must abort and
4291 * wake the other side up.
4292 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004293 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004294 msg->msg_state = HTTP_MSG_ERROR;
4295 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004296 return 1;
4297 }
4298
Willy Tarreaud98cf932009-12-27 22:54:55 +01004299 /* Note that we don't have to send 100-continue back because we don't
4300 * need the data to complete our job, and it's up to the server to
4301 * decide whether to return 100, 417 or anything else in return of
4302 * an "Expect: 100-continue" header.
4303 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004304 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004305 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4306 ? HTTP_MSG_CHUNK_SIZE
4307 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004308
4309 /* TODO/filters: when http-buffer-request option is set or if a
4310 * rule on url_param exists, the first chunk size could be
4311 * already parsed. In that case, msg->next is after the chunk
4312 * size (including the CRLF after the size). So this case should
4313 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004314 }
4315
Willy Tarreau7ba23542014-04-17 21:50:00 +02004316 /* Some post-connect processing might want us to refrain from starting to
4317 * forward data. Currently, the only reason for this is "balance url_param"
4318 * whichs need to parse/process the request after we've enabled forwarding.
4319 */
4320 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004321 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004322 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004323 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004324 channel_dont_close(req); /* don't fail on early shutr */
4325 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004326 }
4327 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4328 }
4329
Willy Tarreau80a92c02014-03-12 10:41:13 +01004330 /* in most states, we should abort in case of early close */
4331 channel_auto_close(req);
4332
Willy Tarreauefdf0942014-04-24 20:08:57 +02004333 if (req->to_forward) {
4334 /* We can't process the buffer's contents yet */
4335 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004336 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004337 }
4338
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004339 if (msg->msg_state < HTTP_MSG_DONE) {
4340 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4341 ? http_msg_forward_chunked_body(s, msg)
4342 : http_msg_forward_body(s, msg));
4343 if (!ret)
4344 goto missing_data_or_waiting;
4345 if (ret < 0)
4346 goto return_bad_req;
4347 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004348
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004349 /* other states, DONE...TUNNEL */
4350 /* we don't want to forward closes on DONE except in tunnel mode. */
4351 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4352 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004353
Christopher Faulet894da4c2017-07-18 11:29:07 +02004354 http_resync_states(s);
4355 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004356 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4357 if (req->flags & CF_SHUTW) {
4358 /* request errors are most likely due to the
4359 * server aborting the transfer. */
4360 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004361 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004362 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004363 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004364 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004365 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004366 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004367 }
4368
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004369 /* If "option abortonclose" is set on the backend, we want to monitor
4370 * the client's connection and forward any shutdown notification to the
4371 * server, which will decide whether to close or to go on processing the
4372 * request. We only do that in tunnel mode, and not in other modes since
4373 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004374 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004375 channel_auto_read(req);
4376 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4377 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4378 s->si[1].flags |= SI_FL_NOLINGER;
4379 channel_auto_close(req);
4380 }
4381 else if (s->txn->meth == HTTP_METH_POST) {
4382 /* POST requests may require to read extra CRLF sent by broken
4383 * browsers and which could cause an RST to be sent upon close
4384 * on some systems (eg: Linux). */
4385 channel_auto_read(req);
4386 }
4387 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004388
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004389 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004390 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004391 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004392 if (!(s->flags & SF_ERR_MASK))
4393 s->flags |= SF_ERR_CLICL;
4394 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004395 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004396 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004397 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004398 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004399 }
4400
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004401 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4402 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004403 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004404 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004405
4406 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004407 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004408
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004409 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004410 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004411 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004412 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004413
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004414 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004415 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004416 * And when content-length is used, we never want to let the possible
4417 * shutdown be forwarded to the other side, as the state machine will
4418 * take care of it once the client responds. It's also important to
4419 * prevent TIME_WAITs from accumulating on the backend side, and for
4420 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004421 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004422 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004423 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004424
Willy Tarreau5c620922011-05-11 19:56:11 +02004425 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004426 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004427 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004428 * modes are already handled by the stream sock layer. We must not do
4429 * this in content-length mode because it could present the MSG_MORE
4430 * flag with the last block of forwarded data, which would cause an
4431 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004432 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004433 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004434 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004435
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004436 return 0;
4437
Willy Tarreaud98cf932009-12-27 22:54:55 +01004438 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004439 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004440 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004441 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004442
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004443 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004444 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004445 txn->req.msg_state = HTTP_MSG_ERROR;
4446 if (txn->status) {
4447 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004448 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004449 } else {
4450 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004451 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004452 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004453 req->analysers &= AN_REQ_FLT_END;
4454 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 +01004455
Willy Tarreaue7dff022015-04-03 01:14:29 +02004456 if (!(s->flags & SF_ERR_MASK))
4457 s->flags |= SF_ERR_PRXCOND;
4458 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004459 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004460 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004461 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004462 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004463 }
4464 return 0;
4465
4466 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004467 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004468 txn->req.msg_state = HTTP_MSG_ERROR;
4469 if (txn->status) {
4470 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004471 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004472 } else {
4473 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004474 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004475 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004476 req->analysers &= AN_REQ_FLT_END;
4477 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004478
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004479 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4480 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004481 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004482 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004483
Willy Tarreaue7dff022015-04-03 01:14:29 +02004484 if (!(s->flags & SF_ERR_MASK))
4485 s->flags |= SF_ERR_SRVCL;
4486 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004487 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004488 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004489 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004490 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004491 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004492 return 0;
4493}
4494
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004495/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4496 * processing can continue on next analysers, or zero if it either needs more
4497 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004498 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004499 * when it has nothing left to do, and may remove any analyser when it wants to
4500 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004501 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004502int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004503{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004504 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004505 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004506 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004507 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004508 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004509 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004510 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004511 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004512
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004513 srv_conn = cs_conn(objt_cs(s->si[1].end));
4514
Christopher Faulet45073512018-07-20 10:16:29 +02004515 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 +02004516 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004517 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004518 rep,
4519 rep->rex, rep->wex,
4520 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004521 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004522 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004523
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004524 /*
4525 * Now parse the partial (or complete) lines.
4526 * We will check the response syntax, and also join multi-line
4527 * headers. An index of all the lines will be elaborated while
4528 * parsing.
4529 *
4530 * For the parsing, we use a 28 states FSM.
4531 *
4532 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004533 * ci_head(rep) = beginning of response
4534 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4535 * ci_tail(rep) = end of input data
4536 * msg->eol = end of current header or line (LF or CRLF)
4537 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004538 */
4539
Willy Tarreau628c40c2014-04-24 19:11:26 +02004540 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004541 /* There's a protected area at the end of the buffer for rewriting
4542 * purposes. We don't want to start to parse the request if the
4543 * protected area is affected, because we may have to move processed
4544 * data later, which is much more complicated.
4545 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004546 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004547 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004548 /* some data has still not left the buffer, wake us once that's done */
4549 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4550 goto abort_response;
4551 channel_dont_close(rep);
4552 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004553 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004554 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004555 }
4556
Willy Tarreau188e2302018-06-15 11:11:53 +02004557 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004558 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004559 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004560
Willy Tarreauf37954d2018-06-15 18:31:02 +02004561 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004562 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004563 }
4564
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004565 /* 1: we might have to print this header in debug mode */
4566 if (unlikely((global.mode & MODE_DEBUG) &&
4567 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004568 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004569 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004570
Willy Tarreauf37954d2018-06-15 18:31:02 +02004571 sol = ci_head(rep);
4572 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004573 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004574
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004575 sol += hdr_idx_first_pos(&txn->hdr_idx);
4576 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004577
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004578 while (cur_idx) {
4579 eol = sol + txn->hdr_idx.v[cur_idx].len;
4580 debug_hdr("srvhdr", s, sol, eol);
4581 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4582 cur_idx = txn->hdr_idx.v[cur_idx].next;
4583 }
4584 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004585
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004586 /*
4587 * Now we quickly check if we have found a full valid response.
4588 * If not so, we check the FD and buffer states before leaving.
4589 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004590 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004591 * responses are checked first.
4592 *
4593 * Depending on whether the client is still there or not, we
4594 * may send an error response back or not. Note that normally
4595 * we should only check for HTTP status there, and check I/O
4596 * errors somewhere else.
4597 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004598
Willy Tarreau655dce92009-11-08 13:10:58 +01004599 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004600 /* Invalid response */
4601 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4602 /* we detected a parsing error. We want to archive this response
4603 * in the dedicated proxy area for later troubleshooting.
4604 */
4605 hdr_response_bad:
4606 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004607 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004608
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004609 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004610 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004611 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004612 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004613 }
Willy Tarreau64648412010-03-05 10:41:54 +01004614 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004615 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004616 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004617 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004618 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004619 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004620 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004621
Willy Tarreaue7dff022015-04-03 01:14:29 +02004622 if (!(s->flags & SF_ERR_MASK))
4623 s->flags |= SF_ERR_PRXCOND;
4624 if (!(s->flags & SF_FINST_MASK))
4625 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004626
4627 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004628 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004629
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004630 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004631 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004632 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004633 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004634 goto hdr_response_bad;
4635 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004636
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004637 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004638 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004639 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004640 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004641 else if (txn->flags & TX_NOT_FIRST)
4642 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004643
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004644 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004645 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004646 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004647 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004648 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004649
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004650 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004651 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004652 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004653
4654 /* Check to see if the server refused the early data.
4655 * If so, just send a 425
4656 */
4657 if (objt_cs(s->si[1].end)) {
4658 struct connection *conn = objt_cs(s->si[1].end)->conn;
4659
4660 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4661 txn->status = 425;
4662 }
4663
Willy Tarreau350f4872014-11-28 14:42:25 +01004664 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004665 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004666 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004667
Willy Tarreaue7dff022015-04-03 01:14:29 +02004668 if (!(s->flags & SF_ERR_MASK))
4669 s->flags |= SF_ERR_SRVCL;
4670 if (!(s->flags & SF_FINST_MASK))
4671 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004672 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004673 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004674
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004675 /* read timeout : return a 504 to the client. */
4676 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004677 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004678 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004679
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004680 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004681 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004682 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004683 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004684 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004685
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004686 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004687 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004688 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004689 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004690 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004691 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004692
Willy Tarreaue7dff022015-04-03 01:14:29 +02004693 if (!(s->flags & SF_ERR_MASK))
4694 s->flags |= SF_ERR_SRVTO;
4695 if (!(s->flags & SF_FINST_MASK))
4696 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004697 return 0;
4698 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004699
Willy Tarreauf003d372012-11-26 13:35:37 +01004700 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004701 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004702 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4703 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004704 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004705 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004706
Christopher Faulet0184ea72017-01-05 14:06:34 +01004707 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004708 channel_auto_close(rep);
4709
4710 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004711 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004712 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004713
Willy Tarreaue7dff022015-04-03 01:14:29 +02004714 if (!(s->flags & SF_ERR_MASK))
4715 s->flags |= SF_ERR_CLICL;
4716 if (!(s->flags & SF_FINST_MASK))
4717 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004718
Willy Tarreau87b09662015-04-03 00:22:06 +02004719 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004720 return 0;
4721 }
4722
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004723 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004724 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004725 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004726 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004727 else if (txn->flags & TX_NOT_FIRST)
4728 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004729
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004730 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004731 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004732 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004733 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004734 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004735
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004736 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004737 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004738 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004739 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004740 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004741 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004742
Willy Tarreaue7dff022015-04-03 01:14:29 +02004743 if (!(s->flags & SF_ERR_MASK))
4744 s->flags |= SF_ERR_SRVCL;
4745 if (!(s->flags & SF_FINST_MASK))
4746 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004747 return 0;
4748 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004749
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004750 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004751 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004752 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004753 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004754 else if (txn->flags & TX_NOT_FIRST)
4755 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004756
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004757 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004758 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004759 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004760
Willy Tarreaue7dff022015-04-03 01:14:29 +02004761 if (!(s->flags & SF_ERR_MASK))
4762 s->flags |= SF_ERR_CLICL;
4763 if (!(s->flags & SF_FINST_MASK))
4764 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004765
Willy Tarreau87b09662015-04-03 00:22:06 +02004766 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004767 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004768 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004769
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004770 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004771 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004772 return 0;
4773 }
4774
4775 /* More interesting part now : we know that we have a complete
4776 * response which at least looks like HTTP. We have an indicator
4777 * of each header's length, so we can parse them quickly.
4778 */
4779
4780 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004781 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004782
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004783 /*
4784 * 1: get the status code
4785 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004786 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004787 if (n < 1 || n > 5)
4788 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004789 /* when the client triggers a 4xx from the server, it's most often due
4790 * to a missing object or permission. These events should be tracked
4791 * because if they happen often, it may indicate a brute force or a
4792 * vulnerability scan.
4793 */
4794 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004795 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004796
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004797 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004798 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004799
Willy Tarreau91852eb2015-05-01 13:26:00 +02004800 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4801 * exactly one digit "." one digit. This check may be disabled using
4802 * option accept-invalid-http-response.
4803 */
4804 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4805 if (msg->sl.st.v_l != 8) {
4806 msg->err_pos = 0;
4807 goto hdr_response_bad;
4808 }
4809
Willy Tarreauf37954d2018-06-15 18:31:02 +02004810 if (ci_head(rep)[4] != '/' ||
4811 !isdigit((unsigned char)ci_head(rep)[5]) ||
4812 ci_head(rep)[6] != '.' ||
4813 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004814 msg->err_pos = 4;
4815 goto hdr_response_bad;
4816 }
4817 }
4818
Willy Tarreau5b154472009-12-21 20:11:07 +01004819 /* check if the response is HTTP/1.1 or above */
4820 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004821 ((ci_head(rep)[5] > '1') ||
4822 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004823 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004824
4825 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004826 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 +01004827
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004828 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004829 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004830
Willy Tarreauf37954d2018-06-15 18:31:02 +02004831 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004832
Willy Tarreau39650402010-03-15 19:44:39 +01004833 /* Adjust server's health based on status code. Note: status codes 501
4834 * and 505 are triggered on demand by client request, so we must not
4835 * count them as server failures.
4836 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004837 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004838 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004839 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004840 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004841 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004842 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004843
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004844 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004845 * We may be facing a 100-continue response, or any other informational
4846 * 1xx response which is non-final, in which case this is not the right
4847 * response, and we're waiting for the next one. Let's allow this response
4848 * to go to the client and wait for the next one. There's an exception for
4849 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004850 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004851 if (txn->status < 200 &&
4852 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004853 hdr_idx_init(&txn->hdr_idx);
4854 msg->next -= channel_forward(rep, msg->next);
4855 msg->msg_state = HTTP_MSG_RPBEFORE;
4856 txn->status = 0;
4857 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004858 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004859 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004860 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004861
Willy Tarreaua14ad722017-07-07 11:36:32 +02004862 /*
4863 * 2: check for cacheability.
4864 */
4865
4866 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004867 case 200:
4868 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004869 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004870 case 206:
4871 case 300:
4872 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004873 case 404:
4874 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004875 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004876 case 414:
4877 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004878 break;
4879 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004880 /* RFC7231#6.1:
4881 * Responses with status codes that are defined as
4882 * cacheable by default (e.g., 200, 203, 204, 206,
4883 * 300, 301, 404, 405, 410, 414, and 501 in this
4884 * specification) can be reused by a cache with
4885 * heuristic expiration unless otherwise indicated
4886 * by the method definition or explicit cache
4887 * controls [RFC7234]; all other status codes are
4888 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004889 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004890 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004891 break;
4892 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004893
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004894 /*
4895 * 3: we may need to capture headers
4896 */
4897 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004898 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004899 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4900 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004901
Willy Tarreau557f1992015-05-01 10:05:17 +02004902 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4903 * by RFC7230#3.3.3 :
4904 *
4905 * The length of a message body is determined by one of the following
4906 * (in order of precedence):
4907 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004908 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4909 * the connection will become a tunnel immediately after the empty
4910 * line that concludes the header fields. A client MUST ignore
4911 * any Content-Length or Transfer-Encoding header fields received
4912 * in such a message. Any 101 response (Switching Protocols) is
4913 * managed in the same manner.
4914 *
4915 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004916 * (Informational), 204 (No Content), or 304 (Not Modified) status
4917 * code is always terminated by the first empty line after the
4918 * header fields, regardless of the header fields present in the
4919 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004920 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004921 * 3. If a Transfer-Encoding header field is present and the chunked
4922 * transfer coding (Section 4.1) is the final encoding, the message
4923 * body length is determined by reading and decoding the chunked
4924 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004925 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004926 * If a Transfer-Encoding header field is present in a response and
4927 * the chunked transfer coding is not the final encoding, the
4928 * message body length is determined by reading the connection until
4929 * it is closed by the server. If a Transfer-Encoding header field
4930 * is present in a request and the chunked transfer coding is not
4931 * the final encoding, the message body length cannot be determined
4932 * reliably; the server MUST respond with the 400 (Bad Request)
4933 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004934 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004935 * If a message is received with both a Transfer-Encoding and a
4936 * Content-Length header field, the Transfer-Encoding overrides the
4937 * Content-Length. Such a message might indicate an attempt to
4938 * perform request smuggling (Section 9.5) or response splitting
4939 * (Section 9.4) and ought to be handled as an error. A sender MUST
4940 * remove the received Content-Length field prior to forwarding such
4941 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004942 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004943 * 4. If a message is received without Transfer-Encoding and with
4944 * either multiple Content-Length header fields having differing
4945 * field-values or a single Content-Length header field having an
4946 * invalid value, then the message framing is invalid and the
4947 * recipient MUST treat it as an unrecoverable error. If this is a
4948 * request message, the server MUST respond with a 400 (Bad Request)
4949 * status code and then close the connection. If this is a response
4950 * message received by a proxy, the proxy MUST close the connection
4951 * to the server, discard the received response, and send a 502 (Bad
4952 * Gateway) response to the client. If this is a response message
4953 * received by a user agent, the user agent MUST close the
4954 * connection to the server and discard the received response.
4955 *
4956 * 5. If a valid Content-Length header field is present without
4957 * Transfer-Encoding, its decimal value defines the expected message
4958 * body length in octets. If the sender closes the connection or
4959 * the recipient times out before the indicated number of octets are
4960 * received, the recipient MUST consider the message to be
4961 * incomplete and close the connection.
4962 *
4963 * 6. If this is a request message and none of the above are true, then
4964 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004965 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004966 * 7. Otherwise, this is a response message without a declared message
4967 * body length, so the message body length is determined by the
4968 * number of octets received prior to the server closing the
4969 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004970 */
4971
4972 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004973 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004974 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004975 * FIXME: should we parse anyway and return an error on chunked encoding ?
4976 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004977 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4978 txn->status == 101)) {
4979 /* Either we've established an explicit tunnel, or we're
4980 * switching the protocol. In both cases, we're very unlikely
4981 * to understand the next protocols. We have to switch to tunnel
4982 * mode, so that we transfer the request and responses then let
4983 * this protocol pass unmodified. When we later implement specific
4984 * parsers for such protocols, we'll want to check the Upgrade
4985 * header which contains information about that protocol for
4986 * responses with status 101 (eg: see RFC2817 about TLS).
4987 */
4988 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4989 msg->flags |= HTTP_MSGF_XFER_LEN;
4990 goto end;
4991 }
4992
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004993 if (txn->meth == HTTP_METH_HEAD ||
4994 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004995 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004996 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004997 goto skip_content_length;
4998 }
4999
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005000 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005001 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005002 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005003 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005004 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5005 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005006 /* bad transfer-encoding (chunked followed by something else) */
5007 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005008 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005009 break;
5010 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005011 }
5012
Willy Tarreau1c913912015-04-30 10:57:51 +02005013 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005014 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005015 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005016 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005017 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5018 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005019 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005020 signed long long cl;
5021
Willy Tarreauad14f752011-09-02 20:33:27 +02005022 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005023 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005024 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005025 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005026
Willy Tarreauad14f752011-09-02 20:33:27 +02005027 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005028 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005029 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005030 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005031
Willy Tarreauad14f752011-09-02 20:33:27 +02005032 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005033 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005034 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005035 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005036
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005037 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005038 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005039 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005040 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005041
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005042 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005043 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005044 }
5045
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02005046 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
5047 * 407 (Proxy-Authenticate) responses and set the connection to private
5048 */
5049 if (srv_conn && txn->status == 401) {
5050 /* check for Negotiate/NTLM WWW-Authenticate headers */
5051 ctx.idx = 0;
5052 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
5053 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5054 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5055 srv_conn->flags |= CO_FL_PRIVATE;
5056 }
5057 } else if (srv_conn && txn->status == 407) {
5058 /* check for Negotiate/NTLM Proxy-Authenticate headers */
5059 ctx.idx = 0;
5060 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
5061 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5062 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5063 srv_conn->flags |= CO_FL_PRIVATE;
5064 }
5065 }
5066
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005067 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005068 /* Now we have to check if we need to modify the Connection header.
5069 * This is more difficult on the response than it is on the request,
5070 * because we can have two different HTTP versions and we don't know
5071 * how the client will interprete a response. For instance, let's say
5072 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5073 * HTTP/1.1 response without any header. Maybe it will bound itself to
5074 * HTTP/1.0 because it only knows about it, and will consider the lack
5075 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5076 * the lack of header as a keep-alive. Thus we will use two flags
5077 * indicating how a request MAY be understood by the client. In case
5078 * of multiple possibilities, we'll fix the header to be explicit. If
5079 * ambiguous cases such as both close and keepalive are seen, then we
5080 * will fall back to explicit close. Note that we won't take risks with
5081 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005082 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005083 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005084 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005085 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01005086 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005087
Willy Tarreau60466522010-01-18 19:08:45 +01005088 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005089 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01005090 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005091
Willy Tarreau60466522010-01-18 19:08:45 +01005092 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005093 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01005094 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005095 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005096 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005097 }
Willy Tarreau60466522010-01-18 19:08:45 +01005098 else { /* SCL / KAL */
5099 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005100 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005101 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005102 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005103
Willy Tarreau60466522010-01-18 19:08:45 +01005104 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005105 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005106
Willy Tarreau60466522010-01-18 19:08:45 +01005107 /* Some keep-alive responses are converted to Server-close if
5108 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005109 */
Willy Tarreau60466522010-01-18 19:08:45 +01005110 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5111 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005112 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005113 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005114 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005115 }
5116
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005117 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005118 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005119 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005120
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005121 /* end of job, return OK */
5122 rep->analysers &= ~an_bit;
5123 rep->analyse_exp = TICK_ETERNITY;
5124 channel_auto_close(rep);
5125 return 1;
5126
5127 abort_keep_alive:
5128 /* A keep-alive request to the server failed on a network error.
5129 * The client is required to retry. We need to close without returning
5130 * any other information so that the client retries.
5131 */
5132 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005133 rep->analysers &= AN_RES_FLT_END;
5134 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005135 channel_auto_close(rep);
5136 s->logs.logwait = 0;
5137 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005138 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005139 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005140 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005141 return 0;
5142}
5143
5144/* This function performs all the processing enabled for the current response.
5145 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005146 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005147 * other functions. It works like process_request (see indications above).
5148 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005149int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005150{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005151 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005152 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005153 struct http_msg *msg = &txn->rsp;
5154 struct proxy *cur_proxy;
5155 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005156 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005157
Christopher Faulet45073512018-07-20 10:16:29 +02005158 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 +02005159 now_ms, __FUNCTION__,
5160 s,
5161 rep,
5162 rep->rex, rep->wex,
5163 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005164 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005165 rep->analysers);
5166
5167 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5168 return 0;
5169
Willy Tarreau70730dd2014-04-24 18:06:27 +02005170 /* The stats applet needs to adjust the Connection header but we don't
5171 * apply any filter there.
5172 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005173 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5174 rep->analysers &= ~an_bit;
5175 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005176 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005177 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005178
Willy Tarreau58975672014-04-24 21:13:57 +02005179 /*
5180 * We will have to evaluate the filters.
5181 * As opposed to version 1.2, now they will be evaluated in the
5182 * filters order and not in the header order. This means that
5183 * each filter has to be validated among all headers.
5184 *
5185 * Filters are tried with ->be first, then with ->fe if it is
5186 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005187 *
5188 * Maybe we are in resume condiion. In this case I choose the
5189 * "struct proxy" which contains the rule list matching the resume
5190 * pointer. If none of theses "struct proxy" match, I initialise
5191 * the process with the first one.
5192 *
5193 * In fact, I check only correspondance betwwen the current list
5194 * pointer and the ->fe rule list. If it doesn't match, I initialize
5195 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005196 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005197 if (s->current_rule_list == &sess->fe->http_res_rules)
5198 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005199 else
5200 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005201 while (1) {
5202 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005203
Willy Tarreau58975672014-04-24 21:13:57 +02005204 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005205 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005206 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005207
Willy Tarreau51d861a2015-05-22 17:30:48 +02005208 if (ret == HTTP_RULE_RES_BADREQ)
5209 goto return_srv_prx_502;
5210
5211 if (ret == HTTP_RULE_RES_DONE) {
5212 rep->analysers &= ~an_bit;
5213 rep->analyse_exp = TICK_ETERNITY;
5214 return 1;
5215 }
5216 }
5217
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005218 /* we need to be called again. */
5219 if (ret == HTTP_RULE_RES_YIELD) {
5220 channel_dont_close(rep);
5221 return 0;
5222 }
5223
Willy Tarreau58975672014-04-24 21:13:57 +02005224 /* try headers filters */
5225 if (rule_set->rsp_exp != NULL) {
5226 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5227 return_bad_resp:
5228 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005229 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005230 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005231 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005232 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005233 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005234 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005235 txn->status = 502;
5236 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005237 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005238 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005239 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005240 if (!(s->flags & SF_ERR_MASK))
5241 s->flags |= SF_ERR_PRXCOND;
5242 if (!(s->flags & SF_FINST_MASK))
5243 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005244 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005245 }
Willy Tarreau58975672014-04-24 21:13:57 +02005246 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005247
Willy Tarreau58975672014-04-24 21:13:57 +02005248 /* has the response been denied ? */
5249 if (txn->flags & TX_SVDENY) {
5250 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005251 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005252
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005253 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5254 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005255 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005256 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005257
Willy Tarreau58975672014-04-24 21:13:57 +02005258 goto return_srv_prx_502;
5259 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005260
Willy Tarreau58975672014-04-24 21:13:57 +02005261 /* add response headers from the rule sets in the same order */
5262 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005263 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005264 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005265 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005266 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005267 ret = acl_pass(ret);
5268 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5269 ret = !ret;
5270 if (!ret)
5271 continue;
5272 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005273 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005274 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005275 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005276
Willy Tarreau58975672014-04-24 21:13:57 +02005277 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005278 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005279 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005280 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005281 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005282
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005283 /* After this point, this anayzer can't return yield, so we can
5284 * remove the bit corresponding to this analyzer from the list.
5285 *
5286 * Note that the intermediate returns and goto found previously
5287 * reset the analyzers.
5288 */
5289 rep->analysers &= ~an_bit;
5290 rep->analyse_exp = TICK_ETERNITY;
5291
Willy Tarreau58975672014-04-24 21:13:57 +02005292 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005293 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005294 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005295
Willy Tarreau58975672014-04-24 21:13:57 +02005296 /*
5297 * Now check for a server cookie.
5298 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005299 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005300 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005301
Willy Tarreau58975672014-04-24 21:13:57 +02005302 /*
5303 * Check for cache-control or pragma headers if required.
5304 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005305 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005306 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005307
Willy Tarreau58975672014-04-24 21:13:57 +02005308 /*
5309 * Add server cookie in the response if needed
5310 */
5311 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5312 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005313 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005314 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5315 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5316 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5317 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5318 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005319 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005320 /* the server is known, it's not the one the client requested, or the
5321 * cookie's last seen date needs to be refreshed. We have to
5322 * insert a set-cookie here, except if we want to insert only on POST
5323 * requests and this one isn't. Note that servers which don't have cookies
5324 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005325 */
Willy Tarreau58975672014-04-24 21:13:57 +02005326 if (!objt_server(s->target)->cookie) {
5327 chunk_printf(&trash,
5328 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5329 s->be->cookie_name);
5330 }
5331 else {
5332 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005333
Willy Tarreau58975672014-04-24 21:13:57 +02005334 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5335 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005336 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5337 s30tob64((date.tv_sec+3) >> 2,
5338 trash.area + trash.data);
5339 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005340
Willy Tarreau58975672014-04-24 21:13:57 +02005341 if (s->be->cookie_maxlife) {
5342 /* emit first_date, which is either the original one or
5343 * the current date.
5344 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005345 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005346 s30tob64(txn->cookie_first_date ?
5347 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005348 (date.tv_sec+3) >> 2,
5349 trash.area + trash.data);
5350 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005351 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005352 }
Willy Tarreau58975672014-04-24 21:13:57 +02005353 chunk_appendf(&trash, "; path=/");
5354 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005355
Willy Tarreau58975672014-04-24 21:13:57 +02005356 if (s->be->cookie_domain)
5357 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005358
Willy Tarreau58975672014-04-24 21:13:57 +02005359 if (s->be->ck_opts & PR_CK_HTTPONLY)
5360 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005361
Willy Tarreau58975672014-04-24 21:13:57 +02005362 if (s->be->ck_opts & PR_CK_SECURE)
5363 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005364
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005365 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005366 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005367
Willy Tarreau58975672014-04-24 21:13:57 +02005368 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005369 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005370 /* the server did not change, only the date was updated */
5371 txn->flags |= TX_SCK_UPDATED;
5372 else
5373 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005374
Willy Tarreau58975672014-04-24 21:13:57 +02005375 /* Here, we will tell an eventual cache on the client side that we don't
5376 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5377 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5378 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005379 */
Willy Tarreau58975672014-04-24 21:13:57 +02005380 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005381
Willy Tarreau58975672014-04-24 21:13:57 +02005382 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005383
Willy Tarreau58975672014-04-24 21:13:57 +02005384 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5385 "Cache-control: private", 22) < 0))
5386 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005387 }
Willy Tarreau58975672014-04-24 21:13:57 +02005388 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005389
Willy Tarreau58975672014-04-24 21:13:57 +02005390 /*
5391 * Check if result will be cacheable with a cookie.
5392 * We'll block the response if security checks have caught
5393 * nasty things such as a cacheable cookie.
5394 */
5395 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5396 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5397 (s->be->options & PR_O_CHK_CACHE)) {
5398 /* we're in presence of a cacheable response containing
5399 * a set-cookie header. We'll block it as requested by
5400 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005401 */
Willy Tarreau58975672014-04-24 21:13:57 +02005402 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005403 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005404
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005405 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5406 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005407 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005408 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005409
Christopher Faulet767a84b2017-11-24 16:50:31 +01005410 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5411 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005412 send_log(s->be, LOG_ALERT,
5413 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5414 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5415 goto return_srv_prx_502;
5416 }
Willy Tarreau03945942009-12-22 16:50:27 +01005417
Willy Tarreau70730dd2014-04-24 18:06:27 +02005418 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005419 /*
5420 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5421 * If an "Upgrade" token is found, the header is left untouched in order
5422 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005423 * if anything but "Upgrade" is present in the Connection header. We don't
5424 * want to touch any 101 response either since it's switching to another
5425 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005426 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005427 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005428 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005429 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005430
Willy Tarreau58975672014-04-24 21:13:57 +02005431 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5432 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5433 /* we want a keep-alive response here. Keep-alive header
5434 * required if either side is not 1.1.
5435 */
5436 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5437 want_flags |= TX_CON_KAL_SET;
5438 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005439 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005440 /* we want a close response here. Close header required if
5441 * the server is 1.1, regardless of the client.
5442 */
5443 if (msg->flags & HTTP_MSGF_VER_11)
5444 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005445 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005446
Willy Tarreau58975672014-04-24 21:13:57 +02005447 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5448 http_change_connection_header(txn, msg, want_flags);
5449 }
5450
5451 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005452 /* Always enter in the body analyzer */
5453 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5454 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005455
Willy Tarreau58975672014-04-24 21:13:57 +02005456 /* if the user wants to log as soon as possible, without counting
5457 * bytes from the server, then this is the right moment. We have
5458 * to temporarily assign bytes_out to log what we currently have.
5459 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005460 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005461 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5462 s->logs.bytes_out = txn->rsp.eoh;
5463 s->do_log(s);
5464 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005465 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005466 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005467}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005468
Willy Tarreaud98cf932009-12-27 22:54:55 +01005469/* This function is an analyser which forwards response body (including chunk
5470 * sizes if any). It is called as soon as we must forward, even if we forward
5471 * zero byte. The only situation where it must not be called is when we're in
5472 * tunnel mode and we want to forward till the close. It's used both to forward
5473 * remaining data and to resync after end of body. It expects the msg_state to
5474 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005475 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005476 *
5477 * It is capable of compressing response data both in content-length mode and
5478 * in chunked mode. The state machines follows different flows depending on
5479 * whether content-length and chunked modes are used, since there are no
5480 * trailers in content-length :
5481 *
5482 * chk-mode cl-mode
5483 * ,----- BODY -----.
5484 * / \
5485 * V size > 0 V chk-mode
5486 * .--> SIZE -------------> DATA -------------> CRLF
5487 * | | size == 0 | last byte |
5488 * | v final crlf v inspected |
5489 * | TRAILERS -----------> DONE |
5490 * | |
5491 * `----------------------------------------------'
5492 *
5493 * Compression only happens in the DATA state, and must be flushed in final
5494 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5495 * is performed at once on final states for all bytes parsed, or when leaving
5496 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005497 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005498int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005499{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005500 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005501 struct http_txn *txn = s->txn;
5502 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005503 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005504
Christopher Faulet45073512018-07-20 10:16:29 +02005505 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 +02005506 now_ms, __FUNCTION__,
5507 s,
5508 res,
5509 res->rex, res->wex,
5510 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005511 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005512 res->analysers);
5513
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005514 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5515 return 0;
5516
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005517 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005518 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005519 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005520 /* Output closed while we were sending data. We must abort and
5521 * wake the other side up.
5522 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005523 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005524 msg->msg_state = HTTP_MSG_ERROR;
5525 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005526 return 1;
5527 }
5528
Willy Tarreau4fe41902010-06-07 22:27:41 +02005529 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005530 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005531
Christopher Fauletd7c91962015-04-30 11:48:27 +02005532 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005533 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5534 ? HTTP_MSG_CHUNK_SIZE
5535 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005536 }
5537
Willy Tarreauefdf0942014-04-24 20:08:57 +02005538 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005539 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005540 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005541 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005542 }
5543
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005544 if (msg->msg_state < HTTP_MSG_DONE) {
5545 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5546 ? http_msg_forward_chunked_body(s, msg)
5547 : http_msg_forward_body(s, msg));
5548 if (!ret)
5549 goto missing_data_or_waiting;
5550 if (ret < 0)
5551 goto return_bad_res;
5552 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005553
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005554 /* other states, DONE...TUNNEL */
5555 /* for keep-alive we don't want to forward closes on DONE */
5556 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5557 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5558 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005559
Christopher Faulet894da4c2017-07-18 11:29:07 +02005560 http_resync_states(s);
5561 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005562 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5563 if (res->flags & CF_SHUTW) {
5564 /* response errors are most likely due to the
5565 * client aborting the transfer. */
5566 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005567 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005568 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005569 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005570 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005571 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005572 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005573 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005574 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005575
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005576 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005577 if (res->flags & CF_SHUTW)
5578 goto aborted_xfer;
5579
5580 /* stop waiting for data if the input is closed before the end. If the
5581 * client side was already closed, it means that the client has aborted,
5582 * so we don't want to count this as a server abort. Otherwise it's a
5583 * server abort.
5584 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005585 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005586 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005587 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005588 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005589 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005590 if (!(s->flags & SF_ERR_MASK))
5591 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005592 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005593 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005594 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005595 goto return_bad_res_stats_ok;
5596 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005597 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005598
Willy Tarreau40dba092010-03-04 18:14:51 +01005599 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005600 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005601 goto return_bad_res;
5602
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005603 /* When TE: chunked is used, we need to get there again to parse
5604 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005605 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5606 * or if there are filters registered on the stream, we don't want to
5607 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005608 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005609 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005610 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005611 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5612 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005613 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005614
Willy Tarreau5c620922011-05-11 19:56:11 +02005615 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005616 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005617 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005618 * modes are already handled by the stream sock layer. We must not do
5619 * this in content-length mode because it could present the MSG_MORE
5620 * flag with the last block of forwarded data, which would cause an
5621 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005622 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005623 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005624 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005625
Willy Tarreau87b09662015-04-03 00:22:06 +02005626 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005627 return 0;
5628
Willy Tarreau40dba092010-03-04 18:14:51 +01005629 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005630 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005631 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005632 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005633
5634 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005635 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005636 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005637 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005638 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005639 res->analysers &= AN_RES_FLT_END;
5640 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 +01005641 if (objt_server(s->target))
5642 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005643
Willy Tarreaue7dff022015-04-03 01:14:29 +02005644 if (!(s->flags & SF_ERR_MASK))
5645 s->flags |= SF_ERR_PRXCOND;
5646 if (!(s->flags & SF_FINST_MASK))
5647 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005648 return 0;
5649
5650 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005651 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005652 txn->rsp.msg_state = HTTP_MSG_ERROR;
5653 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005654 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005655 res->analysers &= AN_RES_FLT_END;
5656 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 +01005657
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005658 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5659 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005660 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005661 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005662
Willy Tarreaue7dff022015-04-03 01:14:29 +02005663 if (!(s->flags & SF_ERR_MASK))
5664 s->flags |= SF_ERR_CLICL;
5665 if (!(s->flags & SF_FINST_MASK))
5666 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005667 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005668}
5669
5670
Christopher Faulet10079f52018-10-03 15:17:28 +02005671int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005672{
5673 struct channel *chn = msg->chn;
5674 int ret;
5675
5676 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5677
5678 if (msg->msg_state == HTTP_MSG_ENDING)
5679 goto ending;
5680
5681 /* Neither content-length, nor transfer-encoding was found, so we must
5682 * read the body until the server connection is closed. In that case, we
5683 * eat data as they come. Of course, this happens for response only. */
5684 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005685 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005686 msg->chunk_len += len;
5687 msg->body_len += len;
5688 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005689 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005690 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005691 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005692 msg->next += ret;
5693 msg->chunk_len -= ret;
5694 if (msg->chunk_len) {
5695 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005696 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005697 chn->flags |= CF_WAKE_WRITE;
5698 goto missing_data_or_waiting;
5699 }
5700
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005701 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5702 * always set for a request. */
5703 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5704 /* The server still sending data that should be filtered */
5705 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5706 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005707 msg->msg_state = HTTP_MSG_TUNNEL;
5708 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005709 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005710
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005711 msg->msg_state = HTTP_MSG_ENDING;
5712
5713 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005714 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005715 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005716 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5717 /* default_ret */ msg->next,
5718 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005719 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005720 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005721 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5722 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005723 if (msg->next)
5724 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005725
Christopher Fauletda02e172015-12-04 09:25:05 +01005726 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5727 /* default_ret */ 1,
5728 /* on_error */ goto error,
5729 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005730 if (msg->msg_state == HTTP_MSG_ENDING)
5731 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005732 return 1;
5733
5734 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005735 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005736 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5737 /* default_ret */ msg->next,
5738 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005739 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005740 msg->next -= ret;
5741 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5742 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005743 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005744 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005745 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005746 return 0;
5747 error:
5748 return -1;
5749}
5750
Christopher Faulet10079f52018-10-03 15:17:28 +02005751int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005752{
5753 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005754 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005755 int ret;
5756
5757 /* Here we have the guarantee to be in one of the following state:
5758 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5759 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5760
Christopher Fauletca874b82018-09-20 11:31:01 +02005761 if (msg->msg_state == HTTP_MSG_ENDING)
5762 goto ending;
5763
5764 /* Don't parse chunks if there is no input data */
5765 if (!ci_data(chn))
5766 goto waiting;
5767
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005768 switch_states:
5769 switch (msg->msg_state) {
5770 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005771 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005772 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005773 /* on_error */ goto error);
5774 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005775 msg->chunk_len -= ret;
5776 if (msg->chunk_len) {
5777 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005778 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005779 chn->flags |= CF_WAKE_WRITE;
5780 goto missing_data_or_waiting;
5781 }
5782
5783 /* nothing left to forward for this chunk*/
5784 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5785 /* fall through for HTTP_MSG_CHUNK_CRLF */
5786
5787 case HTTP_MSG_CHUNK_CRLF:
5788 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005789 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005790 if (ret == 0)
5791 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005792 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005793 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005794 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005795 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005796 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005797 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005798 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005799 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5800 /* fall through for HTTP_MSG_CHUNK_SIZE */
5801
5802 case HTTP_MSG_CHUNK_SIZE:
5803 /* read the chunk size and assign it to ->chunk_len,
5804 * then set ->next to point to the body and switch to
5805 * DATA or TRAILERS state.
5806 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005807 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005808 if (ret == 0)
5809 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005810 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005811 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005812 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005813 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005814 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005815 }
5816
5817 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005818 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005819 msg->chunk_len = chunk;
5820 msg->body_len += chunk;
5821
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005822 if (msg->chunk_len) {
5823 msg->msg_state = HTTP_MSG_DATA;
5824 goto switch_states;
5825 }
5826 msg->msg_state = HTTP_MSG_TRAILERS;
5827 /* fall through for HTTP_MSG_TRAILERS */
5828
5829 case HTTP_MSG_TRAILERS:
5830 ret = http_forward_trailers(msg);
5831 if (ret < 0)
5832 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005833 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5834 /* default_ret */ 1,
5835 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005836 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005837 if (!ret)
5838 goto missing_data_or_waiting;
5839 break;
5840
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005841 default:
5842 /* This should no happen in this function */
5843 goto error;
5844 }
5845
5846 msg->msg_state = HTTP_MSG_ENDING;
5847 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005848 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005849 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005850 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005851 /* default_ret */ msg->next,
5852 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005853 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005854 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005855 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5856 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005857 if (msg->next)
5858 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005859
Christopher Fauletda02e172015-12-04 09:25:05 +01005860 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005861 /* default_ret */ 1,
5862 /* on_error */ goto error,
5863 /* on_wait */ goto waiting);
5864 msg->msg_state = HTTP_MSG_DONE;
5865 return 1;
5866
5867 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005868 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005869 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005870 /* default_ret */ msg->next,
5871 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005872 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005873 msg->next -= ret;
5874 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5875 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005876 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005877 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005878 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005879 return 0;
5880
5881 chunk_parsing_error:
5882 if (msg->err_pos >= 0) {
5883 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005884 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005885 msg->msg_state, strm_fe(s));
5886 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005887 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005888 msg, msg->msg_state, s->be);
5889 }
5890 error:
5891 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005892}
5893
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005894
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005895/* Iterate the same filter through all request headers.
5896 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005897 * Since it can manage the switch to another backend, it updates the per-proxy
5898 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005899 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005900int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005901{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005902 char *cur_ptr, *cur_end, *cur_next;
5903 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005904 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005905 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005906 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005907
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005908 last_hdr = 0;
5909
Willy Tarreauf37954d2018-06-15 18:31:02 +02005910 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005911 old_idx = 0;
5912
5913 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005914 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005915 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005916 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005917 (exp->action == ACT_ALLOW ||
5918 exp->action == ACT_DENY ||
5919 exp->action == ACT_TARPIT))
5920 return 0;
5921
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005922 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005923 if (!cur_idx)
5924 break;
5925
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005926 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005927 cur_ptr = cur_next;
5928 cur_end = cur_ptr + cur_hdr->len;
5929 cur_next = cur_end + cur_hdr->cr + 1;
5930
5931 /* Now we have one header between cur_ptr and cur_end,
5932 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005933 */
5934
Willy Tarreau15a53a42015-01-21 13:39:42 +01005935 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005936 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005937 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005938 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005939 last_hdr = 1;
5940 break;
5941
5942 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005943 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005944 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005945 break;
5946
5947 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005948 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005949 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005950 break;
5951
5952 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005953 len = exp_replace(trash.area,
5954 trash.size, cur_ptr,
5955 exp->replace, pmatch);
5956 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005957 return -1;
5958
Willy Tarreau6e27be12018-08-22 04:46:47 +02005959 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5960
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005961 /* FIXME: if the user adds a newline in the replacement, the
5962 * index will not be recalculated for now, and the new line
5963 * will not be counted as a new header.
5964 */
5965
5966 cur_end += delta;
5967 cur_next += delta;
5968 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005969 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005970 break;
5971
5972 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005973 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005974 cur_next += delta;
5975
Willy Tarreaufa355d42009-11-29 18:12:29 +01005976 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005977 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5978 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005979 cur_hdr->len = 0;
5980 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005981 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005982 break;
5983
5984 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005985 }
5986
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005987 /* keep the link from this header to next one in case of later
5988 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005989 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005990 old_idx = cur_idx;
5991 }
5992 return 0;
5993}
5994
5995
5996/* Apply the filter to the request line.
5997 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5998 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005999 * Since it can manage the switch to another backend, it updates the per-proxy
6000 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006001 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006002int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006003{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006004 char *cur_ptr, *cur_end;
6005 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006006 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006007 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006008
Willy Tarreau3d300592007-03-18 18:34:41 +01006009 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006010 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006011 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006012 (exp->action == ACT_ALLOW ||
6013 exp->action == ACT_DENY ||
6014 exp->action == ACT_TARPIT))
6015 return 0;
6016 else if (exp->action == ACT_REMOVE)
6017 return 0;
6018
6019 done = 0;
6020
Willy Tarreauf37954d2018-06-15 18:31:02 +02006021 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006022 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006023
6024 /* Now we have the request line between cur_ptr and cur_end */
6025
Willy Tarreau15a53a42015-01-21 13:39:42 +01006026 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006027 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006028 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006029 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006030 done = 1;
6031 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006032
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006033 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006034 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006035 done = 1;
6036 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006037
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006038 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006039 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006040 done = 1;
6041 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006042
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006043 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006044 len = exp_replace(trash.area, trash.size,
6045 cur_ptr, exp->replace, pmatch);
6046 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006047 return -1;
6048
Willy Tarreau6e27be12018-08-22 04:46:47 +02006049 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6050
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006051 /* FIXME: if the user adds a newline in the replacement, the
6052 * index will not be recalculated for now, and the new line
6053 * will not be counted as a new header.
6054 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006055
Willy Tarreaufa355d42009-11-29 18:12:29 +01006056 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006057 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006058 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006059 HTTP_MSG_RQMETH,
6060 cur_ptr, cur_end + 1,
6061 NULL, NULL);
6062 if (unlikely(!cur_end))
6063 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006064
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006065 /* we have a full request and we know that we have either a CR
6066 * or an LF at <ptr>.
6067 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006068 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6069 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006070 /* there is no point trying this regex on headers */
6071 return 1;
6072 }
6073 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006074 return done;
6075}
Willy Tarreau97de6242006-12-27 17:18:38 +01006076
Willy Tarreau58f10d72006-12-04 02:26:12 +01006077
Willy Tarreau58f10d72006-12-04 02:26:12 +01006078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006079/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006080 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006081 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006082 * unparsable request. Since it can manage the switch to another backend, it
6083 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006084 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006085int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006086{
Willy Tarreau192252e2015-04-04 01:47:55 +02006087 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006088 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006089 struct hdr_exp *exp;
6090
6091 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006092 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006093
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006094 /*
6095 * The interleaving of transformations and verdicts
6096 * makes it difficult to decide to continue or stop
6097 * the evaluation.
6098 */
6099
Willy Tarreau6c123b12010-01-28 20:22:06 +01006100 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6101 break;
6102
Willy Tarreau3d300592007-03-18 18:34:41 +01006103 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006104 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006105 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006106 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006107
6108 /* if this filter had a condition, evaluate it now and skip to
6109 * next filter if the condition does not match.
6110 */
6111 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006112 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006113 ret = acl_pass(ret);
6114 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6115 ret = !ret;
6116
6117 if (!ret)
6118 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006119 }
6120
6121 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006122 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006123 if (unlikely(ret < 0))
6124 return -1;
6125
6126 if (likely(ret == 0)) {
6127 /* The filter did not match the request, it can be
6128 * iterated through all headers.
6129 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006130 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6131 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006132 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006133 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006134 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006135}
6136
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006137
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006138/* Delete a value in a header between delimiters <from> and <next> in buffer
6139 * <buf>. The number of characters displaced is returned, and the pointer to
6140 * the first delimiter is updated if required. The function tries as much as
6141 * possible to respect the following principles :
6142 * - replace <from> delimiter by the <next> one unless <from> points to a
6143 * colon, in which case <next> is simply removed
6144 * - set exactly one space character after the new first delimiter, unless
6145 * there are not enough characters in the block being moved to do so.
6146 * - remove unneeded spaces before the previous delimiter and after the new
6147 * one.
6148 *
6149 * It is the caller's responsibility to ensure that :
6150 * - <from> points to a valid delimiter or the colon ;
6151 * - <next> points to a valid delimiter or the final CR/LF ;
6152 * - there are non-space chars before <from> ;
6153 * - there is a CR/LF at or after <next>.
6154 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006155int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006156{
6157 char *prev = *from;
6158
6159 if (*prev == ':') {
6160 /* We're removing the first value, preserve the colon and add a
6161 * space if possible.
6162 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006163 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006164 next++;
6165 prev++;
6166 if (prev < next)
6167 *prev++ = ' ';
6168
Willy Tarreau2235b262016-11-05 15:50:20 +01006169 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006170 next++;
6171 } else {
6172 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006173 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006174 prev--;
6175 *from = prev;
6176
6177 /* copy the delimiter and if possible a space if we're
6178 * not at the end of the line.
6179 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006180 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006181 *prev++ = *next++;
6182 if (prev + 1 < next)
6183 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006184 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006185 next++;
6186 }
6187 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006188 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006189}
6190
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006191/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006192 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006193 * desirable to call it only when needed. This code is quite complex because
6194 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6195 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006196 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006197void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006198{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006199 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006200 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006201 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006202 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006203 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6204 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006205
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006206 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006207 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006208 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006209
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006210 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006211 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006212 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006213
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006214 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006215 hdr_beg = hdr_next;
6216 hdr_end = hdr_beg + cur_hdr->len;
6217 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006218
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006219 /* We have one full header between hdr_beg and hdr_end, and the
6220 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006221 * "Cookie:" headers.
6222 */
6223
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006224 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006225 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006226 old_idx = cur_idx;
6227 continue;
6228 }
6229
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006230 del_from = NULL; /* nothing to be deleted */
6231 preserve_hdr = 0; /* assume we may kill the whole header */
6232
Willy Tarreau58f10d72006-12-04 02:26:12 +01006233 /* Now look for cookies. Conforming to RFC2109, we have to support
6234 * attributes whose name begin with a '$', and associate them with
6235 * the right cookie, if we want to delete this cookie.
6236 * So there are 3 cases for each cookie read :
6237 * 1) it's a special attribute, beginning with a '$' : ignore it.
6238 * 2) it's a server id cookie that we *MAY* want to delete : save
6239 * some pointers on it (last semi-colon, beginning of cookie...)
6240 * 3) it's an application cookie : we *MAY* have to delete a previous
6241 * "special" cookie.
6242 * At the end of loop, if a "special" cookie remains, we may have to
6243 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006244 * *MUST* delete it.
6245 *
6246 * Note: RFC2965 is unclear about the processing of spaces around
6247 * the equal sign in the ATTR=VALUE form. A careful inspection of
6248 * the RFC explicitly allows spaces before it, and not within the
6249 * tokens (attrs or values). An inspection of RFC2109 allows that
6250 * too but section 10.1.3 lets one think that spaces may be allowed
6251 * after the equal sign too, resulting in some (rare) buggy
6252 * implementations trying to do that. So let's do what servers do.
6253 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6254 * allowed quoted strings in values, with any possible character
6255 * after a backslash, including control chars and delimitors, which
6256 * causes parsing to become ambiguous. Browsers also allow spaces
6257 * within values even without quotes.
6258 *
6259 * We have to keep multiple pointers in order to support cookie
6260 * removal at the beginning, middle or end of header without
6261 * corrupting the header. All of these headers are valid :
6262 *
6263 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6264 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6265 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6266 * | | | | | | | | |
6267 * | | | | | | | | hdr_end <--+
6268 * | | | | | | | +--> next
6269 * | | | | | | +----> val_end
6270 * | | | | | +-----------> val_beg
6271 * | | | | +--------------> equal
6272 * | | | +----------------> att_end
6273 * | | +---------------------> att_beg
6274 * | +--------------------------> prev
6275 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006276 */
6277
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006278 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6279 /* Iterate through all cookies on this line */
6280
6281 /* find att_beg */
6282 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006283 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006284 att_beg++;
6285
6286 /* find att_end : this is the first character after the last non
6287 * space before the equal. It may be equal to hdr_end.
6288 */
6289 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006290
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006291 while (equal < hdr_end) {
6292 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006293 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006294 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006295 continue;
6296 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006297 }
6298
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006299 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6300 * is between <att_beg> and <equal>, both may be identical.
6301 */
6302
6303 /* look for end of cookie if there is an equal sign */
6304 if (equal < hdr_end && *equal == '=') {
6305 /* look for the beginning of the value */
6306 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006307 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006308 val_beg++;
6309
6310 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006311 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006312
6313 /* make val_end point to the first white space or delimitor after the value */
6314 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006315 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006316 val_end--;
6317 } else {
6318 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006319 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006320
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006321 /* We have nothing to do with attributes beginning with '$'. However,
6322 * they will automatically be removed if a header before them is removed,
6323 * since they're supposed to be linked together.
6324 */
6325 if (*att_beg == '$')
6326 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006327
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006328 /* Ignore cookies with no equal sign */
6329 if (equal == next) {
6330 /* This is not our cookie, so we must preserve it. But if we already
6331 * scheduled another cookie for removal, we cannot remove the
6332 * complete header, but we can remove the previous block itself.
6333 */
6334 preserve_hdr = 1;
6335 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006336 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006337 val_end += delta;
6338 next += delta;
6339 hdr_end += delta;
6340 hdr_next += delta;
6341 cur_hdr->len += delta;
6342 http_msg_move_end(&txn->req, delta);
6343 prev = del_from;
6344 del_from = NULL;
6345 }
6346 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006347 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006348
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006349 /* if there are spaces around the equal sign, we need to
6350 * strip them otherwise we'll get trouble for cookie captures,
6351 * or even for rewrites. Since this happens extremely rarely,
6352 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006353 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006354 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6355 int stripped_before = 0;
6356 int stripped_after = 0;
6357
6358 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006359 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006360 equal += stripped_before;
6361 val_beg += stripped_before;
6362 }
6363
6364 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006365 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006366 val_beg += stripped_after;
6367 stripped_before += stripped_after;
6368 }
6369
6370 val_end += stripped_before;
6371 next += stripped_before;
6372 hdr_end += stripped_before;
6373 hdr_next += stripped_before;
6374 cur_hdr->len += stripped_before;
6375 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006376 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006377 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006378
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006379 /* First, let's see if we want to capture this cookie. We check
6380 * that we don't already have a client side cookie, because we
6381 * can only capture one. Also as an optimisation, we ignore
6382 * cookies shorter than the declared name.
6383 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006384 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6385 (val_end - att_beg >= sess->fe->capture_namelen) &&
6386 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006387 int log_len = val_end - att_beg;
6388
Willy Tarreaubafbe012017-11-24 17:34:44 +01006389 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006390 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006391 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006392 if (log_len > sess->fe->capture_len)
6393 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006394 memcpy(txn->cli_cookie, att_beg, log_len);
6395 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006396 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006397 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006398
Willy Tarreaubca99692010-10-06 19:25:55 +02006399 /* Persistence cookies in passive, rewrite or insert mode have the
6400 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006401 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006402 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006403 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006404 * For cookies in prefix mode, the form is :
6405 *
6406 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006407 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006408 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6409 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6410 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006411 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006412
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006413 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6414 * have the server ID between val_beg and delim, and the original cookie between
6415 * delim+1 and val_end. Otherwise, delim==val_end :
6416 *
6417 * Cookie: NAME=SRV; # in all but prefix modes
6418 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6419 * | || || | |+-> next
6420 * | || || | +--> val_end
6421 * | || || +---------> delim
6422 * | || |+------------> val_beg
6423 * | || +-------------> att_end = equal
6424 * | |+-----------------> att_beg
6425 * | +------------------> prev
6426 * +-------------------------> hdr_beg
6427 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006428
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006429 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006430 for (delim = val_beg; delim < val_end; delim++)
6431 if (*delim == COOKIE_DELIM)
6432 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006433 } else {
6434 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006435 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006436 /* Now check if the cookie contains a date field, which would
6437 * appear after a vertical bar ('|') just after the server name
6438 * and before the delimiter.
6439 */
6440 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6441 if (vbar1) {
6442 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006443 * right is the last seen date. It is a base64 encoded
6444 * 30-bit value representing the UNIX date since the
6445 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006446 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006447 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006448 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006449 if (val_end - vbar1 >= 5) {
6450 val = b64tos30(vbar1);
6451 if (val > 0)
6452 txn->cookie_last_date = val << 2;
6453 }
6454 /* look for a second vertical bar */
6455 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6456 if (vbar1 && (val_end - vbar1 > 5)) {
6457 val = b64tos30(vbar1 + 1);
6458 if (val > 0)
6459 txn->cookie_first_date = val << 2;
6460 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006461 }
6462 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006463
Willy Tarreauf64d1412010-10-07 20:06:11 +02006464 /* if the cookie has an expiration date and the proxy wants to check
6465 * it, then we do that now. We first check if the cookie is too old,
6466 * then only if it has expired. We detect strict overflow because the
6467 * time resolution here is not great (4 seconds). Cookies with dates
6468 * in the future are ignored if their offset is beyond one day. This
6469 * allows an admin to fix timezone issues without expiring everyone
6470 * and at the same time avoids keeping unwanted side effects for too
6471 * long.
6472 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006473 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6474 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006475 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006476 txn->flags &= ~TX_CK_MASK;
6477 txn->flags |= TX_CK_OLD;
6478 delim = val_beg; // let's pretend we have not found the cookie
6479 txn->cookie_first_date = 0;
6480 txn->cookie_last_date = 0;
6481 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006482 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6483 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006484 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006485 txn->flags &= ~TX_CK_MASK;
6486 txn->flags |= TX_CK_EXPIRED;
6487 delim = val_beg; // let's pretend we have not found the cookie
6488 txn->cookie_first_date = 0;
6489 txn->cookie_last_date = 0;
6490 }
6491
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006492 /* Here, we'll look for the first running server which supports the cookie.
6493 * This allows to share a same cookie between several servers, for example
6494 * to dedicate backup servers to specific servers only.
6495 * However, to prevent clients from sticking to cookie-less backup server
6496 * when they have incidentely learned an empty cookie, we simply ignore
6497 * empty cookies and mark them as invalid.
6498 * The same behaviour is applied when persistence must be ignored.
6499 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006500 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006501 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006502
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006503 while (srv) {
6504 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6505 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006506 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006507 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006508 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006509 /* we found the server and we can use it */
6510 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006511 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006512 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006513 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006514 break;
6515 } else {
6516 /* we found a server, but it's down,
6517 * mark it as such and go on in case
6518 * another one is available.
6519 */
6520 txn->flags &= ~TX_CK_MASK;
6521 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006522 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006523 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006524 srv = srv->next;
6525 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006526
Willy Tarreauf64d1412010-10-07 20:06:11 +02006527 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006528 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006529 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006530 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006531 txn->flags |= TX_CK_UNUSED;
6532 else
6533 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006534 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006535
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006536 /* depending on the cookie mode, we may have to either :
6537 * - delete the complete cookie if we're in insert+indirect mode, so that
6538 * the server never sees it ;
6539 * - remove the server id from the cookie value, and tag the cookie as an
6540 * application cookie so that it does not get accidentely removed later,
6541 * if we're in cookie prefix mode
6542 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006543 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006544 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006545
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006546 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006547 val_end += delta;
6548 next += delta;
6549 hdr_end += delta;
6550 hdr_next += delta;
6551 cur_hdr->len += delta;
6552 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006553
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006554 del_from = NULL;
6555 preserve_hdr = 1; /* we want to keep this cookie */
6556 }
6557 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006558 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006559 del_from = prev;
6560 }
6561 } else {
6562 /* This is not our cookie, so we must preserve it. But if we already
6563 * scheduled another cookie for removal, we cannot remove the
6564 * complete header, but we can remove the previous block itself.
6565 */
6566 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006567
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006568 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006569 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006570 if (att_beg >= del_from)
6571 att_beg += delta;
6572 if (att_end >= del_from)
6573 att_end += delta;
6574 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006575 val_end += delta;
6576 next += delta;
6577 hdr_end += delta;
6578 hdr_next += delta;
6579 cur_hdr->len += delta;
6580 http_msg_move_end(&txn->req, delta);
6581 prev = del_from;
6582 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006583 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006584 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006585
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006586 /* continue with next cookie on this header line */
6587 att_beg = next;
6588 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006589
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006590 /* There are no more cookies on this line.
6591 * We may still have one (or several) marked for deletion at the
6592 * end of the line. We must do this now in two ways :
6593 * - if some cookies must be preserved, we only delete from the
6594 * mark to the end of line ;
6595 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006596 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006597 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006598 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006599 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006600 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006601 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006602 cur_hdr->len += delta;
6603 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006604 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006605
6606 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006607 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6608 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006609 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006610 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006611 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006612 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006613 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006614 }
6615
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006616 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006617 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006618 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006619}
6620
6621
Willy Tarreaua15645d2007-03-18 16:22:39 +01006622/* Iterate the same filter through all response headers contained in <rtr>.
6623 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6624 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006625int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006626{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006627 char *cur_ptr, *cur_end, *cur_next;
6628 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006629 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006630 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006631 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006632
6633 last_hdr = 0;
6634
Willy Tarreauf37954d2018-06-15 18:31:02 +02006635 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006636 old_idx = 0;
6637
6638 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006639 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006640 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006641 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006642 (exp->action == ACT_ALLOW ||
6643 exp->action == ACT_DENY))
6644 return 0;
6645
6646 cur_idx = txn->hdr_idx.v[old_idx].next;
6647 if (!cur_idx)
6648 break;
6649
6650 cur_hdr = &txn->hdr_idx.v[cur_idx];
6651 cur_ptr = cur_next;
6652 cur_end = cur_ptr + cur_hdr->len;
6653 cur_next = cur_end + cur_hdr->cr + 1;
6654
6655 /* Now we have one header between cur_ptr and cur_end,
6656 * and the next header starts at cur_next.
6657 */
6658
Willy Tarreau15a53a42015-01-21 13:39:42 +01006659 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006660 switch (exp->action) {
6661 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006662 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006663 last_hdr = 1;
6664 break;
6665
6666 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006667 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006668 last_hdr = 1;
6669 break;
6670
6671 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006672 len = exp_replace(trash.area,
6673 trash.size, cur_ptr,
6674 exp->replace, pmatch);
6675 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006676 return -1;
6677
Willy Tarreau6e27be12018-08-22 04:46:47 +02006678 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6679
Willy Tarreaua15645d2007-03-18 16:22:39 +01006680 /* FIXME: if the user adds a newline in the replacement, the
6681 * index will not be recalculated for now, and the new line
6682 * will not be counted as a new header.
6683 */
6684
6685 cur_end += delta;
6686 cur_next += delta;
6687 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006688 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006689 break;
6690
6691 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006692 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006693 cur_next += delta;
6694
Willy Tarreaufa355d42009-11-29 18:12:29 +01006695 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006696 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6697 txn->hdr_idx.used--;
6698 cur_hdr->len = 0;
6699 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006700 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701 break;
6702
6703 }
6704 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006705
6706 /* keep the link from this header to next one in case of later
6707 * removal of next header.
6708 */
6709 old_idx = cur_idx;
6710 }
6711 return 0;
6712}
6713
6714
6715/* Apply the filter to the status line in the response buffer <rtr>.
6716 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6717 * or -1 if a replacement resulted in an invalid status line.
6718 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006719int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006720{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006721 char *cur_ptr, *cur_end;
6722 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006723 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006724 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006725
Willy Tarreau3d300592007-03-18 18:34:41 +01006726 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006727 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006728 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006729 (exp->action == ACT_ALLOW ||
6730 exp->action == ACT_DENY))
6731 return 0;
6732 else if (exp->action == ACT_REMOVE)
6733 return 0;
6734
6735 done = 0;
6736
Willy Tarreauf37954d2018-06-15 18:31:02 +02006737 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006738 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006739
6740 /* Now we have the status line between cur_ptr and cur_end */
6741
Willy Tarreau15a53a42015-01-21 13:39:42 +01006742 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006743 switch (exp->action) {
6744 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006745 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006746 done = 1;
6747 break;
6748
6749 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006750 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006751 done = 1;
6752 break;
6753
6754 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006755 len = exp_replace(trash.area, trash.size,
6756 cur_ptr, exp->replace, pmatch);
6757 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006758 return -1;
6759
Willy Tarreau6e27be12018-08-22 04:46:47 +02006760 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6761
Willy Tarreaua15645d2007-03-18 16:22:39 +01006762 /* FIXME: if the user adds a newline in the replacement, the
6763 * index will not be recalculated for now, and the new line
6764 * will not be counted as a new header.
6765 */
6766
Willy Tarreaufa355d42009-11-29 18:12:29 +01006767 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006768 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006769 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006770 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006771 cur_ptr, cur_end + 1,
6772 NULL, NULL);
6773 if (unlikely(!cur_end))
6774 return -1;
6775
6776 /* we have a full respnse and we know that we have either a CR
6777 * or an LF at <ptr>.
6778 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006779 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006780 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006781 /* there is no point trying this regex on headers */
6782 return 1;
6783 }
6784 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006785 return done;
6786}
6787
6788
6789
6790/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006791 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006792 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6793 * unparsable response.
6794 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006795int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006796{
Willy Tarreau192252e2015-04-04 01:47:55 +02006797 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006798 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006799 struct hdr_exp *exp;
6800
6801 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006802 int ret;
6803
6804 /*
6805 * The interleaving of transformations and verdicts
6806 * makes it difficult to decide to continue or stop
6807 * the evaluation.
6808 */
6809
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006810 if (txn->flags & TX_SVDENY)
6811 break;
6812
Willy Tarreau3d300592007-03-18 18:34:41 +01006813 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006814 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6815 exp->action == ACT_PASS)) {
6816 exp = exp->next;
6817 continue;
6818 }
6819
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006820 /* if this filter had a condition, evaluate it now and skip to
6821 * next filter if the condition does not match.
6822 */
6823 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006824 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006825 ret = acl_pass(ret);
6826 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6827 ret = !ret;
6828 if (!ret)
6829 continue;
6830 }
6831
Willy Tarreaua15645d2007-03-18 16:22:39 +01006832 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006833 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006834 if (unlikely(ret < 0))
6835 return -1;
6836
6837 if (likely(ret == 0)) {
6838 /* The filter did not match the response, it can be
6839 * iterated through all headers.
6840 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006841 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6842 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006843 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006844 }
6845 return 0;
6846}
6847
6848
Willy Tarreaua15645d2007-03-18 16:22:39 +01006849/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006850 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006851 * desirable to call it only when needed. This function is also used when we
6852 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006853 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006854void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006855{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006856 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006857 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006858 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006859 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006860 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006861 char *hdr_beg, *hdr_end, *hdr_next;
6862 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006863
Willy Tarreaua15645d2007-03-18 16:22:39 +01006864 /* Iterate through the headers.
6865 * we start with the start line.
6866 */
6867 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006868 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006869
6870 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6871 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006872 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006873
6874 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006875 hdr_beg = hdr_next;
6876 hdr_end = hdr_beg + cur_hdr->len;
6877 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006878
Willy Tarreau24581ba2010-08-31 22:39:35 +02006879 /* We have one full header between hdr_beg and hdr_end, and the
6880 * next header starts at hdr_next. We're only interested in
6881 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006882 */
6883
Willy Tarreau24581ba2010-08-31 22:39:35 +02006884 is_cookie2 = 0;
6885 prev = hdr_beg + 10;
6886 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006887 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006888 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6889 if (!val) {
6890 old_idx = cur_idx;
6891 continue;
6892 }
6893 is_cookie2 = 1;
6894 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006895 }
6896
Willy Tarreau24581ba2010-08-31 22:39:35 +02006897 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6898 * <prev> points to the colon.
6899 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006900 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006901
Willy Tarreau24581ba2010-08-31 22:39:35 +02006902 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6903 * check-cache is enabled) and we are not interested in checking
6904 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006905 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006906 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006907 return;
6908
Willy Tarreau24581ba2010-08-31 22:39:35 +02006909 /* OK so now we know we have to process this response cookie.
6910 * The format of the Set-Cookie header is slightly different
6911 * from the format of the Cookie header in that it does not
6912 * support the comma as a cookie delimiter (thus the header
6913 * cannot be folded) because the Expires attribute described in
6914 * the original Netscape's spec may contain an unquoted date
6915 * with a comma inside. We have to live with this because
6916 * many browsers don't support Max-Age and some browsers don't
6917 * support quoted strings. However the Set-Cookie2 header is
6918 * clean.
6919 *
6920 * We have to keep multiple pointers in order to support cookie
6921 * removal at the beginning, middle or end of header without
6922 * corrupting the header (in case of set-cookie2). A special
6923 * pointer, <scav> points to the beginning of the set-cookie-av
6924 * fields after the first semi-colon. The <next> pointer points
6925 * either to the end of line (set-cookie) or next unquoted comma
6926 * (set-cookie2). All of these headers are valid :
6927 *
6928 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6929 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6930 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6931 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6932 * | | | | | | | | | |
6933 * | | | | | | | | +-> next hdr_end <--+
6934 * | | | | | | | +------------> scav
6935 * | | | | | | +--------------> val_end
6936 * | | | | | +--------------------> val_beg
6937 * | | | | +----------------------> equal
6938 * | | | +------------------------> att_end
6939 * | | +----------------------------> att_beg
6940 * | +------------------------------> prev
6941 * +-----------------------------------------> hdr_beg
6942 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006943
Willy Tarreau24581ba2010-08-31 22:39:35 +02006944 for (; prev < hdr_end; prev = next) {
6945 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006946
Willy Tarreau24581ba2010-08-31 22:39:35 +02006947 /* find att_beg */
6948 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006949 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006950 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006951
Willy Tarreau24581ba2010-08-31 22:39:35 +02006952 /* find att_end : this is the first character after the last non
6953 * space before the equal. It may be equal to hdr_end.
6954 */
6955 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006956
Willy Tarreau24581ba2010-08-31 22:39:35 +02006957 while (equal < hdr_end) {
6958 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6959 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006960 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006961 continue;
6962 att_end = equal;
6963 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006964
Willy Tarreau24581ba2010-08-31 22:39:35 +02006965 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6966 * is between <att_beg> and <equal>, both may be identical.
6967 */
6968
6969 /* look for end of cookie if there is an equal sign */
6970 if (equal < hdr_end && *equal == '=') {
6971 /* look for the beginning of the value */
6972 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006973 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006974 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006975
Willy Tarreau24581ba2010-08-31 22:39:35 +02006976 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006977 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006978
6979 /* make val_end point to the first white space or delimitor after the value */
6980 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006981 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006982 val_end--;
6983 } else {
6984 /* <equal> points to next comma, semi-colon or EOL */
6985 val_beg = val_end = next = equal;
6986 }
6987
6988 if (next < hdr_end) {
6989 /* Set-Cookie2 supports multiple cookies, and <next> points to
6990 * a colon or semi-colon before the end. So skip all attr-value
6991 * pairs and look for the next comma. For Set-Cookie, since
6992 * commas are permitted in values, skip to the end.
6993 */
6994 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006995 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006996 else
6997 next = hdr_end;
6998 }
6999
7000 /* Now everything is as on the diagram above */
7001
7002 /* Ignore cookies with no equal sign */
7003 if (equal == val_end)
7004 continue;
7005
7006 /* If there are spaces around the equal sign, we need to
7007 * strip them otherwise we'll get trouble for cookie captures,
7008 * or even for rewrites. Since this happens extremely rarely,
7009 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007010 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007011 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7012 int stripped_before = 0;
7013 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007014
Willy Tarreau24581ba2010-08-31 22:39:35 +02007015 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007016 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007017 equal += stripped_before;
7018 val_beg += stripped_before;
7019 }
7020
7021 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007022 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007023 val_beg += stripped_after;
7024 stripped_before += stripped_after;
7025 }
7026
7027 val_end += stripped_before;
7028 next += stripped_before;
7029 hdr_end += stripped_before;
7030 hdr_next += stripped_before;
7031 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007032 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007033 }
7034
7035 /* First, let's see if we want to capture this cookie. We check
7036 * that we don't already have a server side cookie, because we
7037 * can only capture one. Also as an optimisation, we ignore
7038 * cookies shorter than the declared name.
7039 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007040 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007041 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007042 (val_end - att_beg >= sess->fe->capture_namelen) &&
7043 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007044 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007045 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007046 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007047 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007048 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007049 if (log_len > sess->fe->capture_len)
7050 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007051 memcpy(txn->srv_cookie, att_beg, log_len);
7052 txn->srv_cookie[log_len] = 0;
7053 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007054 }
7055
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007056 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007057 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007058 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007059 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7060 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007061 /* assume passive cookie by default */
7062 txn->flags &= ~TX_SCK_MASK;
7063 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007064
7065 /* If the cookie is in insert mode on a known server, we'll delete
7066 * this occurrence because we'll insert another one later.
7067 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007068 * a direct access.
7069 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007070 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007071 /* The "preserve" flag was set, we don't want to touch the
7072 * server's cookie.
7073 */
7074 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007075 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007076 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007077 /* this cookie must be deleted */
7078 if (*prev == ':' && next == hdr_end) {
7079 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007080 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007081 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7082 txn->hdr_idx.used--;
7083 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007084 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007085 hdr_next += delta;
7086 http_msg_move_end(&txn->rsp, delta);
7087 /* note: while both invalid now, <next> and <hdr_end>
7088 * are still equal, so the for() will stop as expected.
7089 */
7090 } else {
7091 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007092 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007093 next = prev;
7094 hdr_end += delta;
7095 hdr_next += delta;
7096 cur_hdr->len += delta;
7097 http_msg_move_end(&txn->rsp, delta);
7098 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007099 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007100 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007101 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007102 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007103 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007104 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007105 * with this server since we know it.
7106 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007107 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007108 next += delta;
7109 hdr_end += delta;
7110 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007111 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007112 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007113
Willy Tarreauf1348312010-10-07 15:54:11 +02007114 txn->flags &= ~TX_SCK_MASK;
7115 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007116 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007117 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007118 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007119 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007120 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007121 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007122 next += delta;
7123 hdr_end += delta;
7124 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007125 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007126 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007127
Willy Tarreau827aee92011-03-10 16:55:02 +01007128 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007129 txn->flags &= ~TX_SCK_MASK;
7130 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007131 }
7132 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007133 /* that's done for this cookie, check the next one on the same
7134 * line when next != hdr_end (only if is_cookie2).
7135 */
7136 }
7137 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007138 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007139 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007140}
7141
7142
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007144 * Parses the Cache-Control and Pragma request header fields to determine if
7145 * the request may be served from the cache and/or if it is cacheable. Updates
7146 * s->txn->flags.
7147 */
7148void check_request_for_cacheability(struct stream *s, struct channel *chn)
7149{
7150 struct http_txn *txn = s->txn;
7151 char *p1, *p2;
7152 char *cur_ptr, *cur_end, *cur_next;
7153 int pragma_found;
7154 int cc_found;
7155 int cur_idx;
7156
7157 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7158 return; /* nothing more to do here */
7159
7160 cur_idx = 0;
7161 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007162 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007163
7164 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7165 struct hdr_idx_elem *cur_hdr;
7166 int val;
7167
7168 cur_hdr = &txn->hdr_idx.v[cur_idx];
7169 cur_ptr = cur_next;
7170 cur_end = cur_ptr + cur_hdr->len;
7171 cur_next = cur_end + cur_hdr->cr + 1;
7172
7173 /* We have one full header between cur_ptr and cur_end, and the
7174 * next header starts at cur_next.
7175 */
7176
7177 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7178 if (val) {
7179 if ((cur_end - (cur_ptr + val) >= 8) &&
7180 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7181 pragma_found = 1;
7182 continue;
7183 }
7184 }
7185
William Lallemand8a16fe02018-05-22 11:04:33 +02007186 /* Don't use the cache and don't try to store if we found the
7187 * Authorization header */
7188 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7189 if (val) {
7190 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7191 txn->flags |= TX_CACHE_IGNORE;
7192 continue;
7193 }
7194
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007195 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7196 if (!val)
7197 continue;
7198
7199 /* OK, right now we know we have a cache-control header at cur_ptr */
7200 cc_found = 1;
7201 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7202
7203 if (p1 >= cur_end) /* no more info */
7204 continue;
7205
7206 /* p1 is at the beginning of the value */
7207 p2 = p1;
7208 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7209 p2++;
7210
7211 /* we have a complete value between p1 and p2. We don't check the
7212 * values after max-age, max-stale nor min-fresh, we simply don't
7213 * use the cache when they're specified.
7214 */
7215 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7216 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7217 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7218 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7219 txn->flags |= TX_CACHE_IGNORE;
7220 continue;
7221 }
7222
7223 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7224 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7225 continue;
7226 }
7227 }
7228
7229 /* RFC7234#5.4:
7230 * When the Cache-Control header field is also present and
7231 * understood in a request, Pragma is ignored.
7232 * When the Cache-Control header field is not present in a
7233 * request, caches MUST consider the no-cache request
7234 * pragma-directive as having the same effect as if
7235 * "Cache-Control: no-cache" were present.
7236 */
7237 if (!cc_found && pragma_found)
7238 txn->flags |= TX_CACHE_IGNORE;
7239}
7240
7241/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007242 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007243 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007244void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007246 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007247 char *p1, *p2;
7248
7249 char *cur_ptr, *cur_end, *cur_next;
7250 int cur_idx;
7251
Willy Tarreau12b32f22017-12-21 16:08:09 +01007252 if (txn->status < 200) {
7253 /* do not try to cache interim responses! */
7254 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007256 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007257
7258 /* Iterate through the headers.
7259 * we start with the start line.
7260 */
7261 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007262 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007263
7264 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7265 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007266 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007267
7268 cur_hdr = &txn->hdr_idx.v[cur_idx];
7269 cur_ptr = cur_next;
7270 cur_end = cur_ptr + cur_hdr->len;
7271 cur_next = cur_end + cur_hdr->cr + 1;
7272
7273 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007274 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007275 */
7276
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007277 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7278 if (val) {
7279 if ((cur_end - (cur_ptr + val) >= 8) &&
7280 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7281 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7282 return;
7283 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007284 }
7285
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007286 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7287 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007288 continue;
7289
7290 /* OK, right now we know we have a cache-control header at cur_ptr */
7291
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007292 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007293
7294 if (p1 >= cur_end) /* no more info */
7295 continue;
7296
7297 /* p1 is at the beginning of the value */
7298 p2 = p1;
7299
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007300 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301 p2++;
7302
7303 /* we have a complete value between p1 and p2 */
7304 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007305 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7306 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7307 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7308 continue;
7309 }
7310
Willy Tarreaua15645d2007-03-18 16:22:39 +01007311 /* we have something of the form no-cache="set-cookie" */
7312 if ((cur_end - p1 >= 21) &&
7313 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7314 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007315 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007316 continue;
7317 }
7318
7319 /* OK, so we know that either p2 points to the end of string or to a comma */
7320 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007321 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007322 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007323 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007324 return;
7325 }
7326
7327 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007328 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007329 continue;
7330 }
7331 }
7332}
7333
Willy Tarreau58f10d72006-12-04 02:26:12 +01007334
Willy Tarreaub2513902006-12-17 14:52:38 +01007335/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007336 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007337 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007338 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007339 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007340 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007341 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007342 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007343 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007344int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007345{
7346 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007347 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007348 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007349
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007350 if (!uri_auth)
7351 return 0;
7352
Cyril Bonté70be45d2010-10-12 00:14:35 +02007353 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007354 return 0;
7355
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007356 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007357 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007358 return 0;
7359
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007360 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007361 return 0;
7362
Willy Tarreaub2513902006-12-17 14:52:38 +01007363 return 1;
7364}
7365
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007366/* Append the description of what is present in error snapshot <es> into <out>.
7367 * The description must be small enough to always fit in a trash. The output
7368 * buffer may be the trash so the trash must not be used inside this function.
7369 */
7370void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7371{
7372 chunk_appendf(&trash,
7373 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7374 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7375 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7376 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7377 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7378 es->ctx.http.m_flags, es->ctx.http.m_clen,
7379 es->ctx.http.m_blen, es->ctx.http.b_flags);
7380}
7381
Willy Tarreau4076a152009-04-02 15:18:36 +02007382/*
7383 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007384 * By default it tries to report the error position as msg->err_pos. However if
7385 * this one is not set, it will then report msg->next, which is the last known
7386 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007387 * displays buffers as a contiguous area starting at buf->p. The direction is
7388 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007389 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007390void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007391 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007392 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007393{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007394 union error_snapshot_ctx ctx;
7395 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007396
Willy Tarreauef3ca732018-09-07 15:47:35 +02007397 /* http-specific part now */
7398 ctx.http.sid = s->uniq_id;
7399 ctx.http.state = state;
7400 ctx.http.b_flags = msg->chn->flags;
7401 ctx.http.s_flags = s->flags;
7402 ctx.http.t_flags = s->txn->flags;
7403 ctx.http.m_flags = msg->flags;
7404 ctx.http.m_clen = msg->chunk_len;
7405 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007406
Willy Tarreauef3ca732018-09-07 15:47:35 +02007407 ofs = msg->chn->total - ci_data(msg->chn);
7408 if (ofs < 0)
7409 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007410
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007411 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007412 other_end, s->target,
7413 strm_sess(s), &msg->chn->buf,
7414 ofs, co_data(msg->chn),
7415 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7416 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007417}
Willy Tarreaub2513902006-12-17 14:52:38 +01007418
Willy Tarreau294c4732011-12-16 21:35:50 +01007419/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7420 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7421 * performed over the whole headers. Otherwise it must contain a valid header
7422 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7423 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7424 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7425 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007426 * -1. The value fetch stops at commas, so this function is suited for use with
7427 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007428 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007429 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007430unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007431 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007432 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007433{
Willy Tarreau294c4732011-12-16 21:35:50 +01007434 struct hdr_ctx local_ctx;
7435 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007436 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007437 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007438 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007439
Willy Tarreau294c4732011-12-16 21:35:50 +01007440 if (!ctx) {
7441 local_ctx.idx = 0;
7442 ctx = &local_ctx;
7443 }
7444
Willy Tarreaubce70882009-09-07 11:51:47 +02007445 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007446 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007447 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007448 occ--;
7449 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007450 *vptr = ctx->line + ctx->val;
7451 *vlen = ctx->vlen;
7452 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007453 }
7454 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007455 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007456 }
7457
7458 /* negative occurrence, we scan all the list then walk back */
7459 if (-occ > MAX_HDR_HISTORY)
7460 return 0;
7461
Willy Tarreau294c4732011-12-16 21:35:50 +01007462 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007463 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007464 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7465 len_hist[hist_ptr] = ctx->vlen;
7466 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007467 hist_ptr = 0;
7468 found++;
7469 }
7470 if (-occ > found)
7471 return 0;
7472 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007473 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7474 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7475 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007476 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007477 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007478 if (hist_ptr >= MAX_HDR_HISTORY)
7479 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007480 *vptr = ptr_hist[hist_ptr];
7481 *vlen = len_hist[hist_ptr];
7482 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007483}
7484
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007485/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7486 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7487 * performed over the whole headers. Otherwise it must contain a valid header
7488 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7489 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7490 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7491 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7492 * -1. This function differs from http_get_hdr() in that it only returns full
7493 * line header values and does not stop at commas.
7494 * The return value is 0 if nothing was found, or non-zero otherwise.
7495 */
7496unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7497 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007498 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007499{
7500 struct hdr_ctx local_ctx;
7501 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007502 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007503 unsigned int hist_ptr;
7504 int found;
7505
7506 if (!ctx) {
7507 local_ctx.idx = 0;
7508 ctx = &local_ctx;
7509 }
7510
7511 if (occ >= 0) {
7512 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007513 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007514 occ--;
7515 if (occ <= 0) {
7516 *vptr = ctx->line + ctx->val;
7517 *vlen = ctx->vlen;
7518 return 1;
7519 }
7520 }
7521 return 0;
7522 }
7523
7524 /* negative occurrence, we scan all the list then walk back */
7525 if (-occ > MAX_HDR_HISTORY)
7526 return 0;
7527
7528 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007529 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007530 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7531 len_hist[hist_ptr] = ctx->vlen;
7532 if (++hist_ptr >= MAX_HDR_HISTORY)
7533 hist_ptr = 0;
7534 found++;
7535 }
7536 if (-occ > found)
7537 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007538
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007539 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007540 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7541 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7542 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007543 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007544 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007545 if (hist_ptr >= MAX_HDR_HISTORY)
7546 hist_ptr -= MAX_HDR_HISTORY;
7547 *vptr = ptr_hist[hist_ptr];
7548 *vlen = len_hist[hist_ptr];
7549 return 1;
7550}
7551
Willy Tarreaubaaee002006-06-26 02:48:02 +02007552/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007553 * Print a debug line with a header. Always stop at the first CR or LF char,
7554 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7555 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007556 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007557void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007558{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007559 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007560 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007561
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007562 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007563 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007564 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007565 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 +02007566
7567 for (max = 0; start + max < end; max++)
7568 if (start[max] == '\r' || start[max] == '\n')
7569 break;
7570
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007571 UBOUND(max, trash.size - trash.data - 3);
7572 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7573 trash.area[trash.data++] = '\n';
7574 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007575}
7576
Willy Tarreaueee5b512015-04-03 23:46:31 +02007577
7578/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7579 * The hdr_idx is allocated as well. In case of allocation failure, everything
7580 * allocated is freed and NULL is returned. Otherwise the new transaction is
7581 * assigned to the stream and returned.
7582 */
7583struct http_txn *http_alloc_txn(struct stream *s)
7584{
7585 struct http_txn *txn = s->txn;
7586
7587 if (txn)
7588 return txn;
7589
Willy Tarreaubafbe012017-11-24 17:34:44 +01007590 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007591 if (!txn)
7592 return txn;
7593
7594 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007595 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007596 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007597 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007598 return NULL;
7599 }
7600
7601 s->txn = txn;
7602 return txn;
7603}
7604
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007605void http_txn_reset_req(struct http_txn *txn)
7606{
7607 txn->req.flags = 0;
7608 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7609 txn->req.next = 0;
7610 txn->req.chunk_len = 0LL;
7611 txn->req.body_len = 0LL;
7612 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7613}
7614
7615void http_txn_reset_res(struct http_txn *txn)
7616{
7617 txn->rsp.flags = 0;
7618 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7619 txn->rsp.next = 0;
7620 txn->rsp.chunk_len = 0LL;
7621 txn->rsp.body_len = 0LL;
7622 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7623}
7624
Willy Tarreau0937bc42009-12-22 15:03:09 +01007625/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007626 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007627 * the required fields are properly allocated and that we only need to (re)init
7628 * them. This should be used before processing any new request.
7629 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007630void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007631{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007632 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007633 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007634
7635 txn->flags = 0;
7636 txn->status = -1;
7637
Willy Tarreauf64d1412010-10-07 20:06:11 +02007638 txn->cookie_first_date = 0;
7639 txn->cookie_last_date = 0;
7640
Willy Tarreaueee5b512015-04-03 23:46:31 +02007641 txn->srv_cookie = NULL;
7642 txn->cli_cookie = NULL;
7643 txn->uri = NULL;
7644
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007645 http_txn_reset_req(txn);
7646 http_txn_reset_res(txn);
7647
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007648 txn->req.chn = &s->req;
7649 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007650
7651 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007652
7653 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7654 if (fe->options2 & PR_O2_REQBUG_OK)
7655 txn->req.err_pos = -1; /* let buggy requests pass */
7656
Willy Tarreau0937bc42009-12-22 15:03:09 +01007657 if (txn->hdr_idx.v)
7658 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007659
7660 vars_init(&s->vars_txn, SCOPE_TXN);
7661 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007662}
7663
7664/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007665void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007666{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007667 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007668 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007669
7670 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007671 pool_free(pool_head_requri, txn->uri);
7672 pool_free(pool_head_capture, txn->cli_cookie);
7673 pool_free(pool_head_capture, txn->srv_cookie);
7674 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007675
William Lallemanda73203e2012-03-12 12:48:57 +01007676 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007677 txn->uri = NULL;
7678 txn->srv_cookie = NULL;
7679 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007680
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007681 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007682 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007683 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007684 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007685 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007686 }
7687
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007688 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007689 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007690 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007691 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007692 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007693 }
7694
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007695 if (!LIST_ISEMPTY(&s->vars_txn.head))
7696 vars_prune(&s->vars_txn, s->sess, s);
7697 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7698 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007699}
7700
7701/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007702void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007703{
7704 http_end_txn(s);
7705 http_init_txn(s);
7706
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007707 /* reinitialise the current rule list pointer to NULL. We are sure that
7708 * any rulelist match the NULL pointer.
7709 */
7710 s->current_rule_list = NULL;
7711
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007712 s->be = strm_fe(s);
7713 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007714 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007715 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007716 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007717 /* re-init store persistence */
7718 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007719 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007720
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007721 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007722
Willy Tarreau739cfba2010-01-25 23:11:14 +01007723 /* We must trim any excess data from the response buffer, because we
7724 * may have blocked an invalid response from a server that we don't
7725 * want to accidentely forward once we disable the analysers, nor do
7726 * we want those data to come along with next response. A typical
7727 * example of such data would be from a buggy server responding to
7728 * a HEAD with some data, or sending more than the advertised
7729 * content-length.
7730 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007731 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007732 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007733
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007734 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007735 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007736
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007737 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007738 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007739
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007740 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007741 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007742
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007743 s->req.rex = TICK_ETERNITY;
7744 s->req.wex = TICK_ETERNITY;
7745 s->req.analyse_exp = TICK_ETERNITY;
7746 s->res.rex = TICK_ETERNITY;
7747 s->res.wex = TICK_ETERNITY;
7748 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007749 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007750}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007751
Willy Tarreau79e57332018-10-02 16:01:16 +02007752/* This function executes one of the set-{method,path,query,uri} actions. It
7753 * takes the string from the variable 'replace' with length 'len', then modifies
7754 * the relevant part of the request line accordingly. Then it updates various
7755 * pointers to the next elements which were moved, and the total buffer length.
7756 * It finds the action to be performed in p[2], previously filled by function
7757 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7758 * error, though this can be revisited when this code is finally exploited.
7759 *
7760 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7761 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007762 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007763 * In query string case, the mark question '?' must be set at the start of the
7764 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007765 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007766int http_replace_req_line(int action, const char *replace, int len,
7767 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007768{
Willy Tarreau79e57332018-10-02 16:01:16 +02007769 struct http_txn *txn = s->txn;
7770 char *cur_ptr, *cur_end;
7771 int offset = 0;
7772 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007773
Willy Tarreau79e57332018-10-02 16:01:16 +02007774 switch (action) {
7775 case 0: // method
7776 cur_ptr = ci_head(&s->req);
7777 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007778
Willy Tarreau79e57332018-10-02 16:01:16 +02007779 /* adjust req line offsets and lengths */
7780 delta = len - offset - (cur_end - cur_ptr);
7781 txn->req.sl.rq.m_l += delta;
7782 txn->req.sl.rq.u += delta;
7783 txn->req.sl.rq.v += delta;
7784 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007785
Willy Tarreau79e57332018-10-02 16:01:16 +02007786 case 1: // path
7787 cur_ptr = http_txn_get_path(txn);
7788 if (!cur_ptr)
7789 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007790
Willy Tarreau79e57332018-10-02 16:01:16 +02007791 cur_end = cur_ptr;
7792 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7793 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007794
Willy Tarreau79e57332018-10-02 16:01:16 +02007795 /* adjust req line offsets and lengths */
7796 delta = len - offset - (cur_end - cur_ptr);
7797 txn->req.sl.rq.u_l += delta;
7798 txn->req.sl.rq.v += delta;
7799 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007800
Willy Tarreau79e57332018-10-02 16:01:16 +02007801 case 2: // query
7802 offset = 1;
7803 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7804 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7805 while (cur_ptr < cur_end && *cur_ptr != '?')
7806 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007807
Willy Tarreau79e57332018-10-02 16:01:16 +02007808 /* skip the question mark or indicate that we must insert it
7809 * (but only if the format string is not empty then).
7810 */
7811 if (cur_ptr < cur_end)
7812 cur_ptr++;
7813 else if (len > 1)
7814 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007815
Willy Tarreau79e57332018-10-02 16:01:16 +02007816 /* adjust req line offsets and lengths */
7817 delta = len - offset - (cur_end - cur_ptr);
7818 txn->req.sl.rq.u_l += delta;
7819 txn->req.sl.rq.v += delta;
7820 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007821
Willy Tarreau79e57332018-10-02 16:01:16 +02007822 case 3: // uri
7823 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7824 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007825
Willy Tarreau79e57332018-10-02 16:01:16 +02007826 /* adjust req line offsets and lengths */
7827 delta = len - offset - (cur_end - cur_ptr);
7828 txn->req.sl.rq.u_l += delta;
7829 txn->req.sl.rq.v += delta;
7830 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007831
Willy Tarreau79e57332018-10-02 16:01:16 +02007832 default:
7833 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007834 }
7835
Willy Tarreau79e57332018-10-02 16:01:16 +02007836 /* commit changes and adjust end of message */
7837 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7838 txn->req.sl.rq.l += delta;
7839 txn->hdr_idx.v[0].len += delta;
7840 http_msg_move_end(&txn->req, delta);
7841 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007842}
7843
Willy Tarreau79e57332018-10-02 16:01:16 +02007844/* This function replace the HTTP status code and the associated message. The
7845 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007846 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007847void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007848{
Willy Tarreau79e57332018-10-02 16:01:16 +02007849 struct http_txn *txn = s->txn;
7850 char *cur_ptr, *cur_end;
7851 int delta;
7852 char *res;
7853 int c_l;
7854 const char *msg = reason;
7855 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007856
Willy Tarreau79e57332018-10-02 16:01:16 +02007857 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007858
Willy Tarreau79e57332018-10-02 16:01:16 +02007859 res = ultoa_o(status, trash.area, trash.size);
7860 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007861
Willy Tarreau79e57332018-10-02 16:01:16 +02007862 trash.area[c_l] = ' ';
7863 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007864
Willy Tarreau79e57332018-10-02 16:01:16 +02007865 /* Do we have a custom reason format string? */
7866 if (msg == NULL)
7867 msg = http_get_reason(status);
7868 msg_len = strlen(msg);
7869 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7870 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007871
Willy Tarreau79e57332018-10-02 16:01:16 +02007872 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7873 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007874
Willy Tarreau79e57332018-10-02 16:01:16 +02007875 /* commit changes and adjust message */
7876 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7877 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007878
Willy Tarreau79e57332018-10-02 16:01:16 +02007879 /* adjust res line offsets and lengths */
7880 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7881 txn->rsp.sl.st.c_l = c_l;
7882 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007883
Willy Tarreau79e57332018-10-02 16:01:16 +02007884 delta = trash.data - (cur_end - cur_ptr);
7885 txn->rsp.sl.st.l += delta;
7886 txn->hdr_idx.v[0].len += delta;
7887 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007888}
7889
Willy Tarreau8797c062007-05-07 00:55:35 +02007890__attribute__((constructor))
7891static void __http_protocol_init(void)
7892{
Willy Tarreau8797c062007-05-07 00:55:35 +02007893}
7894
7895
Willy Tarreau58f10d72006-12-04 02:26:12 +01007896/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007897 * Local variables:
7898 * c-indent-level: 8
7899 * c-basic-offset: 8
7900 * End:
7901 */