blob: 7c658d92b27b37470de6ea6e2ae005fac05befbe [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>
Olivier Houchard985f1392018-11-30 17:31:52 +010073#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020074#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010075#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020076#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020077#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020078#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020079
Cyril Bonté19979e12012-04-04 12:57:21 +020080/* status codes available for the stats admin page (strictly 4 chars length) */
81const char *stat_status_codes[STAT_STATUS_SIZE] = {
82 [STAT_STATUS_DENY] = "DENY",
83 [STAT_STATUS_DONE] = "DONE",
84 [STAT_STATUS_ERRP] = "ERRP",
85 [STAT_STATUS_EXCD] = "EXCD",
86 [STAT_STATUS_NONE] = "NONE",
87 [STAT_STATUS_PART] = "PART",
88 [STAT_STATUS_UNKN] = "UNKN",
89};
90
Willy Tarreau53b6c742006-12-17 13:37:46 +010091/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +010092 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +020093 * the last CRLF. <len> bytes are copied, not counting the CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +010094 * The header is also automatically added to the index <hdr_idx>, and the end
95 * of headers is automatically adjusted. The number of bytes added is returned
96 * on success, otherwise <0 is returned indicating an error.
97 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +010098int http_header_add_tail2(struct http_msg *msg,
99 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100100{
101 int bytes;
102
Willy Tarreau4d893d42018-07-12 15:43:32 +0200103 bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100104 if (!bytes)
105 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100106 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100107 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
108}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200110/* Find the first or next occurrence of header <name> in message buffer <sol>
111 * using headers index <idx>, and return it in the <ctx> structure. This
112 * structure holds everything necessary to use the header and find next
113 * occurrence. If its <idx> member is 0, the header is searched from the
114 * beginning. Otherwise, the next occurrence is returned. The function returns
115 * 1 when it finds a value, and 0 when there is no more. It is very similar to
116 * http_find_header2() except that it is designed to work with full-line headers
117 * whose comma is not a delimiter but is part of the syntax. As a special case,
118 * if ctx->val is NULL when searching for a new values of a header, the current
119 * header is rescanned. This allows rescanning after a header deletion.
120 */
121int http_find_full_header2(const char *name, int len,
122 char *sol, struct hdr_idx *idx,
123 struct hdr_ctx *ctx)
124{
125 char *eol, *sov;
126 int cur_idx, old_idx;
127
128 cur_idx = ctx->idx;
129 if (cur_idx) {
130 /* We have previously returned a header, let's search another one */
131 sol = ctx->line;
132 eol = sol + idx->v[cur_idx].len;
133 goto next_hdr;
134 }
135
136 /* first request for this header */
137 sol += hdr_idx_first_pos(idx);
138 old_idx = 0;
139 cur_idx = hdr_idx_first_idx(idx);
140 while (cur_idx) {
141 eol = sol + idx->v[cur_idx].len;
142
143 if (len == 0) {
144 /* No argument was passed, we want any header.
145 * To achieve this, we simply build a fake request. */
146 while (sol + len < eol && sol[len] != ':')
147 len++;
148 name = sol;
149 }
150
151 if ((len < eol - sol) &&
152 (sol[len] == ':') &&
153 (strncasecmp(sol, name, len) == 0)) {
154 ctx->del = len;
155 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100156 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200157 sov++;
158
159 ctx->line = sol;
160 ctx->prev = old_idx;
161 ctx->idx = cur_idx;
162 ctx->val = sov - sol;
163 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100164 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200165 eol--;
166 ctx->tws++;
167 }
168 ctx->vlen = eol - sov;
169 return 1;
170 }
171 next_hdr:
172 sol = eol + idx->v[cur_idx].cr + 1;
173 old_idx = cur_idx;
174 cur_idx = idx->v[cur_idx].next;
175 }
176 return 0;
177}
178
Willy Tarreauc90dc232015-02-20 13:51:36 +0100179/* Find the first or next header field in message buffer <sol> using headers
180 * index <idx>, and return it in the <ctx> structure. This structure holds
181 * everything necessary to use the header and find next occurrence. If its
182 * <idx> member is 0, the first header is retrieved. Otherwise, the next
183 * occurrence is returned. The function returns 1 when it finds a value, and
184 * 0 when there is no more. It is equivalent to http_find_full_header2() with
185 * no header name.
186 */
187int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
188{
189 char *eol, *sov;
190 int cur_idx, old_idx;
191 int len;
192
193 cur_idx = ctx->idx;
194 if (cur_idx) {
195 /* We have previously returned a header, let's search another one */
196 sol = ctx->line;
197 eol = sol + idx->v[cur_idx].len;
198 goto next_hdr;
199 }
200
201 /* first request for this header */
202 sol += hdr_idx_first_pos(idx);
203 old_idx = 0;
204 cur_idx = hdr_idx_first_idx(idx);
205 while (cur_idx) {
206 eol = sol + idx->v[cur_idx].len;
207
208 len = 0;
209 while (1) {
210 if (len >= eol - sol)
211 goto next_hdr;
212 if (sol[len] == ':')
213 break;
214 len++;
215 }
216
217 ctx->del = len;
218 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100219 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100220 sov++;
221
222 ctx->line = sol;
223 ctx->prev = old_idx;
224 ctx->idx = cur_idx;
225 ctx->val = sov - sol;
226 ctx->tws = 0;
227
Willy Tarreau2235b262016-11-05 15:50:20 +0100228 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100229 eol--;
230 ctx->tws++;
231 }
232 ctx->vlen = eol - sov;
233 return 1;
234
235 next_hdr:
236 sol = eol + idx->v[cur_idx].cr + 1;
237 old_idx = cur_idx;
238 cur_idx = idx->v[cur_idx].next;
239 }
240 return 0;
241}
242
Willy Tarreau33a7e692007-06-10 19:45:56 +0200243/* Find the first or next occurrence of header <name> in message buffer <sol>
244 * using headers index <idx>, and return it in the <ctx> structure. This
245 * structure holds everything necessary to use the header and find next
246 * occurrence. If its <idx> member is 0, the header is searched from the
247 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100248 * 1 when it finds a value, and 0 when there is no more. It is designed to work
249 * with headers defined as comma-separated lists. As a special case, if ctx->val
250 * is NULL when searching for a new values of a header, the current header is
251 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200252 */
253int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100254 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200255 struct hdr_ctx *ctx)
256{
Willy Tarreau68085d82010-01-18 14:54:04 +0100257 char *eol, *sov;
258 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200259
Willy Tarreau68085d82010-01-18 14:54:04 +0100260 cur_idx = ctx->idx;
261 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200262 /* We have previously returned a value, let's search
263 * another one on the same line.
264 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200265 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200266 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100267 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200268 eol = sol + idx->v[cur_idx].len;
269
270 if (sov >= eol)
271 /* no more values in this header */
272 goto next_hdr;
273
Willy Tarreau68085d82010-01-18 14:54:04 +0100274 /* values remaining for this header, skip the comma but save it
275 * for later use (eg: for header deletion).
276 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200277 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100278 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200279 sov++;
280
281 goto return_hdr;
282 }
283
284 /* first request for this header */
285 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100286 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200287 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200288 while (cur_idx) {
289 eol = sol + idx->v[cur_idx].len;
290
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200291 if (len == 0) {
292 /* No argument was passed, we want any header.
293 * To achieve this, we simply build a fake request. */
294 while (sol + len < eol && sol[len] != ':')
295 len++;
296 name = sol;
297 }
298
Willy Tarreau33a7e692007-06-10 19:45:56 +0200299 if ((len < eol - sol) &&
300 (sol[len] == ':') &&
301 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100302 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200303 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100304 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200305 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100306
Willy Tarreau33a7e692007-06-10 19:45:56 +0200307 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100308 ctx->prev = old_idx;
309 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200310 ctx->idx = cur_idx;
311 ctx->val = sov - sol;
312
Willy Tarreauab813a42018-09-10 18:41:28 +0200313 eol = http_find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200314 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100315 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200316 eol--;
317 ctx->tws++;
318 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200319 ctx->vlen = eol - sov;
320 return 1;
321 }
322 next_hdr:
323 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100324 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200325 cur_idx = idx->v[cur_idx].next;
326 }
327 return 0;
328}
329
Willy Tarreau68085d82010-01-18 14:54:04 +0100330/* Remove one value of a header. This only works on a <ctx> returned by one of
331 * the http_find_header functions. The value is removed, as well as surrounding
332 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100333 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100334 * message <msg>. The new index is returned. If it is zero, it means there is
335 * no more header, so any processing may stop. The ctx is always left in a form
336 * that can be handled by http_find_header2() to find next occurrence.
337 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100338int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100339{
340 int cur_idx = ctx->idx;
341 char *sol = ctx->line;
342 struct hdr_idx_elem *hdr;
343 int delta, skip_comma;
344
345 if (!cur_idx)
346 return 0;
347
348 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200349 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100350 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200351 delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100352 http_msg_move_end(msg, delta);
353 idx->used--;
354 hdr->len = 0; /* unused entry */
355 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100356 if (idx->tail == ctx->idx)
357 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100358 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100359 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100360 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200361 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100362 return ctx->idx;
363 }
364
365 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200366 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
367 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100368 */
369
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200370 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200371 delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200372 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100373 NULL, 0);
374 hdr->len += delta;
375 http_msg_move_end(msg, delta);
376 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200377 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100378 return ctx->idx;
379}
380
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100381/* This function handles a server error at the stream interface level. The
382 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100383 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100384 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100385 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200387static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +0200388 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389{
Christopher Faulet9768c262018-10-22 09:34:31 +0200390 if (IS_HTX_STRM(s))
391 return htx_server_error(s, si, err, finst, msg);
392
Willy Tarreau2019f952017-03-14 11:07:31 +0100393 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100394 channel_auto_read(si_oc(si));
395 channel_abort(si_oc(si));
396 channel_auto_close(si_oc(si));
397 channel_erase(si_oc(si));
398 channel_auto_close(si_ic(si));
399 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200400 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200401 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200402 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200403 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200404 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200405 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200406}
407
Willy Tarreau87b09662015-04-03 00:22:06 +0200408/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100409 * and message.
410 */
411
Willy Tarreau83061a82018-07-13 11:56:34 +0200412struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100413{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200414 const int msgnum = http_get_status_idx(s->txn->status);
415
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200416 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200417 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200418 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200419 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100420 else
421 return &http_err_chunks[msgnum];
422}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200423
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100424void
Willy Tarreau83061a82018-07-13 11:56:34 +0200425http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100426{
Christopher Faulet9768c262018-10-22 09:34:31 +0200427 if (IS_HTX_STRM(s))
428 return htx_reply_and_close(s, status, msg);
429
Christopher Fauletd7c91962015-04-30 11:48:27 +0200430 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100431 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100432 stream_int_retnclose(&s->si[0], msg);
433}
434
Willy Tarreau21d2af32008-02-14 20:25:24 +0100435/* Parse the URI from the given transaction (which is assumed to be in request
436 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
437 * It is returned otherwise.
438 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200439char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100440{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200441 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100442
Willy Tarreau9d9ccdb2018-10-28 20:13:12 +0100443 if (!txn->req.chn->buf.size)
444 return NULL;
445
Willy Tarreau6b952c82018-09-10 17:45:34 +0200446 ret = http_get_path(ist2(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l));
William Lallemand65ad6e12014-01-31 15:08:02 +0100447
Willy Tarreau6b952c82018-09-10 17:45:34 +0200448 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100449}
450
Willy Tarreau71241ab2012-12-27 11:30:54 +0100451/* Returns a 302 for a redirectable request that reaches a server working in
452 * in redirect mode. This may only be called just after the stream interface
453 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
454 * follow normal proxy processing. NOTE: this function is designed to support
455 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100456 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200457void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100458{
459 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100460 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100461 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200462 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100463
Christopher Fauletfefc73d2018-10-24 21:18:04 +0200464 if (IS_HTX_STRM(s))
465 return htx_perform_server_redirect(s, si);
466
Willy Tarreauefb453c2008-10-26 20:49:47 +0100467 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200468 trash.data = strlen(HTTP_302);
469 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100470
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200471 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100472
Willy Tarreauefb453c2008-10-26 20:49:47 +0100473 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200474 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100475 return;
476
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100477 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100478 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
480 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100481 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100482
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200483 /* 3: add the request URI. Since it was already forwarded, we need
484 * to temporarily rewind the buffer.
485 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200486 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200487 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200488
Willy Tarreau6b952c82018-09-10 17:45:34 +0200489 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200490 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 +0200491
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200492 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200493
Willy Tarreauefb453c2008-10-26 20:49:47 +0100494 if (!path)
495 return;
496
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200497 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100498 return;
499
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 memcpy(trash.area + trash.data, path, len);
501 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100502
503 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200504 memcpy(trash.area + trash.data,
505 "\r\nProxy-Connection: close\r\n\r\n", 29);
506 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100507 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200508 memcpy(trash.area + trash.data,
509 "\r\nConnection: close\r\n\r\n", 23);
510 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100511 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100512
513 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200514 si_shutr(si);
515 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100516 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100517 si->state = SI_ST_CLO;
518
519 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100520 txn->status = 302;
521 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100522
523 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100524 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500525 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100526}
527
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100528/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100529 * that the server side is closed. Note that err_type is actually a
530 * bitmask, where almost only aborts may be cumulated with other
531 * values. We consider that aborted operations are more important
532 * than timeouts or errors due to the fact that nobody else in the
533 * logs might explain incomplete retries. All others should avoid
534 * being cumulated. It should normally not be possible to have multiple
535 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100536 * Note that connection errors appearing on the second request of a keep-alive
537 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100538 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200539void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100540{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100541 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100542
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200543 /* set s->txn->status for http_error_message(s) */
544 s->txn->status = 503;
545
Willy Tarreauefb453c2008-10-26 20:49:47 +0100546 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200547 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100548 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100549 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200550 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100551 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200552 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100553 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200554 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100555 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100556 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200557 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100558 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100559 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200560 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100561 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200562 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100563 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200564 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100565 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200566 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200567 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200568 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100569 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200570 http_error_message(s));
571 else { /* SI_ET_CONN_OTHER and others */
572 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200573 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100574 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200575 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100576}
577
Willy Tarreau42250582007-04-01 01:30:43 +0200578extern const char sess_term_cond[8];
579extern const char sess_fin_state[8];
580extern const char *monthname[12];
Willy Tarreau8ceae722018-11-26 11:58:30 +0100581
582DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
583DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
584
585struct pool_head *pool_head_requri = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100586struct pool_head *pool_head_capture = NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100587
Willy Tarreau117f59e2007-03-04 18:17:17 +0100588/*
589 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200590 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100591 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200592void http_capture_headers(char *som, struct hdr_idx *idx,
593 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100594{
595 char *eol, *sol, *col, *sov;
596 int cur_idx;
597 struct cap_hdr *h;
598 int len;
599
600 sol = som + hdr_idx_first_pos(idx);
601 cur_idx = hdr_idx_first_idx(idx);
602
603 while (cur_idx) {
604 eol = sol + idx->v[cur_idx].len;
605
606 col = sol;
607 while (col < eol && *col != ':')
608 col++;
609
610 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100611 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100612 sov++;
613
614 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200615 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100616 (strncasecmp(sol, h->name, h->namelen) == 0)) {
617 if (cap[h->index] == NULL)
618 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100619 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100620
621 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100622 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100623 continue;
624 }
625
626 len = eol - sov;
627 if (len > h->len)
628 len = h->len;
629
630 memcpy(cap[h->index], sov, len);
631 cap[h->index][len]=0;
632 }
633 }
634 sol = eol + idx->v[cur_idx].cr + 1;
635 cur_idx = idx->v[cur_idx].next;
636 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100637}
638
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200639/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
640 * conversion succeeded, 0 in case of error. If the request was already 1.X,
641 * nothing is done and 1 is returned.
642 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200643int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200644{
645 int delta;
646 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100647 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200648
649 if (msg->sl.rq.v_l != 0)
650 return 1;
651
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300652 /* RFC 1945 allows only GET for HTTP/0.9 requests */
653 if (txn->meth != HTTP_METH_GET)
654 return 0;
655
Willy Tarreauf37954d2018-06-15 18:31:02 +0200656 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200657
658 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300659 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
660 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200661 }
662 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200663 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100664 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200665 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200666 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200667 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200668 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200669 NULL, NULL);
670 if (unlikely(!cur_end))
671 return 0;
672
673 /* we have a full HTTP/1.0 request now and we know that
674 * we have either a CR or an LF at <ptr>.
675 */
676 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
677 return 1;
678}
679
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100680/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100681 * and "keep-alive" values. If we already know that some headers may safely
682 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100683 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
684 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100685 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100686 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
687 * found, and TX_CON_*_SET is adjusted depending on what is left so only
688 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100689 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100690 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100691void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100692{
Willy Tarreau5b154472009-12-21 20:11:07 +0100693 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100694 const char *hdr_val = "Connection";
695 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100696
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100697 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100698 return;
699
Willy Tarreau88d349d2010-01-25 12:15:43 +0100700 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
701 hdr_val = "Proxy-Connection";
702 hdr_len = 16;
703 }
704
Willy Tarreau5b154472009-12-21 20:11:07 +0100705 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100706 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200707 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100708 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
709 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100710 if (to_del & 2)
711 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100712 else
713 txn->flags |= TX_CON_KAL_SET;
714 }
715 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
716 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100717 if (to_del & 1)
718 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100719 else
720 txn->flags |= TX_CON_CLO_SET;
721 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100722 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
723 txn->flags |= TX_HDR_CONN_UPG;
724 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100725 }
726
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100727 txn->flags |= TX_HDR_CONN_PRS;
728 return;
729}
Willy Tarreau5b154472009-12-21 20:11:07 +0100730
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100731/* Apply desired changes on the Connection: header. Values may be removed and/or
732 * added depending on the <wanted> flags, which are exclusively composed of
733 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
734 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
735 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100736void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100737{
738 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100739 const char *hdr_val = "Connection";
740 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100741
742 ctx.idx = 0;
743
Willy Tarreau88d349d2010-01-25 12:15:43 +0100744
745 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
746 hdr_val = "Proxy-Connection";
747 hdr_len = 16;
748 }
749
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100750 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200751 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100752 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
753 if (wanted & TX_CON_KAL_SET)
754 txn->flags |= TX_CON_KAL_SET;
755 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100756 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100757 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100758 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
759 if (wanted & TX_CON_CLO_SET)
760 txn->flags |= TX_CON_CLO_SET;
761 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100762 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100763 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100764 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100765
766 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
767 return;
768
769 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
770 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100771 hdr_val = "Connection: close";
772 hdr_len = 17;
773 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
774 hdr_val = "Proxy-Connection: close";
775 hdr_len = 23;
776 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100777 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100778 }
779
780 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
781 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100782 hdr_val = "Connection: keep-alive";
783 hdr_len = 22;
784 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
785 hdr_val = "Proxy-Connection: keep-alive";
786 hdr_len = 28;
787 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100788 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100789 }
790 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100791}
792
Willy Tarreau87b09662015-04-03 00:22:06 +0200793void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200794{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200795 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200796 int tmp = TX_CON_WANT_KAL;
797
Christopher Fauletf2824e62018-10-01 12:12:37 +0200798 if (IS_HTX_STRM(s))
Christopher Faulet0f226952018-10-22 09:29:56 +0200799 return htx_adjust_conn_mode(s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200800
Christopher Faulet315b39c2018-09-21 16:26:19 +0200801 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
802 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
803 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200804
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200805 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200806 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200807 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200808
Christopher Faulet315b39c2018-09-21 16:26:19 +0200809 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
810 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200811 tmp = TX_CON_WANT_CLO;
812
813 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
814 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
815
816 if (!(txn->flags & TX_HDR_CONN_PRS) &&
817 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
818 /* parse the Connection header and possibly clean it */
819 int to_del = 0;
820 if ((msg->flags & HTTP_MSGF_VER_11) ||
821 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200822 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200823 to_del |= 2; /* remove "keep-alive" */
824 if (!(msg->flags & HTTP_MSGF_VER_11))
825 to_del |= 1; /* remove "close" */
826 http_parse_connection_header(txn, msg, to_del);
827 }
828
829 /* check if client or config asks for explicit close in KAL/SCL */
830 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
831 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
832 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
833 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
834 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200835 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200836 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
837}
William Lallemand82fe75c2012-10-23 10:25:10 +0200838
Willy Tarreaud787e662009-07-07 10:14:51 +0200839/* This stream analyser waits for a complete HTTP request. It returns 1 if the
840 * processing can continue on next analysers, or zero if it either needs more
841 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100842 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200843 * when it has nothing left to do, and may remove any analyser when it wants to
844 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100845 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200846int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100847{
Willy Tarreau59234e92008-11-30 23:51:27 +0100848 /*
849 * We will parse the partial (or complete) lines.
850 * We will check the request syntax, and also join multi-line
851 * headers. An index of all the lines will be elaborated while
852 * parsing.
853 *
854 * For the parsing, we use a 28 states FSM.
855 *
856 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200857 * ci_head(req) = beginning of request
858 * ci_head(req) + msg->eoh = end of processed headers / start of current one
859 * ci_tail(req) = end of input data
860 * msg->eol = end of current header or line (LF or CRLF)
861 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200862 *
863 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200864 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200865 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
866 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100867 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100868
Willy Tarreau59234e92008-11-30 23:51:27 +0100869 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200870 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200871 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100872 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200873 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100874
Christopher Faulete0768eb2018-10-03 16:38:02 +0200875 if (IS_HTX_STRM(s))
876 return htx_wait_for_request(s, req, an_bit);
877
Christopher Faulet45073512018-07-20 10:16:29 +0200878 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 +0100879 now_ms, __FUNCTION__,
880 s,
881 req,
882 req->rex, req->wex,
883 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200884 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100885 req->analysers);
886
Willy Tarreau52a0c602009-08-16 22:45:38 +0200887 /* we're speaking HTTP here, so let's speak HTTP to the client */
888 s->srv_error = http_return_srv_error;
889
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300890 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200891 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300892 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
893
Willy Tarreau83e3af02009-12-28 17:39:57 +0100894 /* There's a protected area at the end of the buffer for rewriting
895 * purposes. We don't want to start to parse the request if the
896 * protected area is affected, because we may have to move processed
897 * data later, which is much more complicated.
898 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200899 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200900 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100901 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200902 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100903 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100904 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200905 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200906 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100907 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100908 return 0;
909 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200910 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200911 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200912 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100913 }
914
Willy Tarreauf37954d2018-06-15 18:31:02 +0200915 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100916 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100917 }
918
Willy Tarreau59234e92008-11-30 23:51:27 +0100919 /* 1: we might have to print this header in debug mode */
920 if (unlikely((global.mode & MODE_DEBUG) &&
921 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200922 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100923 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100924
Willy Tarreauf37954d2018-06-15 18:31:02 +0200925 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200926 /* this is a bit complex : in case of error on the request line,
927 * we know that rq.l is still zero, so we display only the part
928 * up to the end of the line (truncated by debug_hdr).
929 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200930 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100931 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100932
Willy Tarreau59234e92008-11-30 23:51:27 +0100933 sol += hdr_idx_first_pos(&txn->hdr_idx);
934 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100935
Willy Tarreau59234e92008-11-30 23:51:27 +0100936 while (cur_idx) {
937 eol = sol + txn->hdr_idx.v[cur_idx].len;
938 debug_hdr("clihdr", s, sol, eol);
939 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
940 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100941 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100942 }
943
Willy Tarreau58f10d72006-12-04 02:26:12 +0100944
Willy Tarreau59234e92008-11-30 23:51:27 +0100945 /*
946 * Now we quickly check if we have found a full valid request.
947 * If not so, we check the FD and buffer states before leaving.
948 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100949 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100950 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200951 * on a keep-alive stream, if we encounter and error, close, t/o,
952 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100953 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200954 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200955 * Last, we may increase some tracked counters' http request errors on
956 * the cases that are deliberately the client's fault. For instance,
957 * a timeout or connection reset is not counted as an error. However
958 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100959 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100960
Willy Tarreau655dce92009-11-08 13:10:58 +0100961 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100962 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100963 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100964 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100965 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200966 stream_inc_http_req_ctr(s);
967 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200968 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100969 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100970 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100971
Willy Tarreau59234e92008-11-30 23:51:27 +0100972 /* 1: Since we are in header mode, if there's no space
973 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200974 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100975 * must terminate it now.
976 */
Willy Tarreau23752332018-06-15 14:54:53 +0200977 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100978 /* FIXME: check if URI is set and return Status
979 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100980 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200981 stream_inc_http_req_ctr(s);
982 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200983 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200984 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200985 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100986 goto return_bad_req;
987 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100988
Willy Tarreau59234e92008-11-30 23:51:27 +0100989 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200990 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200991 if (!(s->flags & SF_ERR_MASK))
992 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100993
Willy Tarreaufcffa692010-01-10 14:21:19 +0100994 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100995 goto failed_keep_alive;
996
Willy Tarreau0f228a02015-05-01 15:37:53 +0200997 if (sess->fe->options & PR_O_IGNORE_PRB)
998 goto failed_keep_alive;
999
Willy Tarreau59234e92008-11-30 23:51:27 +01001000 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001001 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001002 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001003 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001004 }
1005
Willy Tarreaudc979f22012-12-04 10:39:01 +01001006 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001007 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001008 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001009 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001010 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001011 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001012 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001013 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001014 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001015 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001016
Willy Tarreaue7dff022015-04-03 01:14:29 +02001017 if (!(s->flags & SF_FINST_MASK))
1018 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001019 return 0;
1020 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001021
Willy Tarreau59234e92008-11-30 23:51:27 +01001022 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001023 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001024 if (!(s->flags & SF_ERR_MASK))
1025 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001026
Willy Tarreaufcffa692010-01-10 14:21:19 +01001027 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001028 goto failed_keep_alive;
1029
Willy Tarreau0f228a02015-05-01 15:37:53 +02001030 if (sess->fe->options & PR_O_IGNORE_PRB)
1031 goto failed_keep_alive;
1032
Willy Tarreau59234e92008-11-30 23:51:27 +01001033 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001034 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001035 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001036 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001037 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001038 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001039 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001040 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001041 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001042 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001043
Willy Tarreau87b09662015-04-03 00:22:06 +02001044 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001045 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001046 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001047 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001048 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001049
Willy Tarreaue7dff022015-04-03 01:14:29 +02001050 if (!(s->flags & SF_FINST_MASK))
1051 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001052 return 0;
1053 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001054
Willy Tarreau59234e92008-11-30 23:51:27 +01001055 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001056 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001057 if (!(s->flags & SF_ERR_MASK))
1058 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001059
Willy Tarreaufcffa692010-01-10 14:21:19 +01001060 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001061 goto failed_keep_alive;
1062
Willy Tarreau0f228a02015-05-01 15:37:53 +02001063 if (sess->fe->options & PR_O_IGNORE_PRB)
1064 goto failed_keep_alive;
1065
Willy Tarreau4076a152009-04-02 15:18:36 +02001066 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001067 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001068 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001069 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001070 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001071 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001072 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001073 stream_inc_http_err_ctr(s);
1074 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001075 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001076 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001077 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001078 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001079
Willy Tarreaue7dff022015-04-03 01:14:29 +02001080 if (!(s->flags & SF_FINST_MASK))
1081 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001082 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001083 }
1084
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001085 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001086 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001087 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001088#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001089 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001090 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001091 /* We need more data, we have to re-enable quick-ack in case we
1092 * previously disabled it, otherwise we might cause the client
1093 * to delay next data.
1094 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001095 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001096 }
1097#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001098
Willy Tarreaufcffa692010-01-10 14:21:19 +01001099 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1100 /* If the client starts to talk, let's fall back to
1101 * request timeout processing.
1102 */
1103 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001104 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001105 }
1106
Willy Tarreau59234e92008-11-30 23:51:27 +01001107 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001108 if (!tick_isset(req->analyse_exp)) {
1109 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1110 (txn->flags & TX_WAIT_NEXT_RQ) &&
1111 tick_isset(s->be->timeout.httpka))
1112 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1113 else
1114 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1115 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001116
Willy Tarreau59234e92008-11-30 23:51:27 +01001117 /* we're not ready yet */
1118 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001119
1120 failed_keep_alive:
1121 /* Here we process low-level errors for keep-alive requests. In
1122 * short, if the request is not the first one and it experiences
1123 * a timeout, read error or shutdown, we just silently close so
1124 * that the client can try again.
1125 */
1126 txn->status = 0;
1127 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001128 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001129 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001130 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001131 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001132 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001133 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001134 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001135
Willy Tarreaud787e662009-07-07 10:14:51 +02001136 /* OK now we have a complete HTTP request with indexed headers. Let's
1137 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001138 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001139 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001140 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001141 * byte after the last LF. msg->sov points to the first byte of data.
1142 * msg->eol cannot be trusted because it may have been left uninitialized
1143 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001144 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001145
Willy Tarreau87b09662015-04-03 00:22:06 +02001146 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001147 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001148
Willy Tarreaub16a5742010-01-10 14:46:16 +01001149 if (txn->flags & TX_WAIT_NEXT_RQ) {
1150 /* kill the pending keep-alive timeout */
1151 txn->flags &= ~TX_WAIT_NEXT_RQ;
1152 req->analyse_exp = TICK_ETERNITY;
1153 }
1154
1155
Willy Tarreaud787e662009-07-07 10:14:51 +02001156 /* Maybe we found in invalid header name while we were configured not
1157 * to block on that, so we have to capture it now.
1158 */
1159 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001160 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001161
Willy Tarreau59234e92008-11-30 23:51:27 +01001162 /*
1163 * 1: identify the method
1164 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001165 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001166
1167 /* we can make use of server redirect on GET and HEAD */
1168 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001169 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001170 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001171 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001172 /* PRI is reserved for the HTTP/2 preface */
1173 msg->err_pos = 0;
1174 goto return_bad_req;
1175 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001176
Willy Tarreau59234e92008-11-30 23:51:27 +01001177 /*
1178 * 2: check if the URI matches the monitor_uri.
1179 * We have to do this for every request which gets in, because
1180 * the monitor-uri is defined by the frontend.
1181 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001182 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1183 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001184 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001185 sess->fe->monitor_uri,
1186 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001187 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001188 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001189 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001190 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001191
Willy Tarreaue7dff022015-04-03 01:14:29 +02001192 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001193 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001194
Willy Tarreau59234e92008-11-30 23:51:27 +01001195 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001196 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001197 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001198
Willy Tarreau59234e92008-11-30 23:51:27 +01001199 ret = acl_pass(ret);
1200 if (cond->pol == ACL_COND_UNLESS)
1201 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001202
Willy Tarreau59234e92008-11-30 23:51:27 +01001203 if (ret) {
1204 /* we fail this request, let's return 503 service unavail */
1205 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001206 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001207 if (!(s->flags & SF_ERR_MASK))
1208 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001209 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001210 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001211 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001212
Joseph Herlant5ba80252018-11-15 09:25:36 -08001213 /* nothing to fail, let's reply normally */
Willy Tarreau59234e92008-11-30 23:51:27 +01001214 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001215 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001216 if (!(s->flags & SF_ERR_MASK))
1217 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001218 goto return_prx_cond;
1219 }
1220
1221 /*
1222 * 3: Maybe we have to copy the original REQURI for the logs ?
1223 * Note: we cannot log anymore if the request has been
1224 * classified as invalid.
1225 */
1226 if (unlikely(s->logs.logwait & LW_REQ)) {
1227 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001228 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001229 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001230
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001231 if (urilen >= global.tune.requri_len )
1232 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001233 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001234 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001235
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001236 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001237 s->do_log(s);
1238 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001239 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001240 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001241 }
Willy Tarreau06619262006-12-17 08:37:22 +01001242
Willy Tarreau91852eb2015-05-01 13:26:00 +02001243 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1244 * exactly one digit "." one digit. This check may be disabled using
1245 * option accept-invalid-http-request.
1246 */
1247 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1248 if (msg->sl.rq.v_l != 8) {
1249 msg->err_pos = msg->sl.rq.v;
1250 goto return_bad_req;
1251 }
1252
Willy Tarreauf37954d2018-06-15 18:31:02 +02001253 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1254 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1255 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1256 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001257 msg->err_pos = msg->sl.rq.v + 4;
1258 goto return_bad_req;
1259 }
1260 }
Willy Tarreau13317662015-05-01 13:47:08 +02001261 else {
1262 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1263 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1264 goto return_bad_req;
1265 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001266
Willy Tarreau5b154472009-12-21 20:11:07 +01001267 /* ... and check if the request is HTTP/1.1 or above */
1268 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001269 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1270 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1271 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001272 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001273
1274 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001275 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 +01001276
Willy Tarreau88d349d2010-01-25 12:15:43 +01001277 /* if the frontend has "option http-use-proxy-header", we'll check if
1278 * we have what looks like a proxied connection instead of a connection,
1279 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1280 * Note that this is *not* RFC-compliant, however browsers and proxies
1281 * happen to do that despite being non-standard :-(
1282 * We consider that a request not beginning with either '/' or '*' is
1283 * a proxied connection, which covers both "scheme://location" and
1284 * CONNECT ip:port.
1285 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001286 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001287 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001288 txn->flags |= TX_USE_PX_CONN;
1289
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001290 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001291 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001292
Willy Tarreau59234e92008-11-30 23:51:27 +01001293 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001294 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001295 http_capture_headers(ci_head(req), &txn->hdr_idx,
1296 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001297
Willy Tarreau557f1992015-05-01 10:05:17 +02001298 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1299 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001300 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001301 * The length of a message body is determined by one of the following
1302 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001303 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001304 * 1. Any response to a HEAD request and any response with a 1xx
1305 * (Informational), 204 (No Content), or 304 (Not Modified) status
1306 * code is always terminated by the first empty line after the
1307 * header fields, regardless of the header fields present in the
1308 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001309 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001310 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1311 * the connection will become a tunnel immediately after the empty
1312 * line that concludes the header fields. A client MUST ignore any
1313 * Content-Length or Transfer-Encoding header fields received in
1314 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001315 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001316 * 3. If a Transfer-Encoding header field is present and the chunked
1317 * transfer coding (Section 4.1) is the final encoding, the message
1318 * body length is determined by reading and decoding the chunked
1319 * data until the transfer coding indicates the data is complete.
1320 *
1321 * If a Transfer-Encoding header field is present in a response and
1322 * the chunked transfer coding is not the final encoding, the
1323 * message body length is determined by reading the connection until
1324 * it is closed by the server. If a Transfer-Encoding header field
1325 * is present in a request and the chunked transfer coding is not
1326 * the final encoding, the message body length cannot be determined
1327 * reliably; the server MUST respond with the 400 (Bad Request)
1328 * status code and then close the connection.
1329 *
1330 * If a message is received with both a Transfer-Encoding and a
1331 * Content-Length header field, the Transfer-Encoding overrides the
1332 * Content-Length. Such a message might indicate an attempt to
1333 * perform request smuggling (Section 9.5) or response splitting
1334 * (Section 9.4) and ought to be handled as an error. A sender MUST
1335 * remove the received Content-Length field prior to forwarding such
1336 * a message downstream.
1337 *
1338 * 4. If a message is received without Transfer-Encoding and with
1339 * either multiple Content-Length header fields having differing
1340 * field-values or a single Content-Length header field having an
1341 * invalid value, then the message framing is invalid and the
1342 * recipient MUST treat it as an unrecoverable error. If this is a
1343 * request message, the server MUST respond with a 400 (Bad Request)
1344 * status code and then close the connection. If this is a response
1345 * message received by a proxy, the proxy MUST close the connection
1346 * to the server, discard the received response, and send a 502 (Bad
1347 * Gateway) response to the client. If this is a response message
1348 * received by a user agent, the user agent MUST close the
1349 * connection to the server and discard the received response.
1350 *
1351 * 5. If a valid Content-Length header field is present without
1352 * Transfer-Encoding, its decimal value defines the expected message
1353 * body length in octets. If the sender closes the connection or
1354 * the recipient times out before the indicated number of octets are
1355 * received, the recipient MUST consider the message to be
1356 * incomplete and close the connection.
1357 *
1358 * 6. If this is a request message and none of the above are true, then
1359 * the message body length is zero (no message body is present).
1360 *
1361 * 7. Otherwise, this is a response message without a declared message
1362 * body length, so the message body length is determined by the
1363 * number of octets received prior to the server closing the
1364 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001365 */
1366
Willy Tarreau32b47f42009-10-18 20:55:02 +02001367 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001368 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001369 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001370 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001371 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001372 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001373 /* chunked not last, return badreq */
1374 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001375 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001376 }
1377
Willy Tarreau1c913912015-04-30 10:57:51 +02001378 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001379 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001380 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001381 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001382 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1383 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001384 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001385 signed long long cl;
1386
Willy Tarreauad14f752011-09-02 20:33:27 +02001387 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001388 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001389 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001390 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001391
Willy Tarreauad14f752011-09-02 20:33:27 +02001392 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001393 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001394 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001395 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001396
Willy Tarreauad14f752011-09-02 20:33:27 +02001397 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001398 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001399 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001400 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001401
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001402 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001403 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001404 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001405 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001406
Christopher Fauletbe821b92017-03-30 11:21:53 +02001407 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001408 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001409 }
1410
Willy Tarreau34dfc602015-05-01 10:09:49 +02001411 /* even bodyless requests have a known length */
1412 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001413
Willy Tarreau179085c2014-04-28 16:48:56 +02001414 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1415 * only change if both the request and the config reference something else.
1416 * Option httpclose by itself sets tunnel mode where headers are mangled.
1417 * However, if another mode is set, it will affect it (eg: server-close/
1418 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1419 * if FE and BE have the same settings (common). The method consists in
1420 * checking if options changed between the two calls (implying that either
1421 * one is non-null, or one of them is non-null and we are there for the first
1422 * time.
1423 */
1424 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001425 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001426 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001427
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001428 /* we may have to wait for the request's body */
1429 if ((s->be->options & PR_O_WREQ_BODY) &&
1430 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1431 req->analysers |= AN_REQ_HTTP_BODY;
1432
Willy Tarreau83ece462017-12-21 15:13:09 +01001433 /*
1434 * RFC7234#4:
1435 * A cache MUST write through requests with methods
1436 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1437 * the origin server; i.e., a cache is not allowed
1438 * to generate a reply to such a request before
1439 * having forwarded the request and having received
1440 * a corresponding response.
1441 *
1442 * RFC7231#4.2.1:
1443 * Of the request methods defined by this
1444 * specification, the GET, HEAD, OPTIONS, and TRACE
1445 * methods are defined to be safe.
1446 */
1447 if (likely(txn->meth == HTTP_METH_GET ||
1448 txn->meth == HTTP_METH_HEAD ||
1449 txn->meth == HTTP_METH_OPTIONS ||
1450 txn->meth == HTTP_METH_TRACE))
1451 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1452
Willy Tarreaud787e662009-07-07 10:14:51 +02001453 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001454 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001455 req->analyse_exp = TICK_ETERNITY;
1456 return 1;
1457
1458 return_bad_req:
1459 /* We centralize bad requests processing here */
1460 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1461 /* we detected a parsing error. We want to archive this request
1462 * in the dedicated proxy area for later troubleshooting.
1463 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001464 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001465 }
1466
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001467 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001468 txn->req.msg_state = HTTP_MSG_ERROR;
1469 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001470 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001471
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001472 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001473 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001474 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001475
1476 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001477 if (!(s->flags & SF_ERR_MASK))
1478 s->flags |= SF_ERR_PRXCOND;
1479 if (!(s->flags & SF_FINST_MASK))
1480 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001481
Christopher Faulet0184ea72017-01-05 14:06:34 +01001482 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001483 req->analyse_exp = TICK_ETERNITY;
1484 return 0;
1485}
1486
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001487
Willy Tarreau347a35d2013-11-22 17:51:09 +01001488/* This function prepares an applet to handle the stats. It can deal with the
1489 * "100-continue" expectation, check that admin rules are met for POST requests,
1490 * and program a response message if something was unexpected. It cannot fail
1491 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001492 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001493 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001494 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001495 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001496int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001497{
1498 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001499 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001500 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001501 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001502 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001503 struct uri_auth *uri_auth = s->be->uri_auth;
1504 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001505 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001506
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001507 appctx = si_appctx(si);
1508 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1509 appctx->st1 = appctx->st2 = 0;
1510 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1511 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001512 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001513 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001514
Willy Tarreauf37954d2018-06-15 18:31:02 +02001515 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001516 lookup = uri + uri_auth->uri_len;
1517
1518 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1519 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001520 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001521 break;
1522 }
1523 }
1524
1525 if (uri_auth->refresh) {
1526 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1527 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001528 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001529 break;
1530 }
1531 }
1532 }
1533
1534 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1535 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001536 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001537 break;
1538 }
1539 }
1540
Willy Tarreau1e62df92016-01-11 18:57:53 +01001541 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1542 if (memcmp(h, ";typed", 6) == 0) {
1543 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1544 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1545 break;
1546 }
1547 }
1548
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001549 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1550 if (memcmp(h, ";st=", 4) == 0) {
1551 int i;
1552 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001553 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001554 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1555 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001556 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001557 break;
1558 }
1559 }
1560 break;
1561 }
1562 }
1563
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001564 appctx->ctx.stats.scope_str = 0;
1565 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001566 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1567 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1568 int itx = 0;
1569 const char *h2;
1570 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1571 const char *err;
1572
1573 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1574 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001575 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001576 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1577 itx++;
1578 h++;
1579 }
1580
1581 if (itx > STAT_SCOPE_TXT_MAXLEN)
1582 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001583 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001584
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001585 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001586 memcpy(scope_txt, h2, itx);
1587 scope_txt[itx] = '\0';
1588 err = invalid_char(scope_txt);
1589 if (err) {
1590 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001591 appctx->ctx.stats.scope_str = 0;
1592 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001593 }
1594 break;
1595 }
1596 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001597
1598 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001599 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001600 int ret = 1;
1601
1602 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001603 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 +01001604 ret = acl_pass(ret);
1605 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1606 ret = !ret;
1607 }
1608
1609 if (ret) {
1610 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001611 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001612 break;
1613 }
1614 }
1615
1616 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001617 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001618 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001619 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001620 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1621 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001622 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001623 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001624 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001625 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1626 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001627 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001628 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001629 else {
1630 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001631 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001632 }
1633
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001634 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001635 return 1;
1636}
1637
Lukas Tribus67db8df2013-06-23 17:37:13 +02001638/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1639 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1640 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001641void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001642{
1643#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001644 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001645 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1646#endif
1647#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001648 if (from->ss_family == AF_INET6) {
1649 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001650 /* v4-mapped addresses need IP_TOS */
1651 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1652 else
1653 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1654 }
1655#endif
1656}
1657
Willy Tarreau87b09662015-04-03 00:22:06 +02001658int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001659 const char* name, unsigned int name_len,
1660 const char *str, struct my_regex *re,
1661 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001662{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001663 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001664 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001665 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001666 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1667 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001668 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001669
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001670 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001671
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001672 /* Choose the header browsing function. */
1673 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001674 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001675 http_find_hdr_func = http_find_header2;
1676 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001677 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001678 http_find_hdr_func = http_find_full_header2;
1679 break;
1680 default: /* impossible */
1681 return -1;
1682 }
1683
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001684 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1685 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001686 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001687 char *val = ctx.line + ctx.val;
1688 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001689
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001690 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1691 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001692
Willy Tarreau6e27be12018-08-22 04:46:47 +02001693 len = exp_replace(output->area, output->size, val, str, pmatch);
1694 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001695 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001696
Willy Tarreau6e27be12018-08-22 04:46:47 +02001697 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001698
1699 hdr->len += delta;
1700 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001701
1702 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001703 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001704 }
1705
1706 return 0;
1707}
1708
Willy Tarreau87b09662015-04-03 00:22:06 +02001709static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001710 const char* name, unsigned int name_len,
1711 struct list *fmt, struct my_regex *re,
1712 int action)
1713{
Willy Tarreau83061a82018-07-13 11:56:34 +02001714 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001715 int ret = -1;
1716
1717 replace = alloc_trash_chunk();
1718 if (!replace)
1719 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001720
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001721 replace->data = build_logline(s, replace->area, replace->size, fmt);
1722 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001723 goto leave;
1724
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001725 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1726 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001727
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001728 leave:
1729 free_trash_chunk(replace);
1730 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001731}
1732
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001733/*
1734 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1735 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001736 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1737 * line is inserted before the header. If an error occurred <early_hints> is
1738 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001739 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001740static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1741 const char* name, unsigned int name_len,
1742 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001743{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001744 if (!early_hints) {
1745 early_hints = alloc_trash_chunk();
1746 if (!early_hints)
1747 goto fail;
1748 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1749 goto fail;
1750 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001751
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001752 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1753 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001754
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001755 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1756 if (!chunk_memcat(early_hints, "\r\n", 2))
1757 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001758
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001759 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001760
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001761 fail:
1762 free_trash_chunk(early_hints);
1763 return NULL;
1764}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001765
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001766/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1767 * response is added. If an error occurred or if another response was already
1768 * sent, this function does nothing.
1769 */
1770static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1771{
1772 struct channel *chn = s->txn->rsp.chn;
1773 char *cur_ptr = ci_head(chn);
1774 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001775
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001776 /* If a response was already sent, skip early hints */
1777 if (s->txn->status > 0)
1778 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001779
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001780 if (!chunk_memcat(early_hints, "\r\n", 2))
1781 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001782
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001783 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1784 c_adv(chn, ret);
1785 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001786}
1787
Willy Tarreau87b09662015-04-03 00:22:06 +02001788/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001789 * transaction <txn>. Returns the verdict of the first rule that prevents
1790 * further processing of the request (auth, deny, ...), and defaults to
1791 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1792 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001793 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1794 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1795 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001796 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001797enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001798http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001799{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001800 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001801 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001802 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001803 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001804 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001805 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001806 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001807 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001808 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001809 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001810
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001811 /* If "the current_rule_list" match the executed rule list, we are in
1812 * resume condition. If a resume is needed it is always in the action
1813 * and never in the ACL or converters. In this case, we initialise the
1814 * current rule, and go to the action execution point.
1815 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001816 if (s->current_rule) {
1817 rule = s->current_rule;
1818 s->current_rule = NULL;
1819 if (s->current_rule_list == rules)
1820 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001821 }
1822 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001823
Willy Tarreauff011f22011-01-06 17:51:27 +01001824 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001825
Willy Tarreau96257ec2012-12-27 10:46:37 +01001826 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001827 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001828 int ret;
1829
Willy Tarreau192252e2015-04-04 01:47:55 +02001830 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001831 ret = acl_pass(ret);
1832
Willy Tarreauff011f22011-01-06 17:51:27 +01001833 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001834 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001835
1836 if (!ret) /* condition not matched */
1837 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001838 }
1839
Willy Tarreauacc98002015-09-27 23:34:39 +02001840 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001841resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001842 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001843 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001844 rule_ret = HTTP_RULE_RES_STOP;
1845 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001846
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001847 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001848 if (deny_status)
1849 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001850 rule_ret = HTTP_RULE_RES_DENY;
1851 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001852
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001853 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001854 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001855 if (deny_status)
1856 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001857 rule_ret = HTTP_RULE_RES_DENY;
1858 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001859
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001860 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001861 /* Be sure to send any pending HTTP 103 response first */
1862 if (early_hints) {
1863 http_send_early_hints(s, early_hints);
1864 free_trash_chunk(early_hints);
1865 early_hints = NULL;
1866 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001867 /* Auth might be performed on regular http-req rules as well as on stats */
1868 auth_realm = rule->arg.auth.realm;
1869 if (!auth_realm) {
1870 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1871 auth_realm = STATS_DEFAULT_REALM;
1872 else
1873 auth_realm = px->id;
1874 }
1875 /* send 401/407 depending on whether we use a proxy or not. We still
1876 * count one error, because normal browsing won't significantly
1877 * increase the counter but brute force attempts will.
1878 */
1879 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1880 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001881 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001882 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001883 rule_ret = HTTP_RULE_RES_ABRT;
1884 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001885
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001886 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001887 /* Be sure to send any pending HTTP 103 response first */
1888 if (early_hints) {
1889 http_send_early_hints(s, early_hints);
1890 free_trash_chunk(early_hints);
1891 early_hints = NULL;
1892 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001893 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001894 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001895 rule_ret = HTTP_RULE_RES_BADREQ;
1896 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001897
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001898 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001899 s->task->nice = rule->arg.nice;
1900 break;
1901
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001902 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001903 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001904 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001905 break;
1906
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001907 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001908#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001909 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001910 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001911#endif
1912 break;
1913
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001914 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001915 s->logs.level = rule->arg.loglevel;
1916 break;
1917
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001918 case ACT_HTTP_REPLACE_HDR:
1919 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001920 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1921 rule->arg.hdr_add.name_len,
1922 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001923 &rule->arg.hdr_add.re, rule->action)) {
1924 rule_ret = HTTP_RULE_RES_BADREQ;
1925 goto end;
1926 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001927 break;
1928
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001929 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001930 ctx.idx = 0;
1931 /* remove all occurrences of the header */
1932 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001933 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001934 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001935 }
Willy Tarreau85603282015-01-21 20:39:27 +01001936 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001937
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001938 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001939 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001940 /* The scope of the trash buffer must be limited to this function. The
1941 * build_logline() function can execute a lot of other function which
1942 * can use the trash buffer. So for limiting the scope of this global
1943 * buffer, we build first the header value using build_logline, and
1944 * after we store the header name.
1945 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001946 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001947
1948 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001949 if (!replace) {
1950 rule_ret = HTTP_RULE_RES_BADREQ;
1951 goto end;
1952 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001953
Thierry Fournier4b788f72016-06-01 13:35:36 +02001954 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001955 len += build_logline(s, replace->area + len,
1956 replace->size - len,
1957 &rule->arg.hdr_add.fmt);
1958 memcpy(replace->area, rule->arg.hdr_add.name,
1959 rule->arg.hdr_add.name_len);
1960 replace->area[rule->arg.hdr_add.name_len] = ':';
1961 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1962 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001963
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001964 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001965 /* remove all occurrences of the header */
1966 ctx.idx = 0;
1967 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001968 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001969 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1970 }
1971 }
1972
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001973 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001974 static unsigned char rate_limit = 0;
1975
1976 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001977 replace->area[rule->arg.hdr_add.name_len] = 0;
1978 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,
1979 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001980 }
1981
1982 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1983 if (sess->fe != s->be)
1984 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1985 if (sess->listener->counters)
1986 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1987 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001988
1989 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001990 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001991 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001992
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001993 case ACT_HTTP_DEL_ACL:
1994 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001995 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001996 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001997
1998 /* collect reference */
1999 ref = pat_ref_lookup(rule->arg.map.ref);
2000 if (!ref)
2001 continue;
2002
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002003 /* allocate key */
2004 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002005 if (!key) {
2006 rule_ret = HTTP_RULE_RES_BADREQ;
2007 goto end;
2008 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002009
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002010 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002011 key->data = build_logline(s, key->area, key->size,
2012 &rule->arg.map.key);
2013 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002014
2015 /* perform update */
2016 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002017 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002018 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002019 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002020
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002021 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002022 break;
2023 }
2024
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002025 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002026 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002027 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002028
2029 /* collect reference */
2030 ref = pat_ref_lookup(rule->arg.map.ref);
2031 if (!ref)
2032 continue;
2033
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002034 /* allocate key */
2035 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002036 if (!key) {
2037 rule_ret = HTTP_RULE_RES_BADREQ;
2038 goto end;
2039 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002040
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002041 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002042 key->data = build_logline(s, key->area, key->size,
2043 &rule->arg.map.key);
2044 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002045
2046 /* perform update */
2047 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002048 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002049 if (pat_ref_find_elt(ref, key->area) == NULL)
2050 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002051 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002052
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002053 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002054 break;
2055 }
2056
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002057 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002058 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002059 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002060
2061 /* collect reference */
2062 ref = pat_ref_lookup(rule->arg.map.ref);
2063 if (!ref)
2064 continue;
2065
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002066 /* allocate key */
2067 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002068 if (!key) {
2069 rule_ret = HTTP_RULE_RES_BADREQ;
2070 goto end;
2071 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002072
2073 /* allocate value */
2074 value = alloc_trash_chunk();
2075 if (!value) {
2076 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01002077 rule_ret = HTTP_RULE_RES_BADREQ;
2078 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002079 }
2080
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002081 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002082 key->data = build_logline(s, key->area, key->size,
2083 &rule->arg.map.key);
2084 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002085
2086 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002087 value->data = build_logline(s, value->area,
2088 value->size,
2089 &rule->arg.map.value);
2090 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002091
2092 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002093 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002094 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002095 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002096 else
2097 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002098 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002099
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002100 free_trash_chunk(key);
2101 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002102 break;
2103 }
William Lallemand73025dd2014-04-24 14:38:37 +02002104
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002105 case ACT_HTTP_EARLY_HINT:
2106 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2107 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002108 early_hints = http_apply_early_hint_rule(s, early_hints,
2109 rule->arg.early_hint.name,
2110 rule->arg.early_hint.name_len,
2111 &rule->arg.early_hint.fmt);
2112 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01002113 rule_ret = HTTP_RULE_RES_DONE;
2114 goto end;
2115 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002116 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002117 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002118 if ((s->req.flags & CF_READ_ERROR) ||
2119 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2120 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2121 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002122 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002123
Willy Tarreauacc98002015-09-27 23:34:39 +02002124 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002125 case ACT_RET_ERR:
2126 case ACT_RET_CONT:
2127 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002128 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01002129 rule_ret = HTTP_RULE_RES_DONE;
2130 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002131 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002132 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01002133 rule_ret = HTTP_RULE_RES_YIELD;
2134 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002135 }
William Lallemand73025dd2014-04-24 14:38:37 +02002136 break;
2137
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002138 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002139 /* Note: only the first valid tracking parameter of each
2140 * applies.
2141 */
2142
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002143 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002144 struct stktable *t;
2145 struct stksess *ts;
2146 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002147 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002148
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002149 t = rule->arg.trk_ctr.table.t;
2150 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 +02002151
2152 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002153 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002154
2155 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002156 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2157 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2158 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002159 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002160
Emeric Brun819fc6f2017-06-13 19:37:32 +02002161 if (ptr1)
2162 stktable_data_cast(ptr1, http_req_cnt)++;
2163
2164 if (ptr2)
2165 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2166 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2167
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002168 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002169
2170 /* If data was modified, we need to touch to re-schedule sync */
2171 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002172 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002173
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002174 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002175 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002176 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002177 }
2178 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002179 break;
2180
Joseph Herlant5ba80252018-11-15 09:25:36 -08002181 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002182 default:
2183 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002184 }
2185 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002186
Christopher Fauletea827bd2018-11-15 15:34:11 +01002187 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002188 if (early_hints) {
2189 http_send_early_hints(s, early_hints);
2190 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002191 }
2192
Willy Tarreau96257ec2012-12-27 10:46:37 +01002193 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01002194 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002195}
2196
Willy Tarreau71241ab2012-12-27 11:30:54 +01002197
Willy Tarreau51d861a2015-05-22 17:30:48 +02002198/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2199 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2200 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2201 * is returned, the process can continue the evaluation of next rule list. If
2202 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2203 * is returned, it means the operation could not be processed and a server error
2204 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2205 * deny rule. If *YIELD is returned, the caller must call again the function
2206 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002207 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002208enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002209http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002210{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002211 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002212 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002213 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002214 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002215 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002216 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02002217 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002218
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002219 /* If "the current_rule_list" match the executed rule list, we are in
2220 * resume condition. If a resume is needed it is always in the action
2221 * and never in the ACL or converters. In this case, we initialise the
2222 * current rule, and go to the action execution point.
2223 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002224 if (s->current_rule) {
2225 rule = s->current_rule;
2226 s->current_rule = NULL;
2227 if (s->current_rule_list == rules)
2228 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002229 }
2230 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002231
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002232 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002233
2234 /* check optional condition */
2235 if (rule->cond) {
2236 int ret;
2237
Willy Tarreau192252e2015-04-04 01:47:55 +02002238 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002239 ret = acl_pass(ret);
2240
2241 if (rule->cond->pol == ACL_COND_UNLESS)
2242 ret = !ret;
2243
2244 if (!ret) /* condition not matched */
2245 continue;
2246 }
2247
Willy Tarreauacc98002015-09-27 23:34:39 +02002248 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002249resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002250 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002251 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002252 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2253 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002254
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002255 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002256 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002257 rule_ret = HTTP_RULE_RES_STOP;
2258 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002259
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002260 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002261 s->task->nice = rule->arg.nice;
2262 break;
2263
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002264 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002265 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002266 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002267 break;
2268
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002269 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002270#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002271 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002272 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002273#endif
2274 break;
2275
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002276 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002277 s->logs.level = rule->arg.loglevel;
2278 break;
2279
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002280 case ACT_HTTP_REPLACE_HDR:
2281 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002282 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2283 rule->arg.hdr_add.name_len,
2284 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002285 &rule->arg.hdr_add.re, rule->action)) {
2286 rule_ret = HTTP_RULE_RES_BADREQ;
2287 goto end;
2288 }
Sasha Pachev218f0642014-06-16 12:05:59 -06002289 break;
2290
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002291 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002292 ctx.idx = 0;
2293 /* remove all occurrences of the header */
2294 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002295 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002296 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2297 }
Willy Tarreau85603282015-01-21 20:39:27 +01002298 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002299
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002300 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002301 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002302 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002303
2304 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002305 if (!replace) {
2306 rule_ret = HTTP_RULE_RES_BADREQ;
2307 goto end;
2308 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002309
2310 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002311 memcpy(replace->area, rule->arg.hdr_add.name,
2312 rule->arg.hdr_add.name_len);
2313 replace->data = rule->arg.hdr_add.name_len;
2314 replace->area[replace->data++] = ':';
2315 replace->area[replace->data++] = ' ';
2316 replace->data += build_logline(s,
2317 replace->area + replace->data,
2318 replace->size - replace->data,
2319 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002320
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002321 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002322 /* remove all occurrences of the header */
2323 ctx.idx = 0;
2324 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002325 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002326 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2327 }
2328 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002329
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002330 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002331 static unsigned char rate_limit = 0;
2332
2333 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002334 replace->area[rule->arg.hdr_add.name_len] = 0;
2335 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,
2336 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002337 }
2338
2339 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2340 if (sess->fe != s->be)
2341 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2342 if (sess->listener->counters)
2343 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2344 if (objt_server(s->target))
2345 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2346 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002347
2348 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002349 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002350 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002351
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002352 case ACT_HTTP_DEL_ACL:
2353 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002354 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002355 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002356
2357 /* collect reference */
2358 ref = pat_ref_lookup(rule->arg.map.ref);
2359 if (!ref)
2360 continue;
2361
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002362 /* allocate key */
2363 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002364 if (!key) {
2365 rule_ret = HTTP_RULE_RES_BADREQ;
2366 goto end;
2367 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002368
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002369 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002370 key->data = build_logline(s, key->area, key->size,
2371 &rule->arg.map.key);
2372 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002373
2374 /* perform update */
2375 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002376 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002377 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002378 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002379
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002380 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002381 break;
2382 }
2383
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002384 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002385 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002386 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002387
2388 /* collect reference */
2389 ref = pat_ref_lookup(rule->arg.map.ref);
2390 if (!ref)
2391 continue;
2392
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002393 /* allocate key */
2394 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002395 if (!key) {
2396 rule_ret = HTTP_RULE_RES_BADREQ;
2397 goto end;
2398 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002399
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002400 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002401 key->data = build_logline(s, key->area, key->size,
2402 &rule->arg.map.key);
2403 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002404
2405 /* perform update */
2406 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002407 if (pat_ref_find_elt(ref, key->area) == NULL)
2408 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002409
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002410 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002411 break;
2412 }
2413
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002414 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002415 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002416 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002417
2418 /* collect reference */
2419 ref = pat_ref_lookup(rule->arg.map.ref);
2420 if (!ref)
2421 continue;
2422
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002423 /* allocate key */
2424 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002425 if (!key) {
2426 rule_ret = HTTP_RULE_RES_BADREQ;
2427 goto end;
2428 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002429
2430 /* allocate value */
2431 value = alloc_trash_chunk();
2432 if (!value) {
2433 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002434 rule_ret = HTTP_RULE_RES_BADREQ;
2435 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002436 }
2437
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002438 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002439 key->data = build_logline(s, key->area, key->size,
2440 &rule->arg.map.key);
2441 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002442
2443 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002444 value->data = build_logline(s, value->area,
2445 value->size,
2446 &rule->arg.map.value);
2447 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002448
2449 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002450 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002451 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002452 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002453 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002454 else
2455 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002456 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002457 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002458 free_trash_chunk(key);
2459 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002460 break;
2461 }
William Lallemand73025dd2014-04-24 14:38:37 +02002462
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002463 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002464 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002465 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002466 rule_ret = HTTP_RULE_RES_BADREQ;
2467 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002468
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002469 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2470 /* Note: only the first valid tracking parameter of each
2471 * applies.
2472 */
2473
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002474 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002475 struct stktable *t;
2476 struct stksess *ts;
2477 struct stktable_key *key;
2478 void *ptr;
2479
2480 t = rule->arg.trk_ctr.table.t;
2481 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2482
2483 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002484 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002485
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002486 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002487
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002488 /* let's count a new HTTP request as it's the first time we do it */
2489 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2490 if (ptr)
2491 stktable_data_cast(ptr, http_req_cnt)++;
2492
2493 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2494 if (ptr)
2495 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2496 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2497
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002498 /* When the client triggers a 4xx from the server, it's most often due
2499 * to a missing object or permission. These events should be tracked
2500 * because if they happen often, it may indicate a brute force or a
2501 * vulnerability scan. Normally this is done when receiving the response
2502 * but here we're tracking after this ought to have been done so we have
2503 * to do it on purpose.
2504 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002505 if ((unsigned)(txn->status - 400) < 100) {
2506 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2507 if (ptr)
2508 stktable_data_cast(ptr, http_err_cnt)++;
2509
2510 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2511 if (ptr)
2512 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2513 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2514 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002515
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002516 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002517
Emeric Brun0fed0b02017-11-29 16:15:07 +01002518 /* If data was modified, we need to touch to re-schedule sync */
2519 stktable_touch_local(t, ts, 0);
2520
Emeric Brun819fc6f2017-06-13 19:37:32 +02002521 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2522 if (sess->fe != s->be)
2523 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2524
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002525 }
2526 }
2527 break;
2528
Thierry FOURNIER42148732015-09-02 17:17:33 +02002529 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002530 if ((s->req.flags & CF_READ_ERROR) ||
2531 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2532 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2533 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002534 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002535
Willy Tarreauacc98002015-09-27 23:34:39 +02002536 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002537 case ACT_RET_ERR:
2538 case ACT_RET_CONT:
2539 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002540 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002541 rule_ret = HTTP_RULE_RES_STOP;
2542 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002543 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002544 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002545 rule_ret = HTTP_RULE_RES_YIELD;
2546 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002547 }
William Lallemand73025dd2014-04-24 14:38:37 +02002548 break;
2549
Joseph Herlant5ba80252018-11-15 09:25:36 -08002550 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002551 default:
2552 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002553 }
2554 }
2555
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002556 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002557 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002558 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002559}
2560
2561
Willy Tarreau71241ab2012-12-27 11:30:54 +01002562/* Perform an HTTP redirect based on the information in <rule>. The function
2563 * returns non-zero on success, or zero in case of a, irrecoverable error such
2564 * as too large a request to build a valid response.
2565 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002566int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002567{
Willy Tarreaub329a312015-05-22 16:27:37 +02002568 struct http_msg *req = &txn->req;
2569 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002570 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002571 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002572 int ret = 0;
2573
Christopher Fauletf2824e62018-10-01 12:12:37 +02002574 if (IS_HTX_STRM(s))
2575 return htx_apply_redirect_rule(rule, s, txn);
2576
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002577 chunk = alloc_trash_chunk();
2578 if (!chunk)
2579 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002580
2581 /* build redirect message */
2582 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002583 case 308:
2584 msg_fmt = HTTP_308;
2585 break;
2586 case 307:
2587 msg_fmt = HTTP_307;
2588 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002589 case 303:
2590 msg_fmt = HTTP_303;
2591 break;
2592 case 301:
2593 msg_fmt = HTTP_301;
2594 break;
2595 case 302:
2596 default:
2597 msg_fmt = HTTP_302;
2598 break;
2599 }
2600
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002601 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2602 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002603
2604 switch(rule->type) {
2605 case REDIRECT_TYPE_SCHEME: {
2606 const char *path;
2607 const char *host;
2608 struct hdr_ctx ctx;
2609 int pathlen;
2610 int hostlen;
2611
2612 host = "";
2613 hostlen = 0;
2614 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002615 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002616 host = ctx.line + ctx.val;
2617 hostlen = ctx.vlen;
2618 }
2619
Willy Tarreau6b952c82018-09-10 17:45:34 +02002620 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002621 /* build message using path */
2622 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002623 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002624 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2625 int qs = 0;
2626 while (qs < pathlen) {
2627 if (path[qs] == '?') {
2628 pathlen = qs;
2629 break;
2630 }
2631 qs++;
2632 }
2633 }
2634 } else {
2635 path = "/";
2636 pathlen = 1;
2637 }
2638
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002639 if (rule->rdr_str) { /* this is an old "redirect" rule */
2640 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002641 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002642 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002643
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002644 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002645 memcpy(chunk->area + chunk->data, rule->rdr_str,
2646 rule->rdr_len);
2647 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002648 }
2649 else {
2650 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002651 chunk->data += build_logline(s,
2652 chunk->area + chunk->data,
2653 chunk->size - chunk->data,
2654 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002655
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002656 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002657 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002658 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002659 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002660 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002661 memcpy(chunk->area + chunk->data, "://", 3);
2662 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002663
2664 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002665 memcpy(chunk->area + chunk->data, host, hostlen);
2666 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002667
2668 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002669 memcpy(chunk->area + chunk->data, path, pathlen);
2670 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002671
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002672 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002673 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002674 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002675 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002676 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002677 chunk->area[chunk->data] = '/';
2678 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002679 }
2680
2681 break;
2682 }
2683 case REDIRECT_TYPE_PREFIX: {
2684 const char *path;
2685 int pathlen;
2686
Willy Tarreau6b952c82018-09-10 17:45:34 +02002687 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002688 /* build message using path */
2689 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002690 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002691 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2692 int qs = 0;
2693 while (qs < pathlen) {
2694 if (path[qs] == '?') {
2695 pathlen = qs;
2696 break;
2697 }
2698 qs++;
2699 }
2700 }
2701 } else {
2702 path = "/";
2703 pathlen = 1;
2704 }
2705
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002706 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002707 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002708 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002709
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002710 /* add prefix. Note that if prefix == "/", we don't want to
2711 * add anything, otherwise it makes it hard for the user to
2712 * configure a self-redirection.
2713 */
2714 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002715 memcpy(chunk->area + chunk->data,
2716 rule->rdr_str, rule->rdr_len);
2717 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002718 }
2719 }
2720 else {
2721 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002722 chunk->data += build_logline(s,
2723 chunk->area + chunk->data,
2724 chunk->size - chunk->data,
2725 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002726
2727 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002728 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002729 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002730 }
2731
2732 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002733 memcpy(chunk->area + chunk->data, path, pathlen);
2734 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002735
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002736 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002737 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002738 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002739 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002740 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002741 chunk->area[chunk->data] = '/';
2742 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002743 }
2744
2745 break;
2746 }
2747 case REDIRECT_TYPE_LOCATION:
2748 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002749 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002750 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002751 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002752
2753 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002754 memcpy(chunk->area + chunk->data, rule->rdr_str,
2755 rule->rdr_len);
2756 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002757 }
2758 else {
2759 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002760 chunk->data += build_logline(s,
2761 chunk->area + chunk->data,
2762 chunk->size - chunk->data,
2763 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002764
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002765 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002766 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002767 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002768 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002769 break;
2770 }
2771
2772 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002773 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2774 chunk->data += 14;
2775 memcpy(chunk->area + chunk->data, rule->cookie_str,
2776 rule->cookie_len);
2777 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002778 }
2779
Willy Tarreau19b14122017-02-28 09:48:11 +01002780 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002781 txn->status = rule->code;
2782 /* let's log the request time */
2783 s->logs.tv_request = now;
2784
Christopher Fauletbe821b92017-03-30 11:21:53 +02002785 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002786 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2787 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2788 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002789 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002790 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002791 memcpy(chunk->area + chunk->data,
2792 "\r\nProxy-Connection: keep-alive", 30);
2793 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002794 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002795 memcpy(chunk->area + chunk->data,
2796 "\r\nConnection: keep-alive", 24);
2797 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002798 }
2799 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002800 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2801 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002802 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002803 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002804 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002805 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002806 req->next -= req->sov;
2807 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002808 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002809 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002810 req->msg_state = HTTP_MSG_CLOSED;
2811 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002812 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002813 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002814 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002815 /* let the server side turn to SI_ST_CLO */
2816 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002817 } else {
2818 /* keep-alive not possible */
2819 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002820 memcpy(chunk->area + chunk->data,
2821 "\r\nProxy-Connection: close\r\n\r\n", 29);
2822 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002823 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002824 memcpy(chunk->area + chunk->data,
2825 "\r\nConnection: close\r\n\r\n", 23);
2826 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002827 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002828 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002829 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002830 }
2831
Willy Tarreaue7dff022015-04-03 01:14:29 +02002832 if (!(s->flags & SF_ERR_MASK))
2833 s->flags |= SF_ERR_LOCAL;
2834 if (!(s->flags & SF_FINST_MASK))
2835 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002836
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002837 ret = 1;
2838 leave:
2839 free_trash_chunk(chunk);
2840 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002841}
2842
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002843/* This stream analyser runs all HTTP request processing which is common to
2844 * frontends and backends, which means blocking ACLs, filters, connection-close,
2845 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002846 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002847 * either needs more data or wants to immediately abort the request (eg: deny,
2848 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002849 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002850int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002851{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002852 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002853 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002854 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002855 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002856 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002857 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002858 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002859 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002860
Christopher Faulete0768eb2018-10-03 16:38:02 +02002861 if (IS_HTX_STRM(s))
2862 return htx_process_req_common(s, req, an_bit, px);
2863
Willy Tarreau655dce92009-11-08 13:10:58 +01002864 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002865 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002866 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002867 }
2868
Christopher Faulet45073512018-07-20 10:16:29 +02002869 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 +02002870 now_ms, __FUNCTION__,
2871 s,
2872 req,
2873 req->rex, req->wex,
2874 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002875 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002876 req->analysers);
2877
Willy Tarreau65410832014-04-28 21:25:43 +02002878 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002879 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002880
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002881 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002882 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002883 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002884
Willy Tarreau0b748332014-04-29 00:13:29 +02002885 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002886 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2887 goto return_prx_yield;
2888
Willy Tarreau0b748332014-04-29 00:13:29 +02002889 case HTTP_RULE_RES_CONT:
2890 case HTTP_RULE_RES_STOP: /* nothing to do */
2891 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002892
Willy Tarreau0b748332014-04-29 00:13:29 +02002893 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2894 if (txn->flags & TX_CLTARPIT)
2895 goto tarpit;
2896 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002897
Willy Tarreau0b748332014-04-29 00:13:29 +02002898 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2899 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002900
Willy Tarreau0b748332014-04-29 00:13:29 +02002901 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002902 goto done;
2903
Willy Tarreau0b748332014-04-29 00:13:29 +02002904 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2905 goto return_bad_req;
2906 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002907 }
2908
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002909 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2910 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002911 struct hdr_ctx ctx;
2912
2913 ctx.idx = 0;
2914 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002915 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002916 if (unlikely(http_header_add_tail2(&txn->req,
2917 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002918 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002919 goto return_bad_req;
2920 }
2921 }
2922
2923 }
2924
Willy Tarreau52542592014-04-28 18:33:26 +02002925 /* OK at this stage, we know that the request was accepted according to
2926 * the http-request rules, we can check for the stats. Note that the
2927 * URI is detected *before* the req* rules in order not to be affected
2928 * by a possible reqrep, while they are processed *after* so that a
2929 * reqdeny can still block them. This clearly needs to change in 1.6!
2930 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002931 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002932 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002933 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002934 txn->status = 500;
2935 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002936 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002937
Willy Tarreaue7dff022015-04-03 01:14:29 +02002938 if (!(s->flags & SF_ERR_MASK))
2939 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002940 goto return_prx_cond;
2941 }
2942
2943 /* parse the whole stats request and extract the relevant information */
2944 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002945 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002946 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002947
Willy Tarreau0b748332014-04-29 00:13:29 +02002948 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2949 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002950
Willy Tarreau0b748332014-04-29 00:13:29 +02002951 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2952 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002953 }
2954
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002955 /* evaluate the req* rules except reqadd */
2956 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002957 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002958 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002959
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002960 if (txn->flags & TX_CLDENY)
2961 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002962
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002963 if (txn->flags & TX_CLTARPIT) {
2964 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002965 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002966 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002967 }
Willy Tarreau06619262006-12-17 08:37:22 +01002968
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002969 /* add request headers from the rule sets in the same order */
2970 list_for_each_entry(wl, &px->req_add, list) {
2971 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002972 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002973 ret = acl_pass(ret);
2974 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2975 ret = !ret;
2976 if (!ret)
2977 continue;
2978 }
2979
Christopher Faulet10079f52018-10-03 15:17:28 +02002980 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002981 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002982 }
2983
Willy Tarreau52542592014-04-28 18:33:26 +02002984
2985 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002986 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2987 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002988 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002989 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002990 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002991
Willy Tarreaue7dff022015-04-03 01:14:29 +02002992 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2993 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2994 if (!(s->flags & SF_FINST_MASK))
2995 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002996
Willy Tarreau70730dd2014-04-24 18:06:27 +02002997 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002998 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2999 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003000 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003001 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003002 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003003
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003004 /* check whether we have some ACLs set to redirect this request */
3005 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003006 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003007 int ret;
3008
Willy Tarreau192252e2015-04-04 01:47:55 +02003009 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003010 ret = acl_pass(ret);
3011 if (rule->cond->pol == ACL_COND_UNLESS)
3012 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003013 if (!ret)
3014 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003015 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003016 if (!http_apply_redirect_rule(rule, s, txn))
3017 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003018 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003019 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003020
Willy Tarreau2be39392010-01-03 17:24:51 +01003021 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3022 * If this happens, then the data will not come immediately, so we must
3023 * send all what we have without waiting. Note that due to the small gain
3024 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003025 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003026 * itself once used.
3027 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003028 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003029
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003030 done: /* done with this analyser, continue with next ones that the calling
3031 * points will have set, if any.
3032 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003033 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003034 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3035 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003036 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003037
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003038 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003039 /* Allow cookie logging
3040 */
3041 if (s->be->cookie_name || sess->fe->capture_name)
3042 manage_client_side_cookies(s, req);
3043
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003044 /* When a connection is tarpitted, we use the tarpit timeout,
3045 * which may be the same as the connect timeout if unspecified.
3046 * If unset, then set it to zero because we really want it to
3047 * eventually expire. We build the tarpit as an analyser.
3048 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003049 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003050
3051 /* wipe the request out so that we can drop the connection early
3052 * if the client closes first.
3053 */
3054 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003055
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003056 txn->status = http_err_codes[deny_status];
3057
Christopher Faulet0184ea72017-01-05 14:06:34 +01003058 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003059 req->analysers |= AN_REQ_HTTP_TARPIT;
3060 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3061 if (!req->analyse_exp)
3062 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003063 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003064 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003065 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003066 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003067 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003068 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003069 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003070
3071 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003072
3073 /* Allow cookie logging
3074 */
3075 if (s->be->cookie_name || sess->fe->capture_name)
3076 manage_client_side_cookies(s, req);
3077
Willy Tarreau0b748332014-04-29 00:13:29 +02003078 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003079 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003080 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003081 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003082 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003083 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003084 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003085 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003086 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003087 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003088 goto return_prx_cond;
3089
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003090 return_bad_req:
3091 /* We centralize bad requests processing here */
3092 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3093 /* we detected a parsing error. We want to archive this request
3094 * in the dedicated proxy area for later troubleshooting.
3095 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003096 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003097 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003098
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003099 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003100 txn->req.msg_state = HTTP_MSG_ERROR;
3101 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003102 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003103
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003104 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003105 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003106 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003107
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003108 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003109 if (!(s->flags & SF_ERR_MASK))
3110 s->flags |= SF_ERR_PRXCOND;
3111 if (!(s->flags & SF_FINST_MASK))
3112 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003113
Christopher Faulet0184ea72017-01-05 14:06:34 +01003114 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003115 req->analyse_exp = TICK_ETERNITY;
3116 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003117
3118 return_prx_yield:
3119 channel_dont_connect(req);
3120 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003121}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003122
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003123/* This function performs all the processing enabled for the current request.
3124 * It returns 1 if the processing can continue on next analysers, or zero if it
3125 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003126 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003127 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003128int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003129{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003130 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003131 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003132 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003133 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003134
Christopher Faulete0768eb2018-10-03 16:38:02 +02003135 if (IS_HTX_STRM(s))
3136 return htx_process_request(s, req, an_bit);
3137
Willy Tarreau655dce92009-11-08 13:10:58 +01003138 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003139 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003140 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003141 return 0;
3142 }
3143
Christopher Faulet45073512018-07-20 10:16:29 +02003144 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 +02003145 now_ms, __FUNCTION__,
3146 s,
3147 req,
3148 req->rex, req->wex,
3149 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003150 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003151 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003152
Willy Tarreau59234e92008-11-30 23:51:27 +01003153 /*
3154 * Right now, we know that we have processed the entire headers
3155 * and that unwanted requests have been filtered out. We can do
3156 * whatever we want with the remaining request. Also, now we
3157 * may have separate values for ->fe, ->be.
3158 */
Willy Tarreau06619262006-12-17 08:37:22 +01003159
Willy Tarreau59234e92008-11-30 23:51:27 +01003160 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003161 * If HTTP PROXY is set we simply get remote server address parsing
3162 * incoming request. Note that this requires that a connection is
3163 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003164 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003165 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003166 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003167 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003168
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003169 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003170 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003171 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003172 txn->req.msg_state = HTTP_MSG_ERROR;
3173 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003174 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003175 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003176
Willy Tarreaue7dff022015-04-03 01:14:29 +02003177 if (!(s->flags & SF_ERR_MASK))
3178 s->flags |= SF_ERR_RESOURCE;
3179 if (!(s->flags & SF_FINST_MASK))
3180 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003181
3182 return 0;
3183 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003184
Willy Tarreau6b952c82018-09-10 17:45:34 +02003185 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003186 if (url2sa(ci_head(req) + msg->sl.rq.u,
3187 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003188 &conn->addr.to, NULL) == -1)
3189 goto return_bad_req;
3190
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003191 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003192 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3193 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003194 * u_l characters by a single "/".
3195 */
3196 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003197 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003198 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3199 int delta;
3200
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003201 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003202 http_msg_move_end(&txn->req, delta);
3203 cur_end += delta;
3204 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3205 goto return_bad_req;
3206 }
3207 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003208 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003209 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3210 int delta;
3211
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003212 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003213 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003214 http_msg_move_end(&txn->req, delta);
3215 cur_end += delta;
3216 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3217 goto return_bad_req;
3218 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003219 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003220
Willy Tarreau59234e92008-11-30 23:51:27 +01003221 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003222 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003223 * Note that doing so might move headers in the request, but
3224 * the fields will stay coherent and the URI will not move.
3225 * This should only be performed in the backend.
3226 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003227 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003228 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003229
William Lallemanda73203e2012-03-12 12:48:57 +01003230 /* add unique-id if "header-unique-id" is specified */
3231
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003232 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003233 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003234 goto return_bad_req;
3235 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003236 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003237 }
William Lallemanda73203e2012-03-12 12:48:57 +01003238
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003239 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003240 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003241 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003242 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003243 goto return_bad_req;
3244 }
3245
Cyril Bontéb21570a2009-11-29 20:04:48 +01003246 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003247 * 9: add X-Forwarded-For if either the frontend or the backend
3248 * asks for it.
3249 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003250 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003251 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003252 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3253 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3254 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003255 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003256 /* The header is set to be added only if none is present
3257 * and we found it, so don't do anything.
3258 */
3259 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003260 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003261 /* Add an X-Forwarded-For header unless the source IP is
3262 * in the 'except' network range.
3263 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003264 if ((!sess->fe->except_mask.s_addr ||
3265 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3266 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003267 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003268 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003269 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003270 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003271 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003272 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003273
3274 /* Note: we rely on the backend to get the header name to be used for
3275 * x-forwarded-for, because the header is really meant for the backends.
3276 * However, if the backend did not specify any option, we have to rely
3277 * on the frontend's header name.
3278 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003279 if (s->be->fwdfor_hdr_len) {
3280 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003281 memcpy(trash.area,
3282 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003283 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003284 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003285 memcpy(trash.area,
3286 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003287 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003288 len += snprintf(trash.area + len,
3289 trash.size - len,
3290 ": %d.%d.%d.%d", pn[0], pn[1],
3291 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003292
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003293 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003294 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003295 }
3296 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003297 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003298 /* FIXME: for the sake of completeness, we should also support
3299 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003300 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003301 int len;
3302 char pn[INET6_ADDRSTRLEN];
3303 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003304 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003305 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003306
Willy Tarreau59234e92008-11-30 23:51:27 +01003307 /* Note: we rely on the backend to get the header name to be used for
3308 * x-forwarded-for, because the header is really meant for the backends.
3309 * However, if the backend did not specify any option, we have to rely
3310 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003311 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003312 if (s->be->fwdfor_hdr_len) {
3313 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003314 memcpy(trash.area, s->be->fwdfor_hdr_name,
3315 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003316 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003317 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003318 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3319 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003320 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003321 len += snprintf(trash.area + len, trash.size - len,
3322 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003323
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003324 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003325 goto return_bad_req;
3326 }
3327 }
3328
3329 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003330 * 10: add X-Original-To if either the frontend or the backend
3331 * asks for it.
3332 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003333 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003334
3335 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003336 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003337 /* Add an X-Original-To header unless the destination IP is
3338 * in the 'except' network range.
3339 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003340 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003341
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003342 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003343 ((!sess->fe->except_mask_to.s_addr ||
3344 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3345 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003346 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003347 (((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 +02003348 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003349 int len;
3350 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003351 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003352
3353 /* Note: we rely on the backend to get the header name to be used for
3354 * x-original-to, because the header is really meant for the backends.
3355 * However, if the backend did not specify any option, we have to rely
3356 * on the frontend's header name.
3357 */
3358 if (s->be->orgto_hdr_len) {
3359 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003360 memcpy(trash.area,
3361 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003362 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003363 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003364 memcpy(trash.area,
3365 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003366 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003367 len += snprintf(trash.area + len,
3368 trash.size - len,
3369 ": %d.%d.%d.%d", pn[0], pn[1],
3370 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003371
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003372 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003373 goto return_bad_req;
3374 }
3375 }
3376 }
3377
Willy Tarreau50fc7772012-11-11 22:19:57 +01003378 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3379 * If an "Upgrade" token is found, the header is left untouched in order not to have
3380 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3381 * "Upgrade" is present in the Connection header.
3382 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003383 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003384 unsigned int want_flags = 0;
3385
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003386 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003387 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003388 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003389 want_flags |= TX_CON_CLO_SET;
3390 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003391 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003392 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003393 want_flags |= TX_CON_KAL_SET;
3394 }
3395
3396 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003397 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003398 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003399
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003400
Willy Tarreau522d6c02009-12-06 18:49:18 +01003401 /* If we have no server assigned yet and we're balancing on url_param
3402 * with a POST request, we may be interested in checking the body for
3403 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003404 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003405 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003406 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003407 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003408 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003409 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003410 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003411
Christopher Fauletbe821b92017-03-30 11:21:53 +02003412 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3413 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003414#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003415 /* We expect some data from the client. Unless we know for sure
3416 * we already have a full request, we have to re-enable quick-ack
3417 * in case we previously disabled it, otherwise we might cause
3418 * the client to delay further data.
3419 */
3420 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3421 cli_conn && conn_ctrl_ready(cli_conn) &&
3422 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003423 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003424 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003425#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003426
Willy Tarreau59234e92008-11-30 23:51:27 +01003427 /*************************************************************
3428 * OK, that's finished for the headers. We have done what we *
3429 * could. Let's switch to the DATA state. *
3430 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003431 req->analyse_exp = TICK_ETERNITY;
3432 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003433
Willy Tarreau59234e92008-11-30 23:51:27 +01003434 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003435 /* OK let's go on with the BODY now */
3436 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003437
Willy Tarreau59234e92008-11-30 23:51:27 +01003438 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003439 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003440 /* we detected a parsing error. We want to archive this request
3441 * in the dedicated proxy area for later troubleshooting.
3442 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003443 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003444 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003445
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003446 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003447 txn->req.msg_state = HTTP_MSG_ERROR;
3448 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003449 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003450 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003451
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003452 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003453 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003454 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003455
Willy Tarreaue7dff022015-04-03 01:14:29 +02003456 if (!(s->flags & SF_ERR_MASK))
3457 s->flags |= SF_ERR_PRXCOND;
3458 if (!(s->flags & SF_FINST_MASK))
3459 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003460 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003461}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003462
Willy Tarreau60b85b02008-11-30 23:28:40 +01003463/* This function is an analyser which processes the HTTP tarpit. It always
3464 * returns zero, at the beginning because it prevents any other processing
3465 * from occurring, and at the end because it terminates the request.
3466 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003467int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003468{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003469 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003470
Christopher Faulete0768eb2018-10-03 16:38:02 +02003471 if (IS_HTX_STRM(s))
3472 return htx_process_tarpit(s, req, an_bit);
3473
Willy Tarreau60b85b02008-11-30 23:28:40 +01003474 /* This connection is being tarpitted. The CLIENT side has
3475 * already set the connect expiration date to the right
3476 * timeout. We just have to check that the client is still
3477 * there and that the timeout has not expired.
3478 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003479 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003480 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003481 !tick_is_expired(req->analyse_exp, now_ms))
3482 return 0;
3483
3484 /* We will set the queue timer to the time spent, just for
3485 * logging purposes. We fake a 500 server error, so that the
3486 * attacker will not suspect his connection has been tarpitted.
3487 * It will not cause trouble to the logs because we can exclude
3488 * the tarpitted connections by filtering on the 'PT' status flags.
3489 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003490 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3491
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003492 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003493 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003494
Christopher Faulet0184ea72017-01-05 14:06:34 +01003495 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003496 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003497
Willy Tarreaue7dff022015-04-03 01:14:29 +02003498 if (!(s->flags & SF_ERR_MASK))
3499 s->flags |= SF_ERR_PRXCOND;
3500 if (!(s->flags & SF_FINST_MASK))
3501 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003502 return 0;
3503}
3504
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003505/* This function is an analyser which waits for the HTTP request body. It waits
3506 * for either the buffer to be full, or the full advertised contents to have
3507 * reached the buffer. It must only be called after the standard HTTP request
3508 * processing has occurred, because it expects the request to be parsed and will
3509 * look for the Expect header. It may send a 100-Continue interim response. It
3510 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3511 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3512 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003513 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003514int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003515{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003516 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003517 struct http_txn *txn = s->txn;
3518 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003519
Christopher Faulete0768eb2018-10-03 16:38:02 +02003520 if (IS_HTX_STRM(s))
3521 return htx_wait_for_request_body(s, req, an_bit);
3522
Willy Tarreaud34af782008-11-30 23:36:37 +01003523 /* We have to parse the HTTP request body to find any required data.
3524 * "balance url_param check_post" should have been the only way to get
3525 * into this. We were brought here after HTTP header analysis, so all
3526 * related structures are ready.
3527 */
3528
Willy Tarreau890988f2014-04-10 11:59:33 +02003529 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3530 /* This is the first call */
3531 if (msg->msg_state < HTTP_MSG_BODY)
3532 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003533
Willy Tarreau890988f2014-04-10 11:59:33 +02003534 if (msg->msg_state < HTTP_MSG_100_SENT) {
3535 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3536 * send an HTTP/1.1 100 Continue intermediate response.
3537 */
3538 if (msg->flags & HTTP_MSGF_VER_11) {
3539 struct hdr_ctx ctx;
3540 ctx.idx = 0;
3541 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003542 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003543 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003544 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003545 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003546 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003547 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003548 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003549 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003550
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003551 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003552 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003553 * must save the body in msg->next because it survives buffer
3554 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003555 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003556 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003557
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003558 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003559 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3560 else
3561 msg->msg_state = HTTP_MSG_DATA;
3562 }
3563
Willy Tarreau890988f2014-04-10 11:59:33 +02003564 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3565 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003566 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003567 goto missing_data;
3568
3569 /* OK we have everything we need now */
3570 goto http_end;
3571 }
3572
3573 /* OK here we're parsing a chunked-encoded message */
3574
Willy Tarreau522d6c02009-12-06 18:49:18 +01003575 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003576 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003577 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003578 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003579 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003580 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003581 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003582
Willy Tarreau115acb92009-12-26 13:56:06 +01003583 if (!ret)
3584 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003585 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003586 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003587 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003588 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003589 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003590 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003591 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003592
3593 msg->chunk_len = chunk;
3594 msg->body_len += chunk;
3595
3596 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003597 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003598 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003599 }
3600
Willy Tarreaud98cf932009-12-27 22:54:55 +01003601 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003602 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3603 * for at least a whole chunk or the whole content length bytes after
3604 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003605 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003606 if (msg->msg_state == HTTP_MSG_TRAILERS)
3607 goto http_end;
3608
Willy Tarreaue115b492015-05-01 23:05:14 +02003609 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003610 goto http_end;
3611
3612 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003613 /* we get here if we need to wait for more data. If the buffer is full,
3614 * we have the maximum we can expect.
3615 */
Willy Tarreau23752332018-06-15 14:54:53 +02003616 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003617 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003618
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003619 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003620 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003621 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003622
Willy Tarreaue7dff022015-04-03 01:14:29 +02003623 if (!(s->flags & SF_ERR_MASK))
3624 s->flags |= SF_ERR_CLITO;
3625 if (!(s->flags & SF_FINST_MASK))
3626 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003627 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003628 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003629
3630 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003631 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003632 /* Not enough data. We'll re-use the http-request
3633 * timeout here. Ideally, we should set the timeout
3634 * relative to the accept() date. We just set the
3635 * request timeout once at the beginning of the
3636 * request.
3637 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003638 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003639 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003640 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003641 return 0;
3642 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003643
3644 http_end:
3645 /* The situation will not evolve, so let's give up on the analysis. */
3646 s->logs.tv_request = now; /* update the request timer to reflect full request */
3647 req->analysers &= ~an_bit;
3648 req->analyse_exp = TICK_ETERNITY;
3649 return 1;
3650
3651 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003652 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003653 txn->req.msg_state = HTTP_MSG_ERROR;
3654 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003655 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003656
Willy Tarreaue7dff022015-04-03 01:14:29 +02003657 if (!(s->flags & SF_ERR_MASK))
3658 s->flags |= SF_ERR_PRXCOND;
3659 if (!(s->flags & SF_FINST_MASK))
3660 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003661
Willy Tarreau522d6c02009-12-06 18:49:18 +01003662 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003663 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003664 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003665 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003666 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003667 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003668}
3669
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003670/* send a server's name with an outgoing request over an established connection.
3671 * Note: this function is designed to be called once the request has been scheduled
3672 * for being forwarded. This is the reason why it rewinds the buffer before
3673 * proceeding.
3674 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003675int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003676
3677 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003678 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003679 char *hdr_name = be->server_id_hdr_name;
3680 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003681 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003682 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003683 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003684
Christopher Faulet64159df2018-10-24 21:15:35 +02003685 if (IS_HTX_STRM(s))
3686 return htx_send_name_header(s, be, srv_name);
William Lallemandd9e90662012-01-30 17:27:17 +01003687 ctx.idx = 0;
3688
Willy Tarreau211cdec2014-04-17 20:18:08 +02003689 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003690 if (old_o) {
3691 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003692 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003693 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003694 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003695 }
3696
Willy Tarreauf37954d2018-06-15 18:31:02 +02003697 old_i = ci_data(chn);
3698 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 -05003699 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003700 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003701 }
3702
3703 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003704 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003705 memcpy(hdr_val, hdr_name, hdr_name_len);
3706 hdr_val += hdr_name_len;
3707 *hdr_val++ = ':';
3708 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003709 hdr_val += strlcpy2(hdr_val, srv_name,
3710 trash.area + trash.size - hdr_val);
3711 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3712 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003713
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003714 if (old_o) {
3715 /* If this was a forwarded request, we must readjust the amount of
3716 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003717 * variations. Note that the current state is >= HTTP_MSG_BODY,
3718 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003719 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003720 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003721 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003722 txn->req.next -= old_o;
3723 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003724 }
3725
Mark Lamourinec2247f02012-01-04 13:02:01 -05003726 return 0;
3727}
3728
Willy Tarreau610ecce2010-01-04 21:15:02 +01003729/* Terminate current transaction and prepare a new one. This is very tricky
3730 * right now but it works.
3731 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003732void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003733{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003734 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003735 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003736 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003737 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003738 struct connection *srv_conn;
3739 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003740 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003741
Willy Tarreau610ecce2010-01-04 21:15:02 +01003742 /* FIXME: We need a more portable way of releasing a backend's and a
3743 * server's connections. We need a safer way to reinitialize buffer
3744 * flags. We also need a more accurate method for computing per-request
3745 * data.
3746 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003747 cs = objt_cs(s->si[1].end);
3748 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003749
Willy Tarreau4213a112013-12-15 10:25:42 +01003750 /* unless we're doing keep-alive, we want to quickly close the connection
3751 * to the server.
3752 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003753 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003754 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003755 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3756 si_shutr(&s->si[1]);
3757 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003758 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003759
Willy Tarreaue7dff022015-04-03 01:14:29 +02003760 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003761 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003762 if (unlikely(s->srv_conn))
3763 sess_change_server(s, NULL);
3764 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003765
3766 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003767 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003768
Willy Tarreaueee5b512015-04-03 23:46:31 +02003769 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003770 int n;
3771
Willy Tarreaueee5b512015-04-03 23:46:31 +02003772 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003773 if (n < 1 || n > 5)
3774 n = 0;
3775
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003776 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003777 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003778 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003779 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003780 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003781 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3782 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003783 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003784 }
3785
3786 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003787 s->logs.bytes_in -= ci_data(&s->req);
3788 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003789
Willy Tarreau66425e32018-07-25 06:55:12 +02003790 /* we may need to know the position in the queue */
3791 pendconn_free(s);
3792
Willy Tarreau610ecce2010-01-04 21:15:02 +01003793 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003794 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003795 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003796 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003797 s->do_log(s);
3798 }
3799
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003800 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003801 stream_stop_content_counters(s);
3802 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003803
Willy Tarreau9efd7452018-05-31 14:48:54 +02003804 /* reset the profiling counter */
3805 s->task->calls = 0;
3806 s->task->cpu_time = 0;
3807 s->task->lat_time = 0;
3808 s->task->call_date = (profiling & HA_PROF_TASKS) ? now_mono_time() : 0;
3809
Willy Tarreau610ecce2010-01-04 21:15:02 +01003810 s->logs.accept_date = date; /* user-visible date for logging */
3811 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003812 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3813 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003814 tv_zero(&s->logs.tv_request);
3815 s->logs.t_queue = -1;
3816 s->logs.t_connect = -1;
3817 s->logs.t_data = -1;
3818 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003819 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3820 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003821
Willy Tarreauf37954d2018-06-15 18:31:02 +02003822 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3823 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003824
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003825 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003826 if (s->flags & SF_CURR_SESS) {
3827 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003828 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003829 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003830 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003831 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003832 }
3833
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003834 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003835
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003836
3837 /* If we're doing keepalive, first call the mux detach() method
3838 * to let it know we want to detach without freing the connection.
3839 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003840 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003841 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard985f1392018-11-30 17:31:52 +01003842 !si_conn_ready(&s->si[1]))
Willy Tarreau323a2d92015-08-04 19:00:17 +02003843 srv_conn = NULL;
Olivier Houchard985f1392018-11-30 17:31:52 +01003844 else if (!srv_conn->owner) {
3845 srv_conn->owner = s->sess;
3846 session_add_conn(s->sess, srv_conn, s->target);
3847 }
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003848 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003849
Willy Tarreau350f4872014-11-28 14:42:25 +01003850 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3851 s->si[1].err_type = SI_ET_NONE;
3852 s->si[1].conn_retries = 0; /* used for logging too */
3853 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003854 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 +01003855 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 +02003856 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 +02003857 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3858 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3859 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003860
Patrick Hemmere3faf022018-08-22 10:02:00 -04003861 hlua_ctx_destroy(s->hlua);
3862 s->hlua = NULL;
3863
Willy Tarreaueee5b512015-04-03 23:46:31 +02003864 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003865 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003866 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003867
3868 if (prev_status == 401 || prev_status == 407) {
3869 /* In HTTP keep-alive mode, if we receive a 401, we still have
3870 * a chance of being able to send the visitor again to the same
3871 * server over the same connection. This is required by some
3872 * broken protocols such as NTLM, and anyway whenever there is
3873 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003874 * it's better to do it (at least it helps with debugging), at
3875 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003876 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003877 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003878 }
3879
Willy Tarreau53f96852016-02-02 18:50:47 +01003880 /* Never ever allow to reuse a connection from a non-reuse backend */
3881 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3882 srv_conn->flags |= CO_FL_PRIVATE;
3883
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003884 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003885 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003886
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003887 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003888 s->req.flags |= CF_NEVER_WAIT;
3889 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003890 }
3891
Willy Tarreau714ea782015-11-25 20:11:11 +01003892 /* we're removing the analysers, we MUST re-enable events detection.
3893 * We don't enable close on the response channel since it's either
3894 * already closed, or in keep-alive with an idle connection handler.
3895 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003896 channel_auto_read(&s->req);
3897 channel_auto_close(&s->req);
3898 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003899
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003900 /* we're in keep-alive with an idle connection, monitor it if not already done */
3901 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003902 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003903 if (srv) {
3904 if (srv_conn->flags & CO_FL_PRIVATE)
3905 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3906 else if (prev_flags & TX_NOT_FIRST)
3907 /* note: we check the request, not the connection, but
3908 * this is valid for strategies SAFE and AGGR, and in
3909 * case of ALWS, we don't care anyway.
3910 */
3911 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3912 else
3913 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3914 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003915 }
Christopher Faulete6006242017-03-10 11:52:44 +01003916 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3917 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003918}
3919
3920
3921/* This function updates the request state machine according to the response
3922 * state machine and buffer flags. It returns 1 if it changes anything (flag
3923 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3924 * it is only used to find when a request/response couple is complete. Both
3925 * this function and its equivalent should loop until both return zero. It
3926 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3927 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003928int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003929{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003930 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003931 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003932 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003933 unsigned int old_state = txn->req.msg_state;
3934
Christopher Faulet4be98032017-07-18 10:48:24 +02003935 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003936 return 0;
3937
3938 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003939 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003940 * We can shut the read side unless we want to abort_on_close,
3941 * or we have a POST request. The issue with POST requests is
3942 * that some browsers still send a CRLF after the request, and
3943 * this CRLF must be read so that it does not remain in the kernel
3944 * buffers, otherwise a close could cause an RST on some systems
3945 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003946 * Note that if we're using keep-alive on the client side, we'd
3947 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003948 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003949 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003950 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003951 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3952 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003953 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3954 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003955 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003956 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003957
Willy Tarreau40f151a2012-12-20 12:10:09 +01003958 /* if the server closes the connection, we want to immediately react
3959 * and close the socket to save packets and syscalls.
3960 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003961 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003962
Willy Tarreau7f876a12015-11-18 11:59:55 +01003963 /* In any case we've finished parsing the request so we must
3964 * disable Nagle when sending data because 1) we're not going
3965 * to shut this side, and 2) the server is waiting for us to
3966 * send pending data.
3967 */
3968 chn->flags |= CF_NEVER_WAIT;
3969
Willy Tarreau610ecce2010-01-04 21:15:02 +01003970 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3971 goto wait_other_side;
3972
3973 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3974 /* The server has not finished to respond, so we
3975 * don't want to move in order not to upset it.
3976 */
3977 goto wait_other_side;
3978 }
3979
Willy Tarreau610ecce2010-01-04 21:15:02 +01003980 /* When we get here, it means that both the request and the
3981 * response have finished receiving. Depending on the connection
3982 * mode, we'll have to wait for the last bytes to leave in either
3983 * direction, and sometimes for a close to be effective.
3984 */
3985
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003986 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3987 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003988 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3989 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003990 }
3991 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3992 /* Option forceclose is set, or either side wants to close,
3993 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003994 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003995 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003996 *
3997 * However, there is an exception if the response
3998 * length is undefined. In this case, we need to wait
3999 * the close from the server. The response will be
4000 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004001 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004002 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4003 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4004 goto check_channel_flags;
4005
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004006 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4007 channel_shutr_now(chn);
4008 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004010 }
4011 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004012 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4013 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004014 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004015 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4016 channel_auto_read(chn);
4017 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004018 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004019 }
4020
Christopher Faulet4be98032017-07-18 10:48:24 +02004021 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022 }
4023
4024 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4025 http_msg_closing:
4026 /* nothing else to forward, just waiting for the output buffer
4027 * to be empty and for the shutw_now to take effect.
4028 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004029 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004030 txn->req.msg_state = HTTP_MSG_CLOSED;
4031 goto http_msg_closed;
4032 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004033 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004034 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004035 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004036 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004037 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004038 }
4039
4040 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4041 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004042 /* if we don't know whether the server will close, we need to hard close */
4043 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4044 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4045
Willy Tarreau3988d932013-12-27 23:03:08 +01004046 /* see above in MSG_DONE why we only do this in these states */
4047 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4048 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004049 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4050 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004051 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004052 goto wait_other_side;
4053 }
4054
Christopher Faulet4be98032017-07-18 10:48:24 +02004055 check_channel_flags:
4056 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4057 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4058 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004059 txn->req.msg_state = HTTP_MSG_CLOSING;
4060 goto http_msg_closing;
4061 }
4062
4063
Willy Tarreau610ecce2010-01-04 21:15:02 +01004064 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004065 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004066}
4067
4068
4069/* This function updates the response state machine according to the request
4070 * state machine and buffer flags. It returns 1 if it changes anything (flag
4071 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4072 * it is only used to find when a request/response couple is complete. Both
4073 * this function and its equivalent should loop until both return zero. It
4074 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4075 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004076int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004077{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004078 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004079 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004080 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004081 unsigned int old_state = txn->rsp.msg_state;
4082
Christopher Faulet4be98032017-07-18 10:48:24 +02004083 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004084 return 0;
4085
4086 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4087 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004088 * still monitor the server connection for a possible close
4089 * while the request is being uploaded, so we don't disable
4090 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004091 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004092 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004093
4094 if (txn->req.msg_state == HTTP_MSG_ERROR)
4095 goto wait_other_side;
4096
4097 if (txn->req.msg_state < HTTP_MSG_DONE) {
4098 /* The client seems to still be sending data, probably
4099 * because we got an error response during an upload.
4100 * We have the choice of either breaking the connection
4101 * or letting it pass through. Let's do the later.
4102 */
4103 goto wait_other_side;
4104 }
4105
Willy Tarreau610ecce2010-01-04 21:15:02 +01004106 /* When we get here, it means that both the request and the
4107 * response have finished receiving. Depending on the connection
4108 * mode, we'll have to wait for the last bytes to leave in either
4109 * direction, and sometimes for a close to be effective.
4110 */
4111
4112 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4113 /* Server-close mode : shut read and wait for the request
4114 * side to close its output buffer. The caller will detect
4115 * when we're in DONE and the other is in CLOSED and will
4116 * catch that for the final cleanup.
4117 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004118 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4119 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004120 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004121 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4122 /* Option forceclose is set, or either side wants to close,
4123 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004124 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004125 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004126 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004127 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004128 channel_shutr_now(chn);
4129 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004130 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004131 }
4132 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004133 /* The last possible modes are keep-alive and tunnel. Tunnel will
4134 * need to forward remaining data. Keep-alive will need to monitor
4135 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004136 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004137 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004138 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004139 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4140 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004141 }
4142
Christopher Faulet4be98032017-07-18 10:48:24 +02004143 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004144 }
4145
4146 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4147 http_msg_closing:
4148 /* nothing else to forward, just waiting for the output buffer
4149 * to be empty and for the shutw_now to take effect.
4150 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004151 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004152 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4153 goto http_msg_closed;
4154 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004155 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004156 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004157 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004158 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004159 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004160 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004161 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004162 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004163 }
4164
4165 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4166 http_msg_closed:
4167 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004168 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004169 channel_auto_close(chn);
4170 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004171 goto wait_other_side;
4172 }
4173
Christopher Faulet4be98032017-07-18 10:48:24 +02004174 check_channel_flags:
4175 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4176 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4177 /* if we've just closed an output, let's switch */
4178 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4179 goto http_msg_closing;
4180 }
4181
Willy Tarreau610ecce2010-01-04 21:15:02 +01004182 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004183 /* We force the response to leave immediately if we're waiting for the
4184 * other side, since there is no pending shutdown to push it out.
4185 */
4186 if (!channel_is_empty(chn))
4187 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004188 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004189}
4190
4191
Christopher Faulet894da4c2017-07-18 11:29:07 +02004192/* Resync the request and response state machines. */
4193void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004194{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004195 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004196#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004197 int old_req_state = txn->req.msg_state;
4198 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004199#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004200
Willy Tarreau610ecce2010-01-04 21:15:02 +01004201 http_sync_req_state(s);
4202 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004203 if (!http_sync_res_state(s))
4204 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004205 if (!http_sync_req_state(s))
4206 break;
4207 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004208
Christopher Faulet894da4c2017-07-18 11:29:07 +02004209 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4210 "req->analysers=0x%08x res->analysers=0x%08x\n",
4211 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004212 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4213 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004214 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004215
4216
Willy Tarreau610ecce2010-01-04 21:15:02 +01004217 /* OK, both state machines agree on a compatible state.
4218 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004219 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4220 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004221 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4222 * means we must abort the request.
4223 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4224 * corresponding channel.
4225 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4226 * on the response with server-close mode means we've completed one
4227 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004228 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004229 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4230 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004231 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004232 channel_auto_close(&s->req);
4233 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004234 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004235 channel_auto_close(&s->res);
4236 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004237 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004238 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4239 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004240 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004241 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004242 channel_auto_close(&s->res);
4243 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004244 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004245 channel_abort(&s->req);
4246 channel_auto_close(&s->req);
4247 channel_auto_read(&s->req);
4248 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004249 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004250 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4251 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4252 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4253 s->req.analysers &= AN_REQ_FLT_END;
4254 if (HAS_REQ_DATA_FILTERS(s))
4255 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4256 }
4257 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4258 s->res.analysers &= AN_RES_FLT_END;
4259 if (HAS_RSP_DATA_FILTERS(s))
4260 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4261 }
4262 channel_auto_close(&s->req);
4263 channel_auto_read(&s->req);
4264 channel_auto_close(&s->res);
4265 channel_auto_read(&s->res);
4266 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004267 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4268 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004269 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004270 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4271 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4272 /* server-close/keep-alive: terminate this transaction,
4273 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004274 * a fresh-new transaction, but only once we're sure there's
4275 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004276 * another request. They must not hold any pending output data
4277 * and the response buffer must realigned
4278 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004279 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004280 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004281 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004282 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004283 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004284 else {
4285 s->req.analysers = AN_REQ_FLT_END;
4286 s->res.analysers = AN_RES_FLT_END;
4287 txn->flags |= TX_WAIT_CLEANUP;
4288 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004289 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004290}
4291
Willy Tarreaud98cf932009-12-27 22:54:55 +01004292/* This function is an analyser which forwards request body (including chunk
4293 * sizes if any). It is called as soon as we must forward, even if we forward
4294 * zero byte. The only situation where it must not be called is when we're in
4295 * tunnel mode and we want to forward till the close. It's used both to forward
4296 * remaining data and to resync after end of body. It expects the msg_state to
4297 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004298 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004299 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004300 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004301 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004302int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004303{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004304 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004305 struct http_txn *txn = s->txn;
4306 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004307 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004308
Christopher Faulete0768eb2018-10-03 16:38:02 +02004309 if (IS_HTX_STRM(s))
4310 return htx_request_forward_body(s, req, an_bit);
4311
Christopher Faulet45073512018-07-20 10:16:29 +02004312 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 +02004313 now_ms, __FUNCTION__,
4314 s,
4315 req,
4316 req->rex, req->wex,
4317 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004318 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004319 req->analysers);
4320
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004321 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4322 return 0;
4323
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004324 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004325 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004326 /* Output closed while we were sending data. We must abort and
4327 * wake the other side up.
4328 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004329 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004330 msg->msg_state = HTTP_MSG_ERROR;
4331 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004332 return 1;
4333 }
4334
Willy Tarreaud98cf932009-12-27 22:54:55 +01004335 /* Note that we don't have to send 100-continue back because we don't
4336 * need the data to complete our job, and it's up to the server to
4337 * decide whether to return 100, 417 or anything else in return of
4338 * an "Expect: 100-continue" header.
4339 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004340 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004341 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4342 ? HTTP_MSG_CHUNK_SIZE
4343 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004344
4345 /* TODO/filters: when http-buffer-request option is set or if a
4346 * rule on url_param exists, the first chunk size could be
4347 * already parsed. In that case, msg->next is after the chunk
4348 * size (including the CRLF after the size). So this case should
4349 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004350 }
4351
Willy Tarreau7ba23542014-04-17 21:50:00 +02004352 /* Some post-connect processing might want us to refrain from starting to
4353 * forward data. Currently, the only reason for this is "balance url_param"
4354 * whichs need to parse/process the request after we've enabled forwarding.
4355 */
4356 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004357 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004358 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004359 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004360 channel_dont_close(req); /* don't fail on early shutr */
4361 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004362 }
4363 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4364 }
4365
Willy Tarreau80a92c02014-03-12 10:41:13 +01004366 /* in most states, we should abort in case of early close */
4367 channel_auto_close(req);
4368
Willy Tarreauefdf0942014-04-24 20:08:57 +02004369 if (req->to_forward) {
4370 /* We can't process the buffer's contents yet */
4371 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004372 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004373 }
4374
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004375 if (msg->msg_state < HTTP_MSG_DONE) {
4376 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4377 ? http_msg_forward_chunked_body(s, msg)
4378 : http_msg_forward_body(s, msg));
4379 if (!ret)
4380 goto missing_data_or_waiting;
4381 if (ret < 0)
4382 goto return_bad_req;
4383 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004384
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004385 /* other states, DONE...TUNNEL */
4386 /* we don't want to forward closes on DONE except in tunnel mode. */
4387 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4388 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004389
Christopher Faulet894da4c2017-07-18 11:29:07 +02004390 http_resync_states(s);
4391 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004392 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4393 if (req->flags & CF_SHUTW) {
4394 /* request errors are most likely due to the
4395 * server aborting the transfer. */
4396 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004397 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004398 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004399 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004400 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004401 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004402 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004403 }
4404
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004405 /* If "option abortonclose" is set on the backend, we want to monitor
4406 * the client's connection and forward any shutdown notification to the
4407 * server, which will decide whether to close or to go on processing the
4408 * request. We only do that in tunnel mode, and not in other modes since
4409 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004410 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004411 channel_auto_read(req);
4412 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4413 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4414 s->si[1].flags |= SI_FL_NOLINGER;
4415 channel_auto_close(req);
4416 }
4417 else if (s->txn->meth == HTTP_METH_POST) {
4418 /* POST requests may require to read extra CRLF sent by broken
4419 * browsers and which could cause an RST to be sent upon close
4420 * on some systems (eg: Linux). */
4421 channel_auto_read(req);
4422 }
4423 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004424
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004425 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004426 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004427 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004428 if (!(s->flags & SF_ERR_MASK))
4429 s->flags |= SF_ERR_CLICL;
4430 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004431 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004432 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004433 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004434 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004435 }
4436
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004437 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4438 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004439 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004440 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004441
4442 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004443 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004444
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004445 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004446 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004447 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004448 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004449
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004450 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004451 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004452 * And when content-length is used, we never want to let the possible
4453 * shutdown be forwarded to the other side, as the state machine will
4454 * take care of it once the client responds. It's also important to
4455 * prevent TIME_WAITs from accumulating on the backend side, and for
4456 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004457 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004458 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004459 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004460
Willy Tarreau5c620922011-05-11 19:56:11 +02004461 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004462 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004463 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004464 * modes are already handled by the stream sock layer. We must not do
4465 * this in content-length mode because it could present the MSG_MORE
4466 * flag with the last block of forwarded data, which would cause an
4467 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004468 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004469 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004470 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004471
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004472 return 0;
4473
Willy Tarreaud98cf932009-12-27 22:54:55 +01004474 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004475 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004476 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004477 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004478
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004479 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004480 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004481 txn->req.msg_state = HTTP_MSG_ERROR;
4482 if (txn->status) {
4483 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004484 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004485 } else {
4486 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004487 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004488 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004489 req->analysers &= AN_REQ_FLT_END;
4490 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 +01004491
Willy Tarreaue7dff022015-04-03 01:14:29 +02004492 if (!(s->flags & SF_ERR_MASK))
4493 s->flags |= SF_ERR_PRXCOND;
4494 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004495 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004496 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004497 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004498 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004499 }
4500 return 0;
4501
4502 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004503 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004504 txn->req.msg_state = HTTP_MSG_ERROR;
4505 if (txn->status) {
4506 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004507 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004508 } else {
4509 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004510 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004511 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004512 req->analysers &= AN_REQ_FLT_END;
4513 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 +01004514
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004515 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4516 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004517 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004518 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004519
Willy Tarreaue7dff022015-04-03 01:14:29 +02004520 if (!(s->flags & SF_ERR_MASK))
4521 s->flags |= SF_ERR_SRVCL;
4522 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004523 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004524 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004525 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004526 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004527 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004528 return 0;
4529}
4530
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004531/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4532 * processing can continue on next analysers, or zero if it either needs more
4533 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004534 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004535 * when it has nothing left to do, and may remove any analyser when it wants to
4536 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004537 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004538int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004539{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004540 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004541 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004542 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004543 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004544 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004545 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004546 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004547 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004548
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004549 srv_conn = cs_conn(objt_cs(s->si[1].end));
4550
Christopher Faulete0768eb2018-10-03 16:38:02 +02004551 if (IS_HTX_STRM(s))
4552 return htx_wait_for_response(s, rep, an_bit);
4553
Christopher Faulet45073512018-07-20 10:16:29 +02004554 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 +02004555 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004556 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004557 rep,
4558 rep->rex, rep->wex,
4559 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004560 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004561 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004562
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004563 /*
4564 * Now parse the partial (or complete) lines.
4565 * We will check the response syntax, and also join multi-line
4566 * headers. An index of all the lines will be elaborated while
4567 * parsing.
4568 *
4569 * For the parsing, we use a 28 states FSM.
4570 *
4571 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004572 * ci_head(rep) = beginning of response
4573 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4574 * ci_tail(rep) = end of input data
4575 * msg->eol = end of current header or line (LF or CRLF)
4576 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004577 */
4578
Willy Tarreau628c40c2014-04-24 19:11:26 +02004579 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004580 /* There's a protected area at the end of the buffer for rewriting
4581 * purposes. We don't want to start to parse the request if the
4582 * protected area is affected, because we may have to move processed
4583 * data later, which is much more complicated.
4584 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004585 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004586 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004587 /* some data has still not left the buffer, wake us once that's done */
4588 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4589 goto abort_response;
4590 channel_dont_close(rep);
4591 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004592 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004593 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004594 }
4595
Willy Tarreau188e2302018-06-15 11:11:53 +02004596 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004597 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004598 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004599
Willy Tarreauf37954d2018-06-15 18:31:02 +02004600 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004601 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004602 }
4603
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004604 /* 1: we might have to print this header in debug mode */
4605 if (unlikely((global.mode & MODE_DEBUG) &&
4606 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004607 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004608 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004609
Willy Tarreauf37954d2018-06-15 18:31:02 +02004610 sol = ci_head(rep);
4611 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004612 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004613
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004614 sol += hdr_idx_first_pos(&txn->hdr_idx);
4615 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004616
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004617 while (cur_idx) {
4618 eol = sol + txn->hdr_idx.v[cur_idx].len;
4619 debug_hdr("srvhdr", s, sol, eol);
4620 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4621 cur_idx = txn->hdr_idx.v[cur_idx].next;
4622 }
4623 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004624
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004625 /*
4626 * Now we quickly check if we have found a full valid response.
4627 * If not so, we check the FD and buffer states before leaving.
4628 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004629 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004630 * responses are checked first.
4631 *
4632 * Depending on whether the client is still there or not, we
4633 * may send an error response back or not. Note that normally
4634 * we should only check for HTTP status there, and check I/O
4635 * errors somewhere else.
4636 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004637
Willy Tarreau655dce92009-11-08 13:10:58 +01004638 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004639 /* Invalid response */
4640 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4641 /* we detected a parsing error. We want to archive this response
4642 * in the dedicated proxy area for later troubleshooting.
4643 */
4644 hdr_response_bad:
4645 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004646 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004647
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004648 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004649 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004650 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004651 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004652 }
Willy Tarreau64648412010-03-05 10:41:54 +01004653 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004654 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004655 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004656 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004657 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004658 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004659 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004660
Willy Tarreaue7dff022015-04-03 01:14:29 +02004661 if (!(s->flags & SF_ERR_MASK))
4662 s->flags |= SF_ERR_PRXCOND;
4663 if (!(s->flags & SF_FINST_MASK))
4664 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004665
4666 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004667 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004668
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004669 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004670 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004671 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004672 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004673 goto hdr_response_bad;
4674 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004675
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004676 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004677 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004678 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004679 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004680 else if (txn->flags & TX_NOT_FIRST)
4681 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004682
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004683 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004684 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004685 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004686 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004687 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004688
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004689 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004690 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004691 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004692
4693 /* Check to see if the server refused the early data.
4694 * If so, just send a 425
4695 */
4696 if (objt_cs(s->si[1].end)) {
4697 struct connection *conn = objt_cs(s->si[1].end)->conn;
4698
4699 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4700 txn->status = 425;
4701 }
4702
Willy Tarreau350f4872014-11-28 14:42:25 +01004703 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004704 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004705 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004706
Willy Tarreaue7dff022015-04-03 01:14:29 +02004707 if (!(s->flags & SF_ERR_MASK))
4708 s->flags |= SF_ERR_SRVCL;
4709 if (!(s->flags & SF_FINST_MASK))
4710 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004711 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004712 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004713
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004714 /* read timeout : return a 504 to the client. */
4715 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004716 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004717 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004718
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004719 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004720 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004721 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004722 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004723 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004724
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004725 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004726 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004727 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004728 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004729 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004730 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004731
Willy Tarreaue7dff022015-04-03 01:14:29 +02004732 if (!(s->flags & SF_ERR_MASK))
4733 s->flags |= SF_ERR_SRVTO;
4734 if (!(s->flags & SF_FINST_MASK))
4735 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004736 return 0;
4737 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004738
Willy Tarreauf003d372012-11-26 13:35:37 +01004739 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004740 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004741 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4742 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004743 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004744 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004745
Christopher Faulet0184ea72017-01-05 14:06:34 +01004746 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004747 channel_auto_close(rep);
4748
4749 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004750 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004751 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004752
Willy Tarreaue7dff022015-04-03 01:14:29 +02004753 if (!(s->flags & SF_ERR_MASK))
4754 s->flags |= SF_ERR_CLICL;
4755 if (!(s->flags & SF_FINST_MASK))
4756 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004757
Willy Tarreau87b09662015-04-03 00:22:06 +02004758 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004759 return 0;
4760 }
4761
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004762 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004763 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004764 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004765 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004766 else if (txn->flags & TX_NOT_FIRST)
4767 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004768
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004769 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004770 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004771 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004772 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004773 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004774
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004775 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004776 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004777 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004778 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004779 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004780 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004781
Willy Tarreaue7dff022015-04-03 01:14:29 +02004782 if (!(s->flags & SF_ERR_MASK))
4783 s->flags |= SF_ERR_SRVCL;
4784 if (!(s->flags & SF_FINST_MASK))
4785 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004786 return 0;
4787 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004788
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004789 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004790 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004791 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004792 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004793 else if (txn->flags & TX_NOT_FIRST)
4794 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004795
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004796 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004797 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004798 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004799
Willy Tarreaue7dff022015-04-03 01:14:29 +02004800 if (!(s->flags & SF_ERR_MASK))
4801 s->flags |= SF_ERR_CLICL;
4802 if (!(s->flags & SF_FINST_MASK))
4803 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004804
Willy Tarreau87b09662015-04-03 00:22:06 +02004805 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004806 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004807 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004808
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004809 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004810 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004811 return 0;
4812 }
4813
4814 /* More interesting part now : we know that we have a complete
4815 * response which at least looks like HTTP. We have an indicator
4816 * of each header's length, so we can parse them quickly.
4817 */
4818
4819 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004820 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004821
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004822 /*
4823 * 1: get the status code
4824 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004825 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004826 if (n < 1 || n > 5)
4827 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004828 /* when the client triggers a 4xx from the server, it's most often due
4829 * to a missing object or permission. These events should be tracked
4830 * because if they happen often, it may indicate a brute force or a
4831 * vulnerability scan.
4832 */
4833 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004834 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004835
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004836 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004837 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004838
Willy Tarreau91852eb2015-05-01 13:26:00 +02004839 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4840 * exactly one digit "." one digit. This check may be disabled using
4841 * option accept-invalid-http-response.
4842 */
4843 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4844 if (msg->sl.st.v_l != 8) {
4845 msg->err_pos = 0;
4846 goto hdr_response_bad;
4847 }
4848
Willy Tarreauf37954d2018-06-15 18:31:02 +02004849 if (ci_head(rep)[4] != '/' ||
4850 !isdigit((unsigned char)ci_head(rep)[5]) ||
4851 ci_head(rep)[6] != '.' ||
4852 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004853 msg->err_pos = 4;
4854 goto hdr_response_bad;
4855 }
4856 }
4857
Willy Tarreau5b154472009-12-21 20:11:07 +01004858 /* check if the response is HTTP/1.1 or above */
4859 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004860 ((ci_head(rep)[5] > '1') ||
4861 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004862 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004863
4864 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004865 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 +01004866
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004867 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004868 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004869
Willy Tarreauf37954d2018-06-15 18:31:02 +02004870 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004871
Willy Tarreau39650402010-03-15 19:44:39 +01004872 /* Adjust server's health based on status code. Note: status codes 501
4873 * and 505 are triggered on demand by client request, so we must not
4874 * count them as server failures.
4875 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004876 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004877 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004878 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004879 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004880 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004881 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004882
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004883 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004884 * We may be facing a 100-continue response, or any other informational
4885 * 1xx response which is non-final, in which case this is not the right
4886 * response, and we're waiting for the next one. Let's allow this response
4887 * to go to the client and wait for the next one. There's an exception for
4888 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004889 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004890 if (txn->status < 200 &&
4891 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004892 hdr_idx_init(&txn->hdr_idx);
4893 msg->next -= channel_forward(rep, msg->next);
4894 msg->msg_state = HTTP_MSG_RPBEFORE;
4895 txn->status = 0;
4896 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004897 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004898 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004899 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004900
Willy Tarreaua14ad722017-07-07 11:36:32 +02004901 /*
4902 * 2: check for cacheability.
4903 */
4904
4905 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004906 case 200:
4907 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004908 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004909 case 206:
4910 case 300:
4911 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004912 case 404:
4913 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004914 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004915 case 414:
4916 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004917 break;
4918 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004919 /* RFC7231#6.1:
4920 * Responses with status codes that are defined as
4921 * cacheable by default (e.g., 200, 203, 204, 206,
4922 * 300, 301, 404, 405, 410, 414, and 501 in this
4923 * specification) can be reused by a cache with
4924 * heuristic expiration unless otherwise indicated
4925 * by the method definition or explicit cache
4926 * controls [RFC7234]; all other status codes are
4927 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004928 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004929 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004930 break;
4931 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004932
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004933 /*
4934 * 3: we may need to capture headers
4935 */
4936 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004937 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004938 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4939 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004940
Willy Tarreau557f1992015-05-01 10:05:17 +02004941 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4942 * by RFC7230#3.3.3 :
4943 *
4944 * The length of a message body is determined by one of the following
4945 * (in order of precedence):
4946 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004947 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4948 * the connection will become a tunnel immediately after the empty
4949 * line that concludes the header fields. A client MUST ignore
4950 * any Content-Length or Transfer-Encoding header fields received
4951 * in such a message. Any 101 response (Switching Protocols) is
4952 * managed in the same manner.
4953 *
4954 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004955 * (Informational), 204 (No Content), or 304 (Not Modified) status
4956 * code is always terminated by the first empty line after the
4957 * header fields, regardless of the header fields present in the
4958 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004959 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004960 * 3. If a Transfer-Encoding header field is present and the chunked
4961 * transfer coding (Section 4.1) is the final encoding, the message
4962 * body length is determined by reading and decoding the chunked
4963 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004964 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004965 * If a Transfer-Encoding header field is present in a response and
4966 * the chunked transfer coding is not the final encoding, the
4967 * message body length is determined by reading the connection until
4968 * it is closed by the server. If a Transfer-Encoding header field
4969 * is present in a request and the chunked transfer coding is not
4970 * the final encoding, the message body length cannot be determined
4971 * reliably; the server MUST respond with the 400 (Bad Request)
4972 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004973 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004974 * If a message is received with both a Transfer-Encoding and a
4975 * Content-Length header field, the Transfer-Encoding overrides the
4976 * Content-Length. Such a message might indicate an attempt to
4977 * perform request smuggling (Section 9.5) or response splitting
4978 * (Section 9.4) and ought to be handled as an error. A sender MUST
4979 * remove the received Content-Length field prior to forwarding such
4980 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004981 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004982 * 4. If a message is received without Transfer-Encoding and with
4983 * either multiple Content-Length header fields having differing
4984 * field-values or a single Content-Length header field having an
4985 * invalid value, then the message framing is invalid and the
4986 * recipient MUST treat it as an unrecoverable error. If this is a
4987 * request message, the server MUST respond with a 400 (Bad Request)
4988 * status code and then close the connection. If this is a response
4989 * message received by a proxy, the proxy MUST close the connection
4990 * to the server, discard the received response, and send a 502 (Bad
4991 * Gateway) response to the client. If this is a response message
4992 * received by a user agent, the user agent MUST close the
4993 * connection to the server and discard the received response.
4994 *
4995 * 5. If a valid Content-Length header field is present without
4996 * Transfer-Encoding, its decimal value defines the expected message
4997 * body length in octets. If the sender closes the connection or
4998 * the recipient times out before the indicated number of octets are
4999 * received, the recipient MUST consider the message to be
5000 * incomplete and close the connection.
5001 *
5002 * 6. If this is a request message and none of the above are true, then
5003 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005004 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005005 * 7. Otherwise, this is a response message without a declared message
5006 * body length, so the message body length is determined by the
5007 * number of octets received prior to the server closing the
5008 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005009 */
5010
5011 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005012 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005013 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005014 * FIXME: should we parse anyway and return an error on chunked encoding ?
5015 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005016 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5017 txn->status == 101)) {
5018 /* Either we've established an explicit tunnel, or we're
5019 * switching the protocol. In both cases, we're very unlikely
5020 * to understand the next protocols. We have to switch to tunnel
5021 * mode, so that we transfer the request and responses then let
5022 * this protocol pass unmodified. When we later implement specific
5023 * parsers for such protocols, we'll want to check the Upgrade
5024 * header which contains information about that protocol for
5025 * responses with status 101 (eg: see RFC2817 about TLS).
5026 */
5027 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5028 msg->flags |= HTTP_MSGF_XFER_LEN;
5029 goto end;
5030 }
5031
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005032 if (txn->meth == HTTP_METH_HEAD ||
5033 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005034 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005035 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005036 goto skip_content_length;
5037 }
5038
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005039 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005040 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005041 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005042 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005043 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5044 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005045 /* bad transfer-encoding (chunked followed by something else) */
5046 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005047 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005048 break;
5049 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005050 }
5051
Willy Tarreau1c913912015-04-30 10:57:51 +02005052 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005053 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005054 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005055 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005056 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5057 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005058 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005059 signed long long cl;
5060
Willy Tarreauad14f752011-09-02 20:33:27 +02005061 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005062 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005063 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005064 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005065
Willy Tarreauad14f752011-09-02 20:33:27 +02005066 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005067 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005068 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005069 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005070
Willy Tarreauad14f752011-09-02 20:33:27 +02005071 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005072 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005073 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005074 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005075
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005076 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005077 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005078 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005079 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005080
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005081 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005082 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005083 }
5084
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02005085 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
5086 * 407 (Proxy-Authenticate) responses and set the connection to private
5087 */
5088 if (srv_conn && txn->status == 401) {
5089 /* check for Negotiate/NTLM WWW-Authenticate headers */
5090 ctx.idx = 0;
5091 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
5092 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5093 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5094 srv_conn->flags |= CO_FL_PRIVATE;
5095 }
5096 } else if (srv_conn && txn->status == 407) {
5097 /* check for Negotiate/NTLM Proxy-Authenticate headers */
5098 ctx.idx = 0;
5099 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
5100 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5101 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5102 srv_conn->flags |= CO_FL_PRIVATE;
5103 }
5104 }
5105
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005106 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005107 /* Now we have to check if we need to modify the Connection header.
5108 * This is more difficult on the response than it is on the request,
5109 * because we can have two different HTTP versions and we don't know
5110 * how the client will interprete a response. For instance, let's say
5111 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5112 * HTTP/1.1 response without any header. Maybe it will bound itself to
5113 * HTTP/1.0 because it only knows about it, and will consider the lack
5114 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5115 * the lack of header as a keep-alive. Thus we will use two flags
5116 * indicating how a request MAY be understood by the client. In case
5117 * of multiple possibilities, we'll fix the header to be explicit. If
5118 * ambiguous cases such as both close and keepalive are seen, then we
5119 * will fall back to explicit close. Note that we won't take risks with
5120 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005121 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005122 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005123 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005124 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01005125 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005126
Willy Tarreau60466522010-01-18 19:08:45 +01005127 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005128 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01005129 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005130
Willy Tarreau60466522010-01-18 19:08:45 +01005131 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005132 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01005133 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005134 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005135 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005136 }
Willy Tarreau60466522010-01-18 19:08:45 +01005137 else { /* SCL / KAL */
5138 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005139 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005140 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005141 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005142
Willy Tarreau60466522010-01-18 19:08:45 +01005143 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005144 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005145
Willy Tarreau60466522010-01-18 19:08:45 +01005146 /* Some keep-alive responses are converted to Server-close if
5147 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005148 */
Willy Tarreau60466522010-01-18 19:08:45 +01005149 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5150 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005151 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005152 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005153 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005154 }
5155
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005156 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005157 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005158 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005159
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005160 /* end of job, return OK */
5161 rep->analysers &= ~an_bit;
5162 rep->analyse_exp = TICK_ETERNITY;
5163 channel_auto_close(rep);
5164 return 1;
5165
5166 abort_keep_alive:
5167 /* A keep-alive request to the server failed on a network error.
5168 * The client is required to retry. We need to close without returning
5169 * any other information so that the client retries.
5170 */
5171 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005172 rep->analysers &= AN_RES_FLT_END;
5173 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005174 channel_auto_close(rep);
5175 s->logs.logwait = 0;
5176 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005177 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005178 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005179 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005180 return 0;
5181}
5182
5183/* This function performs all the processing enabled for the current response.
5184 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005185 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005186 * other functions. It works like process_request (see indications above).
5187 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005188int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005189{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005190 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005191 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005192 struct http_msg *msg = &txn->rsp;
5193 struct proxy *cur_proxy;
5194 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005195 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005196
Christopher Faulete0768eb2018-10-03 16:38:02 +02005197 if (IS_HTX_STRM(s))
5198 return htx_process_res_common(s, rep, an_bit, px);
5199
Christopher Faulet45073512018-07-20 10:16:29 +02005200 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 +02005201 now_ms, __FUNCTION__,
5202 s,
5203 rep,
5204 rep->rex, rep->wex,
5205 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005206 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005207 rep->analysers);
5208
5209 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5210 return 0;
5211
Willy Tarreau70730dd2014-04-24 18:06:27 +02005212 /* The stats applet needs to adjust the Connection header but we don't
5213 * apply any filter there.
5214 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005215 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5216 rep->analysers &= ~an_bit;
5217 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005218 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005219 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005220
Willy Tarreau58975672014-04-24 21:13:57 +02005221 /*
5222 * We will have to evaluate the filters.
5223 * As opposed to version 1.2, now they will be evaluated in the
5224 * filters order and not in the header order. This means that
5225 * each filter has to be validated among all headers.
5226 *
5227 * Filters are tried with ->be first, then with ->fe if it is
5228 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005229 *
5230 * Maybe we are in resume condiion. In this case I choose the
5231 * "struct proxy" which contains the rule list matching the resume
5232 * pointer. If none of theses "struct proxy" match, I initialise
5233 * the process with the first one.
5234 *
5235 * In fact, I check only correspondance betwwen the current list
5236 * pointer and the ->fe rule list. If it doesn't match, I initialize
5237 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005238 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005239 if (s->current_rule_list == &sess->fe->http_res_rules)
5240 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005241 else
5242 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005243 while (1) {
5244 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005245
Willy Tarreau58975672014-04-24 21:13:57 +02005246 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005247 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005248 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005249
Willy Tarreau51d861a2015-05-22 17:30:48 +02005250 if (ret == HTTP_RULE_RES_BADREQ)
5251 goto return_srv_prx_502;
5252
5253 if (ret == HTTP_RULE_RES_DONE) {
5254 rep->analysers &= ~an_bit;
5255 rep->analyse_exp = TICK_ETERNITY;
5256 return 1;
5257 }
5258 }
5259
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005260 /* we need to be called again. */
5261 if (ret == HTTP_RULE_RES_YIELD) {
5262 channel_dont_close(rep);
5263 return 0;
5264 }
5265
Willy Tarreau58975672014-04-24 21:13:57 +02005266 /* try headers filters */
5267 if (rule_set->rsp_exp != NULL) {
5268 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5269 return_bad_resp:
5270 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005271 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005272 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005273 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005274 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005275 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005276 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005277 txn->status = 502;
5278 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005279 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005280 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005281 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005282 if (!(s->flags & SF_ERR_MASK))
5283 s->flags |= SF_ERR_PRXCOND;
5284 if (!(s->flags & SF_FINST_MASK))
5285 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005286 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005287 }
Willy Tarreau58975672014-04-24 21:13:57 +02005288 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005289
Willy Tarreau58975672014-04-24 21:13:57 +02005290 /* has the response been denied ? */
5291 if (txn->flags & TX_SVDENY) {
5292 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005293 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005294
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005295 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5296 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005297 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005298 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005299
Willy Tarreau58975672014-04-24 21:13:57 +02005300 goto return_srv_prx_502;
5301 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005302
Willy Tarreau58975672014-04-24 21:13:57 +02005303 /* add response headers from the rule sets in the same order */
5304 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005305 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005306 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005307 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005308 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005309 ret = acl_pass(ret);
5310 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5311 ret = !ret;
5312 if (!ret)
5313 continue;
5314 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005315 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005316 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005317 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005318
Willy Tarreau58975672014-04-24 21:13:57 +02005319 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005320 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005321 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005322 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005323 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005324
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005325 /* After this point, this anayzer can't return yield, so we can
5326 * remove the bit corresponding to this analyzer from the list.
5327 *
5328 * Note that the intermediate returns and goto found previously
5329 * reset the analyzers.
5330 */
5331 rep->analysers &= ~an_bit;
5332 rep->analyse_exp = TICK_ETERNITY;
5333
Willy Tarreau58975672014-04-24 21:13:57 +02005334 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005335 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005336 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005337
Willy Tarreau58975672014-04-24 21:13:57 +02005338 /*
5339 * Now check for a server cookie.
5340 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005341 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005342 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005343
Willy Tarreau58975672014-04-24 21:13:57 +02005344 /*
5345 * Check for cache-control or pragma headers if required.
5346 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005347 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005348 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005349
Willy Tarreau58975672014-04-24 21:13:57 +02005350 /*
5351 * Add server cookie in the response if needed
5352 */
5353 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5354 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005355 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005356 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5357 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5358 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5359 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5360 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005361 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005362 /* the server is known, it's not the one the client requested, or the
5363 * cookie's last seen date needs to be refreshed. We have to
5364 * insert a set-cookie here, except if we want to insert only on POST
5365 * requests and this one isn't. Note that servers which don't have cookies
5366 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005367 */
Willy Tarreau58975672014-04-24 21:13:57 +02005368 if (!objt_server(s->target)->cookie) {
5369 chunk_printf(&trash,
5370 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5371 s->be->cookie_name);
5372 }
5373 else {
5374 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005375
Willy Tarreau58975672014-04-24 21:13:57 +02005376 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5377 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005378 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5379 s30tob64((date.tv_sec+3) >> 2,
5380 trash.area + trash.data);
5381 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005382
Willy Tarreau58975672014-04-24 21:13:57 +02005383 if (s->be->cookie_maxlife) {
5384 /* emit first_date, which is either the original one or
5385 * the current date.
5386 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005387 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005388 s30tob64(txn->cookie_first_date ?
5389 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005390 (date.tv_sec+3) >> 2,
5391 trash.area + trash.data);
5392 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005393 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005394 }
Willy Tarreau58975672014-04-24 21:13:57 +02005395 chunk_appendf(&trash, "; path=/");
5396 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005397
Willy Tarreau58975672014-04-24 21:13:57 +02005398 if (s->be->cookie_domain)
5399 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005400
Willy Tarreau58975672014-04-24 21:13:57 +02005401 if (s->be->ck_opts & PR_CK_HTTPONLY)
5402 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005403
Willy Tarreau58975672014-04-24 21:13:57 +02005404 if (s->be->ck_opts & PR_CK_SECURE)
5405 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005406
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005407 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005408 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005409
Willy Tarreau58975672014-04-24 21:13:57 +02005410 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005411 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005412 /* the server did not change, only the date was updated */
5413 txn->flags |= TX_SCK_UPDATED;
5414 else
5415 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005416
Willy Tarreau58975672014-04-24 21:13:57 +02005417 /* Here, we will tell an eventual cache on the client side that we don't
5418 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5419 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5420 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005421 */
Willy Tarreau58975672014-04-24 21:13:57 +02005422 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005423
Willy Tarreau58975672014-04-24 21:13:57 +02005424 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005425
Willy Tarreau58975672014-04-24 21:13:57 +02005426 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5427 "Cache-control: private", 22) < 0))
5428 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005429 }
Willy Tarreau58975672014-04-24 21:13:57 +02005430 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005431
Willy Tarreau58975672014-04-24 21:13:57 +02005432 /*
5433 * Check if result will be cacheable with a cookie.
5434 * We'll block the response if security checks have caught
5435 * nasty things such as a cacheable cookie.
5436 */
5437 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5438 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5439 (s->be->options & PR_O_CHK_CACHE)) {
5440 /* we're in presence of a cacheable response containing
5441 * a set-cookie header. We'll block it as requested by
5442 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005443 */
Willy Tarreau58975672014-04-24 21:13:57 +02005444 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005445 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005446
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005447 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5448 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005449 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005450 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005451
Christopher Faulet767a84b2017-11-24 16:50:31 +01005452 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5453 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005454 send_log(s->be, LOG_ALERT,
5455 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5456 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5457 goto return_srv_prx_502;
5458 }
Willy Tarreau03945942009-12-22 16:50:27 +01005459
Willy Tarreau70730dd2014-04-24 18:06:27 +02005460 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005461 /*
5462 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5463 * If an "Upgrade" token is found, the header is left untouched in order
5464 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005465 * if anything but "Upgrade" is present in the Connection header. We don't
5466 * want to touch any 101 response either since it's switching to another
5467 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005468 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005469 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005470 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005471 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005472
Willy Tarreau58975672014-04-24 21:13:57 +02005473 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5474 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5475 /* we want a keep-alive response here. Keep-alive header
5476 * required if either side is not 1.1.
5477 */
5478 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5479 want_flags |= TX_CON_KAL_SET;
5480 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005481 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005482 /* we want a close response here. Close header required if
5483 * the server is 1.1, regardless of the client.
5484 */
5485 if (msg->flags & HTTP_MSGF_VER_11)
5486 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005487 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005488
Willy Tarreau58975672014-04-24 21:13:57 +02005489 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5490 http_change_connection_header(txn, msg, want_flags);
5491 }
5492
5493 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005494 /* Always enter in the body analyzer */
5495 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5496 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005497
Willy Tarreau58975672014-04-24 21:13:57 +02005498 /* if the user wants to log as soon as possible, without counting
5499 * bytes from the server, then this is the right moment. We have
5500 * to temporarily assign bytes_out to log what we currently have.
5501 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005502 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005503 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5504 s->logs.bytes_out = txn->rsp.eoh;
5505 s->do_log(s);
5506 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005507 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005508 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005509}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005510
Willy Tarreaud98cf932009-12-27 22:54:55 +01005511/* This function is an analyser which forwards response body (including chunk
5512 * sizes if any). It is called as soon as we must forward, even if we forward
5513 * zero byte. The only situation where it must not be called is when we're in
5514 * tunnel mode and we want to forward till the close. It's used both to forward
5515 * remaining data and to resync after end of body. It expects the msg_state to
5516 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005517 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005518 *
5519 * It is capable of compressing response data both in content-length mode and
5520 * in chunked mode. The state machines follows different flows depending on
5521 * whether content-length and chunked modes are used, since there are no
5522 * trailers in content-length :
5523 *
5524 * chk-mode cl-mode
5525 * ,----- BODY -----.
5526 * / \
5527 * V size > 0 V chk-mode
5528 * .--> SIZE -------------> DATA -------------> CRLF
5529 * | | size == 0 | last byte |
5530 * | v final crlf v inspected |
5531 * | TRAILERS -----------> DONE |
5532 * | |
5533 * `----------------------------------------------'
5534 *
5535 * Compression only happens in the DATA state, and must be flushed in final
5536 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5537 * is performed at once on final states for all bytes parsed, or when leaving
5538 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005539 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005540int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005541{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005542 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005543 struct http_txn *txn = s->txn;
5544 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005545 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005546
Christopher Faulete0768eb2018-10-03 16:38:02 +02005547 if (IS_HTX_STRM(s))
5548 return htx_response_forward_body(s, res, an_bit);
5549
Christopher Faulet45073512018-07-20 10:16:29 +02005550 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 +02005551 now_ms, __FUNCTION__,
5552 s,
5553 res,
5554 res->rex, res->wex,
5555 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005556 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005557 res->analysers);
5558
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005559 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5560 return 0;
5561
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005562 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005563 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005564 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005565 /* Output closed while we were sending data. We must abort and
5566 * wake the other side up.
5567 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005568 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005569 msg->msg_state = HTTP_MSG_ERROR;
5570 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005571 return 1;
5572 }
5573
Willy Tarreau4fe41902010-06-07 22:27:41 +02005574 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005575 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005576
Christopher Fauletd7c91962015-04-30 11:48:27 +02005577 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005578 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5579 ? HTTP_MSG_CHUNK_SIZE
5580 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005581 }
5582
Willy Tarreauefdf0942014-04-24 20:08:57 +02005583 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005584 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005585 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005586 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005587 }
5588
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005589 if (msg->msg_state < HTTP_MSG_DONE) {
5590 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5591 ? http_msg_forward_chunked_body(s, msg)
5592 : http_msg_forward_body(s, msg));
5593 if (!ret)
5594 goto missing_data_or_waiting;
5595 if (ret < 0)
5596 goto return_bad_res;
5597 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005598
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005599 /* other states, DONE...TUNNEL */
5600 /* for keep-alive we don't want to forward closes on DONE */
5601 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5602 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5603 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005604
Christopher Faulet894da4c2017-07-18 11:29:07 +02005605 http_resync_states(s);
5606 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005607 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5608 if (res->flags & CF_SHUTW) {
5609 /* response errors are most likely due to the
5610 * client aborting the transfer. */
5611 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005612 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005613 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005614 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005615 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005616 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005617 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005618 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005619 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005620
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005621 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005622 if (res->flags & CF_SHUTW)
5623 goto aborted_xfer;
5624
5625 /* stop waiting for data if the input is closed before the end. If the
5626 * client side was already closed, it means that the client has aborted,
5627 * so we don't want to count this as a server abort. Otherwise it's a
5628 * server abort.
5629 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005630 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005631 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005632 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005633 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005634 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005635 if (!(s->flags & SF_ERR_MASK))
5636 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005637 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005638 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005639 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005640 goto return_bad_res_stats_ok;
5641 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005642 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005643
Willy Tarreau40dba092010-03-04 18:14:51 +01005644 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005645 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005646 goto return_bad_res;
5647
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005648 /* When TE: chunked is used, we need to get there again to parse
5649 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005650 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5651 * or if there are filters registered on the stream, we don't want to
5652 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005653 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005654 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005655 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005656 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5657 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005658 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005659
Willy Tarreau5c620922011-05-11 19:56:11 +02005660 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005661 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005662 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005663 * modes are already handled by the stream sock layer. We must not do
5664 * this in content-length mode because it could present the MSG_MORE
5665 * flag with the last block of forwarded data, which would cause an
5666 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005667 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005668 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005669 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005670
Willy Tarreau87b09662015-04-03 00:22:06 +02005671 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005672 return 0;
5673
Willy Tarreau40dba092010-03-04 18:14:51 +01005674 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005675 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005676 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005677 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005678
5679 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005680 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005681 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005682 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005683 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005684 res->analysers &= AN_RES_FLT_END;
5685 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 +01005686 if (objt_server(s->target))
5687 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005688
Willy Tarreaue7dff022015-04-03 01:14:29 +02005689 if (!(s->flags & SF_ERR_MASK))
5690 s->flags |= SF_ERR_PRXCOND;
5691 if (!(s->flags & SF_FINST_MASK))
5692 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005693 return 0;
5694
5695 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005696 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005697 txn->rsp.msg_state = HTTP_MSG_ERROR;
5698 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005699 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005700 res->analysers &= AN_RES_FLT_END;
5701 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 +01005702
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005703 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5704 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005705 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005706 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005707
Willy Tarreaue7dff022015-04-03 01:14:29 +02005708 if (!(s->flags & SF_ERR_MASK))
5709 s->flags |= SF_ERR_CLICL;
5710 if (!(s->flags & SF_FINST_MASK))
5711 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005712 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005713}
5714
5715
Christopher Faulet10079f52018-10-03 15:17:28 +02005716int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005717{
5718 struct channel *chn = msg->chn;
5719 int ret;
5720
5721 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5722
5723 if (msg->msg_state == HTTP_MSG_ENDING)
5724 goto ending;
5725
5726 /* Neither content-length, nor transfer-encoding was found, so we must
5727 * read the body until the server connection is closed. In that case, we
5728 * eat data as they come. Of course, this happens for response only. */
5729 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005730 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005731 msg->chunk_len += len;
5732 msg->body_len += len;
5733 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005734 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005735 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005736 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005737 msg->next += ret;
5738 msg->chunk_len -= ret;
5739 if (msg->chunk_len) {
5740 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005741 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005742 chn->flags |= CF_WAKE_WRITE;
5743 goto missing_data_or_waiting;
5744 }
5745
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005746 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5747 * always set for a request. */
5748 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5749 /* The server still sending data that should be filtered */
5750 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5751 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005752 msg->msg_state = HTTP_MSG_TUNNEL;
5753 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005754 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005755
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005756 msg->msg_state = HTTP_MSG_ENDING;
5757
5758 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005759 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005760 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005761 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5762 /* default_ret */ msg->next,
5763 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005764 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005765 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005766 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5767 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005768 if (msg->next)
5769 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005770
Christopher Fauletda02e172015-12-04 09:25:05 +01005771 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5772 /* default_ret */ 1,
5773 /* on_error */ goto error,
5774 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005775 if (msg->msg_state == HTTP_MSG_ENDING)
5776 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005777 return 1;
5778
5779 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005780 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005781 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5782 /* default_ret */ msg->next,
5783 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005784 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005785 msg->next -= ret;
5786 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5787 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005788 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005789 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005790 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005791 return 0;
5792 error:
5793 return -1;
5794}
5795
Christopher Faulet10079f52018-10-03 15:17:28 +02005796int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005797{
5798 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005799 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005800 int ret;
5801
5802 /* Here we have the guarantee to be in one of the following state:
5803 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5804 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5805
Christopher Fauletca874b82018-09-20 11:31:01 +02005806 if (msg->msg_state == HTTP_MSG_ENDING)
5807 goto ending;
5808
5809 /* Don't parse chunks if there is no input data */
5810 if (!ci_data(chn))
5811 goto waiting;
5812
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005813 switch_states:
5814 switch (msg->msg_state) {
5815 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005816 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005817 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005818 /* on_error */ goto error);
5819 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005820 msg->chunk_len -= ret;
5821 if (msg->chunk_len) {
5822 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005823 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005824 chn->flags |= CF_WAKE_WRITE;
5825 goto missing_data_or_waiting;
5826 }
5827
5828 /* nothing left to forward for this chunk*/
5829 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5830 /* fall through for HTTP_MSG_CHUNK_CRLF */
5831
5832 case HTTP_MSG_CHUNK_CRLF:
5833 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005834 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005835 if (ret == 0)
5836 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005837 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005838 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005839 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005840 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005841 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005842 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005843 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005844 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5845 /* fall through for HTTP_MSG_CHUNK_SIZE */
5846
5847 case HTTP_MSG_CHUNK_SIZE:
5848 /* read the chunk size and assign it to ->chunk_len,
5849 * then set ->next to point to the body and switch to
5850 * DATA or TRAILERS state.
5851 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005852 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005853 if (ret == 0)
5854 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005855 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005856 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005857 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005858 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005859 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005860 }
5861
5862 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005863 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005864 msg->chunk_len = chunk;
5865 msg->body_len += chunk;
5866
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005867 if (msg->chunk_len) {
5868 msg->msg_state = HTTP_MSG_DATA;
5869 goto switch_states;
5870 }
5871 msg->msg_state = HTTP_MSG_TRAILERS;
5872 /* fall through for HTTP_MSG_TRAILERS */
5873
5874 case HTTP_MSG_TRAILERS:
5875 ret = http_forward_trailers(msg);
5876 if (ret < 0)
5877 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005878 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5879 /* default_ret */ 1,
5880 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005881 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005882 if (!ret)
5883 goto missing_data_or_waiting;
5884 break;
5885
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005886 default:
5887 /* This should no happen in this function */
5888 goto error;
5889 }
5890
5891 msg->msg_state = HTTP_MSG_ENDING;
5892 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005893 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005894 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005895 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005896 /* default_ret */ msg->next,
5897 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005898 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005899 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005900 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5901 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005902 if (msg->next)
5903 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005904
Christopher Fauletda02e172015-12-04 09:25:05 +01005905 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005906 /* default_ret */ 1,
5907 /* on_error */ goto error,
5908 /* on_wait */ goto waiting);
5909 msg->msg_state = HTTP_MSG_DONE;
5910 return 1;
5911
5912 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005913 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005914 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005915 /* default_ret */ msg->next,
5916 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005917 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005918 msg->next -= ret;
5919 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5920 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005921 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005922 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005923 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005924 return 0;
5925
5926 chunk_parsing_error:
5927 if (msg->err_pos >= 0) {
5928 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005929 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005930 msg->msg_state, strm_fe(s));
5931 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005932 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005933 msg, msg->msg_state, s->be);
5934 }
5935 error:
5936 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005937}
5938
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005939
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005940/* Iterate the same filter through all request headers.
5941 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005942 * Since it can manage the switch to another backend, it updates the per-proxy
5943 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005944 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005945int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005946{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005947 char *cur_ptr, *cur_end, *cur_next;
5948 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005949 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005950 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005951 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005952
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005953 last_hdr = 0;
5954
Willy Tarreauf37954d2018-06-15 18:31:02 +02005955 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005956 old_idx = 0;
5957
5958 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005959 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005960 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005961 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005962 (exp->action == ACT_ALLOW ||
5963 exp->action == ACT_DENY ||
5964 exp->action == ACT_TARPIT))
5965 return 0;
5966
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005967 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005968 if (!cur_idx)
5969 break;
5970
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005971 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005972 cur_ptr = cur_next;
5973 cur_end = cur_ptr + cur_hdr->len;
5974 cur_next = cur_end + cur_hdr->cr + 1;
5975
5976 /* Now we have one header between cur_ptr and cur_end,
5977 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005978 */
5979
Willy Tarreau15a53a42015-01-21 13:39:42 +01005980 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005981 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005982 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005983 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005984 last_hdr = 1;
5985 break;
5986
5987 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005988 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005989 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005990 break;
5991
5992 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005993 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005994 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005995 break;
5996
5997 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005998 len = exp_replace(trash.area,
5999 trash.size, cur_ptr,
6000 exp->replace, pmatch);
6001 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006002 return -1;
6003
Willy Tarreau6e27be12018-08-22 04:46:47 +02006004 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6005
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006006 /* FIXME: if the user adds a newline in the replacement, the
6007 * index will not be recalculated for now, and the new line
6008 * will not be counted as a new header.
6009 */
6010
6011 cur_end += delta;
6012 cur_next += delta;
6013 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006014 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006015 break;
6016
6017 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006018 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006019 cur_next += delta;
6020
Willy Tarreaufa355d42009-11-29 18:12:29 +01006021 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006022 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6023 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006024 cur_hdr->len = 0;
6025 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006026 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006027 break;
6028
6029 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006030 }
6031
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006032 /* keep the link from this header to next one in case of later
6033 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006034 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006035 old_idx = cur_idx;
6036 }
6037 return 0;
6038}
6039
6040
6041/* Apply the filter to the request line.
6042 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6043 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006044 * Since it can manage the switch to another backend, it updates the per-proxy
6045 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006046 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006047int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006048{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006049 char *cur_ptr, *cur_end;
6050 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006051 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006052 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006053
Willy Tarreau3d300592007-03-18 18:34:41 +01006054 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006055 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006056 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006057 (exp->action == ACT_ALLOW ||
6058 exp->action == ACT_DENY ||
6059 exp->action == ACT_TARPIT))
6060 return 0;
6061 else if (exp->action == ACT_REMOVE)
6062 return 0;
6063
6064 done = 0;
6065
Willy Tarreauf37954d2018-06-15 18:31:02 +02006066 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006067 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006068
6069 /* Now we have the request line between cur_ptr and cur_end */
6070
Willy Tarreau15a53a42015-01-21 13:39:42 +01006071 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006072 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006073 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006074 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006075 done = 1;
6076 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006078 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006079 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006080 done = 1;
6081 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006083 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006084 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006085 done = 1;
6086 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006087
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006088 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006089 len = exp_replace(trash.area, trash.size,
6090 cur_ptr, exp->replace, pmatch);
6091 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006092 return -1;
6093
Willy Tarreau6e27be12018-08-22 04:46:47 +02006094 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6095
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006096 /* FIXME: if the user adds a newline in the replacement, the
6097 * index will not be recalculated for now, and the new line
6098 * will not be counted as a new header.
6099 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006100
Willy Tarreaufa355d42009-11-29 18:12:29 +01006101 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006102 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006103 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006104 HTTP_MSG_RQMETH,
6105 cur_ptr, cur_end + 1,
6106 NULL, NULL);
6107 if (unlikely(!cur_end))
6108 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006109
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006110 /* we have a full request and we know that we have either a CR
6111 * or an LF at <ptr>.
6112 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006113 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6114 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006115 /* there is no point trying this regex on headers */
6116 return 1;
6117 }
6118 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006119 return done;
6120}
Willy Tarreau97de6242006-12-27 17:18:38 +01006121
Willy Tarreau58f10d72006-12-04 02:26:12 +01006122
Willy Tarreau58f10d72006-12-04 02:26:12 +01006123
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006124/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006125 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006126 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006127 * unparsable request. Since it can manage the switch to another backend, it
6128 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006129 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006130int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006131{
Willy Tarreau192252e2015-04-04 01:47:55 +02006132 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006133 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006134 struct hdr_exp *exp;
6135
6136 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006137 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006138
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006139 /*
6140 * The interleaving of transformations and verdicts
6141 * makes it difficult to decide to continue or stop
6142 * the evaluation.
6143 */
6144
Willy Tarreau6c123b12010-01-28 20:22:06 +01006145 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6146 break;
6147
Willy Tarreau3d300592007-03-18 18:34:41 +01006148 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006149 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006150 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006151 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006152
6153 /* if this filter had a condition, evaluate it now and skip to
6154 * next filter if the condition does not match.
6155 */
6156 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006157 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006158 ret = acl_pass(ret);
6159 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6160 ret = !ret;
6161
6162 if (!ret)
6163 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006164 }
6165
6166 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006167 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006168 if (unlikely(ret < 0))
6169 return -1;
6170
6171 if (likely(ret == 0)) {
6172 /* The filter did not match the request, it can be
6173 * iterated through all headers.
6174 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006175 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6176 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006177 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006178 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006179 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006180}
6181
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006182
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006183/* Delete a value in a header between delimiters <from> and <next> in buffer
6184 * <buf>. The number of characters displaced is returned, and the pointer to
6185 * the first delimiter is updated if required. The function tries as much as
6186 * possible to respect the following principles :
6187 * - replace <from> delimiter by the <next> one unless <from> points to a
6188 * colon, in which case <next> is simply removed
6189 * - set exactly one space character after the new first delimiter, unless
6190 * there are not enough characters in the block being moved to do so.
6191 * - remove unneeded spaces before the previous delimiter and after the new
6192 * one.
6193 *
6194 * It is the caller's responsibility to ensure that :
6195 * - <from> points to a valid delimiter or the colon ;
6196 * - <next> points to a valid delimiter or the final CR/LF ;
6197 * - there are non-space chars before <from> ;
6198 * - there is a CR/LF at or after <next>.
6199 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006200int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006201{
6202 char *prev = *from;
6203
6204 if (*prev == ':') {
6205 /* We're removing the first value, preserve the colon and add a
6206 * space if possible.
6207 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006208 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006209 next++;
6210 prev++;
6211 if (prev < next)
6212 *prev++ = ' ';
6213
Willy Tarreau2235b262016-11-05 15:50:20 +01006214 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006215 next++;
6216 } else {
6217 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006218 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006219 prev--;
6220 *from = prev;
6221
6222 /* copy the delimiter and if possible a space if we're
6223 * not at the end of the line.
6224 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006225 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006226 *prev++ = *next++;
6227 if (prev + 1 < next)
6228 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006229 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006230 next++;
6231 }
6232 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006233 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006234}
6235
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006236/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006237 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006238 * desirable to call it only when needed. This code is quite complex because
6239 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6240 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006241 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006242void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006243{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006244 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006245 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006246 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006247 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006248 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6249 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006250
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006251 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006252 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006253 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006254
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006255 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006256 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006257 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006258
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006259 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006260 hdr_beg = hdr_next;
6261 hdr_end = hdr_beg + cur_hdr->len;
6262 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006263
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006264 /* We have one full header between hdr_beg and hdr_end, and the
6265 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006266 * "Cookie:" headers.
6267 */
6268
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006269 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006270 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006271 old_idx = cur_idx;
6272 continue;
6273 }
6274
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006275 del_from = NULL; /* nothing to be deleted */
6276 preserve_hdr = 0; /* assume we may kill the whole header */
6277
Willy Tarreau58f10d72006-12-04 02:26:12 +01006278 /* Now look for cookies. Conforming to RFC2109, we have to support
6279 * attributes whose name begin with a '$', and associate them with
6280 * the right cookie, if we want to delete this cookie.
6281 * So there are 3 cases for each cookie read :
6282 * 1) it's a special attribute, beginning with a '$' : ignore it.
6283 * 2) it's a server id cookie that we *MAY* want to delete : save
6284 * some pointers on it (last semi-colon, beginning of cookie...)
6285 * 3) it's an application cookie : we *MAY* have to delete a previous
6286 * "special" cookie.
6287 * At the end of loop, if a "special" cookie remains, we may have to
6288 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006289 * *MUST* delete it.
6290 *
6291 * Note: RFC2965 is unclear about the processing of spaces around
6292 * the equal sign in the ATTR=VALUE form. A careful inspection of
6293 * the RFC explicitly allows spaces before it, and not within the
6294 * tokens (attrs or values). An inspection of RFC2109 allows that
6295 * too but section 10.1.3 lets one think that spaces may be allowed
6296 * after the equal sign too, resulting in some (rare) buggy
6297 * implementations trying to do that. So let's do what servers do.
6298 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6299 * allowed quoted strings in values, with any possible character
6300 * after a backslash, including control chars and delimitors, which
6301 * causes parsing to become ambiguous. Browsers also allow spaces
6302 * within values even without quotes.
6303 *
6304 * We have to keep multiple pointers in order to support cookie
6305 * removal at the beginning, middle or end of header without
6306 * corrupting the header. All of these headers are valid :
6307 *
6308 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6309 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6310 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6311 * | | | | | | | | |
6312 * | | | | | | | | hdr_end <--+
6313 * | | | | | | | +--> next
6314 * | | | | | | +----> val_end
6315 * | | | | | +-----------> val_beg
6316 * | | | | +--------------> equal
6317 * | | | +----------------> att_end
6318 * | | +---------------------> att_beg
6319 * | +--------------------------> prev
6320 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006321 */
6322
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006323 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6324 /* Iterate through all cookies on this line */
6325
6326 /* find att_beg */
6327 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006328 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006329 att_beg++;
6330
6331 /* find att_end : this is the first character after the last non
6332 * space before the equal. It may be equal to hdr_end.
6333 */
6334 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006335
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006336 while (equal < hdr_end) {
6337 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006338 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006339 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006340 continue;
6341 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006342 }
6343
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006344 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6345 * is between <att_beg> and <equal>, both may be identical.
6346 */
6347
6348 /* look for end of cookie if there is an equal sign */
6349 if (equal < hdr_end && *equal == '=') {
6350 /* look for the beginning of the value */
6351 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006352 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006353 val_beg++;
6354
6355 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006356 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006357
6358 /* make val_end point to the first white space or delimitor after the value */
6359 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006360 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006361 val_end--;
6362 } else {
6363 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006364 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006365
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006366 /* We have nothing to do with attributes beginning with '$'. However,
6367 * they will automatically be removed if a header before them is removed,
6368 * since they're supposed to be linked together.
6369 */
6370 if (*att_beg == '$')
6371 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006372
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006373 /* Ignore cookies with no equal sign */
6374 if (equal == next) {
6375 /* This is not our cookie, so we must preserve it. But if we already
6376 * scheduled another cookie for removal, we cannot remove the
6377 * complete header, but we can remove the previous block itself.
6378 */
6379 preserve_hdr = 1;
6380 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006381 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006382 val_end += delta;
6383 next += delta;
6384 hdr_end += delta;
6385 hdr_next += delta;
6386 cur_hdr->len += delta;
6387 http_msg_move_end(&txn->req, delta);
6388 prev = del_from;
6389 del_from = NULL;
6390 }
6391 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006392 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006393
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006394 /* if there are spaces around the equal sign, we need to
6395 * strip them otherwise we'll get trouble for cookie captures,
6396 * or even for rewrites. Since this happens extremely rarely,
6397 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006398 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006399 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6400 int stripped_before = 0;
6401 int stripped_after = 0;
6402
6403 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006404 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006405 equal += stripped_before;
6406 val_beg += stripped_before;
6407 }
6408
6409 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006410 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006411 val_beg += stripped_after;
6412 stripped_before += stripped_after;
6413 }
6414
6415 val_end += stripped_before;
6416 next += stripped_before;
6417 hdr_end += stripped_before;
6418 hdr_next += stripped_before;
6419 cur_hdr->len += stripped_before;
6420 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006421 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006422 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006423
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006424 /* First, let's see if we want to capture this cookie. We check
6425 * that we don't already have a client side cookie, because we
6426 * can only capture one. Also as an optimisation, we ignore
6427 * cookies shorter than the declared name.
6428 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006429 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6430 (val_end - att_beg >= sess->fe->capture_namelen) &&
6431 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006432 int log_len = val_end - att_beg;
6433
Willy Tarreaubafbe012017-11-24 17:34:44 +01006434 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006435 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006436 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006437 if (log_len > sess->fe->capture_len)
6438 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006439 memcpy(txn->cli_cookie, att_beg, log_len);
6440 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006441 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006442 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006443
Willy Tarreaubca99692010-10-06 19:25:55 +02006444 /* Persistence cookies in passive, rewrite or insert mode have the
6445 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006446 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006447 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006448 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006449 * For cookies in prefix mode, the form is :
6450 *
6451 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006452 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006453 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6454 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6455 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006456 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006457
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006458 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6459 * have the server ID between val_beg and delim, and the original cookie between
6460 * delim+1 and val_end. Otherwise, delim==val_end :
6461 *
6462 * Cookie: NAME=SRV; # in all but prefix modes
6463 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6464 * | || || | |+-> next
6465 * | || || | +--> val_end
6466 * | || || +---------> delim
6467 * | || |+------------> val_beg
6468 * | || +-------------> att_end = equal
6469 * | |+-----------------> att_beg
6470 * | +------------------> prev
6471 * +-------------------------> hdr_beg
6472 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006473
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006474 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006475 for (delim = val_beg; delim < val_end; delim++)
6476 if (*delim == COOKIE_DELIM)
6477 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006478 } else {
6479 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006480 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006481 /* Now check if the cookie contains a date field, which would
6482 * appear after a vertical bar ('|') just after the server name
6483 * and before the delimiter.
6484 */
6485 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6486 if (vbar1) {
6487 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006488 * right is the last seen date. It is a base64 encoded
6489 * 30-bit value representing the UNIX date since the
6490 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006491 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006492 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006493 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006494 if (val_end - vbar1 >= 5) {
6495 val = b64tos30(vbar1);
6496 if (val > 0)
6497 txn->cookie_last_date = val << 2;
6498 }
6499 /* look for a second vertical bar */
6500 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6501 if (vbar1 && (val_end - vbar1 > 5)) {
6502 val = b64tos30(vbar1 + 1);
6503 if (val > 0)
6504 txn->cookie_first_date = val << 2;
6505 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006506 }
6507 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006508
Willy Tarreauf64d1412010-10-07 20:06:11 +02006509 /* if the cookie has an expiration date and the proxy wants to check
6510 * it, then we do that now. We first check if the cookie is too old,
6511 * then only if it has expired. We detect strict overflow because the
6512 * time resolution here is not great (4 seconds). Cookies with dates
6513 * in the future are ignored if their offset is beyond one day. This
6514 * allows an admin to fix timezone issues without expiring everyone
6515 * and at the same time avoids keeping unwanted side effects for too
6516 * long.
6517 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006518 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6519 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006520 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006521 txn->flags &= ~TX_CK_MASK;
6522 txn->flags |= TX_CK_OLD;
6523 delim = val_beg; // let's pretend we have not found the cookie
6524 txn->cookie_first_date = 0;
6525 txn->cookie_last_date = 0;
6526 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006527 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6528 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006529 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006530 txn->flags &= ~TX_CK_MASK;
6531 txn->flags |= TX_CK_EXPIRED;
6532 delim = val_beg; // let's pretend we have not found the cookie
6533 txn->cookie_first_date = 0;
6534 txn->cookie_last_date = 0;
6535 }
6536
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006537 /* Here, we'll look for the first running server which supports the cookie.
6538 * This allows to share a same cookie between several servers, for example
6539 * to dedicate backup servers to specific servers only.
6540 * However, to prevent clients from sticking to cookie-less backup server
6541 * when they have incidentely learned an empty cookie, we simply ignore
6542 * empty cookies and mark them as invalid.
6543 * The same behaviour is applied when persistence must be ignored.
6544 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006545 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006546 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006547
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006548 while (srv) {
6549 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6550 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006551 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006552 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006553 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006554 /* we found the server and we can use it */
6555 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006556 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006557 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006558 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006559 break;
6560 } else {
6561 /* we found a server, but it's down,
6562 * mark it as such and go on in case
6563 * another one is available.
6564 */
6565 txn->flags &= ~TX_CK_MASK;
6566 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006567 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006568 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006569 srv = srv->next;
6570 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006571
Willy Tarreauf64d1412010-10-07 20:06:11 +02006572 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006573 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006574 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006575 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006576 txn->flags |= TX_CK_UNUSED;
6577 else
6578 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006579 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006580
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006581 /* depending on the cookie mode, we may have to either :
6582 * - delete the complete cookie if we're in insert+indirect mode, so that
6583 * the server never sees it ;
6584 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlant5ba80252018-11-15 09:25:36 -08006585 * application cookie so that it does not get accidently removed later,
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006586 * if we're in cookie prefix mode
6587 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006588 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006589 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006590
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006591 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006592 val_end += delta;
6593 next += delta;
6594 hdr_end += delta;
6595 hdr_next += delta;
6596 cur_hdr->len += delta;
6597 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006598
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006599 del_from = NULL;
6600 preserve_hdr = 1; /* we want to keep this cookie */
6601 }
6602 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006603 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006604 del_from = prev;
6605 }
6606 } else {
6607 /* This is not our cookie, so we must preserve it. But if we already
6608 * scheduled another cookie for removal, we cannot remove the
6609 * complete header, but we can remove the previous block itself.
6610 */
6611 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006612
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006613 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006614 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006615 if (att_beg >= del_from)
6616 att_beg += delta;
6617 if (att_end >= del_from)
6618 att_end += delta;
6619 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006620 val_end += delta;
6621 next += delta;
6622 hdr_end += delta;
6623 hdr_next += delta;
6624 cur_hdr->len += delta;
6625 http_msg_move_end(&txn->req, delta);
6626 prev = del_from;
6627 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006628 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006629 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006630
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006631 /* continue with next cookie on this header line */
6632 att_beg = next;
6633 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006634
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006635 /* There are no more cookies on this line.
6636 * We may still have one (or several) marked for deletion at the
6637 * end of the line. We must do this now in two ways :
6638 * - if some cookies must be preserved, we only delete from the
6639 * mark to the end of line ;
6640 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006641 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006642 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006643 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006644 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006645 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006646 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006647 cur_hdr->len += delta;
6648 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006649 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006650
6651 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006652 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6653 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006654 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006655 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006656 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006657 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006658 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006659 }
6660
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006661 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006662 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006663 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006664}
6665
6666
Willy Tarreaua15645d2007-03-18 16:22:39 +01006667/* Iterate the same filter through all response headers contained in <rtr>.
6668 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6669 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006670int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006671{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006672 char *cur_ptr, *cur_end, *cur_next;
6673 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006674 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006675 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006676 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006677
6678 last_hdr = 0;
6679
Willy Tarreauf37954d2018-06-15 18:31:02 +02006680 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006681 old_idx = 0;
6682
6683 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006684 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006685 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006686 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006687 (exp->action == ACT_ALLOW ||
6688 exp->action == ACT_DENY))
6689 return 0;
6690
6691 cur_idx = txn->hdr_idx.v[old_idx].next;
6692 if (!cur_idx)
6693 break;
6694
6695 cur_hdr = &txn->hdr_idx.v[cur_idx];
6696 cur_ptr = cur_next;
6697 cur_end = cur_ptr + cur_hdr->len;
6698 cur_next = cur_end + cur_hdr->cr + 1;
6699
6700 /* Now we have one header between cur_ptr and cur_end,
6701 * and the next header starts at cur_next.
6702 */
6703
Willy Tarreau15a53a42015-01-21 13:39:42 +01006704 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006705 switch (exp->action) {
6706 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006707 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708 last_hdr = 1;
6709 break;
6710
6711 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006712 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006713 last_hdr = 1;
6714 break;
6715
6716 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006717 len = exp_replace(trash.area,
6718 trash.size, cur_ptr,
6719 exp->replace, pmatch);
6720 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006721 return -1;
6722
Willy Tarreau6e27be12018-08-22 04:46:47 +02006723 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6724
Willy Tarreaua15645d2007-03-18 16:22:39 +01006725 /* FIXME: if the user adds a newline in the replacement, the
6726 * index will not be recalculated for now, and the new line
6727 * will not be counted as a new header.
6728 */
6729
6730 cur_end += delta;
6731 cur_next += delta;
6732 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006733 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006734 break;
6735
6736 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006737 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006738 cur_next += delta;
6739
Willy Tarreaufa355d42009-11-29 18:12:29 +01006740 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006741 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6742 txn->hdr_idx.used--;
6743 cur_hdr->len = 0;
6744 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006745 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006746 break;
6747
6748 }
6749 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006750
6751 /* keep the link from this header to next one in case of later
6752 * removal of next header.
6753 */
6754 old_idx = cur_idx;
6755 }
6756 return 0;
6757}
6758
6759
6760/* Apply the filter to the status line in the response buffer <rtr>.
6761 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6762 * or -1 if a replacement resulted in an invalid status line.
6763 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006764int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006765{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006766 char *cur_ptr, *cur_end;
6767 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006768 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006769 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006770
Willy Tarreau3d300592007-03-18 18:34:41 +01006771 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006772 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006773 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006774 (exp->action == ACT_ALLOW ||
6775 exp->action == ACT_DENY))
6776 return 0;
6777 else if (exp->action == ACT_REMOVE)
6778 return 0;
6779
6780 done = 0;
6781
Willy Tarreauf37954d2018-06-15 18:31:02 +02006782 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006783 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006784
6785 /* Now we have the status line between cur_ptr and cur_end */
6786
Willy Tarreau15a53a42015-01-21 13:39:42 +01006787 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006788 switch (exp->action) {
6789 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006790 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006791 done = 1;
6792 break;
6793
6794 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006795 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006796 done = 1;
6797 break;
6798
6799 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006800 len = exp_replace(trash.area, trash.size,
6801 cur_ptr, exp->replace, pmatch);
6802 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006803 return -1;
6804
Willy Tarreau6e27be12018-08-22 04:46:47 +02006805 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6806
Willy Tarreaua15645d2007-03-18 16:22:39 +01006807 /* FIXME: if the user adds a newline in the replacement, the
6808 * index will not be recalculated for now, and the new line
6809 * will not be counted as a new header.
6810 */
6811
Willy Tarreaufa355d42009-11-29 18:12:29 +01006812 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006813 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006814 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006815 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006816 cur_ptr, cur_end + 1,
6817 NULL, NULL);
6818 if (unlikely(!cur_end))
6819 return -1;
6820
6821 /* we have a full respnse and we know that we have either a CR
6822 * or an LF at <ptr>.
6823 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006824 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006825 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006826 /* there is no point trying this regex on headers */
6827 return 1;
6828 }
6829 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006830 return done;
6831}
6832
6833
6834
6835/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006836 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006837 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6838 * unparsable response.
6839 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006840int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841{
Willy Tarreau192252e2015-04-04 01:47:55 +02006842 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006843 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006844 struct hdr_exp *exp;
6845
6846 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006847 int ret;
6848
6849 /*
6850 * The interleaving of transformations and verdicts
6851 * makes it difficult to decide to continue or stop
6852 * the evaluation.
6853 */
6854
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006855 if (txn->flags & TX_SVDENY)
6856 break;
6857
Willy Tarreau3d300592007-03-18 18:34:41 +01006858 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006859 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6860 exp->action == ACT_PASS)) {
6861 exp = exp->next;
6862 continue;
6863 }
6864
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006865 /* if this filter had a condition, evaluate it now and skip to
6866 * next filter if the condition does not match.
6867 */
6868 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006869 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006870 ret = acl_pass(ret);
6871 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6872 ret = !ret;
6873 if (!ret)
6874 continue;
6875 }
6876
Willy Tarreaua15645d2007-03-18 16:22:39 +01006877 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006878 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006879 if (unlikely(ret < 0))
6880 return -1;
6881
6882 if (likely(ret == 0)) {
6883 /* The filter did not match the response, it can be
6884 * iterated through all headers.
6885 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006886 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6887 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006888 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006889 }
6890 return 0;
6891}
6892
6893
Willy Tarreaua15645d2007-03-18 16:22:39 +01006894/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006895 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006896 * desirable to call it only when needed. This function is also used when we
6897 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006898 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006899void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006900{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006901 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006902 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006903 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006904 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006905 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006906 char *hdr_beg, *hdr_end, *hdr_next;
6907 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006908
Willy Tarreaua15645d2007-03-18 16:22:39 +01006909 /* Iterate through the headers.
6910 * we start with the start line.
6911 */
6912 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006913 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006914
6915 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6916 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006917 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006918
6919 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006920 hdr_beg = hdr_next;
6921 hdr_end = hdr_beg + cur_hdr->len;
6922 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006923
Willy Tarreau24581ba2010-08-31 22:39:35 +02006924 /* We have one full header between hdr_beg and hdr_end, and the
6925 * next header starts at hdr_next. We're only interested in
6926 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006927 */
6928
Willy Tarreau24581ba2010-08-31 22:39:35 +02006929 is_cookie2 = 0;
6930 prev = hdr_beg + 10;
6931 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006932 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006933 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6934 if (!val) {
6935 old_idx = cur_idx;
6936 continue;
6937 }
6938 is_cookie2 = 1;
6939 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006940 }
6941
Willy Tarreau24581ba2010-08-31 22:39:35 +02006942 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6943 * <prev> points to the colon.
6944 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006945 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006946
Willy Tarreau24581ba2010-08-31 22:39:35 +02006947 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6948 * check-cache is enabled) and we are not interested in checking
6949 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006950 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006951 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006952 return;
6953
Willy Tarreau24581ba2010-08-31 22:39:35 +02006954 /* OK so now we know we have to process this response cookie.
6955 * The format of the Set-Cookie header is slightly different
6956 * from the format of the Cookie header in that it does not
6957 * support the comma as a cookie delimiter (thus the header
6958 * cannot be folded) because the Expires attribute described in
6959 * the original Netscape's spec may contain an unquoted date
6960 * with a comma inside. We have to live with this because
6961 * many browsers don't support Max-Age and some browsers don't
6962 * support quoted strings. However the Set-Cookie2 header is
6963 * clean.
6964 *
6965 * We have to keep multiple pointers in order to support cookie
6966 * removal at the beginning, middle or end of header without
6967 * corrupting the header (in case of set-cookie2). A special
6968 * pointer, <scav> points to the beginning of the set-cookie-av
6969 * fields after the first semi-colon. The <next> pointer points
6970 * either to the end of line (set-cookie) or next unquoted comma
6971 * (set-cookie2). All of these headers are valid :
6972 *
6973 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6974 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6975 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6976 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6977 * | | | | | | | | | |
6978 * | | | | | | | | +-> next hdr_end <--+
6979 * | | | | | | | +------------> scav
6980 * | | | | | | +--------------> val_end
6981 * | | | | | +--------------------> val_beg
6982 * | | | | +----------------------> equal
6983 * | | | +------------------------> att_end
6984 * | | +----------------------------> att_beg
6985 * | +------------------------------> prev
6986 * +-----------------------------------------> hdr_beg
6987 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006988
Willy Tarreau24581ba2010-08-31 22:39:35 +02006989 for (; prev < hdr_end; prev = next) {
6990 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006991
Willy Tarreau24581ba2010-08-31 22:39:35 +02006992 /* find att_beg */
6993 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006994 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006995 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006996
Willy Tarreau24581ba2010-08-31 22:39:35 +02006997 /* find att_end : this is the first character after the last non
6998 * space before the equal. It may be equal to hdr_end.
6999 */
7000 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007001
Willy Tarreau24581ba2010-08-31 22:39:35 +02007002 while (equal < hdr_end) {
7003 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7004 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007005 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007006 continue;
7007 att_end = equal;
7008 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007009
Willy Tarreau24581ba2010-08-31 22:39:35 +02007010 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7011 * is between <att_beg> and <equal>, both may be identical.
7012 */
7013
7014 /* look for end of cookie if there is an equal sign */
7015 if (equal < hdr_end && *equal == '=') {
7016 /* look for the beginning of the value */
7017 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007018 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007019 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007020
Willy Tarreau24581ba2010-08-31 22:39:35 +02007021 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02007022 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007023
7024 /* make val_end point to the first white space or delimitor after the value */
7025 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007026 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007027 val_end--;
7028 } else {
7029 /* <equal> points to next comma, semi-colon or EOL */
7030 val_beg = val_end = next = equal;
7031 }
7032
7033 if (next < hdr_end) {
7034 /* Set-Cookie2 supports multiple cookies, and <next> points to
7035 * a colon or semi-colon before the end. So skip all attr-value
7036 * pairs and look for the next comma. For Set-Cookie, since
7037 * commas are permitted in values, skip to the end.
7038 */
7039 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02007040 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007041 else
7042 next = hdr_end;
7043 }
7044
7045 /* Now everything is as on the diagram above */
7046
7047 /* Ignore cookies with no equal sign */
7048 if (equal == val_end)
7049 continue;
7050
7051 /* If there are spaces around the equal sign, we need to
7052 * strip them otherwise we'll get trouble for cookie captures,
7053 * or even for rewrites. Since this happens extremely rarely,
7054 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007055 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007056 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7057 int stripped_before = 0;
7058 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007059
Willy Tarreau24581ba2010-08-31 22:39:35 +02007060 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007061 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007062 equal += stripped_before;
7063 val_beg += stripped_before;
7064 }
7065
7066 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007067 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007068 val_beg += stripped_after;
7069 stripped_before += stripped_after;
7070 }
7071
7072 val_end += stripped_before;
7073 next += stripped_before;
7074 hdr_end += stripped_before;
7075 hdr_next += stripped_before;
7076 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007077 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007078 }
7079
7080 /* First, let's see if we want to capture this cookie. We check
7081 * that we don't already have a server side cookie, because we
7082 * can only capture one. Also as an optimisation, we ignore
7083 * cookies shorter than the declared name.
7084 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007085 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007086 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007087 (val_end - att_beg >= sess->fe->capture_namelen) &&
7088 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007089 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007090 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007091 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007092 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007093 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007094 if (log_len > sess->fe->capture_len)
7095 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007096 memcpy(txn->srv_cookie, att_beg, log_len);
7097 txn->srv_cookie[log_len] = 0;
7098 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007099 }
7100
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007101 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007102 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007103 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007104 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7105 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007106 /* assume passive cookie by default */
7107 txn->flags &= ~TX_SCK_MASK;
7108 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007109
7110 /* If the cookie is in insert mode on a known server, we'll delete
7111 * this occurrence because we'll insert another one later.
7112 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007113 * a direct access.
7114 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007115 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007116 /* The "preserve" flag was set, we don't want to touch the
7117 * server's cookie.
7118 */
7119 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007120 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007121 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007122 /* this cookie must be deleted */
7123 if (*prev == ':' && next == hdr_end) {
7124 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007125 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007126 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7127 txn->hdr_idx.used--;
7128 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007129 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007130 hdr_next += delta;
7131 http_msg_move_end(&txn->rsp, delta);
7132 /* note: while both invalid now, <next> and <hdr_end>
7133 * are still equal, so the for() will stop as expected.
7134 */
7135 } else {
7136 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007137 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007138 next = prev;
7139 hdr_end += delta;
7140 hdr_next += delta;
7141 cur_hdr->len += delta;
7142 http_msg_move_end(&txn->rsp, delta);
7143 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007144 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007145 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007146 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007147 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007148 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007149 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007150 * with this server since we know it.
7151 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007152 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007153 next += delta;
7154 hdr_end += delta;
7155 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007156 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007157 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007158
Willy Tarreauf1348312010-10-07 15:54:11 +02007159 txn->flags &= ~TX_SCK_MASK;
7160 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007161 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007162 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007163 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007164 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007165 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007166 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007167 next += delta;
7168 hdr_end += delta;
7169 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007170 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007171 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007172
Willy Tarreau827aee92011-03-10 16:55:02 +01007173 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007174 txn->flags &= ~TX_SCK_MASK;
7175 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176 }
7177 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007178 /* that's done for this cookie, check the next one on the same
7179 * line when next != hdr_end (only if is_cookie2).
7180 */
7181 }
7182 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007183 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007184 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185}
7186
7187
Willy Tarreaua15645d2007-03-18 16:22:39 +01007188/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007189 * Parses the Cache-Control and Pragma request header fields to determine if
7190 * the request may be served from the cache and/or if it is cacheable. Updates
7191 * s->txn->flags.
7192 */
7193void check_request_for_cacheability(struct stream *s, struct channel *chn)
7194{
7195 struct http_txn *txn = s->txn;
7196 char *p1, *p2;
7197 char *cur_ptr, *cur_end, *cur_next;
7198 int pragma_found;
7199 int cc_found;
7200 int cur_idx;
7201
Christopher Faulet25a02f62018-10-24 12:00:25 +02007202 if (IS_HTX_STRM(s))
7203 return htx_check_request_for_cacheability(s, chn);
7204
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007205 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7206 return; /* nothing more to do here */
7207
7208 cur_idx = 0;
7209 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007210 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007211
7212 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7213 struct hdr_idx_elem *cur_hdr;
7214 int val;
7215
7216 cur_hdr = &txn->hdr_idx.v[cur_idx];
7217 cur_ptr = cur_next;
7218 cur_end = cur_ptr + cur_hdr->len;
7219 cur_next = cur_end + cur_hdr->cr + 1;
7220
7221 /* We have one full header between cur_ptr and cur_end, and the
7222 * next header starts at cur_next.
7223 */
7224
7225 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7226 if (val) {
7227 if ((cur_end - (cur_ptr + val) >= 8) &&
7228 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7229 pragma_found = 1;
7230 continue;
7231 }
7232 }
7233
William Lallemand8a16fe02018-05-22 11:04:33 +02007234 /* Don't use the cache and don't try to store if we found the
7235 * Authorization header */
7236 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7237 if (val) {
7238 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7239 txn->flags |= TX_CACHE_IGNORE;
7240 continue;
7241 }
7242
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007243 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7244 if (!val)
7245 continue;
7246
7247 /* OK, right now we know we have a cache-control header at cur_ptr */
7248 cc_found = 1;
7249 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7250
7251 if (p1 >= cur_end) /* no more info */
7252 continue;
7253
7254 /* p1 is at the beginning of the value */
7255 p2 = p1;
7256 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7257 p2++;
7258
7259 /* we have a complete value between p1 and p2. We don't check the
7260 * values after max-age, max-stale nor min-fresh, we simply don't
7261 * use the cache when they're specified.
7262 */
7263 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7264 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7265 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7266 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7267 txn->flags |= TX_CACHE_IGNORE;
7268 continue;
7269 }
7270
7271 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7272 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7273 continue;
7274 }
7275 }
7276
7277 /* RFC7234#5.4:
7278 * When the Cache-Control header field is also present and
7279 * understood in a request, Pragma is ignored.
7280 * When the Cache-Control header field is not present in a
7281 * request, caches MUST consider the no-cache request
7282 * pragma-directive as having the same effect as if
7283 * "Cache-Control: no-cache" were present.
7284 */
7285 if (!cc_found && pragma_found)
7286 txn->flags |= TX_CACHE_IGNORE;
7287}
7288
7289/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007290 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007291 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007292void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007293{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007294 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007295 char *p1, *p2;
7296
7297 char *cur_ptr, *cur_end, *cur_next;
7298 int cur_idx;
7299
Christopher Faulet25a02f62018-10-24 12:00:25 +02007300
7301 if (IS_HTX_STRM(s))
7302 return htx_check_response_for_cacheability(s, rtr);
7303
Willy Tarreau12b32f22017-12-21 16:08:09 +01007304 if (txn->status < 200) {
7305 /* do not try to cache interim responses! */
7306 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007307 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007308 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007309
7310 /* Iterate through the headers.
7311 * we start with the start line.
7312 */
7313 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007314 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007315
7316 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7317 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007318 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007319
7320 cur_hdr = &txn->hdr_idx.v[cur_idx];
7321 cur_ptr = cur_next;
7322 cur_end = cur_ptr + cur_hdr->len;
7323 cur_next = cur_end + cur_hdr->cr + 1;
7324
7325 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007326 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007327 */
7328
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007329 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7330 if (val) {
7331 if ((cur_end - (cur_ptr + val) >= 8) &&
7332 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7333 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7334 return;
7335 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007336 }
7337
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007338 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7339 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007340 continue;
7341
7342 /* OK, right now we know we have a cache-control header at cur_ptr */
7343
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007344 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007345
7346 if (p1 >= cur_end) /* no more info */
7347 continue;
7348
7349 /* p1 is at the beginning of the value */
7350 p2 = p1;
7351
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007352 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007353 p2++;
7354
7355 /* we have a complete value between p1 and p2 */
7356 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007357 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7358 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7359 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7360 continue;
7361 }
7362
Willy Tarreaua15645d2007-03-18 16:22:39 +01007363 /* we have something of the form no-cache="set-cookie" */
7364 if ((cur_end - p1 >= 21) &&
7365 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7366 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007367 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007368 continue;
7369 }
7370
7371 /* OK, so we know that either p2 points to the end of string or to a comma */
7372 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007373 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007374 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007375 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007376 return;
7377 }
7378
7379 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007380 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007381 continue;
7382 }
7383 }
7384}
7385
Willy Tarreau58f10d72006-12-04 02:26:12 +01007386
Willy Tarreaub2513902006-12-17 14:52:38 +01007387/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007388 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007389 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007390 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007391 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007392 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007393 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007394 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007395 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007396int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007397{
7398 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007399 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007400 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007401
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007402 if (!uri_auth)
7403 return 0;
7404
Cyril Bonté70be45d2010-10-12 00:14:35 +02007405 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007406 return 0;
7407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007408 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007409 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007410 return 0;
7411
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007412 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007413 return 0;
7414
Willy Tarreaub2513902006-12-17 14:52:38 +01007415 return 1;
7416}
7417
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007418/* Append the description of what is present in error snapshot <es> into <out>.
7419 * The description must be small enough to always fit in a trash. The output
7420 * buffer may be the trash so the trash must not be used inside this function.
7421 */
7422void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7423{
7424 chunk_appendf(&trash,
7425 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7426 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7427 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7428 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7429 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7430 es->ctx.http.m_flags, es->ctx.http.m_clen,
7431 es->ctx.http.m_blen, es->ctx.http.b_flags);
7432}
7433
Willy Tarreau4076a152009-04-02 15:18:36 +02007434/*
7435 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007436 * By default it tries to report the error position as msg->err_pos. However if
7437 * this one is not set, it will then report msg->next, which is the last known
7438 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007439 * displays buffers as a contiguous area starting at buf->p. The direction is
7440 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007441 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007442void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007443 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007444 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007445{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007446 union error_snapshot_ctx ctx;
7447 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007448
Willy Tarreauef3ca732018-09-07 15:47:35 +02007449 /* http-specific part now */
7450 ctx.http.sid = s->uniq_id;
7451 ctx.http.state = state;
7452 ctx.http.b_flags = msg->chn->flags;
7453 ctx.http.s_flags = s->flags;
7454 ctx.http.t_flags = s->txn->flags;
7455 ctx.http.m_flags = msg->flags;
7456 ctx.http.m_clen = msg->chunk_len;
7457 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007458
Willy Tarreauef3ca732018-09-07 15:47:35 +02007459 ofs = msg->chn->total - ci_data(msg->chn);
7460 if (ofs < 0)
7461 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007462
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007463 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007464 other_end, s->target,
7465 strm_sess(s), &msg->chn->buf,
7466 ofs, co_data(msg->chn),
7467 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7468 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007469}
Willy Tarreaub2513902006-12-17 14:52:38 +01007470
Willy Tarreau294c4732011-12-16 21:35:50 +01007471/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7472 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7473 * performed over the whole headers. Otherwise it must contain a valid header
7474 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7475 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7476 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7477 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007478 * -1. The value fetch stops at commas, so this function is suited for use with
7479 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007480 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007481 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007482unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007483 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007484 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007485{
Willy Tarreau294c4732011-12-16 21:35:50 +01007486 struct hdr_ctx local_ctx;
7487 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007488 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007489 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007490 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007491
Willy Tarreau294c4732011-12-16 21:35:50 +01007492 if (!ctx) {
7493 local_ctx.idx = 0;
7494 ctx = &local_ctx;
7495 }
7496
Willy Tarreaubce70882009-09-07 11:51:47 +02007497 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007498 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007499 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007500 occ--;
7501 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007502 *vptr = ctx->line + ctx->val;
7503 *vlen = ctx->vlen;
7504 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007505 }
7506 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007507 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007508 }
7509
7510 /* negative occurrence, we scan all the list then walk back */
7511 if (-occ > MAX_HDR_HISTORY)
7512 return 0;
7513
Willy Tarreau294c4732011-12-16 21:35:50 +01007514 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007515 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007516 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7517 len_hist[hist_ptr] = ctx->vlen;
7518 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007519 hist_ptr = 0;
7520 found++;
7521 }
7522 if (-occ > found)
7523 return 0;
7524 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007525 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7526 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7527 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007528 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007529 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007530 if (hist_ptr >= MAX_HDR_HISTORY)
7531 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007532 *vptr = ptr_hist[hist_ptr];
7533 *vlen = len_hist[hist_ptr];
7534 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007535}
7536
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007537/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7538 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7539 * performed over the whole headers. Otherwise it must contain a valid header
7540 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7541 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7542 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7543 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7544 * -1. This function differs from http_get_hdr() in that it only returns full
7545 * line header values and does not stop at commas.
7546 * The return value is 0 if nothing was found, or non-zero otherwise.
7547 */
7548unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7549 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007550 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007551{
7552 struct hdr_ctx local_ctx;
7553 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007554 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007555 unsigned int hist_ptr;
7556 int found;
7557
7558 if (!ctx) {
7559 local_ctx.idx = 0;
7560 ctx = &local_ctx;
7561 }
7562
7563 if (occ >= 0) {
7564 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007565 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007566 occ--;
7567 if (occ <= 0) {
7568 *vptr = ctx->line + ctx->val;
7569 *vlen = ctx->vlen;
7570 return 1;
7571 }
7572 }
7573 return 0;
7574 }
7575
7576 /* negative occurrence, we scan all the list then walk back */
7577 if (-occ > MAX_HDR_HISTORY)
7578 return 0;
7579
7580 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007581 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007582 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7583 len_hist[hist_ptr] = ctx->vlen;
7584 if (++hist_ptr >= MAX_HDR_HISTORY)
7585 hist_ptr = 0;
7586 found++;
7587 }
7588 if (-occ > found)
7589 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007590
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007591 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007592 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7593 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7594 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007595 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007596 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007597 if (hist_ptr >= MAX_HDR_HISTORY)
7598 hist_ptr -= MAX_HDR_HISTORY;
7599 *vptr = ptr_hist[hist_ptr];
7600 *vlen = len_hist[hist_ptr];
7601 return 1;
7602}
7603
Willy Tarreaubaaee002006-06-26 02:48:02 +02007604/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007605 * Print a debug line with a header. Always stop at the first CR or LF char,
7606 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7607 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007608 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007609void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007610{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007611 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007612 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007613
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007614 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007615 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007616 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007617 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 +02007618
7619 for (max = 0; start + max < end; max++)
7620 if (start[max] == '\r' || start[max] == '\n')
7621 break;
7622
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007623 UBOUND(max, trash.size - trash.data - 3);
7624 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7625 trash.area[trash.data++] = '\n';
7626 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007627}
7628
Willy Tarreaueee5b512015-04-03 23:46:31 +02007629
7630/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7631 * The hdr_idx is allocated as well. In case of allocation failure, everything
7632 * allocated is freed and NULL is returned. Otherwise the new transaction is
7633 * assigned to the stream and returned.
7634 */
7635struct http_txn *http_alloc_txn(struct stream *s)
7636{
7637 struct http_txn *txn = s->txn;
7638
7639 if (txn)
7640 return txn;
7641
Willy Tarreaubafbe012017-11-24 17:34:44 +01007642 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007643 if (!txn)
7644 return txn;
7645
7646 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007647 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007648 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007649 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007650 return NULL;
7651 }
7652
7653 s->txn = txn;
7654 return txn;
7655}
7656
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007657void http_txn_reset_req(struct http_txn *txn)
7658{
7659 txn->req.flags = 0;
7660 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7661 txn->req.next = 0;
7662 txn->req.chunk_len = 0LL;
7663 txn->req.body_len = 0LL;
7664 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7665}
7666
7667void http_txn_reset_res(struct http_txn *txn)
7668{
7669 txn->rsp.flags = 0;
7670 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7671 txn->rsp.next = 0;
7672 txn->rsp.chunk_len = 0LL;
7673 txn->rsp.body_len = 0LL;
7674 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7675}
7676
Willy Tarreau0937bc42009-12-22 15:03:09 +01007677/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007678 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007679 * the required fields are properly allocated and that we only need to (re)init
7680 * them. This should be used before processing any new request.
7681 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007682void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007683{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007684 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007685 struct proxy *fe = strm_fe(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02007686 struct conn_stream *cs = objt_cs(s->si[0].end);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007687
Christopher Fauletf2824e62018-10-01 12:12:37 +02007688 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
7689 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
7690 : 0);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007691 txn->status = -1;
7692
Willy Tarreauf64d1412010-10-07 20:06:11 +02007693 txn->cookie_first_date = 0;
7694 txn->cookie_last_date = 0;
7695
Willy Tarreaueee5b512015-04-03 23:46:31 +02007696 txn->srv_cookie = NULL;
7697 txn->cli_cookie = NULL;
7698 txn->uri = NULL;
7699
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007700 http_txn_reset_req(txn);
7701 http_txn_reset_res(txn);
7702
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007703 txn->req.chn = &s->req;
7704 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007705
7706 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007707
7708 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7709 if (fe->options2 & PR_O2_REQBUG_OK)
7710 txn->req.err_pos = -1; /* let buggy requests pass */
7711
Willy Tarreau0937bc42009-12-22 15:03:09 +01007712 if (txn->hdr_idx.v)
7713 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007714
7715 vars_init(&s->vars_txn, SCOPE_TXN);
7716 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007717}
7718
7719/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007720void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007721{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007722 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007723 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007724
7725 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007726 pool_free(pool_head_requri, txn->uri);
7727 pool_free(pool_head_capture, txn->cli_cookie);
7728 pool_free(pool_head_capture, txn->srv_cookie);
7729 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007730
William Lallemanda73203e2012-03-12 12:48:57 +01007731 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007732 txn->uri = NULL;
7733 txn->srv_cookie = NULL;
7734 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007735
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007736 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007737 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007738 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007739 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007740 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007741 }
7742
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007743 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007744 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007745 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007746 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007747 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007748 }
7749
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007750 if (!LIST_ISEMPTY(&s->vars_txn.head))
7751 vars_prune(&s->vars_txn, s->sess, s);
7752 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7753 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007754}
7755
7756/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007757void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007758{
7759 http_end_txn(s);
7760 http_init_txn(s);
7761
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007762 /* reinitialise the current rule list pointer to NULL. We are sure that
7763 * any rulelist match the NULL pointer.
7764 */
7765 s->current_rule_list = NULL;
7766
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007767 s->be = strm_fe(s);
7768 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007769 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007770 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007771 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007772 /* re-init store persistence */
7773 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007774 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007775
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007776 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007777
Willy Tarreau739cfba2010-01-25 23:11:14 +01007778 /* We must trim any excess data from the response buffer, because we
7779 * may have blocked an invalid response from a server that we don't
Joseph Herlant5ba80252018-11-15 09:25:36 -08007780 * want to accidently forward once we disable the analysers, nor do
Willy Tarreau739cfba2010-01-25 23:11:14 +01007781 * we want those data to come along with next response. A typical
7782 * example of such data would be from a buggy server responding to
7783 * a HEAD with some data, or sending more than the advertised
7784 * content-length.
7785 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007786 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007787 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007788
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007789 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007790 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007791
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007792 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007793 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007794
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007795 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007796 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007797
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007798 s->req.rex = TICK_ETERNITY;
7799 s->req.wex = TICK_ETERNITY;
7800 s->req.analyse_exp = TICK_ETERNITY;
7801 s->res.rex = TICK_ETERNITY;
7802 s->res.wex = TICK_ETERNITY;
7803 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007804 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007805}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007806
Willy Tarreau79e57332018-10-02 16:01:16 +02007807/* This function executes one of the set-{method,path,query,uri} actions. It
7808 * takes the string from the variable 'replace' with length 'len', then modifies
7809 * the relevant part of the request line accordingly. Then it updates various
7810 * pointers to the next elements which were moved, and the total buffer length.
7811 * It finds the action to be performed in p[2], previously filled by function
7812 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7813 * error, though this can be revisited when this code is finally exploited.
7814 *
7815 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7816 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007817 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007818 * In query string case, the mark question '?' must be set at the start of the
7819 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007820 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007821int http_replace_req_line(int action, const char *replace, int len,
7822 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007823{
Willy Tarreau79e57332018-10-02 16:01:16 +02007824 struct http_txn *txn = s->txn;
7825 char *cur_ptr, *cur_end;
7826 int offset = 0;
7827 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007828
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007829 if (IS_HTX_STRM(s))
7830 return htx_req_replace_stline(action, replace, len, px, s);
7831
Willy Tarreau79e57332018-10-02 16:01:16 +02007832 switch (action) {
7833 case 0: // method
7834 cur_ptr = ci_head(&s->req);
7835 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007836
Willy Tarreau79e57332018-10-02 16:01:16 +02007837 /* adjust req line offsets and lengths */
7838 delta = len - offset - (cur_end - cur_ptr);
7839 txn->req.sl.rq.m_l += delta;
7840 txn->req.sl.rq.u += delta;
7841 txn->req.sl.rq.v += delta;
7842 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007843
Willy Tarreau79e57332018-10-02 16:01:16 +02007844 case 1: // path
7845 cur_ptr = http_txn_get_path(txn);
7846 if (!cur_ptr)
7847 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007848
Willy Tarreau79e57332018-10-02 16:01:16 +02007849 cur_end = cur_ptr;
7850 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7851 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007852
Willy Tarreau79e57332018-10-02 16:01:16 +02007853 /* adjust req line offsets and lengths */
7854 delta = len - offset - (cur_end - cur_ptr);
7855 txn->req.sl.rq.u_l += delta;
7856 txn->req.sl.rq.v += delta;
7857 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007858
Willy Tarreau79e57332018-10-02 16:01:16 +02007859 case 2: // query
7860 offset = 1;
7861 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7862 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7863 while (cur_ptr < cur_end && *cur_ptr != '?')
7864 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007865
Willy Tarreau79e57332018-10-02 16:01:16 +02007866 /* skip the question mark or indicate that we must insert it
7867 * (but only if the format string is not empty then).
7868 */
7869 if (cur_ptr < cur_end)
7870 cur_ptr++;
7871 else if (len > 1)
7872 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007873
Willy Tarreau79e57332018-10-02 16:01:16 +02007874 /* adjust req line offsets and lengths */
7875 delta = len - offset - (cur_end - cur_ptr);
7876 txn->req.sl.rq.u_l += delta;
7877 txn->req.sl.rq.v += delta;
7878 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007879
Willy Tarreau79e57332018-10-02 16:01:16 +02007880 case 3: // uri
7881 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7882 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007883
Willy Tarreau79e57332018-10-02 16:01:16 +02007884 /* adjust req line offsets and lengths */
7885 delta = len - offset - (cur_end - cur_ptr);
7886 txn->req.sl.rq.u_l += delta;
7887 txn->req.sl.rq.v += delta;
7888 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007889
Willy Tarreau79e57332018-10-02 16:01:16 +02007890 default:
7891 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007892 }
7893
Willy Tarreau79e57332018-10-02 16:01:16 +02007894 /* commit changes and adjust end of message */
7895 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7896 txn->req.sl.rq.l += delta;
7897 txn->hdr_idx.v[0].len += delta;
7898 http_msg_move_end(&txn->req, delta);
7899 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007900}
7901
Willy Tarreau79e57332018-10-02 16:01:16 +02007902/* This function replace the HTTP status code and the associated message. The
7903 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007904 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007905void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007906{
Willy Tarreau79e57332018-10-02 16:01:16 +02007907 struct http_txn *txn = s->txn;
7908 char *cur_ptr, *cur_end;
7909 int delta;
7910 char *res;
7911 int c_l;
7912 const char *msg = reason;
7913 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007914
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007915 if (IS_HTX_STRM(s))
7916 return htx_res_set_status(status, reason, s);
7917
Willy Tarreau79e57332018-10-02 16:01:16 +02007918 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007919
Willy Tarreau79e57332018-10-02 16:01:16 +02007920 res = ultoa_o(status, trash.area, trash.size);
7921 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007922
Willy Tarreau79e57332018-10-02 16:01:16 +02007923 trash.area[c_l] = ' ';
7924 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007925
Willy Tarreau79e57332018-10-02 16:01:16 +02007926 /* Do we have a custom reason format string? */
7927 if (msg == NULL)
7928 msg = http_get_reason(status);
7929 msg_len = strlen(msg);
7930 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7931 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007932
Willy Tarreau79e57332018-10-02 16:01:16 +02007933 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7934 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007935
Willy Tarreau79e57332018-10-02 16:01:16 +02007936 /* commit changes and adjust message */
7937 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7938 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007939
Willy Tarreau79e57332018-10-02 16:01:16 +02007940 /* adjust res line offsets and lengths */
7941 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7942 txn->rsp.sl.st.c_l = c_l;
7943 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007944
Willy Tarreau79e57332018-10-02 16:01:16 +02007945 delta = trash.data - (cur_end - cur_ptr);
7946 txn->rsp.sl.st.l += delta;
7947 txn->hdr_idx.v[0].len += delta;
7948 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007949}
7950
Willy Tarreau58f10d72006-12-04 02:26:12 +01007951/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007952 * Local variables:
7953 * c-indent-level: 8
7954 * c-basic-offset: 8
7955 * End:
7956 */