blob: a8a1728a8d4ef486ab2d1de802148c3456b81dce [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
Willy Tarreau87b09662015-04-03 00:22:06 +02001722/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001723 * transaction <txn>. Returns the verdict of the first rule that prevents
1724 * further processing of the request (auth, deny, ...), and defaults to
1725 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1726 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001727 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1728 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1729 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001730 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001731enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001732http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001733{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001734 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001735 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001736 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001737 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001738 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001739 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02001740 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001741 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001742
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001743 /* If "the current_rule_list" match the executed rule list, we are in
1744 * resume condition. If a resume is needed it is always in the action
1745 * and never in the ACL or converters. In this case, we initialise the
1746 * current rule, and go to the action execution point.
1747 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001748 if (s->current_rule) {
1749 rule = s->current_rule;
1750 s->current_rule = NULL;
1751 if (s->current_rule_list == rules)
1752 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001753 }
1754 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001755
Willy Tarreauff011f22011-01-06 17:51:27 +01001756 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001757
Willy Tarreau96257ec2012-12-27 10:46:37 +01001758 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001759 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001760 int ret;
1761
Willy Tarreau192252e2015-04-04 01:47:55 +02001762 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001763 ret = acl_pass(ret);
1764
Willy Tarreauff011f22011-01-06 17:51:27 +01001765 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001766 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001767
1768 if (!ret) /* condition not matched */
1769 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001770 }
1771
Willy Tarreauacc98002015-09-27 23:34:39 +02001772 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001773resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001774 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001775 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02001776 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001777
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001778 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001779 if (deny_status)
1780 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001781 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001782
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001783 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001784 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001785 if (deny_status)
1786 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001787 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001788
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001789 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02001790 /* Auth might be performed on regular http-req rules as well as on stats */
1791 auth_realm = rule->arg.auth.realm;
1792 if (!auth_realm) {
1793 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1794 auth_realm = STATS_DEFAULT_REALM;
1795 else
1796 auth_realm = px->id;
1797 }
1798 /* send 401/407 depending on whether we use a proxy or not. We still
1799 * count one error, because normal browsing won't significantly
1800 * increase the counter but brute force attempts will.
1801 */
1802 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1803 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001804 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001805 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02001806 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001807
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001808 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02001809 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
1810 return HTTP_RULE_RES_BADREQ;
1811 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001812
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001813 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001814 s->task->nice = rule->arg.nice;
1815 break;
1816
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001817 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001818 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001819 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001820 break;
1821
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001822 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001823#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001824 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001825 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001826#endif
1827 break;
1828
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001829 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001830 s->logs.level = rule->arg.loglevel;
1831 break;
1832
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001833 case ACT_HTTP_REPLACE_HDR:
1834 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001835 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1836 rule->arg.hdr_add.name_len,
1837 &rule->arg.hdr_add.fmt,
1838 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06001839 return HTTP_RULE_RES_BADREQ;
1840 break;
1841
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001842 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001843 ctx.idx = 0;
1844 /* remove all occurrences of the header */
1845 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001846 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001847 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001848 }
Willy Tarreau85603282015-01-21 20:39:27 +01001849 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001850
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001851 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001852 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001853 /* The scope of the trash buffer must be limited to this function. The
1854 * build_logline() function can execute a lot of other function which
1855 * can use the trash buffer. So for limiting the scope of this global
1856 * buffer, we build first the header value using build_logline, and
1857 * after we store the header name.
1858 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001859 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001860
1861 replace = alloc_trash_chunk();
1862 if (!replace)
1863 return HTTP_RULE_RES_BADREQ;
1864
Thierry Fournier4b788f72016-06-01 13:35:36 +02001865 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001866 len += build_logline(s, replace->area + len,
1867 replace->size - len,
1868 &rule->arg.hdr_add.fmt);
1869 memcpy(replace->area, rule->arg.hdr_add.name,
1870 rule->arg.hdr_add.name_len);
1871 replace->area[rule->arg.hdr_add.name_len] = ':';
1872 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1873 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001874
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001875 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001876 /* remove all occurrences of the header */
1877 ctx.idx = 0;
1878 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001879 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001880 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1881 }
1882 }
1883
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001884 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001885 static unsigned char rate_limit = 0;
1886
1887 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001888 replace->area[rule->arg.hdr_add.name_len] = 0;
1889 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,
1890 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001891 }
1892
1893 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1894 if (sess->fe != s->be)
1895 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1896 if (sess->listener->counters)
1897 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1898 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001899
1900 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001901 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001902 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001903
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001904 case ACT_HTTP_DEL_ACL:
1905 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001906 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001907 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001908
1909 /* collect reference */
1910 ref = pat_ref_lookup(rule->arg.map.ref);
1911 if (!ref)
1912 continue;
1913
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001914 /* allocate key */
1915 key = alloc_trash_chunk();
1916 if (!key)
1917 return HTTP_RULE_RES_BADREQ;
1918
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001919 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001920 key->data = build_logline(s, key->area, key->size,
1921 &rule->arg.map.key);
1922 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001923
1924 /* perform update */
1925 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001926 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001927 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001928 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001929
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001930 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001931 break;
1932 }
1933
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001934 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001935 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001936 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001937
1938 /* collect reference */
1939 ref = pat_ref_lookup(rule->arg.map.ref);
1940 if (!ref)
1941 continue;
1942
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001943 /* allocate key */
1944 key = alloc_trash_chunk();
1945 if (!key)
1946 return HTTP_RULE_RES_BADREQ;
1947
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001948 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001949 key->data = build_logline(s, key->area, key->size,
1950 &rule->arg.map.key);
1951 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001952
1953 /* perform update */
1954 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001955 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001956 if (pat_ref_find_elt(ref, key->area) == NULL)
1957 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001958 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001959
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001960 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001961 break;
1962 }
1963
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001964 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001965 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001966 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001967
1968 /* collect reference */
1969 ref = pat_ref_lookup(rule->arg.map.ref);
1970 if (!ref)
1971 continue;
1972
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001973 /* allocate key */
1974 key = alloc_trash_chunk();
1975 if (!key)
1976 return HTTP_RULE_RES_BADREQ;
1977
1978 /* allocate value */
1979 value = alloc_trash_chunk();
1980 if (!value) {
1981 free_trash_chunk(key);
1982 return HTTP_RULE_RES_BADREQ;
1983 }
1984
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001985 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001986 key->data = build_logline(s, key->area, key->size,
1987 &rule->arg.map.key);
1988 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001989
1990 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001991 value->data = build_logline(s, value->area,
1992 value->size,
1993 &rule->arg.map.value);
1994 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001995
1996 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001997 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001998 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001999 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002000 else
2001 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002002 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002003
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002004 free_trash_chunk(key);
2005 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002006 break;
2007 }
William Lallemand73025dd2014-04-24 14:38:37 +02002008
Thierry FOURNIER42148732015-09-02 17:17:33 +02002009 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002010 if ((s->req.flags & CF_READ_ERROR) ||
2011 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2012 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2013 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002014 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002015
Willy Tarreauacc98002015-09-27 23:34:39 +02002016 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002017 case ACT_RET_ERR:
2018 case ACT_RET_CONT:
2019 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002020 case ACT_RET_STOP:
2021 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002022 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002023 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002024 return HTTP_RULE_RES_YIELD;
2025 }
William Lallemand73025dd2014-04-24 14:38:37 +02002026 break;
2027
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002028 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002029 /* Note: only the first valid tracking parameter of each
2030 * applies.
2031 */
2032
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002033 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002034 struct stktable *t;
2035 struct stksess *ts;
2036 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002037 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002038
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002039 t = rule->arg.trk_ctr.table.t;
2040 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 +02002041
2042 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002043 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002044
2045 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002046 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2047 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2048 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002049 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002050
Emeric Brun819fc6f2017-06-13 19:37:32 +02002051 if (ptr1)
2052 stktable_data_cast(ptr1, http_req_cnt)++;
2053
2054 if (ptr2)
2055 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2056 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2057
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002058 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002059
2060 /* If data was modified, we need to touch to re-schedule sync */
2061 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002062 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002063
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002064 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002065 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002066 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002067 }
2068 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002069 break;
2070
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002071 /* other flags exists, but normaly, they never be matched. */
2072 default:
2073 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002074 }
2075 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002076
2077 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002078 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002079}
2080
Willy Tarreau71241ab2012-12-27 11:30:54 +01002081
Willy Tarreau51d861a2015-05-22 17:30:48 +02002082/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2083 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2084 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2085 * is returned, the process can continue the evaluation of next rule list. If
2086 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2087 * is returned, it means the operation could not be processed and a server error
2088 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2089 * deny rule. If *YIELD is returned, the caller must call again the function
2090 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002091 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002092enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002093http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002094{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002095 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002096 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002097 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002098 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002099 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002100 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002101
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002102 /* If "the current_rule_list" match the executed rule list, we are in
2103 * resume condition. If a resume is needed it is always in the action
2104 * and never in the ACL or converters. In this case, we initialise the
2105 * current rule, and go to the action execution point.
2106 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002107 if (s->current_rule) {
2108 rule = s->current_rule;
2109 s->current_rule = NULL;
2110 if (s->current_rule_list == rules)
2111 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002112 }
2113 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002114
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002115 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002116
2117 /* check optional condition */
2118 if (rule->cond) {
2119 int ret;
2120
Willy Tarreau192252e2015-04-04 01:47:55 +02002121 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002122 ret = acl_pass(ret);
2123
2124 if (rule->cond->pol == ACL_COND_UNLESS)
2125 ret = !ret;
2126
2127 if (!ret) /* condition not matched */
2128 continue;
2129 }
2130
Willy Tarreauacc98002015-09-27 23:34:39 +02002131 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002132resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002133 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002134 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002135 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002136
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002137 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002138 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002139 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002140
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002141 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002142 s->task->nice = rule->arg.nice;
2143 break;
2144
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002145 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002146 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002147 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002148 break;
2149
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002150 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002151#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002152 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002153 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002154#endif
2155 break;
2156
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002157 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002158 s->logs.level = rule->arg.loglevel;
2159 break;
2160
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002161 case ACT_HTTP_REPLACE_HDR:
2162 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002163 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2164 rule->arg.hdr_add.name_len,
2165 &rule->arg.hdr_add.fmt,
2166 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002167 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002168 break;
2169
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002170 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002171 ctx.idx = 0;
2172 /* remove all occurrences of the header */
2173 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002174 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002175 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2176 }
Willy Tarreau85603282015-01-21 20:39:27 +01002177 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002178
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002179 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002180 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002181 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002182
2183 replace = alloc_trash_chunk();
2184 if (!replace)
2185 return HTTP_RULE_RES_BADREQ;
2186
2187 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002188 memcpy(replace->area, rule->arg.hdr_add.name,
2189 rule->arg.hdr_add.name_len);
2190 replace->data = rule->arg.hdr_add.name_len;
2191 replace->area[replace->data++] = ':';
2192 replace->area[replace->data++] = ' ';
2193 replace->data += build_logline(s,
2194 replace->area + replace->data,
2195 replace->size - replace->data,
2196 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002197
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002198 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002199 /* remove all occurrences of the header */
2200 ctx.idx = 0;
2201 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002202 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002203 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2204 }
2205 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002206
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002207 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002208 static unsigned char rate_limit = 0;
2209
2210 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002211 replace->area[rule->arg.hdr_add.name_len] = 0;
2212 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,
2213 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002214 }
2215
2216 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2217 if (sess->fe != s->be)
2218 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2219 if (sess->listener->counters)
2220 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2221 if (objt_server(s->target))
2222 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2223 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002224
2225 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002226 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002227 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002228
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002229 case ACT_HTTP_DEL_ACL:
2230 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002231 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002232 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002233
2234 /* collect reference */
2235 ref = pat_ref_lookup(rule->arg.map.ref);
2236 if (!ref)
2237 continue;
2238
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002239 /* allocate key */
2240 key = alloc_trash_chunk();
2241 if (!key)
2242 return HTTP_RULE_RES_BADREQ;
2243
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002244 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002245 key->data = build_logline(s, key->area, key->size,
2246 &rule->arg.map.key);
2247 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002248
2249 /* perform update */
2250 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002251 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002252 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002253 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002254
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002255 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002256 break;
2257 }
2258
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002259 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002260 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002261 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002262
2263 /* collect reference */
2264 ref = pat_ref_lookup(rule->arg.map.ref);
2265 if (!ref)
2266 continue;
2267
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002268 /* allocate key */
2269 key = alloc_trash_chunk();
2270 if (!key)
2271 return HTTP_RULE_RES_BADREQ;
2272
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002273 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002274 key->data = build_logline(s, key->area, key->size,
2275 &rule->arg.map.key);
2276 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002277
2278 /* perform update */
2279 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002280 if (pat_ref_find_elt(ref, key->area) == NULL)
2281 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002282
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002283 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002284 break;
2285 }
2286
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002287 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002288 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002289 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002290
2291 /* collect reference */
2292 ref = pat_ref_lookup(rule->arg.map.ref);
2293 if (!ref)
2294 continue;
2295
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002296 /* allocate key */
2297 key = alloc_trash_chunk();
2298 if (!key)
2299 return HTTP_RULE_RES_BADREQ;
2300
2301 /* allocate value */
2302 value = alloc_trash_chunk();
2303 if (!value) {
2304 free_trash_chunk(key);
2305 return HTTP_RULE_RES_BADREQ;
2306 }
2307
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002308 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002309 key->data = build_logline(s, key->area, key->size,
2310 &rule->arg.map.key);
2311 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002312
2313 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002314 value->data = build_logline(s, value->area,
2315 value->size,
2316 &rule->arg.map.value);
2317 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002318
2319 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002320 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002321 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002322 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002323 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002324 else
2325 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002326 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002327 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002328 free_trash_chunk(key);
2329 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002330 break;
2331 }
William Lallemand73025dd2014-04-24 14:38:37 +02002332
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002333 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02002334 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2335 return HTTP_RULE_RES_BADREQ;
2336 return HTTP_RULE_RES_DONE;
2337
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002338 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2339 /* Note: only the first valid tracking parameter of each
2340 * applies.
2341 */
2342
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002343 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002344 struct stktable *t;
2345 struct stksess *ts;
2346 struct stktable_key *key;
2347 void *ptr;
2348
2349 t = rule->arg.trk_ctr.table.t;
2350 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2351
2352 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002353 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002354
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002355 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002356
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002357 /* let's count a new HTTP request as it's the first time we do it */
2358 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2359 if (ptr)
2360 stktable_data_cast(ptr, http_req_cnt)++;
2361
2362 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2363 if (ptr)
2364 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2365 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2366
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002367 /* When the client triggers a 4xx from the server, it's most often due
2368 * to a missing object or permission. These events should be tracked
2369 * because if they happen often, it may indicate a brute force or a
2370 * vulnerability scan. Normally this is done when receiving the response
2371 * but here we're tracking after this ought to have been done so we have
2372 * to do it on purpose.
2373 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002374 if ((unsigned)(txn->status - 400) < 100) {
2375 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2376 if (ptr)
2377 stktable_data_cast(ptr, http_err_cnt)++;
2378
2379 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2380 if (ptr)
2381 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2382 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2383 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002384
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002385 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002386
Emeric Brun0fed0b02017-11-29 16:15:07 +01002387 /* If data was modified, we need to touch to re-schedule sync */
2388 stktable_touch_local(t, ts, 0);
2389
Emeric Brun819fc6f2017-06-13 19:37:32 +02002390 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2391 if (sess->fe != s->be)
2392 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2393
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002394 }
2395 }
2396 break;
2397
Thierry FOURNIER42148732015-09-02 17:17:33 +02002398 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002399 if ((s->req.flags & CF_READ_ERROR) ||
2400 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2401 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2402 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002403 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002404
Willy Tarreauacc98002015-09-27 23:34:39 +02002405 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002406 case ACT_RET_ERR:
2407 case ACT_RET_CONT:
2408 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002409 case ACT_RET_STOP:
2410 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002411 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002412 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002413 return HTTP_RULE_RES_YIELD;
2414 }
William Lallemand73025dd2014-04-24 14:38:37 +02002415 break;
2416
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002417 /* other flags exists, but normaly, they never be matched. */
2418 default:
2419 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002420 }
2421 }
2422
2423 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002424 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002425}
2426
2427
Willy Tarreau71241ab2012-12-27 11:30:54 +01002428/* Perform an HTTP redirect based on the information in <rule>. The function
2429 * returns non-zero on success, or zero in case of a, irrecoverable error such
2430 * as too large a request to build a valid response.
2431 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002432int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002433{
Willy Tarreaub329a312015-05-22 16:27:37 +02002434 struct http_msg *req = &txn->req;
2435 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002436 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002437 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002438 int ret = 0;
2439
2440 chunk = alloc_trash_chunk();
2441 if (!chunk)
2442 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002443
2444 /* build redirect message */
2445 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002446 case 308:
2447 msg_fmt = HTTP_308;
2448 break;
2449 case 307:
2450 msg_fmt = HTTP_307;
2451 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002452 case 303:
2453 msg_fmt = HTTP_303;
2454 break;
2455 case 301:
2456 msg_fmt = HTTP_301;
2457 break;
2458 case 302:
2459 default:
2460 msg_fmt = HTTP_302;
2461 break;
2462 }
2463
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002464 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2465 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002466
2467 switch(rule->type) {
2468 case REDIRECT_TYPE_SCHEME: {
2469 const char *path;
2470 const char *host;
2471 struct hdr_ctx ctx;
2472 int pathlen;
2473 int hostlen;
2474
2475 host = "";
2476 hostlen = 0;
2477 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002478 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002479 host = ctx.line + ctx.val;
2480 hostlen = ctx.vlen;
2481 }
2482
Willy Tarreau6b952c82018-09-10 17:45:34 +02002483 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002484 /* build message using path */
2485 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002486 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002487 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2488 int qs = 0;
2489 while (qs < pathlen) {
2490 if (path[qs] == '?') {
2491 pathlen = qs;
2492 break;
2493 }
2494 qs++;
2495 }
2496 }
2497 } else {
2498 path = "/";
2499 pathlen = 1;
2500 }
2501
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002502 if (rule->rdr_str) { /* this is an old "redirect" rule */
2503 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002504 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002505 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002506
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002507 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002508 memcpy(chunk->area + chunk->data, rule->rdr_str,
2509 rule->rdr_len);
2510 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002511 }
2512 else {
2513 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002514 chunk->data += build_logline(s,
2515 chunk->area + chunk->data,
2516 chunk->size - chunk->data,
2517 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002518
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002519 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002520 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002521 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002522 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002523 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002524 memcpy(chunk->area + chunk->data, "://", 3);
2525 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002526
2527 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002528 memcpy(chunk->area + chunk->data, host, hostlen);
2529 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002530
2531 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002532 memcpy(chunk->area + chunk->data, path, pathlen);
2533 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002534
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002535 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002536 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002537 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002538 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002539 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002540 chunk->area[chunk->data] = '/';
2541 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002542 }
2543
2544 break;
2545 }
2546 case REDIRECT_TYPE_PREFIX: {
2547 const char *path;
2548 int pathlen;
2549
Willy Tarreau6b952c82018-09-10 17:45:34 +02002550 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002551 /* build message using path */
2552 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002553 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002554 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2555 int qs = 0;
2556 while (qs < pathlen) {
2557 if (path[qs] == '?') {
2558 pathlen = qs;
2559 break;
2560 }
2561 qs++;
2562 }
2563 }
2564 } else {
2565 path = "/";
2566 pathlen = 1;
2567 }
2568
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002569 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002570 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002571 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002572
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002573 /* add prefix. Note that if prefix == "/", we don't want to
2574 * add anything, otherwise it makes it hard for the user to
2575 * configure a self-redirection.
2576 */
2577 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002578 memcpy(chunk->area + chunk->data,
2579 rule->rdr_str, rule->rdr_len);
2580 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002581 }
2582 }
2583 else {
2584 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002585 chunk->data += build_logline(s,
2586 chunk->area + chunk->data,
2587 chunk->size - chunk->data,
2588 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002589
2590 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002591 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002592 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002593 }
2594
2595 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002596 memcpy(chunk->area + chunk->data, path, pathlen);
2597 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002598
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002599 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002600 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002601 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002602 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002603 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002604 chunk->area[chunk->data] = '/';
2605 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002606 }
2607
2608 break;
2609 }
2610 case REDIRECT_TYPE_LOCATION:
2611 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002612 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002613 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002614 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002615
2616 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002617 memcpy(chunk->area + chunk->data, rule->rdr_str,
2618 rule->rdr_len);
2619 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002620 }
2621 else {
2622 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002623 chunk->data += build_logline(s,
2624 chunk->area + chunk->data,
2625 chunk->size - chunk->data,
2626 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002627
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002628 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002629 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002630 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002631 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002632 break;
2633 }
2634
2635 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002636 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2637 chunk->data += 14;
2638 memcpy(chunk->area + chunk->data, rule->cookie_str,
2639 rule->cookie_len);
2640 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002641 }
2642
Willy Tarreau19b14122017-02-28 09:48:11 +01002643 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002644 txn->status = rule->code;
2645 /* let's log the request time */
2646 s->logs.tv_request = now;
2647
Christopher Fauletbe821b92017-03-30 11:21:53 +02002648 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002649 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2650 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2651 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002652 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002653 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002654 memcpy(chunk->area + chunk->data,
2655 "\r\nProxy-Connection: keep-alive", 30);
2656 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002657 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002658 memcpy(chunk->area + chunk->data,
2659 "\r\nConnection: keep-alive", 24);
2660 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002661 }
2662 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002663 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2664 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002665 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002666 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002667 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002668 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002669 req->next -= req->sov;
2670 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002671 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002672 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002673 req->msg_state = HTTP_MSG_CLOSED;
2674 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002675 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002676 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002677 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002678 /* let the server side turn to SI_ST_CLO */
2679 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002680 } else {
2681 /* keep-alive not possible */
2682 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002683 memcpy(chunk->area + chunk->data,
2684 "\r\nProxy-Connection: close\r\n\r\n", 29);
2685 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002686 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002687 memcpy(chunk->area + chunk->data,
2688 "\r\nConnection: close\r\n\r\n", 23);
2689 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002690 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002691 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002692 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002693 }
2694
Willy Tarreaue7dff022015-04-03 01:14:29 +02002695 if (!(s->flags & SF_ERR_MASK))
2696 s->flags |= SF_ERR_LOCAL;
2697 if (!(s->flags & SF_FINST_MASK))
2698 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002699
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002700 ret = 1;
2701 leave:
2702 free_trash_chunk(chunk);
2703 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002704}
2705
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002706/* This stream analyser runs all HTTP request processing which is common to
2707 * frontends and backends, which means blocking ACLs, filters, connection-close,
2708 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002709 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002710 * either needs more data or wants to immediately abort the request (eg: deny,
2711 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002712 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002713int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002714{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002715 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002716 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002717 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002718 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002719 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002720 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002721 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002722 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002723
Willy Tarreau655dce92009-11-08 13:10:58 +01002724 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002725 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002726 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002727 }
2728
Christopher Faulet45073512018-07-20 10:16:29 +02002729 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 +02002730 now_ms, __FUNCTION__,
2731 s,
2732 req,
2733 req->rex, req->wex,
2734 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002735 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002736 req->analysers);
2737
Willy Tarreau65410832014-04-28 21:25:43 +02002738 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002739 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002740
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002741 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002742 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002743 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002744
Willy Tarreau0b748332014-04-29 00:13:29 +02002745 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002746 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2747 goto return_prx_yield;
2748
Willy Tarreau0b748332014-04-29 00:13:29 +02002749 case HTTP_RULE_RES_CONT:
2750 case HTTP_RULE_RES_STOP: /* nothing to do */
2751 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002752
Willy Tarreau0b748332014-04-29 00:13:29 +02002753 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2754 if (txn->flags & TX_CLTARPIT)
2755 goto tarpit;
2756 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002757
Willy Tarreau0b748332014-04-29 00:13:29 +02002758 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2759 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002760
Willy Tarreau0b748332014-04-29 00:13:29 +02002761 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002762 goto done;
2763
Willy Tarreau0b748332014-04-29 00:13:29 +02002764 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2765 goto return_bad_req;
2766 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002767 }
2768
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002769 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2770 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002771 struct hdr_ctx ctx;
2772
2773 ctx.idx = 0;
2774 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002775 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002776 if (unlikely(http_header_add_tail2(&txn->req,
2777 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002778 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002779 goto return_bad_req;
2780 }
2781 }
2782
2783 }
2784
Willy Tarreau52542592014-04-28 18:33:26 +02002785 /* OK at this stage, we know that the request was accepted according to
2786 * the http-request rules, we can check for the stats. Note that the
2787 * URI is detected *before* the req* rules in order not to be affected
2788 * by a possible reqrep, while they are processed *after* so that a
2789 * reqdeny can still block them. This clearly needs to change in 1.6!
2790 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002791 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002792 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002793 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002794 txn->status = 500;
2795 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002796 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002797
Willy Tarreaue7dff022015-04-03 01:14:29 +02002798 if (!(s->flags & SF_ERR_MASK))
2799 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002800 goto return_prx_cond;
2801 }
2802
2803 /* parse the whole stats request and extract the relevant information */
2804 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002805 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002806 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002807
Willy Tarreau0b748332014-04-29 00:13:29 +02002808 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2809 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002810
Willy Tarreau0b748332014-04-29 00:13:29 +02002811 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2812 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002813 }
2814
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002815 /* evaluate the req* rules except reqadd */
2816 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002817 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002818 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002819
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002820 if (txn->flags & TX_CLDENY)
2821 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002822
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002823 if (txn->flags & TX_CLTARPIT) {
2824 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002825 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002826 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002827 }
Willy Tarreau06619262006-12-17 08:37:22 +01002828
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002829 /* add request headers from the rule sets in the same order */
2830 list_for_each_entry(wl, &px->req_add, list) {
2831 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002832 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002833 ret = acl_pass(ret);
2834 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2835 ret = !ret;
2836 if (!ret)
2837 continue;
2838 }
2839
Christopher Faulet10079f52018-10-03 15:17:28 +02002840 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002841 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002842 }
2843
Willy Tarreau52542592014-04-28 18:33:26 +02002844
2845 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002846 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2847 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002848 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002849 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002850 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002851
Willy Tarreaue7dff022015-04-03 01:14:29 +02002852 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2853 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2854 if (!(s->flags & SF_FINST_MASK))
2855 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002856
Willy Tarreau70730dd2014-04-24 18:06:27 +02002857 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002858 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2859 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002860 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002861 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002862 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002863
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002864 /* check whether we have some ACLs set to redirect this request */
2865 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002866 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002867 int ret;
2868
Willy Tarreau192252e2015-04-04 01:47:55 +02002869 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002870 ret = acl_pass(ret);
2871 if (rule->cond->pol == ACL_COND_UNLESS)
2872 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002873 if (!ret)
2874 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002875 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002876 if (!http_apply_redirect_rule(rule, s, txn))
2877 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002878 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002879 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002880
Willy Tarreau2be39392010-01-03 17:24:51 +01002881 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2882 * If this happens, then the data will not come immediately, so we must
2883 * send all what we have without waiting. Note that due to the small gain
2884 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002885 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002886 * itself once used.
2887 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002888 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002889
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002890 done: /* done with this analyser, continue with next ones that the calling
2891 * points will have set, if any.
2892 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002893 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002894 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
2895 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002896 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002897
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002898 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02002899 /* Allow cookie logging
2900 */
2901 if (s->be->cookie_name || sess->fe->capture_name)
2902 manage_client_side_cookies(s, req);
2903
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002904 /* When a connection is tarpitted, we use the tarpit timeout,
2905 * which may be the same as the connect timeout if unspecified.
2906 * If unset, then set it to zero because we really want it to
2907 * eventually expire. We build the tarpit as an analyser.
2908 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002909 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002910
2911 /* wipe the request out so that we can drop the connection early
2912 * if the client closes first.
2913 */
2914 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002915
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002916 txn->status = http_err_codes[deny_status];
2917
Christopher Faulet0184ea72017-01-05 14:06:34 +01002918 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002919 req->analysers |= AN_REQ_HTTP_TARPIT;
2920 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2921 if (!req->analyse_exp)
2922 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02002923 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002924 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002925 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002926 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002927 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002928 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002929 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002930
2931 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002932
2933 /* Allow cookie logging
2934 */
2935 if (s->be->cookie_name || sess->fe->capture_name)
2936 manage_client_side_cookies(s, req);
2937
Willy Tarreau0b748332014-04-29 00:13:29 +02002938 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002939 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002940 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002941 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02002942 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002943 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002944 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002945 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002946 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002947 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002948 goto return_prx_cond;
2949
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002950 return_bad_req:
2951 /* We centralize bad requests processing here */
2952 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2953 /* we detected a parsing error. We want to archive this request
2954 * in the dedicated proxy area for later troubleshooting.
2955 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02002956 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002957 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002958
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002959 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002960 txn->req.msg_state = HTTP_MSG_ERROR;
2961 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002962 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002963
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002964 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002965 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002966 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002967
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002968 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002969 if (!(s->flags & SF_ERR_MASK))
2970 s->flags |= SF_ERR_PRXCOND;
2971 if (!(s->flags & SF_FINST_MASK))
2972 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002973
Christopher Faulet0184ea72017-01-05 14:06:34 +01002974 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002975 req->analyse_exp = TICK_ETERNITY;
2976 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002977
2978 return_prx_yield:
2979 channel_dont_connect(req);
2980 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002981}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002982
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002983/* This function performs all the processing enabled for the current request.
2984 * It returns 1 if the processing can continue on next analysers, or zero if it
2985 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002986 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002987 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002988int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002989{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002990 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002991 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002992 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02002993 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002994
Willy Tarreau655dce92009-11-08 13:10:58 +01002995 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002996 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002997 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002998 return 0;
2999 }
3000
Christopher Faulet45073512018-07-20 10:16:29 +02003001 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 +02003002 now_ms, __FUNCTION__,
3003 s,
3004 req,
3005 req->rex, req->wex,
3006 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003007 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003008 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003009
Willy Tarreau59234e92008-11-30 23:51:27 +01003010 /*
3011 * Right now, we know that we have processed the entire headers
3012 * and that unwanted requests have been filtered out. We can do
3013 * whatever we want with the remaining request. Also, now we
3014 * may have separate values for ->fe, ->be.
3015 */
Willy Tarreau06619262006-12-17 08:37:22 +01003016
Willy Tarreau59234e92008-11-30 23:51:27 +01003017 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003018 * If HTTP PROXY is set we simply get remote server address parsing
3019 * incoming request. Note that this requires that a connection is
3020 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003021 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003022 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003023 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003024 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003025
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003026 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003027 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003028 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003029 txn->req.msg_state = HTTP_MSG_ERROR;
3030 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003031 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003032 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003033
Willy Tarreaue7dff022015-04-03 01:14:29 +02003034 if (!(s->flags & SF_ERR_MASK))
3035 s->flags |= SF_ERR_RESOURCE;
3036 if (!(s->flags & SF_FINST_MASK))
3037 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003038
3039 return 0;
3040 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003041
Willy Tarreau6b952c82018-09-10 17:45:34 +02003042 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003043 if (url2sa(ci_head(req) + msg->sl.rq.u,
3044 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003045 &conn->addr.to, NULL) == -1)
3046 goto return_bad_req;
3047
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003048 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003049 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3050 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003051 * u_l characters by a single "/".
3052 */
3053 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003054 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003055 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3056 int delta;
3057
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003058 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003059 http_msg_move_end(&txn->req, delta);
3060 cur_end += delta;
3061 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3062 goto return_bad_req;
3063 }
3064 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003065 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003066 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3067 int delta;
3068
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003069 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003070 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003071 http_msg_move_end(&txn->req, delta);
3072 cur_end += delta;
3073 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3074 goto return_bad_req;
3075 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003076 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003077
Willy Tarreau59234e92008-11-30 23:51:27 +01003078 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003079 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003080 * Note that doing so might move headers in the request, but
3081 * the fields will stay coherent and the URI will not move.
3082 * This should only be performed in the backend.
3083 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003084 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003085 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003086
William Lallemanda73203e2012-03-12 12:48:57 +01003087 /* add unique-id if "header-unique-id" is specified */
3088
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003089 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003090 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003091 goto return_bad_req;
3092 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003093 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003094 }
William Lallemanda73203e2012-03-12 12:48:57 +01003095
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003096 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003097 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003098 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003099 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003100 goto return_bad_req;
3101 }
3102
Cyril Bontéb21570a2009-11-29 20:04:48 +01003103 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003104 * 9: add X-Forwarded-For if either the frontend or the backend
3105 * asks for it.
3106 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003107 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003108 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003109 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3110 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3111 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003112 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003113 /* The header is set to be added only if none is present
3114 * and we found it, so don't do anything.
3115 */
3116 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003117 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003118 /* Add an X-Forwarded-For header unless the source IP is
3119 * in the 'except' network range.
3120 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003121 if ((!sess->fe->except_mask.s_addr ||
3122 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3123 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003124 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003125 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003126 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003127 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003128 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003129 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003130
3131 /* Note: we rely on the backend to get the header name to be used for
3132 * x-forwarded-for, because the header is really meant for the backends.
3133 * However, if the backend did not specify any option, we have to rely
3134 * on the frontend's header name.
3135 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003136 if (s->be->fwdfor_hdr_len) {
3137 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003138 memcpy(trash.area,
3139 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003140 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003141 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003142 memcpy(trash.area,
3143 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003144 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003145 len += snprintf(trash.area + len,
3146 trash.size - len,
3147 ": %d.%d.%d.%d", pn[0], pn[1],
3148 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003149
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003150 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003151 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003152 }
3153 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003154 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003155 /* FIXME: for the sake of completeness, we should also support
3156 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003157 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003158 int len;
3159 char pn[INET6_ADDRSTRLEN];
3160 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003161 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003162 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003163
Willy Tarreau59234e92008-11-30 23:51:27 +01003164 /* Note: we rely on the backend to get the header name to be used for
3165 * x-forwarded-for, because the header is really meant for the backends.
3166 * However, if the backend did not specify any option, we have to rely
3167 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003168 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003169 if (s->be->fwdfor_hdr_len) {
3170 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003171 memcpy(trash.area, s->be->fwdfor_hdr_name,
3172 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003173 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003174 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003175 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3176 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003177 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003178 len += snprintf(trash.area + len, trash.size - len,
3179 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003180
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003181 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003182 goto return_bad_req;
3183 }
3184 }
3185
3186 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003187 * 10: add X-Original-To if either the frontend or the backend
3188 * asks for it.
3189 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003190 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003191
3192 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003193 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003194 /* Add an X-Original-To header unless the destination IP is
3195 * in the 'except' network range.
3196 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003197 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003198
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003199 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003200 ((!sess->fe->except_mask_to.s_addr ||
3201 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3202 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003203 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003204 (((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 +02003205 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003206 int len;
3207 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003208 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003209
3210 /* Note: we rely on the backend to get the header name to be used for
3211 * x-original-to, because the header is really meant for the backends.
3212 * However, if the backend did not specify any option, we have to rely
3213 * on the frontend's header name.
3214 */
3215 if (s->be->orgto_hdr_len) {
3216 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003217 memcpy(trash.area,
3218 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003219 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003220 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003221 memcpy(trash.area,
3222 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003223 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003224 len += snprintf(trash.area + len,
3225 trash.size - len,
3226 ": %d.%d.%d.%d", pn[0], pn[1],
3227 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003228
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003229 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003230 goto return_bad_req;
3231 }
3232 }
3233 }
3234
Willy Tarreau50fc7772012-11-11 22:19:57 +01003235 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3236 * If an "Upgrade" token is found, the header is left untouched in order not to have
3237 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3238 * "Upgrade" is present in the Connection header.
3239 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003240 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003241 unsigned int want_flags = 0;
3242
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003243 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003244 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003245 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003246 want_flags |= TX_CON_CLO_SET;
3247 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003248 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003249 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003250 want_flags |= TX_CON_KAL_SET;
3251 }
3252
3253 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003254 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003255 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003256
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003257
Willy Tarreau522d6c02009-12-06 18:49:18 +01003258 /* If we have no server assigned yet and we're balancing on url_param
3259 * with a POST request, we may be interested in checking the body for
3260 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003261 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003262 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003263 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003264 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003265 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003266 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003267 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003268
Christopher Fauletbe821b92017-03-30 11:21:53 +02003269 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3270 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003271#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003272 /* We expect some data from the client. Unless we know for sure
3273 * we already have a full request, we have to re-enable quick-ack
3274 * in case we previously disabled it, otherwise we might cause
3275 * the client to delay further data.
3276 */
3277 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3278 cli_conn && conn_ctrl_ready(cli_conn) &&
3279 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003280 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003281 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003282#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003283
Willy Tarreau59234e92008-11-30 23:51:27 +01003284 /*************************************************************
3285 * OK, that's finished for the headers. We have done what we *
3286 * could. Let's switch to the DATA state. *
3287 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003288 req->analyse_exp = TICK_ETERNITY;
3289 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003290
Willy Tarreau59234e92008-11-30 23:51:27 +01003291 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003292 /* OK let's go on with the BODY now */
3293 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003294
Willy Tarreau59234e92008-11-30 23:51:27 +01003295 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003296 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003297 /* we detected a parsing error. We want to archive this request
3298 * in the dedicated proxy area for later troubleshooting.
3299 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003300 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003301 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003302
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003303 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003304 txn->req.msg_state = HTTP_MSG_ERROR;
3305 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003306 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003307 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003308
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003309 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003310 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003311 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003312
Willy Tarreaue7dff022015-04-03 01:14:29 +02003313 if (!(s->flags & SF_ERR_MASK))
3314 s->flags |= SF_ERR_PRXCOND;
3315 if (!(s->flags & SF_FINST_MASK))
3316 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003317 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003318}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003319
Willy Tarreau60b85b02008-11-30 23:28:40 +01003320/* This function is an analyser which processes the HTTP tarpit. It always
3321 * returns zero, at the beginning because it prevents any other processing
3322 * from occurring, and at the end because it terminates the request.
3323 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003324int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003325{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003326 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003327
3328 /* This connection is being tarpitted. The CLIENT side has
3329 * already set the connect expiration date to the right
3330 * timeout. We just have to check that the client is still
3331 * there and that the timeout has not expired.
3332 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003333 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003334 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003335 !tick_is_expired(req->analyse_exp, now_ms))
3336 return 0;
3337
3338 /* We will set the queue timer to the time spent, just for
3339 * logging purposes. We fake a 500 server error, so that the
3340 * attacker will not suspect his connection has been tarpitted.
3341 * It will not cause trouble to the logs because we can exclude
3342 * the tarpitted connections by filtering on the 'PT' status flags.
3343 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003344 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3345
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003346 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003347 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003348
Christopher Faulet0184ea72017-01-05 14:06:34 +01003349 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003350 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003351
Willy Tarreaue7dff022015-04-03 01:14:29 +02003352 if (!(s->flags & SF_ERR_MASK))
3353 s->flags |= SF_ERR_PRXCOND;
3354 if (!(s->flags & SF_FINST_MASK))
3355 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003356 return 0;
3357}
3358
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003359/* This function is an analyser which waits for the HTTP request body. It waits
3360 * for either the buffer to be full, or the full advertised contents to have
3361 * reached the buffer. It must only be called after the standard HTTP request
3362 * processing has occurred, because it expects the request to be parsed and will
3363 * look for the Expect header. It may send a 100-Continue interim response. It
3364 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3365 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3366 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003367 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003368int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003369{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003370 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003371 struct http_txn *txn = s->txn;
3372 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003373
3374 /* We have to parse the HTTP request body to find any required data.
3375 * "balance url_param check_post" should have been the only way to get
3376 * into this. We were brought here after HTTP header analysis, so all
3377 * related structures are ready.
3378 */
3379
Willy Tarreau890988f2014-04-10 11:59:33 +02003380 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3381 /* This is the first call */
3382 if (msg->msg_state < HTTP_MSG_BODY)
3383 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003384
Willy Tarreau890988f2014-04-10 11:59:33 +02003385 if (msg->msg_state < HTTP_MSG_100_SENT) {
3386 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3387 * send an HTTP/1.1 100 Continue intermediate response.
3388 */
3389 if (msg->flags & HTTP_MSGF_VER_11) {
3390 struct hdr_ctx ctx;
3391 ctx.idx = 0;
3392 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003393 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003394 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003395 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003396 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003397 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003398 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003399 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003400 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003401
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003402 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003403 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003404 * must save the body in msg->next because it survives buffer
3405 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003406 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003407 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003408
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003409 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003410 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3411 else
3412 msg->msg_state = HTTP_MSG_DATA;
3413 }
3414
Willy Tarreau890988f2014-04-10 11:59:33 +02003415 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3416 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003417 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003418 goto missing_data;
3419
3420 /* OK we have everything we need now */
3421 goto http_end;
3422 }
3423
3424 /* OK here we're parsing a chunked-encoded message */
3425
Willy Tarreau522d6c02009-12-06 18:49:18 +01003426 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003427 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003428 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003429 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003430 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003431 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003432 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003433
Willy Tarreau115acb92009-12-26 13:56:06 +01003434 if (!ret)
3435 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003436 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003437 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003438 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003439 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003440 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003441 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003442 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003443
3444 msg->chunk_len = chunk;
3445 msg->body_len += chunk;
3446
3447 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003448 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003449 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003450 }
3451
Willy Tarreaud98cf932009-12-27 22:54:55 +01003452 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003453 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3454 * for at least a whole chunk or the whole content length bytes after
3455 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003456 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003457 if (msg->msg_state == HTTP_MSG_TRAILERS)
3458 goto http_end;
3459
Willy Tarreaue115b492015-05-01 23:05:14 +02003460 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003461 goto http_end;
3462
3463 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003464 /* we get here if we need to wait for more data. If the buffer is full,
3465 * we have the maximum we can expect.
3466 */
Willy Tarreau23752332018-06-15 14:54:53 +02003467 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003468 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003469
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003470 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003471 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003472 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003473
Willy Tarreaue7dff022015-04-03 01:14:29 +02003474 if (!(s->flags & SF_ERR_MASK))
3475 s->flags |= SF_ERR_CLITO;
3476 if (!(s->flags & SF_FINST_MASK))
3477 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003478 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003479 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003480
3481 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003482 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003483 /* Not enough data. We'll re-use the http-request
3484 * timeout here. Ideally, we should set the timeout
3485 * relative to the accept() date. We just set the
3486 * request timeout once at the beginning of the
3487 * request.
3488 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003489 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003490 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003491 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003492 return 0;
3493 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003494
3495 http_end:
3496 /* The situation will not evolve, so let's give up on the analysis. */
3497 s->logs.tv_request = now; /* update the request timer to reflect full request */
3498 req->analysers &= ~an_bit;
3499 req->analyse_exp = TICK_ETERNITY;
3500 return 1;
3501
3502 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003503 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003504 txn->req.msg_state = HTTP_MSG_ERROR;
3505 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003506 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003507
Willy Tarreaue7dff022015-04-03 01:14:29 +02003508 if (!(s->flags & SF_ERR_MASK))
3509 s->flags |= SF_ERR_PRXCOND;
3510 if (!(s->flags & SF_FINST_MASK))
3511 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003512
Willy Tarreau522d6c02009-12-06 18:49:18 +01003513 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003514 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003515 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003516 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003517 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003518 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003519}
3520
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003521/* send a server's name with an outgoing request over an established connection.
3522 * Note: this function is designed to be called once the request has been scheduled
3523 * for being forwarded. This is the reason why it rewinds the buffer before
3524 * proceeding.
3525 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003526int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003527
3528 struct hdr_ctx ctx;
3529
Mark Lamourinec2247f02012-01-04 13:02:01 -05003530 char *hdr_name = be->server_id_hdr_name;
3531 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003532 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003533 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003534 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003535
William Lallemandd9e90662012-01-30 17:27:17 +01003536 ctx.idx = 0;
3537
Willy Tarreau211cdec2014-04-17 20:18:08 +02003538 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003539 if (old_o) {
3540 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003541 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003542 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003543 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003544 }
3545
Willy Tarreauf37954d2018-06-15 18:31:02 +02003546 old_i = ci_data(chn);
3547 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 -05003548 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003549 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003550 }
3551
3552 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003553 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003554 memcpy(hdr_val, hdr_name, hdr_name_len);
3555 hdr_val += hdr_name_len;
3556 *hdr_val++ = ':';
3557 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003558 hdr_val += strlcpy2(hdr_val, srv_name,
3559 trash.area + trash.size - hdr_val);
3560 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3561 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003562
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003563 if (old_o) {
3564 /* If this was a forwarded request, we must readjust the amount of
3565 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003566 * variations. Note that the current state is >= HTTP_MSG_BODY,
3567 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003568 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003569 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003570 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003571 txn->req.next -= old_o;
3572 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003573 }
3574
Mark Lamourinec2247f02012-01-04 13:02:01 -05003575 return 0;
3576}
3577
Willy Tarreau610ecce2010-01-04 21:15:02 +01003578/* Terminate current transaction and prepare a new one. This is very tricky
3579 * right now but it works.
3580 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003581void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003582{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003583 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003584 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003585 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003586 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003587 struct connection *srv_conn;
3588 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003589 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003590
Willy Tarreau610ecce2010-01-04 21:15:02 +01003591 /* FIXME: We need a more portable way of releasing a backend's and a
3592 * server's connections. We need a safer way to reinitialize buffer
3593 * flags. We also need a more accurate method for computing per-request
3594 * data.
3595 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003596 /*
3597 * XXX cognet: This is probably wrong, this is killing a whole
3598 * connection, in the new world order, we probably want to just kill
3599 * the stream, this is to be revisited the day we handle multiple
3600 * streams in one server connection.
3601 */
3602 cs = objt_cs(s->si[1].end);
3603 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003604
Willy Tarreau4213a112013-12-15 10:25:42 +01003605 /* unless we're doing keep-alive, we want to quickly close the connection
3606 * to the server.
3607 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003608 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003609 !si_conn_ready(&s->si[1])) {
3610 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3611 si_shutr(&s->si[1]);
3612 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003613 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003614
Willy Tarreaue7dff022015-04-03 01:14:29 +02003615 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003616 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003617 if (unlikely(s->srv_conn))
3618 sess_change_server(s, NULL);
3619 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003620
3621 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003622 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003623
Willy Tarreaueee5b512015-04-03 23:46:31 +02003624 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003625 int n;
3626
Willy Tarreaueee5b512015-04-03 23:46:31 +02003627 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003628 if (n < 1 || n > 5)
3629 n = 0;
3630
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003631 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003632 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003633 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003634 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003635 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003636 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3637 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003638 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003639 }
3640
3641 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003642 s->logs.bytes_in -= ci_data(&s->req);
3643 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003644
Willy Tarreau66425e32018-07-25 06:55:12 +02003645 /* we may need to know the position in the queue */
3646 pendconn_free(s);
3647
Willy Tarreau610ecce2010-01-04 21:15:02 +01003648 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003649 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003650 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003651 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003652 s->do_log(s);
3653 }
3654
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003655 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003656 stream_stop_content_counters(s);
3657 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003658
Willy Tarreau610ecce2010-01-04 21:15:02 +01003659 s->logs.accept_date = date; /* user-visible date for logging */
3660 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003661 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3662 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003663 tv_zero(&s->logs.tv_request);
3664 s->logs.t_queue = -1;
3665 s->logs.t_connect = -1;
3666 s->logs.t_data = -1;
3667 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003668 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3669 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003670
Willy Tarreauf37954d2018-06-15 18:31:02 +02003671 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3672 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003673
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003674 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003675 if (s->flags & SF_CURR_SESS) {
3676 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003677 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003678 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003679 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003680 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003681 }
3682
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003683 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003684
Willy Tarreau4213a112013-12-15 10:25:42 +01003685 /* only release our endpoint if we don't intend to reuse the
3686 * connection.
3687 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003688 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003689 !si_conn_ready(&s->si[1])) {
3690 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02003691 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003692 }
3693
Willy Tarreau350f4872014-11-28 14:42:25 +01003694 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3695 s->si[1].err_type = SI_ET_NONE;
3696 s->si[1].conn_retries = 0; /* used for logging too */
3697 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003698 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 +01003699 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 +02003700 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 +02003701 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3702 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3703 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003704
Patrick Hemmere3faf022018-08-22 10:02:00 -04003705 hlua_ctx_destroy(s->hlua);
3706 s->hlua = NULL;
3707
Willy Tarreaueee5b512015-04-03 23:46:31 +02003708 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003709 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003710 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003711
3712 if (prev_status == 401 || prev_status == 407) {
3713 /* In HTTP keep-alive mode, if we receive a 401, we still have
3714 * a chance of being able to send the visitor again to the same
3715 * server over the same connection. This is required by some
3716 * broken protocols such as NTLM, and anyway whenever there is
3717 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003718 * it's better to do it (at least it helps with debugging), at
3719 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003720 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003721 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003722 }
3723
Willy Tarreau53f96852016-02-02 18:50:47 +01003724 /* Never ever allow to reuse a connection from a non-reuse backend */
3725 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3726 srv_conn->flags |= CO_FL_PRIVATE;
3727
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003728 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003729 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003730
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003731 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003732 s->req.flags |= CF_NEVER_WAIT;
3733 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003734 }
3735
Willy Tarreau714ea782015-11-25 20:11:11 +01003736 /* we're removing the analysers, we MUST re-enable events detection.
3737 * We don't enable close on the response channel since it's either
3738 * already closed, or in keep-alive with an idle connection handler.
3739 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003740 channel_auto_read(&s->req);
3741 channel_auto_close(&s->req);
3742 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003743
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003744 /* we're in keep-alive with an idle connection, monitor it if not already done */
3745 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003746 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02003747 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003748 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01003749 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003750 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02003751 else if (prev_flags & TX_NOT_FIRST)
3752 /* note: we check the request, not the connection, but
3753 * this is valid for strategies SAFE and AGGR, and in
3754 * case of ALWS, we don't care anyway.
3755 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003756 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02003757 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003758 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003759 }
Christopher Faulete6006242017-03-10 11:52:44 +01003760 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3761 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003762}
3763
3764
3765/* This function updates the request state machine according to the response
3766 * state machine and buffer flags. It returns 1 if it changes anything (flag
3767 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3768 * it is only used to find when a request/response couple is complete. Both
3769 * this function and its equivalent should loop until both return zero. It
3770 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3771 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003772int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003773{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003774 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003775 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003776 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003777 unsigned int old_state = txn->req.msg_state;
3778
Christopher Faulet4be98032017-07-18 10:48:24 +02003779 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003780 return 0;
3781
3782 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003783 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003784 * We can shut the read side unless we want to abort_on_close,
3785 * or we have a POST request. The issue with POST requests is
3786 * that some browsers still send a CRLF after the request, and
3787 * this CRLF must be read so that it does not remain in the kernel
3788 * buffers, otherwise a close could cause an RST on some systems
3789 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003790 * Note that if we're using keep-alive on the client side, we'd
3791 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003792 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003793 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003794 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003795 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3796 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003797 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3798 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003799 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003800 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003801
Willy Tarreau40f151a2012-12-20 12:10:09 +01003802 /* if the server closes the connection, we want to immediately react
3803 * and close the socket to save packets and syscalls.
3804 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003805 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003806
Willy Tarreau7f876a12015-11-18 11:59:55 +01003807 /* In any case we've finished parsing the request so we must
3808 * disable Nagle when sending data because 1) we're not going
3809 * to shut this side, and 2) the server is waiting for us to
3810 * send pending data.
3811 */
3812 chn->flags |= CF_NEVER_WAIT;
3813
Willy Tarreau610ecce2010-01-04 21:15:02 +01003814 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3815 goto wait_other_side;
3816
3817 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3818 /* The server has not finished to respond, so we
3819 * don't want to move in order not to upset it.
3820 */
3821 goto wait_other_side;
3822 }
3823
Willy Tarreau610ecce2010-01-04 21:15:02 +01003824 /* When we get here, it means that both the request and the
3825 * response have finished receiving. Depending on the connection
3826 * mode, we'll have to wait for the last bytes to leave in either
3827 * direction, and sometimes for a close to be effective.
3828 */
3829
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003830 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3831 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003832 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3833 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003834 }
3835 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3836 /* Option forceclose is set, or either side wants to close,
3837 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003838 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003839 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003840 *
3841 * However, there is an exception if the response
3842 * length is undefined. In this case, we need to wait
3843 * the close from the server. The response will be
3844 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003845 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003846 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3847 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3848 goto check_channel_flags;
3849
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003850 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3851 channel_shutr_now(chn);
3852 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003853 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003854 }
3855 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003856 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3857 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003858 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003859 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3860 channel_auto_read(chn);
3861 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003862 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003863 }
3864
Christopher Faulet4be98032017-07-18 10:48:24 +02003865 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003866 }
3867
3868 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3869 http_msg_closing:
3870 /* nothing else to forward, just waiting for the output buffer
3871 * to be empty and for the shutw_now to take effect.
3872 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003873 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003874 txn->req.msg_state = HTTP_MSG_CLOSED;
3875 goto http_msg_closed;
3876 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003877 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003878 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003879 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003880 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003881 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003882 }
3883
3884 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3885 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01003886 /* if we don't know whether the server will close, we need to hard close */
3887 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
3888 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3889
Willy Tarreau3988d932013-12-27 23:03:08 +01003890 /* see above in MSG_DONE why we only do this in these states */
3891 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3892 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003893 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3894 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01003895 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003896 goto wait_other_side;
3897 }
3898
Christopher Faulet4be98032017-07-18 10:48:24 +02003899 check_channel_flags:
3900 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3901 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3902 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02003903 txn->req.msg_state = HTTP_MSG_CLOSING;
3904 goto http_msg_closing;
3905 }
3906
3907
Willy Tarreau610ecce2010-01-04 21:15:02 +01003908 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003909 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003910}
3911
3912
3913/* This function updates the response state machine according to the request
3914 * state machine and buffer flags. It returns 1 if it changes anything (flag
3915 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3916 * it is only used to find when a request/response couple is complete. Both
3917 * this function and its equivalent should loop until both return zero. It
3918 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3919 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003920int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003921{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003922 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003923 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003924 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003925 unsigned int old_state = txn->rsp.msg_state;
3926
Christopher Faulet4be98032017-07-18 10:48:24 +02003927 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003928 return 0;
3929
3930 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3931 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003932 * still monitor the server connection for a possible close
3933 * while the request is being uploaded, so we don't disable
3934 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003935 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003936 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003937
3938 if (txn->req.msg_state == HTTP_MSG_ERROR)
3939 goto wait_other_side;
3940
3941 if (txn->req.msg_state < HTTP_MSG_DONE) {
3942 /* The client seems to still be sending data, probably
3943 * because we got an error response during an upload.
3944 * We have the choice of either breaking the connection
3945 * or letting it pass through. Let's do the later.
3946 */
3947 goto wait_other_side;
3948 }
3949
Willy Tarreau610ecce2010-01-04 21:15:02 +01003950 /* When we get here, it means that both the request and the
3951 * response have finished receiving. Depending on the connection
3952 * mode, we'll have to wait for the last bytes to leave in either
3953 * direction, and sometimes for a close to be effective.
3954 */
3955
3956 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3957 /* Server-close mode : shut read and wait for the request
3958 * side to close its output buffer. The caller will detect
3959 * when we're in DONE and the other is in CLOSED and will
3960 * catch that for the final cleanup.
3961 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003962 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
3963 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003964 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003965 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3966 /* Option forceclose is set, or either side wants to close,
3967 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003968 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003969 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003970 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01003971 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003972 channel_shutr_now(chn);
3973 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003974 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003975 }
3976 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003977 /* The last possible modes are keep-alive and tunnel. Tunnel will
3978 * need to forward remaining data. Keep-alive will need to monitor
3979 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003980 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003981 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02003982 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01003983 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
3984 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003985 }
3986
Christopher Faulet4be98032017-07-18 10:48:24 +02003987 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003988 }
3989
3990 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3991 http_msg_closing:
3992 /* nothing else to forward, just waiting for the output buffer
3993 * to be empty and for the shutw_now to take effect.
3994 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003995 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003996 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3997 goto http_msg_closed;
3998 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003999 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004000 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004001 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004002 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004003 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004004 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004005 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004006 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004007 }
4008
4009 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4010 http_msg_closed:
4011 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004012 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004013 channel_auto_close(chn);
4014 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004015 goto wait_other_side;
4016 }
4017
Christopher Faulet4be98032017-07-18 10:48:24 +02004018 check_channel_flags:
4019 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4020 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4021 /* if we've just closed an output, let's switch */
4022 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4023 goto http_msg_closing;
4024 }
4025
Willy Tarreau610ecce2010-01-04 21:15:02 +01004026 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004027 /* We force the response to leave immediately if we're waiting for the
4028 * other side, since there is no pending shutdown to push it out.
4029 */
4030 if (!channel_is_empty(chn))
4031 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004032 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033}
4034
4035
Christopher Faulet894da4c2017-07-18 11:29:07 +02004036/* Resync the request and response state machines. */
4037void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004038{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004039 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004040#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004041 int old_req_state = txn->req.msg_state;
4042 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004043#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004044
Willy Tarreau610ecce2010-01-04 21:15:02 +01004045 http_sync_req_state(s);
4046 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004047 if (!http_sync_res_state(s))
4048 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004049 if (!http_sync_req_state(s))
4050 break;
4051 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004052
Christopher Faulet894da4c2017-07-18 11:29:07 +02004053 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4054 "req->analysers=0x%08x res->analysers=0x%08x\n",
4055 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004056 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4057 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004058 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004059
4060
Willy Tarreau610ecce2010-01-04 21:15:02 +01004061 /* OK, both state machines agree on a compatible state.
4062 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004063 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4064 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004065 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4066 * means we must abort the request.
4067 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4068 * corresponding channel.
4069 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4070 * on the response with server-close mode means we've completed one
4071 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004072 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004073 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4074 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004075 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004076 channel_auto_close(&s->req);
4077 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004078 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004079 channel_auto_close(&s->res);
4080 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004081 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004082 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4083 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004084 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004085 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004086 channel_auto_close(&s->res);
4087 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004088 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004089 channel_abort(&s->req);
4090 channel_auto_close(&s->req);
4091 channel_auto_read(&s->req);
4092 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004093 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004094 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4095 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4096 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4097 s->req.analysers &= AN_REQ_FLT_END;
4098 if (HAS_REQ_DATA_FILTERS(s))
4099 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4100 }
4101 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4102 s->res.analysers &= AN_RES_FLT_END;
4103 if (HAS_RSP_DATA_FILTERS(s))
4104 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4105 }
4106 channel_auto_close(&s->req);
4107 channel_auto_read(&s->req);
4108 channel_auto_close(&s->res);
4109 channel_auto_read(&s->res);
4110 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004111 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4112 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004113 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004114 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4115 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4116 /* server-close/keep-alive: terminate this transaction,
4117 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004118 * a fresh-new transaction, but only once we're sure there's
4119 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004120 * another request. They must not hold any pending output data
4121 * and the response buffer must realigned
4122 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004123 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004124 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004125 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004126 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004127 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004128 else {
4129 s->req.analysers = AN_REQ_FLT_END;
4130 s->res.analysers = AN_RES_FLT_END;
4131 txn->flags |= TX_WAIT_CLEANUP;
4132 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004133 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004134}
4135
Willy Tarreaud98cf932009-12-27 22:54:55 +01004136/* This function is an analyser which forwards request body (including chunk
4137 * sizes if any). It is called as soon as we must forward, even if we forward
4138 * zero byte. The only situation where it must not be called is when we're in
4139 * tunnel mode and we want to forward till the close. It's used both to forward
4140 * remaining data and to resync after end of body. It expects the msg_state to
4141 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004142 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004143 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004144 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004145 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004146int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004147{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004148 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004149 struct http_txn *txn = s->txn;
4150 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004151 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004152
Christopher Faulet45073512018-07-20 10:16:29 +02004153 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 +02004154 now_ms, __FUNCTION__,
4155 s,
4156 req,
4157 req->rex, req->wex,
4158 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004159 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004160 req->analysers);
4161
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004162 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4163 return 0;
4164
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004165 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004166 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004167 /* Output closed while we were sending data. We must abort and
4168 * wake the other side up.
4169 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004170 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004171 msg->msg_state = HTTP_MSG_ERROR;
4172 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004173 return 1;
4174 }
4175
Willy Tarreaud98cf932009-12-27 22:54:55 +01004176 /* Note that we don't have to send 100-continue back because we don't
4177 * need the data to complete our job, and it's up to the server to
4178 * decide whether to return 100, 417 or anything else in return of
4179 * an "Expect: 100-continue" header.
4180 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004181 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004182 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4183 ? HTTP_MSG_CHUNK_SIZE
4184 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004185
4186 /* TODO/filters: when http-buffer-request option is set or if a
4187 * rule on url_param exists, the first chunk size could be
4188 * already parsed. In that case, msg->next is after the chunk
4189 * size (including the CRLF after the size). So this case should
4190 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004191 }
4192
Willy Tarreau7ba23542014-04-17 21:50:00 +02004193 /* Some post-connect processing might want us to refrain from starting to
4194 * forward data. Currently, the only reason for this is "balance url_param"
4195 * whichs need to parse/process the request after we've enabled forwarding.
4196 */
4197 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004198 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004199 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004200 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004201 channel_dont_close(req); /* don't fail on early shutr */
4202 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004203 }
4204 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4205 }
4206
Willy Tarreau80a92c02014-03-12 10:41:13 +01004207 /* in most states, we should abort in case of early close */
4208 channel_auto_close(req);
4209
Willy Tarreauefdf0942014-04-24 20:08:57 +02004210 if (req->to_forward) {
4211 /* We can't process the buffer's contents yet */
4212 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004213 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004214 }
4215
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004216 if (msg->msg_state < HTTP_MSG_DONE) {
4217 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4218 ? http_msg_forward_chunked_body(s, msg)
4219 : http_msg_forward_body(s, msg));
4220 if (!ret)
4221 goto missing_data_or_waiting;
4222 if (ret < 0)
4223 goto return_bad_req;
4224 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004225
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004226 /* other states, DONE...TUNNEL */
4227 /* we don't want to forward closes on DONE except in tunnel mode. */
4228 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4229 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004230
Christopher Faulet894da4c2017-07-18 11:29:07 +02004231 http_resync_states(s);
4232 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004233 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4234 if (req->flags & CF_SHUTW) {
4235 /* request errors are most likely due to the
4236 * server aborting the transfer. */
4237 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004238 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004239 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004240 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004241 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004242 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004243 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004244 }
4245
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004246 /* If "option abortonclose" is set on the backend, we want to monitor
4247 * the client's connection and forward any shutdown notification to the
4248 * server, which will decide whether to close or to go on processing the
4249 * request. We only do that in tunnel mode, and not in other modes since
4250 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004251 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004252 channel_auto_read(req);
4253 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4254 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4255 s->si[1].flags |= SI_FL_NOLINGER;
4256 channel_auto_close(req);
4257 }
4258 else if (s->txn->meth == HTTP_METH_POST) {
4259 /* POST requests may require to read extra CRLF sent by broken
4260 * browsers and which could cause an RST to be sent upon close
4261 * on some systems (eg: Linux). */
4262 channel_auto_read(req);
4263 }
4264 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004265
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004266 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004267 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004268 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004269 if (!(s->flags & SF_ERR_MASK))
4270 s->flags |= SF_ERR_CLICL;
4271 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004272 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004273 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004274 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004275 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004276 }
4277
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004278 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4279 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004280 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004281 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004282
4283 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004284 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004285
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004286 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004287 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004288 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004289 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004290
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004291 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004292 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004293 * And when content-length is used, we never want to let the possible
4294 * shutdown be forwarded to the other side, as the state machine will
4295 * take care of it once the client responds. It's also important to
4296 * prevent TIME_WAITs from accumulating on the backend side, and for
4297 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004298 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004299 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004300 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004301
Willy Tarreau5c620922011-05-11 19:56:11 +02004302 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004303 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004304 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004305 * modes are already handled by the stream sock layer. We must not do
4306 * this in content-length mode because it could present the MSG_MORE
4307 * flag with the last block of forwarded data, which would cause an
4308 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004309 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004310 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004311 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004312
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004313 return 0;
4314
Willy Tarreaud98cf932009-12-27 22:54:55 +01004315 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004316 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004317 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004318 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004319
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004320 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004321 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004322 txn->req.msg_state = HTTP_MSG_ERROR;
4323 if (txn->status) {
4324 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004325 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004326 } else {
4327 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004328 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004329 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004330 req->analysers &= AN_REQ_FLT_END;
4331 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 +01004332
Willy Tarreaue7dff022015-04-03 01:14:29 +02004333 if (!(s->flags & SF_ERR_MASK))
4334 s->flags |= SF_ERR_PRXCOND;
4335 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004336 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004337 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004338 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004339 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004340 }
4341 return 0;
4342
4343 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004344 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004345 txn->req.msg_state = HTTP_MSG_ERROR;
4346 if (txn->status) {
4347 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004348 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004349 } else {
4350 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004351 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004352 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004353 req->analysers &= AN_REQ_FLT_END;
4354 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 +01004355
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004356 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4357 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004358 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004359 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004360
Willy Tarreaue7dff022015-04-03 01:14:29 +02004361 if (!(s->flags & SF_ERR_MASK))
4362 s->flags |= SF_ERR_SRVCL;
4363 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004364 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004365 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004366 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004367 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004368 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004369 return 0;
4370}
4371
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004372/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4373 * processing can continue on next analysers, or zero if it either needs more
4374 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004375 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004376 * when it has nothing left to do, and may remove any analyser when it wants to
4377 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004378 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004379int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004380{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004381 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004382 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004383 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004384 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004385 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004386 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004387 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004388 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004389
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004390 srv_conn = cs_conn(objt_cs(s->si[1].end));
4391
Christopher Faulet45073512018-07-20 10:16:29 +02004392 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 +02004393 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004394 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004395 rep,
4396 rep->rex, rep->wex,
4397 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004398 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004399 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004400
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004401 /*
4402 * Now parse the partial (or complete) lines.
4403 * We will check the response syntax, and also join multi-line
4404 * headers. An index of all the lines will be elaborated while
4405 * parsing.
4406 *
4407 * For the parsing, we use a 28 states FSM.
4408 *
4409 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004410 * ci_head(rep) = beginning of response
4411 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4412 * ci_tail(rep) = end of input data
4413 * msg->eol = end of current header or line (LF or CRLF)
4414 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004415 */
4416
Willy Tarreau628c40c2014-04-24 19:11:26 +02004417 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004418 /* There's a protected area at the end of the buffer for rewriting
4419 * purposes. We don't want to start to parse the request if the
4420 * protected area is affected, because we may have to move processed
4421 * data later, which is much more complicated.
4422 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004423 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004424 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004425 /* some data has still not left the buffer, wake us once that's done */
4426 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4427 goto abort_response;
4428 channel_dont_close(rep);
4429 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004430 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004431 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004432 }
4433
Willy Tarreau188e2302018-06-15 11:11:53 +02004434 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004435 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004436 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004437
Willy Tarreauf37954d2018-06-15 18:31:02 +02004438 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004439 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004440 }
4441
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004442 /* 1: we might have to print this header in debug mode */
4443 if (unlikely((global.mode & MODE_DEBUG) &&
4444 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004445 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004446 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004447
Willy Tarreauf37954d2018-06-15 18:31:02 +02004448 sol = ci_head(rep);
4449 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004450 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004451
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004452 sol += hdr_idx_first_pos(&txn->hdr_idx);
4453 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004454
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004455 while (cur_idx) {
4456 eol = sol + txn->hdr_idx.v[cur_idx].len;
4457 debug_hdr("srvhdr", s, sol, eol);
4458 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4459 cur_idx = txn->hdr_idx.v[cur_idx].next;
4460 }
4461 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004462
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004463 /*
4464 * Now we quickly check if we have found a full valid response.
4465 * If not so, we check the FD and buffer states before leaving.
4466 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004467 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004468 * responses are checked first.
4469 *
4470 * Depending on whether the client is still there or not, we
4471 * may send an error response back or not. Note that normally
4472 * we should only check for HTTP status there, and check I/O
4473 * errors somewhere else.
4474 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004475
Willy Tarreau655dce92009-11-08 13:10:58 +01004476 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004477 /* Invalid response */
4478 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4479 /* we detected a parsing error. We want to archive this response
4480 * in the dedicated proxy area for later troubleshooting.
4481 */
4482 hdr_response_bad:
4483 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004484 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004485
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004486 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004487 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004488 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004489 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004490 }
Willy Tarreau64648412010-03-05 10:41:54 +01004491 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004492 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004493 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004494 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004495 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004496 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004497 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004498
Willy Tarreaue7dff022015-04-03 01:14:29 +02004499 if (!(s->flags & SF_ERR_MASK))
4500 s->flags |= SF_ERR_PRXCOND;
4501 if (!(s->flags & SF_FINST_MASK))
4502 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004503
4504 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004505 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004506
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004507 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004508 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004509 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004510 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004511 goto hdr_response_bad;
4512 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004513
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004514 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004515 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004516 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004517 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004518 else if (txn->flags & TX_NOT_FIRST)
4519 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004520
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004521 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004522 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004523 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004524 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004525 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004526
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004527 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004528 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004529 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004530
4531 /* Check to see if the server refused the early data.
4532 * If so, just send a 425
4533 */
4534 if (objt_cs(s->si[1].end)) {
4535 struct connection *conn = objt_cs(s->si[1].end)->conn;
4536
4537 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4538 txn->status = 425;
4539 }
4540
Willy Tarreau350f4872014-11-28 14:42:25 +01004541 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004542 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004543 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004544
Willy Tarreaue7dff022015-04-03 01:14:29 +02004545 if (!(s->flags & SF_ERR_MASK))
4546 s->flags |= SF_ERR_SRVCL;
4547 if (!(s->flags & SF_FINST_MASK))
4548 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004549 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004550 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004551
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004552 /* read timeout : return a 504 to the client. */
4553 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004554 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004555 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004556
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004557 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004558 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004559 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004560 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004561 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004562
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004563 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004564 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004565 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004566 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004567 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004568 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004569
Willy Tarreaue7dff022015-04-03 01:14:29 +02004570 if (!(s->flags & SF_ERR_MASK))
4571 s->flags |= SF_ERR_SRVTO;
4572 if (!(s->flags & SF_FINST_MASK))
4573 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004574 return 0;
4575 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004576
Willy Tarreauf003d372012-11-26 13:35:37 +01004577 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004578 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004579 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4580 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004581 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004582 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004583
Christopher Faulet0184ea72017-01-05 14:06:34 +01004584 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004585 channel_auto_close(rep);
4586
4587 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004588 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004589 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004590
Willy Tarreaue7dff022015-04-03 01:14:29 +02004591 if (!(s->flags & SF_ERR_MASK))
4592 s->flags |= SF_ERR_CLICL;
4593 if (!(s->flags & SF_FINST_MASK))
4594 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004595
Willy Tarreau87b09662015-04-03 00:22:06 +02004596 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004597 return 0;
4598 }
4599
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004600 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004601 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004602 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004603 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004604 else if (txn->flags & TX_NOT_FIRST)
4605 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004606
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004607 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004608 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004609 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004610 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004611 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004612
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004613 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004614 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004615 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004616 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004617 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004618 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004619
Willy Tarreaue7dff022015-04-03 01:14:29 +02004620 if (!(s->flags & SF_ERR_MASK))
4621 s->flags |= SF_ERR_SRVCL;
4622 if (!(s->flags & SF_FINST_MASK))
4623 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004624 return 0;
4625 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004626
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004627 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004628 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004630 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004631 else if (txn->flags & TX_NOT_FIRST)
4632 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004633
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004634 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004635 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004636 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004637
Willy Tarreaue7dff022015-04-03 01:14:29 +02004638 if (!(s->flags & SF_ERR_MASK))
4639 s->flags |= SF_ERR_CLICL;
4640 if (!(s->flags & SF_FINST_MASK))
4641 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004642
Willy Tarreau87b09662015-04-03 00:22:06 +02004643 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004644 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004645 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004646
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004647 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004648 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004649 return 0;
4650 }
4651
4652 /* More interesting part now : we know that we have a complete
4653 * response which at least looks like HTTP. We have an indicator
4654 * of each header's length, so we can parse them quickly.
4655 */
4656
4657 if (unlikely(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 Tarreaub37c27e2009-10-18 22:53:08 +02004659
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004660 /*
4661 * 1: get the status code
4662 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004663 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004664 if (n < 1 || n > 5)
4665 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004666 /* when the client triggers a 4xx from the server, it's most often due
4667 * to a missing object or permission. These events should be tracked
4668 * because if they happen often, it may indicate a brute force or a
4669 * vulnerability scan.
4670 */
4671 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004672 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004673
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004674 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004675 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004676
Willy Tarreau91852eb2015-05-01 13:26:00 +02004677 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4678 * exactly one digit "." one digit. This check may be disabled using
4679 * option accept-invalid-http-response.
4680 */
4681 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4682 if (msg->sl.st.v_l != 8) {
4683 msg->err_pos = 0;
4684 goto hdr_response_bad;
4685 }
4686
Willy Tarreauf37954d2018-06-15 18:31:02 +02004687 if (ci_head(rep)[4] != '/' ||
4688 !isdigit((unsigned char)ci_head(rep)[5]) ||
4689 ci_head(rep)[6] != '.' ||
4690 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004691 msg->err_pos = 4;
4692 goto hdr_response_bad;
4693 }
4694 }
4695
Willy Tarreau5b154472009-12-21 20:11:07 +01004696 /* check if the response is HTTP/1.1 or above */
4697 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004698 ((ci_head(rep)[5] > '1') ||
4699 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004700 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004701
4702 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004703 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 +01004704
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004705 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004706 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004707
Willy Tarreauf37954d2018-06-15 18:31:02 +02004708 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004709
Willy Tarreau39650402010-03-15 19:44:39 +01004710 /* Adjust server's health based on status code. Note: status codes 501
4711 * and 505 are triggered on demand by client request, so we must not
4712 * count them as server failures.
4713 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004714 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004715 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004716 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004717 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004718 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004719 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004720
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004721 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004722 * We may be facing a 100-continue response, or any other informational
4723 * 1xx response which is non-final, in which case this is not the right
4724 * response, and we're waiting for the next one. Let's allow this response
4725 * to go to the client and wait for the next one. There's an exception for
4726 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004727 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004728 if (txn->status < 200 &&
4729 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004730 hdr_idx_init(&txn->hdr_idx);
4731 msg->next -= channel_forward(rep, msg->next);
4732 msg->msg_state = HTTP_MSG_RPBEFORE;
4733 txn->status = 0;
4734 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004735 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004736 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004737 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004738
Willy Tarreaua14ad722017-07-07 11:36:32 +02004739 /*
4740 * 2: check for cacheability.
4741 */
4742
4743 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004744 case 200:
4745 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004746 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004747 case 206:
4748 case 300:
4749 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004750 case 404:
4751 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004752 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004753 case 414:
4754 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004755 break;
4756 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004757 /* RFC7231#6.1:
4758 * Responses with status codes that are defined as
4759 * cacheable by default (e.g., 200, 203, 204, 206,
4760 * 300, 301, 404, 405, 410, 414, and 501 in this
4761 * specification) can be reused by a cache with
4762 * heuristic expiration unless otherwise indicated
4763 * by the method definition or explicit cache
4764 * controls [RFC7234]; all other status codes are
4765 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004766 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004767 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004768 break;
4769 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004770
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004771 /*
4772 * 3: we may need to capture headers
4773 */
4774 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004775 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004776 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4777 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004778
Willy Tarreau557f1992015-05-01 10:05:17 +02004779 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4780 * by RFC7230#3.3.3 :
4781 *
4782 * The length of a message body is determined by one of the following
4783 * (in order of precedence):
4784 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004785 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4786 * the connection will become a tunnel immediately after the empty
4787 * line that concludes the header fields. A client MUST ignore
4788 * any Content-Length or Transfer-Encoding header fields received
4789 * in such a message. Any 101 response (Switching Protocols) is
4790 * managed in the same manner.
4791 *
4792 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004793 * (Informational), 204 (No Content), or 304 (Not Modified) status
4794 * code is always terminated by the first empty line after the
4795 * header fields, regardless of the header fields present in the
4796 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004797 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004798 * 3. If a Transfer-Encoding header field is present and the chunked
4799 * transfer coding (Section 4.1) is the final encoding, the message
4800 * body length is determined by reading and decoding the chunked
4801 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004802 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004803 * If a Transfer-Encoding header field is present in a response and
4804 * the chunked transfer coding is not the final encoding, the
4805 * message body length is determined by reading the connection until
4806 * it is closed by the server. If a Transfer-Encoding header field
4807 * is present in a request and the chunked transfer coding is not
4808 * the final encoding, the message body length cannot be determined
4809 * reliably; the server MUST respond with the 400 (Bad Request)
4810 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004811 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004812 * If a message is received with both a Transfer-Encoding and a
4813 * Content-Length header field, the Transfer-Encoding overrides the
4814 * Content-Length. Such a message might indicate an attempt to
4815 * perform request smuggling (Section 9.5) or response splitting
4816 * (Section 9.4) and ought to be handled as an error. A sender MUST
4817 * remove the received Content-Length field prior to forwarding such
4818 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004819 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004820 * 4. If a message is received without Transfer-Encoding and with
4821 * either multiple Content-Length header fields having differing
4822 * field-values or a single Content-Length header field having an
4823 * invalid value, then the message framing is invalid and the
4824 * recipient MUST treat it as an unrecoverable error. If this is a
4825 * request message, the server MUST respond with a 400 (Bad Request)
4826 * status code and then close the connection. If this is a response
4827 * message received by a proxy, the proxy MUST close the connection
4828 * to the server, discard the received response, and send a 502 (Bad
4829 * Gateway) response to the client. If this is a response message
4830 * received by a user agent, the user agent MUST close the
4831 * connection to the server and discard the received response.
4832 *
4833 * 5. If a valid Content-Length header field is present without
4834 * Transfer-Encoding, its decimal value defines the expected message
4835 * body length in octets. If the sender closes the connection or
4836 * the recipient times out before the indicated number of octets are
4837 * received, the recipient MUST consider the message to be
4838 * incomplete and close the connection.
4839 *
4840 * 6. If this is a request message and none of the above are true, then
4841 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004842 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004843 * 7. Otherwise, this is a response message without a declared message
4844 * body length, so the message body length is determined by the
4845 * number of octets received prior to the server closing the
4846 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004847 */
4848
4849 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004850 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004851 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004852 * FIXME: should we parse anyway and return an error on chunked encoding ?
4853 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004854 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4855 txn->status == 101)) {
4856 /* Either we've established an explicit tunnel, or we're
4857 * switching the protocol. In both cases, we're very unlikely
4858 * to understand the next protocols. We have to switch to tunnel
4859 * mode, so that we transfer the request and responses then let
4860 * this protocol pass unmodified. When we later implement specific
4861 * parsers for such protocols, we'll want to check the Upgrade
4862 * header which contains information about that protocol for
4863 * responses with status 101 (eg: see RFC2817 about TLS).
4864 */
4865 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4866 msg->flags |= HTTP_MSGF_XFER_LEN;
4867 goto end;
4868 }
4869
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004870 if (txn->meth == HTTP_METH_HEAD ||
4871 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004872 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004873 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004874 goto skip_content_length;
4875 }
4876
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004877 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004878 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004879 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004880 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004881 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4882 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004883 /* bad transfer-encoding (chunked followed by something else) */
4884 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004885 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004886 break;
4887 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004888 }
4889
Willy Tarreau1c913912015-04-30 10:57:51 +02004890 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004891 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02004892 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004893 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02004894 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4895 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02004896 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004897 signed long long cl;
4898
Willy Tarreauad14f752011-09-02 20:33:27 +02004899 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004900 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004901 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004902 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004903
Willy Tarreauad14f752011-09-02 20:33:27 +02004904 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004905 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004906 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004907 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004908
Willy Tarreauad14f752011-09-02 20:33:27 +02004909 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004910 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004911 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004912 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004913
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004914 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004915 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004916 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004917 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004918
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004919 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004920 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004921 }
4922
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004923 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
4924 * 407 (Proxy-Authenticate) responses and set the connection to private
4925 */
4926 if (srv_conn && txn->status == 401) {
4927 /* check for Negotiate/NTLM WWW-Authenticate headers */
4928 ctx.idx = 0;
4929 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
4930 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4931 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4932 srv_conn->flags |= CO_FL_PRIVATE;
4933 }
4934 } else if (srv_conn && txn->status == 407) {
4935 /* check for Negotiate/NTLM Proxy-Authenticate headers */
4936 ctx.idx = 0;
4937 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
4938 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4939 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4940 srv_conn->flags |= CO_FL_PRIVATE;
4941 }
4942 }
4943
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004944 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01004945 /* Now we have to check if we need to modify the Connection header.
4946 * This is more difficult on the response than it is on the request,
4947 * because we can have two different HTTP versions and we don't know
4948 * how the client will interprete a response. For instance, let's say
4949 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4950 * HTTP/1.1 response without any header. Maybe it will bound itself to
4951 * HTTP/1.0 because it only knows about it, and will consider the lack
4952 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4953 * the lack of header as a keep-alive. Thus we will use two flags
4954 * indicating how a request MAY be understood by the client. In case
4955 * of multiple possibilities, we'll fix the header to be explicit. If
4956 * ambiguous cases such as both close and keepalive are seen, then we
4957 * will fall back to explicit close. Note that we won't take risks with
4958 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004959 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004960 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004961 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02004962 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01004963 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004964
Willy Tarreau60466522010-01-18 19:08:45 +01004965 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004966 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01004967 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004968
Willy Tarreau60466522010-01-18 19:08:45 +01004969 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004970 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01004971 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004972 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004973 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004974 }
Willy Tarreau60466522010-01-18 19:08:45 +01004975 else { /* SCL / KAL */
4976 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004977 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004978 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004979 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004980
Willy Tarreau60466522010-01-18 19:08:45 +01004981 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004982 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004983
Willy Tarreau60466522010-01-18 19:08:45 +01004984 /* Some keep-alive responses are converted to Server-close if
4985 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004986 */
Willy Tarreau60466522010-01-18 19:08:45 +01004987 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4988 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004989 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004990 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004991 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004992 }
4993
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004994 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02004995 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02004996 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02004997
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004998 /* end of job, return OK */
4999 rep->analysers &= ~an_bit;
5000 rep->analyse_exp = TICK_ETERNITY;
5001 channel_auto_close(rep);
5002 return 1;
5003
5004 abort_keep_alive:
5005 /* A keep-alive request to the server failed on a network error.
5006 * The client is required to retry. We need to close without returning
5007 * any other information so that the client retries.
5008 */
5009 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005010 rep->analysers &= AN_RES_FLT_END;
5011 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005012 channel_auto_close(rep);
5013 s->logs.logwait = 0;
5014 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005015 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005016 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005017 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005018 return 0;
5019}
5020
5021/* This function performs all the processing enabled for the current response.
5022 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005023 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005024 * other functions. It works like process_request (see indications above).
5025 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005026int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005027{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005028 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005029 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005030 struct http_msg *msg = &txn->rsp;
5031 struct proxy *cur_proxy;
5032 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005033 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005034
Christopher Faulet45073512018-07-20 10:16:29 +02005035 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 +02005036 now_ms, __FUNCTION__,
5037 s,
5038 rep,
5039 rep->rex, rep->wex,
5040 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005041 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005042 rep->analysers);
5043
5044 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5045 return 0;
5046
Willy Tarreau70730dd2014-04-24 18:06:27 +02005047 /* The stats applet needs to adjust the Connection header but we don't
5048 * apply any filter there.
5049 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005050 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5051 rep->analysers &= ~an_bit;
5052 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005053 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005054 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005055
Willy Tarreau58975672014-04-24 21:13:57 +02005056 /*
5057 * We will have to evaluate the filters.
5058 * As opposed to version 1.2, now they will be evaluated in the
5059 * filters order and not in the header order. This means that
5060 * each filter has to be validated among all headers.
5061 *
5062 * Filters are tried with ->be first, then with ->fe if it is
5063 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005064 *
5065 * Maybe we are in resume condiion. In this case I choose the
5066 * "struct proxy" which contains the rule list matching the resume
5067 * pointer. If none of theses "struct proxy" match, I initialise
5068 * the process with the first one.
5069 *
5070 * In fact, I check only correspondance betwwen the current list
5071 * pointer and the ->fe rule list. If it doesn't match, I initialize
5072 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005073 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005074 if (s->current_rule_list == &sess->fe->http_res_rules)
5075 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005076 else
5077 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005078 while (1) {
5079 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005080
Willy Tarreau58975672014-04-24 21:13:57 +02005081 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005082 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005083 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005084
Willy Tarreau51d861a2015-05-22 17:30:48 +02005085 if (ret == HTTP_RULE_RES_BADREQ)
5086 goto return_srv_prx_502;
5087
5088 if (ret == HTTP_RULE_RES_DONE) {
5089 rep->analysers &= ~an_bit;
5090 rep->analyse_exp = TICK_ETERNITY;
5091 return 1;
5092 }
5093 }
5094
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005095 /* we need to be called again. */
5096 if (ret == HTTP_RULE_RES_YIELD) {
5097 channel_dont_close(rep);
5098 return 0;
5099 }
5100
Willy Tarreau58975672014-04-24 21:13:57 +02005101 /* try headers filters */
5102 if (rule_set->rsp_exp != NULL) {
5103 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5104 return_bad_resp:
5105 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005106 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005107 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005108 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005109 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005110 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005111 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005112 txn->status = 502;
5113 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005114 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005115 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005116 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005117 if (!(s->flags & SF_ERR_MASK))
5118 s->flags |= SF_ERR_PRXCOND;
5119 if (!(s->flags & SF_FINST_MASK))
5120 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005121 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005122 }
Willy Tarreau58975672014-04-24 21:13:57 +02005123 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005124
Willy Tarreau58975672014-04-24 21:13:57 +02005125 /* has the response been denied ? */
5126 if (txn->flags & TX_SVDENY) {
5127 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005128 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005129
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005130 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5131 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005132 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005133 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005134
Willy Tarreau58975672014-04-24 21:13:57 +02005135 goto return_srv_prx_502;
5136 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005137
Willy Tarreau58975672014-04-24 21:13:57 +02005138 /* add response headers from the rule sets in the same order */
5139 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005140 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005141 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005142 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005143 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005144 ret = acl_pass(ret);
5145 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5146 ret = !ret;
5147 if (!ret)
5148 continue;
5149 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005150 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005151 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005152 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005153
Willy Tarreau58975672014-04-24 21:13:57 +02005154 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005155 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005156 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005157 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005158 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005159
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005160 /* After this point, this anayzer can't return yield, so we can
5161 * remove the bit corresponding to this analyzer from the list.
5162 *
5163 * Note that the intermediate returns and goto found previously
5164 * reset the analyzers.
5165 */
5166 rep->analysers &= ~an_bit;
5167 rep->analyse_exp = TICK_ETERNITY;
5168
Willy Tarreau58975672014-04-24 21:13:57 +02005169 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005170 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005171 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005172
Willy Tarreau58975672014-04-24 21:13:57 +02005173 /*
5174 * Now check for a server cookie.
5175 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005176 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005177 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005178
Willy Tarreau58975672014-04-24 21:13:57 +02005179 /*
5180 * Check for cache-control or pragma headers if required.
5181 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005182 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005183 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005184
Willy Tarreau58975672014-04-24 21:13:57 +02005185 /*
5186 * Add server cookie in the response if needed
5187 */
5188 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5189 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005190 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005191 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5192 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5193 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5194 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5195 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005196 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005197 /* the server is known, it's not the one the client requested, or the
5198 * cookie's last seen date needs to be refreshed. We have to
5199 * insert a set-cookie here, except if we want to insert only on POST
5200 * requests and this one isn't. Note that servers which don't have cookies
5201 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005202 */
Willy Tarreau58975672014-04-24 21:13:57 +02005203 if (!objt_server(s->target)->cookie) {
5204 chunk_printf(&trash,
5205 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5206 s->be->cookie_name);
5207 }
5208 else {
5209 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005210
Willy Tarreau58975672014-04-24 21:13:57 +02005211 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5212 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005213 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5214 s30tob64((date.tv_sec+3) >> 2,
5215 trash.area + trash.data);
5216 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005217
Willy Tarreau58975672014-04-24 21:13:57 +02005218 if (s->be->cookie_maxlife) {
5219 /* emit first_date, which is either the original one or
5220 * the current date.
5221 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005222 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005223 s30tob64(txn->cookie_first_date ?
5224 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005225 (date.tv_sec+3) >> 2,
5226 trash.area + trash.data);
5227 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005228 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005229 }
Willy Tarreau58975672014-04-24 21:13:57 +02005230 chunk_appendf(&trash, "; path=/");
5231 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005232
Willy Tarreau58975672014-04-24 21:13:57 +02005233 if (s->be->cookie_domain)
5234 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005235
Willy Tarreau58975672014-04-24 21:13:57 +02005236 if (s->be->ck_opts & PR_CK_HTTPONLY)
5237 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005238
Willy Tarreau58975672014-04-24 21:13:57 +02005239 if (s->be->ck_opts & PR_CK_SECURE)
5240 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005241
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005242 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005243 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005244
Willy Tarreau58975672014-04-24 21:13:57 +02005245 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005246 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005247 /* the server did not change, only the date was updated */
5248 txn->flags |= TX_SCK_UPDATED;
5249 else
5250 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005251
Willy Tarreau58975672014-04-24 21:13:57 +02005252 /* Here, we will tell an eventual cache on the client side that we don't
5253 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5254 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5255 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005256 */
Willy Tarreau58975672014-04-24 21:13:57 +02005257 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005258
Willy Tarreau58975672014-04-24 21:13:57 +02005259 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005260
Willy Tarreau58975672014-04-24 21:13:57 +02005261 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5262 "Cache-control: private", 22) < 0))
5263 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005264 }
Willy Tarreau58975672014-04-24 21:13:57 +02005265 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005266
Willy Tarreau58975672014-04-24 21:13:57 +02005267 /*
5268 * Check if result will be cacheable with a cookie.
5269 * We'll block the response if security checks have caught
5270 * nasty things such as a cacheable cookie.
5271 */
5272 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5273 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5274 (s->be->options & PR_O_CHK_CACHE)) {
5275 /* we're in presence of a cacheable response containing
5276 * a set-cookie header. We'll block it as requested by
5277 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005278 */
Willy Tarreau58975672014-04-24 21:13:57 +02005279 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005280 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005281
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005282 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5283 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005284 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005285 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005286
Christopher Faulet767a84b2017-11-24 16:50:31 +01005287 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5288 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005289 send_log(s->be, LOG_ALERT,
5290 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5291 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5292 goto return_srv_prx_502;
5293 }
Willy Tarreau03945942009-12-22 16:50:27 +01005294
Willy Tarreau70730dd2014-04-24 18:06:27 +02005295 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005296 /*
5297 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5298 * If an "Upgrade" token is found, the header is left untouched in order
5299 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005300 * if anything but "Upgrade" is present in the Connection header. We don't
5301 * want to touch any 101 response either since it's switching to another
5302 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005303 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005304 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005305 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005306 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005307
Willy Tarreau58975672014-04-24 21:13:57 +02005308 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5309 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5310 /* we want a keep-alive response here. Keep-alive header
5311 * required if either side is not 1.1.
5312 */
5313 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5314 want_flags |= TX_CON_KAL_SET;
5315 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005316 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005317 /* we want a close response here. Close header required if
5318 * the server is 1.1, regardless of the client.
5319 */
5320 if (msg->flags & HTTP_MSGF_VER_11)
5321 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005322 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005323
Willy Tarreau58975672014-04-24 21:13:57 +02005324 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5325 http_change_connection_header(txn, msg, want_flags);
5326 }
5327
5328 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005329 /* Always enter in the body analyzer */
5330 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5331 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005332
Willy Tarreau58975672014-04-24 21:13:57 +02005333 /* if the user wants to log as soon as possible, without counting
5334 * bytes from the server, then this is the right moment. We have
5335 * to temporarily assign bytes_out to log what we currently have.
5336 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005337 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005338 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5339 s->logs.bytes_out = txn->rsp.eoh;
5340 s->do_log(s);
5341 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005342 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005343 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005344}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005345
Willy Tarreaud98cf932009-12-27 22:54:55 +01005346/* This function is an analyser which forwards response body (including chunk
5347 * sizes if any). It is called as soon as we must forward, even if we forward
5348 * zero byte. The only situation where it must not be called is when we're in
5349 * tunnel mode and we want to forward till the close. It's used both to forward
5350 * remaining data and to resync after end of body. It expects the msg_state to
5351 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005352 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005353 *
5354 * It is capable of compressing response data both in content-length mode and
5355 * in chunked mode. The state machines follows different flows depending on
5356 * whether content-length and chunked modes are used, since there are no
5357 * trailers in content-length :
5358 *
5359 * chk-mode cl-mode
5360 * ,----- BODY -----.
5361 * / \
5362 * V size > 0 V chk-mode
5363 * .--> SIZE -------------> DATA -------------> CRLF
5364 * | | size == 0 | last byte |
5365 * | v final crlf v inspected |
5366 * | TRAILERS -----------> DONE |
5367 * | |
5368 * `----------------------------------------------'
5369 *
5370 * Compression only happens in the DATA state, and must be flushed in final
5371 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5372 * is performed at once on final states for all bytes parsed, or when leaving
5373 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005374 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005375int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005376{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005377 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005378 struct http_txn *txn = s->txn;
5379 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005380 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005381
Christopher Faulet45073512018-07-20 10:16:29 +02005382 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 +02005383 now_ms, __FUNCTION__,
5384 s,
5385 res,
5386 res->rex, res->wex,
5387 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005388 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005389 res->analysers);
5390
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005391 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5392 return 0;
5393
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005394 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005395 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005396 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005397 /* Output closed while we were sending data. We must abort and
5398 * wake the other side up.
5399 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005400 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005401 msg->msg_state = HTTP_MSG_ERROR;
5402 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005403 return 1;
5404 }
5405
Willy Tarreau4fe41902010-06-07 22:27:41 +02005406 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005407 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005408
Christopher Fauletd7c91962015-04-30 11:48:27 +02005409 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005410 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5411 ? HTTP_MSG_CHUNK_SIZE
5412 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005413 }
5414
Willy Tarreauefdf0942014-04-24 20:08:57 +02005415 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005416 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005417 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005418 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005419 }
5420
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005421 if (msg->msg_state < HTTP_MSG_DONE) {
5422 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5423 ? http_msg_forward_chunked_body(s, msg)
5424 : http_msg_forward_body(s, msg));
5425 if (!ret)
5426 goto missing_data_or_waiting;
5427 if (ret < 0)
5428 goto return_bad_res;
5429 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005430
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005431 /* other states, DONE...TUNNEL */
5432 /* for keep-alive we don't want to forward closes on DONE */
5433 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5434 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5435 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005436
Christopher Faulet894da4c2017-07-18 11:29:07 +02005437 http_resync_states(s);
5438 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005439 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5440 if (res->flags & CF_SHUTW) {
5441 /* response errors are most likely due to the
5442 * client aborting the transfer. */
5443 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005444 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005445 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005446 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005447 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005448 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005449 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005450 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005451 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005452
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005453 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005454 if (res->flags & CF_SHUTW)
5455 goto aborted_xfer;
5456
5457 /* stop waiting for data if the input is closed before the end. If the
5458 * client side was already closed, it means that the client has aborted,
5459 * so we don't want to count this as a server abort. Otherwise it's a
5460 * server abort.
5461 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005462 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005463 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005464 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005465 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005466 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005467 if (!(s->flags & SF_ERR_MASK))
5468 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005469 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005470 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005471 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005472 goto return_bad_res_stats_ok;
5473 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005474 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005475
Willy Tarreau40dba092010-03-04 18:14:51 +01005476 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005477 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005478 goto return_bad_res;
5479
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005480 /* When TE: chunked is used, we need to get there again to parse
5481 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005482 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5483 * or if there are filters registered on the stream, we don't want to
5484 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005485 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005486 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005487 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005488 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5489 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005490 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005491
Willy Tarreau5c620922011-05-11 19:56:11 +02005492 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005493 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005494 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005495 * modes are already handled by the stream sock layer. We must not do
5496 * this in content-length mode because it could present the MSG_MORE
5497 * flag with the last block of forwarded data, which would cause an
5498 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005499 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005500 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005501 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005502
Willy Tarreau87b09662015-04-03 00:22:06 +02005503 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005504 return 0;
5505
Willy Tarreau40dba092010-03-04 18:14:51 +01005506 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005507 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005508 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005509 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005510
5511 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005512 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005513 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005514 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005515 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005516 res->analysers &= AN_RES_FLT_END;
5517 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 +01005518 if (objt_server(s->target))
5519 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005520
Willy Tarreaue7dff022015-04-03 01:14:29 +02005521 if (!(s->flags & SF_ERR_MASK))
5522 s->flags |= SF_ERR_PRXCOND;
5523 if (!(s->flags & SF_FINST_MASK))
5524 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005525 return 0;
5526
5527 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005528 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005529 txn->rsp.msg_state = HTTP_MSG_ERROR;
5530 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005531 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005532 res->analysers &= AN_RES_FLT_END;
5533 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 +01005534
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005535 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5536 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005537 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005538 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005539
Willy Tarreaue7dff022015-04-03 01:14:29 +02005540 if (!(s->flags & SF_ERR_MASK))
5541 s->flags |= SF_ERR_CLICL;
5542 if (!(s->flags & SF_FINST_MASK))
5543 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005544 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005545}
5546
5547
Christopher Faulet10079f52018-10-03 15:17:28 +02005548int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005549{
5550 struct channel *chn = msg->chn;
5551 int ret;
5552
5553 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5554
5555 if (msg->msg_state == HTTP_MSG_ENDING)
5556 goto ending;
5557
5558 /* Neither content-length, nor transfer-encoding was found, so we must
5559 * read the body until the server connection is closed. In that case, we
5560 * eat data as they come. Of course, this happens for response only. */
5561 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005562 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005563 msg->chunk_len += len;
5564 msg->body_len += len;
5565 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005566 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005567 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005568 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005569 msg->next += ret;
5570 msg->chunk_len -= ret;
5571 if (msg->chunk_len) {
5572 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005573 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005574 chn->flags |= CF_WAKE_WRITE;
5575 goto missing_data_or_waiting;
5576 }
5577
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005578 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5579 * always set for a request. */
5580 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5581 /* The server still sending data that should be filtered */
5582 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5583 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005584 msg->msg_state = HTTP_MSG_TUNNEL;
5585 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005586 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005587
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005588 msg->msg_state = HTTP_MSG_ENDING;
5589
5590 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005591 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005592 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005593 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5594 /* default_ret */ msg->next,
5595 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005596 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005597 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005598 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5599 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005600 if (msg->next)
5601 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005602
Christopher Fauletda02e172015-12-04 09:25:05 +01005603 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5604 /* default_ret */ 1,
5605 /* on_error */ goto error,
5606 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005607 if (msg->msg_state == HTTP_MSG_ENDING)
5608 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005609 return 1;
5610
5611 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005612 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005613 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5614 /* default_ret */ msg->next,
5615 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005616 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005617 msg->next -= ret;
5618 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5619 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005620 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005621 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005622 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005623 return 0;
5624 error:
5625 return -1;
5626}
5627
Christopher Faulet10079f52018-10-03 15:17:28 +02005628int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005629{
5630 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005631 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005632 int ret;
5633
5634 /* Here we have the guarantee to be in one of the following state:
5635 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5636 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5637
Christopher Fauletca874b82018-09-20 11:31:01 +02005638 if (msg->msg_state == HTTP_MSG_ENDING)
5639 goto ending;
5640
5641 /* Don't parse chunks if there is no input data */
5642 if (!ci_data(chn))
5643 goto waiting;
5644
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005645 switch_states:
5646 switch (msg->msg_state) {
5647 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005648 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005649 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005650 /* on_error */ goto error);
5651 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005652 msg->chunk_len -= ret;
5653 if (msg->chunk_len) {
5654 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005655 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005656 chn->flags |= CF_WAKE_WRITE;
5657 goto missing_data_or_waiting;
5658 }
5659
5660 /* nothing left to forward for this chunk*/
5661 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5662 /* fall through for HTTP_MSG_CHUNK_CRLF */
5663
5664 case HTTP_MSG_CHUNK_CRLF:
5665 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005666 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005667 if (ret == 0)
5668 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005669 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005670 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005671 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005672 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005673 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005674 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005675 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005676 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5677 /* fall through for HTTP_MSG_CHUNK_SIZE */
5678
5679 case HTTP_MSG_CHUNK_SIZE:
5680 /* read the chunk size and assign it to ->chunk_len,
5681 * then set ->next to point to the body and switch to
5682 * DATA or TRAILERS state.
5683 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005684 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005685 if (ret == 0)
5686 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005687 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005688 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005689 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005690 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005691 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005692 }
5693
5694 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005695 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005696 msg->chunk_len = chunk;
5697 msg->body_len += chunk;
5698
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005699 if (msg->chunk_len) {
5700 msg->msg_state = HTTP_MSG_DATA;
5701 goto switch_states;
5702 }
5703 msg->msg_state = HTTP_MSG_TRAILERS;
5704 /* fall through for HTTP_MSG_TRAILERS */
5705
5706 case HTTP_MSG_TRAILERS:
5707 ret = http_forward_trailers(msg);
5708 if (ret < 0)
5709 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005710 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5711 /* default_ret */ 1,
5712 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005713 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005714 if (!ret)
5715 goto missing_data_or_waiting;
5716 break;
5717
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005718 default:
5719 /* This should no happen in this function */
5720 goto error;
5721 }
5722
5723 msg->msg_state = HTTP_MSG_ENDING;
5724 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005725 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005726 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005727 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005728 /* default_ret */ msg->next,
5729 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005730 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005731 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005732 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5733 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005734 if (msg->next)
5735 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005736
Christopher Fauletda02e172015-12-04 09:25:05 +01005737 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005738 /* default_ret */ 1,
5739 /* on_error */ goto error,
5740 /* on_wait */ goto waiting);
5741 msg->msg_state = HTTP_MSG_DONE;
5742 return 1;
5743
5744 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005745 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005746 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005747 /* default_ret */ msg->next,
5748 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005749 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005750 msg->next -= ret;
5751 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5752 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005753 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005754 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005755 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005756 return 0;
5757
5758 chunk_parsing_error:
5759 if (msg->err_pos >= 0) {
5760 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005761 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005762 msg->msg_state, strm_fe(s));
5763 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005764 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005765 msg, msg->msg_state, s->be);
5766 }
5767 error:
5768 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005769}
5770
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005771
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005772/* Iterate the same filter through all request headers.
5773 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005774 * Since it can manage the switch to another backend, it updates the per-proxy
5775 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005776 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005777int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005778{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005779 char *cur_ptr, *cur_end, *cur_next;
5780 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005781 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005782 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005783 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005784
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005785 last_hdr = 0;
5786
Willy Tarreauf37954d2018-06-15 18:31:02 +02005787 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005788 old_idx = 0;
5789
5790 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005791 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005792 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005793 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005794 (exp->action == ACT_ALLOW ||
5795 exp->action == ACT_DENY ||
5796 exp->action == ACT_TARPIT))
5797 return 0;
5798
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005799 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005800 if (!cur_idx)
5801 break;
5802
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005803 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005804 cur_ptr = cur_next;
5805 cur_end = cur_ptr + cur_hdr->len;
5806 cur_next = cur_end + cur_hdr->cr + 1;
5807
5808 /* Now we have one header between cur_ptr and cur_end,
5809 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005810 */
5811
Willy Tarreau15a53a42015-01-21 13:39:42 +01005812 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005813 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005814 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005815 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005816 last_hdr = 1;
5817 break;
5818
5819 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005820 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005821 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005822 break;
5823
5824 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005825 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005826 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005827 break;
5828
5829 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005830 len = exp_replace(trash.area,
5831 trash.size, cur_ptr,
5832 exp->replace, pmatch);
5833 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005834 return -1;
5835
Willy Tarreau6e27be12018-08-22 04:46:47 +02005836 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5837
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005838 /* FIXME: if the user adds a newline in the replacement, the
5839 * index will not be recalculated for now, and the new line
5840 * will not be counted as a new header.
5841 */
5842
5843 cur_end += delta;
5844 cur_next += delta;
5845 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005846 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005847 break;
5848
5849 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005850 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005851 cur_next += delta;
5852
Willy Tarreaufa355d42009-11-29 18:12:29 +01005853 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005854 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5855 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005856 cur_hdr->len = 0;
5857 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005858 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005859 break;
5860
5861 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005862 }
5863
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005864 /* keep the link from this header to next one in case of later
5865 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005866 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005867 old_idx = cur_idx;
5868 }
5869 return 0;
5870}
5871
5872
5873/* Apply the filter to the request line.
5874 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5875 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005876 * Since it can manage the switch to another backend, it updates the per-proxy
5877 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005878 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005879int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005880{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005881 char *cur_ptr, *cur_end;
5882 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005883 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005884 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005885
Willy Tarreau3d300592007-03-18 18:34:41 +01005886 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005887 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005888 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005889 (exp->action == ACT_ALLOW ||
5890 exp->action == ACT_DENY ||
5891 exp->action == ACT_TARPIT))
5892 return 0;
5893 else if (exp->action == ACT_REMOVE)
5894 return 0;
5895
5896 done = 0;
5897
Willy Tarreauf37954d2018-06-15 18:31:02 +02005898 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005899 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005900
5901 /* Now we have the request line between cur_ptr and cur_end */
5902
Willy Tarreau15a53a42015-01-21 13:39:42 +01005903 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005904 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005905 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005906 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005907 done = 1;
5908 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005909
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005910 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005911 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005912 done = 1;
5913 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005914
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005915 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005916 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005917 done = 1;
5918 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005919
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005920 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005921 len = exp_replace(trash.area, trash.size,
5922 cur_ptr, exp->replace, pmatch);
5923 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005924 return -1;
5925
Willy Tarreau6e27be12018-08-22 04:46:47 +02005926 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5927
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005928 /* FIXME: if the user adds a newline in the replacement, the
5929 * index will not be recalculated for now, and the new line
5930 * will not be counted as a new header.
5931 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005932
Willy Tarreaufa355d42009-11-29 18:12:29 +01005933 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005934 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005935 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005936 HTTP_MSG_RQMETH,
5937 cur_ptr, cur_end + 1,
5938 NULL, NULL);
5939 if (unlikely(!cur_end))
5940 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005941
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005942 /* we have a full request and we know that we have either a CR
5943 * or an LF at <ptr>.
5944 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005945 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5946 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005947 /* there is no point trying this regex on headers */
5948 return 1;
5949 }
5950 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005951 return done;
5952}
Willy Tarreau97de6242006-12-27 17:18:38 +01005953
Willy Tarreau58f10d72006-12-04 02:26:12 +01005954
Willy Tarreau58f10d72006-12-04 02:26:12 +01005955
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005956/*
Willy Tarreau87b09662015-04-03 00:22:06 +02005957 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005958 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005959 * unparsable request. Since it can manage the switch to another backend, it
5960 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005961 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005962int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005963{
Willy Tarreau192252e2015-04-04 01:47:55 +02005964 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005965 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005966 struct hdr_exp *exp;
5967
5968 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005969 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005970
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005971 /*
5972 * The interleaving of transformations and verdicts
5973 * makes it difficult to decide to continue or stop
5974 * the evaluation.
5975 */
5976
Willy Tarreau6c123b12010-01-28 20:22:06 +01005977 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5978 break;
5979
Willy Tarreau3d300592007-03-18 18:34:41 +01005980 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005981 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005982 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005983 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005984
5985 /* if this filter had a condition, evaluate it now and skip to
5986 * next filter if the condition does not match.
5987 */
5988 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005989 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005990 ret = acl_pass(ret);
5991 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5992 ret = !ret;
5993
5994 if (!ret)
5995 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005996 }
5997
5998 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005999 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006000 if (unlikely(ret < 0))
6001 return -1;
6002
6003 if (likely(ret == 0)) {
6004 /* The filter did not match the request, it can be
6005 * iterated through all headers.
6006 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006007 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6008 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006009 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006010 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006011 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006012}
6013
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006014
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006015/* Delete a value in a header between delimiters <from> and <next> in buffer
6016 * <buf>. The number of characters displaced is returned, and the pointer to
6017 * the first delimiter is updated if required. The function tries as much as
6018 * possible to respect the following principles :
6019 * - replace <from> delimiter by the <next> one unless <from> points to a
6020 * colon, in which case <next> is simply removed
6021 * - set exactly one space character after the new first delimiter, unless
6022 * there are not enough characters in the block being moved to do so.
6023 * - remove unneeded spaces before the previous delimiter and after the new
6024 * one.
6025 *
6026 * It is the caller's responsibility to ensure that :
6027 * - <from> points to a valid delimiter or the colon ;
6028 * - <next> points to a valid delimiter or the final CR/LF ;
6029 * - there are non-space chars before <from> ;
6030 * - there is a CR/LF at or after <next>.
6031 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006032int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006033{
6034 char *prev = *from;
6035
6036 if (*prev == ':') {
6037 /* We're removing the first value, preserve the colon and add a
6038 * space if possible.
6039 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006040 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006041 next++;
6042 prev++;
6043 if (prev < next)
6044 *prev++ = ' ';
6045
Willy Tarreau2235b262016-11-05 15:50:20 +01006046 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006047 next++;
6048 } else {
6049 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006050 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006051 prev--;
6052 *from = prev;
6053
6054 /* copy the delimiter and if possible a space if we're
6055 * not at the end of the line.
6056 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006057 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006058 *prev++ = *next++;
6059 if (prev + 1 < next)
6060 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006061 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006062 next++;
6063 }
6064 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006065 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006066}
6067
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006068/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006069 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006070 * desirable to call it only when needed. This code is quite complex because
6071 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6072 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006073 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006074void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006075{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006076 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006077 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006078 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006079 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006080 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6081 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006082
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006083 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006084 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006085 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006086
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006087 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006088 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006089 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006090
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006091 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006092 hdr_beg = hdr_next;
6093 hdr_end = hdr_beg + cur_hdr->len;
6094 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006095
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006096 /* We have one full header between hdr_beg and hdr_end, and the
6097 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006098 * "Cookie:" headers.
6099 */
6100
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006101 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006102 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006103 old_idx = cur_idx;
6104 continue;
6105 }
6106
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006107 del_from = NULL; /* nothing to be deleted */
6108 preserve_hdr = 0; /* assume we may kill the whole header */
6109
Willy Tarreau58f10d72006-12-04 02:26:12 +01006110 /* Now look for cookies. Conforming to RFC2109, we have to support
6111 * attributes whose name begin with a '$', and associate them with
6112 * the right cookie, if we want to delete this cookie.
6113 * So there are 3 cases for each cookie read :
6114 * 1) it's a special attribute, beginning with a '$' : ignore it.
6115 * 2) it's a server id cookie that we *MAY* want to delete : save
6116 * some pointers on it (last semi-colon, beginning of cookie...)
6117 * 3) it's an application cookie : we *MAY* have to delete a previous
6118 * "special" cookie.
6119 * At the end of loop, if a "special" cookie remains, we may have to
6120 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006121 * *MUST* delete it.
6122 *
6123 * Note: RFC2965 is unclear about the processing of spaces around
6124 * the equal sign in the ATTR=VALUE form. A careful inspection of
6125 * the RFC explicitly allows spaces before it, and not within the
6126 * tokens (attrs or values). An inspection of RFC2109 allows that
6127 * too but section 10.1.3 lets one think that spaces may be allowed
6128 * after the equal sign too, resulting in some (rare) buggy
6129 * implementations trying to do that. So let's do what servers do.
6130 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6131 * allowed quoted strings in values, with any possible character
6132 * after a backslash, including control chars and delimitors, which
6133 * causes parsing to become ambiguous. Browsers also allow spaces
6134 * within values even without quotes.
6135 *
6136 * We have to keep multiple pointers in order to support cookie
6137 * removal at the beginning, middle or end of header without
6138 * corrupting the header. All of these headers are valid :
6139 *
6140 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6141 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6142 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6143 * | | | | | | | | |
6144 * | | | | | | | | hdr_end <--+
6145 * | | | | | | | +--> next
6146 * | | | | | | +----> val_end
6147 * | | | | | +-----------> val_beg
6148 * | | | | +--------------> equal
6149 * | | | +----------------> att_end
6150 * | | +---------------------> att_beg
6151 * | +--------------------------> prev
6152 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006153 */
6154
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006155 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6156 /* Iterate through all cookies on this line */
6157
6158 /* find att_beg */
6159 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006160 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006161 att_beg++;
6162
6163 /* find att_end : this is the first character after the last non
6164 * space before the equal. It may be equal to hdr_end.
6165 */
6166 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006167
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006168 while (equal < hdr_end) {
6169 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006170 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006171 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006172 continue;
6173 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006174 }
6175
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006176 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6177 * is between <att_beg> and <equal>, both may be identical.
6178 */
6179
6180 /* look for end of cookie if there is an equal sign */
6181 if (equal < hdr_end && *equal == '=') {
6182 /* look for the beginning of the value */
6183 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006184 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006185 val_beg++;
6186
6187 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006188 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006189
6190 /* make val_end point to the first white space or delimitor after the value */
6191 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006192 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006193 val_end--;
6194 } else {
6195 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006196 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006197
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006198 /* We have nothing to do with attributes beginning with '$'. However,
6199 * they will automatically be removed if a header before them is removed,
6200 * since they're supposed to be linked together.
6201 */
6202 if (*att_beg == '$')
6203 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006204
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006205 /* Ignore cookies with no equal sign */
6206 if (equal == next) {
6207 /* This is not our cookie, so we must preserve it. But if we already
6208 * scheduled another cookie for removal, we cannot remove the
6209 * complete header, but we can remove the previous block itself.
6210 */
6211 preserve_hdr = 1;
6212 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006213 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006214 val_end += delta;
6215 next += delta;
6216 hdr_end += delta;
6217 hdr_next += delta;
6218 cur_hdr->len += delta;
6219 http_msg_move_end(&txn->req, delta);
6220 prev = del_from;
6221 del_from = NULL;
6222 }
6223 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006224 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006225
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006226 /* if there are spaces around the equal sign, we need to
6227 * strip them otherwise we'll get trouble for cookie captures,
6228 * or even for rewrites. Since this happens extremely rarely,
6229 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006230 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006231 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6232 int stripped_before = 0;
6233 int stripped_after = 0;
6234
6235 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006236 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006237 equal += stripped_before;
6238 val_beg += stripped_before;
6239 }
6240
6241 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006242 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006243 val_beg += stripped_after;
6244 stripped_before += stripped_after;
6245 }
6246
6247 val_end += stripped_before;
6248 next += stripped_before;
6249 hdr_end += stripped_before;
6250 hdr_next += stripped_before;
6251 cur_hdr->len += stripped_before;
6252 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006253 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006254 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006255
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 /* First, let's see if we want to capture this cookie. We check
6257 * that we don't already have a client side cookie, because we
6258 * can only capture one. Also as an optimisation, we ignore
6259 * cookies shorter than the declared name.
6260 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006261 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6262 (val_end - att_beg >= sess->fe->capture_namelen) &&
6263 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006264 int log_len = val_end - att_beg;
6265
Willy Tarreaubafbe012017-11-24 17:34:44 +01006266 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006267 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006268 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006269 if (log_len > sess->fe->capture_len)
6270 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006271 memcpy(txn->cli_cookie, att_beg, log_len);
6272 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006273 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006274 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006275
Willy Tarreaubca99692010-10-06 19:25:55 +02006276 /* Persistence cookies in passive, rewrite or insert mode have the
6277 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006278 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006279 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006280 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006281 * For cookies in prefix mode, the form is :
6282 *
6283 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006284 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006285 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6286 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6287 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006289
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006290 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6291 * have the server ID between val_beg and delim, and the original cookie between
6292 * delim+1 and val_end. Otherwise, delim==val_end :
6293 *
6294 * Cookie: NAME=SRV; # in all but prefix modes
6295 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6296 * | || || | |+-> next
6297 * | || || | +--> val_end
6298 * | || || +---------> delim
6299 * | || |+------------> val_beg
6300 * | || +-------------> att_end = equal
6301 * | |+-----------------> att_beg
6302 * | +------------------> prev
6303 * +-------------------------> hdr_beg
6304 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006305
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006306 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006307 for (delim = val_beg; delim < val_end; delim++)
6308 if (*delim == COOKIE_DELIM)
6309 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006310 } else {
6311 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006312 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006313 /* Now check if the cookie contains a date field, which would
6314 * appear after a vertical bar ('|') just after the server name
6315 * and before the delimiter.
6316 */
6317 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6318 if (vbar1) {
6319 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006320 * right is the last seen date. It is a base64 encoded
6321 * 30-bit value representing the UNIX date since the
6322 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006323 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006324 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006325 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006326 if (val_end - vbar1 >= 5) {
6327 val = b64tos30(vbar1);
6328 if (val > 0)
6329 txn->cookie_last_date = val << 2;
6330 }
6331 /* look for a second vertical bar */
6332 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6333 if (vbar1 && (val_end - vbar1 > 5)) {
6334 val = b64tos30(vbar1 + 1);
6335 if (val > 0)
6336 txn->cookie_first_date = val << 2;
6337 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006338 }
6339 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006340
Willy Tarreauf64d1412010-10-07 20:06:11 +02006341 /* if the cookie has an expiration date and the proxy wants to check
6342 * it, then we do that now. We first check if the cookie is too old,
6343 * then only if it has expired. We detect strict overflow because the
6344 * time resolution here is not great (4 seconds). Cookies with dates
6345 * in the future are ignored if their offset is beyond one day. This
6346 * allows an admin to fix timezone issues without expiring everyone
6347 * and at the same time avoids keeping unwanted side effects for too
6348 * long.
6349 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006350 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6351 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006352 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006353 txn->flags &= ~TX_CK_MASK;
6354 txn->flags |= TX_CK_OLD;
6355 delim = val_beg; // let's pretend we have not found the cookie
6356 txn->cookie_first_date = 0;
6357 txn->cookie_last_date = 0;
6358 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006359 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6360 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006361 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006362 txn->flags &= ~TX_CK_MASK;
6363 txn->flags |= TX_CK_EXPIRED;
6364 delim = val_beg; // let's pretend we have not found the cookie
6365 txn->cookie_first_date = 0;
6366 txn->cookie_last_date = 0;
6367 }
6368
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006369 /* Here, we'll look for the first running server which supports the cookie.
6370 * This allows to share a same cookie between several servers, for example
6371 * to dedicate backup servers to specific servers only.
6372 * However, to prevent clients from sticking to cookie-less backup server
6373 * when they have incidentely learned an empty cookie, we simply ignore
6374 * empty cookies and mark them as invalid.
6375 * The same behaviour is applied when persistence must be ignored.
6376 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006377 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006378 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006379
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006380 while (srv) {
6381 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6382 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006383 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006384 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006385 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006386 /* we found the server and we can use it */
6387 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006388 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006389 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006390 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006391 break;
6392 } else {
6393 /* we found a server, but it's down,
6394 * mark it as such and go on in case
6395 * another one is available.
6396 */
6397 txn->flags &= ~TX_CK_MASK;
6398 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006399 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006400 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006401 srv = srv->next;
6402 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006403
Willy Tarreauf64d1412010-10-07 20:06:11 +02006404 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006405 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006406 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006407 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006408 txn->flags |= TX_CK_UNUSED;
6409 else
6410 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006411 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006412
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006413 /* depending on the cookie mode, we may have to either :
6414 * - delete the complete cookie if we're in insert+indirect mode, so that
6415 * the server never sees it ;
6416 * - remove the server id from the cookie value, and tag the cookie as an
6417 * application cookie so that it does not get accidentely removed later,
6418 * if we're in cookie prefix mode
6419 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006420 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006421 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006422
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006423 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006424 val_end += delta;
6425 next += delta;
6426 hdr_end += delta;
6427 hdr_next += delta;
6428 cur_hdr->len += delta;
6429 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006430
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006431 del_from = NULL;
6432 preserve_hdr = 1; /* we want to keep this cookie */
6433 }
6434 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006435 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006436 del_from = prev;
6437 }
6438 } else {
6439 /* This is not our cookie, so we must preserve it. But if we already
6440 * scheduled another cookie for removal, we cannot remove the
6441 * complete header, but we can remove the previous block itself.
6442 */
6443 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006444
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006445 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006446 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006447 if (att_beg >= del_from)
6448 att_beg += delta;
6449 if (att_end >= del_from)
6450 att_end += delta;
6451 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006452 val_end += delta;
6453 next += delta;
6454 hdr_end += delta;
6455 hdr_next += delta;
6456 cur_hdr->len += delta;
6457 http_msg_move_end(&txn->req, delta);
6458 prev = del_from;
6459 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006460 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006461 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006462
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006463 /* continue with next cookie on this header line */
6464 att_beg = next;
6465 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006466
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006467 /* There are no more cookies on this line.
6468 * We may still have one (or several) marked for deletion at the
6469 * end of the line. We must do this now in two ways :
6470 * - if some cookies must be preserved, we only delete from the
6471 * mark to the end of line ;
6472 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006473 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006474 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006475 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006476 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006477 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006478 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006479 cur_hdr->len += delta;
6480 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006481 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006482
6483 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006484 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6485 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006486 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006487 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006488 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006489 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006490 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006491 }
6492
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006493 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006494 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006495 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006496}
6497
6498
Willy Tarreaua15645d2007-03-18 16:22:39 +01006499/* Iterate the same filter through all response headers contained in <rtr>.
6500 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6501 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006502int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006503{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006504 char *cur_ptr, *cur_end, *cur_next;
6505 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006506 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006507 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006508 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006509
6510 last_hdr = 0;
6511
Willy Tarreauf37954d2018-06-15 18:31:02 +02006512 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006513 old_idx = 0;
6514
6515 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006516 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006517 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006518 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006519 (exp->action == ACT_ALLOW ||
6520 exp->action == ACT_DENY))
6521 return 0;
6522
6523 cur_idx = txn->hdr_idx.v[old_idx].next;
6524 if (!cur_idx)
6525 break;
6526
6527 cur_hdr = &txn->hdr_idx.v[cur_idx];
6528 cur_ptr = cur_next;
6529 cur_end = cur_ptr + cur_hdr->len;
6530 cur_next = cur_end + cur_hdr->cr + 1;
6531
6532 /* Now we have one header between cur_ptr and cur_end,
6533 * and the next header starts at cur_next.
6534 */
6535
Willy Tarreau15a53a42015-01-21 13:39:42 +01006536 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006537 switch (exp->action) {
6538 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006539 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006540 last_hdr = 1;
6541 break;
6542
6543 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006544 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006545 last_hdr = 1;
6546 break;
6547
6548 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006549 len = exp_replace(trash.area,
6550 trash.size, cur_ptr,
6551 exp->replace, pmatch);
6552 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006553 return -1;
6554
Willy Tarreau6e27be12018-08-22 04:46:47 +02006555 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6556
Willy Tarreaua15645d2007-03-18 16:22:39 +01006557 /* FIXME: if the user adds a newline in the replacement, the
6558 * index will not be recalculated for now, and the new line
6559 * will not be counted as a new header.
6560 */
6561
6562 cur_end += delta;
6563 cur_next += delta;
6564 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006565 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006566 break;
6567
6568 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006569 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006570 cur_next += delta;
6571
Willy Tarreaufa355d42009-11-29 18:12:29 +01006572 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006573 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6574 txn->hdr_idx.used--;
6575 cur_hdr->len = 0;
6576 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006577 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006578 break;
6579
6580 }
6581 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006582
6583 /* keep the link from this header to next one in case of later
6584 * removal of next header.
6585 */
6586 old_idx = cur_idx;
6587 }
6588 return 0;
6589}
6590
6591
6592/* Apply the filter to the status line in the response buffer <rtr>.
6593 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6594 * or -1 if a replacement resulted in an invalid status line.
6595 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006596int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006597{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006598 char *cur_ptr, *cur_end;
6599 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006600 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006601 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006602
Willy Tarreau3d300592007-03-18 18:34:41 +01006603 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006604 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006605 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006606 (exp->action == ACT_ALLOW ||
6607 exp->action == ACT_DENY))
6608 return 0;
6609 else if (exp->action == ACT_REMOVE)
6610 return 0;
6611
6612 done = 0;
6613
Willy Tarreauf37954d2018-06-15 18:31:02 +02006614 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006615 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006616
6617 /* Now we have the status line between cur_ptr and cur_end */
6618
Willy Tarreau15a53a42015-01-21 13:39:42 +01006619 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006620 switch (exp->action) {
6621 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006622 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006623 done = 1;
6624 break;
6625
6626 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006627 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628 done = 1;
6629 break;
6630
6631 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006632 len = exp_replace(trash.area, trash.size,
6633 cur_ptr, exp->replace, pmatch);
6634 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006635 return -1;
6636
Willy Tarreau6e27be12018-08-22 04:46:47 +02006637 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6638
Willy Tarreaua15645d2007-03-18 16:22:39 +01006639 /* FIXME: if the user adds a newline in the replacement, the
6640 * index will not be recalculated for now, and the new line
6641 * will not be counted as a new header.
6642 */
6643
Willy Tarreaufa355d42009-11-29 18:12:29 +01006644 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006645 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006646 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006647 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648 cur_ptr, cur_end + 1,
6649 NULL, NULL);
6650 if (unlikely(!cur_end))
6651 return -1;
6652
6653 /* we have a full respnse and we know that we have either a CR
6654 * or an LF at <ptr>.
6655 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006656 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006657 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006658 /* there is no point trying this regex on headers */
6659 return 1;
6660 }
6661 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006662 return done;
6663}
6664
6665
6666
6667/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006668 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006669 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6670 * unparsable response.
6671 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006672int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006673{
Willy Tarreau192252e2015-04-04 01:47:55 +02006674 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006675 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006676 struct hdr_exp *exp;
6677
6678 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006679 int ret;
6680
6681 /*
6682 * The interleaving of transformations and verdicts
6683 * makes it difficult to decide to continue or stop
6684 * the evaluation.
6685 */
6686
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006687 if (txn->flags & TX_SVDENY)
6688 break;
6689
Willy Tarreau3d300592007-03-18 18:34:41 +01006690 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006691 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6692 exp->action == ACT_PASS)) {
6693 exp = exp->next;
6694 continue;
6695 }
6696
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006697 /* if this filter had a condition, evaluate it now and skip to
6698 * next filter if the condition does not match.
6699 */
6700 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006701 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006702 ret = acl_pass(ret);
6703 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6704 ret = !ret;
6705 if (!ret)
6706 continue;
6707 }
6708
Willy Tarreaua15645d2007-03-18 16:22:39 +01006709 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006710 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006711 if (unlikely(ret < 0))
6712 return -1;
6713
6714 if (likely(ret == 0)) {
6715 /* The filter did not match the response, it can be
6716 * iterated through all headers.
6717 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006718 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6719 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006720 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006721 }
6722 return 0;
6723}
6724
6725
Willy Tarreaua15645d2007-03-18 16:22:39 +01006726/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006727 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006728 * desirable to call it only when needed. This function is also used when we
6729 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006730 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006731void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006732{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006733 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006734 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006735 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006736 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006738 char *hdr_beg, *hdr_end, *hdr_next;
6739 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006740
Willy Tarreaua15645d2007-03-18 16:22:39 +01006741 /* Iterate through the headers.
6742 * we start with the start line.
6743 */
6744 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006745 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006746
6747 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6748 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006749 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006750
6751 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006752 hdr_beg = hdr_next;
6753 hdr_end = hdr_beg + cur_hdr->len;
6754 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006755
Willy Tarreau24581ba2010-08-31 22:39:35 +02006756 /* We have one full header between hdr_beg and hdr_end, and the
6757 * next header starts at hdr_next. We're only interested in
6758 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006759 */
6760
Willy Tarreau24581ba2010-08-31 22:39:35 +02006761 is_cookie2 = 0;
6762 prev = hdr_beg + 10;
6763 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006764 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006765 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6766 if (!val) {
6767 old_idx = cur_idx;
6768 continue;
6769 }
6770 is_cookie2 = 1;
6771 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006772 }
6773
Willy Tarreau24581ba2010-08-31 22:39:35 +02006774 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6775 * <prev> points to the colon.
6776 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006777 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006778
Willy Tarreau24581ba2010-08-31 22:39:35 +02006779 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6780 * check-cache is enabled) and we are not interested in checking
6781 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006782 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006783 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006784 return;
6785
Willy Tarreau24581ba2010-08-31 22:39:35 +02006786 /* OK so now we know we have to process this response cookie.
6787 * The format of the Set-Cookie header is slightly different
6788 * from the format of the Cookie header in that it does not
6789 * support the comma as a cookie delimiter (thus the header
6790 * cannot be folded) because the Expires attribute described in
6791 * the original Netscape's spec may contain an unquoted date
6792 * with a comma inside. We have to live with this because
6793 * many browsers don't support Max-Age and some browsers don't
6794 * support quoted strings. However the Set-Cookie2 header is
6795 * clean.
6796 *
6797 * We have to keep multiple pointers in order to support cookie
6798 * removal at the beginning, middle or end of header without
6799 * corrupting the header (in case of set-cookie2). A special
6800 * pointer, <scav> points to the beginning of the set-cookie-av
6801 * fields after the first semi-colon. The <next> pointer points
6802 * either to the end of line (set-cookie) or next unquoted comma
6803 * (set-cookie2). All of these headers are valid :
6804 *
6805 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6806 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6807 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6808 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6809 * | | | | | | | | | |
6810 * | | | | | | | | +-> next hdr_end <--+
6811 * | | | | | | | +------------> scav
6812 * | | | | | | +--------------> val_end
6813 * | | | | | +--------------------> val_beg
6814 * | | | | +----------------------> equal
6815 * | | | +------------------------> att_end
6816 * | | +----------------------------> att_beg
6817 * | +------------------------------> prev
6818 * +-----------------------------------------> hdr_beg
6819 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006820
Willy Tarreau24581ba2010-08-31 22:39:35 +02006821 for (; prev < hdr_end; prev = next) {
6822 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006823
Willy Tarreau24581ba2010-08-31 22:39:35 +02006824 /* find att_beg */
6825 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006826 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006827 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006828
Willy Tarreau24581ba2010-08-31 22:39:35 +02006829 /* find att_end : this is the first character after the last non
6830 * space before the equal. It may be equal to hdr_end.
6831 */
6832 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833
Willy Tarreau24581ba2010-08-31 22:39:35 +02006834 while (equal < hdr_end) {
6835 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6836 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006837 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006838 continue;
6839 att_end = equal;
6840 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841
Willy Tarreau24581ba2010-08-31 22:39:35 +02006842 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6843 * is between <att_beg> and <equal>, both may be identical.
6844 */
6845
6846 /* look for end of cookie if there is an equal sign */
6847 if (equal < hdr_end && *equal == '=') {
6848 /* look for the beginning of the value */
6849 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006850 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006851 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006852
Willy Tarreau24581ba2010-08-31 22:39:35 +02006853 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006854 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006855
6856 /* make val_end point to the first white space or delimitor after the value */
6857 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006858 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006859 val_end--;
6860 } else {
6861 /* <equal> points to next comma, semi-colon or EOL */
6862 val_beg = val_end = next = equal;
6863 }
6864
6865 if (next < hdr_end) {
6866 /* Set-Cookie2 supports multiple cookies, and <next> points to
6867 * a colon or semi-colon before the end. So skip all attr-value
6868 * pairs and look for the next comma. For Set-Cookie, since
6869 * commas are permitted in values, skip to the end.
6870 */
6871 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006872 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006873 else
6874 next = hdr_end;
6875 }
6876
6877 /* Now everything is as on the diagram above */
6878
6879 /* Ignore cookies with no equal sign */
6880 if (equal == val_end)
6881 continue;
6882
6883 /* If there are spaces around the equal sign, we need to
6884 * strip them otherwise we'll get trouble for cookie captures,
6885 * or even for rewrites. Since this happens extremely rarely,
6886 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006887 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006888 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6889 int stripped_before = 0;
6890 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006891
Willy Tarreau24581ba2010-08-31 22:39:35 +02006892 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006893 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006894 equal += stripped_before;
6895 val_beg += stripped_before;
6896 }
6897
6898 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006899 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006900 val_beg += stripped_after;
6901 stripped_before += stripped_after;
6902 }
6903
6904 val_end += stripped_before;
6905 next += stripped_before;
6906 hdr_end += stripped_before;
6907 hdr_next += stripped_before;
6908 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006909 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006910 }
6911
6912 /* First, let's see if we want to capture this cookie. We check
6913 * that we don't already have a server side cookie, because we
6914 * can only capture one. Also as an optimisation, we ignore
6915 * cookies shorter than the declared name.
6916 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006917 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006918 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006919 (val_end - att_beg >= sess->fe->capture_namelen) &&
6920 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006921 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006922 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006923 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01006924 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006925 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006926 if (log_len > sess->fe->capture_len)
6927 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01006928 memcpy(txn->srv_cookie, att_beg, log_len);
6929 txn->srv_cookie[log_len] = 0;
6930 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006931 }
6932
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006933 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006934 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006935 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006936 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6937 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006938 /* assume passive cookie by default */
6939 txn->flags &= ~TX_SCK_MASK;
6940 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006941
6942 /* If the cookie is in insert mode on a known server, we'll delete
6943 * this occurrence because we'll insert another one later.
6944 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006945 * a direct access.
6946 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006947 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006948 /* The "preserve" flag was set, we don't want to touch the
6949 * server's cookie.
6950 */
6951 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006952 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006953 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006954 /* this cookie must be deleted */
6955 if (*prev == ':' && next == hdr_end) {
6956 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006957 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006958 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6959 txn->hdr_idx.used--;
6960 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006961 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006962 hdr_next += delta;
6963 http_msg_move_end(&txn->rsp, delta);
6964 /* note: while both invalid now, <next> and <hdr_end>
6965 * are still equal, so the for() will stop as expected.
6966 */
6967 } else {
6968 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006969 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006970 next = prev;
6971 hdr_end += delta;
6972 hdr_next += delta;
6973 cur_hdr->len += delta;
6974 http_msg_move_end(&txn->rsp, delta);
6975 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006976 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006977 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006978 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006979 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006980 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006981 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006982 * with this server since we know it.
6983 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006984 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006985 next += delta;
6986 hdr_end += delta;
6987 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006988 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006989 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006990
Willy Tarreauf1348312010-10-07 15:54:11 +02006991 txn->flags &= ~TX_SCK_MASK;
6992 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006993 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006994 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006995 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006996 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006997 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006998 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006999 next += delta;
7000 hdr_end += delta;
7001 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007002 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007003 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007004
Willy Tarreau827aee92011-03-10 16:55:02 +01007005 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007006 txn->flags &= ~TX_SCK_MASK;
7007 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007008 }
7009 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007010 /* that's done for this cookie, check the next one on the same
7011 * line when next != hdr_end (only if is_cookie2).
7012 */
7013 }
7014 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007015 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007016 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007017}
7018
7019
Willy Tarreaua15645d2007-03-18 16:22:39 +01007020/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007021 * Parses the Cache-Control and Pragma request header fields to determine if
7022 * the request may be served from the cache and/or if it is cacheable. Updates
7023 * s->txn->flags.
7024 */
7025void check_request_for_cacheability(struct stream *s, struct channel *chn)
7026{
7027 struct http_txn *txn = s->txn;
7028 char *p1, *p2;
7029 char *cur_ptr, *cur_end, *cur_next;
7030 int pragma_found;
7031 int cc_found;
7032 int cur_idx;
7033
7034 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7035 return; /* nothing more to do here */
7036
7037 cur_idx = 0;
7038 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007039 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007040
7041 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7042 struct hdr_idx_elem *cur_hdr;
7043 int val;
7044
7045 cur_hdr = &txn->hdr_idx.v[cur_idx];
7046 cur_ptr = cur_next;
7047 cur_end = cur_ptr + cur_hdr->len;
7048 cur_next = cur_end + cur_hdr->cr + 1;
7049
7050 /* We have one full header between cur_ptr and cur_end, and the
7051 * next header starts at cur_next.
7052 */
7053
7054 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7055 if (val) {
7056 if ((cur_end - (cur_ptr + val) >= 8) &&
7057 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7058 pragma_found = 1;
7059 continue;
7060 }
7061 }
7062
William Lallemand8a16fe02018-05-22 11:04:33 +02007063 /* Don't use the cache and don't try to store if we found the
7064 * Authorization header */
7065 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7066 if (val) {
7067 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7068 txn->flags |= TX_CACHE_IGNORE;
7069 continue;
7070 }
7071
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007072 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7073 if (!val)
7074 continue;
7075
7076 /* OK, right now we know we have a cache-control header at cur_ptr */
7077 cc_found = 1;
7078 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7079
7080 if (p1 >= cur_end) /* no more info */
7081 continue;
7082
7083 /* p1 is at the beginning of the value */
7084 p2 = p1;
7085 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7086 p2++;
7087
7088 /* we have a complete value between p1 and p2. We don't check the
7089 * values after max-age, max-stale nor min-fresh, we simply don't
7090 * use the cache when they're specified.
7091 */
7092 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7093 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7094 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7095 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7096 txn->flags |= TX_CACHE_IGNORE;
7097 continue;
7098 }
7099
7100 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7101 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7102 continue;
7103 }
7104 }
7105
7106 /* RFC7234#5.4:
7107 * When the Cache-Control header field is also present and
7108 * understood in a request, Pragma is ignored.
7109 * When the Cache-Control header field is not present in a
7110 * request, caches MUST consider the no-cache request
7111 * pragma-directive as having the same effect as if
7112 * "Cache-Control: no-cache" were present.
7113 */
7114 if (!cc_found && pragma_found)
7115 txn->flags |= TX_CACHE_IGNORE;
7116}
7117
7118/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007119 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007120 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007121void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007122{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007123 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007124 char *p1, *p2;
7125
7126 char *cur_ptr, *cur_end, *cur_next;
7127 int cur_idx;
7128
Willy Tarreau12b32f22017-12-21 16:08:09 +01007129 if (txn->status < 200) {
7130 /* do not try to cache interim responses! */
7131 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007132 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007133 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134
7135 /* Iterate through the headers.
7136 * we start with the start line.
7137 */
7138 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007139 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007140
7141 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7142 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007143 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007144
7145 cur_hdr = &txn->hdr_idx.v[cur_idx];
7146 cur_ptr = cur_next;
7147 cur_end = cur_ptr + cur_hdr->len;
7148 cur_next = cur_end + cur_hdr->cr + 1;
7149
7150 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007151 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007152 */
7153
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007154 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7155 if (val) {
7156 if ((cur_end - (cur_ptr + val) >= 8) &&
7157 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7158 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7159 return;
7160 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007161 }
7162
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007163 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7164 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007165 continue;
7166
7167 /* OK, right now we know we have a cache-control header at cur_ptr */
7168
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007169 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007170
7171 if (p1 >= cur_end) /* no more info */
7172 continue;
7173
7174 /* p1 is at the beginning of the value */
7175 p2 = p1;
7176
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007177 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007178 p2++;
7179
7180 /* we have a complete value between p1 and p2 */
7181 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007182 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7183 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7184 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7185 continue;
7186 }
7187
Willy Tarreaua15645d2007-03-18 16:22:39 +01007188 /* we have something of the form no-cache="set-cookie" */
7189 if ((cur_end - p1 >= 21) &&
7190 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7191 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007192 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007193 continue;
7194 }
7195
7196 /* OK, so we know that either p2 points to the end of string or to a comma */
7197 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007198 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007199 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007200 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007201 return;
7202 }
7203
7204 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007205 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206 continue;
7207 }
7208 }
7209}
7210
Willy Tarreau58f10d72006-12-04 02:26:12 +01007211
Willy Tarreaub2513902006-12-17 14:52:38 +01007212/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007213 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007214 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007215 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007216 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007217 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007218 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007219 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007220 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007221int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007222{
7223 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007224 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007225 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007226
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007227 if (!uri_auth)
7228 return 0;
7229
Cyril Bonté70be45d2010-10-12 00:14:35 +02007230 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007231 return 0;
7232
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007233 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007234 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007235 return 0;
7236
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007237 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007238 return 0;
7239
Willy Tarreaub2513902006-12-17 14:52:38 +01007240 return 1;
7241}
7242
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007243/* Append the description of what is present in error snapshot <es> into <out>.
7244 * The description must be small enough to always fit in a trash. The output
7245 * buffer may be the trash so the trash must not be used inside this function.
7246 */
7247void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7248{
7249 chunk_appendf(&trash,
7250 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7251 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7252 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7253 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7254 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7255 es->ctx.http.m_flags, es->ctx.http.m_clen,
7256 es->ctx.http.m_blen, es->ctx.http.b_flags);
7257}
7258
Willy Tarreau4076a152009-04-02 15:18:36 +02007259/*
7260 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007261 * By default it tries to report the error position as msg->err_pos. However if
7262 * this one is not set, it will then report msg->next, which is the last known
7263 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007264 * displays buffers as a contiguous area starting at buf->p. The direction is
7265 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007266 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007267void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007268 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007269 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007270{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007271 union error_snapshot_ctx ctx;
7272 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007273
Willy Tarreauef3ca732018-09-07 15:47:35 +02007274 /* http-specific part now */
7275 ctx.http.sid = s->uniq_id;
7276 ctx.http.state = state;
7277 ctx.http.b_flags = msg->chn->flags;
7278 ctx.http.s_flags = s->flags;
7279 ctx.http.t_flags = s->txn->flags;
7280 ctx.http.m_flags = msg->flags;
7281 ctx.http.m_clen = msg->chunk_len;
7282 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007283
Willy Tarreauef3ca732018-09-07 15:47:35 +02007284 ofs = msg->chn->total - ci_data(msg->chn);
7285 if (ofs < 0)
7286 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007287
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007288 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007289 other_end, s->target,
7290 strm_sess(s), &msg->chn->buf,
7291 ofs, co_data(msg->chn),
7292 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7293 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007294}
Willy Tarreaub2513902006-12-17 14:52:38 +01007295
Willy Tarreau294c4732011-12-16 21:35:50 +01007296/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7297 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7298 * performed over the whole headers. Otherwise it must contain a valid header
7299 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7300 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7301 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7302 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007303 * -1. The value fetch stops at commas, so this function is suited for use with
7304 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007305 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007306 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007307unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007308 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007309 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007310{
Willy Tarreau294c4732011-12-16 21:35:50 +01007311 struct hdr_ctx local_ctx;
7312 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007313 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007314 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007315 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007316
Willy Tarreau294c4732011-12-16 21:35:50 +01007317 if (!ctx) {
7318 local_ctx.idx = 0;
7319 ctx = &local_ctx;
7320 }
7321
Willy Tarreaubce70882009-09-07 11:51:47 +02007322 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007323 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007324 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007325 occ--;
7326 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007327 *vptr = ctx->line + ctx->val;
7328 *vlen = ctx->vlen;
7329 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007330 }
7331 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007332 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007333 }
7334
7335 /* negative occurrence, we scan all the list then walk back */
7336 if (-occ > MAX_HDR_HISTORY)
7337 return 0;
7338
Willy Tarreau294c4732011-12-16 21:35:50 +01007339 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007340 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007341 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7342 len_hist[hist_ptr] = ctx->vlen;
7343 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007344 hist_ptr = 0;
7345 found++;
7346 }
7347 if (-occ > found)
7348 return 0;
7349 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007350 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7351 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7352 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007353 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007354 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007355 if (hist_ptr >= MAX_HDR_HISTORY)
7356 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007357 *vptr = ptr_hist[hist_ptr];
7358 *vlen = len_hist[hist_ptr];
7359 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007360}
7361
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007362/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7363 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7364 * performed over the whole headers. Otherwise it must contain a valid header
7365 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7366 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7367 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7368 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7369 * -1. This function differs from http_get_hdr() in that it only returns full
7370 * line header values and does not stop at commas.
7371 * The return value is 0 if nothing was found, or non-zero otherwise.
7372 */
7373unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7374 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007375 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007376{
7377 struct hdr_ctx local_ctx;
7378 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007379 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007380 unsigned int hist_ptr;
7381 int found;
7382
7383 if (!ctx) {
7384 local_ctx.idx = 0;
7385 ctx = &local_ctx;
7386 }
7387
7388 if (occ >= 0) {
7389 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007390 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007391 occ--;
7392 if (occ <= 0) {
7393 *vptr = ctx->line + ctx->val;
7394 *vlen = ctx->vlen;
7395 return 1;
7396 }
7397 }
7398 return 0;
7399 }
7400
7401 /* negative occurrence, we scan all the list then walk back */
7402 if (-occ > MAX_HDR_HISTORY)
7403 return 0;
7404
7405 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007406 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007407 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7408 len_hist[hist_ptr] = ctx->vlen;
7409 if (++hist_ptr >= MAX_HDR_HISTORY)
7410 hist_ptr = 0;
7411 found++;
7412 }
7413 if (-occ > found)
7414 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007415
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007416 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007417 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7418 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7419 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007420 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007421 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007422 if (hist_ptr >= MAX_HDR_HISTORY)
7423 hist_ptr -= MAX_HDR_HISTORY;
7424 *vptr = ptr_hist[hist_ptr];
7425 *vlen = len_hist[hist_ptr];
7426 return 1;
7427}
7428
Willy Tarreaubaaee002006-06-26 02:48:02 +02007429/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007430 * Print a debug line with a header. Always stop at the first CR or LF char,
7431 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7432 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007433 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007434void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007435{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007436 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007437 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007438
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007439 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007440 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007441 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007442 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 +02007443
7444 for (max = 0; start + max < end; max++)
7445 if (start[max] == '\r' || start[max] == '\n')
7446 break;
7447
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007448 UBOUND(max, trash.size - trash.data - 3);
7449 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7450 trash.area[trash.data++] = '\n';
7451 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007452}
7453
Willy Tarreaueee5b512015-04-03 23:46:31 +02007454
7455/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7456 * The hdr_idx is allocated as well. In case of allocation failure, everything
7457 * allocated is freed and NULL is returned. Otherwise the new transaction is
7458 * assigned to the stream and returned.
7459 */
7460struct http_txn *http_alloc_txn(struct stream *s)
7461{
7462 struct http_txn *txn = s->txn;
7463
7464 if (txn)
7465 return txn;
7466
Willy Tarreaubafbe012017-11-24 17:34:44 +01007467 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007468 if (!txn)
7469 return txn;
7470
7471 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007472 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007473 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007474 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007475 return NULL;
7476 }
7477
7478 s->txn = txn;
7479 return txn;
7480}
7481
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007482void http_txn_reset_req(struct http_txn *txn)
7483{
7484 txn->req.flags = 0;
7485 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7486 txn->req.next = 0;
7487 txn->req.chunk_len = 0LL;
7488 txn->req.body_len = 0LL;
7489 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7490}
7491
7492void http_txn_reset_res(struct http_txn *txn)
7493{
7494 txn->rsp.flags = 0;
7495 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7496 txn->rsp.next = 0;
7497 txn->rsp.chunk_len = 0LL;
7498 txn->rsp.body_len = 0LL;
7499 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7500}
7501
Willy Tarreau0937bc42009-12-22 15:03:09 +01007502/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007503 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007504 * the required fields are properly allocated and that we only need to (re)init
7505 * them. This should be used before processing any new request.
7506 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007507void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007508{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007509 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007510 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007511
7512 txn->flags = 0;
7513 txn->status = -1;
7514
Willy Tarreauf64d1412010-10-07 20:06:11 +02007515 txn->cookie_first_date = 0;
7516 txn->cookie_last_date = 0;
7517
Willy Tarreaueee5b512015-04-03 23:46:31 +02007518 txn->srv_cookie = NULL;
7519 txn->cli_cookie = NULL;
7520 txn->uri = NULL;
7521
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007522 http_txn_reset_req(txn);
7523 http_txn_reset_res(txn);
7524
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007525 txn->req.chn = &s->req;
7526 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007527
7528 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007529
7530 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7531 if (fe->options2 & PR_O2_REQBUG_OK)
7532 txn->req.err_pos = -1; /* let buggy requests pass */
7533
Willy Tarreau0937bc42009-12-22 15:03:09 +01007534 if (txn->hdr_idx.v)
7535 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007536
7537 vars_init(&s->vars_txn, SCOPE_TXN);
7538 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007539}
7540
7541/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007542void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007543{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007544 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007545 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007546
7547 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007548 pool_free(pool_head_requri, txn->uri);
7549 pool_free(pool_head_capture, txn->cli_cookie);
7550 pool_free(pool_head_capture, txn->srv_cookie);
7551 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007552
William Lallemanda73203e2012-03-12 12:48:57 +01007553 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007554 txn->uri = NULL;
7555 txn->srv_cookie = NULL;
7556 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007557
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007558 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007559 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007560 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007561 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007562 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007563 }
7564
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007565 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007566 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007567 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007568 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007569 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007570 }
7571
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007572 if (!LIST_ISEMPTY(&s->vars_txn.head))
7573 vars_prune(&s->vars_txn, s->sess, s);
7574 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7575 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007576}
7577
7578/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007579void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007580{
7581 http_end_txn(s);
7582 http_init_txn(s);
7583
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007584 /* reinitialise the current rule list pointer to NULL. We are sure that
7585 * any rulelist match the NULL pointer.
7586 */
7587 s->current_rule_list = NULL;
7588
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007589 s->be = strm_fe(s);
7590 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007591 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007592 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007593 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007594 /* re-init store persistence */
7595 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007596 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007597
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007598 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007599
Willy Tarreau739cfba2010-01-25 23:11:14 +01007600 /* We must trim any excess data from the response buffer, because we
7601 * may have blocked an invalid response from a server that we don't
7602 * want to accidentely forward once we disable the analysers, nor do
7603 * we want those data to come along with next response. A typical
7604 * example of such data would be from a buggy server responding to
7605 * a HEAD with some data, or sending more than the advertised
7606 * content-length.
7607 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007608 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007609 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007610
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007611 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007612 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007613
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007614 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007615 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007616
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007617 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007618 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007619
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007620 s->req.rex = TICK_ETERNITY;
7621 s->req.wex = TICK_ETERNITY;
7622 s->req.analyse_exp = TICK_ETERNITY;
7623 s->res.rex = TICK_ETERNITY;
7624 s->res.wex = TICK_ETERNITY;
7625 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007626 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007627}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007628
Willy Tarreau79e57332018-10-02 16:01:16 +02007629/* This function executes one of the set-{method,path,query,uri} actions. It
7630 * takes the string from the variable 'replace' with length 'len', then modifies
7631 * the relevant part of the request line accordingly. Then it updates various
7632 * pointers to the next elements which were moved, and the total buffer length.
7633 * It finds the action to be performed in p[2], previously filled by function
7634 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7635 * error, though this can be revisited when this code is finally exploited.
7636 *
7637 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7638 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007639 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007640 * In query string case, the mark question '?' must be set at the start of the
7641 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007642 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007643int http_replace_req_line(int action, const char *replace, int len,
7644 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007645{
Willy Tarreau79e57332018-10-02 16:01:16 +02007646 struct http_txn *txn = s->txn;
7647 char *cur_ptr, *cur_end;
7648 int offset = 0;
7649 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007650
Willy Tarreau79e57332018-10-02 16:01:16 +02007651 switch (action) {
7652 case 0: // method
7653 cur_ptr = ci_head(&s->req);
7654 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007655
Willy Tarreau79e57332018-10-02 16:01:16 +02007656 /* adjust req line offsets and lengths */
7657 delta = len - offset - (cur_end - cur_ptr);
7658 txn->req.sl.rq.m_l += delta;
7659 txn->req.sl.rq.u += delta;
7660 txn->req.sl.rq.v += delta;
7661 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007662
Willy Tarreau79e57332018-10-02 16:01:16 +02007663 case 1: // path
7664 cur_ptr = http_txn_get_path(txn);
7665 if (!cur_ptr)
7666 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007667
Willy Tarreau79e57332018-10-02 16:01:16 +02007668 cur_end = cur_ptr;
7669 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7670 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007671
Willy Tarreau79e57332018-10-02 16:01:16 +02007672 /* adjust req line offsets and lengths */
7673 delta = len - offset - (cur_end - cur_ptr);
7674 txn->req.sl.rq.u_l += delta;
7675 txn->req.sl.rq.v += delta;
7676 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007677
Willy Tarreau79e57332018-10-02 16:01:16 +02007678 case 2: // query
7679 offset = 1;
7680 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7681 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7682 while (cur_ptr < cur_end && *cur_ptr != '?')
7683 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007684
Willy Tarreau79e57332018-10-02 16:01:16 +02007685 /* skip the question mark or indicate that we must insert it
7686 * (but only if the format string is not empty then).
7687 */
7688 if (cur_ptr < cur_end)
7689 cur_ptr++;
7690 else if (len > 1)
7691 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007692
Willy Tarreau79e57332018-10-02 16:01:16 +02007693 /* adjust req line offsets and lengths */
7694 delta = len - offset - (cur_end - cur_ptr);
7695 txn->req.sl.rq.u_l += delta;
7696 txn->req.sl.rq.v += delta;
7697 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007698
Willy Tarreau79e57332018-10-02 16:01:16 +02007699 case 3: // uri
7700 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7701 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007702
Willy Tarreau79e57332018-10-02 16:01:16 +02007703 /* adjust req line offsets and lengths */
7704 delta = len - offset - (cur_end - cur_ptr);
7705 txn->req.sl.rq.u_l += delta;
7706 txn->req.sl.rq.v += delta;
7707 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007708
Willy Tarreau79e57332018-10-02 16:01:16 +02007709 default:
7710 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007711 }
7712
Willy Tarreau79e57332018-10-02 16:01:16 +02007713 /* commit changes and adjust end of message */
7714 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7715 txn->req.sl.rq.l += delta;
7716 txn->hdr_idx.v[0].len += delta;
7717 http_msg_move_end(&txn->req, delta);
7718 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007719}
7720
Willy Tarreau79e57332018-10-02 16:01:16 +02007721/* This function replace the HTTP status code and the associated message. The
7722 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007723 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007724void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007725{
Willy Tarreau79e57332018-10-02 16:01:16 +02007726 struct http_txn *txn = s->txn;
7727 char *cur_ptr, *cur_end;
7728 int delta;
7729 char *res;
7730 int c_l;
7731 const char *msg = reason;
7732 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007733
Willy Tarreau79e57332018-10-02 16:01:16 +02007734 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007735
Willy Tarreau79e57332018-10-02 16:01:16 +02007736 res = ultoa_o(status, trash.area, trash.size);
7737 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007738
Willy Tarreau79e57332018-10-02 16:01:16 +02007739 trash.area[c_l] = ' ';
7740 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007741
Willy Tarreau79e57332018-10-02 16:01:16 +02007742 /* Do we have a custom reason format string? */
7743 if (msg == NULL)
7744 msg = http_get_reason(status);
7745 msg_len = strlen(msg);
7746 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7747 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007748
Willy Tarreau79e57332018-10-02 16:01:16 +02007749 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7750 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007751
Willy Tarreau79e57332018-10-02 16:01:16 +02007752 /* commit changes and adjust message */
7753 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7754 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007755
Willy Tarreau79e57332018-10-02 16:01:16 +02007756 /* adjust res line offsets and lengths */
7757 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7758 txn->rsp.sl.st.c_l = c_l;
7759 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007760
Willy Tarreau79e57332018-10-02 16:01:16 +02007761 delta = trash.data - (cur_end - cur_ptr);
7762 txn->rsp.sl.st.l += delta;
7763 txn->hdr_idx.v[0].len += delta;
7764 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007765}
7766
Willy Tarreau8797c062007-05-07 00:55:35 +02007767__attribute__((constructor))
7768static void __http_protocol_init(void)
7769{
Willy Tarreau8797c062007-05-07 00:55:35 +02007770}
7771
7772
Willy Tarreau58f10d72006-12-04 02:26:12 +01007773/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007774 * Local variables:
7775 * c-indent-level: 8
7776 * c-basic-offset: 8
7777 * End:
7778 */