blob: 019556cc912e5c10667cb7f36c7f34a39e275486 [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{
Christopher Faulet9768c262018-10-22 09:34:31 +0200396 if (IS_HTX_STRM(s))
397 return htx_server_error(s, si, err, finst, msg);
398
Willy Tarreau2019f952017-03-14 11:07:31 +0100399 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100400 channel_auto_read(si_oc(si));
401 channel_abort(si_oc(si));
402 channel_auto_close(si_oc(si));
403 channel_erase(si_oc(si));
404 channel_auto_close(si_ic(si));
405 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200406 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200407 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200408 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200409 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200410 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200411 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412}
413
Willy Tarreau87b09662015-04-03 00:22:06 +0200414/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100415 * and message.
416 */
417
Willy Tarreau83061a82018-07-13 11:56:34 +0200418struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100419{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200420 const int msgnum = http_get_status_idx(s->txn->status);
421
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200422 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200423 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200424 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200425 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100426 else
427 return &http_err_chunks[msgnum];
428}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200429
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100430void
Willy Tarreau83061a82018-07-13 11:56:34 +0200431http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100432{
Christopher Faulet9768c262018-10-22 09:34:31 +0200433 if (IS_HTX_STRM(s))
434 return htx_reply_and_close(s, status, msg);
435
Christopher Fauletd7c91962015-04-30 11:48:27 +0200436 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100437 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100438 stream_int_retnclose(&s->si[0], msg);
439}
440
Willy Tarreau21d2af32008-02-14 20:25:24 +0100441/* Parse the URI from the given transaction (which is assumed to be in request
442 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
443 * It is returned otherwise.
444 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200445char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100446{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200447 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100448
Willy Tarreau9d9ccdb2018-10-28 20:13:12 +0100449 if (!txn->req.chn->buf.size)
450 return NULL;
451
Willy Tarreau6b952c82018-09-10 17:45:34 +0200452 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 +0100453
Willy Tarreau6b952c82018-09-10 17:45:34 +0200454 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100455}
456
Willy Tarreau71241ab2012-12-27 11:30:54 +0100457/* Returns a 302 for a redirectable request that reaches a server working in
458 * in redirect mode. This may only be called just after the stream interface
459 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
460 * follow normal proxy processing. NOTE: this function is designed to support
461 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100462 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200463void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100464{
465 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100466 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100467 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200468 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100469
Christopher Fauletfefc73d2018-10-24 21:18:04 +0200470 if (IS_HTX_STRM(s))
471 return htx_perform_server_redirect(s, si);
472
Willy Tarreauefb453c2008-10-26 20:49:47 +0100473 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200474 trash.data = strlen(HTTP_302);
475 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100476
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200477 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100478
Willy Tarreauefb453c2008-10-26 20:49:47 +0100479 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200480 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100481 return;
482
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100483 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100484 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200485 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
486 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100487 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100488
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200489 /* 3: add the request URI. Since it was already forwarded, we need
490 * to temporarily rewind the buffer.
491 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200492 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200493 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200494
Willy Tarreau6b952c82018-09-10 17:45:34 +0200495 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200496 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 +0200497
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200498 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200499
Willy Tarreauefb453c2008-10-26 20:49:47 +0100500 if (!path)
501 return;
502
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100504 return;
505
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200506 memcpy(trash.area + trash.data, path, len);
507 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100508
509 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200510 memcpy(trash.area + trash.data,
511 "\r\nProxy-Connection: close\r\n\r\n", 29);
512 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100513 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 memcpy(trash.area + trash.data,
515 "\r\nConnection: close\r\n\r\n", 23);
516 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100517 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100518
519 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200520 si_shutr(si);
521 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100522 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100523 si->state = SI_ST_CLO;
524
525 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100526 txn->status = 302;
527 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100528
529 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100530 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500531 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100532}
533
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100534/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100535 * that the server side is closed. Note that err_type is actually a
536 * bitmask, where almost only aborts may be cumulated with other
537 * values. We consider that aborted operations are more important
538 * than timeouts or errors due to the fact that nobody else in the
539 * logs might explain incomplete retries. All others should avoid
540 * being cumulated. It should normally not be possible to have multiple
541 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100542 * Note that connection errors appearing on the second request of a keep-alive
543 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100544 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200545void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100546{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100547 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100548
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200549 /* set s->txn->status for http_error_message(s) */
550 s->txn->status = 503;
551
Willy Tarreauefb453c2008-10-26 20:49:47 +0100552 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200553 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100554 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100555 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200556 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100557 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200558 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100559 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200560 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100561 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100562 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200563 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100564 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100565 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200566 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100567 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200568 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100569 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200570 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100571 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200572 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200573 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200574 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100575 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200576 http_error_message(s));
577 else { /* SI_ET_CONN_OTHER and others */
578 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200579 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100580 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200581 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100582}
583
Willy Tarreau42250582007-04-01 01:30:43 +0200584extern const char sess_term_cond[8];
585extern const char sess_fin_state[8];
586extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +0100587struct pool_head *pool_head_http_txn;
588struct pool_head *pool_head_requri;
589struct pool_head *pool_head_capture = NULL;
590struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100591
Willy Tarreau117f59e2007-03-04 18:17:17 +0100592/*
593 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200594 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100595 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200596void http_capture_headers(char *som, struct hdr_idx *idx,
597 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100598{
599 char *eol, *sol, *col, *sov;
600 int cur_idx;
601 struct cap_hdr *h;
602 int len;
603
604 sol = som + hdr_idx_first_pos(idx);
605 cur_idx = hdr_idx_first_idx(idx);
606
607 while (cur_idx) {
608 eol = sol + idx->v[cur_idx].len;
609
610 col = sol;
611 while (col < eol && *col != ':')
612 col++;
613
614 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100615 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100616 sov++;
617
618 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200619 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100620 (strncasecmp(sol, h->name, h->namelen) == 0)) {
621 if (cap[h->index] == NULL)
622 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100623 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100624
625 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100626 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100627 continue;
628 }
629
630 len = eol - sov;
631 if (len > h->len)
632 len = h->len;
633
634 memcpy(cap[h->index], sov, len);
635 cap[h->index][len]=0;
636 }
637 }
638 sol = eol + idx->v[cur_idx].cr + 1;
639 cur_idx = idx->v[cur_idx].next;
640 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100641}
642
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200643/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
644 * conversion succeeded, 0 in case of error. If the request was already 1.X,
645 * nothing is done and 1 is returned.
646 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200647int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200648{
649 int delta;
650 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100651 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200652
653 if (msg->sl.rq.v_l != 0)
654 return 1;
655
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300656 /* RFC 1945 allows only GET for HTTP/0.9 requests */
657 if (txn->meth != HTTP_METH_GET)
658 return 0;
659
Willy Tarreauf37954d2018-06-15 18:31:02 +0200660 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200661
662 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300663 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
664 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200665 }
666 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200667 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100668 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200669 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200670 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200671 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200672 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200673 NULL, NULL);
674 if (unlikely(!cur_end))
675 return 0;
676
677 /* we have a full HTTP/1.0 request now and we know that
678 * we have either a CR or an LF at <ptr>.
679 */
680 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
681 return 1;
682}
683
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100684/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100685 * and "keep-alive" values. If we already know that some headers may safely
686 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100687 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
688 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100689 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100690 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
691 * found, and TX_CON_*_SET is adjusted depending on what is left so only
692 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100693 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100694 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100695void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100696{
Willy Tarreau5b154472009-12-21 20:11:07 +0100697 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100698 const char *hdr_val = "Connection";
699 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100700
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100701 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100702 return;
703
Willy Tarreau88d349d2010-01-25 12:15:43 +0100704 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
705 hdr_val = "Proxy-Connection";
706 hdr_len = 16;
707 }
708
Willy Tarreau5b154472009-12-21 20:11:07 +0100709 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100710 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200711 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100712 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
713 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100714 if (to_del & 2)
715 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100716 else
717 txn->flags |= TX_CON_KAL_SET;
718 }
719 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
720 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100721 if (to_del & 1)
722 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100723 else
724 txn->flags |= TX_CON_CLO_SET;
725 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100726 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
727 txn->flags |= TX_HDR_CONN_UPG;
728 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100729 }
730
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100731 txn->flags |= TX_HDR_CONN_PRS;
732 return;
733}
Willy Tarreau5b154472009-12-21 20:11:07 +0100734
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100735/* Apply desired changes on the Connection: header. Values may be removed and/or
736 * added depending on the <wanted> flags, which are exclusively composed of
737 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
738 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
739 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100740void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100741{
742 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100743 const char *hdr_val = "Connection";
744 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100745
746 ctx.idx = 0;
747
Willy Tarreau88d349d2010-01-25 12:15:43 +0100748
749 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
750 hdr_val = "Proxy-Connection";
751 hdr_len = 16;
752 }
753
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100754 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200755 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100756 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
757 if (wanted & TX_CON_KAL_SET)
758 txn->flags |= TX_CON_KAL_SET;
759 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100760 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100761 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100762 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
763 if (wanted & TX_CON_CLO_SET)
764 txn->flags |= TX_CON_CLO_SET;
765 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100766 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100767 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100768 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100769
770 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
771 return;
772
773 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
774 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100775 hdr_val = "Connection: close";
776 hdr_len = 17;
777 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
778 hdr_val = "Proxy-Connection: close";
779 hdr_len = 23;
780 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100781 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100782 }
783
784 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
785 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100786 hdr_val = "Connection: keep-alive";
787 hdr_len = 22;
788 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
789 hdr_val = "Proxy-Connection: keep-alive";
790 hdr_len = 28;
791 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100792 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100793 }
794 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100795}
796
Willy Tarreau87b09662015-04-03 00:22:06 +0200797void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200798{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200799 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200800 int tmp = TX_CON_WANT_KAL;
801
Christopher Fauletf2824e62018-10-01 12:12:37 +0200802 if (IS_HTX_STRM(s))
Christopher Faulet0f226952018-10-22 09:29:56 +0200803 return htx_adjust_conn_mode(s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200804
Christopher Faulet315b39c2018-09-21 16:26:19 +0200805 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
806 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
807 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200808
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200809 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200810 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200811 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200812
Christopher Faulet315b39c2018-09-21 16:26:19 +0200813 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
814 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200815 tmp = TX_CON_WANT_CLO;
816
817 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
818 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
819
820 if (!(txn->flags & TX_HDR_CONN_PRS) &&
821 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
822 /* parse the Connection header and possibly clean it */
823 int to_del = 0;
824 if ((msg->flags & HTTP_MSGF_VER_11) ||
825 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200826 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200827 to_del |= 2; /* remove "keep-alive" */
828 if (!(msg->flags & HTTP_MSGF_VER_11))
829 to_del |= 1; /* remove "close" */
830 http_parse_connection_header(txn, msg, to_del);
831 }
832
833 /* check if client or config asks for explicit close in KAL/SCL */
834 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
835 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
836 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
837 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
838 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200839 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200840 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
841}
William Lallemand82fe75c2012-10-23 10:25:10 +0200842
Willy Tarreaud787e662009-07-07 10:14:51 +0200843/* This stream analyser waits for a complete HTTP request. It returns 1 if the
844 * processing can continue on next analysers, or zero if it either needs more
845 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100846 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200847 * when it has nothing left to do, and may remove any analyser when it wants to
848 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100849 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200850int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100851{
Willy Tarreau59234e92008-11-30 23:51:27 +0100852 /*
853 * We will parse the partial (or complete) lines.
854 * We will check the request syntax, and also join multi-line
855 * headers. An index of all the lines will be elaborated while
856 * parsing.
857 *
858 * For the parsing, we use a 28 states FSM.
859 *
860 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200861 * ci_head(req) = beginning of request
862 * ci_head(req) + msg->eoh = end of processed headers / start of current one
863 * ci_tail(req) = end of input data
864 * msg->eol = end of current header or line (LF or CRLF)
865 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200866 *
867 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200868 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200869 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
870 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100871 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100872
Willy Tarreau59234e92008-11-30 23:51:27 +0100873 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200874 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200875 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100876 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200877 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100878
Christopher Faulete0768eb2018-10-03 16:38:02 +0200879 if (IS_HTX_STRM(s))
880 return htx_wait_for_request(s, req, an_bit);
881
Christopher Faulet45073512018-07-20 10:16:29 +0200882 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 +0100883 now_ms, __FUNCTION__,
884 s,
885 req,
886 req->rex, req->wex,
887 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200888 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100889 req->analysers);
890
Willy Tarreau52a0c602009-08-16 22:45:38 +0200891 /* we're speaking HTTP here, so let's speak HTTP to the client */
892 s->srv_error = http_return_srv_error;
893
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300894 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200895 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300896 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
897
Willy Tarreau83e3af02009-12-28 17:39:57 +0100898 /* There's a protected area at the end of the buffer for rewriting
899 * purposes. We don't want to start to parse the request if the
900 * protected area is affected, because we may have to move processed
901 * data later, which is much more complicated.
902 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200903 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200904 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100905 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200906 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100907 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100908 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200909 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200910 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100911 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100912 return 0;
913 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200914 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200915 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200916 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100917 }
918
Willy Tarreauf37954d2018-06-15 18:31:02 +0200919 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100920 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100921 }
922
Willy Tarreau59234e92008-11-30 23:51:27 +0100923 /* 1: we might have to print this header in debug mode */
924 if (unlikely((global.mode & MODE_DEBUG) &&
925 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200926 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100927 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100928
Willy Tarreauf37954d2018-06-15 18:31:02 +0200929 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200930 /* this is a bit complex : in case of error on the request line,
931 * we know that rq.l is still zero, so we display only the part
932 * up to the end of the line (truncated by debug_hdr).
933 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200934 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100935 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100936
Willy Tarreau59234e92008-11-30 23:51:27 +0100937 sol += hdr_idx_first_pos(&txn->hdr_idx);
938 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100939
Willy Tarreau59234e92008-11-30 23:51:27 +0100940 while (cur_idx) {
941 eol = sol + txn->hdr_idx.v[cur_idx].len;
942 debug_hdr("clihdr", s, sol, eol);
943 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
944 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100945 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100946 }
947
Willy Tarreau58f10d72006-12-04 02:26:12 +0100948
Willy Tarreau59234e92008-11-30 23:51:27 +0100949 /*
950 * Now we quickly check if we have found a full valid request.
951 * If not so, we check the FD and buffer states before leaving.
952 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100953 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100954 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200955 * on a keep-alive stream, if we encounter and error, close, t/o,
956 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100957 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200958 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200959 * Last, we may increase some tracked counters' http request errors on
960 * the cases that are deliberately the client's fault. For instance,
961 * a timeout or connection reset is not counted as an error. However
962 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100963 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100964
Willy Tarreau655dce92009-11-08 13:10:58 +0100965 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100966 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100967 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100968 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100969 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
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 Tarreau59234e92008-11-30 23:51:27 +0100973 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100974 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100975
Willy Tarreau59234e92008-11-30 23:51:27 +0100976 /* 1: Since we are in header mode, if there's no space
977 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200978 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100979 * must terminate it now.
980 */
Willy Tarreau23752332018-06-15 14:54:53 +0200981 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100982 /* FIXME: check if URI is set and return Status
983 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100984 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200985 stream_inc_http_req_ctr(s);
986 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200987 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200988 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200989 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100990 goto return_bad_req;
991 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100992
Willy Tarreau59234e92008-11-30 23:51:27 +0100993 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200994 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200995 if (!(s->flags & SF_ERR_MASK))
996 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100997
Willy Tarreaufcffa692010-01-10 14:21:19 +0100998 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100999 goto failed_keep_alive;
1000
Willy Tarreau0f228a02015-05-01 15:37:53 +02001001 if (sess->fe->options & PR_O_IGNORE_PRB)
1002 goto failed_keep_alive;
1003
Willy Tarreau59234e92008-11-30 23:51:27 +01001004 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001005 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001006 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001007 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001008 }
1009
Willy Tarreaudc979f22012-12-04 10:39:01 +01001010 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001011 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001012 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001013 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001014 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001015 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001016 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001017 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001018 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001019 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001020
Willy Tarreaue7dff022015-04-03 01:14:29 +02001021 if (!(s->flags & SF_FINST_MASK))
1022 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001023 return 0;
1024 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001025
Willy Tarreau59234e92008-11-30 23:51:27 +01001026 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001027 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001028 if (!(s->flags & SF_ERR_MASK))
1029 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001030
Willy Tarreaufcffa692010-01-10 14:21:19 +01001031 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001032 goto failed_keep_alive;
1033
Willy Tarreau0f228a02015-05-01 15:37:53 +02001034 if (sess->fe->options & PR_O_IGNORE_PRB)
1035 goto failed_keep_alive;
1036
Willy Tarreau59234e92008-11-30 23:51:27 +01001037 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001038 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001039 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001040 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001041 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001042 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001043 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001044 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001045 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001046 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001047
Willy Tarreau87b09662015-04-03 00:22:06 +02001048 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001049 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001050 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001051 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001052 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001053
Willy Tarreaue7dff022015-04-03 01:14:29 +02001054 if (!(s->flags & SF_FINST_MASK))
1055 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001056 return 0;
1057 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001058
Willy Tarreau59234e92008-11-30 23:51:27 +01001059 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001060 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001061 if (!(s->flags & SF_ERR_MASK))
1062 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001063
Willy Tarreaufcffa692010-01-10 14:21:19 +01001064 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001065 goto failed_keep_alive;
1066
Willy Tarreau0f228a02015-05-01 15:37:53 +02001067 if (sess->fe->options & PR_O_IGNORE_PRB)
1068 goto failed_keep_alive;
1069
Willy Tarreau4076a152009-04-02 15:18:36 +02001070 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001071 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001072 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001073 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001074 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001075 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001076 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001077 stream_inc_http_err_ctr(s);
1078 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001079 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001080 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001081 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001082 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001083
Willy Tarreaue7dff022015-04-03 01:14:29 +02001084 if (!(s->flags & SF_FINST_MASK))
1085 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001086 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001087 }
1088
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001089 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001090 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001091 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001092#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001093 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001094 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001095 /* We need more data, we have to re-enable quick-ack in case we
1096 * previously disabled it, otherwise we might cause the client
1097 * to delay next data.
1098 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001099 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001100 }
1101#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001102
Willy Tarreaufcffa692010-01-10 14:21:19 +01001103 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1104 /* If the client starts to talk, let's fall back to
1105 * request timeout processing.
1106 */
1107 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001108 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001109 }
1110
Willy Tarreau59234e92008-11-30 23:51:27 +01001111 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001112 if (!tick_isset(req->analyse_exp)) {
1113 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1114 (txn->flags & TX_WAIT_NEXT_RQ) &&
1115 tick_isset(s->be->timeout.httpka))
1116 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1117 else
1118 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1119 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001120
Willy Tarreau59234e92008-11-30 23:51:27 +01001121 /* we're not ready yet */
1122 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001123
1124 failed_keep_alive:
1125 /* Here we process low-level errors for keep-alive requests. In
1126 * short, if the request is not the first one and it experiences
1127 * a timeout, read error or shutdown, we just silently close so
1128 * that the client can try again.
1129 */
1130 txn->status = 0;
1131 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001132 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001133 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001134 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001135 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001136 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001137 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001138 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001139
Willy Tarreaud787e662009-07-07 10:14:51 +02001140 /* OK now we have a complete HTTP request with indexed headers. Let's
1141 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001142 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001143 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001144 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001145 * byte after the last LF. msg->sov points to the first byte of data.
1146 * msg->eol cannot be trusted because it may have been left uninitialized
1147 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001148 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001149
Willy Tarreau87b09662015-04-03 00:22:06 +02001150 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001151 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001152
Willy Tarreaub16a5742010-01-10 14:46:16 +01001153 if (txn->flags & TX_WAIT_NEXT_RQ) {
1154 /* kill the pending keep-alive timeout */
1155 txn->flags &= ~TX_WAIT_NEXT_RQ;
1156 req->analyse_exp = TICK_ETERNITY;
1157 }
1158
1159
Willy Tarreaud787e662009-07-07 10:14:51 +02001160 /* Maybe we found in invalid header name while we were configured not
1161 * to block on that, so we have to capture it now.
1162 */
1163 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001164 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001165
Willy Tarreau59234e92008-11-30 23:51:27 +01001166 /*
1167 * 1: identify the method
1168 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001169 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001170
1171 /* we can make use of server redirect on GET and HEAD */
1172 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001173 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001174 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001175 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001176 /* PRI is reserved for the HTTP/2 preface */
1177 msg->err_pos = 0;
1178 goto return_bad_req;
1179 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001180
Willy Tarreau59234e92008-11-30 23:51:27 +01001181 /*
1182 * 2: check if the URI matches the monitor_uri.
1183 * We have to do this for every request which gets in, because
1184 * the monitor-uri is defined by the frontend.
1185 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001186 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1187 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001188 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001189 sess->fe->monitor_uri,
1190 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001191 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001192 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001193 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001194 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001195
Willy Tarreaue7dff022015-04-03 01:14:29 +02001196 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001197 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001198
Willy Tarreau59234e92008-11-30 23:51:27 +01001199 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001200 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001201 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001202
Willy Tarreau59234e92008-11-30 23:51:27 +01001203 ret = acl_pass(ret);
1204 if (cond->pol == ACL_COND_UNLESS)
1205 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001206
Willy Tarreau59234e92008-11-30 23:51:27 +01001207 if (ret) {
1208 /* we fail this request, let's return 503 service unavail */
1209 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001210 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001211 if (!(s->flags & SF_ERR_MASK))
1212 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001213 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001214 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001215 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001216
Joseph Herlant5ba80252018-11-15 09:25:36 -08001217 /* nothing to fail, let's reply normally */
Willy Tarreau59234e92008-11-30 23:51:27 +01001218 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001219 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001220 if (!(s->flags & SF_ERR_MASK))
1221 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001222 goto return_prx_cond;
1223 }
1224
1225 /*
1226 * 3: Maybe we have to copy the original REQURI for the logs ?
1227 * Note: we cannot log anymore if the request has been
1228 * classified as invalid.
1229 */
1230 if (unlikely(s->logs.logwait & LW_REQ)) {
1231 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001232 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001233 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001235 if (urilen >= global.tune.requri_len )
1236 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001237 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001238 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001240 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001241 s->do_log(s);
1242 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001243 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001245 }
Willy Tarreau06619262006-12-17 08:37:22 +01001246
Willy Tarreau91852eb2015-05-01 13:26:00 +02001247 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1248 * exactly one digit "." one digit. This check may be disabled using
1249 * option accept-invalid-http-request.
1250 */
1251 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1252 if (msg->sl.rq.v_l != 8) {
1253 msg->err_pos = msg->sl.rq.v;
1254 goto return_bad_req;
1255 }
1256
Willy Tarreauf37954d2018-06-15 18:31:02 +02001257 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1258 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1259 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1260 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001261 msg->err_pos = msg->sl.rq.v + 4;
1262 goto return_bad_req;
1263 }
1264 }
Willy Tarreau13317662015-05-01 13:47:08 +02001265 else {
1266 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1267 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1268 goto return_bad_req;
1269 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001270
Willy Tarreau5b154472009-12-21 20:11:07 +01001271 /* ... and check if the request is HTTP/1.1 or above */
1272 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001273 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1274 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1275 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001276 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001277
1278 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001279 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 +01001280
Willy Tarreau88d349d2010-01-25 12:15:43 +01001281 /* if the frontend has "option http-use-proxy-header", we'll check if
1282 * we have what looks like a proxied connection instead of a connection,
1283 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1284 * Note that this is *not* RFC-compliant, however browsers and proxies
1285 * happen to do that despite being non-standard :-(
1286 * We consider that a request not beginning with either '/' or '*' is
1287 * a proxied connection, which covers both "scheme://location" and
1288 * CONNECT ip:port.
1289 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001290 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001291 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001292 txn->flags |= TX_USE_PX_CONN;
1293
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001294 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001295 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001296
Willy Tarreau59234e92008-11-30 23:51:27 +01001297 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001298 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001299 http_capture_headers(ci_head(req), &txn->hdr_idx,
1300 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001301
Willy Tarreau557f1992015-05-01 10:05:17 +02001302 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1303 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001304 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001305 * The length of a message body is determined by one of the following
1306 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001307 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001308 * 1. Any response to a HEAD request and any response with a 1xx
1309 * (Informational), 204 (No Content), or 304 (Not Modified) status
1310 * code is always terminated by the first empty line after the
1311 * header fields, regardless of the header fields present in the
1312 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001313 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001314 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1315 * the connection will become a tunnel immediately after the empty
1316 * line that concludes the header fields. A client MUST ignore any
1317 * Content-Length or Transfer-Encoding header fields received in
1318 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001319 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001320 * 3. If a Transfer-Encoding header field is present and the chunked
1321 * transfer coding (Section 4.1) is the final encoding, the message
1322 * body length is determined by reading and decoding the chunked
1323 * data until the transfer coding indicates the data is complete.
1324 *
1325 * If a Transfer-Encoding header field is present in a response and
1326 * the chunked transfer coding is not the final encoding, the
1327 * message body length is determined by reading the connection until
1328 * it is closed by the server. If a Transfer-Encoding header field
1329 * is present in a request and the chunked transfer coding is not
1330 * the final encoding, the message body length cannot be determined
1331 * reliably; the server MUST respond with the 400 (Bad Request)
1332 * status code and then close the connection.
1333 *
1334 * If a message is received with both a Transfer-Encoding and a
1335 * Content-Length header field, the Transfer-Encoding overrides the
1336 * Content-Length. Such a message might indicate an attempt to
1337 * perform request smuggling (Section 9.5) or response splitting
1338 * (Section 9.4) and ought to be handled as an error. A sender MUST
1339 * remove the received Content-Length field prior to forwarding such
1340 * a message downstream.
1341 *
1342 * 4. If a message is received without Transfer-Encoding and with
1343 * either multiple Content-Length header fields having differing
1344 * field-values or a single Content-Length header field having an
1345 * invalid value, then the message framing is invalid and the
1346 * recipient MUST treat it as an unrecoverable error. If this is a
1347 * request message, the server MUST respond with a 400 (Bad Request)
1348 * status code and then close the connection. If this is a response
1349 * message received by a proxy, the proxy MUST close the connection
1350 * to the server, discard the received response, and send a 502 (Bad
1351 * Gateway) response to the client. If this is a response message
1352 * received by a user agent, the user agent MUST close the
1353 * connection to the server and discard the received response.
1354 *
1355 * 5. If a valid Content-Length header field is present without
1356 * Transfer-Encoding, its decimal value defines the expected message
1357 * body length in octets. If the sender closes the connection or
1358 * the recipient times out before the indicated number of octets are
1359 * received, the recipient MUST consider the message to be
1360 * incomplete and close the connection.
1361 *
1362 * 6. If this is a request message and none of the above are true, then
1363 * the message body length is zero (no message body is present).
1364 *
1365 * 7. Otherwise, this is a response message without a declared message
1366 * body length, so the message body length is determined by the
1367 * number of octets received prior to the server closing the
1368 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001369 */
1370
Willy Tarreau32b47f42009-10-18 20:55:02 +02001371 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001372 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001373 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001374 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001375 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001376 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001377 /* chunked not last, return badreq */
1378 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001379 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001380 }
1381
Willy Tarreau1c913912015-04-30 10:57:51 +02001382 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001383 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001384 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001385 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001386 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1387 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001388 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001389 signed long long cl;
1390
Willy Tarreauad14f752011-09-02 20:33:27 +02001391 if (!ctx.vlen) {
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;
Willy Tarreauad14f752011-09-02 20:33:27 +02001394 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001395
Willy Tarreauad14f752011-09-02 20:33:27 +02001396 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001397 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001398 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001399 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001400
Willy Tarreauad14f752011-09-02 20:33:27 +02001401 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001402 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001403 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001404 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001405
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001406 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001407 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001408 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001409 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001410
Christopher Fauletbe821b92017-03-30 11:21:53 +02001411 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001412 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001413 }
1414
Willy Tarreau34dfc602015-05-01 10:09:49 +02001415 /* even bodyless requests have a known length */
1416 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001417
Willy Tarreau179085c2014-04-28 16:48:56 +02001418 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1419 * only change if both the request and the config reference something else.
1420 * Option httpclose by itself sets tunnel mode where headers are mangled.
1421 * However, if another mode is set, it will affect it (eg: server-close/
1422 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1423 * if FE and BE have the same settings (common). The method consists in
1424 * checking if options changed between the two calls (implying that either
1425 * one is non-null, or one of them is non-null and we are there for the first
1426 * time.
1427 */
1428 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001429 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001430 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001431
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001432 /* we may have to wait for the request's body */
1433 if ((s->be->options & PR_O_WREQ_BODY) &&
1434 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1435 req->analysers |= AN_REQ_HTTP_BODY;
1436
Willy Tarreau83ece462017-12-21 15:13:09 +01001437 /*
1438 * RFC7234#4:
1439 * A cache MUST write through requests with methods
1440 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1441 * the origin server; i.e., a cache is not allowed
1442 * to generate a reply to such a request before
1443 * having forwarded the request and having received
1444 * a corresponding response.
1445 *
1446 * RFC7231#4.2.1:
1447 * Of the request methods defined by this
1448 * specification, the GET, HEAD, OPTIONS, and TRACE
1449 * methods are defined to be safe.
1450 */
1451 if (likely(txn->meth == HTTP_METH_GET ||
1452 txn->meth == HTTP_METH_HEAD ||
1453 txn->meth == HTTP_METH_OPTIONS ||
1454 txn->meth == HTTP_METH_TRACE))
1455 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1456
Willy Tarreaud787e662009-07-07 10:14:51 +02001457 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001458 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001459 req->analyse_exp = TICK_ETERNITY;
1460 return 1;
1461
1462 return_bad_req:
1463 /* We centralize bad requests processing here */
1464 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1465 /* we detected a parsing error. We want to archive this request
1466 * in the dedicated proxy area for later troubleshooting.
1467 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001468 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001469 }
1470
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001471 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001472 txn->req.msg_state = HTTP_MSG_ERROR;
1473 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001474 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001475
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001476 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001477 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001478 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001479
1480 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001481 if (!(s->flags & SF_ERR_MASK))
1482 s->flags |= SF_ERR_PRXCOND;
1483 if (!(s->flags & SF_FINST_MASK))
1484 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001485
Christopher Faulet0184ea72017-01-05 14:06:34 +01001486 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001487 req->analyse_exp = TICK_ETERNITY;
1488 return 0;
1489}
1490
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001491
Willy Tarreau347a35d2013-11-22 17:51:09 +01001492/* This function prepares an applet to handle the stats. It can deal with the
1493 * "100-continue" expectation, check that admin rules are met for POST requests,
1494 * and program a response message if something was unexpected. It cannot fail
1495 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001496 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001497 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001498 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001499 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001500int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001501{
1502 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001503 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001504 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001505 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001506 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001507 struct uri_auth *uri_auth = s->be->uri_auth;
1508 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001509 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001510
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001511 appctx = si_appctx(si);
1512 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1513 appctx->st1 = appctx->st2 = 0;
1514 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1515 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001516 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001517 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001518
Willy Tarreauf37954d2018-06-15 18:31:02 +02001519 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001520 lookup = uri + uri_auth->uri_len;
1521
1522 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1523 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001524 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001525 break;
1526 }
1527 }
1528
1529 if (uri_auth->refresh) {
1530 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1531 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001532 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001533 break;
1534 }
1535 }
1536 }
1537
1538 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1539 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001540 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001541 break;
1542 }
1543 }
1544
Willy Tarreau1e62df92016-01-11 18:57:53 +01001545 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1546 if (memcmp(h, ";typed", 6) == 0) {
1547 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1548 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1549 break;
1550 }
1551 }
1552
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001553 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1554 if (memcmp(h, ";st=", 4) == 0) {
1555 int i;
1556 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001557 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001558 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1559 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001560 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001561 break;
1562 }
1563 }
1564 break;
1565 }
1566 }
1567
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001568 appctx->ctx.stats.scope_str = 0;
1569 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001570 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1571 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1572 int itx = 0;
1573 const char *h2;
1574 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1575 const char *err;
1576
1577 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1578 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001579 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001580 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1581 itx++;
1582 h++;
1583 }
1584
1585 if (itx > STAT_SCOPE_TXT_MAXLEN)
1586 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001587 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001588
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001589 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001590 memcpy(scope_txt, h2, itx);
1591 scope_txt[itx] = '\0';
1592 err = invalid_char(scope_txt);
1593 if (err) {
1594 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001595 appctx->ctx.stats.scope_str = 0;
1596 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001597 }
1598 break;
1599 }
1600 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001601
1602 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001603 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001604 int ret = 1;
1605
1606 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001607 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 +01001608 ret = acl_pass(ret);
1609 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1610 ret = !ret;
1611 }
1612
1613 if (ret) {
1614 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001615 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001616 break;
1617 }
1618 }
1619
1620 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001621 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001622 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001623 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001624 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1625 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001626 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001627 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001628 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001629 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1630 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001631 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001632 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001633 else {
1634 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001635 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001636 }
1637
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001638 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001639 return 1;
1640}
1641
Lukas Tribus67db8df2013-06-23 17:37:13 +02001642/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1643 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1644 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001645void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001646{
1647#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001648 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001649 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1650#endif
1651#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001652 if (from->ss_family == AF_INET6) {
1653 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001654 /* v4-mapped addresses need IP_TOS */
1655 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1656 else
1657 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1658 }
1659#endif
1660}
1661
Willy Tarreau87b09662015-04-03 00:22:06 +02001662int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001663 const char* name, unsigned int name_len,
1664 const char *str, struct my_regex *re,
1665 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001666{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001667 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001668 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001669 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001670 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1671 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001672 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001673
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001674 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001675
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001676 /* Choose the header browsing function. */
1677 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001678 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001679 http_find_hdr_func = http_find_header2;
1680 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001681 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001682 http_find_hdr_func = http_find_full_header2;
1683 break;
1684 default: /* impossible */
1685 return -1;
1686 }
1687
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001688 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1689 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001690 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001691 char *val = ctx.line + ctx.val;
1692 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001693
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001694 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1695 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001696
Willy Tarreau6e27be12018-08-22 04:46:47 +02001697 len = exp_replace(output->area, output->size, val, str, pmatch);
1698 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001699 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001700
Willy Tarreau6e27be12018-08-22 04:46:47 +02001701 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001702
1703 hdr->len += delta;
1704 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001705
1706 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001707 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001708 }
1709
1710 return 0;
1711}
1712
Willy Tarreau87b09662015-04-03 00:22:06 +02001713static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001714 const char* name, unsigned int name_len,
1715 struct list *fmt, struct my_regex *re,
1716 int action)
1717{
Willy Tarreau83061a82018-07-13 11:56:34 +02001718 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001719 int ret = -1;
1720
1721 replace = alloc_trash_chunk();
1722 if (!replace)
1723 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001724
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001725 replace->data = build_logline(s, replace->area, replace->size, fmt);
1726 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001727 goto leave;
1728
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001729 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1730 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001731
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001732 leave:
1733 free_trash_chunk(replace);
1734 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001735}
1736
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001737/*
1738 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1739 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001740 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1741 * line is inserted before the header. If an error occurred <early_hints> is
1742 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001743 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001744static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1745 const char* name, unsigned int name_len,
1746 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001747{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001748 if (!early_hints) {
1749 early_hints = alloc_trash_chunk();
1750 if (!early_hints)
1751 goto fail;
1752 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1753 goto fail;
1754 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001755
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001756 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1757 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001758
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001759 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1760 if (!chunk_memcat(early_hints, "\r\n", 2))
1761 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001762
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001763 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001764
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001765 fail:
1766 free_trash_chunk(early_hints);
1767 return NULL;
1768}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001769
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001770/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1771 * response is added. If an error occurred or if another response was already
1772 * sent, this function does nothing.
1773 */
1774static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1775{
1776 struct channel *chn = s->txn->rsp.chn;
1777 char *cur_ptr = ci_head(chn);
1778 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001779
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001780 /* If a response was already sent, skip early hints */
1781 if (s->txn->status > 0)
1782 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001783
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001784 if (!chunk_memcat(early_hints, "\r\n", 2))
1785 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001786
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001787 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1788 c_adv(chn, ret);
1789 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001790}
1791
Willy Tarreau87b09662015-04-03 00:22:06 +02001792/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001793 * transaction <txn>. Returns the verdict of the first rule that prevents
1794 * further processing of the request (auth, deny, ...), and defaults to
1795 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1796 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001797 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1798 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1799 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001800 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001801enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001802http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001803{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001804 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001805 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001806 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001807 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001808 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001809 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001810 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001811 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001812 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001813 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001814
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001815 /* If "the current_rule_list" match the executed rule list, we are in
1816 * resume condition. If a resume is needed it is always in the action
1817 * and never in the ACL or converters. In this case, we initialise the
1818 * current rule, and go to the action execution point.
1819 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001820 if (s->current_rule) {
1821 rule = s->current_rule;
1822 s->current_rule = NULL;
1823 if (s->current_rule_list == rules)
1824 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001825 }
1826 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001827
Willy Tarreauff011f22011-01-06 17:51:27 +01001828 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001829
Willy Tarreau96257ec2012-12-27 10:46:37 +01001830 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001831 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001832 int ret;
1833
Willy Tarreau192252e2015-04-04 01:47:55 +02001834 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001835 ret = acl_pass(ret);
1836
Willy Tarreauff011f22011-01-06 17:51:27 +01001837 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001838 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001839
1840 if (!ret) /* condition not matched */
1841 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001842 }
1843
Willy Tarreauacc98002015-09-27 23:34:39 +02001844 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001845resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001846 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001847 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001848 rule_ret = HTTP_RULE_RES_STOP;
1849 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001850
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001851 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001852 if (deny_status)
1853 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001854 rule_ret = HTTP_RULE_RES_DENY;
1855 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001856
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001857 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001858 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001859 if (deny_status)
1860 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001861 rule_ret = HTTP_RULE_RES_DENY;
1862 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001863
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001864 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001865 /* Be sure to send any pending HTTP 103 response first */
1866 if (early_hints) {
1867 http_send_early_hints(s, early_hints);
1868 free_trash_chunk(early_hints);
1869 early_hints = NULL;
1870 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001871 /* Auth might be performed on regular http-req rules as well as on stats */
1872 auth_realm = rule->arg.auth.realm;
1873 if (!auth_realm) {
1874 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1875 auth_realm = STATS_DEFAULT_REALM;
1876 else
1877 auth_realm = px->id;
1878 }
1879 /* send 401/407 depending on whether we use a proxy or not. We still
1880 * count one error, because normal browsing won't significantly
1881 * increase the counter but brute force attempts will.
1882 */
1883 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1884 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001885 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001886 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001887 rule_ret = HTTP_RULE_RES_ABRT;
1888 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001889
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001890 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001891 /* Be sure to send any pending HTTP 103 response first */
1892 if (early_hints) {
1893 http_send_early_hints(s, early_hints);
1894 free_trash_chunk(early_hints);
1895 early_hints = NULL;
1896 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001897 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001898 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001899 rule_ret = HTTP_RULE_RES_BADREQ;
1900 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001901
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001902 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001903 s->task->nice = rule->arg.nice;
1904 break;
1905
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001906 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001907 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001908 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001909 break;
1910
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001911 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001912#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001913 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001914 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001915#endif
1916 break;
1917
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001918 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001919 s->logs.level = rule->arg.loglevel;
1920 break;
1921
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001922 case ACT_HTTP_REPLACE_HDR:
1923 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001924 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1925 rule->arg.hdr_add.name_len,
1926 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001927 &rule->arg.hdr_add.re, rule->action)) {
1928 rule_ret = HTTP_RULE_RES_BADREQ;
1929 goto end;
1930 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001931 break;
1932
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001933 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001934 ctx.idx = 0;
1935 /* remove all occurrences of the header */
1936 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001937 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001938 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001939 }
Willy Tarreau85603282015-01-21 20:39:27 +01001940 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001941
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001942 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001943 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001944 /* The scope of the trash buffer must be limited to this function. The
1945 * build_logline() function can execute a lot of other function which
1946 * can use the trash buffer. So for limiting the scope of this global
1947 * buffer, we build first the header value using build_logline, and
1948 * after we store the header name.
1949 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001950 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001951
1952 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001953 if (!replace) {
1954 rule_ret = HTTP_RULE_RES_BADREQ;
1955 goto end;
1956 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001957
Thierry Fournier4b788f72016-06-01 13:35:36 +02001958 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001959 len += build_logline(s, replace->area + len,
1960 replace->size - len,
1961 &rule->arg.hdr_add.fmt);
1962 memcpy(replace->area, rule->arg.hdr_add.name,
1963 rule->arg.hdr_add.name_len);
1964 replace->area[rule->arg.hdr_add.name_len] = ':';
1965 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1966 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001967
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001968 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001969 /* remove all occurrences of the header */
1970 ctx.idx = 0;
1971 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001972 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001973 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1974 }
1975 }
1976
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001977 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001978 static unsigned char rate_limit = 0;
1979
1980 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001981 replace->area[rule->arg.hdr_add.name_len] = 0;
1982 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,
1983 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001984 }
1985
1986 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1987 if (sess->fe != s->be)
1988 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1989 if (sess->listener->counters)
1990 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1991 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001992
1993 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001994 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001995 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001996
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001997 case ACT_HTTP_DEL_ACL:
1998 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001999 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002000 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002001
2002 /* collect reference */
2003 ref = pat_ref_lookup(rule->arg.map.ref);
2004 if (!ref)
2005 continue;
2006
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002007 /* allocate key */
2008 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002009 if (!key) {
2010 rule_ret = HTTP_RULE_RES_BADREQ;
2011 goto end;
2012 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002013
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002014 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002015 key->data = build_logline(s, key->area, key->size,
2016 &rule->arg.map.key);
2017 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002018
2019 /* perform update */
2020 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002021 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002022 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002023 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002024
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002025 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002026 break;
2027 }
2028
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002029 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002030 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002031 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002032
2033 /* collect reference */
2034 ref = pat_ref_lookup(rule->arg.map.ref);
2035 if (!ref)
2036 continue;
2037
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002038 /* allocate key */
2039 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002040 if (!key) {
2041 rule_ret = HTTP_RULE_RES_BADREQ;
2042 goto end;
2043 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002044
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002045 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002046 key->data = build_logline(s, key->area, key->size,
2047 &rule->arg.map.key);
2048 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002049
2050 /* perform update */
2051 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002052 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002053 if (pat_ref_find_elt(ref, key->area) == NULL)
2054 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002055 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002056
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002057 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002058 break;
2059 }
2060
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002061 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002062 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002063 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002064
2065 /* collect reference */
2066 ref = pat_ref_lookup(rule->arg.map.ref);
2067 if (!ref)
2068 continue;
2069
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002070 /* allocate key */
2071 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002072 if (!key) {
2073 rule_ret = HTTP_RULE_RES_BADREQ;
2074 goto end;
2075 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002076
2077 /* allocate value */
2078 value = alloc_trash_chunk();
2079 if (!value) {
2080 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01002081 rule_ret = HTTP_RULE_RES_BADREQ;
2082 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002083 }
2084
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002085 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002086 key->data = build_logline(s, key->area, key->size,
2087 &rule->arg.map.key);
2088 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002089
2090 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002091 value->data = build_logline(s, value->area,
2092 value->size,
2093 &rule->arg.map.value);
2094 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002095
2096 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002097 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002098 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002099 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002100 else
2101 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002102 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002103
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002104 free_trash_chunk(key);
2105 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002106 break;
2107 }
William Lallemand73025dd2014-04-24 14:38:37 +02002108
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002109 case ACT_HTTP_EARLY_HINT:
2110 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2111 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002112 early_hints = http_apply_early_hint_rule(s, early_hints,
2113 rule->arg.early_hint.name,
2114 rule->arg.early_hint.name_len,
2115 &rule->arg.early_hint.fmt);
2116 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01002117 rule_ret = HTTP_RULE_RES_DONE;
2118 goto end;
2119 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002120 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002121 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002122 if ((s->req.flags & CF_READ_ERROR) ||
2123 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2124 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2125 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002126 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002127
Willy Tarreauacc98002015-09-27 23:34:39 +02002128 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002129 case ACT_RET_ERR:
2130 case ACT_RET_CONT:
2131 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002132 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01002133 rule_ret = HTTP_RULE_RES_DONE;
2134 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002135 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002136 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01002137 rule_ret = HTTP_RULE_RES_YIELD;
2138 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002139 }
William Lallemand73025dd2014-04-24 14:38:37 +02002140 break;
2141
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002142 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002143 /* Note: only the first valid tracking parameter of each
2144 * applies.
2145 */
2146
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002147 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002148 struct stktable *t;
2149 struct stksess *ts;
2150 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002151 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002152
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002153 t = rule->arg.trk_ctr.table.t;
2154 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 +02002155
2156 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002157 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002158
2159 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002160 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2161 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2162 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002163 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002164
Emeric Brun819fc6f2017-06-13 19:37:32 +02002165 if (ptr1)
2166 stktable_data_cast(ptr1, http_req_cnt)++;
2167
2168 if (ptr2)
2169 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2170 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2171
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002172 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002173
2174 /* If data was modified, we need to touch to re-schedule sync */
2175 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002176 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002177
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002178 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002179 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002180 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002181 }
2182 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002183 break;
2184
Joseph Herlant5ba80252018-11-15 09:25:36 -08002185 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002186 default:
2187 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002188 }
2189 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002190
Christopher Fauletea827bd2018-11-15 15:34:11 +01002191 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002192 if (early_hints) {
2193 http_send_early_hints(s, early_hints);
2194 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002195 }
2196
Willy Tarreau96257ec2012-12-27 10:46:37 +01002197 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01002198 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002199}
2200
Willy Tarreau71241ab2012-12-27 11:30:54 +01002201
Willy Tarreau51d861a2015-05-22 17:30:48 +02002202/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2203 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2204 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2205 * is returned, the process can continue the evaluation of next rule list. If
2206 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2207 * is returned, it means the operation could not be processed and a server error
2208 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2209 * deny rule. If *YIELD is returned, the caller must call again the function
2210 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002211 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002212enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002213http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002214{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002215 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002216 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002217 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002218 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002219 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002220 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02002221 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002222
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002223 /* If "the current_rule_list" match the executed rule list, we are in
2224 * resume condition. If a resume is needed it is always in the action
2225 * and never in the ACL or converters. In this case, we initialise the
2226 * current rule, and go to the action execution point.
2227 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002228 if (s->current_rule) {
2229 rule = s->current_rule;
2230 s->current_rule = NULL;
2231 if (s->current_rule_list == rules)
2232 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002233 }
2234 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002235
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002236 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002237
2238 /* check optional condition */
2239 if (rule->cond) {
2240 int ret;
2241
Willy Tarreau192252e2015-04-04 01:47:55 +02002242 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002243 ret = acl_pass(ret);
2244
2245 if (rule->cond->pol == ACL_COND_UNLESS)
2246 ret = !ret;
2247
2248 if (!ret) /* condition not matched */
2249 continue;
2250 }
2251
Willy Tarreauacc98002015-09-27 23:34:39 +02002252 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002253resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002254 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002255 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002256 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2257 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002258
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002259 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002260 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002261 rule_ret = HTTP_RULE_RES_STOP;
2262 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002263
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002264 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002265 s->task->nice = rule->arg.nice;
2266 break;
2267
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002268 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002269 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002270 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002271 break;
2272
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002273 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002274#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002275 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002276 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002277#endif
2278 break;
2279
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002280 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002281 s->logs.level = rule->arg.loglevel;
2282 break;
2283
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002284 case ACT_HTTP_REPLACE_HDR:
2285 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002286 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2287 rule->arg.hdr_add.name_len,
2288 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002289 &rule->arg.hdr_add.re, rule->action)) {
2290 rule_ret = HTTP_RULE_RES_BADREQ;
2291 goto end;
2292 }
Sasha Pachev218f0642014-06-16 12:05:59 -06002293 break;
2294
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002295 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002296 ctx.idx = 0;
2297 /* remove all occurrences of the header */
2298 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002299 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002300 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2301 }
Willy Tarreau85603282015-01-21 20:39:27 +01002302 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002303
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002304 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002305 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002306 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002307
2308 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002309 if (!replace) {
2310 rule_ret = HTTP_RULE_RES_BADREQ;
2311 goto end;
2312 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002313
2314 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002315 memcpy(replace->area, rule->arg.hdr_add.name,
2316 rule->arg.hdr_add.name_len);
2317 replace->data = rule->arg.hdr_add.name_len;
2318 replace->area[replace->data++] = ':';
2319 replace->area[replace->data++] = ' ';
2320 replace->data += build_logline(s,
2321 replace->area + replace->data,
2322 replace->size - replace->data,
2323 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002324
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002325 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002326 /* remove all occurrences of the header */
2327 ctx.idx = 0;
2328 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002329 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002330 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2331 }
2332 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002333
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002334 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002335 static unsigned char rate_limit = 0;
2336
2337 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002338 replace->area[rule->arg.hdr_add.name_len] = 0;
2339 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,
2340 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002341 }
2342
2343 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2344 if (sess->fe != s->be)
2345 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2346 if (sess->listener->counters)
2347 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2348 if (objt_server(s->target))
2349 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2350 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002351
2352 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002353 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002354 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002355
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002356 case ACT_HTTP_DEL_ACL:
2357 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002358 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002359 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002360
2361 /* collect reference */
2362 ref = pat_ref_lookup(rule->arg.map.ref);
2363 if (!ref)
2364 continue;
2365
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002366 /* allocate key */
2367 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002368 if (!key) {
2369 rule_ret = HTTP_RULE_RES_BADREQ;
2370 goto end;
2371 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002372
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002373 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002374 key->data = build_logline(s, key->area, key->size,
2375 &rule->arg.map.key);
2376 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002377
2378 /* perform update */
2379 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002380 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002381 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002382 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002383
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002384 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002385 break;
2386 }
2387
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002388 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002389 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002390 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002391
2392 /* collect reference */
2393 ref = pat_ref_lookup(rule->arg.map.ref);
2394 if (!ref)
2395 continue;
2396
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002397 /* allocate key */
2398 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002399 if (!key) {
2400 rule_ret = HTTP_RULE_RES_BADREQ;
2401 goto end;
2402 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002403
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002404 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002405 key->data = build_logline(s, key->area, key->size,
2406 &rule->arg.map.key);
2407 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002408
2409 /* perform update */
2410 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002411 if (pat_ref_find_elt(ref, key->area) == NULL)
2412 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002413
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002414 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002415 break;
2416 }
2417
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002418 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002419 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002420 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002421
2422 /* collect reference */
2423 ref = pat_ref_lookup(rule->arg.map.ref);
2424 if (!ref)
2425 continue;
2426
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002427 /* allocate key */
2428 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002429 if (!key) {
2430 rule_ret = HTTP_RULE_RES_BADREQ;
2431 goto end;
2432 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002433
2434 /* allocate value */
2435 value = alloc_trash_chunk();
2436 if (!value) {
2437 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002438 rule_ret = HTTP_RULE_RES_BADREQ;
2439 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002440 }
2441
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002442 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002443 key->data = build_logline(s, key->area, key->size,
2444 &rule->arg.map.key);
2445 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002446
2447 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002448 value->data = build_logline(s, value->area,
2449 value->size,
2450 &rule->arg.map.value);
2451 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002452
2453 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002454 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002455 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002456 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002457 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002458 else
2459 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002460 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002461 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002462 free_trash_chunk(key);
2463 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002464 break;
2465 }
William Lallemand73025dd2014-04-24 14:38:37 +02002466
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002467 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002468 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002469 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002470 rule_ret = HTTP_RULE_RES_BADREQ;
2471 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002472
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002473 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2474 /* Note: only the first valid tracking parameter of each
2475 * applies.
2476 */
2477
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002478 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002479 struct stktable *t;
2480 struct stksess *ts;
2481 struct stktable_key *key;
2482 void *ptr;
2483
2484 t = rule->arg.trk_ctr.table.t;
2485 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2486
2487 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002488 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002489
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002490 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002491
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002492 /* let's count a new HTTP request as it's the first time we do it */
2493 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2494 if (ptr)
2495 stktable_data_cast(ptr, http_req_cnt)++;
2496
2497 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2498 if (ptr)
2499 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2500 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2501
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002502 /* When the client triggers a 4xx from the server, it's most often due
2503 * to a missing object or permission. These events should be tracked
2504 * because if they happen often, it may indicate a brute force or a
2505 * vulnerability scan. Normally this is done when receiving the response
2506 * but here we're tracking after this ought to have been done so we have
2507 * to do it on purpose.
2508 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002509 if ((unsigned)(txn->status - 400) < 100) {
2510 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2511 if (ptr)
2512 stktable_data_cast(ptr, http_err_cnt)++;
2513
2514 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2515 if (ptr)
2516 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2517 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2518 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002519
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002520 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002521
Emeric Brun0fed0b02017-11-29 16:15:07 +01002522 /* If data was modified, we need to touch to re-schedule sync */
2523 stktable_touch_local(t, ts, 0);
2524
Emeric Brun819fc6f2017-06-13 19:37:32 +02002525 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2526 if (sess->fe != s->be)
2527 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2528
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002529 }
2530 }
2531 break;
2532
Thierry FOURNIER42148732015-09-02 17:17:33 +02002533 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002534 if ((s->req.flags & CF_READ_ERROR) ||
2535 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2536 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2537 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002538 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002539
Willy Tarreauacc98002015-09-27 23:34:39 +02002540 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002541 case ACT_RET_ERR:
2542 case ACT_RET_CONT:
2543 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002544 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002545 rule_ret = HTTP_RULE_RES_STOP;
2546 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002547 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002548 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002549 rule_ret = HTTP_RULE_RES_YIELD;
2550 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002551 }
William Lallemand73025dd2014-04-24 14:38:37 +02002552 break;
2553
Joseph Herlant5ba80252018-11-15 09:25:36 -08002554 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002555 default:
2556 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002557 }
2558 }
2559
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002560 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002561 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002562 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002563}
2564
2565
Willy Tarreau71241ab2012-12-27 11:30:54 +01002566/* Perform an HTTP redirect based on the information in <rule>. The function
2567 * returns non-zero on success, or zero in case of a, irrecoverable error such
2568 * as too large a request to build a valid response.
2569 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002570int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002571{
Willy Tarreaub329a312015-05-22 16:27:37 +02002572 struct http_msg *req = &txn->req;
2573 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002574 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002575 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002576 int ret = 0;
2577
Christopher Fauletf2824e62018-10-01 12:12:37 +02002578 if (IS_HTX_STRM(s))
2579 return htx_apply_redirect_rule(rule, s, txn);
2580
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002581 chunk = alloc_trash_chunk();
2582 if (!chunk)
2583 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002584
2585 /* build redirect message */
2586 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002587 case 308:
2588 msg_fmt = HTTP_308;
2589 break;
2590 case 307:
2591 msg_fmt = HTTP_307;
2592 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002593 case 303:
2594 msg_fmt = HTTP_303;
2595 break;
2596 case 301:
2597 msg_fmt = HTTP_301;
2598 break;
2599 case 302:
2600 default:
2601 msg_fmt = HTTP_302;
2602 break;
2603 }
2604
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002605 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2606 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002607
2608 switch(rule->type) {
2609 case REDIRECT_TYPE_SCHEME: {
2610 const char *path;
2611 const char *host;
2612 struct hdr_ctx ctx;
2613 int pathlen;
2614 int hostlen;
2615
2616 host = "";
2617 hostlen = 0;
2618 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002619 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002620 host = ctx.line + ctx.val;
2621 hostlen = ctx.vlen;
2622 }
2623
Willy Tarreau6b952c82018-09-10 17:45:34 +02002624 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002625 /* build message using path */
2626 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002627 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002628 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2629 int qs = 0;
2630 while (qs < pathlen) {
2631 if (path[qs] == '?') {
2632 pathlen = qs;
2633 break;
2634 }
2635 qs++;
2636 }
2637 }
2638 } else {
2639 path = "/";
2640 pathlen = 1;
2641 }
2642
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002643 if (rule->rdr_str) { /* this is an old "redirect" rule */
2644 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002645 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002646 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002647
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002648 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002649 memcpy(chunk->area + chunk->data, rule->rdr_str,
2650 rule->rdr_len);
2651 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002652 }
2653 else {
2654 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002655 chunk->data += build_logline(s,
2656 chunk->area + chunk->data,
2657 chunk->size - chunk->data,
2658 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002659
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002660 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002661 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002662 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002663 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002664 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002665 memcpy(chunk->area + chunk->data, "://", 3);
2666 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002667
2668 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002669 memcpy(chunk->area + chunk->data, host, hostlen);
2670 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002671
2672 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002673 memcpy(chunk->area + chunk->data, path, pathlen);
2674 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002675
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002676 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002677 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002678 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002679 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002680 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002681 chunk->area[chunk->data] = '/';
2682 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002683 }
2684
2685 break;
2686 }
2687 case REDIRECT_TYPE_PREFIX: {
2688 const char *path;
2689 int pathlen;
2690
Willy Tarreau6b952c82018-09-10 17:45:34 +02002691 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002692 /* build message using path */
2693 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002694 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002695 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2696 int qs = 0;
2697 while (qs < pathlen) {
2698 if (path[qs] == '?') {
2699 pathlen = qs;
2700 break;
2701 }
2702 qs++;
2703 }
2704 }
2705 } else {
2706 path = "/";
2707 pathlen = 1;
2708 }
2709
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002710 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002711 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002712 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002713
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002714 /* add prefix. Note that if prefix == "/", we don't want to
2715 * add anything, otherwise it makes it hard for the user to
2716 * configure a self-redirection.
2717 */
2718 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002719 memcpy(chunk->area + chunk->data,
2720 rule->rdr_str, rule->rdr_len);
2721 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002722 }
2723 }
2724 else {
2725 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002726 chunk->data += build_logline(s,
2727 chunk->area + chunk->data,
2728 chunk->size - chunk->data,
2729 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002730
2731 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002732 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002733 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002734 }
2735
2736 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002737 memcpy(chunk->area + chunk->data, path, pathlen);
2738 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002739
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002740 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002741 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002742 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002743 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002744 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002745 chunk->area[chunk->data] = '/';
2746 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002747 }
2748
2749 break;
2750 }
2751 case REDIRECT_TYPE_LOCATION:
2752 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002753 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002754 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002755 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002756
2757 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002758 memcpy(chunk->area + chunk->data, rule->rdr_str,
2759 rule->rdr_len);
2760 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002761 }
2762 else {
2763 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002764 chunk->data += build_logline(s,
2765 chunk->area + chunk->data,
2766 chunk->size - chunk->data,
2767 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002768
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002769 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002770 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002771 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002772 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002773 break;
2774 }
2775
2776 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002777 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2778 chunk->data += 14;
2779 memcpy(chunk->area + chunk->data, rule->cookie_str,
2780 rule->cookie_len);
2781 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002782 }
2783
Willy Tarreau19b14122017-02-28 09:48:11 +01002784 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002785 txn->status = rule->code;
2786 /* let's log the request time */
2787 s->logs.tv_request = now;
2788
Christopher Fauletbe821b92017-03-30 11:21:53 +02002789 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002790 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2791 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2792 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002793 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002794 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002795 memcpy(chunk->area + chunk->data,
2796 "\r\nProxy-Connection: keep-alive", 30);
2797 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002798 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002799 memcpy(chunk->area + chunk->data,
2800 "\r\nConnection: keep-alive", 24);
2801 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002802 }
2803 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002804 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2805 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002806 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002807 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002808 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002809 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002810 req->next -= req->sov;
2811 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002812 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002813 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002814 req->msg_state = HTTP_MSG_CLOSED;
2815 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002816 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002817 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002818 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002819 /* let the server side turn to SI_ST_CLO */
2820 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002821 } else {
2822 /* keep-alive not possible */
2823 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002824 memcpy(chunk->area + chunk->data,
2825 "\r\nProxy-Connection: close\r\n\r\n", 29);
2826 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002827 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002828 memcpy(chunk->area + chunk->data,
2829 "\r\nConnection: close\r\n\r\n", 23);
2830 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002831 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002832 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002833 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002834 }
2835
Willy Tarreaue7dff022015-04-03 01:14:29 +02002836 if (!(s->flags & SF_ERR_MASK))
2837 s->flags |= SF_ERR_LOCAL;
2838 if (!(s->flags & SF_FINST_MASK))
2839 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002840
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002841 ret = 1;
2842 leave:
2843 free_trash_chunk(chunk);
2844 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002845}
2846
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002847/* This stream analyser runs all HTTP request processing which is common to
2848 * frontends and backends, which means blocking ACLs, filters, connection-close,
2849 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002850 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002851 * either needs more data or wants to immediately abort the request (eg: deny,
2852 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002853 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002854int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002855{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002856 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002857 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002858 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002859 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002860 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002861 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002862 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002863 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002864
Christopher Faulete0768eb2018-10-03 16:38:02 +02002865 if (IS_HTX_STRM(s))
2866 return htx_process_req_common(s, req, an_bit, px);
2867
Willy Tarreau655dce92009-11-08 13:10:58 +01002868 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002869 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002870 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002871 }
2872
Christopher Faulet45073512018-07-20 10:16:29 +02002873 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 +02002874 now_ms, __FUNCTION__,
2875 s,
2876 req,
2877 req->rex, req->wex,
2878 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002879 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002880 req->analysers);
2881
Willy Tarreau65410832014-04-28 21:25:43 +02002882 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002883 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002884
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002885 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002886 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002887 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002888
Willy Tarreau0b748332014-04-29 00:13:29 +02002889 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002890 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2891 goto return_prx_yield;
2892
Willy Tarreau0b748332014-04-29 00:13:29 +02002893 case HTTP_RULE_RES_CONT:
2894 case HTTP_RULE_RES_STOP: /* nothing to do */
2895 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002896
Willy Tarreau0b748332014-04-29 00:13:29 +02002897 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2898 if (txn->flags & TX_CLTARPIT)
2899 goto tarpit;
2900 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002901
Willy Tarreau0b748332014-04-29 00:13:29 +02002902 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2903 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002904
Willy Tarreau0b748332014-04-29 00:13:29 +02002905 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002906 goto done;
2907
Willy Tarreau0b748332014-04-29 00:13:29 +02002908 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2909 goto return_bad_req;
2910 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002911 }
2912
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002913 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2914 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002915 struct hdr_ctx ctx;
2916
2917 ctx.idx = 0;
2918 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002919 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002920 if (unlikely(http_header_add_tail2(&txn->req,
2921 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002922 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002923 goto return_bad_req;
2924 }
2925 }
2926
2927 }
2928
Willy Tarreau52542592014-04-28 18:33:26 +02002929 /* OK at this stage, we know that the request was accepted according to
2930 * the http-request rules, we can check for the stats. Note that the
2931 * URI is detected *before* the req* rules in order not to be affected
2932 * by a possible reqrep, while they are processed *after* so that a
2933 * reqdeny can still block them. This clearly needs to change in 1.6!
2934 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002935 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002936 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002937 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002938 txn->status = 500;
2939 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002940 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002941
Willy Tarreaue7dff022015-04-03 01:14:29 +02002942 if (!(s->flags & SF_ERR_MASK))
2943 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002944 goto return_prx_cond;
2945 }
2946
2947 /* parse the whole stats request and extract the relevant information */
2948 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002949 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002950 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002951
Willy Tarreau0b748332014-04-29 00:13:29 +02002952 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2953 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002954
Willy Tarreau0b748332014-04-29 00:13:29 +02002955 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2956 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002957 }
2958
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002959 /* evaluate the req* rules except reqadd */
2960 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002961 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002962 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002963
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002964 if (txn->flags & TX_CLDENY)
2965 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002966
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002967 if (txn->flags & TX_CLTARPIT) {
2968 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002969 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002970 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002971 }
Willy Tarreau06619262006-12-17 08:37:22 +01002972
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002973 /* add request headers from the rule sets in the same order */
2974 list_for_each_entry(wl, &px->req_add, list) {
2975 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002976 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002977 ret = acl_pass(ret);
2978 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2979 ret = !ret;
2980 if (!ret)
2981 continue;
2982 }
2983
Christopher Faulet10079f52018-10-03 15:17:28 +02002984 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002985 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002986 }
2987
Willy Tarreau52542592014-04-28 18:33:26 +02002988
2989 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002990 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2991 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002992 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002993 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002994 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002995
Willy Tarreaue7dff022015-04-03 01:14:29 +02002996 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2997 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2998 if (!(s->flags & SF_FINST_MASK))
2999 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003000
Willy Tarreau70730dd2014-04-24 18:06:27 +02003001 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003002 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3003 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003004 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003005 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003006 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003007
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003008 /* check whether we have some ACLs set to redirect this request */
3009 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003010 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003011 int ret;
3012
Willy Tarreau192252e2015-04-04 01:47:55 +02003013 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003014 ret = acl_pass(ret);
3015 if (rule->cond->pol == ACL_COND_UNLESS)
3016 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003017 if (!ret)
3018 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003019 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003020 if (!http_apply_redirect_rule(rule, s, txn))
3021 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003022 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003023 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003024
Willy Tarreau2be39392010-01-03 17:24:51 +01003025 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3026 * If this happens, then the data will not come immediately, so we must
3027 * send all what we have without waiting. Note that due to the small gain
3028 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003029 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003030 * itself once used.
3031 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003032 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003033
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003034 done: /* done with this analyser, continue with next ones that the calling
3035 * points will have set, if any.
3036 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003037 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003038 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3039 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003040 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003041
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003042 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003043 /* Allow cookie logging
3044 */
3045 if (s->be->cookie_name || sess->fe->capture_name)
3046 manage_client_side_cookies(s, req);
3047
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003048 /* When a connection is tarpitted, we use the tarpit timeout,
3049 * which may be the same as the connect timeout if unspecified.
3050 * If unset, then set it to zero because we really want it to
3051 * eventually expire. We build the tarpit as an analyser.
3052 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003053 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003054
3055 /* wipe the request out so that we can drop the connection early
3056 * if the client closes first.
3057 */
3058 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003059
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003060 txn->status = http_err_codes[deny_status];
3061
Christopher Faulet0184ea72017-01-05 14:06:34 +01003062 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003063 req->analysers |= AN_REQ_HTTP_TARPIT;
3064 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3065 if (!req->analyse_exp)
3066 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003067 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003068 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003069 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003070 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003071 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003072 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003073 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003074
3075 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003076
3077 /* Allow cookie logging
3078 */
3079 if (s->be->cookie_name || sess->fe->capture_name)
3080 manage_client_side_cookies(s, req);
3081
Willy Tarreau0b748332014-04-29 00:13:29 +02003082 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003083 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003084 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003085 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003086 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003087 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003088 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003089 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003090 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003091 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003092 goto return_prx_cond;
3093
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003094 return_bad_req:
3095 /* We centralize bad requests processing here */
3096 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3097 /* we detected a parsing error. We want to archive this request
3098 * in the dedicated proxy area for later troubleshooting.
3099 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003100 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003101 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003102
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003103 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003104 txn->req.msg_state = HTTP_MSG_ERROR;
3105 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003106 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003107
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003108 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003109 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003110 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003111
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003112 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003113 if (!(s->flags & SF_ERR_MASK))
3114 s->flags |= SF_ERR_PRXCOND;
3115 if (!(s->flags & SF_FINST_MASK))
3116 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003117
Christopher Faulet0184ea72017-01-05 14:06:34 +01003118 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003119 req->analyse_exp = TICK_ETERNITY;
3120 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003121
3122 return_prx_yield:
3123 channel_dont_connect(req);
3124 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003125}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003126
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003127/* This function performs all the processing enabled for the current request.
3128 * It returns 1 if the processing can continue on next analysers, or zero if it
3129 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003130 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003131 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003132int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003133{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003134 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003135 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003136 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003137 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003138
Christopher Faulete0768eb2018-10-03 16:38:02 +02003139 if (IS_HTX_STRM(s))
3140 return htx_process_request(s, req, an_bit);
3141
Willy Tarreau655dce92009-11-08 13:10:58 +01003142 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003143 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003144 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003145 return 0;
3146 }
3147
Christopher Faulet45073512018-07-20 10:16:29 +02003148 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 +02003149 now_ms, __FUNCTION__,
3150 s,
3151 req,
3152 req->rex, req->wex,
3153 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003154 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003155 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003156
Willy Tarreau59234e92008-11-30 23:51:27 +01003157 /*
3158 * Right now, we know that we have processed the entire headers
3159 * and that unwanted requests have been filtered out. We can do
3160 * whatever we want with the remaining request. Also, now we
3161 * may have separate values for ->fe, ->be.
3162 */
Willy Tarreau06619262006-12-17 08:37:22 +01003163
Willy Tarreau59234e92008-11-30 23:51:27 +01003164 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003165 * If HTTP PROXY is set we simply get remote server address parsing
3166 * incoming request. Note that this requires that a connection is
3167 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003168 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003169 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003170 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003171 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003172
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003173 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003174 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003175 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003176 txn->req.msg_state = HTTP_MSG_ERROR;
3177 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003178 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003179 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003180
Willy Tarreaue7dff022015-04-03 01:14:29 +02003181 if (!(s->flags & SF_ERR_MASK))
3182 s->flags |= SF_ERR_RESOURCE;
3183 if (!(s->flags & SF_FINST_MASK))
3184 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003185
3186 return 0;
3187 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003188
Willy Tarreau6b952c82018-09-10 17:45:34 +02003189 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003190 if (url2sa(ci_head(req) + msg->sl.rq.u,
3191 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003192 &conn->addr.to, NULL) == -1)
3193 goto return_bad_req;
3194
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003195 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003196 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3197 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003198 * u_l characters by a single "/".
3199 */
3200 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003201 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003202 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3203 int delta;
3204
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003205 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003206 http_msg_move_end(&txn->req, delta);
3207 cur_end += delta;
3208 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3209 goto return_bad_req;
3210 }
3211 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003212 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003213 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3214 int delta;
3215
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003216 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003217 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003218 http_msg_move_end(&txn->req, delta);
3219 cur_end += delta;
3220 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3221 goto return_bad_req;
3222 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003223 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003224
Willy Tarreau59234e92008-11-30 23:51:27 +01003225 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003226 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003227 * Note that doing so might move headers in the request, but
3228 * the fields will stay coherent and the URI will not move.
3229 * This should only be performed in the backend.
3230 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003231 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003232 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003233
William Lallemanda73203e2012-03-12 12:48:57 +01003234 /* add unique-id if "header-unique-id" is specified */
3235
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003236 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003237 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003238 goto return_bad_req;
3239 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003240 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003241 }
William Lallemanda73203e2012-03-12 12:48:57 +01003242
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003243 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003244 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003245 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003246 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003247 goto return_bad_req;
3248 }
3249
Cyril Bontéb21570a2009-11-29 20:04:48 +01003250 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003251 * 9: add X-Forwarded-For if either the frontend or the backend
3252 * asks for it.
3253 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003254 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003255 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003256 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3257 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3258 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003259 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003260 /* The header is set to be added only if none is present
3261 * and we found it, so don't do anything.
3262 */
3263 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003264 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003265 /* Add an X-Forwarded-For header unless the source IP is
3266 * in the 'except' network range.
3267 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003268 if ((!sess->fe->except_mask.s_addr ||
3269 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3270 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003271 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003272 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003273 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003274 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003275 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003276 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003277
3278 /* Note: we rely on the backend to get the header name to be used for
3279 * x-forwarded-for, because the header is really meant for the backends.
3280 * However, if the backend did not specify any option, we have to rely
3281 * on the frontend's header name.
3282 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003283 if (s->be->fwdfor_hdr_len) {
3284 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003285 memcpy(trash.area,
3286 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003287 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003288 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003289 memcpy(trash.area,
3290 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003291 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003292 len += snprintf(trash.area + len,
3293 trash.size - len,
3294 ": %d.%d.%d.%d", pn[0], pn[1],
3295 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003296
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003297 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003298 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003299 }
3300 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003301 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003302 /* FIXME: for the sake of completeness, we should also support
3303 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003304 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003305 int len;
3306 char pn[INET6_ADDRSTRLEN];
3307 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003308 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003309 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003310
Willy Tarreau59234e92008-11-30 23:51:27 +01003311 /* Note: we rely on the backend to get the header name to be used for
3312 * x-forwarded-for, because the header is really meant for the backends.
3313 * However, if the backend did not specify any option, we have to rely
3314 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003315 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003316 if (s->be->fwdfor_hdr_len) {
3317 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003318 memcpy(trash.area, s->be->fwdfor_hdr_name,
3319 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003320 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003321 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003322 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3323 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003324 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003325 len += snprintf(trash.area + len, trash.size - len,
3326 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003327
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003328 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003329 goto return_bad_req;
3330 }
3331 }
3332
3333 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003334 * 10: add X-Original-To if either the frontend or the backend
3335 * asks for it.
3336 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003337 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003338
3339 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003340 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003341 /* Add an X-Original-To header unless the destination IP is
3342 * in the 'except' network range.
3343 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003344 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003345
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003346 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003347 ((!sess->fe->except_mask_to.s_addr ||
3348 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3349 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003350 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003351 (((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 +02003352 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003353 int len;
3354 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003355 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003356
3357 /* Note: we rely on the backend to get the header name to be used for
3358 * x-original-to, because the header is really meant for the backends.
3359 * However, if the backend did not specify any option, we have to rely
3360 * on the frontend's header name.
3361 */
3362 if (s->be->orgto_hdr_len) {
3363 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003364 memcpy(trash.area,
3365 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003366 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003367 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003368 memcpy(trash.area,
3369 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003370 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003371 len += snprintf(trash.area + len,
3372 trash.size - len,
3373 ": %d.%d.%d.%d", pn[0], pn[1],
3374 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003375
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003376 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003377 goto return_bad_req;
3378 }
3379 }
3380 }
3381
Willy Tarreau50fc7772012-11-11 22:19:57 +01003382 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3383 * If an "Upgrade" token is found, the header is left untouched in order not to have
3384 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3385 * "Upgrade" is present in the Connection header.
3386 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003387 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003388 unsigned int want_flags = 0;
3389
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003390 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003391 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003392 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003393 want_flags |= TX_CON_CLO_SET;
3394 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003395 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003396 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003397 want_flags |= TX_CON_KAL_SET;
3398 }
3399
3400 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003401 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003402 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003403
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003404
Willy Tarreau522d6c02009-12-06 18:49:18 +01003405 /* If we have no server assigned yet and we're balancing on url_param
3406 * with a POST request, we may be interested in checking the body for
3407 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003408 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003409 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003410 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003411 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003412 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003413 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003414 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003415
Christopher Fauletbe821b92017-03-30 11:21:53 +02003416 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3417 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003418#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003419 /* We expect some data from the client. Unless we know for sure
3420 * we already have a full request, we have to re-enable quick-ack
3421 * in case we previously disabled it, otherwise we might cause
3422 * the client to delay further data.
3423 */
3424 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3425 cli_conn && conn_ctrl_ready(cli_conn) &&
3426 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003427 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003428 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003429#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003430
Willy Tarreau59234e92008-11-30 23:51:27 +01003431 /*************************************************************
3432 * OK, that's finished for the headers. We have done what we *
3433 * could. Let's switch to the DATA state. *
3434 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003435 req->analyse_exp = TICK_ETERNITY;
3436 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003437
Willy Tarreau59234e92008-11-30 23:51:27 +01003438 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003439 /* OK let's go on with the BODY now */
3440 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003441
Willy Tarreau59234e92008-11-30 23:51:27 +01003442 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003443 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003444 /* we detected a parsing error. We want to archive this request
3445 * in the dedicated proxy area for later troubleshooting.
3446 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003447 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003448 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003449
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003450 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003451 txn->req.msg_state = HTTP_MSG_ERROR;
3452 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003453 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003454 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003455
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003456 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003457 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003458 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003459
Willy Tarreaue7dff022015-04-03 01:14:29 +02003460 if (!(s->flags & SF_ERR_MASK))
3461 s->flags |= SF_ERR_PRXCOND;
3462 if (!(s->flags & SF_FINST_MASK))
3463 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003464 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003465}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003466
Willy Tarreau60b85b02008-11-30 23:28:40 +01003467/* This function is an analyser which processes the HTTP tarpit. It always
3468 * returns zero, at the beginning because it prevents any other processing
3469 * from occurring, and at the end because it terminates the request.
3470 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003471int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003472{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003473 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003474
Christopher Faulete0768eb2018-10-03 16:38:02 +02003475 if (IS_HTX_STRM(s))
3476 return htx_process_tarpit(s, req, an_bit);
3477
Willy Tarreau60b85b02008-11-30 23:28:40 +01003478 /* This connection is being tarpitted. The CLIENT side has
3479 * already set the connect expiration date to the right
3480 * timeout. We just have to check that the client is still
3481 * there and that the timeout has not expired.
3482 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003483 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003484 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003485 !tick_is_expired(req->analyse_exp, now_ms))
3486 return 0;
3487
3488 /* We will set the queue timer to the time spent, just for
3489 * logging purposes. We fake a 500 server error, so that the
3490 * attacker will not suspect his connection has been tarpitted.
3491 * It will not cause trouble to the logs because we can exclude
3492 * the tarpitted connections by filtering on the 'PT' status flags.
3493 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003494 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3495
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003496 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003497 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003498
Christopher Faulet0184ea72017-01-05 14:06:34 +01003499 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003500 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003501
Willy Tarreaue7dff022015-04-03 01:14:29 +02003502 if (!(s->flags & SF_ERR_MASK))
3503 s->flags |= SF_ERR_PRXCOND;
3504 if (!(s->flags & SF_FINST_MASK))
3505 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003506 return 0;
3507}
3508
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003509/* This function is an analyser which waits for the HTTP request body. It waits
3510 * for either the buffer to be full, or the full advertised contents to have
3511 * reached the buffer. It must only be called after the standard HTTP request
3512 * processing has occurred, because it expects the request to be parsed and will
3513 * look for the Expect header. It may send a 100-Continue interim response. It
3514 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3515 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3516 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003517 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003518int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003519{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003520 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003521 struct http_txn *txn = s->txn;
3522 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003523
Christopher Faulete0768eb2018-10-03 16:38:02 +02003524 if (IS_HTX_STRM(s))
3525 return htx_wait_for_request_body(s, req, an_bit);
3526
Willy Tarreaud34af782008-11-30 23:36:37 +01003527 /* We have to parse the HTTP request body to find any required data.
3528 * "balance url_param check_post" should have been the only way to get
3529 * into this. We were brought here after HTTP header analysis, so all
3530 * related structures are ready.
3531 */
3532
Willy Tarreau890988f2014-04-10 11:59:33 +02003533 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3534 /* This is the first call */
3535 if (msg->msg_state < HTTP_MSG_BODY)
3536 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003537
Willy Tarreau890988f2014-04-10 11:59:33 +02003538 if (msg->msg_state < HTTP_MSG_100_SENT) {
3539 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3540 * send an HTTP/1.1 100 Continue intermediate response.
3541 */
3542 if (msg->flags & HTTP_MSGF_VER_11) {
3543 struct hdr_ctx ctx;
3544 ctx.idx = 0;
3545 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003546 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003547 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003548 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003549 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003550 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003551 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003552 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003553 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003554
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003555 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003556 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003557 * must save the body in msg->next because it survives buffer
3558 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003559 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003560 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003561
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003562 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003563 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3564 else
3565 msg->msg_state = HTTP_MSG_DATA;
3566 }
3567
Willy Tarreau890988f2014-04-10 11:59:33 +02003568 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3569 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003570 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003571 goto missing_data;
3572
3573 /* OK we have everything we need now */
3574 goto http_end;
3575 }
3576
3577 /* OK here we're parsing a chunked-encoded message */
3578
Willy Tarreau522d6c02009-12-06 18:49:18 +01003579 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003580 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003581 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003582 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003583 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003584 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003585 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003586
Willy Tarreau115acb92009-12-26 13:56:06 +01003587 if (!ret)
3588 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003589 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003590 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003591 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003592 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003593 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003594 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003595 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003596
3597 msg->chunk_len = chunk;
3598 msg->body_len += chunk;
3599
3600 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003601 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003602 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003603 }
3604
Willy Tarreaud98cf932009-12-27 22:54:55 +01003605 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003606 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3607 * for at least a whole chunk or the whole content length bytes after
3608 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003609 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003610 if (msg->msg_state == HTTP_MSG_TRAILERS)
3611 goto http_end;
3612
Willy Tarreaue115b492015-05-01 23:05:14 +02003613 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003614 goto http_end;
3615
3616 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003617 /* we get here if we need to wait for more data. If the buffer is full,
3618 * we have the maximum we can expect.
3619 */
Willy Tarreau23752332018-06-15 14:54:53 +02003620 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003621 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003622
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003623 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003624 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003625 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003626
Willy Tarreaue7dff022015-04-03 01:14:29 +02003627 if (!(s->flags & SF_ERR_MASK))
3628 s->flags |= SF_ERR_CLITO;
3629 if (!(s->flags & SF_FINST_MASK))
3630 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003631 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003632 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003633
3634 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003635 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003636 /* Not enough data. We'll re-use the http-request
3637 * timeout here. Ideally, we should set the timeout
3638 * relative to the accept() date. We just set the
3639 * request timeout once at the beginning of the
3640 * request.
3641 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003642 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003643 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003644 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003645 return 0;
3646 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003647
3648 http_end:
3649 /* The situation will not evolve, so let's give up on the analysis. */
3650 s->logs.tv_request = now; /* update the request timer to reflect full request */
3651 req->analysers &= ~an_bit;
3652 req->analyse_exp = TICK_ETERNITY;
3653 return 1;
3654
3655 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003656 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003657 txn->req.msg_state = HTTP_MSG_ERROR;
3658 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003659 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003660
Willy Tarreaue7dff022015-04-03 01:14:29 +02003661 if (!(s->flags & SF_ERR_MASK))
3662 s->flags |= SF_ERR_PRXCOND;
3663 if (!(s->flags & SF_FINST_MASK))
3664 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003665
Willy Tarreau522d6c02009-12-06 18:49:18 +01003666 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003667 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003668 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003669 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003670 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003671 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003672}
3673
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003674/* send a server's name with an outgoing request over an established connection.
3675 * Note: this function is designed to be called once the request has been scheduled
3676 * for being forwarded. This is the reason why it rewinds the buffer before
3677 * proceeding.
3678 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003679int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003680
3681 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003682 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003683 char *hdr_name = be->server_id_hdr_name;
3684 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003685 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003686 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003687 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003688
Christopher Faulet64159df2018-10-24 21:15:35 +02003689 if (IS_HTX_STRM(s))
3690 return htx_send_name_header(s, be, srv_name);
William Lallemandd9e90662012-01-30 17:27:17 +01003691 ctx.idx = 0;
3692
Willy Tarreau211cdec2014-04-17 20:18:08 +02003693 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003694 if (old_o) {
3695 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003696 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003697 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003698 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003699 }
3700
Willy Tarreauf37954d2018-06-15 18:31:02 +02003701 old_i = ci_data(chn);
3702 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 -05003703 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003704 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003705 }
3706
3707 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003708 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003709 memcpy(hdr_val, hdr_name, hdr_name_len);
3710 hdr_val += hdr_name_len;
3711 *hdr_val++ = ':';
3712 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003713 hdr_val += strlcpy2(hdr_val, srv_name,
3714 trash.area + trash.size - hdr_val);
3715 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3716 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003717
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003718 if (old_o) {
3719 /* If this was a forwarded request, we must readjust the amount of
3720 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003721 * variations. Note that the current state is >= HTTP_MSG_BODY,
3722 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003723 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003724 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003725 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003726 txn->req.next -= old_o;
3727 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003728 }
3729
Mark Lamourinec2247f02012-01-04 13:02:01 -05003730 return 0;
3731}
3732
Willy Tarreau610ecce2010-01-04 21:15:02 +01003733/* Terminate current transaction and prepare a new one. This is very tricky
3734 * right now but it works.
3735 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003736void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003737{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003738 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003739 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003740 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003741 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003742 struct connection *srv_conn;
3743 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003744 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003745
Willy Tarreau610ecce2010-01-04 21:15:02 +01003746 /* FIXME: We need a more portable way of releasing a backend's and a
3747 * server's connections. We need a safer way to reinitialize buffer
3748 * flags. We also need a more accurate method for computing per-request
3749 * data.
3750 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003751 cs = objt_cs(s->si[1].end);
3752 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003753
Willy Tarreau4213a112013-12-15 10:25:42 +01003754 /* unless we're doing keep-alive, we want to quickly close the connection
3755 * to the server.
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003756 * XXX cognet: If the connection doesn't have a owner then it may not
3757 * be referenced anywhere, just kill it now, even if it could be reused.
3758 * To be revisited later when revisited later when we handle connection
3759 * pools properly.
Willy Tarreau4213a112013-12-15 10:25:42 +01003760 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003761 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003762 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003763 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3764 si_shutr(&s->si[1]);
3765 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003766 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003767
Willy Tarreaue7dff022015-04-03 01:14:29 +02003768 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003769 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003770 if (unlikely(s->srv_conn))
3771 sess_change_server(s, NULL);
3772 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003773
3774 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003775 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003776
Willy Tarreaueee5b512015-04-03 23:46:31 +02003777 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003778 int n;
3779
Willy Tarreaueee5b512015-04-03 23:46:31 +02003780 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003781 if (n < 1 || n > 5)
3782 n = 0;
3783
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003784 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003785 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003786 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003787 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003788 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003789 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3790 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003791 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003792 }
3793
3794 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003795 s->logs.bytes_in -= ci_data(&s->req);
3796 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003797
Willy Tarreau66425e32018-07-25 06:55:12 +02003798 /* we may need to know the position in the queue */
3799 pendconn_free(s);
3800
Willy Tarreau610ecce2010-01-04 21:15:02 +01003801 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003802 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003803 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003804 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003805 s->do_log(s);
3806 }
3807
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003808 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003809 stream_stop_content_counters(s);
3810 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003811
Willy Tarreau9efd7452018-05-31 14:48:54 +02003812 /* reset the profiling counter */
3813 s->task->calls = 0;
3814 s->task->cpu_time = 0;
3815 s->task->lat_time = 0;
3816 s->task->call_date = (profiling & HA_PROF_TASKS) ? now_mono_time() : 0;
3817
Willy Tarreau610ecce2010-01-04 21:15:02 +01003818 s->logs.accept_date = date; /* user-visible date for logging */
3819 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003820 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3821 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003822 tv_zero(&s->logs.tv_request);
3823 s->logs.t_queue = -1;
3824 s->logs.t_connect = -1;
3825 s->logs.t_data = -1;
3826 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003827 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3828 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003829
Willy Tarreauf37954d2018-06-15 18:31:02 +02003830 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3831 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003832
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003833 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003834 if (s->flags & SF_CURR_SESS) {
3835 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003836 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003837 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003838 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003839 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003840 }
3841
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003842 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003843
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003844
3845 /* If we're doing keepalive, first call the mux detach() method
3846 * to let it know we want to detach without freing the connection.
3847 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003848 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003849 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003850 !si_conn_ready(&s->si[1]) || !srv_conn->owner)
Willy Tarreau323a2d92015-08-04 19:00:17 +02003851 srv_conn = NULL;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003852 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003853
Willy Tarreau350f4872014-11-28 14:42:25 +01003854 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3855 s->si[1].err_type = SI_ET_NONE;
3856 s->si[1].conn_retries = 0; /* used for logging too */
3857 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003858 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 +01003859 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 +02003860 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 +02003861 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3862 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3863 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003864
Patrick Hemmere3faf022018-08-22 10:02:00 -04003865 hlua_ctx_destroy(s->hlua);
3866 s->hlua = NULL;
3867
Willy Tarreaueee5b512015-04-03 23:46:31 +02003868 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003869 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003870 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003871
3872 if (prev_status == 401 || prev_status == 407) {
3873 /* In HTTP keep-alive mode, if we receive a 401, we still have
3874 * a chance of being able to send the visitor again to the same
3875 * server over the same connection. This is required by some
3876 * broken protocols such as NTLM, and anyway whenever there is
3877 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003878 * it's better to do it (at least it helps with debugging), at
3879 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003880 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003881 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003882 }
3883
Willy Tarreau53f96852016-02-02 18:50:47 +01003884 /* Never ever allow to reuse a connection from a non-reuse backend */
3885 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3886 srv_conn->flags |= CO_FL_PRIVATE;
3887
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003888 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003889 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003890
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003891 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003892 s->req.flags |= CF_NEVER_WAIT;
3893 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003894 }
3895
Willy Tarreau714ea782015-11-25 20:11:11 +01003896 /* we're removing the analysers, we MUST re-enable events detection.
3897 * We don't enable close on the response channel since it's either
3898 * already closed, or in keep-alive with an idle connection handler.
3899 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003900 channel_auto_read(&s->req);
3901 channel_auto_close(&s->req);
3902 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003903
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003904 /* we're in keep-alive with an idle connection, monitor it if not already done */
3905 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003906 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003907 if (srv) {
3908 if (srv_conn->flags & CO_FL_PRIVATE)
3909 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3910 else if (prev_flags & TX_NOT_FIRST)
3911 /* note: we check the request, not the connection, but
3912 * this is valid for strategies SAFE and AGGR, and in
3913 * case of ALWS, we don't care anyway.
3914 */
3915 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3916 else
3917 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3918 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003919 }
Christopher Faulete6006242017-03-10 11:52:44 +01003920 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3921 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003922}
3923
3924
3925/* This function updates the request state machine according to the response
3926 * state machine and buffer flags. It returns 1 if it changes anything (flag
3927 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3928 * it is only used to find when a request/response couple is complete. Both
3929 * this function and its equivalent should loop until both return zero. It
3930 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3931 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003932int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003933{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003934 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003935 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003936 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003937 unsigned int old_state = txn->req.msg_state;
3938
Christopher Faulet4be98032017-07-18 10:48:24 +02003939 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003940 return 0;
3941
3942 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003943 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003944 * We can shut the read side unless we want to abort_on_close,
3945 * or we have a POST request. The issue with POST requests is
3946 * that some browsers still send a CRLF after the request, and
3947 * this CRLF must be read so that it does not remain in the kernel
3948 * buffers, otherwise a close could cause an RST on some systems
3949 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003950 * Note that if we're using keep-alive on the client side, we'd
3951 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003952 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003953 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003954 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003955 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3956 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003957 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3958 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003959 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003960 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003961
Willy Tarreau40f151a2012-12-20 12:10:09 +01003962 /* if the server closes the connection, we want to immediately react
3963 * and close the socket to save packets and syscalls.
3964 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003965 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003966
Willy Tarreau7f876a12015-11-18 11:59:55 +01003967 /* In any case we've finished parsing the request so we must
3968 * disable Nagle when sending data because 1) we're not going
3969 * to shut this side, and 2) the server is waiting for us to
3970 * send pending data.
3971 */
3972 chn->flags |= CF_NEVER_WAIT;
3973
Willy Tarreau610ecce2010-01-04 21:15:02 +01003974 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3975 goto wait_other_side;
3976
3977 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3978 /* The server has not finished to respond, so we
3979 * don't want to move in order not to upset it.
3980 */
3981 goto wait_other_side;
3982 }
3983
Willy Tarreau610ecce2010-01-04 21:15:02 +01003984 /* When we get here, it means that both the request and the
3985 * response have finished receiving. Depending on the connection
3986 * mode, we'll have to wait for the last bytes to leave in either
3987 * direction, and sometimes for a close to be effective.
3988 */
3989
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003990 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3991 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003992 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3993 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003994 }
3995 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3996 /* Option forceclose is set, or either side wants to close,
3997 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003998 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003999 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004000 *
4001 * However, there is an exception if the response
4002 * length is undefined. In this case, we need to wait
4003 * the close from the server. The response will be
4004 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004005 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004006 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4007 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4008 goto check_channel_flags;
4009
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004010 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4011 channel_shutr_now(chn);
4012 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004013 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004014 }
4015 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004016 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4017 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004018 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004019 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4020 channel_auto_read(chn);
4021 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004022 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004023 }
4024
Christopher Faulet4be98032017-07-18 10:48:24 +02004025 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004026 }
4027
4028 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4029 http_msg_closing:
4030 /* nothing else to forward, just waiting for the output buffer
4031 * to be empty and for the shutw_now to take effect.
4032 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004033 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004034 txn->req.msg_state = HTTP_MSG_CLOSED;
4035 goto http_msg_closed;
4036 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004037 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004038 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004039 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004040 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004041 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004042 }
4043
4044 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4045 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004046 /* if we don't know whether the server will close, we need to hard close */
4047 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4048 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4049
Willy Tarreau3988d932013-12-27 23:03:08 +01004050 /* see above in MSG_DONE why we only do this in these states */
4051 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4052 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004053 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4054 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004055 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004056 goto wait_other_side;
4057 }
4058
Christopher Faulet4be98032017-07-18 10:48:24 +02004059 check_channel_flags:
4060 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4061 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4062 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004063 txn->req.msg_state = HTTP_MSG_CLOSING;
4064 goto http_msg_closing;
4065 }
4066
4067
Willy Tarreau610ecce2010-01-04 21:15:02 +01004068 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004069 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004070}
4071
4072
4073/* This function updates the response state machine according to the request
4074 * state machine and buffer flags. It returns 1 if it changes anything (flag
4075 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4076 * it is only used to find when a request/response couple is complete. Both
4077 * this function and its equivalent should loop until both return zero. It
4078 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4079 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004080int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004081{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004082 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004083 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004084 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004085 unsigned int old_state = txn->rsp.msg_state;
4086
Christopher Faulet4be98032017-07-18 10:48:24 +02004087 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004088 return 0;
4089
4090 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4091 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004092 * still monitor the server connection for a possible close
4093 * while the request is being uploaded, so we don't disable
4094 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004095 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004096 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004097
4098 if (txn->req.msg_state == HTTP_MSG_ERROR)
4099 goto wait_other_side;
4100
4101 if (txn->req.msg_state < HTTP_MSG_DONE) {
4102 /* The client seems to still be sending data, probably
4103 * because we got an error response during an upload.
4104 * We have the choice of either breaking the connection
4105 * or letting it pass through. Let's do the later.
4106 */
4107 goto wait_other_side;
4108 }
4109
Willy Tarreau610ecce2010-01-04 21:15:02 +01004110 /* When we get here, it means that both the request and the
4111 * response have finished receiving. Depending on the connection
4112 * mode, we'll have to wait for the last bytes to leave in either
4113 * direction, and sometimes for a close to be effective.
4114 */
4115
4116 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4117 /* Server-close mode : shut read and wait for the request
4118 * side to close its output buffer. The caller will detect
4119 * when we're in DONE and the other is in CLOSED and will
4120 * catch that for the final cleanup.
4121 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004122 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4123 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004124 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004125 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4126 /* Option forceclose is set, or either side wants to close,
4127 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004128 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004129 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004130 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004131 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004132 channel_shutr_now(chn);
4133 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004134 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004135 }
4136 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004137 /* The last possible modes are keep-alive and tunnel. Tunnel will
4138 * need to forward remaining data. Keep-alive will need to monitor
4139 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004140 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004141 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004142 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004143 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4144 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004145 }
4146
Christopher Faulet4be98032017-07-18 10:48:24 +02004147 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004148 }
4149
4150 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4151 http_msg_closing:
4152 /* nothing else to forward, just waiting for the output buffer
4153 * to be empty and for the shutw_now to take effect.
4154 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004155 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004156 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4157 goto http_msg_closed;
4158 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004159 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004160 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004161 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004162 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004163 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004164 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004165 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004166 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004167 }
4168
4169 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4170 http_msg_closed:
4171 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004172 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004173 channel_auto_close(chn);
4174 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004175 goto wait_other_side;
4176 }
4177
Christopher Faulet4be98032017-07-18 10:48:24 +02004178 check_channel_flags:
4179 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4180 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4181 /* if we've just closed an output, let's switch */
4182 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4183 goto http_msg_closing;
4184 }
4185
Willy Tarreau610ecce2010-01-04 21:15:02 +01004186 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004187 /* We force the response to leave immediately if we're waiting for the
4188 * other side, since there is no pending shutdown to push it out.
4189 */
4190 if (!channel_is_empty(chn))
4191 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004192 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004193}
4194
4195
Christopher Faulet894da4c2017-07-18 11:29:07 +02004196/* Resync the request and response state machines. */
4197void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004198{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004199 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004200#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004201 int old_req_state = txn->req.msg_state;
4202 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004203#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004204
Willy Tarreau610ecce2010-01-04 21:15:02 +01004205 http_sync_req_state(s);
4206 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004207 if (!http_sync_res_state(s))
4208 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004209 if (!http_sync_req_state(s))
4210 break;
4211 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004212
Christopher Faulet894da4c2017-07-18 11:29:07 +02004213 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4214 "req->analysers=0x%08x res->analysers=0x%08x\n",
4215 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004216 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4217 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004218 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004219
4220
Willy Tarreau610ecce2010-01-04 21:15:02 +01004221 /* OK, both state machines agree on a compatible state.
4222 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004223 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4224 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004225 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4226 * means we must abort the request.
4227 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4228 * corresponding channel.
4229 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4230 * on the response with server-close mode means we've completed one
4231 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004232 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004233 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4234 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004235 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004236 channel_auto_close(&s->req);
4237 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004238 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004239 channel_auto_close(&s->res);
4240 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004241 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004242 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4243 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004244 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004245 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004246 channel_auto_close(&s->res);
4247 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004248 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004249 channel_abort(&s->req);
4250 channel_auto_close(&s->req);
4251 channel_auto_read(&s->req);
4252 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004253 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004254 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4255 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4256 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4257 s->req.analysers &= AN_REQ_FLT_END;
4258 if (HAS_REQ_DATA_FILTERS(s))
4259 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4260 }
4261 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4262 s->res.analysers &= AN_RES_FLT_END;
4263 if (HAS_RSP_DATA_FILTERS(s))
4264 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4265 }
4266 channel_auto_close(&s->req);
4267 channel_auto_read(&s->req);
4268 channel_auto_close(&s->res);
4269 channel_auto_read(&s->res);
4270 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004271 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4272 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004273 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004274 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4275 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4276 /* server-close/keep-alive: terminate this transaction,
4277 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004278 * a fresh-new transaction, but only once we're sure there's
4279 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004280 * another request. They must not hold any pending output data
4281 * and the response buffer must realigned
4282 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004283 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004284 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004285 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004286 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004287 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004288 else {
4289 s->req.analysers = AN_REQ_FLT_END;
4290 s->res.analysers = AN_RES_FLT_END;
4291 txn->flags |= TX_WAIT_CLEANUP;
4292 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004294}
4295
Willy Tarreaud98cf932009-12-27 22:54:55 +01004296/* This function is an analyser which forwards request body (including chunk
4297 * sizes if any). It is called as soon as we must forward, even if we forward
4298 * zero byte. The only situation where it must not be called is when we're in
4299 * tunnel mode and we want to forward till the close. It's used both to forward
4300 * remaining data and to resync after end of body. It expects the msg_state to
4301 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004302 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004303 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004304 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004305 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004306int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004307{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004308 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004309 struct http_txn *txn = s->txn;
4310 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004311 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004312
Christopher Faulete0768eb2018-10-03 16:38:02 +02004313 if (IS_HTX_STRM(s))
4314 return htx_request_forward_body(s, req, an_bit);
4315
Christopher Faulet45073512018-07-20 10:16:29 +02004316 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 +02004317 now_ms, __FUNCTION__,
4318 s,
4319 req,
4320 req->rex, req->wex,
4321 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004322 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004323 req->analysers);
4324
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004325 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4326 return 0;
4327
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004328 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004329 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004330 /* Output closed while we were sending data. We must abort and
4331 * wake the other side up.
4332 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004333 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004334 msg->msg_state = HTTP_MSG_ERROR;
4335 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004336 return 1;
4337 }
4338
Willy Tarreaud98cf932009-12-27 22:54:55 +01004339 /* Note that we don't have to send 100-continue back because we don't
4340 * need the data to complete our job, and it's up to the server to
4341 * decide whether to return 100, 417 or anything else in return of
4342 * an "Expect: 100-continue" header.
4343 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004344 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004345 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4346 ? HTTP_MSG_CHUNK_SIZE
4347 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004348
4349 /* TODO/filters: when http-buffer-request option is set or if a
4350 * rule on url_param exists, the first chunk size could be
4351 * already parsed. In that case, msg->next is after the chunk
4352 * size (including the CRLF after the size). So this case should
4353 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004354 }
4355
Willy Tarreau7ba23542014-04-17 21:50:00 +02004356 /* Some post-connect processing might want us to refrain from starting to
4357 * forward data. Currently, the only reason for this is "balance url_param"
4358 * whichs need to parse/process the request after we've enabled forwarding.
4359 */
4360 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004361 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004362 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004363 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004364 channel_dont_close(req); /* don't fail on early shutr */
4365 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004366 }
4367 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4368 }
4369
Willy Tarreau80a92c02014-03-12 10:41:13 +01004370 /* in most states, we should abort in case of early close */
4371 channel_auto_close(req);
4372
Willy Tarreauefdf0942014-04-24 20:08:57 +02004373 if (req->to_forward) {
4374 /* We can't process the buffer's contents yet */
4375 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004376 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004377 }
4378
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004379 if (msg->msg_state < HTTP_MSG_DONE) {
4380 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4381 ? http_msg_forward_chunked_body(s, msg)
4382 : http_msg_forward_body(s, msg));
4383 if (!ret)
4384 goto missing_data_or_waiting;
4385 if (ret < 0)
4386 goto return_bad_req;
4387 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004388
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004389 /* other states, DONE...TUNNEL */
4390 /* we don't want to forward closes on DONE except in tunnel mode. */
4391 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4392 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004393
Christopher Faulet894da4c2017-07-18 11:29:07 +02004394 http_resync_states(s);
4395 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004396 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4397 if (req->flags & CF_SHUTW) {
4398 /* request errors are most likely due to the
4399 * server aborting the transfer. */
4400 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004401 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004402 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004403 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004404 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004405 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004406 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004407 }
4408
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004409 /* If "option abortonclose" is set on the backend, we want to monitor
4410 * the client's connection and forward any shutdown notification to the
4411 * server, which will decide whether to close or to go on processing the
4412 * request. We only do that in tunnel mode, and not in other modes since
4413 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004414 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004415 channel_auto_read(req);
4416 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4417 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4418 s->si[1].flags |= SI_FL_NOLINGER;
4419 channel_auto_close(req);
4420 }
4421 else if (s->txn->meth == HTTP_METH_POST) {
4422 /* POST requests may require to read extra CRLF sent by broken
4423 * browsers and which could cause an RST to be sent upon close
4424 * on some systems (eg: Linux). */
4425 channel_auto_read(req);
4426 }
4427 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004428
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004429 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004430 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004431 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004432 if (!(s->flags & SF_ERR_MASK))
4433 s->flags |= SF_ERR_CLICL;
4434 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004435 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004436 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004437 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004438 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004439 }
4440
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004441 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4442 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004443 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004444 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004445
4446 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004447 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004448
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004449 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004450 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004451 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004452 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004453
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004454 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004455 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004456 * And when content-length is used, we never want to let the possible
4457 * shutdown be forwarded to the other side, as the state machine will
4458 * take care of it once the client responds. It's also important to
4459 * prevent TIME_WAITs from accumulating on the backend side, and for
4460 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004461 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004462 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004463 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004464
Willy Tarreau5c620922011-05-11 19:56:11 +02004465 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004466 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004467 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004468 * modes are already handled by the stream sock layer. We must not do
4469 * this in content-length mode because it could present the MSG_MORE
4470 * flag with the last block of forwarded data, which would cause an
4471 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004472 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004473 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004474 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004475
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004476 return 0;
4477
Willy Tarreaud98cf932009-12-27 22:54:55 +01004478 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004479 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004480 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004481 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004482
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004483 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004484 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004485 txn->req.msg_state = HTTP_MSG_ERROR;
4486 if (txn->status) {
4487 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004488 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004489 } else {
4490 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004491 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004492 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004493 req->analysers &= AN_REQ_FLT_END;
4494 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004495
Willy Tarreaue7dff022015-04-03 01:14:29 +02004496 if (!(s->flags & SF_ERR_MASK))
4497 s->flags |= SF_ERR_PRXCOND;
4498 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004499 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004500 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004501 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004502 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004503 }
4504 return 0;
4505
4506 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004507 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004508 txn->req.msg_state = HTTP_MSG_ERROR;
4509 if (txn->status) {
4510 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004511 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004512 } else {
4513 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004514 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004515 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004516 req->analysers &= AN_REQ_FLT_END;
4517 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 +01004518
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004519 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4520 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004521 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004522 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004523
Willy Tarreaue7dff022015-04-03 01:14:29 +02004524 if (!(s->flags & SF_ERR_MASK))
4525 s->flags |= SF_ERR_SRVCL;
4526 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004527 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004528 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004529 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004530 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004531 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004532 return 0;
4533}
4534
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004535/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4536 * processing can continue on next analysers, or zero if it either needs more
4537 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004538 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004539 * when it has nothing left to do, and may remove any analyser when it wants to
4540 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004541 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004542int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004543{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004544 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004545 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004546 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004547 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004548 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004549 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004550 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004551 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004552
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004553 srv_conn = cs_conn(objt_cs(s->si[1].end));
4554
Christopher Faulete0768eb2018-10-03 16:38:02 +02004555 if (IS_HTX_STRM(s))
4556 return htx_wait_for_response(s, rep, an_bit);
4557
Christopher Faulet45073512018-07-20 10:16:29 +02004558 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 +02004559 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004560 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004561 rep,
4562 rep->rex, rep->wex,
4563 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004564 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004565 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004566
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004567 /*
4568 * Now parse the partial (or complete) lines.
4569 * We will check the response syntax, and also join multi-line
4570 * headers. An index of all the lines will be elaborated while
4571 * parsing.
4572 *
4573 * For the parsing, we use a 28 states FSM.
4574 *
4575 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004576 * ci_head(rep) = beginning of response
4577 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4578 * ci_tail(rep) = end of input data
4579 * msg->eol = end of current header or line (LF or CRLF)
4580 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004581 */
4582
Willy Tarreau628c40c2014-04-24 19:11:26 +02004583 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004584 /* There's a protected area at the end of the buffer for rewriting
4585 * purposes. We don't want to start to parse the request if the
4586 * protected area is affected, because we may have to move processed
4587 * data later, which is much more complicated.
4588 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004589 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004590 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004591 /* some data has still not left the buffer, wake us once that's done */
4592 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4593 goto abort_response;
4594 channel_dont_close(rep);
4595 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004596 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004597 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004598 }
4599
Willy Tarreau188e2302018-06-15 11:11:53 +02004600 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004601 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004602 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004603
Willy Tarreauf37954d2018-06-15 18:31:02 +02004604 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004605 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004606 }
4607
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004608 /* 1: we might have to print this header in debug mode */
4609 if (unlikely((global.mode & MODE_DEBUG) &&
4610 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004611 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004612 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004613
Willy Tarreauf37954d2018-06-15 18:31:02 +02004614 sol = ci_head(rep);
4615 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004616 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004617
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004618 sol += hdr_idx_first_pos(&txn->hdr_idx);
4619 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004620
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004621 while (cur_idx) {
4622 eol = sol + txn->hdr_idx.v[cur_idx].len;
4623 debug_hdr("srvhdr", s, sol, eol);
4624 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4625 cur_idx = txn->hdr_idx.v[cur_idx].next;
4626 }
4627 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004628
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629 /*
4630 * Now we quickly check if we have found a full valid response.
4631 * If not so, we check the FD and buffer states before leaving.
4632 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004633 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004634 * responses are checked first.
4635 *
4636 * Depending on whether the client is still there or not, we
4637 * may send an error response back or not. Note that normally
4638 * we should only check for HTTP status there, and check I/O
4639 * errors somewhere else.
4640 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004641
Willy Tarreau655dce92009-11-08 13:10:58 +01004642 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004643 /* Invalid response */
4644 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4645 /* we detected a parsing error. We want to archive this response
4646 * in the dedicated proxy area for later troubleshooting.
4647 */
4648 hdr_response_bad:
4649 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004650 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004651
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004652 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004653 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004654 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004655 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004656 }
Willy Tarreau64648412010-03-05 10:41:54 +01004657 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004658 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004659 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004660 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004661 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004662 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004663 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004664
Willy Tarreaue7dff022015-04-03 01:14:29 +02004665 if (!(s->flags & SF_ERR_MASK))
4666 s->flags |= SF_ERR_PRXCOND;
4667 if (!(s->flags & SF_FINST_MASK))
4668 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004669
4670 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004671 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004672
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004673 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004674 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004675 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004676 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004677 goto hdr_response_bad;
4678 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004679
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004680 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004681 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004682 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004683 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004684 else if (txn->flags & TX_NOT_FIRST)
4685 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004686
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004687 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004688 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004689 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004690 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004691 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004692
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004693 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004694 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004695 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004696
4697 /* Check to see if the server refused the early data.
4698 * If so, just send a 425
4699 */
4700 if (objt_cs(s->si[1].end)) {
4701 struct connection *conn = objt_cs(s->si[1].end)->conn;
4702
4703 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4704 txn->status = 425;
4705 }
4706
Willy Tarreau350f4872014-11-28 14:42:25 +01004707 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004708 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004709 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004710
Willy Tarreaue7dff022015-04-03 01:14:29 +02004711 if (!(s->flags & SF_ERR_MASK))
4712 s->flags |= SF_ERR_SRVCL;
4713 if (!(s->flags & SF_FINST_MASK))
4714 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004715 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004716 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004717
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004718 /* read timeout : return a 504 to the client. */
4719 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004720 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004721 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004722
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004723 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004724 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004725 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004726 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004727 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004728
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004729 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004730 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004731 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004732 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004733 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004734 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004735
Willy Tarreaue7dff022015-04-03 01:14:29 +02004736 if (!(s->flags & SF_ERR_MASK))
4737 s->flags |= SF_ERR_SRVTO;
4738 if (!(s->flags & SF_FINST_MASK))
4739 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004740 return 0;
4741 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004742
Willy Tarreauf003d372012-11-26 13:35:37 +01004743 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004744 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004745 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4746 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004747 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004748 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004749
Christopher Faulet0184ea72017-01-05 14:06:34 +01004750 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004751 channel_auto_close(rep);
4752
4753 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004754 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004755 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004756
Willy Tarreaue7dff022015-04-03 01:14:29 +02004757 if (!(s->flags & SF_ERR_MASK))
4758 s->flags |= SF_ERR_CLICL;
4759 if (!(s->flags & SF_FINST_MASK))
4760 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004761
Willy Tarreau87b09662015-04-03 00:22:06 +02004762 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004763 return 0;
4764 }
4765
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004766 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004767 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004768 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004769 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004770 else if (txn->flags & TX_NOT_FIRST)
4771 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004772
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004773 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004774 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004775 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004776 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004777 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004778
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004779 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004780 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004781 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004782 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004783 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004784 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004785
Willy Tarreaue7dff022015-04-03 01:14:29 +02004786 if (!(s->flags & SF_ERR_MASK))
4787 s->flags |= SF_ERR_SRVCL;
4788 if (!(s->flags & SF_FINST_MASK))
4789 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004790 return 0;
4791 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004792
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004793 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004794 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004795 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004796 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004797 else if (txn->flags & TX_NOT_FIRST)
4798 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004799
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004800 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004801 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004802 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004803
Willy Tarreaue7dff022015-04-03 01:14:29 +02004804 if (!(s->flags & SF_ERR_MASK))
4805 s->flags |= SF_ERR_CLICL;
4806 if (!(s->flags & SF_FINST_MASK))
4807 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004808
Willy Tarreau87b09662015-04-03 00:22:06 +02004809 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004810 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004811 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004812
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004813 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004814 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004815 return 0;
4816 }
4817
4818 /* More interesting part now : we know that we have a complete
4819 * response which at least looks like HTTP. We have an indicator
4820 * of each header's length, so we can parse them quickly.
4821 */
4822
4823 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004824 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004825
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004826 /*
4827 * 1: get the status code
4828 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004829 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004830 if (n < 1 || n > 5)
4831 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004832 /* when the client triggers a 4xx from the server, it's most often due
4833 * to a missing object or permission. These events should be tracked
4834 * because if they happen often, it may indicate a brute force or a
4835 * vulnerability scan.
4836 */
4837 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004838 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004839
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004840 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004841 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004842
Willy Tarreau91852eb2015-05-01 13:26:00 +02004843 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4844 * exactly one digit "." one digit. This check may be disabled using
4845 * option accept-invalid-http-response.
4846 */
4847 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4848 if (msg->sl.st.v_l != 8) {
4849 msg->err_pos = 0;
4850 goto hdr_response_bad;
4851 }
4852
Willy Tarreauf37954d2018-06-15 18:31:02 +02004853 if (ci_head(rep)[4] != '/' ||
4854 !isdigit((unsigned char)ci_head(rep)[5]) ||
4855 ci_head(rep)[6] != '.' ||
4856 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004857 msg->err_pos = 4;
4858 goto hdr_response_bad;
4859 }
4860 }
4861
Willy Tarreau5b154472009-12-21 20:11:07 +01004862 /* check if the response is HTTP/1.1 or above */
4863 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004864 ((ci_head(rep)[5] > '1') ||
4865 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004866 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004867
4868 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004869 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 +01004870
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004871 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004872 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004873
Willy Tarreauf37954d2018-06-15 18:31:02 +02004874 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004875
Willy Tarreau39650402010-03-15 19:44:39 +01004876 /* Adjust server's health based on status code. Note: status codes 501
4877 * and 505 are triggered on demand by client request, so we must not
4878 * count them as server failures.
4879 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004880 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004881 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004882 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004883 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004884 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004885 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004886
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004887 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004888 * We may be facing a 100-continue response, or any other informational
4889 * 1xx response which is non-final, in which case this is not the right
4890 * response, and we're waiting for the next one. Let's allow this response
4891 * to go to the client and wait for the next one. There's an exception for
4892 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004893 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004894 if (txn->status < 200 &&
4895 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004896 hdr_idx_init(&txn->hdr_idx);
4897 msg->next -= channel_forward(rep, msg->next);
4898 msg->msg_state = HTTP_MSG_RPBEFORE;
4899 txn->status = 0;
4900 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004901 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004902 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004903 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004904
Willy Tarreaua14ad722017-07-07 11:36:32 +02004905 /*
4906 * 2: check for cacheability.
4907 */
4908
4909 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004910 case 200:
4911 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004912 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004913 case 206:
4914 case 300:
4915 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004916 case 404:
4917 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004918 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004919 case 414:
4920 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004921 break;
4922 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004923 /* RFC7231#6.1:
4924 * Responses with status codes that are defined as
4925 * cacheable by default (e.g., 200, 203, 204, 206,
4926 * 300, 301, 404, 405, 410, 414, and 501 in this
4927 * specification) can be reused by a cache with
4928 * heuristic expiration unless otherwise indicated
4929 * by the method definition or explicit cache
4930 * controls [RFC7234]; all other status codes are
4931 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004932 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004933 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004934 break;
4935 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004936
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004937 /*
4938 * 3: we may need to capture headers
4939 */
4940 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004941 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004942 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4943 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004944
Willy Tarreau557f1992015-05-01 10:05:17 +02004945 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4946 * by RFC7230#3.3.3 :
4947 *
4948 * The length of a message body is determined by one of the following
4949 * (in order of precedence):
4950 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004951 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4952 * the connection will become a tunnel immediately after the empty
4953 * line that concludes the header fields. A client MUST ignore
4954 * any Content-Length or Transfer-Encoding header fields received
4955 * in such a message. Any 101 response (Switching Protocols) is
4956 * managed in the same manner.
4957 *
4958 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004959 * (Informational), 204 (No Content), or 304 (Not Modified) status
4960 * code is always terminated by the first empty line after the
4961 * header fields, regardless of the header fields present in the
4962 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004963 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004964 * 3. If a Transfer-Encoding header field is present and the chunked
4965 * transfer coding (Section 4.1) is the final encoding, the message
4966 * body length is determined by reading and decoding the chunked
4967 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004968 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004969 * If a Transfer-Encoding header field is present in a response and
4970 * the chunked transfer coding is not the final encoding, the
4971 * message body length is determined by reading the connection until
4972 * it is closed by the server. If a Transfer-Encoding header field
4973 * is present in a request and the chunked transfer coding is not
4974 * the final encoding, the message body length cannot be determined
4975 * reliably; the server MUST respond with the 400 (Bad Request)
4976 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004977 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004978 * If a message is received with both a Transfer-Encoding and a
4979 * Content-Length header field, the Transfer-Encoding overrides the
4980 * Content-Length. Such a message might indicate an attempt to
4981 * perform request smuggling (Section 9.5) or response splitting
4982 * (Section 9.4) and ought to be handled as an error. A sender MUST
4983 * remove the received Content-Length field prior to forwarding such
4984 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004985 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004986 * 4. If a message is received without Transfer-Encoding and with
4987 * either multiple Content-Length header fields having differing
4988 * field-values or a single Content-Length header field having an
4989 * invalid value, then the message framing is invalid and the
4990 * recipient MUST treat it as an unrecoverable error. If this is a
4991 * request message, the server MUST respond with a 400 (Bad Request)
4992 * status code and then close the connection. If this is a response
4993 * message received by a proxy, the proxy MUST close the connection
4994 * to the server, discard the received response, and send a 502 (Bad
4995 * Gateway) response to the client. If this is a response message
4996 * received by a user agent, the user agent MUST close the
4997 * connection to the server and discard the received response.
4998 *
4999 * 5. If a valid Content-Length header field is present without
5000 * Transfer-Encoding, its decimal value defines the expected message
5001 * body length in octets. If the sender closes the connection or
5002 * the recipient times out before the indicated number of octets are
5003 * received, the recipient MUST consider the message to be
5004 * incomplete and close the connection.
5005 *
5006 * 6. If this is a request message and none of the above are true, then
5007 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005008 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005009 * 7. Otherwise, this is a response message without a declared message
5010 * body length, so the message body length is determined by the
5011 * number of octets received prior to the server closing the
5012 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005013 */
5014
5015 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005016 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005017 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005018 * FIXME: should we parse anyway and return an error on chunked encoding ?
5019 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005020 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5021 txn->status == 101)) {
5022 /* Either we've established an explicit tunnel, or we're
5023 * switching the protocol. In both cases, we're very unlikely
5024 * to understand the next protocols. We have to switch to tunnel
5025 * mode, so that we transfer the request and responses then let
5026 * this protocol pass unmodified. When we later implement specific
5027 * parsers for such protocols, we'll want to check the Upgrade
5028 * header which contains information about that protocol for
5029 * responses with status 101 (eg: see RFC2817 about TLS).
5030 */
5031 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5032 msg->flags |= HTTP_MSGF_XFER_LEN;
5033 goto end;
5034 }
5035
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005036 if (txn->meth == HTTP_METH_HEAD ||
5037 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005038 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005039 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005040 goto skip_content_length;
5041 }
5042
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005043 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005044 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005045 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005046 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005047 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5048 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005049 /* bad transfer-encoding (chunked followed by something else) */
5050 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005051 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005052 break;
5053 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005054 }
5055
Willy Tarreau1c913912015-04-30 10:57:51 +02005056 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005057 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005058 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005059 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005060 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5061 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005062 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005063 signed long long cl;
5064
Willy Tarreauad14f752011-09-02 20:33:27 +02005065 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005066 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005067 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005068 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005069
Willy Tarreauad14f752011-09-02 20:33:27 +02005070 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005071 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005072 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005073 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005074
Willy Tarreauad14f752011-09-02 20:33:27 +02005075 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005076 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005077 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005078 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005079
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005080 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005081 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005082 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005083 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005084
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005085 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005086 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005087 }
5088
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02005089 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
5090 * 407 (Proxy-Authenticate) responses and set the connection to private
5091 */
5092 if (srv_conn && txn->status == 401) {
5093 /* check for Negotiate/NTLM WWW-Authenticate headers */
5094 ctx.idx = 0;
5095 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
5096 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5097 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5098 srv_conn->flags |= CO_FL_PRIVATE;
5099 }
5100 } else if (srv_conn && txn->status == 407) {
5101 /* check for Negotiate/NTLM Proxy-Authenticate headers */
5102 ctx.idx = 0;
5103 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
5104 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5105 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5106 srv_conn->flags |= CO_FL_PRIVATE;
5107 }
5108 }
5109
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005110 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005111 /* Now we have to check if we need to modify the Connection header.
5112 * This is more difficult on the response than it is on the request,
5113 * because we can have two different HTTP versions and we don't know
5114 * how the client will interprete a response. For instance, let's say
5115 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5116 * HTTP/1.1 response without any header. Maybe it will bound itself to
5117 * HTTP/1.0 because it only knows about it, and will consider the lack
5118 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5119 * the lack of header as a keep-alive. Thus we will use two flags
5120 * indicating how a request MAY be understood by the client. In case
5121 * of multiple possibilities, we'll fix the header to be explicit. If
5122 * ambiguous cases such as both close and keepalive are seen, then we
5123 * will fall back to explicit close. Note that we won't take risks with
5124 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005125 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005126 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005127 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005128 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01005129 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005130
Willy Tarreau60466522010-01-18 19:08:45 +01005131 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005132 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01005133 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005134
Willy Tarreau60466522010-01-18 19:08:45 +01005135 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005136 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01005137 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005138 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005139 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005140 }
Willy Tarreau60466522010-01-18 19:08:45 +01005141 else { /* SCL / KAL */
5142 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005143 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005144 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005145 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005146
Willy Tarreau60466522010-01-18 19:08:45 +01005147 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005148 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005149
Willy Tarreau60466522010-01-18 19:08:45 +01005150 /* Some keep-alive responses are converted to Server-close if
5151 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005152 */
Willy Tarreau60466522010-01-18 19:08:45 +01005153 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5154 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005155 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005156 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005157 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005158 }
5159
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005160 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005161 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005162 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005163
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005164 /* end of job, return OK */
5165 rep->analysers &= ~an_bit;
5166 rep->analyse_exp = TICK_ETERNITY;
5167 channel_auto_close(rep);
5168 return 1;
5169
5170 abort_keep_alive:
5171 /* A keep-alive request to the server failed on a network error.
5172 * The client is required to retry. We need to close without returning
5173 * any other information so that the client retries.
5174 */
5175 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005176 rep->analysers &= AN_RES_FLT_END;
5177 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005178 channel_auto_close(rep);
5179 s->logs.logwait = 0;
5180 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005181 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005182 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005183 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005184 return 0;
5185}
5186
5187/* This function performs all the processing enabled for the current response.
5188 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005189 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005190 * other functions. It works like process_request (see indications above).
5191 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005192int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005193{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005194 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005195 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005196 struct http_msg *msg = &txn->rsp;
5197 struct proxy *cur_proxy;
5198 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005199 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005200
Christopher Faulete0768eb2018-10-03 16:38:02 +02005201 if (IS_HTX_STRM(s))
5202 return htx_process_res_common(s, rep, an_bit, px);
5203
Christopher Faulet45073512018-07-20 10:16:29 +02005204 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 +02005205 now_ms, __FUNCTION__,
5206 s,
5207 rep,
5208 rep->rex, rep->wex,
5209 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005210 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005211 rep->analysers);
5212
5213 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5214 return 0;
5215
Willy Tarreau70730dd2014-04-24 18:06:27 +02005216 /* The stats applet needs to adjust the Connection header but we don't
5217 * apply any filter there.
5218 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005219 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5220 rep->analysers &= ~an_bit;
5221 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005222 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005223 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005224
Willy Tarreau58975672014-04-24 21:13:57 +02005225 /*
5226 * We will have to evaluate the filters.
5227 * As opposed to version 1.2, now they will be evaluated in the
5228 * filters order and not in the header order. This means that
5229 * each filter has to be validated among all headers.
5230 *
5231 * Filters are tried with ->be first, then with ->fe if it is
5232 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005233 *
5234 * Maybe we are in resume condiion. In this case I choose the
5235 * "struct proxy" which contains the rule list matching the resume
5236 * pointer. If none of theses "struct proxy" match, I initialise
5237 * the process with the first one.
5238 *
5239 * In fact, I check only correspondance betwwen the current list
5240 * pointer and the ->fe rule list. If it doesn't match, I initialize
5241 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005242 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005243 if (s->current_rule_list == &sess->fe->http_res_rules)
5244 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005245 else
5246 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005247 while (1) {
5248 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005249
Willy Tarreau58975672014-04-24 21:13:57 +02005250 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005251 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005252 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005253
Willy Tarreau51d861a2015-05-22 17:30:48 +02005254 if (ret == HTTP_RULE_RES_BADREQ)
5255 goto return_srv_prx_502;
5256
5257 if (ret == HTTP_RULE_RES_DONE) {
5258 rep->analysers &= ~an_bit;
5259 rep->analyse_exp = TICK_ETERNITY;
5260 return 1;
5261 }
5262 }
5263
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005264 /* we need to be called again. */
5265 if (ret == HTTP_RULE_RES_YIELD) {
5266 channel_dont_close(rep);
5267 return 0;
5268 }
5269
Willy Tarreau58975672014-04-24 21:13:57 +02005270 /* try headers filters */
5271 if (rule_set->rsp_exp != NULL) {
5272 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5273 return_bad_resp:
5274 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005275 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005276 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005277 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005278 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005279 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005280 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005281 txn->status = 502;
5282 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005283 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005284 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005285 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005286 if (!(s->flags & SF_ERR_MASK))
5287 s->flags |= SF_ERR_PRXCOND;
5288 if (!(s->flags & SF_FINST_MASK))
5289 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005290 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005291 }
Willy Tarreau58975672014-04-24 21:13:57 +02005292 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005293
Willy Tarreau58975672014-04-24 21:13:57 +02005294 /* has the response been denied ? */
5295 if (txn->flags & TX_SVDENY) {
5296 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005297 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005298
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005299 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5300 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005301 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005302 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005303
Willy Tarreau58975672014-04-24 21:13:57 +02005304 goto return_srv_prx_502;
5305 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005306
Willy Tarreau58975672014-04-24 21:13:57 +02005307 /* add response headers from the rule sets in the same order */
5308 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005309 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005310 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005311 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005312 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005313 ret = acl_pass(ret);
5314 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5315 ret = !ret;
5316 if (!ret)
5317 continue;
5318 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005319 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005320 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005321 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005322
Willy Tarreau58975672014-04-24 21:13:57 +02005323 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005324 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005325 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005326 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005327 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005328
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005329 /* After this point, this anayzer can't return yield, so we can
5330 * remove the bit corresponding to this analyzer from the list.
5331 *
5332 * Note that the intermediate returns and goto found previously
5333 * reset the analyzers.
5334 */
5335 rep->analysers &= ~an_bit;
5336 rep->analyse_exp = TICK_ETERNITY;
5337
Willy Tarreau58975672014-04-24 21:13:57 +02005338 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005339 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005340 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005341
Willy Tarreau58975672014-04-24 21:13:57 +02005342 /*
5343 * Now check for a server cookie.
5344 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005345 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005346 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005347
Willy Tarreau58975672014-04-24 21:13:57 +02005348 /*
5349 * Check for cache-control or pragma headers if required.
5350 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005351 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005352 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005353
Willy Tarreau58975672014-04-24 21:13:57 +02005354 /*
5355 * Add server cookie in the response if needed
5356 */
5357 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5358 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005359 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005360 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5361 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5362 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5363 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5364 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005365 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005366 /* the server is known, it's not the one the client requested, or the
5367 * cookie's last seen date needs to be refreshed. We have to
5368 * insert a set-cookie here, except if we want to insert only on POST
5369 * requests and this one isn't. Note that servers which don't have cookies
5370 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005371 */
Willy Tarreau58975672014-04-24 21:13:57 +02005372 if (!objt_server(s->target)->cookie) {
5373 chunk_printf(&trash,
5374 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5375 s->be->cookie_name);
5376 }
5377 else {
5378 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005379
Willy Tarreau58975672014-04-24 21:13:57 +02005380 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5381 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005382 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5383 s30tob64((date.tv_sec+3) >> 2,
5384 trash.area + trash.data);
5385 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005386
Willy Tarreau58975672014-04-24 21:13:57 +02005387 if (s->be->cookie_maxlife) {
5388 /* emit first_date, which is either the original one or
5389 * the current date.
5390 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005391 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005392 s30tob64(txn->cookie_first_date ?
5393 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005394 (date.tv_sec+3) >> 2,
5395 trash.area + trash.data);
5396 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005397 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005398 }
Willy Tarreau58975672014-04-24 21:13:57 +02005399 chunk_appendf(&trash, "; path=/");
5400 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005401
Willy Tarreau58975672014-04-24 21:13:57 +02005402 if (s->be->cookie_domain)
5403 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005404
Willy Tarreau58975672014-04-24 21:13:57 +02005405 if (s->be->ck_opts & PR_CK_HTTPONLY)
5406 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005407
Willy Tarreau58975672014-04-24 21:13:57 +02005408 if (s->be->ck_opts & PR_CK_SECURE)
5409 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005410
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005411 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005412 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005413
Willy Tarreau58975672014-04-24 21:13:57 +02005414 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005415 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005416 /* the server did not change, only the date was updated */
5417 txn->flags |= TX_SCK_UPDATED;
5418 else
5419 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005420
Willy Tarreau58975672014-04-24 21:13:57 +02005421 /* Here, we will tell an eventual cache on the client side that we don't
5422 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5423 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5424 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005425 */
Willy Tarreau58975672014-04-24 21:13:57 +02005426 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005427
Willy Tarreau58975672014-04-24 21:13:57 +02005428 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005429
Willy Tarreau58975672014-04-24 21:13:57 +02005430 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5431 "Cache-control: private", 22) < 0))
5432 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005433 }
Willy Tarreau58975672014-04-24 21:13:57 +02005434 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005435
Willy Tarreau58975672014-04-24 21:13:57 +02005436 /*
5437 * Check if result will be cacheable with a cookie.
5438 * We'll block the response if security checks have caught
5439 * nasty things such as a cacheable cookie.
5440 */
5441 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5442 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5443 (s->be->options & PR_O_CHK_CACHE)) {
5444 /* we're in presence of a cacheable response containing
5445 * a set-cookie header. We'll block it as requested by
5446 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005447 */
Willy Tarreau58975672014-04-24 21:13:57 +02005448 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005449 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005450
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005451 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5452 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005453 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005454 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005455
Christopher Faulet767a84b2017-11-24 16:50:31 +01005456 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5457 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005458 send_log(s->be, LOG_ALERT,
5459 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5460 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5461 goto return_srv_prx_502;
5462 }
Willy Tarreau03945942009-12-22 16:50:27 +01005463
Willy Tarreau70730dd2014-04-24 18:06:27 +02005464 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005465 /*
5466 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5467 * If an "Upgrade" token is found, the header is left untouched in order
5468 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005469 * if anything but "Upgrade" is present in the Connection header. We don't
5470 * want to touch any 101 response either since it's switching to another
5471 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005472 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005473 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005474 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005475 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005476
Willy Tarreau58975672014-04-24 21:13:57 +02005477 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5478 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5479 /* we want a keep-alive response here. Keep-alive header
5480 * required if either side is not 1.1.
5481 */
5482 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5483 want_flags |= TX_CON_KAL_SET;
5484 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005485 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005486 /* we want a close response here. Close header required if
5487 * the server is 1.1, regardless of the client.
5488 */
5489 if (msg->flags & HTTP_MSGF_VER_11)
5490 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005491 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005492
Willy Tarreau58975672014-04-24 21:13:57 +02005493 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5494 http_change_connection_header(txn, msg, want_flags);
5495 }
5496
5497 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005498 /* Always enter in the body analyzer */
5499 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5500 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005501
Willy Tarreau58975672014-04-24 21:13:57 +02005502 /* if the user wants to log as soon as possible, without counting
5503 * bytes from the server, then this is the right moment. We have
5504 * to temporarily assign bytes_out to log what we currently have.
5505 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005506 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005507 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5508 s->logs.bytes_out = txn->rsp.eoh;
5509 s->do_log(s);
5510 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005511 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005512 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005513}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005514
Willy Tarreaud98cf932009-12-27 22:54:55 +01005515/* This function is an analyser which forwards response body (including chunk
5516 * sizes if any). It is called as soon as we must forward, even if we forward
5517 * zero byte. The only situation where it must not be called is when we're in
5518 * tunnel mode and we want to forward till the close. It's used both to forward
5519 * remaining data and to resync after end of body. It expects the msg_state to
5520 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005521 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005522 *
5523 * It is capable of compressing response data both in content-length mode and
5524 * in chunked mode. The state machines follows different flows depending on
5525 * whether content-length and chunked modes are used, since there are no
5526 * trailers in content-length :
5527 *
5528 * chk-mode cl-mode
5529 * ,----- BODY -----.
5530 * / \
5531 * V size > 0 V chk-mode
5532 * .--> SIZE -------------> DATA -------------> CRLF
5533 * | | size == 0 | last byte |
5534 * | v final crlf v inspected |
5535 * | TRAILERS -----------> DONE |
5536 * | |
5537 * `----------------------------------------------'
5538 *
5539 * Compression only happens in the DATA state, and must be flushed in final
5540 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5541 * is performed at once on final states for all bytes parsed, or when leaving
5542 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005543 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005544int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005545{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005546 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005547 struct http_txn *txn = s->txn;
5548 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005549 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005550
Christopher Faulete0768eb2018-10-03 16:38:02 +02005551 if (IS_HTX_STRM(s))
5552 return htx_response_forward_body(s, res, an_bit);
5553
Christopher Faulet45073512018-07-20 10:16:29 +02005554 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 +02005555 now_ms, __FUNCTION__,
5556 s,
5557 res,
5558 res->rex, res->wex,
5559 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005560 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005561 res->analysers);
5562
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005563 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5564 return 0;
5565
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005566 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005567 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005568 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005569 /* Output closed while we were sending data. We must abort and
5570 * wake the other side up.
5571 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005572 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005573 msg->msg_state = HTTP_MSG_ERROR;
5574 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005575 return 1;
5576 }
5577
Willy Tarreau4fe41902010-06-07 22:27:41 +02005578 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005579 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005580
Christopher Fauletd7c91962015-04-30 11:48:27 +02005581 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005582 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5583 ? HTTP_MSG_CHUNK_SIZE
5584 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005585 }
5586
Willy Tarreauefdf0942014-04-24 20:08:57 +02005587 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005588 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005589 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005590 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005591 }
5592
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005593 if (msg->msg_state < HTTP_MSG_DONE) {
5594 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5595 ? http_msg_forward_chunked_body(s, msg)
5596 : http_msg_forward_body(s, msg));
5597 if (!ret)
5598 goto missing_data_or_waiting;
5599 if (ret < 0)
5600 goto return_bad_res;
5601 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005602
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005603 /* other states, DONE...TUNNEL */
5604 /* for keep-alive we don't want to forward closes on DONE */
5605 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5606 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5607 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005608
Christopher Faulet894da4c2017-07-18 11:29:07 +02005609 http_resync_states(s);
5610 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005611 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5612 if (res->flags & CF_SHUTW) {
5613 /* response errors are most likely due to the
5614 * client aborting the transfer. */
5615 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005616 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005617 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005618 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005619 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005620 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005621 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005622 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005623 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005624
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005625 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005626 if (res->flags & CF_SHUTW)
5627 goto aborted_xfer;
5628
5629 /* stop waiting for data if the input is closed before the end. If the
5630 * client side was already closed, it means that the client has aborted,
5631 * so we don't want to count this as a server abort. Otherwise it's a
5632 * server abort.
5633 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005634 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005635 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005636 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005637 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005638 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005639 if (!(s->flags & SF_ERR_MASK))
5640 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005641 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005642 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005643 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005644 goto return_bad_res_stats_ok;
5645 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005646 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005647
Willy Tarreau40dba092010-03-04 18:14:51 +01005648 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005649 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005650 goto return_bad_res;
5651
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005652 /* When TE: chunked is used, we need to get there again to parse
5653 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005654 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5655 * or if there are filters registered on the stream, we don't want to
5656 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005657 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005658 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005659 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005660 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5661 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005662 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005663
Willy Tarreau5c620922011-05-11 19:56:11 +02005664 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005665 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005666 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005667 * modes are already handled by the stream sock layer. We must not do
5668 * this in content-length mode because it could present the MSG_MORE
5669 * flag with the last block of forwarded data, which would cause an
5670 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005671 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005672 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005673 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005674
Willy Tarreau87b09662015-04-03 00:22:06 +02005675 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005676 return 0;
5677
Willy Tarreau40dba092010-03-04 18:14:51 +01005678 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005679 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005680 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005681 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005682
5683 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005684 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005685 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005686 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005687 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005688 res->analysers &= AN_RES_FLT_END;
5689 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 +01005690 if (objt_server(s->target))
5691 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005692
Willy Tarreaue7dff022015-04-03 01:14:29 +02005693 if (!(s->flags & SF_ERR_MASK))
5694 s->flags |= SF_ERR_PRXCOND;
5695 if (!(s->flags & SF_FINST_MASK))
5696 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005697 return 0;
5698
5699 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005700 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005701 txn->rsp.msg_state = HTTP_MSG_ERROR;
5702 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005703 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005704 res->analysers &= AN_RES_FLT_END;
5705 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 +01005706
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005707 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5708 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005709 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005710 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005711
Willy Tarreaue7dff022015-04-03 01:14:29 +02005712 if (!(s->flags & SF_ERR_MASK))
5713 s->flags |= SF_ERR_CLICL;
5714 if (!(s->flags & SF_FINST_MASK))
5715 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005716 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005717}
5718
5719
Christopher Faulet10079f52018-10-03 15:17:28 +02005720int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005721{
5722 struct channel *chn = msg->chn;
5723 int ret;
5724
5725 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5726
5727 if (msg->msg_state == HTTP_MSG_ENDING)
5728 goto ending;
5729
5730 /* Neither content-length, nor transfer-encoding was found, so we must
5731 * read the body until the server connection is closed. In that case, we
5732 * eat data as they come. Of course, this happens for response only. */
5733 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005734 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005735 msg->chunk_len += len;
5736 msg->body_len += len;
5737 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005738 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005739 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005740 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005741 msg->next += ret;
5742 msg->chunk_len -= ret;
5743 if (msg->chunk_len) {
5744 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005745 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005746 chn->flags |= CF_WAKE_WRITE;
5747 goto missing_data_or_waiting;
5748 }
5749
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005750 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5751 * always set for a request. */
5752 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5753 /* The server still sending data that should be filtered */
5754 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5755 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005756 msg->msg_state = HTTP_MSG_TUNNEL;
5757 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005758 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005759
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005760 msg->msg_state = HTTP_MSG_ENDING;
5761
5762 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005763 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005764 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005765 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5766 /* default_ret */ msg->next,
5767 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005768 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005769 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005770 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5771 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005772 if (msg->next)
5773 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005774
Christopher Fauletda02e172015-12-04 09:25:05 +01005775 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5776 /* default_ret */ 1,
5777 /* on_error */ goto error,
5778 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005779 if (msg->msg_state == HTTP_MSG_ENDING)
5780 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005781 return 1;
5782
5783 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005784 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005785 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5786 /* default_ret */ msg->next,
5787 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005788 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005789 msg->next -= ret;
5790 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5791 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005792 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005793 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005794 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005795 return 0;
5796 error:
5797 return -1;
5798}
5799
Christopher Faulet10079f52018-10-03 15:17:28 +02005800int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005801{
5802 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005803 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005804 int ret;
5805
5806 /* Here we have the guarantee to be in one of the following state:
5807 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5808 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5809
Christopher Fauletca874b82018-09-20 11:31:01 +02005810 if (msg->msg_state == HTTP_MSG_ENDING)
5811 goto ending;
5812
5813 /* Don't parse chunks if there is no input data */
5814 if (!ci_data(chn))
5815 goto waiting;
5816
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005817 switch_states:
5818 switch (msg->msg_state) {
5819 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005820 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005821 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005822 /* on_error */ goto error);
5823 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005824 msg->chunk_len -= ret;
5825 if (msg->chunk_len) {
5826 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005827 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005828 chn->flags |= CF_WAKE_WRITE;
5829 goto missing_data_or_waiting;
5830 }
5831
5832 /* nothing left to forward for this chunk*/
5833 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5834 /* fall through for HTTP_MSG_CHUNK_CRLF */
5835
5836 case HTTP_MSG_CHUNK_CRLF:
5837 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005838 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005839 if (ret == 0)
5840 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005841 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005842 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005843 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005844 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005845 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005846 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005847 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005848 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5849 /* fall through for HTTP_MSG_CHUNK_SIZE */
5850
5851 case HTTP_MSG_CHUNK_SIZE:
5852 /* read the chunk size and assign it to ->chunk_len,
5853 * then set ->next to point to the body and switch to
5854 * DATA or TRAILERS state.
5855 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005856 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005857 if (ret == 0)
5858 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005859 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005860 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005861 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005862 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005863 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005864 }
5865
5866 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005867 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005868 msg->chunk_len = chunk;
5869 msg->body_len += chunk;
5870
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005871 if (msg->chunk_len) {
5872 msg->msg_state = HTTP_MSG_DATA;
5873 goto switch_states;
5874 }
5875 msg->msg_state = HTTP_MSG_TRAILERS;
5876 /* fall through for HTTP_MSG_TRAILERS */
5877
5878 case HTTP_MSG_TRAILERS:
5879 ret = http_forward_trailers(msg);
5880 if (ret < 0)
5881 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005882 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5883 /* default_ret */ 1,
5884 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005885 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005886 if (!ret)
5887 goto missing_data_or_waiting;
5888 break;
5889
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005890 default:
5891 /* This should no happen in this function */
5892 goto error;
5893 }
5894
5895 msg->msg_state = HTTP_MSG_ENDING;
5896 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005897 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005898 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005899 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005900 /* default_ret */ msg->next,
5901 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005902 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005903 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005904 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5905 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005906 if (msg->next)
5907 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005908
Christopher Fauletda02e172015-12-04 09:25:05 +01005909 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005910 /* default_ret */ 1,
5911 /* on_error */ goto error,
5912 /* on_wait */ goto waiting);
5913 msg->msg_state = HTTP_MSG_DONE;
5914 return 1;
5915
5916 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005917 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005918 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005919 /* default_ret */ msg->next,
5920 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005921 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005922 msg->next -= ret;
5923 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5924 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005925 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005926 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005927 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005928 return 0;
5929
5930 chunk_parsing_error:
5931 if (msg->err_pos >= 0) {
5932 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005933 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005934 msg->msg_state, strm_fe(s));
5935 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005936 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005937 msg, msg->msg_state, s->be);
5938 }
5939 error:
5940 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005941}
5942
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005943
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005944/* Iterate the same filter through all request headers.
5945 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005946 * Since it can manage the switch to another backend, it updates the per-proxy
5947 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005948 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005949int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005950{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005951 char *cur_ptr, *cur_end, *cur_next;
5952 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005953 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005954 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005955 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005956
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005957 last_hdr = 0;
5958
Willy Tarreauf37954d2018-06-15 18:31:02 +02005959 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005960 old_idx = 0;
5961
5962 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005963 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005964 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005965 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005966 (exp->action == ACT_ALLOW ||
5967 exp->action == ACT_DENY ||
5968 exp->action == ACT_TARPIT))
5969 return 0;
5970
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005971 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005972 if (!cur_idx)
5973 break;
5974
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005975 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005976 cur_ptr = cur_next;
5977 cur_end = cur_ptr + cur_hdr->len;
5978 cur_next = cur_end + cur_hdr->cr + 1;
5979
5980 /* Now we have one header between cur_ptr and cur_end,
5981 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005982 */
5983
Willy Tarreau15a53a42015-01-21 13:39:42 +01005984 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005985 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005986 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005987 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005988 last_hdr = 1;
5989 break;
5990
5991 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005992 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005993 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005994 break;
5995
5996 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005997 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005998 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005999 break;
6000
6001 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006002 len = exp_replace(trash.area,
6003 trash.size, cur_ptr,
6004 exp->replace, pmatch);
6005 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006006 return -1;
6007
Willy Tarreau6e27be12018-08-22 04:46:47 +02006008 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6009
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006010 /* FIXME: if the user adds a newline in the replacement, the
6011 * index will not be recalculated for now, and the new line
6012 * will not be counted as a new header.
6013 */
6014
6015 cur_end += delta;
6016 cur_next += delta;
6017 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006018 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006019 break;
6020
6021 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006022 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006023 cur_next += delta;
6024
Willy Tarreaufa355d42009-11-29 18:12:29 +01006025 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006026 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6027 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006028 cur_hdr->len = 0;
6029 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006030 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006031 break;
6032
6033 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006034 }
6035
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006036 /* keep the link from this header to next one in case of later
6037 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006038 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006039 old_idx = cur_idx;
6040 }
6041 return 0;
6042}
6043
6044
6045/* Apply the filter to the request line.
6046 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6047 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006048 * Since it can manage the switch to another backend, it updates the per-proxy
6049 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006050 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006051int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006052{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006053 char *cur_ptr, *cur_end;
6054 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006055 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006056 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006057
Willy Tarreau3d300592007-03-18 18:34:41 +01006058 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006059 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006060 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006061 (exp->action == ACT_ALLOW ||
6062 exp->action == ACT_DENY ||
6063 exp->action == ACT_TARPIT))
6064 return 0;
6065 else if (exp->action == ACT_REMOVE)
6066 return 0;
6067
6068 done = 0;
6069
Willy Tarreauf37954d2018-06-15 18:31:02 +02006070 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006071 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006072
6073 /* Now we have the request line between cur_ptr and cur_end */
6074
Willy Tarreau15a53a42015-01-21 13:39:42 +01006075 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006076 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006077 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006078 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006079 done = 1;
6080 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006081
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006082 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006083 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006084 done = 1;
6085 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006086
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006087 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006088 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006089 done = 1;
6090 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006091
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006092 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006093 len = exp_replace(trash.area, trash.size,
6094 cur_ptr, exp->replace, pmatch);
6095 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006096 return -1;
6097
Willy Tarreau6e27be12018-08-22 04:46:47 +02006098 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6099
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006100 /* FIXME: if the user adds a newline in the replacement, the
6101 * index will not be recalculated for now, and the new line
6102 * will not be counted as a new header.
6103 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006104
Willy Tarreaufa355d42009-11-29 18:12:29 +01006105 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006106 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006107 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006108 HTTP_MSG_RQMETH,
6109 cur_ptr, cur_end + 1,
6110 NULL, NULL);
6111 if (unlikely(!cur_end))
6112 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006114 /* we have a full request and we know that we have either a CR
6115 * or an LF at <ptr>.
6116 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006117 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6118 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006119 /* there is no point trying this regex on headers */
6120 return 1;
6121 }
6122 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006123 return done;
6124}
Willy Tarreau97de6242006-12-27 17:18:38 +01006125
Willy Tarreau58f10d72006-12-04 02:26:12 +01006126
Willy Tarreau58f10d72006-12-04 02:26:12 +01006127
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006128/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006129 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006130 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006131 * unparsable request. Since it can manage the switch to another backend, it
6132 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006133 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006134int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006135{
Willy Tarreau192252e2015-04-04 01:47:55 +02006136 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006137 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006138 struct hdr_exp *exp;
6139
6140 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006141 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006142
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006143 /*
6144 * The interleaving of transformations and verdicts
6145 * makes it difficult to decide to continue or stop
6146 * the evaluation.
6147 */
6148
Willy Tarreau6c123b12010-01-28 20:22:06 +01006149 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6150 break;
6151
Willy Tarreau3d300592007-03-18 18:34:41 +01006152 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006153 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006154 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006155 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006156
6157 /* if this filter had a condition, evaluate it now and skip to
6158 * next filter if the condition does not match.
6159 */
6160 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006161 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006162 ret = acl_pass(ret);
6163 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6164 ret = !ret;
6165
6166 if (!ret)
6167 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006168 }
6169
6170 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006171 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006172 if (unlikely(ret < 0))
6173 return -1;
6174
6175 if (likely(ret == 0)) {
6176 /* The filter did not match the request, it can be
6177 * iterated through all headers.
6178 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006179 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6180 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006181 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006182 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006183 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006184}
6185
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006186
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006187/* Delete a value in a header between delimiters <from> and <next> in buffer
6188 * <buf>. The number of characters displaced is returned, and the pointer to
6189 * the first delimiter is updated if required. The function tries as much as
6190 * possible to respect the following principles :
6191 * - replace <from> delimiter by the <next> one unless <from> points to a
6192 * colon, in which case <next> is simply removed
6193 * - set exactly one space character after the new first delimiter, unless
6194 * there are not enough characters in the block being moved to do so.
6195 * - remove unneeded spaces before the previous delimiter and after the new
6196 * one.
6197 *
6198 * It is the caller's responsibility to ensure that :
6199 * - <from> points to a valid delimiter or the colon ;
6200 * - <next> points to a valid delimiter or the final CR/LF ;
6201 * - there are non-space chars before <from> ;
6202 * - there is a CR/LF at or after <next>.
6203 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006204int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006205{
6206 char *prev = *from;
6207
6208 if (*prev == ':') {
6209 /* We're removing the first value, preserve the colon and add a
6210 * space if possible.
6211 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006212 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006213 next++;
6214 prev++;
6215 if (prev < next)
6216 *prev++ = ' ';
6217
Willy Tarreau2235b262016-11-05 15:50:20 +01006218 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006219 next++;
6220 } else {
6221 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006222 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006223 prev--;
6224 *from = prev;
6225
6226 /* copy the delimiter and if possible a space if we're
6227 * not at the end of the line.
6228 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006229 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006230 *prev++ = *next++;
6231 if (prev + 1 < next)
6232 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006233 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006234 next++;
6235 }
6236 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006237 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006238}
6239
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006240/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006241 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006242 * desirable to call it only when needed. This code is quite complex because
6243 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6244 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006245 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006246void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006247{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006248 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006249 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006250 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006251 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006252 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6253 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006254
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006255 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006256 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006257 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006258
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006259 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006260 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006261 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006262
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006263 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006264 hdr_beg = hdr_next;
6265 hdr_end = hdr_beg + cur_hdr->len;
6266 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006267
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006268 /* We have one full header between hdr_beg and hdr_end, and the
6269 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006270 * "Cookie:" headers.
6271 */
6272
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006273 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006274 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006275 old_idx = cur_idx;
6276 continue;
6277 }
6278
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006279 del_from = NULL; /* nothing to be deleted */
6280 preserve_hdr = 0; /* assume we may kill the whole header */
6281
Willy Tarreau58f10d72006-12-04 02:26:12 +01006282 /* Now look for cookies. Conforming to RFC2109, we have to support
6283 * attributes whose name begin with a '$', and associate them with
6284 * the right cookie, if we want to delete this cookie.
6285 * So there are 3 cases for each cookie read :
6286 * 1) it's a special attribute, beginning with a '$' : ignore it.
6287 * 2) it's a server id cookie that we *MAY* want to delete : save
6288 * some pointers on it (last semi-colon, beginning of cookie...)
6289 * 3) it's an application cookie : we *MAY* have to delete a previous
6290 * "special" cookie.
6291 * At the end of loop, if a "special" cookie remains, we may have to
6292 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006293 * *MUST* delete it.
6294 *
6295 * Note: RFC2965 is unclear about the processing of spaces around
6296 * the equal sign in the ATTR=VALUE form. A careful inspection of
6297 * the RFC explicitly allows spaces before it, and not within the
6298 * tokens (attrs or values). An inspection of RFC2109 allows that
6299 * too but section 10.1.3 lets one think that spaces may be allowed
6300 * after the equal sign too, resulting in some (rare) buggy
6301 * implementations trying to do that. So let's do what servers do.
6302 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6303 * allowed quoted strings in values, with any possible character
6304 * after a backslash, including control chars and delimitors, which
6305 * causes parsing to become ambiguous. Browsers also allow spaces
6306 * within values even without quotes.
6307 *
6308 * We have to keep multiple pointers in order to support cookie
6309 * removal at the beginning, middle or end of header without
6310 * corrupting the header. All of these headers are valid :
6311 *
6312 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6313 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6314 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6315 * | | | | | | | | |
6316 * | | | | | | | | hdr_end <--+
6317 * | | | | | | | +--> next
6318 * | | | | | | +----> val_end
6319 * | | | | | +-----------> val_beg
6320 * | | | | +--------------> equal
6321 * | | | +----------------> att_end
6322 * | | +---------------------> att_beg
6323 * | +--------------------------> prev
6324 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006325 */
6326
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006327 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6328 /* Iterate through all cookies on this line */
6329
6330 /* find att_beg */
6331 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006332 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006333 att_beg++;
6334
6335 /* find att_end : this is the first character after the last non
6336 * space before the equal. It may be equal to hdr_end.
6337 */
6338 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006339
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006340 while (equal < hdr_end) {
6341 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006342 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006343 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006344 continue;
6345 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006346 }
6347
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006348 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6349 * is between <att_beg> and <equal>, both may be identical.
6350 */
6351
6352 /* look for end of cookie if there is an equal sign */
6353 if (equal < hdr_end && *equal == '=') {
6354 /* look for the beginning of the value */
6355 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006356 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006357 val_beg++;
6358
6359 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006360 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006361
6362 /* make val_end point to the first white space or delimitor after the value */
6363 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006364 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006365 val_end--;
6366 } else {
6367 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006368 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006369
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006370 /* We have nothing to do with attributes beginning with '$'. However,
6371 * they will automatically be removed if a header before them is removed,
6372 * since they're supposed to be linked together.
6373 */
6374 if (*att_beg == '$')
6375 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006376
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006377 /* Ignore cookies with no equal sign */
6378 if (equal == next) {
6379 /* This is not our cookie, so we must preserve it. But if we already
6380 * scheduled another cookie for removal, we cannot remove the
6381 * complete header, but we can remove the previous block itself.
6382 */
6383 preserve_hdr = 1;
6384 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006385 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006386 val_end += delta;
6387 next += delta;
6388 hdr_end += delta;
6389 hdr_next += delta;
6390 cur_hdr->len += delta;
6391 http_msg_move_end(&txn->req, delta);
6392 prev = del_from;
6393 del_from = NULL;
6394 }
6395 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006396 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006397
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006398 /* if there are spaces around the equal sign, we need to
6399 * strip them otherwise we'll get trouble for cookie captures,
6400 * or even for rewrites. Since this happens extremely rarely,
6401 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006402 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006403 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6404 int stripped_before = 0;
6405 int stripped_after = 0;
6406
6407 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006408 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006409 equal += stripped_before;
6410 val_beg += stripped_before;
6411 }
6412
6413 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006414 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006415 val_beg += stripped_after;
6416 stripped_before += stripped_after;
6417 }
6418
6419 val_end += stripped_before;
6420 next += stripped_before;
6421 hdr_end += stripped_before;
6422 hdr_next += stripped_before;
6423 cur_hdr->len += stripped_before;
6424 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006425 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006426 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006427
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006428 /* First, let's see if we want to capture this cookie. We check
6429 * that we don't already have a client side cookie, because we
6430 * can only capture one. Also as an optimisation, we ignore
6431 * cookies shorter than the declared name.
6432 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006433 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6434 (val_end - att_beg >= sess->fe->capture_namelen) &&
6435 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006436 int log_len = val_end - att_beg;
6437
Willy Tarreaubafbe012017-11-24 17:34:44 +01006438 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006439 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006440 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006441 if (log_len > sess->fe->capture_len)
6442 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006443 memcpy(txn->cli_cookie, att_beg, log_len);
6444 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006445 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006446 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006447
Willy Tarreaubca99692010-10-06 19:25:55 +02006448 /* Persistence cookies in passive, rewrite or insert mode have the
6449 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006450 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006451 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006452 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006453 * For cookies in prefix mode, the form is :
6454 *
6455 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006456 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006457 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6458 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6459 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006460 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006461
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006462 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6463 * have the server ID between val_beg and delim, and the original cookie between
6464 * delim+1 and val_end. Otherwise, delim==val_end :
6465 *
6466 * Cookie: NAME=SRV; # in all but prefix modes
6467 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6468 * | || || | |+-> next
6469 * | || || | +--> val_end
6470 * | || || +---------> delim
6471 * | || |+------------> val_beg
6472 * | || +-------------> att_end = equal
6473 * | |+-----------------> att_beg
6474 * | +------------------> prev
6475 * +-------------------------> hdr_beg
6476 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006477
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006478 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006479 for (delim = val_beg; delim < val_end; delim++)
6480 if (*delim == COOKIE_DELIM)
6481 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006482 } else {
6483 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006484 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006485 /* Now check if the cookie contains a date field, which would
6486 * appear after a vertical bar ('|') just after the server name
6487 * and before the delimiter.
6488 */
6489 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6490 if (vbar1) {
6491 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006492 * right is the last seen date. It is a base64 encoded
6493 * 30-bit value representing the UNIX date since the
6494 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006495 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006496 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006497 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006498 if (val_end - vbar1 >= 5) {
6499 val = b64tos30(vbar1);
6500 if (val > 0)
6501 txn->cookie_last_date = val << 2;
6502 }
6503 /* look for a second vertical bar */
6504 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6505 if (vbar1 && (val_end - vbar1 > 5)) {
6506 val = b64tos30(vbar1 + 1);
6507 if (val > 0)
6508 txn->cookie_first_date = val << 2;
6509 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006510 }
6511 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006512
Willy Tarreauf64d1412010-10-07 20:06:11 +02006513 /* if the cookie has an expiration date and the proxy wants to check
6514 * it, then we do that now. We first check if the cookie is too old,
6515 * then only if it has expired. We detect strict overflow because the
6516 * time resolution here is not great (4 seconds). Cookies with dates
6517 * in the future are ignored if their offset is beyond one day. This
6518 * allows an admin to fix timezone issues without expiring everyone
6519 * and at the same time avoids keeping unwanted side effects for too
6520 * long.
6521 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006522 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6523 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006524 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006525 txn->flags &= ~TX_CK_MASK;
6526 txn->flags |= TX_CK_OLD;
6527 delim = val_beg; // let's pretend we have not found the cookie
6528 txn->cookie_first_date = 0;
6529 txn->cookie_last_date = 0;
6530 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006531 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6532 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006533 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006534 txn->flags &= ~TX_CK_MASK;
6535 txn->flags |= TX_CK_EXPIRED;
6536 delim = val_beg; // let's pretend we have not found the cookie
6537 txn->cookie_first_date = 0;
6538 txn->cookie_last_date = 0;
6539 }
6540
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006541 /* Here, we'll look for the first running server which supports the cookie.
6542 * This allows to share a same cookie between several servers, for example
6543 * to dedicate backup servers to specific servers only.
6544 * However, to prevent clients from sticking to cookie-less backup server
6545 * when they have incidentely learned an empty cookie, we simply ignore
6546 * empty cookies and mark them as invalid.
6547 * The same behaviour is applied when persistence must be ignored.
6548 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006549 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006550 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006551
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006552 while (srv) {
6553 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6554 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006555 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006556 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006557 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006558 /* we found the server and we can use it */
6559 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006560 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006561 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006562 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006563 break;
6564 } else {
6565 /* we found a server, but it's down,
6566 * mark it as such and go on in case
6567 * another one is available.
6568 */
6569 txn->flags &= ~TX_CK_MASK;
6570 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006571 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006572 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006573 srv = srv->next;
6574 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006575
Willy Tarreauf64d1412010-10-07 20:06:11 +02006576 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006577 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006578 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006579 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006580 txn->flags |= TX_CK_UNUSED;
6581 else
6582 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006583 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006584
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006585 /* depending on the cookie mode, we may have to either :
6586 * - delete the complete cookie if we're in insert+indirect mode, so that
6587 * the server never sees it ;
6588 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlant5ba80252018-11-15 09:25:36 -08006589 * application cookie so that it does not get accidently removed later,
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006590 * if we're in cookie prefix mode
6591 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006592 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006593 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006594
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006595 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006596 val_end += delta;
6597 next += delta;
6598 hdr_end += delta;
6599 hdr_next += delta;
6600 cur_hdr->len += delta;
6601 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006602
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006603 del_from = NULL;
6604 preserve_hdr = 1; /* we want to keep this cookie */
6605 }
6606 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006607 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006608 del_from = prev;
6609 }
6610 } else {
6611 /* This is not our cookie, so we must preserve it. But if we already
6612 * scheduled another cookie for removal, we cannot remove the
6613 * complete header, but we can remove the previous block itself.
6614 */
6615 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006616
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006617 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006618 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006619 if (att_beg >= del_from)
6620 att_beg += delta;
6621 if (att_end >= del_from)
6622 att_end += delta;
6623 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006624 val_end += delta;
6625 next += delta;
6626 hdr_end += delta;
6627 hdr_next += delta;
6628 cur_hdr->len += delta;
6629 http_msg_move_end(&txn->req, delta);
6630 prev = del_from;
6631 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006632 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006633 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006634
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006635 /* continue with next cookie on this header line */
6636 att_beg = next;
6637 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006638
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006639 /* There are no more cookies on this line.
6640 * We may still have one (or several) marked for deletion at the
6641 * end of the line. We must do this now in two ways :
6642 * - if some cookies must be preserved, we only delete from the
6643 * mark to the end of line ;
6644 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006645 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006646 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006647 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006648 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006649 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006650 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006651 cur_hdr->len += delta;
6652 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006653 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006654
6655 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006656 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6657 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006658 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006659 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006660 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006661 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006662 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006663 }
6664
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006665 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006666 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006667 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006668}
6669
6670
Willy Tarreaua15645d2007-03-18 16:22:39 +01006671/* Iterate the same filter through all response headers contained in <rtr>.
6672 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6673 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006674int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006675{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006676 char *cur_ptr, *cur_end, *cur_next;
6677 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006678 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006679 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006680 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006681
6682 last_hdr = 0;
6683
Willy Tarreauf37954d2018-06-15 18:31:02 +02006684 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006685 old_idx = 0;
6686
6687 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006688 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006689 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006690 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006691 (exp->action == ACT_ALLOW ||
6692 exp->action == ACT_DENY))
6693 return 0;
6694
6695 cur_idx = txn->hdr_idx.v[old_idx].next;
6696 if (!cur_idx)
6697 break;
6698
6699 cur_hdr = &txn->hdr_idx.v[cur_idx];
6700 cur_ptr = cur_next;
6701 cur_end = cur_ptr + cur_hdr->len;
6702 cur_next = cur_end + cur_hdr->cr + 1;
6703
6704 /* Now we have one header between cur_ptr and cur_end,
6705 * and the next header starts at cur_next.
6706 */
6707
Willy Tarreau15a53a42015-01-21 13:39:42 +01006708 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006709 switch (exp->action) {
6710 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006711 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006712 last_hdr = 1;
6713 break;
6714
6715 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006716 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006717 last_hdr = 1;
6718 break;
6719
6720 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006721 len = exp_replace(trash.area,
6722 trash.size, cur_ptr,
6723 exp->replace, pmatch);
6724 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006725 return -1;
6726
Willy Tarreau6e27be12018-08-22 04:46:47 +02006727 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6728
Willy Tarreaua15645d2007-03-18 16:22:39 +01006729 /* FIXME: if the user adds a newline in the replacement, the
6730 * index will not be recalculated for now, and the new line
6731 * will not be counted as a new header.
6732 */
6733
6734 cur_end += delta;
6735 cur_next += delta;
6736 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006737 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006738 break;
6739
6740 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006741 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006742 cur_next += delta;
6743
Willy Tarreaufa355d42009-11-29 18:12:29 +01006744 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006745 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6746 txn->hdr_idx.used--;
6747 cur_hdr->len = 0;
6748 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006749 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006750 break;
6751
6752 }
6753 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006754
6755 /* keep the link from this header to next one in case of later
6756 * removal of next header.
6757 */
6758 old_idx = cur_idx;
6759 }
6760 return 0;
6761}
6762
6763
6764/* Apply the filter to the status line in the response buffer <rtr>.
6765 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6766 * or -1 if a replacement resulted in an invalid status line.
6767 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006768int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006769{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006770 char *cur_ptr, *cur_end;
6771 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006772 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006773 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006774
Willy Tarreau3d300592007-03-18 18:34:41 +01006775 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006776 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006777 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006778 (exp->action == ACT_ALLOW ||
6779 exp->action == ACT_DENY))
6780 return 0;
6781 else if (exp->action == ACT_REMOVE)
6782 return 0;
6783
6784 done = 0;
6785
Willy Tarreauf37954d2018-06-15 18:31:02 +02006786 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006787 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006788
6789 /* Now we have the status line between cur_ptr and cur_end */
6790
Willy Tarreau15a53a42015-01-21 13:39:42 +01006791 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006792 switch (exp->action) {
6793 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006794 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006795 done = 1;
6796 break;
6797
6798 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006799 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006800 done = 1;
6801 break;
6802
6803 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006804 len = exp_replace(trash.area, trash.size,
6805 cur_ptr, exp->replace, pmatch);
6806 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006807 return -1;
6808
Willy Tarreau6e27be12018-08-22 04:46:47 +02006809 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6810
Willy Tarreaua15645d2007-03-18 16:22:39 +01006811 /* FIXME: if the user adds a newline in the replacement, the
6812 * index will not be recalculated for now, and the new line
6813 * will not be counted as a new header.
6814 */
6815
Willy Tarreaufa355d42009-11-29 18:12:29 +01006816 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006817 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006818 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006819 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006820 cur_ptr, cur_end + 1,
6821 NULL, NULL);
6822 if (unlikely(!cur_end))
6823 return -1;
6824
6825 /* we have a full respnse and we know that we have either a CR
6826 * or an LF at <ptr>.
6827 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006828 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006829 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006830 /* there is no point trying this regex on headers */
6831 return 1;
6832 }
6833 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006834 return done;
6835}
6836
6837
6838
6839/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006840 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6842 * unparsable response.
6843 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006844int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006845{
Willy Tarreau192252e2015-04-04 01:47:55 +02006846 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006847 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006848 struct hdr_exp *exp;
6849
6850 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006851 int ret;
6852
6853 /*
6854 * The interleaving of transformations and verdicts
6855 * makes it difficult to decide to continue or stop
6856 * the evaluation.
6857 */
6858
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006859 if (txn->flags & TX_SVDENY)
6860 break;
6861
Willy Tarreau3d300592007-03-18 18:34:41 +01006862 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006863 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6864 exp->action == ACT_PASS)) {
6865 exp = exp->next;
6866 continue;
6867 }
6868
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006869 /* if this filter had a condition, evaluate it now and skip to
6870 * next filter if the condition does not match.
6871 */
6872 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006873 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006874 ret = acl_pass(ret);
6875 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6876 ret = !ret;
6877 if (!ret)
6878 continue;
6879 }
6880
Willy Tarreaua15645d2007-03-18 16:22:39 +01006881 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006882 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006883 if (unlikely(ret < 0))
6884 return -1;
6885
6886 if (likely(ret == 0)) {
6887 /* The filter did not match the response, it can be
6888 * iterated through all headers.
6889 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006890 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6891 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006892 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006893 }
6894 return 0;
6895}
6896
6897
Willy Tarreaua15645d2007-03-18 16:22:39 +01006898/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006899 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006900 * desirable to call it only when needed. This function is also used when we
6901 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006902 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006903void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006904{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006905 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006906 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006907 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006908 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006909 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006910 char *hdr_beg, *hdr_end, *hdr_next;
6911 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006912
Willy Tarreaua15645d2007-03-18 16:22:39 +01006913 /* Iterate through the headers.
6914 * we start with the start line.
6915 */
6916 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006917 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006918
6919 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6920 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006921 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006922
6923 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006924 hdr_beg = hdr_next;
6925 hdr_end = hdr_beg + cur_hdr->len;
6926 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006927
Willy Tarreau24581ba2010-08-31 22:39:35 +02006928 /* We have one full header between hdr_beg and hdr_end, and the
6929 * next header starts at hdr_next. We're only interested in
6930 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006931 */
6932
Willy Tarreau24581ba2010-08-31 22:39:35 +02006933 is_cookie2 = 0;
6934 prev = hdr_beg + 10;
6935 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006936 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006937 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6938 if (!val) {
6939 old_idx = cur_idx;
6940 continue;
6941 }
6942 is_cookie2 = 1;
6943 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006944 }
6945
Willy Tarreau24581ba2010-08-31 22:39:35 +02006946 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6947 * <prev> points to the colon.
6948 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006949 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006950
Willy Tarreau24581ba2010-08-31 22:39:35 +02006951 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6952 * check-cache is enabled) and we are not interested in checking
6953 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006954 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006955 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006956 return;
6957
Willy Tarreau24581ba2010-08-31 22:39:35 +02006958 /* OK so now we know we have to process this response cookie.
6959 * The format of the Set-Cookie header is slightly different
6960 * from the format of the Cookie header in that it does not
6961 * support the comma as a cookie delimiter (thus the header
6962 * cannot be folded) because the Expires attribute described in
6963 * the original Netscape's spec may contain an unquoted date
6964 * with a comma inside. We have to live with this because
6965 * many browsers don't support Max-Age and some browsers don't
6966 * support quoted strings. However the Set-Cookie2 header is
6967 * clean.
6968 *
6969 * We have to keep multiple pointers in order to support cookie
6970 * removal at the beginning, middle or end of header without
6971 * corrupting the header (in case of set-cookie2). A special
6972 * pointer, <scav> points to the beginning of the set-cookie-av
6973 * fields after the first semi-colon. The <next> pointer points
6974 * either to the end of line (set-cookie) or next unquoted comma
6975 * (set-cookie2). All of these headers are valid :
6976 *
6977 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6978 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6979 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6980 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6981 * | | | | | | | | | |
6982 * | | | | | | | | +-> next hdr_end <--+
6983 * | | | | | | | +------------> scav
6984 * | | | | | | +--------------> val_end
6985 * | | | | | +--------------------> val_beg
6986 * | | | | +----------------------> equal
6987 * | | | +------------------------> att_end
6988 * | | +----------------------------> att_beg
6989 * | +------------------------------> prev
6990 * +-----------------------------------------> hdr_beg
6991 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006992
Willy Tarreau24581ba2010-08-31 22:39:35 +02006993 for (; prev < hdr_end; prev = next) {
6994 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006995
Willy Tarreau24581ba2010-08-31 22:39:35 +02006996 /* find att_beg */
6997 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006998 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006999 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007000
Willy Tarreau24581ba2010-08-31 22:39:35 +02007001 /* find att_end : this is the first character after the last non
7002 * space before the equal. It may be equal to hdr_end.
7003 */
7004 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007005
Willy Tarreau24581ba2010-08-31 22:39:35 +02007006 while (equal < hdr_end) {
7007 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7008 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007009 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007010 continue;
7011 att_end = equal;
7012 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007013
Willy Tarreau24581ba2010-08-31 22:39:35 +02007014 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7015 * is between <att_beg> and <equal>, both may be identical.
7016 */
7017
7018 /* look for end of cookie if there is an equal sign */
7019 if (equal < hdr_end && *equal == '=') {
7020 /* look for the beginning of the value */
7021 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007022 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007023 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007024
Willy Tarreau24581ba2010-08-31 22:39:35 +02007025 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02007026 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007027
7028 /* make val_end point to the first white space or delimitor after the value */
7029 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007030 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007031 val_end--;
7032 } else {
7033 /* <equal> points to next comma, semi-colon or EOL */
7034 val_beg = val_end = next = equal;
7035 }
7036
7037 if (next < hdr_end) {
7038 /* Set-Cookie2 supports multiple cookies, and <next> points to
7039 * a colon or semi-colon before the end. So skip all attr-value
7040 * pairs and look for the next comma. For Set-Cookie, since
7041 * commas are permitted in values, skip to the end.
7042 */
7043 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02007044 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007045 else
7046 next = hdr_end;
7047 }
7048
7049 /* Now everything is as on the diagram above */
7050
7051 /* Ignore cookies with no equal sign */
7052 if (equal == val_end)
7053 continue;
7054
7055 /* If there are spaces around the equal sign, we need to
7056 * strip them otherwise we'll get trouble for cookie captures,
7057 * or even for rewrites. Since this happens extremely rarely,
7058 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007059 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007060 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7061 int stripped_before = 0;
7062 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007063
Willy Tarreau24581ba2010-08-31 22:39:35 +02007064 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007065 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007066 equal += stripped_before;
7067 val_beg += stripped_before;
7068 }
7069
7070 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007071 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007072 val_beg += stripped_after;
7073 stripped_before += stripped_after;
7074 }
7075
7076 val_end += stripped_before;
7077 next += stripped_before;
7078 hdr_end += stripped_before;
7079 hdr_next += stripped_before;
7080 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007081 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007082 }
7083
7084 /* First, let's see if we want to capture this cookie. We check
7085 * that we don't already have a server side cookie, because we
7086 * can only capture one. Also as an optimisation, we ignore
7087 * cookies shorter than the declared name.
7088 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007089 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007090 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007091 (val_end - att_beg >= sess->fe->capture_namelen) &&
7092 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007093 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007094 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007095 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007096 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007097 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007098 if (log_len > sess->fe->capture_len)
7099 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007100 memcpy(txn->srv_cookie, att_beg, log_len);
7101 txn->srv_cookie[log_len] = 0;
7102 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007103 }
7104
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007105 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007106 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007107 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007108 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7109 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007110 /* assume passive cookie by default */
7111 txn->flags &= ~TX_SCK_MASK;
7112 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007113
7114 /* If the cookie is in insert mode on a known server, we'll delete
7115 * this occurrence because we'll insert another one later.
7116 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007117 * a direct access.
7118 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007119 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007120 /* The "preserve" flag was set, we don't want to touch the
7121 * server's cookie.
7122 */
7123 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007124 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007125 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007126 /* this cookie must be deleted */
7127 if (*prev == ':' && next == hdr_end) {
7128 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007129 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007130 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7131 txn->hdr_idx.used--;
7132 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007133 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007134 hdr_next += delta;
7135 http_msg_move_end(&txn->rsp, delta);
7136 /* note: while both invalid now, <next> and <hdr_end>
7137 * are still equal, so the for() will stop as expected.
7138 */
7139 } else {
7140 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007141 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007142 next = prev;
7143 hdr_end += delta;
7144 hdr_next += delta;
7145 cur_hdr->len += delta;
7146 http_msg_move_end(&txn->rsp, delta);
7147 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007148 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007149 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007150 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007151 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007152 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007153 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007154 * with this server since we know it.
7155 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007156 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007157 next += delta;
7158 hdr_end += delta;
7159 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007160 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007161 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007162
Willy Tarreauf1348312010-10-07 15:54:11 +02007163 txn->flags &= ~TX_SCK_MASK;
7164 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007165 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007166 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007167 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007168 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007169 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007170 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007171 next += delta;
7172 hdr_end += delta;
7173 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007174 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007175 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176
Willy Tarreau827aee92011-03-10 16:55:02 +01007177 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007178 txn->flags &= ~TX_SCK_MASK;
7179 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007180 }
7181 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007182 /* that's done for this cookie, check the next one on the same
7183 * line when next != hdr_end (only if is_cookie2).
7184 */
7185 }
7186 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007187 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007188 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007189}
7190
7191
Willy Tarreaua15645d2007-03-18 16:22:39 +01007192/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007193 * Parses the Cache-Control and Pragma request header fields to determine if
7194 * the request may be served from the cache and/or if it is cacheable. Updates
7195 * s->txn->flags.
7196 */
7197void check_request_for_cacheability(struct stream *s, struct channel *chn)
7198{
7199 struct http_txn *txn = s->txn;
7200 char *p1, *p2;
7201 char *cur_ptr, *cur_end, *cur_next;
7202 int pragma_found;
7203 int cc_found;
7204 int cur_idx;
7205
Christopher Faulet25a02f62018-10-24 12:00:25 +02007206 if (IS_HTX_STRM(s))
7207 return htx_check_request_for_cacheability(s, chn);
7208
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007209 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7210 return; /* nothing more to do here */
7211
7212 cur_idx = 0;
7213 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007214 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007215
7216 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7217 struct hdr_idx_elem *cur_hdr;
7218 int val;
7219
7220 cur_hdr = &txn->hdr_idx.v[cur_idx];
7221 cur_ptr = cur_next;
7222 cur_end = cur_ptr + cur_hdr->len;
7223 cur_next = cur_end + cur_hdr->cr + 1;
7224
7225 /* We have one full header between cur_ptr and cur_end, and the
7226 * next header starts at cur_next.
7227 */
7228
7229 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7230 if (val) {
7231 if ((cur_end - (cur_ptr + val) >= 8) &&
7232 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7233 pragma_found = 1;
7234 continue;
7235 }
7236 }
7237
William Lallemand8a16fe02018-05-22 11:04:33 +02007238 /* Don't use the cache and don't try to store if we found the
7239 * Authorization header */
7240 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7241 if (val) {
7242 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7243 txn->flags |= TX_CACHE_IGNORE;
7244 continue;
7245 }
7246
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007247 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7248 if (!val)
7249 continue;
7250
7251 /* OK, right now we know we have a cache-control header at cur_ptr */
7252 cc_found = 1;
7253 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7254
7255 if (p1 >= cur_end) /* no more info */
7256 continue;
7257
7258 /* p1 is at the beginning of the value */
7259 p2 = p1;
7260 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7261 p2++;
7262
7263 /* we have a complete value between p1 and p2. We don't check the
7264 * values after max-age, max-stale nor min-fresh, we simply don't
7265 * use the cache when they're specified.
7266 */
7267 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7268 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7269 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7270 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7271 txn->flags |= TX_CACHE_IGNORE;
7272 continue;
7273 }
7274
7275 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7276 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7277 continue;
7278 }
7279 }
7280
7281 /* RFC7234#5.4:
7282 * When the Cache-Control header field is also present and
7283 * understood in a request, Pragma is ignored.
7284 * When the Cache-Control header field is not present in a
7285 * request, caches MUST consider the no-cache request
7286 * pragma-directive as having the same effect as if
7287 * "Cache-Control: no-cache" were present.
7288 */
7289 if (!cc_found && pragma_found)
7290 txn->flags |= TX_CACHE_IGNORE;
7291}
7292
7293/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007294 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007295 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007296void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007297{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007298 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007299 char *p1, *p2;
7300
7301 char *cur_ptr, *cur_end, *cur_next;
7302 int cur_idx;
7303
Christopher Faulet25a02f62018-10-24 12:00:25 +02007304
7305 if (IS_HTX_STRM(s))
7306 return htx_check_response_for_cacheability(s, rtr);
7307
Willy Tarreau12b32f22017-12-21 16:08:09 +01007308 if (txn->status < 200) {
7309 /* do not try to cache interim responses! */
7310 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007311 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007312 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007313
7314 /* Iterate through the headers.
7315 * we start with the start line.
7316 */
7317 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007318 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007319
7320 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7321 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007322 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007323
7324 cur_hdr = &txn->hdr_idx.v[cur_idx];
7325 cur_ptr = cur_next;
7326 cur_end = cur_ptr + cur_hdr->len;
7327 cur_next = cur_end + cur_hdr->cr + 1;
7328
7329 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007330 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007331 */
7332
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007333 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7334 if (val) {
7335 if ((cur_end - (cur_ptr + val) >= 8) &&
7336 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7337 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7338 return;
7339 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007340 }
7341
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007342 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7343 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344 continue;
7345
7346 /* OK, right now we know we have a cache-control header at cur_ptr */
7347
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007348 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007349
7350 if (p1 >= cur_end) /* no more info */
7351 continue;
7352
7353 /* p1 is at the beginning of the value */
7354 p2 = p1;
7355
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007356 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007357 p2++;
7358
7359 /* we have a complete value between p1 and p2 */
7360 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007361 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7362 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7363 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7364 continue;
7365 }
7366
Willy Tarreaua15645d2007-03-18 16:22:39 +01007367 /* we have something of the form no-cache="set-cookie" */
7368 if ((cur_end - p1 >= 21) &&
7369 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7370 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007371 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007372 continue;
7373 }
7374
7375 /* OK, so we know that either p2 points to the end of string or to a comma */
7376 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007377 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007378 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007379 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007380 return;
7381 }
7382
7383 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007384 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007385 continue;
7386 }
7387 }
7388}
7389
Willy Tarreau58f10d72006-12-04 02:26:12 +01007390
Willy Tarreaub2513902006-12-17 14:52:38 +01007391/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007392 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007393 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007394 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007395 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007396 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007397 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007398 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007399 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007400int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007401{
7402 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007403 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007404 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007405
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007406 if (!uri_auth)
7407 return 0;
7408
Cyril Bonté70be45d2010-10-12 00:14:35 +02007409 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007410 return 0;
7411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007412 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007413 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007414 return 0;
7415
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007416 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007417 return 0;
7418
Willy Tarreaub2513902006-12-17 14:52:38 +01007419 return 1;
7420}
7421
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007422/* Append the description of what is present in error snapshot <es> into <out>.
7423 * The description must be small enough to always fit in a trash. The output
7424 * buffer may be the trash so the trash must not be used inside this function.
7425 */
7426void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7427{
7428 chunk_appendf(&trash,
7429 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7430 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7431 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7432 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7433 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7434 es->ctx.http.m_flags, es->ctx.http.m_clen,
7435 es->ctx.http.m_blen, es->ctx.http.b_flags);
7436}
7437
Willy Tarreau4076a152009-04-02 15:18:36 +02007438/*
7439 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007440 * By default it tries to report the error position as msg->err_pos. However if
7441 * this one is not set, it will then report msg->next, which is the last known
7442 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007443 * displays buffers as a contiguous area starting at buf->p. The direction is
7444 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007445 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007446void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007447 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007448 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007449{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007450 union error_snapshot_ctx ctx;
7451 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007452
Willy Tarreauef3ca732018-09-07 15:47:35 +02007453 /* http-specific part now */
7454 ctx.http.sid = s->uniq_id;
7455 ctx.http.state = state;
7456 ctx.http.b_flags = msg->chn->flags;
7457 ctx.http.s_flags = s->flags;
7458 ctx.http.t_flags = s->txn->flags;
7459 ctx.http.m_flags = msg->flags;
7460 ctx.http.m_clen = msg->chunk_len;
7461 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007462
Willy Tarreauef3ca732018-09-07 15:47:35 +02007463 ofs = msg->chn->total - ci_data(msg->chn);
7464 if (ofs < 0)
7465 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007466
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007467 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007468 other_end, s->target,
7469 strm_sess(s), &msg->chn->buf,
7470 ofs, co_data(msg->chn),
7471 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7472 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007473}
Willy Tarreaub2513902006-12-17 14:52:38 +01007474
Willy Tarreau294c4732011-12-16 21:35:50 +01007475/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7476 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7477 * performed over the whole headers. Otherwise it must contain a valid header
7478 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7479 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7480 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7481 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007482 * -1. The value fetch stops at commas, so this function is suited for use with
7483 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007484 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007485 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007486unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007487 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007488 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007489{
Willy Tarreau294c4732011-12-16 21:35:50 +01007490 struct hdr_ctx local_ctx;
7491 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007492 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007493 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007494 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007495
Willy Tarreau294c4732011-12-16 21:35:50 +01007496 if (!ctx) {
7497 local_ctx.idx = 0;
7498 ctx = &local_ctx;
7499 }
7500
Willy Tarreaubce70882009-09-07 11:51:47 +02007501 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007502 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007503 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007504 occ--;
7505 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007506 *vptr = ctx->line + ctx->val;
7507 *vlen = ctx->vlen;
7508 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007509 }
7510 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007511 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007512 }
7513
7514 /* negative occurrence, we scan all the list then walk back */
7515 if (-occ > MAX_HDR_HISTORY)
7516 return 0;
7517
Willy Tarreau294c4732011-12-16 21:35:50 +01007518 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007519 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007520 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7521 len_hist[hist_ptr] = ctx->vlen;
7522 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007523 hist_ptr = 0;
7524 found++;
7525 }
7526 if (-occ > found)
7527 return 0;
7528 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007529 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7530 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7531 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007532 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007533 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007534 if (hist_ptr >= MAX_HDR_HISTORY)
7535 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007536 *vptr = ptr_hist[hist_ptr];
7537 *vlen = len_hist[hist_ptr];
7538 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007539}
7540
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007541/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7542 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7543 * performed over the whole headers. Otherwise it must contain a valid header
7544 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7545 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7546 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7547 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7548 * -1. This function differs from http_get_hdr() in that it only returns full
7549 * line header values and does not stop at commas.
7550 * The return value is 0 if nothing was found, or non-zero otherwise.
7551 */
7552unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7553 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007554 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007555{
7556 struct hdr_ctx local_ctx;
7557 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007558 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007559 unsigned int hist_ptr;
7560 int found;
7561
7562 if (!ctx) {
7563 local_ctx.idx = 0;
7564 ctx = &local_ctx;
7565 }
7566
7567 if (occ >= 0) {
7568 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007569 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007570 occ--;
7571 if (occ <= 0) {
7572 *vptr = ctx->line + ctx->val;
7573 *vlen = ctx->vlen;
7574 return 1;
7575 }
7576 }
7577 return 0;
7578 }
7579
7580 /* negative occurrence, we scan all the list then walk back */
7581 if (-occ > MAX_HDR_HISTORY)
7582 return 0;
7583
7584 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007585 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007586 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7587 len_hist[hist_ptr] = ctx->vlen;
7588 if (++hist_ptr >= MAX_HDR_HISTORY)
7589 hist_ptr = 0;
7590 found++;
7591 }
7592 if (-occ > found)
7593 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007594
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007595 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007596 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7597 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7598 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007599 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007600 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007601 if (hist_ptr >= MAX_HDR_HISTORY)
7602 hist_ptr -= MAX_HDR_HISTORY;
7603 *vptr = ptr_hist[hist_ptr];
7604 *vlen = len_hist[hist_ptr];
7605 return 1;
7606}
7607
Willy Tarreaubaaee002006-06-26 02:48:02 +02007608/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007609 * Print a debug line with a header. Always stop at the first CR or LF char,
7610 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7611 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007612 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007613void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007614{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007615 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007616 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007617
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007618 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007619 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007620 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007621 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 +02007622
7623 for (max = 0; start + max < end; max++)
7624 if (start[max] == '\r' || start[max] == '\n')
7625 break;
7626
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007627 UBOUND(max, trash.size - trash.data - 3);
7628 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7629 trash.area[trash.data++] = '\n';
7630 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007631}
7632
Willy Tarreaueee5b512015-04-03 23:46:31 +02007633
7634/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7635 * The hdr_idx is allocated as well. In case of allocation failure, everything
7636 * allocated is freed and NULL is returned. Otherwise the new transaction is
7637 * assigned to the stream and returned.
7638 */
7639struct http_txn *http_alloc_txn(struct stream *s)
7640{
7641 struct http_txn *txn = s->txn;
7642
7643 if (txn)
7644 return txn;
7645
Willy Tarreaubafbe012017-11-24 17:34:44 +01007646 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007647 if (!txn)
7648 return txn;
7649
7650 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007651 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007652 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007653 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007654 return NULL;
7655 }
7656
7657 s->txn = txn;
7658 return txn;
7659}
7660
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007661void http_txn_reset_req(struct http_txn *txn)
7662{
7663 txn->req.flags = 0;
7664 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7665 txn->req.next = 0;
7666 txn->req.chunk_len = 0LL;
7667 txn->req.body_len = 0LL;
7668 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7669}
7670
7671void http_txn_reset_res(struct http_txn *txn)
7672{
7673 txn->rsp.flags = 0;
7674 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7675 txn->rsp.next = 0;
7676 txn->rsp.chunk_len = 0LL;
7677 txn->rsp.body_len = 0LL;
7678 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7679}
7680
Willy Tarreau0937bc42009-12-22 15:03:09 +01007681/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007682 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007683 * the required fields are properly allocated and that we only need to (re)init
7684 * them. This should be used before processing any new request.
7685 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007686void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007687{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007688 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007689 struct proxy *fe = strm_fe(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02007690 struct conn_stream *cs = objt_cs(s->si[0].end);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007691
Christopher Fauletf2824e62018-10-01 12:12:37 +02007692 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
7693 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
7694 : 0);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007695 txn->status = -1;
7696
Willy Tarreauf64d1412010-10-07 20:06:11 +02007697 txn->cookie_first_date = 0;
7698 txn->cookie_last_date = 0;
7699
Willy Tarreaueee5b512015-04-03 23:46:31 +02007700 txn->srv_cookie = NULL;
7701 txn->cli_cookie = NULL;
7702 txn->uri = NULL;
7703
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007704 http_txn_reset_req(txn);
7705 http_txn_reset_res(txn);
7706
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007707 txn->req.chn = &s->req;
7708 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007709
7710 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007711
7712 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7713 if (fe->options2 & PR_O2_REQBUG_OK)
7714 txn->req.err_pos = -1; /* let buggy requests pass */
7715
Willy Tarreau0937bc42009-12-22 15:03:09 +01007716 if (txn->hdr_idx.v)
7717 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007718
7719 vars_init(&s->vars_txn, SCOPE_TXN);
7720 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007721}
7722
7723/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007724void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007725{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007726 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007727 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007728
7729 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007730 pool_free(pool_head_requri, txn->uri);
7731 pool_free(pool_head_capture, txn->cli_cookie);
7732 pool_free(pool_head_capture, txn->srv_cookie);
7733 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007734
William Lallemanda73203e2012-03-12 12:48:57 +01007735 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007736 txn->uri = NULL;
7737 txn->srv_cookie = NULL;
7738 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007739
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007740 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007741 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007742 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007743 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007744 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007745 }
7746
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007747 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007748 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007749 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007750 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007751 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007752 }
7753
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007754 if (!LIST_ISEMPTY(&s->vars_txn.head))
7755 vars_prune(&s->vars_txn, s->sess, s);
7756 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7757 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007758}
7759
7760/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007761void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007762{
7763 http_end_txn(s);
7764 http_init_txn(s);
7765
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007766 /* reinitialise the current rule list pointer to NULL. We are sure that
7767 * any rulelist match the NULL pointer.
7768 */
7769 s->current_rule_list = NULL;
7770
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007771 s->be = strm_fe(s);
7772 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007773 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007774 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007775 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007776 /* re-init store persistence */
7777 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007778 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007779
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007780 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007781
Willy Tarreau739cfba2010-01-25 23:11:14 +01007782 /* We must trim any excess data from the response buffer, because we
7783 * may have blocked an invalid response from a server that we don't
Joseph Herlant5ba80252018-11-15 09:25:36 -08007784 * want to accidently forward once we disable the analysers, nor do
Willy Tarreau739cfba2010-01-25 23:11:14 +01007785 * we want those data to come along with next response. A typical
7786 * example of such data would be from a buggy server responding to
7787 * a HEAD with some data, or sending more than the advertised
7788 * content-length.
7789 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007790 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007791 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007792
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007793 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007794 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007795
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007796 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007797 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007798
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007799 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007800 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007801
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007802 s->req.rex = TICK_ETERNITY;
7803 s->req.wex = TICK_ETERNITY;
7804 s->req.analyse_exp = TICK_ETERNITY;
7805 s->res.rex = TICK_ETERNITY;
7806 s->res.wex = TICK_ETERNITY;
7807 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007808 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007809}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007810
Willy Tarreau79e57332018-10-02 16:01:16 +02007811/* This function executes one of the set-{method,path,query,uri} actions. It
7812 * takes the string from the variable 'replace' with length 'len', then modifies
7813 * the relevant part of the request line accordingly. Then it updates various
7814 * pointers to the next elements which were moved, and the total buffer length.
7815 * It finds the action to be performed in p[2], previously filled by function
7816 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7817 * error, though this can be revisited when this code is finally exploited.
7818 *
7819 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7820 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007821 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007822 * In query string case, the mark question '?' must be set at the start of the
7823 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007824 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007825int http_replace_req_line(int action, const char *replace, int len,
7826 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007827{
Willy Tarreau79e57332018-10-02 16:01:16 +02007828 struct http_txn *txn = s->txn;
7829 char *cur_ptr, *cur_end;
7830 int offset = 0;
7831 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007832
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007833 if (IS_HTX_STRM(s))
7834 return htx_req_replace_stline(action, replace, len, px, s);
7835
Willy Tarreau79e57332018-10-02 16:01:16 +02007836 switch (action) {
7837 case 0: // method
7838 cur_ptr = ci_head(&s->req);
7839 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007840
Willy Tarreau79e57332018-10-02 16:01:16 +02007841 /* adjust req line offsets and lengths */
7842 delta = len - offset - (cur_end - cur_ptr);
7843 txn->req.sl.rq.m_l += delta;
7844 txn->req.sl.rq.u += delta;
7845 txn->req.sl.rq.v += delta;
7846 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007847
Willy Tarreau79e57332018-10-02 16:01:16 +02007848 case 1: // path
7849 cur_ptr = http_txn_get_path(txn);
7850 if (!cur_ptr)
7851 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007852
Willy Tarreau79e57332018-10-02 16:01:16 +02007853 cur_end = cur_ptr;
7854 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7855 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007856
Willy Tarreau79e57332018-10-02 16:01:16 +02007857 /* adjust req line offsets and lengths */
7858 delta = len - offset - (cur_end - cur_ptr);
7859 txn->req.sl.rq.u_l += delta;
7860 txn->req.sl.rq.v += delta;
7861 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007862
Willy Tarreau79e57332018-10-02 16:01:16 +02007863 case 2: // query
7864 offset = 1;
7865 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7866 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7867 while (cur_ptr < cur_end && *cur_ptr != '?')
7868 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007869
Willy Tarreau79e57332018-10-02 16:01:16 +02007870 /* skip the question mark or indicate that we must insert it
7871 * (but only if the format string is not empty then).
7872 */
7873 if (cur_ptr < cur_end)
7874 cur_ptr++;
7875 else if (len > 1)
7876 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007877
Willy Tarreau79e57332018-10-02 16:01:16 +02007878 /* adjust req line offsets and lengths */
7879 delta = len - offset - (cur_end - cur_ptr);
7880 txn->req.sl.rq.u_l += delta;
7881 txn->req.sl.rq.v += delta;
7882 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007883
Willy Tarreau79e57332018-10-02 16:01:16 +02007884 case 3: // uri
7885 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7886 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007887
Willy Tarreau79e57332018-10-02 16:01:16 +02007888 /* adjust req line offsets and lengths */
7889 delta = len - offset - (cur_end - cur_ptr);
7890 txn->req.sl.rq.u_l += delta;
7891 txn->req.sl.rq.v += delta;
7892 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007893
Willy Tarreau79e57332018-10-02 16:01:16 +02007894 default:
7895 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007896 }
7897
Willy Tarreau79e57332018-10-02 16:01:16 +02007898 /* commit changes and adjust end of message */
7899 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7900 txn->req.sl.rq.l += delta;
7901 txn->hdr_idx.v[0].len += delta;
7902 http_msg_move_end(&txn->req, delta);
7903 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007904}
7905
Willy Tarreau79e57332018-10-02 16:01:16 +02007906/* This function replace the HTTP status code and the associated message. The
7907 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007908 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007909void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007910{
Willy Tarreau79e57332018-10-02 16:01:16 +02007911 struct http_txn *txn = s->txn;
7912 char *cur_ptr, *cur_end;
7913 int delta;
7914 char *res;
7915 int c_l;
7916 const char *msg = reason;
7917 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007918
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007919 if (IS_HTX_STRM(s))
7920 return htx_res_set_status(status, reason, s);
7921
Willy Tarreau79e57332018-10-02 16:01:16 +02007922 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007923
Willy Tarreau79e57332018-10-02 16:01:16 +02007924 res = ultoa_o(status, trash.area, trash.size);
7925 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007926
Willy Tarreau79e57332018-10-02 16:01:16 +02007927 trash.area[c_l] = ' ';
7928 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007929
Willy Tarreau79e57332018-10-02 16:01:16 +02007930 /* Do we have a custom reason format string? */
7931 if (msg == NULL)
7932 msg = http_get_reason(status);
7933 msg_len = strlen(msg);
7934 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7935 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007936
Willy Tarreau79e57332018-10-02 16:01:16 +02007937 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7938 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007939
Willy Tarreau79e57332018-10-02 16:01:16 +02007940 /* commit changes and adjust message */
7941 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7942 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007943
Willy Tarreau79e57332018-10-02 16:01:16 +02007944 /* adjust res line offsets and lengths */
7945 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7946 txn->rsp.sl.st.c_l = c_l;
7947 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007948
Willy Tarreau79e57332018-10-02 16:01:16 +02007949 delta = trash.data - (cur_end - cur_ptr);
7950 txn->rsp.sl.st.l += delta;
7951 txn->hdr_idx.v[0].len += delta;
7952 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007953}
7954
Willy Tarreau8797c062007-05-07 00:55:35 +02007955__attribute__((constructor))
7956static void __http_protocol_init(void)
7957{
Willy Tarreau8797c062007-05-07 00:55:35 +02007958}
7959
7960
Willy Tarreau58f10d72006-12-04 02:26:12 +01007961/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007962 * Local variables:
7963 * c-indent-level: 8
7964 * c-basic-offset: 8
7965 * End:
7966 */