blob: e2f300950ddc47a2ac418b0289d5a107987bf1e9 [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
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100416 if (IS_HTX_STRM(s))
417 return htx_error_message(s);
418
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200419 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200420 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200421 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200422 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100423 else
424 return &http_err_chunks[msgnum];
425}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200426
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100427void
Willy Tarreau83061a82018-07-13 11:56:34 +0200428http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100429{
Christopher Faulet9768c262018-10-22 09:34:31 +0200430 if (IS_HTX_STRM(s))
431 return htx_reply_and_close(s, status, msg);
432
Christopher Fauletd7c91962015-04-30 11:48:27 +0200433 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100434 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100435 stream_int_retnclose(&s->si[0], msg);
436}
437
Willy Tarreau21d2af32008-02-14 20:25:24 +0100438/* Parse the URI from the given transaction (which is assumed to be in request
439 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
440 * It is returned otherwise.
441 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200442char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100443{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200444 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100445
Willy Tarreau9d9ccdb2018-10-28 20:13:12 +0100446 if (!txn->req.chn->buf.size)
447 return NULL;
448
Willy Tarreau6b952c82018-09-10 17:45:34 +0200449 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 +0100450
Willy Tarreau6b952c82018-09-10 17:45:34 +0200451 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100452}
453
Willy Tarreau71241ab2012-12-27 11:30:54 +0100454/* Returns a 302 for a redirectable request that reaches a server working in
455 * in redirect mode. This may only be called just after the stream interface
456 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
457 * follow normal proxy processing. NOTE: this function is designed to support
458 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100459 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200460void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100461{
462 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100463 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100464 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200465 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100466
Christopher Fauletfefc73d2018-10-24 21:18:04 +0200467 if (IS_HTX_STRM(s))
468 return htx_perform_server_redirect(s, si);
469
Willy Tarreauefb453c2008-10-26 20:49:47 +0100470 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200471 trash.data = strlen(HTTP_302);
472 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100473
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200474 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100475
Willy Tarreauefb453c2008-10-26 20:49:47 +0100476 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200477 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100478 return;
479
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100480 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100481 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200482 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
483 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100484 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100485
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200486 /* 3: add the request URI. Since it was already forwarded, we need
487 * to temporarily rewind the buffer.
488 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200489 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200490 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200491
Willy Tarreau6b952c82018-09-10 17:45:34 +0200492 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200493 len = b_dist(&s->req.buf, path, c_ptr(&s->req, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200494
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200495 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200496
Willy Tarreauefb453c2008-10-26 20:49:47 +0100497 if (!path)
498 return;
499
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100501 return;
502
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 memcpy(trash.area + trash.data, path, len);
504 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100505
506 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200507 memcpy(trash.area + trash.data,
508 "\r\nProxy-Connection: close\r\n\r\n", 29);
509 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100510 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200511 memcpy(trash.area + trash.data,
512 "\r\nConnection: close\r\n\r\n", 23);
513 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100514 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100515
516 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200517 si_shutr(si);
518 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100519 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100520 si->state = SI_ST_CLO;
521
522 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100523 txn->status = 302;
524 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100525
526 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100527 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500528 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100529}
530
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100531/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100532 * that the server side is closed. Note that err_type is actually a
533 * bitmask, where almost only aborts may be cumulated with other
534 * values. We consider that aborted operations are more important
535 * than timeouts or errors due to the fact that nobody else in the
536 * logs might explain incomplete retries. All others should avoid
537 * being cumulated. It should normally not be possible to have multiple
538 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100539 * Note that connection errors appearing on the second request of a keep-alive
540 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100541 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200542void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100543{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100544 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100545
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200546 /* set s->txn->status for http_error_message(s) */
547 s->txn->status = 503;
548
Willy Tarreauefb453c2008-10-26 20:49:47 +0100549 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200550 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100551 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100552 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200553 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100554 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200555 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100556 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200557 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100558 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100559 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200560 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100561 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100562 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200563 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100564 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200565 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100566 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200567 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100568 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200569 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200570 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200571 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100572 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200573 http_error_message(s));
574 else { /* SI_ET_CONN_OTHER and others */
575 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200576 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100577 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200578 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100579}
580
Willy Tarreau42250582007-04-01 01:30:43 +0200581extern const char sess_term_cond[8];
582extern const char sess_fin_state[8];
583extern const char *monthname[12];
Willy Tarreau8ceae722018-11-26 11:58:30 +0100584
585DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
586DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
587
588struct pool_head *pool_head_requri = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100589struct pool_head *pool_head_capture = NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100590
Willy Tarreau117f59e2007-03-04 18:17:17 +0100591/*
592 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200593 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100594 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200595void http_capture_headers(char *som, struct hdr_idx *idx,
596 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100597{
598 char *eol, *sol, *col, *sov;
599 int cur_idx;
600 struct cap_hdr *h;
601 int len;
602
603 sol = som + hdr_idx_first_pos(idx);
604 cur_idx = hdr_idx_first_idx(idx);
605
606 while (cur_idx) {
607 eol = sol + idx->v[cur_idx].len;
608
609 col = sol;
610 while (col < eol && *col != ':')
611 col++;
612
613 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100614 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100615 sov++;
616
617 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200618 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100619 (strncasecmp(sol, h->name, h->namelen) == 0)) {
620 if (cap[h->index] == NULL)
621 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100622 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100623
624 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100625 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100626 continue;
627 }
628
629 len = eol - sov;
630 if (len > h->len)
631 len = h->len;
632
633 memcpy(cap[h->index], sov, len);
634 cap[h->index][len]=0;
635 }
636 }
637 sol = eol + idx->v[cur_idx].cr + 1;
638 cur_idx = idx->v[cur_idx].next;
639 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100640}
641
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200642/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
643 * conversion succeeded, 0 in case of error. If the request was already 1.X,
644 * nothing is done and 1 is returned.
645 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200646int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200647{
648 int delta;
649 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100650 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200651
652 if (msg->sl.rq.v_l != 0)
653 return 1;
654
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300655 /* RFC 1945 allows only GET for HTTP/0.9 requests */
656 if (txn->meth != HTTP_METH_GET)
657 return 0;
658
Willy Tarreauf37954d2018-06-15 18:31:02 +0200659 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200660
661 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300662 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
663 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200664 }
665 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200666 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100667 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200668 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200669 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200670 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200671 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200672 NULL, NULL);
673 if (unlikely(!cur_end))
674 return 0;
675
676 /* we have a full HTTP/1.0 request now and we know that
677 * we have either a CR or an LF at <ptr>.
678 */
679 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
680 return 1;
681}
682
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100683/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100684 * and "keep-alive" values. If we already know that some headers may safely
685 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100686 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
687 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100688 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100689 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
690 * found, and TX_CON_*_SET is adjusted depending on what is left so only
691 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100692 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100693 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100694void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100695{
Willy Tarreau5b154472009-12-21 20:11:07 +0100696 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100697 const char *hdr_val = "Connection";
698 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100699
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100700 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100701 return;
702
Willy Tarreau88d349d2010-01-25 12:15:43 +0100703 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
704 hdr_val = "Proxy-Connection";
705 hdr_len = 16;
706 }
707
Willy Tarreau5b154472009-12-21 20:11:07 +0100708 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100709 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200710 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100711 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
712 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100713 if (to_del & 2)
714 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100715 else
716 txn->flags |= TX_CON_KAL_SET;
717 }
718 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
719 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100720 if (to_del & 1)
721 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100722 else
723 txn->flags |= TX_CON_CLO_SET;
724 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100725 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
726 txn->flags |= TX_HDR_CONN_UPG;
727 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100728 }
729
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100730 txn->flags |= TX_HDR_CONN_PRS;
731 return;
732}
Willy Tarreau5b154472009-12-21 20:11:07 +0100733
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100734/* Apply desired changes on the Connection: header. Values may be removed and/or
735 * added depending on the <wanted> flags, which are exclusively composed of
736 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
737 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
738 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100739void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100740{
741 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100742 const char *hdr_val = "Connection";
743 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100744
745 ctx.idx = 0;
746
Willy Tarreau88d349d2010-01-25 12:15:43 +0100747
748 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
749 hdr_val = "Proxy-Connection";
750 hdr_len = 16;
751 }
752
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100753 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200754 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100755 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
756 if (wanted & TX_CON_KAL_SET)
757 txn->flags |= TX_CON_KAL_SET;
758 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100759 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100760 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100761 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
762 if (wanted & TX_CON_CLO_SET)
763 txn->flags |= TX_CON_CLO_SET;
764 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100765 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100766 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100767 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100768
769 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
770 return;
771
772 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
773 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100774 hdr_val = "Connection: close";
775 hdr_len = 17;
776 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
777 hdr_val = "Proxy-Connection: close";
778 hdr_len = 23;
779 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100780 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100781 }
782
783 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
784 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100785 hdr_val = "Connection: keep-alive";
786 hdr_len = 22;
787 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
788 hdr_val = "Proxy-Connection: keep-alive";
789 hdr_len = 28;
790 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100791 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100792 }
793 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100794}
795
Willy Tarreau87b09662015-04-03 00:22:06 +0200796void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200797{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200798 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200799 int tmp = TX_CON_WANT_KAL;
800
Christopher Fauletf2824e62018-10-01 12:12:37 +0200801 if (IS_HTX_STRM(s))
Christopher Faulet0f226952018-10-22 09:29:56 +0200802 return htx_adjust_conn_mode(s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200803
Christopher Faulet315b39c2018-09-21 16:26:19 +0200804 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
805 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
806 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200807
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200808 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200809 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200810 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200811
Christopher Faulet315b39c2018-09-21 16:26:19 +0200812 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
813 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200814 tmp = TX_CON_WANT_CLO;
815
816 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
817 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
818
819 if (!(txn->flags & TX_HDR_CONN_PRS) &&
820 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
821 /* parse the Connection header and possibly clean it */
822 int to_del = 0;
823 if ((msg->flags & HTTP_MSGF_VER_11) ||
824 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200825 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200826 to_del |= 2; /* remove "keep-alive" */
827 if (!(msg->flags & HTTP_MSGF_VER_11))
828 to_del |= 1; /* remove "close" */
829 http_parse_connection_header(txn, msg, to_del);
830 }
831
832 /* check if client or config asks for explicit close in KAL/SCL */
833 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
834 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
835 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
836 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
837 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200838 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200839 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
840}
William Lallemand82fe75c2012-10-23 10:25:10 +0200841
Willy Tarreaud787e662009-07-07 10:14:51 +0200842/* This stream analyser waits for a complete HTTP request. It returns 1 if the
843 * processing can continue on next analysers, or zero if it either needs more
844 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100845 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200846 * when it has nothing left to do, and may remove any analyser when it wants to
847 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100848 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200849int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100850{
Willy Tarreau59234e92008-11-30 23:51:27 +0100851 /*
852 * We will parse the partial (or complete) lines.
853 * We will check the request syntax, and also join multi-line
854 * headers. An index of all the lines will be elaborated while
855 * parsing.
856 *
857 * For the parsing, we use a 28 states FSM.
858 *
859 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200860 * ci_head(req) = beginning of request
861 * ci_head(req) + msg->eoh = end of processed headers / start of current one
862 * ci_tail(req) = end of input data
863 * msg->eol = end of current header or line (LF or CRLF)
864 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200865 *
866 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200867 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200868 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
869 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100870 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100871
Willy Tarreau59234e92008-11-30 23:51:27 +0100872 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200873 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200874 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100875 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200876 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100877
Christopher Faulete0768eb2018-10-03 16:38:02 +0200878 if (IS_HTX_STRM(s))
879 return htx_wait_for_request(s, req, an_bit);
880
Christopher Faulet45073512018-07-20 10:16:29 +0200881 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 +0100882 now_ms, __FUNCTION__,
883 s,
884 req,
885 req->rex, req->wex,
886 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200887 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100888 req->analysers);
889
Willy Tarreau52a0c602009-08-16 22:45:38 +0200890 /* we're speaking HTTP here, so let's speak HTTP to the client */
891 s->srv_error = http_return_srv_error;
892
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300893 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200894 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300895 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
896
Willy Tarreau83e3af02009-12-28 17:39:57 +0100897 /* There's a protected area at the end of the buffer for rewriting
898 * purposes. We don't want to start to parse the request if the
899 * protected area is affected, because we may have to move processed
900 * data later, which is much more complicated.
901 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200902 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200903 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100904 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200905 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100906 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100907 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200908 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200909 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100910 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100911 return 0;
912 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200913 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200914 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200915 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100916 }
917
Willy Tarreauf37954d2018-06-15 18:31:02 +0200918 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100919 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100920 }
921
Willy Tarreau59234e92008-11-30 23:51:27 +0100922 /* 1: we might have to print this header in debug mode */
923 if (unlikely((global.mode & MODE_DEBUG) &&
924 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200925 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100926 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100927
Willy Tarreauf37954d2018-06-15 18:31:02 +0200928 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200929 /* this is a bit complex : in case of error on the request line,
930 * we know that rq.l is still zero, so we display only the part
931 * up to the end of the line (truncated by debug_hdr).
932 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200933 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100934 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100935
Willy Tarreau59234e92008-11-30 23:51:27 +0100936 sol += hdr_idx_first_pos(&txn->hdr_idx);
937 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100938
Willy Tarreau59234e92008-11-30 23:51:27 +0100939 while (cur_idx) {
940 eol = sol + txn->hdr_idx.v[cur_idx].len;
941 debug_hdr("clihdr", s, sol, eol);
942 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
943 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100944 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100945 }
946
Willy Tarreau58f10d72006-12-04 02:26:12 +0100947
Willy Tarreau59234e92008-11-30 23:51:27 +0100948 /*
949 * Now we quickly check if we have found a full valid request.
950 * If not so, we check the FD and buffer states before leaving.
951 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100952 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100953 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200954 * on a keep-alive stream, if we encounter and error, close, t/o,
955 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100956 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200957 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200958 * Last, we may increase some tracked counters' http request errors on
959 * the cases that are deliberately the client's fault. For instance,
960 * a timeout or connection reset is not counted as an error. However
961 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100962 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100963
Willy Tarreau655dce92009-11-08 13:10:58 +0100964 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100965 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100966 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100967 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100968 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200969 stream_inc_http_req_ctr(s);
970 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200971 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100972 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100973 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100974
Willy Tarreau59234e92008-11-30 23:51:27 +0100975 /* 1: Since we are in header mode, if there's no space
976 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200977 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100978 * must terminate it now.
979 */
Willy Tarreau23752332018-06-15 14:54:53 +0200980 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100981 /* FIXME: check if URI is set and return Status
982 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100983 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200984 stream_inc_http_req_ctr(s);
985 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200986 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200987 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200988 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100989 goto return_bad_req;
990 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100991
Willy Tarreau59234e92008-11-30 23:51:27 +0100992 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200993 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200994 if (!(s->flags & SF_ERR_MASK))
995 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100996
Willy Tarreaufcffa692010-01-10 14:21:19 +0100997 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100998 goto failed_keep_alive;
999
Willy Tarreau0f228a02015-05-01 15:37:53 +02001000 if (sess->fe->options & PR_O_IGNORE_PRB)
1001 goto failed_keep_alive;
1002
Willy Tarreau59234e92008-11-30 23:51:27 +01001003 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001004 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001005 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001006 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001007 }
1008
Willy Tarreaudc979f22012-12-04 10:39:01 +01001009 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001010 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001011 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001012 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001013 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001014 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001015 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001016 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001017 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001018 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001019
Willy Tarreaue7dff022015-04-03 01:14:29 +02001020 if (!(s->flags & SF_FINST_MASK))
1021 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001022 return 0;
1023 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001024
Willy Tarreau59234e92008-11-30 23:51:27 +01001025 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001026 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001027 if (!(s->flags & SF_ERR_MASK))
1028 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001029
Willy Tarreaufcffa692010-01-10 14:21:19 +01001030 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001031 goto failed_keep_alive;
1032
Willy Tarreau0f228a02015-05-01 15:37:53 +02001033 if (sess->fe->options & PR_O_IGNORE_PRB)
1034 goto failed_keep_alive;
1035
Willy Tarreau59234e92008-11-30 23:51:27 +01001036 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001037 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001038 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001039 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001040 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001041 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001042 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001043 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001044 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001045 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001046
Willy Tarreau87b09662015-04-03 00:22:06 +02001047 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001048 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001049 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001050 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001051 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001052
Willy Tarreaue7dff022015-04-03 01:14:29 +02001053 if (!(s->flags & SF_FINST_MASK))
1054 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001055 return 0;
1056 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001057
Willy Tarreau59234e92008-11-30 23:51:27 +01001058 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001059 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001060 if (!(s->flags & SF_ERR_MASK))
1061 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001062
Willy Tarreaufcffa692010-01-10 14:21:19 +01001063 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001064 goto failed_keep_alive;
1065
Willy Tarreau0f228a02015-05-01 15:37:53 +02001066 if (sess->fe->options & PR_O_IGNORE_PRB)
1067 goto failed_keep_alive;
1068
Willy Tarreau4076a152009-04-02 15:18:36 +02001069 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001070 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001071 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001072 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001073 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001074 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001075 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001076 stream_inc_http_err_ctr(s);
1077 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001078 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001079 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001080 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001081 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001082
Willy Tarreaue7dff022015-04-03 01:14:29 +02001083 if (!(s->flags & SF_FINST_MASK))
1084 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001085 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001086 }
1087
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001088 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001089 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001090 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001091#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001092 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001093 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001094 /* We need more data, we have to re-enable quick-ack in case we
1095 * previously disabled it, otherwise we might cause the client
1096 * to delay next data.
1097 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001098 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001099 }
1100#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001101
Willy Tarreaufcffa692010-01-10 14:21:19 +01001102 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1103 /* If the client starts to talk, let's fall back to
1104 * request timeout processing.
1105 */
1106 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001107 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001108 }
1109
Willy Tarreau59234e92008-11-30 23:51:27 +01001110 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001111 if (!tick_isset(req->analyse_exp)) {
1112 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1113 (txn->flags & TX_WAIT_NEXT_RQ) &&
1114 tick_isset(s->be->timeout.httpka))
1115 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1116 else
1117 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1118 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001119
Willy Tarreau59234e92008-11-30 23:51:27 +01001120 /* we're not ready yet */
1121 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001122
1123 failed_keep_alive:
1124 /* Here we process low-level errors for keep-alive requests. In
1125 * short, if the request is not the first one and it experiences
1126 * a timeout, read error or shutdown, we just silently close so
1127 * that the client can try again.
1128 */
1129 txn->status = 0;
1130 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001131 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001132 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001133 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001134 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001135 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001136 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001137 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001138
Willy Tarreaud787e662009-07-07 10:14:51 +02001139 /* OK now we have a complete HTTP request with indexed headers. Let's
1140 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001141 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001142 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001143 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001144 * byte after the last LF. msg->sov points to the first byte of data.
1145 * msg->eol cannot be trusted because it may have been left uninitialized
1146 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001147 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001148
Willy Tarreau87b09662015-04-03 00:22:06 +02001149 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001150 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001151
Willy Tarreaub16a5742010-01-10 14:46:16 +01001152 if (txn->flags & TX_WAIT_NEXT_RQ) {
1153 /* kill the pending keep-alive timeout */
1154 txn->flags &= ~TX_WAIT_NEXT_RQ;
1155 req->analyse_exp = TICK_ETERNITY;
1156 }
1157
1158
Willy Tarreaud787e662009-07-07 10:14:51 +02001159 /* Maybe we found in invalid header name while we were configured not
1160 * to block on that, so we have to capture it now.
1161 */
1162 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001163 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001164
Willy Tarreau59234e92008-11-30 23:51:27 +01001165 /*
1166 * 1: identify the method
1167 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001168 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001169
1170 /* we can make use of server redirect on GET and HEAD */
1171 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001172 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001173 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001174 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001175 /* PRI is reserved for the HTTP/2 preface */
1176 msg->err_pos = 0;
1177 goto return_bad_req;
1178 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001179
Willy Tarreau59234e92008-11-30 23:51:27 +01001180 /*
1181 * 2: check if the URI matches the monitor_uri.
1182 * We have to do this for every request which gets in, because
1183 * the monitor-uri is defined by the frontend.
1184 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001185 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1186 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001187 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001188 sess->fe->monitor_uri,
1189 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001190 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001191 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001192 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001193 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001194
Willy Tarreaue7dff022015-04-03 01:14:29 +02001195 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001196 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001197
Willy Tarreau59234e92008-11-30 23:51:27 +01001198 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001199 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001200 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001201
Willy Tarreau59234e92008-11-30 23:51:27 +01001202 ret = acl_pass(ret);
1203 if (cond->pol == ACL_COND_UNLESS)
1204 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001205
Willy Tarreau59234e92008-11-30 23:51:27 +01001206 if (ret) {
1207 /* we fail this request, let's return 503 service unavail */
1208 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001209 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001210 if (!(s->flags & SF_ERR_MASK))
1211 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001212 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001213 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001214 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001215
Joseph Herlant5ba80252018-11-15 09:25:36 -08001216 /* nothing to fail, let's reply normally */
Willy Tarreau59234e92008-11-30 23:51:27 +01001217 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001218 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001219 if (!(s->flags & SF_ERR_MASK))
1220 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001221 goto return_prx_cond;
1222 }
1223
1224 /*
1225 * 3: Maybe we have to copy the original REQURI for the logs ?
1226 * Note: we cannot log anymore if the request has been
1227 * classified as invalid.
1228 */
1229 if (unlikely(s->logs.logwait & LW_REQ)) {
1230 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001231 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001232 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001233
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001234 if (urilen >= global.tune.requri_len )
1235 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001236 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001237 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001238
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001239 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001240 s->do_log(s);
1241 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001242 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001243 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001244 }
Willy Tarreau06619262006-12-17 08:37:22 +01001245
Willy Tarreau91852eb2015-05-01 13:26:00 +02001246 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1247 * exactly one digit "." one digit. This check may be disabled using
1248 * option accept-invalid-http-request.
1249 */
1250 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1251 if (msg->sl.rq.v_l != 8) {
1252 msg->err_pos = msg->sl.rq.v;
1253 goto return_bad_req;
1254 }
1255
Willy Tarreauf37954d2018-06-15 18:31:02 +02001256 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1257 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1258 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1259 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001260 msg->err_pos = msg->sl.rq.v + 4;
1261 goto return_bad_req;
1262 }
1263 }
Willy Tarreau13317662015-05-01 13:47:08 +02001264 else {
1265 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1266 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1267 goto return_bad_req;
1268 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001269
Willy Tarreau5b154472009-12-21 20:11:07 +01001270 /* ... and check if the request is HTTP/1.1 or above */
1271 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001272 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1273 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1274 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001275 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001276
1277 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001278 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 +01001279
Willy Tarreau88d349d2010-01-25 12:15:43 +01001280 /* if the frontend has "option http-use-proxy-header", we'll check if
1281 * we have what looks like a proxied connection instead of a connection,
1282 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1283 * Note that this is *not* RFC-compliant, however browsers and proxies
1284 * happen to do that despite being non-standard :-(
1285 * We consider that a request not beginning with either '/' or '*' is
1286 * a proxied connection, which covers both "scheme://location" and
1287 * CONNECT ip:port.
1288 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001289 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001290 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001291 txn->flags |= TX_USE_PX_CONN;
1292
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001293 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001294 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001295
Willy Tarreau59234e92008-11-30 23:51:27 +01001296 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001297 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001298 http_capture_headers(ci_head(req), &txn->hdr_idx,
1299 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001300
Willy Tarreau557f1992015-05-01 10:05:17 +02001301 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1302 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001303 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001304 * The length of a message body is determined by one of the following
1305 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001306 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001307 * 1. Any response to a HEAD request and any response with a 1xx
1308 * (Informational), 204 (No Content), or 304 (Not Modified) status
1309 * code is always terminated by the first empty line after the
1310 * header fields, regardless of the header fields present in the
1311 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001312 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001313 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1314 * the connection will become a tunnel immediately after the empty
1315 * line that concludes the header fields. A client MUST ignore any
1316 * Content-Length or Transfer-Encoding header fields received in
1317 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001318 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001319 * 3. If a Transfer-Encoding header field is present and the chunked
1320 * transfer coding (Section 4.1) is the final encoding, the message
1321 * body length is determined by reading and decoding the chunked
1322 * data until the transfer coding indicates the data is complete.
1323 *
1324 * If a Transfer-Encoding header field is present in a response and
1325 * the chunked transfer coding is not the final encoding, the
1326 * message body length is determined by reading the connection until
1327 * it is closed by the server. If a Transfer-Encoding header field
1328 * is present in a request and the chunked transfer coding is not
1329 * the final encoding, the message body length cannot be determined
1330 * reliably; the server MUST respond with the 400 (Bad Request)
1331 * status code and then close the connection.
1332 *
1333 * If a message is received with both a Transfer-Encoding and a
1334 * Content-Length header field, the Transfer-Encoding overrides the
1335 * Content-Length. Such a message might indicate an attempt to
1336 * perform request smuggling (Section 9.5) or response splitting
1337 * (Section 9.4) and ought to be handled as an error. A sender MUST
1338 * remove the received Content-Length field prior to forwarding such
1339 * a message downstream.
1340 *
1341 * 4. If a message is received without Transfer-Encoding and with
1342 * either multiple Content-Length header fields having differing
1343 * field-values or a single Content-Length header field having an
1344 * invalid value, then the message framing is invalid and the
1345 * recipient MUST treat it as an unrecoverable error. If this is a
1346 * request message, the server MUST respond with a 400 (Bad Request)
1347 * status code and then close the connection. If this is a response
1348 * message received by a proxy, the proxy MUST close the connection
1349 * to the server, discard the received response, and send a 502 (Bad
1350 * Gateway) response to the client. If this is a response message
1351 * received by a user agent, the user agent MUST close the
1352 * connection to the server and discard the received response.
1353 *
1354 * 5. If a valid Content-Length header field is present without
1355 * Transfer-Encoding, its decimal value defines the expected message
1356 * body length in octets. If the sender closes the connection or
1357 * the recipient times out before the indicated number of octets are
1358 * received, the recipient MUST consider the message to be
1359 * incomplete and close the connection.
1360 *
1361 * 6. If this is a request message and none of the above are true, then
1362 * the message body length is zero (no message body is present).
1363 *
1364 * 7. Otherwise, this is a response message without a declared message
1365 * body length, so the message body length is determined by the
1366 * number of octets received prior to the server closing the
1367 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001368 */
1369
Willy Tarreau32b47f42009-10-18 20:55:02 +02001370 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001371 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001372 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001373 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001374 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001375 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001376 /* chunked not last, return badreq */
1377 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001378 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001379 }
1380
Willy Tarreau1c913912015-04-30 10:57:51 +02001381 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001382 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001383 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001384 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001385 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1386 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001387 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001388 signed long long cl;
1389
Willy Tarreauad14f752011-09-02 20:33:27 +02001390 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001391 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001392 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001393 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001394
Willy Tarreauad14f752011-09-02 20:33:27 +02001395 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001396 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001397 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001398 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001399
Willy Tarreauad14f752011-09-02 20:33:27 +02001400 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001401 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001402 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001403 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001404
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001405 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001406 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001407 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001408 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001409
Christopher Fauletbe821b92017-03-30 11:21:53 +02001410 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001411 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001412 }
1413
Willy Tarreau34dfc602015-05-01 10:09:49 +02001414 /* even bodyless requests have a known length */
1415 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001416
Willy Tarreau179085c2014-04-28 16:48:56 +02001417 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1418 * only change if both the request and the config reference something else.
1419 * Option httpclose by itself sets tunnel mode where headers are mangled.
1420 * However, if another mode is set, it will affect it (eg: server-close/
1421 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1422 * if FE and BE have the same settings (common). The method consists in
1423 * checking if options changed between the two calls (implying that either
1424 * one is non-null, or one of them is non-null and we are there for the first
1425 * time.
1426 */
1427 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001428 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001429 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001430
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001431 /* we may have to wait for the request's body */
1432 if ((s->be->options & PR_O_WREQ_BODY) &&
1433 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1434 req->analysers |= AN_REQ_HTTP_BODY;
1435
Willy Tarreau83ece462017-12-21 15:13:09 +01001436 /*
1437 * RFC7234#4:
1438 * A cache MUST write through requests with methods
1439 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1440 * the origin server; i.e., a cache is not allowed
1441 * to generate a reply to such a request before
1442 * having forwarded the request and having received
1443 * a corresponding response.
1444 *
1445 * RFC7231#4.2.1:
1446 * Of the request methods defined by this
1447 * specification, the GET, HEAD, OPTIONS, and TRACE
1448 * methods are defined to be safe.
1449 */
1450 if (likely(txn->meth == HTTP_METH_GET ||
1451 txn->meth == HTTP_METH_HEAD ||
1452 txn->meth == HTTP_METH_OPTIONS ||
1453 txn->meth == HTTP_METH_TRACE))
1454 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1455
Willy Tarreaud787e662009-07-07 10:14:51 +02001456 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001457 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001458 req->analyse_exp = TICK_ETERNITY;
1459 return 1;
1460
1461 return_bad_req:
1462 /* We centralize bad requests processing here */
1463 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1464 /* we detected a parsing error. We want to archive this request
1465 * in the dedicated proxy area for later troubleshooting.
1466 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001467 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001468 }
1469
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001470 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001471 txn->req.msg_state = HTTP_MSG_ERROR;
1472 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001473 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001474
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001475 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001476 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001477 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001478
1479 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001480 if (!(s->flags & SF_ERR_MASK))
1481 s->flags |= SF_ERR_PRXCOND;
1482 if (!(s->flags & SF_FINST_MASK))
1483 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001484
Christopher Faulet0184ea72017-01-05 14:06:34 +01001485 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001486 req->analyse_exp = TICK_ETERNITY;
1487 return 0;
1488}
1489
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001490
Willy Tarreau347a35d2013-11-22 17:51:09 +01001491/* This function prepares an applet to handle the stats. It can deal with the
1492 * "100-continue" expectation, check that admin rules are met for POST requests,
1493 * and program a response message if something was unexpected. It cannot fail
1494 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001495 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001496 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001497 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001498 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001499int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001500{
1501 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001502 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001503 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001504 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001505 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001506 struct uri_auth *uri_auth = s->be->uri_auth;
1507 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001508 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001509
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001510 appctx = si_appctx(si);
1511 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1512 appctx->st1 = appctx->st2 = 0;
1513 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1514 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001515 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001516 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001517
Willy Tarreauf37954d2018-06-15 18:31:02 +02001518 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001519 lookup = uri + uri_auth->uri_len;
1520
1521 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1522 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001523 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001524 break;
1525 }
1526 }
1527
1528 if (uri_auth->refresh) {
1529 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1530 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001531 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001532 break;
1533 }
1534 }
1535 }
1536
1537 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1538 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001539 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001540 break;
1541 }
1542 }
1543
Willy Tarreau1e62df92016-01-11 18:57:53 +01001544 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1545 if (memcmp(h, ";typed", 6) == 0) {
1546 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1547 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1548 break;
1549 }
1550 }
1551
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001552 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1553 if (memcmp(h, ";st=", 4) == 0) {
1554 int i;
1555 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001556 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001557 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1558 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001559 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001560 break;
1561 }
1562 }
1563 break;
1564 }
1565 }
1566
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001567 appctx->ctx.stats.scope_str = 0;
1568 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001569 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1570 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1571 int itx = 0;
1572 const char *h2;
1573 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1574 const char *err;
1575
1576 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1577 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001578 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001579 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1580 itx++;
1581 h++;
1582 }
1583
1584 if (itx > STAT_SCOPE_TXT_MAXLEN)
1585 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001586 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001587
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001588 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001589 memcpy(scope_txt, h2, itx);
1590 scope_txt[itx] = '\0';
1591 err = invalid_char(scope_txt);
1592 if (err) {
1593 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001594 appctx->ctx.stats.scope_str = 0;
1595 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001596 }
1597 break;
1598 }
1599 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001600
1601 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001602 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001603 int ret = 1;
1604
1605 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001606 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 +01001607 ret = acl_pass(ret);
1608 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1609 ret = !ret;
1610 }
1611
1612 if (ret) {
1613 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001614 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001615 break;
1616 }
1617 }
1618
1619 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001620 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001621 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001622 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001623 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1624 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001625 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001626 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001627 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001628 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1629 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001630 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001631 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001632 else {
1633 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001634 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001635 }
1636
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001637 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001638 return 1;
1639}
1640
Lukas Tribus67db8df2013-06-23 17:37:13 +02001641/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1642 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1643 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001644void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001645{
1646#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001647 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001648 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1649#endif
1650#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001651 if (from->ss_family == AF_INET6) {
1652 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001653 /* v4-mapped addresses need IP_TOS */
1654 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1655 else
1656 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1657 }
1658#endif
1659}
1660
Willy Tarreau87b09662015-04-03 00:22:06 +02001661int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001662 const char* name, unsigned int name_len,
1663 const char *str, struct my_regex *re,
1664 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001665{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001666 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001667 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001668 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001669 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1670 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001671 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001672
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001673 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001674
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001675 /* Choose the header browsing function. */
1676 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001677 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001678 http_find_hdr_func = http_find_header2;
1679 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001680 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001681 http_find_hdr_func = http_find_full_header2;
1682 break;
1683 default: /* impossible */
1684 return -1;
1685 }
1686
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001687 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1688 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001689 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001690 char *val = ctx.line + ctx.val;
1691 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001692
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001693 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1694 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001695
Willy Tarreau6e27be12018-08-22 04:46:47 +02001696 len = exp_replace(output->area, output->size, val, str, pmatch);
1697 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001698 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001699
Willy Tarreau6e27be12018-08-22 04:46:47 +02001700 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001701
1702 hdr->len += delta;
1703 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001704
1705 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001706 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001707 }
1708
1709 return 0;
1710}
1711
Willy Tarreau87b09662015-04-03 00:22:06 +02001712static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001713 const char* name, unsigned int name_len,
1714 struct list *fmt, struct my_regex *re,
1715 int action)
1716{
Willy Tarreau83061a82018-07-13 11:56:34 +02001717 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001718 int ret = -1;
1719
1720 replace = alloc_trash_chunk();
1721 if (!replace)
1722 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001723
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001724 replace->data = build_logline(s, replace->area, replace->size, fmt);
1725 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001726 goto leave;
1727
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001728 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1729 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001730
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001731 leave:
1732 free_trash_chunk(replace);
1733 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001734}
1735
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001736/*
1737 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1738 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001739 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1740 * line is inserted before the header. If an error occurred <early_hints> is
1741 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001742 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001743static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1744 const char* name, unsigned int name_len,
1745 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001746{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001747 if (!early_hints) {
1748 early_hints = alloc_trash_chunk();
1749 if (!early_hints)
1750 goto fail;
1751 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1752 goto fail;
1753 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001754
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001755 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1756 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001757
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001758 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1759 if (!chunk_memcat(early_hints, "\r\n", 2))
1760 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001761
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001762 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001763
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001764 fail:
1765 free_trash_chunk(early_hints);
1766 return NULL;
1767}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001768
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001769/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1770 * response is added. If an error occurred or if another response was already
1771 * sent, this function does nothing.
1772 */
1773static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1774{
1775 struct channel *chn = s->txn->rsp.chn;
1776 char *cur_ptr = ci_head(chn);
1777 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001778
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001779 /* If a response was already sent, skip early hints */
1780 if (s->txn->status > 0)
1781 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001782
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001783 if (!chunk_memcat(early_hints, "\r\n", 2))
1784 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001785
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001786 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1787 c_adv(chn, ret);
1788 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001789}
1790
Willy Tarreau87b09662015-04-03 00:22:06 +02001791/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001792 * transaction <txn>. Returns the verdict of the first rule that prevents
1793 * further processing of the request (auth, deny, ...), and defaults to
1794 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1795 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001796 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1797 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1798 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001799 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001800enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001801http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001802{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001803 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001804 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001805 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001806 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001807 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001808 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001809 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001810 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001811 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001812 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001813
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001814 /* If "the current_rule_list" match the executed rule list, we are in
1815 * resume condition. If a resume is needed it is always in the action
1816 * and never in the ACL or converters. In this case, we initialise the
1817 * current rule, and go to the action execution point.
1818 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001819 if (s->current_rule) {
1820 rule = s->current_rule;
1821 s->current_rule = NULL;
1822 if (s->current_rule_list == rules)
1823 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001824 }
1825 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001826
Willy Tarreauff011f22011-01-06 17:51:27 +01001827 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001828
Willy Tarreau96257ec2012-12-27 10:46:37 +01001829 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001830 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001831 int ret;
1832
Willy Tarreau192252e2015-04-04 01:47:55 +02001833 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001834 ret = acl_pass(ret);
1835
Willy Tarreauff011f22011-01-06 17:51:27 +01001836 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001837 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001838
1839 if (!ret) /* condition not matched */
1840 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001841 }
1842
Willy Tarreauacc98002015-09-27 23:34:39 +02001843 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001844resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001845 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001846 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001847 rule_ret = HTTP_RULE_RES_STOP;
1848 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001849
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001850 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001851 if (deny_status)
1852 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001853 rule_ret = HTTP_RULE_RES_DENY;
1854 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001855
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001856 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001857 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001858 if (deny_status)
1859 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001860 rule_ret = HTTP_RULE_RES_DENY;
1861 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001862
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001863 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001864 /* Be sure to send any pending HTTP 103 response first */
1865 if (early_hints) {
1866 http_send_early_hints(s, early_hints);
1867 free_trash_chunk(early_hints);
1868 early_hints = NULL;
1869 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001870 /* Auth might be performed on regular http-req rules as well as on stats */
1871 auth_realm = rule->arg.auth.realm;
1872 if (!auth_realm) {
1873 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1874 auth_realm = STATS_DEFAULT_REALM;
1875 else
1876 auth_realm = px->id;
1877 }
1878 /* send 401/407 depending on whether we use a proxy or not. We still
1879 * count one error, because normal browsing won't significantly
1880 * increase the counter but brute force attempts will.
1881 */
1882 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1883 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001884 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001885 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001886 rule_ret = HTTP_RULE_RES_ABRT;
1887 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001888
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001889 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001890 /* Be sure to send any pending HTTP 103 response first */
1891 if (early_hints) {
1892 http_send_early_hints(s, early_hints);
1893 free_trash_chunk(early_hints);
1894 early_hints = NULL;
1895 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001896 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001897 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001898 rule_ret = HTTP_RULE_RES_BADREQ;
1899 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001900
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001901 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001902 s->task->nice = rule->arg.nice;
1903 break;
1904
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001905 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001906 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001907 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001908 break;
1909
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001910 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001911#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001912 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001913 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001914#endif
1915 break;
1916
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001917 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001918 s->logs.level = rule->arg.loglevel;
1919 break;
1920
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001921 case ACT_HTTP_REPLACE_HDR:
1922 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001923 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1924 rule->arg.hdr_add.name_len,
1925 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001926 &rule->arg.hdr_add.re, rule->action)) {
1927 rule_ret = HTTP_RULE_RES_BADREQ;
1928 goto end;
1929 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001930 break;
1931
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001932 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001933 ctx.idx = 0;
1934 /* remove all occurrences of the header */
1935 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001936 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001937 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001938 }
Willy Tarreau85603282015-01-21 20:39:27 +01001939 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001940
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001941 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001942 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001943 /* The scope of the trash buffer must be limited to this function. The
1944 * build_logline() function can execute a lot of other function which
1945 * can use the trash buffer. So for limiting the scope of this global
1946 * buffer, we build first the header value using build_logline, and
1947 * after we store the header name.
1948 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001949 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001950
1951 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001952 if (!replace) {
1953 rule_ret = HTTP_RULE_RES_BADREQ;
1954 goto end;
1955 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001956
Thierry Fournier4b788f72016-06-01 13:35:36 +02001957 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001958 len += build_logline(s, replace->area + len,
1959 replace->size - len,
1960 &rule->arg.hdr_add.fmt);
1961 memcpy(replace->area, rule->arg.hdr_add.name,
1962 rule->arg.hdr_add.name_len);
1963 replace->area[rule->arg.hdr_add.name_len] = ':';
1964 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1965 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001966
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001967 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001968 /* remove all occurrences of the header */
1969 ctx.idx = 0;
1970 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001971 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001972 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1973 }
1974 }
1975
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001976 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001977 static unsigned char rate_limit = 0;
1978
1979 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001980 replace->area[rule->arg.hdr_add.name_len] = 0;
1981 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,
1982 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001983 }
1984
1985 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1986 if (sess->fe != s->be)
1987 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1988 if (sess->listener->counters)
1989 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1990 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001991
1992 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001993 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001994 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001995
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001996 case ACT_HTTP_DEL_ACL:
1997 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001998 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001999 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002000
2001 /* collect reference */
2002 ref = pat_ref_lookup(rule->arg.map.ref);
2003 if (!ref)
2004 continue;
2005
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002006 /* allocate key */
2007 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002008 if (!key) {
2009 rule_ret = HTTP_RULE_RES_BADREQ;
2010 goto end;
2011 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002012
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002013 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002014 key->data = build_logline(s, key->area, key->size,
2015 &rule->arg.map.key);
2016 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002017
2018 /* perform update */
2019 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002020 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002021 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002022 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002023
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002024 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002025 break;
2026 }
2027
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002028 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002029 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002030 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002031
2032 /* collect reference */
2033 ref = pat_ref_lookup(rule->arg.map.ref);
2034 if (!ref)
2035 continue;
2036
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002037 /* allocate key */
2038 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002039 if (!key) {
2040 rule_ret = HTTP_RULE_RES_BADREQ;
2041 goto end;
2042 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002043
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002044 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002045 key->data = build_logline(s, key->area, key->size,
2046 &rule->arg.map.key);
2047 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002048
2049 /* perform update */
2050 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002051 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002052 if (pat_ref_find_elt(ref, key->area) == NULL)
2053 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002054 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002055
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002056 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002057 break;
2058 }
2059
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002060 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002061 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002062 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002063
2064 /* collect reference */
2065 ref = pat_ref_lookup(rule->arg.map.ref);
2066 if (!ref)
2067 continue;
2068
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002069 /* allocate key */
2070 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002071 if (!key) {
2072 rule_ret = HTTP_RULE_RES_BADREQ;
2073 goto end;
2074 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002075
2076 /* allocate value */
2077 value = alloc_trash_chunk();
2078 if (!value) {
2079 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01002080 rule_ret = HTTP_RULE_RES_BADREQ;
2081 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002082 }
2083
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002084 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002085 key->data = build_logline(s, key->area, key->size,
2086 &rule->arg.map.key);
2087 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002088
2089 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002090 value->data = build_logline(s, value->area,
2091 value->size,
2092 &rule->arg.map.value);
2093 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002094
2095 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002096 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002097 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002098 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002099 else
2100 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002101 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002102
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002103 free_trash_chunk(key);
2104 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002105 break;
2106 }
William Lallemand73025dd2014-04-24 14:38:37 +02002107
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002108 case ACT_HTTP_EARLY_HINT:
2109 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2110 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002111 early_hints = http_apply_early_hint_rule(s, early_hints,
2112 rule->arg.early_hint.name,
2113 rule->arg.early_hint.name_len,
2114 &rule->arg.early_hint.fmt);
2115 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01002116 rule_ret = HTTP_RULE_RES_DONE;
2117 goto end;
2118 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002119 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002120 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002121 if ((s->req.flags & CF_READ_ERROR) ||
2122 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2123 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2124 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002125 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002126
Willy Tarreauacc98002015-09-27 23:34:39 +02002127 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002128 case ACT_RET_ERR:
2129 case ACT_RET_CONT:
2130 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002131 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01002132 rule_ret = HTTP_RULE_RES_DONE;
2133 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002134 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002135 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01002136 rule_ret = HTTP_RULE_RES_YIELD;
2137 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002138 }
William Lallemand73025dd2014-04-24 14:38:37 +02002139 break;
2140
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002141 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002142 /* Note: only the first valid tracking parameter of each
2143 * applies.
2144 */
2145
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002146 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002147 struct stktable *t;
2148 struct stksess *ts;
2149 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002150 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002151
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002152 t = rule->arg.trk_ctr.table.t;
2153 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 +02002154
2155 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002156 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002157
2158 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002159 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2160 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2161 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002162 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002163
Emeric Brun819fc6f2017-06-13 19:37:32 +02002164 if (ptr1)
2165 stktable_data_cast(ptr1, http_req_cnt)++;
2166
2167 if (ptr2)
2168 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2169 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2170
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002171 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002172
2173 /* If data was modified, we need to touch to re-schedule sync */
2174 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002175 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002176
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002177 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002178 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002179 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002180 }
2181 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002182 break;
2183
Joseph Herlant5ba80252018-11-15 09:25:36 -08002184 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002185 default:
2186 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002187 }
2188 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002189
Christopher Fauletea827bd2018-11-15 15:34:11 +01002190 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002191 if (early_hints) {
2192 http_send_early_hints(s, early_hints);
2193 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002194 }
2195
Willy Tarreau96257ec2012-12-27 10:46:37 +01002196 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01002197 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002198}
2199
Willy Tarreau71241ab2012-12-27 11:30:54 +01002200
Willy Tarreau51d861a2015-05-22 17:30:48 +02002201/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2202 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2203 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2204 * is returned, the process can continue the evaluation of next rule list. If
2205 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2206 * is returned, it means the operation could not be processed and a server error
2207 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2208 * deny rule. If *YIELD is returned, the caller must call again the function
2209 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002210 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002211enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002212http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002213{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002214 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002215 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002216 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002217 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002218 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002219 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02002220 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002221
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002222 /* If "the current_rule_list" match the executed rule list, we are in
2223 * resume condition. If a resume is needed it is always in the action
2224 * and never in the ACL or converters. In this case, we initialise the
2225 * current rule, and go to the action execution point.
2226 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002227 if (s->current_rule) {
2228 rule = s->current_rule;
2229 s->current_rule = NULL;
2230 if (s->current_rule_list == rules)
2231 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002232 }
2233 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002234
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002235 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002236
2237 /* check optional condition */
2238 if (rule->cond) {
2239 int ret;
2240
Willy Tarreau192252e2015-04-04 01:47:55 +02002241 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002242 ret = acl_pass(ret);
2243
2244 if (rule->cond->pol == ACL_COND_UNLESS)
2245 ret = !ret;
2246
2247 if (!ret) /* condition not matched */
2248 continue;
2249 }
2250
Willy Tarreauacc98002015-09-27 23:34:39 +02002251 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002252resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002253 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002254 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002255 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2256 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002257
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002258 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002259 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002260 rule_ret = HTTP_RULE_RES_STOP;
2261 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002262
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002263 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002264 s->task->nice = rule->arg.nice;
2265 break;
2266
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002267 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002268 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002269 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002270 break;
2271
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002272 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002273#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002274 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002275 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002276#endif
2277 break;
2278
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002279 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002280 s->logs.level = rule->arg.loglevel;
2281 break;
2282
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002283 case ACT_HTTP_REPLACE_HDR:
2284 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002285 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2286 rule->arg.hdr_add.name_len,
2287 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002288 &rule->arg.hdr_add.re, rule->action)) {
2289 rule_ret = HTTP_RULE_RES_BADREQ;
2290 goto end;
2291 }
Sasha Pachev218f0642014-06-16 12:05:59 -06002292 break;
2293
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002294 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002295 ctx.idx = 0;
2296 /* remove all occurrences of the header */
2297 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002298 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002299 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2300 }
Willy Tarreau85603282015-01-21 20:39:27 +01002301 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002302
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002303 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002304 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002305 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002306
2307 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002308 if (!replace) {
2309 rule_ret = HTTP_RULE_RES_BADREQ;
2310 goto end;
2311 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002312
2313 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002314 memcpy(replace->area, rule->arg.hdr_add.name,
2315 rule->arg.hdr_add.name_len);
2316 replace->data = rule->arg.hdr_add.name_len;
2317 replace->area[replace->data++] = ':';
2318 replace->area[replace->data++] = ' ';
2319 replace->data += build_logline(s,
2320 replace->area + replace->data,
2321 replace->size - replace->data,
2322 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002323
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002324 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002325 /* remove all occurrences of the header */
2326 ctx.idx = 0;
2327 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002328 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002329 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2330 }
2331 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002332
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002333 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002334 static unsigned char rate_limit = 0;
2335
2336 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002337 replace->area[rule->arg.hdr_add.name_len] = 0;
2338 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,
2339 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002340 }
2341
2342 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2343 if (sess->fe != s->be)
2344 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2345 if (sess->listener->counters)
2346 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2347 if (objt_server(s->target))
2348 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2349 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002350
2351 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002352 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002353 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002354
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002355 case ACT_HTTP_DEL_ACL:
2356 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002357 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002358 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002359
2360 /* collect reference */
2361 ref = pat_ref_lookup(rule->arg.map.ref);
2362 if (!ref)
2363 continue;
2364
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002365 /* allocate key */
2366 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002367 if (!key) {
2368 rule_ret = HTTP_RULE_RES_BADREQ;
2369 goto end;
2370 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002371
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002372 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002373 key->data = build_logline(s, key->area, key->size,
2374 &rule->arg.map.key);
2375 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002376
2377 /* perform update */
2378 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002379 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002380 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002381 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002382
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002383 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002384 break;
2385 }
2386
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002387 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002388 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002389 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002390
2391 /* collect reference */
2392 ref = pat_ref_lookup(rule->arg.map.ref);
2393 if (!ref)
2394 continue;
2395
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002396 /* allocate key */
2397 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002398 if (!key) {
2399 rule_ret = HTTP_RULE_RES_BADREQ;
2400 goto end;
2401 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002402
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002403 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002404 key->data = build_logline(s, key->area, key->size,
2405 &rule->arg.map.key);
2406 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002407
2408 /* perform update */
2409 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002410 if (pat_ref_find_elt(ref, key->area) == NULL)
2411 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002412
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002413 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002414 break;
2415 }
2416
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002417 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002418 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002419 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002420
2421 /* collect reference */
2422 ref = pat_ref_lookup(rule->arg.map.ref);
2423 if (!ref)
2424 continue;
2425
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002426 /* allocate key */
2427 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002428 if (!key) {
2429 rule_ret = HTTP_RULE_RES_BADREQ;
2430 goto end;
2431 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002432
2433 /* allocate value */
2434 value = alloc_trash_chunk();
2435 if (!value) {
2436 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002437 rule_ret = HTTP_RULE_RES_BADREQ;
2438 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002439 }
2440
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002441 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002442 key->data = build_logline(s, key->area, key->size,
2443 &rule->arg.map.key);
2444 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002445
2446 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002447 value->data = build_logline(s, value->area,
2448 value->size,
2449 &rule->arg.map.value);
2450 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002451
2452 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002453 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002454 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002455 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002456 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002457 else
2458 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002459 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002460 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002461 free_trash_chunk(key);
2462 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002463 break;
2464 }
William Lallemand73025dd2014-04-24 14:38:37 +02002465
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002466 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002467 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002468 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002469 rule_ret = HTTP_RULE_RES_BADREQ;
2470 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002471
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002472 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2473 /* Note: only the first valid tracking parameter of each
2474 * applies.
2475 */
2476
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002477 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002478 struct stktable *t;
2479 struct stksess *ts;
2480 struct stktable_key *key;
2481 void *ptr;
2482
2483 t = rule->arg.trk_ctr.table.t;
2484 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2485
2486 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002487 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002488
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002489 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002490
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002491 /* let's count a new HTTP request as it's the first time we do it */
2492 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2493 if (ptr)
2494 stktable_data_cast(ptr, http_req_cnt)++;
2495
2496 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2497 if (ptr)
2498 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2499 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2500
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002501 /* When the client triggers a 4xx from the server, it's most often due
2502 * to a missing object or permission. These events should be tracked
2503 * because if they happen often, it may indicate a brute force or a
2504 * vulnerability scan. Normally this is done when receiving the response
2505 * but here we're tracking after this ought to have been done so we have
2506 * to do it on purpose.
2507 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002508 if ((unsigned)(txn->status - 400) < 100) {
2509 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2510 if (ptr)
2511 stktable_data_cast(ptr, http_err_cnt)++;
2512
2513 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2514 if (ptr)
2515 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2516 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2517 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002518
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002519 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002520
Emeric Brun0fed0b02017-11-29 16:15:07 +01002521 /* If data was modified, we need to touch to re-schedule sync */
2522 stktable_touch_local(t, ts, 0);
2523
Emeric Brun819fc6f2017-06-13 19:37:32 +02002524 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2525 if (sess->fe != s->be)
2526 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2527
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002528 }
2529 }
2530 break;
2531
Thierry FOURNIER42148732015-09-02 17:17:33 +02002532 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002533 if ((s->req.flags & CF_READ_ERROR) ||
2534 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2535 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2536 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002537 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002538
Willy Tarreauacc98002015-09-27 23:34:39 +02002539 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002540 case ACT_RET_ERR:
2541 case ACT_RET_CONT:
2542 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002543 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002544 rule_ret = HTTP_RULE_RES_STOP;
2545 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002546 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002547 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002548 rule_ret = HTTP_RULE_RES_YIELD;
2549 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002550 }
William Lallemand73025dd2014-04-24 14:38:37 +02002551 break;
2552
Joseph Herlant5ba80252018-11-15 09:25:36 -08002553 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002554 default:
2555 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002556 }
2557 }
2558
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002559 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002560 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002561 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002562}
2563
2564
Willy Tarreau71241ab2012-12-27 11:30:54 +01002565/* Perform an HTTP redirect based on the information in <rule>. The function
2566 * returns non-zero on success, or zero in case of a, irrecoverable error such
2567 * as too large a request to build a valid response.
2568 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002569int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002570{
Willy Tarreaub329a312015-05-22 16:27:37 +02002571 struct http_msg *req = &txn->req;
2572 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002573 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002574 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002575 int ret = 0;
2576
Christopher Fauletf2824e62018-10-01 12:12:37 +02002577 if (IS_HTX_STRM(s))
2578 return htx_apply_redirect_rule(rule, s, txn);
2579
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002580 chunk = alloc_trash_chunk();
2581 if (!chunk)
2582 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002583
2584 /* build redirect message */
2585 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002586 case 308:
2587 msg_fmt = HTTP_308;
2588 break;
2589 case 307:
2590 msg_fmt = HTTP_307;
2591 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002592 case 303:
2593 msg_fmt = HTTP_303;
2594 break;
2595 case 301:
2596 msg_fmt = HTTP_301;
2597 break;
2598 case 302:
2599 default:
2600 msg_fmt = HTTP_302;
2601 break;
2602 }
2603
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002604 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2605 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002606
2607 switch(rule->type) {
2608 case REDIRECT_TYPE_SCHEME: {
2609 const char *path;
2610 const char *host;
2611 struct hdr_ctx ctx;
2612 int pathlen;
2613 int hostlen;
2614
2615 host = "";
2616 hostlen = 0;
2617 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002618 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002619 host = ctx.line + ctx.val;
2620 hostlen = ctx.vlen;
2621 }
2622
Willy Tarreau6b952c82018-09-10 17:45:34 +02002623 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002624 /* build message using path */
2625 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002626 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002627 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2628 int qs = 0;
2629 while (qs < pathlen) {
2630 if (path[qs] == '?') {
2631 pathlen = qs;
2632 break;
2633 }
2634 qs++;
2635 }
2636 }
2637 } else {
2638 path = "/";
2639 pathlen = 1;
2640 }
2641
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002642 if (rule->rdr_str) { /* this is an old "redirect" rule */
2643 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002644 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002645 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002646
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002647 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002648 memcpy(chunk->area + chunk->data, rule->rdr_str,
2649 rule->rdr_len);
2650 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002651 }
2652 else {
2653 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002654 chunk->data += build_logline(s,
2655 chunk->area + chunk->data,
2656 chunk->size - chunk->data,
2657 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002658
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002659 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002660 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002661 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002662 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002663 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002664 memcpy(chunk->area + chunk->data, "://", 3);
2665 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002666
2667 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002668 memcpy(chunk->area + chunk->data, host, hostlen);
2669 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002670
2671 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002672 memcpy(chunk->area + chunk->data, path, pathlen);
2673 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002674
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002675 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002676 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002677 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002678 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002679 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002680 chunk->area[chunk->data] = '/';
2681 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002682 }
2683
2684 break;
2685 }
2686 case REDIRECT_TYPE_PREFIX: {
2687 const char *path;
2688 int pathlen;
2689
Willy Tarreau6b952c82018-09-10 17:45:34 +02002690 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002691 /* build message using path */
2692 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002693 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002694 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2695 int qs = 0;
2696 while (qs < pathlen) {
2697 if (path[qs] == '?') {
2698 pathlen = qs;
2699 break;
2700 }
2701 qs++;
2702 }
2703 }
2704 } else {
2705 path = "/";
2706 pathlen = 1;
2707 }
2708
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002709 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002710 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002711 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002712
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002713 /* add prefix. Note that if prefix == "/", we don't want to
2714 * add anything, otherwise it makes it hard for the user to
2715 * configure a self-redirection.
2716 */
2717 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002718 memcpy(chunk->area + chunk->data,
2719 rule->rdr_str, rule->rdr_len);
2720 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002721 }
2722 }
2723 else {
2724 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002725 chunk->data += build_logline(s,
2726 chunk->area + chunk->data,
2727 chunk->size - chunk->data,
2728 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002729
2730 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002731 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002732 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002733 }
2734
2735 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002736 memcpy(chunk->area + chunk->data, path, pathlen);
2737 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002738
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002739 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002740 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002741 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002742 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002743 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002744 chunk->area[chunk->data] = '/';
2745 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002746 }
2747
2748 break;
2749 }
2750 case REDIRECT_TYPE_LOCATION:
2751 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002752 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002753 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002754 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002755
2756 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002757 memcpy(chunk->area + chunk->data, rule->rdr_str,
2758 rule->rdr_len);
2759 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002760 }
2761 else {
2762 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002763 chunk->data += build_logline(s,
2764 chunk->area + chunk->data,
2765 chunk->size - chunk->data,
2766 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002767
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002768 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002769 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002770 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002771 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002772 break;
2773 }
2774
2775 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002776 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2777 chunk->data += 14;
2778 memcpy(chunk->area + chunk->data, rule->cookie_str,
2779 rule->cookie_len);
2780 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002781 }
2782
Willy Tarreau19b14122017-02-28 09:48:11 +01002783 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002784 txn->status = rule->code;
2785 /* let's log the request time */
2786 s->logs.tv_request = now;
2787
Christopher Fauletbe821b92017-03-30 11:21:53 +02002788 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002789 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2790 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2791 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002792 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002793 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002794 memcpy(chunk->area + chunk->data,
2795 "\r\nProxy-Connection: keep-alive", 30);
2796 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002797 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002798 memcpy(chunk->area + chunk->data,
2799 "\r\nConnection: keep-alive", 24);
2800 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002801 }
2802 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002803 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2804 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002805 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002806 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002807 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002808 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002809 req->next -= req->sov;
2810 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002811 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002812 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002813 req->msg_state = HTTP_MSG_CLOSED;
2814 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002815 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002816 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002817 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002818 /* let the server side turn to SI_ST_CLO */
2819 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002820 } else {
2821 /* keep-alive not possible */
2822 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002823 memcpy(chunk->area + chunk->data,
2824 "\r\nProxy-Connection: close\r\n\r\n", 29);
2825 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002826 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002827 memcpy(chunk->area + chunk->data,
2828 "\r\nConnection: close\r\n\r\n", 23);
2829 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002830 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002831 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002832 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002833 }
2834
Willy Tarreaue7dff022015-04-03 01:14:29 +02002835 if (!(s->flags & SF_ERR_MASK))
2836 s->flags |= SF_ERR_LOCAL;
2837 if (!(s->flags & SF_FINST_MASK))
2838 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002839
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002840 ret = 1;
2841 leave:
2842 free_trash_chunk(chunk);
2843 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002844}
2845
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002846/* This stream analyser runs all HTTP request processing which is common to
2847 * frontends and backends, which means blocking ACLs, filters, connection-close,
2848 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002849 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002850 * either needs more data or wants to immediately abort the request (eg: deny,
2851 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002852 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002853int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002854{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002855 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002856 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002857 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002858 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002859 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002860 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002861 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002862 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002863
Christopher Faulete0768eb2018-10-03 16:38:02 +02002864 if (IS_HTX_STRM(s))
2865 return htx_process_req_common(s, req, an_bit, px);
2866
Willy Tarreau655dce92009-11-08 13:10:58 +01002867 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002868 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002869 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002870 }
2871
Christopher Faulet45073512018-07-20 10:16:29 +02002872 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 +02002873 now_ms, __FUNCTION__,
2874 s,
2875 req,
2876 req->rex, req->wex,
2877 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002878 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002879 req->analysers);
2880
Willy Tarreau65410832014-04-28 21:25:43 +02002881 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002882 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002883
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002884 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002885 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002886 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002887
Willy Tarreau0b748332014-04-29 00:13:29 +02002888 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002889 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2890 goto return_prx_yield;
2891
Willy Tarreau0b748332014-04-29 00:13:29 +02002892 case HTTP_RULE_RES_CONT:
2893 case HTTP_RULE_RES_STOP: /* nothing to do */
2894 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002895
Willy Tarreau0b748332014-04-29 00:13:29 +02002896 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2897 if (txn->flags & TX_CLTARPIT)
2898 goto tarpit;
2899 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002900
Willy Tarreau0b748332014-04-29 00:13:29 +02002901 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2902 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002903
Willy Tarreau0b748332014-04-29 00:13:29 +02002904 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002905 goto done;
2906
Willy Tarreau0b748332014-04-29 00:13:29 +02002907 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2908 goto return_bad_req;
2909 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002910 }
2911
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002912 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2913 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002914 struct hdr_ctx ctx;
2915
2916 ctx.idx = 0;
2917 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002918 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002919 if (unlikely(http_header_add_tail2(&txn->req,
2920 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002921 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002922 goto return_bad_req;
2923 }
2924 }
2925
2926 }
2927
Willy Tarreau52542592014-04-28 18:33:26 +02002928 /* OK at this stage, we know that the request was accepted according to
2929 * the http-request rules, we can check for the stats. Note that the
2930 * URI is detected *before* the req* rules in order not to be affected
2931 * by a possible reqrep, while they are processed *after* so that a
2932 * reqdeny can still block them. This clearly needs to change in 1.6!
2933 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002934 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002935 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002936 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002937 txn->status = 500;
2938 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002939 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002940
Willy Tarreaue7dff022015-04-03 01:14:29 +02002941 if (!(s->flags & SF_ERR_MASK))
2942 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002943 goto return_prx_cond;
2944 }
2945
2946 /* parse the whole stats request and extract the relevant information */
2947 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002948 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002949 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002950
Willy Tarreau0b748332014-04-29 00:13:29 +02002951 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2952 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002953
Willy Tarreau0b748332014-04-29 00:13:29 +02002954 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2955 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002956 }
2957
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002958 /* evaluate the req* rules except reqadd */
2959 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002960 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002961 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002962
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002963 if (txn->flags & TX_CLDENY)
2964 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002965
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002966 if (txn->flags & TX_CLTARPIT) {
2967 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002968 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002969 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002970 }
Willy Tarreau06619262006-12-17 08:37:22 +01002971
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002972 /* add request headers from the rule sets in the same order */
2973 list_for_each_entry(wl, &px->req_add, list) {
2974 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002975 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002976 ret = acl_pass(ret);
2977 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2978 ret = !ret;
2979 if (!ret)
2980 continue;
2981 }
2982
Christopher Faulet10079f52018-10-03 15:17:28 +02002983 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002984 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002985 }
2986
Willy Tarreau52542592014-04-28 18:33:26 +02002987
2988 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002989 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2990 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002991 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002992 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002993 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002994
Willy Tarreaue7dff022015-04-03 01:14:29 +02002995 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2996 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2997 if (!(s->flags & SF_FINST_MASK))
2998 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002999
Willy Tarreau70730dd2014-04-24 18:06:27 +02003000 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003001 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3002 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003003 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003004 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003005 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003006
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003007 /* check whether we have some ACLs set to redirect this request */
3008 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003009 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003010 int ret;
3011
Willy Tarreau192252e2015-04-04 01:47:55 +02003012 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003013 ret = acl_pass(ret);
3014 if (rule->cond->pol == ACL_COND_UNLESS)
3015 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003016 if (!ret)
3017 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003018 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003019 if (!http_apply_redirect_rule(rule, s, txn))
3020 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003021 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003022 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003023
Willy Tarreau2be39392010-01-03 17:24:51 +01003024 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3025 * If this happens, then the data will not come immediately, so we must
3026 * send all what we have without waiting. Note that due to the small gain
3027 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003028 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003029 * itself once used.
3030 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003031 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003032
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003033 done: /* done with this analyser, continue with next ones that the calling
3034 * points will have set, if any.
3035 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003036 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003037 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3038 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003039 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003040
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003041 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003042 /* Allow cookie logging
3043 */
3044 if (s->be->cookie_name || sess->fe->capture_name)
3045 manage_client_side_cookies(s, req);
3046
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003047 /* When a connection is tarpitted, we use the tarpit timeout,
3048 * which may be the same as the connect timeout if unspecified.
3049 * If unset, then set it to zero because we really want it to
3050 * eventually expire. We build the tarpit as an analyser.
3051 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003052 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003053
3054 /* wipe the request out so that we can drop the connection early
3055 * if the client closes first.
3056 */
3057 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003058
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003059 txn->status = http_err_codes[deny_status];
3060
Christopher Faulet0184ea72017-01-05 14:06:34 +01003061 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003062 req->analysers |= AN_REQ_HTTP_TARPIT;
3063 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3064 if (!req->analyse_exp)
3065 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003066 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003067 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003068 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003069 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003070 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003071 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003072 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003073
3074 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003075
3076 /* Allow cookie logging
3077 */
3078 if (s->be->cookie_name || sess->fe->capture_name)
3079 manage_client_side_cookies(s, req);
3080
Willy Tarreau0b748332014-04-29 00:13:29 +02003081 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003082 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003083 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003084 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003085 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003086 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003087 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003088 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003089 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003090 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003091 goto return_prx_cond;
3092
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003093 return_bad_req:
3094 /* We centralize bad requests processing here */
3095 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3096 /* we detected a parsing error. We want to archive this request
3097 * in the dedicated proxy area for later troubleshooting.
3098 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003099 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003100 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003101
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003102 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003103 txn->req.msg_state = HTTP_MSG_ERROR;
3104 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003105 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003106
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003107 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003108 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003109 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003110
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003111 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003112 if (!(s->flags & SF_ERR_MASK))
3113 s->flags |= SF_ERR_PRXCOND;
3114 if (!(s->flags & SF_FINST_MASK))
3115 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003116
Christopher Faulet0184ea72017-01-05 14:06:34 +01003117 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003118 req->analyse_exp = TICK_ETERNITY;
3119 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003120
3121 return_prx_yield:
3122 channel_dont_connect(req);
3123 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003124}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003125
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003126/* This function performs all the processing enabled for the current request.
3127 * It returns 1 if the processing can continue on next analysers, or zero if it
3128 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003129 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003130 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003131int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003132{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003133 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003134 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003135 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003136 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003137
Christopher Faulete0768eb2018-10-03 16:38:02 +02003138 if (IS_HTX_STRM(s))
3139 return htx_process_request(s, req, an_bit);
3140
Willy Tarreau655dce92009-11-08 13:10:58 +01003141 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003142 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003143 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003144 return 0;
3145 }
3146
Christopher Faulet45073512018-07-20 10:16:29 +02003147 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 +02003148 now_ms, __FUNCTION__,
3149 s,
3150 req,
3151 req->rex, req->wex,
3152 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003153 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003154 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003155
Willy Tarreau59234e92008-11-30 23:51:27 +01003156 /*
3157 * Right now, we know that we have processed the entire headers
3158 * and that unwanted requests have been filtered out. We can do
3159 * whatever we want with the remaining request. Also, now we
3160 * may have separate values for ->fe, ->be.
3161 */
Willy Tarreau06619262006-12-17 08:37:22 +01003162
Willy Tarreau59234e92008-11-30 23:51:27 +01003163 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003164 * If HTTP PROXY is set we simply get remote server address parsing
3165 * incoming request. Note that this requires that a connection is
3166 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003167 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003168 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003169 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003170 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003171
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003172 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003173 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003174 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003175 txn->req.msg_state = HTTP_MSG_ERROR;
3176 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003177 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003178 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003179
Willy Tarreaue7dff022015-04-03 01:14:29 +02003180 if (!(s->flags & SF_ERR_MASK))
3181 s->flags |= SF_ERR_RESOURCE;
3182 if (!(s->flags & SF_FINST_MASK))
3183 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003184
3185 return 0;
3186 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003187
Willy Tarreau6b952c82018-09-10 17:45:34 +02003188 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003189 if (url2sa(ci_head(req) + msg->sl.rq.u,
3190 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003191 &conn->addr.to, NULL) == -1)
3192 goto return_bad_req;
3193
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003194 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003195 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3196 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003197 * u_l characters by a single "/".
3198 */
3199 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003200 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003201 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3202 int delta;
3203
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003204 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003205 http_msg_move_end(&txn->req, delta);
3206 cur_end += delta;
3207 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3208 goto return_bad_req;
3209 }
3210 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003211 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003212 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3213 int delta;
3214
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003215 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003216 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003217 http_msg_move_end(&txn->req, delta);
3218 cur_end += delta;
3219 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3220 goto return_bad_req;
3221 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003222 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003223
Willy Tarreau59234e92008-11-30 23:51:27 +01003224 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003225 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003226 * Note that doing so might move headers in the request, but
3227 * the fields will stay coherent and the URI will not move.
3228 * This should only be performed in the backend.
3229 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003230 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003231 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003232
William Lallemanda73203e2012-03-12 12:48:57 +01003233 /* add unique-id if "header-unique-id" is specified */
3234
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003235 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003236 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003237 goto return_bad_req;
3238 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003239 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003240 }
William Lallemanda73203e2012-03-12 12:48:57 +01003241
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003242 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003243 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003244 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003245 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003246 goto return_bad_req;
3247 }
3248
Cyril Bontéb21570a2009-11-29 20:04:48 +01003249 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003250 * 9: add X-Forwarded-For if either the frontend or the backend
3251 * asks for it.
3252 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003253 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003254 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003255 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3256 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3257 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003258 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003259 /* The header is set to be added only if none is present
3260 * and we found it, so don't do anything.
3261 */
3262 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003263 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003264 /* Add an X-Forwarded-For header unless the source IP is
3265 * in the 'except' network range.
3266 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003267 if ((!sess->fe->except_mask.s_addr ||
3268 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3269 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003270 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003271 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003272 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003273 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003274 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003275 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003276
3277 /* Note: we rely on the backend to get the header name to be used for
3278 * x-forwarded-for, because the header is really meant for the backends.
3279 * However, if the backend did not specify any option, we have to rely
3280 * on the frontend's header name.
3281 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003282 if (s->be->fwdfor_hdr_len) {
3283 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003284 memcpy(trash.area,
3285 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003286 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003287 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003288 memcpy(trash.area,
3289 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003290 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003291 len += snprintf(trash.area + len,
3292 trash.size - len,
3293 ": %d.%d.%d.%d", pn[0], pn[1],
3294 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003295
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003296 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003297 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003298 }
3299 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003300 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003301 /* FIXME: for the sake of completeness, we should also support
3302 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003303 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003304 int len;
3305 char pn[INET6_ADDRSTRLEN];
3306 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003307 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003308 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003309
Willy Tarreau59234e92008-11-30 23:51:27 +01003310 /* Note: we rely on the backend to get the header name to be used for
3311 * x-forwarded-for, because the header is really meant for the backends.
3312 * However, if the backend did not specify any option, we have to rely
3313 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003314 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003315 if (s->be->fwdfor_hdr_len) {
3316 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003317 memcpy(trash.area, s->be->fwdfor_hdr_name,
3318 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003319 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003320 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003321 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3322 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003323 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003324 len += snprintf(trash.area + len, trash.size - len,
3325 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003326
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003327 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003328 goto return_bad_req;
3329 }
3330 }
3331
3332 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003333 * 10: add X-Original-To if either the frontend or the backend
3334 * asks for it.
3335 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003336 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003337
3338 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003339 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003340 /* Add an X-Original-To header unless the destination IP is
3341 * in the 'except' network range.
3342 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003343 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003344
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003345 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003346 ((!sess->fe->except_mask_to.s_addr ||
3347 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3348 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003349 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003350 (((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 +02003351 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003352 int len;
3353 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003354 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003355
3356 /* Note: we rely on the backend to get the header name to be used for
3357 * x-original-to, because the header is really meant for the backends.
3358 * However, if the backend did not specify any option, we have to rely
3359 * on the frontend's header name.
3360 */
3361 if (s->be->orgto_hdr_len) {
3362 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003363 memcpy(trash.area,
3364 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003365 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003366 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003367 memcpy(trash.area,
3368 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003369 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003370 len += snprintf(trash.area + len,
3371 trash.size - len,
3372 ": %d.%d.%d.%d", pn[0], pn[1],
3373 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003374
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003375 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003376 goto return_bad_req;
3377 }
3378 }
3379 }
3380
Willy Tarreau50fc7772012-11-11 22:19:57 +01003381 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3382 * If an "Upgrade" token is found, the header is left untouched in order not to have
3383 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3384 * "Upgrade" is present in the Connection header.
3385 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003386 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003387 unsigned int want_flags = 0;
3388
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003389 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003390 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003391 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003392 want_flags |= TX_CON_CLO_SET;
3393 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003394 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003395 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003396 want_flags |= TX_CON_KAL_SET;
3397 }
3398
3399 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003400 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003401 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003402
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003403
Willy Tarreau522d6c02009-12-06 18:49:18 +01003404 /* If we have no server assigned yet and we're balancing on url_param
3405 * with a POST request, we may be interested in checking the body for
3406 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003407 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003408 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003409 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003410 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003411 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003412 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003413 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003414
Christopher Fauletbe821b92017-03-30 11:21:53 +02003415 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3416 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003417#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003418 /* We expect some data from the client. Unless we know for sure
3419 * we already have a full request, we have to re-enable quick-ack
3420 * in case we previously disabled it, otherwise we might cause
3421 * the client to delay further data.
3422 */
3423 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3424 cli_conn && conn_ctrl_ready(cli_conn) &&
3425 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003426 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003427 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003428#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003429
Willy Tarreau59234e92008-11-30 23:51:27 +01003430 /*************************************************************
3431 * OK, that's finished for the headers. We have done what we *
3432 * could. Let's switch to the DATA state. *
3433 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003434 req->analyse_exp = TICK_ETERNITY;
3435 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003436
Willy Tarreau59234e92008-11-30 23:51:27 +01003437 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003438 /* OK let's go on with the BODY now */
3439 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003440
Willy Tarreau59234e92008-11-30 23:51:27 +01003441 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003442 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003443 /* we detected a parsing error. We want to archive this request
3444 * in the dedicated proxy area for later troubleshooting.
3445 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003446 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003447 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003448
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003449 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003450 txn->req.msg_state = HTTP_MSG_ERROR;
3451 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003452 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003453 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003454
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003455 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003456 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003457 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003458
Willy Tarreaue7dff022015-04-03 01:14:29 +02003459 if (!(s->flags & SF_ERR_MASK))
3460 s->flags |= SF_ERR_PRXCOND;
3461 if (!(s->flags & SF_FINST_MASK))
3462 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003463 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003464}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003465
Willy Tarreau60b85b02008-11-30 23:28:40 +01003466/* This function is an analyser which processes the HTTP tarpit. It always
3467 * returns zero, at the beginning because it prevents any other processing
3468 * from occurring, and at the end because it terminates the request.
3469 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003470int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003471{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003472 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003473
Christopher Faulete0768eb2018-10-03 16:38:02 +02003474 if (IS_HTX_STRM(s))
3475 return htx_process_tarpit(s, req, an_bit);
3476
Willy Tarreau60b85b02008-11-30 23:28:40 +01003477 /* This connection is being tarpitted. The CLIENT side has
3478 * already set the connect expiration date to the right
3479 * timeout. We just have to check that the client is still
3480 * there and that the timeout has not expired.
3481 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003482 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003483 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003484 !tick_is_expired(req->analyse_exp, now_ms))
3485 return 0;
3486
3487 /* We will set the queue timer to the time spent, just for
3488 * logging purposes. We fake a 500 server error, so that the
3489 * attacker will not suspect his connection has been tarpitted.
3490 * It will not cause trouble to the logs because we can exclude
3491 * the tarpitted connections by filtering on the 'PT' status flags.
3492 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003493 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3494
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003495 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003496 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003497
Christopher Faulet0184ea72017-01-05 14:06:34 +01003498 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003499 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003500
Willy Tarreaue7dff022015-04-03 01:14:29 +02003501 if (!(s->flags & SF_ERR_MASK))
3502 s->flags |= SF_ERR_PRXCOND;
3503 if (!(s->flags & SF_FINST_MASK))
3504 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003505 return 0;
3506}
3507
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003508/* This function is an analyser which waits for the HTTP request body. It waits
3509 * for either the buffer to be full, or the full advertised contents to have
3510 * reached the buffer. It must only be called after the standard HTTP request
3511 * processing has occurred, because it expects the request to be parsed and will
3512 * look for the Expect header. It may send a 100-Continue interim response. It
3513 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3514 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3515 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003516 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003517int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003518{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003519 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003520 struct http_txn *txn = s->txn;
3521 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003522
Christopher Faulete0768eb2018-10-03 16:38:02 +02003523 if (IS_HTX_STRM(s))
3524 return htx_wait_for_request_body(s, req, an_bit);
3525
Willy Tarreaud34af782008-11-30 23:36:37 +01003526 /* We have to parse the HTTP request body to find any required data.
3527 * "balance url_param check_post" should have been the only way to get
3528 * into this. We were brought here after HTTP header analysis, so all
3529 * related structures are ready.
3530 */
3531
Willy Tarreau890988f2014-04-10 11:59:33 +02003532 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3533 /* This is the first call */
3534 if (msg->msg_state < HTTP_MSG_BODY)
3535 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003536
Willy Tarreau890988f2014-04-10 11:59:33 +02003537 if (msg->msg_state < HTTP_MSG_100_SENT) {
3538 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3539 * send an HTTP/1.1 100 Continue intermediate response.
3540 */
3541 if (msg->flags & HTTP_MSGF_VER_11) {
3542 struct hdr_ctx ctx;
3543 ctx.idx = 0;
3544 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003545 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003546 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003547 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003548 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003549 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003550 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003551 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003552 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003553
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003554 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003555 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003556 * must save the body in msg->next because it survives buffer
3557 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003558 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003559 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003560
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003561 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003562 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3563 else
3564 msg->msg_state = HTTP_MSG_DATA;
3565 }
3566
Willy Tarreau890988f2014-04-10 11:59:33 +02003567 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3568 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003569 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003570 goto missing_data;
3571
3572 /* OK we have everything we need now */
3573 goto http_end;
3574 }
3575
3576 /* OK here we're parsing a chunked-encoded message */
3577
Willy Tarreau522d6c02009-12-06 18:49:18 +01003578 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003579 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003580 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003581 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003582 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003583 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003584 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003585
Willy Tarreau115acb92009-12-26 13:56:06 +01003586 if (!ret)
3587 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003588 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003589 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003590 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003591 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003592 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003593 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003594 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003595
3596 msg->chunk_len = chunk;
3597 msg->body_len += chunk;
3598
3599 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003600 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003601 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003602 }
3603
Willy Tarreaud98cf932009-12-27 22:54:55 +01003604 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003605 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3606 * for at least a whole chunk or the whole content length bytes after
3607 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003608 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003609 if (msg->msg_state == HTTP_MSG_TRAILERS)
3610 goto http_end;
3611
Willy Tarreaue115b492015-05-01 23:05:14 +02003612 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003613 goto http_end;
3614
3615 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003616 /* we get here if we need to wait for more data. If the buffer is full,
3617 * we have the maximum we can expect.
3618 */
Willy Tarreau23752332018-06-15 14:54:53 +02003619 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003620 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003621
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003622 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003623 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003624 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003625
Willy Tarreaue7dff022015-04-03 01:14:29 +02003626 if (!(s->flags & SF_ERR_MASK))
3627 s->flags |= SF_ERR_CLITO;
3628 if (!(s->flags & SF_FINST_MASK))
3629 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003630 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003631 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003632
3633 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003634 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003635 /* Not enough data. We'll re-use the http-request
3636 * timeout here. Ideally, we should set the timeout
3637 * relative to the accept() date. We just set the
3638 * request timeout once at the beginning of the
3639 * request.
3640 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003641 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003642 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003643 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003644 return 0;
3645 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003646
3647 http_end:
3648 /* The situation will not evolve, so let's give up on the analysis. */
3649 s->logs.tv_request = now; /* update the request timer to reflect full request */
3650 req->analysers &= ~an_bit;
3651 req->analyse_exp = TICK_ETERNITY;
3652 return 1;
3653
3654 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003655 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003656 txn->req.msg_state = HTTP_MSG_ERROR;
3657 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003658 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003659
Willy Tarreaue7dff022015-04-03 01:14:29 +02003660 if (!(s->flags & SF_ERR_MASK))
3661 s->flags |= SF_ERR_PRXCOND;
3662 if (!(s->flags & SF_FINST_MASK))
3663 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003664
Willy Tarreau522d6c02009-12-06 18:49:18 +01003665 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003666 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003667 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003668 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003669 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003670 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003671}
3672
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003673/* send a server's name with an outgoing request over an established connection.
3674 * Note: this function is designed to be called once the request has been scheduled
3675 * for being forwarded. This is the reason why it rewinds the buffer before
3676 * proceeding.
3677 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003678int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003679
3680 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003681 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003682 char *hdr_name = be->server_id_hdr_name;
3683 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003684 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003685 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003686 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003687
Christopher Faulet64159df2018-10-24 21:15:35 +02003688 if (IS_HTX_STRM(s))
3689 return htx_send_name_header(s, be, srv_name);
William Lallemandd9e90662012-01-30 17:27:17 +01003690 ctx.idx = 0;
3691
Willy Tarreau211cdec2014-04-17 20:18:08 +02003692 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003693 if (old_o) {
3694 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003695 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003696 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003697 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003698 }
3699
Willy Tarreauf37954d2018-06-15 18:31:02 +02003700 old_i = ci_data(chn);
3701 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 -05003702 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003703 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003704 }
3705
3706 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003707 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003708 memcpy(hdr_val, hdr_name, hdr_name_len);
3709 hdr_val += hdr_name_len;
3710 *hdr_val++ = ':';
3711 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003712 hdr_val += strlcpy2(hdr_val, srv_name,
3713 trash.area + trash.size - hdr_val);
3714 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3715 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003716
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003717 if (old_o) {
3718 /* If this was a forwarded request, we must readjust the amount of
3719 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003720 * variations. Note that the current state is >= HTTP_MSG_BODY,
3721 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003722 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003723 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003724 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003725 txn->req.next -= old_o;
3726 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003727 }
3728
Mark Lamourinec2247f02012-01-04 13:02:01 -05003729 return 0;
3730}
3731
Willy Tarreau610ecce2010-01-04 21:15:02 +01003732/* Terminate current transaction and prepare a new one. This is very tricky
3733 * right now but it works.
3734 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003735void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003736{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003737 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003738 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003739 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003740 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003741 struct connection *srv_conn;
3742 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003743 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003744
Willy Tarreau610ecce2010-01-04 21:15:02 +01003745 /* FIXME: We need a more portable way of releasing a backend's and a
3746 * server's connections. We need a safer way to reinitialize buffer
3747 * flags. We also need a more accurate method for computing per-request
3748 * data.
3749 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003750 cs = objt_cs(s->si[1].end);
3751 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003752
Willy Tarreau4213a112013-12-15 10:25:42 +01003753 /* unless we're doing keep-alive, we want to quickly close the connection
3754 * to the server.
3755 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003756 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003757 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003758 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3759 si_shutr(&s->si[1]);
3760 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003761 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003762
Willy Tarreaue7dff022015-04-03 01:14:29 +02003763 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003764 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003765 if (unlikely(s->srv_conn))
3766 sess_change_server(s, NULL);
3767 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003768
3769 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003770 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003771
Willy Tarreaueee5b512015-04-03 23:46:31 +02003772 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003773 int n;
3774
Willy Tarreaueee5b512015-04-03 23:46:31 +02003775 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003776 if (n < 1 || n > 5)
3777 n = 0;
3778
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003779 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003780 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003781 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003782 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003783 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003784 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3785 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003786 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003787 }
3788
3789 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003790 s->logs.bytes_in -= ci_data(&s->req);
3791 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003792
Willy Tarreau66425e32018-07-25 06:55:12 +02003793 /* we may need to know the position in the queue */
3794 pendconn_free(s);
3795
Willy Tarreau610ecce2010-01-04 21:15:02 +01003796 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003797 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003798 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003799 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003800 s->do_log(s);
3801 }
3802
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003803 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003804 stream_stop_content_counters(s);
3805 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003806
Willy Tarreau9efd7452018-05-31 14:48:54 +02003807 /* reset the profiling counter */
3808 s->task->calls = 0;
3809 s->task->cpu_time = 0;
3810 s->task->lat_time = 0;
3811 s->task->call_date = (profiling & HA_PROF_TASKS) ? now_mono_time() : 0;
3812
Willy Tarreau610ecce2010-01-04 21:15:02 +01003813 s->logs.accept_date = date; /* user-visible date for logging */
3814 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003815 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3816 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003817 tv_zero(&s->logs.tv_request);
3818 s->logs.t_queue = -1;
3819 s->logs.t_connect = -1;
3820 s->logs.t_data = -1;
3821 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003822 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3823 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003824
Willy Tarreauf37954d2018-06-15 18:31:02 +02003825 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3826 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003827
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003828 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003829 if (s->flags & SF_CURR_SESS) {
3830 s->flags &= ~SF_CURR_SESS;
Willy Tarreaub54c40a2018-12-02 19:28:41 +01003831 HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003832 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003833 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003834 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003835 }
3836
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003837 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003838
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003839
3840 /* If we're doing keepalive, first call the mux detach() method
3841 * to let it know we want to detach without freing the connection.
3842 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003843 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003844 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard985f1392018-11-30 17:31:52 +01003845 !si_conn_ready(&s->si[1]))
Willy Tarreau323a2d92015-08-04 19:00:17 +02003846 srv_conn = NULL;
Olivier Houchard985f1392018-11-30 17:31:52 +01003847 else if (!srv_conn->owner) {
3848 srv_conn->owner = s->sess;
3849 session_add_conn(s->sess, srv_conn, s->target);
3850 }
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003851 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003852
Willy Tarreau350f4872014-11-28 14:42:25 +01003853 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3854 s->si[1].err_type = SI_ET_NONE;
3855 s->si[1].conn_retries = 0; /* used for logging too */
3856 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003857 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 +01003858 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 +02003859 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 +02003860 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3861 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3862 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003863
Patrick Hemmere3faf022018-08-22 10:02:00 -04003864 hlua_ctx_destroy(s->hlua);
3865 s->hlua = NULL;
3866
Willy Tarreaueee5b512015-04-03 23:46:31 +02003867 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003868 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003869 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003870
3871 if (prev_status == 401 || prev_status == 407) {
3872 /* In HTTP keep-alive mode, if we receive a 401, we still have
3873 * a chance of being able to send the visitor again to the same
3874 * server over the same connection. This is required by some
3875 * broken protocols such as NTLM, and anyway whenever there is
3876 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003877 * it's better to do it (at least it helps with debugging), at
3878 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003879 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003880 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003881 }
3882
Willy Tarreau53f96852016-02-02 18:50:47 +01003883 /* Never ever allow to reuse a connection from a non-reuse backend */
3884 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3885 srv_conn->flags |= CO_FL_PRIVATE;
3886
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003887 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003888 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003889
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003890 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003891 s->req.flags |= CF_NEVER_WAIT;
3892 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003893 }
3894
Willy Tarreau714ea782015-11-25 20:11:11 +01003895 /* we're removing the analysers, we MUST re-enable events detection.
3896 * We don't enable close on the response channel since it's either
3897 * already closed, or in keep-alive with an idle connection handler.
3898 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003899 channel_auto_read(&s->req);
3900 channel_auto_close(&s->req);
3901 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003902
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003903 /* we're in keep-alive with an idle connection, monitor it if not already done */
3904 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003905 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003906 if (srv) {
3907 if (srv_conn->flags & CO_FL_PRIVATE)
3908 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3909 else if (prev_flags & TX_NOT_FIRST)
3910 /* note: we check the request, not the connection, but
3911 * this is valid for strategies SAFE and AGGR, and in
3912 * case of ALWS, we don't care anyway.
3913 */
3914 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3915 else
3916 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3917 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003918 }
Christopher Faulete6006242017-03-10 11:52:44 +01003919 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3920 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003921}
3922
3923
3924/* This function updates the request state machine according to the response
3925 * state machine and buffer flags. It returns 1 if it changes anything (flag
3926 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3927 * it is only used to find when a request/response couple is complete. Both
3928 * this function and its equivalent should loop until both return zero. It
3929 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3930 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003931int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003932{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003933 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003934 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003935 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003936 unsigned int old_state = txn->req.msg_state;
3937
Christopher Faulet4be98032017-07-18 10:48:24 +02003938 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003939 return 0;
3940
3941 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003942 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003943 * We can shut the read side unless we want to abort_on_close,
3944 * or we have a POST request. The issue with POST requests is
3945 * that some browsers still send a CRLF after the request, and
3946 * this CRLF must be read so that it does not remain in the kernel
3947 * buffers, otherwise a close could cause an RST on some systems
3948 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003949 * Note that if we're using keep-alive on the client side, we'd
3950 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003951 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003952 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003953 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003954 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3955 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003956 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3957 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003958 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003959 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003960
Willy Tarreau40f151a2012-12-20 12:10:09 +01003961 /* if the server closes the connection, we want to immediately react
3962 * and close the socket to save packets and syscalls.
3963 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003964 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003965
Willy Tarreau7f876a12015-11-18 11:59:55 +01003966 /* In any case we've finished parsing the request so we must
3967 * disable Nagle when sending data because 1) we're not going
3968 * to shut this side, and 2) the server is waiting for us to
3969 * send pending data.
3970 */
3971 chn->flags |= CF_NEVER_WAIT;
3972
Willy Tarreau610ecce2010-01-04 21:15:02 +01003973 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3974 goto wait_other_side;
3975
3976 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3977 /* The server has not finished to respond, so we
3978 * don't want to move in order not to upset it.
3979 */
3980 goto wait_other_side;
3981 }
3982
Willy Tarreau610ecce2010-01-04 21:15:02 +01003983 /* When we get here, it means that both the request and the
3984 * response have finished receiving. Depending on the connection
3985 * mode, we'll have to wait for the last bytes to leave in either
3986 * direction, and sometimes for a close to be effective.
3987 */
3988
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003989 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3990 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003991 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3992 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003993 }
3994 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3995 /* Option forceclose is set, or either side wants to close,
3996 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003997 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003998 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003999 *
4000 * However, there is an exception if the response
4001 * length is undefined. In this case, we need to wait
4002 * the close from the server. The response will be
4003 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004004 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004005 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4006 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4007 goto check_channel_flags;
4008
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004009 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4010 channel_shutr_now(chn);
4011 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004012 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004013 }
4014 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004015 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4016 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004017 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004018 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4019 channel_auto_read(chn);
4020 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004021 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022 }
4023
Christopher Faulet4be98032017-07-18 10:48:24 +02004024 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004025 }
4026
4027 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4028 http_msg_closing:
4029 /* nothing else to forward, just waiting for the output buffer
4030 * to be empty and for the shutw_now to take effect.
4031 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004032 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033 txn->req.msg_state = HTTP_MSG_CLOSED;
4034 goto http_msg_closed;
4035 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004036 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004037 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004038 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004039 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004040 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004041 }
4042
4043 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4044 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004045 /* if we don't know whether the server will close, we need to hard close */
4046 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4047 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4048
Willy Tarreau3988d932013-12-27 23:03:08 +01004049 /* see above in MSG_DONE why we only do this in these states */
4050 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4051 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004052 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4053 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004054 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004055 goto wait_other_side;
4056 }
4057
Christopher Faulet4be98032017-07-18 10:48:24 +02004058 check_channel_flags:
4059 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4060 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4061 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004062 txn->req.msg_state = HTTP_MSG_CLOSING;
4063 goto http_msg_closing;
4064 }
4065
4066
Willy Tarreau610ecce2010-01-04 21:15:02 +01004067 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004068 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004069}
4070
4071
4072/* This function updates the response state machine according to the request
4073 * state machine and buffer flags. It returns 1 if it changes anything (flag
4074 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4075 * it is only used to find when a request/response couple is complete. Both
4076 * this function and its equivalent should loop until both return zero. It
4077 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4078 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004079int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004080{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004081 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004082 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004083 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004084 unsigned int old_state = txn->rsp.msg_state;
4085
Christopher Faulet4be98032017-07-18 10:48:24 +02004086 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004087 return 0;
4088
4089 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4090 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004091 * still monitor the server connection for a possible close
4092 * while the request is being uploaded, so we don't disable
4093 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004094 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004095 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004096
4097 if (txn->req.msg_state == HTTP_MSG_ERROR)
4098 goto wait_other_side;
4099
4100 if (txn->req.msg_state < HTTP_MSG_DONE) {
4101 /* The client seems to still be sending data, probably
4102 * because we got an error response during an upload.
4103 * We have the choice of either breaking the connection
4104 * or letting it pass through. Let's do the later.
4105 */
4106 goto wait_other_side;
4107 }
4108
Willy Tarreau610ecce2010-01-04 21:15:02 +01004109 /* When we get here, it means that both the request and the
4110 * response have finished receiving. Depending on the connection
4111 * mode, we'll have to wait for the last bytes to leave in either
4112 * direction, and sometimes for a close to be effective.
4113 */
4114
4115 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4116 /* Server-close mode : shut read and wait for the request
4117 * side to close its output buffer. The caller will detect
4118 * when we're in DONE and the other is in CLOSED and will
4119 * catch that for the final cleanup.
4120 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004121 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4122 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004123 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004124 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4125 /* Option forceclose is set, or either side wants to close,
4126 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004127 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004128 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004129 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004130 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004131 channel_shutr_now(chn);
4132 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004133 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004134 }
4135 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004136 /* The last possible modes are keep-alive and tunnel. Tunnel will
4137 * need to forward remaining data. Keep-alive will need to monitor
4138 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004139 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004140 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004141 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004142 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4143 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004144 }
4145
Christopher Faulet4be98032017-07-18 10:48:24 +02004146 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004147 }
4148
4149 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4150 http_msg_closing:
4151 /* nothing else to forward, just waiting for the output buffer
4152 * to be empty and for the shutw_now to take effect.
4153 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004154 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004155 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4156 goto http_msg_closed;
4157 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004158 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004159 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004160 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004161 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004162 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004163 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004164 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004165 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004166 }
4167
4168 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4169 http_msg_closed:
4170 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004171 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004172 channel_auto_close(chn);
4173 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004174 goto wait_other_side;
4175 }
4176
Christopher Faulet4be98032017-07-18 10:48:24 +02004177 check_channel_flags:
4178 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4179 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4180 /* if we've just closed an output, let's switch */
4181 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4182 goto http_msg_closing;
4183 }
4184
Willy Tarreau610ecce2010-01-04 21:15:02 +01004185 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004186 /* We force the response to leave immediately if we're waiting for the
4187 * other side, since there is no pending shutdown to push it out.
4188 */
4189 if (!channel_is_empty(chn))
4190 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004191 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004192}
4193
4194
Christopher Faulet894da4c2017-07-18 11:29:07 +02004195/* Resync the request and response state machines. */
4196void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004197{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004198 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004199#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004200 int old_req_state = txn->req.msg_state;
4201 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004202#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004203
Willy Tarreau610ecce2010-01-04 21:15:02 +01004204 http_sync_req_state(s);
4205 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004206 if (!http_sync_res_state(s))
4207 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004208 if (!http_sync_req_state(s))
4209 break;
4210 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004211
Christopher Faulet894da4c2017-07-18 11:29:07 +02004212 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4213 "req->analysers=0x%08x res->analysers=0x%08x\n",
4214 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004215 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4216 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004217 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004218
4219
Willy Tarreau610ecce2010-01-04 21:15:02 +01004220 /* OK, both state machines agree on a compatible state.
4221 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004222 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4223 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004224 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4225 * means we must abort the request.
4226 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4227 * corresponding channel.
4228 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4229 * on the response with server-close mode means we've completed one
4230 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004231 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004232 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4233 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004234 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004235 channel_auto_close(&s->req);
4236 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004237 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004238 channel_auto_close(&s->res);
4239 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004240 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004241 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4242 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004243 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004244 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004245 channel_auto_close(&s->res);
4246 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004247 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004248 channel_abort(&s->req);
4249 channel_auto_close(&s->req);
4250 channel_auto_read(&s->req);
4251 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004252 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004253 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4254 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4255 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4256 s->req.analysers &= AN_REQ_FLT_END;
4257 if (HAS_REQ_DATA_FILTERS(s))
4258 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4259 }
4260 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4261 s->res.analysers &= AN_RES_FLT_END;
4262 if (HAS_RSP_DATA_FILTERS(s))
4263 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4264 }
4265 channel_auto_close(&s->req);
4266 channel_auto_read(&s->req);
4267 channel_auto_close(&s->res);
4268 channel_auto_read(&s->res);
4269 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004270 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4271 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004272 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004273 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4274 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4275 /* server-close/keep-alive: terminate this transaction,
4276 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004277 * a fresh-new transaction, but only once we're sure there's
4278 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004279 * another request. They must not hold any pending output data
4280 * and the response buffer must realigned
4281 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004282 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004283 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004284 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004285 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004286 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004287 else {
4288 s->req.analysers = AN_REQ_FLT_END;
4289 s->res.analysers = AN_RES_FLT_END;
4290 txn->flags |= TX_WAIT_CLEANUP;
4291 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004292 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293}
4294
Willy Tarreaud98cf932009-12-27 22:54:55 +01004295/* This function is an analyser which forwards request body (including chunk
4296 * sizes if any). It is called as soon as we must forward, even if we forward
4297 * zero byte. The only situation where it must not be called is when we're in
4298 * tunnel mode and we want to forward till the close. It's used both to forward
4299 * remaining data and to resync after end of body. It expects the msg_state to
4300 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004301 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004302 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004303 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004304 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004305int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004306{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004307 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004308 struct http_txn *txn = s->txn;
4309 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004310 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004311
Christopher Faulete0768eb2018-10-03 16:38:02 +02004312 if (IS_HTX_STRM(s))
4313 return htx_request_forward_body(s, req, an_bit);
4314
Christopher Faulet45073512018-07-20 10:16:29 +02004315 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 +02004316 now_ms, __FUNCTION__,
4317 s,
4318 req,
4319 req->rex, req->wex,
4320 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004321 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004322 req->analysers);
4323
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004324 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4325 return 0;
4326
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004327 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004328 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004329 /* Output closed while we were sending data. We must abort and
4330 * wake the other side up.
4331 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004332 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004333 msg->msg_state = HTTP_MSG_ERROR;
4334 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004335 return 1;
4336 }
4337
Willy Tarreaud98cf932009-12-27 22:54:55 +01004338 /* Note that we don't have to send 100-continue back because we don't
4339 * need the data to complete our job, and it's up to the server to
4340 * decide whether to return 100, 417 or anything else in return of
4341 * an "Expect: 100-continue" header.
4342 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004343 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004344 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4345 ? HTTP_MSG_CHUNK_SIZE
4346 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004347
4348 /* TODO/filters: when http-buffer-request option is set or if a
4349 * rule on url_param exists, the first chunk size could be
4350 * already parsed. In that case, msg->next is after the chunk
4351 * size (including the CRLF after the size). So this case should
4352 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004353 }
4354
Willy Tarreau7ba23542014-04-17 21:50:00 +02004355 /* Some post-connect processing might want us to refrain from starting to
4356 * forward data. Currently, the only reason for this is "balance url_param"
4357 * whichs need to parse/process the request after we've enabled forwarding.
4358 */
4359 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004360 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004361 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004362 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004363 channel_dont_close(req); /* don't fail on early shutr */
4364 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004365 }
4366 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4367 }
4368
Willy Tarreau80a92c02014-03-12 10:41:13 +01004369 /* in most states, we should abort in case of early close */
4370 channel_auto_close(req);
4371
Willy Tarreauefdf0942014-04-24 20:08:57 +02004372 if (req->to_forward) {
4373 /* We can't process the buffer's contents yet */
4374 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004375 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004376 }
4377
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004378 if (msg->msg_state < HTTP_MSG_DONE) {
4379 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4380 ? http_msg_forward_chunked_body(s, msg)
4381 : http_msg_forward_body(s, msg));
4382 if (!ret)
4383 goto missing_data_or_waiting;
4384 if (ret < 0)
4385 goto return_bad_req;
4386 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004387
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004388 /* other states, DONE...TUNNEL */
4389 /* we don't want to forward closes on DONE except in tunnel mode. */
4390 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4391 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004392
Christopher Faulet894da4c2017-07-18 11:29:07 +02004393 http_resync_states(s);
4394 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004395 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4396 if (req->flags & CF_SHUTW) {
4397 /* request errors are most likely due to the
4398 * server aborting the transfer. */
4399 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004400 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004401 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004402 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004403 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004404 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004405 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004406 }
4407
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004408 /* If "option abortonclose" is set on the backend, we want to monitor
4409 * the client's connection and forward any shutdown notification to the
4410 * server, which will decide whether to close or to go on processing the
4411 * request. We only do that in tunnel mode, and not in other modes since
4412 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004413 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004414 channel_auto_read(req);
4415 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4416 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4417 s->si[1].flags |= SI_FL_NOLINGER;
4418 channel_auto_close(req);
4419 }
4420 else if (s->txn->meth == HTTP_METH_POST) {
4421 /* POST requests may require to read extra CRLF sent by broken
4422 * browsers and which could cause an RST to be sent upon close
4423 * on some systems (eg: Linux). */
4424 channel_auto_read(req);
4425 }
4426 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004427
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004428 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004429 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004430 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004431 if (!(s->flags & SF_ERR_MASK))
4432 s->flags |= SF_ERR_CLICL;
4433 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004434 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004435 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004436 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004437 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004438 }
4439
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004440 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4441 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004442 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004443 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004444
4445 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004446 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004447
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004448 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004449 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004450 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004451 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004452
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004453 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004454 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004455 * And when content-length is used, we never want to let the possible
4456 * shutdown be forwarded to the other side, as the state machine will
4457 * take care of it once the client responds. It's also important to
4458 * prevent TIME_WAITs from accumulating on the backend side, and for
4459 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004460 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004461 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004462 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004463
Willy Tarreau5c620922011-05-11 19:56:11 +02004464 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004465 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004466 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004467 * modes are already handled by the stream sock layer. We must not do
4468 * this in content-length mode because it could present the MSG_MORE
4469 * flag with the last block of forwarded data, which would cause an
4470 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004471 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004472 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004473 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004474
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004475 return 0;
4476
Willy Tarreaud98cf932009-12-27 22:54:55 +01004477 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004478 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004479 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004480 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004481
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004482 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004483 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004484 txn->req.msg_state = HTTP_MSG_ERROR;
4485 if (txn->status) {
4486 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004487 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004488 } else {
4489 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004490 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004491 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004492 req->analysers &= AN_REQ_FLT_END;
4493 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 +01004494
Willy Tarreaue7dff022015-04-03 01:14:29 +02004495 if (!(s->flags & SF_ERR_MASK))
4496 s->flags |= SF_ERR_PRXCOND;
4497 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004498 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004499 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004500 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004501 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004502 }
4503 return 0;
4504
4505 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004506 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004507 txn->req.msg_state = HTTP_MSG_ERROR;
4508 if (txn->status) {
4509 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004510 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004511 } else {
4512 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004513 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004514 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004515 req->analysers &= AN_REQ_FLT_END;
4516 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 +01004517
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004518 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4519 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004520 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004521 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004522
Willy Tarreaue7dff022015-04-03 01:14:29 +02004523 if (!(s->flags & SF_ERR_MASK))
4524 s->flags |= SF_ERR_SRVCL;
4525 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004526 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004527 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004528 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004529 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004530 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004531 return 0;
4532}
4533
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004534/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4535 * processing can continue on next analysers, or zero if it either needs more
4536 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004537 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004538 * when it has nothing left to do, and may remove any analyser when it wants to
4539 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004540 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004541int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004542{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004543 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004544 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004545 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004546 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004547 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004548 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004549 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004550 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004551
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004552 srv_conn = cs_conn(objt_cs(s->si[1].end));
4553
Christopher Faulete0768eb2018-10-03 16:38:02 +02004554 if (IS_HTX_STRM(s))
4555 return htx_wait_for_response(s, rep, an_bit);
4556
Christopher Faulet45073512018-07-20 10:16:29 +02004557 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 +02004558 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004559 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004560 rep,
4561 rep->rex, rep->wex,
4562 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004563 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004564 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004565
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004566 /*
4567 * Now parse the partial (or complete) lines.
4568 * We will check the response syntax, and also join multi-line
4569 * headers. An index of all the lines will be elaborated while
4570 * parsing.
4571 *
4572 * For the parsing, we use a 28 states FSM.
4573 *
4574 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004575 * ci_head(rep) = beginning of response
4576 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4577 * ci_tail(rep) = end of input data
4578 * msg->eol = end of current header or line (LF or CRLF)
4579 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004580 */
4581
Willy Tarreau628c40c2014-04-24 19:11:26 +02004582 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004583 /* There's a protected area at the end of the buffer for rewriting
4584 * purposes. We don't want to start to parse the request if the
4585 * protected area is affected, because we may have to move processed
4586 * data later, which is much more complicated.
4587 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004588 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004589 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004590 /* some data has still not left the buffer, wake us once that's done */
4591 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4592 goto abort_response;
4593 channel_dont_close(rep);
4594 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004595 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004596 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004597 }
4598
Willy Tarreau188e2302018-06-15 11:11:53 +02004599 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004600 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004601 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004602
Willy Tarreauf37954d2018-06-15 18:31:02 +02004603 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004604 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004605 }
4606
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004607 /* 1: we might have to print this header in debug mode */
4608 if (unlikely((global.mode & MODE_DEBUG) &&
4609 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004610 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004611 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004612
Willy Tarreauf37954d2018-06-15 18:31:02 +02004613 sol = ci_head(rep);
4614 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004615 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004616
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004617 sol += hdr_idx_first_pos(&txn->hdr_idx);
4618 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004619
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004620 while (cur_idx) {
4621 eol = sol + txn->hdr_idx.v[cur_idx].len;
4622 debug_hdr("srvhdr", s, sol, eol);
4623 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4624 cur_idx = txn->hdr_idx.v[cur_idx].next;
4625 }
4626 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004627
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004628 /*
4629 * Now we quickly check if we have found a full valid response.
4630 * If not so, we check the FD and buffer states before leaving.
4631 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004632 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004633 * responses are checked first.
4634 *
4635 * Depending on whether the client is still there or not, we
4636 * may send an error response back or not. Note that normally
4637 * we should only check for HTTP status there, and check I/O
4638 * errors somewhere else.
4639 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004640
Willy Tarreau655dce92009-11-08 13:10:58 +01004641 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004642 /* Invalid response */
4643 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4644 /* we detected a parsing error. We want to archive this response
4645 * in the dedicated proxy area for later troubleshooting.
4646 */
4647 hdr_response_bad:
4648 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004649 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004650
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004651 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004652 if (objt_server(s->target)) {
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004653 HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
4654 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004655 }
Willy Tarreau64648412010-03-05 10:41:54 +01004656 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004657 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004658 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004659 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004660 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004661 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004662 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004663
Willy Tarreaue7dff022015-04-03 01:14:29 +02004664 if (!(s->flags & SF_ERR_MASK))
4665 s->flags |= SF_ERR_PRXCOND;
4666 if (!(s->flags & SF_FINST_MASK))
4667 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004668
4669 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004670 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004671
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004672 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004673 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004674 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004675 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004676 goto hdr_response_bad;
4677 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004678
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004679 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004680 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004681 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004682 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004683 else if (txn->flags & TX_NOT_FIRST)
4684 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004685
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004686 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004687 if (objt_server(s->target)) {
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004688 HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
4689 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004690 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004691
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004692 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004693 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004694 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004695
4696 /* Check to see if the server refused the early data.
4697 * If so, just send a 425
4698 */
4699 if (objt_cs(s->si[1].end)) {
4700 struct connection *conn = objt_cs(s->si[1].end)->conn;
4701
4702 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4703 txn->status = 425;
4704 }
4705
Willy Tarreau350f4872014-11-28 14:42:25 +01004706 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004707 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004708 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004709
Willy Tarreaue7dff022015-04-03 01:14:29 +02004710 if (!(s->flags & SF_ERR_MASK))
4711 s->flags |= SF_ERR_SRVCL;
4712 if (!(s->flags & SF_FINST_MASK))
4713 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004714 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004715 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004716
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004717 /* read timeout : return a 504 to the client. */
4718 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004719 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004720 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004721
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004722 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004723 if (objt_server(s->target)) {
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004724 HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
4725 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004726 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004727
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004728 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004729 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004730 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004731 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004732 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004733 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004734
Willy Tarreaue7dff022015-04-03 01:14:29 +02004735 if (!(s->flags & SF_ERR_MASK))
4736 s->flags |= SF_ERR_SRVTO;
4737 if (!(s->flags & SF_FINST_MASK))
4738 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004739 return 0;
4740 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004741
Willy Tarreauf003d372012-11-26 13:35:37 +01004742 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004743 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004744 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4745 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004746 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004747 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004748
Christopher Faulet0184ea72017-01-05 14:06:34 +01004749 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004750 channel_auto_close(rep);
4751
4752 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004753 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004754 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004755
Willy Tarreaue7dff022015-04-03 01:14:29 +02004756 if (!(s->flags & SF_ERR_MASK))
4757 s->flags |= SF_ERR_CLICL;
4758 if (!(s->flags & SF_FINST_MASK))
4759 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004760
Willy Tarreau87b09662015-04-03 00:22:06 +02004761 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004762 return 0;
4763 }
4764
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004765 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004766 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004767 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004768 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004769 else if (txn->flags & TX_NOT_FIRST)
4770 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004771
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004772 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004773 if (objt_server(s->target)) {
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004774 HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
4775 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004776 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004777
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004778 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004779 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004780 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004781 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004782 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004783 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004784
Willy Tarreaue7dff022015-04-03 01:14:29 +02004785 if (!(s->flags & SF_ERR_MASK))
4786 s->flags |= SF_ERR_SRVCL;
4787 if (!(s->flags & SF_FINST_MASK))
4788 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004789 return 0;
4790 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004791
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004792 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004793 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004794 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004795 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004796 else if (txn->flags & TX_NOT_FIRST)
4797 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004798
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004799 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004800 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004801 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004802
Willy Tarreaue7dff022015-04-03 01:14:29 +02004803 if (!(s->flags & SF_ERR_MASK))
4804 s->flags |= SF_ERR_CLICL;
4805 if (!(s->flags & SF_FINST_MASK))
4806 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004807
Willy Tarreau87b09662015-04-03 00:22:06 +02004808 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004809 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004810 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004811
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004812 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004813 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004814 return 0;
4815 }
4816
4817 /* More interesting part now : we know that we have a complete
4818 * response which at least looks like HTTP. We have an indicator
4819 * of each header's length, so we can parse them quickly.
4820 */
4821
4822 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004823 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004824
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004825 /*
4826 * 1: get the status code
4827 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004828 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004829 if (n < 1 || n > 5)
4830 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004831 /* when the client triggers a 4xx from the server, it's most often due
4832 * to a missing object or permission. These events should be tracked
4833 * because if they happen often, it may indicate a brute force or a
4834 * vulnerability scan.
4835 */
4836 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004837 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004838
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004839 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004840 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004841
Willy Tarreau91852eb2015-05-01 13:26:00 +02004842 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4843 * exactly one digit "." one digit. This check may be disabled using
4844 * option accept-invalid-http-response.
4845 */
4846 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4847 if (msg->sl.st.v_l != 8) {
4848 msg->err_pos = 0;
4849 goto hdr_response_bad;
4850 }
4851
Willy Tarreauf37954d2018-06-15 18:31:02 +02004852 if (ci_head(rep)[4] != '/' ||
4853 !isdigit((unsigned char)ci_head(rep)[5]) ||
4854 ci_head(rep)[6] != '.' ||
4855 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004856 msg->err_pos = 4;
4857 goto hdr_response_bad;
4858 }
4859 }
4860
Willy Tarreau5b154472009-12-21 20:11:07 +01004861 /* check if the response is HTTP/1.1 or above */
4862 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004863 ((ci_head(rep)[5] > '1') ||
4864 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004865 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004866
4867 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004868 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 +01004869
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004870 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004871 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004872
Willy Tarreauf37954d2018-06-15 18:31:02 +02004873 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004874
Willy Tarreau39650402010-03-15 19:44:39 +01004875 /* Adjust server's health based on status code. Note: status codes 501
4876 * and 505 are triggered on demand by client request, so we must not
4877 * count them as server failures.
4878 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004879 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004880 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004881 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004882 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004883 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004884 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004885
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004886 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004887 * We may be facing a 100-continue response, or any other informational
4888 * 1xx response which is non-final, in which case this is not the right
4889 * response, and we're waiting for the next one. Let's allow this response
4890 * to go to the client and wait for the next one. There's an exception for
4891 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004892 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004893 if (txn->status < 200 &&
4894 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004895 hdr_idx_init(&txn->hdr_idx);
4896 msg->next -= channel_forward(rep, msg->next);
4897 msg->msg_state = HTTP_MSG_RPBEFORE;
4898 txn->status = 0;
4899 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004900 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004901 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004902 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004903
Willy Tarreaua14ad722017-07-07 11:36:32 +02004904 /*
4905 * 2: check for cacheability.
4906 */
4907
4908 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004909 case 200:
4910 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004911 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004912 case 206:
4913 case 300:
4914 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004915 case 404:
4916 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004917 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004918 case 414:
4919 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004920 break;
4921 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004922 /* RFC7231#6.1:
4923 * Responses with status codes that are defined as
4924 * cacheable by default (e.g., 200, 203, 204, 206,
4925 * 300, 301, 404, 405, 410, 414, and 501 in this
4926 * specification) can be reused by a cache with
4927 * heuristic expiration unless otherwise indicated
4928 * by the method definition or explicit cache
4929 * controls [RFC7234]; all other status codes are
4930 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004931 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004932 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004933 break;
4934 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004935
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004936 /*
4937 * 3: we may need to capture headers
4938 */
4939 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004940 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004941 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4942 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004943
Willy Tarreau557f1992015-05-01 10:05:17 +02004944 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4945 * by RFC7230#3.3.3 :
4946 *
4947 * The length of a message body is determined by one of the following
4948 * (in order of precedence):
4949 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004950 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4951 * the connection will become a tunnel immediately after the empty
4952 * line that concludes the header fields. A client MUST ignore
4953 * any Content-Length or Transfer-Encoding header fields received
4954 * in such a message. Any 101 response (Switching Protocols) is
4955 * managed in the same manner.
4956 *
4957 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004958 * (Informational), 204 (No Content), or 304 (Not Modified) status
4959 * code is always terminated by the first empty line after the
4960 * header fields, regardless of the header fields present in the
4961 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004962 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004963 * 3. If a Transfer-Encoding header field is present and the chunked
4964 * transfer coding (Section 4.1) is the final encoding, the message
4965 * body length is determined by reading and decoding the chunked
4966 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004967 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004968 * If a Transfer-Encoding header field is present in a response and
4969 * the chunked transfer coding is not the final encoding, the
4970 * message body length is determined by reading the connection until
4971 * it is closed by the server. If a Transfer-Encoding header field
4972 * is present in a request and the chunked transfer coding is not
4973 * the final encoding, the message body length cannot be determined
4974 * reliably; the server MUST respond with the 400 (Bad Request)
4975 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004976 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004977 * If a message is received with both a Transfer-Encoding and a
4978 * Content-Length header field, the Transfer-Encoding overrides the
4979 * Content-Length. Such a message might indicate an attempt to
4980 * perform request smuggling (Section 9.5) or response splitting
4981 * (Section 9.4) and ought to be handled as an error. A sender MUST
4982 * remove the received Content-Length field prior to forwarding such
4983 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004984 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004985 * 4. If a message is received without Transfer-Encoding and with
4986 * either multiple Content-Length header fields having differing
4987 * field-values or a single Content-Length header field having an
4988 * invalid value, then the message framing is invalid and the
4989 * recipient MUST treat it as an unrecoverable error. If this is a
4990 * request message, the server MUST respond with a 400 (Bad Request)
4991 * status code and then close the connection. If this is a response
4992 * message received by a proxy, the proxy MUST close the connection
4993 * to the server, discard the received response, and send a 502 (Bad
4994 * Gateway) response to the client. If this is a response message
4995 * received by a user agent, the user agent MUST close the
4996 * connection to the server and discard the received response.
4997 *
4998 * 5. If a valid Content-Length header field is present without
4999 * Transfer-Encoding, its decimal value defines the expected message
5000 * body length in octets. If the sender closes the connection or
5001 * the recipient times out before the indicated number of octets are
5002 * received, the recipient MUST consider the message to be
5003 * incomplete and close the connection.
5004 *
5005 * 6. If this is a request message and none of the above are true, then
5006 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005007 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005008 * 7. Otherwise, this is a response message without a declared message
5009 * body length, so the message body length is determined by the
5010 * number of octets received prior to the server closing the
5011 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005012 */
5013
5014 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005015 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005016 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005017 * FIXME: should we parse anyway and return an error on chunked encoding ?
5018 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005019 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5020 txn->status == 101)) {
5021 /* Either we've established an explicit tunnel, or we're
5022 * switching the protocol. In both cases, we're very unlikely
5023 * to understand the next protocols. We have to switch to tunnel
5024 * mode, so that we transfer the request and responses then let
5025 * this protocol pass unmodified. When we later implement specific
5026 * parsers for such protocols, we'll want to check the Upgrade
5027 * header which contains information about that protocol for
5028 * responses with status 101 (eg: see RFC2817 about TLS).
5029 */
5030 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5031 msg->flags |= HTTP_MSGF_XFER_LEN;
5032 goto end;
5033 }
5034
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005035 if (txn->meth == HTTP_METH_HEAD ||
5036 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005037 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005038 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005039 goto skip_content_length;
5040 }
5041
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005042 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005043 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005044 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005045 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005046 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5047 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005048 /* bad transfer-encoding (chunked followed by something else) */
5049 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005050 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005051 break;
5052 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005053 }
5054
Willy Tarreau1c913912015-04-30 10:57:51 +02005055 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005056 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005057 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005058 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005059 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5060 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005061 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005062 signed long long cl;
5063
Willy Tarreauad14f752011-09-02 20:33:27 +02005064 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005065 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005066 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005067 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005068
Willy Tarreauad14f752011-09-02 20:33:27 +02005069 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005070 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005071 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005072 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005073
Willy Tarreauad14f752011-09-02 20:33:27 +02005074 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005075 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005076 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005077 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005078
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005079 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005080 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005081 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005082 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005083
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005084 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005085 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005086 }
5087
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02005088 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
5089 * 407 (Proxy-Authenticate) responses and set the connection to private
5090 */
5091 if (srv_conn && txn->status == 401) {
5092 /* check for Negotiate/NTLM WWW-Authenticate headers */
5093 ctx.idx = 0;
5094 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
5095 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5096 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5097 srv_conn->flags |= CO_FL_PRIVATE;
5098 }
5099 } else if (srv_conn && txn->status == 407) {
5100 /* check for Negotiate/NTLM Proxy-Authenticate headers */
5101 ctx.idx = 0;
5102 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
5103 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5104 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5105 srv_conn->flags |= CO_FL_PRIVATE;
5106 }
5107 }
5108
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005109 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005110 /* Now we have to check if we need to modify the Connection header.
5111 * This is more difficult on the response than it is on the request,
5112 * because we can have two different HTTP versions and we don't know
5113 * how the client will interprete a response. For instance, let's say
5114 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5115 * HTTP/1.1 response without any header. Maybe it will bound itself to
5116 * HTTP/1.0 because it only knows about it, and will consider the lack
5117 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5118 * the lack of header as a keep-alive. Thus we will use two flags
5119 * indicating how a request MAY be understood by the client. In case
5120 * of multiple possibilities, we'll fix the header to be explicit. If
5121 * ambiguous cases such as both close and keepalive are seen, then we
5122 * will fall back to explicit close. Note that we won't take risks with
5123 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005124 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005125 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005126 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005127 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01005128 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005129
Willy Tarreau60466522010-01-18 19:08:45 +01005130 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005131 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01005132 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005133
Willy Tarreau60466522010-01-18 19:08:45 +01005134 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005135 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01005136 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005137 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005138 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005139 }
Willy Tarreau60466522010-01-18 19:08:45 +01005140 else { /* SCL / KAL */
5141 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005142 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005143 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005144 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005145
Willy Tarreau60466522010-01-18 19:08:45 +01005146 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005147 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005148
Willy Tarreau60466522010-01-18 19:08:45 +01005149 /* Some keep-alive responses are converted to Server-close if
5150 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005151 */
Willy Tarreau60466522010-01-18 19:08:45 +01005152 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5153 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005154 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005155 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005156 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005157 }
5158
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005159 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005160 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005161 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005162
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005163 /* end of job, return OK */
5164 rep->analysers &= ~an_bit;
5165 rep->analyse_exp = TICK_ETERNITY;
5166 channel_auto_close(rep);
5167 return 1;
5168
5169 abort_keep_alive:
5170 /* A keep-alive request to the server failed on a network error.
5171 * The client is required to retry. We need to close without returning
5172 * any other information so that the client retries.
5173 */
5174 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005175 rep->analysers &= AN_RES_FLT_END;
5176 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005177 channel_auto_close(rep);
5178 s->logs.logwait = 0;
5179 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005180 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005181 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005182 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005183 return 0;
5184}
5185
5186/* This function performs all the processing enabled for the current response.
5187 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005188 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005189 * other functions. It works like process_request (see indications above).
5190 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005191int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005192{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005193 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005194 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005195 struct http_msg *msg = &txn->rsp;
5196 struct proxy *cur_proxy;
5197 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005198 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005199
Christopher Faulete0768eb2018-10-03 16:38:02 +02005200 if (IS_HTX_STRM(s))
5201 return htx_process_res_common(s, rep, an_bit, px);
5202
Christopher Faulet45073512018-07-20 10:16:29 +02005203 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 +02005204 now_ms, __FUNCTION__,
5205 s,
5206 rep,
5207 rep->rex, rep->wex,
5208 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005209 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005210 rep->analysers);
5211
5212 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5213 return 0;
5214
Willy Tarreau70730dd2014-04-24 18:06:27 +02005215 /* The stats applet needs to adjust the Connection header but we don't
5216 * apply any filter there.
5217 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005218 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5219 rep->analysers &= ~an_bit;
5220 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005221 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005222 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005223
Willy Tarreau58975672014-04-24 21:13:57 +02005224 /*
5225 * We will have to evaluate the filters.
5226 * As opposed to version 1.2, now they will be evaluated in the
5227 * filters order and not in the header order. This means that
5228 * each filter has to be validated among all headers.
5229 *
5230 * Filters are tried with ->be first, then with ->fe if it is
5231 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005232 *
5233 * Maybe we are in resume condiion. In this case I choose the
5234 * "struct proxy" which contains the rule list matching the resume
5235 * pointer. If none of theses "struct proxy" match, I initialise
5236 * the process with the first one.
5237 *
5238 * In fact, I check only correspondance betwwen the current list
5239 * pointer and the ->fe rule list. If it doesn't match, I initialize
5240 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005241 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005242 if (s->current_rule_list == &sess->fe->http_res_rules)
5243 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005244 else
5245 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005246 while (1) {
5247 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005248
Willy Tarreau58975672014-04-24 21:13:57 +02005249 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005250 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005251 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005252
Willy Tarreau51d861a2015-05-22 17:30:48 +02005253 if (ret == HTTP_RULE_RES_BADREQ)
5254 goto return_srv_prx_502;
5255
5256 if (ret == HTTP_RULE_RES_DONE) {
5257 rep->analysers &= ~an_bit;
5258 rep->analyse_exp = TICK_ETERNITY;
5259 return 1;
5260 }
5261 }
5262
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005263 /* we need to be called again. */
5264 if (ret == HTTP_RULE_RES_YIELD) {
5265 channel_dont_close(rep);
5266 return 0;
5267 }
5268
Willy Tarreau58975672014-04-24 21:13:57 +02005269 /* try headers filters */
5270 if (rule_set->rsp_exp != NULL) {
5271 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5272 return_bad_resp:
5273 if (objt_server(s->target)) {
Willy Tarreaub54c40a2018-12-02 19:28:41 +01005274 HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
5275 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005276 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005277 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005278 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005279 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005280 txn->status = 502;
5281 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005282 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005283 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005284 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005285 if (!(s->flags & SF_ERR_MASK))
5286 s->flags |= SF_ERR_PRXCOND;
5287 if (!(s->flags & SF_FINST_MASK))
5288 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005289 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005290 }
Willy Tarreau58975672014-04-24 21:13:57 +02005291 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005292
Willy Tarreau58975672014-04-24 21:13:57 +02005293 /* has the response been denied ? */
5294 if (txn->flags & TX_SVDENY) {
5295 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005296 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005297
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005298 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5299 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005300 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005301 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005302
Willy Tarreau58975672014-04-24 21:13:57 +02005303 goto return_srv_prx_502;
5304 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005305
Willy Tarreau58975672014-04-24 21:13:57 +02005306 /* add response headers from the rule sets in the same order */
5307 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005308 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005309 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005310 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005311 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005312 ret = acl_pass(ret);
5313 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5314 ret = !ret;
5315 if (!ret)
5316 continue;
5317 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005318 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005319 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005320 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005321
Willy Tarreau58975672014-04-24 21:13:57 +02005322 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005323 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005324 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005325 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005326 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005327
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005328 /* After this point, this anayzer can't return yield, so we can
5329 * remove the bit corresponding to this analyzer from the list.
5330 *
5331 * Note that the intermediate returns and goto found previously
5332 * reset the analyzers.
5333 */
5334 rep->analysers &= ~an_bit;
5335 rep->analyse_exp = TICK_ETERNITY;
5336
Willy Tarreau58975672014-04-24 21:13:57 +02005337 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005338 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005339 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005340
Willy Tarreau58975672014-04-24 21:13:57 +02005341 /*
5342 * Now check for a server cookie.
5343 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005344 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005345 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005346
Willy Tarreau58975672014-04-24 21:13:57 +02005347 /*
5348 * Check for cache-control or pragma headers if required.
5349 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005350 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005351 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005352
Willy Tarreau58975672014-04-24 21:13:57 +02005353 /*
5354 * Add server cookie in the response if needed
5355 */
5356 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5357 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005358 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005359 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5360 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5361 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5362 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5363 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005364 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005365 /* the server is known, it's not the one the client requested, or the
5366 * cookie's last seen date needs to be refreshed. We have to
5367 * insert a set-cookie here, except if we want to insert only on POST
5368 * requests and this one isn't. Note that servers which don't have cookies
5369 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005370 */
Willy Tarreau58975672014-04-24 21:13:57 +02005371 if (!objt_server(s->target)->cookie) {
5372 chunk_printf(&trash,
5373 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5374 s->be->cookie_name);
5375 }
5376 else {
5377 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005378
Willy Tarreau58975672014-04-24 21:13:57 +02005379 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5380 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005381 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5382 s30tob64((date.tv_sec+3) >> 2,
5383 trash.area + trash.data);
5384 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005385
Willy Tarreau58975672014-04-24 21:13:57 +02005386 if (s->be->cookie_maxlife) {
5387 /* emit first_date, which is either the original one or
5388 * the current date.
5389 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005390 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005391 s30tob64(txn->cookie_first_date ?
5392 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005393 (date.tv_sec+3) >> 2,
5394 trash.area + trash.data);
5395 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005396 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005397 }
Willy Tarreau58975672014-04-24 21:13:57 +02005398 chunk_appendf(&trash, "; path=/");
5399 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005400
Willy Tarreau58975672014-04-24 21:13:57 +02005401 if (s->be->cookie_domain)
5402 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005403
Willy Tarreau58975672014-04-24 21:13:57 +02005404 if (s->be->ck_opts & PR_CK_HTTPONLY)
5405 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005406
Willy Tarreau58975672014-04-24 21:13:57 +02005407 if (s->be->ck_opts & PR_CK_SECURE)
5408 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005409
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005410 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005411 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005412
Willy Tarreau58975672014-04-24 21:13:57 +02005413 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005414 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005415 /* the server did not change, only the date was updated */
5416 txn->flags |= TX_SCK_UPDATED;
5417 else
5418 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005419
Willy Tarreau58975672014-04-24 21:13:57 +02005420 /* Here, we will tell an eventual cache on the client side that we don't
5421 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5422 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5423 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005424 */
Willy Tarreau58975672014-04-24 21:13:57 +02005425 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005426
Willy Tarreau58975672014-04-24 21:13:57 +02005427 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005428
Willy Tarreau58975672014-04-24 21:13:57 +02005429 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5430 "Cache-control: private", 22) < 0))
5431 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005432 }
Willy Tarreau58975672014-04-24 21:13:57 +02005433 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005434
Willy Tarreau58975672014-04-24 21:13:57 +02005435 /*
5436 * Check if result will be cacheable with a cookie.
5437 * We'll block the response if security checks have caught
5438 * nasty things such as a cacheable cookie.
5439 */
5440 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5441 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5442 (s->be->options & PR_O_CHK_CACHE)) {
5443 /* we're in presence of a cacheable response containing
5444 * a set-cookie header. We'll block it as requested by
5445 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005446 */
Willy Tarreau58975672014-04-24 21:13:57 +02005447 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005448 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005449
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005450 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5451 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005452 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005453 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005454
Christopher Faulet767a84b2017-11-24 16:50:31 +01005455 ha_alert("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>");
Willy Tarreau58975672014-04-24 21:13:57 +02005457 send_log(s->be, LOG_ALERT,
5458 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5459 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5460 goto return_srv_prx_502;
5461 }
Willy Tarreau03945942009-12-22 16:50:27 +01005462
Willy Tarreau70730dd2014-04-24 18:06:27 +02005463 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005464 /*
5465 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5466 * If an "Upgrade" token is found, the header is left untouched in order
5467 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005468 * if anything but "Upgrade" is present in the Connection header. We don't
5469 * want to touch any 101 response either since it's switching to another
5470 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005471 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005472 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005473 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005474 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005475
Willy Tarreau58975672014-04-24 21:13:57 +02005476 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5477 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5478 /* we want a keep-alive response here. Keep-alive header
5479 * required if either side is not 1.1.
5480 */
5481 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5482 want_flags |= TX_CON_KAL_SET;
5483 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005484 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005485 /* we want a close response here. Close header required if
5486 * the server is 1.1, regardless of the client.
5487 */
5488 if (msg->flags & HTTP_MSGF_VER_11)
5489 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005490 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005491
Willy Tarreau58975672014-04-24 21:13:57 +02005492 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5493 http_change_connection_header(txn, msg, want_flags);
5494 }
5495
5496 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005497 /* Always enter in the body analyzer */
5498 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5499 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005500
Willy Tarreau58975672014-04-24 21:13:57 +02005501 /* if the user wants to log as soon as possible, without counting
5502 * bytes from the server, then this is the right moment. We have
5503 * to temporarily assign bytes_out to log what we currently have.
5504 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005505 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005506 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5507 s->logs.bytes_out = txn->rsp.eoh;
5508 s->do_log(s);
5509 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005510 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005511 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005512}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005513
Willy Tarreaud98cf932009-12-27 22:54:55 +01005514/* This function is an analyser which forwards response body (including chunk
5515 * sizes if any). It is called as soon as we must forward, even if we forward
5516 * zero byte. The only situation where it must not be called is when we're in
5517 * tunnel mode and we want to forward till the close. It's used both to forward
5518 * remaining data and to resync after end of body. It expects the msg_state to
5519 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005520 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005521 *
5522 * It is capable of compressing response data both in content-length mode and
5523 * in chunked mode. The state machines follows different flows depending on
5524 * whether content-length and chunked modes are used, since there are no
5525 * trailers in content-length :
5526 *
5527 * chk-mode cl-mode
5528 * ,----- BODY -----.
5529 * / \
5530 * V size > 0 V chk-mode
5531 * .--> SIZE -------------> DATA -------------> CRLF
5532 * | | size == 0 | last byte |
5533 * | v final crlf v inspected |
5534 * | TRAILERS -----------> DONE |
5535 * | |
5536 * `----------------------------------------------'
5537 *
5538 * Compression only happens in the DATA state, and must be flushed in final
5539 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5540 * is performed at once on final states for all bytes parsed, or when leaving
5541 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005542 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005543int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005544{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005545 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005546 struct http_txn *txn = s->txn;
5547 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005548 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005549
Christopher Faulete0768eb2018-10-03 16:38:02 +02005550 if (IS_HTX_STRM(s))
5551 return htx_response_forward_body(s, res, an_bit);
5552
Christopher Faulet45073512018-07-20 10:16:29 +02005553 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 +02005554 now_ms, __FUNCTION__,
5555 s,
5556 res,
5557 res->rex, res->wex,
5558 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005559 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005560 res->analysers);
5561
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005562 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5563 return 0;
5564
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005565 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005566 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005567 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005568 /* Output closed while we were sending data. We must abort and
5569 * wake the other side up.
5570 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005571 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005572 msg->msg_state = HTTP_MSG_ERROR;
5573 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005574 return 1;
5575 }
5576
Willy Tarreau4fe41902010-06-07 22:27:41 +02005577 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005578 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005579
Christopher Fauletd7c91962015-04-30 11:48:27 +02005580 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005581 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5582 ? HTTP_MSG_CHUNK_SIZE
5583 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005584 }
5585
Willy Tarreauefdf0942014-04-24 20:08:57 +02005586 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005587 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005588 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005589 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005590 }
5591
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005592 if (msg->msg_state < HTTP_MSG_DONE) {
5593 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5594 ? http_msg_forward_chunked_body(s, msg)
5595 : http_msg_forward_body(s, msg));
5596 if (!ret)
5597 goto missing_data_or_waiting;
5598 if (ret < 0)
5599 goto return_bad_res;
5600 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005601
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005602 /* other states, DONE...TUNNEL */
5603 /* for keep-alive we don't want to forward closes on DONE */
5604 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5605 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5606 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005607
Christopher Faulet894da4c2017-07-18 11:29:07 +02005608 http_resync_states(s);
5609 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005610 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5611 if (res->flags & CF_SHUTW) {
5612 /* response errors are most likely due to the
5613 * client aborting the transfer. */
5614 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005615 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005616 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005617 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005618 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005619 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005620 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005621 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005622 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005623
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005624 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005625 if (res->flags & CF_SHUTW)
5626 goto aborted_xfer;
5627
5628 /* stop waiting for data if the input is closed before the end. If the
5629 * client side was already closed, it means that the client has aborted,
5630 * so we don't want to count this as a server abort. Otherwise it's a
5631 * server abort.
5632 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005633 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005634 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005635 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005636 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005637 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005638 if (!(s->flags & SF_ERR_MASK))
5639 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005640 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005641 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005642 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005643 goto return_bad_res_stats_ok;
5644 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005645 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005646
Willy Tarreau40dba092010-03-04 18:14:51 +01005647 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005648 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005649 goto return_bad_res;
5650
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005651 /* When TE: chunked is used, we need to get there again to parse
5652 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005653 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5654 * or if there are filters registered on the stream, we don't want to
5655 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005656 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005657 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005658 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005659 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5660 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005661 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005662
Willy Tarreau5c620922011-05-11 19:56:11 +02005663 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005664 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005665 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005666 * modes are already handled by the stream sock layer. We must not do
5667 * this in content-length mode because it could present the MSG_MORE
5668 * flag with the last block of forwarded data, which would cause an
5669 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005670 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005671 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005672 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005673
Willy Tarreau87b09662015-04-03 00:22:06 +02005674 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005675 return 0;
5676
Willy Tarreau40dba092010-03-04 18:14:51 +01005677 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005678 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005679 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005680 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005681
5682 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005683 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005684 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005685 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005686 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005687 res->analysers &= AN_RES_FLT_END;
5688 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 +01005689 if (objt_server(s->target))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01005690 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005691
Willy Tarreaue7dff022015-04-03 01:14:29 +02005692 if (!(s->flags & SF_ERR_MASK))
5693 s->flags |= SF_ERR_PRXCOND;
5694 if (!(s->flags & SF_FINST_MASK))
5695 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005696 return 0;
5697
5698 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005699 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005700 txn->rsp.msg_state = HTTP_MSG_ERROR;
5701 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005702 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005703 res->analysers &= AN_RES_FLT_END;
5704 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 +01005705
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005706 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5707 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005708 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005709 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005710
Willy Tarreaue7dff022015-04-03 01:14:29 +02005711 if (!(s->flags & SF_ERR_MASK))
5712 s->flags |= SF_ERR_CLICL;
5713 if (!(s->flags & SF_FINST_MASK))
5714 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005715 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005716}
5717
5718
Christopher Faulet10079f52018-10-03 15:17:28 +02005719int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005720{
5721 struct channel *chn = msg->chn;
5722 int ret;
5723
5724 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5725
5726 if (msg->msg_state == HTTP_MSG_ENDING)
5727 goto ending;
5728
5729 /* Neither content-length, nor transfer-encoding was found, so we must
5730 * read the body until the server connection is closed. In that case, we
5731 * eat data as they come. Of course, this happens for response only. */
5732 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005733 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005734 msg->chunk_len += len;
5735 msg->body_len += len;
5736 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005737 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005738 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005739 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005740 msg->next += ret;
5741 msg->chunk_len -= ret;
5742 if (msg->chunk_len) {
5743 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005744 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005745 chn->flags |= CF_WAKE_WRITE;
5746 goto missing_data_or_waiting;
5747 }
5748
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005749 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5750 * always set for a request. */
5751 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5752 /* The server still sending data that should be filtered */
5753 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5754 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005755 msg->msg_state = HTTP_MSG_TUNNEL;
5756 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005757 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005758
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005759 msg->msg_state = HTTP_MSG_ENDING;
5760
5761 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005762 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005763 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005764 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5765 /* default_ret */ msg->next,
5766 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005767 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005768 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005769 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5770 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005771 if (msg->next)
5772 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005773
Christopher Fauletda02e172015-12-04 09:25:05 +01005774 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5775 /* default_ret */ 1,
5776 /* on_error */ goto error,
5777 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005778 if (msg->msg_state == HTTP_MSG_ENDING)
5779 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005780 return 1;
5781
5782 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005783 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005784 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5785 /* default_ret */ msg->next,
5786 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005787 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005788 msg->next -= ret;
5789 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5790 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005791 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005792 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005793 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005794 return 0;
5795 error:
5796 return -1;
5797}
5798
Christopher Faulet10079f52018-10-03 15:17:28 +02005799int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005800{
5801 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005802 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005803 int ret;
5804
5805 /* Here we have the guarantee to be in one of the following state:
5806 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5807 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5808
Christopher Fauletca874b82018-09-20 11:31:01 +02005809 if (msg->msg_state == HTTP_MSG_ENDING)
5810 goto ending;
5811
5812 /* Don't parse chunks if there is no input data */
5813 if (!ci_data(chn))
5814 goto waiting;
5815
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005816 switch_states:
5817 switch (msg->msg_state) {
5818 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005819 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005820 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005821 /* on_error */ goto error);
5822 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005823 msg->chunk_len -= ret;
5824 if (msg->chunk_len) {
5825 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005826 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005827 chn->flags |= CF_WAKE_WRITE;
5828 goto missing_data_or_waiting;
5829 }
5830
5831 /* nothing left to forward for this chunk*/
5832 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5833 /* fall through for HTTP_MSG_CHUNK_CRLF */
5834
5835 case HTTP_MSG_CHUNK_CRLF:
5836 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005837 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005838 if (ret == 0)
5839 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005840 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005841 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005842 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005843 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005844 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005845 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005846 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005847 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5848 /* fall through for HTTP_MSG_CHUNK_SIZE */
5849
5850 case HTTP_MSG_CHUNK_SIZE:
5851 /* read the chunk size and assign it to ->chunk_len,
5852 * then set ->next to point to the body and switch to
5853 * DATA or TRAILERS state.
5854 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005855 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005856 if (ret == 0)
5857 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005858 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005859 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005860 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005861 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005862 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005863 }
5864
5865 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005866 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005867 msg->chunk_len = chunk;
5868 msg->body_len += chunk;
5869
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005870 if (msg->chunk_len) {
5871 msg->msg_state = HTTP_MSG_DATA;
5872 goto switch_states;
5873 }
5874 msg->msg_state = HTTP_MSG_TRAILERS;
5875 /* fall through for HTTP_MSG_TRAILERS */
5876
5877 case HTTP_MSG_TRAILERS:
5878 ret = http_forward_trailers(msg);
5879 if (ret < 0)
5880 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005881 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5882 /* default_ret */ 1,
5883 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005884 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005885 if (!ret)
5886 goto missing_data_or_waiting;
5887 break;
5888
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005889 default:
5890 /* This should no happen in this function */
5891 goto error;
5892 }
5893
5894 msg->msg_state = HTTP_MSG_ENDING;
5895 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005896 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005897 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005898 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005899 /* default_ret */ msg->next,
5900 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005901 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005902 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005903 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5904 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005905 if (msg->next)
5906 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005907
Christopher Fauletda02e172015-12-04 09:25:05 +01005908 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005909 /* default_ret */ 1,
5910 /* on_error */ goto error,
5911 /* on_wait */ goto waiting);
5912 msg->msg_state = HTTP_MSG_DONE;
5913 return 1;
5914
5915 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005916 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005917 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005918 /* default_ret */ msg->next,
5919 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005920 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005921 msg->next -= ret;
5922 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5923 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005924 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005925 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005926 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005927 return 0;
5928
5929 chunk_parsing_error:
5930 if (msg->err_pos >= 0) {
5931 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005932 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005933 msg->msg_state, strm_fe(s));
5934 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005935 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005936 msg, msg->msg_state, s->be);
5937 }
5938 error:
5939 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005940}
5941
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005942
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005943/* Iterate the same filter through all request headers.
5944 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005945 * Since it can manage the switch to another backend, it updates the per-proxy
5946 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005947 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005948int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005949{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005950 char *cur_ptr, *cur_end, *cur_next;
5951 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005952 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005953 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005954 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005955
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005956 last_hdr = 0;
5957
Willy Tarreauf37954d2018-06-15 18:31:02 +02005958 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005959 old_idx = 0;
5960
5961 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005962 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005963 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005964 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005965 (exp->action == ACT_ALLOW ||
5966 exp->action == ACT_DENY ||
5967 exp->action == ACT_TARPIT))
5968 return 0;
5969
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005970 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005971 if (!cur_idx)
5972 break;
5973
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005974 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005975 cur_ptr = cur_next;
5976 cur_end = cur_ptr + cur_hdr->len;
5977 cur_next = cur_end + cur_hdr->cr + 1;
5978
5979 /* Now we have one header between cur_ptr and cur_end,
5980 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005981 */
5982
Willy Tarreau15a53a42015-01-21 13:39:42 +01005983 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005984 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005985 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005986 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005987 last_hdr = 1;
5988 break;
5989
5990 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005991 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005992 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005993 break;
5994
5995 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005996 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005997 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005998 break;
5999
6000 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006001 len = exp_replace(trash.area,
6002 trash.size, cur_ptr,
6003 exp->replace, pmatch);
6004 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006005 return -1;
6006
Willy Tarreau6e27be12018-08-22 04:46:47 +02006007 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6008
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006009 /* FIXME: if the user adds a newline in the replacement, the
6010 * index will not be recalculated for now, and the new line
6011 * will not be counted as a new header.
6012 */
6013
6014 cur_end += delta;
6015 cur_next += delta;
6016 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006017 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006018 break;
6019
6020 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006021 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006022 cur_next += delta;
6023
Willy Tarreaufa355d42009-11-29 18:12:29 +01006024 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006025 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6026 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006027 cur_hdr->len = 0;
6028 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006029 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006030 break;
6031
6032 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006033 }
6034
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006035 /* keep the link from this header to next one in case of later
6036 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006037 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006038 old_idx = cur_idx;
6039 }
6040 return 0;
6041}
6042
6043
6044/* Apply the filter to the request line.
6045 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6046 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006047 * Since it can manage the switch to another backend, it updates the per-proxy
6048 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006049 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006050int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006051{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006052 char *cur_ptr, *cur_end;
6053 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006054 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006055 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006056
Willy Tarreau3d300592007-03-18 18:34:41 +01006057 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006058 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006059 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006060 (exp->action == ACT_ALLOW ||
6061 exp->action == ACT_DENY ||
6062 exp->action == ACT_TARPIT))
6063 return 0;
6064 else if (exp->action == ACT_REMOVE)
6065 return 0;
6066
6067 done = 0;
6068
Willy Tarreauf37954d2018-06-15 18:31:02 +02006069 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006070 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006071
6072 /* Now we have the request line between cur_ptr and cur_end */
6073
Willy Tarreau15a53a42015-01-21 13:39:42 +01006074 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006075 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006076 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006077 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006078 done = 1;
6079 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006080
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006081 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006082 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006083 done = 1;
6084 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006085
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006086 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006087 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006088 done = 1;
6089 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006090
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006091 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006092 len = exp_replace(trash.area, trash.size,
6093 cur_ptr, exp->replace, pmatch);
6094 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006095 return -1;
6096
Willy Tarreau6e27be12018-08-22 04:46:47 +02006097 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6098
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006099 /* FIXME: if the user adds a newline in the replacement, the
6100 * index will not be recalculated for now, and the new line
6101 * will not be counted as a new header.
6102 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006103
Willy Tarreaufa355d42009-11-29 18:12:29 +01006104 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006105 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006106 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006107 HTTP_MSG_RQMETH,
6108 cur_ptr, cur_end + 1,
6109 NULL, NULL);
6110 if (unlikely(!cur_end))
6111 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006113 /* we have a full request and we know that we have either a CR
6114 * or an LF at <ptr>.
6115 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006116 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6117 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006118 /* there is no point trying this regex on headers */
6119 return 1;
6120 }
6121 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006122 return done;
6123}
Willy Tarreau97de6242006-12-27 17:18:38 +01006124
Willy Tarreau58f10d72006-12-04 02:26:12 +01006125
Willy Tarreau58f10d72006-12-04 02:26:12 +01006126
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006127/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006128 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006129 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006130 * unparsable request. Since it can manage the switch to another backend, it
6131 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006132 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006133int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006134{
Willy Tarreau192252e2015-04-04 01:47:55 +02006135 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006136 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006137 struct hdr_exp *exp;
6138
6139 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006140 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006141
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006142 /*
6143 * The interleaving of transformations and verdicts
6144 * makes it difficult to decide to continue or stop
6145 * the evaluation.
6146 */
6147
Willy Tarreau6c123b12010-01-28 20:22:06 +01006148 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6149 break;
6150
Willy Tarreau3d300592007-03-18 18:34:41 +01006151 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006152 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006153 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006154 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006155
6156 /* if this filter had a condition, evaluate it now and skip to
6157 * next filter if the condition does not match.
6158 */
6159 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006160 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006161 ret = acl_pass(ret);
6162 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6163 ret = !ret;
6164
6165 if (!ret)
6166 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006167 }
6168
6169 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006170 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006171 if (unlikely(ret < 0))
6172 return -1;
6173
6174 if (likely(ret == 0)) {
6175 /* The filter did not match the request, it can be
6176 * iterated through all headers.
6177 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006178 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6179 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006180 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006181 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006182 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006183}
6184
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006185
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006186/* Delete a value in a header between delimiters <from> and <next> in buffer
6187 * <buf>. The number of characters displaced is returned, and the pointer to
6188 * the first delimiter is updated if required. The function tries as much as
6189 * possible to respect the following principles :
6190 * - replace <from> delimiter by the <next> one unless <from> points to a
6191 * colon, in which case <next> is simply removed
6192 * - set exactly one space character after the new first delimiter, unless
6193 * there are not enough characters in the block being moved to do so.
6194 * - remove unneeded spaces before the previous delimiter and after the new
6195 * one.
6196 *
6197 * It is the caller's responsibility to ensure that :
6198 * - <from> points to a valid delimiter or the colon ;
6199 * - <next> points to a valid delimiter or the final CR/LF ;
6200 * - there are non-space chars before <from> ;
6201 * - there is a CR/LF at or after <next>.
6202 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006203int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006204{
6205 char *prev = *from;
6206
6207 if (*prev == ':') {
6208 /* We're removing the first value, preserve the colon and add a
6209 * space if possible.
6210 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006211 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006212 next++;
6213 prev++;
6214 if (prev < next)
6215 *prev++ = ' ';
6216
Willy Tarreau2235b262016-11-05 15:50:20 +01006217 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006218 next++;
6219 } else {
6220 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006221 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006222 prev--;
6223 *from = prev;
6224
6225 /* copy the delimiter and if possible a space if we're
6226 * not at the end of the line.
6227 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006228 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006229 *prev++ = *next++;
6230 if (prev + 1 < next)
6231 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006232 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006233 next++;
6234 }
6235 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006236 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006237}
6238
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006239/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006240 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006241 * desirable to call it only when needed. This code is quite complex because
6242 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6243 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006244 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006245void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006246{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006247 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006248 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006249 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006250 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006251 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6252 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006253
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006254 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006255 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006256 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006258 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006259 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006260 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006261
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006262 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006263 hdr_beg = hdr_next;
6264 hdr_end = hdr_beg + cur_hdr->len;
6265 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006266
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006267 /* We have one full header between hdr_beg and hdr_end, and the
6268 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006269 * "Cookie:" headers.
6270 */
6271
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006272 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006273 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006274 old_idx = cur_idx;
6275 continue;
6276 }
6277
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006278 del_from = NULL; /* nothing to be deleted */
6279 preserve_hdr = 0; /* assume we may kill the whole header */
6280
Willy Tarreau58f10d72006-12-04 02:26:12 +01006281 /* Now look for cookies. Conforming to RFC2109, we have to support
6282 * attributes whose name begin with a '$', and associate them with
6283 * the right cookie, if we want to delete this cookie.
6284 * So there are 3 cases for each cookie read :
6285 * 1) it's a special attribute, beginning with a '$' : ignore it.
6286 * 2) it's a server id cookie that we *MAY* want to delete : save
6287 * some pointers on it (last semi-colon, beginning of cookie...)
6288 * 3) it's an application cookie : we *MAY* have to delete a previous
6289 * "special" cookie.
6290 * At the end of loop, if a "special" cookie remains, we may have to
6291 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006292 * *MUST* delete it.
6293 *
6294 * Note: RFC2965 is unclear about the processing of spaces around
6295 * the equal sign in the ATTR=VALUE form. A careful inspection of
6296 * the RFC explicitly allows spaces before it, and not within the
6297 * tokens (attrs or values). An inspection of RFC2109 allows that
6298 * too but section 10.1.3 lets one think that spaces may be allowed
6299 * after the equal sign too, resulting in some (rare) buggy
6300 * implementations trying to do that. So let's do what servers do.
6301 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6302 * allowed quoted strings in values, with any possible character
6303 * after a backslash, including control chars and delimitors, which
6304 * causes parsing to become ambiguous. Browsers also allow spaces
6305 * within values even without quotes.
6306 *
6307 * We have to keep multiple pointers in order to support cookie
6308 * removal at the beginning, middle or end of header without
6309 * corrupting the header. All of these headers are valid :
6310 *
6311 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6312 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6313 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6314 * | | | | | | | | |
6315 * | | | | | | | | hdr_end <--+
6316 * | | | | | | | +--> next
6317 * | | | | | | +----> val_end
6318 * | | | | | +-----------> val_beg
6319 * | | | | +--------------> equal
6320 * | | | +----------------> att_end
6321 * | | +---------------------> att_beg
6322 * | +--------------------------> prev
6323 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006324 */
6325
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006326 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6327 /* Iterate through all cookies on this line */
6328
6329 /* find att_beg */
6330 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006331 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006332 att_beg++;
6333
6334 /* find att_end : this is the first character after the last non
6335 * space before the equal. It may be equal to hdr_end.
6336 */
6337 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006338
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006339 while (equal < hdr_end) {
6340 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006341 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006342 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006343 continue;
6344 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006345 }
6346
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006347 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6348 * is between <att_beg> and <equal>, both may be identical.
6349 */
6350
6351 /* look for end of cookie if there is an equal sign */
6352 if (equal < hdr_end && *equal == '=') {
6353 /* look for the beginning of the value */
6354 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006355 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006356 val_beg++;
6357
6358 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006359 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006360
6361 /* make val_end point to the first white space or delimitor after the value */
6362 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006363 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006364 val_end--;
6365 } else {
6366 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006367 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006368
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006369 /* We have nothing to do with attributes beginning with '$'. However,
6370 * they will automatically be removed if a header before them is removed,
6371 * since they're supposed to be linked together.
6372 */
6373 if (*att_beg == '$')
6374 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006375
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006376 /* Ignore cookies with no equal sign */
6377 if (equal == next) {
6378 /* This is not our cookie, so we must preserve it. But if we already
6379 * scheduled another cookie for removal, we cannot remove the
6380 * complete header, but we can remove the previous block itself.
6381 */
6382 preserve_hdr = 1;
6383 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006384 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006385 val_end += delta;
6386 next += delta;
6387 hdr_end += delta;
6388 hdr_next += delta;
6389 cur_hdr->len += delta;
6390 http_msg_move_end(&txn->req, delta);
6391 prev = del_from;
6392 del_from = NULL;
6393 }
6394 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006395 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006396
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006397 /* if there are spaces around the equal sign, we need to
6398 * strip them otherwise we'll get trouble for cookie captures,
6399 * or even for rewrites. Since this happens extremely rarely,
6400 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006401 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006402 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6403 int stripped_before = 0;
6404 int stripped_after = 0;
6405
6406 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006407 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006408 equal += stripped_before;
6409 val_beg += stripped_before;
6410 }
6411
6412 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006413 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006414 val_beg += stripped_after;
6415 stripped_before += stripped_after;
6416 }
6417
6418 val_end += stripped_before;
6419 next += stripped_before;
6420 hdr_end += stripped_before;
6421 hdr_next += stripped_before;
6422 cur_hdr->len += stripped_before;
6423 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006424 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006425 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006426
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006427 /* First, let's see if we want to capture this cookie. We check
6428 * that we don't already have a client side cookie, because we
6429 * can only capture one. Also as an optimisation, we ignore
6430 * cookies shorter than the declared name.
6431 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006432 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6433 (val_end - att_beg >= sess->fe->capture_namelen) &&
6434 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006435 int log_len = val_end - att_beg;
6436
Willy Tarreaubafbe012017-11-24 17:34:44 +01006437 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006438 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006439 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006440 if (log_len > sess->fe->capture_len)
6441 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006442 memcpy(txn->cli_cookie, att_beg, log_len);
6443 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006444 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006445 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006446
Willy Tarreaubca99692010-10-06 19:25:55 +02006447 /* Persistence cookies in passive, rewrite or insert mode have the
6448 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006449 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006450 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006451 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006452 * For cookies in prefix mode, the form is :
6453 *
6454 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006455 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006456 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6457 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6458 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006459 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006460
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006461 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6462 * have the server ID between val_beg and delim, and the original cookie between
6463 * delim+1 and val_end. Otherwise, delim==val_end :
6464 *
6465 * Cookie: NAME=SRV; # in all but prefix modes
6466 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6467 * | || || | |+-> next
6468 * | || || | +--> val_end
6469 * | || || +---------> delim
6470 * | || |+------------> val_beg
6471 * | || +-------------> att_end = equal
6472 * | |+-----------------> att_beg
6473 * | +------------------> prev
6474 * +-------------------------> hdr_beg
6475 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006476
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006477 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006478 for (delim = val_beg; delim < val_end; delim++)
6479 if (*delim == COOKIE_DELIM)
6480 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006481 } else {
6482 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006483 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006484 /* Now check if the cookie contains a date field, which would
6485 * appear after a vertical bar ('|') just after the server name
6486 * and before the delimiter.
6487 */
6488 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6489 if (vbar1) {
6490 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006491 * right is the last seen date. It is a base64 encoded
6492 * 30-bit value representing the UNIX date since the
6493 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006494 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006495 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006496 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006497 if (val_end - vbar1 >= 5) {
6498 val = b64tos30(vbar1);
6499 if (val > 0)
6500 txn->cookie_last_date = val << 2;
6501 }
6502 /* look for a second vertical bar */
6503 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6504 if (vbar1 && (val_end - vbar1 > 5)) {
6505 val = b64tos30(vbar1 + 1);
6506 if (val > 0)
6507 txn->cookie_first_date = val << 2;
6508 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006509 }
6510 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006511
Willy Tarreauf64d1412010-10-07 20:06:11 +02006512 /* if the cookie has an expiration date and the proxy wants to check
6513 * it, then we do that now. We first check if the cookie is too old,
6514 * then only if it has expired. We detect strict overflow because the
6515 * time resolution here is not great (4 seconds). Cookies with dates
6516 * in the future are ignored if their offset is beyond one day. This
6517 * allows an admin to fix timezone issues without expiring everyone
6518 * and at the same time avoids keeping unwanted side effects for too
6519 * long.
6520 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006521 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6522 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006523 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006524 txn->flags &= ~TX_CK_MASK;
6525 txn->flags |= TX_CK_OLD;
6526 delim = val_beg; // let's pretend we have not found the cookie
6527 txn->cookie_first_date = 0;
6528 txn->cookie_last_date = 0;
6529 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006530 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6531 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006532 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006533 txn->flags &= ~TX_CK_MASK;
6534 txn->flags |= TX_CK_EXPIRED;
6535 delim = val_beg; // let's pretend we have not found the cookie
6536 txn->cookie_first_date = 0;
6537 txn->cookie_last_date = 0;
6538 }
6539
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006540 /* Here, we'll look for the first running server which supports the cookie.
6541 * This allows to share a same cookie between several servers, for example
6542 * to dedicate backup servers to specific servers only.
6543 * However, to prevent clients from sticking to cookie-less backup server
6544 * when they have incidentely learned an empty cookie, we simply ignore
6545 * empty cookies and mark them as invalid.
6546 * The same behaviour is applied when persistence must be ignored.
6547 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006548 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006549 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006550
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006551 while (srv) {
6552 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6553 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006554 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006555 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006556 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006557 /* we found the server and we can use it */
6558 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006559 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006560 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006561 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006562 break;
6563 } else {
6564 /* we found a server, but it's down,
6565 * mark it as such and go on in case
6566 * another one is available.
6567 */
6568 txn->flags &= ~TX_CK_MASK;
6569 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006570 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006571 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006572 srv = srv->next;
6573 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006574
Willy Tarreauf64d1412010-10-07 20:06:11 +02006575 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006576 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006577 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006578 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006579 txn->flags |= TX_CK_UNUSED;
6580 else
6581 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006582 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006583
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006584 /* depending on the cookie mode, we may have to either :
6585 * - delete the complete cookie if we're in insert+indirect mode, so that
6586 * the server never sees it ;
6587 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlant5ba80252018-11-15 09:25:36 -08006588 * application cookie so that it does not get accidently removed later,
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006589 * if we're in cookie prefix mode
6590 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006591 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006592 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006593
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006594 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006595 val_end += delta;
6596 next += delta;
6597 hdr_end += delta;
6598 hdr_next += delta;
6599 cur_hdr->len += delta;
6600 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006601
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006602 del_from = NULL;
6603 preserve_hdr = 1; /* we want to keep this cookie */
6604 }
6605 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006606 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006607 del_from = prev;
6608 }
6609 } else {
6610 /* This is not our cookie, so we must preserve it. But if we already
6611 * scheduled another cookie for removal, we cannot remove the
6612 * complete header, but we can remove the previous block itself.
6613 */
6614 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006615
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006616 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006617 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006618 if (att_beg >= del_from)
6619 att_beg += delta;
6620 if (att_end >= del_from)
6621 att_end += delta;
6622 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006623 val_end += delta;
6624 next += delta;
6625 hdr_end += delta;
6626 hdr_next += delta;
6627 cur_hdr->len += delta;
6628 http_msg_move_end(&txn->req, delta);
6629 prev = del_from;
6630 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006631 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006632 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006633
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006634 /* continue with next cookie on this header line */
6635 att_beg = next;
6636 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006637
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006638 /* There are no more cookies on this line.
6639 * We may still have one (or several) marked for deletion at the
6640 * end of the line. We must do this now in two ways :
6641 * - if some cookies must be preserved, we only delete from the
6642 * mark to the end of line ;
6643 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006644 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006645 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006646 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006647 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006648 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006649 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006650 cur_hdr->len += delta;
6651 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006652 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006653
6654 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006655 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6656 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006657 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006658 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006659 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006660 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006661 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006662 }
6663
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006664 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006665 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006666 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006667}
6668
6669
Willy Tarreaua15645d2007-03-18 16:22:39 +01006670/* Iterate the same filter through all response headers contained in <rtr>.
6671 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6672 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006673int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006674{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006675 char *cur_ptr, *cur_end, *cur_next;
6676 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006677 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006678 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006679 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006680
6681 last_hdr = 0;
6682
Willy Tarreauf37954d2018-06-15 18:31:02 +02006683 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006684 old_idx = 0;
6685
6686 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006687 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006688 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006689 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006690 (exp->action == ACT_ALLOW ||
6691 exp->action == ACT_DENY))
6692 return 0;
6693
6694 cur_idx = txn->hdr_idx.v[old_idx].next;
6695 if (!cur_idx)
6696 break;
6697
6698 cur_hdr = &txn->hdr_idx.v[cur_idx];
6699 cur_ptr = cur_next;
6700 cur_end = cur_ptr + cur_hdr->len;
6701 cur_next = cur_end + cur_hdr->cr + 1;
6702
6703 /* Now we have one header between cur_ptr and cur_end,
6704 * and the next header starts at cur_next.
6705 */
6706
Willy Tarreau15a53a42015-01-21 13:39:42 +01006707 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708 switch (exp->action) {
6709 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006710 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006711 last_hdr = 1;
6712 break;
6713
6714 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006715 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006716 last_hdr = 1;
6717 break;
6718
6719 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006720 len = exp_replace(trash.area,
6721 trash.size, cur_ptr,
6722 exp->replace, pmatch);
6723 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006724 return -1;
6725
Willy Tarreau6e27be12018-08-22 04:46:47 +02006726 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6727
Willy Tarreaua15645d2007-03-18 16:22:39 +01006728 /* FIXME: if the user adds a newline in the replacement, the
6729 * index will not be recalculated for now, and the new line
6730 * will not be counted as a new header.
6731 */
6732
6733 cur_end += delta;
6734 cur_next += delta;
6735 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006736 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737 break;
6738
6739 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006740 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006741 cur_next += delta;
6742
Willy Tarreaufa355d42009-11-29 18:12:29 +01006743 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006744 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6745 txn->hdr_idx.used--;
6746 cur_hdr->len = 0;
6747 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006748 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006749 break;
6750
6751 }
6752 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006753
6754 /* keep the link from this header to next one in case of later
6755 * removal of next header.
6756 */
6757 old_idx = cur_idx;
6758 }
6759 return 0;
6760}
6761
6762
6763/* Apply the filter to the status line in the response buffer <rtr>.
6764 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6765 * or -1 if a replacement resulted in an invalid status line.
6766 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006767int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006768{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006769 char *cur_ptr, *cur_end;
6770 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006771 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006772 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006773
Willy Tarreau3d300592007-03-18 18:34:41 +01006774 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006775 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006776 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006777 (exp->action == ACT_ALLOW ||
6778 exp->action == ACT_DENY))
6779 return 0;
6780 else if (exp->action == ACT_REMOVE)
6781 return 0;
6782
6783 done = 0;
6784
Willy Tarreauf37954d2018-06-15 18:31:02 +02006785 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006786 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006787
6788 /* Now we have the status line between cur_ptr and cur_end */
6789
Willy Tarreau15a53a42015-01-21 13:39:42 +01006790 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006791 switch (exp->action) {
6792 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006793 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006794 done = 1;
6795 break;
6796
6797 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006798 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006799 done = 1;
6800 break;
6801
6802 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006803 len = exp_replace(trash.area, trash.size,
6804 cur_ptr, exp->replace, pmatch);
6805 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006806 return -1;
6807
Willy Tarreau6e27be12018-08-22 04:46:47 +02006808 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6809
Willy Tarreaua15645d2007-03-18 16:22:39 +01006810 /* FIXME: if the user adds a newline in the replacement, the
6811 * index will not be recalculated for now, and the new line
6812 * will not be counted as a new header.
6813 */
6814
Willy Tarreaufa355d42009-11-29 18:12:29 +01006815 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006816 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006817 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006818 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006819 cur_ptr, cur_end + 1,
6820 NULL, NULL);
6821 if (unlikely(!cur_end))
6822 return -1;
6823
6824 /* we have a full respnse and we know that we have either a CR
6825 * or an LF at <ptr>.
6826 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006827 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006828 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006829 /* there is no point trying this regex on headers */
6830 return 1;
6831 }
6832 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833 return done;
6834}
6835
6836
6837
6838/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006839 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006840 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6841 * unparsable response.
6842 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006843int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006844{
Willy Tarreau192252e2015-04-04 01:47:55 +02006845 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006846 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006847 struct hdr_exp *exp;
6848
6849 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006850 int ret;
6851
6852 /*
6853 * The interleaving of transformations and verdicts
6854 * makes it difficult to decide to continue or stop
6855 * the evaluation.
6856 */
6857
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006858 if (txn->flags & TX_SVDENY)
6859 break;
6860
Willy Tarreau3d300592007-03-18 18:34:41 +01006861 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006862 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6863 exp->action == ACT_PASS)) {
6864 exp = exp->next;
6865 continue;
6866 }
6867
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006868 /* if this filter had a condition, evaluate it now and skip to
6869 * next filter if the condition does not match.
6870 */
6871 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006872 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006873 ret = acl_pass(ret);
6874 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6875 ret = !ret;
6876 if (!ret)
6877 continue;
6878 }
6879
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006881 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006882 if (unlikely(ret < 0))
6883 return -1;
6884
6885 if (likely(ret == 0)) {
6886 /* The filter did not match the response, it can be
6887 * iterated through all headers.
6888 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006889 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6890 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006891 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006892 }
6893 return 0;
6894}
6895
6896
Willy Tarreaua15645d2007-03-18 16:22:39 +01006897/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006898 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006899 * desirable to call it only when needed. This function is also used when we
6900 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006901 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006902void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006903{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006904 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006905 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006906 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006907 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006908 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006909 char *hdr_beg, *hdr_end, *hdr_next;
6910 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006911
Willy Tarreaua15645d2007-03-18 16:22:39 +01006912 /* Iterate through the headers.
6913 * we start with the start line.
6914 */
6915 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006916 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006917
6918 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6919 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006920 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006921
6922 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006923 hdr_beg = hdr_next;
6924 hdr_end = hdr_beg + cur_hdr->len;
6925 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006926
Willy Tarreau24581ba2010-08-31 22:39:35 +02006927 /* We have one full header between hdr_beg and hdr_end, and the
6928 * next header starts at hdr_next. We're only interested in
6929 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006930 */
6931
Willy Tarreau24581ba2010-08-31 22:39:35 +02006932 is_cookie2 = 0;
6933 prev = hdr_beg + 10;
6934 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006935 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006936 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6937 if (!val) {
6938 old_idx = cur_idx;
6939 continue;
6940 }
6941 is_cookie2 = 1;
6942 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006943 }
6944
Willy Tarreau24581ba2010-08-31 22:39:35 +02006945 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6946 * <prev> points to the colon.
6947 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006948 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006949
Willy Tarreau24581ba2010-08-31 22:39:35 +02006950 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6951 * check-cache is enabled) and we are not interested in checking
6952 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006953 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006954 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006955 return;
6956
Willy Tarreau24581ba2010-08-31 22:39:35 +02006957 /* OK so now we know we have to process this response cookie.
6958 * The format of the Set-Cookie header is slightly different
6959 * from the format of the Cookie header in that it does not
6960 * support the comma as a cookie delimiter (thus the header
6961 * cannot be folded) because the Expires attribute described in
6962 * the original Netscape's spec may contain an unquoted date
6963 * with a comma inside. We have to live with this because
6964 * many browsers don't support Max-Age and some browsers don't
6965 * support quoted strings. However the Set-Cookie2 header is
6966 * clean.
6967 *
6968 * We have to keep multiple pointers in order to support cookie
6969 * removal at the beginning, middle or end of header without
6970 * corrupting the header (in case of set-cookie2). A special
6971 * pointer, <scav> points to the beginning of the set-cookie-av
6972 * fields after the first semi-colon. The <next> pointer points
6973 * either to the end of line (set-cookie) or next unquoted comma
6974 * (set-cookie2). All of these headers are valid :
6975 *
6976 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6977 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6978 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6979 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6980 * | | | | | | | | | |
6981 * | | | | | | | | +-> next hdr_end <--+
6982 * | | | | | | | +------------> scav
6983 * | | | | | | +--------------> val_end
6984 * | | | | | +--------------------> val_beg
6985 * | | | | +----------------------> equal
6986 * | | | +------------------------> att_end
6987 * | | +----------------------------> att_beg
6988 * | +------------------------------> prev
6989 * +-----------------------------------------> hdr_beg
6990 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006991
Willy Tarreau24581ba2010-08-31 22:39:35 +02006992 for (; prev < hdr_end; prev = next) {
6993 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994
Willy Tarreau24581ba2010-08-31 22:39:35 +02006995 /* find att_beg */
6996 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006997 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006998 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006999
Willy Tarreau24581ba2010-08-31 22:39:35 +02007000 /* find att_end : this is the first character after the last non
7001 * space before the equal. It may be equal to hdr_end.
7002 */
7003 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007004
Willy Tarreau24581ba2010-08-31 22:39:35 +02007005 while (equal < hdr_end) {
7006 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7007 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007008 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007009 continue;
7010 att_end = equal;
7011 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007012
Willy Tarreau24581ba2010-08-31 22:39:35 +02007013 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7014 * is between <att_beg> and <equal>, both may be identical.
7015 */
7016
7017 /* look for end of cookie if there is an equal sign */
7018 if (equal < hdr_end && *equal == '=') {
7019 /* look for the beginning of the value */
7020 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007021 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007022 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007023
Willy Tarreau24581ba2010-08-31 22:39:35 +02007024 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02007025 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007026
7027 /* make val_end point to the first white space or delimitor after the value */
7028 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007029 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007030 val_end--;
7031 } else {
7032 /* <equal> points to next comma, semi-colon or EOL */
7033 val_beg = val_end = next = equal;
7034 }
7035
7036 if (next < hdr_end) {
7037 /* Set-Cookie2 supports multiple cookies, and <next> points to
7038 * a colon or semi-colon before the end. So skip all attr-value
7039 * pairs and look for the next comma. For Set-Cookie, since
7040 * commas are permitted in values, skip to the end.
7041 */
7042 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02007043 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007044 else
7045 next = hdr_end;
7046 }
7047
7048 /* Now everything is as on the diagram above */
7049
7050 /* Ignore cookies with no equal sign */
7051 if (equal == val_end)
7052 continue;
7053
7054 /* If there are spaces around the equal sign, we need to
7055 * strip them otherwise we'll get trouble for cookie captures,
7056 * or even for rewrites. Since this happens extremely rarely,
7057 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007058 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007059 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7060 int stripped_before = 0;
7061 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007062
Willy Tarreau24581ba2010-08-31 22:39:35 +02007063 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007064 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007065 equal += stripped_before;
7066 val_beg += stripped_before;
7067 }
7068
7069 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007070 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007071 val_beg += stripped_after;
7072 stripped_before += stripped_after;
7073 }
7074
7075 val_end += stripped_before;
7076 next += stripped_before;
7077 hdr_end += stripped_before;
7078 hdr_next += stripped_before;
7079 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007080 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007081 }
7082
7083 /* First, let's see if we want to capture this cookie. We check
7084 * that we don't already have a server side cookie, because we
7085 * can only capture one. Also as an optimisation, we ignore
7086 * cookies shorter than the declared name.
7087 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007088 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007089 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007090 (val_end - att_beg >= sess->fe->capture_namelen) &&
7091 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007092 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007093 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007094 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007095 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007096 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007097 if (log_len > sess->fe->capture_len)
7098 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007099 memcpy(txn->srv_cookie, att_beg, log_len);
7100 txn->srv_cookie[log_len] = 0;
7101 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007102 }
7103
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007104 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007105 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007106 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007107 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7108 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007109 /* assume passive cookie by default */
7110 txn->flags &= ~TX_SCK_MASK;
7111 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007112
7113 /* If the cookie is in insert mode on a known server, we'll delete
7114 * this occurrence because we'll insert another one later.
7115 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007116 * a direct access.
7117 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007118 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007119 /* The "preserve" flag was set, we don't want to touch the
7120 * server's cookie.
7121 */
7122 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007123 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007124 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007125 /* this cookie must be deleted */
7126 if (*prev == ':' && next == hdr_end) {
7127 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007128 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007129 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7130 txn->hdr_idx.used--;
7131 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007132 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007133 hdr_next += delta;
7134 http_msg_move_end(&txn->rsp, delta);
7135 /* note: while both invalid now, <next> and <hdr_end>
7136 * are still equal, so the for() will stop as expected.
7137 */
7138 } else {
7139 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007140 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007141 next = prev;
7142 hdr_end += delta;
7143 hdr_next += delta;
7144 cur_hdr->len += delta;
7145 http_msg_move_end(&txn->rsp, delta);
7146 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007147 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007148 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007149 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007150 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007151 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007152 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007153 * with this server since we know it.
7154 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007155 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007156 next += delta;
7157 hdr_end += delta;
7158 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007159 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007160 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007161
Willy Tarreauf1348312010-10-07 15:54:11 +02007162 txn->flags &= ~TX_SCK_MASK;
7163 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007165 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007166 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007167 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007168 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007169 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007170 next += delta;
7171 hdr_end += delta;
7172 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007173 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007174 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007175
Willy Tarreau827aee92011-03-10 16:55:02 +01007176 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007177 txn->flags &= ~TX_SCK_MASK;
7178 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007179 }
7180 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007181 /* that's done for this cookie, check the next one on the same
7182 * line when next != hdr_end (only if is_cookie2).
7183 */
7184 }
7185 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007186 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007187 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007188}
7189
7190
Willy Tarreaua15645d2007-03-18 16:22:39 +01007191/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007192 * Parses the Cache-Control and Pragma request header fields to determine if
7193 * the request may be served from the cache and/or if it is cacheable. Updates
7194 * s->txn->flags.
7195 */
7196void check_request_for_cacheability(struct stream *s, struct channel *chn)
7197{
7198 struct http_txn *txn = s->txn;
7199 char *p1, *p2;
7200 char *cur_ptr, *cur_end, *cur_next;
7201 int pragma_found;
7202 int cc_found;
7203 int cur_idx;
7204
Christopher Faulet25a02f62018-10-24 12:00:25 +02007205 if (IS_HTX_STRM(s))
7206 return htx_check_request_for_cacheability(s, chn);
7207
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007208 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7209 return; /* nothing more to do here */
7210
7211 cur_idx = 0;
7212 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007213 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007214
7215 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7216 struct hdr_idx_elem *cur_hdr;
7217 int val;
7218
7219 cur_hdr = &txn->hdr_idx.v[cur_idx];
7220 cur_ptr = cur_next;
7221 cur_end = cur_ptr + cur_hdr->len;
7222 cur_next = cur_end + cur_hdr->cr + 1;
7223
7224 /* We have one full header between cur_ptr and cur_end, and the
7225 * next header starts at cur_next.
7226 */
7227
7228 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7229 if (val) {
7230 if ((cur_end - (cur_ptr + val) >= 8) &&
7231 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7232 pragma_found = 1;
7233 continue;
7234 }
7235 }
7236
William Lallemand8a16fe02018-05-22 11:04:33 +02007237 /* Don't use the cache and don't try to store if we found the
7238 * Authorization header */
7239 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7240 if (val) {
7241 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7242 txn->flags |= TX_CACHE_IGNORE;
7243 continue;
7244 }
7245
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007246 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7247 if (!val)
7248 continue;
7249
7250 /* OK, right now we know we have a cache-control header at cur_ptr */
7251 cc_found = 1;
7252 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7253
7254 if (p1 >= cur_end) /* no more info */
7255 continue;
7256
7257 /* p1 is at the beginning of the value */
7258 p2 = p1;
7259 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7260 p2++;
7261
7262 /* we have a complete value between p1 and p2. We don't check the
7263 * values after max-age, max-stale nor min-fresh, we simply don't
7264 * use the cache when they're specified.
7265 */
7266 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7267 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7268 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7269 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7270 txn->flags |= TX_CACHE_IGNORE;
7271 continue;
7272 }
7273
7274 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7275 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7276 continue;
7277 }
7278 }
7279
7280 /* RFC7234#5.4:
7281 * When the Cache-Control header field is also present and
7282 * understood in a request, Pragma is ignored.
7283 * When the Cache-Control header field is not present in a
7284 * request, caches MUST consider the no-cache request
7285 * pragma-directive as having the same effect as if
7286 * "Cache-Control: no-cache" were present.
7287 */
7288 if (!cc_found && pragma_found)
7289 txn->flags |= TX_CACHE_IGNORE;
7290}
7291
7292/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007293 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007295void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007296{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007297 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007298 char *p1, *p2;
7299
7300 char *cur_ptr, *cur_end, *cur_next;
7301 int cur_idx;
7302
Christopher Faulet25a02f62018-10-24 12:00:25 +02007303
7304 if (IS_HTX_STRM(s))
7305 return htx_check_response_for_cacheability(s, rtr);
7306
Willy Tarreau12b32f22017-12-21 16:08:09 +01007307 if (txn->status < 200) {
7308 /* do not try to cache interim responses! */
7309 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007310 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007311 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312
7313 /* Iterate through the headers.
7314 * we start with the start line.
7315 */
7316 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007317 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007318
7319 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7320 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007321 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007322
7323 cur_hdr = &txn->hdr_idx.v[cur_idx];
7324 cur_ptr = cur_next;
7325 cur_end = cur_ptr + cur_hdr->len;
7326 cur_next = cur_end + cur_hdr->cr + 1;
7327
7328 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007329 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007330 */
7331
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007332 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7333 if (val) {
7334 if ((cur_end - (cur_ptr + val) >= 8) &&
7335 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7336 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7337 return;
7338 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339 }
7340
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007341 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7342 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007343 continue;
7344
7345 /* OK, right now we know we have a cache-control header at cur_ptr */
7346
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007347 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007348
7349 if (p1 >= cur_end) /* no more info */
7350 continue;
7351
7352 /* p1 is at the beginning of the value */
7353 p2 = p1;
7354
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007355 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007356 p2++;
7357
7358 /* we have a complete value between p1 and p2 */
7359 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007360 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7361 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7362 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7363 continue;
7364 }
7365
Willy Tarreaua15645d2007-03-18 16:22:39 +01007366 /* we have something of the form no-cache="set-cookie" */
7367 if ((cur_end - p1 >= 21) &&
7368 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7369 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007370 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007371 continue;
7372 }
7373
7374 /* OK, so we know that either p2 points to the end of string or to a comma */
7375 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007376 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007377 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007378 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007379 return;
7380 }
7381
7382 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007383 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007384 continue;
7385 }
7386 }
7387}
7388
Willy Tarreau58f10d72006-12-04 02:26:12 +01007389
Willy Tarreaub2513902006-12-17 14:52:38 +01007390/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007391 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007392 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007393 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007394 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007395 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007396 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007397 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007398 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007399int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007400{
7401 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007402 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007403 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007404
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007405 if (!uri_auth)
7406 return 0;
7407
Cyril Bonté70be45d2010-10-12 00:14:35 +02007408 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007409 return 0;
7410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007411 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007412 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007413 return 0;
7414
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007415 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007416 return 0;
7417
Willy Tarreaub2513902006-12-17 14:52:38 +01007418 return 1;
7419}
7420
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007421/* Append the description of what is present in error snapshot <es> into <out>.
7422 * The description must be small enough to always fit in a trash. The output
7423 * buffer may be the trash so the trash must not be used inside this function.
7424 */
7425void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7426{
Christopher Fauleted26fb82018-11-29 22:53:30 +01007427 chunk_appendf(out,
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007428 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7429 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7430 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7431 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7432 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7433 es->ctx.http.m_flags, es->ctx.http.m_clen,
7434 es->ctx.http.m_blen, es->ctx.http.b_flags);
7435}
7436
Willy Tarreau4076a152009-04-02 15:18:36 +02007437/*
7438 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007439 * By default it tries to report the error position as msg->err_pos. However if
7440 * this one is not set, it will then report msg->next, which is the last known
7441 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007442 * displays buffers as a contiguous area starting at buf->p. The direction is
7443 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007444 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007445void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007446 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007447 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007448{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007449 union error_snapshot_ctx ctx;
7450 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007451
Willy Tarreauef3ca732018-09-07 15:47:35 +02007452 /* http-specific part now */
7453 ctx.http.sid = s->uniq_id;
7454 ctx.http.state = state;
7455 ctx.http.b_flags = msg->chn->flags;
7456 ctx.http.s_flags = s->flags;
7457 ctx.http.t_flags = s->txn->flags;
7458 ctx.http.m_flags = msg->flags;
7459 ctx.http.m_clen = msg->chunk_len;
7460 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007461
Willy Tarreauef3ca732018-09-07 15:47:35 +02007462 ofs = msg->chn->total - ci_data(msg->chn);
7463 if (ofs < 0)
7464 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007465
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007466 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007467 other_end, s->target,
7468 strm_sess(s), &msg->chn->buf,
7469 ofs, co_data(msg->chn),
7470 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7471 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007472}
Willy Tarreaub2513902006-12-17 14:52:38 +01007473
Willy Tarreau294c4732011-12-16 21:35:50 +01007474/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7475 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7476 * performed over the whole headers. Otherwise it must contain a valid header
7477 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7478 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7479 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7480 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007481 * -1. The value fetch stops at commas, so this function is suited for use with
7482 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007483 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007484 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007485unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007486 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007487 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007488{
Willy Tarreau294c4732011-12-16 21:35:50 +01007489 struct hdr_ctx local_ctx;
7490 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007491 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007492 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007493 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007494
Willy Tarreau294c4732011-12-16 21:35:50 +01007495 if (!ctx) {
7496 local_ctx.idx = 0;
7497 ctx = &local_ctx;
7498 }
7499
Willy Tarreaubce70882009-09-07 11:51:47 +02007500 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007501 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007502 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007503 occ--;
7504 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007505 *vptr = ctx->line + ctx->val;
7506 *vlen = ctx->vlen;
7507 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007508 }
7509 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007510 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007511 }
7512
7513 /* negative occurrence, we scan all the list then walk back */
7514 if (-occ > MAX_HDR_HISTORY)
7515 return 0;
7516
Willy Tarreau294c4732011-12-16 21:35:50 +01007517 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007518 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007519 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7520 len_hist[hist_ptr] = ctx->vlen;
7521 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007522 hist_ptr = 0;
7523 found++;
7524 }
7525 if (-occ > found)
7526 return 0;
7527 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007528 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7529 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7530 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007531 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007532 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007533 if (hist_ptr >= MAX_HDR_HISTORY)
7534 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007535 *vptr = ptr_hist[hist_ptr];
7536 *vlen = len_hist[hist_ptr];
7537 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007538}
7539
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007540/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7541 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7542 * performed over the whole headers. Otherwise it must contain a valid header
7543 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7544 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7545 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7546 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7547 * -1. This function differs from http_get_hdr() in that it only returns full
7548 * line header values and does not stop at commas.
7549 * The return value is 0 if nothing was found, or non-zero otherwise.
7550 */
7551unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7552 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007553 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007554{
7555 struct hdr_ctx local_ctx;
7556 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007557 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007558 unsigned int hist_ptr;
7559 int found;
7560
7561 if (!ctx) {
7562 local_ctx.idx = 0;
7563 ctx = &local_ctx;
7564 }
7565
7566 if (occ >= 0) {
7567 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007568 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007569 occ--;
7570 if (occ <= 0) {
7571 *vptr = ctx->line + ctx->val;
7572 *vlen = ctx->vlen;
7573 return 1;
7574 }
7575 }
7576 return 0;
7577 }
7578
7579 /* negative occurrence, we scan all the list then walk back */
7580 if (-occ > MAX_HDR_HISTORY)
7581 return 0;
7582
7583 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007584 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007585 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7586 len_hist[hist_ptr] = ctx->vlen;
7587 if (++hist_ptr >= MAX_HDR_HISTORY)
7588 hist_ptr = 0;
7589 found++;
7590 }
7591 if (-occ > found)
7592 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007593
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007594 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007595 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7596 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7597 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007598 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007599 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007600 if (hist_ptr >= MAX_HDR_HISTORY)
7601 hist_ptr -= MAX_HDR_HISTORY;
7602 *vptr = ptr_hist[hist_ptr];
7603 *vlen = len_hist[hist_ptr];
7604 return 1;
7605}
7606
Willy Tarreaubaaee002006-06-26 02:48:02 +02007607/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007608 * Print a debug line with a header. Always stop at the first CR or LF char,
7609 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7610 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007611 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007612void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007613{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007614 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007615 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007616
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007617 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007618 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007619 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007620 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 +02007621
7622 for (max = 0; start + max < end; max++)
7623 if (start[max] == '\r' || start[max] == '\n')
7624 break;
7625
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007626 UBOUND(max, trash.size - trash.data - 3);
7627 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7628 trash.area[trash.data++] = '\n';
7629 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007630}
7631
Willy Tarreaueee5b512015-04-03 23:46:31 +02007632
7633/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7634 * The hdr_idx is allocated as well. In case of allocation failure, everything
7635 * allocated is freed and NULL is returned. Otherwise the new transaction is
7636 * assigned to the stream and returned.
7637 */
7638struct http_txn *http_alloc_txn(struct stream *s)
7639{
7640 struct http_txn *txn = s->txn;
7641
7642 if (txn)
7643 return txn;
7644
Willy Tarreaubafbe012017-11-24 17:34:44 +01007645 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007646 if (!txn)
7647 return txn;
7648
7649 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007650 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007651 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007652 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007653 return NULL;
7654 }
7655
7656 s->txn = txn;
7657 return txn;
7658}
7659
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007660void http_txn_reset_req(struct http_txn *txn)
7661{
7662 txn->req.flags = 0;
7663 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7664 txn->req.next = 0;
7665 txn->req.chunk_len = 0LL;
7666 txn->req.body_len = 0LL;
7667 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7668}
7669
7670void http_txn_reset_res(struct http_txn *txn)
7671{
7672 txn->rsp.flags = 0;
7673 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7674 txn->rsp.next = 0;
7675 txn->rsp.chunk_len = 0LL;
7676 txn->rsp.body_len = 0LL;
7677 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7678}
7679
Willy Tarreau0937bc42009-12-22 15:03:09 +01007680/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007681 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007682 * the required fields are properly allocated and that we only need to (re)init
7683 * them. This should be used before processing any new request.
7684 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007685void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007686{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007687 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007688 struct proxy *fe = strm_fe(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02007689 struct conn_stream *cs = objt_cs(s->si[0].end);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007690
Christopher Fauletf2824e62018-10-01 12:12:37 +02007691 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
7692 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
7693 : 0);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007694 txn->status = -1;
7695
Willy Tarreauf64d1412010-10-07 20:06:11 +02007696 txn->cookie_first_date = 0;
7697 txn->cookie_last_date = 0;
7698
Willy Tarreaueee5b512015-04-03 23:46:31 +02007699 txn->srv_cookie = NULL;
7700 txn->cli_cookie = NULL;
7701 txn->uri = NULL;
7702
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007703 http_txn_reset_req(txn);
7704 http_txn_reset_res(txn);
7705
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007706 txn->req.chn = &s->req;
7707 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007708
7709 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007710
7711 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7712 if (fe->options2 & PR_O2_REQBUG_OK)
7713 txn->req.err_pos = -1; /* let buggy requests pass */
7714
Willy Tarreau0937bc42009-12-22 15:03:09 +01007715 if (txn->hdr_idx.v)
7716 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007717
7718 vars_init(&s->vars_txn, SCOPE_TXN);
7719 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007720}
7721
7722/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007723void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007724{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007725 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007726 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007727
7728 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007729 pool_free(pool_head_requri, txn->uri);
7730 pool_free(pool_head_capture, txn->cli_cookie);
7731 pool_free(pool_head_capture, txn->srv_cookie);
7732 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007733
William Lallemanda73203e2012-03-12 12:48:57 +01007734 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007735 txn->uri = NULL;
7736 txn->srv_cookie = NULL;
7737 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007738
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007739 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007740 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007741 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007742 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007743 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007744 }
7745
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007746 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007747 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007748 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007749 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007750 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007751 }
7752
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007753 if (!LIST_ISEMPTY(&s->vars_txn.head))
7754 vars_prune(&s->vars_txn, s->sess, s);
7755 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7756 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007757}
7758
7759/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007760void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007761{
7762 http_end_txn(s);
7763 http_init_txn(s);
7764
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007765 /* reinitialise the current rule list pointer to NULL. We are sure that
7766 * any rulelist match the NULL pointer.
7767 */
7768 s->current_rule_list = NULL;
7769
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007770 s->be = strm_fe(s);
7771 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007772 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007773 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007774 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007775 /* re-init store persistence */
7776 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007777 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007778
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007779 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007780
Willy Tarreau739cfba2010-01-25 23:11:14 +01007781 /* We must trim any excess data from the response buffer, because we
7782 * may have blocked an invalid response from a server that we don't
Joseph Herlant5ba80252018-11-15 09:25:36 -08007783 * want to accidently forward once we disable the analysers, nor do
Willy Tarreau739cfba2010-01-25 23:11:14 +01007784 * we want those data to come along with next response. A typical
7785 * example of such data would be from a buggy server responding to
7786 * a HEAD with some data, or sending more than the advertised
7787 * content-length.
7788 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007789 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007790 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007791
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007792 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007793 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007794
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007795 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007796 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007797
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007798 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007799 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007800
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007801 s->req.rex = TICK_ETERNITY;
7802 s->req.wex = TICK_ETERNITY;
7803 s->req.analyse_exp = TICK_ETERNITY;
7804 s->res.rex = TICK_ETERNITY;
7805 s->res.wex = TICK_ETERNITY;
7806 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007807 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007808}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007809
Willy Tarreau79e57332018-10-02 16:01:16 +02007810/* This function executes one of the set-{method,path,query,uri} actions. It
7811 * takes the string from the variable 'replace' with length 'len', then modifies
7812 * the relevant part of the request line accordingly. Then it updates various
7813 * pointers to the next elements which were moved, and the total buffer length.
7814 * It finds the action to be performed in p[2], previously filled by function
7815 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7816 * error, though this can be revisited when this code is finally exploited.
7817 *
7818 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7819 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007820 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007821 * In query string case, the mark question '?' must be set at the start of the
7822 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007823 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007824int http_replace_req_line(int action, const char *replace, int len,
7825 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007826{
Willy Tarreau79e57332018-10-02 16:01:16 +02007827 struct http_txn *txn = s->txn;
7828 char *cur_ptr, *cur_end;
7829 int offset = 0;
7830 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007831
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007832 if (IS_HTX_STRM(s))
7833 return htx_req_replace_stline(action, replace, len, px, s);
7834
Willy Tarreau79e57332018-10-02 16:01:16 +02007835 switch (action) {
7836 case 0: // method
7837 cur_ptr = ci_head(&s->req);
7838 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007839
Willy Tarreau79e57332018-10-02 16:01:16 +02007840 /* adjust req line offsets and lengths */
7841 delta = len - offset - (cur_end - cur_ptr);
7842 txn->req.sl.rq.m_l += delta;
7843 txn->req.sl.rq.u += delta;
7844 txn->req.sl.rq.v += delta;
7845 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007846
Willy Tarreau79e57332018-10-02 16:01:16 +02007847 case 1: // path
7848 cur_ptr = http_txn_get_path(txn);
7849 if (!cur_ptr)
7850 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007851
Willy Tarreau79e57332018-10-02 16:01:16 +02007852 cur_end = cur_ptr;
7853 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7854 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007855
Willy Tarreau79e57332018-10-02 16:01:16 +02007856 /* adjust req line offsets and lengths */
7857 delta = len - offset - (cur_end - cur_ptr);
7858 txn->req.sl.rq.u_l += delta;
7859 txn->req.sl.rq.v += delta;
7860 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007861
Willy Tarreau79e57332018-10-02 16:01:16 +02007862 case 2: // query
7863 offset = 1;
7864 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7865 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7866 while (cur_ptr < cur_end && *cur_ptr != '?')
7867 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007868
Willy Tarreau79e57332018-10-02 16:01:16 +02007869 /* skip the question mark or indicate that we must insert it
7870 * (but only if the format string is not empty then).
7871 */
7872 if (cur_ptr < cur_end)
7873 cur_ptr++;
7874 else if (len > 1)
7875 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007876
Willy Tarreau79e57332018-10-02 16:01:16 +02007877 /* adjust req line offsets and lengths */
7878 delta = len - offset - (cur_end - cur_ptr);
7879 txn->req.sl.rq.u_l += delta;
7880 txn->req.sl.rq.v += delta;
7881 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007882
Willy Tarreau79e57332018-10-02 16:01:16 +02007883 case 3: // uri
7884 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7885 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007886
Willy Tarreau79e57332018-10-02 16:01:16 +02007887 /* adjust req line offsets and lengths */
7888 delta = len - offset - (cur_end - cur_ptr);
7889 txn->req.sl.rq.u_l += delta;
7890 txn->req.sl.rq.v += delta;
7891 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007892
Willy Tarreau79e57332018-10-02 16:01:16 +02007893 default:
7894 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007895 }
7896
Willy Tarreau79e57332018-10-02 16:01:16 +02007897 /* commit changes and adjust end of message */
7898 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7899 txn->req.sl.rq.l += delta;
7900 txn->hdr_idx.v[0].len += delta;
7901 http_msg_move_end(&txn->req, delta);
7902 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007903}
7904
Willy Tarreau79e57332018-10-02 16:01:16 +02007905/* This function replace the HTTP status code and the associated message. The
7906 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007907 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007908void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007909{
Willy Tarreau79e57332018-10-02 16:01:16 +02007910 struct http_txn *txn = s->txn;
7911 char *cur_ptr, *cur_end;
7912 int delta;
7913 char *res;
7914 int c_l;
7915 const char *msg = reason;
7916 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007917
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007918 if (IS_HTX_STRM(s))
7919 return htx_res_set_status(status, reason, s);
7920
Willy Tarreau79e57332018-10-02 16:01:16 +02007921 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007922
Willy Tarreau79e57332018-10-02 16:01:16 +02007923 res = ultoa_o(status, trash.area, trash.size);
7924 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007925
Willy Tarreau79e57332018-10-02 16:01:16 +02007926 trash.area[c_l] = ' ';
7927 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007928
Willy Tarreau79e57332018-10-02 16:01:16 +02007929 /* Do we have a custom reason format string? */
7930 if (msg == NULL)
7931 msg = http_get_reason(status);
7932 msg_len = strlen(msg);
7933 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7934 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007935
Willy Tarreau79e57332018-10-02 16:01:16 +02007936 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7937 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007938
Willy Tarreau79e57332018-10-02 16:01:16 +02007939 /* commit changes and adjust message */
7940 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7941 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007942
Willy Tarreau79e57332018-10-02 16:01:16 +02007943 /* adjust res line offsets and lengths */
7944 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7945 txn->rsp.sl.st.c_l = c_l;
7946 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007947
Willy Tarreau79e57332018-10-02 16:01:16 +02007948 delta = trash.data - (cur_end - cur_ptr);
7949 txn->rsp.sl.st.l += delta;
7950 txn->hdr_idx.v[0].len += delta;
7951 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007952}
7953
Willy Tarreau58f10d72006-12-04 02:26:12 +01007954/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007955 * Local variables:
7956 * c-indent-level: 8
7957 * c-basic-offset: 8
7958 * End:
7959 */