blob: dad6b4d5d6229ff23a061d1b139af96eab2b4177 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Frédéric Lécaillea41d5312018-01-29 12:05:07 +010029#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010043#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020044#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <types/global.h>
William Lallemand71bd11a2017-11-20 19:13:14 +010046#include <types/cache.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreau8797c062007-05-07 00:55:35 +020049#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020050#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020051#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010052#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020054#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010055#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010056#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020057#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010058#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020060#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020061#include <proto/frontend.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020062#include <proto/h1.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010064#include <proto/hdr_idx.h>
Patrick Hemmere3faf022018-08-22 10:02:00 -040065#include <proto/hlua.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010066#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020067#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010069#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020070#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020071#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010072#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020073#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010074#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020075#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020076#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020077#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020078
Cyril Bonté19979e12012-04-04 12:57:21 +020079/* status codes available for the stats admin page (strictly 4 chars length) */
80const char *stat_status_codes[STAT_STATUS_SIZE] = {
81 [STAT_STATUS_DENY] = "DENY",
82 [STAT_STATUS_DONE] = "DONE",
83 [STAT_STATUS_ERRP] = "ERRP",
84 [STAT_STATUS_EXCD] = "EXCD",
85 [STAT_STATUS_NONE] = "NONE",
86 [STAT_STATUS_PART] = "PART",
87 [STAT_STATUS_UNKN] = "UNKN",
88};
89
Willy Tarreau80587432006-12-24 17:47:20 +010090void init_proto_http()
91{
Willy Tarreau332f8bf2007-05-13 21:36:56 +020092 /* memory allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +010093 pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
94 pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +010095}
Willy Tarreaubaaee002006-06-26 02:48:02 +020096
Willy Tarreau53b6c742006-12-17 13:37:46 +010097/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +010098 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +020099 * the last CRLF. <len> bytes are copied, not counting the CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100100 * The header is also automatically added to the index <hdr_idx>, and the end
101 * of headers is automatically adjusted. The number of bytes added is returned
102 * on success, otherwise <0 is returned indicating an error.
103 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100104int http_header_add_tail2(struct http_msg *msg,
105 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100106{
107 int bytes;
108
Willy Tarreau4d893d42018-07-12 15:43:32 +0200109 bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100110 if (!bytes)
111 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100112 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100113 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
114}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200116/* Find the first or next occurrence of header <name> in message buffer <sol>
117 * using headers index <idx>, and return it in the <ctx> structure. This
118 * structure holds everything necessary to use the header and find next
119 * occurrence. If its <idx> member is 0, the header is searched from the
120 * beginning. Otherwise, the next occurrence is returned. The function returns
121 * 1 when it finds a value, and 0 when there is no more. It is very similar to
122 * http_find_header2() except that it is designed to work with full-line headers
123 * whose comma is not a delimiter but is part of the syntax. As a special case,
124 * if ctx->val is NULL when searching for a new values of a header, the current
125 * header is rescanned. This allows rescanning after a header deletion.
126 */
127int http_find_full_header2(const char *name, int len,
128 char *sol, struct hdr_idx *idx,
129 struct hdr_ctx *ctx)
130{
131 char *eol, *sov;
132 int cur_idx, old_idx;
133
134 cur_idx = ctx->idx;
135 if (cur_idx) {
136 /* We have previously returned a header, let's search another one */
137 sol = ctx->line;
138 eol = sol + idx->v[cur_idx].len;
139 goto next_hdr;
140 }
141
142 /* first request for this header */
143 sol += hdr_idx_first_pos(idx);
144 old_idx = 0;
145 cur_idx = hdr_idx_first_idx(idx);
146 while (cur_idx) {
147 eol = sol + idx->v[cur_idx].len;
148
149 if (len == 0) {
150 /* No argument was passed, we want any header.
151 * To achieve this, we simply build a fake request. */
152 while (sol + len < eol && sol[len] != ':')
153 len++;
154 name = sol;
155 }
156
157 if ((len < eol - sol) &&
158 (sol[len] == ':') &&
159 (strncasecmp(sol, name, len) == 0)) {
160 ctx->del = len;
161 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100162 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200163 sov++;
164
165 ctx->line = sol;
166 ctx->prev = old_idx;
167 ctx->idx = cur_idx;
168 ctx->val = sov - sol;
169 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100170 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200171 eol--;
172 ctx->tws++;
173 }
174 ctx->vlen = eol - sov;
175 return 1;
176 }
177 next_hdr:
178 sol = eol + idx->v[cur_idx].cr + 1;
179 old_idx = cur_idx;
180 cur_idx = idx->v[cur_idx].next;
181 }
182 return 0;
183}
184
Willy Tarreauc90dc232015-02-20 13:51:36 +0100185/* Find the first or next header field in message buffer <sol> using headers
186 * index <idx>, and return it in the <ctx> structure. This structure holds
187 * everything necessary to use the header and find next occurrence. If its
188 * <idx> member is 0, the first header is retrieved. Otherwise, the next
189 * occurrence is returned. The function returns 1 when it finds a value, and
190 * 0 when there is no more. It is equivalent to http_find_full_header2() with
191 * no header name.
192 */
193int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
194{
195 char *eol, *sov;
196 int cur_idx, old_idx;
197 int len;
198
199 cur_idx = ctx->idx;
200 if (cur_idx) {
201 /* We have previously returned a header, let's search another one */
202 sol = ctx->line;
203 eol = sol + idx->v[cur_idx].len;
204 goto next_hdr;
205 }
206
207 /* first request for this header */
208 sol += hdr_idx_first_pos(idx);
209 old_idx = 0;
210 cur_idx = hdr_idx_first_idx(idx);
211 while (cur_idx) {
212 eol = sol + idx->v[cur_idx].len;
213
214 len = 0;
215 while (1) {
216 if (len >= eol - sol)
217 goto next_hdr;
218 if (sol[len] == ':')
219 break;
220 len++;
221 }
222
223 ctx->del = len;
224 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100225 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100226 sov++;
227
228 ctx->line = sol;
229 ctx->prev = old_idx;
230 ctx->idx = cur_idx;
231 ctx->val = sov - sol;
232 ctx->tws = 0;
233
Willy Tarreau2235b262016-11-05 15:50:20 +0100234 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100235 eol--;
236 ctx->tws++;
237 }
238 ctx->vlen = eol - sov;
239 return 1;
240
241 next_hdr:
242 sol = eol + idx->v[cur_idx].cr + 1;
243 old_idx = cur_idx;
244 cur_idx = idx->v[cur_idx].next;
245 }
246 return 0;
247}
248
Willy Tarreau33a7e692007-06-10 19:45:56 +0200249/* Find the first or next occurrence of header <name> in message buffer <sol>
250 * using headers index <idx>, and return it in the <ctx> structure. This
251 * structure holds everything necessary to use the header and find next
252 * occurrence. If its <idx> member is 0, the header is searched from the
253 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100254 * 1 when it finds a value, and 0 when there is no more. It is designed to work
255 * with headers defined as comma-separated lists. As a special case, if ctx->val
256 * is NULL when searching for a new values of a header, the current header is
257 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200258 */
259int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100260 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200261 struct hdr_ctx *ctx)
262{
Willy Tarreau68085d82010-01-18 14:54:04 +0100263 char *eol, *sov;
264 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200265
Willy Tarreau68085d82010-01-18 14:54:04 +0100266 cur_idx = ctx->idx;
267 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200268 /* We have previously returned a value, let's search
269 * another one on the same line.
270 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200271 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200272 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100273 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200274 eol = sol + idx->v[cur_idx].len;
275
276 if (sov >= eol)
277 /* no more values in this header */
278 goto next_hdr;
279
Willy Tarreau68085d82010-01-18 14:54:04 +0100280 /* values remaining for this header, skip the comma but save it
281 * for later use (eg: for header deletion).
282 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200283 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100284 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200285 sov++;
286
287 goto return_hdr;
288 }
289
290 /* first request for this header */
291 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100292 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200293 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200294 while (cur_idx) {
295 eol = sol + idx->v[cur_idx].len;
296
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200297 if (len == 0) {
298 /* No argument was passed, we want any header.
299 * To achieve this, we simply build a fake request. */
300 while (sol + len < eol && sol[len] != ':')
301 len++;
302 name = sol;
303 }
304
Willy Tarreau33a7e692007-06-10 19:45:56 +0200305 if ((len < eol - sol) &&
306 (sol[len] == ':') &&
307 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100308 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200309 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100310 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200311 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100312
Willy Tarreau33a7e692007-06-10 19:45:56 +0200313 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100314 ctx->prev = old_idx;
315 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200316 ctx->idx = cur_idx;
317 ctx->val = sov - sol;
318
Willy Tarreauab813a42018-09-10 18:41:28 +0200319 eol = http_find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200320 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100321 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200322 eol--;
323 ctx->tws++;
324 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200325 ctx->vlen = eol - sov;
326 return 1;
327 }
328 next_hdr:
329 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100330 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200331 cur_idx = idx->v[cur_idx].next;
332 }
333 return 0;
334}
335
Willy Tarreau68085d82010-01-18 14:54:04 +0100336/* Remove one value of a header. This only works on a <ctx> returned by one of
337 * the http_find_header functions. The value is removed, as well as surrounding
338 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100339 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100340 * message <msg>. The new index is returned. If it is zero, it means there is
341 * no more header, so any processing may stop. The ctx is always left in a form
342 * that can be handled by http_find_header2() to find next occurrence.
343 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100344int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100345{
346 int cur_idx = ctx->idx;
347 char *sol = ctx->line;
348 struct hdr_idx_elem *hdr;
349 int delta, skip_comma;
350
351 if (!cur_idx)
352 return 0;
353
354 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200355 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100356 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200357 delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100358 http_msg_move_end(msg, delta);
359 idx->used--;
360 hdr->len = 0; /* unused entry */
361 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100362 if (idx->tail == ctx->idx)
363 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100364 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100365 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100366 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200367 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100368 return ctx->idx;
369 }
370
371 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200372 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
373 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100374 */
375
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200376 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200377 delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200378 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100379 NULL, 0);
380 hdr->len += delta;
381 http_msg_move_end(msg, delta);
382 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200383 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100384 return ctx->idx;
385}
386
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100387/* This function handles a server error at the stream interface level. The
388 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100389 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100390 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100391 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200393static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +0200394 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395{
Willy Tarreau2019f952017-03-14 11:07:31 +0100396 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100397 channel_auto_read(si_oc(si));
398 channel_abort(si_oc(si));
399 channel_auto_close(si_oc(si));
400 channel_erase(si_oc(si));
401 channel_auto_close(si_ic(si));
402 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200403 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200404 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200405 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200406 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200407 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200408 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409}
410
Willy Tarreau87b09662015-04-03 00:22:06 +0200411/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100412 * and message.
413 */
414
Willy Tarreau83061a82018-07-13 11:56:34 +0200415struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100416{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200417 const int msgnum = http_get_status_idx(s->txn->status);
418
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200419 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200420 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200421 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200422 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100423 else
424 return &http_err_chunks[msgnum];
425}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200426
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100427void
Willy Tarreau83061a82018-07-13 11:56:34 +0200428http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100429{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200430 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100431 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100432 stream_int_retnclose(&s->si[0], msg);
433}
434
Willy Tarreau21d2af32008-02-14 20:25:24 +0100435/* Parse the URI from the given transaction (which is assumed to be in request
436 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
437 * It is returned otherwise.
438 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200439char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100440{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200441 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100442
Willy Tarreau9d9ccdb2018-10-28 20:13:12 +0100443 if (!txn->req.chn->buf.size)
444 return NULL;
445
Willy Tarreau6b952c82018-09-10 17:45:34 +0200446 ret = http_get_path(ist2(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l));
William Lallemand65ad6e12014-01-31 15:08:02 +0100447
Willy Tarreau6b952c82018-09-10 17:45:34 +0200448 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100449}
450
Willy Tarreau71241ab2012-12-27 11:30:54 +0100451/* Returns a 302 for a redirectable request that reaches a server working in
452 * in redirect mode. This may only be called just after the stream interface
453 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
454 * follow normal proxy processing. NOTE: this function is designed to support
455 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100456 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200457void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100458{
459 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100460 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100461 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200462 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100463
464 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200465 trash.data = strlen(HTTP_302);
466 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100467
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200468 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100469
Willy Tarreauefb453c2008-10-26 20:49:47 +0100470 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200471 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100472 return;
473
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100474 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100475 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200476 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
477 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100478 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100479
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200480 /* 3: add the request URI. Since it was already forwarded, we need
481 * to temporarily rewind the buffer.
482 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200483 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200484 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200485
Willy Tarreau6b952c82018-09-10 17:45:34 +0200486 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200487 len = b_dist(&s->req.buf, path, c_ptr(&s->req, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200488
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200489 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200490
Willy Tarreauefb453c2008-10-26 20:49:47 +0100491 if (!path)
492 return;
493
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200494 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100495 return;
496
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200497 memcpy(trash.area + trash.data, path, len);
498 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100499
500 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 memcpy(trash.area + trash.data,
502 "\r\nProxy-Connection: close\r\n\r\n", 29);
503 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100504 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200505 memcpy(trash.area + trash.data,
506 "\r\nConnection: close\r\n\r\n", 23);
507 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100508 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100509
510 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200511 si_shutr(si);
512 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100513 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100514 si->state = SI_ST_CLO;
515
516 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100517 txn->status = 302;
518 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100519
520 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100521 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500522 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100523}
524
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100525/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100526 * that the server side is closed. Note that err_type is actually a
527 * bitmask, where almost only aborts may be cumulated with other
528 * values. We consider that aborted operations are more important
529 * than timeouts or errors due to the fact that nobody else in the
530 * logs might explain incomplete retries. All others should avoid
531 * being cumulated. It should normally not be possible to have multiple
532 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100533 * Note that connection errors appearing on the second request of a keep-alive
534 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100535 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200536void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100537{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100538 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100539
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200540 /* set s->txn->status for http_error_message(s) */
541 s->txn->status = 503;
542
Willy Tarreauefb453c2008-10-26 20:49:47 +0100543 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200544 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100545 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100546 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200547 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100548 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200549 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100550 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200551 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100552 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100553 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200554 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100555 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100556 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200557 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100558 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200559 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100560 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200561 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100562 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200563 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200564 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200565 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100566 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200567 http_error_message(s));
568 else { /* SI_ET_CONN_OTHER and others */
569 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200570 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100571 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200572 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100573}
574
Willy Tarreau42250582007-04-01 01:30:43 +0200575extern const char sess_term_cond[8];
576extern const char sess_fin_state[8];
577extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +0100578struct pool_head *pool_head_http_txn;
579struct pool_head *pool_head_requri;
580struct pool_head *pool_head_capture = NULL;
581struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100582
Willy Tarreau117f59e2007-03-04 18:17:17 +0100583/*
584 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200585 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100586 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200587void http_capture_headers(char *som, struct hdr_idx *idx,
588 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100589{
590 char *eol, *sol, *col, *sov;
591 int cur_idx;
592 struct cap_hdr *h;
593 int len;
594
595 sol = som + hdr_idx_first_pos(idx);
596 cur_idx = hdr_idx_first_idx(idx);
597
598 while (cur_idx) {
599 eol = sol + idx->v[cur_idx].len;
600
601 col = sol;
602 while (col < eol && *col != ':')
603 col++;
604
605 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100606 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100607 sov++;
608
609 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200610 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100611 (strncasecmp(sol, h->name, h->namelen) == 0)) {
612 if (cap[h->index] == NULL)
613 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100614 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100615
616 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100617 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100618 continue;
619 }
620
621 len = eol - sov;
622 if (len > h->len)
623 len = h->len;
624
625 memcpy(cap[h->index], sov, len);
626 cap[h->index][len]=0;
627 }
628 }
629 sol = eol + idx->v[cur_idx].cr + 1;
630 cur_idx = idx->v[cur_idx].next;
631 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100632}
633
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200634/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
635 * conversion succeeded, 0 in case of error. If the request was already 1.X,
636 * nothing is done and 1 is returned.
637 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200638int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200639{
640 int delta;
641 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100642 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200643
644 if (msg->sl.rq.v_l != 0)
645 return 1;
646
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300647 /* RFC 1945 allows only GET for HTTP/0.9 requests */
648 if (txn->meth != HTTP_METH_GET)
649 return 0;
650
Willy Tarreauf37954d2018-06-15 18:31:02 +0200651 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200652
653 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300654 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
655 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200656 }
657 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200658 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100659 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200660 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200661 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200662 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200663 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200664 NULL, NULL);
665 if (unlikely(!cur_end))
666 return 0;
667
668 /* we have a full HTTP/1.0 request now and we know that
669 * we have either a CR or an LF at <ptr>.
670 */
671 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
672 return 1;
673}
674
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100675/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100676 * and "keep-alive" values. If we already know that some headers may safely
677 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100678 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
679 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100680 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100681 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
682 * found, and TX_CON_*_SET is adjusted depending on what is left so only
683 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100684 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100685 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100686void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100687{
Willy Tarreau5b154472009-12-21 20:11:07 +0100688 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100689 const char *hdr_val = "Connection";
690 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100691
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100692 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100693 return;
694
Willy Tarreau88d349d2010-01-25 12:15:43 +0100695 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
696 hdr_val = "Proxy-Connection";
697 hdr_len = 16;
698 }
699
Willy Tarreau5b154472009-12-21 20:11:07 +0100700 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100701 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200702 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100703 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
704 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100705 if (to_del & 2)
706 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100707 else
708 txn->flags |= TX_CON_KAL_SET;
709 }
710 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
711 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100712 if (to_del & 1)
713 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100714 else
715 txn->flags |= TX_CON_CLO_SET;
716 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100717 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
718 txn->flags |= TX_HDR_CONN_UPG;
719 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100720 }
721
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100722 txn->flags |= TX_HDR_CONN_PRS;
723 return;
724}
Willy Tarreau5b154472009-12-21 20:11:07 +0100725
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100726/* Apply desired changes on the Connection: header. Values may be removed and/or
727 * added depending on the <wanted> flags, which are exclusively composed of
728 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
729 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
730 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100731void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100732{
733 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100734 const char *hdr_val = "Connection";
735 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100736
737 ctx.idx = 0;
738
Willy Tarreau88d349d2010-01-25 12:15:43 +0100739
740 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
741 hdr_val = "Proxy-Connection";
742 hdr_len = 16;
743 }
744
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100745 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200746 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100747 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
748 if (wanted & TX_CON_KAL_SET)
749 txn->flags |= TX_CON_KAL_SET;
750 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100751 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100752 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100753 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
754 if (wanted & TX_CON_CLO_SET)
755 txn->flags |= TX_CON_CLO_SET;
756 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100757 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100758 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100759 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100760
761 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
762 return;
763
764 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
765 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100766 hdr_val = "Connection: close";
767 hdr_len = 17;
768 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
769 hdr_val = "Proxy-Connection: close";
770 hdr_len = 23;
771 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100772 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100773 }
774
775 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
776 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100777 hdr_val = "Connection: keep-alive";
778 hdr_len = 22;
779 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
780 hdr_val = "Proxy-Connection: keep-alive";
781 hdr_len = 28;
782 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100783 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100784 }
785 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100786}
787
Willy Tarreau87b09662015-04-03 00:22:06 +0200788void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200789{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200790 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200791 int tmp = TX_CON_WANT_KAL;
792
Christopher Faulet315b39c2018-09-21 16:26:19 +0200793 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
794 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
795 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200796
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200797 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200798 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200799 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200800
Christopher Faulet315b39c2018-09-21 16:26:19 +0200801 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
802 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200803 tmp = TX_CON_WANT_CLO;
804
805 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
806 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
807
808 if (!(txn->flags & TX_HDR_CONN_PRS) &&
809 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
810 /* parse the Connection header and possibly clean it */
811 int to_del = 0;
812 if ((msg->flags & HTTP_MSGF_VER_11) ||
813 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200814 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200815 to_del |= 2; /* remove "keep-alive" */
816 if (!(msg->flags & HTTP_MSGF_VER_11))
817 to_del |= 1; /* remove "close" */
818 http_parse_connection_header(txn, msg, to_del);
819 }
820
821 /* check if client or config asks for explicit close in KAL/SCL */
822 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
823 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
824 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
825 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
826 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200827 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200828 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
829}
William Lallemand82fe75c2012-10-23 10:25:10 +0200830
Willy Tarreaud787e662009-07-07 10:14:51 +0200831/* This stream analyser waits for a complete HTTP request. It returns 1 if the
832 * processing can continue on next analysers, or zero if it either needs more
833 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100834 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200835 * when it has nothing left to do, and may remove any analyser when it wants to
836 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100837 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200838int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100839{
Willy Tarreau59234e92008-11-30 23:51:27 +0100840 /*
841 * We will parse the partial (or complete) lines.
842 * We will check the request syntax, and also join multi-line
843 * headers. An index of all the lines will be elaborated while
844 * parsing.
845 *
846 * For the parsing, we use a 28 states FSM.
847 *
848 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200849 * ci_head(req) = beginning of request
850 * ci_head(req) + msg->eoh = end of processed headers / start of current one
851 * ci_tail(req) = end of input data
852 * msg->eol = end of current header or line (LF or CRLF)
853 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200854 *
855 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200856 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200857 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
858 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100859 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100860
Willy Tarreau59234e92008-11-30 23:51:27 +0100861 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200862 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200863 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100864 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200865 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100866
Christopher Faulet45073512018-07-20 10:16:29 +0200867 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +0100868 now_ms, __FUNCTION__,
869 s,
870 req,
871 req->rex, req->wex,
872 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200873 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100874 req->analysers);
875
Willy Tarreau52a0c602009-08-16 22:45:38 +0200876 /* we're speaking HTTP here, so let's speak HTTP to the client */
877 s->srv_error = http_return_srv_error;
878
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300879 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200880 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300881 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
882
Willy Tarreau83e3af02009-12-28 17:39:57 +0100883 /* There's a protected area at the end of the buffer for rewriting
884 * purposes. We don't want to start to parse the request if the
885 * protected area is affected, because we may have to move processed
886 * data later, which is much more complicated.
887 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200888 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200889 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100890 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200891 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100892 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100893 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200894 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200895 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100896 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100897 return 0;
898 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200899 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200900 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200901 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100902 }
903
Willy Tarreauf37954d2018-06-15 18:31:02 +0200904 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100905 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100906 }
907
Willy Tarreau59234e92008-11-30 23:51:27 +0100908 /* 1: we might have to print this header in debug mode */
909 if (unlikely((global.mode & MODE_DEBUG) &&
910 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200911 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100912 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100913
Willy Tarreauf37954d2018-06-15 18:31:02 +0200914 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200915 /* this is a bit complex : in case of error on the request line,
916 * we know that rq.l is still zero, so we display only the part
917 * up to the end of the line (truncated by debug_hdr).
918 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200919 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100920 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100921
Willy Tarreau59234e92008-11-30 23:51:27 +0100922 sol += hdr_idx_first_pos(&txn->hdr_idx);
923 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100924
Willy Tarreau59234e92008-11-30 23:51:27 +0100925 while (cur_idx) {
926 eol = sol + txn->hdr_idx.v[cur_idx].len;
927 debug_hdr("clihdr", s, sol, eol);
928 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
929 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100930 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100931 }
932
Willy Tarreau58f10d72006-12-04 02:26:12 +0100933
Willy Tarreau59234e92008-11-30 23:51:27 +0100934 /*
935 * Now we quickly check if we have found a full valid request.
936 * If not so, we check the FD and buffer states before leaving.
937 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100938 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100939 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200940 * on a keep-alive stream, if we encounter and error, close, t/o,
941 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100942 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200943 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200944 * Last, we may increase some tracked counters' http request errors on
945 * the cases that are deliberately the client's fault. For instance,
946 * a timeout or connection reset is not counted as an error. However
947 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100948 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100949
Willy Tarreau655dce92009-11-08 13:10:58 +0100950 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100951 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100952 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100953 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100954 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200955 stream_inc_http_req_ctr(s);
956 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200957 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100958 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100959 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100960
Willy Tarreau59234e92008-11-30 23:51:27 +0100961 /* 1: Since we are in header mode, if there's no space
962 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200963 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100964 * must terminate it now.
965 */
Willy Tarreau23752332018-06-15 14:54:53 +0200966 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100967 /* FIXME: check if URI is set and return Status
968 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100969 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200970 stream_inc_http_req_ctr(s);
971 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200972 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200973 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200974 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100975 goto return_bad_req;
976 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100977
Willy Tarreau59234e92008-11-30 23:51:27 +0100978 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200979 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200980 if (!(s->flags & SF_ERR_MASK))
981 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100982
Willy Tarreaufcffa692010-01-10 14:21:19 +0100983 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100984 goto failed_keep_alive;
985
Willy Tarreau0f228a02015-05-01 15:37:53 +0200986 if (sess->fe->options & PR_O_IGNORE_PRB)
987 goto failed_keep_alive;
988
Willy Tarreau59234e92008-11-30 23:51:27 +0100989 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200990 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200991 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200992 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200993 }
994
Willy Tarreaudc979f22012-12-04 10:39:01 +0100995 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100996 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100997 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100998 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +0100999 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001000 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001001 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001002 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001003 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001004 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001005
Willy Tarreaue7dff022015-04-03 01:14:29 +02001006 if (!(s->flags & SF_FINST_MASK))
1007 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001008 return 0;
1009 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001010
Willy Tarreau59234e92008-11-30 23:51:27 +01001011 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001012 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001013 if (!(s->flags & SF_ERR_MASK))
1014 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001015
Willy Tarreaufcffa692010-01-10 14:21:19 +01001016 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001017 goto failed_keep_alive;
1018
Willy Tarreau0f228a02015-05-01 15:37:53 +02001019 if (sess->fe->options & PR_O_IGNORE_PRB)
1020 goto failed_keep_alive;
1021
Willy Tarreau59234e92008-11-30 23:51:27 +01001022 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001023 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001024 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001025 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001026 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001027 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001028 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001029 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001030 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001031 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001032
Willy Tarreau87b09662015-04-03 00:22:06 +02001033 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001034 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001035 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001036 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001037 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001038
Willy Tarreaue7dff022015-04-03 01:14:29 +02001039 if (!(s->flags & SF_FINST_MASK))
1040 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001041 return 0;
1042 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001043
Willy Tarreau59234e92008-11-30 23:51:27 +01001044 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001045 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001046 if (!(s->flags & SF_ERR_MASK))
1047 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001048
Willy Tarreaufcffa692010-01-10 14:21:19 +01001049 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001050 goto failed_keep_alive;
1051
Willy Tarreau0f228a02015-05-01 15:37:53 +02001052 if (sess->fe->options & PR_O_IGNORE_PRB)
1053 goto failed_keep_alive;
1054
Willy Tarreau4076a152009-04-02 15:18:36 +02001055 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001056 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001057 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001058 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001059 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001060 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001061 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001062 stream_inc_http_err_ctr(s);
1063 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001064 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001065 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001066 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001067 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001068
Willy Tarreaue7dff022015-04-03 01:14:29 +02001069 if (!(s->flags & SF_FINST_MASK))
1070 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001071 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001072 }
1073
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001074 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001075 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001076 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001077#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001078 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001079 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001080 /* We need more data, we have to re-enable quick-ack in case we
1081 * previously disabled it, otherwise we might cause the client
1082 * to delay next data.
1083 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001084 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001085 }
1086#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001087
Willy Tarreaufcffa692010-01-10 14:21:19 +01001088 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1089 /* If the client starts to talk, let's fall back to
1090 * request timeout processing.
1091 */
1092 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001093 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001094 }
1095
Willy Tarreau59234e92008-11-30 23:51:27 +01001096 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001097 if (!tick_isset(req->analyse_exp)) {
1098 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1099 (txn->flags & TX_WAIT_NEXT_RQ) &&
1100 tick_isset(s->be->timeout.httpka))
1101 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1102 else
1103 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1104 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001105
Willy Tarreau59234e92008-11-30 23:51:27 +01001106 /* we're not ready yet */
1107 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001108
1109 failed_keep_alive:
1110 /* Here we process low-level errors for keep-alive requests. In
1111 * short, if the request is not the first one and it experiences
1112 * a timeout, read error or shutdown, we just silently close so
1113 * that the client can try again.
1114 */
1115 txn->status = 0;
1116 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001117 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001118 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001119 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001120 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001121 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001122 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001123 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001124
Willy Tarreaud787e662009-07-07 10:14:51 +02001125 /* OK now we have a complete HTTP request with indexed headers. Let's
1126 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001127 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001128 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001129 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001130 * byte after the last LF. msg->sov points to the first byte of data.
1131 * msg->eol cannot be trusted because it may have been left uninitialized
1132 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001133 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001134
Willy Tarreau87b09662015-04-03 00:22:06 +02001135 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001136 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001137
Willy Tarreaub16a5742010-01-10 14:46:16 +01001138 if (txn->flags & TX_WAIT_NEXT_RQ) {
1139 /* kill the pending keep-alive timeout */
1140 txn->flags &= ~TX_WAIT_NEXT_RQ;
1141 req->analyse_exp = TICK_ETERNITY;
1142 }
1143
1144
Willy Tarreaud787e662009-07-07 10:14:51 +02001145 /* Maybe we found in invalid header name while we were configured not
1146 * to block on that, so we have to capture it now.
1147 */
1148 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001149 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001150
Willy Tarreau59234e92008-11-30 23:51:27 +01001151 /*
1152 * 1: identify the method
1153 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001154 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001155
1156 /* we can make use of server redirect on GET and HEAD */
1157 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001158 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001159 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001160 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001161 /* PRI is reserved for the HTTP/2 preface */
1162 msg->err_pos = 0;
1163 goto return_bad_req;
1164 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001165
Willy Tarreau59234e92008-11-30 23:51:27 +01001166 /*
1167 * 2: check if the URI matches the monitor_uri.
1168 * We have to do this for every request which gets in, because
1169 * the monitor-uri is defined by the frontend.
1170 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001171 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1172 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001173 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001174 sess->fe->monitor_uri,
1175 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001176 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001177 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001178 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001179 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001180
Willy Tarreaue7dff022015-04-03 01:14:29 +02001181 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001182 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001183
Willy Tarreau59234e92008-11-30 23:51:27 +01001184 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001185 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001186 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001187
Willy Tarreau59234e92008-11-30 23:51:27 +01001188 ret = acl_pass(ret);
1189 if (cond->pol == ACL_COND_UNLESS)
1190 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001191
Willy Tarreau59234e92008-11-30 23:51:27 +01001192 if (ret) {
1193 /* we fail this request, let's return 503 service unavail */
1194 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001195 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001196 if (!(s->flags & SF_ERR_MASK))
1197 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001198 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001199 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001200 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001201
Willy Tarreau59234e92008-11-30 23:51:27 +01001202 /* nothing to fail, let's reply normaly */
1203 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001204 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001205 if (!(s->flags & SF_ERR_MASK))
1206 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001207 goto return_prx_cond;
1208 }
1209
1210 /*
1211 * 3: Maybe we have to copy the original REQURI for the logs ?
1212 * Note: we cannot log anymore if the request has been
1213 * classified as invalid.
1214 */
1215 if (unlikely(s->logs.logwait & LW_REQ)) {
1216 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001217 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001218 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001219
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001220 if (urilen >= global.tune.requri_len )
1221 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001222 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001223 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001224
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001225 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001226 s->do_log(s);
1227 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001228 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001230 }
Willy Tarreau06619262006-12-17 08:37:22 +01001231
Willy Tarreau91852eb2015-05-01 13:26:00 +02001232 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1233 * exactly one digit "." one digit. This check may be disabled using
1234 * option accept-invalid-http-request.
1235 */
1236 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1237 if (msg->sl.rq.v_l != 8) {
1238 msg->err_pos = msg->sl.rq.v;
1239 goto return_bad_req;
1240 }
1241
Willy Tarreauf37954d2018-06-15 18:31:02 +02001242 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1243 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1244 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1245 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001246 msg->err_pos = msg->sl.rq.v + 4;
1247 goto return_bad_req;
1248 }
1249 }
Willy Tarreau13317662015-05-01 13:47:08 +02001250 else {
1251 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1252 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1253 goto return_bad_req;
1254 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001255
Willy Tarreau5b154472009-12-21 20:11:07 +01001256 /* ... and check if the request is HTTP/1.1 or above */
1257 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001258 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1259 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1260 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001261 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001262
1263 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001264 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01001265
Willy Tarreau88d349d2010-01-25 12:15:43 +01001266 /* if the frontend has "option http-use-proxy-header", we'll check if
1267 * we have what looks like a proxied connection instead of a connection,
1268 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1269 * Note that this is *not* RFC-compliant, however browsers and proxies
1270 * happen to do that despite being non-standard :-(
1271 * We consider that a request not beginning with either '/' or '*' is
1272 * a proxied connection, which covers both "scheme://location" and
1273 * CONNECT ip:port.
1274 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001275 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001276 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001277 txn->flags |= TX_USE_PX_CONN;
1278
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001279 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001280 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001281
Willy Tarreau59234e92008-11-30 23:51:27 +01001282 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001283 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001284 http_capture_headers(ci_head(req), &txn->hdr_idx,
1285 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001286
Willy Tarreau557f1992015-05-01 10:05:17 +02001287 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1288 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001289 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001290 * The length of a message body is determined by one of the following
1291 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001292 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001293 * 1. Any response to a HEAD request and any response with a 1xx
1294 * (Informational), 204 (No Content), or 304 (Not Modified) status
1295 * code is always terminated by the first empty line after the
1296 * header fields, regardless of the header fields present in the
1297 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001298 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001299 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1300 * the connection will become a tunnel immediately after the empty
1301 * line that concludes the header fields. A client MUST ignore any
1302 * Content-Length or Transfer-Encoding header fields received in
1303 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001304 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001305 * 3. If a Transfer-Encoding header field is present and the chunked
1306 * transfer coding (Section 4.1) is the final encoding, the message
1307 * body length is determined by reading and decoding the chunked
1308 * data until the transfer coding indicates the data is complete.
1309 *
1310 * If a Transfer-Encoding header field is present in a response and
1311 * the chunked transfer coding is not the final encoding, the
1312 * message body length is determined by reading the connection until
1313 * it is closed by the server. If a Transfer-Encoding header field
1314 * is present in a request and the chunked transfer coding is not
1315 * the final encoding, the message body length cannot be determined
1316 * reliably; the server MUST respond with the 400 (Bad Request)
1317 * status code and then close the connection.
1318 *
1319 * If a message is received with both a Transfer-Encoding and a
1320 * Content-Length header field, the Transfer-Encoding overrides the
1321 * Content-Length. Such a message might indicate an attempt to
1322 * perform request smuggling (Section 9.5) or response splitting
1323 * (Section 9.4) and ought to be handled as an error. A sender MUST
1324 * remove the received Content-Length field prior to forwarding such
1325 * a message downstream.
1326 *
1327 * 4. If a message is received without Transfer-Encoding and with
1328 * either multiple Content-Length header fields having differing
1329 * field-values or a single Content-Length header field having an
1330 * invalid value, then the message framing is invalid and the
1331 * recipient MUST treat it as an unrecoverable error. If this is a
1332 * request message, the server MUST respond with a 400 (Bad Request)
1333 * status code and then close the connection. If this is a response
1334 * message received by a proxy, the proxy MUST close the connection
1335 * to the server, discard the received response, and send a 502 (Bad
1336 * Gateway) response to the client. If this is a response message
1337 * received by a user agent, the user agent MUST close the
1338 * connection to the server and discard the received response.
1339 *
1340 * 5. If a valid Content-Length header field is present without
1341 * Transfer-Encoding, its decimal value defines the expected message
1342 * body length in octets. If the sender closes the connection or
1343 * the recipient times out before the indicated number of octets are
1344 * received, the recipient MUST consider the message to be
1345 * incomplete and close the connection.
1346 *
1347 * 6. If this is a request message and none of the above are true, then
1348 * the message body length is zero (no message body is present).
1349 *
1350 * 7. Otherwise, this is a response message without a declared message
1351 * body length, so the message body length is determined by the
1352 * number of octets received prior to the server closing the
1353 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001354 */
1355
Willy Tarreau32b47f42009-10-18 20:55:02 +02001356 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001357 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001358 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001359 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001360 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001361 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001362 /* chunked not last, return badreq */
1363 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001364 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001365 }
1366
Willy Tarreau1c913912015-04-30 10:57:51 +02001367 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001368 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001369 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001370 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001371 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1372 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001373 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001374 signed long long cl;
1375
Willy Tarreauad14f752011-09-02 20:33:27 +02001376 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001377 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001378 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001379 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001380
Willy Tarreauad14f752011-09-02 20:33:27 +02001381 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001382 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001383 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001384 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001385
Willy Tarreauad14f752011-09-02 20:33:27 +02001386 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001387 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001388 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001389 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001390
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001391 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001392 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001393 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001394 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001395
Christopher Fauletbe821b92017-03-30 11:21:53 +02001396 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001397 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001398 }
1399
Willy Tarreau34dfc602015-05-01 10:09:49 +02001400 /* even bodyless requests have a known length */
1401 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001402
Willy Tarreau179085c2014-04-28 16:48:56 +02001403 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1404 * only change if both the request and the config reference something else.
1405 * Option httpclose by itself sets tunnel mode where headers are mangled.
1406 * However, if another mode is set, it will affect it (eg: server-close/
1407 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1408 * if FE and BE have the same settings (common). The method consists in
1409 * checking if options changed between the two calls (implying that either
1410 * one is non-null, or one of them is non-null and we are there for the first
1411 * time.
1412 */
1413 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001414 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001415 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001416
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001417 /* we may have to wait for the request's body */
1418 if ((s->be->options & PR_O_WREQ_BODY) &&
1419 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1420 req->analysers |= AN_REQ_HTTP_BODY;
1421
Willy Tarreau83ece462017-12-21 15:13:09 +01001422 /*
1423 * RFC7234#4:
1424 * A cache MUST write through requests with methods
1425 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1426 * the origin server; i.e., a cache is not allowed
1427 * to generate a reply to such a request before
1428 * having forwarded the request and having received
1429 * a corresponding response.
1430 *
1431 * RFC7231#4.2.1:
1432 * Of the request methods defined by this
1433 * specification, the GET, HEAD, OPTIONS, and TRACE
1434 * methods are defined to be safe.
1435 */
1436 if (likely(txn->meth == HTTP_METH_GET ||
1437 txn->meth == HTTP_METH_HEAD ||
1438 txn->meth == HTTP_METH_OPTIONS ||
1439 txn->meth == HTTP_METH_TRACE))
1440 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1441
Willy Tarreaud787e662009-07-07 10:14:51 +02001442 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001443 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001444 req->analyse_exp = TICK_ETERNITY;
1445 return 1;
1446
1447 return_bad_req:
1448 /* We centralize bad requests processing here */
1449 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1450 /* we detected a parsing error. We want to archive this request
1451 * in the dedicated proxy area for later troubleshooting.
1452 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001453 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001454 }
1455
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001456 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001457 txn->req.msg_state = HTTP_MSG_ERROR;
1458 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001459 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001460
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001461 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001462 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001463 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001464
1465 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001466 if (!(s->flags & SF_ERR_MASK))
1467 s->flags |= SF_ERR_PRXCOND;
1468 if (!(s->flags & SF_FINST_MASK))
1469 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001470
Christopher Faulet0184ea72017-01-05 14:06:34 +01001471 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001472 req->analyse_exp = TICK_ETERNITY;
1473 return 0;
1474}
1475
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001476
Willy Tarreau347a35d2013-11-22 17:51:09 +01001477/* This function prepares an applet to handle the stats. It can deal with the
1478 * "100-continue" expectation, check that admin rules are met for POST requests,
1479 * and program a response message if something was unexpected. It cannot fail
1480 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001481 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001482 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001483 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001484 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001485int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001486{
1487 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001488 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001489 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001490 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001491 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001492 struct uri_auth *uri_auth = s->be->uri_auth;
1493 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001494 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001495
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001496 appctx = si_appctx(si);
1497 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1498 appctx->st1 = appctx->st2 = 0;
1499 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1500 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001501 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001502 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001503
Willy Tarreauf37954d2018-06-15 18:31:02 +02001504 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001505 lookup = uri + uri_auth->uri_len;
1506
1507 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1508 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001509 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001510 break;
1511 }
1512 }
1513
1514 if (uri_auth->refresh) {
1515 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1516 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001517 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001518 break;
1519 }
1520 }
1521 }
1522
1523 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1524 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001525 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001526 break;
1527 }
1528 }
1529
Willy Tarreau1e62df92016-01-11 18:57:53 +01001530 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1531 if (memcmp(h, ";typed", 6) == 0) {
1532 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1533 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1534 break;
1535 }
1536 }
1537
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001538 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1539 if (memcmp(h, ";st=", 4) == 0) {
1540 int i;
1541 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001542 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001543 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1544 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001545 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001546 break;
1547 }
1548 }
1549 break;
1550 }
1551 }
1552
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001553 appctx->ctx.stats.scope_str = 0;
1554 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001555 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1556 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1557 int itx = 0;
1558 const char *h2;
1559 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1560 const char *err;
1561
1562 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1563 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001564 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001565 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1566 itx++;
1567 h++;
1568 }
1569
1570 if (itx > STAT_SCOPE_TXT_MAXLEN)
1571 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001572 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001573
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001574 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001575 memcpy(scope_txt, h2, itx);
1576 scope_txt[itx] = '\0';
1577 err = invalid_char(scope_txt);
1578 if (err) {
1579 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001580 appctx->ctx.stats.scope_str = 0;
1581 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001582 }
1583 break;
1584 }
1585 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001586
1587 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001588 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001589 int ret = 1;
1590
1591 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001592 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001593 ret = acl_pass(ret);
1594 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1595 ret = !ret;
1596 }
1597
1598 if (ret) {
1599 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001600 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001601 break;
1602 }
1603 }
1604
1605 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001606 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001607 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001608 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001609 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1610 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001611 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001612 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001613 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001614 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1615 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001616 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001617 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001618 else {
1619 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001620 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001621 }
1622
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001623 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001624 return 1;
1625}
1626
Lukas Tribus67db8df2013-06-23 17:37:13 +02001627/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1628 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1629 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001630void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001631{
1632#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001633 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001634 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1635#endif
1636#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001637 if (from->ss_family == AF_INET6) {
1638 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001639 /* v4-mapped addresses need IP_TOS */
1640 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1641 else
1642 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1643 }
1644#endif
1645}
1646
Willy Tarreau87b09662015-04-03 00:22:06 +02001647int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001648 const char* name, unsigned int name_len,
1649 const char *str, struct my_regex *re,
1650 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001651{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001652 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001653 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001654 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001655 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1656 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001657 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001658
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001659 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001660
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001661 /* Choose the header browsing function. */
1662 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001663 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001664 http_find_hdr_func = http_find_header2;
1665 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001666 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001667 http_find_hdr_func = http_find_full_header2;
1668 break;
1669 default: /* impossible */
1670 return -1;
1671 }
1672
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001673 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1674 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001675 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001676 char *val = ctx.line + ctx.val;
1677 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001678
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001679 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1680 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001681
Willy Tarreau6e27be12018-08-22 04:46:47 +02001682 len = exp_replace(output->area, output->size, val, str, pmatch);
1683 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001684 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001685
Willy Tarreau6e27be12018-08-22 04:46:47 +02001686 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001687
1688 hdr->len += delta;
1689 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001690
1691 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001692 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001693 }
1694
1695 return 0;
1696}
1697
Willy Tarreau87b09662015-04-03 00:22:06 +02001698static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001699 const char* name, unsigned int name_len,
1700 struct list *fmt, struct my_regex *re,
1701 int action)
1702{
Willy Tarreau83061a82018-07-13 11:56:34 +02001703 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001704 int ret = -1;
1705
1706 replace = alloc_trash_chunk();
1707 if (!replace)
1708 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001709
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001710 replace->data = build_logline(s, replace->area, replace->size, fmt);
1711 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001712 goto leave;
1713
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001714 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1715 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001716
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001717 leave:
1718 free_trash_chunk(replace);
1719 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001720}
1721
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001722/*
1723 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1724 * built according to <fmt> log line format.
1725 * If <early_hint> is false the HTTP 103 response first line is inserted before
1726 * the header.
1727 */
1728static int http_apply_early_hint_rule(struct stream* s, struct channel *resp, int early_hint,
1729 const char* name, unsigned int name_len, struct list *fmt)
1730{
1731 int ret;
1732 size_t data;
1733 struct buffer *chunk;
1734 char *cur_ptr = ci_head(resp);
1735
1736 ret = 0;
1737 data = co_data(resp);
1738
1739 chunk = alloc_trash_chunk();
1740 if (!chunk)
1741 goto leave;
1742
1743 if (!early_hint && !chunk_memcat(chunk, HTTP_103.ptr, HTTP_103.len))
1744 goto leave;
1745
1746 if (!chunk_memcat(chunk, name, name_len) || !chunk_memcat(chunk, ": ", 2))
1747 goto leave;
1748
1749 chunk->data += build_logline(s, chunk->area + chunk->data, chunk->size - chunk->data, fmt);
1750 if (!chunk_memcat(chunk, "\r\n", 2))
1751 goto leave;
1752
1753 ret = b_rep_blk(&resp->buf, cur_ptr, cur_ptr, chunk->area, chunk->data);
1754 c_adv(resp, ret);
1755
1756 leave:
1757 if (!ret)
1758 co_set_data(resp, data);
1759 free_trash_chunk(chunk);
1760
1761 return ret;
1762}
1763
Willy Tarreau87b09662015-04-03 00:22:06 +02001764/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001765 * transaction <txn>. Returns the verdict of the first rule that prevents
1766 * further processing of the request (auth, deny, ...), and defaults to
1767 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1768 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001769 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1770 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1771 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001772 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001773enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001774http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001775{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001776 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001777 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001778 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001779 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001780 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001781 const char *auth_realm;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001782 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001783 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001784 int len;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001785 int early_hint = 0;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001786
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001787 /* If "the current_rule_list" match the executed rule list, we are in
1788 * resume condition. If a resume is needed it is always in the action
1789 * and never in the ACL or converters. In this case, we initialise the
1790 * current rule, and go to the action execution point.
1791 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001792 if (s->current_rule) {
1793 rule = s->current_rule;
1794 s->current_rule = NULL;
1795 if (s->current_rule_list == rules)
1796 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001797 }
1798 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001799
Willy Tarreauff011f22011-01-06 17:51:27 +01001800 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001801
Willy Tarreau96257ec2012-12-27 10:46:37 +01001802 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001803 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001804 int ret;
1805
Willy Tarreau192252e2015-04-04 01:47:55 +02001806 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001807 ret = acl_pass(ret);
1808
Willy Tarreauff011f22011-01-06 17:51:27 +01001809 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001810 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001811
1812 if (!ret) /* condition not matched */
1813 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001814 }
1815
Willy Tarreauacc98002015-09-27 23:34:39 +02001816 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001817resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001818 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001819 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001820 rule_ret = HTTP_RULE_RES_STOP;
1821 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001822
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001823 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001824 if (deny_status)
1825 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001826 rule_ret = HTTP_RULE_RES_DENY;
1827 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001828
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001829 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001830 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001831 if (deny_status)
1832 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001833 rule_ret = HTTP_RULE_RES_DENY;
1834 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001835
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001836 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02001837 /* Auth might be performed on regular http-req rules as well as on stats */
1838 auth_realm = rule->arg.auth.realm;
1839 if (!auth_realm) {
1840 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1841 auth_realm = STATS_DEFAULT_REALM;
1842 else
1843 auth_realm = px->id;
1844 }
1845 /* send 401/407 depending on whether we use a proxy or not. We still
1846 * count one error, because normal browsing won't significantly
1847 * increase the counter but brute force attempts will.
1848 */
1849 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1850 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001851 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001852 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001853 rule_ret = HTTP_RULE_RES_ABRT;
1854 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001855
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001856 case ACT_HTTP_REDIR:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001857 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001858 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001859 rule_ret = HTTP_RULE_RES_BADREQ;
1860 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001861
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001862 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001863 s->task->nice = rule->arg.nice;
1864 break;
1865
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001866 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001867 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001868 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001869 break;
1870
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001871 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001872#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001873 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001874 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001875#endif
1876 break;
1877
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001878 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001879 s->logs.level = rule->arg.loglevel;
1880 break;
1881
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001882 case ACT_HTTP_REPLACE_HDR:
1883 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001884 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1885 rule->arg.hdr_add.name_len,
1886 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001887 &rule->arg.hdr_add.re, rule->action)) {
1888 rule_ret = HTTP_RULE_RES_BADREQ;
1889 goto end;
1890 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001891 break;
1892
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001893 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001894 ctx.idx = 0;
1895 /* remove all occurrences of the header */
1896 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001897 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001898 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001899 }
Willy Tarreau85603282015-01-21 20:39:27 +01001900 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001901
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001902 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001903 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001904 /* The scope of the trash buffer must be limited to this function. The
1905 * build_logline() function can execute a lot of other function which
1906 * can use the trash buffer. So for limiting the scope of this global
1907 * buffer, we build first the header value using build_logline, and
1908 * after we store the header name.
1909 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001910 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001911
1912 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001913 if (!replace) {
1914 rule_ret = HTTP_RULE_RES_BADREQ;
1915 goto end;
1916 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001917
Thierry Fournier4b788f72016-06-01 13:35:36 +02001918 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001919 len += build_logline(s, replace->area + len,
1920 replace->size - len,
1921 &rule->arg.hdr_add.fmt);
1922 memcpy(replace->area, rule->arg.hdr_add.name,
1923 rule->arg.hdr_add.name_len);
1924 replace->area[rule->arg.hdr_add.name_len] = ':';
1925 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1926 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001927
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001928 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001929 /* remove all occurrences of the header */
1930 ctx.idx = 0;
1931 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001932 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001933 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1934 }
1935 }
1936
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001937 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001938 static unsigned char rate_limit = 0;
1939
1940 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001941 replace->area[rule->arg.hdr_add.name_len] = 0;
1942 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,
1943 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001944 }
1945
1946 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1947 if (sess->fe != s->be)
1948 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1949 if (sess->listener->counters)
1950 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1951 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001952
1953 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001954 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001955 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001956
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001957 case ACT_HTTP_DEL_ACL:
1958 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001959 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001960 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001961
1962 /* collect reference */
1963 ref = pat_ref_lookup(rule->arg.map.ref);
1964 if (!ref)
1965 continue;
1966
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001967 /* allocate key */
1968 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001969 if (!key) {
1970 rule_ret = HTTP_RULE_RES_BADREQ;
1971 goto end;
1972 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001973
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001974 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001975 key->data = build_logline(s, key->area, key->size,
1976 &rule->arg.map.key);
1977 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001978
1979 /* perform update */
1980 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001981 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001982 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001983 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001984
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001985 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001986 break;
1987 }
1988
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001989 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001990 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001991 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001992
1993 /* collect reference */
1994 ref = pat_ref_lookup(rule->arg.map.ref);
1995 if (!ref)
1996 continue;
1997
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001998 /* allocate key */
1999 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002000 if (!key) {
2001 rule_ret = HTTP_RULE_RES_BADREQ;
2002 goto end;
2003 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002004
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002005 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002006 key->data = build_logline(s, key->area, key->size,
2007 &rule->arg.map.key);
2008 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002009
2010 /* perform update */
2011 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002012 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002013 if (pat_ref_find_elt(ref, key->area) == NULL)
2014 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002015 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002016
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002017 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002018 break;
2019 }
2020
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002021 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002022 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002023 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002024
2025 /* collect reference */
2026 ref = pat_ref_lookup(rule->arg.map.ref);
2027 if (!ref)
2028 continue;
2029
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002030 /* allocate key */
2031 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002032 if (!key) {
2033 rule_ret = HTTP_RULE_RES_BADREQ;
2034 goto end;
2035 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002036
2037 /* allocate value */
2038 value = alloc_trash_chunk();
2039 if (!value) {
2040 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01002041 rule_ret = HTTP_RULE_RES_BADREQ;
2042 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002043 }
2044
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002045 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002046 key->data = build_logline(s, key->area, key->size,
2047 &rule->arg.map.key);
2048 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002049
2050 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002051 value->data = build_logline(s, value->area,
2052 value->size,
2053 &rule->arg.map.value);
2054 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002055
2056 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002057 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002058 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002059 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002060 else
2061 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002062 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002063
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002064 free_trash_chunk(key);
2065 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002066 break;
2067 }
William Lallemand73025dd2014-04-24 14:38:37 +02002068
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002069 case ACT_HTTP_EARLY_HINT:
2070 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2071 break;
2072
2073 if (!http_apply_early_hint_rule(s, txn->rsp.chn, early_hint,
2074 rule->arg.early_hint.name,
2075 rule->arg.early_hint.name_len,
Christopher Fauletea827bd2018-11-15 15:34:11 +01002076 &rule->arg.early_hint.fmt)) {
2077 rule_ret = HTTP_RULE_RES_DONE;
2078 goto end;
2079 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002080 early_hint = 1;
2081 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002082 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002083 if ((s->req.flags & CF_READ_ERROR) ||
2084 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2085 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2086 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002087 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002088
Willy Tarreauacc98002015-09-27 23:34:39 +02002089 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002090 case ACT_RET_ERR:
2091 case ACT_RET_CONT:
2092 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002093 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01002094 rule_ret = HTTP_RULE_RES_DONE;
2095 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002096 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002097 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01002098 rule_ret = HTTP_RULE_RES_YIELD;
2099 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002100 }
William Lallemand73025dd2014-04-24 14:38:37 +02002101 break;
2102
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002103 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002104 /* Note: only the first valid tracking parameter of each
2105 * applies.
2106 */
2107
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002108 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002109 struct stktable *t;
2110 struct stksess *ts;
2111 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002112 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002113
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002114 t = rule->arg.trk_ctr.table.t;
2115 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 +02002116
2117 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002118 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002119
2120 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002121 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2122 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2123 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002124 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002125
Emeric Brun819fc6f2017-06-13 19:37:32 +02002126 if (ptr1)
2127 stktable_data_cast(ptr1, http_req_cnt)++;
2128
2129 if (ptr2)
2130 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2131 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2132
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002133 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002134
2135 /* If data was modified, we need to touch to re-schedule sync */
2136 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002137 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002138
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002139 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002140 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002141 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002142 }
2143 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002144 break;
2145
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002146 /* other flags exists, but normaly, they never be matched. */
2147 default:
2148 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002149 }
2150 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002151
Christopher Fauletea827bd2018-11-15 15:34:11 +01002152 end:
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002153 if (early_hint) {
2154 struct channel *chn = s->txn->rsp.chn;
2155 char *cur_ptr = ci_head(chn);
2156
2157 /* Add an empty line after Early Hint informational response headers */
2158 b_rep_blk(&chn->buf, cur_ptr, cur_ptr, "\r\n", 2);
2159 c_adv(chn, 2);
2160 }
2161
Willy Tarreau96257ec2012-12-27 10:46:37 +01002162 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01002163 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002164}
2165
Willy Tarreau71241ab2012-12-27 11:30:54 +01002166
Willy Tarreau51d861a2015-05-22 17:30:48 +02002167/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2168 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2169 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2170 * is returned, the process can continue the evaluation of next rule list. If
2171 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2172 * is returned, it means the operation could not be processed and a server error
2173 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2174 * deny rule. If *YIELD is returned, the caller must call again the function
2175 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002176 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002177enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002178http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002179{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002180 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002181 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002182 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002183 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002184 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002185 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02002186 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002187
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002188 /* If "the current_rule_list" match the executed rule list, we are in
2189 * resume condition. If a resume is needed it is always in the action
2190 * and never in the ACL or converters. In this case, we initialise the
2191 * current rule, and go to the action execution point.
2192 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002193 if (s->current_rule) {
2194 rule = s->current_rule;
2195 s->current_rule = NULL;
2196 if (s->current_rule_list == rules)
2197 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002198 }
2199 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002200
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002201 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002202
2203 /* check optional condition */
2204 if (rule->cond) {
2205 int ret;
2206
Willy Tarreau192252e2015-04-04 01:47:55 +02002207 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002208 ret = acl_pass(ret);
2209
2210 if (rule->cond->pol == ACL_COND_UNLESS)
2211 ret = !ret;
2212
2213 if (!ret) /* condition not matched */
2214 continue;
2215 }
2216
Willy Tarreauacc98002015-09-27 23:34:39 +02002217 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002218resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002219 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002220 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002221 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2222 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002223
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002224 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002225 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002226 rule_ret = HTTP_RULE_RES_STOP;
2227 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002228
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002229 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002230 s->task->nice = rule->arg.nice;
2231 break;
2232
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002233 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002234 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002235 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002236 break;
2237
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002238 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002239#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002240 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002241 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002242#endif
2243 break;
2244
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002245 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002246 s->logs.level = rule->arg.loglevel;
2247 break;
2248
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002249 case ACT_HTTP_REPLACE_HDR:
2250 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002251 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2252 rule->arg.hdr_add.name_len,
2253 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002254 &rule->arg.hdr_add.re, rule->action)) {
2255 rule_ret = HTTP_RULE_RES_BADREQ;
2256 goto end;
2257 }
Sasha Pachev218f0642014-06-16 12:05:59 -06002258 break;
2259
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002260 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002261 ctx.idx = 0;
2262 /* remove all occurrences of the header */
2263 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002264 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002265 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2266 }
Willy Tarreau85603282015-01-21 20:39:27 +01002267 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002268
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002269 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002270 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002271 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002272
2273 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002274 if (!replace) {
2275 rule_ret = HTTP_RULE_RES_BADREQ;
2276 goto end;
2277 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002278
2279 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002280 memcpy(replace->area, rule->arg.hdr_add.name,
2281 rule->arg.hdr_add.name_len);
2282 replace->data = rule->arg.hdr_add.name_len;
2283 replace->area[replace->data++] = ':';
2284 replace->area[replace->data++] = ' ';
2285 replace->data += build_logline(s,
2286 replace->area + replace->data,
2287 replace->size - replace->data,
2288 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002289
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002290 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002291 /* remove all occurrences of the header */
2292 ctx.idx = 0;
2293 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002294 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002295 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2296 }
2297 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002298
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002299 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002300 static unsigned char rate_limit = 0;
2301
2302 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002303 replace->area[rule->arg.hdr_add.name_len] = 0;
2304 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,
2305 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002306 }
2307
2308 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2309 if (sess->fe != s->be)
2310 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2311 if (sess->listener->counters)
2312 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2313 if (objt_server(s->target))
2314 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2315 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002316
2317 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002318 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002319 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002320
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002321 case ACT_HTTP_DEL_ACL:
2322 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002323 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002324 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002325
2326 /* collect reference */
2327 ref = pat_ref_lookup(rule->arg.map.ref);
2328 if (!ref)
2329 continue;
2330
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002331 /* allocate key */
2332 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002333 if (!key) {
2334 rule_ret = HTTP_RULE_RES_BADREQ;
2335 goto end;
2336 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002337
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002338 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002339 key->data = build_logline(s, key->area, key->size,
2340 &rule->arg.map.key);
2341 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002342
2343 /* perform update */
2344 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002345 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002346 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002347 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002348
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002349 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002350 break;
2351 }
2352
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002353 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002354 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002355 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002356
2357 /* collect reference */
2358 ref = pat_ref_lookup(rule->arg.map.ref);
2359 if (!ref)
2360 continue;
2361
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002362 /* allocate key */
2363 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002364 if (!key) {
2365 rule_ret = HTTP_RULE_RES_BADREQ;
2366 goto end;
2367 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002368
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002369 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002370 key->data = build_logline(s, key->area, key->size,
2371 &rule->arg.map.key);
2372 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002373
2374 /* perform update */
2375 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002376 if (pat_ref_find_elt(ref, key->area) == NULL)
2377 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002378
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002379 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002380 break;
2381 }
2382
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002383 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002384 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002385 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002386
2387 /* collect reference */
2388 ref = pat_ref_lookup(rule->arg.map.ref);
2389 if (!ref)
2390 continue;
2391
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002392 /* allocate key */
2393 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002394 if (!key) {
2395 rule_ret = HTTP_RULE_RES_BADREQ;
2396 goto end;
2397 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002398
2399 /* allocate value */
2400 value = alloc_trash_chunk();
2401 if (!value) {
2402 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002403 rule_ret = HTTP_RULE_RES_BADREQ;
2404 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002405 }
2406
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002407 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002408 key->data = build_logline(s, key->area, key->size,
2409 &rule->arg.map.key);
2410 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002411
2412 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002413 value->data = build_logline(s, value->area,
2414 value->size,
2415 &rule->arg.map.value);
2416 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002417
2418 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002419 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002420 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002421 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002422 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002423 else
2424 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002425 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002426 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002427 free_trash_chunk(key);
2428 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002429 break;
2430 }
William Lallemand73025dd2014-04-24 14:38:37 +02002431
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002432 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002433 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002434 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002435 rule_ret = HTTP_RULE_RES_BADREQ;
2436 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002437
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002438 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2439 /* Note: only the first valid tracking parameter of each
2440 * applies.
2441 */
2442
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002443 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002444 struct stktable *t;
2445 struct stksess *ts;
2446 struct stktable_key *key;
2447 void *ptr;
2448
2449 t = rule->arg.trk_ctr.table.t;
2450 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2451
2452 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002453 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002454
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002455 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002456
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002457 /* let's count a new HTTP request as it's the first time we do it */
2458 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2459 if (ptr)
2460 stktable_data_cast(ptr, http_req_cnt)++;
2461
2462 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2463 if (ptr)
2464 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2465 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2466
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002467 /* When the client triggers a 4xx from the server, it's most often due
2468 * to a missing object or permission. These events should be tracked
2469 * because if they happen often, it may indicate a brute force or a
2470 * vulnerability scan. Normally this is done when receiving the response
2471 * but here we're tracking after this ought to have been done so we have
2472 * to do it on purpose.
2473 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002474 if ((unsigned)(txn->status - 400) < 100) {
2475 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2476 if (ptr)
2477 stktable_data_cast(ptr, http_err_cnt)++;
2478
2479 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2480 if (ptr)
2481 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2482 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2483 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002484
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002485 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002486
Emeric Brun0fed0b02017-11-29 16:15:07 +01002487 /* If data was modified, we need to touch to re-schedule sync */
2488 stktable_touch_local(t, ts, 0);
2489
Emeric Brun819fc6f2017-06-13 19:37:32 +02002490 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2491 if (sess->fe != s->be)
2492 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2493
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002494 }
2495 }
2496 break;
2497
Thierry FOURNIER42148732015-09-02 17:17:33 +02002498 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002499 if ((s->req.flags & CF_READ_ERROR) ||
2500 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2501 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2502 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002503 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002504
Willy Tarreauacc98002015-09-27 23:34:39 +02002505 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002506 case ACT_RET_ERR:
2507 case ACT_RET_CONT:
2508 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002509 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002510 rule_ret = HTTP_RULE_RES_STOP;
2511 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002512 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002513 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002514 rule_ret = HTTP_RULE_RES_YIELD;
2515 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002516 }
William Lallemand73025dd2014-04-24 14:38:37 +02002517 break;
2518
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002519 /* other flags exists, but normaly, they never be matched. */
2520 default:
2521 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002522 }
2523 }
2524
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002525 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002526 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002527 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002528}
2529
2530
Willy Tarreau71241ab2012-12-27 11:30:54 +01002531/* Perform an HTTP redirect based on the information in <rule>. The function
2532 * returns non-zero on success, or zero in case of a, irrecoverable error such
2533 * as too large a request to build a valid response.
2534 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002535int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002536{
Willy Tarreaub329a312015-05-22 16:27:37 +02002537 struct http_msg *req = &txn->req;
2538 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002539 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002540 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002541 int ret = 0;
2542
2543 chunk = alloc_trash_chunk();
2544 if (!chunk)
2545 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002546
2547 /* build redirect message */
2548 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002549 case 308:
2550 msg_fmt = HTTP_308;
2551 break;
2552 case 307:
2553 msg_fmt = HTTP_307;
2554 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002555 case 303:
2556 msg_fmt = HTTP_303;
2557 break;
2558 case 301:
2559 msg_fmt = HTTP_301;
2560 break;
2561 case 302:
2562 default:
2563 msg_fmt = HTTP_302;
2564 break;
2565 }
2566
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002567 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2568 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002569
2570 switch(rule->type) {
2571 case REDIRECT_TYPE_SCHEME: {
2572 const char *path;
2573 const char *host;
2574 struct hdr_ctx ctx;
2575 int pathlen;
2576 int hostlen;
2577
2578 host = "";
2579 hostlen = 0;
2580 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002581 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002582 host = ctx.line + ctx.val;
2583 hostlen = ctx.vlen;
2584 }
2585
Willy Tarreau6b952c82018-09-10 17:45:34 +02002586 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002587 /* build message using path */
2588 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002589 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002590 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2591 int qs = 0;
2592 while (qs < pathlen) {
2593 if (path[qs] == '?') {
2594 pathlen = qs;
2595 break;
2596 }
2597 qs++;
2598 }
2599 }
2600 } else {
2601 path = "/";
2602 pathlen = 1;
2603 }
2604
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002605 if (rule->rdr_str) { /* this is an old "redirect" rule */
2606 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002607 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002608 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002609
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002610 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002611 memcpy(chunk->area + chunk->data, rule->rdr_str,
2612 rule->rdr_len);
2613 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002614 }
2615 else {
2616 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002617 chunk->data += build_logline(s,
2618 chunk->area + chunk->data,
2619 chunk->size - chunk->data,
2620 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002621
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002622 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002623 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002624 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002625 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002626 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002627 memcpy(chunk->area + chunk->data, "://", 3);
2628 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002629
2630 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002631 memcpy(chunk->area + chunk->data, host, hostlen);
2632 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002633
2634 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002635 memcpy(chunk->area + chunk->data, path, pathlen);
2636 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002637
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002638 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002639 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002640 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002641 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002642 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002643 chunk->area[chunk->data] = '/';
2644 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002645 }
2646
2647 break;
2648 }
2649 case REDIRECT_TYPE_PREFIX: {
2650 const char *path;
2651 int pathlen;
2652
Willy Tarreau6b952c82018-09-10 17:45:34 +02002653 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002654 /* build message using path */
2655 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002656 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002657 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2658 int qs = 0;
2659 while (qs < pathlen) {
2660 if (path[qs] == '?') {
2661 pathlen = qs;
2662 break;
2663 }
2664 qs++;
2665 }
2666 }
2667 } else {
2668 path = "/";
2669 pathlen = 1;
2670 }
2671
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002672 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002673 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002674 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002675
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002676 /* add prefix. Note that if prefix == "/", we don't want to
2677 * add anything, otherwise it makes it hard for the user to
2678 * configure a self-redirection.
2679 */
2680 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002681 memcpy(chunk->area + chunk->data,
2682 rule->rdr_str, rule->rdr_len);
2683 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002684 }
2685 }
2686 else {
2687 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002688 chunk->data += build_logline(s,
2689 chunk->area + chunk->data,
2690 chunk->size - chunk->data,
2691 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002692
2693 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002694 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002695 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002696 }
2697
2698 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002699 memcpy(chunk->area + chunk->data, path, pathlen);
2700 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002701
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002702 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002703 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002704 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002705 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002706 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002707 chunk->area[chunk->data] = '/';
2708 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002709 }
2710
2711 break;
2712 }
2713 case REDIRECT_TYPE_LOCATION:
2714 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002715 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002716 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002717 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002718
2719 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002720 memcpy(chunk->area + chunk->data, rule->rdr_str,
2721 rule->rdr_len);
2722 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002723 }
2724 else {
2725 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002726 chunk->data += build_logline(s,
2727 chunk->area + chunk->data,
2728 chunk->size - chunk->data,
2729 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002730
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002731 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002732 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002733 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002734 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002735 break;
2736 }
2737
2738 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002739 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2740 chunk->data += 14;
2741 memcpy(chunk->area + chunk->data, rule->cookie_str,
2742 rule->cookie_len);
2743 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002744 }
2745
Willy Tarreau19b14122017-02-28 09:48:11 +01002746 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002747 txn->status = rule->code;
2748 /* let's log the request time */
2749 s->logs.tv_request = now;
2750
Christopher Fauletbe821b92017-03-30 11:21:53 +02002751 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002752 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2753 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2754 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002755 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002756 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002757 memcpy(chunk->area + chunk->data,
2758 "\r\nProxy-Connection: keep-alive", 30);
2759 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002760 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002761 memcpy(chunk->area + chunk->data,
2762 "\r\nConnection: keep-alive", 24);
2763 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002764 }
2765 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002766 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2767 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002768 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002769 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002770 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002771 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002772 req->next -= req->sov;
2773 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002774 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002775 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002776 req->msg_state = HTTP_MSG_CLOSED;
2777 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002778 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002779 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002780 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002781 /* let the server side turn to SI_ST_CLO */
2782 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002783 } else {
2784 /* keep-alive not possible */
2785 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002786 memcpy(chunk->area + chunk->data,
2787 "\r\nProxy-Connection: close\r\n\r\n", 29);
2788 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002789 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002790 memcpy(chunk->area + chunk->data,
2791 "\r\nConnection: close\r\n\r\n", 23);
2792 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002793 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002794 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002795 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002796 }
2797
Willy Tarreaue7dff022015-04-03 01:14:29 +02002798 if (!(s->flags & SF_ERR_MASK))
2799 s->flags |= SF_ERR_LOCAL;
2800 if (!(s->flags & SF_FINST_MASK))
2801 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002802
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002803 ret = 1;
2804 leave:
2805 free_trash_chunk(chunk);
2806 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002807}
2808
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002809/* This stream analyser runs all HTTP request processing which is common to
2810 * frontends and backends, which means blocking ACLs, filters, connection-close,
2811 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002812 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002813 * either needs more data or wants to immediately abort the request (eg: deny,
2814 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002815 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002816int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002817{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002818 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002819 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002820 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002821 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002822 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002823 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002824 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002825 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002826
Willy Tarreau655dce92009-11-08 13:10:58 +01002827 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002828 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002829 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002830 }
2831
Christopher Faulet45073512018-07-20 10:16:29 +02002832 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 +02002833 now_ms, __FUNCTION__,
2834 s,
2835 req,
2836 req->rex, req->wex,
2837 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002838 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002839 req->analysers);
2840
Willy Tarreau65410832014-04-28 21:25:43 +02002841 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002842 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002843
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002844 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002845 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002846 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002847
Willy Tarreau0b748332014-04-29 00:13:29 +02002848 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002849 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2850 goto return_prx_yield;
2851
Willy Tarreau0b748332014-04-29 00:13:29 +02002852 case HTTP_RULE_RES_CONT:
2853 case HTTP_RULE_RES_STOP: /* nothing to do */
2854 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002855
Willy Tarreau0b748332014-04-29 00:13:29 +02002856 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2857 if (txn->flags & TX_CLTARPIT)
2858 goto tarpit;
2859 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002860
Willy Tarreau0b748332014-04-29 00:13:29 +02002861 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2862 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002863
Willy Tarreau0b748332014-04-29 00:13:29 +02002864 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002865 goto done;
2866
Willy Tarreau0b748332014-04-29 00:13:29 +02002867 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2868 goto return_bad_req;
2869 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002870 }
2871
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002872 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2873 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002874 struct hdr_ctx ctx;
2875
2876 ctx.idx = 0;
2877 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002878 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002879 if (unlikely(http_header_add_tail2(&txn->req,
2880 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002881 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002882 goto return_bad_req;
2883 }
2884 }
2885
2886 }
2887
Willy Tarreau52542592014-04-28 18:33:26 +02002888 /* OK at this stage, we know that the request was accepted according to
2889 * the http-request rules, we can check for the stats. Note that the
2890 * URI is detected *before* the req* rules in order not to be affected
2891 * by a possible reqrep, while they are processed *after* so that a
2892 * reqdeny can still block them. This clearly needs to change in 1.6!
2893 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002894 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002895 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002896 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002897 txn->status = 500;
2898 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002899 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002900
Willy Tarreaue7dff022015-04-03 01:14:29 +02002901 if (!(s->flags & SF_ERR_MASK))
2902 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002903 goto return_prx_cond;
2904 }
2905
2906 /* parse the whole stats request and extract the relevant information */
2907 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002908 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002909 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002910
Willy Tarreau0b748332014-04-29 00:13:29 +02002911 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2912 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002913
Willy Tarreau0b748332014-04-29 00:13:29 +02002914 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2915 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002916 }
2917
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002918 /* evaluate the req* rules except reqadd */
2919 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002920 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002921 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002922
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002923 if (txn->flags & TX_CLDENY)
2924 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002925
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002926 if (txn->flags & TX_CLTARPIT) {
2927 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002928 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002929 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002930 }
Willy Tarreau06619262006-12-17 08:37:22 +01002931
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002932 /* add request headers from the rule sets in the same order */
2933 list_for_each_entry(wl, &px->req_add, list) {
2934 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002935 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002936 ret = acl_pass(ret);
2937 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2938 ret = !ret;
2939 if (!ret)
2940 continue;
2941 }
2942
Christopher Faulet10079f52018-10-03 15:17:28 +02002943 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002944 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002945 }
2946
Willy Tarreau52542592014-04-28 18:33:26 +02002947
2948 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002949 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2950 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002951 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002952 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002953 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002954
Willy Tarreaue7dff022015-04-03 01:14:29 +02002955 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2956 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2957 if (!(s->flags & SF_FINST_MASK))
2958 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002959
Willy Tarreau70730dd2014-04-24 18:06:27 +02002960 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002961 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2962 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002963 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002964 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002965 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002966
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002967 /* check whether we have some ACLs set to redirect this request */
2968 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002969 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002970 int ret;
2971
Willy Tarreau192252e2015-04-04 01:47:55 +02002972 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002973 ret = acl_pass(ret);
2974 if (rule->cond->pol == ACL_COND_UNLESS)
2975 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002976 if (!ret)
2977 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002978 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002979 if (!http_apply_redirect_rule(rule, s, txn))
2980 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002981 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002982 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002983
Willy Tarreau2be39392010-01-03 17:24:51 +01002984 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2985 * If this happens, then the data will not come immediately, so we must
2986 * send all what we have without waiting. Note that due to the small gain
2987 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002988 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002989 * itself once used.
2990 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002991 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002992
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002993 done: /* done with this analyser, continue with next ones that the calling
2994 * points will have set, if any.
2995 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002996 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002997 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
2998 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002999 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003000
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003001 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003002 /* Allow cookie logging
3003 */
3004 if (s->be->cookie_name || sess->fe->capture_name)
3005 manage_client_side_cookies(s, req);
3006
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003007 /* When a connection is tarpitted, we use the tarpit timeout,
3008 * which may be the same as the connect timeout if unspecified.
3009 * If unset, then set it to zero because we really want it to
3010 * eventually expire. We build the tarpit as an analyser.
3011 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003012 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003013
3014 /* wipe the request out so that we can drop the connection early
3015 * if the client closes first.
3016 */
3017 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003018
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003019 txn->status = http_err_codes[deny_status];
3020
Christopher Faulet0184ea72017-01-05 14:06:34 +01003021 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003022 req->analysers |= AN_REQ_HTTP_TARPIT;
3023 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3024 if (!req->analyse_exp)
3025 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003026 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003027 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003028 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003029 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003030 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003031 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003032 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003033
3034 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003035
3036 /* Allow cookie logging
3037 */
3038 if (s->be->cookie_name || sess->fe->capture_name)
3039 manage_client_side_cookies(s, req);
3040
Willy Tarreau0b748332014-04-29 00:13:29 +02003041 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003042 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003043 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003044 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003045 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003046 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003047 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003048 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003049 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003050 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003051 goto return_prx_cond;
3052
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003053 return_bad_req:
3054 /* We centralize bad requests processing here */
3055 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3056 /* we detected a parsing error. We want to archive this request
3057 * in the dedicated proxy area for later troubleshooting.
3058 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003059 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003060 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003061
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003062 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003063 txn->req.msg_state = HTTP_MSG_ERROR;
3064 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003065 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003066
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003067 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_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->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003070
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003071 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003072 if (!(s->flags & SF_ERR_MASK))
3073 s->flags |= SF_ERR_PRXCOND;
3074 if (!(s->flags & SF_FINST_MASK))
3075 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003076
Christopher Faulet0184ea72017-01-05 14:06:34 +01003077 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003078 req->analyse_exp = TICK_ETERNITY;
3079 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003080
3081 return_prx_yield:
3082 channel_dont_connect(req);
3083 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003084}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003085
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003086/* This function performs all the processing enabled for the current request.
3087 * It returns 1 if the processing can continue on next analysers, or zero if it
3088 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003089 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003090 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003091int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003092{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003093 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003094 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003095 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003096 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003097
Willy Tarreau655dce92009-11-08 13:10:58 +01003098 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003099 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003100 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003101 return 0;
3102 }
3103
Christopher Faulet45073512018-07-20 10:16:29 +02003104 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 +02003105 now_ms, __FUNCTION__,
3106 s,
3107 req,
3108 req->rex, req->wex,
3109 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003110 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003111 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003112
Willy Tarreau59234e92008-11-30 23:51:27 +01003113 /*
3114 * Right now, we know that we have processed the entire headers
3115 * and that unwanted requests have been filtered out. We can do
3116 * whatever we want with the remaining request. Also, now we
3117 * may have separate values for ->fe, ->be.
3118 */
Willy Tarreau06619262006-12-17 08:37:22 +01003119
Willy Tarreau59234e92008-11-30 23:51:27 +01003120 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003121 * If HTTP PROXY is set we simply get remote server address parsing
3122 * incoming request. Note that this requires that a connection is
3123 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003124 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003125 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003126 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003127 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003128
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003129 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003130 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003131 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003132 txn->req.msg_state = HTTP_MSG_ERROR;
3133 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003134 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003135 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003136
Willy Tarreaue7dff022015-04-03 01:14:29 +02003137 if (!(s->flags & SF_ERR_MASK))
3138 s->flags |= SF_ERR_RESOURCE;
3139 if (!(s->flags & SF_FINST_MASK))
3140 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003141
3142 return 0;
3143 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003144
Willy Tarreau6b952c82018-09-10 17:45:34 +02003145 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003146 if (url2sa(ci_head(req) + msg->sl.rq.u,
3147 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003148 &conn->addr.to, NULL) == -1)
3149 goto return_bad_req;
3150
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003151 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003152 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3153 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003154 * u_l characters by a single "/".
3155 */
3156 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003157 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003158 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3159 int delta;
3160
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003161 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003162 http_msg_move_end(&txn->req, delta);
3163 cur_end += delta;
3164 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3165 goto return_bad_req;
3166 }
3167 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003168 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003169 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3170 int delta;
3171
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003172 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003173 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003174 http_msg_move_end(&txn->req, delta);
3175 cur_end += delta;
3176 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3177 goto return_bad_req;
3178 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003179 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003180
Willy Tarreau59234e92008-11-30 23:51:27 +01003181 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003182 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003183 * Note that doing so might move headers in the request, but
3184 * the fields will stay coherent and the URI will not move.
3185 * This should only be performed in the backend.
3186 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003187 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003188 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003189
William Lallemanda73203e2012-03-12 12:48:57 +01003190 /* add unique-id if "header-unique-id" is specified */
3191
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003192 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003193 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003194 goto return_bad_req;
3195 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003196 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003197 }
William Lallemanda73203e2012-03-12 12:48:57 +01003198
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003199 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003200 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003201 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003202 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003203 goto return_bad_req;
3204 }
3205
Cyril Bontéb21570a2009-11-29 20:04:48 +01003206 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003207 * 9: add X-Forwarded-For if either the frontend or the backend
3208 * asks for it.
3209 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003210 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003211 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003212 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3213 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3214 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003215 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003216 /* The header is set to be added only if none is present
3217 * and we found it, so don't do anything.
3218 */
3219 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003220 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003221 /* Add an X-Forwarded-For header unless the source IP is
3222 * in the 'except' network range.
3223 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003224 if ((!sess->fe->except_mask.s_addr ||
3225 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3226 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003227 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003228 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003229 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003230 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003231 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003232 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003233
3234 /* Note: we rely on the backend to get the header name to be used for
3235 * x-forwarded-for, because the header is really meant for the backends.
3236 * However, if the backend did not specify any option, we have to rely
3237 * on the frontend's header name.
3238 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003239 if (s->be->fwdfor_hdr_len) {
3240 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003241 memcpy(trash.area,
3242 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003243 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003244 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003245 memcpy(trash.area,
3246 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003247 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003248 len += snprintf(trash.area + len,
3249 trash.size - len,
3250 ": %d.%d.%d.%d", pn[0], pn[1],
3251 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003252
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003253 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003254 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003255 }
3256 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003257 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003258 /* FIXME: for the sake of completeness, we should also support
3259 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003260 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003261 int len;
3262 char pn[INET6_ADDRSTRLEN];
3263 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003264 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003265 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003266
Willy Tarreau59234e92008-11-30 23:51:27 +01003267 /* Note: we rely on the backend to get the header name to be used for
3268 * x-forwarded-for, because the header is really meant for the backends.
3269 * However, if the backend did not specify any option, we have to rely
3270 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003271 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003272 if (s->be->fwdfor_hdr_len) {
3273 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003274 memcpy(trash.area, s->be->fwdfor_hdr_name,
3275 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003276 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003277 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003278 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3279 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003280 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003281 len += snprintf(trash.area + len, trash.size - len,
3282 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003283
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003284 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003285 goto return_bad_req;
3286 }
3287 }
3288
3289 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003290 * 10: add X-Original-To if either the frontend or the backend
3291 * asks for it.
3292 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003293 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003294
3295 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003296 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003297 /* Add an X-Original-To header unless the destination IP is
3298 * in the 'except' network range.
3299 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003300 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003301
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003302 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003303 ((!sess->fe->except_mask_to.s_addr ||
3304 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3305 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003306 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003307 (((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 +02003308 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003309 int len;
3310 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003311 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003312
3313 /* Note: we rely on the backend to get the header name to be used for
3314 * x-original-to, because the header is really meant for the backends.
3315 * However, if the backend did not specify any option, we have to rely
3316 * on the frontend's header name.
3317 */
3318 if (s->be->orgto_hdr_len) {
3319 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003320 memcpy(trash.area,
3321 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003322 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003323 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003324 memcpy(trash.area,
3325 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003326 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003327 len += snprintf(trash.area + len,
3328 trash.size - len,
3329 ": %d.%d.%d.%d", pn[0], pn[1],
3330 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003331
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003332 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003333 goto return_bad_req;
3334 }
3335 }
3336 }
3337
Willy Tarreau50fc7772012-11-11 22:19:57 +01003338 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3339 * If an "Upgrade" token is found, the header is left untouched in order not to have
3340 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3341 * "Upgrade" is present in the Connection header.
3342 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003343 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003344 unsigned int want_flags = 0;
3345
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003346 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003347 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003348 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003349 want_flags |= TX_CON_CLO_SET;
3350 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003351 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003352 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003353 want_flags |= TX_CON_KAL_SET;
3354 }
3355
3356 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003357 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003358 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003359
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003360
Willy Tarreau522d6c02009-12-06 18:49:18 +01003361 /* If we have no server assigned yet and we're balancing on url_param
3362 * with a POST request, we may be interested in checking the body for
3363 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003364 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003365 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003366 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003367 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003368 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003369 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003370 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003371
Christopher Fauletbe821b92017-03-30 11:21:53 +02003372 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3373 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003374#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003375 /* We expect some data from the client. Unless we know for sure
3376 * we already have a full request, we have to re-enable quick-ack
3377 * in case we previously disabled it, otherwise we might cause
3378 * the client to delay further data.
3379 */
3380 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3381 cli_conn && conn_ctrl_ready(cli_conn) &&
3382 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003383 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003384 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003385#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003386
Willy Tarreau59234e92008-11-30 23:51:27 +01003387 /*************************************************************
3388 * OK, that's finished for the headers. We have done what we *
3389 * could. Let's switch to the DATA state. *
3390 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003391 req->analyse_exp = TICK_ETERNITY;
3392 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003393
Willy Tarreau59234e92008-11-30 23:51:27 +01003394 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003395 /* OK let's go on with the BODY now */
3396 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003397
Willy Tarreau59234e92008-11-30 23:51:27 +01003398 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003399 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003400 /* we detected a parsing error. We want to archive this request
3401 * in the dedicated proxy area for later troubleshooting.
3402 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003403 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003404 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003405
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003406 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003407 txn->req.msg_state = HTTP_MSG_ERROR;
3408 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003409 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003410 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003411
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003412 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003413 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003414 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003415
Willy Tarreaue7dff022015-04-03 01:14:29 +02003416 if (!(s->flags & SF_ERR_MASK))
3417 s->flags |= SF_ERR_PRXCOND;
3418 if (!(s->flags & SF_FINST_MASK))
3419 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003420 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003421}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003422
Willy Tarreau60b85b02008-11-30 23:28:40 +01003423/* This function is an analyser which processes the HTTP tarpit. It always
3424 * returns zero, at the beginning because it prevents any other processing
3425 * from occurring, and at the end because it terminates the request.
3426 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003427int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003428{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003429 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003430
3431 /* This connection is being tarpitted. The CLIENT side has
3432 * already set the connect expiration date to the right
3433 * timeout. We just have to check that the client is still
3434 * there and that the timeout has not expired.
3435 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003436 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003437 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003438 !tick_is_expired(req->analyse_exp, now_ms))
3439 return 0;
3440
3441 /* We will set the queue timer to the time spent, just for
3442 * logging purposes. We fake a 500 server error, so that the
3443 * attacker will not suspect his connection has been tarpitted.
3444 * It will not cause trouble to the logs because we can exclude
3445 * the tarpitted connections by filtering on the 'PT' status flags.
3446 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003447 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3448
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003449 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003450 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003451
Christopher Faulet0184ea72017-01-05 14:06:34 +01003452 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003453 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003454
Willy Tarreaue7dff022015-04-03 01:14:29 +02003455 if (!(s->flags & SF_ERR_MASK))
3456 s->flags |= SF_ERR_PRXCOND;
3457 if (!(s->flags & SF_FINST_MASK))
3458 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003459 return 0;
3460}
3461
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003462/* This function is an analyser which waits for the HTTP request body. It waits
3463 * for either the buffer to be full, or the full advertised contents to have
3464 * reached the buffer. It must only be called after the standard HTTP request
3465 * processing has occurred, because it expects the request to be parsed and will
3466 * look for the Expect header. It may send a 100-Continue interim response. It
3467 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3468 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3469 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003470 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003471int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003472{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003473 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003474 struct http_txn *txn = s->txn;
3475 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003476
3477 /* We have to parse the HTTP request body to find any required data.
3478 * "balance url_param check_post" should have been the only way to get
3479 * into this. We were brought here after HTTP header analysis, so all
3480 * related structures are ready.
3481 */
3482
Willy Tarreau890988f2014-04-10 11:59:33 +02003483 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3484 /* This is the first call */
3485 if (msg->msg_state < HTTP_MSG_BODY)
3486 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003487
Willy Tarreau890988f2014-04-10 11:59:33 +02003488 if (msg->msg_state < HTTP_MSG_100_SENT) {
3489 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3490 * send an HTTP/1.1 100 Continue intermediate response.
3491 */
3492 if (msg->flags & HTTP_MSGF_VER_11) {
3493 struct hdr_ctx ctx;
3494 ctx.idx = 0;
3495 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003496 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003497 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003498 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003499 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003500 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003501 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003502 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003503 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003504
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003505 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003506 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003507 * must save the body in msg->next because it survives buffer
3508 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003509 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003510 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003511
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003512 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003513 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3514 else
3515 msg->msg_state = HTTP_MSG_DATA;
3516 }
3517
Willy Tarreau890988f2014-04-10 11:59:33 +02003518 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3519 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003520 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003521 goto missing_data;
3522
3523 /* OK we have everything we need now */
3524 goto http_end;
3525 }
3526
3527 /* OK here we're parsing a chunked-encoded message */
3528
Willy Tarreau522d6c02009-12-06 18:49:18 +01003529 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003530 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003531 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003532 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003533 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003534 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003535 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003536
Willy Tarreau115acb92009-12-26 13:56:06 +01003537 if (!ret)
3538 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003539 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003540 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003541 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003542 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003543 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003544 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003545 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003546
3547 msg->chunk_len = chunk;
3548 msg->body_len += chunk;
3549
3550 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003551 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003552 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003553 }
3554
Willy Tarreaud98cf932009-12-27 22:54:55 +01003555 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003556 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3557 * for at least a whole chunk or the whole content length bytes after
3558 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003559 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003560 if (msg->msg_state == HTTP_MSG_TRAILERS)
3561 goto http_end;
3562
Willy Tarreaue115b492015-05-01 23:05:14 +02003563 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003564 goto http_end;
3565
3566 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003567 /* we get here if we need to wait for more data. If the buffer is full,
3568 * we have the maximum we can expect.
3569 */
Willy Tarreau23752332018-06-15 14:54:53 +02003570 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003571 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003572
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003573 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003574 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003575 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003576
Willy Tarreaue7dff022015-04-03 01:14:29 +02003577 if (!(s->flags & SF_ERR_MASK))
3578 s->flags |= SF_ERR_CLITO;
3579 if (!(s->flags & SF_FINST_MASK))
3580 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003581 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003582 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003583
3584 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003585 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003586 /* Not enough data. We'll re-use the http-request
3587 * timeout here. Ideally, we should set the timeout
3588 * relative to the accept() date. We just set the
3589 * request timeout once at the beginning of the
3590 * request.
3591 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003592 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003593 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003594 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003595 return 0;
3596 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003597
3598 http_end:
3599 /* The situation will not evolve, so let's give up on the analysis. */
3600 s->logs.tv_request = now; /* update the request timer to reflect full request */
3601 req->analysers &= ~an_bit;
3602 req->analyse_exp = TICK_ETERNITY;
3603 return 1;
3604
3605 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003606 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003607 txn->req.msg_state = HTTP_MSG_ERROR;
3608 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003609 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003610
Willy Tarreaue7dff022015-04-03 01:14:29 +02003611 if (!(s->flags & SF_ERR_MASK))
3612 s->flags |= SF_ERR_PRXCOND;
3613 if (!(s->flags & SF_FINST_MASK))
3614 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003615
Willy Tarreau522d6c02009-12-06 18:49:18 +01003616 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003617 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003618 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003619 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003620 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003621 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003622}
3623
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003624/* send a server's name with an outgoing request over an established connection.
3625 * Note: this function is designed to be called once the request has been scheduled
3626 * for being forwarded. This is the reason why it rewinds the buffer before
3627 * proceeding.
3628 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003629int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003630
3631 struct hdr_ctx ctx;
3632
Mark Lamourinec2247f02012-01-04 13:02:01 -05003633 char *hdr_name = be->server_id_hdr_name;
3634 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003635 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003636 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003637 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003638
William Lallemandd9e90662012-01-30 17:27:17 +01003639 ctx.idx = 0;
3640
Willy Tarreau211cdec2014-04-17 20:18:08 +02003641 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003642 if (old_o) {
3643 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003644 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003645 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003646 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003647 }
3648
Willy Tarreauf37954d2018-06-15 18:31:02 +02003649 old_i = ci_data(chn);
3650 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 -05003651 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003652 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003653 }
3654
3655 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003656 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003657 memcpy(hdr_val, hdr_name, hdr_name_len);
3658 hdr_val += hdr_name_len;
3659 *hdr_val++ = ':';
3660 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003661 hdr_val += strlcpy2(hdr_val, srv_name,
3662 trash.area + trash.size - hdr_val);
3663 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3664 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003665
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003666 if (old_o) {
3667 /* If this was a forwarded request, we must readjust the amount of
3668 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003669 * variations. Note that the current state is >= HTTP_MSG_BODY,
3670 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003671 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003672 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003673 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003674 txn->req.next -= old_o;
3675 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003676 }
3677
Mark Lamourinec2247f02012-01-04 13:02:01 -05003678 return 0;
3679}
3680
Willy Tarreau610ecce2010-01-04 21:15:02 +01003681/* Terminate current transaction and prepare a new one. This is very tricky
3682 * right now but it works.
3683 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003684void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003685{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003686 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003687 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003688 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003689 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003690 struct connection *srv_conn;
3691 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003692 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003693
Willy Tarreau610ecce2010-01-04 21:15:02 +01003694 /* FIXME: We need a more portable way of releasing a backend's and a
3695 * server's connections. We need a safer way to reinitialize buffer
3696 * flags. We also need a more accurate method for computing per-request
3697 * data.
3698 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003699 /*
3700 * XXX cognet: This is probably wrong, this is killing a whole
3701 * connection, in the new world order, we probably want to just kill
3702 * the stream, this is to be revisited the day we handle multiple
3703 * streams in one server connection.
3704 */
3705 cs = objt_cs(s->si[1].end);
3706 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003707
Willy Tarreau4213a112013-12-15 10:25:42 +01003708 /* unless we're doing keep-alive, we want to quickly close the connection
3709 * to the server.
3710 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003711 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003712 !si_conn_ready(&s->si[1])) {
3713 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3714 si_shutr(&s->si[1]);
3715 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003716 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003717
Willy Tarreaue7dff022015-04-03 01:14:29 +02003718 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003719 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003720 if (unlikely(s->srv_conn))
3721 sess_change_server(s, NULL);
3722 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003723
3724 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003725 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003726
Willy Tarreaueee5b512015-04-03 23:46:31 +02003727 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003728 int n;
3729
Willy Tarreaueee5b512015-04-03 23:46:31 +02003730 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003731 if (n < 1 || n > 5)
3732 n = 0;
3733
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003734 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003735 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003736 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003737 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003738 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003739 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3740 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003741 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003742 }
3743
3744 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003745 s->logs.bytes_in -= ci_data(&s->req);
3746 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003747
Willy Tarreau66425e32018-07-25 06:55:12 +02003748 /* we may need to know the position in the queue */
3749 pendconn_free(s);
3750
Willy Tarreau610ecce2010-01-04 21:15:02 +01003751 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003752 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003753 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003754 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003755 s->do_log(s);
3756 }
3757
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003758 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003759 stream_stop_content_counters(s);
3760 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003761
Willy Tarreau610ecce2010-01-04 21:15:02 +01003762 s->logs.accept_date = date; /* user-visible date for logging */
3763 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003764 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3765 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003766 tv_zero(&s->logs.tv_request);
3767 s->logs.t_queue = -1;
3768 s->logs.t_connect = -1;
3769 s->logs.t_data = -1;
3770 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003771 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3772 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003773
Willy Tarreauf37954d2018-06-15 18:31:02 +02003774 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3775 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003776
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003777 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003778 if (s->flags & SF_CURR_SESS) {
3779 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003780 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003781 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003782 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003783 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003784 }
3785
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003786 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003787
Willy Tarreau4213a112013-12-15 10:25:42 +01003788 /* only release our endpoint if we don't intend to reuse the
3789 * connection.
3790 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003791 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003792 !si_conn_ready(&s->si[1])) {
3793 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02003794 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003795 }
3796
Willy Tarreau350f4872014-11-28 14:42:25 +01003797 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3798 s->si[1].err_type = SI_ET_NONE;
3799 s->si[1].conn_retries = 0; /* used for logging too */
3800 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003801 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 +01003802 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 +02003803 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 +02003804 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3805 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3806 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003807
Patrick Hemmere3faf022018-08-22 10:02:00 -04003808 hlua_ctx_destroy(s->hlua);
3809 s->hlua = NULL;
3810
Willy Tarreaueee5b512015-04-03 23:46:31 +02003811 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003812 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003813 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003814
3815 if (prev_status == 401 || prev_status == 407) {
3816 /* In HTTP keep-alive mode, if we receive a 401, we still have
3817 * a chance of being able to send the visitor again to the same
3818 * server over the same connection. This is required by some
3819 * broken protocols such as NTLM, and anyway whenever there is
3820 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003821 * it's better to do it (at least it helps with debugging), at
3822 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003823 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003824 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003825 }
3826
Willy Tarreau53f96852016-02-02 18:50:47 +01003827 /* Never ever allow to reuse a connection from a non-reuse backend */
3828 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3829 srv_conn->flags |= CO_FL_PRIVATE;
3830
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003831 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003832 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003833
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003834 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003835 s->req.flags |= CF_NEVER_WAIT;
3836 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003837 }
3838
Willy Tarreau714ea782015-11-25 20:11:11 +01003839 /* we're removing the analysers, we MUST re-enable events detection.
3840 * We don't enable close on the response channel since it's either
3841 * already closed, or in keep-alive with an idle connection handler.
3842 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003843 channel_auto_read(&s->req);
3844 channel_auto_close(&s->req);
3845 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003846
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003847 /* we're in keep-alive with an idle connection, monitor it if not already done */
3848 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003849 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02003850 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003851 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01003852 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003853 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02003854 else if (prev_flags & TX_NOT_FIRST)
3855 /* note: we check the request, not the connection, but
3856 * this is valid for strategies SAFE and AGGR, and in
3857 * case of ALWS, we don't care anyway.
3858 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003859 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02003860 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003861 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003862 }
Christopher Faulete6006242017-03-10 11:52:44 +01003863 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3864 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003865}
3866
3867
3868/* This function updates the request state machine according to the response
3869 * state machine and buffer flags. It returns 1 if it changes anything (flag
3870 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3871 * it is only used to find when a request/response couple is complete. Both
3872 * this function and its equivalent should loop until both return zero. It
3873 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3874 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003875int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003876{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003877 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003878 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003879 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003880 unsigned int old_state = txn->req.msg_state;
3881
Christopher Faulet4be98032017-07-18 10:48:24 +02003882 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003883 return 0;
3884
3885 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003886 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003887 * We can shut the read side unless we want to abort_on_close,
3888 * or we have a POST request. The issue with POST requests is
3889 * that some browsers still send a CRLF after the request, and
3890 * this CRLF must be read so that it does not remain in the kernel
3891 * buffers, otherwise a close could cause an RST on some systems
3892 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003893 * Note that if we're using keep-alive on the client side, we'd
3894 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003895 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003896 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003897 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003898 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3899 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003900 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3901 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003902 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003903 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003904
Willy Tarreau40f151a2012-12-20 12:10:09 +01003905 /* if the server closes the connection, we want to immediately react
3906 * and close the socket to save packets and syscalls.
3907 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003908 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003909
Willy Tarreau7f876a12015-11-18 11:59:55 +01003910 /* In any case we've finished parsing the request so we must
3911 * disable Nagle when sending data because 1) we're not going
3912 * to shut this side, and 2) the server is waiting for us to
3913 * send pending data.
3914 */
3915 chn->flags |= CF_NEVER_WAIT;
3916
Willy Tarreau610ecce2010-01-04 21:15:02 +01003917 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3918 goto wait_other_side;
3919
3920 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3921 /* The server has not finished to respond, so we
3922 * don't want to move in order not to upset it.
3923 */
3924 goto wait_other_side;
3925 }
3926
Willy Tarreau610ecce2010-01-04 21:15:02 +01003927 /* When we get here, it means that both the request and the
3928 * response have finished receiving. Depending on the connection
3929 * mode, we'll have to wait for the last bytes to leave in either
3930 * direction, and sometimes for a close to be effective.
3931 */
3932
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003933 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3934 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003935 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3936 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003937 }
3938 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3939 /* Option forceclose is set, or either side wants to close,
3940 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003941 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003942 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003943 *
3944 * However, there is an exception if the response
3945 * length is undefined. In this case, we need to wait
3946 * the close from the server. The response will be
3947 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003948 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003949 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3950 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3951 goto check_channel_flags;
3952
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003953 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3954 channel_shutr_now(chn);
3955 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003956 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003957 }
3958 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003959 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3960 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003961 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003962 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3963 channel_auto_read(chn);
3964 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003965 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003966 }
3967
Christopher Faulet4be98032017-07-18 10:48:24 +02003968 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003969 }
3970
3971 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3972 http_msg_closing:
3973 /* nothing else to forward, just waiting for the output buffer
3974 * to be empty and for the shutw_now to take effect.
3975 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003976 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003977 txn->req.msg_state = HTTP_MSG_CLOSED;
3978 goto http_msg_closed;
3979 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003980 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003981 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003982 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003983 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003984 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003985 }
3986
3987 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3988 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01003989 /* if we don't know whether the server will close, we need to hard close */
3990 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
3991 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3992
Willy Tarreau3988d932013-12-27 23:03:08 +01003993 /* see above in MSG_DONE why we only do this in these states */
3994 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3995 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003996 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3997 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01003998 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003999 goto wait_other_side;
4000 }
4001
Christopher Faulet4be98032017-07-18 10:48:24 +02004002 check_channel_flags:
4003 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4004 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4005 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004006 txn->req.msg_state = HTTP_MSG_CLOSING;
4007 goto http_msg_closing;
4008 }
4009
4010
Willy Tarreau610ecce2010-01-04 21:15:02 +01004011 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004012 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004013}
4014
4015
4016/* This function updates the response state machine according to the request
4017 * state machine and buffer flags. It returns 1 if it changes anything (flag
4018 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4019 * it is only used to find when a request/response couple is complete. Both
4020 * this function and its equivalent should loop until both return zero. It
4021 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4022 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004023int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004024{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004025 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004026 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004027 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004028 unsigned int old_state = txn->rsp.msg_state;
4029
Christopher Faulet4be98032017-07-18 10:48:24 +02004030 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004031 return 0;
4032
4033 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4034 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004035 * still monitor the server connection for a possible close
4036 * while the request is being uploaded, so we don't disable
4037 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004038 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004039 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004040
4041 if (txn->req.msg_state == HTTP_MSG_ERROR)
4042 goto wait_other_side;
4043
4044 if (txn->req.msg_state < HTTP_MSG_DONE) {
4045 /* The client seems to still be sending data, probably
4046 * because we got an error response during an upload.
4047 * We have the choice of either breaking the connection
4048 * or letting it pass through. Let's do the later.
4049 */
4050 goto wait_other_side;
4051 }
4052
Willy Tarreau610ecce2010-01-04 21:15:02 +01004053 /* When we get here, it means that both the request and the
4054 * response have finished receiving. Depending on the connection
4055 * mode, we'll have to wait for the last bytes to leave in either
4056 * direction, and sometimes for a close to be effective.
4057 */
4058
4059 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4060 /* Server-close mode : shut read and wait for the request
4061 * side to close its output buffer. The caller will detect
4062 * when we're in DONE and the other is in CLOSED and will
4063 * catch that for the final cleanup.
4064 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004065 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4066 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004067 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004068 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4069 /* Option forceclose is set, or either side wants to close,
4070 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004071 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004072 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004073 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004074 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004075 channel_shutr_now(chn);
4076 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004077 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004078 }
4079 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004080 /* The last possible modes are keep-alive and tunnel. Tunnel will
4081 * need to forward remaining data. Keep-alive will need to monitor
4082 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004083 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004084 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004085 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004086 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4087 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004088 }
4089
Christopher Faulet4be98032017-07-18 10:48:24 +02004090 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004091 }
4092
4093 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4094 http_msg_closing:
4095 /* nothing else to forward, just waiting for the output buffer
4096 * to be empty and for the shutw_now to take effect.
4097 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004098 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004099 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4100 goto http_msg_closed;
4101 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004102 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004103 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004104 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004105 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004106 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004107 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004108 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004109 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004110 }
4111
4112 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4113 http_msg_closed:
4114 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004115 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004116 channel_auto_close(chn);
4117 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004118 goto wait_other_side;
4119 }
4120
Christopher Faulet4be98032017-07-18 10:48:24 +02004121 check_channel_flags:
4122 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4123 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4124 /* if we've just closed an output, let's switch */
4125 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4126 goto http_msg_closing;
4127 }
4128
Willy Tarreau610ecce2010-01-04 21:15:02 +01004129 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004130 /* We force the response to leave immediately if we're waiting for the
4131 * other side, since there is no pending shutdown to push it out.
4132 */
4133 if (!channel_is_empty(chn))
4134 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004135 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004136}
4137
4138
Christopher Faulet894da4c2017-07-18 11:29:07 +02004139/* Resync the request and response state machines. */
4140void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004141{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004142 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004143#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004144 int old_req_state = txn->req.msg_state;
4145 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004146#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004147
Willy Tarreau610ecce2010-01-04 21:15:02 +01004148 http_sync_req_state(s);
4149 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004150 if (!http_sync_res_state(s))
4151 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004152 if (!http_sync_req_state(s))
4153 break;
4154 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004155
Christopher Faulet894da4c2017-07-18 11:29:07 +02004156 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4157 "req->analysers=0x%08x res->analysers=0x%08x\n",
4158 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004159 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4160 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004161 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004162
4163
Willy Tarreau610ecce2010-01-04 21:15:02 +01004164 /* OK, both state machines agree on a compatible state.
4165 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004166 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4167 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004168 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4169 * means we must abort the request.
4170 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4171 * corresponding channel.
4172 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4173 * on the response with server-close mode means we've completed one
4174 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004175 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004176 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4177 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004178 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004179 channel_auto_close(&s->req);
4180 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004181 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004182 channel_auto_close(&s->res);
4183 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004184 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004185 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4186 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004187 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004188 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004189 channel_auto_close(&s->res);
4190 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004191 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004192 channel_abort(&s->req);
4193 channel_auto_close(&s->req);
4194 channel_auto_read(&s->req);
4195 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004196 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004197 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4198 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4199 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4200 s->req.analysers &= AN_REQ_FLT_END;
4201 if (HAS_REQ_DATA_FILTERS(s))
4202 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4203 }
4204 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4205 s->res.analysers &= AN_RES_FLT_END;
4206 if (HAS_RSP_DATA_FILTERS(s))
4207 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4208 }
4209 channel_auto_close(&s->req);
4210 channel_auto_read(&s->req);
4211 channel_auto_close(&s->res);
4212 channel_auto_read(&s->res);
4213 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004214 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4215 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004216 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004217 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4218 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4219 /* server-close/keep-alive: terminate this transaction,
4220 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004221 * a fresh-new transaction, but only once we're sure there's
4222 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004223 * another request. They must not hold any pending output data
4224 * and the response buffer must realigned
4225 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004226 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004227 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004228 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004229 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004230 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004231 else {
4232 s->req.analysers = AN_REQ_FLT_END;
4233 s->res.analysers = AN_RES_FLT_END;
4234 txn->flags |= TX_WAIT_CLEANUP;
4235 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004236 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004237}
4238
Willy Tarreaud98cf932009-12-27 22:54:55 +01004239/* This function is an analyser which forwards request body (including chunk
4240 * sizes if any). It is called as soon as we must forward, even if we forward
4241 * zero byte. The only situation where it must not be called is when we're in
4242 * tunnel mode and we want to forward till the close. It's used both to forward
4243 * remaining data and to resync after end of body. It expects the msg_state to
4244 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004245 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004246 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004247 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004248 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004249int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004250{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004251 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004252 struct http_txn *txn = s->txn;
4253 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004254 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004255
Christopher Faulet45073512018-07-20 10:16:29 +02004256 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 +02004257 now_ms, __FUNCTION__,
4258 s,
4259 req,
4260 req->rex, req->wex,
4261 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004262 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004263 req->analysers);
4264
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004265 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4266 return 0;
4267
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004268 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004269 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004270 /* Output closed while we were sending data. We must abort and
4271 * wake the other side up.
4272 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004273 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004274 msg->msg_state = HTTP_MSG_ERROR;
4275 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004276 return 1;
4277 }
4278
Willy Tarreaud98cf932009-12-27 22:54:55 +01004279 /* Note that we don't have to send 100-continue back because we don't
4280 * need the data to complete our job, and it's up to the server to
4281 * decide whether to return 100, 417 or anything else in return of
4282 * an "Expect: 100-continue" header.
4283 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004284 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004285 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4286 ? HTTP_MSG_CHUNK_SIZE
4287 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004288
4289 /* TODO/filters: when http-buffer-request option is set or if a
4290 * rule on url_param exists, the first chunk size could be
4291 * already parsed. In that case, msg->next is after the chunk
4292 * size (including the CRLF after the size). So this case should
4293 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004294 }
4295
Willy Tarreau7ba23542014-04-17 21:50:00 +02004296 /* Some post-connect processing might want us to refrain from starting to
4297 * forward data. Currently, the only reason for this is "balance url_param"
4298 * whichs need to parse/process the request after we've enabled forwarding.
4299 */
4300 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004301 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004302 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004303 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004304 channel_dont_close(req); /* don't fail on early shutr */
4305 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004306 }
4307 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4308 }
4309
Willy Tarreau80a92c02014-03-12 10:41:13 +01004310 /* in most states, we should abort in case of early close */
4311 channel_auto_close(req);
4312
Willy Tarreauefdf0942014-04-24 20:08:57 +02004313 if (req->to_forward) {
4314 /* We can't process the buffer's contents yet */
4315 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004316 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004317 }
4318
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004319 if (msg->msg_state < HTTP_MSG_DONE) {
4320 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4321 ? http_msg_forward_chunked_body(s, msg)
4322 : http_msg_forward_body(s, msg));
4323 if (!ret)
4324 goto missing_data_or_waiting;
4325 if (ret < 0)
4326 goto return_bad_req;
4327 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004328
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004329 /* other states, DONE...TUNNEL */
4330 /* we don't want to forward closes on DONE except in tunnel mode. */
4331 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4332 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004333
Christopher Faulet894da4c2017-07-18 11:29:07 +02004334 http_resync_states(s);
4335 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004336 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4337 if (req->flags & CF_SHUTW) {
4338 /* request errors are most likely due to the
4339 * server aborting the transfer. */
4340 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004341 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004342 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004343 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004344 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004345 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004346 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004347 }
4348
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004349 /* If "option abortonclose" is set on the backend, we want to monitor
4350 * the client's connection and forward any shutdown notification to the
4351 * server, which will decide whether to close or to go on processing the
4352 * request. We only do that in tunnel mode, and not in other modes since
4353 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004354 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004355 channel_auto_read(req);
4356 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4357 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4358 s->si[1].flags |= SI_FL_NOLINGER;
4359 channel_auto_close(req);
4360 }
4361 else if (s->txn->meth == HTTP_METH_POST) {
4362 /* POST requests may require to read extra CRLF sent by broken
4363 * browsers and which could cause an RST to be sent upon close
4364 * on some systems (eg: Linux). */
4365 channel_auto_read(req);
4366 }
4367 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004368
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004369 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004370 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004371 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004372 if (!(s->flags & SF_ERR_MASK))
4373 s->flags |= SF_ERR_CLICL;
4374 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004375 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004376 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004377 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004378 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004379 }
4380
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004381 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4382 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004383 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004384 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004385
4386 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004387 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004388
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004389 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004390 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004391 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004392 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004393
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004394 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004395 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004396 * And when content-length is used, we never want to let the possible
4397 * shutdown be forwarded to the other side, as the state machine will
4398 * take care of it once the client responds. It's also important to
4399 * prevent TIME_WAITs from accumulating on the backend side, and for
4400 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004401 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004402 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004403 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004404
Willy Tarreau5c620922011-05-11 19:56:11 +02004405 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004406 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004407 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004408 * modes are already handled by the stream sock layer. We must not do
4409 * this in content-length mode because it could present the MSG_MORE
4410 * flag with the last block of forwarded data, which would cause an
4411 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004412 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004413 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004414 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004415
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004416 return 0;
4417
Willy Tarreaud98cf932009-12-27 22:54:55 +01004418 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004419 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004420 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004421 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004422
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004423 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004424 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004425 txn->req.msg_state = HTTP_MSG_ERROR;
4426 if (txn->status) {
4427 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004428 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004429 } else {
4430 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004431 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004432 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004433 req->analysers &= AN_REQ_FLT_END;
4434 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 +01004435
Willy Tarreaue7dff022015-04-03 01:14:29 +02004436 if (!(s->flags & SF_ERR_MASK))
4437 s->flags |= SF_ERR_PRXCOND;
4438 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004439 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004440 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004441 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004442 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004443 }
4444 return 0;
4445
4446 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004447 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004448 txn->req.msg_state = HTTP_MSG_ERROR;
4449 if (txn->status) {
4450 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004451 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004452 } else {
4453 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004454 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004455 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004456 req->analysers &= AN_REQ_FLT_END;
4457 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 +01004458
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004459 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4460 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004461 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004462 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004463
Willy Tarreaue7dff022015-04-03 01:14:29 +02004464 if (!(s->flags & SF_ERR_MASK))
4465 s->flags |= SF_ERR_SRVCL;
4466 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004467 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004468 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004469 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004470 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004471 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004472 return 0;
4473}
4474
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004475/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4476 * processing can continue on next analysers, or zero if it either needs more
4477 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004478 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004479 * when it has nothing left to do, and may remove any analyser when it wants to
4480 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004481 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004482int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004483{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004484 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004485 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004486 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004487 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004488 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004489 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004490 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004491 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004492
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004493 srv_conn = cs_conn(objt_cs(s->si[1].end));
4494
Christopher Faulet45073512018-07-20 10:16:29 +02004495 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 +02004496 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004497 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004498 rep,
4499 rep->rex, rep->wex,
4500 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004501 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004502 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004503
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004504 /*
4505 * Now parse the partial (or complete) lines.
4506 * We will check the response syntax, and also join multi-line
4507 * headers. An index of all the lines will be elaborated while
4508 * parsing.
4509 *
4510 * For the parsing, we use a 28 states FSM.
4511 *
4512 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004513 * ci_head(rep) = beginning of response
4514 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4515 * ci_tail(rep) = end of input data
4516 * msg->eol = end of current header or line (LF or CRLF)
4517 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004518 */
4519
Willy Tarreau628c40c2014-04-24 19:11:26 +02004520 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004521 /* There's a protected area at the end of the buffer for rewriting
4522 * purposes. We don't want to start to parse the request if the
4523 * protected area is affected, because we may have to move processed
4524 * data later, which is much more complicated.
4525 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004526 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004527 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004528 /* some data has still not left the buffer, wake us once that's done */
4529 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4530 goto abort_response;
4531 channel_dont_close(rep);
4532 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004533 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004534 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004535 }
4536
Willy Tarreau188e2302018-06-15 11:11:53 +02004537 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004538 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004539 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004540
Willy Tarreauf37954d2018-06-15 18:31:02 +02004541 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004542 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004543 }
4544
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004545 /* 1: we might have to print this header in debug mode */
4546 if (unlikely((global.mode & MODE_DEBUG) &&
4547 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004548 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004549 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004550
Willy Tarreauf37954d2018-06-15 18:31:02 +02004551 sol = ci_head(rep);
4552 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004553 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004554
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004555 sol += hdr_idx_first_pos(&txn->hdr_idx);
4556 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004557
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004558 while (cur_idx) {
4559 eol = sol + txn->hdr_idx.v[cur_idx].len;
4560 debug_hdr("srvhdr", s, sol, eol);
4561 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4562 cur_idx = txn->hdr_idx.v[cur_idx].next;
4563 }
4564 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004565
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004566 /*
4567 * Now we quickly check if we have found a full valid response.
4568 * If not so, we check the FD and buffer states before leaving.
4569 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004570 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004571 * responses are checked first.
4572 *
4573 * Depending on whether the client is still there or not, we
4574 * may send an error response back or not. Note that normally
4575 * we should only check for HTTP status there, and check I/O
4576 * errors somewhere else.
4577 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004578
Willy Tarreau655dce92009-11-08 13:10:58 +01004579 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004580 /* Invalid response */
4581 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4582 /* we detected a parsing error. We want to archive this response
4583 * in the dedicated proxy area for later troubleshooting.
4584 */
4585 hdr_response_bad:
4586 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004587 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004588
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004589 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004590 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004591 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004592 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004593 }
Willy Tarreau64648412010-03-05 10:41:54 +01004594 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004595 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004596 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004597 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004598 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004599 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004600 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004601
Willy Tarreaue7dff022015-04-03 01:14:29 +02004602 if (!(s->flags & SF_ERR_MASK))
4603 s->flags |= SF_ERR_PRXCOND;
4604 if (!(s->flags & SF_FINST_MASK))
4605 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004606
4607 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004608 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004609
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004610 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004611 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004612 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004613 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004614 goto hdr_response_bad;
4615 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004616
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004617 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004618 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004619 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004620 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004621 else if (txn->flags & TX_NOT_FIRST)
4622 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004623
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004624 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004625 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004626 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004627 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004628 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004629
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004630 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004631 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004632 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004633
4634 /* Check to see if the server refused the early data.
4635 * If so, just send a 425
4636 */
4637 if (objt_cs(s->si[1].end)) {
4638 struct connection *conn = objt_cs(s->si[1].end)->conn;
4639
4640 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4641 txn->status = 425;
4642 }
4643
Willy Tarreau350f4872014-11-28 14:42:25 +01004644 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004645 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004646 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004647
Willy Tarreaue7dff022015-04-03 01:14:29 +02004648 if (!(s->flags & SF_ERR_MASK))
4649 s->flags |= SF_ERR_SRVCL;
4650 if (!(s->flags & SF_FINST_MASK))
4651 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004652 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004653 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004654
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004655 /* read timeout : return a 504 to the client. */
4656 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004657 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004658 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004659
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004660 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004661 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004662 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004663 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004664 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004665
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004666 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004667 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004668 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004669 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004670 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004671 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004672
Willy Tarreaue7dff022015-04-03 01:14:29 +02004673 if (!(s->flags & SF_ERR_MASK))
4674 s->flags |= SF_ERR_SRVTO;
4675 if (!(s->flags & SF_FINST_MASK))
4676 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004677 return 0;
4678 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004679
Willy Tarreauf003d372012-11-26 13:35:37 +01004680 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004681 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004682 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4683 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004684 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004685 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004686
Christopher Faulet0184ea72017-01-05 14:06:34 +01004687 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004688 channel_auto_close(rep);
4689
4690 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004691 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004692 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004693
Willy Tarreaue7dff022015-04-03 01:14:29 +02004694 if (!(s->flags & SF_ERR_MASK))
4695 s->flags |= SF_ERR_CLICL;
4696 if (!(s->flags & SF_FINST_MASK))
4697 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004698
Willy Tarreau87b09662015-04-03 00:22:06 +02004699 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004700 return 0;
4701 }
4702
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004703 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004704 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004705 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004706 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004707 else if (txn->flags & TX_NOT_FIRST)
4708 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004709
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004710 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004711 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004712 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004713 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004714 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004715
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004716 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004717 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004718 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004719 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004720 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004721 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004722
Willy Tarreaue7dff022015-04-03 01:14:29 +02004723 if (!(s->flags & SF_ERR_MASK))
4724 s->flags |= SF_ERR_SRVCL;
4725 if (!(s->flags & SF_FINST_MASK))
4726 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004727 return 0;
4728 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004729
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004730 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004731 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004732 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004733 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004734 else if (txn->flags & TX_NOT_FIRST)
4735 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004736
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004737 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004738 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004739 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004740
Willy Tarreaue7dff022015-04-03 01:14:29 +02004741 if (!(s->flags & SF_ERR_MASK))
4742 s->flags |= SF_ERR_CLICL;
4743 if (!(s->flags & SF_FINST_MASK))
4744 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004745
Willy Tarreau87b09662015-04-03 00:22:06 +02004746 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004747 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004748 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004749
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004750 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004751 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004752 return 0;
4753 }
4754
4755 /* More interesting part now : we know that we have a complete
4756 * response which at least looks like HTTP. We have an indicator
4757 * of each header's length, so we can parse them quickly.
4758 */
4759
4760 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004761 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004762
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004763 /*
4764 * 1: get the status code
4765 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004766 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004767 if (n < 1 || n > 5)
4768 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004769 /* when the client triggers a 4xx from the server, it's most often due
4770 * to a missing object or permission. These events should be tracked
4771 * because if they happen often, it may indicate a brute force or a
4772 * vulnerability scan.
4773 */
4774 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004775 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004776
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004777 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004778 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004779
Willy Tarreau91852eb2015-05-01 13:26:00 +02004780 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4781 * exactly one digit "." one digit. This check may be disabled using
4782 * option accept-invalid-http-response.
4783 */
4784 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4785 if (msg->sl.st.v_l != 8) {
4786 msg->err_pos = 0;
4787 goto hdr_response_bad;
4788 }
4789
Willy Tarreauf37954d2018-06-15 18:31:02 +02004790 if (ci_head(rep)[4] != '/' ||
4791 !isdigit((unsigned char)ci_head(rep)[5]) ||
4792 ci_head(rep)[6] != '.' ||
4793 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004794 msg->err_pos = 4;
4795 goto hdr_response_bad;
4796 }
4797 }
4798
Willy Tarreau5b154472009-12-21 20:11:07 +01004799 /* check if the response is HTTP/1.1 or above */
4800 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004801 ((ci_head(rep)[5] > '1') ||
4802 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004803 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004804
4805 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004806 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 +01004807
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004808 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004809 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004810
Willy Tarreauf37954d2018-06-15 18:31:02 +02004811 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004812
Willy Tarreau39650402010-03-15 19:44:39 +01004813 /* Adjust server's health based on status code. Note: status codes 501
4814 * and 505 are triggered on demand by client request, so we must not
4815 * count them as server failures.
4816 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004817 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004818 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004819 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004820 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004821 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004822 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004823
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004824 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004825 * We may be facing a 100-continue response, or any other informational
4826 * 1xx response which is non-final, in which case this is not the right
4827 * response, and we're waiting for the next one. Let's allow this response
4828 * to go to the client and wait for the next one. There's an exception for
4829 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004830 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004831 if (txn->status < 200 &&
4832 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004833 hdr_idx_init(&txn->hdr_idx);
4834 msg->next -= channel_forward(rep, msg->next);
4835 msg->msg_state = HTTP_MSG_RPBEFORE;
4836 txn->status = 0;
4837 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004838 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004839 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004840 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004841
Willy Tarreaua14ad722017-07-07 11:36:32 +02004842 /*
4843 * 2: check for cacheability.
4844 */
4845
4846 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004847 case 200:
4848 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004849 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004850 case 206:
4851 case 300:
4852 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004853 case 404:
4854 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004855 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004856 case 414:
4857 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004858 break;
4859 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004860 /* RFC7231#6.1:
4861 * Responses with status codes that are defined as
4862 * cacheable by default (e.g., 200, 203, 204, 206,
4863 * 300, 301, 404, 405, 410, 414, and 501 in this
4864 * specification) can be reused by a cache with
4865 * heuristic expiration unless otherwise indicated
4866 * by the method definition or explicit cache
4867 * controls [RFC7234]; all other status codes are
4868 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004869 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004870 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004871 break;
4872 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004873
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004874 /*
4875 * 3: we may need to capture headers
4876 */
4877 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004878 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004879 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4880 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004881
Willy Tarreau557f1992015-05-01 10:05:17 +02004882 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4883 * by RFC7230#3.3.3 :
4884 *
4885 * The length of a message body is determined by one of the following
4886 * (in order of precedence):
4887 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004888 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4889 * the connection will become a tunnel immediately after the empty
4890 * line that concludes the header fields. A client MUST ignore
4891 * any Content-Length or Transfer-Encoding header fields received
4892 * in such a message. Any 101 response (Switching Protocols) is
4893 * managed in the same manner.
4894 *
4895 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004896 * (Informational), 204 (No Content), or 304 (Not Modified) status
4897 * code is always terminated by the first empty line after the
4898 * header fields, regardless of the header fields present in the
4899 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004900 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004901 * 3. If a Transfer-Encoding header field is present and the chunked
4902 * transfer coding (Section 4.1) is the final encoding, the message
4903 * body length is determined by reading and decoding the chunked
4904 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004905 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004906 * If a Transfer-Encoding header field is present in a response and
4907 * the chunked transfer coding is not the final encoding, the
4908 * message body length is determined by reading the connection until
4909 * it is closed by the server. If a Transfer-Encoding header field
4910 * is present in a request and the chunked transfer coding is not
4911 * the final encoding, the message body length cannot be determined
4912 * reliably; the server MUST respond with the 400 (Bad Request)
4913 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004914 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004915 * If a message is received with both a Transfer-Encoding and a
4916 * Content-Length header field, the Transfer-Encoding overrides the
4917 * Content-Length. Such a message might indicate an attempt to
4918 * perform request smuggling (Section 9.5) or response splitting
4919 * (Section 9.4) and ought to be handled as an error. A sender MUST
4920 * remove the received Content-Length field prior to forwarding such
4921 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004922 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004923 * 4. If a message is received without Transfer-Encoding and with
4924 * either multiple Content-Length header fields having differing
4925 * field-values or a single Content-Length header field having an
4926 * invalid value, then the message framing is invalid and the
4927 * recipient MUST treat it as an unrecoverable error. If this is a
4928 * request message, the server MUST respond with a 400 (Bad Request)
4929 * status code and then close the connection. If this is a response
4930 * message received by a proxy, the proxy MUST close the connection
4931 * to the server, discard the received response, and send a 502 (Bad
4932 * Gateway) response to the client. If this is a response message
4933 * received by a user agent, the user agent MUST close the
4934 * connection to the server and discard the received response.
4935 *
4936 * 5. If a valid Content-Length header field is present without
4937 * Transfer-Encoding, its decimal value defines the expected message
4938 * body length in octets. If the sender closes the connection or
4939 * the recipient times out before the indicated number of octets are
4940 * received, the recipient MUST consider the message to be
4941 * incomplete and close the connection.
4942 *
4943 * 6. If this is a request message and none of the above are true, then
4944 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004945 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004946 * 7. Otherwise, this is a response message without a declared message
4947 * body length, so the message body length is determined by the
4948 * number of octets received prior to the server closing the
4949 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004950 */
4951
4952 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004953 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004954 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004955 * FIXME: should we parse anyway and return an error on chunked encoding ?
4956 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004957 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4958 txn->status == 101)) {
4959 /* Either we've established an explicit tunnel, or we're
4960 * switching the protocol. In both cases, we're very unlikely
4961 * to understand the next protocols. We have to switch to tunnel
4962 * mode, so that we transfer the request and responses then let
4963 * this protocol pass unmodified. When we later implement specific
4964 * parsers for such protocols, we'll want to check the Upgrade
4965 * header which contains information about that protocol for
4966 * responses with status 101 (eg: see RFC2817 about TLS).
4967 */
4968 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4969 msg->flags |= HTTP_MSGF_XFER_LEN;
4970 goto end;
4971 }
4972
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004973 if (txn->meth == HTTP_METH_HEAD ||
4974 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004975 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004976 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004977 goto skip_content_length;
4978 }
4979
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004980 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004981 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004982 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004983 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004984 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4985 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004986 /* bad transfer-encoding (chunked followed by something else) */
4987 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004988 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004989 break;
4990 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004991 }
4992
Willy Tarreau1c913912015-04-30 10:57:51 +02004993 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004994 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02004995 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004996 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02004997 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4998 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02004999 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005000 signed long long cl;
5001
Willy Tarreauad14f752011-09-02 20:33:27 +02005002 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005003 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005004 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005005 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005006
Willy Tarreauad14f752011-09-02 20:33:27 +02005007 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005008 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005009 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005010 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005011
Willy Tarreauad14f752011-09-02 20:33:27 +02005012 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005013 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005014 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005015 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005016
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005017 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005018 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005019 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005020 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005021
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005022 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005023 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005024 }
5025
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02005026 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
5027 * 407 (Proxy-Authenticate) responses and set the connection to private
5028 */
5029 if (srv_conn && txn->status == 401) {
5030 /* check for Negotiate/NTLM WWW-Authenticate headers */
5031 ctx.idx = 0;
5032 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
5033 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5034 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5035 srv_conn->flags |= CO_FL_PRIVATE;
5036 }
5037 } else if (srv_conn && txn->status == 407) {
5038 /* check for Negotiate/NTLM Proxy-Authenticate headers */
5039 ctx.idx = 0;
5040 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
5041 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5042 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5043 srv_conn->flags |= CO_FL_PRIVATE;
5044 }
5045 }
5046
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005047 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005048 /* Now we have to check if we need to modify the Connection header.
5049 * This is more difficult on the response than it is on the request,
5050 * because we can have two different HTTP versions and we don't know
5051 * how the client will interprete a response. For instance, let's say
5052 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5053 * HTTP/1.1 response without any header. Maybe it will bound itself to
5054 * HTTP/1.0 because it only knows about it, and will consider the lack
5055 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5056 * the lack of header as a keep-alive. Thus we will use two flags
5057 * indicating how a request MAY be understood by the client. In case
5058 * of multiple possibilities, we'll fix the header to be explicit. If
5059 * ambiguous cases such as both close and keepalive are seen, then we
5060 * will fall back to explicit close. Note that we won't take risks with
5061 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005062 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005063 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005064 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005065 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01005066 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005067
Willy Tarreau60466522010-01-18 19:08:45 +01005068 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005069 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01005070 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005071
Willy Tarreau60466522010-01-18 19:08:45 +01005072 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005073 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01005074 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005075 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005076 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005077 }
Willy Tarreau60466522010-01-18 19:08:45 +01005078 else { /* SCL / KAL */
5079 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005080 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005081 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005082 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005083
Willy Tarreau60466522010-01-18 19:08:45 +01005084 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005085 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005086
Willy Tarreau60466522010-01-18 19:08:45 +01005087 /* Some keep-alive responses are converted to Server-close if
5088 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005089 */
Willy Tarreau60466522010-01-18 19:08:45 +01005090 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5091 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005092 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005093 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005094 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005095 }
5096
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005097 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005098 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005099 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005100
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005101 /* end of job, return OK */
5102 rep->analysers &= ~an_bit;
5103 rep->analyse_exp = TICK_ETERNITY;
5104 channel_auto_close(rep);
5105 return 1;
5106
5107 abort_keep_alive:
5108 /* A keep-alive request to the server failed on a network error.
5109 * The client is required to retry. We need to close without returning
5110 * any other information so that the client retries.
5111 */
5112 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005113 rep->analysers &= AN_RES_FLT_END;
5114 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005115 channel_auto_close(rep);
5116 s->logs.logwait = 0;
5117 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005118 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005119 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005120 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005121 return 0;
5122}
5123
5124/* This function performs all the processing enabled for the current response.
5125 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005126 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005127 * other functions. It works like process_request (see indications above).
5128 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005129int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005130{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005131 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005132 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005133 struct http_msg *msg = &txn->rsp;
5134 struct proxy *cur_proxy;
5135 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005136 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005137
Christopher Faulet45073512018-07-20 10:16:29 +02005138 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 +02005139 now_ms, __FUNCTION__,
5140 s,
5141 rep,
5142 rep->rex, rep->wex,
5143 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005144 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005145 rep->analysers);
5146
5147 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5148 return 0;
5149
Willy Tarreau70730dd2014-04-24 18:06:27 +02005150 /* The stats applet needs to adjust the Connection header but we don't
5151 * apply any filter there.
5152 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005153 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5154 rep->analysers &= ~an_bit;
5155 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005156 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005157 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005158
Willy Tarreau58975672014-04-24 21:13:57 +02005159 /*
5160 * We will have to evaluate the filters.
5161 * As opposed to version 1.2, now they will be evaluated in the
5162 * filters order and not in the header order. This means that
5163 * each filter has to be validated among all headers.
5164 *
5165 * Filters are tried with ->be first, then with ->fe if it is
5166 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005167 *
5168 * Maybe we are in resume condiion. In this case I choose the
5169 * "struct proxy" which contains the rule list matching the resume
5170 * pointer. If none of theses "struct proxy" match, I initialise
5171 * the process with the first one.
5172 *
5173 * In fact, I check only correspondance betwwen the current list
5174 * pointer and the ->fe rule list. If it doesn't match, I initialize
5175 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005176 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005177 if (s->current_rule_list == &sess->fe->http_res_rules)
5178 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005179 else
5180 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005181 while (1) {
5182 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005183
Willy Tarreau58975672014-04-24 21:13:57 +02005184 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005185 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005186 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005187
Willy Tarreau51d861a2015-05-22 17:30:48 +02005188 if (ret == HTTP_RULE_RES_BADREQ)
5189 goto return_srv_prx_502;
5190
5191 if (ret == HTTP_RULE_RES_DONE) {
5192 rep->analysers &= ~an_bit;
5193 rep->analyse_exp = TICK_ETERNITY;
5194 return 1;
5195 }
5196 }
5197
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005198 /* we need to be called again. */
5199 if (ret == HTTP_RULE_RES_YIELD) {
5200 channel_dont_close(rep);
5201 return 0;
5202 }
5203
Willy Tarreau58975672014-04-24 21:13:57 +02005204 /* try headers filters */
5205 if (rule_set->rsp_exp != NULL) {
5206 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5207 return_bad_resp:
5208 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005209 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005210 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005211 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005212 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005213 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005214 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005215 txn->status = 502;
5216 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005217 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005218 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005219 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005220 if (!(s->flags & SF_ERR_MASK))
5221 s->flags |= SF_ERR_PRXCOND;
5222 if (!(s->flags & SF_FINST_MASK))
5223 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005224 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005225 }
Willy Tarreau58975672014-04-24 21:13:57 +02005226 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005227
Willy Tarreau58975672014-04-24 21:13:57 +02005228 /* has the response been denied ? */
5229 if (txn->flags & TX_SVDENY) {
5230 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005231 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005232
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005233 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5234 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005235 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005236 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005237
Willy Tarreau58975672014-04-24 21:13:57 +02005238 goto return_srv_prx_502;
5239 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005240
Willy Tarreau58975672014-04-24 21:13:57 +02005241 /* add response headers from the rule sets in the same order */
5242 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005243 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005244 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005245 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005246 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005247 ret = acl_pass(ret);
5248 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5249 ret = !ret;
5250 if (!ret)
5251 continue;
5252 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005253 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005254 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005255 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005256
Willy Tarreau58975672014-04-24 21:13:57 +02005257 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005258 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005259 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005260 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005261 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005262
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005263 /* After this point, this anayzer can't return yield, so we can
5264 * remove the bit corresponding to this analyzer from the list.
5265 *
5266 * Note that the intermediate returns and goto found previously
5267 * reset the analyzers.
5268 */
5269 rep->analysers &= ~an_bit;
5270 rep->analyse_exp = TICK_ETERNITY;
5271
Willy Tarreau58975672014-04-24 21:13:57 +02005272 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005273 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005274 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005275
Willy Tarreau58975672014-04-24 21:13:57 +02005276 /*
5277 * Now check for a server cookie.
5278 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005279 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005280 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005281
Willy Tarreau58975672014-04-24 21:13:57 +02005282 /*
5283 * Check for cache-control or pragma headers if required.
5284 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005285 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005286 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005287
Willy Tarreau58975672014-04-24 21:13:57 +02005288 /*
5289 * Add server cookie in the response if needed
5290 */
5291 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5292 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005293 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005294 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5295 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5296 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5297 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5298 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005299 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005300 /* the server is known, it's not the one the client requested, or the
5301 * cookie's last seen date needs to be refreshed. We have to
5302 * insert a set-cookie here, except if we want to insert only on POST
5303 * requests and this one isn't. Note that servers which don't have cookies
5304 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005305 */
Willy Tarreau58975672014-04-24 21:13:57 +02005306 if (!objt_server(s->target)->cookie) {
5307 chunk_printf(&trash,
5308 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5309 s->be->cookie_name);
5310 }
5311 else {
5312 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005313
Willy Tarreau58975672014-04-24 21:13:57 +02005314 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5315 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005316 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5317 s30tob64((date.tv_sec+3) >> 2,
5318 trash.area + trash.data);
5319 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005320
Willy Tarreau58975672014-04-24 21:13:57 +02005321 if (s->be->cookie_maxlife) {
5322 /* emit first_date, which is either the original one or
5323 * the current date.
5324 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005325 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005326 s30tob64(txn->cookie_first_date ?
5327 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005328 (date.tv_sec+3) >> 2,
5329 trash.area + trash.data);
5330 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005331 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005332 }
Willy Tarreau58975672014-04-24 21:13:57 +02005333 chunk_appendf(&trash, "; path=/");
5334 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005335
Willy Tarreau58975672014-04-24 21:13:57 +02005336 if (s->be->cookie_domain)
5337 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005338
Willy Tarreau58975672014-04-24 21:13:57 +02005339 if (s->be->ck_opts & PR_CK_HTTPONLY)
5340 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005341
Willy Tarreau58975672014-04-24 21:13:57 +02005342 if (s->be->ck_opts & PR_CK_SECURE)
5343 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005344
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005345 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005346 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005347
Willy Tarreau58975672014-04-24 21:13:57 +02005348 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005349 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005350 /* the server did not change, only the date was updated */
5351 txn->flags |= TX_SCK_UPDATED;
5352 else
5353 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005354
Willy Tarreau58975672014-04-24 21:13:57 +02005355 /* Here, we will tell an eventual cache on the client side that we don't
5356 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5357 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5358 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005359 */
Willy Tarreau58975672014-04-24 21:13:57 +02005360 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005361
Willy Tarreau58975672014-04-24 21:13:57 +02005362 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005363
Willy Tarreau58975672014-04-24 21:13:57 +02005364 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5365 "Cache-control: private", 22) < 0))
5366 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005367 }
Willy Tarreau58975672014-04-24 21:13:57 +02005368 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005369
Willy Tarreau58975672014-04-24 21:13:57 +02005370 /*
5371 * Check if result will be cacheable with a cookie.
5372 * We'll block the response if security checks have caught
5373 * nasty things such as a cacheable cookie.
5374 */
5375 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5376 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5377 (s->be->options & PR_O_CHK_CACHE)) {
5378 /* we're in presence of a cacheable response containing
5379 * a set-cookie header. We'll block it as requested by
5380 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005381 */
Willy Tarreau58975672014-04-24 21:13:57 +02005382 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005383 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005384
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005385 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5386 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005387 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005388 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005389
Christopher Faulet767a84b2017-11-24 16:50:31 +01005390 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5391 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005392 send_log(s->be, LOG_ALERT,
5393 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5394 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5395 goto return_srv_prx_502;
5396 }
Willy Tarreau03945942009-12-22 16:50:27 +01005397
Willy Tarreau70730dd2014-04-24 18:06:27 +02005398 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005399 /*
5400 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5401 * If an "Upgrade" token is found, the header is left untouched in order
5402 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005403 * if anything but "Upgrade" is present in the Connection header. We don't
5404 * want to touch any 101 response either since it's switching to another
5405 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005406 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005407 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005408 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005409 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005410
Willy Tarreau58975672014-04-24 21:13:57 +02005411 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5412 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5413 /* we want a keep-alive response here. Keep-alive header
5414 * required if either side is not 1.1.
5415 */
5416 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5417 want_flags |= TX_CON_KAL_SET;
5418 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005419 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005420 /* we want a close response here. Close header required if
5421 * the server is 1.1, regardless of the client.
5422 */
5423 if (msg->flags & HTTP_MSGF_VER_11)
5424 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005425 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005426
Willy Tarreau58975672014-04-24 21:13:57 +02005427 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5428 http_change_connection_header(txn, msg, want_flags);
5429 }
5430
5431 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005432 /* Always enter in the body analyzer */
5433 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5434 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005435
Willy Tarreau58975672014-04-24 21:13:57 +02005436 /* if the user wants to log as soon as possible, without counting
5437 * bytes from the server, then this is the right moment. We have
5438 * to temporarily assign bytes_out to log what we currently have.
5439 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005440 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005441 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5442 s->logs.bytes_out = txn->rsp.eoh;
5443 s->do_log(s);
5444 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005445 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005446 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005447}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005448
Willy Tarreaud98cf932009-12-27 22:54:55 +01005449/* This function is an analyser which forwards response body (including chunk
5450 * sizes if any). It is called as soon as we must forward, even if we forward
5451 * zero byte. The only situation where it must not be called is when we're in
5452 * tunnel mode and we want to forward till the close. It's used both to forward
5453 * remaining data and to resync after end of body. It expects the msg_state to
5454 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005455 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005456 *
5457 * It is capable of compressing response data both in content-length mode and
5458 * in chunked mode. The state machines follows different flows depending on
5459 * whether content-length and chunked modes are used, since there are no
5460 * trailers in content-length :
5461 *
5462 * chk-mode cl-mode
5463 * ,----- BODY -----.
5464 * / \
5465 * V size > 0 V chk-mode
5466 * .--> SIZE -------------> DATA -------------> CRLF
5467 * | | size == 0 | last byte |
5468 * | v final crlf v inspected |
5469 * | TRAILERS -----------> DONE |
5470 * | |
5471 * `----------------------------------------------'
5472 *
5473 * Compression only happens in the DATA state, and must be flushed in final
5474 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5475 * is performed at once on final states for all bytes parsed, or when leaving
5476 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005477 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005478int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005479{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005480 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005481 struct http_txn *txn = s->txn;
5482 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005483 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005484
Christopher Faulet45073512018-07-20 10:16:29 +02005485 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 +02005486 now_ms, __FUNCTION__,
5487 s,
5488 res,
5489 res->rex, res->wex,
5490 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005491 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005492 res->analysers);
5493
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005494 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5495 return 0;
5496
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005497 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005498 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005499 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005500 /* Output closed while we were sending data. We must abort and
5501 * wake the other side up.
5502 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005503 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005504 msg->msg_state = HTTP_MSG_ERROR;
5505 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005506 return 1;
5507 }
5508
Willy Tarreau4fe41902010-06-07 22:27:41 +02005509 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005510 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005511
Christopher Fauletd7c91962015-04-30 11:48:27 +02005512 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005513 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5514 ? HTTP_MSG_CHUNK_SIZE
5515 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005516 }
5517
Willy Tarreauefdf0942014-04-24 20:08:57 +02005518 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005519 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005520 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005521 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005522 }
5523
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005524 if (msg->msg_state < HTTP_MSG_DONE) {
5525 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5526 ? http_msg_forward_chunked_body(s, msg)
5527 : http_msg_forward_body(s, msg));
5528 if (!ret)
5529 goto missing_data_or_waiting;
5530 if (ret < 0)
5531 goto return_bad_res;
5532 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005533
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005534 /* other states, DONE...TUNNEL */
5535 /* for keep-alive we don't want to forward closes on DONE */
5536 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5537 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5538 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005539
Christopher Faulet894da4c2017-07-18 11:29:07 +02005540 http_resync_states(s);
5541 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005542 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5543 if (res->flags & CF_SHUTW) {
5544 /* response errors are most likely due to the
5545 * client aborting the transfer. */
5546 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005547 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005548 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005549 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005550 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005551 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005552 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005553 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005554 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005555
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005556 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005557 if (res->flags & CF_SHUTW)
5558 goto aborted_xfer;
5559
5560 /* stop waiting for data if the input is closed before the end. If the
5561 * client side was already closed, it means that the client has aborted,
5562 * so we don't want to count this as a server abort. Otherwise it's a
5563 * server abort.
5564 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005565 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005566 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005567 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005568 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005569 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005570 if (!(s->flags & SF_ERR_MASK))
5571 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005572 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005573 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005574 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005575 goto return_bad_res_stats_ok;
5576 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005577 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005578
Willy Tarreau40dba092010-03-04 18:14:51 +01005579 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005580 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005581 goto return_bad_res;
5582
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005583 /* When TE: chunked is used, we need to get there again to parse
5584 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005585 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5586 * or if there are filters registered on the stream, we don't want to
5587 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005588 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005589 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005590 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005591 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5592 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005593 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005594
Willy Tarreau5c620922011-05-11 19:56:11 +02005595 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005596 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005597 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005598 * modes are already handled by the stream sock layer. We must not do
5599 * this in content-length mode because it could present the MSG_MORE
5600 * flag with the last block of forwarded data, which would cause an
5601 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005602 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005603 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005604 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005605
Willy Tarreau87b09662015-04-03 00:22:06 +02005606 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005607 return 0;
5608
Willy Tarreau40dba092010-03-04 18:14:51 +01005609 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005610 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005611 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005612 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005613
5614 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005615 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005616 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005617 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005618 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005619 res->analysers &= AN_RES_FLT_END;
5620 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 +01005621 if (objt_server(s->target))
5622 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005623
Willy Tarreaue7dff022015-04-03 01:14:29 +02005624 if (!(s->flags & SF_ERR_MASK))
5625 s->flags |= SF_ERR_PRXCOND;
5626 if (!(s->flags & SF_FINST_MASK))
5627 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005628 return 0;
5629
5630 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005631 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005632 txn->rsp.msg_state = HTTP_MSG_ERROR;
5633 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005634 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005635 res->analysers &= AN_RES_FLT_END;
5636 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 +01005637
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005638 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5639 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005640 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005641 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005642
Willy Tarreaue7dff022015-04-03 01:14:29 +02005643 if (!(s->flags & SF_ERR_MASK))
5644 s->flags |= SF_ERR_CLICL;
5645 if (!(s->flags & SF_FINST_MASK))
5646 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005647 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005648}
5649
5650
Christopher Faulet10079f52018-10-03 15:17:28 +02005651int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005652{
5653 struct channel *chn = msg->chn;
5654 int ret;
5655
5656 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5657
5658 if (msg->msg_state == HTTP_MSG_ENDING)
5659 goto ending;
5660
5661 /* Neither content-length, nor transfer-encoding was found, so we must
5662 * read the body until the server connection is closed. In that case, we
5663 * eat data as they come. Of course, this happens for response only. */
5664 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005665 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005666 msg->chunk_len += len;
5667 msg->body_len += len;
5668 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005669 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005670 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005671 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005672 msg->next += ret;
5673 msg->chunk_len -= ret;
5674 if (msg->chunk_len) {
5675 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005676 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005677 chn->flags |= CF_WAKE_WRITE;
5678 goto missing_data_or_waiting;
5679 }
5680
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005681 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5682 * always set for a request. */
5683 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5684 /* The server still sending data that should be filtered */
5685 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5686 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005687 msg->msg_state = HTTP_MSG_TUNNEL;
5688 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005689 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005690
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005691 msg->msg_state = HTTP_MSG_ENDING;
5692
5693 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005694 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005695 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005696 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5697 /* default_ret */ msg->next,
5698 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005699 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005700 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005701 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5702 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005703 if (msg->next)
5704 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005705
Christopher Fauletda02e172015-12-04 09:25:05 +01005706 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5707 /* default_ret */ 1,
5708 /* on_error */ goto error,
5709 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005710 if (msg->msg_state == HTTP_MSG_ENDING)
5711 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005712 return 1;
5713
5714 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005715 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005716 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5717 /* default_ret */ msg->next,
5718 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005719 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005720 msg->next -= ret;
5721 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5722 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005723 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005724 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005725 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005726 return 0;
5727 error:
5728 return -1;
5729}
5730
Christopher Faulet10079f52018-10-03 15:17:28 +02005731int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005732{
5733 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005734 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005735 int ret;
5736
5737 /* Here we have the guarantee to be in one of the following state:
5738 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5739 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5740
Christopher Fauletca874b82018-09-20 11:31:01 +02005741 if (msg->msg_state == HTTP_MSG_ENDING)
5742 goto ending;
5743
5744 /* Don't parse chunks if there is no input data */
5745 if (!ci_data(chn))
5746 goto waiting;
5747
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005748 switch_states:
5749 switch (msg->msg_state) {
5750 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005751 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005752 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005753 /* on_error */ goto error);
5754 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005755 msg->chunk_len -= ret;
5756 if (msg->chunk_len) {
5757 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005758 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005759 chn->flags |= CF_WAKE_WRITE;
5760 goto missing_data_or_waiting;
5761 }
5762
5763 /* nothing left to forward for this chunk*/
5764 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5765 /* fall through for HTTP_MSG_CHUNK_CRLF */
5766
5767 case HTTP_MSG_CHUNK_CRLF:
5768 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005769 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005770 if (ret == 0)
5771 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005772 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005773 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005774 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005775 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005776 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005777 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005778 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005779 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5780 /* fall through for HTTP_MSG_CHUNK_SIZE */
5781
5782 case HTTP_MSG_CHUNK_SIZE:
5783 /* read the chunk size and assign it to ->chunk_len,
5784 * then set ->next to point to the body and switch to
5785 * DATA or TRAILERS state.
5786 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005787 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005788 if (ret == 0)
5789 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005790 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005791 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005792 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005793 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005794 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005795 }
5796
5797 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005798 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005799 msg->chunk_len = chunk;
5800 msg->body_len += chunk;
5801
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005802 if (msg->chunk_len) {
5803 msg->msg_state = HTTP_MSG_DATA;
5804 goto switch_states;
5805 }
5806 msg->msg_state = HTTP_MSG_TRAILERS;
5807 /* fall through for HTTP_MSG_TRAILERS */
5808
5809 case HTTP_MSG_TRAILERS:
5810 ret = http_forward_trailers(msg);
5811 if (ret < 0)
5812 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005813 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5814 /* default_ret */ 1,
5815 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005816 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005817 if (!ret)
5818 goto missing_data_or_waiting;
5819 break;
5820
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005821 default:
5822 /* This should no happen in this function */
5823 goto error;
5824 }
5825
5826 msg->msg_state = HTTP_MSG_ENDING;
5827 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005828 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005829 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005830 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005831 /* default_ret */ msg->next,
5832 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005833 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005834 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005835 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5836 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005837 if (msg->next)
5838 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005839
Christopher Fauletda02e172015-12-04 09:25:05 +01005840 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005841 /* default_ret */ 1,
5842 /* on_error */ goto error,
5843 /* on_wait */ goto waiting);
5844 msg->msg_state = HTTP_MSG_DONE;
5845 return 1;
5846
5847 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005848 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005849 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005850 /* default_ret */ msg->next,
5851 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005852 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005853 msg->next -= ret;
5854 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5855 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005856 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005857 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005858 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005859 return 0;
5860
5861 chunk_parsing_error:
5862 if (msg->err_pos >= 0) {
5863 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005864 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005865 msg->msg_state, strm_fe(s));
5866 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005867 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005868 msg, msg->msg_state, s->be);
5869 }
5870 error:
5871 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005872}
5873
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005874
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005875/* Iterate the same filter through all request headers.
5876 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005877 * Since it can manage the switch to another backend, it updates the per-proxy
5878 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005879 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005880int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005881{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005882 char *cur_ptr, *cur_end, *cur_next;
5883 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005884 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005885 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005886 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005887
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005888 last_hdr = 0;
5889
Willy Tarreauf37954d2018-06-15 18:31:02 +02005890 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005891 old_idx = 0;
5892
5893 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005894 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005895 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005896 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005897 (exp->action == ACT_ALLOW ||
5898 exp->action == ACT_DENY ||
5899 exp->action == ACT_TARPIT))
5900 return 0;
5901
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005902 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005903 if (!cur_idx)
5904 break;
5905
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005906 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005907 cur_ptr = cur_next;
5908 cur_end = cur_ptr + cur_hdr->len;
5909 cur_next = cur_end + cur_hdr->cr + 1;
5910
5911 /* Now we have one header between cur_ptr and cur_end,
5912 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005913 */
5914
Willy Tarreau15a53a42015-01-21 13:39:42 +01005915 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005916 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005917 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005918 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005919 last_hdr = 1;
5920 break;
5921
5922 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005923 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005924 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005925 break;
5926
5927 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005928 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005929 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005930 break;
5931
5932 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005933 len = exp_replace(trash.area,
5934 trash.size, cur_ptr,
5935 exp->replace, pmatch);
5936 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005937 return -1;
5938
Willy Tarreau6e27be12018-08-22 04:46:47 +02005939 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5940
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005941 /* FIXME: if the user adds a newline in the replacement, the
5942 * index will not be recalculated for now, and the new line
5943 * will not be counted as a new header.
5944 */
5945
5946 cur_end += delta;
5947 cur_next += delta;
5948 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005949 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005950 break;
5951
5952 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005953 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005954 cur_next += delta;
5955
Willy Tarreaufa355d42009-11-29 18:12:29 +01005956 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005957 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5958 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005959 cur_hdr->len = 0;
5960 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005961 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005962 break;
5963
5964 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005965 }
5966
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005967 /* keep the link from this header to next one in case of later
5968 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005969 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005970 old_idx = cur_idx;
5971 }
5972 return 0;
5973}
5974
5975
5976/* Apply the filter to the request line.
5977 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5978 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005979 * Since it can manage the switch to another backend, it updates the per-proxy
5980 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005981 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005982int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005983{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005984 char *cur_ptr, *cur_end;
5985 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005986 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005987 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005988
Willy Tarreau3d300592007-03-18 18:34:41 +01005989 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005990 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005991 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005992 (exp->action == ACT_ALLOW ||
5993 exp->action == ACT_DENY ||
5994 exp->action == ACT_TARPIT))
5995 return 0;
5996 else if (exp->action == ACT_REMOVE)
5997 return 0;
5998
5999 done = 0;
6000
Willy Tarreauf37954d2018-06-15 18:31:02 +02006001 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006002 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006003
6004 /* Now we have the request line between cur_ptr and cur_end */
6005
Willy Tarreau15a53a42015-01-21 13:39:42 +01006006 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006007 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006008 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006009 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006010 done = 1;
6011 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006012
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006013 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006014 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006015 done = 1;
6016 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006017
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006018 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006019 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006020 done = 1;
6021 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006022
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006023 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006024 len = exp_replace(trash.area, trash.size,
6025 cur_ptr, exp->replace, pmatch);
6026 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006027 return -1;
6028
Willy Tarreau6e27be12018-08-22 04:46:47 +02006029 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6030
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006031 /* FIXME: if the user adds a newline in the replacement, the
6032 * index will not be recalculated for now, and the new line
6033 * will not be counted as a new header.
6034 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006035
Willy Tarreaufa355d42009-11-29 18:12:29 +01006036 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006037 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006038 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006039 HTTP_MSG_RQMETH,
6040 cur_ptr, cur_end + 1,
6041 NULL, NULL);
6042 if (unlikely(!cur_end))
6043 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006044
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006045 /* we have a full request and we know that we have either a CR
6046 * or an LF at <ptr>.
6047 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006048 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6049 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006050 /* there is no point trying this regex on headers */
6051 return 1;
6052 }
6053 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006054 return done;
6055}
Willy Tarreau97de6242006-12-27 17:18:38 +01006056
Willy Tarreau58f10d72006-12-04 02:26:12 +01006057
Willy Tarreau58f10d72006-12-04 02:26:12 +01006058
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006059/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006060 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006061 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006062 * unparsable request. Since it can manage the switch to another backend, it
6063 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006064 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006065int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006066{
Willy Tarreau192252e2015-04-04 01:47:55 +02006067 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006068 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006069 struct hdr_exp *exp;
6070
6071 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006072 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006073
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006074 /*
6075 * The interleaving of transformations and verdicts
6076 * makes it difficult to decide to continue or stop
6077 * the evaluation.
6078 */
6079
Willy Tarreau6c123b12010-01-28 20:22:06 +01006080 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6081 break;
6082
Willy Tarreau3d300592007-03-18 18:34:41 +01006083 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006084 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006085 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006086 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006087
6088 /* if this filter had a condition, evaluate it now and skip to
6089 * next filter if the condition does not match.
6090 */
6091 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006092 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006093 ret = acl_pass(ret);
6094 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6095 ret = !ret;
6096
6097 if (!ret)
6098 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006099 }
6100
6101 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006102 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006103 if (unlikely(ret < 0))
6104 return -1;
6105
6106 if (likely(ret == 0)) {
6107 /* The filter did not match the request, it can be
6108 * iterated through all headers.
6109 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006110 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6111 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006112 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006113 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006114 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006115}
6116
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006117
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006118/* Delete a value in a header between delimiters <from> and <next> in buffer
6119 * <buf>. The number of characters displaced is returned, and the pointer to
6120 * the first delimiter is updated if required. The function tries as much as
6121 * possible to respect the following principles :
6122 * - replace <from> delimiter by the <next> one unless <from> points to a
6123 * colon, in which case <next> is simply removed
6124 * - set exactly one space character after the new first delimiter, unless
6125 * there are not enough characters in the block being moved to do so.
6126 * - remove unneeded spaces before the previous delimiter and after the new
6127 * one.
6128 *
6129 * It is the caller's responsibility to ensure that :
6130 * - <from> points to a valid delimiter or the colon ;
6131 * - <next> points to a valid delimiter or the final CR/LF ;
6132 * - there are non-space chars before <from> ;
6133 * - there is a CR/LF at or after <next>.
6134 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006135int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006136{
6137 char *prev = *from;
6138
6139 if (*prev == ':') {
6140 /* We're removing the first value, preserve the colon and add a
6141 * space if possible.
6142 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006143 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006144 next++;
6145 prev++;
6146 if (prev < next)
6147 *prev++ = ' ';
6148
Willy Tarreau2235b262016-11-05 15:50:20 +01006149 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006150 next++;
6151 } else {
6152 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006153 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006154 prev--;
6155 *from = prev;
6156
6157 /* copy the delimiter and if possible a space if we're
6158 * not at the end of the line.
6159 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006160 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006161 *prev++ = *next++;
6162 if (prev + 1 < next)
6163 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006164 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006165 next++;
6166 }
6167 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006168 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006169}
6170
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006171/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006172 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006173 * desirable to call it only when needed. This code is quite complex because
6174 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6175 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006176 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006177void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006178{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006179 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006180 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006181 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006182 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006183 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6184 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006185
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006186 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006187 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006188 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006189
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006190 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006191 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006192 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006193
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006194 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006195 hdr_beg = hdr_next;
6196 hdr_end = hdr_beg + cur_hdr->len;
6197 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006198
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006199 /* We have one full header between hdr_beg and hdr_end, and the
6200 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006201 * "Cookie:" headers.
6202 */
6203
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006204 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006205 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006206 old_idx = cur_idx;
6207 continue;
6208 }
6209
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006210 del_from = NULL; /* nothing to be deleted */
6211 preserve_hdr = 0; /* assume we may kill the whole header */
6212
Willy Tarreau58f10d72006-12-04 02:26:12 +01006213 /* Now look for cookies. Conforming to RFC2109, we have to support
6214 * attributes whose name begin with a '$', and associate them with
6215 * the right cookie, if we want to delete this cookie.
6216 * So there are 3 cases for each cookie read :
6217 * 1) it's a special attribute, beginning with a '$' : ignore it.
6218 * 2) it's a server id cookie that we *MAY* want to delete : save
6219 * some pointers on it (last semi-colon, beginning of cookie...)
6220 * 3) it's an application cookie : we *MAY* have to delete a previous
6221 * "special" cookie.
6222 * At the end of loop, if a "special" cookie remains, we may have to
6223 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006224 * *MUST* delete it.
6225 *
6226 * Note: RFC2965 is unclear about the processing of spaces around
6227 * the equal sign in the ATTR=VALUE form. A careful inspection of
6228 * the RFC explicitly allows spaces before it, and not within the
6229 * tokens (attrs or values). An inspection of RFC2109 allows that
6230 * too but section 10.1.3 lets one think that spaces may be allowed
6231 * after the equal sign too, resulting in some (rare) buggy
6232 * implementations trying to do that. So let's do what servers do.
6233 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6234 * allowed quoted strings in values, with any possible character
6235 * after a backslash, including control chars and delimitors, which
6236 * causes parsing to become ambiguous. Browsers also allow spaces
6237 * within values even without quotes.
6238 *
6239 * We have to keep multiple pointers in order to support cookie
6240 * removal at the beginning, middle or end of header without
6241 * corrupting the header. All of these headers are valid :
6242 *
6243 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6244 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6245 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6246 * | | | | | | | | |
6247 * | | | | | | | | hdr_end <--+
6248 * | | | | | | | +--> next
6249 * | | | | | | +----> val_end
6250 * | | | | | +-----------> val_beg
6251 * | | | | +--------------> equal
6252 * | | | +----------------> att_end
6253 * | | +---------------------> att_beg
6254 * | +--------------------------> prev
6255 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006256 */
6257
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006258 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6259 /* Iterate through all cookies on this line */
6260
6261 /* find att_beg */
6262 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006263 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006264 att_beg++;
6265
6266 /* find att_end : this is the first character after the last non
6267 * space before the equal. It may be equal to hdr_end.
6268 */
6269 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006270
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006271 while (equal < hdr_end) {
6272 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006273 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006274 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006275 continue;
6276 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006277 }
6278
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006279 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6280 * is between <att_beg> and <equal>, both may be identical.
6281 */
6282
6283 /* look for end of cookie if there is an equal sign */
6284 if (equal < hdr_end && *equal == '=') {
6285 /* look for the beginning of the value */
6286 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006287 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 val_beg++;
6289
6290 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006291 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006292
6293 /* make val_end point to the first white space or delimitor after the value */
6294 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006295 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006296 val_end--;
6297 } else {
6298 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006299 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006300
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006301 /* We have nothing to do with attributes beginning with '$'. However,
6302 * they will automatically be removed if a header before them is removed,
6303 * since they're supposed to be linked together.
6304 */
6305 if (*att_beg == '$')
6306 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006307
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006308 /* Ignore cookies with no equal sign */
6309 if (equal == next) {
6310 /* This is not our cookie, so we must preserve it. But if we already
6311 * scheduled another cookie for removal, we cannot remove the
6312 * complete header, but we can remove the previous block itself.
6313 */
6314 preserve_hdr = 1;
6315 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006316 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006317 val_end += delta;
6318 next += delta;
6319 hdr_end += delta;
6320 hdr_next += delta;
6321 cur_hdr->len += delta;
6322 http_msg_move_end(&txn->req, delta);
6323 prev = del_from;
6324 del_from = NULL;
6325 }
6326 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006327 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006328
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006329 /* if there are spaces around the equal sign, we need to
6330 * strip them otherwise we'll get trouble for cookie captures,
6331 * or even for rewrites. Since this happens extremely rarely,
6332 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006333 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006334 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6335 int stripped_before = 0;
6336 int stripped_after = 0;
6337
6338 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006339 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006340 equal += stripped_before;
6341 val_beg += stripped_before;
6342 }
6343
6344 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006345 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006346 val_beg += stripped_after;
6347 stripped_before += stripped_after;
6348 }
6349
6350 val_end += stripped_before;
6351 next += stripped_before;
6352 hdr_end += stripped_before;
6353 hdr_next += stripped_before;
6354 cur_hdr->len += stripped_before;
6355 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006356 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006357 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006358
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006359 /* First, let's see if we want to capture this cookie. We check
6360 * that we don't already have a client side cookie, because we
6361 * can only capture one. Also as an optimisation, we ignore
6362 * cookies shorter than the declared name.
6363 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006364 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6365 (val_end - att_beg >= sess->fe->capture_namelen) &&
6366 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006367 int log_len = val_end - att_beg;
6368
Willy Tarreaubafbe012017-11-24 17:34:44 +01006369 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006370 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006371 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006372 if (log_len > sess->fe->capture_len)
6373 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006374 memcpy(txn->cli_cookie, att_beg, log_len);
6375 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006376 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006377 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006378
Willy Tarreaubca99692010-10-06 19:25:55 +02006379 /* Persistence cookies in passive, rewrite or insert mode have the
6380 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006381 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006382 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006383 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006384 * For cookies in prefix mode, the form is :
6385 *
6386 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006387 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006388 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6389 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6390 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006391 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006392
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006393 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6394 * have the server ID between val_beg and delim, and the original cookie between
6395 * delim+1 and val_end. Otherwise, delim==val_end :
6396 *
6397 * Cookie: NAME=SRV; # in all but prefix modes
6398 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6399 * | || || | |+-> next
6400 * | || || | +--> val_end
6401 * | || || +---------> delim
6402 * | || |+------------> val_beg
6403 * | || +-------------> att_end = equal
6404 * | |+-----------------> att_beg
6405 * | +------------------> prev
6406 * +-------------------------> hdr_beg
6407 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006408
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006409 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006410 for (delim = val_beg; delim < val_end; delim++)
6411 if (*delim == COOKIE_DELIM)
6412 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006413 } else {
6414 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006415 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006416 /* Now check if the cookie contains a date field, which would
6417 * appear after a vertical bar ('|') just after the server name
6418 * and before the delimiter.
6419 */
6420 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6421 if (vbar1) {
6422 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006423 * right is the last seen date. It is a base64 encoded
6424 * 30-bit value representing the UNIX date since the
6425 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006426 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006427 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006428 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006429 if (val_end - vbar1 >= 5) {
6430 val = b64tos30(vbar1);
6431 if (val > 0)
6432 txn->cookie_last_date = val << 2;
6433 }
6434 /* look for a second vertical bar */
6435 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6436 if (vbar1 && (val_end - vbar1 > 5)) {
6437 val = b64tos30(vbar1 + 1);
6438 if (val > 0)
6439 txn->cookie_first_date = val << 2;
6440 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006441 }
6442 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006443
Willy Tarreauf64d1412010-10-07 20:06:11 +02006444 /* if the cookie has an expiration date and the proxy wants to check
6445 * it, then we do that now. We first check if the cookie is too old,
6446 * then only if it has expired. We detect strict overflow because the
6447 * time resolution here is not great (4 seconds). Cookies with dates
6448 * in the future are ignored if their offset is beyond one day. This
6449 * allows an admin to fix timezone issues without expiring everyone
6450 * and at the same time avoids keeping unwanted side effects for too
6451 * long.
6452 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006453 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6454 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006455 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006456 txn->flags &= ~TX_CK_MASK;
6457 txn->flags |= TX_CK_OLD;
6458 delim = val_beg; // let's pretend we have not found the cookie
6459 txn->cookie_first_date = 0;
6460 txn->cookie_last_date = 0;
6461 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006462 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6463 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006464 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006465 txn->flags &= ~TX_CK_MASK;
6466 txn->flags |= TX_CK_EXPIRED;
6467 delim = val_beg; // let's pretend we have not found the cookie
6468 txn->cookie_first_date = 0;
6469 txn->cookie_last_date = 0;
6470 }
6471
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006472 /* Here, we'll look for the first running server which supports the cookie.
6473 * This allows to share a same cookie between several servers, for example
6474 * to dedicate backup servers to specific servers only.
6475 * However, to prevent clients from sticking to cookie-less backup server
6476 * when they have incidentely learned an empty cookie, we simply ignore
6477 * empty cookies and mark them as invalid.
6478 * The same behaviour is applied when persistence must be ignored.
6479 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006480 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006481 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006482
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006483 while (srv) {
6484 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6485 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006486 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006487 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006488 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006489 /* we found the server and we can use it */
6490 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006491 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006492 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006493 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006494 break;
6495 } else {
6496 /* we found a server, but it's down,
6497 * mark it as such and go on in case
6498 * another one is available.
6499 */
6500 txn->flags &= ~TX_CK_MASK;
6501 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006502 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006503 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006504 srv = srv->next;
6505 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006506
Willy Tarreauf64d1412010-10-07 20:06:11 +02006507 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006508 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006509 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006510 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006511 txn->flags |= TX_CK_UNUSED;
6512 else
6513 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006514 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006515
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006516 /* depending on the cookie mode, we may have to either :
6517 * - delete the complete cookie if we're in insert+indirect mode, so that
6518 * the server never sees it ;
6519 * - remove the server id from the cookie value, and tag the cookie as an
6520 * application cookie so that it does not get accidentely removed later,
6521 * if we're in cookie prefix mode
6522 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006523 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006524 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006525
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006526 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006527 val_end += delta;
6528 next += delta;
6529 hdr_end += delta;
6530 hdr_next += delta;
6531 cur_hdr->len += delta;
6532 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006533
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006534 del_from = NULL;
6535 preserve_hdr = 1; /* we want to keep this cookie */
6536 }
6537 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006538 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006539 del_from = prev;
6540 }
6541 } else {
6542 /* This is not our cookie, so we must preserve it. But if we already
6543 * scheduled another cookie for removal, we cannot remove the
6544 * complete header, but we can remove the previous block itself.
6545 */
6546 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006547
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006548 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006549 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006550 if (att_beg >= del_from)
6551 att_beg += delta;
6552 if (att_end >= del_from)
6553 att_end += delta;
6554 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006555 val_end += delta;
6556 next += delta;
6557 hdr_end += delta;
6558 hdr_next += delta;
6559 cur_hdr->len += delta;
6560 http_msg_move_end(&txn->req, delta);
6561 prev = del_from;
6562 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006563 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006564 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006565
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006566 /* continue with next cookie on this header line */
6567 att_beg = next;
6568 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006569
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006570 /* There are no more cookies on this line.
6571 * We may still have one (or several) marked for deletion at the
6572 * end of the line. We must do this now in two ways :
6573 * - if some cookies must be preserved, we only delete from the
6574 * mark to the end of line ;
6575 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006576 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006577 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006578 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006579 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006580 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006581 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006582 cur_hdr->len += delta;
6583 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006584 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006585
6586 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006587 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6588 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006589 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006590 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006591 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006592 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006593 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006594 }
6595
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006596 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006597 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006598 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006599}
6600
6601
Willy Tarreaua15645d2007-03-18 16:22:39 +01006602/* Iterate the same filter through all response headers contained in <rtr>.
6603 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6604 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006605int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006606{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607 char *cur_ptr, *cur_end, *cur_next;
6608 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006609 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006610 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006611 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006612
6613 last_hdr = 0;
6614
Willy Tarreauf37954d2018-06-15 18:31:02 +02006615 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006616 old_idx = 0;
6617
6618 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006619 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006620 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006621 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006622 (exp->action == ACT_ALLOW ||
6623 exp->action == ACT_DENY))
6624 return 0;
6625
6626 cur_idx = txn->hdr_idx.v[old_idx].next;
6627 if (!cur_idx)
6628 break;
6629
6630 cur_hdr = &txn->hdr_idx.v[cur_idx];
6631 cur_ptr = cur_next;
6632 cur_end = cur_ptr + cur_hdr->len;
6633 cur_next = cur_end + cur_hdr->cr + 1;
6634
6635 /* Now we have one header between cur_ptr and cur_end,
6636 * and the next header starts at cur_next.
6637 */
6638
Willy Tarreau15a53a42015-01-21 13:39:42 +01006639 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006640 switch (exp->action) {
6641 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006642 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006643 last_hdr = 1;
6644 break;
6645
6646 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006647 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648 last_hdr = 1;
6649 break;
6650
6651 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006652 len = exp_replace(trash.area,
6653 trash.size, cur_ptr,
6654 exp->replace, pmatch);
6655 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006656 return -1;
6657
Willy Tarreau6e27be12018-08-22 04:46:47 +02006658 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6659
Willy Tarreaua15645d2007-03-18 16:22:39 +01006660 /* FIXME: if the user adds a newline in the replacement, the
6661 * index will not be recalculated for now, and the new line
6662 * will not be counted as a new header.
6663 */
6664
6665 cur_end += delta;
6666 cur_next += delta;
6667 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006668 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006669 break;
6670
6671 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006672 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006673 cur_next += delta;
6674
Willy Tarreaufa355d42009-11-29 18:12:29 +01006675 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006676 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6677 txn->hdr_idx.used--;
6678 cur_hdr->len = 0;
6679 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006680 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006681 break;
6682
6683 }
6684 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006685
6686 /* keep the link from this header to next one in case of later
6687 * removal of next header.
6688 */
6689 old_idx = cur_idx;
6690 }
6691 return 0;
6692}
6693
6694
6695/* Apply the filter to the status line in the response buffer <rtr>.
6696 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6697 * or -1 if a replacement resulted in an invalid status line.
6698 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006699int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006700{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701 char *cur_ptr, *cur_end;
6702 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006703 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006704 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006705
Willy Tarreau3d300592007-03-18 18:34:41 +01006706 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006707 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006708 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006709 (exp->action == ACT_ALLOW ||
6710 exp->action == ACT_DENY))
6711 return 0;
6712 else if (exp->action == ACT_REMOVE)
6713 return 0;
6714
6715 done = 0;
6716
Willy Tarreauf37954d2018-06-15 18:31:02 +02006717 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006718 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006719
6720 /* Now we have the status line between cur_ptr and cur_end */
6721
Willy Tarreau15a53a42015-01-21 13:39:42 +01006722 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006723 switch (exp->action) {
6724 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006725 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006726 done = 1;
6727 break;
6728
6729 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006730 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006731 done = 1;
6732 break;
6733
6734 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006735 len = exp_replace(trash.area, trash.size,
6736 cur_ptr, exp->replace, pmatch);
6737 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006738 return -1;
6739
Willy Tarreau6e27be12018-08-22 04:46:47 +02006740 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6741
Willy Tarreaua15645d2007-03-18 16:22:39 +01006742 /* FIXME: if the user adds a newline in the replacement, the
6743 * index will not be recalculated for now, and the new line
6744 * will not be counted as a new header.
6745 */
6746
Willy Tarreaufa355d42009-11-29 18:12:29 +01006747 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006748 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006749 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006750 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006751 cur_ptr, cur_end + 1,
6752 NULL, NULL);
6753 if (unlikely(!cur_end))
6754 return -1;
6755
6756 /* we have a full respnse and we know that we have either a CR
6757 * or an LF at <ptr>.
6758 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006759 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006760 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006761 /* there is no point trying this regex on headers */
6762 return 1;
6763 }
6764 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006765 return done;
6766}
6767
6768
6769
6770/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006771 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006772 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6773 * unparsable response.
6774 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006775int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006776{
Willy Tarreau192252e2015-04-04 01:47:55 +02006777 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006778 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006779 struct hdr_exp *exp;
6780
6781 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006782 int ret;
6783
6784 /*
6785 * The interleaving of transformations and verdicts
6786 * makes it difficult to decide to continue or stop
6787 * the evaluation.
6788 */
6789
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006790 if (txn->flags & TX_SVDENY)
6791 break;
6792
Willy Tarreau3d300592007-03-18 18:34:41 +01006793 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006794 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6795 exp->action == ACT_PASS)) {
6796 exp = exp->next;
6797 continue;
6798 }
6799
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006800 /* if this filter had a condition, evaluate it now and skip to
6801 * next filter if the condition does not match.
6802 */
6803 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006804 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006805 ret = acl_pass(ret);
6806 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6807 ret = !ret;
6808 if (!ret)
6809 continue;
6810 }
6811
Willy Tarreaua15645d2007-03-18 16:22:39 +01006812 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006813 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006814 if (unlikely(ret < 0))
6815 return -1;
6816
6817 if (likely(ret == 0)) {
6818 /* The filter did not match the response, it can be
6819 * iterated through all headers.
6820 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006821 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6822 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006823 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006824 }
6825 return 0;
6826}
6827
6828
Willy Tarreaua15645d2007-03-18 16:22:39 +01006829/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006830 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006831 * desirable to call it only when needed. This function is also used when we
6832 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006834void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006835{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006836 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006837 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006838 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006839 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006840 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006841 char *hdr_beg, *hdr_end, *hdr_next;
6842 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006843
Willy Tarreaua15645d2007-03-18 16:22:39 +01006844 /* Iterate through the headers.
6845 * we start with the start line.
6846 */
6847 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006848 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006849
6850 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6851 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006852 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006853
6854 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006855 hdr_beg = hdr_next;
6856 hdr_end = hdr_beg + cur_hdr->len;
6857 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006858
Willy Tarreau24581ba2010-08-31 22:39:35 +02006859 /* We have one full header between hdr_beg and hdr_end, and the
6860 * next header starts at hdr_next. We're only interested in
6861 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006862 */
6863
Willy Tarreau24581ba2010-08-31 22:39:35 +02006864 is_cookie2 = 0;
6865 prev = hdr_beg + 10;
6866 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006867 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006868 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6869 if (!val) {
6870 old_idx = cur_idx;
6871 continue;
6872 }
6873 is_cookie2 = 1;
6874 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006875 }
6876
Willy Tarreau24581ba2010-08-31 22:39:35 +02006877 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6878 * <prev> points to the colon.
6879 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006880 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006881
Willy Tarreau24581ba2010-08-31 22:39:35 +02006882 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6883 * check-cache is enabled) and we are not interested in checking
6884 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006885 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006886 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006887 return;
6888
Willy Tarreau24581ba2010-08-31 22:39:35 +02006889 /* OK so now we know we have to process this response cookie.
6890 * The format of the Set-Cookie header is slightly different
6891 * from the format of the Cookie header in that it does not
6892 * support the comma as a cookie delimiter (thus the header
6893 * cannot be folded) because the Expires attribute described in
6894 * the original Netscape's spec may contain an unquoted date
6895 * with a comma inside. We have to live with this because
6896 * many browsers don't support Max-Age and some browsers don't
6897 * support quoted strings. However the Set-Cookie2 header is
6898 * clean.
6899 *
6900 * We have to keep multiple pointers in order to support cookie
6901 * removal at the beginning, middle or end of header without
6902 * corrupting the header (in case of set-cookie2). A special
6903 * pointer, <scav> points to the beginning of the set-cookie-av
6904 * fields after the first semi-colon. The <next> pointer points
6905 * either to the end of line (set-cookie) or next unquoted comma
6906 * (set-cookie2). All of these headers are valid :
6907 *
6908 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6909 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6910 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6911 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6912 * | | | | | | | | | |
6913 * | | | | | | | | +-> next hdr_end <--+
6914 * | | | | | | | +------------> scav
6915 * | | | | | | +--------------> val_end
6916 * | | | | | +--------------------> val_beg
6917 * | | | | +----------------------> equal
6918 * | | | +------------------------> att_end
6919 * | | +----------------------------> att_beg
6920 * | +------------------------------> prev
6921 * +-----------------------------------------> hdr_beg
6922 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006923
Willy Tarreau24581ba2010-08-31 22:39:35 +02006924 for (; prev < hdr_end; prev = next) {
6925 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006926
Willy Tarreau24581ba2010-08-31 22:39:35 +02006927 /* find att_beg */
6928 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006929 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006930 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006931
Willy Tarreau24581ba2010-08-31 22:39:35 +02006932 /* find att_end : this is the first character after the last non
6933 * space before the equal. It may be equal to hdr_end.
6934 */
6935 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006936
Willy Tarreau24581ba2010-08-31 22:39:35 +02006937 while (equal < hdr_end) {
6938 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6939 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006940 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006941 continue;
6942 att_end = equal;
6943 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006944
Willy Tarreau24581ba2010-08-31 22:39:35 +02006945 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6946 * is between <att_beg> and <equal>, both may be identical.
6947 */
6948
6949 /* look for end of cookie if there is an equal sign */
6950 if (equal < hdr_end && *equal == '=') {
6951 /* look for the beginning of the value */
6952 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006953 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006954 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006955
Willy Tarreau24581ba2010-08-31 22:39:35 +02006956 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006957 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006958
6959 /* make val_end point to the first white space or delimitor after the value */
6960 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006961 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006962 val_end--;
6963 } else {
6964 /* <equal> points to next comma, semi-colon or EOL */
6965 val_beg = val_end = next = equal;
6966 }
6967
6968 if (next < hdr_end) {
6969 /* Set-Cookie2 supports multiple cookies, and <next> points to
6970 * a colon or semi-colon before the end. So skip all attr-value
6971 * pairs and look for the next comma. For Set-Cookie, since
6972 * commas are permitted in values, skip to the end.
6973 */
6974 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006975 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006976 else
6977 next = hdr_end;
6978 }
6979
6980 /* Now everything is as on the diagram above */
6981
6982 /* Ignore cookies with no equal sign */
6983 if (equal == val_end)
6984 continue;
6985
6986 /* If there are spaces around the equal sign, we need to
6987 * strip them otherwise we'll get trouble for cookie captures,
6988 * or even for rewrites. Since this happens extremely rarely,
6989 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006990 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006991 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6992 int stripped_before = 0;
6993 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994
Willy Tarreau24581ba2010-08-31 22:39:35 +02006995 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006996 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006997 equal += stripped_before;
6998 val_beg += stripped_before;
6999 }
7000
7001 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007002 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007003 val_beg += stripped_after;
7004 stripped_before += stripped_after;
7005 }
7006
7007 val_end += stripped_before;
7008 next += stripped_before;
7009 hdr_end += stripped_before;
7010 hdr_next += stripped_before;
7011 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007012 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007013 }
7014
7015 /* First, let's see if we want to capture this cookie. We check
7016 * that we don't already have a server side cookie, because we
7017 * can only capture one. Also as an optimisation, we ignore
7018 * cookies shorter than the declared name.
7019 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007020 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007021 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007022 (val_end - att_beg >= sess->fe->capture_namelen) &&
7023 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007024 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007025 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007026 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007027 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007028 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007029 if (log_len > sess->fe->capture_len)
7030 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007031 memcpy(txn->srv_cookie, att_beg, log_len);
7032 txn->srv_cookie[log_len] = 0;
7033 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007034 }
7035
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007036 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007037 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007038 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007039 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7040 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007041 /* assume passive cookie by default */
7042 txn->flags &= ~TX_SCK_MASK;
7043 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007044
7045 /* If the cookie is in insert mode on a known server, we'll delete
7046 * this occurrence because we'll insert another one later.
7047 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007048 * a direct access.
7049 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007050 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007051 /* The "preserve" flag was set, we don't want to touch the
7052 * server's cookie.
7053 */
7054 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007055 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007056 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007057 /* this cookie must be deleted */
7058 if (*prev == ':' && next == hdr_end) {
7059 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007060 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007061 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7062 txn->hdr_idx.used--;
7063 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007064 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007065 hdr_next += delta;
7066 http_msg_move_end(&txn->rsp, delta);
7067 /* note: while both invalid now, <next> and <hdr_end>
7068 * are still equal, so the for() will stop as expected.
7069 */
7070 } else {
7071 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007072 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007073 next = prev;
7074 hdr_end += delta;
7075 hdr_next += delta;
7076 cur_hdr->len += delta;
7077 http_msg_move_end(&txn->rsp, delta);
7078 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007079 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007080 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007081 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007082 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007083 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007084 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007085 * with this server since we know it.
7086 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007087 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007088 next += delta;
7089 hdr_end += delta;
7090 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007091 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007092 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007093
Willy Tarreauf1348312010-10-07 15:54:11 +02007094 txn->flags &= ~TX_SCK_MASK;
7095 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007096 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007097 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007098 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007099 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007100 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007101 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007102 next += delta;
7103 hdr_end += delta;
7104 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007105 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007106 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007107
Willy Tarreau827aee92011-03-10 16:55:02 +01007108 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007109 txn->flags &= ~TX_SCK_MASK;
7110 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007111 }
7112 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007113 /* that's done for this cookie, check the next one on the same
7114 * line when next != hdr_end (only if is_cookie2).
7115 */
7116 }
7117 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007118 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007119 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007120}
7121
7122
Willy Tarreaua15645d2007-03-18 16:22:39 +01007123/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007124 * Parses the Cache-Control and Pragma request header fields to determine if
7125 * the request may be served from the cache and/or if it is cacheable. Updates
7126 * s->txn->flags.
7127 */
7128void check_request_for_cacheability(struct stream *s, struct channel *chn)
7129{
7130 struct http_txn *txn = s->txn;
7131 char *p1, *p2;
7132 char *cur_ptr, *cur_end, *cur_next;
7133 int pragma_found;
7134 int cc_found;
7135 int cur_idx;
7136
7137 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7138 return; /* nothing more to do here */
7139
7140 cur_idx = 0;
7141 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007142 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007143
7144 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7145 struct hdr_idx_elem *cur_hdr;
7146 int val;
7147
7148 cur_hdr = &txn->hdr_idx.v[cur_idx];
7149 cur_ptr = cur_next;
7150 cur_end = cur_ptr + cur_hdr->len;
7151 cur_next = cur_end + cur_hdr->cr + 1;
7152
7153 /* We have one full header between cur_ptr and cur_end, and the
7154 * next header starts at cur_next.
7155 */
7156
7157 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7158 if (val) {
7159 if ((cur_end - (cur_ptr + val) >= 8) &&
7160 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7161 pragma_found = 1;
7162 continue;
7163 }
7164 }
7165
William Lallemand8a16fe02018-05-22 11:04:33 +02007166 /* Don't use the cache and don't try to store if we found the
7167 * Authorization header */
7168 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7169 if (val) {
7170 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7171 txn->flags |= TX_CACHE_IGNORE;
7172 continue;
7173 }
7174
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007175 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7176 if (!val)
7177 continue;
7178
7179 /* OK, right now we know we have a cache-control header at cur_ptr */
7180 cc_found = 1;
7181 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7182
7183 if (p1 >= cur_end) /* no more info */
7184 continue;
7185
7186 /* p1 is at the beginning of the value */
7187 p2 = p1;
7188 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7189 p2++;
7190
7191 /* we have a complete value between p1 and p2. We don't check the
7192 * values after max-age, max-stale nor min-fresh, we simply don't
7193 * use the cache when they're specified.
7194 */
7195 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7196 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7197 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7198 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7199 txn->flags |= TX_CACHE_IGNORE;
7200 continue;
7201 }
7202
7203 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7204 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7205 continue;
7206 }
7207 }
7208
7209 /* RFC7234#5.4:
7210 * When the Cache-Control header field is also present and
7211 * understood in a request, Pragma is ignored.
7212 * When the Cache-Control header field is not present in a
7213 * request, caches MUST consider the no-cache request
7214 * pragma-directive as having the same effect as if
7215 * "Cache-Control: no-cache" were present.
7216 */
7217 if (!cc_found && pragma_found)
7218 txn->flags |= TX_CACHE_IGNORE;
7219}
7220
7221/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007222 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007223 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007224void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007225{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007226 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007227 char *p1, *p2;
7228
7229 char *cur_ptr, *cur_end, *cur_next;
7230 int cur_idx;
7231
Willy Tarreau12b32f22017-12-21 16:08:09 +01007232 if (txn->status < 200) {
7233 /* do not try to cache interim responses! */
7234 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007235 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007236 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007237
7238 /* Iterate through the headers.
7239 * we start with the start line.
7240 */
7241 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007242 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007243
7244 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7245 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007246 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007247
7248 cur_hdr = &txn->hdr_idx.v[cur_idx];
7249 cur_ptr = cur_next;
7250 cur_end = cur_ptr + cur_hdr->len;
7251 cur_next = cur_end + cur_hdr->cr + 1;
7252
7253 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007254 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255 */
7256
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007257 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7258 if (val) {
7259 if ((cur_end - (cur_ptr + val) >= 8) &&
7260 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7261 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7262 return;
7263 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007264 }
7265
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007266 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7267 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007268 continue;
7269
7270 /* OK, right now we know we have a cache-control header at cur_ptr */
7271
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007272 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007273
7274 if (p1 >= cur_end) /* no more info */
7275 continue;
7276
7277 /* p1 is at the beginning of the value */
7278 p2 = p1;
7279
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007280 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007281 p2++;
7282
7283 /* we have a complete value between p1 and p2 */
7284 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007285 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7286 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7287 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7288 continue;
7289 }
7290
Willy Tarreaua15645d2007-03-18 16:22:39 +01007291 /* we have something of the form no-cache="set-cookie" */
7292 if ((cur_end - p1 >= 21) &&
7293 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7294 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007295 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007296 continue;
7297 }
7298
7299 /* OK, so we know that either p2 points to the end of string or to a comma */
7300 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007301 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007302 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007303 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007304 return;
7305 }
7306
7307 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007308 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007309 continue;
7310 }
7311 }
7312}
7313
Willy Tarreau58f10d72006-12-04 02:26:12 +01007314
Willy Tarreaub2513902006-12-17 14:52:38 +01007315/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007316 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007317 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007318 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007319 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007320 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007321 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007322 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007323 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007324int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007325{
7326 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007327 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007328 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007329
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007330 if (!uri_auth)
7331 return 0;
7332
Cyril Bonté70be45d2010-10-12 00:14:35 +02007333 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007334 return 0;
7335
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007337 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007338 return 0;
7339
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007340 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007341 return 0;
7342
Willy Tarreaub2513902006-12-17 14:52:38 +01007343 return 1;
7344}
7345
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007346/* Append the description of what is present in error snapshot <es> into <out>.
7347 * The description must be small enough to always fit in a trash. The output
7348 * buffer may be the trash so the trash must not be used inside this function.
7349 */
7350void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7351{
7352 chunk_appendf(&trash,
7353 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7354 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7355 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7356 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7357 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7358 es->ctx.http.m_flags, es->ctx.http.m_clen,
7359 es->ctx.http.m_blen, es->ctx.http.b_flags);
7360}
7361
Willy Tarreau4076a152009-04-02 15:18:36 +02007362/*
7363 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007364 * By default it tries to report the error position as msg->err_pos. However if
7365 * this one is not set, it will then report msg->next, which is the last known
7366 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007367 * displays buffers as a contiguous area starting at buf->p. The direction is
7368 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007369 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007370void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007371 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007372 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007373{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007374 union error_snapshot_ctx ctx;
7375 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007376
Willy Tarreauef3ca732018-09-07 15:47:35 +02007377 /* http-specific part now */
7378 ctx.http.sid = s->uniq_id;
7379 ctx.http.state = state;
7380 ctx.http.b_flags = msg->chn->flags;
7381 ctx.http.s_flags = s->flags;
7382 ctx.http.t_flags = s->txn->flags;
7383 ctx.http.m_flags = msg->flags;
7384 ctx.http.m_clen = msg->chunk_len;
7385 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007386
Willy Tarreauef3ca732018-09-07 15:47:35 +02007387 ofs = msg->chn->total - ci_data(msg->chn);
7388 if (ofs < 0)
7389 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007390
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007391 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007392 other_end, s->target,
7393 strm_sess(s), &msg->chn->buf,
7394 ofs, co_data(msg->chn),
7395 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7396 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007397}
Willy Tarreaub2513902006-12-17 14:52:38 +01007398
Willy Tarreau294c4732011-12-16 21:35:50 +01007399/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7400 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7401 * performed over the whole headers. Otherwise it must contain a valid header
7402 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7403 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7404 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7405 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007406 * -1. The value fetch stops at commas, so this function is suited for use with
7407 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007408 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007409 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007410unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007411 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007412 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007413{
Willy Tarreau294c4732011-12-16 21:35:50 +01007414 struct hdr_ctx local_ctx;
7415 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007416 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007417 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007418 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007419
Willy Tarreau294c4732011-12-16 21:35:50 +01007420 if (!ctx) {
7421 local_ctx.idx = 0;
7422 ctx = &local_ctx;
7423 }
7424
Willy Tarreaubce70882009-09-07 11:51:47 +02007425 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007426 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007427 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007428 occ--;
7429 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007430 *vptr = ctx->line + ctx->val;
7431 *vlen = ctx->vlen;
7432 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007433 }
7434 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007435 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007436 }
7437
7438 /* negative occurrence, we scan all the list then walk back */
7439 if (-occ > MAX_HDR_HISTORY)
7440 return 0;
7441
Willy Tarreau294c4732011-12-16 21:35:50 +01007442 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007443 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007444 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7445 len_hist[hist_ptr] = ctx->vlen;
7446 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007447 hist_ptr = 0;
7448 found++;
7449 }
7450 if (-occ > found)
7451 return 0;
7452 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007453 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7454 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7455 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007456 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007457 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007458 if (hist_ptr >= MAX_HDR_HISTORY)
7459 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007460 *vptr = ptr_hist[hist_ptr];
7461 *vlen = len_hist[hist_ptr];
7462 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007463}
7464
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007465/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7466 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7467 * performed over the whole headers. Otherwise it must contain a valid header
7468 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7469 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7470 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7471 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7472 * -1. This function differs from http_get_hdr() in that it only returns full
7473 * line header values and does not stop at commas.
7474 * The return value is 0 if nothing was found, or non-zero otherwise.
7475 */
7476unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7477 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007478 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007479{
7480 struct hdr_ctx local_ctx;
7481 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007482 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007483 unsigned int hist_ptr;
7484 int found;
7485
7486 if (!ctx) {
7487 local_ctx.idx = 0;
7488 ctx = &local_ctx;
7489 }
7490
7491 if (occ >= 0) {
7492 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007493 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007494 occ--;
7495 if (occ <= 0) {
7496 *vptr = ctx->line + ctx->val;
7497 *vlen = ctx->vlen;
7498 return 1;
7499 }
7500 }
7501 return 0;
7502 }
7503
7504 /* negative occurrence, we scan all the list then walk back */
7505 if (-occ > MAX_HDR_HISTORY)
7506 return 0;
7507
7508 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007509 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007510 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7511 len_hist[hist_ptr] = ctx->vlen;
7512 if (++hist_ptr >= MAX_HDR_HISTORY)
7513 hist_ptr = 0;
7514 found++;
7515 }
7516 if (-occ > found)
7517 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007518
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007519 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007520 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7521 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7522 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007523 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007524 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007525 if (hist_ptr >= MAX_HDR_HISTORY)
7526 hist_ptr -= MAX_HDR_HISTORY;
7527 *vptr = ptr_hist[hist_ptr];
7528 *vlen = len_hist[hist_ptr];
7529 return 1;
7530}
7531
Willy Tarreaubaaee002006-06-26 02:48:02 +02007532/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007533 * Print a debug line with a header. Always stop at the first CR or LF char,
7534 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7535 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007536 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007537void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007538{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007539 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007540 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007541
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007542 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007543 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007544 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007545 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 +02007546
7547 for (max = 0; start + max < end; max++)
7548 if (start[max] == '\r' || start[max] == '\n')
7549 break;
7550
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007551 UBOUND(max, trash.size - trash.data - 3);
7552 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7553 trash.area[trash.data++] = '\n';
7554 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007555}
7556
Willy Tarreaueee5b512015-04-03 23:46:31 +02007557
7558/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7559 * The hdr_idx is allocated as well. In case of allocation failure, everything
7560 * allocated is freed and NULL is returned. Otherwise the new transaction is
7561 * assigned to the stream and returned.
7562 */
7563struct http_txn *http_alloc_txn(struct stream *s)
7564{
7565 struct http_txn *txn = s->txn;
7566
7567 if (txn)
7568 return txn;
7569
Willy Tarreaubafbe012017-11-24 17:34:44 +01007570 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007571 if (!txn)
7572 return txn;
7573
7574 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007575 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007576 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007577 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007578 return NULL;
7579 }
7580
7581 s->txn = txn;
7582 return txn;
7583}
7584
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007585void http_txn_reset_req(struct http_txn *txn)
7586{
7587 txn->req.flags = 0;
7588 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7589 txn->req.next = 0;
7590 txn->req.chunk_len = 0LL;
7591 txn->req.body_len = 0LL;
7592 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7593}
7594
7595void http_txn_reset_res(struct http_txn *txn)
7596{
7597 txn->rsp.flags = 0;
7598 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7599 txn->rsp.next = 0;
7600 txn->rsp.chunk_len = 0LL;
7601 txn->rsp.body_len = 0LL;
7602 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7603}
7604
Willy Tarreau0937bc42009-12-22 15:03:09 +01007605/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007606 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007607 * the required fields are properly allocated and that we only need to (re)init
7608 * them. This should be used before processing any new request.
7609 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007610void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007611{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007612 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007613 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007614
7615 txn->flags = 0;
7616 txn->status = -1;
7617
Willy Tarreauf64d1412010-10-07 20:06:11 +02007618 txn->cookie_first_date = 0;
7619 txn->cookie_last_date = 0;
7620
Willy Tarreaueee5b512015-04-03 23:46:31 +02007621 txn->srv_cookie = NULL;
7622 txn->cli_cookie = NULL;
7623 txn->uri = NULL;
7624
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007625 http_txn_reset_req(txn);
7626 http_txn_reset_res(txn);
7627
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007628 txn->req.chn = &s->req;
7629 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007630
7631 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007632
7633 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7634 if (fe->options2 & PR_O2_REQBUG_OK)
7635 txn->req.err_pos = -1; /* let buggy requests pass */
7636
Willy Tarreau0937bc42009-12-22 15:03:09 +01007637 if (txn->hdr_idx.v)
7638 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007639
7640 vars_init(&s->vars_txn, SCOPE_TXN);
7641 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007642}
7643
7644/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007645void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007646{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007647 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007648 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007649
7650 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007651 pool_free(pool_head_requri, txn->uri);
7652 pool_free(pool_head_capture, txn->cli_cookie);
7653 pool_free(pool_head_capture, txn->srv_cookie);
7654 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007655
William Lallemanda73203e2012-03-12 12:48:57 +01007656 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007657 txn->uri = NULL;
7658 txn->srv_cookie = NULL;
7659 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007660
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007661 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007662 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007663 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007664 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007665 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007666 }
7667
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007668 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007669 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007670 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007671 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007672 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007673 }
7674
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007675 if (!LIST_ISEMPTY(&s->vars_txn.head))
7676 vars_prune(&s->vars_txn, s->sess, s);
7677 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7678 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007679}
7680
7681/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007682void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007683{
7684 http_end_txn(s);
7685 http_init_txn(s);
7686
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007687 /* reinitialise the current rule list pointer to NULL. We are sure that
7688 * any rulelist match the NULL pointer.
7689 */
7690 s->current_rule_list = NULL;
7691
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007692 s->be = strm_fe(s);
7693 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007694 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007695 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007696 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007697 /* re-init store persistence */
7698 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007699 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007700
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007701 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007702
Willy Tarreau739cfba2010-01-25 23:11:14 +01007703 /* We must trim any excess data from the response buffer, because we
7704 * may have blocked an invalid response from a server that we don't
7705 * want to accidentely forward once we disable the analysers, nor do
7706 * we want those data to come along with next response. A typical
7707 * example of such data would be from a buggy server responding to
7708 * a HEAD with some data, or sending more than the advertised
7709 * content-length.
7710 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007711 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007712 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007713
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007714 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007715 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007716
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007717 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007718 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007719
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007720 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007721 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007722
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007723 s->req.rex = TICK_ETERNITY;
7724 s->req.wex = TICK_ETERNITY;
7725 s->req.analyse_exp = TICK_ETERNITY;
7726 s->res.rex = TICK_ETERNITY;
7727 s->res.wex = TICK_ETERNITY;
7728 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007729 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007730}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007731
Willy Tarreau79e57332018-10-02 16:01:16 +02007732/* This function executes one of the set-{method,path,query,uri} actions. It
7733 * takes the string from the variable 'replace' with length 'len', then modifies
7734 * the relevant part of the request line accordingly. Then it updates various
7735 * pointers to the next elements which were moved, and the total buffer length.
7736 * It finds the action to be performed in p[2], previously filled by function
7737 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7738 * error, though this can be revisited when this code is finally exploited.
7739 *
7740 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7741 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007742 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007743 * In query string case, the mark question '?' must be set at the start of the
7744 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007745 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007746int http_replace_req_line(int action, const char *replace, int len,
7747 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007748{
Willy Tarreau79e57332018-10-02 16:01:16 +02007749 struct http_txn *txn = s->txn;
7750 char *cur_ptr, *cur_end;
7751 int offset = 0;
7752 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007753
Willy Tarreau79e57332018-10-02 16:01:16 +02007754 switch (action) {
7755 case 0: // method
7756 cur_ptr = ci_head(&s->req);
7757 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007758
Willy Tarreau79e57332018-10-02 16:01:16 +02007759 /* adjust req line offsets and lengths */
7760 delta = len - offset - (cur_end - cur_ptr);
7761 txn->req.sl.rq.m_l += delta;
7762 txn->req.sl.rq.u += delta;
7763 txn->req.sl.rq.v += delta;
7764 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007765
Willy Tarreau79e57332018-10-02 16:01:16 +02007766 case 1: // path
7767 cur_ptr = http_txn_get_path(txn);
7768 if (!cur_ptr)
7769 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007770
Willy Tarreau79e57332018-10-02 16:01:16 +02007771 cur_end = cur_ptr;
7772 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7773 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007774
Willy Tarreau79e57332018-10-02 16:01:16 +02007775 /* adjust req line offsets and lengths */
7776 delta = len - offset - (cur_end - cur_ptr);
7777 txn->req.sl.rq.u_l += delta;
7778 txn->req.sl.rq.v += delta;
7779 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007780
Willy Tarreau79e57332018-10-02 16:01:16 +02007781 case 2: // query
7782 offset = 1;
7783 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7784 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7785 while (cur_ptr < cur_end && *cur_ptr != '?')
7786 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007787
Willy Tarreau79e57332018-10-02 16:01:16 +02007788 /* skip the question mark or indicate that we must insert it
7789 * (but only if the format string is not empty then).
7790 */
7791 if (cur_ptr < cur_end)
7792 cur_ptr++;
7793 else if (len > 1)
7794 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007795
Willy Tarreau79e57332018-10-02 16:01:16 +02007796 /* adjust req line offsets and lengths */
7797 delta = len - offset - (cur_end - cur_ptr);
7798 txn->req.sl.rq.u_l += delta;
7799 txn->req.sl.rq.v += delta;
7800 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007801
Willy Tarreau79e57332018-10-02 16:01:16 +02007802 case 3: // uri
7803 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7804 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007805
Willy Tarreau79e57332018-10-02 16:01:16 +02007806 /* adjust req line offsets and lengths */
7807 delta = len - offset - (cur_end - cur_ptr);
7808 txn->req.sl.rq.u_l += delta;
7809 txn->req.sl.rq.v += delta;
7810 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007811
Willy Tarreau79e57332018-10-02 16:01:16 +02007812 default:
7813 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007814 }
7815
Willy Tarreau79e57332018-10-02 16:01:16 +02007816 /* commit changes and adjust end of message */
7817 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7818 txn->req.sl.rq.l += delta;
7819 txn->hdr_idx.v[0].len += delta;
7820 http_msg_move_end(&txn->req, delta);
7821 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007822}
7823
Willy Tarreau79e57332018-10-02 16:01:16 +02007824/* This function replace the HTTP status code and the associated message. The
7825 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007826 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007827void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007828{
Willy Tarreau79e57332018-10-02 16:01:16 +02007829 struct http_txn *txn = s->txn;
7830 char *cur_ptr, *cur_end;
7831 int delta;
7832 char *res;
7833 int c_l;
7834 const char *msg = reason;
7835 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007836
Willy Tarreau79e57332018-10-02 16:01:16 +02007837 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007838
Willy Tarreau79e57332018-10-02 16:01:16 +02007839 res = ultoa_o(status, trash.area, trash.size);
7840 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007841
Willy Tarreau79e57332018-10-02 16:01:16 +02007842 trash.area[c_l] = ' ';
7843 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007844
Willy Tarreau79e57332018-10-02 16:01:16 +02007845 /* Do we have a custom reason format string? */
7846 if (msg == NULL)
7847 msg = http_get_reason(status);
7848 msg_len = strlen(msg);
7849 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7850 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007851
Willy Tarreau79e57332018-10-02 16:01:16 +02007852 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7853 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007854
Willy Tarreau79e57332018-10-02 16:01:16 +02007855 /* commit changes and adjust message */
7856 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7857 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007858
Willy Tarreau79e57332018-10-02 16:01:16 +02007859 /* adjust res line offsets and lengths */
7860 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7861 txn->rsp.sl.st.c_l = c_l;
7862 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007863
Willy Tarreau79e57332018-10-02 16:01:16 +02007864 delta = trash.data - (cur_end - cur_ptr);
7865 txn->rsp.sl.st.l += delta;
7866 txn->hdr_idx.v[0].len += delta;
7867 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007868}
7869
Willy Tarreau8797c062007-05-07 00:55:35 +02007870__attribute__((constructor))
7871static void __http_protocol_init(void)
7872{
Willy Tarreau8797c062007-05-07 00:55:35 +02007873}
7874
7875
Willy Tarreau58f10d72006-12-04 02:26:12 +01007876/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007877 * Local variables:
7878 * c-indent-level: 8
7879 * c-basic-offset: 8
7880 * End:
7881 */