blob: 0f71e83ed40219f326a9afb53562465551d13c53 [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
470 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200471 trash.data = strlen(HTTP_302);
472 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100473
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200474 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100475
Willy Tarreauefb453c2008-10-26 20:49:47 +0100476 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200477 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100478 return;
479
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100480 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100481 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200482 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
483 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100484 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100485
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200486 /* 3: add the request URI. Since it was already forwarded, we need
487 * to temporarily rewind the buffer.
488 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200489 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200490 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200491
Willy Tarreau6b952c82018-09-10 17:45:34 +0200492 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200493 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 +0200494
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200495 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200496
Willy Tarreauefb453c2008-10-26 20:49:47 +0100497 if (!path)
498 return;
499
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100501 return;
502
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 memcpy(trash.area + trash.data, path, len);
504 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100505
506 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200507 memcpy(trash.area + trash.data,
508 "\r\nProxy-Connection: close\r\n\r\n", 29);
509 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100510 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200511 memcpy(trash.area + trash.data,
512 "\r\nConnection: close\r\n\r\n", 23);
513 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100514 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100515
516 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200517 si_shutr(si);
518 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100519 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100520 si->state = SI_ST_CLO;
521
522 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100523 txn->status = 302;
524 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100525
526 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100527 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500528 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100529}
530
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100531/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100532 * that the server side is closed. Note that err_type is actually a
533 * bitmask, where almost only aborts may be cumulated with other
534 * values. We consider that aborted operations are more important
535 * than timeouts or errors due to the fact that nobody else in the
536 * logs might explain incomplete retries. All others should avoid
537 * being cumulated. It should normally not be possible to have multiple
538 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100539 * Note that connection errors appearing on the second request of a keep-alive
540 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100541 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200542void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100543{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100544 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100545
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200546 /* set s->txn->status for http_error_message(s) */
547 s->txn->status = 503;
548
Willy Tarreauefb453c2008-10-26 20:49:47 +0100549 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200550 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100551 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100552 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200553 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100554 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200555 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100556 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200557 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100558 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100559 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200560 http_server_error(s, si, SF_ERR_SRVCL, 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_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200563 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100564 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200565 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100566 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200567 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100568 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200569 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200570 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200571 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100572 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200573 http_error_message(s));
574 else { /* SI_ET_CONN_OTHER and others */
575 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200576 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100577 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200578 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100579}
580
Willy Tarreau42250582007-04-01 01:30:43 +0200581extern const char sess_term_cond[8];
582extern const char sess_fin_state[8];
583extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +0100584struct pool_head *pool_head_http_txn;
585struct pool_head *pool_head_requri;
586struct pool_head *pool_head_capture = NULL;
587struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100588
Willy Tarreau117f59e2007-03-04 18:17:17 +0100589/*
590 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200591 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100592 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200593void http_capture_headers(char *som, struct hdr_idx *idx,
594 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100595{
596 char *eol, *sol, *col, *sov;
597 int cur_idx;
598 struct cap_hdr *h;
599 int len;
600
601 sol = som + hdr_idx_first_pos(idx);
602 cur_idx = hdr_idx_first_idx(idx);
603
604 while (cur_idx) {
605 eol = sol + idx->v[cur_idx].len;
606
607 col = sol;
608 while (col < eol && *col != ':')
609 col++;
610
611 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100612 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100613 sov++;
614
615 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200616 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100617 (strncasecmp(sol, h->name, h->namelen) == 0)) {
618 if (cap[h->index] == NULL)
619 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100620 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100621
622 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100623 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100624 continue;
625 }
626
627 len = eol - sov;
628 if (len > h->len)
629 len = h->len;
630
631 memcpy(cap[h->index], sov, len);
632 cap[h->index][len]=0;
633 }
634 }
635 sol = eol + idx->v[cur_idx].cr + 1;
636 cur_idx = idx->v[cur_idx].next;
637 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100638}
639
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200640/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
641 * conversion succeeded, 0 in case of error. If the request was already 1.X,
642 * nothing is done and 1 is returned.
643 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200644int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200645{
646 int delta;
647 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100648 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200649
650 if (msg->sl.rq.v_l != 0)
651 return 1;
652
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300653 /* RFC 1945 allows only GET for HTTP/0.9 requests */
654 if (txn->meth != HTTP_METH_GET)
655 return 0;
656
Willy Tarreauf37954d2018-06-15 18:31:02 +0200657 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200658
659 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300660 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
661 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200662 }
663 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200664 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100665 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200666 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200667 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200668 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200669 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200670 NULL, NULL);
671 if (unlikely(!cur_end))
672 return 0;
673
674 /* we have a full HTTP/1.0 request now and we know that
675 * we have either a CR or an LF at <ptr>.
676 */
677 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
678 return 1;
679}
680
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100681/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100682 * and "keep-alive" values. If we already know that some headers may safely
683 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100684 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
685 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100686 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100687 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
688 * found, and TX_CON_*_SET is adjusted depending on what is left so only
689 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100690 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100691 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100692void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100693{
Willy Tarreau5b154472009-12-21 20:11:07 +0100694 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100695 const char *hdr_val = "Connection";
696 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100697
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100698 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100699 return;
700
Willy Tarreau88d349d2010-01-25 12:15:43 +0100701 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
702 hdr_val = "Proxy-Connection";
703 hdr_len = 16;
704 }
705
Willy Tarreau5b154472009-12-21 20:11:07 +0100706 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100707 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200708 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100709 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
710 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100711 if (to_del & 2)
712 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100713 else
714 txn->flags |= TX_CON_KAL_SET;
715 }
716 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
717 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100718 if (to_del & 1)
719 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100720 else
721 txn->flags |= TX_CON_CLO_SET;
722 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100723 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
724 txn->flags |= TX_HDR_CONN_UPG;
725 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100726 }
727
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100728 txn->flags |= TX_HDR_CONN_PRS;
729 return;
730}
Willy Tarreau5b154472009-12-21 20:11:07 +0100731
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100732/* Apply desired changes on the Connection: header. Values may be removed and/or
733 * added depending on the <wanted> flags, which are exclusively composed of
734 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
735 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
736 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100737void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100738{
739 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100740 const char *hdr_val = "Connection";
741 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100742
743 ctx.idx = 0;
744
Willy Tarreau88d349d2010-01-25 12:15:43 +0100745
746 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
747 hdr_val = "Proxy-Connection";
748 hdr_len = 16;
749 }
750
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100751 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200752 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100753 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
754 if (wanted & TX_CON_KAL_SET)
755 txn->flags |= TX_CON_KAL_SET;
756 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100757 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100758 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100759 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
760 if (wanted & TX_CON_CLO_SET)
761 txn->flags |= TX_CON_CLO_SET;
762 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100763 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100764 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100765 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100766
767 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
768 return;
769
770 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
771 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100772 hdr_val = "Connection: close";
773 hdr_len = 17;
774 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
775 hdr_val = "Proxy-Connection: close";
776 hdr_len = 23;
777 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100778 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100779 }
780
781 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
782 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100783 hdr_val = "Connection: keep-alive";
784 hdr_len = 22;
785 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
786 hdr_val = "Proxy-Connection: keep-alive";
787 hdr_len = 28;
788 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100789 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100790 }
791 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100792}
793
Willy Tarreau87b09662015-04-03 00:22:06 +0200794void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200795{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200796 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200797 int tmp = TX_CON_WANT_KAL;
798
Christopher Fauletf2824e62018-10-01 12:12:37 +0200799 if (IS_HTX_STRM(s))
Christopher Faulet0f226952018-10-22 09:29:56 +0200800 return htx_adjust_conn_mode(s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200801
Christopher Faulet315b39c2018-09-21 16:26:19 +0200802 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
803 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
804 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200805
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200806 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200807 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200808 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200809
Christopher Faulet315b39c2018-09-21 16:26:19 +0200810 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
811 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200812 tmp = TX_CON_WANT_CLO;
813
814 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
815 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
816
817 if (!(txn->flags & TX_HDR_CONN_PRS) &&
818 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
819 /* parse the Connection header and possibly clean it */
820 int to_del = 0;
821 if ((msg->flags & HTTP_MSGF_VER_11) ||
822 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200823 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200824 to_del |= 2; /* remove "keep-alive" */
825 if (!(msg->flags & HTTP_MSGF_VER_11))
826 to_del |= 1; /* remove "close" */
827 http_parse_connection_header(txn, msg, to_del);
828 }
829
830 /* check if client or config asks for explicit close in KAL/SCL */
831 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
832 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
833 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
834 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
835 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200836 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200837 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
838}
William Lallemand82fe75c2012-10-23 10:25:10 +0200839
Willy Tarreaud787e662009-07-07 10:14:51 +0200840/* This stream analyser waits for a complete HTTP request. It returns 1 if the
841 * processing can continue on next analysers, or zero if it either needs more
842 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100843 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200844 * when it has nothing left to do, and may remove any analyser when it wants to
845 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100846 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200847int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100848{
Willy Tarreau59234e92008-11-30 23:51:27 +0100849 /*
850 * We will parse the partial (or complete) lines.
851 * We will check the request syntax, and also join multi-line
852 * headers. An index of all the lines will be elaborated while
853 * parsing.
854 *
855 * For the parsing, we use a 28 states FSM.
856 *
857 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200858 * ci_head(req) = beginning of request
859 * ci_head(req) + msg->eoh = end of processed headers / start of current one
860 * ci_tail(req) = end of input data
861 * msg->eol = end of current header or line (LF or CRLF)
862 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200863 *
864 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200865 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200866 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
867 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100868 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100869
Willy Tarreau59234e92008-11-30 23:51:27 +0100870 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200871 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200872 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100873 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200874 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100875
Christopher Faulete0768eb2018-10-03 16:38:02 +0200876 if (IS_HTX_STRM(s))
877 return htx_wait_for_request(s, req, an_bit);
878
Christopher Faulet45073512018-07-20 10:16:29 +0200879 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 +0100880 now_ms, __FUNCTION__,
881 s,
882 req,
883 req->rex, req->wex,
884 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200885 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100886 req->analysers);
887
Willy Tarreau52a0c602009-08-16 22:45:38 +0200888 /* we're speaking HTTP here, so let's speak HTTP to the client */
889 s->srv_error = http_return_srv_error;
890
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300891 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200892 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300893 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
894
Willy Tarreau83e3af02009-12-28 17:39:57 +0100895 /* There's a protected area at the end of the buffer for rewriting
896 * purposes. We don't want to start to parse the request if the
897 * protected area is affected, because we may have to move processed
898 * data later, which is much more complicated.
899 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200900 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200901 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100902 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200903 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100904 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100905 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200906 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200907 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100908 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100909 return 0;
910 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200911 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200912 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200913 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100914 }
915
Willy Tarreauf37954d2018-06-15 18:31:02 +0200916 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100917 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100918 }
919
Willy Tarreau59234e92008-11-30 23:51:27 +0100920 /* 1: we might have to print this header in debug mode */
921 if (unlikely((global.mode & MODE_DEBUG) &&
922 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200923 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100924 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100925
Willy Tarreauf37954d2018-06-15 18:31:02 +0200926 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200927 /* this is a bit complex : in case of error on the request line,
928 * we know that rq.l is still zero, so we display only the part
929 * up to the end of the line (truncated by debug_hdr).
930 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200931 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100932 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100933
Willy Tarreau59234e92008-11-30 23:51:27 +0100934 sol += hdr_idx_first_pos(&txn->hdr_idx);
935 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100936
Willy Tarreau59234e92008-11-30 23:51:27 +0100937 while (cur_idx) {
938 eol = sol + txn->hdr_idx.v[cur_idx].len;
939 debug_hdr("clihdr", s, sol, eol);
940 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
941 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100942 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100943 }
944
Willy Tarreau58f10d72006-12-04 02:26:12 +0100945
Willy Tarreau59234e92008-11-30 23:51:27 +0100946 /*
947 * Now we quickly check if we have found a full valid request.
948 * If not so, we check the FD and buffer states before leaving.
949 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100950 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100951 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200952 * on a keep-alive stream, if we encounter and error, close, t/o,
953 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100954 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200955 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200956 * Last, we may increase some tracked counters' http request errors on
957 * the cases that are deliberately the client's fault. For instance,
958 * a timeout or connection reset is not counted as an error. However
959 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100960 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100961
Willy Tarreau655dce92009-11-08 13:10:58 +0100962 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100963 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100964 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100965 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100966 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200967 stream_inc_http_req_ctr(s);
968 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200969 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100970 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100971 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100972
Willy Tarreau59234e92008-11-30 23:51:27 +0100973 /* 1: Since we are in header mode, if there's no space
974 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200975 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100976 * must terminate it now.
977 */
Willy Tarreau23752332018-06-15 14:54:53 +0200978 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100979 /* FIXME: check if URI is set and return Status
980 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100981 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200982 stream_inc_http_req_ctr(s);
983 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200984 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200985 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200986 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100987 goto return_bad_req;
988 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100989
Willy Tarreau59234e92008-11-30 23:51:27 +0100990 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200991 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200992 if (!(s->flags & SF_ERR_MASK))
993 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100994
Willy Tarreaufcffa692010-01-10 14:21:19 +0100995 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100996 goto failed_keep_alive;
997
Willy Tarreau0f228a02015-05-01 15:37:53 +0200998 if (sess->fe->options & PR_O_IGNORE_PRB)
999 goto failed_keep_alive;
1000
Willy Tarreau59234e92008-11-30 23:51:27 +01001001 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001002 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001003 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001004 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001005 }
1006
Willy Tarreaudc979f22012-12-04 10:39:01 +01001007 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001008 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001009 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001010 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001011 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001012 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001013 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001014 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001015 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001016 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001017
Willy Tarreaue7dff022015-04-03 01:14:29 +02001018 if (!(s->flags & SF_FINST_MASK))
1019 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001020 return 0;
1021 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001022
Willy Tarreau59234e92008-11-30 23:51:27 +01001023 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001024 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001025 if (!(s->flags & SF_ERR_MASK))
1026 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001027
Willy Tarreaufcffa692010-01-10 14:21:19 +01001028 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001029 goto failed_keep_alive;
1030
Willy Tarreau0f228a02015-05-01 15:37:53 +02001031 if (sess->fe->options & PR_O_IGNORE_PRB)
1032 goto failed_keep_alive;
1033
Willy Tarreau59234e92008-11-30 23:51:27 +01001034 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001035 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001036 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001037 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001038 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001039 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001040 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001041 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001042 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001043 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001044
Willy Tarreau87b09662015-04-03 00:22:06 +02001045 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001046 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001047 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001048 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001049 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001050
Willy Tarreaue7dff022015-04-03 01:14:29 +02001051 if (!(s->flags & SF_FINST_MASK))
1052 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001053 return 0;
1054 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001055
Willy Tarreau59234e92008-11-30 23:51:27 +01001056 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001057 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001058 if (!(s->flags & SF_ERR_MASK))
1059 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001060
Willy Tarreaufcffa692010-01-10 14:21:19 +01001061 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001062 goto failed_keep_alive;
1063
Willy Tarreau0f228a02015-05-01 15:37:53 +02001064 if (sess->fe->options & PR_O_IGNORE_PRB)
1065 goto failed_keep_alive;
1066
Willy Tarreau4076a152009-04-02 15:18:36 +02001067 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001068 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001069 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001070 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001071 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001072 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001073 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001074 stream_inc_http_err_ctr(s);
1075 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001076 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001077 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001078 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001079 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001080
Willy Tarreaue7dff022015-04-03 01:14:29 +02001081 if (!(s->flags & SF_FINST_MASK))
1082 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001083 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001084 }
1085
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001086 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001087 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001088 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001089#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001090 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001091 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001092 /* We need more data, we have to re-enable quick-ack in case we
1093 * previously disabled it, otherwise we might cause the client
1094 * to delay next data.
1095 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001096 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001097 }
1098#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001099
Willy Tarreaufcffa692010-01-10 14:21:19 +01001100 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1101 /* If the client starts to talk, let's fall back to
1102 * request timeout processing.
1103 */
1104 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001105 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001106 }
1107
Willy Tarreau59234e92008-11-30 23:51:27 +01001108 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001109 if (!tick_isset(req->analyse_exp)) {
1110 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1111 (txn->flags & TX_WAIT_NEXT_RQ) &&
1112 tick_isset(s->be->timeout.httpka))
1113 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1114 else
1115 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1116 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001117
Willy Tarreau59234e92008-11-30 23:51:27 +01001118 /* we're not ready yet */
1119 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001120
1121 failed_keep_alive:
1122 /* Here we process low-level errors for keep-alive requests. In
1123 * short, if the request is not the first one and it experiences
1124 * a timeout, read error or shutdown, we just silently close so
1125 * that the client can try again.
1126 */
1127 txn->status = 0;
1128 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001129 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001130 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001131 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001132 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001133 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001134 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001135 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001136
Willy Tarreaud787e662009-07-07 10:14:51 +02001137 /* OK now we have a complete HTTP request with indexed headers. Let's
1138 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001139 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001140 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001141 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001142 * byte after the last LF. msg->sov points to the first byte of data.
1143 * msg->eol cannot be trusted because it may have been left uninitialized
1144 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001145 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001146
Willy Tarreau87b09662015-04-03 00:22:06 +02001147 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001148 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001149
Willy Tarreaub16a5742010-01-10 14:46:16 +01001150 if (txn->flags & TX_WAIT_NEXT_RQ) {
1151 /* kill the pending keep-alive timeout */
1152 txn->flags &= ~TX_WAIT_NEXT_RQ;
1153 req->analyse_exp = TICK_ETERNITY;
1154 }
1155
1156
Willy Tarreaud787e662009-07-07 10:14:51 +02001157 /* Maybe we found in invalid header name while we were configured not
1158 * to block on that, so we have to capture it now.
1159 */
1160 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001161 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001162
Willy Tarreau59234e92008-11-30 23:51:27 +01001163 /*
1164 * 1: identify the method
1165 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001166 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001167
1168 /* we can make use of server redirect on GET and HEAD */
1169 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001170 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001171 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001172 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001173 /* PRI is reserved for the HTTP/2 preface */
1174 msg->err_pos = 0;
1175 goto return_bad_req;
1176 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001177
Willy Tarreau59234e92008-11-30 23:51:27 +01001178 /*
1179 * 2: check if the URI matches the monitor_uri.
1180 * We have to do this for every request which gets in, because
1181 * the monitor-uri is defined by the frontend.
1182 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001183 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1184 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001185 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001186 sess->fe->monitor_uri,
1187 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001188 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001189 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001190 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001191 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001192
Willy Tarreaue7dff022015-04-03 01:14:29 +02001193 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001194 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001195
Willy Tarreau59234e92008-11-30 23:51:27 +01001196 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001197 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001198 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001199
Willy Tarreau59234e92008-11-30 23:51:27 +01001200 ret = acl_pass(ret);
1201 if (cond->pol == ACL_COND_UNLESS)
1202 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001203
Willy Tarreau59234e92008-11-30 23:51:27 +01001204 if (ret) {
1205 /* we fail this request, let's return 503 service unavail */
1206 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001207 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001208 if (!(s->flags & SF_ERR_MASK))
1209 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001210 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001211 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001212 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001213
Willy Tarreau59234e92008-11-30 23:51:27 +01001214 /* nothing to fail, let's reply normaly */
1215 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001216 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001217 if (!(s->flags & SF_ERR_MASK))
1218 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001219 goto return_prx_cond;
1220 }
1221
1222 /*
1223 * 3: Maybe we have to copy the original REQURI for the logs ?
1224 * Note: we cannot log anymore if the request has been
1225 * classified as invalid.
1226 */
1227 if (unlikely(s->logs.logwait & LW_REQ)) {
1228 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001229 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001230 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001231
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001232 if (urilen >= global.tune.requri_len )
1233 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001234 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001235 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001237 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001238 s->do_log(s);
1239 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001240 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001242 }
Willy Tarreau06619262006-12-17 08:37:22 +01001243
Willy Tarreau91852eb2015-05-01 13:26:00 +02001244 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1245 * exactly one digit "." one digit. This check may be disabled using
1246 * option accept-invalid-http-request.
1247 */
1248 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1249 if (msg->sl.rq.v_l != 8) {
1250 msg->err_pos = msg->sl.rq.v;
1251 goto return_bad_req;
1252 }
1253
Willy Tarreauf37954d2018-06-15 18:31:02 +02001254 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1255 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1256 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1257 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001258 msg->err_pos = msg->sl.rq.v + 4;
1259 goto return_bad_req;
1260 }
1261 }
Willy Tarreau13317662015-05-01 13:47:08 +02001262 else {
1263 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1264 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1265 goto return_bad_req;
1266 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001267
Willy Tarreau5b154472009-12-21 20:11:07 +01001268 /* ... and check if the request is HTTP/1.1 or above */
1269 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001270 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1271 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1272 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001273 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001274
1275 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001276 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 +01001277
Willy Tarreau88d349d2010-01-25 12:15:43 +01001278 /* if the frontend has "option http-use-proxy-header", we'll check if
1279 * we have what looks like a proxied connection instead of a connection,
1280 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1281 * Note that this is *not* RFC-compliant, however browsers and proxies
1282 * happen to do that despite being non-standard :-(
1283 * We consider that a request not beginning with either '/' or '*' is
1284 * a proxied connection, which covers both "scheme://location" and
1285 * CONNECT ip:port.
1286 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001287 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001288 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001289 txn->flags |= TX_USE_PX_CONN;
1290
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001291 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001292 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001293
Willy Tarreau59234e92008-11-30 23:51:27 +01001294 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001295 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001296 http_capture_headers(ci_head(req), &txn->hdr_idx,
1297 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001298
Willy Tarreau557f1992015-05-01 10:05:17 +02001299 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1300 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001301 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001302 * The length of a message body is determined by one of the following
1303 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001304 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001305 * 1. Any response to a HEAD request and any response with a 1xx
1306 * (Informational), 204 (No Content), or 304 (Not Modified) status
1307 * code is always terminated by the first empty line after the
1308 * header fields, regardless of the header fields present in the
1309 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001310 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001311 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1312 * the connection will become a tunnel immediately after the empty
1313 * line that concludes the header fields. A client MUST ignore any
1314 * Content-Length or Transfer-Encoding header fields received in
1315 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001316 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001317 * 3. If a Transfer-Encoding header field is present and the chunked
1318 * transfer coding (Section 4.1) is the final encoding, the message
1319 * body length is determined by reading and decoding the chunked
1320 * data until the transfer coding indicates the data is complete.
1321 *
1322 * If a Transfer-Encoding header field is present in a response and
1323 * the chunked transfer coding is not the final encoding, the
1324 * message body length is determined by reading the connection until
1325 * it is closed by the server. If a Transfer-Encoding header field
1326 * is present in a request and the chunked transfer coding is not
1327 * the final encoding, the message body length cannot be determined
1328 * reliably; the server MUST respond with the 400 (Bad Request)
1329 * status code and then close the connection.
1330 *
1331 * If a message is received with both a Transfer-Encoding and a
1332 * Content-Length header field, the Transfer-Encoding overrides the
1333 * Content-Length. Such a message might indicate an attempt to
1334 * perform request smuggling (Section 9.5) or response splitting
1335 * (Section 9.4) and ought to be handled as an error. A sender MUST
1336 * remove the received Content-Length field prior to forwarding such
1337 * a message downstream.
1338 *
1339 * 4. If a message is received without Transfer-Encoding and with
1340 * either multiple Content-Length header fields having differing
1341 * field-values or a single Content-Length header field having an
1342 * invalid value, then the message framing is invalid and the
1343 * recipient MUST treat it as an unrecoverable error. If this is a
1344 * request message, the server MUST respond with a 400 (Bad Request)
1345 * status code and then close the connection. If this is a response
1346 * message received by a proxy, the proxy MUST close the connection
1347 * to the server, discard the received response, and send a 502 (Bad
1348 * Gateway) response to the client. If this is a response message
1349 * received by a user agent, the user agent MUST close the
1350 * connection to the server and discard the received response.
1351 *
1352 * 5. If a valid Content-Length header field is present without
1353 * Transfer-Encoding, its decimal value defines the expected message
1354 * body length in octets. If the sender closes the connection or
1355 * the recipient times out before the indicated number of octets are
1356 * received, the recipient MUST consider the message to be
1357 * incomplete and close the connection.
1358 *
1359 * 6. If this is a request message and none of the above are true, then
1360 * the message body length is zero (no message body is present).
1361 *
1362 * 7. Otherwise, this is a response message without a declared message
1363 * body length, so the message body length is determined by the
1364 * number of octets received prior to the server closing the
1365 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001366 */
1367
Willy Tarreau32b47f42009-10-18 20:55:02 +02001368 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001369 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001370 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001371 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001372 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001373 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001374 /* chunked not last, return badreq */
1375 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001376 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001377 }
1378
Willy Tarreau1c913912015-04-30 10:57:51 +02001379 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001380 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001381 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001382 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001383 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1384 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001385 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001386 signed long long cl;
1387
Willy Tarreauad14f752011-09-02 20:33:27 +02001388 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001389 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001390 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001391 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001392
Willy Tarreauad14f752011-09-02 20:33:27 +02001393 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001394 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001395 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001396 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001397
Willy Tarreauad14f752011-09-02 20:33:27 +02001398 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001399 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001400 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001401 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001402
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001403 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001404 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001405 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001406 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001407
Christopher Fauletbe821b92017-03-30 11:21:53 +02001408 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001409 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001410 }
1411
Willy Tarreau34dfc602015-05-01 10:09:49 +02001412 /* even bodyless requests have a known length */
1413 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001414
Willy Tarreau179085c2014-04-28 16:48:56 +02001415 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1416 * only change if both the request and the config reference something else.
1417 * Option httpclose by itself sets tunnel mode where headers are mangled.
1418 * However, if another mode is set, it will affect it (eg: server-close/
1419 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1420 * if FE and BE have the same settings (common). The method consists in
1421 * checking if options changed between the two calls (implying that either
1422 * one is non-null, or one of them is non-null and we are there for the first
1423 * time.
1424 */
1425 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001426 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001427 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001428
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001429 /* we may have to wait for the request's body */
1430 if ((s->be->options & PR_O_WREQ_BODY) &&
1431 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1432 req->analysers |= AN_REQ_HTTP_BODY;
1433
Willy Tarreau83ece462017-12-21 15:13:09 +01001434 /*
1435 * RFC7234#4:
1436 * A cache MUST write through requests with methods
1437 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1438 * the origin server; i.e., a cache is not allowed
1439 * to generate a reply to such a request before
1440 * having forwarded the request and having received
1441 * a corresponding response.
1442 *
1443 * RFC7231#4.2.1:
1444 * Of the request methods defined by this
1445 * specification, the GET, HEAD, OPTIONS, and TRACE
1446 * methods are defined to be safe.
1447 */
1448 if (likely(txn->meth == HTTP_METH_GET ||
1449 txn->meth == HTTP_METH_HEAD ||
1450 txn->meth == HTTP_METH_OPTIONS ||
1451 txn->meth == HTTP_METH_TRACE))
1452 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1453
Willy Tarreaud787e662009-07-07 10:14:51 +02001454 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001455 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001456 req->analyse_exp = TICK_ETERNITY;
1457 return 1;
1458
1459 return_bad_req:
1460 /* We centralize bad requests processing here */
1461 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1462 /* we detected a parsing error. We want to archive this request
1463 * in the dedicated proxy area for later troubleshooting.
1464 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001465 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001466 }
1467
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001468 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001469 txn->req.msg_state = HTTP_MSG_ERROR;
1470 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001471 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001472
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001473 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001474 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001475 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001476
1477 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001478 if (!(s->flags & SF_ERR_MASK))
1479 s->flags |= SF_ERR_PRXCOND;
1480 if (!(s->flags & SF_FINST_MASK))
1481 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001482
Christopher Faulet0184ea72017-01-05 14:06:34 +01001483 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001484 req->analyse_exp = TICK_ETERNITY;
1485 return 0;
1486}
1487
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001488
Willy Tarreau347a35d2013-11-22 17:51:09 +01001489/* This function prepares an applet to handle the stats. It can deal with the
1490 * "100-continue" expectation, check that admin rules are met for POST requests,
1491 * and program a response message if something was unexpected. It cannot fail
1492 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001493 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001494 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001495 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001496 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001497int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001498{
1499 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001500 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001501 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001502 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001503 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001504 struct uri_auth *uri_auth = s->be->uri_auth;
1505 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001506 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001507
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001508 appctx = si_appctx(si);
1509 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1510 appctx->st1 = appctx->st2 = 0;
1511 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1512 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001513 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001514 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001515
Willy Tarreauf37954d2018-06-15 18:31:02 +02001516 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001517 lookup = uri + uri_auth->uri_len;
1518
1519 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1520 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001521 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001522 break;
1523 }
1524 }
1525
1526 if (uri_auth->refresh) {
1527 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1528 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001529 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001530 break;
1531 }
1532 }
1533 }
1534
1535 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1536 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001537 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001538 break;
1539 }
1540 }
1541
Willy Tarreau1e62df92016-01-11 18:57:53 +01001542 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1543 if (memcmp(h, ";typed", 6) == 0) {
1544 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1545 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1546 break;
1547 }
1548 }
1549
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001550 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1551 if (memcmp(h, ";st=", 4) == 0) {
1552 int i;
1553 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001554 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001555 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1556 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001557 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001558 break;
1559 }
1560 }
1561 break;
1562 }
1563 }
1564
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001565 appctx->ctx.stats.scope_str = 0;
1566 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001567 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1568 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1569 int itx = 0;
1570 const char *h2;
1571 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1572 const char *err;
1573
1574 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1575 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001576 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001577 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1578 itx++;
1579 h++;
1580 }
1581
1582 if (itx > STAT_SCOPE_TXT_MAXLEN)
1583 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001584 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001585
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001586 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001587 memcpy(scope_txt, h2, itx);
1588 scope_txt[itx] = '\0';
1589 err = invalid_char(scope_txt);
1590 if (err) {
1591 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001592 appctx->ctx.stats.scope_str = 0;
1593 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001594 }
1595 break;
1596 }
1597 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001598
1599 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001600 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001601 int ret = 1;
1602
1603 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001604 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 +01001605 ret = acl_pass(ret);
1606 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1607 ret = !ret;
1608 }
1609
1610 if (ret) {
1611 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001612 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001613 break;
1614 }
1615 }
1616
1617 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001618 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001619 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001620 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001621 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1622 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001623 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001624 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001625 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001626 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1627 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001628 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001629 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001630 else {
1631 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001632 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001633 }
1634
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001635 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001636 return 1;
1637}
1638
Lukas Tribus67db8df2013-06-23 17:37:13 +02001639/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1640 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1641 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001642void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001643{
1644#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001645 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001646 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1647#endif
1648#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001649 if (from->ss_family == AF_INET6) {
1650 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001651 /* v4-mapped addresses need IP_TOS */
1652 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1653 else
1654 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1655 }
1656#endif
1657}
1658
Willy Tarreau87b09662015-04-03 00:22:06 +02001659int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001660 const char* name, unsigned int name_len,
1661 const char *str, struct my_regex *re,
1662 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001663{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001664 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001665 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001666 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001667 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1668 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001669 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001670
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001671 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001672
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001673 /* Choose the header browsing function. */
1674 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001675 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001676 http_find_hdr_func = http_find_header2;
1677 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001678 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001679 http_find_hdr_func = http_find_full_header2;
1680 break;
1681 default: /* impossible */
1682 return -1;
1683 }
1684
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001685 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1686 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001687 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001688 char *val = ctx.line + ctx.val;
1689 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001690
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001691 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1692 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001693
Willy Tarreau6e27be12018-08-22 04:46:47 +02001694 len = exp_replace(output->area, output->size, val, str, pmatch);
1695 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001696 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001697
Willy Tarreau6e27be12018-08-22 04:46:47 +02001698 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001699
1700 hdr->len += delta;
1701 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001702
1703 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001704 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001705 }
1706
1707 return 0;
1708}
1709
Willy Tarreau87b09662015-04-03 00:22:06 +02001710static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001711 const char* name, unsigned int name_len,
1712 struct list *fmt, struct my_regex *re,
1713 int action)
1714{
Willy Tarreau83061a82018-07-13 11:56:34 +02001715 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001716 int ret = -1;
1717
1718 replace = alloc_trash_chunk();
1719 if (!replace)
1720 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001721
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001722 replace->data = build_logline(s, replace->area, replace->size, fmt);
1723 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001724 goto leave;
1725
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001726 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1727 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001728
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001729 leave:
1730 free_trash_chunk(replace);
1731 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001732}
1733
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001734/*
1735 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1736 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001737 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1738 * line is inserted before the header. If an error occurred <early_hints> is
1739 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001740 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001741static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1742 const char* name, unsigned int name_len,
1743 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001744{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001745 if (!early_hints) {
1746 early_hints = alloc_trash_chunk();
1747 if (!early_hints)
1748 goto fail;
1749 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1750 goto fail;
1751 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001752
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001753 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1754 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001755
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001756 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1757 if (!chunk_memcat(early_hints, "\r\n", 2))
1758 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001759
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001760 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001761
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001762 fail:
1763 free_trash_chunk(early_hints);
1764 return NULL;
1765}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001766
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001767/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1768 * response is added. If an error occurred or if another response was already
1769 * sent, this function does nothing.
1770 */
1771static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1772{
1773 struct channel *chn = s->txn->rsp.chn;
1774 char *cur_ptr = ci_head(chn);
1775 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001776
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001777 /* If a response was already sent, skip early hints */
1778 if (s->txn->status > 0)
1779 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001780
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001781 if (!chunk_memcat(early_hints, "\r\n", 2))
1782 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001783
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001784 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1785 c_adv(chn, ret);
1786 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001787}
1788
Willy Tarreau87b09662015-04-03 00:22:06 +02001789/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001790 * transaction <txn>. Returns the verdict of the first rule that prevents
1791 * further processing of the request (auth, deny, ...), and defaults to
1792 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1793 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001794 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1795 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1796 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001797 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001798enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001799http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001800{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001801 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001802 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001803 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001804 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001805 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001806 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001807 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001808 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001809 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001810 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001811
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001812 /* If "the current_rule_list" match the executed rule list, we are in
1813 * resume condition. If a resume is needed it is always in the action
1814 * and never in the ACL or converters. In this case, we initialise the
1815 * current rule, and go to the action execution point.
1816 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001817 if (s->current_rule) {
1818 rule = s->current_rule;
1819 s->current_rule = NULL;
1820 if (s->current_rule_list == rules)
1821 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001822 }
1823 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001824
Willy Tarreauff011f22011-01-06 17:51:27 +01001825 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001826
Willy Tarreau96257ec2012-12-27 10:46:37 +01001827 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001828 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001829 int ret;
1830
Willy Tarreau192252e2015-04-04 01:47:55 +02001831 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001832 ret = acl_pass(ret);
1833
Willy Tarreauff011f22011-01-06 17:51:27 +01001834 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001835 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001836
1837 if (!ret) /* condition not matched */
1838 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001839 }
1840
Willy Tarreauacc98002015-09-27 23:34:39 +02001841 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001842resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001843 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001844 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001845 rule_ret = HTTP_RULE_RES_STOP;
1846 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001847
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001848 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001849 if (deny_status)
1850 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001851 rule_ret = HTTP_RULE_RES_DENY;
1852 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001853
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001854 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001855 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001856 if (deny_status)
1857 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001858 rule_ret = HTTP_RULE_RES_DENY;
1859 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001860
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001861 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001862 /* Be sure to send any pending HTTP 103 response first */
1863 if (early_hints) {
1864 http_send_early_hints(s, early_hints);
1865 free_trash_chunk(early_hints);
1866 early_hints = NULL;
1867 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001868 /* Auth might be performed on regular http-req rules as well as on stats */
1869 auth_realm = rule->arg.auth.realm;
1870 if (!auth_realm) {
1871 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1872 auth_realm = STATS_DEFAULT_REALM;
1873 else
1874 auth_realm = px->id;
1875 }
1876 /* send 401/407 depending on whether we use a proxy or not. We still
1877 * count one error, because normal browsing won't significantly
1878 * increase the counter but brute force attempts will.
1879 */
1880 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1881 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001882 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001883 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001884 rule_ret = HTTP_RULE_RES_ABRT;
1885 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001886
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001887 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001888 /* Be sure to send any pending HTTP 103 response first */
1889 if (early_hints) {
1890 http_send_early_hints(s, early_hints);
1891 free_trash_chunk(early_hints);
1892 early_hints = NULL;
1893 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001894 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001895 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001896 rule_ret = HTTP_RULE_RES_BADREQ;
1897 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001898
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001899 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001900 s->task->nice = rule->arg.nice;
1901 break;
1902
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001903 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001904 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001905 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001906 break;
1907
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001908 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001909#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001910 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001911 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001912#endif
1913 break;
1914
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001915 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001916 s->logs.level = rule->arg.loglevel;
1917 break;
1918
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001919 case ACT_HTTP_REPLACE_HDR:
1920 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001921 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1922 rule->arg.hdr_add.name_len,
1923 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001924 &rule->arg.hdr_add.re, rule->action)) {
1925 rule_ret = HTTP_RULE_RES_BADREQ;
1926 goto end;
1927 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001928 break;
1929
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001930 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001931 ctx.idx = 0;
1932 /* remove all occurrences of the header */
1933 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001934 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001935 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001936 }
Willy Tarreau85603282015-01-21 20:39:27 +01001937 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001938
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001939 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001940 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001941 /* The scope of the trash buffer must be limited to this function. The
1942 * build_logline() function can execute a lot of other function which
1943 * can use the trash buffer. So for limiting the scope of this global
1944 * buffer, we build first the header value using build_logline, and
1945 * after we store the header name.
1946 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001947 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001948
1949 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001950 if (!replace) {
1951 rule_ret = HTTP_RULE_RES_BADREQ;
1952 goto end;
1953 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001954
Thierry Fournier4b788f72016-06-01 13:35:36 +02001955 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001956 len += build_logline(s, replace->area + len,
1957 replace->size - len,
1958 &rule->arg.hdr_add.fmt);
1959 memcpy(replace->area, rule->arg.hdr_add.name,
1960 rule->arg.hdr_add.name_len);
1961 replace->area[rule->arg.hdr_add.name_len] = ':';
1962 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1963 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001964
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001965 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001966 /* remove all occurrences of the header */
1967 ctx.idx = 0;
1968 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001969 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001970 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1971 }
1972 }
1973
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001974 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001975 static unsigned char rate_limit = 0;
1976
1977 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001978 replace->area[rule->arg.hdr_add.name_len] = 0;
1979 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,
1980 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001981 }
1982
1983 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1984 if (sess->fe != s->be)
1985 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1986 if (sess->listener->counters)
1987 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1988 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001989
1990 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001991 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001992 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001993
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001994 case ACT_HTTP_DEL_ACL:
1995 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001996 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001997 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001998
1999 /* collect reference */
2000 ref = pat_ref_lookup(rule->arg.map.ref);
2001 if (!ref)
2002 continue;
2003
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002004 /* allocate key */
2005 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002006 if (!key) {
2007 rule_ret = HTTP_RULE_RES_BADREQ;
2008 goto end;
2009 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002010
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002011 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002012 key->data = build_logline(s, key->area, key->size,
2013 &rule->arg.map.key);
2014 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002015
2016 /* perform update */
2017 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002018 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002019 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002020 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002021
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002022 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002023 break;
2024 }
2025
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002026 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002027 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002028 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002029
2030 /* collect reference */
2031 ref = pat_ref_lookup(rule->arg.map.ref);
2032 if (!ref)
2033 continue;
2034
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002035 /* allocate key */
2036 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002037 if (!key) {
2038 rule_ret = HTTP_RULE_RES_BADREQ;
2039 goto end;
2040 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002041
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002042 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002043 key->data = build_logline(s, key->area, key->size,
2044 &rule->arg.map.key);
2045 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002046
2047 /* perform update */
2048 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002049 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002050 if (pat_ref_find_elt(ref, key->area) == NULL)
2051 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002052 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002053
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002054 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002055 break;
2056 }
2057
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002058 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002059 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002060 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002061
2062 /* collect reference */
2063 ref = pat_ref_lookup(rule->arg.map.ref);
2064 if (!ref)
2065 continue;
2066
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002067 /* allocate key */
2068 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002069 if (!key) {
2070 rule_ret = HTTP_RULE_RES_BADREQ;
2071 goto end;
2072 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002073
2074 /* allocate value */
2075 value = alloc_trash_chunk();
2076 if (!value) {
2077 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01002078 rule_ret = HTTP_RULE_RES_BADREQ;
2079 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002080 }
2081
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002082 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002083 key->data = build_logline(s, key->area, key->size,
2084 &rule->arg.map.key);
2085 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002086
2087 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002088 value->data = build_logline(s, value->area,
2089 value->size,
2090 &rule->arg.map.value);
2091 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002092
2093 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002094 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002095 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002096 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002097 else
2098 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002099 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002100
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002101 free_trash_chunk(key);
2102 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002103 break;
2104 }
William Lallemand73025dd2014-04-24 14:38:37 +02002105
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002106 case ACT_HTTP_EARLY_HINT:
2107 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2108 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002109 early_hints = http_apply_early_hint_rule(s, early_hints,
2110 rule->arg.early_hint.name,
2111 rule->arg.early_hint.name_len,
2112 &rule->arg.early_hint.fmt);
2113 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01002114 rule_ret = HTTP_RULE_RES_DONE;
2115 goto end;
2116 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002117 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002118 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002119 if ((s->req.flags & CF_READ_ERROR) ||
2120 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2121 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2122 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002123 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002124
Willy Tarreauacc98002015-09-27 23:34:39 +02002125 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002126 case ACT_RET_ERR:
2127 case ACT_RET_CONT:
2128 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002129 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01002130 rule_ret = HTTP_RULE_RES_DONE;
2131 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002132 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002133 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01002134 rule_ret = HTTP_RULE_RES_YIELD;
2135 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002136 }
William Lallemand73025dd2014-04-24 14:38:37 +02002137 break;
2138
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002139 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002140 /* Note: only the first valid tracking parameter of each
2141 * applies.
2142 */
2143
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002144 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002145 struct stktable *t;
2146 struct stksess *ts;
2147 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002148 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002149
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002150 t = rule->arg.trk_ctr.table.t;
2151 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 +02002152
2153 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002154 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002155
2156 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002157 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2158 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2159 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002160 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002161
Emeric Brun819fc6f2017-06-13 19:37:32 +02002162 if (ptr1)
2163 stktable_data_cast(ptr1, http_req_cnt)++;
2164
2165 if (ptr2)
2166 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2167 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2168
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002169 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002170
2171 /* If data was modified, we need to touch to re-schedule sync */
2172 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002173 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002174
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002175 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002176 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002177 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002178 }
2179 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002180 break;
2181
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002182 /* other flags exists, but normaly, they never be matched. */
2183 default:
2184 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002185 }
2186 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002187
Christopher Fauletea827bd2018-11-15 15:34:11 +01002188 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002189 if (early_hints) {
2190 http_send_early_hints(s, early_hints);
2191 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002192 }
2193
Willy Tarreau96257ec2012-12-27 10:46:37 +01002194 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01002195 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002196}
2197
Willy Tarreau71241ab2012-12-27 11:30:54 +01002198
Willy Tarreau51d861a2015-05-22 17:30:48 +02002199/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2200 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2201 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2202 * is returned, the process can continue the evaluation of next rule list. If
2203 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2204 * is returned, it means the operation could not be processed and a server error
2205 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2206 * deny rule. If *YIELD is returned, the caller must call again the function
2207 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002208 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002209enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002210http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002211{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002212 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002213 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002214 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002215 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002216 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002217 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02002218 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002219
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002220 /* If "the current_rule_list" match the executed rule list, we are in
2221 * resume condition. If a resume is needed it is always in the action
2222 * and never in the ACL or converters. In this case, we initialise the
2223 * current rule, and go to the action execution point.
2224 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002225 if (s->current_rule) {
2226 rule = s->current_rule;
2227 s->current_rule = NULL;
2228 if (s->current_rule_list == rules)
2229 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002230 }
2231 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002232
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002233 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002234
2235 /* check optional condition */
2236 if (rule->cond) {
2237 int ret;
2238
Willy Tarreau192252e2015-04-04 01:47:55 +02002239 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002240 ret = acl_pass(ret);
2241
2242 if (rule->cond->pol == ACL_COND_UNLESS)
2243 ret = !ret;
2244
2245 if (!ret) /* condition not matched */
2246 continue;
2247 }
2248
Willy Tarreauacc98002015-09-27 23:34:39 +02002249 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002250resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002251 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002252 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002253 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2254 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002255
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002256 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002257 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002258 rule_ret = HTTP_RULE_RES_STOP;
2259 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002260
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002261 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002262 s->task->nice = rule->arg.nice;
2263 break;
2264
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002265 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002266 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002267 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002268 break;
2269
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002270 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002271#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002272 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002273 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002274#endif
2275 break;
2276
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002277 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002278 s->logs.level = rule->arg.loglevel;
2279 break;
2280
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002281 case ACT_HTTP_REPLACE_HDR:
2282 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002283 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2284 rule->arg.hdr_add.name_len,
2285 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002286 &rule->arg.hdr_add.re, rule->action)) {
2287 rule_ret = HTTP_RULE_RES_BADREQ;
2288 goto end;
2289 }
Sasha Pachev218f0642014-06-16 12:05:59 -06002290 break;
2291
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002292 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002293 ctx.idx = 0;
2294 /* remove all occurrences of the header */
2295 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002296 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002297 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2298 }
Willy Tarreau85603282015-01-21 20:39:27 +01002299 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002300
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002301 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002302 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002303 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002304
2305 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002306 if (!replace) {
2307 rule_ret = HTTP_RULE_RES_BADREQ;
2308 goto end;
2309 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002310
2311 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002312 memcpy(replace->area, rule->arg.hdr_add.name,
2313 rule->arg.hdr_add.name_len);
2314 replace->data = rule->arg.hdr_add.name_len;
2315 replace->area[replace->data++] = ':';
2316 replace->area[replace->data++] = ' ';
2317 replace->data += build_logline(s,
2318 replace->area + replace->data,
2319 replace->size - replace->data,
2320 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002321
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002322 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002323 /* remove all occurrences of the header */
2324 ctx.idx = 0;
2325 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002326 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002327 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2328 }
2329 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002330
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002331 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002332 static unsigned char rate_limit = 0;
2333
2334 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002335 replace->area[rule->arg.hdr_add.name_len] = 0;
2336 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,
2337 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002338 }
2339
2340 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2341 if (sess->fe != s->be)
2342 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2343 if (sess->listener->counters)
2344 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2345 if (objt_server(s->target))
2346 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2347 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002348
2349 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002350 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002351 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002352
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002353 case ACT_HTTP_DEL_ACL:
2354 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002355 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002356 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002357
2358 /* collect reference */
2359 ref = pat_ref_lookup(rule->arg.map.ref);
2360 if (!ref)
2361 continue;
2362
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002363 /* allocate key */
2364 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002365 if (!key) {
2366 rule_ret = HTTP_RULE_RES_BADREQ;
2367 goto end;
2368 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002369
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002370 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002371 key->data = build_logline(s, key->area, key->size,
2372 &rule->arg.map.key);
2373 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002374
2375 /* perform update */
2376 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002377 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002378 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002379 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002380
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002381 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002382 break;
2383 }
2384
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002385 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002386 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002387 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002388
2389 /* collect reference */
2390 ref = pat_ref_lookup(rule->arg.map.ref);
2391 if (!ref)
2392 continue;
2393
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002394 /* allocate key */
2395 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002396 if (!key) {
2397 rule_ret = HTTP_RULE_RES_BADREQ;
2398 goto end;
2399 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002400
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002401 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002402 key->data = build_logline(s, key->area, key->size,
2403 &rule->arg.map.key);
2404 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002405
2406 /* perform update */
2407 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002408 if (pat_ref_find_elt(ref, key->area) == NULL)
2409 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002410
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002411 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002412 break;
2413 }
2414
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002415 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002416 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002417 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002418
2419 /* collect reference */
2420 ref = pat_ref_lookup(rule->arg.map.ref);
2421 if (!ref)
2422 continue;
2423
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002424 /* allocate key */
2425 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002426 if (!key) {
2427 rule_ret = HTTP_RULE_RES_BADREQ;
2428 goto end;
2429 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002430
2431 /* allocate value */
2432 value = alloc_trash_chunk();
2433 if (!value) {
2434 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002435 rule_ret = HTTP_RULE_RES_BADREQ;
2436 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002437 }
2438
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002439 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002440 key->data = build_logline(s, key->area, key->size,
2441 &rule->arg.map.key);
2442 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002443
2444 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002445 value->data = build_logline(s, value->area,
2446 value->size,
2447 &rule->arg.map.value);
2448 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002449
2450 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002451 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002452 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002453 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002454 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002455 else
2456 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002457 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002458 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002459 free_trash_chunk(key);
2460 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002461 break;
2462 }
William Lallemand73025dd2014-04-24 14:38:37 +02002463
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002464 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002465 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002466 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002467 rule_ret = HTTP_RULE_RES_BADREQ;
2468 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002469
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002470 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2471 /* Note: only the first valid tracking parameter of each
2472 * applies.
2473 */
2474
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002475 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002476 struct stktable *t;
2477 struct stksess *ts;
2478 struct stktable_key *key;
2479 void *ptr;
2480
2481 t = rule->arg.trk_ctr.table.t;
2482 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2483
2484 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002485 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002486
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002487 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002488
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002489 /* let's count a new HTTP request as it's the first time we do it */
2490 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2491 if (ptr)
2492 stktable_data_cast(ptr, http_req_cnt)++;
2493
2494 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2495 if (ptr)
2496 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2497 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2498
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002499 /* When the client triggers a 4xx from the server, it's most often due
2500 * to a missing object or permission. These events should be tracked
2501 * because if they happen often, it may indicate a brute force or a
2502 * vulnerability scan. Normally this is done when receiving the response
2503 * but here we're tracking after this ought to have been done so we have
2504 * to do it on purpose.
2505 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002506 if ((unsigned)(txn->status - 400) < 100) {
2507 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2508 if (ptr)
2509 stktable_data_cast(ptr, http_err_cnt)++;
2510
2511 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2512 if (ptr)
2513 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2514 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2515 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002516
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002517 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002518
Emeric Brun0fed0b02017-11-29 16:15:07 +01002519 /* If data was modified, we need to touch to re-schedule sync */
2520 stktable_touch_local(t, ts, 0);
2521
Emeric Brun819fc6f2017-06-13 19:37:32 +02002522 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2523 if (sess->fe != s->be)
2524 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2525
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002526 }
2527 }
2528 break;
2529
Thierry FOURNIER42148732015-09-02 17:17:33 +02002530 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002531 if ((s->req.flags & CF_READ_ERROR) ||
2532 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2533 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2534 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002535 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002536
Willy Tarreauacc98002015-09-27 23:34:39 +02002537 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002538 case ACT_RET_ERR:
2539 case ACT_RET_CONT:
2540 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002541 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002542 rule_ret = HTTP_RULE_RES_STOP;
2543 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002544 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002545 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002546 rule_ret = HTTP_RULE_RES_YIELD;
2547 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002548 }
William Lallemand73025dd2014-04-24 14:38:37 +02002549 break;
2550
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002551 /* other flags exists, but normaly, they never be matched. */
2552 default:
2553 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002554 }
2555 }
2556
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002557 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002558 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002559 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002560}
2561
2562
Willy Tarreau71241ab2012-12-27 11:30:54 +01002563/* Perform an HTTP redirect based on the information in <rule>. The function
2564 * returns non-zero on success, or zero in case of a, irrecoverable error such
2565 * as too large a request to build a valid response.
2566 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002567int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002568{
Willy Tarreaub329a312015-05-22 16:27:37 +02002569 struct http_msg *req = &txn->req;
2570 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002571 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002572 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002573 int ret = 0;
2574
Christopher Fauletf2824e62018-10-01 12:12:37 +02002575 if (IS_HTX_STRM(s))
2576 return htx_apply_redirect_rule(rule, s, txn);
2577
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002578 chunk = alloc_trash_chunk();
2579 if (!chunk)
2580 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002581
2582 /* build redirect message */
2583 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002584 case 308:
2585 msg_fmt = HTTP_308;
2586 break;
2587 case 307:
2588 msg_fmt = HTTP_307;
2589 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002590 case 303:
2591 msg_fmt = HTTP_303;
2592 break;
2593 case 301:
2594 msg_fmt = HTTP_301;
2595 break;
2596 case 302:
2597 default:
2598 msg_fmt = HTTP_302;
2599 break;
2600 }
2601
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002602 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2603 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002604
2605 switch(rule->type) {
2606 case REDIRECT_TYPE_SCHEME: {
2607 const char *path;
2608 const char *host;
2609 struct hdr_ctx ctx;
2610 int pathlen;
2611 int hostlen;
2612
2613 host = "";
2614 hostlen = 0;
2615 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002616 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002617 host = ctx.line + ctx.val;
2618 hostlen = ctx.vlen;
2619 }
2620
Willy Tarreau6b952c82018-09-10 17:45:34 +02002621 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002622 /* build message using path */
2623 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002624 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002625 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2626 int qs = 0;
2627 while (qs < pathlen) {
2628 if (path[qs] == '?') {
2629 pathlen = qs;
2630 break;
2631 }
2632 qs++;
2633 }
2634 }
2635 } else {
2636 path = "/";
2637 pathlen = 1;
2638 }
2639
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002640 if (rule->rdr_str) { /* this is an old "redirect" rule */
2641 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002642 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002643 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002644
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002645 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002646 memcpy(chunk->area + chunk->data, rule->rdr_str,
2647 rule->rdr_len);
2648 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002649 }
2650 else {
2651 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002652 chunk->data += build_logline(s,
2653 chunk->area + chunk->data,
2654 chunk->size - chunk->data,
2655 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002656
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002657 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002658 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002659 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002660 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002661 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002662 memcpy(chunk->area + chunk->data, "://", 3);
2663 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002664
2665 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002666 memcpy(chunk->area + chunk->data, host, hostlen);
2667 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002668
2669 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002670 memcpy(chunk->area + chunk->data, path, pathlen);
2671 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002672
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002673 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002674 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002675 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002676 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002677 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002678 chunk->area[chunk->data] = '/';
2679 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002680 }
2681
2682 break;
2683 }
2684 case REDIRECT_TYPE_PREFIX: {
2685 const char *path;
2686 int pathlen;
2687
Willy Tarreau6b952c82018-09-10 17:45:34 +02002688 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002689 /* build message using path */
2690 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002691 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002692 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2693 int qs = 0;
2694 while (qs < pathlen) {
2695 if (path[qs] == '?') {
2696 pathlen = qs;
2697 break;
2698 }
2699 qs++;
2700 }
2701 }
2702 } else {
2703 path = "/";
2704 pathlen = 1;
2705 }
2706
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002707 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002708 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002709 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002710
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002711 /* add prefix. Note that if prefix == "/", we don't want to
2712 * add anything, otherwise it makes it hard for the user to
2713 * configure a self-redirection.
2714 */
2715 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002716 memcpy(chunk->area + chunk->data,
2717 rule->rdr_str, rule->rdr_len);
2718 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002719 }
2720 }
2721 else {
2722 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002723 chunk->data += build_logline(s,
2724 chunk->area + chunk->data,
2725 chunk->size - chunk->data,
2726 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002727
2728 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002729 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002730 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002731 }
2732
2733 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002734 memcpy(chunk->area + chunk->data, path, pathlen);
2735 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002736
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002737 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002738 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002739 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002740 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002741 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002742 chunk->area[chunk->data] = '/';
2743 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002744 }
2745
2746 break;
2747 }
2748 case REDIRECT_TYPE_LOCATION:
2749 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002750 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002751 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002752 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002753
2754 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002755 memcpy(chunk->area + chunk->data, rule->rdr_str,
2756 rule->rdr_len);
2757 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002758 }
2759 else {
2760 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002761 chunk->data += build_logline(s,
2762 chunk->area + chunk->data,
2763 chunk->size - chunk->data,
2764 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002765
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002766 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002767 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002768 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002769 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002770 break;
2771 }
2772
2773 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002774 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2775 chunk->data += 14;
2776 memcpy(chunk->area + chunk->data, rule->cookie_str,
2777 rule->cookie_len);
2778 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002779 }
2780
Willy Tarreau19b14122017-02-28 09:48:11 +01002781 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002782 txn->status = rule->code;
2783 /* let's log the request time */
2784 s->logs.tv_request = now;
2785
Christopher Fauletbe821b92017-03-30 11:21:53 +02002786 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002787 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2788 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2789 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002790 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002791 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002792 memcpy(chunk->area + chunk->data,
2793 "\r\nProxy-Connection: keep-alive", 30);
2794 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002795 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002796 memcpy(chunk->area + chunk->data,
2797 "\r\nConnection: keep-alive", 24);
2798 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002799 }
2800 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002801 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2802 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002803 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002804 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002805 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002806 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002807 req->next -= req->sov;
2808 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002809 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002810 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002811 req->msg_state = HTTP_MSG_CLOSED;
2812 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002813 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002814 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002815 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002816 /* let the server side turn to SI_ST_CLO */
2817 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002818 } else {
2819 /* keep-alive not possible */
2820 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002821 memcpy(chunk->area + chunk->data,
2822 "\r\nProxy-Connection: close\r\n\r\n", 29);
2823 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002824 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002825 memcpy(chunk->area + chunk->data,
2826 "\r\nConnection: close\r\n\r\n", 23);
2827 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002828 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002829 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002830 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002831 }
2832
Willy Tarreaue7dff022015-04-03 01:14:29 +02002833 if (!(s->flags & SF_ERR_MASK))
2834 s->flags |= SF_ERR_LOCAL;
2835 if (!(s->flags & SF_FINST_MASK))
2836 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002837
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002838 ret = 1;
2839 leave:
2840 free_trash_chunk(chunk);
2841 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002842}
2843
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002844/* This stream analyser runs all HTTP request processing which is common to
2845 * frontends and backends, which means blocking ACLs, filters, connection-close,
2846 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002847 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002848 * either needs more data or wants to immediately abort the request (eg: deny,
2849 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002850 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002851int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002852{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002853 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002854 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002855 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002856 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002857 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002858 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002859 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002860 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002861
Christopher Faulete0768eb2018-10-03 16:38:02 +02002862 if (IS_HTX_STRM(s))
2863 return htx_process_req_common(s, req, an_bit, px);
2864
Willy Tarreau655dce92009-11-08 13:10:58 +01002865 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002866 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002867 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002868 }
2869
Christopher Faulet45073512018-07-20 10:16:29 +02002870 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 +02002871 now_ms, __FUNCTION__,
2872 s,
2873 req,
2874 req->rex, req->wex,
2875 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002876 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002877 req->analysers);
2878
Willy Tarreau65410832014-04-28 21:25:43 +02002879 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002880 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002881
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002882 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002883 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002884 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002885
Willy Tarreau0b748332014-04-29 00:13:29 +02002886 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002887 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2888 goto return_prx_yield;
2889
Willy Tarreau0b748332014-04-29 00:13:29 +02002890 case HTTP_RULE_RES_CONT:
2891 case HTTP_RULE_RES_STOP: /* nothing to do */
2892 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002893
Willy Tarreau0b748332014-04-29 00:13:29 +02002894 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2895 if (txn->flags & TX_CLTARPIT)
2896 goto tarpit;
2897 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002898
Willy Tarreau0b748332014-04-29 00:13:29 +02002899 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2900 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002901
Willy Tarreau0b748332014-04-29 00:13:29 +02002902 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002903 goto done;
2904
Willy Tarreau0b748332014-04-29 00:13:29 +02002905 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2906 goto return_bad_req;
2907 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002908 }
2909
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002910 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2911 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002912 struct hdr_ctx ctx;
2913
2914 ctx.idx = 0;
2915 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002916 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002917 if (unlikely(http_header_add_tail2(&txn->req,
2918 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002919 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002920 goto return_bad_req;
2921 }
2922 }
2923
2924 }
2925
Willy Tarreau52542592014-04-28 18:33:26 +02002926 /* OK at this stage, we know that the request was accepted according to
2927 * the http-request rules, we can check for the stats. Note that the
2928 * URI is detected *before* the req* rules in order not to be affected
2929 * by a possible reqrep, while they are processed *after* so that a
2930 * reqdeny can still block them. This clearly needs to change in 1.6!
2931 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002932 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002933 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002934 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002935 txn->status = 500;
2936 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002937 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002938
Willy Tarreaue7dff022015-04-03 01:14:29 +02002939 if (!(s->flags & SF_ERR_MASK))
2940 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002941 goto return_prx_cond;
2942 }
2943
2944 /* parse the whole stats request and extract the relevant information */
2945 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002946 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002947 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002948
Willy Tarreau0b748332014-04-29 00:13:29 +02002949 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2950 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002951
Willy Tarreau0b748332014-04-29 00:13:29 +02002952 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2953 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002954 }
2955
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002956 /* evaluate the req* rules except reqadd */
2957 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002958 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002959 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002960
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002961 if (txn->flags & TX_CLDENY)
2962 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002963
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002964 if (txn->flags & TX_CLTARPIT) {
2965 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002966 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002967 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002968 }
Willy Tarreau06619262006-12-17 08:37:22 +01002969
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002970 /* add request headers from the rule sets in the same order */
2971 list_for_each_entry(wl, &px->req_add, list) {
2972 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002973 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002974 ret = acl_pass(ret);
2975 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2976 ret = !ret;
2977 if (!ret)
2978 continue;
2979 }
2980
Christopher Faulet10079f52018-10-03 15:17:28 +02002981 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002982 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002983 }
2984
Willy Tarreau52542592014-04-28 18:33:26 +02002985
2986 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002987 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2988 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002989 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002990 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002991 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002992
Willy Tarreaue7dff022015-04-03 01:14:29 +02002993 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2994 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2995 if (!(s->flags & SF_FINST_MASK))
2996 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002997
Willy Tarreau70730dd2014-04-24 18:06:27 +02002998 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002999 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3000 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003001 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003002 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003003 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003004
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003005 /* check whether we have some ACLs set to redirect this request */
3006 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003007 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003008 int ret;
3009
Willy Tarreau192252e2015-04-04 01:47:55 +02003010 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003011 ret = acl_pass(ret);
3012 if (rule->cond->pol == ACL_COND_UNLESS)
3013 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003014 if (!ret)
3015 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003016 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003017 if (!http_apply_redirect_rule(rule, s, txn))
3018 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003019 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003020 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003021
Willy Tarreau2be39392010-01-03 17:24:51 +01003022 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3023 * If this happens, then the data will not come immediately, so we must
3024 * send all what we have without waiting. Note that due to the small gain
3025 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003026 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003027 * itself once used.
3028 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003029 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003030
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003031 done: /* done with this analyser, continue with next ones that the calling
3032 * points will have set, if any.
3033 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003034 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003035 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3036 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003037 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003038
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003039 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003040 /* Allow cookie logging
3041 */
3042 if (s->be->cookie_name || sess->fe->capture_name)
3043 manage_client_side_cookies(s, req);
3044
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003045 /* When a connection is tarpitted, we use the tarpit timeout,
3046 * which may be the same as the connect timeout if unspecified.
3047 * If unset, then set it to zero because we really want it to
3048 * eventually expire. We build the tarpit as an analyser.
3049 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003050 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003051
3052 /* wipe the request out so that we can drop the connection early
3053 * if the client closes first.
3054 */
3055 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003056
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003057 txn->status = http_err_codes[deny_status];
3058
Christopher Faulet0184ea72017-01-05 14:06:34 +01003059 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003060 req->analysers |= AN_REQ_HTTP_TARPIT;
3061 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3062 if (!req->analyse_exp)
3063 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003064 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003065 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003066 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003067 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003068 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003069 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003070 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003071
3072 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003073
3074 /* Allow cookie logging
3075 */
3076 if (s->be->cookie_name || sess->fe->capture_name)
3077 manage_client_side_cookies(s, req);
3078
Willy Tarreau0b748332014-04-29 00:13:29 +02003079 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003080 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003081 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003082 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003083 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003084 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003085 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003086 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003087 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003088 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003089 goto return_prx_cond;
3090
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003091 return_bad_req:
3092 /* We centralize bad requests processing here */
3093 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3094 /* we detected a parsing error. We want to archive this request
3095 * in the dedicated proxy area for later troubleshooting.
3096 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003097 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003099
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003100 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003101 txn->req.msg_state = HTTP_MSG_ERROR;
3102 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003103 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003104
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003105 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003106 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003107 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003108
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003109 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003110 if (!(s->flags & SF_ERR_MASK))
3111 s->flags |= SF_ERR_PRXCOND;
3112 if (!(s->flags & SF_FINST_MASK))
3113 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003114
Christopher Faulet0184ea72017-01-05 14:06:34 +01003115 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003116 req->analyse_exp = TICK_ETERNITY;
3117 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003118
3119 return_prx_yield:
3120 channel_dont_connect(req);
3121 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003122}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003123
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003124/* This function performs all the processing enabled for the current request.
3125 * It returns 1 if the processing can continue on next analysers, or zero if it
3126 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003127 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003128 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003129int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003130{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003131 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003132 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003133 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003134 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003135
Christopher Faulete0768eb2018-10-03 16:38:02 +02003136 if (IS_HTX_STRM(s))
3137 return htx_process_request(s, req, an_bit);
3138
Willy Tarreau655dce92009-11-08 13:10:58 +01003139 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003140 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003141 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003142 return 0;
3143 }
3144
Christopher Faulet45073512018-07-20 10:16:29 +02003145 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 +02003146 now_ms, __FUNCTION__,
3147 s,
3148 req,
3149 req->rex, req->wex,
3150 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003151 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003152 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003153
Willy Tarreau59234e92008-11-30 23:51:27 +01003154 /*
3155 * Right now, we know that we have processed the entire headers
3156 * and that unwanted requests have been filtered out. We can do
3157 * whatever we want with the remaining request. Also, now we
3158 * may have separate values for ->fe, ->be.
3159 */
Willy Tarreau06619262006-12-17 08:37:22 +01003160
Willy Tarreau59234e92008-11-30 23:51:27 +01003161 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003162 * If HTTP PROXY is set we simply get remote server address parsing
3163 * incoming request. Note that this requires that a connection is
3164 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003165 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003166 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003167 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003168 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003169
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003170 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003171 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003172 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003173 txn->req.msg_state = HTTP_MSG_ERROR;
3174 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003175 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003176 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003177
Willy Tarreaue7dff022015-04-03 01:14:29 +02003178 if (!(s->flags & SF_ERR_MASK))
3179 s->flags |= SF_ERR_RESOURCE;
3180 if (!(s->flags & SF_FINST_MASK))
3181 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003182
3183 return 0;
3184 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003185
Willy Tarreau6b952c82018-09-10 17:45:34 +02003186 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003187 if (url2sa(ci_head(req) + msg->sl.rq.u,
3188 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003189 &conn->addr.to, NULL) == -1)
3190 goto return_bad_req;
3191
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003192 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003193 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3194 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003195 * u_l characters by a single "/".
3196 */
3197 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003198 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003199 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3200 int delta;
3201
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003202 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003203 http_msg_move_end(&txn->req, delta);
3204 cur_end += delta;
3205 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3206 goto return_bad_req;
3207 }
3208 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003209 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003210 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3211 int delta;
3212
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003213 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003214 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003215 http_msg_move_end(&txn->req, delta);
3216 cur_end += delta;
3217 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3218 goto return_bad_req;
3219 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003220 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003221
Willy Tarreau59234e92008-11-30 23:51:27 +01003222 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003223 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003224 * Note that doing so might move headers in the request, but
3225 * the fields will stay coherent and the URI will not move.
3226 * This should only be performed in the backend.
3227 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003228 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003229 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003230
William Lallemanda73203e2012-03-12 12:48:57 +01003231 /* add unique-id if "header-unique-id" is specified */
3232
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003233 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003234 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003235 goto return_bad_req;
3236 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003237 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003238 }
William Lallemanda73203e2012-03-12 12:48:57 +01003239
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003240 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003241 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003242 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003243 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003244 goto return_bad_req;
3245 }
3246
Cyril Bontéb21570a2009-11-29 20:04:48 +01003247 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003248 * 9: add X-Forwarded-For if either the frontend or the backend
3249 * asks for it.
3250 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003251 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003252 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003253 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3254 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3255 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003256 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003257 /* The header is set to be added only if none is present
3258 * and we found it, so don't do anything.
3259 */
3260 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003261 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003262 /* Add an X-Forwarded-For header unless the source IP is
3263 * in the 'except' network range.
3264 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003265 if ((!sess->fe->except_mask.s_addr ||
3266 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3267 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003268 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003269 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003270 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003271 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003272 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003273 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003274
3275 /* Note: we rely on the backend to get the header name to be used for
3276 * x-forwarded-for, because the header is really meant for the backends.
3277 * However, if the backend did not specify any option, we have to rely
3278 * on the frontend's header name.
3279 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003280 if (s->be->fwdfor_hdr_len) {
3281 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003282 memcpy(trash.area,
3283 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003284 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003285 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003286 memcpy(trash.area,
3287 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003288 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003289 len += snprintf(trash.area + len,
3290 trash.size - len,
3291 ": %d.%d.%d.%d", pn[0], pn[1],
3292 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003293
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003294 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003295 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003296 }
3297 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003298 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003299 /* FIXME: for the sake of completeness, we should also support
3300 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003301 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003302 int len;
3303 char pn[INET6_ADDRSTRLEN];
3304 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003305 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003306 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003307
Willy Tarreau59234e92008-11-30 23:51:27 +01003308 /* Note: we rely on the backend to get the header name to be used for
3309 * x-forwarded-for, because the header is really meant for the backends.
3310 * However, if the backend did not specify any option, we have to rely
3311 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003312 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003313 if (s->be->fwdfor_hdr_len) {
3314 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003315 memcpy(trash.area, s->be->fwdfor_hdr_name,
3316 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003317 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003318 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003319 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3320 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003321 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003322 len += snprintf(trash.area + len, trash.size - len,
3323 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003324
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003325 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003326 goto return_bad_req;
3327 }
3328 }
3329
3330 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003331 * 10: add X-Original-To if either the frontend or the backend
3332 * asks for it.
3333 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003334 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003335
3336 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003337 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003338 /* Add an X-Original-To header unless the destination IP is
3339 * in the 'except' network range.
3340 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003341 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003342
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003343 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003344 ((!sess->fe->except_mask_to.s_addr ||
3345 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3346 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003347 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003348 (((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 +02003349 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003350 int len;
3351 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003352 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003353
3354 /* Note: we rely on the backend to get the header name to be used for
3355 * x-original-to, because the header is really meant for the backends.
3356 * However, if the backend did not specify any option, we have to rely
3357 * on the frontend's header name.
3358 */
3359 if (s->be->orgto_hdr_len) {
3360 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003361 memcpy(trash.area,
3362 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003363 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003364 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003365 memcpy(trash.area,
3366 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003367 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003368 len += snprintf(trash.area + len,
3369 trash.size - len,
3370 ": %d.%d.%d.%d", pn[0], pn[1],
3371 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003372
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003373 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003374 goto return_bad_req;
3375 }
3376 }
3377 }
3378
Willy Tarreau50fc7772012-11-11 22:19:57 +01003379 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3380 * If an "Upgrade" token is found, the header is left untouched in order not to have
3381 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3382 * "Upgrade" is present in the Connection header.
3383 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003384 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003385 unsigned int want_flags = 0;
3386
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003387 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003388 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003389 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003390 want_flags |= TX_CON_CLO_SET;
3391 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003392 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003393 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003394 want_flags |= TX_CON_KAL_SET;
3395 }
3396
3397 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003398 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003399 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003400
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003401
Willy Tarreau522d6c02009-12-06 18:49:18 +01003402 /* If we have no server assigned yet and we're balancing on url_param
3403 * with a POST request, we may be interested in checking the body for
3404 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003405 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003406 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003407 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003408 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003409 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003410 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003411 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003412
Christopher Fauletbe821b92017-03-30 11:21:53 +02003413 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3414 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003415#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003416 /* We expect some data from the client. Unless we know for sure
3417 * we already have a full request, we have to re-enable quick-ack
3418 * in case we previously disabled it, otherwise we might cause
3419 * the client to delay further data.
3420 */
3421 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3422 cli_conn && conn_ctrl_ready(cli_conn) &&
3423 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003424 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003425 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003426#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003427
Willy Tarreau59234e92008-11-30 23:51:27 +01003428 /*************************************************************
3429 * OK, that's finished for the headers. We have done what we *
3430 * could. Let's switch to the DATA state. *
3431 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003432 req->analyse_exp = TICK_ETERNITY;
3433 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003434
Willy Tarreau59234e92008-11-30 23:51:27 +01003435 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003436 /* OK let's go on with the BODY now */
3437 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003438
Willy Tarreau59234e92008-11-30 23:51:27 +01003439 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003440 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003441 /* we detected a parsing error. We want to archive this request
3442 * in the dedicated proxy area for later troubleshooting.
3443 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003444 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003445 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003446
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003447 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003448 txn->req.msg_state = HTTP_MSG_ERROR;
3449 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003450 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003451 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003452
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003453 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003454 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003455 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003456
Willy Tarreaue7dff022015-04-03 01:14:29 +02003457 if (!(s->flags & SF_ERR_MASK))
3458 s->flags |= SF_ERR_PRXCOND;
3459 if (!(s->flags & SF_FINST_MASK))
3460 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003461 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003462}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003463
Willy Tarreau60b85b02008-11-30 23:28:40 +01003464/* This function is an analyser which processes the HTTP tarpit. It always
3465 * returns zero, at the beginning because it prevents any other processing
3466 * from occurring, and at the end because it terminates the request.
3467 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003468int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003469{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003470 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003471
Christopher Faulete0768eb2018-10-03 16:38:02 +02003472 if (IS_HTX_STRM(s))
3473 return htx_process_tarpit(s, req, an_bit);
3474
Willy Tarreau60b85b02008-11-30 23:28:40 +01003475 /* This connection is being tarpitted. The CLIENT side has
3476 * already set the connect expiration date to the right
3477 * timeout. We just have to check that the client is still
3478 * there and that the timeout has not expired.
3479 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003480 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003481 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003482 !tick_is_expired(req->analyse_exp, now_ms))
3483 return 0;
3484
3485 /* We will set the queue timer to the time spent, just for
3486 * logging purposes. We fake a 500 server error, so that the
3487 * attacker will not suspect his connection has been tarpitted.
3488 * It will not cause trouble to the logs because we can exclude
3489 * the tarpitted connections by filtering on the 'PT' status flags.
3490 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003491 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3492
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003493 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003494 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003495
Christopher Faulet0184ea72017-01-05 14:06:34 +01003496 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003497 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003498
Willy Tarreaue7dff022015-04-03 01:14:29 +02003499 if (!(s->flags & SF_ERR_MASK))
3500 s->flags |= SF_ERR_PRXCOND;
3501 if (!(s->flags & SF_FINST_MASK))
3502 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003503 return 0;
3504}
3505
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003506/* This function is an analyser which waits for the HTTP request body. It waits
3507 * for either the buffer to be full, or the full advertised contents to have
3508 * reached the buffer. It must only be called after the standard HTTP request
3509 * processing has occurred, because it expects the request to be parsed and will
3510 * look for the Expect header. It may send a 100-Continue interim response. It
3511 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3512 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3513 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003514 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003515int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003516{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003517 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003518 struct http_txn *txn = s->txn;
3519 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003520
Christopher Faulete0768eb2018-10-03 16:38:02 +02003521 if (IS_HTX_STRM(s))
3522 return htx_wait_for_request_body(s, req, an_bit);
3523
Willy Tarreaud34af782008-11-30 23:36:37 +01003524 /* We have to parse the HTTP request body to find any required data.
3525 * "balance url_param check_post" should have been the only way to get
3526 * into this. We were brought here after HTTP header analysis, so all
3527 * related structures are ready.
3528 */
3529
Willy Tarreau890988f2014-04-10 11:59:33 +02003530 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3531 /* This is the first call */
3532 if (msg->msg_state < HTTP_MSG_BODY)
3533 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003534
Willy Tarreau890988f2014-04-10 11:59:33 +02003535 if (msg->msg_state < HTTP_MSG_100_SENT) {
3536 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3537 * send an HTTP/1.1 100 Continue intermediate response.
3538 */
3539 if (msg->flags & HTTP_MSGF_VER_11) {
3540 struct hdr_ctx ctx;
3541 ctx.idx = 0;
3542 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003543 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003544 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003545 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003546 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003547 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003548 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003549 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003550 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003551
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003552 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003553 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003554 * must save the body in msg->next because it survives buffer
3555 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003556 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003557 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003558
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003559 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003560 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3561 else
3562 msg->msg_state = HTTP_MSG_DATA;
3563 }
3564
Willy Tarreau890988f2014-04-10 11:59:33 +02003565 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3566 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003567 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003568 goto missing_data;
3569
3570 /* OK we have everything we need now */
3571 goto http_end;
3572 }
3573
3574 /* OK here we're parsing a chunked-encoded message */
3575
Willy Tarreau522d6c02009-12-06 18:49:18 +01003576 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003577 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003578 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003579 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003580 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003581 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003582 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003583
Willy Tarreau115acb92009-12-26 13:56:06 +01003584 if (!ret)
3585 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003586 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003587 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003588 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003589 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003590 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003591 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003592 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003593
3594 msg->chunk_len = chunk;
3595 msg->body_len += chunk;
3596
3597 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003598 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003599 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003600 }
3601
Willy Tarreaud98cf932009-12-27 22:54:55 +01003602 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003603 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3604 * for at least a whole chunk or the whole content length bytes after
3605 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003606 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003607 if (msg->msg_state == HTTP_MSG_TRAILERS)
3608 goto http_end;
3609
Willy Tarreaue115b492015-05-01 23:05:14 +02003610 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003611 goto http_end;
3612
3613 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003614 /* we get here if we need to wait for more data. If the buffer is full,
3615 * we have the maximum we can expect.
3616 */
Willy Tarreau23752332018-06-15 14:54:53 +02003617 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003618 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003619
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003620 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003621 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003622 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003623
Willy Tarreaue7dff022015-04-03 01:14:29 +02003624 if (!(s->flags & SF_ERR_MASK))
3625 s->flags |= SF_ERR_CLITO;
3626 if (!(s->flags & SF_FINST_MASK))
3627 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003628 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003629 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003630
3631 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003632 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003633 /* Not enough data. We'll re-use the http-request
3634 * timeout here. Ideally, we should set the timeout
3635 * relative to the accept() date. We just set the
3636 * request timeout once at the beginning of the
3637 * request.
3638 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003639 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003640 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003641 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003642 return 0;
3643 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003644
3645 http_end:
3646 /* The situation will not evolve, so let's give up on the analysis. */
3647 s->logs.tv_request = now; /* update the request timer to reflect full request */
3648 req->analysers &= ~an_bit;
3649 req->analyse_exp = TICK_ETERNITY;
3650 return 1;
3651
3652 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003653 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003654 txn->req.msg_state = HTTP_MSG_ERROR;
3655 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003656 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003657
Willy Tarreaue7dff022015-04-03 01:14:29 +02003658 if (!(s->flags & SF_ERR_MASK))
3659 s->flags |= SF_ERR_PRXCOND;
3660 if (!(s->flags & SF_FINST_MASK))
3661 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003662
Willy Tarreau522d6c02009-12-06 18:49:18 +01003663 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003664 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003665 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003666 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003667 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003668 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003669}
3670
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003671/* send a server's name with an outgoing request over an established connection.
3672 * Note: this function is designed to be called once the request has been scheduled
3673 * for being forwarded. This is the reason why it rewinds the buffer before
3674 * proceeding.
3675 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003676int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003677
3678 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003679 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003680 char *hdr_name = be->server_id_hdr_name;
3681 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003682 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003683 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003684 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003685
William Lallemandd9e90662012-01-30 17:27:17 +01003686 ctx.idx = 0;
3687
Willy Tarreau211cdec2014-04-17 20:18:08 +02003688 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003689 if (old_o) {
3690 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003691 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003692 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003693 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003694 }
3695
Willy Tarreauf37954d2018-06-15 18:31:02 +02003696 old_i = ci_data(chn);
3697 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 -05003698 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003699 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003700 }
3701
3702 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003703 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003704 memcpy(hdr_val, hdr_name, hdr_name_len);
3705 hdr_val += hdr_name_len;
3706 *hdr_val++ = ':';
3707 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003708 hdr_val += strlcpy2(hdr_val, srv_name,
3709 trash.area + trash.size - hdr_val);
3710 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3711 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003712
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003713 if (old_o) {
3714 /* If this was a forwarded request, we must readjust the amount of
3715 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003716 * variations. Note that the current state is >= HTTP_MSG_BODY,
3717 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003718 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003719 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003720 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003721 txn->req.next -= old_o;
3722 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003723 }
3724
Mark Lamourinec2247f02012-01-04 13:02:01 -05003725 return 0;
3726}
3727
Willy Tarreau610ecce2010-01-04 21:15:02 +01003728/* Terminate current transaction and prepare a new one. This is very tricky
3729 * right now but it works.
3730 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003731void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003732{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003733 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003734 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003735 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003736 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003737 struct connection *srv_conn;
3738 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003739 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003740
Willy Tarreau610ecce2010-01-04 21:15:02 +01003741 /* FIXME: We need a more portable way of releasing a backend's and a
3742 * server's connections. We need a safer way to reinitialize buffer
3743 * flags. We also need a more accurate method for computing per-request
3744 * data.
3745 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003746 cs = objt_cs(s->si[1].end);
3747 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003748
Willy Tarreau4213a112013-12-15 10:25:42 +01003749 /* unless we're doing keep-alive, we want to quickly close the connection
3750 * to the server.
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003751 * XXX cognet: If the connection doesn't have a owner then it may not
3752 * be referenced anywhere, just kill it now, even if it could be reused.
3753 * To be revisited later when revisited later when we handle connection
3754 * pools properly.
Willy Tarreau4213a112013-12-15 10:25:42 +01003755 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003756 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003757 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003758 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3759 si_shutr(&s->si[1]);
3760 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003761 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003762
Willy Tarreaue7dff022015-04-03 01:14:29 +02003763 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003764 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003765 if (unlikely(s->srv_conn))
3766 sess_change_server(s, NULL);
3767 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003768
3769 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003770 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003771
Willy Tarreaueee5b512015-04-03 23:46:31 +02003772 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003773 int n;
3774
Willy Tarreaueee5b512015-04-03 23:46:31 +02003775 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003776 if (n < 1 || n > 5)
3777 n = 0;
3778
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003779 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003780 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003781 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003782 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003783 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003784 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3785 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003786 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003787 }
3788
3789 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003790 s->logs.bytes_in -= ci_data(&s->req);
3791 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003792
Willy Tarreau66425e32018-07-25 06:55:12 +02003793 /* we may need to know the position in the queue */
3794 pendconn_free(s);
3795
Willy Tarreau610ecce2010-01-04 21:15:02 +01003796 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003797 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003798 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003799 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003800 s->do_log(s);
3801 }
3802
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003803 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003804 stream_stop_content_counters(s);
3805 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003806
Willy Tarreau610ecce2010-01-04 21:15:02 +01003807 s->logs.accept_date = date; /* user-visible date for logging */
3808 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003809 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3810 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003811 tv_zero(&s->logs.tv_request);
3812 s->logs.t_queue = -1;
3813 s->logs.t_connect = -1;
3814 s->logs.t_data = -1;
3815 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003816 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3817 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003818
Willy Tarreauf37954d2018-06-15 18:31:02 +02003819 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3820 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003821
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003822 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003823 if (s->flags & SF_CURR_SESS) {
3824 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003825 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003826 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003827 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003828 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003829 }
3830
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003831 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003832
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003833
3834 /* If we're doing keepalive, first call the mux detach() method
3835 * to let it know we want to detach without freing the connection.
3836 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003837 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003838 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003839 !si_conn_ready(&s->si[1]) || !srv_conn->owner)
Willy Tarreau323a2d92015-08-04 19:00:17 +02003840 srv_conn = NULL;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003841 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003842
Willy Tarreau350f4872014-11-28 14:42:25 +01003843 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3844 s->si[1].err_type = SI_ET_NONE;
3845 s->si[1].conn_retries = 0; /* used for logging too */
3846 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003847 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 +01003848 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 +02003849 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 +02003850 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3851 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3852 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003853
Patrick Hemmere3faf022018-08-22 10:02:00 -04003854 hlua_ctx_destroy(s->hlua);
3855 s->hlua = NULL;
3856
Willy Tarreaueee5b512015-04-03 23:46:31 +02003857 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003858 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003859 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003860
3861 if (prev_status == 401 || prev_status == 407) {
3862 /* In HTTP keep-alive mode, if we receive a 401, we still have
3863 * a chance of being able to send the visitor again to the same
3864 * server over the same connection. This is required by some
3865 * broken protocols such as NTLM, and anyway whenever there is
3866 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003867 * it's better to do it (at least it helps with debugging), at
3868 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003869 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003870 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003871 }
3872
Willy Tarreau53f96852016-02-02 18:50:47 +01003873 /* Never ever allow to reuse a connection from a non-reuse backend */
3874 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3875 srv_conn->flags |= CO_FL_PRIVATE;
3876
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003877 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003878 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003879
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003880 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003881 s->req.flags |= CF_NEVER_WAIT;
3882 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003883 }
3884
Willy Tarreau714ea782015-11-25 20:11:11 +01003885 /* we're removing the analysers, we MUST re-enable events detection.
3886 * We don't enable close on the response channel since it's either
3887 * already closed, or in keep-alive with an idle connection handler.
3888 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003889 channel_auto_read(&s->req);
3890 channel_auto_close(&s->req);
3891 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003892
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003893 /* we're in keep-alive with an idle connection, monitor it if not already done */
3894 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003895 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003896 if (srv) {
3897 if (srv_conn->flags & CO_FL_PRIVATE)
3898 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3899 else if (prev_flags & TX_NOT_FIRST)
3900 /* note: we check the request, not the connection, but
3901 * this is valid for strategies SAFE and AGGR, and in
3902 * case of ALWS, we don't care anyway.
3903 */
3904 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3905 else
3906 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3907 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003908 }
Christopher Faulete6006242017-03-10 11:52:44 +01003909 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3910 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003911}
3912
3913
3914/* This function updates the request state machine according to the response
3915 * state machine and buffer flags. It returns 1 if it changes anything (flag
3916 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3917 * it is only used to find when a request/response couple is complete. Both
3918 * this function and its equivalent should loop until both return zero. It
3919 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3920 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003921int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003922{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003923 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003924 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003925 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003926 unsigned int old_state = txn->req.msg_state;
3927
Christopher Faulet4be98032017-07-18 10:48:24 +02003928 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003929 return 0;
3930
3931 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003932 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003933 * We can shut the read side unless we want to abort_on_close,
3934 * or we have a POST request. The issue with POST requests is
3935 * that some browsers still send a CRLF after the request, and
3936 * this CRLF must be read so that it does not remain in the kernel
3937 * buffers, otherwise a close could cause an RST on some systems
3938 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003939 * Note that if we're using keep-alive on the client side, we'd
3940 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003941 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003942 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003943 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003944 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3945 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003946 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3947 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003948 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003949 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003950
Willy Tarreau40f151a2012-12-20 12:10:09 +01003951 /* if the server closes the connection, we want to immediately react
3952 * and close the socket to save packets and syscalls.
3953 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003954 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003955
Willy Tarreau7f876a12015-11-18 11:59:55 +01003956 /* In any case we've finished parsing the request so we must
3957 * disable Nagle when sending data because 1) we're not going
3958 * to shut this side, and 2) the server is waiting for us to
3959 * send pending data.
3960 */
3961 chn->flags |= CF_NEVER_WAIT;
3962
Willy Tarreau610ecce2010-01-04 21:15:02 +01003963 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3964 goto wait_other_side;
3965
3966 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3967 /* The server has not finished to respond, so we
3968 * don't want to move in order not to upset it.
3969 */
3970 goto wait_other_side;
3971 }
3972
Willy Tarreau610ecce2010-01-04 21:15:02 +01003973 /* When we get here, it means that both the request and the
3974 * response have finished receiving. Depending on the connection
3975 * mode, we'll have to wait for the last bytes to leave in either
3976 * direction, and sometimes for a close to be effective.
3977 */
3978
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003979 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3980 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003981 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3982 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003983 }
3984 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3985 /* Option forceclose is set, or either side wants to close,
3986 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003987 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003988 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003989 *
3990 * However, there is an exception if the response
3991 * length is undefined. In this case, we need to wait
3992 * the close from the server. The response will be
3993 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003994 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003995 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3996 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3997 goto check_channel_flags;
3998
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003999 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4000 channel_shutr_now(chn);
4001 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004002 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004003 }
4004 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004005 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4006 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004007 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004008 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4009 channel_auto_read(chn);
4010 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004011 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004012 }
4013
Christopher Faulet4be98032017-07-18 10:48:24 +02004014 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004015 }
4016
4017 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4018 http_msg_closing:
4019 /* nothing else to forward, just waiting for the output buffer
4020 * to be empty and for the shutw_now to take effect.
4021 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004022 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004023 txn->req.msg_state = HTTP_MSG_CLOSED;
4024 goto http_msg_closed;
4025 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004026 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004027 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004028 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004029 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004030 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004031 }
4032
4033 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4034 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004035 /* if we don't know whether the server will close, we need to hard close */
4036 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4037 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4038
Willy Tarreau3988d932013-12-27 23:03:08 +01004039 /* see above in MSG_DONE why we only do this in these states */
4040 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4041 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004042 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4043 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004044 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004045 goto wait_other_side;
4046 }
4047
Christopher Faulet4be98032017-07-18 10:48:24 +02004048 check_channel_flags:
4049 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4050 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4051 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004052 txn->req.msg_state = HTTP_MSG_CLOSING;
4053 goto http_msg_closing;
4054 }
4055
4056
Willy Tarreau610ecce2010-01-04 21:15:02 +01004057 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004058 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004059}
4060
4061
4062/* This function updates the response state machine according to the request
4063 * state machine and buffer flags. It returns 1 if it changes anything (flag
4064 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4065 * it is only used to find when a request/response couple is complete. Both
4066 * this function and its equivalent should loop until both return zero. It
4067 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4068 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004069int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004070{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004071 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004072 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004073 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004074 unsigned int old_state = txn->rsp.msg_state;
4075
Christopher Faulet4be98032017-07-18 10:48:24 +02004076 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004077 return 0;
4078
4079 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4080 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004081 * still monitor the server connection for a possible close
4082 * while the request is being uploaded, so we don't disable
4083 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004084 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004085 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004086
4087 if (txn->req.msg_state == HTTP_MSG_ERROR)
4088 goto wait_other_side;
4089
4090 if (txn->req.msg_state < HTTP_MSG_DONE) {
4091 /* The client seems to still be sending data, probably
4092 * because we got an error response during an upload.
4093 * We have the choice of either breaking the connection
4094 * or letting it pass through. Let's do the later.
4095 */
4096 goto wait_other_side;
4097 }
4098
Willy Tarreau610ecce2010-01-04 21:15:02 +01004099 /* When we get here, it means that both the request and the
4100 * response have finished receiving. Depending on the connection
4101 * mode, we'll have to wait for the last bytes to leave in either
4102 * direction, and sometimes for a close to be effective.
4103 */
4104
4105 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4106 /* Server-close mode : shut read and wait for the request
4107 * side to close its output buffer. The caller will detect
4108 * when we're in DONE and the other is in CLOSED and will
4109 * catch that for the final cleanup.
4110 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004111 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4112 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004113 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004114 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4115 /* Option forceclose is set, or either side wants to close,
4116 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004117 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004118 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004119 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004120 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004121 channel_shutr_now(chn);
4122 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004123 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004124 }
4125 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004126 /* The last possible modes are keep-alive and tunnel. Tunnel will
4127 * need to forward remaining data. Keep-alive will need to monitor
4128 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004129 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004130 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004131 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004132 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4133 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004134 }
4135
Christopher Faulet4be98032017-07-18 10:48:24 +02004136 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004137 }
4138
4139 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4140 http_msg_closing:
4141 /* nothing else to forward, just waiting for the output buffer
4142 * to be empty and for the shutw_now to take effect.
4143 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004144 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004145 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4146 goto http_msg_closed;
4147 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004148 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004149 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004150 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004151 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004152 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004153 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004154 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004155 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004156 }
4157
4158 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4159 http_msg_closed:
4160 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004161 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004162 channel_auto_close(chn);
4163 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004164 goto wait_other_side;
4165 }
4166
Christopher Faulet4be98032017-07-18 10:48:24 +02004167 check_channel_flags:
4168 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4169 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4170 /* if we've just closed an output, let's switch */
4171 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4172 goto http_msg_closing;
4173 }
4174
Willy Tarreau610ecce2010-01-04 21:15:02 +01004175 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004176 /* We force the response to leave immediately if we're waiting for the
4177 * other side, since there is no pending shutdown to push it out.
4178 */
4179 if (!channel_is_empty(chn))
4180 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004181 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004182}
4183
4184
Christopher Faulet894da4c2017-07-18 11:29:07 +02004185/* Resync the request and response state machines. */
4186void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004187{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004188 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004189#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004190 int old_req_state = txn->req.msg_state;
4191 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004192#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004193
Willy Tarreau610ecce2010-01-04 21:15:02 +01004194 http_sync_req_state(s);
4195 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004196 if (!http_sync_res_state(s))
4197 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004198 if (!http_sync_req_state(s))
4199 break;
4200 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004201
Christopher Faulet894da4c2017-07-18 11:29:07 +02004202 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4203 "req->analysers=0x%08x res->analysers=0x%08x\n",
4204 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004205 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4206 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004207 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004208
4209
Willy Tarreau610ecce2010-01-04 21:15:02 +01004210 /* OK, both state machines agree on a compatible state.
4211 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004212 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4213 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004214 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4215 * means we must abort the request.
4216 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4217 * corresponding channel.
4218 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4219 * on the response with server-close mode means we've completed one
4220 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004221 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004222 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4223 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004224 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004225 channel_auto_close(&s->req);
4226 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004227 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004228 channel_auto_close(&s->res);
4229 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004230 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004231 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4232 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004233 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004234 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004235 channel_auto_close(&s->res);
4236 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004237 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004238 channel_abort(&s->req);
4239 channel_auto_close(&s->req);
4240 channel_auto_read(&s->req);
4241 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004242 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004243 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4244 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4245 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4246 s->req.analysers &= AN_REQ_FLT_END;
4247 if (HAS_REQ_DATA_FILTERS(s))
4248 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4249 }
4250 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4251 s->res.analysers &= AN_RES_FLT_END;
4252 if (HAS_RSP_DATA_FILTERS(s))
4253 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4254 }
4255 channel_auto_close(&s->req);
4256 channel_auto_read(&s->req);
4257 channel_auto_close(&s->res);
4258 channel_auto_read(&s->res);
4259 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004260 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4261 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004262 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004263 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4264 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4265 /* server-close/keep-alive: terminate this transaction,
4266 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004267 * a fresh-new transaction, but only once we're sure there's
4268 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004269 * another request. They must not hold any pending output data
4270 * and the response buffer must realigned
4271 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004272 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004273 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004274 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004275 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004276 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004277 else {
4278 s->req.analysers = AN_REQ_FLT_END;
4279 s->res.analysers = AN_RES_FLT_END;
4280 txn->flags |= TX_WAIT_CLEANUP;
4281 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004282 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004283}
4284
Willy Tarreaud98cf932009-12-27 22:54:55 +01004285/* This function is an analyser which forwards request body (including chunk
4286 * sizes if any). It is called as soon as we must forward, even if we forward
4287 * zero byte. The only situation where it must not be called is when we're in
4288 * tunnel mode and we want to forward till the close. It's used both to forward
4289 * remaining data and to resync after end of body. It expects the msg_state to
4290 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004291 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004292 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004293 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004294 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004295int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004296{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004297 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004298 struct http_txn *txn = s->txn;
4299 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004300 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004301
Christopher Faulete0768eb2018-10-03 16:38:02 +02004302 if (IS_HTX_STRM(s))
4303 return htx_request_forward_body(s, req, an_bit);
4304
Christopher Faulet45073512018-07-20 10:16:29 +02004305 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 +02004306 now_ms, __FUNCTION__,
4307 s,
4308 req,
4309 req->rex, req->wex,
4310 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004311 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004312 req->analysers);
4313
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004314 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4315 return 0;
4316
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004317 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004318 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004319 /* Output closed while we were sending data. We must abort and
4320 * wake the other side up.
4321 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004322 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004323 msg->msg_state = HTTP_MSG_ERROR;
4324 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004325 return 1;
4326 }
4327
Willy Tarreaud98cf932009-12-27 22:54:55 +01004328 /* Note that we don't have to send 100-continue back because we don't
4329 * need the data to complete our job, and it's up to the server to
4330 * decide whether to return 100, 417 or anything else in return of
4331 * an "Expect: 100-continue" header.
4332 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004333 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004334 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4335 ? HTTP_MSG_CHUNK_SIZE
4336 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004337
4338 /* TODO/filters: when http-buffer-request option is set or if a
4339 * rule on url_param exists, the first chunk size could be
4340 * already parsed. In that case, msg->next is after the chunk
4341 * size (including the CRLF after the size). So this case should
4342 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004343 }
4344
Willy Tarreau7ba23542014-04-17 21:50:00 +02004345 /* Some post-connect processing might want us to refrain from starting to
4346 * forward data. Currently, the only reason for this is "balance url_param"
4347 * whichs need to parse/process the request after we've enabled forwarding.
4348 */
4349 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004350 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004351 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004352 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004353 channel_dont_close(req); /* don't fail on early shutr */
4354 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004355 }
4356 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4357 }
4358
Willy Tarreau80a92c02014-03-12 10:41:13 +01004359 /* in most states, we should abort in case of early close */
4360 channel_auto_close(req);
4361
Willy Tarreauefdf0942014-04-24 20:08:57 +02004362 if (req->to_forward) {
4363 /* We can't process the buffer's contents yet */
4364 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004365 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004366 }
4367
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004368 if (msg->msg_state < HTTP_MSG_DONE) {
4369 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4370 ? http_msg_forward_chunked_body(s, msg)
4371 : http_msg_forward_body(s, msg));
4372 if (!ret)
4373 goto missing_data_or_waiting;
4374 if (ret < 0)
4375 goto return_bad_req;
4376 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004377
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004378 /* other states, DONE...TUNNEL */
4379 /* we don't want to forward closes on DONE except in tunnel mode. */
4380 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4381 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004382
Christopher Faulet894da4c2017-07-18 11:29:07 +02004383 http_resync_states(s);
4384 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004385 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4386 if (req->flags & CF_SHUTW) {
4387 /* request errors are most likely due to the
4388 * server aborting the transfer. */
4389 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004390 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004391 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004392 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004393 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004394 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004395 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004396 }
4397
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004398 /* If "option abortonclose" is set on the backend, we want to monitor
4399 * the client's connection and forward any shutdown notification to the
4400 * server, which will decide whether to close or to go on processing the
4401 * request. We only do that in tunnel mode, and not in other modes since
4402 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004403 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004404 channel_auto_read(req);
4405 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4406 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4407 s->si[1].flags |= SI_FL_NOLINGER;
4408 channel_auto_close(req);
4409 }
4410 else if (s->txn->meth == HTTP_METH_POST) {
4411 /* POST requests may require to read extra CRLF sent by broken
4412 * browsers and which could cause an RST to be sent upon close
4413 * on some systems (eg: Linux). */
4414 channel_auto_read(req);
4415 }
4416 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004417
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004418 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004419 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004420 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004421 if (!(s->flags & SF_ERR_MASK))
4422 s->flags |= SF_ERR_CLICL;
4423 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004424 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004425 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004426 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004427 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004428 }
4429
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004430 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4431 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004432 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004433 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004434
4435 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004436 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004437
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004438 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004439 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004440 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004441 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004442
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004443 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004444 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004445 * And when content-length is used, we never want to let the possible
4446 * shutdown be forwarded to the other side, as the state machine will
4447 * take care of it once the client responds. It's also important to
4448 * prevent TIME_WAITs from accumulating on the backend side, and for
4449 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004450 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004451 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004452 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004453
Willy Tarreau5c620922011-05-11 19:56:11 +02004454 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004455 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004456 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004457 * modes are already handled by the stream sock layer. We must not do
4458 * this in content-length mode because it could present the MSG_MORE
4459 * flag with the last block of forwarded data, which would cause an
4460 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004461 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004462 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004463 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004464
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004465 return 0;
4466
Willy Tarreaud98cf932009-12-27 22:54:55 +01004467 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004468 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004469 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004470 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004471
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004472 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004473 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004474 txn->req.msg_state = HTTP_MSG_ERROR;
4475 if (txn->status) {
4476 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004477 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004478 } else {
4479 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004480 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004481 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004482 req->analysers &= AN_REQ_FLT_END;
4483 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 +01004484
Willy Tarreaue7dff022015-04-03 01:14:29 +02004485 if (!(s->flags & SF_ERR_MASK))
4486 s->flags |= SF_ERR_PRXCOND;
4487 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004488 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004489 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004490 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004491 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004492 }
4493 return 0;
4494
4495 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004496 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004497 txn->req.msg_state = HTTP_MSG_ERROR;
4498 if (txn->status) {
4499 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004500 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004501 } else {
4502 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004503 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004504 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004505 req->analysers &= AN_REQ_FLT_END;
4506 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 +01004507
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004508 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4509 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004510 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004511 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004512
Willy Tarreaue7dff022015-04-03 01:14:29 +02004513 if (!(s->flags & SF_ERR_MASK))
4514 s->flags |= SF_ERR_SRVCL;
4515 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004516 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004517 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004518 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004519 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004520 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004521 return 0;
4522}
4523
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004524/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4525 * processing can continue on next analysers, or zero if it either needs more
4526 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004527 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004528 * when it has nothing left to do, and may remove any analyser when it wants to
4529 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004530 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004531int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004532{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004533 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004534 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004535 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004536 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004537 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004538 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004539 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004540 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004541
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004542 srv_conn = cs_conn(objt_cs(s->si[1].end));
4543
Christopher Faulete0768eb2018-10-03 16:38:02 +02004544 if (IS_HTX_STRM(s))
4545 return htx_wait_for_response(s, rep, an_bit);
4546
Christopher Faulet45073512018-07-20 10:16:29 +02004547 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 +02004548 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004549 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004550 rep,
4551 rep->rex, rep->wex,
4552 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004553 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004554 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004555
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004556 /*
4557 * Now parse the partial (or complete) lines.
4558 * We will check the response syntax, and also join multi-line
4559 * headers. An index of all the lines will be elaborated while
4560 * parsing.
4561 *
4562 * For the parsing, we use a 28 states FSM.
4563 *
4564 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004565 * ci_head(rep) = beginning of response
4566 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4567 * ci_tail(rep) = end of input data
4568 * msg->eol = end of current header or line (LF or CRLF)
4569 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004570 */
4571
Willy Tarreau628c40c2014-04-24 19:11:26 +02004572 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004573 /* There's a protected area at the end of the buffer for rewriting
4574 * purposes. We don't want to start to parse the request if the
4575 * protected area is affected, because we may have to move processed
4576 * data later, which is much more complicated.
4577 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004578 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004579 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004580 /* some data has still not left the buffer, wake us once that's done */
4581 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4582 goto abort_response;
4583 channel_dont_close(rep);
4584 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004585 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004586 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004587 }
4588
Willy Tarreau188e2302018-06-15 11:11:53 +02004589 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004590 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004591 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004592
Willy Tarreauf37954d2018-06-15 18:31:02 +02004593 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004594 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004595 }
4596
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004597 /* 1: we might have to print this header in debug mode */
4598 if (unlikely((global.mode & MODE_DEBUG) &&
4599 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004600 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004601 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004602
Willy Tarreauf37954d2018-06-15 18:31:02 +02004603 sol = ci_head(rep);
4604 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004605 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004606
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004607 sol += hdr_idx_first_pos(&txn->hdr_idx);
4608 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004609
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004610 while (cur_idx) {
4611 eol = sol + txn->hdr_idx.v[cur_idx].len;
4612 debug_hdr("srvhdr", s, sol, eol);
4613 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4614 cur_idx = txn->hdr_idx.v[cur_idx].next;
4615 }
4616 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004617
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004618 /*
4619 * Now we quickly check if we have found a full valid response.
4620 * If not so, we check the FD and buffer states before leaving.
4621 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004622 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004623 * responses are checked first.
4624 *
4625 * Depending on whether the client is still there or not, we
4626 * may send an error response back or not. Note that normally
4627 * we should only check for HTTP status there, and check I/O
4628 * errors somewhere else.
4629 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004630
Willy Tarreau655dce92009-11-08 13:10:58 +01004631 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004632 /* Invalid response */
4633 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4634 /* we detected a parsing error. We want to archive this response
4635 * in the dedicated proxy area for later troubleshooting.
4636 */
4637 hdr_response_bad:
4638 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004639 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004640
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004641 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004642 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004643 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004644 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004645 }
Willy Tarreau64648412010-03-05 10:41:54 +01004646 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004647 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004648 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004649 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004650 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004651 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004652 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004653
Willy Tarreaue7dff022015-04-03 01:14:29 +02004654 if (!(s->flags & SF_ERR_MASK))
4655 s->flags |= SF_ERR_PRXCOND;
4656 if (!(s->flags & SF_FINST_MASK))
4657 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004658
4659 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004660 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004661
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004662 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004663 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004664 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004665 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004666 goto hdr_response_bad;
4667 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004668
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004669 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004670 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004671 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004672 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004673 else if (txn->flags & TX_NOT_FIRST)
4674 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004675
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004676 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004677 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004678 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004679 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004680 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004681
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004682 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004683 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004684 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004685
4686 /* Check to see if the server refused the early data.
4687 * If so, just send a 425
4688 */
4689 if (objt_cs(s->si[1].end)) {
4690 struct connection *conn = objt_cs(s->si[1].end)->conn;
4691
4692 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4693 txn->status = 425;
4694 }
4695
Willy Tarreau350f4872014-11-28 14:42:25 +01004696 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004697 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004698 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004699
Willy Tarreaue7dff022015-04-03 01:14:29 +02004700 if (!(s->flags & SF_ERR_MASK))
4701 s->flags |= SF_ERR_SRVCL;
4702 if (!(s->flags & SF_FINST_MASK))
4703 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004704 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004705 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004706
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004707 /* read timeout : return a 504 to the client. */
4708 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004709 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004710 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004711
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004712 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004713 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004714 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004715 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004716 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004717
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004718 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004719 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004720 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004721 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004722 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004723 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004724
Willy Tarreaue7dff022015-04-03 01:14:29 +02004725 if (!(s->flags & SF_ERR_MASK))
4726 s->flags |= SF_ERR_SRVTO;
4727 if (!(s->flags & SF_FINST_MASK))
4728 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004729 return 0;
4730 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004731
Willy Tarreauf003d372012-11-26 13:35:37 +01004732 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004733 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004734 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4735 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004736 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004737 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004738
Christopher Faulet0184ea72017-01-05 14:06:34 +01004739 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004740 channel_auto_close(rep);
4741
4742 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004743 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004744 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004745
Willy Tarreaue7dff022015-04-03 01:14:29 +02004746 if (!(s->flags & SF_ERR_MASK))
4747 s->flags |= SF_ERR_CLICL;
4748 if (!(s->flags & SF_FINST_MASK))
4749 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004750
Willy Tarreau87b09662015-04-03 00:22:06 +02004751 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004752 return 0;
4753 }
4754
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004755 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004756 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004757 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004758 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004759 else if (txn->flags & TX_NOT_FIRST)
4760 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004761
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004762 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004763 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004764 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004765 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004766 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004767
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004768 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004769 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004770 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004771 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004772 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004773 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004774
Willy Tarreaue7dff022015-04-03 01:14:29 +02004775 if (!(s->flags & SF_ERR_MASK))
4776 s->flags |= SF_ERR_SRVCL;
4777 if (!(s->flags & SF_FINST_MASK))
4778 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004779 return 0;
4780 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004781
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004782 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004783 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004784 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004785 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004786 else if (txn->flags & TX_NOT_FIRST)
4787 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004788
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004789 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004790 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004791 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004792
Willy Tarreaue7dff022015-04-03 01:14:29 +02004793 if (!(s->flags & SF_ERR_MASK))
4794 s->flags |= SF_ERR_CLICL;
4795 if (!(s->flags & SF_FINST_MASK))
4796 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004797
Willy Tarreau87b09662015-04-03 00:22:06 +02004798 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004799 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004800 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004801
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004802 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004803 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004804 return 0;
4805 }
4806
4807 /* More interesting part now : we know that we have a complete
4808 * response which at least looks like HTTP. We have an indicator
4809 * of each header's length, so we can parse them quickly.
4810 */
4811
4812 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004813 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004814
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004815 /*
4816 * 1: get the status code
4817 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004818 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004819 if (n < 1 || n > 5)
4820 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004821 /* when the client triggers a 4xx from the server, it's most often due
4822 * to a missing object or permission. These events should be tracked
4823 * because if they happen often, it may indicate a brute force or a
4824 * vulnerability scan.
4825 */
4826 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004827 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004828
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004829 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004830 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004831
Willy Tarreau91852eb2015-05-01 13:26:00 +02004832 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4833 * exactly one digit "." one digit. This check may be disabled using
4834 * option accept-invalid-http-response.
4835 */
4836 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4837 if (msg->sl.st.v_l != 8) {
4838 msg->err_pos = 0;
4839 goto hdr_response_bad;
4840 }
4841
Willy Tarreauf37954d2018-06-15 18:31:02 +02004842 if (ci_head(rep)[4] != '/' ||
4843 !isdigit((unsigned char)ci_head(rep)[5]) ||
4844 ci_head(rep)[6] != '.' ||
4845 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004846 msg->err_pos = 4;
4847 goto hdr_response_bad;
4848 }
4849 }
4850
Willy Tarreau5b154472009-12-21 20:11:07 +01004851 /* check if the response is HTTP/1.1 or above */
4852 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004853 ((ci_head(rep)[5] > '1') ||
4854 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004855 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004856
4857 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004858 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 +01004859
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004860 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004861 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004862
Willy Tarreauf37954d2018-06-15 18:31:02 +02004863 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004864
Willy Tarreau39650402010-03-15 19:44:39 +01004865 /* Adjust server's health based on status code. Note: status codes 501
4866 * and 505 are triggered on demand by client request, so we must not
4867 * count them as server failures.
4868 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004869 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004870 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004871 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004872 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004873 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004874 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004875
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004876 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004877 * We may be facing a 100-continue response, or any other informational
4878 * 1xx response which is non-final, in which case this is not the right
4879 * response, and we're waiting for the next one. Let's allow this response
4880 * to go to the client and wait for the next one. There's an exception for
4881 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004882 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004883 if (txn->status < 200 &&
4884 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004885 hdr_idx_init(&txn->hdr_idx);
4886 msg->next -= channel_forward(rep, msg->next);
4887 msg->msg_state = HTTP_MSG_RPBEFORE;
4888 txn->status = 0;
4889 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004890 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004891 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004892 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004893
Willy Tarreaua14ad722017-07-07 11:36:32 +02004894 /*
4895 * 2: check for cacheability.
4896 */
4897
4898 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004899 case 200:
4900 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004901 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004902 case 206:
4903 case 300:
4904 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004905 case 404:
4906 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004907 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004908 case 414:
4909 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004910 break;
4911 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004912 /* RFC7231#6.1:
4913 * Responses with status codes that are defined as
4914 * cacheable by default (e.g., 200, 203, 204, 206,
4915 * 300, 301, 404, 405, 410, 414, and 501 in this
4916 * specification) can be reused by a cache with
4917 * heuristic expiration unless otherwise indicated
4918 * by the method definition or explicit cache
4919 * controls [RFC7234]; all other status codes are
4920 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004921 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004922 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004923 break;
4924 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004925
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004926 /*
4927 * 3: we may need to capture headers
4928 */
4929 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004930 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004931 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4932 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004933
Willy Tarreau557f1992015-05-01 10:05:17 +02004934 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4935 * by RFC7230#3.3.3 :
4936 *
4937 * The length of a message body is determined by one of the following
4938 * (in order of precedence):
4939 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004940 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4941 * the connection will become a tunnel immediately after the empty
4942 * line that concludes the header fields. A client MUST ignore
4943 * any Content-Length or Transfer-Encoding header fields received
4944 * in such a message. Any 101 response (Switching Protocols) is
4945 * managed in the same manner.
4946 *
4947 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004948 * (Informational), 204 (No Content), or 304 (Not Modified) status
4949 * code is always terminated by the first empty line after the
4950 * header fields, regardless of the header fields present in the
4951 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004952 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004953 * 3. If a Transfer-Encoding header field is present and the chunked
4954 * transfer coding (Section 4.1) is the final encoding, the message
4955 * body length is determined by reading and decoding the chunked
4956 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004957 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004958 * If a Transfer-Encoding header field is present in a response and
4959 * the chunked transfer coding is not the final encoding, the
4960 * message body length is determined by reading the connection until
4961 * it is closed by the server. If a Transfer-Encoding header field
4962 * is present in a request and the chunked transfer coding is not
4963 * the final encoding, the message body length cannot be determined
4964 * reliably; the server MUST respond with the 400 (Bad Request)
4965 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004966 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004967 * If a message is received with both a Transfer-Encoding and a
4968 * Content-Length header field, the Transfer-Encoding overrides the
4969 * Content-Length. Such a message might indicate an attempt to
4970 * perform request smuggling (Section 9.5) or response splitting
4971 * (Section 9.4) and ought to be handled as an error. A sender MUST
4972 * remove the received Content-Length field prior to forwarding such
4973 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004974 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004975 * 4. If a message is received without Transfer-Encoding and with
4976 * either multiple Content-Length header fields having differing
4977 * field-values or a single Content-Length header field having an
4978 * invalid value, then the message framing is invalid and the
4979 * recipient MUST treat it as an unrecoverable error. If this is a
4980 * request message, the server MUST respond with a 400 (Bad Request)
4981 * status code and then close the connection. If this is a response
4982 * message received by a proxy, the proxy MUST close the connection
4983 * to the server, discard the received response, and send a 502 (Bad
4984 * Gateway) response to the client. If this is a response message
4985 * received by a user agent, the user agent MUST close the
4986 * connection to the server and discard the received response.
4987 *
4988 * 5. If a valid Content-Length header field is present without
4989 * Transfer-Encoding, its decimal value defines the expected message
4990 * body length in octets. If the sender closes the connection or
4991 * the recipient times out before the indicated number of octets are
4992 * received, the recipient MUST consider the message to be
4993 * incomplete and close the connection.
4994 *
4995 * 6. If this is a request message and none of the above are true, then
4996 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004997 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004998 * 7. Otherwise, this is a response message without a declared message
4999 * body length, so the message body length is determined by the
5000 * number of octets received prior to the server closing the
5001 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005002 */
5003
5004 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005005 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005006 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005007 * FIXME: should we parse anyway and return an error on chunked encoding ?
5008 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005009 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5010 txn->status == 101)) {
5011 /* Either we've established an explicit tunnel, or we're
5012 * switching the protocol. In both cases, we're very unlikely
5013 * to understand the next protocols. We have to switch to tunnel
5014 * mode, so that we transfer the request and responses then let
5015 * this protocol pass unmodified. When we later implement specific
5016 * parsers for such protocols, we'll want to check the Upgrade
5017 * header which contains information about that protocol for
5018 * responses with status 101 (eg: see RFC2817 about TLS).
5019 */
5020 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5021 msg->flags |= HTTP_MSGF_XFER_LEN;
5022 goto end;
5023 }
5024
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005025 if (txn->meth == HTTP_METH_HEAD ||
5026 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005027 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005028 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005029 goto skip_content_length;
5030 }
5031
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005032 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005033 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005034 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005035 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005036 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5037 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005038 /* bad transfer-encoding (chunked followed by something else) */
5039 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005040 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005041 break;
5042 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005043 }
5044
Willy Tarreau1c913912015-04-30 10:57:51 +02005045 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005046 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005047 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005048 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005049 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5050 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005051 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005052 signed long long cl;
5053
Willy Tarreauad14f752011-09-02 20:33:27 +02005054 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005055 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005056 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005057 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005058
Willy Tarreauad14f752011-09-02 20:33:27 +02005059 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005060 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005061 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005062 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005063
Willy Tarreauad14f752011-09-02 20:33:27 +02005064 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005065 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005066 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005067 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005068
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005069 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005070 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005071 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005072 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005073
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005074 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005075 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005076 }
5077
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02005078 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
5079 * 407 (Proxy-Authenticate) responses and set the connection to private
5080 */
5081 if (srv_conn && txn->status == 401) {
5082 /* check for Negotiate/NTLM WWW-Authenticate headers */
5083 ctx.idx = 0;
5084 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
5085 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5086 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5087 srv_conn->flags |= CO_FL_PRIVATE;
5088 }
5089 } else if (srv_conn && txn->status == 407) {
5090 /* check for Negotiate/NTLM Proxy-Authenticate headers */
5091 ctx.idx = 0;
5092 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
5093 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5094 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5095 srv_conn->flags |= CO_FL_PRIVATE;
5096 }
5097 }
5098
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005099 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005100 /* Now we have to check if we need to modify the Connection header.
5101 * This is more difficult on the response than it is on the request,
5102 * because we can have two different HTTP versions and we don't know
5103 * how the client will interprete a response. For instance, let's say
5104 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5105 * HTTP/1.1 response without any header. Maybe it will bound itself to
5106 * HTTP/1.0 because it only knows about it, and will consider the lack
5107 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5108 * the lack of header as a keep-alive. Thus we will use two flags
5109 * indicating how a request MAY be understood by the client. In case
5110 * of multiple possibilities, we'll fix the header to be explicit. If
5111 * ambiguous cases such as both close and keepalive are seen, then we
5112 * will fall back to explicit close. Note that we won't take risks with
5113 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005114 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005115 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005116 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005117 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01005118 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005119
Willy Tarreau60466522010-01-18 19:08:45 +01005120 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005121 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01005122 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005123
Willy Tarreau60466522010-01-18 19:08:45 +01005124 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005125 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01005126 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005127 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005128 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005129 }
Willy Tarreau60466522010-01-18 19:08:45 +01005130 else { /* SCL / KAL */
5131 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005132 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005133 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005134 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005135
Willy Tarreau60466522010-01-18 19:08:45 +01005136 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005137 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005138
Willy Tarreau60466522010-01-18 19:08:45 +01005139 /* Some keep-alive responses are converted to Server-close if
5140 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005141 */
Willy Tarreau60466522010-01-18 19:08:45 +01005142 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5143 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005144 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005145 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005146 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005147 }
5148
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005149 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005150 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005151 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005152
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005153 /* end of job, return OK */
5154 rep->analysers &= ~an_bit;
5155 rep->analyse_exp = TICK_ETERNITY;
5156 channel_auto_close(rep);
5157 return 1;
5158
5159 abort_keep_alive:
5160 /* A keep-alive request to the server failed on a network error.
5161 * The client is required to retry. We need to close without returning
5162 * any other information so that the client retries.
5163 */
5164 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005165 rep->analysers &= AN_RES_FLT_END;
5166 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005167 channel_auto_close(rep);
5168 s->logs.logwait = 0;
5169 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005170 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005171 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005172 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005173 return 0;
5174}
5175
5176/* This function performs all the processing enabled for the current response.
5177 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005178 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005179 * other functions. It works like process_request (see indications above).
5180 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005181int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005182{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005183 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005184 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005185 struct http_msg *msg = &txn->rsp;
5186 struct proxy *cur_proxy;
5187 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005188 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005189
Christopher Faulete0768eb2018-10-03 16:38:02 +02005190 if (IS_HTX_STRM(s))
5191 return htx_process_res_common(s, rep, an_bit, px);
5192
Christopher Faulet45073512018-07-20 10:16:29 +02005193 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 +02005194 now_ms, __FUNCTION__,
5195 s,
5196 rep,
5197 rep->rex, rep->wex,
5198 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005199 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005200 rep->analysers);
5201
5202 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5203 return 0;
5204
Willy Tarreau70730dd2014-04-24 18:06:27 +02005205 /* The stats applet needs to adjust the Connection header but we don't
5206 * apply any filter there.
5207 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005208 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5209 rep->analysers &= ~an_bit;
5210 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005211 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005212 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005213
Willy Tarreau58975672014-04-24 21:13:57 +02005214 /*
5215 * We will have to evaluate the filters.
5216 * As opposed to version 1.2, now they will be evaluated in the
5217 * filters order and not in the header order. This means that
5218 * each filter has to be validated among all headers.
5219 *
5220 * Filters are tried with ->be first, then with ->fe if it is
5221 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005222 *
5223 * Maybe we are in resume condiion. In this case I choose the
5224 * "struct proxy" which contains the rule list matching the resume
5225 * pointer. If none of theses "struct proxy" match, I initialise
5226 * the process with the first one.
5227 *
5228 * In fact, I check only correspondance betwwen the current list
5229 * pointer and the ->fe rule list. If it doesn't match, I initialize
5230 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005231 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005232 if (s->current_rule_list == &sess->fe->http_res_rules)
5233 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005234 else
5235 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005236 while (1) {
5237 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005238
Willy Tarreau58975672014-04-24 21:13:57 +02005239 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005240 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005241 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005242
Willy Tarreau51d861a2015-05-22 17:30:48 +02005243 if (ret == HTTP_RULE_RES_BADREQ)
5244 goto return_srv_prx_502;
5245
5246 if (ret == HTTP_RULE_RES_DONE) {
5247 rep->analysers &= ~an_bit;
5248 rep->analyse_exp = TICK_ETERNITY;
5249 return 1;
5250 }
5251 }
5252
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005253 /* we need to be called again. */
5254 if (ret == HTTP_RULE_RES_YIELD) {
5255 channel_dont_close(rep);
5256 return 0;
5257 }
5258
Willy Tarreau58975672014-04-24 21:13:57 +02005259 /* try headers filters */
5260 if (rule_set->rsp_exp != NULL) {
5261 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5262 return_bad_resp:
5263 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005264 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005265 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005266 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005267 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005268 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005269 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005270 txn->status = 502;
5271 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005272 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005273 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005274 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005275 if (!(s->flags & SF_ERR_MASK))
5276 s->flags |= SF_ERR_PRXCOND;
5277 if (!(s->flags & SF_FINST_MASK))
5278 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005279 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005280 }
Willy Tarreau58975672014-04-24 21:13:57 +02005281 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005282
Willy Tarreau58975672014-04-24 21:13:57 +02005283 /* has the response been denied ? */
5284 if (txn->flags & TX_SVDENY) {
5285 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005286 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005287
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005288 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5289 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005290 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005291 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005292
Willy Tarreau58975672014-04-24 21:13:57 +02005293 goto return_srv_prx_502;
5294 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005295
Willy Tarreau58975672014-04-24 21:13:57 +02005296 /* add response headers from the rule sets in the same order */
5297 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005298 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005299 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005300 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005301 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005302 ret = acl_pass(ret);
5303 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5304 ret = !ret;
5305 if (!ret)
5306 continue;
5307 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005308 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005309 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005310 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005311
Willy Tarreau58975672014-04-24 21:13:57 +02005312 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005313 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005314 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005315 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005316 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005317
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005318 /* After this point, this anayzer can't return yield, so we can
5319 * remove the bit corresponding to this analyzer from the list.
5320 *
5321 * Note that the intermediate returns and goto found previously
5322 * reset the analyzers.
5323 */
5324 rep->analysers &= ~an_bit;
5325 rep->analyse_exp = TICK_ETERNITY;
5326
Willy Tarreau58975672014-04-24 21:13:57 +02005327 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005328 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005329 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005330
Willy Tarreau58975672014-04-24 21:13:57 +02005331 /*
5332 * Now check for a server cookie.
5333 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005334 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005335 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005336
Willy Tarreau58975672014-04-24 21:13:57 +02005337 /*
5338 * Check for cache-control or pragma headers if required.
5339 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005340 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005341 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005342
Willy Tarreau58975672014-04-24 21:13:57 +02005343 /*
5344 * Add server cookie in the response if needed
5345 */
5346 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5347 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005348 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005349 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5350 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5351 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5352 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5353 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005354 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005355 /* the server is known, it's not the one the client requested, or the
5356 * cookie's last seen date needs to be refreshed. We have to
5357 * insert a set-cookie here, except if we want to insert only on POST
5358 * requests and this one isn't. Note that servers which don't have cookies
5359 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005360 */
Willy Tarreau58975672014-04-24 21:13:57 +02005361 if (!objt_server(s->target)->cookie) {
5362 chunk_printf(&trash,
5363 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5364 s->be->cookie_name);
5365 }
5366 else {
5367 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005368
Willy Tarreau58975672014-04-24 21:13:57 +02005369 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5370 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005371 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5372 s30tob64((date.tv_sec+3) >> 2,
5373 trash.area + trash.data);
5374 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005375
Willy Tarreau58975672014-04-24 21:13:57 +02005376 if (s->be->cookie_maxlife) {
5377 /* emit first_date, which is either the original one or
5378 * the current date.
5379 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005380 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005381 s30tob64(txn->cookie_first_date ?
5382 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005383 (date.tv_sec+3) >> 2,
5384 trash.area + trash.data);
5385 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005386 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005387 }
Willy Tarreau58975672014-04-24 21:13:57 +02005388 chunk_appendf(&trash, "; path=/");
5389 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005390
Willy Tarreau58975672014-04-24 21:13:57 +02005391 if (s->be->cookie_domain)
5392 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005393
Willy Tarreau58975672014-04-24 21:13:57 +02005394 if (s->be->ck_opts & PR_CK_HTTPONLY)
5395 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005396
Willy Tarreau58975672014-04-24 21:13:57 +02005397 if (s->be->ck_opts & PR_CK_SECURE)
5398 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005399
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005400 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005401 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005402
Willy Tarreau58975672014-04-24 21:13:57 +02005403 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005404 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005405 /* the server did not change, only the date was updated */
5406 txn->flags |= TX_SCK_UPDATED;
5407 else
5408 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005409
Willy Tarreau58975672014-04-24 21:13:57 +02005410 /* Here, we will tell an eventual cache on the client side that we don't
5411 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5412 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5413 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005414 */
Willy Tarreau58975672014-04-24 21:13:57 +02005415 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005416
Willy Tarreau58975672014-04-24 21:13:57 +02005417 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005418
Willy Tarreau58975672014-04-24 21:13:57 +02005419 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5420 "Cache-control: private", 22) < 0))
5421 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005422 }
Willy Tarreau58975672014-04-24 21:13:57 +02005423 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005424
Willy Tarreau58975672014-04-24 21:13:57 +02005425 /*
5426 * Check if result will be cacheable with a cookie.
5427 * We'll block the response if security checks have caught
5428 * nasty things such as a cacheable cookie.
5429 */
5430 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5431 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5432 (s->be->options & PR_O_CHK_CACHE)) {
5433 /* we're in presence of a cacheable response containing
5434 * a set-cookie header. We'll block it as requested by
5435 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005436 */
Willy Tarreau58975672014-04-24 21:13:57 +02005437 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005438 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005439
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005440 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5441 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005442 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005443 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005444
Christopher Faulet767a84b2017-11-24 16:50:31 +01005445 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5446 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005447 send_log(s->be, LOG_ALERT,
5448 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5449 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5450 goto return_srv_prx_502;
5451 }
Willy Tarreau03945942009-12-22 16:50:27 +01005452
Willy Tarreau70730dd2014-04-24 18:06:27 +02005453 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005454 /*
5455 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5456 * If an "Upgrade" token is found, the header is left untouched in order
5457 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005458 * if anything but "Upgrade" is present in the Connection header. We don't
5459 * want to touch any 101 response either since it's switching to another
5460 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005461 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005462 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005463 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005464 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005465
Willy Tarreau58975672014-04-24 21:13:57 +02005466 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5467 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5468 /* we want a keep-alive response here. Keep-alive header
5469 * required if either side is not 1.1.
5470 */
5471 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5472 want_flags |= TX_CON_KAL_SET;
5473 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005474 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005475 /* we want a close response here. Close header required if
5476 * the server is 1.1, regardless of the client.
5477 */
5478 if (msg->flags & HTTP_MSGF_VER_11)
5479 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005480 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005481
Willy Tarreau58975672014-04-24 21:13:57 +02005482 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5483 http_change_connection_header(txn, msg, want_flags);
5484 }
5485
5486 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005487 /* Always enter in the body analyzer */
5488 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5489 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005490
Willy Tarreau58975672014-04-24 21:13:57 +02005491 /* if the user wants to log as soon as possible, without counting
5492 * bytes from the server, then this is the right moment. We have
5493 * to temporarily assign bytes_out to log what we currently have.
5494 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005495 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005496 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5497 s->logs.bytes_out = txn->rsp.eoh;
5498 s->do_log(s);
5499 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005500 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005501 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005502}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005503
Willy Tarreaud98cf932009-12-27 22:54:55 +01005504/* This function is an analyser which forwards response body (including chunk
5505 * sizes if any). It is called as soon as we must forward, even if we forward
5506 * zero byte. The only situation where it must not be called is when we're in
5507 * tunnel mode and we want to forward till the close. It's used both to forward
5508 * remaining data and to resync after end of body. It expects the msg_state to
5509 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005510 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005511 *
5512 * It is capable of compressing response data both in content-length mode and
5513 * in chunked mode. The state machines follows different flows depending on
5514 * whether content-length and chunked modes are used, since there are no
5515 * trailers in content-length :
5516 *
5517 * chk-mode cl-mode
5518 * ,----- BODY -----.
5519 * / \
5520 * V size > 0 V chk-mode
5521 * .--> SIZE -------------> DATA -------------> CRLF
5522 * | | size == 0 | last byte |
5523 * | v final crlf v inspected |
5524 * | TRAILERS -----------> DONE |
5525 * | |
5526 * `----------------------------------------------'
5527 *
5528 * Compression only happens in the DATA state, and must be flushed in final
5529 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5530 * is performed at once on final states for all bytes parsed, or when leaving
5531 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005532 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005533int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005534{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005535 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005536 struct http_txn *txn = s->txn;
5537 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005538 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005539
Christopher Faulete0768eb2018-10-03 16:38:02 +02005540 if (IS_HTX_STRM(s))
5541 return htx_response_forward_body(s, res, an_bit);
5542
Christopher Faulet45073512018-07-20 10:16:29 +02005543 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 +02005544 now_ms, __FUNCTION__,
5545 s,
5546 res,
5547 res->rex, res->wex,
5548 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005549 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005550 res->analysers);
5551
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005552 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5553 return 0;
5554
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005555 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005556 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005557 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005558 /* Output closed while we were sending data. We must abort and
5559 * wake the other side up.
5560 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005561 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005562 msg->msg_state = HTTP_MSG_ERROR;
5563 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005564 return 1;
5565 }
5566
Willy Tarreau4fe41902010-06-07 22:27:41 +02005567 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005568 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005569
Christopher Fauletd7c91962015-04-30 11:48:27 +02005570 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005571 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5572 ? HTTP_MSG_CHUNK_SIZE
5573 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005574 }
5575
Willy Tarreauefdf0942014-04-24 20:08:57 +02005576 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005577 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005578 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005579 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005580 }
5581
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005582 if (msg->msg_state < HTTP_MSG_DONE) {
5583 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5584 ? http_msg_forward_chunked_body(s, msg)
5585 : http_msg_forward_body(s, msg));
5586 if (!ret)
5587 goto missing_data_or_waiting;
5588 if (ret < 0)
5589 goto return_bad_res;
5590 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005591
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005592 /* other states, DONE...TUNNEL */
5593 /* for keep-alive we don't want to forward closes on DONE */
5594 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5595 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5596 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005597
Christopher Faulet894da4c2017-07-18 11:29:07 +02005598 http_resync_states(s);
5599 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005600 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5601 if (res->flags & CF_SHUTW) {
5602 /* response errors are most likely due to the
5603 * client aborting the transfer. */
5604 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005605 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005606 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005607 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005608 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005609 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005610 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005611 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005612 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005613
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005614 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005615 if (res->flags & CF_SHUTW)
5616 goto aborted_xfer;
5617
5618 /* stop waiting for data if the input is closed before the end. If the
5619 * client side was already closed, it means that the client has aborted,
5620 * so we don't want to count this as a server abort. Otherwise it's a
5621 * server abort.
5622 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005623 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005624 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005625 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005626 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005627 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005628 if (!(s->flags & SF_ERR_MASK))
5629 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005630 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005631 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005632 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005633 goto return_bad_res_stats_ok;
5634 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005635 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005636
Willy Tarreau40dba092010-03-04 18:14:51 +01005637 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005638 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005639 goto return_bad_res;
5640
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005641 /* When TE: chunked is used, we need to get there again to parse
5642 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005643 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5644 * or if there are filters registered on the stream, we don't want to
5645 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005646 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005647 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005648 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005649 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5650 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005651 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005652
Willy Tarreau5c620922011-05-11 19:56:11 +02005653 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005654 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005655 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005656 * modes are already handled by the stream sock layer. We must not do
5657 * this in content-length mode because it could present the MSG_MORE
5658 * flag with the last block of forwarded data, which would cause an
5659 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005660 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005661 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005662 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005663
Willy Tarreau87b09662015-04-03 00:22:06 +02005664 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005665 return 0;
5666
Willy Tarreau40dba092010-03-04 18:14:51 +01005667 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005668 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005669 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005670 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005671
5672 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005673 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005674 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005675 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005676 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005677 res->analysers &= AN_RES_FLT_END;
5678 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 +01005679 if (objt_server(s->target))
5680 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005681
Willy Tarreaue7dff022015-04-03 01:14:29 +02005682 if (!(s->flags & SF_ERR_MASK))
5683 s->flags |= SF_ERR_PRXCOND;
5684 if (!(s->flags & SF_FINST_MASK))
5685 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005686 return 0;
5687
5688 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005689 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005690 txn->rsp.msg_state = HTTP_MSG_ERROR;
5691 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005692 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005693 res->analysers &= AN_RES_FLT_END;
5694 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 +01005695
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005696 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5697 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005698 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005699 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005700
Willy Tarreaue7dff022015-04-03 01:14:29 +02005701 if (!(s->flags & SF_ERR_MASK))
5702 s->flags |= SF_ERR_CLICL;
5703 if (!(s->flags & SF_FINST_MASK))
5704 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005705 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005706}
5707
5708
Christopher Faulet10079f52018-10-03 15:17:28 +02005709int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005710{
5711 struct channel *chn = msg->chn;
5712 int ret;
5713
5714 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5715
5716 if (msg->msg_state == HTTP_MSG_ENDING)
5717 goto ending;
5718
5719 /* Neither content-length, nor transfer-encoding was found, so we must
5720 * read the body until the server connection is closed. In that case, we
5721 * eat data as they come. Of course, this happens for response only. */
5722 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005723 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005724 msg->chunk_len += len;
5725 msg->body_len += len;
5726 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005727 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005728 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005729 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005730 msg->next += ret;
5731 msg->chunk_len -= ret;
5732 if (msg->chunk_len) {
5733 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005734 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005735 chn->flags |= CF_WAKE_WRITE;
5736 goto missing_data_or_waiting;
5737 }
5738
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005739 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5740 * always set for a request. */
5741 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5742 /* The server still sending data that should be filtered */
5743 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5744 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005745 msg->msg_state = HTTP_MSG_TUNNEL;
5746 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005747 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005748
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005749 msg->msg_state = HTTP_MSG_ENDING;
5750
5751 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005752 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005753 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005754 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5755 /* default_ret */ msg->next,
5756 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005757 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005758 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005759 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5760 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005761 if (msg->next)
5762 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005763
Christopher Fauletda02e172015-12-04 09:25:05 +01005764 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5765 /* default_ret */ 1,
5766 /* on_error */ goto error,
5767 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005768 if (msg->msg_state == HTTP_MSG_ENDING)
5769 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005770 return 1;
5771
5772 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005773 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005774 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5775 /* default_ret */ msg->next,
5776 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005777 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005778 msg->next -= ret;
5779 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5780 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005781 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005782 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005783 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005784 return 0;
5785 error:
5786 return -1;
5787}
5788
Christopher Faulet10079f52018-10-03 15:17:28 +02005789int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005790{
5791 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005792 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005793 int ret;
5794
5795 /* Here we have the guarantee to be in one of the following state:
5796 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5797 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5798
Christopher Fauletca874b82018-09-20 11:31:01 +02005799 if (msg->msg_state == HTTP_MSG_ENDING)
5800 goto ending;
5801
5802 /* Don't parse chunks if there is no input data */
5803 if (!ci_data(chn))
5804 goto waiting;
5805
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005806 switch_states:
5807 switch (msg->msg_state) {
5808 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005809 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005810 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005811 /* on_error */ goto error);
5812 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005813 msg->chunk_len -= ret;
5814 if (msg->chunk_len) {
5815 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005816 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005817 chn->flags |= CF_WAKE_WRITE;
5818 goto missing_data_or_waiting;
5819 }
5820
5821 /* nothing left to forward for this chunk*/
5822 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5823 /* fall through for HTTP_MSG_CHUNK_CRLF */
5824
5825 case HTTP_MSG_CHUNK_CRLF:
5826 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005827 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005828 if (ret == 0)
5829 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005830 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005831 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005832 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005833 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005834 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005835 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005836 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005837 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5838 /* fall through for HTTP_MSG_CHUNK_SIZE */
5839
5840 case HTTP_MSG_CHUNK_SIZE:
5841 /* read the chunk size and assign it to ->chunk_len,
5842 * then set ->next to point to the body and switch to
5843 * DATA or TRAILERS state.
5844 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005845 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005846 if (ret == 0)
5847 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005848 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005849 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005850 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005851 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005852 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005853 }
5854
5855 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005856 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005857 msg->chunk_len = chunk;
5858 msg->body_len += chunk;
5859
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005860 if (msg->chunk_len) {
5861 msg->msg_state = HTTP_MSG_DATA;
5862 goto switch_states;
5863 }
5864 msg->msg_state = HTTP_MSG_TRAILERS;
5865 /* fall through for HTTP_MSG_TRAILERS */
5866
5867 case HTTP_MSG_TRAILERS:
5868 ret = http_forward_trailers(msg);
5869 if (ret < 0)
5870 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005871 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5872 /* default_ret */ 1,
5873 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005874 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005875 if (!ret)
5876 goto missing_data_or_waiting;
5877 break;
5878
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005879 default:
5880 /* This should no happen in this function */
5881 goto error;
5882 }
5883
5884 msg->msg_state = HTTP_MSG_ENDING;
5885 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005886 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005887 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005888 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005889 /* default_ret */ msg->next,
5890 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005891 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005892 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005893 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5894 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005895 if (msg->next)
5896 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005897
Christopher Fauletda02e172015-12-04 09:25:05 +01005898 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005899 /* default_ret */ 1,
5900 /* on_error */ goto error,
5901 /* on_wait */ goto waiting);
5902 msg->msg_state = HTTP_MSG_DONE;
5903 return 1;
5904
5905 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005906 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005907 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005908 /* default_ret */ msg->next,
5909 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005910 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005911 msg->next -= ret;
5912 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5913 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005914 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005915 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005916 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005917 return 0;
5918
5919 chunk_parsing_error:
5920 if (msg->err_pos >= 0) {
5921 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005922 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005923 msg->msg_state, strm_fe(s));
5924 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005925 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005926 msg, msg->msg_state, s->be);
5927 }
5928 error:
5929 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005930}
5931
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005932
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005933/* Iterate the same filter through all request headers.
5934 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005935 * Since it can manage the switch to another backend, it updates the per-proxy
5936 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005937 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005938int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005939{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005940 char *cur_ptr, *cur_end, *cur_next;
5941 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005942 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005943 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005944 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005945
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005946 last_hdr = 0;
5947
Willy Tarreauf37954d2018-06-15 18:31:02 +02005948 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005949 old_idx = 0;
5950
5951 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005952 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005953 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005954 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005955 (exp->action == ACT_ALLOW ||
5956 exp->action == ACT_DENY ||
5957 exp->action == ACT_TARPIT))
5958 return 0;
5959
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005960 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005961 if (!cur_idx)
5962 break;
5963
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005964 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005965 cur_ptr = cur_next;
5966 cur_end = cur_ptr + cur_hdr->len;
5967 cur_next = cur_end + cur_hdr->cr + 1;
5968
5969 /* Now we have one header between cur_ptr and cur_end,
5970 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005971 */
5972
Willy Tarreau15a53a42015-01-21 13:39:42 +01005973 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005974 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005975 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005976 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005977 last_hdr = 1;
5978 break;
5979
5980 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005981 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005982 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005983 break;
5984
5985 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005986 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005987 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005988 break;
5989
5990 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005991 len = exp_replace(trash.area,
5992 trash.size, cur_ptr,
5993 exp->replace, pmatch);
5994 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005995 return -1;
5996
Willy Tarreau6e27be12018-08-22 04:46:47 +02005997 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5998
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005999 /* FIXME: if the user adds a newline in the replacement, the
6000 * index will not be recalculated for now, and the new line
6001 * will not be counted as a new header.
6002 */
6003
6004 cur_end += delta;
6005 cur_next += delta;
6006 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006007 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006008 break;
6009
6010 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006011 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006012 cur_next += delta;
6013
Willy Tarreaufa355d42009-11-29 18:12:29 +01006014 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006015 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6016 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006017 cur_hdr->len = 0;
6018 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006019 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006020 break;
6021
6022 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006023 }
6024
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006025 /* keep the link from this header to next one in case of later
6026 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006027 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006028 old_idx = cur_idx;
6029 }
6030 return 0;
6031}
6032
6033
6034/* Apply the filter to the request line.
6035 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6036 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006037 * Since it can manage the switch to another backend, it updates the per-proxy
6038 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006039 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006040int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006041{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006042 char *cur_ptr, *cur_end;
6043 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006044 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006045 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006046
Willy Tarreau3d300592007-03-18 18:34:41 +01006047 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006048 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006049 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006050 (exp->action == ACT_ALLOW ||
6051 exp->action == ACT_DENY ||
6052 exp->action == ACT_TARPIT))
6053 return 0;
6054 else if (exp->action == ACT_REMOVE)
6055 return 0;
6056
6057 done = 0;
6058
Willy Tarreauf37954d2018-06-15 18:31:02 +02006059 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006060 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006061
6062 /* Now we have the request line between cur_ptr and cur_end */
6063
Willy Tarreau15a53a42015-01-21 13:39:42 +01006064 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006065 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006066 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006067 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006068 done = 1;
6069 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006071 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006072 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006073 done = 1;
6074 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006075
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006076 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006077 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006078 done = 1;
6079 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006080
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006081 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006082 len = exp_replace(trash.area, trash.size,
6083 cur_ptr, exp->replace, pmatch);
6084 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006085 return -1;
6086
Willy Tarreau6e27be12018-08-22 04:46:47 +02006087 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6088
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006089 /* FIXME: if the user adds a newline in the replacement, the
6090 * index will not be recalculated for now, and the new line
6091 * will not be counted as a new header.
6092 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006093
Willy Tarreaufa355d42009-11-29 18:12:29 +01006094 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006095 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006096 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006097 HTTP_MSG_RQMETH,
6098 cur_ptr, cur_end + 1,
6099 NULL, NULL);
6100 if (unlikely(!cur_end))
6101 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006102
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006103 /* we have a full request and we know that we have either a CR
6104 * or an LF at <ptr>.
6105 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006106 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6107 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006108 /* there is no point trying this regex on headers */
6109 return 1;
6110 }
6111 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006112 return done;
6113}
Willy Tarreau97de6242006-12-27 17:18:38 +01006114
Willy Tarreau58f10d72006-12-04 02:26:12 +01006115
Willy Tarreau58f10d72006-12-04 02:26:12 +01006116
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006117/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006118 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006119 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006120 * unparsable request. Since it can manage the switch to another backend, it
6121 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006122 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006123int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006124{
Willy Tarreau192252e2015-04-04 01:47:55 +02006125 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006126 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006127 struct hdr_exp *exp;
6128
6129 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006130 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006132 /*
6133 * The interleaving of transformations and verdicts
6134 * makes it difficult to decide to continue or stop
6135 * the evaluation.
6136 */
6137
Willy Tarreau6c123b12010-01-28 20:22:06 +01006138 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6139 break;
6140
Willy Tarreau3d300592007-03-18 18:34:41 +01006141 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006142 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006143 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006144 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006145
6146 /* if this filter had a condition, evaluate it now and skip to
6147 * next filter if the condition does not match.
6148 */
6149 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006150 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006151 ret = acl_pass(ret);
6152 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6153 ret = !ret;
6154
6155 if (!ret)
6156 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006157 }
6158
6159 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006160 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006161 if (unlikely(ret < 0))
6162 return -1;
6163
6164 if (likely(ret == 0)) {
6165 /* The filter did not match the request, it can be
6166 * iterated through all headers.
6167 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006168 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6169 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006170 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006171 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006172 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006173}
6174
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006175
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006176/* Delete a value in a header between delimiters <from> and <next> in buffer
6177 * <buf>. The number of characters displaced is returned, and the pointer to
6178 * the first delimiter is updated if required. The function tries as much as
6179 * possible to respect the following principles :
6180 * - replace <from> delimiter by the <next> one unless <from> points to a
6181 * colon, in which case <next> is simply removed
6182 * - set exactly one space character after the new first delimiter, unless
6183 * there are not enough characters in the block being moved to do so.
6184 * - remove unneeded spaces before the previous delimiter and after the new
6185 * one.
6186 *
6187 * It is the caller's responsibility to ensure that :
6188 * - <from> points to a valid delimiter or the colon ;
6189 * - <next> points to a valid delimiter or the final CR/LF ;
6190 * - there are non-space chars before <from> ;
6191 * - there is a CR/LF at or after <next>.
6192 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006193int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006194{
6195 char *prev = *from;
6196
6197 if (*prev == ':') {
6198 /* We're removing the first value, preserve the colon and add a
6199 * space if possible.
6200 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006201 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006202 next++;
6203 prev++;
6204 if (prev < next)
6205 *prev++ = ' ';
6206
Willy Tarreau2235b262016-11-05 15:50:20 +01006207 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006208 next++;
6209 } else {
6210 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006211 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006212 prev--;
6213 *from = prev;
6214
6215 /* copy the delimiter and if possible a space if we're
6216 * not at the end of the line.
6217 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006218 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006219 *prev++ = *next++;
6220 if (prev + 1 < next)
6221 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006222 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006223 next++;
6224 }
6225 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006226 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006227}
6228
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006229/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006230 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006231 * desirable to call it only when needed. This code is quite complex because
6232 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6233 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006234 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006235void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006236{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006237 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006238 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006239 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006240 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006241 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6242 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006243
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006244 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006245 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006246 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006247
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006248 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006249 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006250 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006251
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006252 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006253 hdr_beg = hdr_next;
6254 hdr_end = hdr_beg + cur_hdr->len;
6255 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006256
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006257 /* We have one full header between hdr_beg and hdr_end, and the
6258 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006259 * "Cookie:" headers.
6260 */
6261
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006262 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006263 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006264 old_idx = cur_idx;
6265 continue;
6266 }
6267
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006268 del_from = NULL; /* nothing to be deleted */
6269 preserve_hdr = 0; /* assume we may kill the whole header */
6270
Willy Tarreau58f10d72006-12-04 02:26:12 +01006271 /* Now look for cookies. Conforming to RFC2109, we have to support
6272 * attributes whose name begin with a '$', and associate them with
6273 * the right cookie, if we want to delete this cookie.
6274 * So there are 3 cases for each cookie read :
6275 * 1) it's a special attribute, beginning with a '$' : ignore it.
6276 * 2) it's a server id cookie that we *MAY* want to delete : save
6277 * some pointers on it (last semi-colon, beginning of cookie...)
6278 * 3) it's an application cookie : we *MAY* have to delete a previous
6279 * "special" cookie.
6280 * At the end of loop, if a "special" cookie remains, we may have to
6281 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006282 * *MUST* delete it.
6283 *
6284 * Note: RFC2965 is unclear about the processing of spaces around
6285 * the equal sign in the ATTR=VALUE form. A careful inspection of
6286 * the RFC explicitly allows spaces before it, and not within the
6287 * tokens (attrs or values). An inspection of RFC2109 allows that
6288 * too but section 10.1.3 lets one think that spaces may be allowed
6289 * after the equal sign too, resulting in some (rare) buggy
6290 * implementations trying to do that. So let's do what servers do.
6291 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6292 * allowed quoted strings in values, with any possible character
6293 * after a backslash, including control chars and delimitors, which
6294 * causes parsing to become ambiguous. Browsers also allow spaces
6295 * within values even without quotes.
6296 *
6297 * We have to keep multiple pointers in order to support cookie
6298 * removal at the beginning, middle or end of header without
6299 * corrupting the header. All of these headers are valid :
6300 *
6301 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6302 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6303 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6304 * | | | | | | | | |
6305 * | | | | | | | | hdr_end <--+
6306 * | | | | | | | +--> next
6307 * | | | | | | +----> val_end
6308 * | | | | | +-----------> val_beg
6309 * | | | | +--------------> equal
6310 * | | | +----------------> att_end
6311 * | | +---------------------> att_beg
6312 * | +--------------------------> prev
6313 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006314 */
6315
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006316 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6317 /* Iterate through all cookies on this line */
6318
6319 /* find att_beg */
6320 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006321 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006322 att_beg++;
6323
6324 /* find att_end : this is the first character after the last non
6325 * space before the equal. It may be equal to hdr_end.
6326 */
6327 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006328
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006329 while (equal < hdr_end) {
6330 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006331 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006332 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006333 continue;
6334 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006335 }
6336
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006337 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6338 * is between <att_beg> and <equal>, both may be identical.
6339 */
6340
6341 /* look for end of cookie if there is an equal sign */
6342 if (equal < hdr_end && *equal == '=') {
6343 /* look for the beginning of the value */
6344 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006345 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006346 val_beg++;
6347
6348 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006349 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006350
6351 /* make val_end point to the first white space or delimitor after the value */
6352 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006353 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006354 val_end--;
6355 } else {
6356 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006357 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006358
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006359 /* We have nothing to do with attributes beginning with '$'. However,
6360 * they will automatically be removed if a header before them is removed,
6361 * since they're supposed to be linked together.
6362 */
6363 if (*att_beg == '$')
6364 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006365
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006366 /* Ignore cookies with no equal sign */
6367 if (equal == next) {
6368 /* This is not our cookie, so we must preserve it. But if we already
6369 * scheduled another cookie for removal, we cannot remove the
6370 * complete header, but we can remove the previous block itself.
6371 */
6372 preserve_hdr = 1;
6373 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006374 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006375 val_end += delta;
6376 next += delta;
6377 hdr_end += delta;
6378 hdr_next += delta;
6379 cur_hdr->len += delta;
6380 http_msg_move_end(&txn->req, delta);
6381 prev = del_from;
6382 del_from = NULL;
6383 }
6384 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006385 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006386
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006387 /* if there are spaces around the equal sign, we need to
6388 * strip them otherwise we'll get trouble for cookie captures,
6389 * or even for rewrites. Since this happens extremely rarely,
6390 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006391 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006392 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6393 int stripped_before = 0;
6394 int stripped_after = 0;
6395
6396 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006397 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006398 equal += stripped_before;
6399 val_beg += stripped_before;
6400 }
6401
6402 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006403 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006404 val_beg += stripped_after;
6405 stripped_before += stripped_after;
6406 }
6407
6408 val_end += stripped_before;
6409 next += stripped_before;
6410 hdr_end += stripped_before;
6411 hdr_next += stripped_before;
6412 cur_hdr->len += stripped_before;
6413 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006414 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006415 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006416
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006417 /* First, let's see if we want to capture this cookie. We check
6418 * that we don't already have a client side cookie, because we
6419 * can only capture one. Also as an optimisation, we ignore
6420 * cookies shorter than the declared name.
6421 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006422 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6423 (val_end - att_beg >= sess->fe->capture_namelen) &&
6424 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006425 int log_len = val_end - att_beg;
6426
Willy Tarreaubafbe012017-11-24 17:34:44 +01006427 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006428 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006429 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006430 if (log_len > sess->fe->capture_len)
6431 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006432 memcpy(txn->cli_cookie, att_beg, log_len);
6433 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006434 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006435 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006436
Willy Tarreaubca99692010-10-06 19:25:55 +02006437 /* Persistence cookies in passive, rewrite or insert mode have the
6438 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006439 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006440 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006441 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006442 * For cookies in prefix mode, the form is :
6443 *
6444 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006445 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006446 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6447 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6448 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006449 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006450
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006451 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6452 * have the server ID between val_beg and delim, and the original cookie between
6453 * delim+1 and val_end. Otherwise, delim==val_end :
6454 *
6455 * Cookie: NAME=SRV; # in all but prefix modes
6456 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6457 * | || || | |+-> next
6458 * | || || | +--> val_end
6459 * | || || +---------> delim
6460 * | || |+------------> val_beg
6461 * | || +-------------> att_end = equal
6462 * | |+-----------------> att_beg
6463 * | +------------------> prev
6464 * +-------------------------> hdr_beg
6465 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006466
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006467 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006468 for (delim = val_beg; delim < val_end; delim++)
6469 if (*delim == COOKIE_DELIM)
6470 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006471 } else {
6472 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006473 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006474 /* Now check if the cookie contains a date field, which would
6475 * appear after a vertical bar ('|') just after the server name
6476 * and before the delimiter.
6477 */
6478 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6479 if (vbar1) {
6480 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006481 * right is the last seen date. It is a base64 encoded
6482 * 30-bit value representing the UNIX date since the
6483 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006484 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006485 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006486 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006487 if (val_end - vbar1 >= 5) {
6488 val = b64tos30(vbar1);
6489 if (val > 0)
6490 txn->cookie_last_date = val << 2;
6491 }
6492 /* look for a second vertical bar */
6493 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6494 if (vbar1 && (val_end - vbar1 > 5)) {
6495 val = b64tos30(vbar1 + 1);
6496 if (val > 0)
6497 txn->cookie_first_date = val << 2;
6498 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006499 }
6500 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006501
Willy Tarreauf64d1412010-10-07 20:06:11 +02006502 /* if the cookie has an expiration date and the proxy wants to check
6503 * it, then we do that now. We first check if the cookie is too old,
6504 * then only if it has expired. We detect strict overflow because the
6505 * time resolution here is not great (4 seconds). Cookies with dates
6506 * in the future are ignored if their offset is beyond one day. This
6507 * allows an admin to fix timezone issues without expiring everyone
6508 * and at the same time avoids keeping unwanted side effects for too
6509 * long.
6510 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006511 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6512 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006513 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006514 txn->flags &= ~TX_CK_MASK;
6515 txn->flags |= TX_CK_OLD;
6516 delim = val_beg; // let's pretend we have not found the cookie
6517 txn->cookie_first_date = 0;
6518 txn->cookie_last_date = 0;
6519 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006520 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6521 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006522 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006523 txn->flags &= ~TX_CK_MASK;
6524 txn->flags |= TX_CK_EXPIRED;
6525 delim = val_beg; // let's pretend we have not found the cookie
6526 txn->cookie_first_date = 0;
6527 txn->cookie_last_date = 0;
6528 }
6529
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006530 /* Here, we'll look for the first running server which supports the cookie.
6531 * This allows to share a same cookie between several servers, for example
6532 * to dedicate backup servers to specific servers only.
6533 * However, to prevent clients from sticking to cookie-less backup server
6534 * when they have incidentely learned an empty cookie, we simply ignore
6535 * empty cookies and mark them as invalid.
6536 * The same behaviour is applied when persistence must be ignored.
6537 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006538 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006539 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006540
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006541 while (srv) {
6542 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6543 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006544 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006545 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006546 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006547 /* we found the server and we can use it */
6548 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006549 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006550 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006551 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006552 break;
6553 } else {
6554 /* we found a server, but it's down,
6555 * mark it as such and go on in case
6556 * another one is available.
6557 */
6558 txn->flags &= ~TX_CK_MASK;
6559 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006560 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006561 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006562 srv = srv->next;
6563 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006564
Willy Tarreauf64d1412010-10-07 20:06:11 +02006565 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006566 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006567 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006568 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006569 txn->flags |= TX_CK_UNUSED;
6570 else
6571 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006572 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006573
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006574 /* depending on the cookie mode, we may have to either :
6575 * - delete the complete cookie if we're in insert+indirect mode, so that
6576 * the server never sees it ;
6577 * - remove the server id from the cookie value, and tag the cookie as an
6578 * application cookie so that it does not get accidentely removed later,
6579 * if we're in cookie prefix mode
6580 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006581 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006582 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006583
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006584 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006585 val_end += delta;
6586 next += delta;
6587 hdr_end += delta;
6588 hdr_next += delta;
6589 cur_hdr->len += delta;
6590 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006591
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006592 del_from = NULL;
6593 preserve_hdr = 1; /* we want to keep this cookie */
6594 }
6595 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006596 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006597 del_from = prev;
6598 }
6599 } else {
6600 /* This is not our cookie, so we must preserve it. But if we already
6601 * scheduled another cookie for removal, we cannot remove the
6602 * complete header, but we can remove the previous block itself.
6603 */
6604 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006605
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006606 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006607 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006608 if (att_beg >= del_from)
6609 att_beg += delta;
6610 if (att_end >= del_from)
6611 att_end += delta;
6612 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006613 val_end += delta;
6614 next += delta;
6615 hdr_end += delta;
6616 hdr_next += delta;
6617 cur_hdr->len += delta;
6618 http_msg_move_end(&txn->req, delta);
6619 prev = del_from;
6620 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006621 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006622 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006623
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006624 /* continue with next cookie on this header line */
6625 att_beg = next;
6626 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006627
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006628 /* There are no more cookies on this line.
6629 * We may still have one (or several) marked for deletion at the
6630 * end of the line. We must do this now in two ways :
6631 * - if some cookies must be preserved, we only delete from the
6632 * mark to the end of line ;
6633 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006634 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006635 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006636 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006637 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006638 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006639 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006640 cur_hdr->len += delta;
6641 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006642 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006643
6644 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006645 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6646 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006647 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006648 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006649 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006650 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006651 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006652 }
6653
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006654 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006655 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006656 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006657}
6658
6659
Willy Tarreaua15645d2007-03-18 16:22:39 +01006660/* Iterate the same filter through all response headers contained in <rtr>.
6661 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6662 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006663int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006664{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006665 char *cur_ptr, *cur_end, *cur_next;
6666 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006667 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006668 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006669 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006670
6671 last_hdr = 0;
6672
Willy Tarreauf37954d2018-06-15 18:31:02 +02006673 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006674 old_idx = 0;
6675
6676 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006677 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006678 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006679 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006680 (exp->action == ACT_ALLOW ||
6681 exp->action == ACT_DENY))
6682 return 0;
6683
6684 cur_idx = txn->hdr_idx.v[old_idx].next;
6685 if (!cur_idx)
6686 break;
6687
6688 cur_hdr = &txn->hdr_idx.v[cur_idx];
6689 cur_ptr = cur_next;
6690 cur_end = cur_ptr + cur_hdr->len;
6691 cur_next = cur_end + cur_hdr->cr + 1;
6692
6693 /* Now we have one header between cur_ptr and cur_end,
6694 * and the next header starts at cur_next.
6695 */
6696
Willy Tarreau15a53a42015-01-21 13:39:42 +01006697 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006698 switch (exp->action) {
6699 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006700 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701 last_hdr = 1;
6702 break;
6703
6704 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006705 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006706 last_hdr = 1;
6707 break;
6708
6709 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006710 len = exp_replace(trash.area,
6711 trash.size, cur_ptr,
6712 exp->replace, pmatch);
6713 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006714 return -1;
6715
Willy Tarreau6e27be12018-08-22 04:46:47 +02006716 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6717
Willy Tarreaua15645d2007-03-18 16:22:39 +01006718 /* FIXME: if the user adds a newline in the replacement, the
6719 * index will not be recalculated for now, and the new line
6720 * will not be counted as a new header.
6721 */
6722
6723 cur_end += delta;
6724 cur_next += delta;
6725 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006726 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006727 break;
6728
6729 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006730 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006731 cur_next += delta;
6732
Willy Tarreaufa355d42009-11-29 18:12:29 +01006733 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006734 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6735 txn->hdr_idx.used--;
6736 cur_hdr->len = 0;
6737 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006738 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006739 break;
6740
6741 }
6742 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006743
6744 /* keep the link from this header to next one in case of later
6745 * removal of next header.
6746 */
6747 old_idx = cur_idx;
6748 }
6749 return 0;
6750}
6751
6752
6753/* Apply the filter to the status line in the response buffer <rtr>.
6754 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6755 * or -1 if a replacement resulted in an invalid status line.
6756 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006757int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006758{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006759 char *cur_ptr, *cur_end;
6760 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006761 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006762 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006763
Willy Tarreau3d300592007-03-18 18:34:41 +01006764 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006765 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006766 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006767 (exp->action == ACT_ALLOW ||
6768 exp->action == ACT_DENY))
6769 return 0;
6770 else if (exp->action == ACT_REMOVE)
6771 return 0;
6772
6773 done = 0;
6774
Willy Tarreauf37954d2018-06-15 18:31:02 +02006775 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006776 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006777
6778 /* Now we have the status line between cur_ptr and cur_end */
6779
Willy Tarreau15a53a42015-01-21 13:39:42 +01006780 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006781 switch (exp->action) {
6782 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006783 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006784 done = 1;
6785 break;
6786
6787 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006788 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006789 done = 1;
6790 break;
6791
6792 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006793 len = exp_replace(trash.area, trash.size,
6794 cur_ptr, exp->replace, pmatch);
6795 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006796 return -1;
6797
Willy Tarreau6e27be12018-08-22 04:46:47 +02006798 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6799
Willy Tarreaua15645d2007-03-18 16:22:39 +01006800 /* FIXME: if the user adds a newline in the replacement, the
6801 * index will not be recalculated for now, and the new line
6802 * will not be counted as a new header.
6803 */
6804
Willy Tarreaufa355d42009-11-29 18:12:29 +01006805 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006806 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006807 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006808 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006809 cur_ptr, cur_end + 1,
6810 NULL, NULL);
6811 if (unlikely(!cur_end))
6812 return -1;
6813
6814 /* we have a full respnse and we know that we have either a CR
6815 * or an LF at <ptr>.
6816 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006817 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006818 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006819 /* there is no point trying this regex on headers */
6820 return 1;
6821 }
6822 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006823 return done;
6824}
6825
6826
6827
6828/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006829 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006830 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6831 * unparsable response.
6832 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006833int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006834{
Willy Tarreau192252e2015-04-04 01:47:55 +02006835 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006836 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006837 struct hdr_exp *exp;
6838
6839 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006840 int ret;
6841
6842 /*
6843 * The interleaving of transformations and verdicts
6844 * makes it difficult to decide to continue or stop
6845 * the evaluation.
6846 */
6847
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006848 if (txn->flags & TX_SVDENY)
6849 break;
6850
Willy Tarreau3d300592007-03-18 18:34:41 +01006851 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006852 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6853 exp->action == ACT_PASS)) {
6854 exp = exp->next;
6855 continue;
6856 }
6857
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006858 /* if this filter had a condition, evaluate it now and skip to
6859 * next filter if the condition does not match.
6860 */
6861 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006862 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006863 ret = acl_pass(ret);
6864 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6865 ret = !ret;
6866 if (!ret)
6867 continue;
6868 }
6869
Willy Tarreaua15645d2007-03-18 16:22:39 +01006870 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006871 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006872 if (unlikely(ret < 0))
6873 return -1;
6874
6875 if (likely(ret == 0)) {
6876 /* The filter did not match the response, it can be
6877 * iterated through all headers.
6878 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006879 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6880 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006881 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006882 }
6883 return 0;
6884}
6885
6886
Willy Tarreaua15645d2007-03-18 16:22:39 +01006887/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006888 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006889 * desirable to call it only when needed. This function is also used when we
6890 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006891 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006892void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006893{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006894 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006895 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006896 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006897 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006898 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006899 char *hdr_beg, *hdr_end, *hdr_next;
6900 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006901
Willy Tarreaua15645d2007-03-18 16:22:39 +01006902 /* Iterate through the headers.
6903 * we start with the start line.
6904 */
6905 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006906 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006907
6908 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6909 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006910 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006911
6912 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006913 hdr_beg = hdr_next;
6914 hdr_end = hdr_beg + cur_hdr->len;
6915 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006916
Willy Tarreau24581ba2010-08-31 22:39:35 +02006917 /* We have one full header between hdr_beg and hdr_end, and the
6918 * next header starts at hdr_next. We're only interested in
6919 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006920 */
6921
Willy Tarreau24581ba2010-08-31 22:39:35 +02006922 is_cookie2 = 0;
6923 prev = hdr_beg + 10;
6924 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006925 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006926 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6927 if (!val) {
6928 old_idx = cur_idx;
6929 continue;
6930 }
6931 is_cookie2 = 1;
6932 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006933 }
6934
Willy Tarreau24581ba2010-08-31 22:39:35 +02006935 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6936 * <prev> points to the colon.
6937 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006938 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006939
Willy Tarreau24581ba2010-08-31 22:39:35 +02006940 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6941 * check-cache is enabled) and we are not interested in checking
6942 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006943 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006944 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006945 return;
6946
Willy Tarreau24581ba2010-08-31 22:39:35 +02006947 /* OK so now we know we have to process this response cookie.
6948 * The format of the Set-Cookie header is slightly different
6949 * from the format of the Cookie header in that it does not
6950 * support the comma as a cookie delimiter (thus the header
6951 * cannot be folded) because the Expires attribute described in
6952 * the original Netscape's spec may contain an unquoted date
6953 * with a comma inside. We have to live with this because
6954 * many browsers don't support Max-Age and some browsers don't
6955 * support quoted strings. However the Set-Cookie2 header is
6956 * clean.
6957 *
6958 * We have to keep multiple pointers in order to support cookie
6959 * removal at the beginning, middle or end of header without
6960 * corrupting the header (in case of set-cookie2). A special
6961 * pointer, <scav> points to the beginning of the set-cookie-av
6962 * fields after the first semi-colon. The <next> pointer points
6963 * either to the end of line (set-cookie) or next unquoted comma
6964 * (set-cookie2). All of these headers are valid :
6965 *
6966 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6967 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6968 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6969 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6970 * | | | | | | | | | |
6971 * | | | | | | | | +-> next hdr_end <--+
6972 * | | | | | | | +------------> scav
6973 * | | | | | | +--------------> val_end
6974 * | | | | | +--------------------> val_beg
6975 * | | | | +----------------------> equal
6976 * | | | +------------------------> att_end
6977 * | | +----------------------------> att_beg
6978 * | +------------------------------> prev
6979 * +-----------------------------------------> hdr_beg
6980 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006981
Willy Tarreau24581ba2010-08-31 22:39:35 +02006982 for (; prev < hdr_end; prev = next) {
6983 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006984
Willy Tarreau24581ba2010-08-31 22:39:35 +02006985 /* find att_beg */
6986 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006987 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006988 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006989
Willy Tarreau24581ba2010-08-31 22:39:35 +02006990 /* find att_end : this is the first character after the last non
6991 * space before the equal. It may be equal to hdr_end.
6992 */
6993 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994
Willy Tarreau24581ba2010-08-31 22:39:35 +02006995 while (equal < hdr_end) {
6996 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6997 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006998 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006999 continue;
7000 att_end = equal;
7001 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007002
Willy Tarreau24581ba2010-08-31 22:39:35 +02007003 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7004 * is between <att_beg> and <equal>, both may be identical.
7005 */
7006
7007 /* look for end of cookie if there is an equal sign */
7008 if (equal < hdr_end && *equal == '=') {
7009 /* look for the beginning of the value */
7010 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007011 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007012 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007013
Willy Tarreau24581ba2010-08-31 22:39:35 +02007014 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02007015 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007016
7017 /* make val_end point to the first white space or delimitor after the value */
7018 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007019 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007020 val_end--;
7021 } else {
7022 /* <equal> points to next comma, semi-colon or EOL */
7023 val_beg = val_end = next = equal;
7024 }
7025
7026 if (next < hdr_end) {
7027 /* Set-Cookie2 supports multiple cookies, and <next> points to
7028 * a colon or semi-colon before the end. So skip all attr-value
7029 * pairs and look for the next comma. For Set-Cookie, since
7030 * commas are permitted in values, skip to the end.
7031 */
7032 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02007033 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007034 else
7035 next = hdr_end;
7036 }
7037
7038 /* Now everything is as on the diagram above */
7039
7040 /* Ignore cookies with no equal sign */
7041 if (equal == val_end)
7042 continue;
7043
7044 /* If there are spaces around the equal sign, we need to
7045 * strip them otherwise we'll get trouble for cookie captures,
7046 * or even for rewrites. Since this happens extremely rarely,
7047 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007048 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007049 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7050 int stripped_before = 0;
7051 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007052
Willy Tarreau24581ba2010-08-31 22:39:35 +02007053 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007054 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007055 equal += stripped_before;
7056 val_beg += stripped_before;
7057 }
7058
7059 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007060 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007061 val_beg += stripped_after;
7062 stripped_before += stripped_after;
7063 }
7064
7065 val_end += stripped_before;
7066 next += stripped_before;
7067 hdr_end += stripped_before;
7068 hdr_next += stripped_before;
7069 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007070 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007071 }
7072
7073 /* First, let's see if we want to capture this cookie. We check
7074 * that we don't already have a server side cookie, because we
7075 * can only capture one. Also as an optimisation, we ignore
7076 * cookies shorter than the declared name.
7077 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007078 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007079 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007080 (val_end - att_beg >= sess->fe->capture_namelen) &&
7081 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007082 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007083 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007084 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007085 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007086 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007087 if (log_len > sess->fe->capture_len)
7088 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007089 memcpy(txn->srv_cookie, att_beg, log_len);
7090 txn->srv_cookie[log_len] = 0;
7091 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007092 }
7093
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007094 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007095 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007096 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007097 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7098 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007099 /* assume passive cookie by default */
7100 txn->flags &= ~TX_SCK_MASK;
7101 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007102
7103 /* If the cookie is in insert mode on a known server, we'll delete
7104 * this occurrence because we'll insert another one later.
7105 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007106 * a direct access.
7107 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007108 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007109 /* The "preserve" flag was set, we don't want to touch the
7110 * server's cookie.
7111 */
7112 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007113 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007114 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007115 /* this cookie must be deleted */
7116 if (*prev == ':' && next == hdr_end) {
7117 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007118 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007119 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7120 txn->hdr_idx.used--;
7121 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007122 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007123 hdr_next += delta;
7124 http_msg_move_end(&txn->rsp, delta);
7125 /* note: while both invalid now, <next> and <hdr_end>
7126 * are still equal, so the for() will stop as expected.
7127 */
7128 } else {
7129 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007130 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007131 next = prev;
7132 hdr_end += delta;
7133 hdr_next += delta;
7134 cur_hdr->len += delta;
7135 http_msg_move_end(&txn->rsp, delta);
7136 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007137 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007138 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007139 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007140 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007141 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007142 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 * with this server since we know it.
7144 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007145 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007146 next += delta;
7147 hdr_end += delta;
7148 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007149 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007150 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007151
Willy Tarreauf1348312010-10-07 15:54:11 +02007152 txn->flags &= ~TX_SCK_MASK;
7153 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007154 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007155 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007156 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007157 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007158 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007159 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007160 next += delta;
7161 hdr_end += delta;
7162 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007163 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007164 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007165
Willy Tarreau827aee92011-03-10 16:55:02 +01007166 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007167 txn->flags &= ~TX_SCK_MASK;
7168 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007169 }
7170 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007171 /* that's done for this cookie, check the next one on the same
7172 * line when next != hdr_end (only if is_cookie2).
7173 */
7174 }
7175 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007177 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007178}
7179
7180
Willy Tarreaua15645d2007-03-18 16:22:39 +01007181/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007182 * Parses the Cache-Control and Pragma request header fields to determine if
7183 * the request may be served from the cache and/or if it is cacheable. Updates
7184 * s->txn->flags.
7185 */
7186void check_request_for_cacheability(struct stream *s, struct channel *chn)
7187{
7188 struct http_txn *txn = s->txn;
7189 char *p1, *p2;
7190 char *cur_ptr, *cur_end, *cur_next;
7191 int pragma_found;
7192 int cc_found;
7193 int cur_idx;
7194
7195 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7196 return; /* nothing more to do here */
7197
7198 cur_idx = 0;
7199 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007200 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007201
7202 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7203 struct hdr_idx_elem *cur_hdr;
7204 int val;
7205
7206 cur_hdr = &txn->hdr_idx.v[cur_idx];
7207 cur_ptr = cur_next;
7208 cur_end = cur_ptr + cur_hdr->len;
7209 cur_next = cur_end + cur_hdr->cr + 1;
7210
7211 /* We have one full header between cur_ptr and cur_end, and the
7212 * next header starts at cur_next.
7213 */
7214
7215 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7216 if (val) {
7217 if ((cur_end - (cur_ptr + val) >= 8) &&
7218 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7219 pragma_found = 1;
7220 continue;
7221 }
7222 }
7223
William Lallemand8a16fe02018-05-22 11:04:33 +02007224 /* Don't use the cache and don't try to store if we found the
7225 * Authorization header */
7226 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7227 if (val) {
7228 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7229 txn->flags |= TX_CACHE_IGNORE;
7230 continue;
7231 }
7232
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007233 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7234 if (!val)
7235 continue;
7236
7237 /* OK, right now we know we have a cache-control header at cur_ptr */
7238 cc_found = 1;
7239 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7240
7241 if (p1 >= cur_end) /* no more info */
7242 continue;
7243
7244 /* p1 is at the beginning of the value */
7245 p2 = p1;
7246 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7247 p2++;
7248
7249 /* we have a complete value between p1 and p2. We don't check the
7250 * values after max-age, max-stale nor min-fresh, we simply don't
7251 * use the cache when they're specified.
7252 */
7253 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7254 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7255 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7256 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7257 txn->flags |= TX_CACHE_IGNORE;
7258 continue;
7259 }
7260
7261 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7262 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7263 continue;
7264 }
7265 }
7266
7267 /* RFC7234#5.4:
7268 * When the Cache-Control header field is also present and
7269 * understood in a request, Pragma is ignored.
7270 * When the Cache-Control header field is not present in a
7271 * request, caches MUST consider the no-cache request
7272 * pragma-directive as having the same effect as if
7273 * "Cache-Control: no-cache" were present.
7274 */
7275 if (!cc_found && pragma_found)
7276 txn->flags |= TX_CACHE_IGNORE;
7277}
7278
7279/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007280 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007281 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007282void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007283{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007284 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007285 char *p1, *p2;
7286
7287 char *cur_ptr, *cur_end, *cur_next;
7288 int cur_idx;
7289
Willy Tarreau12b32f22017-12-21 16:08:09 +01007290 if (txn->status < 200) {
7291 /* do not try to cache interim responses! */
7292 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007293 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007294 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007295
7296 /* Iterate through the headers.
7297 * we start with the start line.
7298 */
7299 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007300 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301
7302 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7303 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007304 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007305
7306 cur_hdr = &txn->hdr_idx.v[cur_idx];
7307 cur_ptr = cur_next;
7308 cur_end = cur_ptr + cur_hdr->len;
7309 cur_next = cur_end + cur_hdr->cr + 1;
7310
7311 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007312 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007313 */
7314
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007315 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7316 if (val) {
7317 if ((cur_end - (cur_ptr + val) >= 8) &&
7318 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7319 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7320 return;
7321 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007322 }
7323
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007324 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7325 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007326 continue;
7327
7328 /* OK, right now we know we have a cache-control header at cur_ptr */
7329
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007330 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007331
7332 if (p1 >= cur_end) /* no more info */
7333 continue;
7334
7335 /* p1 is at the beginning of the value */
7336 p2 = p1;
7337
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007338 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339 p2++;
7340
7341 /* we have a complete value between p1 and p2 */
7342 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007343 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7344 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7345 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7346 continue;
7347 }
7348
Willy Tarreaua15645d2007-03-18 16:22:39 +01007349 /* we have something of the form no-cache="set-cookie" */
7350 if ((cur_end - p1 >= 21) &&
7351 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7352 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007353 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007354 continue;
7355 }
7356
7357 /* OK, so we know that either p2 points to the end of string or to a comma */
7358 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007359 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007360 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007361 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007362 return;
7363 }
7364
7365 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007366 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007367 continue;
7368 }
7369 }
7370}
7371
Willy Tarreau58f10d72006-12-04 02:26:12 +01007372
Willy Tarreaub2513902006-12-17 14:52:38 +01007373/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007374 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007375 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007376 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007377 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007378 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007379 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007380 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007381 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007382int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007383{
7384 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007385 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007386 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007387
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007388 if (!uri_auth)
7389 return 0;
7390
Cyril Bonté70be45d2010-10-12 00:14:35 +02007391 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007392 return 0;
7393
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007394 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007395 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007396 return 0;
7397
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007398 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007399 return 0;
7400
Willy Tarreaub2513902006-12-17 14:52:38 +01007401 return 1;
7402}
7403
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007404/* Append the description of what is present in error snapshot <es> into <out>.
7405 * The description must be small enough to always fit in a trash. The output
7406 * buffer may be the trash so the trash must not be used inside this function.
7407 */
7408void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7409{
7410 chunk_appendf(&trash,
7411 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7412 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7413 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7414 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7415 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7416 es->ctx.http.m_flags, es->ctx.http.m_clen,
7417 es->ctx.http.m_blen, es->ctx.http.b_flags);
7418}
7419
Willy Tarreau4076a152009-04-02 15:18:36 +02007420/*
7421 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007422 * By default it tries to report the error position as msg->err_pos. However if
7423 * this one is not set, it will then report msg->next, which is the last known
7424 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007425 * displays buffers as a contiguous area starting at buf->p. The direction is
7426 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007427 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007428void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007429 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007430 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007431{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007432 union error_snapshot_ctx ctx;
7433 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007434
Willy Tarreauef3ca732018-09-07 15:47:35 +02007435 /* http-specific part now */
7436 ctx.http.sid = s->uniq_id;
7437 ctx.http.state = state;
7438 ctx.http.b_flags = msg->chn->flags;
7439 ctx.http.s_flags = s->flags;
7440 ctx.http.t_flags = s->txn->flags;
7441 ctx.http.m_flags = msg->flags;
7442 ctx.http.m_clen = msg->chunk_len;
7443 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007444
Willy Tarreauef3ca732018-09-07 15:47:35 +02007445 ofs = msg->chn->total - ci_data(msg->chn);
7446 if (ofs < 0)
7447 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007448
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007449 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007450 other_end, s->target,
7451 strm_sess(s), &msg->chn->buf,
7452 ofs, co_data(msg->chn),
7453 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7454 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007455}
Willy Tarreaub2513902006-12-17 14:52:38 +01007456
Willy Tarreau294c4732011-12-16 21:35:50 +01007457/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7458 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7459 * performed over the whole headers. Otherwise it must contain a valid header
7460 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7461 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7462 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7463 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007464 * -1. The value fetch stops at commas, so this function is suited for use with
7465 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007466 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007467 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007468unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007469 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007470 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007471{
Willy Tarreau294c4732011-12-16 21:35:50 +01007472 struct hdr_ctx local_ctx;
7473 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007474 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007475 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007476 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007477
Willy Tarreau294c4732011-12-16 21:35:50 +01007478 if (!ctx) {
7479 local_ctx.idx = 0;
7480 ctx = &local_ctx;
7481 }
7482
Willy Tarreaubce70882009-09-07 11:51:47 +02007483 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007484 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007485 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007486 occ--;
7487 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007488 *vptr = ctx->line + ctx->val;
7489 *vlen = ctx->vlen;
7490 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007491 }
7492 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007493 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007494 }
7495
7496 /* negative occurrence, we scan all the list then walk back */
7497 if (-occ > MAX_HDR_HISTORY)
7498 return 0;
7499
Willy Tarreau294c4732011-12-16 21:35:50 +01007500 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007501 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007502 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7503 len_hist[hist_ptr] = ctx->vlen;
7504 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007505 hist_ptr = 0;
7506 found++;
7507 }
7508 if (-occ > found)
7509 return 0;
7510 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007511 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7512 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7513 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007514 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007515 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007516 if (hist_ptr >= MAX_HDR_HISTORY)
7517 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007518 *vptr = ptr_hist[hist_ptr];
7519 *vlen = len_hist[hist_ptr];
7520 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007521}
7522
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007523/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7524 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7525 * performed over the whole headers. Otherwise it must contain a valid header
7526 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7527 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7528 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7529 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7530 * -1. This function differs from http_get_hdr() in that it only returns full
7531 * line header values and does not stop at commas.
7532 * The return value is 0 if nothing was found, or non-zero otherwise.
7533 */
7534unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7535 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007536 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007537{
7538 struct hdr_ctx local_ctx;
7539 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007540 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007541 unsigned int hist_ptr;
7542 int found;
7543
7544 if (!ctx) {
7545 local_ctx.idx = 0;
7546 ctx = &local_ctx;
7547 }
7548
7549 if (occ >= 0) {
7550 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007551 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007552 occ--;
7553 if (occ <= 0) {
7554 *vptr = ctx->line + ctx->val;
7555 *vlen = ctx->vlen;
7556 return 1;
7557 }
7558 }
7559 return 0;
7560 }
7561
7562 /* negative occurrence, we scan all the list then walk back */
7563 if (-occ > MAX_HDR_HISTORY)
7564 return 0;
7565
7566 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007567 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007568 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7569 len_hist[hist_ptr] = ctx->vlen;
7570 if (++hist_ptr >= MAX_HDR_HISTORY)
7571 hist_ptr = 0;
7572 found++;
7573 }
7574 if (-occ > found)
7575 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007576
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007577 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007578 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7579 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7580 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007581 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007582 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007583 if (hist_ptr >= MAX_HDR_HISTORY)
7584 hist_ptr -= MAX_HDR_HISTORY;
7585 *vptr = ptr_hist[hist_ptr];
7586 *vlen = len_hist[hist_ptr];
7587 return 1;
7588}
7589
Willy Tarreaubaaee002006-06-26 02:48:02 +02007590/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007591 * Print a debug line with a header. Always stop at the first CR or LF char,
7592 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7593 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007594 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007595void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007596{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007597 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007598 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007599
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007600 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007601 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007602 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007603 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 +02007604
7605 for (max = 0; start + max < end; max++)
7606 if (start[max] == '\r' || start[max] == '\n')
7607 break;
7608
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007609 UBOUND(max, trash.size - trash.data - 3);
7610 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7611 trash.area[trash.data++] = '\n';
7612 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007613}
7614
Willy Tarreaueee5b512015-04-03 23:46:31 +02007615
7616/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7617 * The hdr_idx is allocated as well. In case of allocation failure, everything
7618 * allocated is freed and NULL is returned. Otherwise the new transaction is
7619 * assigned to the stream and returned.
7620 */
7621struct http_txn *http_alloc_txn(struct stream *s)
7622{
7623 struct http_txn *txn = s->txn;
7624
7625 if (txn)
7626 return txn;
7627
Willy Tarreaubafbe012017-11-24 17:34:44 +01007628 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007629 if (!txn)
7630 return txn;
7631
7632 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007633 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007634 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007635 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007636 return NULL;
7637 }
7638
7639 s->txn = txn;
7640 return txn;
7641}
7642
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007643void http_txn_reset_req(struct http_txn *txn)
7644{
7645 txn->req.flags = 0;
7646 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7647 txn->req.next = 0;
7648 txn->req.chunk_len = 0LL;
7649 txn->req.body_len = 0LL;
7650 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7651}
7652
7653void http_txn_reset_res(struct http_txn *txn)
7654{
7655 txn->rsp.flags = 0;
7656 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7657 txn->rsp.next = 0;
7658 txn->rsp.chunk_len = 0LL;
7659 txn->rsp.body_len = 0LL;
7660 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7661}
7662
Willy Tarreau0937bc42009-12-22 15:03:09 +01007663/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007664 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007665 * the required fields are properly allocated and that we only need to (re)init
7666 * them. This should be used before processing any new request.
7667 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007668void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007669{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007670 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007671 struct proxy *fe = strm_fe(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02007672 struct conn_stream *cs = objt_cs(s->si[0].end);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007673
Christopher Fauletf2824e62018-10-01 12:12:37 +02007674 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
7675 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
7676 : 0);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007677 txn->status = -1;
7678
Willy Tarreauf64d1412010-10-07 20:06:11 +02007679 txn->cookie_first_date = 0;
7680 txn->cookie_last_date = 0;
7681
Willy Tarreaueee5b512015-04-03 23:46:31 +02007682 txn->srv_cookie = NULL;
7683 txn->cli_cookie = NULL;
7684 txn->uri = NULL;
7685
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007686 http_txn_reset_req(txn);
7687 http_txn_reset_res(txn);
7688
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007689 txn->req.chn = &s->req;
7690 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007691
7692 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007693
7694 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7695 if (fe->options2 & PR_O2_REQBUG_OK)
7696 txn->req.err_pos = -1; /* let buggy requests pass */
7697
Willy Tarreau0937bc42009-12-22 15:03:09 +01007698 if (txn->hdr_idx.v)
7699 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007700
7701 vars_init(&s->vars_txn, SCOPE_TXN);
7702 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007703}
7704
7705/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007706void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007707{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007708 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007709 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007710
7711 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007712 pool_free(pool_head_requri, txn->uri);
7713 pool_free(pool_head_capture, txn->cli_cookie);
7714 pool_free(pool_head_capture, txn->srv_cookie);
7715 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007716
William Lallemanda73203e2012-03-12 12:48:57 +01007717 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007718 txn->uri = NULL;
7719 txn->srv_cookie = NULL;
7720 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007721
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007722 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007723 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007724 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007725 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007726 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007727 }
7728
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007729 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007730 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007731 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007732 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007733 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007734 }
7735
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007736 if (!LIST_ISEMPTY(&s->vars_txn.head))
7737 vars_prune(&s->vars_txn, s->sess, s);
7738 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7739 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007740}
7741
7742/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007743void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007744{
7745 http_end_txn(s);
7746 http_init_txn(s);
7747
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007748 /* reinitialise the current rule list pointer to NULL. We are sure that
7749 * any rulelist match the NULL pointer.
7750 */
7751 s->current_rule_list = NULL;
7752
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007753 s->be = strm_fe(s);
7754 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007755 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007756 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007757 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007758 /* re-init store persistence */
7759 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007760 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007761
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007762 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007763
Willy Tarreau739cfba2010-01-25 23:11:14 +01007764 /* We must trim any excess data from the response buffer, because we
7765 * may have blocked an invalid response from a server that we don't
7766 * want to accidentely forward once we disable the analysers, nor do
7767 * we want those data to come along with next response. A typical
7768 * example of such data would be from a buggy server responding to
7769 * a HEAD with some data, or sending more than the advertised
7770 * content-length.
7771 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007772 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007773 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007774
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007775 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007776 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007777
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007778 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007779 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007780
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007781 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007782 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007783
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007784 s->req.rex = TICK_ETERNITY;
7785 s->req.wex = TICK_ETERNITY;
7786 s->req.analyse_exp = TICK_ETERNITY;
7787 s->res.rex = TICK_ETERNITY;
7788 s->res.wex = TICK_ETERNITY;
7789 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007790 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007791}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007792
Willy Tarreau79e57332018-10-02 16:01:16 +02007793/* This function executes one of the set-{method,path,query,uri} actions. It
7794 * takes the string from the variable 'replace' with length 'len', then modifies
7795 * the relevant part of the request line accordingly. Then it updates various
7796 * pointers to the next elements which were moved, and the total buffer length.
7797 * It finds the action to be performed in p[2], previously filled by function
7798 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7799 * error, though this can be revisited when this code is finally exploited.
7800 *
7801 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7802 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007803 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007804 * In query string case, the mark question '?' must be set at the start of the
7805 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007806 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007807int http_replace_req_line(int action, const char *replace, int len,
7808 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007809{
Willy Tarreau79e57332018-10-02 16:01:16 +02007810 struct http_txn *txn = s->txn;
7811 char *cur_ptr, *cur_end;
7812 int offset = 0;
7813 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007814
Willy Tarreau79e57332018-10-02 16:01:16 +02007815 switch (action) {
7816 case 0: // method
7817 cur_ptr = ci_head(&s->req);
7818 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007819
Willy Tarreau79e57332018-10-02 16:01:16 +02007820 /* adjust req line offsets and lengths */
7821 delta = len - offset - (cur_end - cur_ptr);
7822 txn->req.sl.rq.m_l += delta;
7823 txn->req.sl.rq.u += delta;
7824 txn->req.sl.rq.v += delta;
7825 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007826
Willy Tarreau79e57332018-10-02 16:01:16 +02007827 case 1: // path
7828 cur_ptr = http_txn_get_path(txn);
7829 if (!cur_ptr)
7830 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007831
Willy Tarreau79e57332018-10-02 16:01:16 +02007832 cur_end = cur_ptr;
7833 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7834 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007835
Willy Tarreau79e57332018-10-02 16:01:16 +02007836 /* adjust req line offsets and lengths */
7837 delta = len - offset - (cur_end - cur_ptr);
7838 txn->req.sl.rq.u_l += delta;
7839 txn->req.sl.rq.v += delta;
7840 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007841
Willy Tarreau79e57332018-10-02 16:01:16 +02007842 case 2: // query
7843 offset = 1;
7844 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7845 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7846 while (cur_ptr < cur_end && *cur_ptr != '?')
7847 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007848
Willy Tarreau79e57332018-10-02 16:01:16 +02007849 /* skip the question mark or indicate that we must insert it
7850 * (but only if the format string is not empty then).
7851 */
7852 if (cur_ptr < cur_end)
7853 cur_ptr++;
7854 else if (len > 1)
7855 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007856
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 3: // uri
7864 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7865 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007866
Willy Tarreau79e57332018-10-02 16:01:16 +02007867 /* adjust req line offsets and lengths */
7868 delta = len - offset - (cur_end - cur_ptr);
7869 txn->req.sl.rq.u_l += delta;
7870 txn->req.sl.rq.v += delta;
7871 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007872
Willy Tarreau79e57332018-10-02 16:01:16 +02007873 default:
7874 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007875 }
7876
Willy Tarreau79e57332018-10-02 16:01:16 +02007877 /* commit changes and adjust end of message */
7878 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7879 txn->req.sl.rq.l += delta;
7880 txn->hdr_idx.v[0].len += delta;
7881 http_msg_move_end(&txn->req, delta);
7882 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007883}
7884
Willy Tarreau79e57332018-10-02 16:01:16 +02007885/* This function replace the HTTP status code and the associated message. The
7886 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007887 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007888void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007889{
Willy Tarreau79e57332018-10-02 16:01:16 +02007890 struct http_txn *txn = s->txn;
7891 char *cur_ptr, *cur_end;
7892 int delta;
7893 char *res;
7894 int c_l;
7895 const char *msg = reason;
7896 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007897
Willy Tarreau79e57332018-10-02 16:01:16 +02007898 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007899
Willy Tarreau79e57332018-10-02 16:01:16 +02007900 res = ultoa_o(status, trash.area, trash.size);
7901 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007902
Willy Tarreau79e57332018-10-02 16:01:16 +02007903 trash.area[c_l] = ' ';
7904 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007905
Willy Tarreau79e57332018-10-02 16:01:16 +02007906 /* Do we have a custom reason format string? */
7907 if (msg == NULL)
7908 msg = http_get_reason(status);
7909 msg_len = strlen(msg);
7910 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7911 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007912
Willy Tarreau79e57332018-10-02 16:01:16 +02007913 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7914 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007915
Willy Tarreau79e57332018-10-02 16:01:16 +02007916 /* commit changes and adjust message */
7917 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7918 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007919
Willy Tarreau79e57332018-10-02 16:01:16 +02007920 /* adjust res line offsets and lengths */
7921 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7922 txn->rsp.sl.st.c_l = c_l;
7923 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007924
Willy Tarreau79e57332018-10-02 16:01:16 +02007925 delta = trash.data - (cur_end - cur_ptr);
7926 txn->rsp.sl.st.l += delta;
7927 txn->hdr_idx.v[0].len += delta;
7928 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007929}
7930
Willy Tarreau8797c062007-05-07 00:55:35 +02007931__attribute__((constructor))
7932static void __http_protocol_init(void)
7933{
Willy Tarreau8797c062007-05-07 00:55:35 +02007934}
7935
7936
Willy Tarreau58f10d72006-12-04 02:26:12 +01007937/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007938 * Local variables:
7939 * c-indent-level: 8
7940 * c-basic-offset: 8
7941 * End:
7942 */