blob: 39900deac17e57da620d3801bdf32dd7bb16e202 [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 Tarreau6b952c82018-09-10 17:45:34 +0200443 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 +0100444
Willy Tarreau6b952c82018-09-10 17:45:34 +0200445 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100446}
447
Willy Tarreau71241ab2012-12-27 11:30:54 +0100448/* Returns a 302 for a redirectable request that reaches a server working in
449 * in redirect mode. This may only be called just after the stream interface
450 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
451 * follow normal proxy processing. NOTE: this function is designed to support
452 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100453 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200454void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100455{
456 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100457 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100458 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200459 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100460
461 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200462 trash.data = strlen(HTTP_302);
463 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100464
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200465 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100466
Willy Tarreauefb453c2008-10-26 20:49:47 +0100467 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200468 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100469 return;
470
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100471 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100472 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200473 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
474 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100475 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100476
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200477 /* 3: add the request URI. Since it was already forwarded, we need
478 * to temporarily rewind the buffer.
479 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200480 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200481 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200482
Willy Tarreau6b952c82018-09-10 17:45:34 +0200483 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200484 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 +0200485
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200486 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200487
Willy Tarreauefb453c2008-10-26 20:49:47 +0100488 if (!path)
489 return;
490
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200491 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100492 return;
493
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200494 memcpy(trash.area + trash.data, path, len);
495 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100496
497 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200498 memcpy(trash.area + trash.data,
499 "\r\nProxy-Connection: close\r\n\r\n", 29);
500 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100501 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200502 memcpy(trash.area + trash.data,
503 "\r\nConnection: close\r\n\r\n", 23);
504 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100505 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100506
507 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200508 si_shutr(si);
509 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100510 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100511 si->state = SI_ST_CLO;
512
513 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100514 txn->status = 302;
515 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100516
517 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100518 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500519 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100520}
521
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100522/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100523 * that the server side is closed. Note that err_type is actually a
524 * bitmask, where almost only aborts may be cumulated with other
525 * values. We consider that aborted operations are more important
526 * than timeouts or errors due to the fact that nobody else in the
527 * logs might explain incomplete retries. All others should avoid
528 * being cumulated. It should normally not be possible to have multiple
529 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100530 * Note that connection errors appearing on the second request of a keep-alive
531 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100532 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200533void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100534{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100535 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100536
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200537 /* set s->txn->status for http_error_message(s) */
538 s->txn->status = 503;
539
Willy Tarreauefb453c2008-10-26 20:49:47 +0100540 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200541 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100542 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100543 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200544 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100545 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200546 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100547 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200548 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100549 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100550 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200551 http_server_error(s, si, SF_ERR_SRVCL, 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_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200554 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100555 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200556 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100557 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200558 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100559 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200560 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200561 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200562 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100563 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200564 http_error_message(s));
565 else { /* SI_ET_CONN_OTHER and others */
566 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200567 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100568 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200569 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100570}
571
Willy Tarreau42250582007-04-01 01:30:43 +0200572extern const char sess_term_cond[8];
573extern const char sess_fin_state[8];
574extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +0100575struct pool_head *pool_head_http_txn;
576struct pool_head *pool_head_requri;
577struct pool_head *pool_head_capture = NULL;
578struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100579
Willy Tarreau117f59e2007-03-04 18:17:17 +0100580/*
581 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200582 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100583 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200584void http_capture_headers(char *som, struct hdr_idx *idx,
585 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100586{
587 char *eol, *sol, *col, *sov;
588 int cur_idx;
589 struct cap_hdr *h;
590 int len;
591
592 sol = som + hdr_idx_first_pos(idx);
593 cur_idx = hdr_idx_first_idx(idx);
594
595 while (cur_idx) {
596 eol = sol + idx->v[cur_idx].len;
597
598 col = sol;
599 while (col < eol && *col != ':')
600 col++;
601
602 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100603 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100604 sov++;
605
606 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200607 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100608 (strncasecmp(sol, h->name, h->namelen) == 0)) {
609 if (cap[h->index] == NULL)
610 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100611 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100612
613 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100614 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100615 continue;
616 }
617
618 len = eol - sov;
619 if (len > h->len)
620 len = h->len;
621
622 memcpy(cap[h->index], sov, len);
623 cap[h->index][len]=0;
624 }
625 }
626 sol = eol + idx->v[cur_idx].cr + 1;
627 cur_idx = idx->v[cur_idx].next;
628 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100629}
630
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200631/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
632 * conversion succeeded, 0 in case of error. If the request was already 1.X,
633 * nothing is done and 1 is returned.
634 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200635int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200636{
637 int delta;
638 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100639 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200640
641 if (msg->sl.rq.v_l != 0)
642 return 1;
643
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300644 /* RFC 1945 allows only GET for HTTP/0.9 requests */
645 if (txn->meth != HTTP_METH_GET)
646 return 0;
647
Willy Tarreauf37954d2018-06-15 18:31:02 +0200648 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200649
650 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300651 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
652 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200653 }
654 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200655 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100656 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200657 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200658 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200659 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200660 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200661 NULL, NULL);
662 if (unlikely(!cur_end))
663 return 0;
664
665 /* we have a full HTTP/1.0 request now and we know that
666 * we have either a CR or an LF at <ptr>.
667 */
668 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
669 return 1;
670}
671
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100672/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100673 * and "keep-alive" values. If we already know that some headers may safely
674 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100675 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
676 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100677 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100678 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
679 * found, and TX_CON_*_SET is adjusted depending on what is left so only
680 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100681 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100682 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100683void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100684{
Willy Tarreau5b154472009-12-21 20:11:07 +0100685 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100686 const char *hdr_val = "Connection";
687 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100688
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100689 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100690 return;
691
Willy Tarreau88d349d2010-01-25 12:15:43 +0100692 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
693 hdr_val = "Proxy-Connection";
694 hdr_len = 16;
695 }
696
Willy Tarreau5b154472009-12-21 20:11:07 +0100697 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100698 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200699 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100700 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
701 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100702 if (to_del & 2)
703 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100704 else
705 txn->flags |= TX_CON_KAL_SET;
706 }
707 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
708 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100709 if (to_del & 1)
710 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100711 else
712 txn->flags |= TX_CON_CLO_SET;
713 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100714 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
715 txn->flags |= TX_HDR_CONN_UPG;
716 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100717 }
718
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100719 txn->flags |= TX_HDR_CONN_PRS;
720 return;
721}
Willy Tarreau5b154472009-12-21 20:11:07 +0100722
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100723/* Apply desired changes on the Connection: header. Values may be removed and/or
724 * added depending on the <wanted> flags, which are exclusively composed of
725 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
726 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
727 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100728void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100729{
730 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100731 const char *hdr_val = "Connection";
732 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100733
734 ctx.idx = 0;
735
Willy Tarreau88d349d2010-01-25 12:15:43 +0100736
737 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
738 hdr_val = "Proxy-Connection";
739 hdr_len = 16;
740 }
741
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100742 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200743 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100744 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
745 if (wanted & TX_CON_KAL_SET)
746 txn->flags |= TX_CON_KAL_SET;
747 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100748 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100749 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100750 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
751 if (wanted & TX_CON_CLO_SET)
752 txn->flags |= TX_CON_CLO_SET;
753 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100754 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100755 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100756 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100757
758 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
759 return;
760
761 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
762 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100763 hdr_val = "Connection: close";
764 hdr_len = 17;
765 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
766 hdr_val = "Proxy-Connection: close";
767 hdr_len = 23;
768 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100769 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100770 }
771
772 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
773 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100774 hdr_val = "Connection: keep-alive";
775 hdr_len = 22;
776 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
777 hdr_val = "Proxy-Connection: keep-alive";
778 hdr_len = 28;
779 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100780 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100781 }
782 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100783}
784
Willy Tarreau87b09662015-04-03 00:22:06 +0200785void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200786{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200787 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200788 int tmp = TX_CON_WANT_KAL;
789
Christopher Faulet315b39c2018-09-21 16:26:19 +0200790 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
791 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
792 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200793
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200794 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200795 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200796 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200797
Christopher Faulet315b39c2018-09-21 16:26:19 +0200798 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
799 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200800 tmp = TX_CON_WANT_CLO;
801
802 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
803 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
804
805 if (!(txn->flags & TX_HDR_CONN_PRS) &&
806 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
807 /* parse the Connection header and possibly clean it */
808 int to_del = 0;
809 if ((msg->flags & HTTP_MSGF_VER_11) ||
810 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200811 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200812 to_del |= 2; /* remove "keep-alive" */
813 if (!(msg->flags & HTTP_MSGF_VER_11))
814 to_del |= 1; /* remove "close" */
815 http_parse_connection_header(txn, msg, to_del);
816 }
817
818 /* check if client or config asks for explicit close in KAL/SCL */
819 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
820 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
821 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
822 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
823 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200824 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200825 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
826}
William Lallemand82fe75c2012-10-23 10:25:10 +0200827
Willy Tarreaud787e662009-07-07 10:14:51 +0200828/* This stream analyser waits for a complete HTTP request. It returns 1 if the
829 * processing can continue on next analysers, or zero if it either needs more
830 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100831 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200832 * when it has nothing left to do, and may remove any analyser when it wants to
833 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100834 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200835int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100836{
Willy Tarreau59234e92008-11-30 23:51:27 +0100837 /*
838 * We will parse the partial (or complete) lines.
839 * We will check the request syntax, and also join multi-line
840 * headers. An index of all the lines will be elaborated while
841 * parsing.
842 *
843 * For the parsing, we use a 28 states FSM.
844 *
845 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200846 * ci_head(req) = beginning of request
847 * ci_head(req) + msg->eoh = end of processed headers / start of current one
848 * ci_tail(req) = end of input data
849 * msg->eol = end of current header or line (LF or CRLF)
850 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200851 *
852 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200853 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200854 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
855 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100856 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100857
Willy Tarreau59234e92008-11-30 23:51:27 +0100858 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200859 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200860 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100861 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200862 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100863
Christopher Faulet45073512018-07-20 10:16:29 +0200864 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 +0100865 now_ms, __FUNCTION__,
866 s,
867 req,
868 req->rex, req->wex,
869 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200870 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100871 req->analysers);
872
Willy Tarreau52a0c602009-08-16 22:45:38 +0200873 /* we're speaking HTTP here, so let's speak HTTP to the client */
874 s->srv_error = http_return_srv_error;
875
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300876 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200877 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300878 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
879
Willy Tarreau83e3af02009-12-28 17:39:57 +0100880 /* There's a protected area at the end of the buffer for rewriting
881 * purposes. We don't want to start to parse the request if the
882 * protected area is affected, because we may have to move processed
883 * data later, which is much more complicated.
884 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200885 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200886 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100887 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200888 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100889 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100890 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200891 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200892 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100893 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100894 return 0;
895 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200896 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200897 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200898 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100899 }
900
Willy Tarreauf37954d2018-06-15 18:31:02 +0200901 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100902 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100903 }
904
Willy Tarreau59234e92008-11-30 23:51:27 +0100905 /* 1: we might have to print this header in debug mode */
906 if (unlikely((global.mode & MODE_DEBUG) &&
907 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200908 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100909 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100910
Willy Tarreauf37954d2018-06-15 18:31:02 +0200911 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200912 /* this is a bit complex : in case of error on the request line,
913 * we know that rq.l is still zero, so we display only the part
914 * up to the end of the line (truncated by debug_hdr).
915 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200916 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100917 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100918
Willy Tarreau59234e92008-11-30 23:51:27 +0100919 sol += hdr_idx_first_pos(&txn->hdr_idx);
920 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100921
Willy Tarreau59234e92008-11-30 23:51:27 +0100922 while (cur_idx) {
923 eol = sol + txn->hdr_idx.v[cur_idx].len;
924 debug_hdr("clihdr", s, sol, eol);
925 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
926 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100927 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100928 }
929
Willy Tarreau58f10d72006-12-04 02:26:12 +0100930
Willy Tarreau59234e92008-11-30 23:51:27 +0100931 /*
932 * Now we quickly check if we have found a full valid request.
933 * If not so, we check the FD and buffer states before leaving.
934 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100935 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100936 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200937 * on a keep-alive stream, if we encounter and error, close, t/o,
938 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100939 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200940 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200941 * Last, we may increase some tracked counters' http request errors on
942 * the cases that are deliberately the client's fault. For instance,
943 * a timeout or connection reset is not counted as an error. However
944 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100945 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100946
Willy Tarreau655dce92009-11-08 13:10:58 +0100947 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100948 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100949 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100950 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100951 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200952 stream_inc_http_req_ctr(s);
953 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200954 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100955 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100956 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100957
Willy Tarreau59234e92008-11-30 23:51:27 +0100958 /* 1: Since we are in header mode, if there's no space
959 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200960 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100961 * must terminate it now.
962 */
Willy Tarreau23752332018-06-15 14:54:53 +0200963 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100964 /* FIXME: check if URI is set and return Status
965 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100966 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200967 stream_inc_http_req_ctr(s);
968 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200969 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200970 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200971 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100972 goto return_bad_req;
973 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100974
Willy Tarreau59234e92008-11-30 23:51:27 +0100975 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200976 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200977 if (!(s->flags & SF_ERR_MASK))
978 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100979
Willy Tarreaufcffa692010-01-10 14:21:19 +0100980 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100981 goto failed_keep_alive;
982
Willy Tarreau0f228a02015-05-01 15:37:53 +0200983 if (sess->fe->options & PR_O_IGNORE_PRB)
984 goto failed_keep_alive;
985
Willy Tarreau59234e92008-11-30 23:51:27 +0100986 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200987 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200988 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200989 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200990 }
991
Willy Tarreaudc979f22012-12-04 10:39:01 +0100992 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100993 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100994 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100995 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +0100996 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +0200997 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200998 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200999 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001000 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001001 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001002
Willy Tarreaue7dff022015-04-03 01:14:29 +02001003 if (!(s->flags & SF_FINST_MASK))
1004 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001005 return 0;
1006 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001007
Willy Tarreau59234e92008-11-30 23:51:27 +01001008 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001009 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001010 if (!(s->flags & SF_ERR_MASK))
1011 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001012
Willy Tarreaufcffa692010-01-10 14:21:19 +01001013 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001014 goto failed_keep_alive;
1015
Willy Tarreau0f228a02015-05-01 15:37:53 +02001016 if (sess->fe->options & PR_O_IGNORE_PRB)
1017 goto failed_keep_alive;
1018
Willy Tarreau59234e92008-11-30 23:51:27 +01001019 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001020 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001021 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001022 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001023 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001024 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001025 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001026 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001027 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001028 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001029
Willy Tarreau87b09662015-04-03 00:22:06 +02001030 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001031 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001032 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001033 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001034 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001035
Willy Tarreaue7dff022015-04-03 01:14:29 +02001036 if (!(s->flags & SF_FINST_MASK))
1037 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001038 return 0;
1039 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001040
Willy Tarreau59234e92008-11-30 23:51:27 +01001041 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001042 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001043 if (!(s->flags & SF_ERR_MASK))
1044 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001045
Willy Tarreaufcffa692010-01-10 14:21:19 +01001046 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001047 goto failed_keep_alive;
1048
Willy Tarreau0f228a02015-05-01 15:37:53 +02001049 if (sess->fe->options & PR_O_IGNORE_PRB)
1050 goto failed_keep_alive;
1051
Willy Tarreau4076a152009-04-02 15:18:36 +02001052 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001053 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001054 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001055 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001056 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001057 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001058 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001059 stream_inc_http_err_ctr(s);
1060 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001061 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001062 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001063 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001064 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001065
Willy Tarreaue7dff022015-04-03 01:14:29 +02001066 if (!(s->flags & SF_FINST_MASK))
1067 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001068 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001069 }
1070
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001071 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001072 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001073 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001074#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001075 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001076 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001077 /* We need more data, we have to re-enable quick-ack in case we
1078 * previously disabled it, otherwise we might cause the client
1079 * to delay next data.
1080 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001081 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001082 }
1083#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001084
Willy Tarreaufcffa692010-01-10 14:21:19 +01001085 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1086 /* If the client starts to talk, let's fall back to
1087 * request timeout processing.
1088 */
1089 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001090 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001091 }
1092
Willy Tarreau59234e92008-11-30 23:51:27 +01001093 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001094 if (!tick_isset(req->analyse_exp)) {
1095 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1096 (txn->flags & TX_WAIT_NEXT_RQ) &&
1097 tick_isset(s->be->timeout.httpka))
1098 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1099 else
1100 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1101 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001102
Willy Tarreau59234e92008-11-30 23:51:27 +01001103 /* we're not ready yet */
1104 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001105
1106 failed_keep_alive:
1107 /* Here we process low-level errors for keep-alive requests. In
1108 * short, if the request is not the first one and it experiences
1109 * a timeout, read error or shutdown, we just silently close so
1110 * that the client can try again.
1111 */
1112 txn->status = 0;
1113 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001114 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001115 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001116 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001117 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001118 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001119 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001120 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001121
Willy Tarreaud787e662009-07-07 10:14:51 +02001122 /* OK now we have a complete HTTP request with indexed headers. Let's
1123 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001124 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001125 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001126 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001127 * byte after the last LF. msg->sov points to the first byte of data.
1128 * msg->eol cannot be trusted because it may have been left uninitialized
1129 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001130 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001131
Willy Tarreau87b09662015-04-03 00:22:06 +02001132 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001133 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001134
Willy Tarreaub16a5742010-01-10 14:46:16 +01001135 if (txn->flags & TX_WAIT_NEXT_RQ) {
1136 /* kill the pending keep-alive timeout */
1137 txn->flags &= ~TX_WAIT_NEXT_RQ;
1138 req->analyse_exp = TICK_ETERNITY;
1139 }
1140
1141
Willy Tarreaud787e662009-07-07 10:14:51 +02001142 /* Maybe we found in invalid header name while we were configured not
1143 * to block on that, so we have to capture it now.
1144 */
1145 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001146 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001147
Willy Tarreau59234e92008-11-30 23:51:27 +01001148 /*
1149 * 1: identify the method
1150 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001151 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001152
1153 /* we can make use of server redirect on GET and HEAD */
1154 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001155 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001156 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001157 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001158 /* PRI is reserved for the HTTP/2 preface */
1159 msg->err_pos = 0;
1160 goto return_bad_req;
1161 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001162
Willy Tarreau59234e92008-11-30 23:51:27 +01001163 /*
1164 * 2: check if the URI matches the monitor_uri.
1165 * We have to do this for every request which gets in, because
1166 * the monitor-uri is defined by the frontend.
1167 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001168 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1169 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001170 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001171 sess->fe->monitor_uri,
1172 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001173 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001174 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001175 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001176 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001177
Willy Tarreaue7dff022015-04-03 01:14:29 +02001178 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001179 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001180
Willy Tarreau59234e92008-11-30 23:51:27 +01001181 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001182 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001183 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001184
Willy Tarreau59234e92008-11-30 23:51:27 +01001185 ret = acl_pass(ret);
1186 if (cond->pol == ACL_COND_UNLESS)
1187 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001188
Willy Tarreau59234e92008-11-30 23:51:27 +01001189 if (ret) {
1190 /* we fail this request, let's return 503 service unavail */
1191 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001192 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001193 if (!(s->flags & SF_ERR_MASK))
1194 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001195 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001196 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001197 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001198
Willy Tarreau59234e92008-11-30 23:51:27 +01001199 /* nothing to fail, let's reply normaly */
1200 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001201 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001202 if (!(s->flags & SF_ERR_MASK))
1203 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001204 goto return_prx_cond;
1205 }
1206
1207 /*
1208 * 3: Maybe we have to copy the original REQURI for the logs ?
1209 * Note: we cannot log anymore if the request has been
1210 * classified as invalid.
1211 */
1212 if (unlikely(s->logs.logwait & LW_REQ)) {
1213 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001214 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001215 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001216
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001217 if (urilen >= global.tune.requri_len )
1218 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001219 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001220 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001221
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001222 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001223 s->do_log(s);
1224 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001225 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001227 }
Willy Tarreau06619262006-12-17 08:37:22 +01001228
Willy Tarreau91852eb2015-05-01 13:26:00 +02001229 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1230 * exactly one digit "." one digit. This check may be disabled using
1231 * option accept-invalid-http-request.
1232 */
1233 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1234 if (msg->sl.rq.v_l != 8) {
1235 msg->err_pos = msg->sl.rq.v;
1236 goto return_bad_req;
1237 }
1238
Willy Tarreauf37954d2018-06-15 18:31:02 +02001239 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1240 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1241 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1242 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001243 msg->err_pos = msg->sl.rq.v + 4;
1244 goto return_bad_req;
1245 }
1246 }
Willy Tarreau13317662015-05-01 13:47:08 +02001247 else {
1248 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1249 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1250 goto return_bad_req;
1251 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001252
Willy Tarreau5b154472009-12-21 20:11:07 +01001253 /* ... and check if the request is HTTP/1.1 or above */
1254 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001255 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1256 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1257 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001258 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001259
1260 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001261 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 +01001262
Willy Tarreau88d349d2010-01-25 12:15:43 +01001263 /* if the frontend has "option http-use-proxy-header", we'll check if
1264 * we have what looks like a proxied connection instead of a connection,
1265 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1266 * Note that this is *not* RFC-compliant, however browsers and proxies
1267 * happen to do that despite being non-standard :-(
1268 * We consider that a request not beginning with either '/' or '*' is
1269 * a proxied connection, which covers both "scheme://location" and
1270 * CONNECT ip:port.
1271 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001272 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001273 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001274 txn->flags |= TX_USE_PX_CONN;
1275
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001276 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001277 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001278
Willy Tarreau59234e92008-11-30 23:51:27 +01001279 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001280 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001281 http_capture_headers(ci_head(req), &txn->hdr_idx,
1282 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001283
Willy Tarreau557f1992015-05-01 10:05:17 +02001284 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1285 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001286 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001287 * The length of a message body is determined by one of the following
1288 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001289 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001290 * 1. Any response to a HEAD request and any response with a 1xx
1291 * (Informational), 204 (No Content), or 304 (Not Modified) status
1292 * code is always terminated by the first empty line after the
1293 * header fields, regardless of the header fields present in the
1294 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001295 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001296 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1297 * the connection will become a tunnel immediately after the empty
1298 * line that concludes the header fields. A client MUST ignore any
1299 * Content-Length or Transfer-Encoding header fields received in
1300 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001301 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001302 * 3. If a Transfer-Encoding header field is present and the chunked
1303 * transfer coding (Section 4.1) is the final encoding, the message
1304 * body length is determined by reading and decoding the chunked
1305 * data until the transfer coding indicates the data is complete.
1306 *
1307 * If a Transfer-Encoding header field is present in a response and
1308 * the chunked transfer coding is not the final encoding, the
1309 * message body length is determined by reading the connection until
1310 * it is closed by the server. If a Transfer-Encoding header field
1311 * is present in a request and the chunked transfer coding is not
1312 * the final encoding, the message body length cannot be determined
1313 * reliably; the server MUST respond with the 400 (Bad Request)
1314 * status code and then close the connection.
1315 *
1316 * If a message is received with both a Transfer-Encoding and a
1317 * Content-Length header field, the Transfer-Encoding overrides the
1318 * Content-Length. Such a message might indicate an attempt to
1319 * perform request smuggling (Section 9.5) or response splitting
1320 * (Section 9.4) and ought to be handled as an error. A sender MUST
1321 * remove the received Content-Length field prior to forwarding such
1322 * a message downstream.
1323 *
1324 * 4. If a message is received without Transfer-Encoding and with
1325 * either multiple Content-Length header fields having differing
1326 * field-values or a single Content-Length header field having an
1327 * invalid value, then the message framing is invalid and the
1328 * recipient MUST treat it as an unrecoverable error. If this is a
1329 * request message, the server MUST respond with a 400 (Bad Request)
1330 * status code and then close the connection. If this is a response
1331 * message received by a proxy, the proxy MUST close the connection
1332 * to the server, discard the received response, and send a 502 (Bad
1333 * Gateway) response to the client. If this is a response message
1334 * received by a user agent, the user agent MUST close the
1335 * connection to the server and discard the received response.
1336 *
1337 * 5. If a valid Content-Length header field is present without
1338 * Transfer-Encoding, its decimal value defines the expected message
1339 * body length in octets. If the sender closes the connection or
1340 * the recipient times out before the indicated number of octets are
1341 * received, the recipient MUST consider the message to be
1342 * incomplete and close the connection.
1343 *
1344 * 6. If this is a request message and none of the above are true, then
1345 * the message body length is zero (no message body is present).
1346 *
1347 * 7. Otherwise, this is a response message without a declared message
1348 * body length, so the message body length is determined by the
1349 * number of octets received prior to the server closing the
1350 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001351 */
1352
Willy Tarreau32b47f42009-10-18 20:55:02 +02001353 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001354 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001355 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001356 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001357 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001358 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001359 /* chunked not last, return badreq */
1360 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001361 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001362 }
1363
Willy Tarreau1c913912015-04-30 10:57:51 +02001364 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001365 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001366 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001367 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001368 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1369 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001370 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001371 signed long long cl;
1372
Willy Tarreauad14f752011-09-02 20:33:27 +02001373 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001374 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001375 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001376 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001377
Willy Tarreauad14f752011-09-02 20:33:27 +02001378 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001379 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001380 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001381 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001382
Willy Tarreauad14f752011-09-02 20:33:27 +02001383 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001384 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001385 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001386 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001387
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001388 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001389 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001390 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001391 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001392
Christopher Fauletbe821b92017-03-30 11:21:53 +02001393 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001394 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001395 }
1396
Willy Tarreau34dfc602015-05-01 10:09:49 +02001397 /* even bodyless requests have a known length */
1398 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001399
Willy Tarreau179085c2014-04-28 16:48:56 +02001400 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1401 * only change if both the request and the config reference something else.
1402 * Option httpclose by itself sets tunnel mode where headers are mangled.
1403 * However, if another mode is set, it will affect it (eg: server-close/
1404 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1405 * if FE and BE have the same settings (common). The method consists in
1406 * checking if options changed between the two calls (implying that either
1407 * one is non-null, or one of them is non-null and we are there for the first
1408 * time.
1409 */
1410 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001411 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001412 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001413
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001414 /* we may have to wait for the request's body */
1415 if ((s->be->options & PR_O_WREQ_BODY) &&
1416 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1417 req->analysers |= AN_REQ_HTTP_BODY;
1418
Willy Tarreau83ece462017-12-21 15:13:09 +01001419 /*
1420 * RFC7234#4:
1421 * A cache MUST write through requests with methods
1422 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1423 * the origin server; i.e., a cache is not allowed
1424 * to generate a reply to such a request before
1425 * having forwarded the request and having received
1426 * a corresponding response.
1427 *
1428 * RFC7231#4.2.1:
1429 * Of the request methods defined by this
1430 * specification, the GET, HEAD, OPTIONS, and TRACE
1431 * methods are defined to be safe.
1432 */
1433 if (likely(txn->meth == HTTP_METH_GET ||
1434 txn->meth == HTTP_METH_HEAD ||
1435 txn->meth == HTTP_METH_OPTIONS ||
1436 txn->meth == HTTP_METH_TRACE))
1437 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1438
Willy Tarreaud787e662009-07-07 10:14:51 +02001439 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001440 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001441 req->analyse_exp = TICK_ETERNITY;
1442 return 1;
1443
1444 return_bad_req:
1445 /* We centralize bad requests processing here */
1446 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1447 /* we detected a parsing error. We want to archive this request
1448 * in the dedicated proxy area for later troubleshooting.
1449 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001450 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001451 }
1452
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001453 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001454 txn->req.msg_state = HTTP_MSG_ERROR;
1455 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001456 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001457
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001458 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001459 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001460 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001461
1462 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001463 if (!(s->flags & SF_ERR_MASK))
1464 s->flags |= SF_ERR_PRXCOND;
1465 if (!(s->flags & SF_FINST_MASK))
1466 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001467
Christopher Faulet0184ea72017-01-05 14:06:34 +01001468 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001469 req->analyse_exp = TICK_ETERNITY;
1470 return 0;
1471}
1472
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001473
Willy Tarreau347a35d2013-11-22 17:51:09 +01001474/* This function prepares an applet to handle the stats. It can deal with the
1475 * "100-continue" expectation, check that admin rules are met for POST requests,
1476 * and program a response message if something was unexpected. It cannot fail
1477 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001478 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001479 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001480 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001481 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001482int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001483{
1484 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001485 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001486 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001487 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001488 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001489 struct uri_auth *uri_auth = s->be->uri_auth;
1490 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001491 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001492
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001493 appctx = si_appctx(si);
1494 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1495 appctx->st1 = appctx->st2 = 0;
1496 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1497 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001498 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001499 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001500
Willy Tarreauf37954d2018-06-15 18:31:02 +02001501 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001502 lookup = uri + uri_auth->uri_len;
1503
1504 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1505 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001506 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001507 break;
1508 }
1509 }
1510
1511 if (uri_auth->refresh) {
1512 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1513 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001514 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001515 break;
1516 }
1517 }
1518 }
1519
1520 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1521 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001522 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001523 break;
1524 }
1525 }
1526
Willy Tarreau1e62df92016-01-11 18:57:53 +01001527 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1528 if (memcmp(h, ";typed", 6) == 0) {
1529 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1530 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1531 break;
1532 }
1533 }
1534
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001535 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1536 if (memcmp(h, ";st=", 4) == 0) {
1537 int i;
1538 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001539 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001540 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1541 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001542 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001543 break;
1544 }
1545 }
1546 break;
1547 }
1548 }
1549
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001550 appctx->ctx.stats.scope_str = 0;
1551 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001552 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1553 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1554 int itx = 0;
1555 const char *h2;
1556 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1557 const char *err;
1558
1559 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1560 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001561 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001562 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1563 itx++;
1564 h++;
1565 }
1566
1567 if (itx > STAT_SCOPE_TXT_MAXLEN)
1568 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001569 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001570
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001571 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001572 memcpy(scope_txt, h2, itx);
1573 scope_txt[itx] = '\0';
1574 err = invalid_char(scope_txt);
1575 if (err) {
1576 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001577 appctx->ctx.stats.scope_str = 0;
1578 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001579 }
1580 break;
1581 }
1582 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001583
1584 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001585 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001586 int ret = 1;
1587
1588 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001589 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 +01001590 ret = acl_pass(ret);
1591 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1592 ret = !ret;
1593 }
1594
1595 if (ret) {
1596 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001597 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001598 break;
1599 }
1600 }
1601
1602 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001603 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001604 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001605 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001606 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1607 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001608 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001609 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001610 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001611 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1612 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001613 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001614 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001615 else {
1616 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001617 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001618 }
1619
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001620 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001621 return 1;
1622}
1623
Lukas Tribus67db8df2013-06-23 17:37:13 +02001624/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1625 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1626 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001627void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001628{
1629#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001630 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001631 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1632#endif
1633#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001634 if (from->ss_family == AF_INET6) {
1635 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001636 /* v4-mapped addresses need IP_TOS */
1637 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1638 else
1639 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1640 }
1641#endif
1642}
1643
Willy Tarreau87b09662015-04-03 00:22:06 +02001644int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001645 const char* name, unsigned int name_len,
1646 const char *str, struct my_regex *re,
1647 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001648{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001649 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001650 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001651 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001652 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1653 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001654 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001655
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001656 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001657
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001658 /* Choose the header browsing function. */
1659 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001660 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001661 http_find_hdr_func = http_find_header2;
1662 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001663 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001664 http_find_hdr_func = http_find_full_header2;
1665 break;
1666 default: /* impossible */
1667 return -1;
1668 }
1669
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001670 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1671 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001672 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001673 char *val = ctx.line + ctx.val;
1674 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001675
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001676 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1677 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001678
Willy Tarreau6e27be12018-08-22 04:46:47 +02001679 len = exp_replace(output->area, output->size, val, str, pmatch);
1680 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001681 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001682
Willy Tarreau6e27be12018-08-22 04:46:47 +02001683 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001684
1685 hdr->len += delta;
1686 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001687
1688 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001689 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001690 }
1691
1692 return 0;
1693}
1694
Willy Tarreau87b09662015-04-03 00:22:06 +02001695static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001696 const char* name, unsigned int name_len,
1697 struct list *fmt, struct my_regex *re,
1698 int action)
1699{
Willy Tarreau83061a82018-07-13 11:56:34 +02001700 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001701 int ret = -1;
1702
1703 replace = alloc_trash_chunk();
1704 if (!replace)
1705 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001706
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001707 replace->data = build_logline(s, replace->area, replace->size, fmt);
1708 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001709 goto leave;
1710
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001711 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1712 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001713
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001714 leave:
1715 free_trash_chunk(replace);
1716 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001717}
1718
Willy Tarreau87b09662015-04-03 00:22:06 +02001719/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001720 * transaction <txn>. Returns the verdict of the first rule that prevents
1721 * further processing of the request (auth, deny, ...), and defaults to
1722 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1723 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001724 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1725 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1726 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001727 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001728enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001729http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001730{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001731 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001732 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001733 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001734 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001735 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001736 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02001737 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001738 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001739
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001740 /* If "the current_rule_list" match the executed rule list, we are in
1741 * resume condition. If a resume is needed it is always in the action
1742 * and never in the ACL or converters. In this case, we initialise the
1743 * current rule, and go to the action execution point.
1744 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001745 if (s->current_rule) {
1746 rule = s->current_rule;
1747 s->current_rule = NULL;
1748 if (s->current_rule_list == rules)
1749 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001750 }
1751 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001752
Willy Tarreauff011f22011-01-06 17:51:27 +01001753 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001754
Willy Tarreau96257ec2012-12-27 10:46:37 +01001755 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001756 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001757 int ret;
1758
Willy Tarreau192252e2015-04-04 01:47:55 +02001759 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001760 ret = acl_pass(ret);
1761
Willy Tarreauff011f22011-01-06 17:51:27 +01001762 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001763 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001764
1765 if (!ret) /* condition not matched */
1766 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001767 }
1768
Willy Tarreauacc98002015-09-27 23:34:39 +02001769 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001770resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001771 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001772 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02001773 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001774
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001775 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001776 if (deny_status)
1777 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001778 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001779
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001780 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001781 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001782 if (deny_status)
1783 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001784 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001785
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001786 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02001787 /* Auth might be performed on regular http-req rules as well as on stats */
1788 auth_realm = rule->arg.auth.realm;
1789 if (!auth_realm) {
1790 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1791 auth_realm = STATS_DEFAULT_REALM;
1792 else
1793 auth_realm = px->id;
1794 }
1795 /* send 401/407 depending on whether we use a proxy or not. We still
1796 * count one error, because normal browsing won't significantly
1797 * increase the counter but brute force attempts will.
1798 */
1799 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1800 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001801 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001802 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02001803 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001804
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001805 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02001806 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
1807 return HTTP_RULE_RES_BADREQ;
1808 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001809
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001810 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001811 s->task->nice = rule->arg.nice;
1812 break;
1813
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001814 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001815 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001816 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001817 break;
1818
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001819 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001820#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001821 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001822 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001823#endif
1824 break;
1825
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001826 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001827 s->logs.level = rule->arg.loglevel;
1828 break;
1829
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001830 case ACT_HTTP_REPLACE_HDR:
1831 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001832 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1833 rule->arg.hdr_add.name_len,
1834 &rule->arg.hdr_add.fmt,
1835 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06001836 return HTTP_RULE_RES_BADREQ;
1837 break;
1838
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001839 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001840 ctx.idx = 0;
1841 /* remove all occurrences of the header */
1842 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001843 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001844 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001845 }
Willy Tarreau85603282015-01-21 20:39:27 +01001846 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001847
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001848 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001849 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001850 /* The scope of the trash buffer must be limited to this function. The
1851 * build_logline() function can execute a lot of other function which
1852 * can use the trash buffer. So for limiting the scope of this global
1853 * buffer, we build first the header value using build_logline, and
1854 * after we store the header name.
1855 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001856 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001857
1858 replace = alloc_trash_chunk();
1859 if (!replace)
1860 return HTTP_RULE_RES_BADREQ;
1861
Thierry Fournier4b788f72016-06-01 13:35:36 +02001862 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001863 len += build_logline(s, replace->area + len,
1864 replace->size - len,
1865 &rule->arg.hdr_add.fmt);
1866 memcpy(replace->area, rule->arg.hdr_add.name,
1867 rule->arg.hdr_add.name_len);
1868 replace->area[rule->arg.hdr_add.name_len] = ':';
1869 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1870 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001871
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001872 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001873 /* remove all occurrences of the header */
1874 ctx.idx = 0;
1875 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001876 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001877 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1878 }
1879 }
1880
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001881 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001882 static unsigned char rate_limit = 0;
1883
1884 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001885 replace->area[rule->arg.hdr_add.name_len] = 0;
1886 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,
1887 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001888 }
1889
1890 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1891 if (sess->fe != s->be)
1892 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1893 if (sess->listener->counters)
1894 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1895 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001896
1897 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001898 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001899 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001900
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001901 case ACT_HTTP_DEL_ACL:
1902 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001903 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001904 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001905
1906 /* collect reference */
1907 ref = pat_ref_lookup(rule->arg.map.ref);
1908 if (!ref)
1909 continue;
1910
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001911 /* allocate key */
1912 key = alloc_trash_chunk();
1913 if (!key)
1914 return HTTP_RULE_RES_BADREQ;
1915
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001916 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001917 key->data = build_logline(s, key->area, key->size,
1918 &rule->arg.map.key);
1919 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001920
1921 /* perform update */
1922 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001923 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001924 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001925 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001926
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001927 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001928 break;
1929 }
1930
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001931 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001932 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001933 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001934
1935 /* collect reference */
1936 ref = pat_ref_lookup(rule->arg.map.ref);
1937 if (!ref)
1938 continue;
1939
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001940 /* allocate key */
1941 key = alloc_trash_chunk();
1942 if (!key)
1943 return HTTP_RULE_RES_BADREQ;
1944
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001945 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001946 key->data = build_logline(s, key->area, key->size,
1947 &rule->arg.map.key);
1948 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001949
1950 /* perform update */
1951 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001952 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001953 if (pat_ref_find_elt(ref, key->area) == NULL)
1954 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001955 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001956
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001957 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001958 break;
1959 }
1960
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001961 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001962 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001963 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001964
1965 /* collect reference */
1966 ref = pat_ref_lookup(rule->arg.map.ref);
1967 if (!ref)
1968 continue;
1969
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001970 /* allocate key */
1971 key = alloc_trash_chunk();
1972 if (!key)
1973 return HTTP_RULE_RES_BADREQ;
1974
1975 /* allocate value */
1976 value = alloc_trash_chunk();
1977 if (!value) {
1978 free_trash_chunk(key);
1979 return HTTP_RULE_RES_BADREQ;
1980 }
1981
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001982 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001983 key->data = build_logline(s, key->area, key->size,
1984 &rule->arg.map.key);
1985 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001986
1987 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001988 value->data = build_logline(s, value->area,
1989 value->size,
1990 &rule->arg.map.value);
1991 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001992
1993 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001994 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001995 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001996 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001997 else
1998 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001999 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002000
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002001 free_trash_chunk(key);
2002 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002003 break;
2004 }
William Lallemand73025dd2014-04-24 14:38:37 +02002005
Thierry FOURNIER42148732015-09-02 17:17:33 +02002006 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002007 if ((s->req.flags & CF_READ_ERROR) ||
2008 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2009 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2010 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002011 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002012
Willy Tarreauacc98002015-09-27 23:34:39 +02002013 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002014 case ACT_RET_ERR:
2015 case ACT_RET_CONT:
2016 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002017 case ACT_RET_STOP:
2018 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002019 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002020 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002021 return HTTP_RULE_RES_YIELD;
2022 }
William Lallemand73025dd2014-04-24 14:38:37 +02002023 break;
2024
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002025 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002026 /* Note: only the first valid tracking parameter of each
2027 * applies.
2028 */
2029
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002030 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002031 struct stktable *t;
2032 struct stksess *ts;
2033 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002034 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002035
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002036 t = rule->arg.trk_ctr.table.t;
2037 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 +02002038
2039 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002040 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002041
2042 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002043 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2044 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2045 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002046 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002047
Emeric Brun819fc6f2017-06-13 19:37:32 +02002048 if (ptr1)
2049 stktable_data_cast(ptr1, http_req_cnt)++;
2050
2051 if (ptr2)
2052 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2053 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2054
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002055 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002056
2057 /* If data was modified, we need to touch to re-schedule sync */
2058 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002059 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002060
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002061 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002062 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002063 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002064 }
2065 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002066 break;
2067
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002068 /* other flags exists, but normaly, they never be matched. */
2069 default:
2070 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002071 }
2072 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002073
2074 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002075 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002076}
2077
Willy Tarreau71241ab2012-12-27 11:30:54 +01002078
Willy Tarreau51d861a2015-05-22 17:30:48 +02002079/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2080 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2081 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2082 * is returned, the process can continue the evaluation of next rule list. If
2083 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2084 * is returned, it means the operation could not be processed and a server error
2085 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2086 * deny rule. If *YIELD is returned, the caller must call again the function
2087 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002088 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002089enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002090http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002091{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002092 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002093 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002094 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002095 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002096 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002097 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002098
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002099 /* If "the current_rule_list" match the executed rule list, we are in
2100 * resume condition. If a resume is needed it is always in the action
2101 * and never in the ACL or converters. In this case, we initialise the
2102 * current rule, and go to the action execution point.
2103 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002104 if (s->current_rule) {
2105 rule = s->current_rule;
2106 s->current_rule = NULL;
2107 if (s->current_rule_list == rules)
2108 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002109 }
2110 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002111
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002112 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002113
2114 /* check optional condition */
2115 if (rule->cond) {
2116 int ret;
2117
Willy Tarreau192252e2015-04-04 01:47:55 +02002118 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002119 ret = acl_pass(ret);
2120
2121 if (rule->cond->pol == ACL_COND_UNLESS)
2122 ret = !ret;
2123
2124 if (!ret) /* condition not matched */
2125 continue;
2126 }
2127
Willy Tarreauacc98002015-09-27 23:34:39 +02002128 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002129resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002130 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002131 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002132 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002133
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002134 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002135 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002136 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002137
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002138 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002139 s->task->nice = rule->arg.nice;
2140 break;
2141
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002142 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002143 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002144 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002145 break;
2146
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002147 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002148#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002149 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002150 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002151#endif
2152 break;
2153
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002154 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002155 s->logs.level = rule->arg.loglevel;
2156 break;
2157
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002158 case ACT_HTTP_REPLACE_HDR:
2159 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002160 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2161 rule->arg.hdr_add.name_len,
2162 &rule->arg.hdr_add.fmt,
2163 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002164 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002165 break;
2166
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002167 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002168 ctx.idx = 0;
2169 /* remove all occurrences of the header */
2170 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002171 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002172 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2173 }
Willy Tarreau85603282015-01-21 20:39:27 +01002174 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002175
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002176 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002177 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002178 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002179
2180 replace = alloc_trash_chunk();
2181 if (!replace)
2182 return HTTP_RULE_RES_BADREQ;
2183
2184 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002185 memcpy(replace->area, rule->arg.hdr_add.name,
2186 rule->arg.hdr_add.name_len);
2187 replace->data = rule->arg.hdr_add.name_len;
2188 replace->area[replace->data++] = ':';
2189 replace->area[replace->data++] = ' ';
2190 replace->data += build_logline(s,
2191 replace->area + replace->data,
2192 replace->size - replace->data,
2193 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002194
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002195 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002196 /* remove all occurrences of the header */
2197 ctx.idx = 0;
2198 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002199 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002200 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2201 }
2202 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002203
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002204 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002205 static unsigned char rate_limit = 0;
2206
2207 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002208 replace->area[rule->arg.hdr_add.name_len] = 0;
2209 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,
2210 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002211 }
2212
2213 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2214 if (sess->fe != s->be)
2215 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2216 if (sess->listener->counters)
2217 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2218 if (objt_server(s->target))
2219 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2220 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002221
2222 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002223 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002224 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002225
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002226 case ACT_HTTP_DEL_ACL:
2227 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002228 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002229 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002230
2231 /* collect reference */
2232 ref = pat_ref_lookup(rule->arg.map.ref);
2233 if (!ref)
2234 continue;
2235
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002236 /* allocate key */
2237 key = alloc_trash_chunk();
2238 if (!key)
2239 return HTTP_RULE_RES_BADREQ;
2240
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002241 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002242 key->data = build_logline(s, key->area, key->size,
2243 &rule->arg.map.key);
2244 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002245
2246 /* perform update */
2247 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002248 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002249 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002250 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002251
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002252 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002253 break;
2254 }
2255
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002256 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002257 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002258 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002259
2260 /* collect reference */
2261 ref = pat_ref_lookup(rule->arg.map.ref);
2262 if (!ref)
2263 continue;
2264
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002265 /* allocate key */
2266 key = alloc_trash_chunk();
2267 if (!key)
2268 return HTTP_RULE_RES_BADREQ;
2269
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002270 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002271 key->data = build_logline(s, key->area, key->size,
2272 &rule->arg.map.key);
2273 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002274
2275 /* perform update */
2276 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002277 if (pat_ref_find_elt(ref, key->area) == NULL)
2278 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002279
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002280 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002281 break;
2282 }
2283
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002284 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002285 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002286 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002287
2288 /* collect reference */
2289 ref = pat_ref_lookup(rule->arg.map.ref);
2290 if (!ref)
2291 continue;
2292
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002293 /* allocate key */
2294 key = alloc_trash_chunk();
2295 if (!key)
2296 return HTTP_RULE_RES_BADREQ;
2297
2298 /* allocate value */
2299 value = alloc_trash_chunk();
2300 if (!value) {
2301 free_trash_chunk(key);
2302 return HTTP_RULE_RES_BADREQ;
2303 }
2304
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002305 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002306 key->data = build_logline(s, key->area, key->size,
2307 &rule->arg.map.key);
2308 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002309
2310 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002311 value->data = build_logline(s, value->area,
2312 value->size,
2313 &rule->arg.map.value);
2314 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002315
2316 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002317 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002318 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002319 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002320 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002321 else
2322 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002323 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002324 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002325 free_trash_chunk(key);
2326 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002327 break;
2328 }
William Lallemand73025dd2014-04-24 14:38:37 +02002329
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002330 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02002331 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2332 return HTTP_RULE_RES_BADREQ;
2333 return HTTP_RULE_RES_DONE;
2334
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002335 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2336 /* Note: only the first valid tracking parameter of each
2337 * applies.
2338 */
2339
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002340 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002341 struct stktable *t;
2342 struct stksess *ts;
2343 struct stktable_key *key;
2344 void *ptr;
2345
2346 t = rule->arg.trk_ctr.table.t;
2347 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2348
2349 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002350 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002351
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002352 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002353
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002354 /* let's count a new HTTP request as it's the first time we do it */
2355 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2356 if (ptr)
2357 stktable_data_cast(ptr, http_req_cnt)++;
2358
2359 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2360 if (ptr)
2361 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2362 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2363
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002364 /* When the client triggers a 4xx from the server, it's most often due
2365 * to a missing object or permission. These events should be tracked
2366 * because if they happen often, it may indicate a brute force or a
2367 * vulnerability scan. Normally this is done when receiving the response
2368 * but here we're tracking after this ought to have been done so we have
2369 * to do it on purpose.
2370 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002371 if ((unsigned)(txn->status - 400) < 100) {
2372 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2373 if (ptr)
2374 stktable_data_cast(ptr, http_err_cnt)++;
2375
2376 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2377 if (ptr)
2378 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2379 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2380 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002381
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002382 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002383
Emeric Brun0fed0b02017-11-29 16:15:07 +01002384 /* If data was modified, we need to touch to re-schedule sync */
2385 stktable_touch_local(t, ts, 0);
2386
Emeric Brun819fc6f2017-06-13 19:37:32 +02002387 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2388 if (sess->fe != s->be)
2389 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2390
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002391 }
2392 }
2393 break;
2394
Thierry FOURNIER42148732015-09-02 17:17:33 +02002395 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002396 if ((s->req.flags & CF_READ_ERROR) ||
2397 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2398 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2399 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002400 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002401
Willy Tarreauacc98002015-09-27 23:34:39 +02002402 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002403 case ACT_RET_ERR:
2404 case ACT_RET_CONT:
2405 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002406 case ACT_RET_STOP:
2407 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002408 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002409 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002410 return HTTP_RULE_RES_YIELD;
2411 }
William Lallemand73025dd2014-04-24 14:38:37 +02002412 break;
2413
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002414 /* other flags exists, but normaly, they never be matched. */
2415 default:
2416 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002417 }
2418 }
2419
2420 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002421 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002422}
2423
2424
Willy Tarreau71241ab2012-12-27 11:30:54 +01002425/* Perform an HTTP redirect based on the information in <rule>. The function
2426 * returns non-zero on success, or zero in case of a, irrecoverable error such
2427 * as too large a request to build a valid response.
2428 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002429int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002430{
Willy Tarreaub329a312015-05-22 16:27:37 +02002431 struct http_msg *req = &txn->req;
2432 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002433 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002434 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002435 int ret = 0;
2436
2437 chunk = alloc_trash_chunk();
2438 if (!chunk)
2439 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002440
2441 /* build redirect message */
2442 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002443 case 308:
2444 msg_fmt = HTTP_308;
2445 break;
2446 case 307:
2447 msg_fmt = HTTP_307;
2448 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002449 case 303:
2450 msg_fmt = HTTP_303;
2451 break;
2452 case 301:
2453 msg_fmt = HTTP_301;
2454 break;
2455 case 302:
2456 default:
2457 msg_fmt = HTTP_302;
2458 break;
2459 }
2460
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002461 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2462 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002463
2464 switch(rule->type) {
2465 case REDIRECT_TYPE_SCHEME: {
2466 const char *path;
2467 const char *host;
2468 struct hdr_ctx ctx;
2469 int pathlen;
2470 int hostlen;
2471
2472 host = "";
2473 hostlen = 0;
2474 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002475 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002476 host = ctx.line + ctx.val;
2477 hostlen = ctx.vlen;
2478 }
2479
Willy Tarreau6b952c82018-09-10 17:45:34 +02002480 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002481 /* build message using path */
2482 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002483 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002484 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2485 int qs = 0;
2486 while (qs < pathlen) {
2487 if (path[qs] == '?') {
2488 pathlen = qs;
2489 break;
2490 }
2491 qs++;
2492 }
2493 }
2494 } else {
2495 path = "/";
2496 pathlen = 1;
2497 }
2498
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002499 if (rule->rdr_str) { /* this is an old "redirect" rule */
2500 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002501 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002502 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002503
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002504 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002505 memcpy(chunk->area + chunk->data, rule->rdr_str,
2506 rule->rdr_len);
2507 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002508 }
2509 else {
2510 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002511 chunk->data += build_logline(s,
2512 chunk->area + chunk->data,
2513 chunk->size - chunk->data,
2514 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002515
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002516 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002517 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002518 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002519 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002520 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002521 memcpy(chunk->area + chunk->data, "://", 3);
2522 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002523
2524 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002525 memcpy(chunk->area + chunk->data, host, hostlen);
2526 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002527
2528 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002529 memcpy(chunk->area + chunk->data, path, pathlen);
2530 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002531
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002532 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002533 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002534 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002535 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002536 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002537 chunk->area[chunk->data] = '/';
2538 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002539 }
2540
2541 break;
2542 }
2543 case REDIRECT_TYPE_PREFIX: {
2544 const char *path;
2545 int pathlen;
2546
Willy Tarreau6b952c82018-09-10 17:45:34 +02002547 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002548 /* build message using path */
2549 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002550 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002551 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2552 int qs = 0;
2553 while (qs < pathlen) {
2554 if (path[qs] == '?') {
2555 pathlen = qs;
2556 break;
2557 }
2558 qs++;
2559 }
2560 }
2561 } else {
2562 path = "/";
2563 pathlen = 1;
2564 }
2565
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002566 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002567 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002568 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002569
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002570 /* add prefix. Note that if prefix == "/", we don't want to
2571 * add anything, otherwise it makes it hard for the user to
2572 * configure a self-redirection.
2573 */
2574 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002575 memcpy(chunk->area + chunk->data,
2576 rule->rdr_str, rule->rdr_len);
2577 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002578 }
2579 }
2580 else {
2581 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002582 chunk->data += build_logline(s,
2583 chunk->area + chunk->data,
2584 chunk->size - chunk->data,
2585 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002586
2587 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002588 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002589 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002590 }
2591
2592 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002593 memcpy(chunk->area + chunk->data, path, pathlen);
2594 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002595
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002596 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002597 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002598 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002599 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002600 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002601 chunk->area[chunk->data] = '/';
2602 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002603 }
2604
2605 break;
2606 }
2607 case REDIRECT_TYPE_LOCATION:
2608 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002609 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002610 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002611 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002612
2613 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002614 memcpy(chunk->area + chunk->data, rule->rdr_str,
2615 rule->rdr_len);
2616 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002617 }
2618 else {
2619 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002620 chunk->data += build_logline(s,
2621 chunk->area + chunk->data,
2622 chunk->size - chunk->data,
2623 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002624
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002625 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002626 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002627 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002628 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002629 break;
2630 }
2631
2632 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002633 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2634 chunk->data += 14;
2635 memcpy(chunk->area + chunk->data, rule->cookie_str,
2636 rule->cookie_len);
2637 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002638 }
2639
Willy Tarreau19b14122017-02-28 09:48:11 +01002640 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002641 txn->status = rule->code;
2642 /* let's log the request time */
2643 s->logs.tv_request = now;
2644
Christopher Fauletbe821b92017-03-30 11:21:53 +02002645 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002646 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2647 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2648 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002649 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002650 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002651 memcpy(chunk->area + chunk->data,
2652 "\r\nProxy-Connection: keep-alive", 30);
2653 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002654 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002655 memcpy(chunk->area + chunk->data,
2656 "\r\nConnection: keep-alive", 24);
2657 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002658 }
2659 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002660 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2661 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002662 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002663 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002664 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002665 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002666 req->next -= req->sov;
2667 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002668 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002669 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002670 req->msg_state = HTTP_MSG_CLOSED;
2671 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002672 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002673 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002674 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002675 /* let the server side turn to SI_ST_CLO */
2676 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002677 } else {
2678 /* keep-alive not possible */
2679 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002680 memcpy(chunk->area + chunk->data,
2681 "\r\nProxy-Connection: close\r\n\r\n", 29);
2682 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002683 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002684 memcpy(chunk->area + chunk->data,
2685 "\r\nConnection: close\r\n\r\n", 23);
2686 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002687 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002688 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002689 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002690 }
2691
Willy Tarreaue7dff022015-04-03 01:14:29 +02002692 if (!(s->flags & SF_ERR_MASK))
2693 s->flags |= SF_ERR_LOCAL;
2694 if (!(s->flags & SF_FINST_MASK))
2695 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002696
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002697 ret = 1;
2698 leave:
2699 free_trash_chunk(chunk);
2700 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002701}
2702
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002703/* This stream analyser runs all HTTP request processing which is common to
2704 * frontends and backends, which means blocking ACLs, filters, connection-close,
2705 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002706 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002707 * either needs more data or wants to immediately abort the request (eg: deny,
2708 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002709 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002710int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002711{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002712 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002713 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002714 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002715 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002716 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002717 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002718 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002719 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002720
Willy Tarreau655dce92009-11-08 13:10:58 +01002721 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002722 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002723 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002724 }
2725
Christopher Faulet45073512018-07-20 10:16:29 +02002726 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 +02002727 now_ms, __FUNCTION__,
2728 s,
2729 req,
2730 req->rex, req->wex,
2731 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002732 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002733 req->analysers);
2734
Willy Tarreau65410832014-04-28 21:25:43 +02002735 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002736 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002737
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002738 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002739 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002740 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002741
Willy Tarreau0b748332014-04-29 00:13:29 +02002742 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002743 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2744 goto return_prx_yield;
2745
Willy Tarreau0b748332014-04-29 00:13:29 +02002746 case HTTP_RULE_RES_CONT:
2747 case HTTP_RULE_RES_STOP: /* nothing to do */
2748 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002749
Willy Tarreau0b748332014-04-29 00:13:29 +02002750 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2751 if (txn->flags & TX_CLTARPIT)
2752 goto tarpit;
2753 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002754
Willy Tarreau0b748332014-04-29 00:13:29 +02002755 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2756 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002757
Willy Tarreau0b748332014-04-29 00:13:29 +02002758 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002759 goto done;
2760
Willy Tarreau0b748332014-04-29 00:13:29 +02002761 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2762 goto return_bad_req;
2763 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002764 }
2765
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002766 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2767 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002768 struct hdr_ctx ctx;
2769
2770 ctx.idx = 0;
2771 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002772 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002773 if (unlikely(http_header_add_tail2(&txn->req,
2774 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002775 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002776 goto return_bad_req;
2777 }
2778 }
2779
2780 }
2781
Willy Tarreau52542592014-04-28 18:33:26 +02002782 /* OK at this stage, we know that the request was accepted according to
2783 * the http-request rules, we can check for the stats. Note that the
2784 * URI is detected *before* the req* rules in order not to be affected
2785 * by a possible reqrep, while they are processed *after* so that a
2786 * reqdeny can still block them. This clearly needs to change in 1.6!
2787 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002788 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002789 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002790 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002791 txn->status = 500;
2792 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002793 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002794
Willy Tarreaue7dff022015-04-03 01:14:29 +02002795 if (!(s->flags & SF_ERR_MASK))
2796 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002797 goto return_prx_cond;
2798 }
2799
2800 /* parse the whole stats request and extract the relevant information */
2801 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002802 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002803 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002804
Willy Tarreau0b748332014-04-29 00:13:29 +02002805 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2806 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002807
Willy Tarreau0b748332014-04-29 00:13:29 +02002808 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2809 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002810 }
2811
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002812 /* evaluate the req* rules except reqadd */
2813 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002814 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002815 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002816
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002817 if (txn->flags & TX_CLDENY)
2818 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002819
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002820 if (txn->flags & TX_CLTARPIT) {
2821 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002822 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002823 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002824 }
Willy Tarreau06619262006-12-17 08:37:22 +01002825
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002826 /* add request headers from the rule sets in the same order */
2827 list_for_each_entry(wl, &px->req_add, list) {
2828 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002829 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002830 ret = acl_pass(ret);
2831 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2832 ret = !ret;
2833 if (!ret)
2834 continue;
2835 }
2836
Christopher Faulet10079f52018-10-03 15:17:28 +02002837 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002838 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002839 }
2840
Willy Tarreau52542592014-04-28 18:33:26 +02002841
2842 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002843 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2844 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002845 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002846 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002847 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002848
Willy Tarreaue7dff022015-04-03 01:14:29 +02002849 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2850 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2851 if (!(s->flags & SF_FINST_MASK))
2852 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002853
Willy Tarreau70730dd2014-04-24 18:06:27 +02002854 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002855 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2856 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002857 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002858 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002859 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002860
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002861 /* check whether we have some ACLs set to redirect this request */
2862 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002863 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002864 int ret;
2865
Willy Tarreau192252e2015-04-04 01:47:55 +02002866 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002867 ret = acl_pass(ret);
2868 if (rule->cond->pol == ACL_COND_UNLESS)
2869 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002870 if (!ret)
2871 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002872 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002873 if (!http_apply_redirect_rule(rule, s, txn))
2874 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002875 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002876 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002877
Willy Tarreau2be39392010-01-03 17:24:51 +01002878 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2879 * If this happens, then the data will not come immediately, so we must
2880 * send all what we have without waiting. Note that due to the small gain
2881 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002882 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002883 * itself once used.
2884 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002885 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002886
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002887 done: /* done with this analyser, continue with next ones that the calling
2888 * points will have set, if any.
2889 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002890 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002891 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
2892 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002893 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002894
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002895 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02002896 /* Allow cookie logging
2897 */
2898 if (s->be->cookie_name || sess->fe->capture_name)
2899 manage_client_side_cookies(s, req);
2900
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002901 /* When a connection is tarpitted, we use the tarpit timeout,
2902 * which may be the same as the connect timeout if unspecified.
2903 * If unset, then set it to zero because we really want it to
2904 * eventually expire. We build the tarpit as an analyser.
2905 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002906 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002907
2908 /* wipe the request out so that we can drop the connection early
2909 * if the client closes first.
2910 */
2911 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002912
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002913 txn->status = http_err_codes[deny_status];
2914
Christopher Faulet0184ea72017-01-05 14:06:34 +01002915 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002916 req->analysers |= AN_REQ_HTTP_TARPIT;
2917 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2918 if (!req->analyse_exp)
2919 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02002920 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002921 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002922 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002923 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002924 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002925 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002926 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002927
2928 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002929
2930 /* Allow cookie logging
2931 */
2932 if (s->be->cookie_name || sess->fe->capture_name)
2933 manage_client_side_cookies(s, req);
2934
Willy Tarreau0b748332014-04-29 00:13:29 +02002935 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002936 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002937 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002938 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02002939 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002940 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002941 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002942 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002943 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002944 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002945 goto return_prx_cond;
2946
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002947 return_bad_req:
2948 /* We centralize bad requests processing here */
2949 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2950 /* we detected a parsing error. We want to archive this request
2951 * in the dedicated proxy area for later troubleshooting.
2952 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02002953 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002954 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002955
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002956 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002957 txn->req.msg_state = HTTP_MSG_ERROR;
2958 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002959 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002960
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002961 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002962 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002963 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002964
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002965 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002966 if (!(s->flags & SF_ERR_MASK))
2967 s->flags |= SF_ERR_PRXCOND;
2968 if (!(s->flags & SF_FINST_MASK))
2969 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002970
Christopher Faulet0184ea72017-01-05 14:06:34 +01002971 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002972 req->analyse_exp = TICK_ETERNITY;
2973 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002974
2975 return_prx_yield:
2976 channel_dont_connect(req);
2977 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002978}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002979
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002980/* This function performs all the processing enabled for the current request.
2981 * It returns 1 if the processing can continue on next analysers, or zero if it
2982 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002983 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002984 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002985int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002986{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002987 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002988 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002989 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02002990 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002991
Willy Tarreau655dce92009-11-08 13:10:58 +01002992 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002993 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002994 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002995 return 0;
2996 }
2997
Christopher Faulet45073512018-07-20 10:16:29 +02002998 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 +02002999 now_ms, __FUNCTION__,
3000 s,
3001 req,
3002 req->rex, req->wex,
3003 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003004 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003005 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003006
Willy Tarreau59234e92008-11-30 23:51:27 +01003007 /*
3008 * Right now, we know that we have processed the entire headers
3009 * and that unwanted requests have been filtered out. We can do
3010 * whatever we want with the remaining request. Also, now we
3011 * may have separate values for ->fe, ->be.
3012 */
Willy Tarreau06619262006-12-17 08:37:22 +01003013
Willy Tarreau59234e92008-11-30 23:51:27 +01003014 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003015 * If HTTP PROXY is set we simply get remote server address parsing
3016 * incoming request. Note that this requires that a connection is
3017 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003018 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003019 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003020 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003021 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003022
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003023 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003024 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003025 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003026 txn->req.msg_state = HTTP_MSG_ERROR;
3027 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003028 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003029 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003030
Willy Tarreaue7dff022015-04-03 01:14:29 +02003031 if (!(s->flags & SF_ERR_MASK))
3032 s->flags |= SF_ERR_RESOURCE;
3033 if (!(s->flags & SF_FINST_MASK))
3034 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003035
3036 return 0;
3037 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003038
Willy Tarreau6b952c82018-09-10 17:45:34 +02003039 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003040 if (url2sa(ci_head(req) + msg->sl.rq.u,
3041 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003042 &conn->addr.to, NULL) == -1)
3043 goto return_bad_req;
3044
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003045 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003046 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3047 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003048 * u_l characters by a single "/".
3049 */
3050 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003051 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003052 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3053 int delta;
3054
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003055 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003056 http_msg_move_end(&txn->req, delta);
3057 cur_end += delta;
3058 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3059 goto return_bad_req;
3060 }
3061 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003062 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003063 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3064 int delta;
3065
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003066 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003067 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003068 http_msg_move_end(&txn->req, delta);
3069 cur_end += delta;
3070 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3071 goto return_bad_req;
3072 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003073 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003074
Willy Tarreau59234e92008-11-30 23:51:27 +01003075 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003076 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003077 * Note that doing so might move headers in the request, but
3078 * the fields will stay coherent and the URI will not move.
3079 * This should only be performed in the backend.
3080 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003081 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003082 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003083
William Lallemanda73203e2012-03-12 12:48:57 +01003084 /* add unique-id if "header-unique-id" is specified */
3085
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003086 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003087 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003088 goto return_bad_req;
3089 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003090 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003091 }
William Lallemanda73203e2012-03-12 12:48:57 +01003092
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003093 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003094 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003095 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003096 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003097 goto return_bad_req;
3098 }
3099
Cyril Bontéb21570a2009-11-29 20:04:48 +01003100 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003101 * 9: add X-Forwarded-For if either the frontend or the backend
3102 * asks for it.
3103 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003104 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003105 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003106 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3107 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3108 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003109 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003110 /* The header is set to be added only if none is present
3111 * and we found it, so don't do anything.
3112 */
3113 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003114 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003115 /* Add an X-Forwarded-For header unless the source IP is
3116 * in the 'except' network range.
3117 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003118 if ((!sess->fe->except_mask.s_addr ||
3119 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3120 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003121 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003122 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003123 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003124 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003125 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003126 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003127
3128 /* Note: we rely on the backend to get the header name to be used for
3129 * x-forwarded-for, because the header is really meant for the backends.
3130 * However, if the backend did not specify any option, we have to rely
3131 * on the frontend's header name.
3132 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003133 if (s->be->fwdfor_hdr_len) {
3134 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003135 memcpy(trash.area,
3136 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003137 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003138 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003139 memcpy(trash.area,
3140 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003141 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003142 len += snprintf(trash.area + len,
3143 trash.size - len,
3144 ": %d.%d.%d.%d", pn[0], pn[1],
3145 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003146
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003147 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003148 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003149 }
3150 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003151 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003152 /* FIXME: for the sake of completeness, we should also support
3153 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003154 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003155 int len;
3156 char pn[INET6_ADDRSTRLEN];
3157 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003158 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003159 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003160
Willy Tarreau59234e92008-11-30 23:51:27 +01003161 /* Note: we rely on the backend to get the header name to be used for
3162 * x-forwarded-for, because the header is really meant for the backends.
3163 * However, if the backend did not specify any option, we have to rely
3164 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003165 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003166 if (s->be->fwdfor_hdr_len) {
3167 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003168 memcpy(trash.area, s->be->fwdfor_hdr_name,
3169 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003170 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003171 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003172 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3173 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003174 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003175 len += snprintf(trash.area + len, trash.size - len,
3176 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003177
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003178 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003179 goto return_bad_req;
3180 }
3181 }
3182
3183 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003184 * 10: add X-Original-To if either the frontend or the backend
3185 * asks for it.
3186 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003187 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003188
3189 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003190 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003191 /* Add an X-Original-To header unless the destination IP is
3192 * in the 'except' network range.
3193 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003194 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003195
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003196 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003197 ((!sess->fe->except_mask_to.s_addr ||
3198 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3199 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003200 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003201 (((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 +02003202 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003203 int len;
3204 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003205 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003206
3207 /* Note: we rely on the backend to get the header name to be used for
3208 * x-original-to, because the header is really meant for the backends.
3209 * However, if the backend did not specify any option, we have to rely
3210 * on the frontend's header name.
3211 */
3212 if (s->be->orgto_hdr_len) {
3213 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003214 memcpy(trash.area,
3215 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003216 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003217 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003218 memcpy(trash.area,
3219 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003220 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003221 len += snprintf(trash.area + len,
3222 trash.size - len,
3223 ": %d.%d.%d.%d", pn[0], pn[1],
3224 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003225
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003226 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003227 goto return_bad_req;
3228 }
3229 }
3230 }
3231
Willy Tarreau50fc7772012-11-11 22:19:57 +01003232 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3233 * If an "Upgrade" token is found, the header is left untouched in order not to have
3234 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3235 * "Upgrade" is present in the Connection header.
3236 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003237 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003238 unsigned int want_flags = 0;
3239
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003240 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003241 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003242 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003243 want_flags |= TX_CON_CLO_SET;
3244 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003245 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003246 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003247 want_flags |= TX_CON_KAL_SET;
3248 }
3249
3250 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003251 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003252 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003253
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003254
Willy Tarreau522d6c02009-12-06 18:49:18 +01003255 /* If we have no server assigned yet and we're balancing on url_param
3256 * with a POST request, we may be interested in checking the body for
3257 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003258 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003259 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003260 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003261 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003262 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003263 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003264 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003265
Christopher Fauletbe821b92017-03-30 11:21:53 +02003266 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3267 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003268#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003269 /* We expect some data from the client. Unless we know for sure
3270 * we already have a full request, we have to re-enable quick-ack
3271 * in case we previously disabled it, otherwise we might cause
3272 * the client to delay further data.
3273 */
3274 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3275 cli_conn && conn_ctrl_ready(cli_conn) &&
3276 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003277 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003278 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003279#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003280
Willy Tarreau59234e92008-11-30 23:51:27 +01003281 /*************************************************************
3282 * OK, that's finished for the headers. We have done what we *
3283 * could. Let's switch to the DATA state. *
3284 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003285 req->analyse_exp = TICK_ETERNITY;
3286 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003287
Willy Tarreau59234e92008-11-30 23:51:27 +01003288 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003289 /* OK let's go on with the BODY now */
3290 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003291
Willy Tarreau59234e92008-11-30 23:51:27 +01003292 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003293 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003294 /* we detected a parsing error. We want to archive this request
3295 * in the dedicated proxy area for later troubleshooting.
3296 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003297 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003298 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003299
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003300 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003301 txn->req.msg_state = HTTP_MSG_ERROR;
3302 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003303 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003304 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003305
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003306 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003307 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003308 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003309
Willy Tarreaue7dff022015-04-03 01:14:29 +02003310 if (!(s->flags & SF_ERR_MASK))
3311 s->flags |= SF_ERR_PRXCOND;
3312 if (!(s->flags & SF_FINST_MASK))
3313 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003314 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003315}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003316
Willy Tarreau60b85b02008-11-30 23:28:40 +01003317/* This function is an analyser which processes the HTTP tarpit. It always
3318 * returns zero, at the beginning because it prevents any other processing
3319 * from occurring, and at the end because it terminates the request.
3320 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003321int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003322{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003323 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003324
3325 /* This connection is being tarpitted. The CLIENT side has
3326 * already set the connect expiration date to the right
3327 * timeout. We just have to check that the client is still
3328 * there and that the timeout has not expired.
3329 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003330 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003331 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003332 !tick_is_expired(req->analyse_exp, now_ms))
3333 return 0;
3334
3335 /* We will set the queue timer to the time spent, just for
3336 * logging purposes. We fake a 500 server error, so that the
3337 * attacker will not suspect his connection has been tarpitted.
3338 * It will not cause trouble to the logs because we can exclude
3339 * the tarpitted connections by filtering on the 'PT' status flags.
3340 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003341 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3342
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003343 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003344 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003345
Christopher Faulet0184ea72017-01-05 14:06:34 +01003346 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003347 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003348
Willy Tarreaue7dff022015-04-03 01:14:29 +02003349 if (!(s->flags & SF_ERR_MASK))
3350 s->flags |= SF_ERR_PRXCOND;
3351 if (!(s->flags & SF_FINST_MASK))
3352 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003353 return 0;
3354}
3355
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003356/* This function is an analyser which waits for the HTTP request body. It waits
3357 * for either the buffer to be full, or the full advertised contents to have
3358 * reached the buffer. It must only be called after the standard HTTP request
3359 * processing has occurred, because it expects the request to be parsed and will
3360 * look for the Expect header. It may send a 100-Continue interim response. It
3361 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3362 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3363 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003364 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003365int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003366{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003367 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003368 struct http_txn *txn = s->txn;
3369 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003370
3371 /* We have to parse the HTTP request body to find any required data.
3372 * "balance url_param check_post" should have been the only way to get
3373 * into this. We were brought here after HTTP header analysis, so all
3374 * related structures are ready.
3375 */
3376
Willy Tarreau890988f2014-04-10 11:59:33 +02003377 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3378 /* This is the first call */
3379 if (msg->msg_state < HTTP_MSG_BODY)
3380 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003381
Willy Tarreau890988f2014-04-10 11:59:33 +02003382 if (msg->msg_state < HTTP_MSG_100_SENT) {
3383 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3384 * send an HTTP/1.1 100 Continue intermediate response.
3385 */
3386 if (msg->flags & HTTP_MSGF_VER_11) {
3387 struct hdr_ctx ctx;
3388 ctx.idx = 0;
3389 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003390 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003391 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003392 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003393 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003394 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003395 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003396 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003397 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003398
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003399 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003400 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003401 * must save the body in msg->next because it survives buffer
3402 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003403 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003404 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003405
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003406 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003407 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3408 else
3409 msg->msg_state = HTTP_MSG_DATA;
3410 }
3411
Willy Tarreau890988f2014-04-10 11:59:33 +02003412 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3413 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003414 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003415 goto missing_data;
3416
3417 /* OK we have everything we need now */
3418 goto http_end;
3419 }
3420
3421 /* OK here we're parsing a chunked-encoded message */
3422
Willy Tarreau522d6c02009-12-06 18:49:18 +01003423 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003424 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003425 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003426 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003427 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003428 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003429 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003430
Willy Tarreau115acb92009-12-26 13:56:06 +01003431 if (!ret)
3432 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003433 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003434 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003435 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003436 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003437 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003438 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003439 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003440
3441 msg->chunk_len = chunk;
3442 msg->body_len += chunk;
3443
3444 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003445 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003446 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003447 }
3448
Willy Tarreaud98cf932009-12-27 22:54:55 +01003449 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003450 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3451 * for at least a whole chunk or the whole content length bytes after
3452 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003453 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003454 if (msg->msg_state == HTTP_MSG_TRAILERS)
3455 goto http_end;
3456
Willy Tarreaue115b492015-05-01 23:05:14 +02003457 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003458 goto http_end;
3459
3460 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003461 /* we get here if we need to wait for more data. If the buffer is full,
3462 * we have the maximum we can expect.
3463 */
Willy Tarreau23752332018-06-15 14:54:53 +02003464 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003465 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003466
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003467 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003468 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003469 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003470
Willy Tarreaue7dff022015-04-03 01:14:29 +02003471 if (!(s->flags & SF_ERR_MASK))
3472 s->flags |= SF_ERR_CLITO;
3473 if (!(s->flags & SF_FINST_MASK))
3474 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003475 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003476 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003477
3478 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003479 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003480 /* Not enough data. We'll re-use the http-request
3481 * timeout here. Ideally, we should set the timeout
3482 * relative to the accept() date. We just set the
3483 * request timeout once at the beginning of the
3484 * request.
3485 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003486 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003487 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003488 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003489 return 0;
3490 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003491
3492 http_end:
3493 /* The situation will not evolve, so let's give up on the analysis. */
3494 s->logs.tv_request = now; /* update the request timer to reflect full request */
3495 req->analysers &= ~an_bit;
3496 req->analyse_exp = TICK_ETERNITY;
3497 return 1;
3498
3499 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003500 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003501 txn->req.msg_state = HTTP_MSG_ERROR;
3502 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003503 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003504
Willy Tarreaue7dff022015-04-03 01:14:29 +02003505 if (!(s->flags & SF_ERR_MASK))
3506 s->flags |= SF_ERR_PRXCOND;
3507 if (!(s->flags & SF_FINST_MASK))
3508 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003509
Willy Tarreau522d6c02009-12-06 18:49:18 +01003510 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003511 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003512 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003513 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003514 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003515 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003516}
3517
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003518/* send a server's name with an outgoing request over an established connection.
3519 * Note: this function is designed to be called once the request has been scheduled
3520 * for being forwarded. This is the reason why it rewinds the buffer before
3521 * proceeding.
3522 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003523int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003524
3525 struct hdr_ctx ctx;
3526
Mark Lamourinec2247f02012-01-04 13:02:01 -05003527 char *hdr_name = be->server_id_hdr_name;
3528 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003529 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003530 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003531 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003532
William Lallemandd9e90662012-01-30 17:27:17 +01003533 ctx.idx = 0;
3534
Willy Tarreau211cdec2014-04-17 20:18:08 +02003535 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003536 if (old_o) {
3537 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003538 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003539 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003540 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003541 }
3542
Willy Tarreauf37954d2018-06-15 18:31:02 +02003543 old_i = ci_data(chn);
3544 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 -05003545 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003546 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003547 }
3548
3549 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003550 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003551 memcpy(hdr_val, hdr_name, hdr_name_len);
3552 hdr_val += hdr_name_len;
3553 *hdr_val++ = ':';
3554 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003555 hdr_val += strlcpy2(hdr_val, srv_name,
3556 trash.area + trash.size - hdr_val);
3557 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3558 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003559
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003560 if (old_o) {
3561 /* If this was a forwarded request, we must readjust the amount of
3562 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003563 * variations. Note that the current state is >= HTTP_MSG_BODY,
3564 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003565 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003566 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003567 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003568 txn->req.next -= old_o;
3569 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003570 }
3571
Mark Lamourinec2247f02012-01-04 13:02:01 -05003572 return 0;
3573}
3574
Willy Tarreau610ecce2010-01-04 21:15:02 +01003575/* Terminate current transaction and prepare a new one. This is very tricky
3576 * right now but it works.
3577 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003578void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003579{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003580 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003581 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003582 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003583 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003584 struct connection *srv_conn;
3585 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003586 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003587
Willy Tarreau610ecce2010-01-04 21:15:02 +01003588 /* FIXME: We need a more portable way of releasing a backend's and a
3589 * server's connections. We need a safer way to reinitialize buffer
3590 * flags. We also need a more accurate method for computing per-request
3591 * data.
3592 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003593 /*
3594 * XXX cognet: This is probably wrong, this is killing a whole
3595 * connection, in the new world order, we probably want to just kill
3596 * the stream, this is to be revisited the day we handle multiple
3597 * streams in one server connection.
3598 */
3599 cs = objt_cs(s->si[1].end);
3600 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003601
Willy Tarreau4213a112013-12-15 10:25:42 +01003602 /* unless we're doing keep-alive, we want to quickly close the connection
3603 * to the server.
3604 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003605 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003606 !si_conn_ready(&s->si[1])) {
3607 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3608 si_shutr(&s->si[1]);
3609 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003610 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003611
Willy Tarreaue7dff022015-04-03 01:14:29 +02003612 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003613 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003614 if (unlikely(s->srv_conn))
3615 sess_change_server(s, NULL);
3616 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003617
3618 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003619 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003620
Willy Tarreaueee5b512015-04-03 23:46:31 +02003621 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003622 int n;
3623
Willy Tarreaueee5b512015-04-03 23:46:31 +02003624 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003625 if (n < 1 || n > 5)
3626 n = 0;
3627
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003628 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003629 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003630 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003631 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003632 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003633 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3634 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003635 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003636 }
3637
3638 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003639 s->logs.bytes_in -= ci_data(&s->req);
3640 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003641
Willy Tarreau66425e32018-07-25 06:55:12 +02003642 /* we may need to know the position in the queue */
3643 pendconn_free(s);
3644
Willy Tarreau610ecce2010-01-04 21:15:02 +01003645 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003646 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003647 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003648 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003649 s->do_log(s);
3650 }
3651
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003652 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003653 stream_stop_content_counters(s);
3654 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003655
Willy Tarreau610ecce2010-01-04 21:15:02 +01003656 s->logs.accept_date = date; /* user-visible date for logging */
3657 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003658 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3659 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003660 tv_zero(&s->logs.tv_request);
3661 s->logs.t_queue = -1;
3662 s->logs.t_connect = -1;
3663 s->logs.t_data = -1;
3664 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003665 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3666 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003667
Willy Tarreauf37954d2018-06-15 18:31:02 +02003668 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3669 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003670
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003671 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003672 if (s->flags & SF_CURR_SESS) {
3673 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003674 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003675 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003676 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003677 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003678 }
3679
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003680 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003681
Willy Tarreau4213a112013-12-15 10:25:42 +01003682 /* only release our endpoint if we don't intend to reuse the
3683 * connection.
3684 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003685 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003686 !si_conn_ready(&s->si[1])) {
3687 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02003688 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003689 }
3690
Willy Tarreau350f4872014-11-28 14:42:25 +01003691 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3692 s->si[1].err_type = SI_ET_NONE;
3693 s->si[1].conn_retries = 0; /* used for logging too */
3694 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003695 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 +01003696 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 +02003697 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 +02003698 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3699 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3700 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003701
Patrick Hemmere3faf022018-08-22 10:02:00 -04003702 hlua_ctx_destroy(s->hlua);
3703 s->hlua = NULL;
3704
Willy Tarreaueee5b512015-04-03 23:46:31 +02003705 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003706 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003707 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003708
3709 if (prev_status == 401 || prev_status == 407) {
3710 /* In HTTP keep-alive mode, if we receive a 401, we still have
3711 * a chance of being able to send the visitor again to the same
3712 * server over the same connection. This is required by some
3713 * broken protocols such as NTLM, and anyway whenever there is
3714 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003715 * it's better to do it (at least it helps with debugging), at
3716 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003717 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003718 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003719 }
3720
Willy Tarreau53f96852016-02-02 18:50:47 +01003721 /* Never ever allow to reuse a connection from a non-reuse backend */
3722 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3723 srv_conn->flags |= CO_FL_PRIVATE;
3724
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003725 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003726 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003727
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003728 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003729 s->req.flags |= CF_NEVER_WAIT;
3730 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003731 }
3732
Willy Tarreau714ea782015-11-25 20:11:11 +01003733 /* we're removing the analysers, we MUST re-enable events detection.
3734 * We don't enable close on the response channel since it's either
3735 * already closed, or in keep-alive with an idle connection handler.
3736 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003737 channel_auto_read(&s->req);
3738 channel_auto_close(&s->req);
3739 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003740
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003741 /* we're in keep-alive with an idle connection, monitor it if not already done */
3742 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003743 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02003744 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003745 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01003746 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003747 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02003748 else if (prev_flags & TX_NOT_FIRST)
3749 /* note: we check the request, not the connection, but
3750 * this is valid for strategies SAFE and AGGR, and in
3751 * case of ALWS, we don't care anyway.
3752 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003753 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02003754 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003755 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003756 }
Christopher Faulete6006242017-03-10 11:52:44 +01003757 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3758 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003759}
3760
3761
3762/* This function updates the request state machine according to the response
3763 * state machine and buffer flags. It returns 1 if it changes anything (flag
3764 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3765 * it is only used to find when a request/response couple is complete. Both
3766 * this function and its equivalent should loop until both return zero. It
3767 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3768 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003769int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003770{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003771 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003772 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003773 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003774 unsigned int old_state = txn->req.msg_state;
3775
Christopher Faulet4be98032017-07-18 10:48:24 +02003776 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003777 return 0;
3778
3779 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003780 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003781 * We can shut the read side unless we want to abort_on_close,
3782 * or we have a POST request. The issue with POST requests is
3783 * that some browsers still send a CRLF after the request, and
3784 * this CRLF must be read so that it does not remain in the kernel
3785 * buffers, otherwise a close could cause an RST on some systems
3786 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003787 * Note that if we're using keep-alive on the client side, we'd
3788 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003789 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003790 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003791 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003792 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3793 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003794 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3795 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003796 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003797 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003798
Willy Tarreau40f151a2012-12-20 12:10:09 +01003799 /* if the server closes the connection, we want to immediately react
3800 * and close the socket to save packets and syscalls.
3801 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003802 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003803
Willy Tarreau7f876a12015-11-18 11:59:55 +01003804 /* In any case we've finished parsing the request so we must
3805 * disable Nagle when sending data because 1) we're not going
3806 * to shut this side, and 2) the server is waiting for us to
3807 * send pending data.
3808 */
3809 chn->flags |= CF_NEVER_WAIT;
3810
Willy Tarreau610ecce2010-01-04 21:15:02 +01003811 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3812 goto wait_other_side;
3813
3814 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3815 /* The server has not finished to respond, so we
3816 * don't want to move in order not to upset it.
3817 */
3818 goto wait_other_side;
3819 }
3820
Willy Tarreau610ecce2010-01-04 21:15:02 +01003821 /* When we get here, it means that both the request and the
3822 * response have finished receiving. Depending on the connection
3823 * mode, we'll have to wait for the last bytes to leave in either
3824 * direction, and sometimes for a close to be effective.
3825 */
3826
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003827 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3828 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003829 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3830 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003831 }
3832 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3833 /* Option forceclose is set, or either side wants to close,
3834 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003835 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003836 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003837 *
3838 * However, there is an exception if the response
3839 * length is undefined. In this case, we need to wait
3840 * the close from the server. The response will be
3841 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003842 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003843 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3844 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3845 goto check_channel_flags;
3846
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003847 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3848 channel_shutr_now(chn);
3849 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003850 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003851 }
3852 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003853 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3854 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003855 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003856 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3857 channel_auto_read(chn);
3858 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003859 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003860 }
3861
Christopher Faulet4be98032017-07-18 10:48:24 +02003862 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003863 }
3864
3865 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3866 http_msg_closing:
3867 /* nothing else to forward, just waiting for the output buffer
3868 * to be empty and for the shutw_now to take effect.
3869 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003870 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003871 txn->req.msg_state = HTTP_MSG_CLOSED;
3872 goto http_msg_closed;
3873 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003874 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003875 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003876 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003877 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003878 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003879 }
3880
3881 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3882 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01003883 /* if we don't know whether the server will close, we need to hard close */
3884 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
3885 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3886
Willy Tarreau3988d932013-12-27 23:03:08 +01003887 /* see above in MSG_DONE why we only do this in these states */
3888 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3889 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003890 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3891 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01003892 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003893 goto wait_other_side;
3894 }
3895
Christopher Faulet4be98032017-07-18 10:48:24 +02003896 check_channel_flags:
3897 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3898 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3899 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02003900 txn->req.msg_state = HTTP_MSG_CLOSING;
3901 goto http_msg_closing;
3902 }
3903
3904
Willy Tarreau610ecce2010-01-04 21:15:02 +01003905 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003906 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003907}
3908
3909
3910/* This function updates the response state machine according to the request
3911 * state machine and buffer flags. It returns 1 if it changes anything (flag
3912 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3913 * it is only used to find when a request/response couple is complete. Both
3914 * this function and its equivalent should loop until both return zero. It
3915 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3916 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003917int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003918{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003919 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003920 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003921 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003922 unsigned int old_state = txn->rsp.msg_state;
3923
Christopher Faulet4be98032017-07-18 10:48:24 +02003924 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003925 return 0;
3926
3927 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3928 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003929 * still monitor the server connection for a possible close
3930 * while the request is being uploaded, so we don't disable
3931 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003932 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003933 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003934
3935 if (txn->req.msg_state == HTTP_MSG_ERROR)
3936 goto wait_other_side;
3937
3938 if (txn->req.msg_state < HTTP_MSG_DONE) {
3939 /* The client seems to still be sending data, probably
3940 * because we got an error response during an upload.
3941 * We have the choice of either breaking the connection
3942 * or letting it pass through. Let's do the later.
3943 */
3944 goto wait_other_side;
3945 }
3946
Willy Tarreau610ecce2010-01-04 21:15:02 +01003947 /* When we get here, it means that both the request and the
3948 * response have finished receiving. Depending on the connection
3949 * mode, we'll have to wait for the last bytes to leave in either
3950 * direction, and sometimes for a close to be effective.
3951 */
3952
3953 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3954 /* Server-close mode : shut read and wait for the request
3955 * side to close its output buffer. The caller will detect
3956 * when we're in DONE and the other is in CLOSED and will
3957 * catch that for the final cleanup.
3958 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003959 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
3960 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003961 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003962 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3963 /* Option forceclose is set, or either side wants to close,
3964 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003965 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003966 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003967 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01003968 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003969 channel_shutr_now(chn);
3970 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003971 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003972 }
3973 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003974 /* The last possible modes are keep-alive and tunnel. Tunnel will
3975 * need to forward remaining data. Keep-alive will need to monitor
3976 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003977 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003978 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02003979 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01003980 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
3981 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003982 }
3983
Christopher Faulet4be98032017-07-18 10:48:24 +02003984 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003985 }
3986
3987 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3988 http_msg_closing:
3989 /* nothing else to forward, just waiting for the output buffer
3990 * to be empty and for the shutw_now to take effect.
3991 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003992 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003993 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3994 goto http_msg_closed;
3995 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003996 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02003997 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003998 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003999 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004000 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004001 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004002 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004003 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004004 }
4005
4006 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4007 http_msg_closed:
4008 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004009 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004010 channel_auto_close(chn);
4011 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004012 goto wait_other_side;
4013 }
4014
Christopher Faulet4be98032017-07-18 10:48:24 +02004015 check_channel_flags:
4016 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4017 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4018 /* if we've just closed an output, let's switch */
4019 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4020 goto http_msg_closing;
4021 }
4022
Willy Tarreau610ecce2010-01-04 21:15:02 +01004023 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004024 /* We force the response to leave immediately if we're waiting for the
4025 * other side, since there is no pending shutdown to push it out.
4026 */
4027 if (!channel_is_empty(chn))
4028 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004029 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004030}
4031
4032
Christopher Faulet894da4c2017-07-18 11:29:07 +02004033/* Resync the request and response state machines. */
4034void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004035{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004036 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004037#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004038 int old_req_state = txn->req.msg_state;
4039 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004040#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004041
Willy Tarreau610ecce2010-01-04 21:15:02 +01004042 http_sync_req_state(s);
4043 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004044 if (!http_sync_res_state(s))
4045 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004046 if (!http_sync_req_state(s))
4047 break;
4048 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004049
Christopher Faulet894da4c2017-07-18 11:29:07 +02004050 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4051 "req->analysers=0x%08x res->analysers=0x%08x\n",
4052 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004053 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4054 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004055 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004056
4057
Willy Tarreau610ecce2010-01-04 21:15:02 +01004058 /* OK, both state machines agree on a compatible state.
4059 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004060 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4061 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004062 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4063 * means we must abort the request.
4064 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4065 * corresponding channel.
4066 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4067 * on the response with server-close mode means we've completed one
4068 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004069 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004070 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4071 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004072 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004073 channel_auto_close(&s->req);
4074 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004075 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004076 channel_auto_close(&s->res);
4077 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004078 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004079 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4080 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004081 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004082 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004083 channel_auto_close(&s->res);
4084 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004085 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004086 channel_abort(&s->req);
4087 channel_auto_close(&s->req);
4088 channel_auto_read(&s->req);
4089 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004090 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004091 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4092 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4093 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4094 s->req.analysers &= AN_REQ_FLT_END;
4095 if (HAS_REQ_DATA_FILTERS(s))
4096 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4097 }
4098 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4099 s->res.analysers &= AN_RES_FLT_END;
4100 if (HAS_RSP_DATA_FILTERS(s))
4101 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4102 }
4103 channel_auto_close(&s->req);
4104 channel_auto_read(&s->req);
4105 channel_auto_close(&s->res);
4106 channel_auto_read(&s->res);
4107 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004108 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4109 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004110 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004111 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4112 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4113 /* server-close/keep-alive: terminate this transaction,
4114 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004115 * a fresh-new transaction, but only once we're sure there's
4116 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004117 * another request. They must not hold any pending output data
4118 * and the response buffer must realigned
4119 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004120 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004121 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004122 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004123 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004124 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004125 else {
4126 s->req.analysers = AN_REQ_FLT_END;
4127 s->res.analysers = AN_RES_FLT_END;
4128 txn->flags |= TX_WAIT_CLEANUP;
4129 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004130 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004131}
4132
Willy Tarreaud98cf932009-12-27 22:54:55 +01004133/* This function is an analyser which forwards request body (including chunk
4134 * sizes if any). It is called as soon as we must forward, even if we forward
4135 * zero byte. The only situation where it must not be called is when we're in
4136 * tunnel mode and we want to forward till the close. It's used both to forward
4137 * remaining data and to resync after end of body. It expects the msg_state to
4138 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004139 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004140 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004141 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004142 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004143int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004144{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004145 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004146 struct http_txn *txn = s->txn;
4147 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004148 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004149
Christopher Faulet45073512018-07-20 10:16:29 +02004150 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 +02004151 now_ms, __FUNCTION__,
4152 s,
4153 req,
4154 req->rex, req->wex,
4155 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004156 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004157 req->analysers);
4158
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004159 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4160 return 0;
4161
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004162 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004163 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004164 /* Output closed while we were sending data. We must abort and
4165 * wake the other side up.
4166 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004167 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004168 msg->msg_state = HTTP_MSG_ERROR;
4169 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004170 return 1;
4171 }
4172
Willy Tarreaud98cf932009-12-27 22:54:55 +01004173 /* Note that we don't have to send 100-continue back because we don't
4174 * need the data to complete our job, and it's up to the server to
4175 * decide whether to return 100, 417 or anything else in return of
4176 * an "Expect: 100-continue" header.
4177 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004178 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004179 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4180 ? HTTP_MSG_CHUNK_SIZE
4181 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004182
4183 /* TODO/filters: when http-buffer-request option is set or if a
4184 * rule on url_param exists, the first chunk size could be
4185 * already parsed. In that case, msg->next is after the chunk
4186 * size (including the CRLF after the size). So this case should
4187 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004188 }
4189
Willy Tarreau7ba23542014-04-17 21:50:00 +02004190 /* Some post-connect processing might want us to refrain from starting to
4191 * forward data. Currently, the only reason for this is "balance url_param"
4192 * whichs need to parse/process the request after we've enabled forwarding.
4193 */
4194 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004195 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004196 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004197 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004198 channel_dont_close(req); /* don't fail on early shutr */
4199 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004200 }
4201 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4202 }
4203
Willy Tarreau80a92c02014-03-12 10:41:13 +01004204 /* in most states, we should abort in case of early close */
4205 channel_auto_close(req);
4206
Willy Tarreauefdf0942014-04-24 20:08:57 +02004207 if (req->to_forward) {
4208 /* We can't process the buffer's contents yet */
4209 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004210 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004211 }
4212
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004213 if (msg->msg_state < HTTP_MSG_DONE) {
4214 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4215 ? http_msg_forward_chunked_body(s, msg)
4216 : http_msg_forward_body(s, msg));
4217 if (!ret)
4218 goto missing_data_or_waiting;
4219 if (ret < 0)
4220 goto return_bad_req;
4221 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004222
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004223 /* other states, DONE...TUNNEL */
4224 /* we don't want to forward closes on DONE except in tunnel mode. */
4225 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4226 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004227
Christopher Faulet894da4c2017-07-18 11:29:07 +02004228 http_resync_states(s);
4229 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004230 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4231 if (req->flags & CF_SHUTW) {
4232 /* request errors are most likely due to the
4233 * server aborting the transfer. */
4234 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004235 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004236 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004237 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004238 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004239 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004240 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004241 }
4242
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004243 /* If "option abortonclose" is set on the backend, we want to monitor
4244 * the client's connection and forward any shutdown notification to the
4245 * server, which will decide whether to close or to go on processing the
4246 * request. We only do that in tunnel mode, and not in other modes since
4247 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004248 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004249 channel_auto_read(req);
4250 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4251 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4252 s->si[1].flags |= SI_FL_NOLINGER;
4253 channel_auto_close(req);
4254 }
4255 else if (s->txn->meth == HTTP_METH_POST) {
4256 /* POST requests may require to read extra CRLF sent by broken
4257 * browsers and which could cause an RST to be sent upon close
4258 * on some systems (eg: Linux). */
4259 channel_auto_read(req);
4260 }
4261 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004262
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004263 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004264 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004265 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004266 if (!(s->flags & SF_ERR_MASK))
4267 s->flags |= SF_ERR_CLICL;
4268 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004269 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004270 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004271 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004272 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004273 }
4274
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004275 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4276 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004277 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004278 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004279
4280 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004281 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004282
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004283 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004284 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004285 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004286 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004287
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004288 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004289 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004290 * And when content-length is used, we never want to let the possible
4291 * shutdown be forwarded to the other side, as the state machine will
4292 * take care of it once the client responds. It's also important to
4293 * prevent TIME_WAITs from accumulating on the backend side, and for
4294 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004295 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004296 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004297 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004298
Willy Tarreau5c620922011-05-11 19:56:11 +02004299 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004300 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004301 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004302 * modes are already handled by the stream sock layer. We must not do
4303 * this in content-length mode because it could present the MSG_MORE
4304 * flag with the last block of forwarded data, which would cause an
4305 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004306 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004307 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004308 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004309
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004310 return 0;
4311
Willy Tarreaud98cf932009-12-27 22:54:55 +01004312 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004313 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004314 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004315 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004316
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004317 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004318 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004319 txn->req.msg_state = HTTP_MSG_ERROR;
4320 if (txn->status) {
4321 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004322 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004323 } else {
4324 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004325 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004326 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004327 req->analysers &= AN_REQ_FLT_END;
4328 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 +01004329
Willy Tarreaue7dff022015-04-03 01:14:29 +02004330 if (!(s->flags & SF_ERR_MASK))
4331 s->flags |= SF_ERR_PRXCOND;
4332 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004333 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004334 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004335 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004336 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004337 }
4338 return 0;
4339
4340 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004341 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004342 txn->req.msg_state = HTTP_MSG_ERROR;
4343 if (txn->status) {
4344 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004345 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004346 } else {
4347 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004348 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004349 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004350 req->analysers &= AN_REQ_FLT_END;
4351 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 +01004352
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004353 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4354 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004355 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004356 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004357
Willy Tarreaue7dff022015-04-03 01:14:29 +02004358 if (!(s->flags & SF_ERR_MASK))
4359 s->flags |= SF_ERR_SRVCL;
4360 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004361 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004362 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004363 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004364 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004365 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004366 return 0;
4367}
4368
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004369/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4370 * processing can continue on next analysers, or zero if it either needs more
4371 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004372 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004373 * when it has nothing left to do, and may remove any analyser when it wants to
4374 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004375 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004376int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004377{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004378 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004379 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004380 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004381 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004382 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004383 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004384 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004385 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004386
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004387 srv_conn = cs_conn(objt_cs(s->si[1].end));
4388
Christopher Faulet45073512018-07-20 10:16:29 +02004389 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 +02004390 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004391 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004392 rep,
4393 rep->rex, rep->wex,
4394 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004395 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004396 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004397
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004398 /*
4399 * Now parse the partial (or complete) lines.
4400 * We will check the response syntax, and also join multi-line
4401 * headers. An index of all the lines will be elaborated while
4402 * parsing.
4403 *
4404 * For the parsing, we use a 28 states FSM.
4405 *
4406 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004407 * ci_head(rep) = beginning of response
4408 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4409 * ci_tail(rep) = end of input data
4410 * msg->eol = end of current header or line (LF or CRLF)
4411 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004412 */
4413
Willy Tarreau628c40c2014-04-24 19:11:26 +02004414 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004415 /* There's a protected area at the end of the buffer for rewriting
4416 * purposes. We don't want to start to parse the request if the
4417 * protected area is affected, because we may have to move processed
4418 * data later, which is much more complicated.
4419 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004420 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004421 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004422 /* some data has still not left the buffer, wake us once that's done */
4423 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4424 goto abort_response;
4425 channel_dont_close(rep);
4426 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004427 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004428 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004429 }
4430
Willy Tarreau188e2302018-06-15 11:11:53 +02004431 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004432 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004433 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004434
Willy Tarreauf37954d2018-06-15 18:31:02 +02004435 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004436 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004437 }
4438
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004439 /* 1: we might have to print this header in debug mode */
4440 if (unlikely((global.mode & MODE_DEBUG) &&
4441 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004442 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004443 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004444
Willy Tarreauf37954d2018-06-15 18:31:02 +02004445 sol = ci_head(rep);
4446 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004447 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004448
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004449 sol += hdr_idx_first_pos(&txn->hdr_idx);
4450 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004451
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004452 while (cur_idx) {
4453 eol = sol + txn->hdr_idx.v[cur_idx].len;
4454 debug_hdr("srvhdr", s, sol, eol);
4455 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4456 cur_idx = txn->hdr_idx.v[cur_idx].next;
4457 }
4458 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004459
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004460 /*
4461 * Now we quickly check if we have found a full valid response.
4462 * If not so, we check the FD and buffer states before leaving.
4463 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004464 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004465 * responses are checked first.
4466 *
4467 * Depending on whether the client is still there or not, we
4468 * may send an error response back or not. Note that normally
4469 * we should only check for HTTP status there, and check I/O
4470 * errors somewhere else.
4471 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004472
Willy Tarreau655dce92009-11-08 13:10:58 +01004473 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004474 /* Invalid response */
4475 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4476 /* we detected a parsing error. We want to archive this response
4477 * in the dedicated proxy area for later troubleshooting.
4478 */
4479 hdr_response_bad:
4480 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004481 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004482
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004483 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004484 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004485 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004486 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004487 }
Willy Tarreau64648412010-03-05 10:41:54 +01004488 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004489 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004490 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004491 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004492 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004493 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004494 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004495
Willy Tarreaue7dff022015-04-03 01:14:29 +02004496 if (!(s->flags & SF_ERR_MASK))
4497 s->flags |= SF_ERR_PRXCOND;
4498 if (!(s->flags & SF_FINST_MASK))
4499 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004500
4501 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004502 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004503
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004504 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004505 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004506 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004507 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004508 goto hdr_response_bad;
4509 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004510
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004511 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004512 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004513 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004514 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004515 else if (txn->flags & TX_NOT_FIRST)
4516 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004517
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004518 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004519 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004520 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004521 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004522 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004523
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004524 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004525 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004526 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004527
4528 /* Check to see if the server refused the early data.
4529 * If so, just send a 425
4530 */
4531 if (objt_cs(s->si[1].end)) {
4532 struct connection *conn = objt_cs(s->si[1].end)->conn;
4533
4534 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4535 txn->status = 425;
4536 }
4537
Willy Tarreau350f4872014-11-28 14:42:25 +01004538 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004539 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004540 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004541
Willy Tarreaue7dff022015-04-03 01:14:29 +02004542 if (!(s->flags & SF_ERR_MASK))
4543 s->flags |= SF_ERR_SRVCL;
4544 if (!(s->flags & SF_FINST_MASK))
4545 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004546 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004547 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004548
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004549 /* read timeout : return a 504 to the client. */
4550 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004551 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004552 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004553
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004554 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004555 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004556 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004557 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004558 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004559
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004560 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004561 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004562 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004563 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004564 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004565 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004566
Willy Tarreaue7dff022015-04-03 01:14:29 +02004567 if (!(s->flags & SF_ERR_MASK))
4568 s->flags |= SF_ERR_SRVTO;
4569 if (!(s->flags & SF_FINST_MASK))
4570 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004571 return 0;
4572 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004573
Willy Tarreauf003d372012-11-26 13:35:37 +01004574 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004575 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004576 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4577 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004578 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004579 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004580
Christopher Faulet0184ea72017-01-05 14:06:34 +01004581 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004582 channel_auto_close(rep);
4583
4584 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004585 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004586 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004587
Willy Tarreaue7dff022015-04-03 01:14:29 +02004588 if (!(s->flags & SF_ERR_MASK))
4589 s->flags |= SF_ERR_CLICL;
4590 if (!(s->flags & SF_FINST_MASK))
4591 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004592
Willy Tarreau87b09662015-04-03 00:22:06 +02004593 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004594 return 0;
4595 }
4596
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004597 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004598 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004599 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004600 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004601 else if (txn->flags & TX_NOT_FIRST)
4602 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004603
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004604 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004605 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004606 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004607 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004608 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004609
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004610 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004611 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004612 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004613 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004614 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004615 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004616
Willy Tarreaue7dff022015-04-03 01:14:29 +02004617 if (!(s->flags & SF_ERR_MASK))
4618 s->flags |= SF_ERR_SRVCL;
4619 if (!(s->flags & SF_FINST_MASK))
4620 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004621 return 0;
4622 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004623
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004624 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004625 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004626 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004627 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004628 else if (txn->flags & TX_NOT_FIRST)
4629 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004630
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004631 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004632 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004633 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004634
Willy Tarreaue7dff022015-04-03 01:14:29 +02004635 if (!(s->flags & SF_ERR_MASK))
4636 s->flags |= SF_ERR_CLICL;
4637 if (!(s->flags & SF_FINST_MASK))
4638 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004639
Willy Tarreau87b09662015-04-03 00:22:06 +02004640 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004641 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004642 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004643
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004644 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004645 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646 return 0;
4647 }
4648
4649 /* More interesting part now : we know that we have a complete
4650 * response which at least looks like HTTP. We have an indicator
4651 * of each header's length, so we can parse them quickly.
4652 */
4653
4654 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004655 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004656
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004657 /*
4658 * 1: get the status code
4659 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004660 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004661 if (n < 1 || n > 5)
4662 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004663 /* when the client triggers a 4xx from the server, it's most often due
4664 * to a missing object or permission. These events should be tracked
4665 * because if they happen often, it may indicate a brute force or a
4666 * vulnerability scan.
4667 */
4668 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004669 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004670
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004671 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004672 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004673
Willy Tarreau91852eb2015-05-01 13:26:00 +02004674 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4675 * exactly one digit "." one digit. This check may be disabled using
4676 * option accept-invalid-http-response.
4677 */
4678 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4679 if (msg->sl.st.v_l != 8) {
4680 msg->err_pos = 0;
4681 goto hdr_response_bad;
4682 }
4683
Willy Tarreauf37954d2018-06-15 18:31:02 +02004684 if (ci_head(rep)[4] != '/' ||
4685 !isdigit((unsigned char)ci_head(rep)[5]) ||
4686 ci_head(rep)[6] != '.' ||
4687 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004688 msg->err_pos = 4;
4689 goto hdr_response_bad;
4690 }
4691 }
4692
Willy Tarreau5b154472009-12-21 20:11:07 +01004693 /* check if the response is HTTP/1.1 or above */
4694 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004695 ((ci_head(rep)[5] > '1') ||
4696 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004697 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004698
4699 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004700 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 +01004701
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004702 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004703 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004704
Willy Tarreauf37954d2018-06-15 18:31:02 +02004705 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004706
Willy Tarreau39650402010-03-15 19:44:39 +01004707 /* Adjust server's health based on status code. Note: status codes 501
4708 * and 505 are triggered on demand by client request, so we must not
4709 * count them as server failures.
4710 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004711 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004712 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004713 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004714 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004715 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004716 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004717
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004718 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004719 * We may be facing a 100-continue response, or any other informational
4720 * 1xx response which is non-final, in which case this is not the right
4721 * response, and we're waiting for the next one. Let's allow this response
4722 * to go to the client and wait for the next one. There's an exception for
4723 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004724 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004725 if (txn->status < 200 &&
4726 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004727 hdr_idx_init(&txn->hdr_idx);
4728 msg->next -= channel_forward(rep, msg->next);
4729 msg->msg_state = HTTP_MSG_RPBEFORE;
4730 txn->status = 0;
4731 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004732 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004733 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004734 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004735
Willy Tarreaua14ad722017-07-07 11:36:32 +02004736 /*
4737 * 2: check for cacheability.
4738 */
4739
4740 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004741 case 200:
4742 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004743 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004744 case 206:
4745 case 300:
4746 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004747 case 404:
4748 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004749 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004750 case 414:
4751 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004752 break;
4753 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004754 /* RFC7231#6.1:
4755 * Responses with status codes that are defined as
4756 * cacheable by default (e.g., 200, 203, 204, 206,
4757 * 300, 301, 404, 405, 410, 414, and 501 in this
4758 * specification) can be reused by a cache with
4759 * heuristic expiration unless otherwise indicated
4760 * by the method definition or explicit cache
4761 * controls [RFC7234]; all other status codes are
4762 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004763 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004764 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004765 break;
4766 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004767
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004768 /*
4769 * 3: we may need to capture headers
4770 */
4771 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004772 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004773 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4774 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004775
Willy Tarreau557f1992015-05-01 10:05:17 +02004776 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4777 * by RFC7230#3.3.3 :
4778 *
4779 * The length of a message body is determined by one of the following
4780 * (in order of precedence):
4781 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004782 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4783 * the connection will become a tunnel immediately after the empty
4784 * line that concludes the header fields. A client MUST ignore
4785 * any Content-Length or Transfer-Encoding header fields received
4786 * in such a message. Any 101 response (Switching Protocols) is
4787 * managed in the same manner.
4788 *
4789 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004790 * (Informational), 204 (No Content), or 304 (Not Modified) status
4791 * code is always terminated by the first empty line after the
4792 * header fields, regardless of the header fields present in the
4793 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004794 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004795 * 3. If a Transfer-Encoding header field is present and the chunked
4796 * transfer coding (Section 4.1) is the final encoding, the message
4797 * body length is determined by reading and decoding the chunked
4798 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004799 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004800 * If a Transfer-Encoding header field is present in a response and
4801 * the chunked transfer coding is not the final encoding, the
4802 * message body length is determined by reading the connection until
4803 * it is closed by the server. If a Transfer-Encoding header field
4804 * is present in a request and the chunked transfer coding is not
4805 * the final encoding, the message body length cannot be determined
4806 * reliably; the server MUST respond with the 400 (Bad Request)
4807 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004808 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004809 * If a message is received with both a Transfer-Encoding and a
4810 * Content-Length header field, the Transfer-Encoding overrides the
4811 * Content-Length. Such a message might indicate an attempt to
4812 * perform request smuggling (Section 9.5) or response splitting
4813 * (Section 9.4) and ought to be handled as an error. A sender MUST
4814 * remove the received Content-Length field prior to forwarding such
4815 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004816 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004817 * 4. If a message is received without Transfer-Encoding and with
4818 * either multiple Content-Length header fields having differing
4819 * field-values or a single Content-Length header field having an
4820 * invalid value, then the message framing is invalid and the
4821 * recipient MUST treat it as an unrecoverable error. If this is a
4822 * request message, the server MUST respond with a 400 (Bad Request)
4823 * status code and then close the connection. If this is a response
4824 * message received by a proxy, the proxy MUST close the connection
4825 * to the server, discard the received response, and send a 502 (Bad
4826 * Gateway) response to the client. If this is a response message
4827 * received by a user agent, the user agent MUST close the
4828 * connection to the server and discard the received response.
4829 *
4830 * 5. If a valid Content-Length header field is present without
4831 * Transfer-Encoding, its decimal value defines the expected message
4832 * body length in octets. If the sender closes the connection or
4833 * the recipient times out before the indicated number of octets are
4834 * received, the recipient MUST consider the message to be
4835 * incomplete and close the connection.
4836 *
4837 * 6. If this is a request message and none of the above are true, then
4838 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004839 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004840 * 7. Otherwise, this is a response message without a declared message
4841 * body length, so the message body length is determined by the
4842 * number of octets received prior to the server closing the
4843 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004844 */
4845
4846 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004847 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004848 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004849 * FIXME: should we parse anyway and return an error on chunked encoding ?
4850 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004851 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4852 txn->status == 101)) {
4853 /* Either we've established an explicit tunnel, or we're
4854 * switching the protocol. In both cases, we're very unlikely
4855 * to understand the next protocols. We have to switch to tunnel
4856 * mode, so that we transfer the request and responses then let
4857 * this protocol pass unmodified. When we later implement specific
4858 * parsers for such protocols, we'll want to check the Upgrade
4859 * header which contains information about that protocol for
4860 * responses with status 101 (eg: see RFC2817 about TLS).
4861 */
4862 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4863 msg->flags |= HTTP_MSGF_XFER_LEN;
4864 goto end;
4865 }
4866
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004867 if (txn->meth == HTTP_METH_HEAD ||
4868 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004869 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004870 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004871 goto skip_content_length;
4872 }
4873
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004874 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004875 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004876 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004877 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004878 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4879 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004880 /* bad transfer-encoding (chunked followed by something else) */
4881 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004882 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004883 break;
4884 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004885 }
4886
Willy Tarreau1c913912015-04-30 10:57:51 +02004887 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004888 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02004889 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004890 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02004891 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4892 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02004893 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004894 signed long long cl;
4895
Willy Tarreauad14f752011-09-02 20:33:27 +02004896 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004897 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004898 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004899 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004900
Willy Tarreauad14f752011-09-02 20:33:27 +02004901 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004902 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004903 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004904 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004905
Willy Tarreauad14f752011-09-02 20:33:27 +02004906 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004907 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004908 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004909 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004910
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004911 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004912 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004913 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004914 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004915
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004916 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004917 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004918 }
4919
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004920 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
4921 * 407 (Proxy-Authenticate) responses and set the connection to private
4922 */
4923 if (srv_conn && txn->status == 401) {
4924 /* check for Negotiate/NTLM WWW-Authenticate headers */
4925 ctx.idx = 0;
4926 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
4927 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4928 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4929 srv_conn->flags |= CO_FL_PRIVATE;
4930 }
4931 } else if (srv_conn && txn->status == 407) {
4932 /* check for Negotiate/NTLM Proxy-Authenticate headers */
4933 ctx.idx = 0;
4934 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
4935 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4936 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4937 srv_conn->flags |= CO_FL_PRIVATE;
4938 }
4939 }
4940
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004941 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01004942 /* Now we have to check if we need to modify the Connection header.
4943 * This is more difficult on the response than it is on the request,
4944 * because we can have two different HTTP versions and we don't know
4945 * how the client will interprete a response. For instance, let's say
4946 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4947 * HTTP/1.1 response without any header. Maybe it will bound itself to
4948 * HTTP/1.0 because it only knows about it, and will consider the lack
4949 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4950 * the lack of header as a keep-alive. Thus we will use two flags
4951 * indicating how a request MAY be understood by the client. In case
4952 * of multiple possibilities, we'll fix the header to be explicit. If
4953 * ambiguous cases such as both close and keepalive are seen, then we
4954 * will fall back to explicit close. Note that we won't take risks with
4955 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004956 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004957 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004958 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02004959 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01004960 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004961
Willy Tarreau60466522010-01-18 19:08:45 +01004962 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004963 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01004964 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004965
Willy Tarreau60466522010-01-18 19:08:45 +01004966 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004967 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01004968 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004969 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004970 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004971 }
Willy Tarreau60466522010-01-18 19:08:45 +01004972 else { /* SCL / KAL */
4973 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004974 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004975 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004976 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004977
Willy Tarreau60466522010-01-18 19:08:45 +01004978 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004979 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004980
Willy Tarreau60466522010-01-18 19:08:45 +01004981 /* Some keep-alive responses are converted to Server-close if
4982 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004983 */
Willy Tarreau60466522010-01-18 19:08:45 +01004984 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4985 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004986 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004987 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004988 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004989 }
4990
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004991 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02004992 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02004993 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02004994
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004995 /* end of job, return OK */
4996 rep->analysers &= ~an_bit;
4997 rep->analyse_exp = TICK_ETERNITY;
4998 channel_auto_close(rep);
4999 return 1;
5000
5001 abort_keep_alive:
5002 /* A keep-alive request to the server failed on a network error.
5003 * The client is required to retry. We need to close without returning
5004 * any other information so that the client retries.
5005 */
5006 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005007 rep->analysers &= AN_RES_FLT_END;
5008 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005009 channel_auto_close(rep);
5010 s->logs.logwait = 0;
5011 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005012 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005013 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005014 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005015 return 0;
5016}
5017
5018/* This function performs all the processing enabled for the current response.
5019 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005020 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005021 * other functions. It works like process_request (see indications above).
5022 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005023int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005024{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005025 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005026 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005027 struct http_msg *msg = &txn->rsp;
5028 struct proxy *cur_proxy;
5029 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005030 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005031
Christopher Faulet45073512018-07-20 10:16:29 +02005032 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 +02005033 now_ms, __FUNCTION__,
5034 s,
5035 rep,
5036 rep->rex, rep->wex,
5037 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005038 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005039 rep->analysers);
5040
5041 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5042 return 0;
5043
Willy Tarreau70730dd2014-04-24 18:06:27 +02005044 /* The stats applet needs to adjust the Connection header but we don't
5045 * apply any filter there.
5046 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005047 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5048 rep->analysers &= ~an_bit;
5049 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005050 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005051 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005052
Willy Tarreau58975672014-04-24 21:13:57 +02005053 /*
5054 * We will have to evaluate the filters.
5055 * As opposed to version 1.2, now they will be evaluated in the
5056 * filters order and not in the header order. This means that
5057 * each filter has to be validated among all headers.
5058 *
5059 * Filters are tried with ->be first, then with ->fe if it is
5060 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005061 *
5062 * Maybe we are in resume condiion. In this case I choose the
5063 * "struct proxy" which contains the rule list matching the resume
5064 * pointer. If none of theses "struct proxy" match, I initialise
5065 * the process with the first one.
5066 *
5067 * In fact, I check only correspondance betwwen the current list
5068 * pointer and the ->fe rule list. If it doesn't match, I initialize
5069 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005070 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005071 if (s->current_rule_list == &sess->fe->http_res_rules)
5072 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005073 else
5074 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005075 while (1) {
5076 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005077
Willy Tarreau58975672014-04-24 21:13:57 +02005078 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005079 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005080 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005081
Willy Tarreau51d861a2015-05-22 17:30:48 +02005082 if (ret == HTTP_RULE_RES_BADREQ)
5083 goto return_srv_prx_502;
5084
5085 if (ret == HTTP_RULE_RES_DONE) {
5086 rep->analysers &= ~an_bit;
5087 rep->analyse_exp = TICK_ETERNITY;
5088 return 1;
5089 }
5090 }
5091
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005092 /* we need to be called again. */
5093 if (ret == HTTP_RULE_RES_YIELD) {
5094 channel_dont_close(rep);
5095 return 0;
5096 }
5097
Willy Tarreau58975672014-04-24 21:13:57 +02005098 /* try headers filters */
5099 if (rule_set->rsp_exp != NULL) {
5100 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5101 return_bad_resp:
5102 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005103 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005104 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005105 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005106 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005107 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005108 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005109 txn->status = 502;
5110 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005111 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005112 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005113 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005114 if (!(s->flags & SF_ERR_MASK))
5115 s->flags |= SF_ERR_PRXCOND;
5116 if (!(s->flags & SF_FINST_MASK))
5117 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005118 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005119 }
Willy Tarreau58975672014-04-24 21:13:57 +02005120 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005121
Willy Tarreau58975672014-04-24 21:13:57 +02005122 /* has the response been denied ? */
5123 if (txn->flags & TX_SVDENY) {
5124 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005125 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005126
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005127 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5128 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005129 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005130 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005131
Willy Tarreau58975672014-04-24 21:13:57 +02005132 goto return_srv_prx_502;
5133 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005134
Willy Tarreau58975672014-04-24 21:13:57 +02005135 /* add response headers from the rule sets in the same order */
5136 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005137 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005138 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005139 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005140 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005141 ret = acl_pass(ret);
5142 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5143 ret = !ret;
5144 if (!ret)
5145 continue;
5146 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005147 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005148 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005149 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005150
Willy Tarreau58975672014-04-24 21:13:57 +02005151 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005152 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005153 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005154 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005155 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005156
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005157 /* After this point, this anayzer can't return yield, so we can
5158 * remove the bit corresponding to this analyzer from the list.
5159 *
5160 * Note that the intermediate returns and goto found previously
5161 * reset the analyzers.
5162 */
5163 rep->analysers &= ~an_bit;
5164 rep->analyse_exp = TICK_ETERNITY;
5165
Willy Tarreau58975672014-04-24 21:13:57 +02005166 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005167 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005168 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005169
Willy Tarreau58975672014-04-24 21:13:57 +02005170 /*
5171 * Now check for a server cookie.
5172 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005173 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005174 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005175
Willy Tarreau58975672014-04-24 21:13:57 +02005176 /*
5177 * Check for cache-control or pragma headers if required.
5178 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005179 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005180 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005181
Willy Tarreau58975672014-04-24 21:13:57 +02005182 /*
5183 * Add server cookie in the response if needed
5184 */
5185 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5186 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005187 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005188 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5189 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5190 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5191 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5192 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005193 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005194 /* the server is known, it's not the one the client requested, or the
5195 * cookie's last seen date needs to be refreshed. We have to
5196 * insert a set-cookie here, except if we want to insert only on POST
5197 * requests and this one isn't. Note that servers which don't have cookies
5198 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005199 */
Willy Tarreau58975672014-04-24 21:13:57 +02005200 if (!objt_server(s->target)->cookie) {
5201 chunk_printf(&trash,
5202 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5203 s->be->cookie_name);
5204 }
5205 else {
5206 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005207
Willy Tarreau58975672014-04-24 21:13:57 +02005208 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5209 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005210 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5211 s30tob64((date.tv_sec+3) >> 2,
5212 trash.area + trash.data);
5213 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005214
Willy Tarreau58975672014-04-24 21:13:57 +02005215 if (s->be->cookie_maxlife) {
5216 /* emit first_date, which is either the original one or
5217 * the current date.
5218 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005219 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005220 s30tob64(txn->cookie_first_date ?
5221 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005222 (date.tv_sec+3) >> 2,
5223 trash.area + trash.data);
5224 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005225 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005226 }
Willy Tarreau58975672014-04-24 21:13:57 +02005227 chunk_appendf(&trash, "; path=/");
5228 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005229
Willy Tarreau58975672014-04-24 21:13:57 +02005230 if (s->be->cookie_domain)
5231 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005232
Willy Tarreau58975672014-04-24 21:13:57 +02005233 if (s->be->ck_opts & PR_CK_HTTPONLY)
5234 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005235
Willy Tarreau58975672014-04-24 21:13:57 +02005236 if (s->be->ck_opts & PR_CK_SECURE)
5237 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005238
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005239 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005240 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005241
Willy Tarreau58975672014-04-24 21:13:57 +02005242 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005243 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005244 /* the server did not change, only the date was updated */
5245 txn->flags |= TX_SCK_UPDATED;
5246 else
5247 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005248
Willy Tarreau58975672014-04-24 21:13:57 +02005249 /* Here, we will tell an eventual cache on the client side that we don't
5250 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5251 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5252 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005253 */
Willy Tarreau58975672014-04-24 21:13:57 +02005254 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005255
Willy Tarreau58975672014-04-24 21:13:57 +02005256 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005257
Willy Tarreau58975672014-04-24 21:13:57 +02005258 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5259 "Cache-control: private", 22) < 0))
5260 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005261 }
Willy Tarreau58975672014-04-24 21:13:57 +02005262 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005263
Willy Tarreau58975672014-04-24 21:13:57 +02005264 /*
5265 * Check if result will be cacheable with a cookie.
5266 * We'll block the response if security checks have caught
5267 * nasty things such as a cacheable cookie.
5268 */
5269 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5270 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5271 (s->be->options & PR_O_CHK_CACHE)) {
5272 /* we're in presence of a cacheable response containing
5273 * a set-cookie header. We'll block it as requested by
5274 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005275 */
Willy Tarreau58975672014-04-24 21:13:57 +02005276 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005277 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005278
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005279 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5280 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005281 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005282 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005283
Christopher Faulet767a84b2017-11-24 16:50:31 +01005284 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5285 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005286 send_log(s->be, LOG_ALERT,
5287 "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>");
5289 goto return_srv_prx_502;
5290 }
Willy Tarreau03945942009-12-22 16:50:27 +01005291
Willy Tarreau70730dd2014-04-24 18:06:27 +02005292 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005293 /*
5294 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5295 * If an "Upgrade" token is found, the header is left untouched in order
5296 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005297 * if anything but "Upgrade" is present in the Connection header. We don't
5298 * want to touch any 101 response either since it's switching to another
5299 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005300 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005301 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005302 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005303 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005304
Willy Tarreau58975672014-04-24 21:13:57 +02005305 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5306 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5307 /* we want a keep-alive response here. Keep-alive header
5308 * required if either side is not 1.1.
5309 */
5310 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5311 want_flags |= TX_CON_KAL_SET;
5312 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005313 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005314 /* we want a close response here. Close header required if
5315 * the server is 1.1, regardless of the client.
5316 */
5317 if (msg->flags & HTTP_MSGF_VER_11)
5318 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005319 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005320
Willy Tarreau58975672014-04-24 21:13:57 +02005321 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5322 http_change_connection_header(txn, msg, want_flags);
5323 }
5324
5325 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005326 /* Always enter in the body analyzer */
5327 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5328 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005329
Willy Tarreau58975672014-04-24 21:13:57 +02005330 /* if the user wants to log as soon as possible, without counting
5331 * bytes from the server, then this is the right moment. We have
5332 * to temporarily assign bytes_out to log what we currently have.
5333 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005334 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005335 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5336 s->logs.bytes_out = txn->rsp.eoh;
5337 s->do_log(s);
5338 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005339 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005340 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005341}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005342
Willy Tarreaud98cf932009-12-27 22:54:55 +01005343/* This function is an analyser which forwards response body (including chunk
5344 * sizes if any). It is called as soon as we must forward, even if we forward
5345 * zero byte. The only situation where it must not be called is when we're in
5346 * tunnel mode and we want to forward till the close. It's used both to forward
5347 * remaining data and to resync after end of body. It expects the msg_state to
5348 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005349 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005350 *
5351 * It is capable of compressing response data both in content-length mode and
5352 * in chunked mode. The state machines follows different flows depending on
5353 * whether content-length and chunked modes are used, since there are no
5354 * trailers in content-length :
5355 *
5356 * chk-mode cl-mode
5357 * ,----- BODY -----.
5358 * / \
5359 * V size > 0 V chk-mode
5360 * .--> SIZE -------------> DATA -------------> CRLF
5361 * | | size == 0 | last byte |
5362 * | v final crlf v inspected |
5363 * | TRAILERS -----------> DONE |
5364 * | |
5365 * `----------------------------------------------'
5366 *
5367 * Compression only happens in the DATA state, and must be flushed in final
5368 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5369 * is performed at once on final states for all bytes parsed, or when leaving
5370 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005371 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005372int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005373{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005374 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005375 struct http_txn *txn = s->txn;
5376 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005377 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005378
Christopher Faulet45073512018-07-20 10:16:29 +02005379 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 +02005380 now_ms, __FUNCTION__,
5381 s,
5382 res,
5383 res->rex, res->wex,
5384 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005385 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005386 res->analysers);
5387
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005388 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5389 return 0;
5390
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005391 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005392 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005393 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005394 /* Output closed while we were sending data. We must abort and
5395 * wake the other side up.
5396 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005397 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005398 msg->msg_state = HTTP_MSG_ERROR;
5399 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005400 return 1;
5401 }
5402
Willy Tarreau4fe41902010-06-07 22:27:41 +02005403 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005404 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005405
Christopher Fauletd7c91962015-04-30 11:48:27 +02005406 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005407 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5408 ? HTTP_MSG_CHUNK_SIZE
5409 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005410 }
5411
Willy Tarreauefdf0942014-04-24 20:08:57 +02005412 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005413 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005414 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005415 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005416 }
5417
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005418 if (msg->msg_state < HTTP_MSG_DONE) {
5419 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5420 ? http_msg_forward_chunked_body(s, msg)
5421 : http_msg_forward_body(s, msg));
5422 if (!ret)
5423 goto missing_data_or_waiting;
5424 if (ret < 0)
5425 goto return_bad_res;
5426 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005427
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005428 /* other states, DONE...TUNNEL */
5429 /* for keep-alive we don't want to forward closes on DONE */
5430 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5431 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5432 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005433
Christopher Faulet894da4c2017-07-18 11:29:07 +02005434 http_resync_states(s);
5435 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005436 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5437 if (res->flags & CF_SHUTW) {
5438 /* response errors are most likely due to the
5439 * client aborting the transfer. */
5440 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005441 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005442 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005443 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005444 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005445 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005446 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005447 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005448 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005449
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005450 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005451 if (res->flags & CF_SHUTW)
5452 goto aborted_xfer;
5453
5454 /* stop waiting for data if the input is closed before the end. If the
5455 * client side was already closed, it means that the client has aborted,
5456 * so we don't want to count this as a server abort. Otherwise it's a
5457 * server abort.
5458 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005459 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005460 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005461 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005462 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005463 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005464 if (!(s->flags & SF_ERR_MASK))
5465 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005466 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005467 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005468 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005469 goto return_bad_res_stats_ok;
5470 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005471 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005472
Willy Tarreau40dba092010-03-04 18:14:51 +01005473 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005474 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005475 goto return_bad_res;
5476
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005477 /* When TE: chunked is used, we need to get there again to parse
5478 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005479 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5480 * or if there are filters registered on the stream, we don't want to
5481 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005482 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005483 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005484 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005485 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5486 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005487 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005488
Willy Tarreau5c620922011-05-11 19:56:11 +02005489 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005490 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005491 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005492 * modes are already handled by the stream sock layer. We must not do
5493 * this in content-length mode because it could present the MSG_MORE
5494 * flag with the last block of forwarded data, which would cause an
5495 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005496 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005497 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005498 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005499
Willy Tarreau87b09662015-04-03 00:22:06 +02005500 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005501 return 0;
5502
Willy Tarreau40dba092010-03-04 18:14:51 +01005503 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005504 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005505 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005506 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005507
5508 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005509 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005510 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005511 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005512 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005513 res->analysers &= AN_RES_FLT_END;
5514 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 +01005515 if (objt_server(s->target))
5516 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005517
Willy Tarreaue7dff022015-04-03 01:14:29 +02005518 if (!(s->flags & SF_ERR_MASK))
5519 s->flags |= SF_ERR_PRXCOND;
5520 if (!(s->flags & SF_FINST_MASK))
5521 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005522 return 0;
5523
5524 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005525 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005526 txn->rsp.msg_state = HTTP_MSG_ERROR;
5527 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005528 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005529 res->analysers &= AN_RES_FLT_END;
5530 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 +01005531
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005532 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5533 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005534 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005535 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005536
Willy Tarreaue7dff022015-04-03 01:14:29 +02005537 if (!(s->flags & SF_ERR_MASK))
5538 s->flags |= SF_ERR_CLICL;
5539 if (!(s->flags & SF_FINST_MASK))
5540 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005541 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005542}
5543
5544
Christopher Faulet10079f52018-10-03 15:17:28 +02005545int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005546{
5547 struct channel *chn = msg->chn;
5548 int ret;
5549
5550 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5551
5552 if (msg->msg_state == HTTP_MSG_ENDING)
5553 goto ending;
5554
5555 /* Neither content-length, nor transfer-encoding was found, so we must
5556 * read the body until the server connection is closed. In that case, we
5557 * eat data as they come. Of course, this happens for response only. */
5558 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005559 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005560 msg->chunk_len += len;
5561 msg->body_len += len;
5562 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005563 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005564 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005565 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005566 msg->next += ret;
5567 msg->chunk_len -= ret;
5568 if (msg->chunk_len) {
5569 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005570 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005571 chn->flags |= CF_WAKE_WRITE;
5572 goto missing_data_or_waiting;
5573 }
5574
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005575 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5576 * always set for a request. */
5577 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5578 /* The server still sending data that should be filtered */
5579 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5580 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005581 msg->msg_state = HTTP_MSG_TUNNEL;
5582 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005583 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005584
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005585 msg->msg_state = HTTP_MSG_ENDING;
5586
5587 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005588 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005589 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005590 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5591 /* default_ret */ msg->next,
5592 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005593 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005594 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005595 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5596 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005597 if (msg->next)
5598 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005599
Christopher Fauletda02e172015-12-04 09:25:05 +01005600 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5601 /* default_ret */ 1,
5602 /* on_error */ goto error,
5603 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005604 if (msg->msg_state == HTTP_MSG_ENDING)
5605 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005606 return 1;
5607
5608 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005609 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005610 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5611 /* default_ret */ msg->next,
5612 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005613 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005614 msg->next -= ret;
5615 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5616 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005617 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005618 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005619 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005620 return 0;
5621 error:
5622 return -1;
5623}
5624
Christopher Faulet10079f52018-10-03 15:17:28 +02005625int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005626{
5627 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005628 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005629 int ret;
5630
5631 /* Here we have the guarantee to be in one of the following state:
5632 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5633 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5634
Christopher Fauletca874b82018-09-20 11:31:01 +02005635 if (msg->msg_state == HTTP_MSG_ENDING)
5636 goto ending;
5637
5638 /* Don't parse chunks if there is no input data */
5639 if (!ci_data(chn))
5640 goto waiting;
5641
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005642 switch_states:
5643 switch (msg->msg_state) {
5644 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005645 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005646 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005647 /* on_error */ goto error);
5648 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005649 msg->chunk_len -= ret;
5650 if (msg->chunk_len) {
5651 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005652 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005653 chn->flags |= CF_WAKE_WRITE;
5654 goto missing_data_or_waiting;
5655 }
5656
5657 /* nothing left to forward for this chunk*/
5658 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5659 /* fall through for HTTP_MSG_CHUNK_CRLF */
5660
5661 case HTTP_MSG_CHUNK_CRLF:
5662 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005663 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005664 if (ret == 0)
5665 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005666 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005667 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005668 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005669 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005670 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005671 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005672 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005673 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5674 /* fall through for HTTP_MSG_CHUNK_SIZE */
5675
5676 case HTTP_MSG_CHUNK_SIZE:
5677 /* read the chunk size and assign it to ->chunk_len,
5678 * then set ->next to point to the body and switch to
5679 * DATA or TRAILERS state.
5680 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005681 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005682 if (ret == 0)
5683 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005684 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005685 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005686 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005687 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005688 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005689 }
5690
5691 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005692 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005693 msg->chunk_len = chunk;
5694 msg->body_len += chunk;
5695
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005696 if (msg->chunk_len) {
5697 msg->msg_state = HTTP_MSG_DATA;
5698 goto switch_states;
5699 }
5700 msg->msg_state = HTTP_MSG_TRAILERS;
5701 /* fall through for HTTP_MSG_TRAILERS */
5702
5703 case HTTP_MSG_TRAILERS:
5704 ret = http_forward_trailers(msg);
5705 if (ret < 0)
5706 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005707 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5708 /* default_ret */ 1,
5709 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005710 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005711 if (!ret)
5712 goto missing_data_or_waiting;
5713 break;
5714
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005715 default:
5716 /* This should no happen in this function */
5717 goto error;
5718 }
5719
5720 msg->msg_state = HTTP_MSG_ENDING;
5721 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005722 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005723 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005724 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005725 /* default_ret */ msg->next,
5726 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005727 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005728 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005729 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5730 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005731 if (msg->next)
5732 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005733
Christopher Fauletda02e172015-12-04 09:25:05 +01005734 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005735 /* default_ret */ 1,
5736 /* on_error */ goto error,
5737 /* on_wait */ goto waiting);
5738 msg->msg_state = HTTP_MSG_DONE;
5739 return 1;
5740
5741 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005742 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005743 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005744 /* default_ret */ msg->next,
5745 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005746 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005747 msg->next -= ret;
5748 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5749 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005750 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005751 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005752 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005753 return 0;
5754
5755 chunk_parsing_error:
5756 if (msg->err_pos >= 0) {
5757 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005758 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005759 msg->msg_state, strm_fe(s));
5760 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005761 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005762 msg, msg->msg_state, s->be);
5763 }
5764 error:
5765 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005766}
5767
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005768
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005769/* Iterate the same filter through all request headers.
5770 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005771 * Since it can manage the switch to another backend, it updates the per-proxy
5772 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005773 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005774int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005775{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005776 char *cur_ptr, *cur_end, *cur_next;
5777 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005778 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005779 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005780 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005781
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005782 last_hdr = 0;
5783
Willy Tarreauf37954d2018-06-15 18:31:02 +02005784 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005785 old_idx = 0;
5786
5787 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005788 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005789 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005790 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005791 (exp->action == ACT_ALLOW ||
5792 exp->action == ACT_DENY ||
5793 exp->action == ACT_TARPIT))
5794 return 0;
5795
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005796 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005797 if (!cur_idx)
5798 break;
5799
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005800 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005801 cur_ptr = cur_next;
5802 cur_end = cur_ptr + cur_hdr->len;
5803 cur_next = cur_end + cur_hdr->cr + 1;
5804
5805 /* Now we have one header between cur_ptr and cur_end,
5806 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005807 */
5808
Willy Tarreau15a53a42015-01-21 13:39:42 +01005809 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005810 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005811 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005812 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005813 last_hdr = 1;
5814 break;
5815
5816 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005817 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005818 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005819 break;
5820
5821 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005822 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005823 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005824 break;
5825
5826 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005827 len = exp_replace(trash.area,
5828 trash.size, cur_ptr,
5829 exp->replace, pmatch);
5830 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005831 return -1;
5832
Willy Tarreau6e27be12018-08-22 04:46:47 +02005833 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5834
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005835 /* FIXME: if the user adds a newline in the replacement, the
5836 * index will not be recalculated for now, and the new line
5837 * will not be counted as a new header.
5838 */
5839
5840 cur_end += delta;
5841 cur_next += delta;
5842 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005843 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005844 break;
5845
5846 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005847 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005848 cur_next += delta;
5849
Willy Tarreaufa355d42009-11-29 18:12:29 +01005850 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005851 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5852 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005853 cur_hdr->len = 0;
5854 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005855 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005856 break;
5857
5858 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005859 }
5860
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005861 /* keep the link from this header to next one in case of later
5862 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005863 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005864 old_idx = cur_idx;
5865 }
5866 return 0;
5867}
5868
5869
5870/* Apply the filter to the request line.
5871 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5872 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005873 * Since it can manage the switch to another backend, it updates the per-proxy
5874 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005875 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005876int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005877{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005878 char *cur_ptr, *cur_end;
5879 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005880 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005881 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005882
Willy Tarreau3d300592007-03-18 18:34:41 +01005883 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005884 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005885 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005886 (exp->action == ACT_ALLOW ||
5887 exp->action == ACT_DENY ||
5888 exp->action == ACT_TARPIT))
5889 return 0;
5890 else if (exp->action == ACT_REMOVE)
5891 return 0;
5892
5893 done = 0;
5894
Willy Tarreauf37954d2018-06-15 18:31:02 +02005895 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005896 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005897
5898 /* Now we have the request line between cur_ptr and cur_end */
5899
Willy Tarreau15a53a42015-01-21 13:39:42 +01005900 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005901 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005902 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005903 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005904 done = 1;
5905 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005906
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005907 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005908 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005909 done = 1;
5910 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005911
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005912 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005913 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005914 done = 1;
5915 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005916
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005917 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005918 len = exp_replace(trash.area, trash.size,
5919 cur_ptr, exp->replace, pmatch);
5920 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005921 return -1;
5922
Willy Tarreau6e27be12018-08-22 04:46:47 +02005923 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5924
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005925 /* FIXME: if the user adds a newline in the replacement, the
5926 * index will not be recalculated for now, and the new line
5927 * will not be counted as a new header.
5928 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005929
Willy Tarreaufa355d42009-11-29 18:12:29 +01005930 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005931 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005932 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005933 HTTP_MSG_RQMETH,
5934 cur_ptr, cur_end + 1,
5935 NULL, NULL);
5936 if (unlikely(!cur_end))
5937 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005938
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005939 /* we have a full request and we know that we have either a CR
5940 * or an LF at <ptr>.
5941 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005942 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5943 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005944 /* there is no point trying this regex on headers */
5945 return 1;
5946 }
5947 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005948 return done;
5949}
Willy Tarreau97de6242006-12-27 17:18:38 +01005950
Willy Tarreau58f10d72006-12-04 02:26:12 +01005951
Willy Tarreau58f10d72006-12-04 02:26:12 +01005952
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005953/*
Willy Tarreau87b09662015-04-03 00:22:06 +02005954 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005955 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005956 * unparsable request. Since it can manage the switch to another backend, it
5957 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005958 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005959int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005960{
Willy Tarreau192252e2015-04-04 01:47:55 +02005961 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005962 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005963 struct hdr_exp *exp;
5964
5965 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005966 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005967
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005968 /*
5969 * The interleaving of transformations and verdicts
5970 * makes it difficult to decide to continue or stop
5971 * the evaluation.
5972 */
5973
Willy Tarreau6c123b12010-01-28 20:22:06 +01005974 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5975 break;
5976
Willy Tarreau3d300592007-03-18 18:34:41 +01005977 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005978 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005979 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005980 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005981
5982 /* if this filter had a condition, evaluate it now and skip to
5983 * next filter if the condition does not match.
5984 */
5985 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005986 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005987 ret = acl_pass(ret);
5988 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5989 ret = !ret;
5990
5991 if (!ret)
5992 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005993 }
5994
5995 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005996 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005997 if (unlikely(ret < 0))
5998 return -1;
5999
6000 if (likely(ret == 0)) {
6001 /* The filter did not match the request, it can be
6002 * iterated through all headers.
6003 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006004 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6005 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006006 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006007 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006008 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006009}
6010
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006011
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006012/* Delete a value in a header between delimiters <from> and <next> in buffer
6013 * <buf>. The number of characters displaced is returned, and the pointer to
6014 * the first delimiter is updated if required. The function tries as much as
6015 * possible to respect the following principles :
6016 * - replace <from> delimiter by the <next> one unless <from> points to a
6017 * colon, in which case <next> is simply removed
6018 * - set exactly one space character after the new first delimiter, unless
6019 * there are not enough characters in the block being moved to do so.
6020 * - remove unneeded spaces before the previous delimiter and after the new
6021 * one.
6022 *
6023 * It is the caller's responsibility to ensure that :
6024 * - <from> points to a valid delimiter or the colon ;
6025 * - <next> points to a valid delimiter or the final CR/LF ;
6026 * - there are non-space chars before <from> ;
6027 * - there is a CR/LF at or after <next>.
6028 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006029int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006030{
6031 char *prev = *from;
6032
6033 if (*prev == ':') {
6034 /* We're removing the first value, preserve the colon and add a
6035 * space if possible.
6036 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006037 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006038 next++;
6039 prev++;
6040 if (prev < next)
6041 *prev++ = ' ';
6042
Willy Tarreau2235b262016-11-05 15:50:20 +01006043 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006044 next++;
6045 } else {
6046 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006047 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006048 prev--;
6049 *from = prev;
6050
6051 /* copy the delimiter and if possible a space if we're
6052 * not at the end of the line.
6053 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006054 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006055 *prev++ = *next++;
6056 if (prev + 1 < next)
6057 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006058 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006059 next++;
6060 }
6061 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006062 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006063}
6064
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006065/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006066 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006067 * desirable to call it only when needed. This code is quite complex because
6068 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6069 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006070 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006071void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006072{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006073 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006074 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006075 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006076 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006077 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6078 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006079
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006080 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006081 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006082 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006083
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006084 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006085 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006086 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006087
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006088 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006089 hdr_beg = hdr_next;
6090 hdr_end = hdr_beg + cur_hdr->len;
6091 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006092
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006093 /* We have one full header between hdr_beg and hdr_end, and the
6094 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006095 * "Cookie:" headers.
6096 */
6097
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006098 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006099 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006100 old_idx = cur_idx;
6101 continue;
6102 }
6103
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006104 del_from = NULL; /* nothing to be deleted */
6105 preserve_hdr = 0; /* assume we may kill the whole header */
6106
Willy Tarreau58f10d72006-12-04 02:26:12 +01006107 /* Now look for cookies. Conforming to RFC2109, we have to support
6108 * attributes whose name begin with a '$', and associate them with
6109 * the right cookie, if we want to delete this cookie.
6110 * So there are 3 cases for each cookie read :
6111 * 1) it's a special attribute, beginning with a '$' : ignore it.
6112 * 2) it's a server id cookie that we *MAY* want to delete : save
6113 * some pointers on it (last semi-colon, beginning of cookie...)
6114 * 3) it's an application cookie : we *MAY* have to delete a previous
6115 * "special" cookie.
6116 * At the end of loop, if a "special" cookie remains, we may have to
6117 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006118 * *MUST* delete it.
6119 *
6120 * Note: RFC2965 is unclear about the processing of spaces around
6121 * the equal sign in the ATTR=VALUE form. A careful inspection of
6122 * the RFC explicitly allows spaces before it, and not within the
6123 * tokens (attrs or values). An inspection of RFC2109 allows that
6124 * too but section 10.1.3 lets one think that spaces may be allowed
6125 * after the equal sign too, resulting in some (rare) buggy
6126 * implementations trying to do that. So let's do what servers do.
6127 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6128 * allowed quoted strings in values, with any possible character
6129 * after a backslash, including control chars and delimitors, which
6130 * causes parsing to become ambiguous. Browsers also allow spaces
6131 * within values even without quotes.
6132 *
6133 * We have to keep multiple pointers in order to support cookie
6134 * removal at the beginning, middle or end of header without
6135 * corrupting the header. All of these headers are valid :
6136 *
6137 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6138 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6139 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6140 * | | | | | | | | |
6141 * | | | | | | | | hdr_end <--+
6142 * | | | | | | | +--> next
6143 * | | | | | | +----> val_end
6144 * | | | | | +-----------> val_beg
6145 * | | | | +--------------> equal
6146 * | | | +----------------> att_end
6147 * | | +---------------------> att_beg
6148 * | +--------------------------> prev
6149 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006150 */
6151
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006152 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6153 /* Iterate through all cookies on this line */
6154
6155 /* find att_beg */
6156 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006157 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006158 att_beg++;
6159
6160 /* find att_end : this is the first character after the last non
6161 * space before the equal. It may be equal to hdr_end.
6162 */
6163 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006164
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006165 while (equal < hdr_end) {
6166 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006167 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006168 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006169 continue;
6170 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006171 }
6172
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006173 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6174 * is between <att_beg> and <equal>, both may be identical.
6175 */
6176
6177 /* look for end of cookie if there is an equal sign */
6178 if (equal < hdr_end && *equal == '=') {
6179 /* look for the beginning of the value */
6180 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006181 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006182 val_beg++;
6183
6184 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006185 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006186
6187 /* make val_end point to the first white space or delimitor after the value */
6188 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006189 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006190 val_end--;
6191 } else {
6192 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006193 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006194
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006195 /* We have nothing to do with attributes beginning with '$'. However,
6196 * they will automatically be removed if a header before them is removed,
6197 * since they're supposed to be linked together.
6198 */
6199 if (*att_beg == '$')
6200 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006201
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006202 /* Ignore cookies with no equal sign */
6203 if (equal == next) {
6204 /* This is not our cookie, so we must preserve it. But if we already
6205 * scheduled another cookie for removal, we cannot remove the
6206 * complete header, but we can remove the previous block itself.
6207 */
6208 preserve_hdr = 1;
6209 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006210 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006211 val_end += delta;
6212 next += delta;
6213 hdr_end += delta;
6214 hdr_next += delta;
6215 cur_hdr->len += delta;
6216 http_msg_move_end(&txn->req, delta);
6217 prev = del_from;
6218 del_from = NULL;
6219 }
6220 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006221 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006222
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006223 /* if there are spaces around the equal sign, we need to
6224 * strip them otherwise we'll get trouble for cookie captures,
6225 * or even for rewrites. Since this happens extremely rarely,
6226 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006227 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006228 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6229 int stripped_before = 0;
6230 int stripped_after = 0;
6231
6232 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006233 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006234 equal += stripped_before;
6235 val_beg += stripped_before;
6236 }
6237
6238 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006239 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006240 val_beg += stripped_after;
6241 stripped_before += stripped_after;
6242 }
6243
6244 val_end += stripped_before;
6245 next += stripped_before;
6246 hdr_end += stripped_before;
6247 hdr_next += stripped_before;
6248 cur_hdr->len += stripped_before;
6249 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006250 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006251 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006252
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006253 /* First, let's see if we want to capture this cookie. We check
6254 * that we don't already have a client side cookie, because we
6255 * can only capture one. Also as an optimisation, we ignore
6256 * cookies shorter than the declared name.
6257 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006258 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6259 (val_end - att_beg >= sess->fe->capture_namelen) &&
6260 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006261 int log_len = val_end - att_beg;
6262
Willy Tarreaubafbe012017-11-24 17:34:44 +01006263 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006264 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006265 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006266 if (log_len > sess->fe->capture_len)
6267 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006268 memcpy(txn->cli_cookie, att_beg, log_len);
6269 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006270 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006271 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006272
Willy Tarreaubca99692010-10-06 19:25:55 +02006273 /* Persistence cookies in passive, rewrite or insert mode have the
6274 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006275 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006276 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006277 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006278 * For cookies in prefix mode, the form is :
6279 *
6280 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006281 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006282 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6283 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6284 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006285 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006286
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006287 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6288 * have the server ID between val_beg and delim, and the original cookie between
6289 * delim+1 and val_end. Otherwise, delim==val_end :
6290 *
6291 * Cookie: NAME=SRV; # in all but prefix modes
6292 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6293 * | || || | |+-> next
6294 * | || || | +--> val_end
6295 * | || || +---------> delim
6296 * | || |+------------> val_beg
6297 * | || +-------------> att_end = equal
6298 * | |+-----------------> att_beg
6299 * | +------------------> prev
6300 * +-------------------------> hdr_beg
6301 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006302
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006303 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006304 for (delim = val_beg; delim < val_end; delim++)
6305 if (*delim == COOKIE_DELIM)
6306 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006307 } else {
6308 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006309 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006310 /* Now check if the cookie contains a date field, which would
6311 * appear after a vertical bar ('|') just after the server name
6312 * and before the delimiter.
6313 */
6314 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6315 if (vbar1) {
6316 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006317 * right is the last seen date. It is a base64 encoded
6318 * 30-bit value representing the UNIX date since the
6319 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006320 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006321 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006322 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006323 if (val_end - vbar1 >= 5) {
6324 val = b64tos30(vbar1);
6325 if (val > 0)
6326 txn->cookie_last_date = val << 2;
6327 }
6328 /* look for a second vertical bar */
6329 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6330 if (vbar1 && (val_end - vbar1 > 5)) {
6331 val = b64tos30(vbar1 + 1);
6332 if (val > 0)
6333 txn->cookie_first_date = val << 2;
6334 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006335 }
6336 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006337
Willy Tarreauf64d1412010-10-07 20:06:11 +02006338 /* if the cookie has an expiration date and the proxy wants to check
6339 * it, then we do that now. We first check if the cookie is too old,
6340 * then only if it has expired. We detect strict overflow because the
6341 * time resolution here is not great (4 seconds). Cookies with dates
6342 * in the future are ignored if their offset is beyond one day. This
6343 * allows an admin to fix timezone issues without expiring everyone
6344 * and at the same time avoids keeping unwanted side effects for too
6345 * long.
6346 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006347 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6348 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006349 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006350 txn->flags &= ~TX_CK_MASK;
6351 txn->flags |= TX_CK_OLD;
6352 delim = val_beg; // let's pretend we have not found the cookie
6353 txn->cookie_first_date = 0;
6354 txn->cookie_last_date = 0;
6355 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006356 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6357 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006358 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006359 txn->flags &= ~TX_CK_MASK;
6360 txn->flags |= TX_CK_EXPIRED;
6361 delim = val_beg; // let's pretend we have not found the cookie
6362 txn->cookie_first_date = 0;
6363 txn->cookie_last_date = 0;
6364 }
6365
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006366 /* Here, we'll look for the first running server which supports the cookie.
6367 * This allows to share a same cookie between several servers, for example
6368 * to dedicate backup servers to specific servers only.
6369 * However, to prevent clients from sticking to cookie-less backup server
6370 * when they have incidentely learned an empty cookie, we simply ignore
6371 * empty cookies and mark them as invalid.
6372 * The same behaviour is applied when persistence must be ignored.
6373 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006374 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006375 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006376
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006377 while (srv) {
6378 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6379 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006380 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006381 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006382 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006383 /* we found the server and we can use it */
6384 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006385 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006386 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006387 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006388 break;
6389 } else {
6390 /* we found a server, but it's down,
6391 * mark it as such and go on in case
6392 * another one is available.
6393 */
6394 txn->flags &= ~TX_CK_MASK;
6395 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006396 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006397 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006398 srv = srv->next;
6399 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006400
Willy Tarreauf64d1412010-10-07 20:06:11 +02006401 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006402 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006403 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006404 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006405 txn->flags |= TX_CK_UNUSED;
6406 else
6407 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006408 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006409
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006410 /* depending on the cookie mode, we may have to either :
6411 * - delete the complete cookie if we're in insert+indirect mode, so that
6412 * the server never sees it ;
6413 * - remove the server id from the cookie value, and tag the cookie as an
6414 * application cookie so that it does not get accidentely removed later,
6415 * if we're in cookie prefix mode
6416 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006417 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006418 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006419
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006420 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006421 val_end += delta;
6422 next += delta;
6423 hdr_end += delta;
6424 hdr_next += delta;
6425 cur_hdr->len += delta;
6426 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006427
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006428 del_from = NULL;
6429 preserve_hdr = 1; /* we want to keep this cookie */
6430 }
6431 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006432 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006433 del_from = prev;
6434 }
6435 } else {
6436 /* This is not our cookie, so we must preserve it. But if we already
6437 * scheduled another cookie for removal, we cannot remove the
6438 * complete header, but we can remove the previous block itself.
6439 */
6440 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006441
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006442 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006443 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006444 if (att_beg >= del_from)
6445 att_beg += delta;
6446 if (att_end >= del_from)
6447 att_end += delta;
6448 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006449 val_end += delta;
6450 next += delta;
6451 hdr_end += delta;
6452 hdr_next += delta;
6453 cur_hdr->len += delta;
6454 http_msg_move_end(&txn->req, delta);
6455 prev = del_from;
6456 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006457 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006458 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006459
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006460 /* continue with next cookie on this header line */
6461 att_beg = next;
6462 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006463
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006464 /* There are no more cookies on this line.
6465 * We may still have one (or several) marked for deletion at the
6466 * end of the line. We must do this now in two ways :
6467 * - if some cookies must be preserved, we only delete from the
6468 * mark to the end of line ;
6469 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006470 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006471 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006472 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006473 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006474 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006475 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006476 cur_hdr->len += delta;
6477 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006478 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006479
6480 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006481 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6482 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006483 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006484 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006485 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006486 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006487 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006488 }
6489
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006490 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006491 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006492 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006493}
6494
6495
Willy Tarreaua15645d2007-03-18 16:22:39 +01006496/* Iterate the same filter through all response headers contained in <rtr>.
6497 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6498 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006499int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006500{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006501 char *cur_ptr, *cur_end, *cur_next;
6502 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006503 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006504 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006505 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006506
6507 last_hdr = 0;
6508
Willy Tarreauf37954d2018-06-15 18:31:02 +02006509 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006510 old_idx = 0;
6511
6512 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006513 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006514 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006515 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006516 (exp->action == ACT_ALLOW ||
6517 exp->action == ACT_DENY))
6518 return 0;
6519
6520 cur_idx = txn->hdr_idx.v[old_idx].next;
6521 if (!cur_idx)
6522 break;
6523
6524 cur_hdr = &txn->hdr_idx.v[cur_idx];
6525 cur_ptr = cur_next;
6526 cur_end = cur_ptr + cur_hdr->len;
6527 cur_next = cur_end + cur_hdr->cr + 1;
6528
6529 /* Now we have one header between cur_ptr and cur_end,
6530 * and the next header starts at cur_next.
6531 */
6532
Willy Tarreau15a53a42015-01-21 13:39:42 +01006533 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006534 switch (exp->action) {
6535 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006536 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006537 last_hdr = 1;
6538 break;
6539
6540 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006541 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006542 last_hdr = 1;
6543 break;
6544
6545 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006546 len = exp_replace(trash.area,
6547 trash.size, cur_ptr,
6548 exp->replace, pmatch);
6549 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006550 return -1;
6551
Willy Tarreau6e27be12018-08-22 04:46:47 +02006552 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6553
Willy Tarreaua15645d2007-03-18 16:22:39 +01006554 /* FIXME: if the user adds a newline in the replacement, the
6555 * index will not be recalculated for now, and the new line
6556 * will not be counted as a new header.
6557 */
6558
6559 cur_end += delta;
6560 cur_next += delta;
6561 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006562 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006563 break;
6564
6565 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006566 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006567 cur_next += delta;
6568
Willy Tarreaufa355d42009-11-29 18:12:29 +01006569 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006570 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6571 txn->hdr_idx.used--;
6572 cur_hdr->len = 0;
6573 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006574 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006575 break;
6576
6577 }
6578 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006579
6580 /* keep the link from this header to next one in case of later
6581 * removal of next header.
6582 */
6583 old_idx = cur_idx;
6584 }
6585 return 0;
6586}
6587
6588
6589/* Apply the filter to the status line in the response buffer <rtr>.
6590 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6591 * or -1 if a replacement resulted in an invalid status line.
6592 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006593int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006594{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006595 char *cur_ptr, *cur_end;
6596 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006597 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006598 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006599
Willy Tarreau3d300592007-03-18 18:34:41 +01006600 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006601 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006602 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006603 (exp->action == ACT_ALLOW ||
6604 exp->action == ACT_DENY))
6605 return 0;
6606 else if (exp->action == ACT_REMOVE)
6607 return 0;
6608
6609 done = 0;
6610
Willy Tarreauf37954d2018-06-15 18:31:02 +02006611 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006612 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006613
6614 /* Now we have the status line between cur_ptr and cur_end */
6615
Willy Tarreau15a53a42015-01-21 13:39:42 +01006616 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006617 switch (exp->action) {
6618 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006619 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006620 done = 1;
6621 break;
6622
6623 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006624 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006625 done = 1;
6626 break;
6627
6628 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006629 len = exp_replace(trash.area, trash.size,
6630 cur_ptr, exp->replace, pmatch);
6631 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006632 return -1;
6633
Willy Tarreau6e27be12018-08-22 04:46:47 +02006634 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6635
Willy Tarreaua15645d2007-03-18 16:22:39 +01006636 /* FIXME: if the user adds a newline in the replacement, the
6637 * index will not be recalculated for now, and the new line
6638 * will not be counted as a new header.
6639 */
6640
Willy Tarreaufa355d42009-11-29 18:12:29 +01006641 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006642 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006643 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006644 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006645 cur_ptr, cur_end + 1,
6646 NULL, NULL);
6647 if (unlikely(!cur_end))
6648 return -1;
6649
6650 /* we have a full respnse and we know that we have either a CR
6651 * or an LF at <ptr>.
6652 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006653 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006654 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006655 /* there is no point trying this regex on headers */
6656 return 1;
6657 }
6658 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006659 return done;
6660}
6661
6662
6663
6664/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006665 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006666 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6667 * unparsable response.
6668 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006669int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006670{
Willy Tarreau192252e2015-04-04 01:47:55 +02006671 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006672 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006673 struct hdr_exp *exp;
6674
6675 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006676 int ret;
6677
6678 /*
6679 * The interleaving of transformations and verdicts
6680 * makes it difficult to decide to continue or stop
6681 * the evaluation.
6682 */
6683
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006684 if (txn->flags & TX_SVDENY)
6685 break;
6686
Willy Tarreau3d300592007-03-18 18:34:41 +01006687 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006688 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6689 exp->action == ACT_PASS)) {
6690 exp = exp->next;
6691 continue;
6692 }
6693
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006694 /* if this filter had a condition, evaluate it now and skip to
6695 * next filter if the condition does not match.
6696 */
6697 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006698 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006699 ret = acl_pass(ret);
6700 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6701 ret = !ret;
6702 if (!ret)
6703 continue;
6704 }
6705
Willy Tarreaua15645d2007-03-18 16:22:39 +01006706 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006707 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708 if (unlikely(ret < 0))
6709 return -1;
6710
6711 if (likely(ret == 0)) {
6712 /* The filter did not match the response, it can be
6713 * iterated through all headers.
6714 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006715 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6716 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006717 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006718 }
6719 return 0;
6720}
6721
6722
Willy Tarreaua15645d2007-03-18 16:22:39 +01006723/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006724 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006725 * desirable to call it only when needed. This function is also used when we
6726 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006727 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006728void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006729{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006730 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006731 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006732 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006733 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006734 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006735 char *hdr_beg, *hdr_end, *hdr_next;
6736 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737
Willy Tarreaua15645d2007-03-18 16:22:39 +01006738 /* Iterate through the headers.
6739 * we start with the start line.
6740 */
6741 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006742 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006743
6744 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6745 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006746 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006747
6748 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006749 hdr_beg = hdr_next;
6750 hdr_end = hdr_beg + cur_hdr->len;
6751 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006752
Willy Tarreau24581ba2010-08-31 22:39:35 +02006753 /* We have one full header between hdr_beg and hdr_end, and the
6754 * next header starts at hdr_next. We're only interested in
6755 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006756 */
6757
Willy Tarreau24581ba2010-08-31 22:39:35 +02006758 is_cookie2 = 0;
6759 prev = hdr_beg + 10;
6760 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006761 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006762 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6763 if (!val) {
6764 old_idx = cur_idx;
6765 continue;
6766 }
6767 is_cookie2 = 1;
6768 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006769 }
6770
Willy Tarreau24581ba2010-08-31 22:39:35 +02006771 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6772 * <prev> points to the colon.
6773 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006774 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006775
Willy Tarreau24581ba2010-08-31 22:39:35 +02006776 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6777 * check-cache is enabled) and we are not interested in checking
6778 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006779 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006780 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006781 return;
6782
Willy Tarreau24581ba2010-08-31 22:39:35 +02006783 /* OK so now we know we have to process this response cookie.
6784 * The format of the Set-Cookie header is slightly different
6785 * from the format of the Cookie header in that it does not
6786 * support the comma as a cookie delimiter (thus the header
6787 * cannot be folded) because the Expires attribute described in
6788 * the original Netscape's spec may contain an unquoted date
6789 * with a comma inside. We have to live with this because
6790 * many browsers don't support Max-Age and some browsers don't
6791 * support quoted strings. However the Set-Cookie2 header is
6792 * clean.
6793 *
6794 * We have to keep multiple pointers in order to support cookie
6795 * removal at the beginning, middle or end of header without
6796 * corrupting the header (in case of set-cookie2). A special
6797 * pointer, <scav> points to the beginning of the set-cookie-av
6798 * fields after the first semi-colon. The <next> pointer points
6799 * either to the end of line (set-cookie) or next unquoted comma
6800 * (set-cookie2). All of these headers are valid :
6801 *
6802 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6803 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6804 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6805 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6806 * | | | | | | | | | |
6807 * | | | | | | | | +-> next hdr_end <--+
6808 * | | | | | | | +------------> scav
6809 * | | | | | | +--------------> val_end
6810 * | | | | | +--------------------> val_beg
6811 * | | | | +----------------------> equal
6812 * | | | +------------------------> att_end
6813 * | | +----------------------------> att_beg
6814 * | +------------------------------> prev
6815 * +-----------------------------------------> hdr_beg
6816 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006817
Willy Tarreau24581ba2010-08-31 22:39:35 +02006818 for (; prev < hdr_end; prev = next) {
6819 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006820
Willy Tarreau24581ba2010-08-31 22:39:35 +02006821 /* find att_beg */
6822 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006823 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006824 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006825
Willy Tarreau24581ba2010-08-31 22:39:35 +02006826 /* find att_end : this is the first character after the last non
6827 * space before the equal. It may be equal to hdr_end.
6828 */
6829 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006830
Willy Tarreau24581ba2010-08-31 22:39:35 +02006831 while (equal < hdr_end) {
6832 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6833 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006834 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006835 continue;
6836 att_end = equal;
6837 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006838
Willy Tarreau24581ba2010-08-31 22:39:35 +02006839 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6840 * is between <att_beg> and <equal>, both may be identical.
6841 */
6842
6843 /* look for end of cookie if there is an equal sign */
6844 if (equal < hdr_end && *equal == '=') {
6845 /* look for the beginning of the value */
6846 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006847 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006848 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006849
Willy Tarreau24581ba2010-08-31 22:39:35 +02006850 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006851 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006852
6853 /* make val_end point to the first white space or delimitor after the value */
6854 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006855 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006856 val_end--;
6857 } else {
6858 /* <equal> points to next comma, semi-colon or EOL */
6859 val_beg = val_end = next = equal;
6860 }
6861
6862 if (next < hdr_end) {
6863 /* Set-Cookie2 supports multiple cookies, and <next> points to
6864 * a colon or semi-colon before the end. So skip all attr-value
6865 * pairs and look for the next comma. For Set-Cookie, since
6866 * commas are permitted in values, skip to the end.
6867 */
6868 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006869 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006870 else
6871 next = hdr_end;
6872 }
6873
6874 /* Now everything is as on the diagram above */
6875
6876 /* Ignore cookies with no equal sign */
6877 if (equal == val_end)
6878 continue;
6879
6880 /* If there are spaces around the equal sign, we need to
6881 * strip them otherwise we'll get trouble for cookie captures,
6882 * or even for rewrites. Since this happens extremely rarely,
6883 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006884 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006885 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6886 int stripped_before = 0;
6887 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006888
Willy Tarreau24581ba2010-08-31 22:39:35 +02006889 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006890 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006891 equal += stripped_before;
6892 val_beg += stripped_before;
6893 }
6894
6895 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006896 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006897 val_beg += stripped_after;
6898 stripped_before += stripped_after;
6899 }
6900
6901 val_end += stripped_before;
6902 next += stripped_before;
6903 hdr_end += stripped_before;
6904 hdr_next += stripped_before;
6905 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006906 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006907 }
6908
6909 /* First, let's see if we want to capture this cookie. We check
6910 * that we don't already have a server side cookie, because we
6911 * can only capture one. Also as an optimisation, we ignore
6912 * cookies shorter than the declared name.
6913 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006914 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006915 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006916 (val_end - att_beg >= sess->fe->capture_namelen) &&
6917 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006918 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006919 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006920 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01006921 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006922 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006923 if (log_len > sess->fe->capture_len)
6924 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01006925 memcpy(txn->srv_cookie, att_beg, log_len);
6926 txn->srv_cookie[log_len] = 0;
6927 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006928 }
6929
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006930 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006931 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006932 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006933 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6934 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006935 /* assume passive cookie by default */
6936 txn->flags &= ~TX_SCK_MASK;
6937 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006938
6939 /* If the cookie is in insert mode on a known server, we'll delete
6940 * this occurrence because we'll insert another one later.
6941 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006942 * a direct access.
6943 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006944 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006945 /* The "preserve" flag was set, we don't want to touch the
6946 * server's cookie.
6947 */
6948 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006949 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006950 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006951 /* this cookie must be deleted */
6952 if (*prev == ':' && next == hdr_end) {
6953 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006954 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006955 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6956 txn->hdr_idx.used--;
6957 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006958 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006959 hdr_next += delta;
6960 http_msg_move_end(&txn->rsp, delta);
6961 /* note: while both invalid now, <next> and <hdr_end>
6962 * are still equal, so the for() will stop as expected.
6963 */
6964 } else {
6965 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006966 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006967 next = prev;
6968 hdr_end += delta;
6969 hdr_next += delta;
6970 cur_hdr->len += delta;
6971 http_msg_move_end(&txn->rsp, delta);
6972 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006973 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006974 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006975 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006976 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006977 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006978 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006979 * with this server since we know it.
6980 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006981 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006982 next += delta;
6983 hdr_end += delta;
6984 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006985 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006986 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006987
Willy Tarreauf1348312010-10-07 15:54:11 +02006988 txn->flags &= ~TX_SCK_MASK;
6989 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006990 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006991 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006992 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006993 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006995 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006996 next += delta;
6997 hdr_end += delta;
6998 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006999 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007000 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007001
Willy Tarreau827aee92011-03-10 16:55:02 +01007002 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007003 txn->flags &= ~TX_SCK_MASK;
7004 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007005 }
7006 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007007 /* that's done for this cookie, check the next one on the same
7008 * line when next != hdr_end (only if is_cookie2).
7009 */
7010 }
7011 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007012 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007013 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007014}
7015
7016
Willy Tarreaua15645d2007-03-18 16:22:39 +01007017/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007018 * Parses the Cache-Control and Pragma request header fields to determine if
7019 * the request may be served from the cache and/or if it is cacheable. Updates
7020 * s->txn->flags.
7021 */
7022void check_request_for_cacheability(struct stream *s, struct channel *chn)
7023{
7024 struct http_txn *txn = s->txn;
7025 char *p1, *p2;
7026 char *cur_ptr, *cur_end, *cur_next;
7027 int pragma_found;
7028 int cc_found;
7029 int cur_idx;
7030
7031 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7032 return; /* nothing more to do here */
7033
7034 cur_idx = 0;
7035 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007036 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007037
7038 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7039 struct hdr_idx_elem *cur_hdr;
7040 int val;
7041
7042 cur_hdr = &txn->hdr_idx.v[cur_idx];
7043 cur_ptr = cur_next;
7044 cur_end = cur_ptr + cur_hdr->len;
7045 cur_next = cur_end + cur_hdr->cr + 1;
7046
7047 /* We have one full header between cur_ptr and cur_end, and the
7048 * next header starts at cur_next.
7049 */
7050
7051 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7052 if (val) {
7053 if ((cur_end - (cur_ptr + val) >= 8) &&
7054 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7055 pragma_found = 1;
7056 continue;
7057 }
7058 }
7059
William Lallemand8a16fe02018-05-22 11:04:33 +02007060 /* Don't use the cache and don't try to store if we found the
7061 * Authorization header */
7062 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7063 if (val) {
7064 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7065 txn->flags |= TX_CACHE_IGNORE;
7066 continue;
7067 }
7068
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007069 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7070 if (!val)
7071 continue;
7072
7073 /* OK, right now we know we have a cache-control header at cur_ptr */
7074 cc_found = 1;
7075 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7076
7077 if (p1 >= cur_end) /* no more info */
7078 continue;
7079
7080 /* p1 is at the beginning of the value */
7081 p2 = p1;
7082 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7083 p2++;
7084
7085 /* we have a complete value between p1 and p2. We don't check the
7086 * values after max-age, max-stale nor min-fresh, we simply don't
7087 * use the cache when they're specified.
7088 */
7089 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7090 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7091 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7092 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7093 txn->flags |= TX_CACHE_IGNORE;
7094 continue;
7095 }
7096
7097 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7098 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7099 continue;
7100 }
7101 }
7102
7103 /* RFC7234#5.4:
7104 * When the Cache-Control header field is also present and
7105 * understood in a request, Pragma is ignored.
7106 * When the Cache-Control header field is not present in a
7107 * request, caches MUST consider the no-cache request
7108 * pragma-directive as having the same effect as if
7109 * "Cache-Control: no-cache" were present.
7110 */
7111 if (!cc_found && pragma_found)
7112 txn->flags |= TX_CACHE_IGNORE;
7113}
7114
7115/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007116 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007117 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007118void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007119{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007120 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007121 char *p1, *p2;
7122
7123 char *cur_ptr, *cur_end, *cur_next;
7124 int cur_idx;
7125
Willy Tarreau12b32f22017-12-21 16:08:09 +01007126 if (txn->status < 200) {
7127 /* do not try to cache interim responses! */
7128 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007129 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007130 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007131
7132 /* Iterate through the headers.
7133 * we start with the start line.
7134 */
7135 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007136 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007137
7138 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7139 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007140 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007141
7142 cur_hdr = &txn->hdr_idx.v[cur_idx];
7143 cur_ptr = cur_next;
7144 cur_end = cur_ptr + cur_hdr->len;
7145 cur_next = cur_end + cur_hdr->cr + 1;
7146
7147 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007148 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007149 */
7150
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007151 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7152 if (val) {
7153 if ((cur_end - (cur_ptr + val) >= 8) &&
7154 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7155 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7156 return;
7157 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007158 }
7159
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007160 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7161 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007162 continue;
7163
7164 /* OK, right now we know we have a cache-control header at cur_ptr */
7165
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007166 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007167
7168 if (p1 >= cur_end) /* no more info */
7169 continue;
7170
7171 /* p1 is at the beginning of the value */
7172 p2 = p1;
7173
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007174 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007175 p2++;
7176
7177 /* we have a complete value between p1 and p2 */
7178 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007179 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7180 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7181 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7182 continue;
7183 }
7184
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185 /* we have something of the form no-cache="set-cookie" */
7186 if ((cur_end - p1 >= 21) &&
7187 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7188 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007189 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007190 continue;
7191 }
7192
7193 /* OK, so we know that either p2 points to the end of string or to a comma */
7194 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007195 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007196 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007197 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007198 return;
7199 }
7200
7201 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007202 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203 continue;
7204 }
7205 }
7206}
7207
Willy Tarreau58f10d72006-12-04 02:26:12 +01007208
Willy Tarreaub2513902006-12-17 14:52:38 +01007209/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007210 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007211 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007212 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007213 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007214 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007215 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007216 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007217 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007218int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007219{
7220 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007221 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007222 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007223
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007224 if (!uri_auth)
7225 return 0;
7226
Cyril Bonté70be45d2010-10-12 00:14:35 +02007227 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007228 return 0;
7229
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007230 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007231 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007232 return 0;
7233
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007234 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007235 return 0;
7236
Willy Tarreaub2513902006-12-17 14:52:38 +01007237 return 1;
7238}
7239
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007240/* Append the description of what is present in error snapshot <es> into <out>.
7241 * The description must be small enough to always fit in a trash. The output
7242 * buffer may be the trash so the trash must not be used inside this function.
7243 */
7244void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7245{
7246 chunk_appendf(&trash,
7247 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7248 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7249 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7250 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7251 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7252 es->ctx.http.m_flags, es->ctx.http.m_clen,
7253 es->ctx.http.m_blen, es->ctx.http.b_flags);
7254}
7255
Willy Tarreau4076a152009-04-02 15:18:36 +02007256/*
7257 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007258 * By default it tries to report the error position as msg->err_pos. However if
7259 * this one is not set, it will then report msg->next, which is the last known
7260 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007261 * displays buffers as a contiguous area starting at buf->p. The direction is
7262 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007263 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007264void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007265 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007266 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007267{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007268 union error_snapshot_ctx ctx;
7269 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007270
Willy Tarreauef3ca732018-09-07 15:47:35 +02007271 /* http-specific part now */
7272 ctx.http.sid = s->uniq_id;
7273 ctx.http.state = state;
7274 ctx.http.b_flags = msg->chn->flags;
7275 ctx.http.s_flags = s->flags;
7276 ctx.http.t_flags = s->txn->flags;
7277 ctx.http.m_flags = msg->flags;
7278 ctx.http.m_clen = msg->chunk_len;
7279 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007280
Willy Tarreauef3ca732018-09-07 15:47:35 +02007281 ofs = msg->chn->total - ci_data(msg->chn);
7282 if (ofs < 0)
7283 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007284
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007285 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007286 other_end, s->target,
7287 strm_sess(s), &msg->chn->buf,
7288 ofs, co_data(msg->chn),
7289 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7290 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007291}
Willy Tarreaub2513902006-12-17 14:52:38 +01007292
Willy Tarreau294c4732011-12-16 21:35:50 +01007293/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7294 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7295 * performed over the whole headers. Otherwise it must contain a valid header
7296 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7297 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7298 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7299 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007300 * -1. The value fetch stops at commas, so this function is suited for use with
7301 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007302 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007303 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007304unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007305 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007306 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007307{
Willy Tarreau294c4732011-12-16 21:35:50 +01007308 struct hdr_ctx local_ctx;
7309 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007310 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007311 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007312 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007313
Willy Tarreau294c4732011-12-16 21:35:50 +01007314 if (!ctx) {
7315 local_ctx.idx = 0;
7316 ctx = &local_ctx;
7317 }
7318
Willy Tarreaubce70882009-09-07 11:51:47 +02007319 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007320 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007321 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007322 occ--;
7323 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007324 *vptr = ctx->line + ctx->val;
7325 *vlen = ctx->vlen;
7326 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007327 }
7328 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007329 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007330 }
7331
7332 /* negative occurrence, we scan all the list then walk back */
7333 if (-occ > MAX_HDR_HISTORY)
7334 return 0;
7335
Willy Tarreau294c4732011-12-16 21:35:50 +01007336 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007337 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007338 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7339 len_hist[hist_ptr] = ctx->vlen;
7340 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007341 hist_ptr = 0;
7342 found++;
7343 }
7344 if (-occ > found)
7345 return 0;
7346 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007347 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7348 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7349 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007350 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007351 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007352 if (hist_ptr >= MAX_HDR_HISTORY)
7353 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007354 *vptr = ptr_hist[hist_ptr];
7355 *vlen = len_hist[hist_ptr];
7356 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007357}
7358
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007359/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7360 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7361 * performed over the whole headers. Otherwise it must contain a valid header
7362 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7363 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7364 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7365 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7366 * -1. This function differs from http_get_hdr() in that it only returns full
7367 * line header values and does not stop at commas.
7368 * The return value is 0 if nothing was found, or non-zero otherwise.
7369 */
7370unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7371 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007372 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007373{
7374 struct hdr_ctx local_ctx;
7375 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007376 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007377 unsigned int hist_ptr;
7378 int found;
7379
7380 if (!ctx) {
7381 local_ctx.idx = 0;
7382 ctx = &local_ctx;
7383 }
7384
7385 if (occ >= 0) {
7386 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007387 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007388 occ--;
7389 if (occ <= 0) {
7390 *vptr = ctx->line + ctx->val;
7391 *vlen = ctx->vlen;
7392 return 1;
7393 }
7394 }
7395 return 0;
7396 }
7397
7398 /* negative occurrence, we scan all the list then walk back */
7399 if (-occ > MAX_HDR_HISTORY)
7400 return 0;
7401
7402 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007403 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007404 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7405 len_hist[hist_ptr] = ctx->vlen;
7406 if (++hist_ptr >= MAX_HDR_HISTORY)
7407 hist_ptr = 0;
7408 found++;
7409 }
7410 if (-occ > found)
7411 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007412
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007413 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007414 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7415 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7416 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007417 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007418 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007419 if (hist_ptr >= MAX_HDR_HISTORY)
7420 hist_ptr -= MAX_HDR_HISTORY;
7421 *vptr = ptr_hist[hist_ptr];
7422 *vlen = len_hist[hist_ptr];
7423 return 1;
7424}
7425
Willy Tarreaubaaee002006-06-26 02:48:02 +02007426/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007427 * Print a debug line with a header. Always stop at the first CR or LF char,
7428 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7429 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007430 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007431void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007432{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007433 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007434 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007435
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007436 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007437 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007438 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007439 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 +02007440
7441 for (max = 0; start + max < end; max++)
7442 if (start[max] == '\r' || start[max] == '\n')
7443 break;
7444
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007445 UBOUND(max, trash.size - trash.data - 3);
7446 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7447 trash.area[trash.data++] = '\n';
7448 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007449}
7450
Willy Tarreaueee5b512015-04-03 23:46:31 +02007451
7452/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7453 * The hdr_idx is allocated as well. In case of allocation failure, everything
7454 * allocated is freed and NULL is returned. Otherwise the new transaction is
7455 * assigned to the stream and returned.
7456 */
7457struct http_txn *http_alloc_txn(struct stream *s)
7458{
7459 struct http_txn *txn = s->txn;
7460
7461 if (txn)
7462 return txn;
7463
Willy Tarreaubafbe012017-11-24 17:34:44 +01007464 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007465 if (!txn)
7466 return txn;
7467
7468 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007469 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007470 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007471 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007472 return NULL;
7473 }
7474
7475 s->txn = txn;
7476 return txn;
7477}
7478
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007479void http_txn_reset_req(struct http_txn *txn)
7480{
7481 txn->req.flags = 0;
7482 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7483 txn->req.next = 0;
7484 txn->req.chunk_len = 0LL;
7485 txn->req.body_len = 0LL;
7486 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7487}
7488
7489void http_txn_reset_res(struct http_txn *txn)
7490{
7491 txn->rsp.flags = 0;
7492 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7493 txn->rsp.next = 0;
7494 txn->rsp.chunk_len = 0LL;
7495 txn->rsp.body_len = 0LL;
7496 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7497}
7498
Willy Tarreau0937bc42009-12-22 15:03:09 +01007499/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007500 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007501 * the required fields are properly allocated and that we only need to (re)init
7502 * them. This should be used before processing any new request.
7503 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007504void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007505{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007506 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007507 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007508
7509 txn->flags = 0;
7510 txn->status = -1;
7511
Willy Tarreauf64d1412010-10-07 20:06:11 +02007512 txn->cookie_first_date = 0;
7513 txn->cookie_last_date = 0;
7514
Willy Tarreaueee5b512015-04-03 23:46:31 +02007515 txn->srv_cookie = NULL;
7516 txn->cli_cookie = NULL;
7517 txn->uri = NULL;
7518
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007519 http_txn_reset_req(txn);
7520 http_txn_reset_res(txn);
7521
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007522 txn->req.chn = &s->req;
7523 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007524
7525 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007526
7527 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7528 if (fe->options2 & PR_O2_REQBUG_OK)
7529 txn->req.err_pos = -1; /* let buggy requests pass */
7530
Willy Tarreau0937bc42009-12-22 15:03:09 +01007531 if (txn->hdr_idx.v)
7532 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007533
7534 vars_init(&s->vars_txn, SCOPE_TXN);
7535 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007536}
7537
7538/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007539void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007540{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007541 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007542 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007543
7544 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007545 pool_free(pool_head_requri, txn->uri);
7546 pool_free(pool_head_capture, txn->cli_cookie);
7547 pool_free(pool_head_capture, txn->srv_cookie);
7548 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007549
William Lallemanda73203e2012-03-12 12:48:57 +01007550 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007551 txn->uri = NULL;
7552 txn->srv_cookie = NULL;
7553 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007554
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007555 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007556 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007557 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007558 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007559 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007560 }
7561
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007562 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007563 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007564 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007565 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007566 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007567 }
7568
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007569 if (!LIST_ISEMPTY(&s->vars_txn.head))
7570 vars_prune(&s->vars_txn, s->sess, s);
7571 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7572 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007573}
7574
7575/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007576void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007577{
7578 http_end_txn(s);
7579 http_init_txn(s);
7580
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007581 /* reinitialise the current rule list pointer to NULL. We are sure that
7582 * any rulelist match the NULL pointer.
7583 */
7584 s->current_rule_list = NULL;
7585
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007586 s->be = strm_fe(s);
7587 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007588 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007589 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007590 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007591 /* re-init store persistence */
7592 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007593 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007594
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007595 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007596
Willy Tarreau739cfba2010-01-25 23:11:14 +01007597 /* We must trim any excess data from the response buffer, because we
7598 * may have blocked an invalid response from a server that we don't
7599 * want to accidentely forward once we disable the analysers, nor do
7600 * we want those data to come along with next response. A typical
7601 * example of such data would be from a buggy server responding to
7602 * a HEAD with some data, or sending more than the advertised
7603 * content-length.
7604 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007605 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007606 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007607
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007608 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007609 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007610
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007611 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007612 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007613
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007614 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007615 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007616
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007617 s->req.rex = TICK_ETERNITY;
7618 s->req.wex = TICK_ETERNITY;
7619 s->req.analyse_exp = TICK_ETERNITY;
7620 s->res.rex = TICK_ETERNITY;
7621 s->res.wex = TICK_ETERNITY;
7622 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007623 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007624}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007625
Willy Tarreau79e57332018-10-02 16:01:16 +02007626/* This function executes one of the set-{method,path,query,uri} actions. It
7627 * takes the string from the variable 'replace' with length 'len', then modifies
7628 * the relevant part of the request line accordingly. Then it updates various
7629 * pointers to the next elements which were moved, and the total buffer length.
7630 * It finds the action to be performed in p[2], previously filled by function
7631 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7632 * error, though this can be revisited when this code is finally exploited.
7633 *
7634 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7635 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007636 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007637 * In query string case, the mark question '?' must be set at the start of the
7638 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007639 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007640int http_replace_req_line(int action, const char *replace, int len,
7641 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007642{
Willy Tarreau79e57332018-10-02 16:01:16 +02007643 struct http_txn *txn = s->txn;
7644 char *cur_ptr, *cur_end;
7645 int offset = 0;
7646 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007647
Willy Tarreau79e57332018-10-02 16:01:16 +02007648 switch (action) {
7649 case 0: // method
7650 cur_ptr = ci_head(&s->req);
7651 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007652
Willy Tarreau79e57332018-10-02 16:01:16 +02007653 /* adjust req line offsets and lengths */
7654 delta = len - offset - (cur_end - cur_ptr);
7655 txn->req.sl.rq.m_l += delta;
7656 txn->req.sl.rq.u += delta;
7657 txn->req.sl.rq.v += delta;
7658 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007659
Willy Tarreau79e57332018-10-02 16:01:16 +02007660 case 1: // path
7661 cur_ptr = http_txn_get_path(txn);
7662 if (!cur_ptr)
7663 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007664
Willy Tarreau79e57332018-10-02 16:01:16 +02007665 cur_end = cur_ptr;
7666 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7667 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007668
Willy Tarreau79e57332018-10-02 16:01:16 +02007669 /* adjust req line offsets and lengths */
7670 delta = len - offset - (cur_end - cur_ptr);
7671 txn->req.sl.rq.u_l += delta;
7672 txn->req.sl.rq.v += delta;
7673 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007674
Willy Tarreau79e57332018-10-02 16:01:16 +02007675 case 2: // query
7676 offset = 1;
7677 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7678 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7679 while (cur_ptr < cur_end && *cur_ptr != '?')
7680 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007681
Willy Tarreau79e57332018-10-02 16:01:16 +02007682 /* skip the question mark or indicate that we must insert it
7683 * (but only if the format string is not empty then).
7684 */
7685 if (cur_ptr < cur_end)
7686 cur_ptr++;
7687 else if (len > 1)
7688 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007689
Willy Tarreau79e57332018-10-02 16:01:16 +02007690 /* adjust req line offsets and lengths */
7691 delta = len - offset - (cur_end - cur_ptr);
7692 txn->req.sl.rq.u_l += delta;
7693 txn->req.sl.rq.v += delta;
7694 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007695
Willy Tarreau79e57332018-10-02 16:01:16 +02007696 case 3: // uri
7697 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7698 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007699
Willy Tarreau79e57332018-10-02 16:01:16 +02007700 /* adjust req line offsets and lengths */
7701 delta = len - offset - (cur_end - cur_ptr);
7702 txn->req.sl.rq.u_l += delta;
7703 txn->req.sl.rq.v += delta;
7704 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007705
Willy Tarreau79e57332018-10-02 16:01:16 +02007706 default:
7707 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007708 }
7709
Willy Tarreau79e57332018-10-02 16:01:16 +02007710 /* commit changes and adjust end of message */
7711 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7712 txn->req.sl.rq.l += delta;
7713 txn->hdr_idx.v[0].len += delta;
7714 http_msg_move_end(&txn->req, delta);
7715 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007716}
7717
Willy Tarreau79e57332018-10-02 16:01:16 +02007718/* This function replace the HTTP status code and the associated message. The
7719 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007720 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007721void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007722{
Willy Tarreau79e57332018-10-02 16:01:16 +02007723 struct http_txn *txn = s->txn;
7724 char *cur_ptr, *cur_end;
7725 int delta;
7726 char *res;
7727 int c_l;
7728 const char *msg = reason;
7729 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007730
Willy Tarreau79e57332018-10-02 16:01:16 +02007731 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007732
Willy Tarreau79e57332018-10-02 16:01:16 +02007733 res = ultoa_o(status, trash.area, trash.size);
7734 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007735
Willy Tarreau79e57332018-10-02 16:01:16 +02007736 trash.area[c_l] = ' ';
7737 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007738
Willy Tarreau79e57332018-10-02 16:01:16 +02007739 /* Do we have a custom reason format string? */
7740 if (msg == NULL)
7741 msg = http_get_reason(status);
7742 msg_len = strlen(msg);
7743 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7744 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007745
Willy Tarreau79e57332018-10-02 16:01:16 +02007746 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7747 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007748
Willy Tarreau79e57332018-10-02 16:01:16 +02007749 /* commit changes and adjust message */
7750 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7751 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007752
Willy Tarreau79e57332018-10-02 16:01:16 +02007753 /* adjust res line offsets and lengths */
7754 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7755 txn->rsp.sl.st.c_l = c_l;
7756 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007757
Willy Tarreau79e57332018-10-02 16:01:16 +02007758 delta = trash.data - (cur_end - cur_ptr);
7759 txn->rsp.sl.st.l += delta;
7760 txn->hdr_idx.v[0].len += delta;
7761 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007762}
7763
Willy Tarreau8797c062007-05-07 00:55:35 +02007764__attribute__((constructor))
7765static void __http_protocol_init(void)
7766{
Willy Tarreau8797c062007-05-07 00:55:35 +02007767}
7768
7769
Willy Tarreau58f10d72006-12-04 02:26:12 +01007770/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007771 * Local variables:
7772 * c-indent-level: 8
7773 * c-basic-offset: 8
7774 * End:
7775 */